@@ -1,138 +1,139 | |||
|
1 | 1 | /**************************************************************************** |
|
2 | 2 | ** |
|
3 | 3 | ** Copyright (C) 2012 Digia Plc |
|
4 | 4 | ** All rights reserved. |
|
5 | 5 | ** For any questions to Digia, please use contact form at http://qt.digia.com |
|
6 | 6 | ** |
|
7 | 7 | ** This file is part of the Qt Commercial Charts Add-on. |
|
8 | 8 | ** |
|
9 | 9 | ** $QT_BEGIN_LICENSE$ |
|
10 | 10 | ** Licensees holding valid Qt Commercial licenses may use this file in |
|
11 | 11 | ** accordance with the Qt Commercial License Agreement provided with the |
|
12 | 12 | ** Software or, alternatively, in accordance with the terms contained in |
|
13 | 13 | ** a written agreement between you and Digia. |
|
14 | 14 | ** |
|
15 | 15 | ** If you have questions regarding the use of this file, please use |
|
16 | 16 | ** contact form at http://qt.digia.com |
|
17 | 17 | ** $QT_END_LICENSE$ |
|
18 | 18 | ** |
|
19 | 19 | ****************************************************************************/ |
|
20 | 20 | |
|
21 | 21 | #include "axisanimation_p.h" |
|
22 | 22 | #include "chartaxis_p.h" |
|
23 | 23 | #include <QTimer> |
|
24 | 24 | #include <QDebug> |
|
25 | 25 | |
|
26 | 26 | Q_DECLARE_METATYPE(QVector<qreal>) |
|
27 | 27 | |
|
28 | 28 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
29 | 29 | |
|
30 | 30 | |
|
31 | 31 | AxisAnimation::AxisAnimation(ChartAxis *axis): ChartAnimation(axis), |
|
32 | 32 | m_axis(axis), |
|
33 | 33 | m_type(DefaultAnimation) |
|
34 | 34 | { |
|
35 | 35 | setDuration(ChartAnimationDuration); |
|
36 | 36 | setEasingCurve(QEasingCurve::OutQuart); |
|
37 | 37 | } |
|
38 | 38 | |
|
39 | 39 | AxisAnimation::~AxisAnimation() |
|
40 | 40 | { |
|
41 | 41 | } |
|
42 | 42 | |
|
43 | 43 | void AxisAnimation::setAnimationType(Animation type) |
|
44 | 44 | { |
|
45 | 45 | if (state() != QAbstractAnimation::Stopped) stop(); |
|
46 | 46 | m_type=type; |
|
47 | 47 | } |
|
48 | 48 | |
|
49 | 49 | void AxisAnimation::setAnimationPoint(const QPointF& point) |
|
50 | 50 | { |
|
51 | 51 | if (state() != QAbstractAnimation::Stopped) stop(); |
|
52 | 52 | m_point=point; |
|
53 | 53 | } |
|
54 | 54 | |
|
55 | 55 | void AxisAnimation::setValues(QVector<qreal> &oldLayout, QVector<qreal> &newLayout) |
|
56 | 56 | { |
|
57 | 57 | if (state() != QAbstractAnimation::Stopped) stop(); |
|
58 | 58 | |
|
59 | 59 | if (newLayout.count() == 0) |
|
60 | 60 | return; |
|
61 | 61 | |
|
62 | 62 | switch (m_type) { |
|
63 | 63 | case ZoomOutAnimation: { |
|
64 | 64 | QRectF rect = m_axis->geometry(); |
|
65 | 65 | oldLayout.resize(newLayout.count()); |
|
66 | 66 | |
|
67 | 67 | for(int i = 0, j = oldLayout.count() - 1; i < (oldLayout.count() + 1) / 2; ++i, --j) { |
|
68 | 68 | oldLayout[i] = m_axis->axisType() == ChartAxis::X_AXIS ? rect.left() : rect.bottom(); |
|
69 | 69 | oldLayout[j] = m_axis->axisType() == ChartAxis::X_AXIS ? rect.right() : rect.top(); |
|
70 | 70 | } |
|
71 | 71 | } |
|
72 | 72 | break; |
|
73 | 73 | case ZoomInAnimation: { |
|
74 | 74 | int index = qMin(oldLayout.count() * (m_axis->axisType() == ChartAxis::X_AXIS ? m_point.x() : (1 - m_point.y())), newLayout.count() - (qreal)1.0); |
|
75 | 75 | oldLayout.resize(newLayout.count()); |
|
76 | 76 | |
|
77 | 77 | for(int i = 0; i < oldLayout.count(); i++) |
|
78 | 78 | oldLayout[i]= oldLayout[index]; |
|
79 | 79 | } |
|
80 | 80 | break; |
|
81 | 81 | case MoveForwardAnimation: { |
|
82 | 82 | oldLayout.resize(newLayout.count()); |
|
83 | 83 | |
|
84 | 84 | for(int i = 0, j = i + 1; i < oldLayout.count() - 1; ++i, ++j) |
|
85 | 85 | oldLayout[i]= oldLayout[j]; |
|
86 | 86 | } |
|
87 | 87 | break; |
|
88 | 88 | case MoveBackwordAnimation: { |
|
89 | 89 | oldLayout.resize(newLayout.count()); |
|
90 | 90 | |
|
91 | 91 | for(int i = oldLayout.count() - 1, j = i - 1; i > 0; --i, --j) |
|
92 | 92 | oldLayout[i]= oldLayout[j]; |
|
93 | 93 | } |
|
94 | 94 | break; |
|
95 | 95 | default: { |
|
96 | 96 | oldLayout.resize(newLayout.count()); |
|
97 | 97 | QRectF rect = m_axis->geometry(); |
|
98 | 98 | for(int i = 0, j = oldLayout.count() - 1; i < oldLayout.count(); ++i, --j) |
|
99 | 99 | oldLayout[i] = m_axis->axisType() == ChartAxis::X_AXIS ? rect.left() : rect.top(); |
|
100 | 100 | } |
|
101 | 101 | break; |
|
102 | 102 | } |
|
103 | 103 | |
|
104 | 104 | QVariantAnimation::KeyValues value; |
|
105 | 105 | setKeyValues(value); //workaround for wrong interpolation call |
|
106 | 106 | setKeyValueAt(0.0, qVariantFromValue(oldLayout)); |
|
107 | 107 | setKeyValueAt(1.0, qVariantFromValue(newLayout)); |
|
108 | 108 | } |
|
109 | 109 | |
|
110 | 110 | QVariant AxisAnimation::interpolated(const QVariant &start, const QVariant &end, qreal progress ) const |
|
111 | 111 | { |
|
112 | 112 | QVector<qreal> startVector = qVariantValue<QVector<qreal> >(start); |
|
113 | 113 | QVector<qreal> endVecotr = qVariantValue<QVector<qreal> >(end); |
|
114 | 114 | QVector<qreal> result; |
|
115 | 115 | |
|
116 | 116 | Q_ASSERT(startVector.count() == endVecotr.count()) ; |
|
117 | 117 | |
|
118 | 118 | for(int i = 0; i < startVector.count(); i++){ |
|
119 | 119 | qreal value = startVector[i] + ((endVecotr[i]- startVector[i]) * progress);//qBound(0.0, progress, 1.0)); |
|
120 | 120 | result << value; |
|
121 | 121 | } |
|
122 | 122 | return qVariantFromValue(result); |
|
123 | 123 | } |
|
124 | 124 | |
|
125 | 125 | |
|
126 | 126 | void AxisAnimation::updateCurrentValue (const QVariant &value ) |
|
127 | 127 | { |
|
128 | 128 | if (state() != QAbstractAnimation::Stopped)//workaround |
|
129 | 129 | { |
|
130 | 130 | QVector<qreal> vector = qVariantValue<QVector<qreal> >(value); |
|
131 | 131 | Q_ASSERT(vector.count() != 0); |
|
132 | 132 | m_axis->setLayout(vector); |
|
133 | 133 | m_axis->updateGeometry(); |
|
134 | m_axis->checkLayout(); | |
|
134 | 135 | } |
|
135 | 136 | |
|
136 | 137 | } |
|
137 | 138 | |
|
138 | 139 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,131 +1,132 | |||
|
1 | 1 | /**************************************************************************** |
|
2 | 2 | ** |
|
3 | 3 | ** Copyright (C) 2012 Digia Plc |
|
4 | 4 | ** All rights reserved. |
|
5 | 5 | ** For any questions to Digia, please use contact form at http://qt.digia.com |
|
6 | 6 | ** |
|
7 | 7 | ** This file is part of the Qt Commercial Charts Add-on. |
|
8 | 8 | ** |
|
9 | 9 | ** $QT_BEGIN_LICENSE$ |
|
10 | 10 | ** Licensees holding valid Qt Commercial licenses may use this file in |
|
11 | 11 | ** accordance with the Qt Commercial License Agreement provided with the |
|
12 | 12 | ** Software or, alternatively, in accordance with the terms contained in |
|
13 | 13 | ** a written agreement between you and Digia. |
|
14 | 14 | ** |
|
15 | 15 | ** If you have questions regarding the use of this file, please use |
|
16 | 16 | ** contact form at http://qt.digia.com |
|
17 | 17 | ** $QT_END_LICENSE$ |
|
18 | 18 | ** |
|
19 | 19 | ****************************************************************************/ |
|
20 | 20 | |
|
21 | 21 | #include "chartcategoryaxisx_p.h" |
|
22 | 22 | #include "qcategoryaxis.h" |
|
23 | 23 | #include "qabstractaxis.h" |
|
24 | 24 | #include "chartpresenter_p.h" |
|
25 | 25 | #include <QGraphicsLayout> |
|
26 | 26 | #include <QFontMetrics> |
|
27 | 27 | #include <qmath.h> |
|
28 | 28 | |
|
29 | 29 | static int label_padding = 5; |
|
30 | 30 | |
|
31 | 31 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
32 | 32 | |
|
33 | 33 | ChartCategoryAxisX::ChartCategoryAxisX(QAbstractAxis *axis,ChartPresenter *presenter) : ChartAxis(axis,presenter) |
|
34 | 34 | { |
|
35 | 35 | } |
|
36 | 36 | |
|
37 | 37 | ChartCategoryAxisX::~ChartCategoryAxisX() |
|
38 | 38 | { |
|
39 | 39 | } |
|
40 | 40 | |
|
41 | 41 | QVector<qreal> ChartCategoryAxisX::calculateLayout() const |
|
42 | 42 | { |
|
43 | 43 | QCategoryAxis *axis = qobject_cast<QCategoryAxis *>(m_chartAxis); |
|
44 | 44 | int tickCount = axis->categoriesLabels().count() + 1; |
|
45 | 45 | QVector<qreal> points; |
|
46 | 46 | |
|
47 | 47 | if (tickCount < 2) |
|
48 | 48 | return points; |
|
49 | 49 | |
|
50 | 50 | qreal range = axis->max() - axis->min(); |
|
51 | 51 | if (range > 0) { |
|
52 | 52 | points.resize(tickCount); |
|
53 | 53 | qreal scale = m_rect.width() / range; |
|
54 | 54 | for (int i = 0; i < tickCount; ++i) |
|
55 | 55 | if (i < tickCount - 1) { |
|
56 | 56 | int x = (axis->startValue(axis->categoriesLabels().at(i)) - axis->min()) * scale + m_rect.left(); |
|
57 | 57 | points[i] = x; |
|
58 | 58 | } else { |
|
59 | 59 | int x = (axis->endValue(axis->categoriesLabels().at(i - 1)) - axis->min()) * scale + m_rect.left(); |
|
60 | 60 | points[i] = x; |
|
61 | 61 | } |
|
62 | 62 | } |
|
63 | 63 | return points; |
|
64 | 64 | } |
|
65 | 65 | |
|
66 | 66 | void ChartCategoryAxisX::updateGeometry() |
|
67 | 67 | { |
|
68 | 68 | const QVector<qreal>& layout = ChartAxis::layout(); |
|
69 | 69 | |
|
70 | 70 | m_minWidth = 0; |
|
71 | 71 | m_minHeight = 0; |
|
72 | 72 | |
|
73 | 73 | if(layout.isEmpty()) return; |
|
74 | 74 | |
|
75 | 75 | QCategoryAxis *intervalAxis = qobject_cast<QCategoryAxis *>(m_chartAxis); |
|
76 | 76 | |
|
77 | 77 | QStringList ticksList = intervalAxis->categoriesLabels(); |
|
78 | 78 | |
|
79 | 79 | // createNumberLabels(ticksList,m_min,m_max,layout.size()); |
|
80 | 80 | |
|
81 | 81 | QList<QGraphicsItem *> lines = m_grid->childItems(); |
|
82 | 82 | QList<QGraphicsItem *> labels = m_labels->childItems(); |
|
83 | 83 | QList<QGraphicsItem *> shades = m_shades->childItems(); |
|
84 | 84 | QList<QGraphicsItem *> axis = m_arrow->childItems(); |
|
85 | 85 | |
|
86 | 86 | // Q_ASSERT(labels.size() == ticksList.size()); |
|
87 | 87 | // Q_ASSERT(layout.size() == ticksList.size()); |
|
88 | 88 | |
|
89 | 89 | QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(axis.at(0)); |
|
90 | 90 | lineItem->setLine(m_rect.left(), m_rect.bottom(), m_rect.right(), m_rect.bottom()); |
|
91 | 91 | |
|
92 | 92 | // qreal width = 0; |
|
93 | 93 | for (int i = 0; i < layout.size(); ++i) { |
|
94 | 94 | QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i)); |
|
95 | 95 | lineItem->setLine(layout[i], m_rect.top(), layout[i], m_rect.bottom()); |
|
96 | 96 | QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i)); |
|
97 | 97 | if (i < ticksList.count()) { |
|
98 | 98 | labelItem->setText(ticksList.at(i)); |
|
99 | 99 | } |
|
100 | 100 | const QRectF& rect = labelItem->boundingRect(); |
|
101 | 101 | QPointF center = rect.center(); |
|
102 | 102 | labelItem->setTransformOriginPoint(center.x(), center.y()); |
|
103 | 103 | if (i < ticksList.count()) |
|
104 | 104 | labelItem->setPos(layout[i] + (layout[i + 1] - layout[i]) / 2 - center.x(), m_rect.bottom() + label_padding); |
|
105 | 105 | else |
|
106 | 106 | labelItem->setPos(layout[i] - center.x(), m_rect.bottom() + label_padding); |
|
107 | 107 | |
|
108 | 108 | if(labelItem->pos().x() > m_rect.width() + m_rect.left() - rect.width() / 2){ |
|
109 | 109 | labelItem->setVisible(false); |
|
110 | 110 | lineItem->setVisible(false); |
|
111 | 111 | } |
|
112 | 112 | |
|
113 | 113 | m_minWidth += rect.width(); |
|
114 | 114 | m_minHeight = qMax(rect.height()+ label_padding, m_minHeight); |
|
115 | 115 | |
|
116 | 116 | if ((i + 1) % 2 && i > 1) { |
|
117 | 117 | QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i / 2 - 1)); |
|
118 | 118 | rectItem->setRect(layout[i - 1],m_rect.top(),layout[i]-layout[i - 1],m_rect.height()); |
|
119 | 119 | } |
|
120 | 120 | lineItem = static_cast<QGraphicsLineItem*>(axis.at(i+1)); |
|
121 | 121 | lineItem->setLine(layout[i],m_rect.bottom(),layout[i], m_rect.bottom() + 5); |
|
122 | 122 | } |
|
123 | ||
|
123 | 124 | } |
|
124 | 125 | |
|
125 | 126 | void ChartCategoryAxisX::handleAxisUpdated() |
|
126 | 127 | { |
|
127 | 128 | updateGeometry(); |
|
128 | 129 | ChartAxis::handleAxisUpdated(); |
|
129 | 130 | } |
|
130 | 131 | |
|
131 | 132 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,140 +1,141 | |||
|
1 | 1 | /**************************************************************************** |
|
2 | 2 | ** |
|
3 | 3 | ** Copyright (C) 2012 Digia Plc |
|
4 | 4 | ** All rights reserved. |
|
5 | 5 | ** For any questions to Digia, please use contact form at http://qt.digia.com |
|
6 | 6 | ** |
|
7 | 7 | ** This file is part of the Qt Commercial Charts Add-on. |
|
8 | 8 | ** |
|
9 | 9 | ** $QT_BEGIN_LICENSE$ |
|
10 | 10 | ** Licensees holding valid Qt Commercial licenses may use this file in |
|
11 | 11 | ** accordance with the Qt Commercial License Agreement provided with the |
|
12 | 12 | ** Software or, alternatively, in accordance with the terms contained in |
|
13 | 13 | ** a written agreement between you and Digia. |
|
14 | 14 | ** |
|
15 | 15 | ** If you have questions regarding the use of this file, please use |
|
16 | 16 | ** contact form at http://qt.digia.com |
|
17 | 17 | ** $QT_END_LICENSE$ |
|
18 | 18 | ** |
|
19 | 19 | ****************************************************************************/ |
|
20 | 20 | |
|
21 | 21 | #include "chartcategoryaxisy_p.h" |
|
22 | 22 | #include "qcategoryaxis.h" |
|
23 | 23 | #include "qabstractaxis.h" |
|
24 | 24 | #include "chartpresenter_p.h" |
|
25 | 25 | #include <QGraphicsLayout> |
|
26 | 26 | #include <QFontMetrics> |
|
27 | 27 | #include <qmath.h> |
|
28 | 28 | |
|
29 | 29 | static int label_padding = 5; |
|
30 | 30 | |
|
31 | 31 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
32 | 32 | |
|
33 | 33 | ChartCategoryAxisY::ChartCategoryAxisY(QAbstractAxis *axis,ChartPresenter *presenter) : ChartAxis(axis,presenter) |
|
34 | 34 | { |
|
35 | 35 | } |
|
36 | 36 | |
|
37 | 37 | ChartCategoryAxisY::~ChartCategoryAxisY() |
|
38 | 38 | { |
|
39 | 39 | } |
|
40 | 40 | |
|
41 | 41 | QVector<qreal> ChartCategoryAxisY::calculateLayout() const |
|
42 | 42 | { |
|
43 | 43 | QCategoryAxis *axis = qobject_cast<QCategoryAxis *>(m_chartAxis); |
|
44 | 44 | int tickCount = axis->categoriesLabels().count() + 1; |
|
45 | 45 | QVector<qreal> points; |
|
46 | 46 | |
|
47 | 47 | if (tickCount < 2) |
|
48 | 48 | return points; |
|
49 | 49 | |
|
50 | 50 | qreal range = axis->max() - axis->min(); |
|
51 | 51 | if (range > 0) { |
|
52 | 52 | points.resize(tickCount); |
|
53 | 53 | qreal scale = m_rect.height() / range; |
|
54 | 54 | for (int i = 0; i < tickCount; ++i) |
|
55 | 55 | if (i < tickCount - 1) { |
|
56 | 56 | int y = -(axis->startValue(axis->categoriesLabels().at(i)) - axis->min()) * scale + m_rect.bottom(); |
|
57 | 57 | points[i] = y; |
|
58 | 58 | } else { |
|
59 | 59 | int y = -(axis->endValue(axis->categoriesLabels().at(i - 1)) - axis->min()) * scale + m_rect.bottom(); |
|
60 | 60 | points[i] = y; |
|
61 | 61 | } |
|
62 | 62 | } |
|
63 | 63 | |
|
64 | 64 | return points; |
|
65 | 65 | } |
|
66 | 66 | |
|
67 | 67 | void ChartCategoryAxisY::updateGeometry() |
|
68 | 68 | { |
|
69 | 69 | const QVector<qreal> &layout = ChartAxis::layout(); |
|
70 | 70 | m_minWidth = 0; |
|
71 | 71 | m_minHeight = 0; |
|
72 | 72 | |
|
73 | 73 | if(layout.isEmpty()) { |
|
74 | 74 | return; |
|
75 | 75 | } |
|
76 | 76 | |
|
77 | 77 | QCategoryAxis *intervalAxis = qobject_cast<QCategoryAxis *>(m_chartAxis); |
|
78 | 78 | |
|
79 | 79 | QStringList ticksList = intervalAxis->categoriesLabels(); |
|
80 | 80 | |
|
81 | 81 | QList<QGraphicsItem *> lines = m_grid->childItems(); |
|
82 | 82 | QList<QGraphicsItem *> labels = m_labels->childItems(); |
|
83 | 83 | QList<QGraphicsItem *> shades = m_shades->childItems(); |
|
84 | 84 | QList<QGraphicsItem *> axis = m_arrow->childItems(); |
|
85 | 85 | |
|
86 | 86 | // Q_ASSERT(labels.size() == ticksList.size()); |
|
87 | 87 | // Q_ASSERT(layout.size() == ticksList.size()); |
|
88 | 88 | |
|
89 | 89 | qreal height = 2*m_rect.bottom(); |
|
90 | 90 | |
|
91 | 91 | QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(axis.at(0)); |
|
92 | 92 | lineItem->setLine(m_rect.left() , m_rect.top(), m_rect.left(), m_rect.bottom()); |
|
93 | 93 | |
|
94 | 94 | for (int i = 0; i < layout.size(); ++i) { |
|
95 | 95 | QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i)); |
|
96 | 96 | lineItem->setLine(m_rect.left() , layout[i], m_rect.right(), layout[i]); |
|
97 | 97 | QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i)); |
|
98 | 98 | |
|
99 | 99 | if (i < ticksList.count()) { |
|
100 | 100 | labelItem->setText(ticksList.at(i)); |
|
101 | 101 | } |
|
102 | 102 | const QRectF& rect = labelItem->boundingRect(); |
|
103 | 103 | |
|
104 | 104 | QPointF center = rect.center(); |
|
105 | 105 | labelItem->setTransformOriginPoint(center.x(), center.y()); |
|
106 | 106 | |
|
107 | 107 | if (i < ticksList.count()) |
|
108 | 108 | labelItem->setPos(m_rect.left() - rect.width() - label_padding , layout[i] + (layout[i + 1] - layout[i]) / 2 - center.y()); |
|
109 | 109 | else |
|
110 | 110 | labelItem->setPos(m_rect.left() - rect.width() - label_padding , layout[i]-center.y()); |
|
111 | 111 | |
|
112 | 112 | if(labelItem->pos().y()+rect.height()>height) { |
|
113 | 113 | labelItem->setVisible(false); |
|
114 | 114 | lineItem->setVisible(false); |
|
115 | 115 | } |
|
116 | 116 | else { |
|
117 | 117 | labelItem->setVisible(true); |
|
118 | 118 | lineItem->setVisible(true); |
|
119 | 119 | height=labelItem->pos().y(); |
|
120 | 120 | } |
|
121 | 121 | |
|
122 | 122 | m_minWidth=qMax(rect.width()+label_padding,m_minWidth); |
|
123 | 123 | m_minHeight+=rect.height(); |
|
124 | 124 | |
|
125 | 125 | if ((i+1)%2 && i>1) { |
|
126 | 126 | QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2-1)); |
|
127 | 127 | rectItem->setRect(m_rect.left(),layout[i],m_rect.width(),layout[i-1]-layout[i]); |
|
128 | 128 | } |
|
129 | 129 | lineItem = static_cast<QGraphicsLineItem*>(axis.at(i+1)); |
|
130 | 130 | lineItem->setLine(m_rect.left()-5,layout[i],m_rect.left(),layout[i]); |
|
131 | 131 | } |
|
132 | ||
|
132 | 133 | } |
|
133 | 134 | |
|
134 | 135 | void ChartCategoryAxisY::handleAxisUpdated() |
|
135 | 136 | { |
|
136 | 137 | updateGeometry(); |
|
137 | 138 | ChartAxis::handleAxisUpdated(); |
|
138 | 139 | } |
|
139 | 140 | |
|
140 | 141 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,386 +1,399 | |||
|
1 | 1 | /**************************************************************************** |
|
2 | 2 | ** |
|
3 | 3 | ** Copyright (C) 2012 Digia Plc |
|
4 | 4 | ** All rights reserved. |
|
5 | 5 | ** For any questions to Digia, please use contact form at http://qt.digia.com |
|
6 | 6 | ** |
|
7 | 7 | ** This file is part of the Qt Commercial Charts Add-on. |
|
8 | 8 | ** |
|
9 | 9 | ** $QT_BEGIN_LICENSE$ |
|
10 | 10 | ** Licensees holding valid Qt Commercial licenses may use this file in |
|
11 | 11 | ** accordance with the Qt Commercial License Agreement provided with the |
|
12 | 12 | ** Software or, alternatively, in accordance with the terms contained in |
|
13 | 13 | ** a written agreement between you and Digia. |
|
14 | 14 | ** |
|
15 | 15 | ** If you have questions regarding the use of this file, please use |
|
16 | 16 | ** contact form at http://qt.digia.com |
|
17 | 17 | ** $QT_END_LICENSE$ |
|
18 | 18 | ** |
|
19 | 19 | ****************************************************************************/ |
|
20 | 20 | |
|
21 | 21 | #include "chartaxis_p.h" |
|
22 | 22 | #include "qabstractaxis.h" |
|
23 | 23 | #include "qabstractaxis_p.h" |
|
24 | 24 | #include "chartpresenter_p.h" |
|
25 | 25 | #include "domain_p.h" |
|
26 | 26 | #include <qmath.h> |
|
27 | 27 | #include <QDateTime> |
|
28 | 28 | #include <QValueAxis> |
|
29 | #include <QGraphicsLayout> | |
|
29 | 30 | |
|
30 | 31 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
31 | 32 | |
|
32 | 33 | ChartAxis::ChartAxis(QAbstractAxis *axis,ChartPresenter *presenter) : ChartElement(presenter), |
|
33 | 34 | m_chartAxis(axis), |
|
34 | 35 | m_labelsAngle(0), |
|
35 | 36 | m_grid(new QGraphicsItemGroup(presenter->rootItem())), |
|
36 | 37 | m_shades(new QGraphicsItemGroup(presenter->rootItem())), |
|
37 | 38 | m_labels(new QGraphicsItemGroup(presenter->rootItem())), |
|
38 | 39 | m_arrow(new QGraphicsItemGroup(presenter->rootItem())), |
|
39 | 40 | m_min(0), |
|
40 | 41 | m_max(0), |
|
41 | 42 | m_animation(0), |
|
42 | 43 | m_minWidth(0), |
|
43 | 44 | m_minHeight(0) |
|
44 | 45 | { |
|
45 | 46 | //initial initialization |
|
46 | 47 | m_arrow->setZValue(ChartPresenter::AxisZValue); |
|
47 | 48 | m_arrow->setHandlesChildEvents(false); |
|
48 | 49 | m_labels->setZValue(ChartPresenter::AxisZValue); |
|
49 | 50 | m_shades->setZValue(ChartPresenter::ShadesZValue); |
|
50 | 51 | m_grid->setZValue(ChartPresenter::GridZValue); |
|
51 | 52 | |
|
52 | 53 | QObject::connect(m_chartAxis->d_ptr.data(),SIGNAL(updated()),this,SLOT(handleAxisUpdated())); |
|
53 | 54 | |
|
54 | 55 | QGraphicsSimpleTextItem item; |
|
55 | 56 | m_font = item.font(); |
|
56 | 57 | |
|
57 | 58 | } |
|
58 | 59 | |
|
59 | 60 | ChartAxis::~ChartAxis() |
|
60 | 61 | { |
|
61 | 62 | } |
|
62 | 63 | |
|
63 | 64 | void ChartAxis::setAnimation(AxisAnimation* animation) |
|
64 | 65 | { |
|
65 | 66 | m_animation=animation; |
|
66 | 67 | } |
|
67 | 68 | |
|
68 | 69 | void ChartAxis::setLayout(QVector<qreal> &layout) |
|
69 | 70 | { |
|
70 | 71 | m_layoutVector=layout; |
|
71 | 72 | } |
|
72 | 73 | |
|
73 | 74 | void ChartAxis::createItems(int count) |
|
74 | 75 | { |
|
75 | 76 | if (m_arrow->children().size() == 0) |
|
76 | 77 | m_arrow->addToGroup(new AxisItem(this,presenter()->rootItem())); |
|
77 | 78 | for (int i = 0; i < count; ++i) { |
|
78 | 79 | m_grid->addToGroup(new QGraphicsLineItem(presenter()->rootItem())); |
|
79 | 80 | m_labels->addToGroup(new QGraphicsSimpleTextItem(presenter()->rootItem())); |
|
80 | 81 | m_arrow->addToGroup(new QGraphicsLineItem(presenter()->rootItem())); |
|
81 | 82 | if ((m_grid->childItems().size())%2 && m_grid->childItems().size()>2) m_shades->addToGroup(new QGraphicsRectItem(presenter()->rootItem())); |
|
82 | 83 | } |
|
83 | 84 | } |
|
84 | 85 | |
|
85 | 86 | void ChartAxis::deleteItems(int count) |
|
86 | 87 | { |
|
87 | 88 | QList<QGraphicsItem *> lines = m_grid->childItems(); |
|
88 | 89 | QList<QGraphicsItem *> labels = m_labels->childItems(); |
|
89 | 90 | QList<QGraphicsItem *> shades = m_shades->childItems(); |
|
90 | 91 | QList<QGraphicsItem *> axis = m_arrow->childItems(); |
|
91 | 92 | |
|
92 | 93 | for (int i = 0; i < count; ++i) { |
|
93 | 94 | if (lines.size()%2 && lines.size() > 1) delete(shades.takeLast()); |
|
94 | 95 | delete(lines.takeLast()); |
|
95 | 96 | delete(labels.takeLast()); |
|
96 | 97 | delete(axis.takeLast()); |
|
97 | 98 | } |
|
98 | 99 | } |
|
99 | 100 | |
|
100 | 101 | void ChartAxis::updateLayout(QVector<qreal> &layout) |
|
101 | 102 | { |
|
102 | 103 | int diff = m_layoutVector.size() - layout.size(); |
|
103 | 104 | |
|
104 | 105 | if (diff>0) { |
|
105 | 106 | deleteItems(diff); |
|
106 | 107 | } |
|
107 | 108 | else if (diff<0) { |
|
108 | 109 | createItems(-diff); |
|
109 | 110 | } |
|
110 | 111 | |
|
111 | 112 | if(diff<0) handleAxisUpdated(); |
|
112 | 113 | |
|
113 | 114 | if (m_animation) { |
|
114 | 115 | switch(presenter()->state()){ |
|
115 | 116 | case ChartPresenter::ZoomInState: |
|
116 | 117 | m_animation->setAnimationType(AxisAnimation::ZoomInAnimation); |
|
117 | 118 | m_animation->setAnimationPoint(presenter()->statePoint()); |
|
118 | 119 | break; |
|
119 | 120 | case ChartPresenter::ZoomOutState: |
|
120 | 121 | m_animation->setAnimationType(AxisAnimation::ZoomOutAnimation); |
|
121 | 122 | m_animation->setAnimationPoint(presenter()->statePoint()); |
|
122 | 123 | break; |
|
123 | 124 | case ChartPresenter::ScrollUpState: |
|
124 | 125 | case ChartPresenter::ScrollLeftState: |
|
125 | 126 | m_animation->setAnimationType(AxisAnimation::MoveBackwordAnimation); |
|
126 | 127 | break; |
|
127 | 128 | case ChartPresenter::ScrollDownState: |
|
128 | 129 | case ChartPresenter::ScrollRightState: |
|
129 | 130 | m_animation->setAnimationType(AxisAnimation::MoveForwardAnimation); |
|
130 | 131 | break; |
|
131 | 132 | case ChartPresenter::ShowState: |
|
132 | 133 | m_animation->setAnimationType(AxisAnimation::DefaultAnimation); |
|
133 | 134 | break; |
|
134 | 135 | } |
|
135 | 136 | m_animation->setValues(m_layoutVector,layout); |
|
136 | 137 | presenter()->startAnimation(m_animation); |
|
137 | 138 | } |
|
138 | 139 | else { |
|
139 | 140 | setLayout(layout); |
|
140 | 141 | updateGeometry(); |
|
142 | checkLayout(); | |
|
141 | 143 | } |
|
142 | 144 | } |
|
143 | 145 | |
|
144 | 146 | void ChartAxis::setArrowOpacity(qreal opacity) |
|
145 | 147 | { |
|
146 | 148 | m_arrow->setOpacity(opacity); |
|
147 | 149 | } |
|
148 | 150 | |
|
149 | 151 | qreal ChartAxis::arrowOpacity() const |
|
150 | 152 | { |
|
151 | 153 | return m_arrow->opacity(); |
|
152 | 154 | } |
|
153 | 155 | |
|
154 | 156 | void ChartAxis::setArrowVisibility(bool visible) |
|
155 | 157 | { |
|
156 | 158 | m_arrow->setOpacity(visible); |
|
157 | 159 | } |
|
158 | 160 | |
|
159 | 161 | void ChartAxis::setGridOpacity(qreal opacity) |
|
160 | 162 | { |
|
161 | 163 | m_grid->setOpacity(opacity); |
|
162 | 164 | } |
|
163 | 165 | |
|
164 | 166 | qreal ChartAxis::gridOpacity() const |
|
165 | 167 | { |
|
166 | 168 | return m_grid->opacity(); |
|
167 | 169 | } |
|
168 | 170 | |
|
169 | 171 | void ChartAxis::setGridVisibility(bool visible) |
|
170 | 172 | { |
|
171 | 173 | m_grid->setOpacity(visible); |
|
172 | 174 | } |
|
173 | 175 | |
|
174 | 176 | void ChartAxis::setLabelsOpacity(qreal opacity) |
|
175 | 177 | { |
|
176 | 178 | m_labels->setOpacity(opacity); |
|
177 | 179 | } |
|
178 | 180 | |
|
179 | 181 | qreal ChartAxis::labelsOpacity() const |
|
180 | 182 | { |
|
181 | 183 | return m_labels->opacity(); |
|
182 | 184 | } |
|
183 | 185 | |
|
184 | 186 | void ChartAxis::setLabelsVisibility(bool visible) |
|
185 | 187 | { |
|
186 | 188 | m_labels->setOpacity(visible); |
|
187 | 189 | } |
|
188 | 190 | |
|
189 | 191 | void ChartAxis::setShadesOpacity(qreal opacity) |
|
190 | 192 | { |
|
191 | 193 | m_shades->setOpacity(opacity); |
|
192 | 194 | } |
|
193 | 195 | |
|
194 | 196 | qreal ChartAxis::shadesOpacity() const |
|
195 | 197 | { |
|
196 | 198 | return m_shades->opacity(); |
|
197 | 199 | } |
|
198 | 200 | |
|
199 | 201 | void ChartAxis::setShadesVisibility(bool visible) |
|
200 | 202 | { |
|
201 | 203 | m_shades->setVisible(visible); |
|
202 | 204 | } |
|
203 | 205 | |
|
204 | 206 | void ChartAxis::setLabelsAngle(int angle) |
|
205 | 207 | { |
|
206 | 208 | foreach(QGraphicsItem* item , m_labels->childItems()) { |
|
207 | 209 | item->setRotation(angle); |
|
208 | 210 | } |
|
209 | 211 | |
|
210 | 212 | m_labelsAngle=angle; |
|
211 | 213 | } |
|
212 | 214 | |
|
213 | 215 | void ChartAxis::setLabelsPen(const QPen &pen) |
|
214 | 216 | { |
|
215 | 217 | foreach(QGraphicsItem* item , m_labels->childItems()) { |
|
216 | 218 | static_cast<QGraphicsSimpleTextItem*>(item)->setPen(pen); |
|
217 | 219 | } |
|
218 | 220 | } |
|
219 | 221 | |
|
220 | 222 | void ChartAxis::setLabelsBrush(const QBrush &brush) |
|
221 | 223 | { |
|
222 | 224 | foreach(QGraphicsItem* item , m_labels->childItems()) { |
|
223 | 225 | static_cast<QGraphicsSimpleTextItem*>(item)->setBrush(brush); |
|
224 | 226 | } |
|
225 | 227 | } |
|
226 | 228 | |
|
227 | 229 | void ChartAxis::setLabelsFont(const QFont &font) |
|
228 | 230 | { |
|
229 | 231 | foreach(QGraphicsItem* item , m_labels->childItems()) { |
|
230 | 232 | static_cast<QGraphicsSimpleTextItem*>(item)->setFont(font); |
|
231 | 233 | } |
|
232 | 234 | m_font = font; |
|
233 | 235 | } |
|
234 | 236 | |
|
235 | 237 | void ChartAxis::setShadesBrush(const QBrush &brush) |
|
236 | 238 | { |
|
237 | 239 | foreach(QGraphicsItem* item , m_shades->childItems()) { |
|
238 | 240 | static_cast<QGraphicsRectItem*>(item)->setBrush(brush); |
|
239 | 241 | } |
|
240 | 242 | } |
|
241 | 243 | |
|
242 | 244 | void ChartAxis::setShadesPen(const QPen &pen) |
|
243 | 245 | { |
|
244 | 246 | foreach(QGraphicsItem* item , m_shades->childItems()) { |
|
245 | 247 | static_cast<QGraphicsRectItem*>(item)->setPen(pen); |
|
246 | 248 | } |
|
247 | 249 | } |
|
248 | 250 | |
|
249 | 251 | void ChartAxis::setArrowPen(const QPen &pen) |
|
250 | 252 | { |
|
251 | 253 | foreach(QGraphicsItem* item , m_arrow->childItems()) { |
|
252 | 254 | static_cast<QGraphicsLineItem*>(item)->setPen(pen); |
|
253 | 255 | } |
|
254 | 256 | } |
|
255 | 257 | |
|
256 | 258 | void ChartAxis::setGridPen(const QPen &pen) |
|
257 | 259 | { |
|
258 | 260 | foreach(QGraphicsItem* item , m_grid->childItems()) { |
|
259 | 261 | static_cast<QGraphicsLineItem*>(item)->setPen(pen); |
|
260 | 262 | } |
|
261 | 263 | } |
|
262 | 264 | |
|
263 | 265 | bool ChartAxis::isEmpty() |
|
264 | 266 | { |
|
265 | 267 | return m_rect.isEmpty() || qFuzzyIsNull(m_min - m_max); |
|
266 | 268 | } |
|
267 | 269 | |
|
268 | 270 | void ChartAxis::handleDomainUpdated() |
|
269 | 271 | { |
|
270 | 272 | Domain* domain = qobject_cast<Domain*>(sender()); |
|
271 | 273 | qreal min(0); |
|
272 | 274 | qreal max(0); |
|
273 | 275 | |
|
274 | 276 | if(m_chartAxis->orientation()==Qt::Horizontal) { |
|
275 | 277 | min = domain->minX(); |
|
276 | 278 | max = domain->maxX(); |
|
277 | 279 | } |
|
278 | 280 | else if (m_chartAxis->orientation()==Qt::Vertical) |
|
279 | 281 | { |
|
280 | 282 | min = domain->minY(); |
|
281 | 283 | max = domain->maxY(); |
|
282 | 284 | } |
|
283 | 285 | |
|
284 | 286 | if (!qFuzzyIsNull(m_min - min) || !qFuzzyIsNull(m_max - max)) |
|
285 | 287 | { |
|
286 | 288 | m_min = min; |
|
287 | 289 | m_max = max; |
|
288 | 290 | |
|
289 | 291 | if (!isEmpty()) { |
|
290 | 292 | QVector<qreal> layout = calculateLayout(); |
|
291 | 293 | updateLayout(layout); |
|
292 | 294 | } |
|
293 | 295 | } |
|
294 | 296 | } |
|
295 | 297 | |
|
296 | 298 | void ChartAxis::handleAxisUpdated() |
|
297 | 299 | { |
|
298 | 300 | if(isEmpty()) return; |
|
299 | 301 | |
|
300 | 302 | |
|
301 | 303 | bool visible = m_chartAxis->isVisible(); |
|
302 | 304 | |
|
303 | 305 | setArrowVisibility(visible && m_chartAxis->isLineVisible()); |
|
304 | 306 | setGridVisibility(visible && m_chartAxis->isGridLineVisible()); |
|
305 | 307 | setLabelsVisibility(visible && m_chartAxis->labelsVisible()); |
|
306 | 308 | setShadesVisibility(visible && m_chartAxis->shadesVisible()); |
|
307 | 309 | setLabelsAngle(m_chartAxis->labelsAngle()); |
|
308 | 310 | setArrowPen(m_chartAxis->linePen()); |
|
309 | 311 | setLabelsPen(m_chartAxis->labelsPen()); |
|
310 | 312 | setLabelsBrush(m_chartAxis->labelsBrush()); |
|
311 | 313 | setLabelsFont(m_chartAxis->labelsFont()); |
|
312 | 314 | setGridPen(m_chartAxis->gridLinePen()); |
|
313 | 315 | setShadesPen(m_chartAxis->shadesPen()); |
|
314 | 316 | setShadesBrush(m_chartAxis->shadesBrush()); |
|
315 | 317 | |
|
316 | 318 | } |
|
317 | 319 | |
|
318 | 320 | void ChartAxis::hide() |
|
319 | 321 | { |
|
320 | 322 | setArrowVisibility(false); |
|
321 | 323 | setGridVisibility(false); |
|
322 | 324 | setLabelsVisibility(false); |
|
323 | 325 | setShadesVisibility(false); |
|
324 | 326 | } |
|
325 | 327 | |
|
326 | 328 | void ChartAxis::handleGeometryChanged(const QRectF &rect) |
|
327 | 329 | { |
|
328 | 330 | if(m_rect != rect) |
|
329 | 331 | { |
|
330 | 332 | m_rect = rect; |
|
331 | 333 | if (isEmpty()) return; |
|
332 | 334 | QVector<qreal> layout = calculateLayout(); |
|
333 | 335 | updateLayout(layout); |
|
334 | 336 | } |
|
335 | 337 | } |
|
336 | 338 | |
|
337 | 339 | |
|
338 | 340 | qreal ChartAxis::minimumWidth() |
|
339 | 341 | { |
|
340 | 342 | if(m_minWidth == 0) updateGeometry(); |
|
341 | 343 | return m_minWidth; |
|
342 | 344 | } |
|
343 | 345 | |
|
344 | 346 | qreal ChartAxis::minimumHeight() |
|
345 | 347 | { |
|
346 | 348 | if(m_minHeight == 0) updateGeometry(); |
|
347 | 349 | return m_minHeight; |
|
348 | 350 | } |
|
349 | 351 | |
|
350 | 352 | |
|
351 | 353 | void ChartAxis::axisSelected() |
|
352 | 354 | { |
|
353 | 355 | qDebug()<<"TODO: axis clicked"; |
|
354 | 356 | } |
|
355 | 357 | |
|
356 | 358 | |
|
357 | 359 | void ChartAxis::createNumberLabels(QStringList &labels,qreal min, qreal max, int ticks) const |
|
358 | 360 | { |
|
359 | 361 | Q_ASSERT(max>min); |
|
360 | 362 | Q_ASSERT(ticks>1); |
|
361 | 363 | |
|
362 | 364 | int n = qMax(int(-qFloor(log10((max-min)/(ticks-1)))),0); |
|
363 | 365 | n++; |
|
364 | 366 | |
|
365 | 367 | QValueAxis *axis = qobject_cast<QValueAxis *>(m_chartAxis); |
|
366 | 368 | |
|
367 | 369 | QString format = axis->labelFormat(); |
|
368 | 370 | |
|
369 | 371 | if(format.isNull()) { |
|
370 | 372 | for (int i=0; i< ticks; i++) { |
|
371 | 373 | qreal value = min + (i * (max - min)/ (ticks-1)); |
|
372 | 374 | labels << QString::number(value,'f',n); |
|
373 | 375 | } |
|
374 | 376 | } |
|
375 | 377 | else { |
|
376 | 378 | QByteArray array = format.toAscii(); |
|
377 | 379 | for (int i=0; i< ticks; i++) { |
|
378 | 380 | qreal value = min + (i * (max - min)/ (ticks-1)); |
|
379 | 381 | labels << QString().sprintf(array, value); |
|
380 | 382 | } |
|
381 | 383 | } |
|
382 | 384 | } |
|
383 | 385 | |
|
386 | void ChartAxis::checkLayout() | |
|
387 | { | |
|
388 | if(m_minWidth > m_rect.width()) { | |
|
389 | presenter()->layout()->invalidate(); | |
|
390 | } | |
|
391 | ||
|
392 | if(m_minHeight > m_rect.height()) { | |
|
393 | presenter()->layout()->invalidate(); | |
|
394 | } | |
|
395 | } | |
|
396 | ||
|
384 | 397 | #include "moc_chartaxis_p.cpp" |
|
385 | 398 | |
|
386 | 399 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,168 +1,169 | |||
|
1 | 1 | /**************************************************************************** |
|
2 | 2 | ** |
|
3 | 3 | ** Copyright (C) 2012 Digia Plc |
|
4 | 4 | ** All rights reserved. |
|
5 | 5 | ** For any questions to Digia, please use contact form at http://qt.digia.com |
|
6 | 6 | ** |
|
7 | 7 | ** This file is part of the Qt Commercial Charts Add-on. |
|
8 | 8 | ** |
|
9 | 9 | ** $QT_BEGIN_LICENSE$ |
|
10 | 10 | ** Licensees holding valid Qt Commercial licenses may use this file in |
|
11 | 11 | ** accordance with the Qt Commercial License Agreement provided with the |
|
12 | 12 | ** Software or, alternatively, in accordance with the terms contained in |
|
13 | 13 | ** a written agreement between you and Digia. |
|
14 | 14 | ** |
|
15 | 15 | ** If you have questions regarding the use of this file, please use |
|
16 | 16 | ** contact form at http://qt.digia.com |
|
17 | 17 | ** $QT_END_LICENSE$ |
|
18 | 18 | ** |
|
19 | 19 | ****************************************************************************/ |
|
20 | 20 | |
|
21 | 21 | // W A R N I N G |
|
22 | 22 | // ------------- |
|
23 | 23 | // |
|
24 | 24 | // This file is not part of the QtCommercial Chart API. It exists purely as an |
|
25 | 25 | // implementation detail. This header file may change from version to |
|
26 | 26 | // version without notice, or even be removed. |
|
27 | 27 | // |
|
28 | 28 | // We mean it. |
|
29 | 29 | |
|
30 | 30 | #ifndef CHARTAXIS_H |
|
31 | 31 | #define CHARTAXIS_H |
|
32 | 32 | |
|
33 | 33 | #include "qchartglobal.h" |
|
34 | 34 | #include "chartelement_p.h" |
|
35 | 35 | #include "axisanimation_p.h" |
|
36 | 36 | #include <QGraphicsItem> |
|
37 | 37 | #include <QFont> |
|
38 | 38 | |
|
39 | 39 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
40 | 40 | |
|
41 | 41 | class QAbstractAxis; |
|
42 | 42 | class ChartPresenter; |
|
43 | 43 | |
|
44 | 44 | class ChartAxis : public ChartElement |
|
45 | 45 | { |
|
46 | 46 | Q_OBJECT |
|
47 | 47 | public: |
|
48 | 48 | enum AxisType{ X_AXIS,Y_AXIS }; |
|
49 | 49 | |
|
50 | 50 | ChartAxis(QAbstractAxis *axis, ChartPresenter *presenter); |
|
51 | 51 | ~ChartAxis(); |
|
52 | 52 | |
|
53 | 53 | virtual AxisType axisType() const = 0; |
|
54 | 54 | |
|
55 | 55 | void setArrowOpacity(qreal opacity); |
|
56 | 56 | qreal arrowOpacity() const; |
|
57 | 57 | void setArrowVisibility(bool visible); |
|
58 | 58 | |
|
59 | 59 | void setGridOpacity(qreal opacity); |
|
60 | 60 | qreal gridOpacity() const; |
|
61 | 61 | void setGridVisibility(bool visible); |
|
62 | 62 | |
|
63 | 63 | void setLabelsOpacity(qreal opacity); |
|
64 | 64 | qreal labelsOpacity() const; |
|
65 | 65 | void setLabelsVisibility(bool visible); |
|
66 | 66 | |
|
67 | 67 | void setShadesOpacity(qreal opacity); |
|
68 | 68 | qreal shadesOpacity() const; |
|
69 | 69 | void setShadesVisibility(bool visible); |
|
70 | 70 | |
|
71 | 71 | void setLabelsAngle(int angle); |
|
72 | 72 | int labelsAngle()const { return m_labelsAngle; } |
|
73 | 73 | |
|
74 | 74 | void setShadesBrush(const QBrush &brush); |
|
75 | 75 | void setShadesPen(const QPen &pen); |
|
76 | 76 | |
|
77 | 77 | void setArrowPen(const QPen &pen); |
|
78 | 78 | void setGridPen(const QPen &pen); |
|
79 | 79 | |
|
80 | 80 | void setLabelsPen(const QPen &pen); |
|
81 | 81 | void setLabelsBrush(const QBrush &brush); |
|
82 | 82 | void setLabelsFont(const QFont &font); |
|
83 | 83 | |
|
84 | 84 | void setLayout(QVector<qreal> &layout); |
|
85 | 85 | QVector<qreal> layout() const { return m_layoutVector; } |
|
86 | 86 | |
|
87 | 87 | void setAnimation(AxisAnimation* animation); |
|
88 | 88 | ChartAnimation* animation() const { return m_animation; }; |
|
89 | 89 | |
|
90 | 90 | QRectF geometry() const { return m_rect; } |
|
91 | 91 | |
|
92 | 92 | qreal minimumWidth(); |
|
93 | 93 | qreal minimumHeight(); |
|
94 | 94 | |
|
95 | 95 | void hide(); |
|
96 | 96 | |
|
97 | 97 | protected: |
|
98 | 98 | virtual void updateGeometry() = 0; |
|
99 | 99 | virtual QVector<qreal> calculateLayout() const = 0; |
|
100 | 100 | void createNumberLabels(QStringList &labels,qreal min, qreal max,int ticks) const; |
|
101 | void checkLayout(); | |
|
101 | 102 | |
|
102 | 103 | public Q_SLOTS: |
|
103 | 104 | virtual void handleAxisUpdated(); |
|
104 | 105 | virtual void handleDomainUpdated(); |
|
105 | 106 | void handleGeometryChanged(const QRectF &size); |
|
106 | 107 | |
|
107 | 108 | private: |
|
108 | 109 | inline bool isEmpty(); |
|
109 | 110 | void createItems(int count); |
|
110 | 111 | void deleteItems(int count); |
|
111 | 112 | void updateLayout(QVector<qreal> &layout); |
|
112 | 113 | void axisSelected(); |
|
113 | 114 | |
|
114 | 115 | protected: |
|
115 | 116 | QAbstractAxis* m_chartAxis; |
|
116 | 117 | QRectF m_rect; |
|
117 | 118 | int m_labelsAngle; |
|
118 | 119 | QScopedPointer<QGraphicsItemGroup> m_grid; |
|
119 | 120 | QScopedPointer<QGraphicsItemGroup> m_shades; |
|
120 | 121 | QScopedPointer<QGraphicsItemGroup> m_labels; |
|
121 | 122 | QScopedPointer<QGraphicsItemGroup> m_arrow; |
|
122 | 123 | QVector<qreal> m_layoutVector; |
|
123 | 124 | qreal m_min; |
|
124 | 125 | qreal m_max; |
|
125 | 126 | AxisAnimation *m_animation; |
|
126 | 127 | qreal m_minWidth; |
|
127 | 128 | qreal m_minHeight; |
|
128 | 129 | QFont m_font; |
|
129 | 130 | |
|
130 | 131 | friend class AxisAnimation; |
|
131 | 132 | friend class AxisItem; |
|
132 | 133 | |
|
133 | 134 | }; |
|
134 | 135 | |
|
135 | 136 | class AxisItem: public QGraphicsLineItem |
|
136 | 137 | { |
|
137 | 138 | |
|
138 | 139 | public: |
|
139 | 140 | explicit AxisItem(ChartAxis *axis, QGraphicsItem *parent = 0) : QGraphicsLineItem(parent), m_axis(axis) {} |
|
140 | 141 | |
|
141 | 142 | protected: |
|
142 | 143 | void mousePressEvent(QGraphicsSceneMouseEvent *event) |
|
143 | 144 | { |
|
144 | 145 | Q_UNUSED(event) |
|
145 | 146 | m_axis->axisSelected(); |
|
146 | 147 | } |
|
147 | 148 | |
|
148 | 149 | QRectF boundingRect() const |
|
149 | 150 | { |
|
150 | 151 | return shape().boundingRect(); |
|
151 | 152 | } |
|
152 | 153 | |
|
153 | 154 | QPainterPath shape() const |
|
154 | 155 | { |
|
155 | 156 | QPainterPath path = QGraphicsLineItem::shape(); |
|
156 | 157 | QRectF rect = path.boundingRect(); |
|
157 | 158 | path.addRect(rect.adjusted(0,0,m_axis->axisType()!=ChartAxis::X_AXIS?8:0,m_axis->axisType()!=ChartAxis::Y_AXIS?8:0)); |
|
158 | 159 | return path; |
|
159 | 160 | } |
|
160 | 161 | |
|
161 | 162 | private: |
|
162 | 163 | ChartAxis* m_axis; |
|
163 | 164 | |
|
164 | 165 | }; |
|
165 | 166 | |
|
166 | 167 | QTCOMMERCIALCHART_END_NAMESPACE |
|
167 | 168 | |
|
168 | 169 | #endif /* AXISITEM_H_ */ |
@@ -1,120 +1,121 | |||
|
1 | 1 | /**************************************************************************** |
|
2 | 2 | ** |
|
3 | 3 | ** Copyright (C) 2012 Digia Plc |
|
4 | 4 | ** All rights reserved. |
|
5 | 5 | ** For any questions to Digia, please use contact form at http://qt.digia.com |
|
6 | 6 | ** |
|
7 | 7 | ** This file is part of the Qt Commercial Charts Add-on. |
|
8 | 8 | ** |
|
9 | 9 | ** $QT_BEGIN_LICENSE$ |
|
10 | 10 | ** Licensees holding valid Qt Commercial licenses may use this file in |
|
11 | 11 | ** accordance with the Qt Commercial License Agreement provided with the |
|
12 | 12 | ** Software or, alternatively, in accordance with the terms contained in |
|
13 | 13 | ** a written agreement between you and Digia. |
|
14 | 14 | ** |
|
15 | 15 | ** If you have questions regarding the use of this file, please use |
|
16 | 16 | ** contact form at http://qt.digia.com |
|
17 | 17 | ** $QT_END_LICENSE$ |
|
18 | 18 | ** |
|
19 | 19 | ****************************************************************************/ |
|
20 | 20 | |
|
21 | 21 | #include "chartvalueaxisx_p.h" |
|
22 | 22 | #include "qabstractaxis.h" |
|
23 | 23 | #include "chartpresenter_p.h" |
|
24 | 24 | #include "qvalueaxis.h" |
|
25 | 25 | #include <QGraphicsLayout> |
|
26 | 26 | #include <QFontMetrics> |
|
27 | 27 | #include <qmath.h> |
|
28 | 28 | |
|
29 | 29 | static int label_padding = 5; |
|
30 | 30 | |
|
31 | 31 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
32 | 32 | |
|
33 | 33 | ChartValueAxisX::ChartValueAxisX(QAbstractAxis *axis,ChartPresenter *presenter) : ChartAxis(axis,presenter), |
|
34 | 34 | m_tickCount(0) |
|
35 | 35 | { |
|
36 | 36 | } |
|
37 | 37 | |
|
38 | 38 | ChartValueAxisX::~ChartValueAxisX() |
|
39 | 39 | { |
|
40 | 40 | } |
|
41 | 41 | |
|
42 | 42 | QVector<qreal> ChartValueAxisX::calculateLayout() const |
|
43 | 43 | { |
|
44 | 44 | Q_ASSERT(m_tickCount>=2); |
|
45 | 45 | |
|
46 | 46 | QVector<qreal> points; |
|
47 | 47 | points.resize(m_tickCount); |
|
48 | 48 | |
|
49 | 49 | const qreal deltaX = m_rect.width()/(m_tickCount-1); |
|
50 | 50 | for (int i = 0; i < m_tickCount; ++i) { |
|
51 | 51 | int x = i * deltaX + m_rect.left(); |
|
52 | 52 | points[i] = x; |
|
53 | 53 | } |
|
54 | 54 | return points; |
|
55 | 55 | } |
|
56 | 56 | |
|
57 | 57 | void ChartValueAxisX::updateGeometry() |
|
58 | 58 | { |
|
59 | 59 | const QVector<qreal>& layout = ChartAxis::layout(); |
|
60 | 60 | |
|
61 | 61 | m_minWidth = 0; |
|
62 | 62 | m_minHeight = 0; |
|
63 | 63 | |
|
64 | 64 | if(layout.isEmpty()) return; |
|
65 | 65 | |
|
66 | 66 | QStringList ticksList; |
|
67 | 67 | |
|
68 | 68 | createNumberLabels(ticksList,m_min,m_max,layout.size()); |
|
69 | 69 | |
|
70 | 70 | QList<QGraphicsItem *> lines = m_grid->childItems(); |
|
71 | 71 | QList<QGraphicsItem *> labels = m_labels->childItems(); |
|
72 | 72 | QList<QGraphicsItem *> shades = m_shades->childItems(); |
|
73 | 73 | QList<QGraphicsItem *> axis = m_arrow->childItems(); |
|
74 | 74 | |
|
75 | 75 | Q_ASSERT(labels.size() == ticksList.size()); |
|
76 | 76 | Q_ASSERT(layout.size() == ticksList.size()); |
|
77 | 77 | |
|
78 | 78 | QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(axis.at(0)); |
|
79 | 79 | lineItem->setLine(m_rect.left(), m_rect.bottom(), m_rect.right(), m_rect.bottom()); |
|
80 | 80 | |
|
81 | 81 | qreal width = 0; |
|
82 | 82 | for (int i = 0; i < layout.size(); ++i) { |
|
83 | 83 | QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i)); |
|
84 | 84 | lineItem->setLine(layout[i], m_rect.top(), layout[i], m_rect.bottom()); |
|
85 | 85 | QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i)); |
|
86 | 86 | labelItem->setText(ticksList.at(i)); |
|
87 | 87 | const QRectF& rect = labelItem->boundingRect(); |
|
88 | 88 | QPointF center = rect.center(); |
|
89 | 89 | labelItem->setTransformOriginPoint(center.x(), center.y()); |
|
90 | 90 | labelItem->setPos(layout[i] - center.x(), m_rect.bottom() + label_padding); |
|
91 | 91 | |
|
92 | 92 | if(labelItem->pos().x()<=width){ |
|
93 | 93 | labelItem->setVisible(false); |
|
94 | 94 | lineItem->setVisible(false); |
|
95 | 95 | }else{ |
|
96 | 96 | labelItem->setVisible(true); |
|
97 | 97 | lineItem->setVisible(true); |
|
98 | 98 | width=rect.width()+labelItem->pos().x(); |
|
99 | 99 | } |
|
100 | 100 | m_minWidth+=rect.width(); |
|
101 | 101 | m_minHeight=qMax(rect.height(),m_minHeight); |
|
102 | 102 | |
|
103 | 103 | if ((i+1)%2 && i>1) { |
|
104 | 104 | QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2-1)); |
|
105 | 105 | rectItem->setRect(layout[i-1],m_rect.top(),layout[i]-layout[i-1],m_rect.height()); |
|
106 | 106 | } |
|
107 | 107 | lineItem = static_cast<QGraphicsLineItem*>(axis.at(i+1)); |
|
108 | 108 | lineItem->setLine(layout[i],m_rect.bottom(),layout[i],m_rect.bottom()+5); |
|
109 | 109 | } |
|
110 | ||
|
110 | 111 | } |
|
111 | 112 | |
|
112 | 113 | void ChartValueAxisX::handleAxisUpdated() |
|
113 | 114 | { |
|
114 | 115 | //TODO:: fix this |
|
115 | 116 | QValueAxis* axis = qobject_cast<QValueAxis*>(m_chartAxis); |
|
116 | 117 | m_tickCount = axis->tickCount(); |
|
117 | 118 | ChartAxis::handleAxisUpdated(); |
|
118 | 119 | } |
|
119 | 120 | |
|
120 | 121 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,126 +1,127 | |||
|
1 | 1 | /**************************************************************************** |
|
2 | 2 | ** |
|
3 | 3 | ** Copyright (C) 2012 Digia Plc |
|
4 | 4 | ** All rights reserved. |
|
5 | 5 | ** For any questions to Digia, please use contact form at http://qt.digia.com |
|
6 | 6 | ** |
|
7 | 7 | ** This file is part of the Qt Commercial Charts Add-on. |
|
8 | 8 | ** |
|
9 | 9 | ** $QT_BEGIN_LICENSE$ |
|
10 | 10 | ** Licensees holding valid Qt Commercial licenses may use this file in |
|
11 | 11 | ** accordance with the Qt Commercial License Agreement provided with the |
|
12 | 12 | ** Software or, alternatively, in accordance with the terms contained in |
|
13 | 13 | ** a written agreement between you and Digia. |
|
14 | 14 | ** |
|
15 | 15 | ** If you have questions regarding the use of this file, please use |
|
16 | 16 | ** contact form at http://qt.digia.com |
|
17 | 17 | ** $QT_END_LICENSE$ |
|
18 | 18 | ** |
|
19 | 19 | ****************************************************************************/ |
|
20 | 20 | |
|
21 | 21 | #include "chartvalueaxisy_p.h" |
|
22 | 22 | #include "qabstractaxis.h" |
|
23 | 23 | #include "chartpresenter_p.h" |
|
24 | 24 | #include "qvalueaxis.h" |
|
25 | 25 | #include <QGraphicsLayout> |
|
26 | 26 | #include <QFontMetrics> |
|
27 | 27 | #include <qmath.h> |
|
28 | 28 | |
|
29 | 29 | static int label_padding = 5; |
|
30 | 30 | |
|
31 | 31 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
32 | 32 | |
|
33 | 33 | ChartValueAxisY::ChartValueAxisY(QAbstractAxis *axis,ChartPresenter *presenter) : ChartAxis(axis,presenter), |
|
34 | 34 | m_tickCount(0) |
|
35 | 35 | { |
|
36 | 36 | } |
|
37 | 37 | |
|
38 | 38 | ChartValueAxisY::~ChartValueAxisY() |
|
39 | 39 | { |
|
40 | 40 | } |
|
41 | 41 | |
|
42 | 42 | QVector<qreal> ChartValueAxisY::calculateLayout() const |
|
43 | 43 | { |
|
44 | 44 | Q_ASSERT(m_tickCount>=2); |
|
45 | 45 | |
|
46 | 46 | QVector<qreal> points; |
|
47 | 47 | points.resize(m_tickCount); |
|
48 | 48 | |
|
49 | 49 | const qreal deltaY = m_rect.height()/(m_tickCount-1); |
|
50 | 50 | for (int i = 0; i < m_tickCount; ++i) { |
|
51 | 51 | int y = i * -deltaY + m_rect.bottom(); |
|
52 | 52 | points[i] = y; |
|
53 | 53 | } |
|
54 | 54 | |
|
55 | 55 | return points; |
|
56 | 56 | } |
|
57 | 57 | |
|
58 | 58 | void ChartValueAxisY::updateGeometry() |
|
59 | 59 | { |
|
60 | 60 | const QVector<qreal> &layout = ChartAxis::layout(); |
|
61 | 61 | m_minWidth = 0; |
|
62 | 62 | m_minHeight = 0; |
|
63 | 63 | |
|
64 | 64 | if(layout.isEmpty()) return; |
|
65 | 65 | |
|
66 | 66 | QStringList ticksList; |
|
67 | 67 | |
|
68 | 68 | createNumberLabels(ticksList,m_min,m_max,layout.size()); |
|
69 | 69 | |
|
70 | 70 | QList<QGraphicsItem *> lines = m_grid->childItems(); |
|
71 | 71 | QList<QGraphicsItem *> labels = m_labels->childItems(); |
|
72 | 72 | QList<QGraphicsItem *> shades = m_shades->childItems(); |
|
73 | 73 | QList<QGraphicsItem *> axis = m_arrow->childItems(); |
|
74 | 74 | |
|
75 | 75 | Q_ASSERT(labels.size() == ticksList.size()); |
|
76 | 76 | Q_ASSERT(layout.size() == ticksList.size()); |
|
77 | 77 | |
|
78 | 78 | qreal height = 2*m_rect.bottom(); |
|
79 | 79 | |
|
80 | 80 | QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(axis.at(0)); |
|
81 | 81 | lineItem->setLine(m_rect.left() , m_rect.top(), m_rect.left(), m_rect.bottom()); |
|
82 | 82 | |
|
83 | 83 | for (int i = 0; i < layout.size(); ++i) { |
|
84 | 84 | QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i)); |
|
85 | 85 | lineItem->setLine(m_rect.left() , layout[i], m_rect.right(), layout[i]); |
|
86 | 86 | QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i)); |
|
87 | 87 | |
|
88 | 88 | labelItem->setText(ticksList.at(i)); |
|
89 | 89 | const QRectF& rect = labelItem->boundingRect(); |
|
90 | 90 | |
|
91 | 91 | QPointF center = rect.center(); |
|
92 | 92 | labelItem->setTransformOriginPoint(center.x(), center.y()); |
|
93 | 93 | labelItem->setPos(m_rect.left() - rect.width() - label_padding , layout[i]-center.y()); |
|
94 | 94 | |
|
95 | 95 | if(labelItem->pos().y()+rect.height()>height) { |
|
96 | 96 | labelItem->setVisible(false); |
|
97 | 97 | lineItem->setVisible(false); |
|
98 | 98 | } |
|
99 | 99 | else { |
|
100 | 100 | labelItem->setVisible(true); |
|
101 | 101 | lineItem->setVisible(true); |
|
102 | 102 | height=labelItem->pos().y(); |
|
103 | 103 | } |
|
104 | 104 | |
|
105 | 105 | m_minWidth=qMax(rect.width()+label_padding,m_minWidth); |
|
106 | 106 | m_minHeight+=rect.height(); |
|
107 | 107 | |
|
108 | 108 | if ((i+1)%2 && i>1) { |
|
109 | 109 | QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2-1)); |
|
110 | 110 | rectItem->setRect(m_rect.left(),layout[i],m_rect.width(),layout[i-1]-layout[i]); |
|
111 | 111 | } |
|
112 | 112 | lineItem = static_cast<QGraphicsLineItem*>(axis.at(i+1)); |
|
113 | 113 | lineItem->setLine(m_rect.left()-5,layout[i],m_rect.left(),layout[i]); |
|
114 | 114 | } |
|
115 | ||
|
115 | 116 | } |
|
116 | 117 | |
|
117 | 118 | void ChartValueAxisY::handleAxisUpdated() |
|
118 | 119 | { |
|
119 | 120 | //TODO:: fix this |
|
120 | 121 | QValueAxis* axis = qobject_cast<QValueAxis*>(m_chartAxis); |
|
121 | 122 | m_tickCount = axis->tickCount(); |
|
122 | 123 | ChartAxis::handleAxisUpdated(); |
|
123 | 124 | } |
|
124 | 125 | |
|
125 | 126 | |
|
126 | 127 | QTCOMMERCIALCHART_END_NAMESPACE |
General Comments 0
You need to be logged in to leave comments.
Login now