@@ -1,46 +1,46 | |||||
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 | #ifndef AXISANIMATIONITEM_H_ |
|
21 | #ifndef AXISANIMATIONITEM_H_ | |
22 | #define AXISANIMATIONITEM_H_ |
|
22 | #define AXISANIMATIONITEM_H_ | |
23 |
|
23 | |||
24 |
#include "axis |
|
24 | #include "axis_p.h" | |
25 | #include "chartanimation_p.h" |
|
25 | #include "chartanimation_p.h" | |
26 |
|
26 | |||
27 |
|
27 | |||
28 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
28 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
29 |
|
29 | |||
30 | class AxisAnimation: public ChartAnimation |
|
30 | class AxisAnimation: public ChartAnimation | |
31 | { |
|
31 | { | |
32 | public: |
|
32 | public: | |
33 | AxisAnimation(Axis *axis); |
|
33 | AxisAnimation(Axis *axis); | |
34 | ~AxisAnimation(); |
|
34 | ~AxisAnimation(); | |
35 | protected: |
|
35 | protected: | |
36 |
|
|
36 | QVariant interpolated(const QVariant &from, const QVariant &to, qreal progress ) const; | |
37 |
|
|
37 | void updateCurrentValue(const QVariant &value ); | |
38 | private: |
|
38 | private: | |
39 | Axis *m_axis; |
|
39 | Axis *m_axis; | |
40 | }; |
|
40 | }; | |
41 |
|
41 | |||
42 | QTCOMMERCIALCHART_END_NAMESPACE |
|
42 | QTCOMMERCIALCHART_END_NAMESPACE | |
43 |
|
43 | |||
44 |
|
44 | |||
45 |
|
45 | |||
46 | #endif /* AXISITEM_H_ */ |
|
46 | #endif /* AXISITEM_H_ */ |
@@ -1,465 +1,464 | |||||
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 "axis |
|
21 | #include "axis_p.h" | |
22 | #include "qchartaxis.h" |
|
22 | #include "qchartaxis.h" | |
23 | #include "chartpresenter_p.h" |
|
23 | #include "chartpresenter_p.h" | |
24 | #include "chartanimator_p.h" |
|
24 | #include "chartanimator_p.h" | |
25 | #include <QPainter> |
|
25 | #include <QPainter> | |
26 | #include <QDebug> |
|
|||
27 | #include <cmath> |
|
26 | #include <cmath> | |
28 |
|
27 | |||
29 | static int label_padding = 5; |
|
28 | static int label_padding = 5; | |
30 |
|
29 | |||
31 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
30 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
32 |
|
31 | |||
33 | Axis::Axis(QChartAxis *axis,ChartPresenter *presenter,AxisType type) : Chart(presenter), |
|
32 | Axis::Axis(QChartAxis *axis,ChartPresenter *presenter,AxisType type) : Chart(presenter), | |
34 | m_chartAxis(axis), |
|
33 | m_chartAxis(axis), | |
35 | m_type(type), |
|
34 | m_type(type), | |
36 | m_labelsAngle(0), |
|
35 | m_labelsAngle(0), | |
37 | m_grid(new QGraphicsItemGroup(presenter->rootItem())), |
|
36 | m_grid(new QGraphicsItemGroup(presenter->rootItem())), | |
38 | m_shades(new QGraphicsItemGroup(presenter->rootItem())), |
|
37 | m_shades(new QGraphicsItemGroup(presenter->rootItem())), | |
39 | m_labels(new QGraphicsItemGroup(presenter->rootItem())), |
|
38 | m_labels(new QGraphicsItemGroup(presenter->rootItem())), | |
40 | m_axis(new QGraphicsItemGroup(presenter->rootItem())), |
|
39 | m_axis(new QGraphicsItemGroup(presenter->rootItem())), | |
41 | m_min(0), |
|
40 | m_min(0), | |
42 | m_max(0), |
|
41 | m_max(0), | |
43 | m_ticksCount(0) |
|
42 | m_ticksCount(0) | |
44 | { |
|
43 | { | |
45 | //initial initialization |
|
44 | //initial initialization | |
46 | m_axis->setZValue(ChartPresenter::AxisZValue); |
|
45 | m_axis->setZValue(ChartPresenter::AxisZValue); | |
47 | m_axis->setHandlesChildEvents(false); |
|
46 | m_axis->setHandlesChildEvents(false); | |
48 |
|
47 | |||
49 | m_shades->setZValue(ChartPresenter::ShadesZValue); |
|
48 | m_shades->setZValue(ChartPresenter::ShadesZValue); | |
50 | m_grid->setZValue(ChartPresenter::GridZValue); |
|
49 | m_grid->setZValue(ChartPresenter::GridZValue); | |
51 |
|
50 | |||
52 | connect(m_chartAxis,SIGNAL(updated()),this,SLOT(handleAxisUpdated())); |
|
51 | connect(m_chartAxis,SIGNAL(updated()),this,SLOT(handleAxisUpdated())); | |
53 | connect(m_chartAxis->categories(),SIGNAL(updated()),this,SLOT(handleAxisCategoriesUpdated())); |
|
52 | connect(m_chartAxis->categories(),SIGNAL(updated()),this,SLOT(handleAxisCategoriesUpdated())); | |
54 |
|
53 | |||
55 | handleAxisUpdated(); |
|
54 | handleAxisUpdated(); | |
56 | } |
|
55 | } | |
57 |
|
56 | |||
58 | Axis::~Axis() |
|
57 | Axis::~Axis() | |
59 | { |
|
58 | { | |
60 | } |
|
59 | } | |
61 |
|
60 | |||
62 | void Axis::createItems(int count) |
|
61 | void Axis::createItems(int count) | |
63 | { |
|
62 | { | |
64 |
|
63 | |||
65 | if (m_axis->children().size() == 0) |
|
64 | if (m_axis->children().size() == 0) | |
66 | m_axis->addToGroup(new AxisItem(this)); |
|
65 | m_axis->addToGroup(new AxisItem(this)); | |
67 | for (int i = 0; i < count; ++i) { |
|
66 | for (int i = 0; i < count; ++i) { | |
68 | m_grid->addToGroup(new QGraphicsLineItem()); |
|
67 | m_grid->addToGroup(new QGraphicsLineItem()); | |
69 | m_labels->addToGroup(new QGraphicsSimpleTextItem()); |
|
68 | m_labels->addToGroup(new QGraphicsSimpleTextItem()); | |
70 | m_axis->addToGroup(new QGraphicsLineItem()); |
|
69 | m_axis->addToGroup(new QGraphicsLineItem()); | |
71 | if ((m_grid->childItems().size())%2 && m_grid->childItems().size()>2) m_shades->addToGroup(new QGraphicsRectItem()); |
|
70 | if ((m_grid->childItems().size())%2 && m_grid->childItems().size()>2) m_shades->addToGroup(new QGraphicsRectItem()); | |
72 | } |
|
71 | } | |
73 | } |
|
72 | } | |
74 |
|
73 | |||
75 | void Axis::deleteItems(int count) |
|
74 | void Axis::deleteItems(int count) | |
76 | { |
|
75 | { | |
77 | QList<QGraphicsItem *> lines = m_grid->childItems(); |
|
76 | QList<QGraphicsItem *> lines = m_grid->childItems(); | |
78 | QList<QGraphicsItem *> labels = m_labels->childItems(); |
|
77 | QList<QGraphicsItem *> labels = m_labels->childItems(); | |
79 | QList<QGraphicsItem *> shades = m_shades->childItems(); |
|
78 | QList<QGraphicsItem *> shades = m_shades->childItems(); | |
80 | QList<QGraphicsItem *> axis = m_axis->childItems(); |
|
79 | QList<QGraphicsItem *> axis = m_axis->childItems(); | |
81 |
|
80 | |||
82 | for (int i = 0; i < count; ++i) { |
|
81 | for (int i = 0; i < count; ++i) { | |
83 | if (lines.size()%2 && lines.size() > 1) delete(shades.takeLast()); |
|
82 | if (lines.size()%2 && lines.size() > 1) delete(shades.takeLast()); | |
84 | delete(lines.takeLast()); |
|
83 | delete(lines.takeLast()); | |
85 | delete(labels.takeLast()); |
|
84 | delete(labels.takeLast()); | |
86 | delete(axis.takeLast()); |
|
85 | delete(axis.takeLast()); | |
87 | } |
|
86 | } | |
88 | } |
|
87 | } | |
89 |
|
88 | |||
90 | void Axis::updateLayout(QVector<qreal> &layout) |
|
89 | void Axis::updateLayout(QVector<qreal> &layout) | |
91 | { |
|
90 | { | |
92 | if (animator()) { |
|
91 | if (animator()) { | |
93 | animator()->updateLayout(this,layout); |
|
92 | animator()->updateLayout(this,layout); | |
94 | } else { |
|
93 | } else { | |
95 | setLayout(layout); |
|
94 | setLayout(layout); | |
96 | } |
|
95 | } | |
97 | } |
|
96 | } | |
98 |
|
97 | |||
99 | bool Axis::createLabels(QStringList &labels,qreal min, qreal max,int ticks) const |
|
98 | bool Axis::createLabels(QStringList &labels,qreal min, qreal max,int ticks) const | |
100 | { |
|
99 | { | |
101 | Q_ASSERT(max>=min); |
|
100 | Q_ASSERT(max>=min); | |
102 | Q_ASSERT(ticks>1); |
|
101 | Q_ASSERT(ticks>1); | |
103 |
|
102 | |||
104 | QChartAxisCategories* categories = m_chartAxis->categories(); |
|
103 | QChartAxisCategories* categories = m_chartAxis->categories(); | |
105 |
|
104 | |||
106 | bool category = categories->count()>0; |
|
105 | bool category = categories->count()>0; | |
107 |
|
106 | |||
108 | if (!category) { |
|
107 | if (!category) { | |
109 | int n = qMax(int(-floor(log10((max-min)/(ticks-1)))),0); |
|
108 | int n = qMax(int(-floor(log10((max-min)/(ticks-1)))),0); | |
110 | n++; |
|
109 | n++; | |
111 | for (int i=0; i< ticks; i++) { |
|
110 | for (int i=0; i< ticks; i++) { | |
112 | qreal value = min + (i * (max - min)/ (ticks-1)); |
|
111 | qreal value = min + (i * (max - min)/ (ticks-1)); | |
113 | labels << QString::number(value,'f',n); |
|
112 | labels << QString::number(value,'f',n); | |
114 | } |
|
113 | } | |
115 | } else { |
|
114 | } else { | |
116 | QList<qreal> values = categories->values(); |
|
115 | QList<qreal> values = categories->values(); | |
117 | for (int i=0; i< ticks; i++) { |
|
116 | for (int i=0; i< ticks; i++) { | |
118 | qreal value = (min + (i * (max - min)/ (ticks-1))); |
|
117 | qreal value = (min + (i * (max - min)/ (ticks-1))); | |
119 | int j=0; |
|
118 | int j=0; | |
120 | for (; j<values.count(); j++) { |
|
119 | for (; j<values.count(); j++) { | |
121 | if (values.at(j) > value) break; |
|
120 | if (values.at(j) > value) break; | |
122 | } |
|
121 | } | |
123 | if (j!=0) value=values.at(j-1); |
|
122 | if (j!=0) value=values.at(j-1); | |
124 |
|
123 | |||
125 | QString label = categories->label(value); |
|
124 | QString label = categories->label(value); | |
126 | labels << label; |
|
125 | labels << label; | |
127 | } |
|
126 | } | |
128 | } |
|
127 | } | |
129 |
|
128 | |||
130 | return category; |
|
129 | return category; | |
131 | } |
|
130 | } | |
132 |
|
131 | |||
133 | void Axis::setAxisOpacity(qreal opacity) |
|
132 | void Axis::setAxisOpacity(qreal opacity) | |
134 | { |
|
133 | { | |
135 | m_axis->setOpacity(opacity); |
|
134 | m_axis->setOpacity(opacity); | |
136 | } |
|
135 | } | |
137 |
|
136 | |||
138 | qreal Axis::axisOpacity() const |
|
137 | qreal Axis::axisOpacity() const | |
139 | { |
|
138 | { | |
140 | return m_axis->opacity(); |
|
139 | return m_axis->opacity(); | |
141 | } |
|
140 | } | |
142 |
|
141 | |||
143 | void Axis::setGridOpacity(qreal opacity) |
|
142 | void Axis::setGridOpacity(qreal opacity) | |
144 | { |
|
143 | { | |
145 | m_grid->setOpacity(opacity); |
|
144 | m_grid->setOpacity(opacity); | |
146 | } |
|
145 | } | |
147 |
|
146 | |||
148 | qreal Axis::gridOpacity() const |
|
147 | qreal Axis::gridOpacity() const | |
149 | { |
|
148 | { | |
150 | return m_grid->opacity(); |
|
149 | return m_grid->opacity(); | |
151 | } |
|
150 | } | |
152 |
|
151 | |||
153 | void Axis::setLabelsOpacity(qreal opacity) |
|
152 | void Axis::setLabelsOpacity(qreal opacity) | |
154 | { |
|
153 | { | |
155 | m_labels->setOpacity(opacity); |
|
154 | m_labels->setOpacity(opacity); | |
156 | } |
|
155 | } | |
157 |
|
156 | |||
158 | qreal Axis::labelsOpacity() const |
|
157 | qreal Axis::labelsOpacity() const | |
159 | { |
|
158 | { | |
160 | return m_labels->opacity(); |
|
159 | return m_labels->opacity(); | |
161 | } |
|
160 | } | |
162 |
|
161 | |||
163 | void Axis::setShadesOpacity(qreal opacity) |
|
162 | void Axis::setShadesOpacity(qreal opacity) | |
164 | { |
|
163 | { | |
165 | m_shades->setOpacity(opacity); |
|
164 | m_shades->setOpacity(opacity); | |
166 | } |
|
165 | } | |
167 |
|
166 | |||
168 | qreal Axis::shadesOpacity() const |
|
167 | qreal Axis::shadesOpacity() const | |
169 | { |
|
168 | { | |
170 | return m_shades->opacity(); |
|
169 | return m_shades->opacity(); | |
171 | } |
|
170 | } | |
172 |
|
171 | |||
173 | void Axis::setLabelsAngle(int angle) |
|
172 | void Axis::setLabelsAngle(int angle) | |
174 | { |
|
173 | { | |
175 | foreach(QGraphicsItem* item , m_labels->childItems()) { |
|
174 | foreach(QGraphicsItem* item , m_labels->childItems()) { | |
176 | item->setRotation(angle); |
|
175 | item->setRotation(angle); | |
177 | } |
|
176 | } | |
178 |
|
177 | |||
179 | m_labelsAngle=angle; |
|
178 | m_labelsAngle=angle; | |
180 | } |
|
179 | } | |
181 |
|
180 | |||
182 | void Axis::setLabelsPen(const QPen &pen) |
|
181 | void Axis::setLabelsPen(const QPen &pen) | |
183 | { |
|
182 | { | |
184 | foreach(QGraphicsItem* item , m_labels->childItems()) { |
|
183 | foreach(QGraphicsItem* item , m_labels->childItems()) { | |
185 | static_cast<QGraphicsSimpleTextItem*>(item)->setPen(pen); |
|
184 | static_cast<QGraphicsSimpleTextItem*>(item)->setPen(pen); | |
186 | } |
|
185 | } | |
187 | } |
|
186 | } | |
188 |
|
187 | |||
189 | void Axis::setLabelsBrush(const QBrush &brush) |
|
188 | void Axis::setLabelsBrush(const QBrush &brush) | |
190 | { |
|
189 | { | |
191 | foreach(QGraphicsItem* item , m_labels->childItems()) { |
|
190 | foreach(QGraphicsItem* item , m_labels->childItems()) { | |
192 | static_cast<QGraphicsSimpleTextItem*>(item)->setBrush(brush); |
|
191 | static_cast<QGraphicsSimpleTextItem*>(item)->setBrush(brush); | |
193 | } |
|
192 | } | |
194 | } |
|
193 | } | |
195 |
|
194 | |||
196 | void Axis::setLabelsFont(const QFont &font) |
|
195 | void Axis::setLabelsFont(const QFont &font) | |
197 | { |
|
196 | { | |
198 | foreach(QGraphicsItem* item , m_labels->childItems()) { |
|
197 | foreach(QGraphicsItem* item , m_labels->childItems()) { | |
199 | static_cast<QGraphicsSimpleTextItem*>(item)->setFont(font); |
|
198 | static_cast<QGraphicsSimpleTextItem*>(item)->setFont(font); | |
200 | } |
|
199 | } | |
201 | } |
|
200 | } | |
202 |
|
201 | |||
203 | void Axis::setShadesBrush(const QBrush &brush) |
|
202 | void Axis::setShadesBrush(const QBrush &brush) | |
204 | { |
|
203 | { | |
205 | foreach(QGraphicsItem* item , m_shades->childItems()) { |
|
204 | foreach(QGraphicsItem* item , m_shades->childItems()) { | |
206 | static_cast<QGraphicsRectItem*>(item)->setBrush(brush); |
|
205 | static_cast<QGraphicsRectItem*>(item)->setBrush(brush); | |
207 | } |
|
206 | } | |
208 | } |
|
207 | } | |
209 |
|
208 | |||
210 | void Axis::setShadesPen(const QPen &pen) |
|
209 | void Axis::setShadesPen(const QPen &pen) | |
211 | { |
|
210 | { | |
212 | foreach(QGraphicsItem* item , m_shades->childItems()) { |
|
211 | foreach(QGraphicsItem* item , m_shades->childItems()) { | |
213 | static_cast<QGraphicsRectItem*>(item)->setPen(pen); |
|
212 | static_cast<QGraphicsRectItem*>(item)->setPen(pen); | |
214 | } |
|
213 | } | |
215 | } |
|
214 | } | |
216 |
|
215 | |||
217 | void Axis::setAxisPen(const QPen &pen) |
|
216 | void Axis::setAxisPen(const QPen &pen) | |
218 | { |
|
217 | { | |
219 | foreach(QGraphicsItem* item , m_axis->childItems()) { |
|
218 | foreach(QGraphicsItem* item , m_axis->childItems()) { | |
220 | static_cast<QGraphicsLineItem*>(item)->setPen(pen); |
|
219 | static_cast<QGraphicsLineItem*>(item)->setPen(pen); | |
221 | } |
|
220 | } | |
222 | } |
|
221 | } | |
223 |
|
222 | |||
224 | void Axis::setGridPen(const QPen &pen) |
|
223 | void Axis::setGridPen(const QPen &pen) | |
225 | { |
|
224 | { | |
226 | foreach(QGraphicsItem* item , m_grid->childItems()) { |
|
225 | foreach(QGraphicsItem* item , m_grid->childItems()) { | |
227 | static_cast<QGraphicsLineItem*>(item)->setPen(pen); |
|
226 | static_cast<QGraphicsLineItem*>(item)->setPen(pen); | |
228 | } |
|
227 | } | |
229 | } |
|
228 | } | |
230 |
|
229 | |||
231 | QVector<qreal> Axis::calculateLayout() const |
|
230 | QVector<qreal> Axis::calculateLayout() const | |
232 | { |
|
231 | { | |
233 | Q_ASSERT(m_ticksCount>=2); |
|
232 | Q_ASSERT(m_ticksCount>=2); | |
234 |
|
233 | |||
235 | QVector<qreal> points; |
|
234 | QVector<qreal> points; | |
236 | points.resize(m_ticksCount); |
|
235 | points.resize(m_ticksCount); | |
237 |
|
236 | |||
238 | switch (m_type) |
|
237 | switch (m_type) | |
239 | { |
|
238 | { | |
240 | case X_AXIS: |
|
239 | case X_AXIS: | |
241 | { |
|
240 | { | |
242 | const qreal deltaX = m_rect.width()/(m_ticksCount-1); |
|
241 | const qreal deltaX = m_rect.width()/(m_ticksCount-1); | |
243 | for (int i = 0; i < m_ticksCount; ++i) { |
|
242 | for (int i = 0; i < m_ticksCount; ++i) { | |
244 | int x = i * deltaX + m_rect.left(); |
|
243 | int x = i * deltaX + m_rect.left(); | |
245 | points[i] = x; |
|
244 | points[i] = x; | |
246 | } |
|
245 | } | |
247 | } |
|
246 | } | |
248 | break; |
|
247 | break; | |
249 | case Y_AXIS: |
|
248 | case Y_AXIS: | |
250 | { |
|
249 | { | |
251 | const qreal deltaY = m_rect.height()/(m_ticksCount-1); |
|
250 | const qreal deltaY = m_rect.height()/(m_ticksCount-1); | |
252 | for (int i = 0; i < m_ticksCount; ++i) { |
|
251 | for (int i = 0; i < m_ticksCount; ++i) { | |
253 | int y = i * -deltaY + m_rect.bottom(); |
|
252 | int y = i * -deltaY + m_rect.bottom(); | |
254 | points[i] = y; |
|
253 | points[i] = y; | |
255 | } |
|
254 | } | |
256 | } |
|
255 | } | |
257 | break; |
|
256 | break; | |
258 | } |
|
257 | } | |
259 | return points; |
|
258 | return points; | |
260 | } |
|
259 | } | |
261 |
|
260 | |||
262 | void Axis::setLayout(QVector<qreal> &layout) |
|
261 | void Axis::setLayout(QVector<qreal> &layout) | |
263 | { |
|
262 | { | |
264 | int diff = m_layoutVector.size() - layout.size(); |
|
263 | int diff = m_layoutVector.size() - layout.size(); | |
265 |
|
264 | |||
266 | if (diff>0) { |
|
265 | if (diff>0) { | |
267 | deleteItems(diff); |
|
266 | deleteItems(diff); | |
268 | } else if (diff<0) { |
|
267 | } else if (diff<0) { | |
269 | createItems(-diff); |
|
268 | createItems(-diff); | |
270 | } |
|
269 | } | |
271 |
|
270 | |||
272 | if( diff!=0) handleAxisUpdated(); |
|
271 | if( diff!=0) handleAxisUpdated(); | |
273 |
|
272 | |||
274 | QStringList ticksList; |
|
273 | QStringList ticksList; | |
275 |
|
274 | |||
276 | bool categories = createLabels(ticksList,m_min,m_max,layout.size()); |
|
275 | bool categories = createLabels(ticksList,m_min,m_max,layout.size()); | |
277 |
|
276 | |||
278 | QList<QGraphicsItem *> lines = m_grid->childItems(); |
|
277 | QList<QGraphicsItem *> lines = m_grid->childItems(); | |
279 | QList<QGraphicsItem *> labels = m_labels->childItems(); |
|
278 | QList<QGraphicsItem *> labels = m_labels->childItems(); | |
280 | QList<QGraphicsItem *> shades = m_shades->childItems(); |
|
279 | QList<QGraphicsItem *> shades = m_shades->childItems(); | |
281 | QList<QGraphicsItem *> axis = m_axis->childItems(); |
|
280 | QList<QGraphicsItem *> axis = m_axis->childItems(); | |
282 |
|
281 | |||
283 | Q_ASSERT(labels.size() == ticksList.size()); |
|
282 | Q_ASSERT(labels.size() == ticksList.size()); | |
284 | Q_ASSERT(layout.size() == ticksList.size()); |
|
283 | Q_ASSERT(layout.size() == ticksList.size()); | |
285 |
|
284 | |||
286 | qreal minWidth = 0; |
|
285 | qreal minWidth = 0; | |
287 | qreal minHeight = 0; |
|
286 | qreal minHeight = 0; | |
288 |
|
287 | |||
289 | switch (m_type) |
|
288 | switch (m_type) | |
290 | { |
|
289 | { | |
291 | case X_AXIS: |
|
290 | case X_AXIS: | |
292 | { |
|
291 | { | |
293 | QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(axis.at(0)); |
|
292 | QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(axis.at(0)); | |
294 | lineItem->setLine(m_rect.left(), m_rect.bottom(), m_rect.right(), m_rect.bottom()); |
|
293 | lineItem->setLine(m_rect.left(), m_rect.bottom(), m_rect.right(), m_rect.bottom()); | |
295 |
|
294 | |||
296 | for (int i = 0; i < layout.size(); ++i) { |
|
295 | for (int i = 0; i < layout.size(); ++i) { | |
297 | QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i)); |
|
296 | QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i)); | |
298 | lineItem->setLine(layout[i], m_rect.top(), layout[i], m_rect.bottom()); |
|
297 | lineItem->setLine(layout[i], m_rect.top(), layout[i], m_rect.bottom()); | |
299 | QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i)); |
|
298 | QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i)); | |
300 | if (!categories || i<1) { |
|
299 | if (!categories || i<1) { | |
301 | labelItem->setText(ticksList.at(i)); |
|
300 | labelItem->setText(ticksList.at(i)); | |
302 | const QRectF& rect = labelItem->boundingRect(); |
|
301 | const QRectF& rect = labelItem->boundingRect(); | |
303 | minWidth+=rect.width(); |
|
302 | minWidth+=rect.width(); | |
304 | minHeight=qMax(rect.height(),minHeight); |
|
303 | minHeight=qMax(rect.height(),minHeight); | |
305 | QPointF center = rect.center(); |
|
304 | QPointF center = rect.center(); | |
306 | labelItem->setTransformOriginPoint(center.x(), center.y()); |
|
305 | labelItem->setTransformOriginPoint(center.x(), center.y()); | |
307 | labelItem->setPos(layout[i] - center.x(), m_rect.bottom() + label_padding); |
|
306 | labelItem->setPos(layout[i] - center.x(), m_rect.bottom() + label_padding); | |
308 | } else { |
|
307 | } else { | |
309 | labelItem->setText(ticksList.at(i)); |
|
308 | labelItem->setText(ticksList.at(i)); | |
310 | const QRectF& rect = labelItem->boundingRect(); |
|
309 | const QRectF& rect = labelItem->boundingRect(); | |
311 | minWidth+=rect.width(); |
|
310 | minWidth+=rect.width(); | |
312 | minHeight=qMax(rect.height()+label_padding,minHeight); |
|
311 | minHeight=qMax(rect.height()+label_padding,minHeight); | |
313 | QPointF center = rect.center(); |
|
312 | QPointF center = rect.center(); | |
314 | labelItem->setTransformOriginPoint(center.x(), center.y()); |
|
313 | labelItem->setTransformOriginPoint(center.x(), center.y()); | |
315 | labelItem->setPos(layout[i] - (layout[i] - layout[i-1])/2 - center.x(), m_rect.bottom() + label_padding); |
|
314 | labelItem->setPos(layout[i] - (layout[i] - layout[i-1])/2 - center.x(), m_rect.bottom() + label_padding); | |
316 | } |
|
315 | } | |
317 |
|
316 | |||
318 | if ((i+1)%2 && i>1) { |
|
317 | if ((i+1)%2 && i>1) { | |
319 | QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2-1)); |
|
318 | QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2-1)); | |
320 | rectItem->setRect(layout[i-1],m_rect.top(),layout[i]-layout[i-1],m_rect.height()); |
|
319 | rectItem->setRect(layout[i-1],m_rect.top(),layout[i]-layout[i-1],m_rect.height()); | |
321 | } |
|
320 | } | |
322 | lineItem = static_cast<QGraphicsLineItem*>(axis.at(i+1)); |
|
321 | lineItem = static_cast<QGraphicsLineItem*>(axis.at(i+1)); | |
323 | lineItem->setLine(layout[i],m_rect.bottom(),layout[i],m_rect.bottom()+5); |
|
322 | lineItem->setLine(layout[i],m_rect.bottom(),layout[i],m_rect.bottom()+5); | |
324 | } |
|
323 | } | |
325 |
|
324 | |||
326 | } |
|
325 | } | |
327 | break; |
|
326 | break; | |
328 |
|
327 | |||
329 | case Y_AXIS: |
|
328 | case Y_AXIS: | |
330 | { |
|
329 | { | |
331 | QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(axis.at(0)); |
|
330 | QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(axis.at(0)); | |
332 | lineItem->setLine(m_rect.left() , m_rect.top(), m_rect.left(), m_rect.bottom()); |
|
331 | lineItem->setLine(m_rect.left() , m_rect.top(), m_rect.left(), m_rect.bottom()); | |
333 |
|
332 | |||
334 | for (int i = 0; i < layout.size(); ++i) { |
|
333 | for (int i = 0; i < layout.size(); ++i) { | |
335 | QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i)); |
|
334 | QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i)); | |
336 | lineItem->setLine(m_rect.left() , layout[i], m_rect.right(), layout[i]); |
|
335 | lineItem->setLine(m_rect.left() , layout[i], m_rect.right(), layout[i]); | |
337 | QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i)); |
|
336 | QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i)); | |
338 |
|
337 | |||
339 | if (!categories || i<1) { |
|
338 | if (!categories || i<1) { | |
340 | labelItem->setText(ticksList.at(i)); |
|
339 | labelItem->setText(ticksList.at(i)); | |
341 | const QRectF& rect = labelItem->boundingRect(); |
|
340 | const QRectF& rect = labelItem->boundingRect(); | |
342 | minWidth=qMax(rect.width()+label_padding,minWidth); |
|
341 | minWidth=qMax(rect.width()+label_padding,minWidth); | |
343 | minHeight+=rect.height(); |
|
342 | minHeight+=rect.height(); | |
344 | QPointF center = rect.center(); |
|
343 | QPointF center = rect.center(); | |
345 | labelItem->setTransformOriginPoint(center.x(), center.y()); |
|
344 | labelItem->setTransformOriginPoint(center.x(), center.y()); | |
346 | labelItem->setPos(m_rect.left() - rect.width() - label_padding , layout[i]-center.y()); |
|
345 | labelItem->setPos(m_rect.left() - rect.width() - label_padding , layout[i]-center.y()); | |
347 | } else { |
|
346 | } else { | |
348 | labelItem->setText(ticksList.at(i)); |
|
347 | labelItem->setText(ticksList.at(i)); | |
349 | const QRectF& rect = labelItem->boundingRect(); |
|
348 | const QRectF& rect = labelItem->boundingRect(); | |
350 | minWidth=qMax(rect.width(),minWidth); |
|
349 | minWidth=qMax(rect.width(),minWidth); | |
351 | minHeight+=rect.height(); |
|
350 | minHeight+=rect.height(); | |
352 | QPointF center = rect.center(); |
|
351 | QPointF center = rect.center(); | |
353 | labelItem->setTransformOriginPoint(center.x(), center.y()); |
|
352 | labelItem->setTransformOriginPoint(center.x(), center.y()); | |
354 | labelItem->setPos(m_rect.left() - rect.width() - label_padding , layout[i] - (layout[i] - layout[i-1])/2 -center.y()); |
|
353 | labelItem->setPos(m_rect.left() - rect.width() - label_padding , layout[i] - (layout[i] - layout[i-1])/2 -center.y()); | |
355 | } |
|
354 | } | |
356 |
|
355 | |||
357 | if ((i+1)%2 && i>1) { |
|
356 | if ((i+1)%2 && i>1) { | |
358 | QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2-1)); |
|
357 | QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2-1)); | |
359 | rectItem->setRect(m_rect.left(),layout[i],m_rect.width(),layout[i-1]-layout[i]); |
|
358 | rectItem->setRect(m_rect.left(),layout[i],m_rect.width(),layout[i-1]-layout[i]); | |
360 | } |
|
359 | } | |
361 | lineItem = static_cast<QGraphicsLineItem*>(axis.at(i+1)); |
|
360 | lineItem = static_cast<QGraphicsLineItem*>(axis.at(i+1)); | |
362 | lineItem->setLine(m_rect.left()-5,layout[i],m_rect.left(),layout[i]); |
|
361 | lineItem->setLine(m_rect.left()-5,layout[i],m_rect.left(),layout[i]); | |
363 | } |
|
362 | } | |
364 | } |
|
363 | } | |
365 | break; |
|
364 | break; | |
366 | default: |
|
365 | default: | |
367 | qDebug()<<"Unknown axis type"; |
|
366 | qDebug()<<"Unknown axis type"; | |
368 | break; |
|
367 | break; | |
369 | } |
|
368 | } | |
370 |
|
369 | |||
371 | m_layoutVector=layout; |
|
370 | m_layoutVector=layout; | |
372 |
|
371 | |||
373 | presenter()->setMinimumMarginWidth(this,minWidth); |
|
372 | presenter()->setMinimumMarginWidth(this,minWidth); | |
374 | presenter()->setMinimumMarginHeight(this,minHeight); |
|
373 | presenter()->setMinimumMarginHeight(this,minHeight); | |
375 |
|
374 | |||
376 | } |
|
375 | } | |
377 |
|
376 | |||
378 | bool Axis::isEmpty() |
|
377 | bool Axis::isEmpty() | |
379 | { |
|
378 | { | |
380 | return m_rect.isEmpty() || qFuzzyIsNull(m_min - m_max) || m_ticksCount==0; |
|
379 | return m_rect.isEmpty() || qFuzzyIsNull(m_min - m_max) || m_ticksCount==0; | |
381 | } |
|
380 | } | |
382 |
|
381 | |||
383 | //handlers |
|
382 | //handlers | |
384 |
|
383 | |||
385 | void Axis::handleAxisCategoriesUpdated() |
|
384 | void Axis::handleAxisCategoriesUpdated() | |
386 | { |
|
385 | { | |
387 | if (isEmpty()) return; |
|
386 | if (isEmpty()) return; | |
388 | updateLayout(m_layoutVector); |
|
387 | updateLayout(m_layoutVector); | |
389 | } |
|
388 | } | |
390 |
|
389 | |||
391 | void Axis::handleAxisUpdated() |
|
390 | void Axis::handleAxisUpdated() | |
392 | { |
|
391 | { | |
393 |
|
392 | |||
394 | if (isEmpty()) return; |
|
393 | if (isEmpty()) return; | |
395 |
|
394 | |||
396 | if (m_chartAxis->isAxisVisible()) { |
|
395 | if (m_chartAxis->isAxisVisible()) { | |
397 | setAxisOpacity(100); |
|
396 | setAxisOpacity(100); | |
398 | } else { |
|
397 | } else { | |
399 | setAxisOpacity(0); |
|
398 | setAxisOpacity(0); | |
400 | } |
|
399 | } | |
401 |
|
400 | |||
402 | if (m_chartAxis->isGridLineVisible()) { |
|
401 | if (m_chartAxis->isGridLineVisible()) { | |
403 | setGridOpacity(100); |
|
402 | setGridOpacity(100); | |
404 | } else { |
|
403 | } else { | |
405 | setGridOpacity(0); |
|
404 | setGridOpacity(0); | |
406 | } |
|
405 | } | |
407 |
|
406 | |||
408 | if (m_chartAxis->labelsVisible()) { |
|
407 | if (m_chartAxis->labelsVisible()) { | |
409 | setLabelsOpacity(100); |
|
408 | setLabelsOpacity(100); | |
410 | } else { |
|
409 | } else { | |
411 | setLabelsOpacity(0); |
|
410 | setLabelsOpacity(0); | |
412 | } |
|
411 | } | |
413 |
|
412 | |||
414 | if (m_chartAxis->shadesVisible()) { |
|
413 | if (m_chartAxis->shadesVisible()) { | |
415 | setShadesOpacity(m_chartAxis->shadesOpacity()); |
|
414 | setShadesOpacity(m_chartAxis->shadesOpacity()); | |
416 | } else { |
|
415 | } else { | |
417 | setShadesOpacity(0); |
|
416 | setShadesOpacity(0); | |
418 | } |
|
417 | } | |
419 |
|
418 | |||
420 | setLabelsAngle(m_chartAxis->labelsAngle()); |
|
419 | setLabelsAngle(m_chartAxis->labelsAngle()); | |
421 | setAxisPen(m_chartAxis->axisPen()); |
|
420 | setAxisPen(m_chartAxis->axisPen()); | |
422 | setLabelsPen(m_chartAxis->labelsPen()); |
|
421 | setLabelsPen(m_chartAxis->labelsPen()); | |
423 | setLabelsBrush(m_chartAxis->labelsBrush()); |
|
422 | setLabelsBrush(m_chartAxis->labelsBrush()); | |
424 | setLabelsFont(m_chartAxis->labelsFont()); |
|
423 | setLabelsFont(m_chartAxis->labelsFont()); | |
425 | setGridPen(m_chartAxis->gridLinePen()); |
|
424 | setGridPen(m_chartAxis->gridLinePen()); | |
426 | setShadesPen(m_chartAxis->shadesPen()); |
|
425 | setShadesPen(m_chartAxis->shadesPen()); | |
427 | setShadesBrush(m_chartAxis->shadesBrush()); |
|
426 | setShadesBrush(m_chartAxis->shadesBrush()); | |
428 |
|
427 | |||
429 | } |
|
428 | } | |
430 |
|
429 | |||
431 | void Axis::handleRangeChanged(qreal min, qreal max,int tickCount) |
|
430 | void Axis::handleRangeChanged(qreal min, qreal max,int tickCount) | |
432 | { |
|
431 | { | |
433 | if (qFuzzyIsNull(min - max) || tickCount < 2) |
|
432 | if (qFuzzyIsNull(min - max) || tickCount < 2) | |
434 | return; |
|
433 | return; | |
435 |
|
434 | |||
436 | m_min = min; |
|
435 | m_min = min; | |
437 | m_max = max; |
|
436 | m_max = max; | |
438 | m_ticksCount= tickCount; |
|
437 | m_ticksCount= tickCount; | |
439 |
|
438 | |||
440 | if (isEmpty()) return; |
|
439 | if (isEmpty()) return; | |
441 | QVector<qreal> layout = calculateLayout(); |
|
440 | QVector<qreal> layout = calculateLayout(); | |
442 | updateLayout(layout); |
|
441 | updateLayout(layout); | |
443 |
|
442 | |||
444 | } |
|
443 | } | |
445 |
|
444 | |||
446 | void Axis::handleGeometryChanged(const QRectF &rect) |
|
445 | void Axis::handleGeometryChanged(const QRectF &rect) | |
447 | { |
|
446 | { | |
448 | if(m_rect != rect) |
|
447 | if(m_rect != rect) | |
449 | { |
|
448 | { | |
450 | m_rect = rect; |
|
449 | m_rect = rect; | |
451 | if (isEmpty()) return; |
|
450 | if (isEmpty()) return; | |
452 | QVector<qreal> layout = calculateLayout(); |
|
451 | QVector<qreal> layout = calculateLayout(); | |
453 | updateLayout(layout); |
|
452 | updateLayout(layout); | |
454 | } |
|
453 | } | |
455 | } |
|
454 | } | |
456 |
|
455 | |||
457 | void Axis::axisSelected() |
|
456 | void Axis::axisSelected() | |
458 | { |
|
457 | { | |
459 | qDebug()<<"TODO axis clicked"; |
|
458 | qDebug()<<"TODO axis clicked"; | |
460 | } |
|
459 | } | |
461 |
|
460 | |||
462 | //TODO "nice numbers algorithm" |
|
461 | //TODO "nice numbers algorithm" | |
463 |
#include "moc_axis |
|
462 | #include "moc_axis_p.cpp" | |
464 |
|
463 | |||
465 | QTCOMMERCIALCHART_END_NAMESPACE |
|
464 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,14 +1,14 | |||||
1 | INCLUDEPATH += $$PWD |
|
1 | INCLUDEPATH += $$PWD | |
2 | DEPENDPATH += $$PWD |
|
2 | DEPENDPATH += $$PWD | |
3 |
|
3 | |||
4 | SOURCES += \ |
|
4 | SOURCES += \ | |
5 |
$$PWD/axis |
|
5 | $$PWD/axis.cpp \ | |
6 | $$PWD/qchartaxis.cpp \ |
|
6 | $$PWD/qchartaxis.cpp \ | |
7 | $$PWD/qchartaxiscategories.cpp |
|
7 | $$PWD/qchartaxiscategories.cpp | |
8 |
|
8 | |||
9 | PRIVATE_HEADERS += \ |
|
9 | PRIVATE_HEADERS += \ | |
10 |
$$PWD/axis |
|
10 | $$PWD/axis_p.h | |
11 |
|
11 | |||
12 | PUBLIC_HEADERS += \ |
|
12 | PUBLIC_HEADERS += \ | |
13 | $$PWD/qchartaxis.h \ |
|
13 | $$PWD/qchartaxis.h \ | |
14 | $$PWD/qchartaxiscategories.h No newline at end of file |
|
14 | $$PWD/qchartaxiscategories.h |
1 | NO CONTENT: file renamed from src/axis/axisitem_p.h to src/axis/axis_p.h |
|
NO CONTENT: file renamed from src/axis/axisitem_p.h to src/axis/axis_p.h |
@@ -1,402 +1,402 | |||||
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 "qchart.h" |
|
21 | #include "qchart.h" | |
22 | #include "qchart_p.h" |
|
22 | #include "qchart_p.h" | |
23 | #include "qchartaxis.h" |
|
23 | #include "qchartaxis.h" | |
24 | #include "chartpresenter_p.h" |
|
24 | #include "chartpresenter_p.h" | |
25 | #include "chartdataset_p.h" |
|
25 | #include "chartdataset_p.h" | |
26 | #include "charttheme_p.h" |
|
26 | #include "charttheme_p.h" | |
27 | #include "chartanimator_p.h" |
|
27 | #include "chartanimator_p.h" | |
28 | #include "qseries_p.h" |
|
28 | #include "qseries_p.h" | |
29 | #include "qareaseries.h" |
|
29 | #include "qareaseries.h" | |
30 |
#include "axis |
|
30 | #include "axis_p.h" | |
31 | #include "areachartitem_p.h" |
|
31 | #include "areachartitem_p.h" | |
32 |
|
32 | |||
33 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
33 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
34 |
|
34 | |||
35 | ChartPresenter::ChartPresenter(QChart* chart,ChartDataSet* dataset):QObject(chart), |
|
35 | ChartPresenter::ChartPresenter(QChart* chart,ChartDataSet* dataset):QObject(chart), | |
36 | m_chart(chart), |
|
36 | m_chart(chart), | |
37 | m_animator(0), |
|
37 | m_animator(0), | |
38 | m_dataset(dataset), |
|
38 | m_dataset(dataset), | |
39 | m_chartTheme(0), |
|
39 | m_chartTheme(0), | |
40 | m_chartRect(QRectF(QPoint(0,0),m_chart->size())), |
|
40 | m_chartRect(QRectF(QPoint(0,0),m_chart->size())), | |
41 | m_options(QChart::NoAnimation), |
|
41 | m_options(QChart::NoAnimation), | |
42 | m_minLeftMargin(0), |
|
42 | m_minLeftMargin(0), | |
43 | m_minBottomMargin(0), |
|
43 | m_minBottomMargin(0), | |
44 | m_backgroundItem(0), |
|
44 | m_backgroundItem(0), | |
45 | m_titleItem(0), |
|
45 | m_titleItem(0), | |
46 | m_marginBig(60), |
|
46 | m_marginBig(60), | |
47 | m_marginSmall(20), |
|
47 | m_marginSmall(20), | |
48 | m_marginTiny(10), |
|
48 | m_marginTiny(10), | |
49 | m_chartMargins(QRect(m_marginBig,m_marginBig,0,0)) |
|
49 | m_chartMargins(QRect(m_marginBig,m_marginBig,0,0)) | |
50 | { |
|
50 | { | |
51 | } |
|
51 | } | |
52 |
|
52 | |||
53 | ChartPresenter::~ChartPresenter() |
|
53 | ChartPresenter::~ChartPresenter() | |
54 | { |
|
54 | { | |
55 | delete m_chartTheme; |
|
55 | delete m_chartTheme; | |
56 | } |
|
56 | } | |
57 |
|
57 | |||
58 | void ChartPresenter::setGeometry(const QRectF& rect) |
|
58 | void ChartPresenter::setGeometry(const QRectF& rect) | |
59 | { |
|
59 | { | |
60 | m_rect = rect; |
|
60 | m_rect = rect; | |
61 | Q_ASSERT(m_rect.isValid()); |
|
61 | Q_ASSERT(m_rect.isValid()); | |
62 | updateLayout(); |
|
62 | updateLayout(); | |
63 | } |
|
63 | } | |
64 |
|
64 | |||
65 | void ChartPresenter::setMinimumMarginWidth(Axis* axis, qreal width) |
|
65 | void ChartPresenter::setMinimumMarginWidth(Axis* axis, qreal width) | |
66 | { |
|
66 | { | |
67 | switch(axis->axisType()){ |
|
67 | switch(axis->axisType()){ | |
68 | case Axis::X_AXIS: |
|
68 | case Axis::X_AXIS: | |
69 | { |
|
69 | { | |
70 | if(width>m_chartRect.width()+ m_chartMargins.left()) { |
|
70 | if(width>m_chartRect.width()+ m_chartMargins.left()) { | |
71 | m_minLeftMargin= width - m_chartRect.width(); |
|
71 | m_minLeftMargin= width - m_chartRect.width(); | |
72 | updateLayout(); |
|
72 | updateLayout(); | |
73 | } |
|
73 | } | |
74 | break; |
|
74 | break; | |
75 | } |
|
75 | } | |
76 | case Axis::Y_AXIS: |
|
76 | case Axis::Y_AXIS: | |
77 | { |
|
77 | { | |
78 |
|
78 | |||
79 | if(m_minLeftMargin!=width){ |
|
79 | if(m_minLeftMargin!=width){ | |
80 | m_minLeftMargin= width; |
|
80 | m_minLeftMargin= width; | |
81 | updateLayout(); |
|
81 | updateLayout(); | |
82 | } |
|
82 | } | |
83 | break; |
|
83 | break; | |
84 | } |
|
84 | } | |
85 |
|
85 | |||
86 | } |
|
86 | } | |
87 | } |
|
87 | } | |
88 |
|
88 | |||
89 | void ChartPresenter::setMinimumMarginHeight(Axis* axis, qreal height) |
|
89 | void ChartPresenter::setMinimumMarginHeight(Axis* axis, qreal height) | |
90 | { |
|
90 | { | |
91 | switch(axis->axisType()){ |
|
91 | switch(axis->axisType()){ | |
92 | case Axis::X_AXIS: |
|
92 | case Axis::X_AXIS: | |
93 | { |
|
93 | { | |
94 | if(m_minBottomMargin!=height) { |
|
94 | if(m_minBottomMargin!=height) { | |
95 | m_minBottomMargin= height; |
|
95 | m_minBottomMargin= height; | |
96 | updateLayout(); |
|
96 | updateLayout(); | |
97 | } |
|
97 | } | |
98 | break; |
|
98 | break; | |
99 | } |
|
99 | } | |
100 | case Axis::Y_AXIS: |
|
100 | case Axis::Y_AXIS: | |
101 | { |
|
101 | { | |
102 |
|
102 | |||
103 | if(height>m_chartMargins.bottom()+m_chartRect.height()){ |
|
103 | if(height>m_chartMargins.bottom()+m_chartRect.height()){ | |
104 | m_minBottomMargin= height - m_chartRect.height(); |
|
104 | m_minBottomMargin= height - m_chartRect.height(); | |
105 | updateLayout(); |
|
105 | updateLayout(); | |
106 | } |
|
106 | } | |
107 | break; |
|
107 | break; | |
108 | } |
|
108 | } | |
109 |
|
109 | |||
110 | } |
|
110 | } | |
111 | } |
|
111 | } | |
112 |
|
112 | |||
113 | void ChartPresenter::handleAxisAdded(QChartAxis* axis,Domain* domain) |
|
113 | void ChartPresenter::handleAxisAdded(QChartAxis* axis,Domain* domain) | |
114 | { |
|
114 | { | |
115 | Axis* item = new Axis(axis,this,axis==m_dataset->axisX()?Axis::X_AXIS : Axis::Y_AXIS); |
|
115 | Axis* item = new Axis(axis,this,axis==m_dataset->axisX()?Axis::X_AXIS : Axis::Y_AXIS); | |
116 |
|
116 | |||
117 | if(m_options.testFlag(QChart::GridAxisAnimations)){ |
|
117 | if(m_options.testFlag(QChart::GridAxisAnimations)){ | |
118 | m_animator->addAnimation(item); |
|
118 | m_animator->addAnimation(item); | |
119 | } |
|
119 | } | |
120 |
|
120 | |||
121 | if(axis==m_dataset->axisX()){ |
|
121 | if(axis==m_dataset->axisX()){ | |
122 | m_chartTheme->decorate(axis,true); |
|
122 | m_chartTheme->decorate(axis,true); | |
123 | QObject::connect(domain,SIGNAL(rangeXChanged(qreal,qreal,int)),item,SLOT(handleRangeChanged(qreal,qreal,int))); |
|
123 | QObject::connect(domain,SIGNAL(rangeXChanged(qreal,qreal,int)),item,SLOT(handleRangeChanged(qreal,qreal,int))); | |
124 | //initialize |
|
124 | //initialize | |
125 | item->handleRangeChanged(domain->minX(),domain->maxX(),domain->tickXCount()); |
|
125 | item->handleRangeChanged(domain->minX(),domain->maxX(),domain->tickXCount()); | |
126 |
|
126 | |||
127 | } |
|
127 | } | |
128 | else{ |
|
128 | else{ | |
129 | m_chartTheme->decorate(axis,false); |
|
129 | m_chartTheme->decorate(axis,false); | |
130 | QObject::connect(domain,SIGNAL(rangeYChanged(qreal,qreal,int)),item,SLOT(handleRangeChanged(qreal,qreal,int))); |
|
130 | QObject::connect(domain,SIGNAL(rangeYChanged(qreal,qreal,int)),item,SLOT(handleRangeChanged(qreal,qreal,int))); | |
131 | //initialize |
|
131 | //initialize | |
132 | item->handleRangeChanged(domain->minY(),domain->maxY(),domain->tickYCount()); |
|
132 | item->handleRangeChanged(domain->minY(),domain->maxY(),domain->tickYCount()); | |
133 | } |
|
133 | } | |
134 |
|
134 | |||
135 | QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&))); |
|
135 | QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&))); | |
136 | //initialize |
|
136 | //initialize | |
137 | item->handleGeometryChanged(m_chartRect); |
|
137 | item->handleGeometryChanged(m_chartRect); | |
138 | m_axisItems.insert(axis, item); |
|
138 | m_axisItems.insert(axis, item); | |
139 | } |
|
139 | } | |
140 |
|
140 | |||
141 | void ChartPresenter::handleAxisRemoved(QChartAxis* axis) |
|
141 | void ChartPresenter::handleAxisRemoved(QChartAxis* axis) | |
142 | { |
|
142 | { | |
143 | Axis* item = m_axisItems.take(axis); |
|
143 | Axis* item = m_axisItems.take(axis); | |
144 | Q_ASSERT(item); |
|
144 | Q_ASSERT(item); | |
145 | if(m_animator) m_animator->removeAnimation(item); |
|
145 | if(m_animator) m_animator->removeAnimation(item); | |
146 | delete item; |
|
146 | delete item; | |
147 | } |
|
147 | } | |
148 |
|
148 | |||
149 |
|
149 | |||
150 | void ChartPresenter::handleSeriesAdded(QSeries* series,Domain* domain) |
|
150 | void ChartPresenter::handleSeriesAdded(QSeries* series,Domain* domain) | |
151 | { |
|
151 | { | |
152 | Chart *item = series->d_ptr->createGraphics(this); |
|
152 | Chart *item = series->d_ptr->createGraphics(this); | |
153 | Q_ASSERT(item); |
|
153 | Q_ASSERT(item); | |
154 | QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&))); |
|
154 | QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&))); | |
155 | QObject::connect(domain,SIGNAL(domainChanged(qreal,qreal,qreal,qreal)),item,SLOT(handleDomainChanged(qreal,qreal,qreal,qreal))); |
|
155 | QObject::connect(domain,SIGNAL(domainChanged(qreal,qreal,qreal,qreal)),item,SLOT(handleDomainChanged(qreal,qreal,qreal,qreal))); | |
156 | //initialize |
|
156 | //initialize | |
157 | item->handleDomainChanged(domain->minX(),domain->maxX(),domain->minY(),domain->maxY()); |
|
157 | item->handleDomainChanged(domain->minX(),domain->maxX(),domain->minY(),domain->maxY()); | |
158 | if(m_chartRect.isValid()) item->handleGeometryChanged(m_chartRect); |
|
158 | if(m_chartRect.isValid()) item->handleGeometryChanged(m_chartRect); | |
159 | m_chartItems.insert(series,item); |
|
159 | m_chartItems.insert(series,item); | |
160 | } |
|
160 | } | |
161 |
|
161 | |||
162 | void ChartPresenter::handleSeriesRemoved(QSeries* series) |
|
162 | void ChartPresenter::handleSeriesRemoved(QSeries* series) | |
163 | { |
|
163 | { | |
164 | Chart* item = m_chartItems.take(series); |
|
164 | Chart* item = m_chartItems.take(series); | |
165 | Q_ASSERT(item); |
|
165 | Q_ASSERT(item); | |
166 | if(m_animator) { |
|
166 | if(m_animator) { | |
167 | //small hack to handle area animations |
|
167 | //small hack to handle area animations | |
168 | if(series->type()==QSeries::SeriesTypeArea){ |
|
168 | if(series->type()==QSeries::SeriesTypeArea){ | |
169 | QAreaSeries* areaSeries = static_cast<QAreaSeries*>(series); |
|
169 | QAreaSeries* areaSeries = static_cast<QAreaSeries*>(series); | |
170 | AreaChartItem* area = static_cast<AreaChartItem*>(item); |
|
170 | AreaChartItem* area = static_cast<AreaChartItem*>(item); | |
171 | m_animator->removeAnimation(area->upperLineItem()); |
|
171 | m_animator->removeAnimation(area->upperLineItem()); | |
172 | if(areaSeries->lowerSeries()) m_animator->removeAnimation(area->lowerLineItem()); |
|
172 | if(areaSeries->lowerSeries()) m_animator->removeAnimation(area->lowerLineItem()); | |
173 | }else |
|
173 | }else | |
174 | m_animator->removeAnimation(item); |
|
174 | m_animator->removeAnimation(item); | |
175 | } |
|
175 | } | |
176 | delete item; |
|
176 | delete item; | |
177 | } |
|
177 | } | |
178 |
|
178 | |||
179 | void ChartPresenter::setTheme(QChart::ChartTheme theme,bool force) |
|
179 | void ChartPresenter::setTheme(QChart::ChartTheme theme,bool force) | |
180 | { |
|
180 | { | |
181 | if(m_chartTheme && m_chartTheme->id() == theme) return; |
|
181 | if(m_chartTheme && m_chartTheme->id() == theme) return; | |
182 | delete m_chartTheme; |
|
182 | delete m_chartTheme; | |
183 | m_chartTheme = ChartTheme::createTheme(theme); |
|
183 | m_chartTheme = ChartTheme::createTheme(theme); | |
184 | m_chartTheme->setForced(force); |
|
184 | m_chartTheme->setForced(force); | |
185 | m_chartTheme->decorate(m_chart); |
|
185 | m_chartTheme->decorate(m_chart); | |
186 | m_chartTheme->decorate(m_chart->legend()); |
|
186 | m_chartTheme->decorate(m_chart->legend()); | |
187 | resetAllElements(); |
|
187 | resetAllElements(); | |
188 | } |
|
188 | } | |
189 |
|
189 | |||
190 | QChart::ChartTheme ChartPresenter::theme() |
|
190 | QChart::ChartTheme ChartPresenter::theme() | |
191 | { |
|
191 | { | |
192 | return m_chartTheme->id(); |
|
192 | return m_chartTheme->id(); | |
193 | } |
|
193 | } | |
194 |
|
194 | |||
195 | void ChartPresenter::setAnimationOptions(QChart::AnimationOptions options) |
|
195 | void ChartPresenter::setAnimationOptions(QChart::AnimationOptions options) | |
196 | { |
|
196 | { | |
197 | if(m_options!=options) { |
|
197 | if(m_options!=options) { | |
198 |
|
198 | |||
199 | m_options=options; |
|
199 | m_options=options; | |
200 |
|
200 | |||
201 | if(m_options!=QChart::NoAnimation && !m_animator) { |
|
201 | if(m_options!=QChart::NoAnimation && !m_animator) { | |
202 | m_animator= new ChartAnimator(this); |
|
202 | m_animator= new ChartAnimator(this); | |
203 | } |
|
203 | } | |
204 | resetAllElements(); |
|
204 | resetAllElements(); | |
205 | } |
|
205 | } | |
206 |
|
206 | |||
207 | } |
|
207 | } | |
208 |
|
208 | |||
209 | void ChartPresenter::resetAllElements() |
|
209 | void ChartPresenter::resetAllElements() | |
210 | { |
|
210 | { | |
211 | QList<QChartAxis*> axisList = m_axisItems.uniqueKeys(); |
|
211 | QList<QChartAxis*> axisList = m_axisItems.uniqueKeys(); | |
212 | QList<QSeries*> seriesList = m_chartItems.uniqueKeys(); |
|
212 | QList<QSeries*> seriesList = m_chartItems.uniqueKeys(); | |
213 |
|
213 | |||
214 | foreach(QChartAxis* axis, axisList) { |
|
214 | foreach(QChartAxis* axis, axisList) { | |
215 | handleAxisRemoved(axis); |
|
215 | handleAxisRemoved(axis); | |
216 | handleAxisAdded(axis,m_dataset->domain(axis)); |
|
216 | handleAxisAdded(axis,m_dataset->domain(axis)); | |
217 | } |
|
217 | } | |
218 | foreach(QSeries* series, seriesList) { |
|
218 | foreach(QSeries* series, seriesList) { | |
219 | handleSeriesRemoved(series); |
|
219 | handleSeriesRemoved(series); | |
220 | handleSeriesAdded(series,m_dataset->domain(series)); |
|
220 | handleSeriesAdded(series,m_dataset->domain(series)); | |
221 | } |
|
221 | } | |
222 | } |
|
222 | } | |
223 |
|
223 | |||
224 | void ChartPresenter::zoomIn() |
|
224 | void ChartPresenter::zoomIn() | |
225 | { |
|
225 | { | |
226 | QRectF rect = chartGeometry(); |
|
226 | QRectF rect = chartGeometry(); | |
227 | rect.setWidth(rect.width()/2); |
|
227 | rect.setWidth(rect.width()/2); | |
228 | rect.setHeight(rect.height()/2); |
|
228 | rect.setHeight(rect.height()/2); | |
229 | rect.moveCenter(chartGeometry().center()); |
|
229 | rect.moveCenter(chartGeometry().center()); | |
230 | zoomIn(rect); |
|
230 | zoomIn(rect); | |
231 | } |
|
231 | } | |
232 |
|
232 | |||
233 | void ChartPresenter::zoomIn(const QRectF& rect) |
|
233 | void ChartPresenter::zoomIn(const QRectF& rect) | |
234 | { |
|
234 | { | |
235 | QRectF r = rect.normalized(); |
|
235 | QRectF r = rect.normalized(); | |
236 | r.translate(-m_chartMargins.topLeft()); |
|
236 | r.translate(-m_chartMargins.topLeft()); | |
237 | if(m_animator) { |
|
237 | if(m_animator) { | |
238 |
|
238 | |||
239 | QPointF point(r.center().x()/chartGeometry().width(),r.center().y()/chartGeometry().height()); |
|
239 | QPointF point(r.center().x()/chartGeometry().width(),r.center().y()/chartGeometry().height()); | |
240 | m_animator->setState(ChartAnimator::ZoomInState,point); |
|
240 | m_animator->setState(ChartAnimator::ZoomInState,point); | |
241 | } |
|
241 | } | |
242 | m_dataset->zoomInDomain(r,chartGeometry().size()); |
|
242 | m_dataset->zoomInDomain(r,chartGeometry().size()); | |
243 | if(m_animator) { |
|
243 | if(m_animator) { | |
244 | m_animator->setState(ChartAnimator::ShowState); |
|
244 | m_animator->setState(ChartAnimator::ShowState); | |
245 | } |
|
245 | } | |
246 | } |
|
246 | } | |
247 |
|
247 | |||
248 | void ChartPresenter::zoomOut() |
|
248 | void ChartPresenter::zoomOut() | |
249 | { |
|
249 | { | |
250 | if(m_animator) |
|
250 | if(m_animator) | |
251 | { |
|
251 | { | |
252 | m_animator->setState(ChartAnimator::ZoomOutState); |
|
252 | m_animator->setState(ChartAnimator::ZoomOutState); | |
253 | } |
|
253 | } | |
254 |
|
254 | |||
255 | QSizeF size = chartGeometry().size(); |
|
255 | QSizeF size = chartGeometry().size(); | |
256 | QRectF rect = chartGeometry(); |
|
256 | QRectF rect = chartGeometry(); | |
257 | rect.translate(-m_chartMargins.topLeft()); |
|
257 | rect.translate(-m_chartMargins.topLeft()); | |
258 | m_dataset->zoomOutDomain(rect.adjusted(size.width()/4,size.height()/4,-size.width()/4,-size.height()/4),size); |
|
258 | m_dataset->zoomOutDomain(rect.adjusted(size.width()/4,size.height()/4,-size.width()/4,-size.height()/4),size); | |
259 | //m_dataset->zoomOutDomain(m_zoomStack[m_zoomIndex-1],geometry().size()); |
|
259 | //m_dataset->zoomOutDomain(m_zoomStack[m_zoomIndex-1],geometry().size()); | |
260 |
|
260 | |||
261 | if(m_animator){ |
|
261 | if(m_animator){ | |
262 | m_animator->setState(ChartAnimator::ShowState); |
|
262 | m_animator->setState(ChartAnimator::ShowState); | |
263 | } |
|
263 | } | |
264 | } |
|
264 | } | |
265 |
|
265 | |||
266 | void ChartPresenter::scroll(int dx,int dy) |
|
266 | void ChartPresenter::scroll(int dx,int dy) | |
267 | { |
|
267 | { | |
268 | if(m_animator){ |
|
268 | if(m_animator){ | |
269 | if(dx<0) m_animator->setState(ChartAnimator::ScrollLeftState,QPointF()); |
|
269 | if(dx<0) m_animator->setState(ChartAnimator::ScrollLeftState,QPointF()); | |
270 | if(dx>0) m_animator->setState(ChartAnimator::ScrollRightState,QPointF()); |
|
270 | if(dx>0) m_animator->setState(ChartAnimator::ScrollRightState,QPointF()); | |
271 | if(dy<0) m_animator->setState(ChartAnimator::ScrollUpState,QPointF()); |
|
271 | if(dy<0) m_animator->setState(ChartAnimator::ScrollUpState,QPointF()); | |
272 | if(dy>0) m_animator->setState(ChartAnimator::ScrollDownState,QPointF()); |
|
272 | if(dy>0) m_animator->setState(ChartAnimator::ScrollDownState,QPointF()); | |
273 | } |
|
273 | } | |
274 |
|
274 | |||
275 | m_dataset->scrollDomain(dx,dy,chartGeometry().size()); |
|
275 | m_dataset->scrollDomain(dx,dy,chartGeometry().size()); | |
276 |
|
276 | |||
277 | if(m_animator){ |
|
277 | if(m_animator){ | |
278 | m_animator->setState(ChartAnimator::ShowState); |
|
278 | m_animator->setState(ChartAnimator::ShowState); | |
279 | } |
|
279 | } | |
280 | } |
|
280 | } | |
281 |
|
281 | |||
282 | QChart::AnimationOptions ChartPresenter::animationOptions() const |
|
282 | QChart::AnimationOptions ChartPresenter::animationOptions() const | |
283 | { |
|
283 | { | |
284 | return m_options; |
|
284 | return m_options; | |
285 | } |
|
285 | } | |
286 |
|
286 | |||
287 | void ChartPresenter::updateLayout() |
|
287 | void ChartPresenter::updateLayout() | |
288 | { |
|
288 | { | |
289 | if (!m_rect.isValid()) return; |
|
289 | if (!m_rect.isValid()) return; | |
290 |
|
290 | |||
291 | // recalculate title size |
|
291 | // recalculate title size | |
292 |
|
292 | |||
293 | QSize titleSize; |
|
293 | QSize titleSize; | |
294 | int titlePadding=0; |
|
294 | int titlePadding=0; | |
295 |
|
295 | |||
296 | if (m_titleItem) { |
|
296 | if (m_titleItem) { | |
297 | titleSize= m_titleItem->boundingRect().size().toSize(); |
|
297 | titleSize= m_titleItem->boundingRect().size().toSize(); | |
298 | } |
|
298 | } | |
299 |
|
299 | |||
300 | //defaults |
|
300 | //defaults | |
301 | m_chartMargins = QRect(QPoint(m_minLeftMargin>m_marginBig?m_minLeftMargin:m_marginBig,m_marginBig),QPoint(m_marginBig,m_minBottomMargin>m_marginBig?m_minBottomMargin:m_marginBig)); |
|
301 | m_chartMargins = QRect(QPoint(m_minLeftMargin>m_marginBig?m_minLeftMargin:m_marginBig,m_marginBig),QPoint(m_marginBig,m_minBottomMargin>m_marginBig?m_minBottomMargin:m_marginBig)); | |
302 | titlePadding = m_chartMargins.top()/2; |
|
302 | titlePadding = m_chartMargins.top()/2; | |
303 |
|
303 | |||
304 | QLegend* legend = m_chart->d_ptr->m_legend; |
|
304 | QLegend* legend = m_chart->d_ptr->m_legend; | |
305 |
|
305 | |||
306 | // recalculate legend position |
|
306 | // recalculate legend position | |
307 | if (legend->isAttachedToChart() && legend->isEnabled()) { |
|
307 | if (legend->isAttachedToChart() && legend->isEnabled()) { | |
308 |
|
308 | |||
309 | QRect legendRect; |
|
309 | QRect legendRect; | |
310 |
|
310 | |||
311 | // Reserve some space for legend |
|
311 | // Reserve some space for legend | |
312 | switch (legend->alignment()) { |
|
312 | switch (legend->alignment()) { | |
313 |
|
313 | |||
314 | case QLegend::AlignmentTop: { |
|
314 | case QLegend::AlignmentTop: { | |
315 | int ledgendSize = legend->minHeight(); |
|
315 | int ledgendSize = legend->minHeight(); | |
316 | int topPadding = 2*m_marginTiny + titleSize.height() + ledgendSize + m_marginTiny; |
|
316 | int topPadding = 2*m_marginTiny + titleSize.height() + ledgendSize + m_marginTiny; | |
317 | m_chartMargins = QRect(QPoint(m_chartMargins.left(),topPadding),QPoint(m_chartMargins.right(),m_chartMargins.bottom())); |
|
317 | m_chartMargins = QRect(QPoint(m_chartMargins.left(),topPadding),QPoint(m_chartMargins.right(),m_chartMargins.bottom())); | |
318 | m_legendMargins = QRect(QPoint(m_chartMargins.left(),topPadding - (ledgendSize + m_marginTiny)),QPoint(m_chartMargins.right(),m_rect.height()-topPadding + m_marginTiny)); |
|
318 | m_legendMargins = QRect(QPoint(m_chartMargins.left(),topPadding - (ledgendSize + m_marginTiny)),QPoint(m_chartMargins.right(),m_rect.height()-topPadding + m_marginTiny)); | |
319 | titlePadding = m_marginTiny + m_marginTiny; |
|
319 | titlePadding = m_marginTiny + m_marginTiny; | |
320 | break; |
|
320 | break; | |
321 | } |
|
321 | } | |
322 | case QLegend::AlignmentBottom: { |
|
322 | case QLegend::AlignmentBottom: { | |
323 | int ledgendSize = legend->minHeight(); |
|
323 | int ledgendSize = legend->minHeight(); | |
324 | int bottomPadding = m_marginTiny + m_marginSmall + ledgendSize + m_marginTiny + m_minBottomMargin; |
|
324 | int bottomPadding = m_marginTiny + m_marginSmall + ledgendSize + m_marginTiny + m_minBottomMargin; | |
325 | m_chartMargins = QRect(QPoint(m_chartMargins.left(),m_chartMargins.top()),QPoint(m_chartMargins.right(),bottomPadding)); |
|
325 | m_chartMargins = QRect(QPoint(m_chartMargins.left(),m_chartMargins.top()),QPoint(m_chartMargins.right(),bottomPadding)); | |
326 | m_legendMargins = QRect(QPoint(m_chartMargins.left(),m_rect.height()-bottomPadding + m_marginTiny + m_minBottomMargin),QPoint(m_chartMargins.right(),m_marginTiny + m_marginSmall)); |
|
326 | m_legendMargins = QRect(QPoint(m_chartMargins.left(),m_rect.height()-bottomPadding + m_marginTiny + m_minBottomMargin),QPoint(m_chartMargins.right(),m_marginTiny + m_marginSmall)); | |
327 | titlePadding = m_chartMargins.top()/2; |
|
327 | titlePadding = m_chartMargins.top()/2; | |
328 | break; |
|
328 | break; | |
329 | } |
|
329 | } | |
330 | case QLegend::AlignmentLeft: { |
|
330 | case QLegend::AlignmentLeft: { | |
331 | int ledgendSize = legend->minWidth(); |
|
331 | int ledgendSize = legend->minWidth(); | |
332 | int leftPadding = m_marginTiny + m_marginSmall + ledgendSize + m_marginTiny + m_minLeftMargin; |
|
332 | int leftPadding = m_marginTiny + m_marginSmall + ledgendSize + m_marginTiny + m_minLeftMargin; | |
333 | m_chartMargins = QRect(QPoint(leftPadding,m_chartMargins.top()),QPoint(m_chartMargins.right(),m_chartMargins.bottom())); |
|
333 | m_chartMargins = QRect(QPoint(leftPadding,m_chartMargins.top()),QPoint(m_chartMargins.right(),m_chartMargins.bottom())); | |
334 | m_legendMargins = QRect(QPoint(m_marginTiny + m_marginSmall,m_chartMargins.top()),QPoint(m_rect.width()-leftPadding + m_marginTiny + m_minLeftMargin,m_chartMargins.bottom())); |
|
334 | m_legendMargins = QRect(QPoint(m_marginTiny + m_marginSmall,m_chartMargins.top()),QPoint(m_rect.width()-leftPadding + m_marginTiny + m_minLeftMargin,m_chartMargins.bottom())); | |
335 | titlePadding = m_chartMargins.top()/2; |
|
335 | titlePadding = m_chartMargins.top()/2; | |
336 | break; |
|
336 | break; | |
337 | } |
|
337 | } | |
338 | case QLegend::AlignmentRight: { |
|
338 | case QLegend::AlignmentRight: { | |
339 | int ledgendSize = legend->minWidth(); |
|
339 | int ledgendSize = legend->minWidth(); | |
340 | int rightPadding = m_marginTiny + m_marginSmall + ledgendSize + m_marginTiny; |
|
340 | int rightPadding = m_marginTiny + m_marginSmall + ledgendSize + m_marginTiny; | |
341 | m_chartMargins = QRect(QPoint(m_chartMargins.left(),m_chartMargins.top()),QPoint(rightPadding,m_chartMargins.bottom())); |
|
341 | m_chartMargins = QRect(QPoint(m_chartMargins.left(),m_chartMargins.top()),QPoint(rightPadding,m_chartMargins.bottom())); | |
342 | m_legendMargins = QRect(QPoint(m_rect.width()- rightPadding+ m_marginTiny ,m_chartMargins.top()),QPoint(m_marginTiny + m_marginSmall,m_chartMargins.bottom())); |
|
342 | m_legendMargins = QRect(QPoint(m_rect.width()- rightPadding+ m_marginTiny ,m_chartMargins.top()),QPoint(m_marginTiny + m_marginSmall,m_chartMargins.bottom())); | |
343 | titlePadding = m_chartMargins.top()/2; |
|
343 | titlePadding = m_chartMargins.top()/2; | |
344 | break; |
|
344 | break; | |
345 | } |
|
345 | } | |
346 | default: { |
|
346 | default: { | |
347 | break; |
|
347 | break; | |
348 | } |
|
348 | } | |
349 | } |
|
349 | } | |
350 | } |
|
350 | } | |
351 |
|
351 | |||
352 | if(m_rect.width()<2*(m_chartMargins.top()+m_chartMargins.bottom()) || m_rect.height()< 2*(m_chartMargins.top() + m_chartMargins.bottom())) |
|
352 | if(m_rect.width()<2*(m_chartMargins.top()+m_chartMargins.bottom()) || m_rect.height()< 2*(m_chartMargins.top() + m_chartMargins.bottom())) | |
353 | { |
|
353 | { | |
354 | m_chart->setMinimumSize(2*(m_chartMargins.top()+m_chartMargins.bottom()),2*(m_chartMargins.top() + m_chartMargins.bottom())); |
|
354 | m_chart->setMinimumSize(2*(m_chartMargins.top()+m_chartMargins.bottom()),2*(m_chartMargins.top() + m_chartMargins.bottom())); | |
355 | return; |
|
355 | return; | |
356 | } |
|
356 | } | |
357 |
|
357 | |||
358 |
|
358 | |||
359 | // recalculate title position |
|
359 | // recalculate title position | |
360 | if (m_titleItem) { |
|
360 | if (m_titleItem) { | |
361 | QPointF center = m_rect.center() -m_titleItem->boundingRect().center(); |
|
361 | QPointF center = m_rect.center() -m_titleItem->boundingRect().center(); | |
362 | m_titleItem->setPos(center.x(),titlePadding); |
|
362 | m_titleItem->setPos(center.x(),titlePadding); | |
363 | } |
|
363 | } | |
364 |
|
364 | |||
365 | //recalculate background gradient |
|
365 | //recalculate background gradient | |
366 | if (m_backgroundItem) { |
|
366 | if (m_backgroundItem) { | |
367 | m_backgroundItem->setRect(m_rect.adjusted(m_marginTiny,m_marginTiny, -m_marginTiny, -m_marginTiny)); |
|
367 | m_backgroundItem->setRect(m_rect.adjusted(m_marginTiny,m_marginTiny, -m_marginTiny, -m_marginTiny)); | |
368 | } |
|
368 | } | |
369 |
|
369 | |||
370 |
|
370 | |||
371 | QRectF chartRect = m_rect.adjusted(m_chartMargins.left(),m_chartMargins.top(),-m_chartMargins.right(),-m_chartMargins.bottom()); |
|
371 | QRectF chartRect = m_rect.adjusted(m_chartMargins.left(),m_chartMargins.top(),-m_chartMargins.right(),-m_chartMargins.bottom()); | |
372 |
|
372 | |||
373 | legend->setGeometry(m_rect.adjusted(m_legendMargins.left(),m_legendMargins.top(),-m_legendMargins.right(),-m_legendMargins.bottom())); |
|
373 | legend->setGeometry(m_rect.adjusted(m_legendMargins.left(),m_legendMargins.top(),-m_legendMargins.right(),-m_legendMargins.bottom())); | |
374 |
|
374 | |||
375 | if(m_chartRect!=chartRect){ |
|
375 | if(m_chartRect!=chartRect){ | |
376 | m_chartRect=chartRect; |
|
376 | m_chartRect=chartRect; | |
377 | emit geometryChanged(m_chartRect); |
|
377 | emit geometryChanged(m_chartRect); | |
378 | } |
|
378 | } | |
379 |
|
379 | |||
380 |
|
380 | |||
381 | } |
|
381 | } | |
382 |
|
382 | |||
383 | void ChartPresenter::createChartBackgroundItem() |
|
383 | void ChartPresenter::createChartBackgroundItem() | |
384 | { |
|
384 | { | |
385 | if (!m_backgroundItem) { |
|
385 | if (!m_backgroundItem) { | |
386 | m_backgroundItem = new ChartBackground(rootItem()); |
|
386 | m_backgroundItem = new ChartBackground(rootItem()); | |
387 | m_backgroundItem->setPen(Qt::NoPen); |
|
387 | m_backgroundItem->setPen(Qt::NoPen); | |
388 | m_backgroundItem->setZValue(ChartPresenter::BackgroundZValue); |
|
388 | m_backgroundItem->setZValue(ChartPresenter::BackgroundZValue); | |
389 | } |
|
389 | } | |
390 | } |
|
390 | } | |
391 |
|
391 | |||
392 | void ChartPresenter::createChartTitleItem() |
|
392 | void ChartPresenter::createChartTitleItem() | |
393 | { |
|
393 | { | |
394 | if (!m_titleItem) { |
|
394 | if (!m_titleItem) { | |
395 | m_titleItem = new QGraphicsSimpleTextItem(rootItem()); |
|
395 | m_titleItem = new QGraphicsSimpleTextItem(rootItem()); | |
396 | m_titleItem->setZValue(ChartPresenter::BackgroundZValue); |
|
396 | m_titleItem->setZValue(ChartPresenter::BackgroundZValue); | |
397 | } |
|
397 | } | |
398 | } |
|
398 | } | |
399 |
|
399 | |||
400 | #include "moc_chartpresenter_p.cpp" |
|
400 | #include "moc_chartpresenter_p.cpp" | |
401 |
|
401 | |||
402 | QTCOMMERCIALCHART_END_NAMESPACE |
|
402 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,398 +1,398 | |||||
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 "charttheme_p.h" |
|
21 | #include "charttheme_p.h" | |
22 | #include "qchart.h" |
|
22 | #include "qchart.h" | |
23 | #include "qchartview.h" |
|
23 | #include "qchartview.h" | |
24 | #include "qlegend.h" |
|
24 | #include "qlegend.h" | |
25 | #include "qchartaxis.h" |
|
25 | #include "qchartaxis.h" | |
26 | #include <QTime> |
|
26 | #include <QTime> | |
27 |
|
27 | |||
28 | //series |
|
28 | //series | |
29 | #include "qbarset.h" |
|
29 | #include "qbarset.h" | |
30 | #include "qbarseries.h" |
|
30 | #include "qbarseries.h" | |
31 | #include "qstackedbarseries.h" |
|
31 | #include "qstackedbarseries.h" | |
32 | #include "qpercentbarseries.h" |
|
32 | #include "qpercentbarseries.h" | |
33 | #include "qlineseries.h" |
|
33 | #include "qlineseries.h" | |
34 | #include "qareaseries.h" |
|
34 | #include "qareaseries.h" | |
35 | #include "qscatterseries.h" |
|
35 | #include "qscatterseries.h" | |
36 | #include "qpieseries.h" |
|
36 | #include "qpieseries.h" | |
37 | #include "qpieslice.h" |
|
37 | #include "qpieslice.h" | |
38 | #include "qsplineseries.h" |
|
38 | #include "qsplineseries.h" | |
39 |
|
39 | |||
40 | //items |
|
40 | //items | |
41 |
#include "axis |
|
41 | #include "axis_p.h" | |
42 | #include "barchartitem_p.h" |
|
42 | #include "barchartitem_p.h" | |
43 | #include "stackedbarchartitem_p.h" |
|
43 | #include "stackedbarchartitem_p.h" | |
44 | #include "percentbarchartitem_p.h" |
|
44 | #include "percentbarchartitem_p.h" | |
45 | #include "linechartitem_p.h" |
|
45 | #include "linechartitem_p.h" | |
46 | #include "areachartitem_p.h" |
|
46 | #include "areachartitem_p.h" | |
47 | #include "scatterchartitem_p.h" |
|
47 | #include "scatterchartitem_p.h" | |
48 | #include "piechartitem_p.h" |
|
48 | #include "piechartitem_p.h" | |
49 | #include "splinechartitem_p.h" |
|
49 | #include "splinechartitem_p.h" | |
50 |
|
50 | |||
51 | //themes |
|
51 | //themes | |
52 | #include "chartthemesystem_p.h" |
|
52 | #include "chartthemesystem_p.h" | |
53 | #include "chartthemelight_p.h" |
|
53 | #include "chartthemelight_p.h" | |
54 | #include "chartthemebluecerulean_p.h" |
|
54 | #include "chartthemebluecerulean_p.h" | |
55 | #include "chartthemedark_p.h" |
|
55 | #include "chartthemedark_p.h" | |
56 | #include "chartthemebrownsand_p.h" |
|
56 | #include "chartthemebrownsand_p.h" | |
57 | #include "chartthemebluencs_p.h" |
|
57 | #include "chartthemebluencs_p.h" | |
58 | #include "chartthemehighcontrast_p.h" |
|
58 | #include "chartthemehighcontrast_p.h" | |
59 | #include "chartthemeblueicy_p.h" |
|
59 | #include "chartthemeblueicy_p.h" | |
60 |
|
60 | |||
61 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
61 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
62 |
|
62 | |||
63 | ChartTheme::ChartTheme(QChart::ChartTheme id) : |
|
63 | ChartTheme::ChartTheme(QChart::ChartTheme id) : | |
64 | m_masterFont(QFont("arial", 14)), |
|
64 | m_masterFont(QFont("arial", 14)), | |
65 | m_labelFont(QFont("arial", 10)), |
|
65 | m_labelFont(QFont("arial", 10)), | |
66 | m_titleBrush(QColor(QRgb(0x000000))), |
|
66 | m_titleBrush(QColor(QRgb(0x000000))), | |
67 | m_axisLinePen(QPen(QRgb(0x000000))), |
|
67 | m_axisLinePen(QPen(QRgb(0x000000))), | |
68 | m_axisLabelBrush(QColor(QRgb(0x000000))), |
|
68 | m_axisLabelBrush(QColor(QRgb(0x000000))), | |
69 | m_backgroundShadesPen(Qt::NoPen), |
|
69 | m_backgroundShadesPen(Qt::NoPen), | |
70 | m_backgroundShadesBrush(Qt::NoBrush), |
|
70 | m_backgroundShadesBrush(Qt::NoBrush), | |
71 | m_backgroundShades(BackgroundShadesNone), |
|
71 | m_backgroundShades(BackgroundShadesNone), | |
72 | m_gridLinePen(QPen(QRgb(0x000000))), |
|
72 | m_gridLinePen(QPen(QRgb(0x000000))), | |
73 | m_force(false) |
|
73 | m_force(false) | |
74 | { |
|
74 | { | |
75 | m_id = id; |
|
75 | m_id = id; | |
76 | qsrand(QTime(0,0,0).secsTo(QTime::currentTime())); |
|
76 | qsrand(QTime(0,0,0).secsTo(QTime::currentTime())); | |
77 | } |
|
77 | } | |
78 |
|
78 | |||
79 |
|
79 | |||
80 | ChartTheme* ChartTheme::createTheme(QChart::ChartTheme theme) |
|
80 | ChartTheme* ChartTheme::createTheme(QChart::ChartTheme theme) | |
81 | { |
|
81 | { | |
82 | switch(theme) { |
|
82 | switch(theme) { | |
83 | case QChart::ChartThemeLight: |
|
83 | case QChart::ChartThemeLight: | |
84 | return new ChartThemeLight(); |
|
84 | return new ChartThemeLight(); | |
85 | case QChart::ChartThemeBlueCerulean: |
|
85 | case QChart::ChartThemeBlueCerulean: | |
86 | return new ChartThemeBlueCerulean(); |
|
86 | return new ChartThemeBlueCerulean(); | |
87 | case QChart::ChartThemeDark: |
|
87 | case QChart::ChartThemeDark: | |
88 | return new ChartThemeDark(); |
|
88 | return new ChartThemeDark(); | |
89 | case QChart::ChartThemeBrownSand: |
|
89 | case QChart::ChartThemeBrownSand: | |
90 | return new ChartThemeBrownSand(); |
|
90 | return new ChartThemeBrownSand(); | |
91 | case QChart::ChartThemeBlueNcs: |
|
91 | case QChart::ChartThemeBlueNcs: | |
92 | return new ChartThemeBlueNcs(); |
|
92 | return new ChartThemeBlueNcs(); | |
93 | case QChart::ChartThemeHighContrast: |
|
93 | case QChart::ChartThemeHighContrast: | |
94 | return new ChartThemeHighContrast(); |
|
94 | return new ChartThemeHighContrast(); | |
95 | case QChart::ChartThemeBlueIcy: |
|
95 | case QChart::ChartThemeBlueIcy: | |
96 | return new ChartThemeBlueIcy(); |
|
96 | return new ChartThemeBlueIcy(); | |
97 | default: |
|
97 | default: | |
98 | return new ChartThemeSystem(); |
|
98 | return new ChartThemeSystem(); | |
99 | } |
|
99 | } | |
100 | } |
|
100 | } | |
101 |
|
101 | |||
102 | void ChartTheme::decorate(QChart *chart) |
|
102 | void ChartTheme::decorate(QChart *chart) | |
103 | { |
|
103 | { | |
104 | QBrush brush; |
|
104 | QBrush brush; | |
105 |
|
105 | |||
106 | if(brush == chart->backgroundBrush() || m_force) |
|
106 | if(brush == chart->backgroundBrush() || m_force) | |
107 | chart->setBackgroundBrush(m_chartBackgroundGradient); |
|
107 | chart->setBackgroundBrush(m_chartBackgroundGradient); | |
108 | chart->setTitleFont(m_masterFont); |
|
108 | chart->setTitleFont(m_masterFont); | |
109 | chart->setTitleBrush(m_titleBrush); |
|
109 | chart->setTitleBrush(m_titleBrush); | |
110 | } |
|
110 | } | |
111 |
|
111 | |||
112 | void ChartTheme::decorate(QLegend *legend) |
|
112 | void ChartTheme::decorate(QLegend *legend) | |
113 | { |
|
113 | { | |
114 | QPen pen; |
|
114 | QPen pen; | |
115 | QBrush brush; |
|
115 | QBrush brush; | |
116 |
|
116 | |||
117 | if (pen == legend->pen() || m_force){ |
|
117 | if (pen == legend->pen() || m_force){ | |
118 | legend->setPen(Qt::NoPen); |
|
118 | legend->setPen(Qt::NoPen); | |
119 | } |
|
119 | } | |
120 |
|
120 | |||
121 |
|
121 | |||
122 | if (brush == legend->brush() || m_force) { |
|
122 | if (brush == legend->brush() || m_force) { | |
123 | legend->setBrush(m_chartBackgroundGradient); |
|
123 | legend->setBrush(m_chartBackgroundGradient); | |
124 | } |
|
124 | } | |
125 | } |
|
125 | } | |
126 |
|
126 | |||
127 | void ChartTheme::decorate(QAreaSeries *series, int index) |
|
127 | void ChartTheme::decorate(QAreaSeries *series, int index) | |
128 | { |
|
128 | { | |
129 | QPen pen; |
|
129 | QPen pen; | |
130 | QBrush brush; |
|
130 | QBrush brush; | |
131 |
|
131 | |||
132 | if (pen == series->pen() || m_force){ |
|
132 | if (pen == series->pen() || m_force){ | |
133 | pen.setColor(colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0.0)); |
|
133 | pen.setColor(colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0.0)); | |
134 | pen.setWidthF(2); |
|
134 | pen.setWidthF(2); | |
135 | series->setPen(pen); |
|
135 | series->setPen(pen); | |
136 | } |
|
136 | } | |
137 |
|
137 | |||
138 | if (brush == series->brush() || m_force) { |
|
138 | if (brush == series->brush() || m_force) { | |
139 | QBrush brush(m_seriesColors.at(index % m_seriesColors.size())); |
|
139 | QBrush brush(m_seriesColors.at(index % m_seriesColors.size())); | |
140 | series->setBrush(brush); |
|
140 | series->setBrush(brush); | |
141 | } |
|
141 | } | |
142 | } |
|
142 | } | |
143 |
|
143 | |||
144 |
|
144 | |||
145 | void ChartTheme::decorate(QLineSeries *series,int index) |
|
145 | void ChartTheme::decorate(QLineSeries *series,int index) | |
146 | { |
|
146 | { | |
147 | QPen pen; |
|
147 | QPen pen; | |
148 | if(pen == series->pen() || m_force ){ |
|
148 | if(pen == series->pen() || m_force ){ | |
149 | pen.setColor(m_seriesColors.at(index%m_seriesColors.size())); |
|
149 | pen.setColor(m_seriesColors.at(index%m_seriesColors.size())); | |
150 | pen.setWidthF(2); |
|
150 | pen.setWidthF(2); | |
151 | series->setPen(pen); |
|
151 | series->setPen(pen); | |
152 | } |
|
152 | } | |
153 | } |
|
153 | } | |
154 |
|
154 | |||
155 | void ChartTheme::decorate(QBarSeries *series, int index) |
|
155 | void ChartTheme::decorate(QBarSeries *series, int index) | |
156 | { |
|
156 | { | |
157 | QBrush brush; |
|
157 | QBrush brush; | |
158 | QPen pen; |
|
158 | QPen pen; | |
159 | QList<QBarSet *> sets = series->barSets(); |
|
159 | QList<QBarSet *> sets = series->barSets(); | |
160 |
|
160 | |||
161 | qreal takeAtPos = 0.5; |
|
161 | qreal takeAtPos = 0.5; | |
162 | qreal step = 0.2; |
|
162 | qreal step = 0.2; | |
163 | if (sets.count() > 1 ) { |
|
163 | if (sets.count() > 1 ) { | |
164 | step = 1.0 / (qreal) sets.count(); |
|
164 | step = 1.0 / (qreal) sets.count(); | |
165 | if (sets.count() % m_seriesGradients.count()) |
|
165 | if (sets.count() % m_seriesGradients.count()) | |
166 | step *= m_seriesGradients.count(); |
|
166 | step *= m_seriesGradients.count(); | |
167 | else |
|
167 | else | |
168 | step *= (m_seriesGradients.count() - 1); |
|
168 | step *= (m_seriesGradients.count() - 1); | |
169 | } |
|
169 | } | |
170 |
|
170 | |||
171 | for (int i(0); i < sets.count(); i++) { |
|
171 | for (int i(0); i < sets.count(); i++) { | |
172 | int colorIndex = (index + i) % m_seriesGradients.count(); |
|
172 | int colorIndex = (index + i) % m_seriesGradients.count(); | |
173 | if (i > 0 && i % m_seriesGradients.count() == 0) { |
|
173 | if (i > 0 && i % m_seriesGradients.count() == 0) { | |
174 | // There is no dedicated base color for each sets, generate more colors |
|
174 | // There is no dedicated base color for each sets, generate more colors | |
175 | takeAtPos += step; |
|
175 | takeAtPos += step; | |
176 | if (takeAtPos == 1.0) |
|
176 | if (takeAtPos == 1.0) | |
177 | takeAtPos += step; |
|
177 | takeAtPos += step; | |
178 | takeAtPos -= (int) takeAtPos; |
|
178 | takeAtPos -= (int) takeAtPos; | |
179 | } |
|
179 | } | |
180 | if (brush == sets.at(i)->brush() || m_force ) |
|
180 | if (brush == sets.at(i)->brush() || m_force ) | |
181 | sets.at(i)->setBrush(colorAt(m_seriesGradients.at(colorIndex), takeAtPos)); |
|
181 | sets.at(i)->setBrush(colorAt(m_seriesGradients.at(colorIndex), takeAtPos)); | |
182 |
|
182 | |||
183 | // Pick label color from the opposite end of the gradient. |
|
183 | // Pick label color from the opposite end of the gradient. | |
184 | // 0.3 as a boundary seems to work well. |
|
184 | // 0.3 as a boundary seems to work well. | |
185 | if (takeAtPos < 0.3) |
|
185 | if (takeAtPos < 0.3) | |
186 | sets.at(i)->setLabelBrush(colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 1)); |
|
186 | sets.at(i)->setLabelBrush(colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 1)); | |
187 | else |
|
187 | else | |
188 | sets.at(i)->setLabelBrush(colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0)); |
|
188 | sets.at(i)->setLabelBrush(colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0)); | |
189 |
|
189 | |||
190 | if (pen == sets.at(i)->pen() || m_force) { |
|
190 | if (pen == sets.at(i)->pen() || m_force) { | |
191 | QColor c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0.0); |
|
191 | QColor c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0.0); | |
192 | sets.at(i)->setPen(c); |
|
192 | sets.at(i)->setPen(c); | |
193 | } |
|
193 | } | |
194 | } |
|
194 | } | |
195 | } |
|
195 | } | |
196 |
|
196 | |||
197 | void ChartTheme::decorate(QScatterSeries *series, int index) |
|
197 | void ChartTheme::decorate(QScatterSeries *series, int index) | |
198 | { |
|
198 | { | |
199 | QPen pen; |
|
199 | QPen pen; | |
200 | QBrush brush; |
|
200 | QBrush brush; | |
201 |
|
201 | |||
202 | if (pen == series->pen() || m_force) { |
|
202 | if (pen == series->pen() || m_force) { | |
203 | pen.setColor(colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0.0)); |
|
203 | pen.setColor(colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0.0)); | |
204 | pen.setWidthF(2); |
|
204 | pen.setWidthF(2); | |
205 | series->setPen(pen); |
|
205 | series->setPen(pen); | |
206 | } |
|
206 | } | |
207 |
|
207 | |||
208 | if (brush == series->brush() || m_force) { |
|
208 | if (brush == series->brush() || m_force) { | |
209 | QBrush brush(m_seriesColors.at(index % m_seriesColors.size())); |
|
209 | QBrush brush(m_seriesColors.at(index % m_seriesColors.size())); | |
210 | series->setBrush(brush); |
|
210 | series->setBrush(brush); | |
211 | } |
|
211 | } | |
212 | } |
|
212 | } | |
213 |
|
213 | |||
214 | void ChartTheme::decorate(QPieSeries *series, int index) |
|
214 | void ChartTheme::decorate(QPieSeries *series, int index) | |
215 | { |
|
215 | { | |
216 |
|
216 | |||
217 | for (int i(0); i < series->slices().count(); i++) { |
|
217 | for (int i(0); i < series->slices().count(); i++) { | |
218 |
|
218 | |||
219 | QColor penColor = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0.0); |
|
219 | QColor penColor = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0.0); | |
220 |
|
220 | |||
221 | // Get color for a slice from a gradient linearly, beginning from the start of the gradient |
|
221 | // Get color for a slice from a gradient linearly, beginning from the start of the gradient | |
222 | qreal pos = (qreal) (i + 1) / (qreal) series->count(); |
|
222 | qreal pos = (qreal) (i + 1) / (qreal) series->count(); | |
223 | QColor brushColor = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), pos); |
|
223 | QColor brushColor = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), pos); | |
224 |
|
224 | |||
225 | QPieSlice *s = series->slices().at(i); |
|
225 | QPieSlice *s = series->slices().at(i); | |
226 | PieSliceData data = PieSliceData::data(s); |
|
226 | PieSliceData data = PieSliceData::data(s); | |
227 |
|
227 | |||
228 | if (data.m_slicePen.isThemed() || m_force) { |
|
228 | if (data.m_slicePen.isThemed() || m_force) { | |
229 | data.m_slicePen = penColor; |
|
229 | data.m_slicePen = penColor; | |
230 | data.m_slicePen.setThemed(true); |
|
230 | data.m_slicePen.setThemed(true); | |
231 | } |
|
231 | } | |
232 |
|
232 | |||
233 | if (data.m_sliceBrush.isThemed() || m_force) { |
|
233 | if (data.m_sliceBrush.isThemed() || m_force) { | |
234 | data.m_sliceBrush = brushColor; |
|
234 | data.m_sliceBrush = brushColor; | |
235 | data.m_sliceBrush.setThemed(true); |
|
235 | data.m_sliceBrush.setThemed(true); | |
236 | } |
|
236 | } | |
237 |
|
237 | |||
238 | if (data.m_labelPen.isThemed() || m_force) { |
|
238 | if (data.m_labelPen.isThemed() || m_force) { | |
239 | data.m_labelPen = QPen(m_titleBrush.color()); |
|
239 | data.m_labelPen = QPen(m_titleBrush.color()); | |
240 | data.m_labelPen.setThemed(true); |
|
240 | data.m_labelPen.setThemed(true); | |
241 | } |
|
241 | } | |
242 |
|
242 | |||
243 | if (data.m_labelFont.isThemed() || m_force) { |
|
243 | if (data.m_labelFont.isThemed() || m_force) { | |
244 | data.m_labelFont = m_labelFont; |
|
244 | data.m_labelFont = m_labelFont; | |
245 | data.m_labelFont.setThemed(true); |
|
245 | data.m_labelFont.setThemed(true); | |
246 | } |
|
246 | } | |
247 |
|
247 | |||
248 | if (PieSliceData::data(s) != data) { |
|
248 | if (PieSliceData::data(s) != data) { | |
249 | PieSliceData::data(s) = data; |
|
249 | PieSliceData::data(s) = data; | |
250 | emit PieSliceData::data(s).emitChangedSignal(s); |
|
250 | emit PieSliceData::data(s).emitChangedSignal(s); | |
251 | } |
|
251 | } | |
252 | } |
|
252 | } | |
253 | } |
|
253 | } | |
254 |
|
254 | |||
255 | void ChartTheme::decorate(QSplineSeries *series, int index) |
|
255 | void ChartTheme::decorate(QSplineSeries *series, int index) | |
256 | { |
|
256 | { | |
257 | QPen pen; |
|
257 | QPen pen; | |
258 | if(pen == series->pen() || m_force){ |
|
258 | if(pen == series->pen() || m_force){ | |
259 | pen.setColor(m_seriesColors.at(index%m_seriesColors.size())); |
|
259 | pen.setColor(m_seriesColors.at(index%m_seriesColors.size())); | |
260 | pen.setWidthF(2); |
|
260 | pen.setWidthF(2); | |
261 | series->setPen(pen); |
|
261 | series->setPen(pen); | |
262 | } |
|
262 | } | |
263 | } |
|
263 | } | |
264 |
|
264 | |||
265 | void ChartTheme::decorate(QChartAxis *axis,bool axisX) |
|
265 | void ChartTheme::decorate(QChartAxis *axis,bool axisX) | |
266 | { |
|
266 | { | |
267 | QPen pen; |
|
267 | QPen pen; | |
268 | QBrush brush; |
|
268 | QBrush brush; | |
269 | QFont font; |
|
269 | QFont font; | |
270 |
|
270 | |||
271 | if (axis->isAxisVisible()) { |
|
271 | if (axis->isAxisVisible()) { | |
272 |
|
272 | |||
273 | if(brush == axis->labelsBrush() || m_force){ |
|
273 | if(brush == axis->labelsBrush() || m_force){ | |
274 | axis->setLabelsBrush(m_axisLabelBrush); |
|
274 | axis->setLabelsBrush(m_axisLabelBrush); | |
275 | } |
|
275 | } | |
276 | if(pen == axis->labelsPen() || m_force){ |
|
276 | if(pen == axis->labelsPen() || m_force){ | |
277 | axis->setLabelsPen(Qt::NoPen); // NoPen for performance reasons |
|
277 | axis->setLabelsPen(Qt::NoPen); // NoPen for performance reasons | |
278 | } |
|
278 | } | |
279 |
|
279 | |||
280 |
|
280 | |||
281 | if (axis->shadesVisible() || m_force) { |
|
281 | if (axis->shadesVisible() || m_force) { | |
282 |
|
282 | |||
283 | if(brush == axis->shadesBrush() || m_force){ |
|
283 | if(brush == axis->shadesBrush() || m_force){ | |
284 | axis->setShadesBrush(m_backgroundShadesBrush); |
|
284 | axis->setShadesBrush(m_backgroundShadesBrush); | |
285 | } |
|
285 | } | |
286 |
|
286 | |||
287 | if(pen == axis->shadesPen() || m_force){ |
|
287 | if(pen == axis->shadesPen() || m_force){ | |
288 | axis->setShadesPen(m_backgroundShadesPen); |
|
288 | axis->setShadesPen(m_backgroundShadesPen); | |
289 | } |
|
289 | } | |
290 |
|
290 | |||
291 | if( m_force && (m_backgroundShades == BackgroundShadesBoth |
|
291 | if( m_force && (m_backgroundShades == BackgroundShadesBoth | |
292 | || (m_backgroundShades == BackgroundShadesVertical && axisX) |
|
292 | || (m_backgroundShades == BackgroundShadesVertical && axisX) | |
293 | || (m_backgroundShades == BackgroundShadesHorizontal && !axisX))){ |
|
293 | || (m_backgroundShades == BackgroundShadesHorizontal && !axisX))){ | |
294 | axis->setShadesVisible(true); |
|
294 | axis->setShadesVisible(true); | |
295 |
|
295 | |||
296 | } |
|
296 | } | |
297 | } |
|
297 | } | |
298 |
|
298 | |||
299 | if(pen == axis->axisPen() || m_force){ |
|
299 | if(pen == axis->axisPen() || m_force){ | |
300 | axis->setAxisPen(m_axisLinePen); |
|
300 | axis->setAxisPen(m_axisLinePen); | |
301 | } |
|
301 | } | |
302 |
|
302 | |||
303 | if(pen == axis->gridLinePen() || m_force){ |
|
303 | if(pen == axis->gridLinePen() || m_force){ | |
304 | axis->setGridLinePen(m_gridLinePen); |
|
304 | axis->setGridLinePen(m_gridLinePen); | |
305 | } |
|
305 | } | |
306 |
|
306 | |||
307 | if(font == axis->labelsFont() || m_force){ |
|
307 | if(font == axis->labelsFont() || m_force){ | |
308 | axis->setLabelsFont(m_labelFont); |
|
308 | axis->setLabelsFont(m_labelFont); | |
309 | } |
|
309 | } | |
310 | } |
|
310 | } | |
311 | } |
|
311 | } | |
312 |
|
312 | |||
313 | void ChartTheme::generateSeriesGradients() |
|
313 | void ChartTheme::generateSeriesGradients() | |
314 | { |
|
314 | { | |
315 | // Generate gradients in HSV color space |
|
315 | // Generate gradients in HSV color space | |
316 | foreach (QColor color, m_seriesColors) { |
|
316 | foreach (QColor color, m_seriesColors) { | |
317 | QLinearGradient g; |
|
317 | QLinearGradient g; | |
318 | qreal h = color.hsvHueF(); |
|
318 | qreal h = color.hsvHueF(); | |
319 | qreal s = color.hsvSaturationF(); |
|
319 | qreal s = color.hsvSaturationF(); | |
320 |
|
320 | |||
321 | // TODO: tune the algorithm to give nice results with most base colors defined in |
|
321 | // TODO: tune the algorithm to give nice results with most base colors defined in | |
322 | // most themes. The rest of the gradients we can define manually in theme specific |
|
322 | // most themes. The rest of the gradients we can define manually in theme specific | |
323 | // implementation. |
|
323 | // implementation. | |
324 | QColor start = color; |
|
324 | QColor start = color; | |
325 | start.setHsvF(h, 0.0, 1.0); |
|
325 | start.setHsvF(h, 0.0, 1.0); | |
326 | g.setColorAt(0.0, start); |
|
326 | g.setColorAt(0.0, start); | |
327 |
|
327 | |||
328 | g.setColorAt(0.5, color); |
|
328 | g.setColorAt(0.5, color); | |
329 |
|
329 | |||
330 | QColor end = color; |
|
330 | QColor end = color; | |
331 | end.setHsvF(h, s, 0.25); |
|
331 | end.setHsvF(h, s, 0.25); | |
332 | g.setColorAt(1.0, end); |
|
332 | g.setColorAt(1.0, end); | |
333 |
|
333 | |||
334 | m_seriesGradients << g; |
|
334 | m_seriesGradients << g; | |
335 | } |
|
335 | } | |
336 | } |
|
336 | } | |
337 |
|
337 | |||
338 |
|
338 | |||
339 | QColor ChartTheme::colorAt(const QColor &start, const QColor &end, qreal pos) |
|
339 | QColor ChartTheme::colorAt(const QColor &start, const QColor &end, qreal pos) | |
340 | { |
|
340 | { | |
341 | Q_ASSERT(pos >= 0.0 && pos <= 1.0); |
|
341 | Q_ASSERT(pos >= 0.0 && pos <= 1.0); | |
342 | qreal r = start.redF() + ((end.redF() - start.redF()) * pos); |
|
342 | qreal r = start.redF() + ((end.redF() - start.redF()) * pos); | |
343 | qreal g = start.greenF() + ((end.greenF() - start.greenF()) * pos); |
|
343 | qreal g = start.greenF() + ((end.greenF() - start.greenF()) * pos); | |
344 | qreal b = start.blueF() + ((end.blueF() - start.blueF()) * pos); |
|
344 | qreal b = start.blueF() + ((end.blueF() - start.blueF()) * pos); | |
345 | QColor c; |
|
345 | QColor c; | |
346 | c.setRgbF(r, g, b); |
|
346 | c.setRgbF(r, g, b); | |
347 | return c; |
|
347 | return c; | |
348 | } |
|
348 | } | |
349 |
|
349 | |||
350 | QColor ChartTheme::colorAt(const QGradient &gradient, qreal pos) |
|
350 | QColor ChartTheme::colorAt(const QGradient &gradient, qreal pos) | |
351 | { |
|
351 | { | |
352 | Q_ASSERT(pos >= 0 && pos <= 1.0); |
|
352 | Q_ASSERT(pos >= 0 && pos <= 1.0); | |
353 |
|
353 | |||
354 | // another possibility: |
|
354 | // another possibility: | |
355 | // http://stackoverflow.com/questions/3306786/get-intermediate-color-from-a-gradient |
|
355 | // http://stackoverflow.com/questions/3306786/get-intermediate-color-from-a-gradient | |
356 |
|
356 | |||
357 | QGradientStops stops = gradient.stops(); |
|
357 | QGradientStops stops = gradient.stops(); | |
358 | int count = stops.count(); |
|
358 | int count = stops.count(); | |
359 |
|
359 | |||
360 | // find previous stop relative to position |
|
360 | // find previous stop relative to position | |
361 | QGradientStop prev = stops.first(); |
|
361 | QGradientStop prev = stops.first(); | |
362 | for (int i = 0; i < count; i++) { |
|
362 | for (int i = 0; i < count; i++) { | |
363 | QGradientStop stop = stops.at(i); |
|
363 | QGradientStop stop = stops.at(i); | |
364 | if (pos > stop.first) |
|
364 | if (pos > stop.first) | |
365 | prev = stop; |
|
365 | prev = stop; | |
366 |
|
366 | |||
367 | // given position is actually a stop position? |
|
367 | // given position is actually a stop position? | |
368 | if (pos == stop.first) { |
|
368 | if (pos == stop.first) { | |
369 | //qDebug() << "stop color" << pos; |
|
369 | //qDebug() << "stop color" << pos; | |
370 | return stop.second; |
|
370 | return stop.second; | |
371 | } |
|
371 | } | |
372 | } |
|
372 | } | |
373 |
|
373 | |||
374 | // find next stop relative to position |
|
374 | // find next stop relative to position | |
375 | QGradientStop next = stops.last(); |
|
375 | QGradientStop next = stops.last(); | |
376 | for (int i = count - 1; i >= 0; i--) { |
|
376 | for (int i = count - 1; i >= 0; i--) { | |
377 | QGradientStop stop = stops.at(i); |
|
377 | QGradientStop stop = stops.at(i); | |
378 | if (pos < stop.first) |
|
378 | if (pos < stop.first) | |
379 | next = stop; |
|
379 | next = stop; | |
380 | } |
|
380 | } | |
381 |
|
381 | |||
382 | //qDebug() << "prev" << prev.first << "pos" << pos << "next" << next.first; |
|
382 | //qDebug() << "prev" << prev.first << "pos" << pos << "next" << next.first; | |
383 |
|
383 | |||
384 | qreal range = next.first - prev.first; |
|
384 | qreal range = next.first - prev.first; | |
385 | qreal posDelta = pos - prev.first; |
|
385 | qreal posDelta = pos - prev.first; | |
386 | qreal relativePos = posDelta / range; |
|
386 | qreal relativePos = posDelta / range; | |
387 |
|
387 | |||
388 | //qDebug() << "range" << range << "posDelta" << posDelta << "relativePos" << relativePos; |
|
388 | //qDebug() << "range" << range << "posDelta" << posDelta << "relativePos" << relativePos; | |
389 |
|
389 | |||
390 | return colorAt(prev.second, next.second, relativePos); |
|
390 | return colorAt(prev.second, next.second, relativePos); | |
391 | } |
|
391 | } | |
392 |
|
392 | |||
393 | void ChartTheme::setForced(bool enabled) |
|
393 | void ChartTheme::setForced(bool enabled) | |
394 | { |
|
394 | { | |
395 | m_force=enabled; |
|
395 | m_force=enabled; | |
396 | } |
|
396 | } | |
397 |
|
397 | |||
398 | QTCOMMERCIALCHART_END_NAMESPACE |
|
398 | QTCOMMERCIALCHART_END_NAMESPACE |
General Comments 0
You need to be logged in to leave comments.
Login now