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