@@ -1,351 +1,351 | |||
|
1 | 1 | /**************************************************************************** |
|
2 | 2 | ** |
|
3 | 3 | ** Copyright (C) 2012 Digia Plc |
|
4 | 4 | ** All rights reserved. |
|
5 | 5 | ** For any questions to Digia, please use contact form at http://qt.digia.com |
|
6 | 6 | ** |
|
7 | 7 | ** This file is part of the Qt Commercial Charts Add-on. |
|
8 | 8 | ** |
|
9 | 9 | ** $QT_BEGIN_LICENSE$ |
|
10 | 10 | ** Licensees holding valid Qt Commercial licenses may use this file in |
|
11 | 11 | ** accordance with the Qt Commercial License Agreement provided with the |
|
12 | 12 | ** Software or, alternatively, in accordance with the terms contained in |
|
13 | 13 | ** a written agreement between you and Digia. |
|
14 | 14 | ** |
|
15 | 15 | ** If you have questions regarding the use of this file, please use |
|
16 | 16 | ** contact form at http://qt.digia.com |
|
17 | 17 | ** $QT_END_LICENSE$ |
|
18 | 18 | ** |
|
19 | 19 | ****************************************************************************/ |
|
20 | 20 | |
|
21 | 21 | #include "qchart.h" |
|
22 | 22 | #include "qchart_p.h" |
|
23 | 23 | #include <QGraphicsScene> |
|
24 | 24 | #include <QGraphicsSceneResizeEvent> |
|
25 | 25 | |
|
26 | 26 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
27 | 27 | |
|
28 | 28 | /*! |
|
29 | 29 | \enum QChart::ChartTheme |
|
30 | 30 | |
|
31 | 31 | This enum describes the theme used by the chart. |
|
32 | 32 | |
|
33 | 33 | \value ChartThemeLight The default theme |
|
34 | 34 | \value ChartThemeBlueCerulean |
|
35 | 35 | \value ChartThemeDark |
|
36 | 36 | \value ChartThemeBrownSand |
|
37 | 37 | \value ChartThemeBlueNcs |
|
38 | 38 | \value ChartThemeHighContrast |
|
39 | 39 | \value ChartThemeBlueIcy |
|
40 | 40 | \value ChartThemeCount Not really a theme; the total count of themes. |
|
41 | 41 | */ |
|
42 | 42 | |
|
43 | 43 | /*! |
|
44 | 44 | \enum QChart::AnimationOption |
|
45 | 45 | |
|
46 | 46 | For enabling/disabling animations. Defaults to NoAnimation. |
|
47 | 47 | |
|
48 | 48 | \value NoAnimation |
|
49 | 49 | \value GridAxisAnimations |
|
50 | 50 | \value SeriesAnimations |
|
51 | 51 | \value AllAnimations |
|
52 | 52 | */ |
|
53 | 53 | |
|
54 | 54 | /*! |
|
55 | 55 | \class QChart |
|
56 | 56 | \brief QtCommercial chart API. |
|
57 | 57 | |
|
58 | 58 | QChart is a QGraphicsWidget that you can show in a QGraphicsScene. It manages the graphical |
|
59 | 59 | representation of different types of QChartSeries and other chart related objects like |
|
60 | 60 | QChartAxis and QChartLegend. If you simply want to show a chart in a layout, you can use the |
|
61 | 61 | convenience class QChartView instead of QChart. |
|
62 | 62 | \sa QChartView |
|
63 | 63 | */ |
|
64 | 64 | |
|
65 | 65 | /*! |
|
66 | 66 | Constructs a chart object which is a child of a\a parent. Parameter \a wFlags is passed to the QGraphicsWidget constructor. |
|
67 | 67 | */ |
|
68 | 68 | QChart::QChart(QGraphicsItem *parent, Qt::WindowFlags wFlags) : QGraphicsWidget(parent,wFlags), |
|
69 | 69 | d_ptr(new QChartPrivate()) |
|
70 | 70 | { |
|
71 | d_ptr->m_legend = new QLegend(this); | |
|
71 | d_ptr->m_legend = new ScrolledQLegend(this); | |
|
72 | 72 | d_ptr->m_dataset = new ChartDataSet(this); |
|
73 | 73 | d_ptr->m_presenter = new ChartPresenter(this,d_ptr->m_dataset); |
|
74 | 74 | d_ptr->m_presenter->setTheme(QChart::ChartThemeLight, false); |
|
75 | 75 | //TODO:fix me setMinimumSize(d_ptr->m_padding.left() * 3, d_ptr->m_padding.top() * 3); |
|
76 | 76 | connect(d_ptr->m_dataset,SIGNAL(seriesAdded(QSeries*,Domain*)),d_ptr->m_legend,SLOT(handleSeriesAdded(QSeries*,Domain*))); |
|
77 | 77 | connect(d_ptr->m_dataset,SIGNAL(seriesRemoved(QSeries*)),d_ptr->m_legend,SLOT(handleSeriesRemoved(QSeries*))); |
|
78 | 78 | } |
|
79 | 79 | |
|
80 | 80 | /*! |
|
81 | 81 | Destroys the object and it's children, like QChartSeries and QChartAxis object added to it. |
|
82 | 82 | */ |
|
83 | 83 | QChart::~QChart() |
|
84 | 84 | { |
|
85 | 85 | //delete first presenter , since this is a root of all the graphical items |
|
86 | 86 | delete d_ptr->m_presenter; |
|
87 | 87 | d_ptr->m_presenter=0; |
|
88 | 88 | } |
|
89 | 89 | |
|
90 | 90 | /*! |
|
91 | 91 | Adds the \a series and optional \a axisY onto the chart and takes the ownership of the objects. |
|
92 | 92 | If auto scaling is enabled, re-scales the axes the series is bound to (both the x axis and |
|
93 | 93 | the y axis). |
|
94 | 94 | */ |
|
95 | 95 | void QChart::addSeries(QSeries* series, QChartAxis* axisY) |
|
96 | 96 | { |
|
97 | 97 | d_ptr->m_dataset->addSeries(series, axisY); |
|
98 | 98 | } |
|
99 | 99 | |
|
100 | 100 | /*! |
|
101 | 101 | Removes the \a series specified in a perameter from the QChartView. |
|
102 | 102 | It releses its ownership of the specified QChartSeries object. |
|
103 | 103 | It does not delete the pointed QChartSeries data object |
|
104 | 104 | \sa addSeries(), removeAllSeries() |
|
105 | 105 | */ |
|
106 | 106 | void QChart::removeSeries(QSeries* series) |
|
107 | 107 | { |
|
108 | 108 | d_ptr->m_dataset->removeSeries(series); |
|
109 | 109 | } |
|
110 | 110 | |
|
111 | 111 | /*! |
|
112 | 112 | Removes all the QChartSeries that have been added to the QChartView |
|
113 | 113 | It also deletes the pointed QChartSeries data objects |
|
114 | 114 | \sa addSeries(), removeSeries() |
|
115 | 115 | */ |
|
116 | 116 | void QChart::removeAllSeries() |
|
117 | 117 | { |
|
118 | 118 | d_ptr->m_dataset->removeAllSeries(); |
|
119 | 119 | } |
|
120 | 120 | |
|
121 | 121 | /*! |
|
122 | 122 | Sets the \a brush that is used for painting the background of the chart area. |
|
123 | 123 | */ |
|
124 | 124 | void QChart::setBackgroundBrush(const QBrush& brush) |
|
125 | 125 | { |
|
126 | 126 | d_ptr->m_presenter->createChartBackgroundItem(); |
|
127 | 127 | d_ptr->m_presenter->m_backgroundItem->setBrush(brush); |
|
128 | 128 | d_ptr->m_presenter->m_backgroundItem->update(); |
|
129 | 129 | } |
|
130 | 130 | |
|
131 | 131 | QBrush QChart::backgroundBrush() const |
|
132 | 132 | { |
|
133 | 133 | if (!d_ptr->m_presenter->m_backgroundItem) return QBrush(); |
|
134 | 134 | return (d_ptr->m_presenter->m_backgroundItem)->brush(); |
|
135 | 135 | } |
|
136 | 136 | |
|
137 | 137 | /*! |
|
138 | 138 | Sets the \a pen that is used for painting the background of the chart area. |
|
139 | 139 | */ |
|
140 | 140 | void QChart::setBackgroundPen(const QPen& pen) |
|
141 | 141 | { |
|
142 | 142 | d_ptr->m_presenter->createChartBackgroundItem(); |
|
143 | 143 | d_ptr->m_presenter->m_backgroundItem->setPen(pen); |
|
144 | 144 | d_ptr->m_presenter->m_backgroundItem->update(); |
|
145 | 145 | } |
|
146 | 146 | |
|
147 | 147 | QPen QChart::backgroundPen() const |
|
148 | 148 | { |
|
149 | 149 | if (!d_ptr->m_presenter->m_backgroundItem) return QPen(); |
|
150 | 150 | return d_ptr->m_presenter->m_backgroundItem->pen(); |
|
151 | 151 | } |
|
152 | 152 | |
|
153 | 153 | /*! |
|
154 | 154 | Sets the chart \a title. The description text that is drawn above the chart. |
|
155 | 155 | */ |
|
156 | 156 | void QChart::setTitle(const QString& title) |
|
157 | 157 | { |
|
158 | 158 | d_ptr->m_presenter->createChartTitleItem(); |
|
159 | 159 | d_ptr->m_presenter->m_titleItem->setText(title); |
|
160 | 160 | d_ptr->m_presenter->updateLayout(); |
|
161 | 161 | } |
|
162 | 162 | |
|
163 | 163 | /*! |
|
164 | 164 | Returns the chart title. The description text that is drawn above the chart. |
|
165 | 165 | */ |
|
166 | 166 | QString QChart::title() const |
|
167 | 167 | { |
|
168 | 168 | if (d_ptr->m_presenter->m_titleItem) |
|
169 | 169 | return d_ptr->m_presenter->m_titleItem->text(); |
|
170 | 170 | else |
|
171 | 171 | return QString(); |
|
172 | 172 | } |
|
173 | 173 | |
|
174 | 174 | /*! |
|
175 | 175 | Sets the \a font that is used for rendering the description text that is rendered above the chart. |
|
176 | 176 | */ |
|
177 | 177 | void QChart::setTitleFont(const QFont& font) |
|
178 | 178 | { |
|
179 | 179 | d_ptr->m_presenter->createChartTitleItem(); |
|
180 | 180 | d_ptr->m_presenter->m_titleItem->setFont(font); |
|
181 | 181 | d_ptr->m_presenter->updateLayout(); |
|
182 | 182 | } |
|
183 | 183 | |
|
184 | 184 | /*! |
|
185 | 185 | Sets the \a brush used for rendering the title text. |
|
186 | 186 | */ |
|
187 | 187 | void QChart::setTitleBrush(const QBrush &brush) |
|
188 | 188 | { |
|
189 | 189 | d_ptr->m_presenter->createChartTitleItem(); |
|
190 | 190 | d_ptr->m_presenter->m_titleItem->setBrush(brush); |
|
191 | 191 | d_ptr->m_presenter->updateLayout(); |
|
192 | 192 | } |
|
193 | 193 | |
|
194 | 194 | /*! |
|
195 | 195 | Returns the brush used for rendering the title text. |
|
196 | 196 | */ |
|
197 | 197 | QBrush QChart::titleBrush() const |
|
198 | 198 | { |
|
199 | 199 | if (!d_ptr->m_presenter->m_titleItem) return QBrush(); |
|
200 | 200 | return d_ptr->m_presenter->m_titleItem->brush(); |
|
201 | 201 | } |
|
202 | 202 | |
|
203 | 203 | /*! |
|
204 | 204 | Sets the \a theme used by the chart for rendering the graphical representation of the data |
|
205 | 205 | \sa ChartTheme, chartTheme() |
|
206 | 206 | */ |
|
207 | 207 | void QChart::setTheme(QChart::ChartTheme theme) |
|
208 | 208 | { |
|
209 | 209 | d_ptr->m_presenter->setTheme(theme); |
|
210 | 210 | } |
|
211 | 211 | |
|
212 | 212 | /*! |
|
213 | 213 | Returns the theme enum used by the chart. |
|
214 | 214 | \sa ChartTheme, setChartTheme() |
|
215 | 215 | */ |
|
216 | 216 | QChart::ChartTheme QChart::theme() const |
|
217 | 217 | { |
|
218 | 218 | return d_ptr->m_presenter->theme(); |
|
219 | 219 | } |
|
220 | 220 | |
|
221 | 221 | /*! |
|
222 | 222 | Zooms in the view by a factor of 2 |
|
223 | 223 | */ |
|
224 | 224 | void QChart::zoomIn() |
|
225 | 225 | { |
|
226 | 226 | d_ptr->m_presenter->zoomIn(); |
|
227 | 227 | } |
|
228 | 228 | |
|
229 | 229 | /*! |
|
230 | 230 | Zooms in the view to a maximum level at which \a rect is still fully visible. |
|
231 | 231 | */ |
|
232 | 232 | void QChart::zoomIn(const QRectF& rect) |
|
233 | 233 | { |
|
234 | 234 | if (!rect.isValid()) return; |
|
235 | 235 | d_ptr->m_presenter->zoomIn(rect); |
|
236 | 236 | } |
|
237 | 237 | |
|
238 | 238 | /*! |
|
239 | 239 | Restores the view zoom level to the previous one. |
|
240 | 240 | */ |
|
241 | 241 | void QChart::zoomOut() |
|
242 | 242 | { |
|
243 | 243 | d_ptr->m_presenter->zoomOut(); |
|
244 | 244 | } |
|
245 | 245 | |
|
246 | 246 | /*! |
|
247 | 247 | Returns the pointer to the x axis object of the chart |
|
248 | 248 | */ |
|
249 | 249 | QChartAxis* QChart::axisX() const |
|
250 | 250 | { |
|
251 | 251 | return d_ptr->m_dataset->axisX(); |
|
252 | 252 | } |
|
253 | 253 | |
|
254 | 254 | /*! |
|
255 | 255 | Returns the pointer to the y axis object of the chart |
|
256 | 256 | */ |
|
257 | 257 | QChartAxis* QChart::axisY() const |
|
258 | 258 | { |
|
259 | 259 | return d_ptr->m_dataset->axisY(); |
|
260 | 260 | } |
|
261 | 261 | |
|
262 | 262 | /*! |
|
263 | 263 | Returns the legend object of the chart. Ownership stays in chart. |
|
264 | 264 | */ |
|
265 | 265 | QLegend* QChart::legend() const |
|
266 | 266 | { |
|
267 | 267 | return d_ptr->m_legend; |
|
268 | 268 | } |
|
269 | 269 | |
|
270 | 270 | QRect QChart::margins() const |
|
271 | 271 | { |
|
272 | 272 | return d_ptr->m_presenter->margins(); |
|
273 | 273 | } |
|
274 | 274 | |
|
275 | 275 | |
|
276 | 276 | /*! |
|
277 | 277 | Resizes and updates the chart area using the \a event data |
|
278 | 278 | */ |
|
279 | 279 | void QChart::resizeEvent(QGraphicsSceneResizeEvent *event) |
|
280 | 280 | { |
|
281 | 281 | d_ptr->m_rect = QRectF(QPoint(0,0),event->newSize()); |
|
282 | 282 | QGraphicsWidget::resizeEvent(event); |
|
283 | 283 | d_ptr->m_presenter->setGeometry(d_ptr->m_rect); |
|
284 | 284 | } |
|
285 | 285 | |
|
286 | 286 | /*! |
|
287 | 287 | Sets animation \a options for the chart |
|
288 | 288 | */ |
|
289 | 289 | void QChart::setAnimationOptions(AnimationOptions options) |
|
290 | 290 | { |
|
291 | 291 | d_ptr->m_presenter->setAnimationOptions(options); |
|
292 | 292 | } |
|
293 | 293 | |
|
294 | 294 | /*! |
|
295 | 295 | Returns animation options for the chart |
|
296 | 296 | */ |
|
297 | 297 | QChart::AnimationOptions QChart::animationOptions() const |
|
298 | 298 | { |
|
299 | 299 | return d_ptr->m_presenter->animationOptions(); |
|
300 | 300 | } |
|
301 | 301 | |
|
302 | 302 | void QChart::scrollLeft() |
|
303 | 303 | { |
|
304 | 304 | d_ptr->m_presenter->scroll(-d_ptr->m_presenter->chartGeometry().width()/(axisX()->ticksCount()-1),0); |
|
305 | 305 | } |
|
306 | 306 | |
|
307 | 307 | void QChart::scrollRight() |
|
308 | 308 | { |
|
309 | 309 | d_ptr->m_presenter->scroll(d_ptr->m_presenter->chartGeometry().width()/(axisX()->ticksCount()-1),0); |
|
310 | 310 | } |
|
311 | 311 | |
|
312 | 312 | void QChart::scrollUp() |
|
313 | 313 | { |
|
314 | 314 | d_ptr->m_presenter->scroll(0,d_ptr->m_presenter->chartGeometry().width()/(axisY()->ticksCount()-1)); |
|
315 | 315 | } |
|
316 | 316 | |
|
317 | 317 | void QChart::scrollDown() |
|
318 | 318 | { |
|
319 | 319 | d_ptr->m_presenter->scroll(0,-d_ptr->m_presenter->chartGeometry().width()/(axisY()->ticksCount()-1)); |
|
320 | 320 | } |
|
321 | 321 | |
|
322 | 322 | void QChart::setBackgroundVisible(bool visible) |
|
323 | 323 | { |
|
324 | 324 | d_ptr->m_presenter->createChartBackgroundItem(); |
|
325 | 325 | d_ptr->m_presenter->m_backgroundItem->setVisible(visible); |
|
326 | 326 | } |
|
327 | 327 | |
|
328 | 328 | bool QChart::isBackgroundVisible() const |
|
329 | 329 | { |
|
330 | 330 | if (!d_ptr->m_presenter->m_backgroundItem) return false; |
|
331 | 331 | return d_ptr->m_presenter->m_backgroundItem->isVisible(); |
|
332 | 332 | } |
|
333 | 333 | |
|
334 | 334 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
|
335 | 335 | |
|
336 | 336 | QChartPrivate::QChartPrivate(): |
|
337 | 337 | m_legend(0), |
|
338 | 338 | m_dataset(0), |
|
339 | 339 | m_presenter(0) |
|
340 | 340 | { |
|
341 | 341 | |
|
342 | 342 | } |
|
343 | 343 | |
|
344 | 344 | QChartPrivate::~QChartPrivate() |
|
345 | 345 | { |
|
346 | 346 | |
|
347 | 347 | } |
|
348 | 348 | |
|
349 | 349 | #include "moc_qchart.cpp" |
|
350 | 350 | |
|
351 | 351 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,563 +1,546 | |||
|
1 | 1 | /**************************************************************************** |
|
2 | 2 | ** |
|
3 | 3 | ** Copyright (C) 2012 Digia Plc |
|
4 | 4 | ** All rights reserved. |
|
5 | 5 | ** For any questions to Digia, please use contact form at http://qt.digia.com |
|
6 | 6 | ** |
|
7 | 7 | ** This file is part of the Qt Commercial Charts Add-on. |
|
8 | 8 | ** |
|
9 | 9 | ** $QT_BEGIN_LICENSE$ |
|
10 | 10 | ** Licensees holding valid Qt Commercial licenses may use this file in |
|
11 | 11 | ** accordance with the Qt Commercial License Agreement provided with the |
|
12 | 12 | ** Software or, alternatively, in accordance with the terms contained in |
|
13 | 13 | ** a written agreement between you and Digia. |
|
14 | 14 | ** |
|
15 | 15 | ** If you have questions regarding the use of this file, please use |
|
16 | 16 | ** contact form at http://qt.digia.com |
|
17 | 17 | ** $QT_END_LICENSE$ |
|
18 | 18 | ** |
|
19 | 19 | ****************************************************************************/ |
|
20 | 20 | |
|
21 | 21 | #include "qlegend.h" |
|
22 | 22 | #include "qchart_p.h" |
|
23 | 23 | #include "qseries.h" |
|
24 | 24 | #include "legendmarker_p.h" |
|
25 | 25 | #include "qxyseries.h" |
|
26 | 26 | #include "qlineseries.h" |
|
27 | 27 | #include "qareaseries.h" |
|
28 | 28 | #include "qscatterseries.h" |
|
29 | 29 | #include "qsplineseries.h" |
|
30 | 30 | #include "qbarseries.h" |
|
31 | 31 | #include "qstackedbarseries.h" |
|
32 | 32 | #include "qpercentbarseries.h" |
|
33 | 33 | #include "qbarset.h" |
|
34 | 34 | #include "qpieseries.h" |
|
35 | 35 | #include "qpieslice.h" |
|
36 | 36 | #include "chartpresenter_p.h" |
|
37 | 37 | #include "scroller_p.h" |
|
38 | 38 | #include <QPainter> |
|
39 | 39 | #include <QPen> |
|
40 | 40 | #include <QTimer> |
|
41 | 41 | |
|
42 | 42 | #include <QGraphicsSceneEvent> |
|
43 | 43 | |
|
44 | 44 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
45 | 45 | |
|
46 | 46 | /*! |
|
47 | 47 | \class QLegend |
|
48 | 48 | \brief part of QtCommercial chart API. |
|
49 | 49 | |
|
50 | 50 | QLegend is a graphical object, whics displays legend of the chart. Legend state is updated by QChart, when |
|
51 | 51 | series have been changed. By default, legend is drawn by QChart, but user can set a new parent to legend and |
|
52 | 52 | handle the drawing manually. |
|
53 | 53 | User isn't supposed to create or delete legend objects, but can reference it via QChart class. |
|
54 | 54 | |
|
55 | 55 | \mainclass |
|
56 | 56 | |
|
57 | 57 | \sa QChart, QSeries |
|
58 | 58 | */ |
|
59 | 59 | |
|
60 | 60 | /*! |
|
61 | 61 | \enum QLegend::Layout |
|
62 | 62 | |
|
63 | 63 | This enum describes the possible position for legend inside chart. |
|
64 | 64 | |
|
65 | 65 | \value LayoutTop |
|
66 | 66 | \value LayoutBottom |
|
67 | 67 | \value LayoutLeft |
|
68 | 68 | \value LayoutRight |
|
69 | 69 | */ |
|
70 | 70 | |
|
71 | 71 | |
|
72 | 72 | /*! |
|
73 | 73 | \fn void QLegend::clicked(QSeries* series, Qt::MouseButton button) |
|
74 | 74 | \brief Notifies when series has been clicked on legend \a series \a button |
|
75 | 75 | */ |
|
76 | 76 | |
|
77 | 77 | /*! |
|
78 | 78 | \fn void QLegend::clicked(QBarSet* barset, Qt::MouseButton button) |
|
79 | 79 | \brief Notifies when barset has been clicked on legend \a barset \a button |
|
80 | 80 | */ |
|
81 | 81 | |
|
82 | 82 | /*! |
|
83 | 83 | \fn void QLegend::clicked(QPieSlice* slice, Qt::MouseButton button) |
|
84 | 84 | \brief Notifies when pie slice has been clicked on legend \a slice \a button |
|
85 | 85 | */ |
|
86 | 86 | |
|
87 | 87 | /*! |
|
88 | 88 | Constructs the legend object and sets the parent to \a parent |
|
89 | 89 | */ |
|
90 | 90 | |
|
91 | 91 | QLegend::QLegend(QChart *chart):QGraphicsWidget(chart), |
|
92 | 92 | m_margin(5), |
|
93 | 93 | m_offsetX(0), |
|
94 | 94 | m_offsetY(0), |
|
95 | 95 | m_brush(Qt::darkGray), // TODO: default should come from theme |
|
96 | 96 | m_alignment(QLegend::AlignmentTop), |
|
97 | 97 | m_markers(new QGraphicsItemGroup(this)), |
|
98 | 98 | m_attachedToChart(true), |
|
99 | 99 | m_chart(chart), |
|
100 | 100 | m_minWidth(0), |
|
101 | 101 | m_minHeight(0), |
|
102 | 102 | m_width(0), |
|
103 | 103 | m_height(0), |
|
104 | 104 | m_visible(false), |
|
105 |
m_dirty(false) |
|
|
106 | m_scroller(new Scroller(this)) | |
|
105 | m_dirty(false) | |
|
107 | 106 | { |
|
108 | 107 | setZValue(ChartPresenter::LegendZValue); |
|
109 | 108 | setFlags(QGraphicsItem::ItemClipsChildrenToShape); |
|
110 | 109 | } |
|
111 | 110 | |
|
112 | 111 | /*! |
|
113 | 112 | Paints the legend to given \a painter. Paremeters \a option and \a widget arent used. |
|
114 | 113 | */ |
|
115 | 114 | |
|
116 | 115 | void QLegend::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) |
|
117 | 116 | { |
|
118 | 117 | Q_UNUSED(option) |
|
119 | 118 | Q_UNUSED(widget) |
|
120 | 119 | if(!m_visible) return; |
|
121 | 120 | |
|
122 | 121 | painter->setOpacity(opacity()); |
|
123 | 122 | painter->setPen(m_pen); |
|
124 | 123 | painter->setBrush(m_brush); |
|
125 | 124 | painter->drawRect(boundingRect()); |
|
126 | 125 | } |
|
127 | 126 | |
|
128 | 127 | /*! |
|
129 | 128 | Bounding rect of legend. |
|
130 | 129 | */ |
|
131 | 130 | |
|
132 | 131 | QRectF QLegend::boundingRect() const |
|
133 | 132 | { |
|
134 | 133 | return m_rect; |
|
135 | 134 | } |
|
136 | 135 | |
|
137 | 136 | /*! |
|
138 | 137 | Sets the \a brush of legend. Brush affects the background of legend. |
|
139 | 138 | */ |
|
140 | 139 | void QLegend::setBrush(const QBrush &brush) |
|
141 | 140 | { |
|
142 | 141 | if (m_brush != brush) { |
|
143 | 142 | m_brush = brush; |
|
144 | 143 | update(); |
|
145 | 144 | } |
|
146 | 145 | } |
|
147 | 146 | |
|
148 | 147 | /*! |
|
149 | 148 | Returns the brush used by legend. |
|
150 | 149 | */ |
|
151 | 150 | QBrush QLegend::brush() const |
|
152 | 151 | { |
|
153 | 152 | return m_brush; |
|
154 | 153 | } |
|
155 | 154 | |
|
156 | 155 | /*! |
|
157 | 156 | Sets the \a pen of legend. Pen affects the legend borders. |
|
158 | 157 | */ |
|
159 | 158 | void QLegend::setPen(const QPen &pen) |
|
160 | 159 | { |
|
161 | 160 | if (m_pen != pen) { |
|
162 | 161 | m_pen = pen; |
|
163 | 162 | update(); |
|
164 | 163 | } |
|
165 | 164 | } |
|
166 | 165 | |
|
167 | 166 | /*! |
|
168 | 167 | Returns the pen used by legend |
|
169 | 168 | */ |
|
170 | 169 | |
|
171 | 170 | QPen QLegend::pen() const |
|
172 | 171 | { |
|
173 | 172 | return m_pen; |
|
174 | 173 | } |
|
175 | 174 | |
|
176 | 175 | /*! |
|
177 | 176 | Sets the \a preferred layout for legend. Legend tries to paint itself on the defined position in chart. |
|
178 | 177 | \sa QLegend::Layout |
|
179 | 178 | */ |
|
180 | 179 | void QLegend::setAlignmnent(QLegend::Alignments alignment) |
|
181 | 180 | { |
|
182 | 181 | if(m_alignment!=alignment && m_attachedToChart) { |
|
183 | 182 | m_alignment = alignment; |
|
184 | 183 | updateLayout(); |
|
185 | 184 | } |
|
186 | 185 | } |
|
187 | 186 | |
|
188 | 187 | /*! |
|
189 | 188 | Returns the preferred layout for legend |
|
190 | 189 | */ |
|
191 | 190 | QLegend::Alignments QLegend::alignment() const |
|
192 | 191 | { |
|
193 | 192 | return m_alignment; |
|
194 | 193 | } |
|
195 | 194 | |
|
196 | 195 | /*! |
|
197 | 196 | \internal \a series \a domain Should be called when series is added to chart. |
|
198 | 197 | */ |
|
199 | 198 | void QLegend::handleSeriesAdded(QSeries *series, Domain *domain) |
|
200 | 199 | { |
|
201 | 200 | Q_UNUSED(domain) |
|
202 | 201 | |
|
203 | 202 | switch (series->type()) |
|
204 | 203 | { |
|
205 | 204 | case QSeries::SeriesTypeLine: { |
|
206 | 205 | QLineSeries *lineSeries = static_cast<QLineSeries *>(series); |
|
207 | 206 | appendMarkers(lineSeries); |
|
208 | 207 | break; |
|
209 | 208 | } |
|
210 | 209 | case QSeries::SeriesTypeArea: { |
|
211 | 210 | QAreaSeries *areaSeries = static_cast<QAreaSeries *>(series); |
|
212 | 211 | appendMarkers(areaSeries); |
|
213 | 212 | break; |
|
214 | 213 | } |
|
215 | 214 | case QSeries::SeriesTypeBar: { |
|
216 | 215 | QBarSeries *barSeries = static_cast<QBarSeries *>(series); |
|
217 | 216 | appendMarkers(barSeries); |
|
218 | 217 | break; |
|
219 | 218 | } |
|
220 | 219 | case QSeries::SeriesTypeStackedBar: { |
|
221 | 220 | QStackedBarSeries *stackedBarSeries = static_cast<QStackedBarSeries *>(series); |
|
222 | 221 | appendMarkers(stackedBarSeries); |
|
223 | 222 | break; |
|
224 | 223 | } |
|
225 | 224 | case QSeries::SeriesTypePercentBar: { |
|
226 | 225 | QPercentBarSeries *percentBarSeries = static_cast<QPercentBarSeries *>(series); |
|
227 | 226 | appendMarkers(percentBarSeries); |
|
228 | 227 | break; |
|
229 | 228 | } |
|
230 | 229 | case QSeries::SeriesTypeScatter: { |
|
231 | 230 | QScatterSeries *scatterSeries = static_cast<QScatterSeries *>(series); |
|
232 | 231 | appendMarkers(scatterSeries); |
|
233 | 232 | break; |
|
234 | 233 | } |
|
235 | 234 | case QSeries::SeriesTypePie: { |
|
236 | 235 | QPieSeries *pieSeries = static_cast<QPieSeries *>(series); |
|
237 | 236 | appendMarkers(pieSeries); |
|
238 | 237 | connect(pieSeries,SIGNAL(added(QList<QPieSlice*>)),this,SLOT(handleAdded(QList<QPieSlice*>))); |
|
239 | 238 | break; |
|
240 | 239 | } |
|
241 | 240 | case QSeries::SeriesTypeSpline: { |
|
242 | 241 | QSplineSeries *splineSeries = static_cast<QSplineSeries *>(series); |
|
243 | 242 | appendMarkers(splineSeries); |
|
244 | 243 | break; |
|
245 | 244 | } |
|
246 | 245 | default: { |
|
247 | 246 | qWarning()<< "QLegend::handleSeriesAdded" << series->type() << "unknown series type."; |
|
248 | 247 | break; |
|
249 | 248 | } |
|
250 | 249 | } |
|
251 | 250 | |
|
252 | 251 | // wait for all series added |
|
253 | 252 | if(!m_dirty){ |
|
254 | 253 | QTimer::singleShot(0,this,SLOT(updateLayout())); |
|
255 | 254 | m_dirty=true; |
|
256 | 255 | } |
|
257 | 256 | } |
|
258 | 257 | |
|
259 | 258 | /*! |
|
260 | 259 | \internal \a series Should be called when series is removed from chart. |
|
261 | 260 | */ |
|
262 | 261 | void QLegend::handleSeriesRemoved(QSeries *series) |
|
263 | 262 | { |
|
264 | 263 | switch (series->type()) |
|
265 | 264 | { |
|
266 | 265 | case QSeries::SeriesTypeArea: { |
|
267 | 266 | QAreaSeries *areaSeries = static_cast<QAreaSeries *>(series); |
|
268 | 267 | deleteMarkers(areaSeries); |
|
269 | 268 | break; |
|
270 | 269 | } |
|
271 | 270 | case QSeries::SeriesTypePie: { |
|
272 | 271 | QPieSeries *pieSeries = static_cast<QPieSeries *>(series); |
|
273 | 272 | disconnect(pieSeries, SIGNAL(added(QList<QPieSlice *>)), this, SLOT(handleAdded(QList<QPieSlice *>))); |
|
274 | 273 | deleteMarkers(series); |
|
275 | 274 | break; |
|
276 | 275 | } |
|
277 | 276 | default: { |
|
278 | 277 | // All other types |
|
279 | 278 | deleteMarkers(series); |
|
280 | 279 | break; |
|
281 | 280 | } |
|
282 | 281 | } |
|
283 | 282 | |
|
284 | 283 | updateLayout(); |
|
285 | 284 | } |
|
286 | 285 | |
|
287 | 286 | /*! |
|
288 | 287 | \internal \a slices Should be called when slices are added to pie chart. |
|
289 | 288 | */ |
|
290 | 289 | void QLegend::handleAdded(QList<QPieSlice *> slices) |
|
291 | 290 | { |
|
292 | 291 | QPieSeries* series = static_cast<QPieSeries *> (sender()); |
|
293 | 292 | foreach(QPieSlice* slice, slices) { |
|
294 | 293 | PieLegendMarker* marker = new PieLegendMarker(series,slice, this); |
|
295 | 294 | connect(marker, SIGNAL(destroyed()), this, SLOT(handleMarkerDestroyed())); |
|
296 | 295 | m_markers->addToGroup(marker); |
|
297 | 296 | } |
|
298 | 297 | updateLayout(); |
|
299 | 298 | } |
|
300 | 299 | |
|
301 | 300 | /*! |
|
302 | 301 | \internal \a slices Should be called when slices are removed from pie chart. Currently unused, |
|
303 | 302 | because removed slices are also deleted and we listen destroyed signal |
|
304 | 303 | */ |
|
305 | 304 | void QLegend::handleRemoved(QList<QPieSlice *> slices) |
|
306 | 305 | { |
|
307 | 306 | Q_UNUSED(slices) |
|
308 | 307 | } |
|
309 | 308 | |
|
310 | 309 | |
|
311 | 310 | /*! |
|
312 | 311 | \internal Notifies legend that some marker has been removed. Sent by legend markers when destroyed |
|
313 | 312 | */ |
|
314 | 313 | void QLegend::handleMarkerDestroyed() |
|
315 | 314 | { |
|
316 | 315 | LegendMarker* m = static_cast<LegendMarker *> (sender()); |
|
317 | 316 | delete m; |
|
318 | 317 | // updateLayout(); |
|
319 | 318 | } |
|
320 | 319 | |
|
321 | 320 | /*! |
|
322 | 321 | Detaches the legend from chart. Chart won't change layout of the legend. |
|
323 | 322 | */ |
|
324 | 323 | void QLegend::detachFromChart() |
|
325 | 324 | { |
|
326 | 325 | m_attachedToChart = false; |
|
327 | 326 | } |
|
328 | 327 | |
|
329 | 328 | /*! |
|
330 | 329 | Attaches the legend to chart. Chart may change layout of the legend. |
|
331 | 330 | */ |
|
332 | 331 | void QLegend::attachToChart() |
|
333 | 332 | { |
|
334 | 333 | m_attachedToChart = true; |
|
335 | 334 | } |
|
336 | 335 | |
|
337 | 336 | /*! |
|
338 | 337 | Returns true, if legend is attached to chart. |
|
339 | 338 | */ |
|
340 | 339 | bool QLegend::isAttachedToChart() |
|
341 | 340 | { |
|
342 | 341 | return m_attachedToChart; |
|
343 | 342 | } |
|
344 | 343 | |
|
345 | 344 | /*! |
|
346 | 345 | \internal Helper function. Appends markers from \a series to legend. |
|
347 | 346 | */ |
|
348 | 347 | void QLegend::appendMarkers(QAreaSeries* series) |
|
349 | 348 | { |
|
350 | 349 | AreaLegendMarker* marker = new AreaLegendMarker(series,this); |
|
351 | 350 | connect(marker, SIGNAL(destroyed()), this, SLOT(handleMarkerDestroyed())); |
|
352 | 351 | m_markers->addToGroup(marker); |
|
353 | 352 | } |
|
354 | 353 | |
|
355 | 354 | /*! |
|
356 | 355 | \internal Helper function. Appends markers from \a series to legend. |
|
357 | 356 | */ |
|
358 | 357 | void QLegend::appendMarkers(QXYSeries* series) |
|
359 | 358 | { |
|
360 | 359 | XYLegendMarker* marker = new XYLegendMarker(series,this); |
|
361 | 360 | connect(marker, SIGNAL(destroyed()), this, SLOT(handleMarkerDestroyed())); |
|
362 | 361 | m_markers->addToGroup(marker); |
|
363 | 362 | } |
|
364 | 363 | |
|
365 | 364 | /*! |
|
366 | 365 | \internal Helper function. Appends markers from \a series to legend. |
|
367 | 366 | */ |
|
368 | 367 | void QLegend::appendMarkers(QBarSeries *series) |
|
369 | 368 | { |
|
370 | 369 | foreach(QBarSet* set, series->barSets()) { |
|
371 | 370 | BarLegendMarker* marker = new BarLegendMarker(series,set, this); |
|
372 | 371 | connect(marker, SIGNAL(destroyed()), this, SLOT(handleMarkerDestroyed())); |
|
373 | 372 | m_markers->addToGroup(marker); |
|
374 | 373 | } |
|
375 | 374 | } |
|
376 | 375 | |
|
377 | 376 | /*! |
|
378 | 377 | \internal Helper function. Appends markers from \a series to legend. |
|
379 | 378 | */ |
|
380 | 379 | void QLegend::appendMarkers(QPieSeries *series) |
|
381 | 380 | { |
|
382 | 381 | foreach(QPieSlice* slice, series->slices()) { |
|
383 | 382 | PieLegendMarker* marker = new PieLegendMarker(series,slice, this); |
|
384 | 383 | connect(marker, SIGNAL(destroyed()), this, SLOT(handleMarkerDestroyed())); |
|
385 | 384 | m_markers->addToGroup(marker); |
|
386 | 385 | } |
|
387 | 386 | } |
|
388 | 387 | |
|
389 | 388 | /*! |
|
390 | 389 | \internal Deletes all markers that are created from \a series |
|
391 | 390 | */ |
|
392 | 391 | void QLegend::deleteMarkers(QSeries *series) |
|
393 | 392 | { |
|
394 | 393 | // Search all markers that belong to given series and delete them. |
|
395 | 394 | |
|
396 | 395 | QList<QGraphicsItem *> items = m_markers->childItems(); |
|
397 | 396 | |
|
398 | 397 | foreach (QGraphicsItem *m, items) { |
|
399 | 398 | LegendMarker *marker = static_cast<LegendMarker*>(m); |
|
400 | 399 | if (marker->series() == series) { |
|
401 | 400 | delete marker; |
|
402 | 401 | } |
|
403 | 402 | } |
|
404 | 403 | } |
|
405 | 404 | |
|
406 | 405 | /*! |
|
407 | 406 | \internal Updates layout of legend. Tries to fit as many markers as possible up to the maximum size of legend. |
|
408 | 407 | If items don't fit, sets the visibility of scroll buttons accordingly. |
|
409 | 408 | Causes legend to be resized. |
|
410 | 409 | */ |
|
411 | 410 | |
|
412 | 411 | void QLegend::setOffset(const QPointF& point) |
|
413 | 412 | { |
|
414 | 413 | |
|
415 | 414 | switch(m_alignment) { |
|
416 | 415 | |
|
417 | 416 | case AlignmentTop: |
|
418 | 417 | case AlignmentBottom: { |
|
419 | 418 | if(m_width<=m_rect.width()) return; |
|
420 | 419 | |
|
421 | 420 | if (point.x() != m_offsetX) { |
|
422 | 421 | m_offsetX = qBound(0.0, point.x(), m_width - m_rect.width()); |
|
423 | 422 | m_markers->setPos(-m_offsetX,m_rect.top()); |
|
424 | 423 | } |
|
425 | 424 | break; |
|
426 | 425 | } |
|
427 | 426 | case AlignmentLeft: |
|
428 | 427 | case AlignmentRight: { |
|
429 | 428 | |
|
430 | 429 | if(m_height<=m_rect.height()) return; |
|
431 | 430 | |
|
432 | 431 | if (point.y() != m_offsetY) { |
|
433 | 432 | m_offsetY = qBound(0.0, point.y(), m_height - m_rect.height()); |
|
434 | 433 | m_markers->setPos(m_rect.left(),-m_offsetY); |
|
435 | 434 | } |
|
436 | 435 | break; |
|
437 | 436 | } |
|
438 | 437 | } |
|
439 | 438 | } |
|
440 | 439 | |
|
441 | 440 | QPointF QLegend::offset() const |
|
442 | 441 | { |
|
443 | 442 | return QPointF(m_offsetX,m_offsetY); |
|
444 | 443 | } |
|
445 | 444 | |
|
446 | 445 | // this function runs first to set min max values |
|
447 | 446 | void QLegend::updateLayout() |
|
448 | 447 | { |
|
449 | 448 | m_dirty=false; |
|
450 | 449 | m_offsetX=0; |
|
451 | 450 | QList<QGraphicsItem *> items = m_markers->childItems(); |
|
452 | 451 | |
|
453 | 452 | if(items.isEmpty()) return; |
|
454 | 453 | |
|
455 | 454 | m_minWidth=0; |
|
456 | 455 | m_minHeight=0; |
|
457 | 456 | |
|
458 | 457 | switch(m_alignment) { |
|
459 | 458 | |
|
460 | 459 | case AlignmentTop: |
|
461 | 460 | case AlignmentBottom: { |
|
462 | 461 | QPointF point = m_rect.topLeft(); |
|
463 | 462 | m_width = 0; |
|
464 | 463 | foreach (QGraphicsItem *item, items) { |
|
465 | 464 | item->setPos(point.x(),m_rect.height()/2 -item->boundingRect().height()/2); |
|
466 | 465 | const QRectF& rect = item->boundingRect(); |
|
467 | 466 | qreal w = rect.width(); |
|
468 | 467 | m_minWidth=qMax(m_minWidth,w); |
|
469 | 468 | m_minHeight=qMax(m_minHeight,rect.height()); |
|
470 | 469 | m_width+=w; |
|
471 | 470 | point.setX(point.x() + w); |
|
472 | 471 | } |
|
473 | 472 | if(m_width<m_rect.width()){ |
|
474 | 473 | m_markers->setPos(m_rect.width()/2-m_width/2,m_rect.top()); |
|
475 | 474 | }else{ |
|
476 | 475 | m_markers->setPos(m_rect.topLeft()); |
|
477 | 476 | } |
|
478 | 477 | m_height=m_minHeight; |
|
479 | 478 | } |
|
480 | 479 | break; |
|
481 | 480 | case AlignmentLeft: |
|
482 | 481 | case AlignmentRight:{ |
|
483 | 482 | QPointF point = m_rect.topLeft(); |
|
484 | 483 | m_height = 0; |
|
485 | 484 | foreach (QGraphicsItem *item, items) { |
|
486 | 485 | item->setPos(point); |
|
487 | 486 | const QRectF& rect = item->boundingRect(); |
|
488 | 487 | qreal h = rect.height(); |
|
489 | 488 | m_minWidth=qMax(m_minWidth,rect.width()); |
|
490 | 489 | m_minHeight=qMax(m_minHeight,h); |
|
491 | 490 | m_height+=h; |
|
492 | 491 | point.setY(point.y() + h); |
|
493 | 492 | } |
|
494 | 493 | if(m_height<m_rect.height()){ |
|
495 | 494 | m_markers->setPos(m_rect.left(),m_rect.height()/2-m_height/2); |
|
496 | 495 | }else{ |
|
497 | 496 | m_markers->setPos(m_rect.topLeft()); |
|
498 | 497 | } |
|
499 | 498 | m_width=m_minWidth; |
|
500 | 499 | } |
|
501 | 500 | break; |
|
502 | 501 | } |
|
503 | 502 | |
|
504 | 503 | m_chart->d_ptr->m_presenter->updateLayout(); //TODO fixme; |
|
505 | 504 | } |
|
506 | 505 | |
|
507 | 506 | void QLegend::setBackgroundVisible(bool visible) |
|
508 | 507 | { |
|
509 | 508 | if(m_visible!=visible) |
|
510 | 509 | { |
|
511 | 510 | m_visible=visible; |
|
512 | 511 | update(); |
|
513 | 512 | } |
|
514 | 513 | } |
|
515 | 514 | |
|
516 | 515 | bool QLegend::isBackgroundVisible() const |
|
517 | 516 | { |
|
518 | 517 | return m_visible; |
|
519 | 518 | } |
|
520 | 519 | |
|
521 | 520 | void QLegend::resizeEvent(QGraphicsSceneResizeEvent *event) |
|
522 | 521 | { |
|
523 | 522 | const QRectF& rect = QRectF(QPoint(0,0),event->newSize()); |
|
524 | 523 | QGraphicsWidget::resizeEvent(event); |
|
525 | 524 | if(m_rect != rect){ |
|
526 | 525 | m_rect = rect; |
|
527 | 526 | updateLayout(); |
|
528 | 527 | } |
|
529 | 528 | } |
|
530 | 529 | |
|
531 | 530 | void QLegend::hideEvent(QHideEvent *event) |
|
532 | 531 | { |
|
533 | 532 | QGraphicsWidget::hideEvent(event); |
|
534 | 533 | setEnabled(false); |
|
535 | 534 | updateLayout(); |
|
536 | 535 | } |
|
537 | 536 | |
|
538 | 537 | void QLegend::showEvent(QShowEvent *event) |
|
539 | 538 | { |
|
540 | 539 | QGraphicsWidget::showEvent(event); |
|
541 | 540 | setEnabled(true); |
|
542 | 541 | updateLayout(); |
|
543 | 542 | } |
|
544 | 543 | |
|
545 | void QLegend::mousePressEvent(QGraphicsSceneMouseEvent* event) | |
|
546 | { | |
|
547 | //Q_UNUSED(event); | |
|
548 | m_scroller->mousePressEvent(event); | |
|
549 | } | |
|
550 | void QLegend::mouseMoveEvent(QGraphicsSceneMouseEvent* event) | |
|
551 | { | |
|
552 | //Q_UNUSED(event); | |
|
553 | m_scroller->mouseMoveEvent(event); | |
|
554 | } | |
|
555 | void QLegend::mouseReleaseEvent(QGraphicsSceneMouseEvent* event) | |
|
556 | { | |
|
557 | //Q_UNUSED(event); | |
|
558 | m_scroller->mouseReleaseEvent(event); | |
|
559 | } | |
|
560 | ||
|
561 | 544 | #include "moc_qlegend.cpp" |
|
562 | 545 | |
|
563 | 546 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,152 +1,179 | |||
|
1 | 1 | /**************************************************************************** |
|
2 | 2 | ** |
|
3 | 3 | ** Copyright (C) 2012 Digia Plc |
|
4 | 4 | ** All rights reserved. |
|
5 | 5 | ** For any questions to Digia, please use contact form at http://qt.digia.com |
|
6 | 6 | ** |
|
7 | 7 | ** This file is part of the Qt Commercial Charts Add-on. |
|
8 | 8 | ** |
|
9 | 9 | ** $QT_BEGIN_LICENSE$ |
|
10 | 10 | ** Licensees holding valid Qt Commercial licenses may use this file in |
|
11 | 11 | ** accordance with the Qt Commercial License Agreement provided with the |
|
12 | 12 | ** Software or, alternatively, in accordance with the terms contained in |
|
13 | 13 | ** a written agreement between you and Digia. |
|
14 | 14 | ** |
|
15 | 15 | ** If you have questions regarding the use of this file, please use |
|
16 | 16 | ** contact form at http://qt.digia.com |
|
17 | 17 | ** $QT_END_LICENSE$ |
|
18 | 18 | ** |
|
19 | 19 | ****************************************************************************/ |
|
20 | 20 | |
|
21 | 21 | #ifndef QLEGEND_H |
|
22 | 22 | #define QLEGEND_H |
|
23 | 23 | |
|
24 | 24 | #include <QChartGlobal> |
|
25 | 25 | #include <QGraphicsWidget> |
|
26 | 26 | #include <QPen> |
|
27 | 27 | #include <QBrush> |
|
28 | #include "scroller_p.h" | |
|
28 | 29 | |
|
29 | 30 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
30 | 31 | |
|
31 | 32 | class Domain; |
|
32 | 33 | class LegendMarker; |
|
33 | 34 | class QPieSlice; |
|
34 | 35 | class QXYSeries; |
|
35 | 36 | class QBarSet; |
|
36 | 37 | class QBarSeries; |
|
37 | 38 | class QPieSeries; |
|
38 | 39 | class QAreaSeries; |
|
39 | 40 | class LegendScrollButton; |
|
40 | 41 | class QSeries; |
|
41 | 42 | class QChart; |
|
42 | class Scroller; | |
|
43 | 43 | |
|
44 | 44 | class QTCOMMERCIALCHART_EXPORT QLegend : public QGraphicsWidget |
|
45 | 45 | { |
|
46 | 46 | Q_OBJECT |
|
47 | 47 | public: |
|
48 | 48 | |
|
49 | 49 | // We only support these alignments (for now) |
|
50 | 50 | enum Alignment { |
|
51 | 51 | AlignmentTop = Qt::AlignTop, |
|
52 | 52 | AlignmentBottom = Qt::AlignBottom, |
|
53 | 53 | AlignmentLeft = Qt::AlignLeft, |
|
54 | 54 | AlignmentRight = Qt::AlignRight |
|
55 | 55 | }; |
|
56 | 56 | |
|
57 | 57 | Q_DECLARE_FLAGS(Alignments, Alignment) |
|
58 | 58 | |
|
59 | 59 | private: |
|
60 | 60 | explicit QLegend(QChart *chart); |
|
61 | 61 | |
|
62 | 62 | public: |
|
63 | 63 | void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0); |
|
64 | 64 | QRectF boundingRect() const; |
|
65 | 65 | |
|
66 | 66 | void setBrush(const QBrush &brush); |
|
67 | 67 | QBrush brush() const; |
|
68 | 68 | |
|
69 | 69 | void setPen(const QPen &pen); |
|
70 | 70 | QPen pen() const; |
|
71 | 71 | |
|
72 | 72 | void setAlignmnent(QLegend::Alignments alignment); |
|
73 | 73 | QLegend::Alignments alignment() const; |
|
74 | 74 | |
|
75 | 75 | |
|
76 | 76 | void detachFromChart(); |
|
77 | 77 | void attachToChart(); |
|
78 | 78 | bool isAttachedToChart(); |
|
79 | 79 | |
|
80 | 80 | qreal minWidht() const { return m_minWidth;} |
|
81 | 81 | qreal minHeight() const { return m_minHeight;} |
|
82 | 82 | |
|
83 | 83 | void setBackgroundVisible(bool visible); |
|
84 | 84 | bool isBackgroundVisible() const; |
|
85 | 85 | |
|
86 | 86 | void setOffset(const QPointF& point); |
|
87 | 87 | QPointF offset() const; |
|
88 | 88 | |
|
89 | 89 | protected: |
|
90 | 90 | void resizeEvent(QGraphicsSceneResizeEvent *event); |
|
91 | 91 | void hideEvent(QHideEvent *event); |
|
92 | 92 | void showEvent(QShowEvent *event); |
|
93 | void mousePressEvent(QGraphicsSceneMouseEvent* event); | |
|
94 | void mouseMoveEvent(QGraphicsSceneMouseEvent* event); | |
|
95 | void mouseReleaseEvent(QGraphicsSceneMouseEvent* event); | |
|
96 | 93 | |
|
97 | 94 | public Q_SLOTS: |
|
98 | 95 | // PIMPL ---> |
|
99 | 96 | void handleSeriesAdded(QSeries *series, Domain *domain); |
|
100 | 97 | void handleSeriesRemoved(QSeries *series); |
|
101 | 98 | void handleAdded(QList<QPieSlice *> slices); |
|
102 | 99 | void handleRemoved(QList<QPieSlice *> slices); |
|
103 | 100 | void handleMarkerDestroyed(); |
|
104 | 101 | |
|
105 | 102 | // PIMPL <--- |
|
106 | 103 | |
|
107 | 104 | private: |
|
108 | 105 | // PIMPL ---> |
|
109 | 106 | void appendMarkers(QAreaSeries *series); |
|
110 | 107 | void appendMarkers(QXYSeries *series); |
|
111 | 108 | void appendMarkers(QBarSeries *series); |
|
112 | 109 | void appendMarkers(QPieSeries *series); |
|
113 | 110 | void deleteMarkers(QSeries *series); |
|
114 | 111 | |
|
115 | 112 | |
|
116 | 113 | |
|
117 | 114 | |
|
118 | 115 | private Q_SLOTS: |
|
119 | 116 | void updateLayout(); |
|
120 | 117 | |
|
121 | 118 | private: |
|
122 | 119 | qreal m_margin; |
|
123 | 120 | |
|
124 | 121 | QRectF m_rect; |
|
125 | 122 | qreal m_offsetX; |
|
126 | 123 | qreal m_offsetY; |
|
127 | 124 | |
|
128 | 125 | //QList<LegendMarker *> m_markers; |
|
129 | 126 | |
|
130 | 127 | QBrush m_brush; |
|
131 | 128 | QPen m_pen; |
|
132 | 129 | QLegend::Alignments m_alignment; |
|
133 | 130 | QGraphicsItemGroup* m_markers; |
|
134 | 131 | |
|
135 | 132 | |
|
136 | 133 | bool m_attachedToChart; |
|
137 | 134 | |
|
138 | 135 | QChart *m_chart; |
|
139 | 136 | qreal m_minWidth; |
|
140 | 137 | qreal m_minHeight; |
|
141 | 138 | qreal m_width; |
|
142 | 139 | qreal m_height; |
|
143 | 140 | bool m_visible; |
|
144 | 141 | bool m_dirty; |
|
145 | Scroller* m_scroller; | |
|
146 | friend class QChart; | |
|
142 | friend class ScrolledQLegend; | |
|
147 | 143 | // <--- PIMPL |
|
148 | 144 | }; |
|
149 | 145 | |
|
146 | class ScrolledQLegend: public QLegend, public Scroller | |
|
147 | { | |
|
148 | ||
|
149 | public: | |
|
150 | ScrolledQLegend(QChart *chart):QLegend(chart) | |
|
151 | { | |
|
152 | } | |
|
153 | ||
|
154 | void setOffset(const QPointF& point) | |
|
155 | { | |
|
156 | QLegend::setOffset(point); | |
|
157 | } | |
|
158 | QPointF offset() const | |
|
159 | { | |
|
160 | return QLegend::offset(); | |
|
161 | } | |
|
162 | ||
|
163 | void mousePressEvent(QGraphicsSceneMouseEvent* event){ | |
|
164 | Scroller::mousePressEvent(event); | |
|
165 | //QLegend::mousePressEvent(event); | |
|
166 | } | |
|
167 | void mouseMoveEvent(QGraphicsSceneMouseEvent* event){ | |
|
168 | Scroller::mouseMoveEvent(event); | |
|
169 | //QLegend::mouseMoveEvent(event); | |
|
170 | } | |
|
171 | void mouseReleaseEvent(QGraphicsSceneMouseEvent* event){ | |
|
172 | Scroller::mouseReleaseEvent(event); | |
|
173 | //QLegend::mouseReleaseEvent(event); | |
|
174 | } | |
|
175 | }; | |
|
176 | ||
|
150 | 177 | QTCOMMERCIALCHART_END_NAMESPACE |
|
151 | 178 | |
|
152 | 179 | #endif // QLEGEND_H |
@@ -1,259 +1,248 | |||
|
1 | 1 | /**************************************************************************** |
|
2 | 2 | ** |
|
3 | 3 | ** Copyright (C) 2012 Digia Plc |
|
4 | 4 | ** All rights reserved. |
|
5 | 5 | ** For any questions to Digia, please use contact form at http://qt.digia.com |
|
6 | 6 | ** |
|
7 | 7 | ** This file is part of the Qt Commercial Charts Add-on. |
|
8 | 8 | ** |
|
9 | 9 | ** $QT_BEGIN_LICENSE$ |
|
10 | 10 | ** Licensees holding valid Qt Commercial licenses may use this file in |
|
11 | 11 | ** accordance with the Qt Commercial License Agreement provided with the |
|
12 | 12 | ** Software or, alternatively, in accordance with the terms contained in |
|
13 | 13 | ** a written agreement between you and Digia. |
|
14 | 14 | ** |
|
15 | 15 | ** If you have questions regarding the use of this file, please use |
|
16 | 16 | ** contact form at http://qt.digia.com |
|
17 | 17 | ** $QT_END_LICENSE$ |
|
18 | 18 | ** |
|
19 | 19 | ****************************************************************************/ |
|
20 | 20 | |
|
21 | 21 | #include "scroller_p.h" |
|
22 | 22 | #include "qlegend.h" |
|
23 | 23 | #include <QGraphicsSceneMouseEvent> |
|
24 | 24 | |
|
25 | 25 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
26 | 26 | |
|
27 |
Scroller::Scroller( |
|
|
27 | Scroller::Scroller(): | |
|
28 | 28 | m_ticker(this), |
|
29 | 29 | m_state(Idle), |
|
30 | 30 | m_moveThreshold(10), |
|
31 |
m_timeTreshold(50) |
|
|
32 | m_legend(legend) | |
|
31 | m_timeTreshold(50) | |
|
33 | 32 | { |
|
34 | 33 | |
|
35 | 34 | } |
|
36 | 35 | |
|
37 | 36 | Scroller::~Scroller() |
|
38 | 37 | { |
|
39 | 38 | } |
|
40 | 39 | |
|
41 | 40 | void Scroller::mousePressEvent(QGraphicsSceneMouseEvent* event) |
|
42 | 41 | { |
|
43 | 42 | if (event->button() == Qt::LeftButton) { |
|
44 | 43 | |
|
45 | 44 | switch (m_state) { |
|
46 | 45 | case Idle: |
|
47 | 46 | { |
|
48 | 47 | m_state = Pressed; |
|
49 | 48 | m_offset = offset(); |
|
50 | 49 | m_press = event->pos(); |
|
51 | 50 | m_timeStamp = QTime::currentTime(); |
|
52 | 51 | event->accept(); |
|
53 | 52 | break; |
|
54 | 53 | } |
|
55 | 54 | case Scroll: |
|
56 | 55 | { |
|
57 | 56 | m_state = Stop; |
|
58 | 57 | m_speed = QPoint(0, 0); |
|
59 | 58 | m_offset = offset(); |
|
60 | 59 | m_press = event->pos(); |
|
61 | 60 | event->accept(); |
|
62 | 61 | break; |
|
63 | 62 | } |
|
64 | 63 | case Pressed: |
|
65 | 64 | case Move: |
|
66 | 65 | case Stop: |
|
67 | 66 | qWarning() << __FUNCTION__<<"Scroller unexpected state" << m_state; |
|
68 | 67 | event->ignore(); |
|
69 | 68 | break; |
|
70 | 69 | } |
|
71 | 70 | } |
|
72 | 71 | } |
|
73 | 72 | |
|
74 | 73 | void Scroller::mouseMoveEvent(QGraphicsSceneMouseEvent* event) |
|
75 | 74 | { |
|
76 | 75 | QPointF delta = event->pos() - m_press; |
|
77 | 76 | |
|
78 | 77 | switch (m_state) { |
|
79 | 78 | case Pressed: |
|
80 | 79 | case Stop: |
|
81 | 80 | { |
|
82 | 81 | if (qAbs(delta.x()) > m_moveThreshold || qAbs(delta.y()) > m_moveThreshold) { |
|
83 | 82 | m_state = Move; |
|
84 | 83 | m_timeStamp = QTime::currentTime(); |
|
85 | 84 | m_distance = QPointF(0, 0); |
|
86 | 85 | m_press = event->pos(); |
|
87 | 86 | event->accept(); |
|
88 | 87 | break; |
|
89 | 88 | } |
|
90 | 89 | else { |
|
91 | 90 | event->ignore(); |
|
92 | 91 | break; |
|
93 | 92 | } |
|
94 | 93 | } |
|
95 | 94 | case Move: |
|
96 | 95 | { |
|
97 | 96 | setOffset(m_offset - delta); |
|
98 | 97 | calculateSpeed(event->pos()); |
|
99 | 98 | event->accept(); |
|
100 | 99 | break; |
|
101 | 100 | } |
|
102 | 101 | case Idle: |
|
103 | 102 | case Scroll: |
|
104 | 103 | qWarning() << __FUNCTION__<<"Scroller unexpected state" << m_state; |
|
105 | 104 | event->ignore(); |
|
106 | 105 | break; |
|
107 | 106 | } |
|
108 | 107 | |
|
109 | 108 | } |
|
110 | 109 | |
|
111 | 110 | void Scroller::mouseReleaseEvent(QGraphicsSceneMouseEvent* event) |
|
112 | 111 | { |
|
113 | 112 | if (event->button() == Qt::LeftButton) { |
|
114 | 113 | |
|
115 | 114 | switch (m_state) { |
|
116 | 115 | |
|
117 | 116 | case Scroll: |
|
118 | 117 | m_state = Stop; |
|
119 | 118 | m_speed = QPointF(0, 0); |
|
120 | 119 | m_offset = offset(); |
|
121 | 120 | event->accept(); |
|
122 | 121 | break; |
|
123 | 122 | case Pressed: |
|
124 | 123 | { |
|
125 | 124 | m_state = Idle; |
|
126 | 125 | //if (m_timeStamp.elapsed() < m_clickedPressDelay) { |
|
127 | 126 | |
|
128 | 127 | //emit clicked(m_offset.toPoint()); |
|
129 | 128 | //} |
|
130 | 129 | event->accept(); |
|
131 | 130 | break; |
|
132 | 131 | } |
|
133 | 132 | case Move: |
|
134 | 133 | { |
|
135 | 134 | calculateSpeed(event->pos()); |
|
136 | 135 | m_offset = offset(); |
|
137 | 136 | m_press = event->pos(); |
|
138 | 137 | if (m_speed == QPointF(0, 0)) { |
|
139 | 138 | m_state = Idle; |
|
140 | 139 | } |
|
141 | 140 | else { |
|
142 | 141 | m_speed /= 4; |
|
143 | 142 | m_state = Scroll; |
|
144 | 143 | m_ticker.start(20); |
|
145 | 144 | } |
|
146 | 145 | event->accept(); |
|
147 | 146 | break; |
|
148 | 147 | } |
|
149 | 148 | |
|
150 | 149 | case Stop: |
|
151 | 150 | case Idle: |
|
152 | 151 | qWarning() << __FUNCTION__<<"Scroller unexpected state" << m_state; |
|
153 | 152 | event->ignore(); |
|
154 | 153 | break; |
|
155 | 154 | |
|
156 | 155 | } |
|
157 | 156 | } |
|
158 | 157 | } |
|
159 | 158 | |
|
160 | 159 | void Scroller::scrollTick() |
|
161 | 160 | { |
|
162 | 161 | switch (m_state) { |
|
163 | 162 | case Scroll: |
|
164 | 163 | { |
|
165 | 164 | lowerSpeed(m_speed); |
|
166 | 165 | setOffset(m_offset - m_speed); |
|
167 | 166 | m_offset = offset(); |
|
168 | 167 | if (m_speed == QPointF(0, 0)) { |
|
169 | 168 | m_state = Idle; |
|
170 | 169 | m_ticker.stop(); |
|
171 | 170 | } |
|
172 | 171 | break; |
|
173 | 172 | } |
|
174 | 173 | case Stop: |
|
175 | 174 | m_ticker.stop(); |
|
176 | 175 | break; |
|
177 | 176 | case Idle: |
|
178 | 177 | case Move: |
|
179 | 178 | case Pressed: |
|
180 | 179 | qWarning() << __FUNCTION__<<"Scroller unexpected state" << m_state; |
|
181 | 180 | m_ticker.stop(); |
|
182 | 181 | break; |
|
183 | 182 | |
|
184 | 183 | } |
|
185 | 184 | } |
|
186 | 185 | |
|
187 | 186 | void Scroller::lowerSpeed(QPointF& speed, qreal maxSpeed) |
|
188 | 187 | { |
|
189 | 188 | qreal x = qBound(-maxSpeed, speed.x(), maxSpeed); |
|
190 | 189 | qreal y = qBound(-maxSpeed, speed.y(), maxSpeed); |
|
191 | 190 | |
|
192 | 191 | x = (x == 0) ? x : |
|
193 | 192 | (x > 0) ? qMax(qreal(0), x - m_fraction.x()) : qMin(qreal(0), x + m_fraction.x()); |
|
194 | 193 | y = (y == 0) ? y : |
|
195 | 194 | (y > 0) ? qMax(qreal(0), y - m_fraction.y()) : qMin(qreal(0), y + m_fraction.y()); |
|
196 | 195 | speed.setX(x); |
|
197 | 196 | speed.setY(y); |
|
198 | 197 | } |
|
199 | 198 | |
|
200 | 199 | void Scroller::calculateSpeed(const QPointF& position) |
|
201 | 200 | { |
|
202 | 201 | if (m_timeStamp.elapsed() > m_timeTreshold) { |
|
203 | 202 | |
|
204 | 203 | QPointF distance = position - m_press; |
|
205 | 204 | |
|
206 | 205 | m_timeStamp = QTime::currentTime(); |
|
207 | 206 | m_speed = distance - m_distance; |
|
208 | 207 | m_distance = distance; |
|
209 | 208 | |
|
210 | 209 | qreal fraction = qMax(qAbs(m_speed.x()), qAbs(m_speed.y())); |
|
211 | 210 | |
|
212 | 211 | if (fraction != 0) { |
|
213 | 212 | m_fraction.setX(qAbs(m_speed.x() / fraction)); |
|
214 | 213 | m_fraction.setY(qAbs(m_speed.y() / fraction)); |
|
215 | 214 | } |
|
216 | 215 | else { |
|
217 | 216 | m_fraction.setX(1); |
|
218 | 217 | m_fraction.setY(1); |
|
219 | 218 | } |
|
220 | 219 | } |
|
221 | 220 | } |
|
222 | 221 | |
|
223 | void Scroller::setOffset(const QPointF& point) | |
|
224 | { | |
|
225 | m_legend->setOffset(point); | |
|
226 | } | |
|
227 | ||
|
228 | QPointF Scroller::offset() const | |
|
229 | { | |
|
230 | return m_legend->offset(); | |
|
231 | } | |
|
232 | ||
|
233 | 222 | ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
|
234 | 223 | |
|
235 | 224 | ScrollTicker::ScrollTicker(Scroller *scroller,QObject* parent):QObject(parent), |
|
236 | 225 | m_scroller(scroller) |
|
237 | 226 | { |
|
238 | 227 | |
|
239 | 228 | } |
|
240 | 229 | |
|
241 | 230 | void ScrollTicker::start(int interval) |
|
242 | 231 | { |
|
243 | 232 | if (!m_timer.isActive()){ |
|
244 | 233 | m_timer.start(interval, this); |
|
245 | 234 | } |
|
246 | 235 | } |
|
247 | 236 | |
|
248 | 237 | void ScrollTicker::stop() |
|
249 | 238 | { |
|
250 | 239 | m_timer.stop(); |
|
251 | 240 | } |
|
252 | 241 | |
|
253 | 242 | void ScrollTicker::timerEvent(QTimerEvent *event) |
|
254 | 243 | { |
|
255 | 244 | Q_UNUSED(event); |
|
256 | 245 | m_scroller->scrollTick(); |
|
257 | 246 | } |
|
258 | 247 | |
|
259 | 248 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,107 +1,106 | |||
|
1 | 1 | /**************************************************************************** |
|
2 | 2 | ** |
|
3 | 3 | ** Copyright (C) 2012 Digia Plc |
|
4 | 4 | ** All rights reserved. |
|
5 | 5 | ** For any questions to Digia, please use contact form at http://qt.digia.com |
|
6 | 6 | ** |
|
7 | 7 | ** This file is part of the Qt Commercial Charts Add-on. |
|
8 | 8 | ** |
|
9 | 9 | ** $QT_BEGIN_LICENSE$ |
|
10 | 10 | ** Licensees holding valid Qt Commercial licenses may use this file in |
|
11 | 11 | ** accordance with the Qt Commercial License Agreement provided with the |
|
12 | 12 | ** Software or, alternatively, in accordance with the terms contained in |
|
13 | 13 | ** a written agreement between you and Digia. |
|
14 | 14 | ** |
|
15 | 15 | ** If you have questions regarding the use of this file, please use |
|
16 | 16 | ** contact form at http://qt.digia.com |
|
17 | 17 | ** $QT_END_LICENSE$ |
|
18 | 18 | ** |
|
19 | 19 | ****************************************************************************/ |
|
20 | 20 | |
|
21 | 21 | // W A R N I N G |
|
22 | 22 | // ------------- |
|
23 | 23 | // |
|
24 | 24 | // This file is not part of the QtCommercial Chart API. It exists purely as an |
|
25 | 25 | // implementation detail. This header file may change from version to |
|
26 | 26 | // version without notice, or even be removed. |
|
27 | 27 | // |
|
28 | 28 | // We mean it. |
|
29 | 29 | |
|
30 | 30 | #ifndef SCROLLER_P_H_ |
|
31 | 31 | #define SCROLLER_P_H_ |
|
32 | 32 | |
|
33 | 33 | #include "qchartglobal.h" |
|
34 | 34 | #include <QBasicTimer> |
|
35 | 35 | #include <QTime> |
|
36 | 36 | #include <QPointF> |
|
37 | 37 | |
|
38 | 38 | class QGraphicsSceneMouseEvent; |
|
39 | 39 | |
|
40 | 40 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
41 | 41 | |
|
42 | 42 | class Scroller; |
|
43 | 43 | class QLegend; |
|
44 | 44 | |
|
45 | 45 | class ScrollTicker : public QObject |
|
46 | 46 | { |
|
47 | 47 | public: |
|
48 | 48 | ScrollTicker(Scroller *scroller,QObject *parent=0); |
|
49 | 49 | void start(int interval); |
|
50 | 50 | void stop(); |
|
51 | 51 | protected: |
|
52 | 52 | void timerEvent(QTimerEvent *event); |
|
53 | 53 | |
|
54 | 54 | private: |
|
55 | 55 | QBasicTimer m_timer; |
|
56 | 56 | Scroller *m_scroller; |
|
57 | 57 | }; |
|
58 | 58 | |
|
59 | 59 | class Scroller |
|
60 | 60 | { |
|
61 | 61 | public: |
|
62 | 62 | enum State { |
|
63 | 63 | Idle, |
|
64 | 64 | Pressed, |
|
65 | 65 | Move, |
|
66 | 66 | Scroll, |
|
67 | 67 | Stop |
|
68 | 68 | }; |
|
69 | 69 | |
|
70 | explicit Scroller(QLegend* legend); | |
|
70 | Scroller(); | |
|
71 | 71 | virtual ~Scroller(); |
|
72 | 72 | |
|
73 | virtual void setOffset(const QPointF& point); | |
|
74 | virtual QPointF offset() const; | |
|
73 | virtual void setOffset(const QPointF& point) = 0; | |
|
74 | virtual QPointF offset() const = 0; | |
|
75 | 75 | |
|
76 | 76 | public: |
|
77 | 77 | void scrollTick(); |
|
78 | 78 | |
|
79 | 79 | |
|
80 | 80 | public: |
|
81 | 81 | void mousePressEvent(QGraphicsSceneMouseEvent* event); |
|
82 | 82 | void mouseMoveEvent(QGraphicsSceneMouseEvent* event); |
|
83 | 83 | void mouseReleaseEvent(QGraphicsSceneMouseEvent* event); |
|
84 | 84 | |
|
85 | 85 | private: |
|
86 | 86 | void calculateSpeed(const QPointF& position); |
|
87 | 87 | void lowerSpeed(QPointF& speed,qreal maxSpeed=100); |
|
88 | 88 | |
|
89 | 89 | private: |
|
90 | 90 | ScrollTicker m_ticker; |
|
91 | 91 | State m_state; |
|
92 | 92 | QTime m_timeStamp; |
|
93 | 93 | QPointF m_press; |
|
94 | 94 | QPointF m_offset; |
|
95 | 95 | QPointF m_speed; |
|
96 | 96 | QPointF m_distance; |
|
97 | 97 | QPointF m_fraction; |
|
98 | 98 | int m_moveThreshold; |
|
99 | 99 | int m_timeTreshold; |
|
100 | QLegend* m_legend; | |
|
101 | 100 | |
|
102 | 101 | |
|
103 | 102 | }; |
|
104 | 103 | |
|
105 | 104 | QTCOMMERCIALCHART_END_NAMESPACE |
|
106 | 105 | |
|
107 | 106 | #endif /* SCROLLER_P_H_ */ |
General Comments 0
You need to be logged in to leave comments.
Login now