##// END OF EJS Templates
Adds qlegend pimpl...
Michal Klocek -
r950:2b4630c67306
parent child
Show More
@@ -0,0 +1,72
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
14 **
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
18 **
19 ****************************************************************************/
20
21 // W A R N I N G
22 // -------------
23 //
24 // This file is not part of the QtCommercial Chart API. It exists purely as an
25 // implementation detail. This header file may change from version to
26 // version without notice, or even be removed.
27 //
28 // We mean it.
29
30
31 #ifndef LEGENDSCROLLER_P_H_
32 #define LEGENDSCROLLER_P_H_
33
34 #include "qlegend.h"
35 #include "scroller_p.h"
36
37 QTCOMMERCIALCHART_BEGIN_NAMESPACE
38
39 class LegendScroller: public QLegend, public Scroller
40 {
41
42 public:
43 LegendScroller(QChart *chart):QLegend(chart)
44 {
45 }
46
47 void setOffset(const QPointF& point)
48 {
49 QLegend::setOffset(point);
50 }
51 QPointF offset() const
52 {
53 return QLegend::offset();
54 }
55
56 void mousePressEvent(QGraphicsSceneMouseEvent* event){
57 Scroller::mousePressEvent(event);
58 //QLegend::mousePressEvent(event);
59 }
60 void mouseMoveEvent(QGraphicsSceneMouseEvent* event){
61 Scroller::mouseMoveEvent(event);
62 //QLegend::mouseMoveEvent(event);
63 }
64 void mouseReleaseEvent(QGraphicsSceneMouseEvent* event){
65 Scroller::mouseReleaseEvent(event);
66 //QLegend::mouseReleaseEvent(event);
67 }
68 };
69
70 QTCOMMERCIALCHART_END_NAMESPACE
71
72 #endif
@@ -0,0 +1,76
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
14 **
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
18 **
19 ****************************************************************************/
20
21 // W A R N I N G
22 // -------------
23 //
24 // This file is not part of the QtCommercial Chart API. It exists purely as an
25 // implementation detail. This header file may change from version to
26 // version without notice, or even be removed.
27 //
28 // We mean it.
29
30 #ifndef QLEGEND_P_H_
31 #define QLEGEND_P_H_
32
33 #include "qlegend.h"
34
35 QTCOMMERCIALCHART_BEGIN_NAMESPACE
36
37 class ChartPresenter;
38
39 class QLegendPrivate : public QObject
40 {
41 Q_OBJECT
42 public:
43 QLegendPrivate(ChartPresenter *chart,QLegend *q);
44 ~QLegendPrivate();
45
46 void setOffset(qreal x, qreal y);
47 void updateLayout();
48
49 public Q_SLOTS:
50 void handleSeriesAdded(QSeries *series, Domain *domain);
51 void handleSeriesRemoved(QSeries *series);
52
53 private:
54 QLegend *q_ptr;
55 ChartPresenter *m_presenter;
56 QGraphicsItemGroup* m_markers;
57 QLegend::Alignments m_alignment;
58 QBrush m_brush;
59 QPen m_pen;
60 QRectF m_rect;
61 qreal m_offsetX;
62 qreal m_offsetY;
63 qreal m_minWidth;
64 qreal m_minHeight;
65 qreal m_width;
66 qreal m_height;
67 bool m_attachedToChart;
68 bool m_backgroundVisible;
69
70 friend class QLegend;
71
72 };
73
74 QTCOMMERCIALCHART_END_NAMESPACE
75
76 #endif
@@ -22,6 +22,7
22 22 #include "qareaseries_p.h"
23 23 #include "qlineseries.h"
24 24 #include "areachartitem_p.h"
25 #include "legendmarker_p.h"
25 26 #include "domain_p.h"
26 27 #include "chartdataset_p.h"
27 28 #include "charttheme_p.h"
@@ -261,6 +262,13 Chart* QAreaSeriesPrivate::createGraphics(ChartPresenter* presenter)
261 262 return area;
262 263 }
263 264
265 QList<LegendMarker*> QAreaSeriesPrivate::createLegendMarker(QLegend* legend)
266 {
267 Q_Q(QAreaSeries);
268 QList<LegendMarker*> list;
269 return list << new AreaLegendMarker(q,legend);
270 }
271
264 272 #include "moc_qareaseries.cpp"
265 273 #include "moc_qareaseries_p.cpp"
266 274
@@ -45,6 +45,7 public:
45 45
46 46 void scaleDomain(Domain& domain);
47 47 Chart* createGraphics(ChartPresenter* presenter);
48 QList<LegendMarker*> createLegendMarker(QLegend* legend);
48 49
49 50 Q_SIGNALS:
50 51 void updated();
@@ -24,6 +24,7
24 24 #include "qbarset_p.h"
25 25 #include "barchartmodel_p.h"
26 26 #include "domain_p.h"
27 #include "legendmarker_p.h"
27 28 #include "chartdataset_p.h"
28 29 #include "charttheme_p.h"
29 30 #include "chartanimator_p.h"
@@ -805,6 +806,18 Chart* QBarSeriesPrivate::createGraphics(ChartPresenter* presenter)
805 806
806 807 }
807 808
809 QList<LegendMarker*> QBarSeriesPrivate::createLegendMarker(QLegend* legend)
810 {
811 Q_Q(QBarSeries);
812 QList<LegendMarker*> markers;
813 foreach(QBarSet* set, q->barSets()) {
814 BarLegendMarker* marker = new BarLegendMarker(q,set,legend);
815 markers << marker;
816 }
817
818 return markers;
819 }
820
808 821 #include "moc_qbarseries.cpp"
809 822 #include "moc_qbarseries_p.cpp"
810 823
@@ -19,6 +19,7 public:
19 19
20 20 void scaleDomain(Domain& domain);
21 21 Chart* createGraphics(ChartPresenter* presenter);
22 QList<LegendMarker*> createLegendMarker(QLegend* legend);
22 23
23 24 bool setModel(QAbstractItemModel *model);
24 25 void setModelMapping(int categories, int bottomBoundry, int topBoundry, Qt::Orientation orientation = Qt::Vertical);
@@ -25,6 +25,7
25 25 #include "chartdataset_p.h"
26 26 #include "charttheme_p.h"
27 27 #include "chartanimator_p.h"
28 #include "legendmarker_p.h"
28 29 #include <QAbstractItemModel>
29 30 #include <QDebug>
30 31
@@ -716,6 +717,17 Chart* QPieSeriesPrivate::createGraphics(ChartPresenter* presenter)
716 717 return pie;
717 718 }
718 719
720 QList<LegendMarker*> QPieSeriesPrivate::createLegendMarker(QLegend* legend)
721 {
722 Q_Q(QPieSeries);
723 QList<LegendMarker*> markers;
724 foreach(QPieSlice* slice, q->slices()) {
725 PieLegendMarker* marker = new PieLegendMarker(q,slice,legend);
726 markers << marker;
727 }
728 return markers;
729 }
730
719 731 #include "moc_qpieseries.cpp"
720 732 #include "moc_qpieseries_p.cpp"
721 733
@@ -38,6 +38,7 public:
38 38
39 39 void scaleDomain(Domain& domain);
40 40 Chart* createGraphics(ChartPresenter* presenter);
41 QList<LegendMarker*> createLegendMarker(QLegend* legend);
41 42
42 43 void updateDerivativeData();
43 44
@@ -20,6 +20,8
20 20
21 21 #include "qchart.h"
22 22 #include "qchart_p.h"
23 #include "legendscroller_p.h"
24 #include "qlegend_p.h"
23 25 #include <QGraphicsScene>
24 26 #include <QGraphicsSceneResizeEvent>
25 27
@@ -67,12 +69,13 QTCOMMERCIALCHART_BEGIN_NAMESPACE
67 69 QChart::QChart(QGraphicsItem *parent, Qt::WindowFlags wFlags) : QGraphicsWidget(parent,wFlags),
68 70 d_ptr(new QChartPrivate())
69 71 {
70 d_ptr->m_legend = new ScrolledQLegend(this);
71 d_ptr->m_legend->setVisible(false);
72 72 d_ptr->m_dataset = new ChartDataSet(this);
73 73 d_ptr->m_presenter = new ChartPresenter(this,d_ptr->m_dataset);
74 d_ptr->m_presenter->setTheme(QChart::ChartThemeLight, false);
75 74 d_ptr->createConnections();
75 d_ptr->m_legend = new LegendScroller(this);
76 // d_ptr->m_legend->setVisible(false);
77 d_ptr->m_presenter->setTheme(QChart::ChartThemeLight, false);
78
76 79 //TODO:fix me setMinimumSize(d_ptr->m_padding.left() * 3, d_ptr->m_padding.top() * 3);
77 80 }
78 81
@@ -400,8 +403,7 QChartPrivate::~QChartPrivate()
400 403
401 404 void QChartPrivate::createConnections()
402 405 {
403 QObject::connect(m_dataset,SIGNAL(seriesAdded(QSeries*,Domain*)),m_legend,SLOT(handleSeriesAdded(QSeries*,Domain*)));
404 QObject::connect(m_dataset,SIGNAL(seriesRemoved(QSeries*)),m_legend,SLOT(handleSeriesRemoved(QSeries*)));
406
405 407 QObject::connect(m_dataset,SIGNAL(seriesAdded(QSeries*,Domain*)),m_presenter,SLOT(handleSeriesAdded(QSeries*,Domain*)));
406 408 QObject::connect(m_dataset,SIGNAL(seriesRemoved(QSeries*)),m_presenter,SLOT(handleSeriesRemoved(QSeries*)));
407 409 QObject::connect(m_dataset,SIGNAL(axisAdded(QChartAxis*,Domain*)),m_presenter,SLOT(handleAxisAdded(QChartAxis*,Domain*)));
@@ -19,8 +19,11
19 19 ****************************************************************************/
20 20
21 21 #include "qlegend.h"
22 #include "qchart_p.h"
22 #include "qlegend_p.h"
23 23 #include "qseries.h"
24 #include "qseries_p.h"
25 #include "qchart_p.h"
26
24 27 #include "legendmarker_p.h"
25 28 #include "qxyseries.h"
26 29 #include "qlineseries.h"
@@ -82,23 +85,19 QTCOMMERCIALCHART_BEGIN_NAMESPACE
82 85 */
83 86
84 87 QLegend::QLegend(QChart *chart):QGraphicsWidget(chart),
85 m_margin(5),
86 m_offsetX(0),
87 m_offsetY(0),
88 m_brush(Qt::darkGray), // TODO: default should come from theme
89 m_alignment(QLegend::AlignmentTop),
90 m_markers(new QGraphicsItemGroup(this)),
91 m_attachedToChart(true),
92 m_chart(chart),
93 m_minWidth(0),
94 m_minHeight(0),
95 m_width(0),
96 m_height(0),
97 m_backgroundVisible(false)
88 d_ptr(new QLegendPrivate(chart->d_ptr->m_presenter,this))
98 89 {
99 90 setZValue(ChartPresenter::LegendZValue);
100 91 setFlags(QGraphicsItem::ItemClipsChildrenToShape);
101 setVisible(false); // By default legend is invisible
92 setEnabled(false); // By default legend is disabled
93
94 QObject::connect(chart->d_ptr->m_dataset,SIGNAL(seriesAdded(QSeries*,Domain*)),d_ptr.data(),SLOT(handleSeriesAdded(QSeries*,Domain*)));
95 QObject::connect(chart->d_ptr->m_dataset,SIGNAL(seriesRemoved(QSeries*)),d_ptr.data(),SLOT(handleSeriesRemoved(QSeries*)));
96 }
97
98 QLegend::~QLegend()
99 {
100
102 101 }
103 102
104 103 /*!
@@ -109,11 +108,11 void QLegend::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, Q
109 108 {
110 109 Q_UNUSED(option)
111 110 Q_UNUSED(widget)
112 if(!m_backgroundVisible) return;
111 if(!d_ptr->m_backgroundVisible) return;
113 112
114 113 painter->setOpacity(opacity());
115 painter->setPen(m_pen);
116 painter->setBrush(m_brush);
114 painter->setPen(d_ptr->m_pen);
115 painter->setBrush(d_ptr->m_brush);
117 116 painter->drawRect(boundingRect());
118 117 }
119 118
@@ -123,7 +122,7 void QLegend::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, Q
123 122
124 123 QRectF QLegend::boundingRect() const
125 124 {
126 return m_rect;
125 return d_ptr->m_rect;
127 126 }
128 127
129 128 /*!
@@ -131,8 +130,8 QRectF QLegend::boundingRect() const
131 130 */
132 131 void QLegend::setBrush(const QBrush &brush)
133 132 {
134 if (m_brush != brush) {
135 m_brush = brush;
133 if (d_ptr->m_brush != brush) {
134 d_ptr->m_brush = brush;
136 135 update();
137 136 }
138 137 }
@@ -142,7 +141,7 void QLegend::setBrush(const QBrush &brush)
142 141 */
143 142 QBrush QLegend::brush() const
144 143 {
145 return m_brush;
144 return d_ptr->m_brush;
146 145 }
147 146
148 147 /*!
@@ -150,8 +149,8 QBrush QLegend::brush() const
150 149 */
151 150 void QLegend::setPen(const QPen &pen)
152 151 {
153 if (m_pen != pen) {
154 m_pen = pen;
152 if (d_ptr->m_pen != pen) {
153 d_ptr->m_pen = pen;
155 154 update();
156 155 }
157 156 }
@@ -162,7 +161,7 void QLegend::setPen(const QPen &pen)
162 161
163 162 QPen QLegend::pen() const
164 163 {
165 return m_pen;
164 return d_ptr->m_pen;
166 165 }
167 166
168 167 /*!
@@ -171,9 +170,9 QPen QLegend::pen() const
171 170 */
172 171 void QLegend::setAlignment(QLegend::Alignments alignment)
173 172 {
174 if(m_alignment!=alignment && m_attachedToChart) {
175 m_alignment = alignment;
176 updateLayout();
173 if(d_ptr->m_alignment!=alignment && d_ptr->m_attachedToChart) {
174 d_ptr->m_alignment = alignment;
175 d_ptr->updateLayout();
177 176 }
178 177 }
179 178
@@ -182,227 +181,152 void QLegend::setAlignment(QLegend::Alignments alignment)
182 181 */
183 182 QLegend::Alignments QLegend::alignment() const
184 183 {
185 return m_alignment;
184 return d_ptr->m_alignment;
186 185 }
187 186
188 187 /*!
189 \internal \a series \a domain Should be called when series is added to chart.
188 Detaches the legend from chart. Chart won't change layout of the legend.
190 189 */
191 void QLegend::handleSeriesAdded(QSeries *series, Domain *domain)
192 {
193 Q_UNUSED(domain)
194
195 switch (series->type())
190 void QLegend::detachFromChart()
196 191 {
197 case QSeries::SeriesTypeLine: {
198 QLineSeries *lineSeries = static_cast<QLineSeries *>(series);
199 appendMarkers(lineSeries);
200 break;
201 }
202 case QSeries::SeriesTypeArea: {
203 QAreaSeries *areaSeries = static_cast<QAreaSeries *>(series);
204 appendMarkers(areaSeries);
205 break;
206 }
207 case QSeries::SeriesTypeBar: {
208 QBarSeries *barSeries = static_cast<QBarSeries *>(series);
209 appendMarkers(barSeries);
210 break;
211 }
212 case QSeries::SeriesTypeStackedBar: {
213 QStackedBarSeries *stackedBarSeries = static_cast<QStackedBarSeries *>(series);
214 appendMarkers(stackedBarSeries);
215 break;
216 }
217 case QSeries::SeriesTypePercentBar: {
218 QPercentBarSeries *percentBarSeries = static_cast<QPercentBarSeries *>(series);
219 appendMarkers(percentBarSeries);
220 break;
221 }
222 case QSeries::SeriesTypeScatter: {
223 QScatterSeries *scatterSeries = static_cast<QScatterSeries *>(series);
224 appendMarkers(scatterSeries);
225 break;
226 }
227 case QSeries::SeriesTypePie: {
228 QPieSeries *pieSeries = static_cast<QPieSeries *>(series);
229 appendMarkers(pieSeries);
230 connect(pieSeries,SIGNAL(added(QList<QPieSlice*>)),this,SLOT(handleAdded(QList<QPieSlice*>)));
231 break;
232 }
233 case QSeries::SeriesTypeSpline: {
234 QSplineSeries *splineSeries = static_cast<QSplineSeries *>(series);
235 appendMarkers(splineSeries);
236 break;
237 }
238 default: {
239 qWarning()<< "QLegend::handleSeriesAdded" << series->type() << "unknown series type.";
240 break;
241 }
242 }
243
244 updateLayout();
192 d_ptr->m_attachedToChart = false;
245 193 }
246 194
247 195 /*!
248 \internal \a series Should be called when series is removed from chart.
196 Attaches the legend to chart. Chart may change layout of the legend.
249 197 */
250 void QLegend::handleSeriesRemoved(QSeries *series)
251 {
252 switch (series->type())
198 void QLegend::attachToChart()
253 199 {
254 case QSeries::SeriesTypeArea: {
255 QAreaSeries *areaSeries = static_cast<QAreaSeries *>(series);
256 deleteMarkers(areaSeries);
257 break;
258 }
259 case QSeries::SeriesTypePie: {
260 QPieSeries *pieSeries = static_cast<QPieSeries *>(series);
261 disconnect(pieSeries, SIGNAL(added(QList<QPieSlice *>)), this, SLOT(handleAdded(QList<QPieSlice *>)));
262 deleteMarkers(series);
263 break;
264 }
265 default: {
266 // All other types
267 deleteMarkers(series);
268 break;
269 }
270 }
271
272 updateLayout();
200 d_ptr->m_attachedToChart = true;
273 201 }
274 202
275 203 /*!
276 \internal \a slices Should be called when slices are added to pie chart.
204 Returns true, if legend is attached to chart.
277 205 */
278 void QLegend::handleAdded(QList<QPieSlice *> slices)
206 bool QLegend::isAttachedToChart()
279 207 {
280 QPieSeries* series = static_cast<QPieSeries *> (sender());
281 foreach(QPieSlice* slice, slices) {
282 PieLegendMarker* marker = new PieLegendMarker(series,slice, this);
283 m_markers->addToGroup(marker);
284 }
285 updateLayout();
208 return d_ptr->m_attachedToChart;
286 209 }
287 210
288 /*!
289 \internal \a slices Should be called when slices are removed from pie chart. Currently unused,
290 because removed slices are also deleted and we listen destroyed signal
291 */
292 void QLegend::handleRemoved(QList<QPieSlice *> slices)
211 void QLegend::setOffset(const QPointF& point)
293 212 {
294 Q_UNUSED(slices)
213 d_ptr->setOffset(point.x(),point.y());
295 214 }
296 215
297 /*!
298 Detaches the legend from chart. Chart won't change layout of the legend.
299 */
300 void QLegend::detachFromChart()
216 QPointF QLegend::offset() const
301 217 {
302 m_attachedToChart = false;
218 return QPointF(d_ptr->m_offsetX,d_ptr->m_offsetY);
303 219 }
304 220
305 221 /*!
306 Attaches the legend to chart. Chart may change layout of the legend.
222 Sets the visibility of legend background to \a visible
307 223 */
308 void QLegend::attachToChart()
224 void QLegend::setBackgroundVisible(bool visible)
225 {
226 if(d_ptr->m_backgroundVisible!=visible)
309 227 {
310 m_attachedToChart = true;
228 d_ptr->m_backgroundVisible=visible;
229 update();
230 }
311 231 }
312 232
313 233 /*!
314 Returns true, if legend is attached to chart.
234 Returns the visibility of legend background
315 235 */
316 bool QLegend::isAttachedToChart()
236 bool QLegend::isBackgroundVisible() const
317 237 {
318 return m_attachedToChart;
238 return d_ptr->m_backgroundVisible;
319 239 }
320 240
321 241 /*!
322 \internal Helper function. Appends markers from \a series to legend.
242 \internal \a event see QGraphicsWidget for details
323 243 */
324 void QLegend::appendMarkers(QAreaSeries* series)
244 void QLegend::resizeEvent(QGraphicsSceneResizeEvent *event)
325 245 {
326 AreaLegendMarker* marker = new AreaLegendMarker(series,this);
327 m_markers->addToGroup(marker);
246 const QRectF& rect = QRectF(QPoint(0,0),event->newSize());
247 QGraphicsWidget::resizeEvent(event);
248 if(d_ptr->m_rect != rect) {
249 d_ptr->m_rect = rect;
250 d_ptr->updateLayout();
251 }
328 252 }
329 253
330 254 /*!
331 \internal Helper function. Appends markers from \a series to legend.
255 \internal \a event see QGraphicsWidget for details
332 256 */
333 void QLegend::appendMarkers(QXYSeries* series)
257 void QLegend::hideEvent(QHideEvent *event)
334 258 {
335 XYLegendMarker* marker = new XYLegendMarker(series,this);
336 m_markers->addToGroup(marker);
259 QGraphicsWidget::hideEvent(event);
260 setEnabled(false);
261 d_ptr->updateLayout();
337 262 }
338 263
339 264 /*!
340 \internal Helper function. Appends markers from \a series to legend.
265 \internal \a event see QGraphicsWidget for details
341 266 */
342 void QLegend::appendMarkers(QBarSeries *series)
267 void QLegend::showEvent(QShowEvent *event)
343 268 {
344 foreach(QBarSet* set, series->barSets()) {
345 BarLegendMarker* marker = new BarLegendMarker(series,set, this);
346 m_markers->addToGroup(marker);
347 }
269 QGraphicsWidget::showEvent(event);
270 setEnabled(true);
271 d_ptr->updateLayout();
348 272 }
349 273
350 /*!
351 \internal Helper function. Appends markers from \a series to legend.
352 */
353 void QLegend::appendMarkers(QPieSeries *series)
274 qreal QLegend::minWidth() const
354 275 {
355 foreach(QPieSlice* slice, series->slices()) {
356 PieLegendMarker* marker = new PieLegendMarker(series,slice, this);
357 m_markers->addToGroup(marker);
358 }
276 return d_ptr->m_minWidth;
359 277 }
360 278
361 /*!
362 \internal Deletes all markers that are created from \a series
363 */
364 void QLegend::deleteMarkers(QSeries *series)
279 qreal QLegend::minHeight() const
365 280 {
366 // Search all markers that belong to given series and delete them.
281 return d_ptr->m_minHeight;
282 }
367 283
368 QList<QGraphicsItem *> items = m_markers->childItems();
284 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
285
286 QLegendPrivate::QLegendPrivate(ChartPresenter* presenter,QLegend *q):
287 q_ptr(q),
288 m_presenter(presenter),
289 m_markers(new QGraphicsItemGroup(q)),
290 m_alignment(QLegend::AlignmentTop),
291 m_offsetX(0),
292 m_offsetY(0),
293 m_minWidth(0),
294 m_minHeight(0),
295 m_width(0),
296 m_height(0),
297 m_attachedToChart(true),
298 m_backgroundVisible(false)
299 {
369 300
370 foreach (QGraphicsItem *markers, items) {
371 LegendMarker *marker = static_cast<LegendMarker*>(markers);
372 if (marker->series() == series) {
373 delete marker;
374 }
375 }
376 301 }
377 302
378 /*!
379 \internal Updates layout of legend. Tries to fit as many markers as possible up to the maximum size of legend.
380 If items don't fit, sets the visibility of scroll buttons accordingly.
381 Causes legend to be resized.
382 */
303 QLegendPrivate::~QLegendPrivate()
304 {
383 305
384 void QLegend::setOffset(const QPointF& point)
306 }
307
308 void QLegendPrivate::setOffset(qreal x, qreal y)
385 309 {
386 310
387 311 switch(m_alignment) {
388 312
389 case AlignmentTop:
390 case AlignmentBottom: {
313 case QLegend::AlignmentTop:
314 case QLegend::AlignmentBottom: {
391 315 if(m_width<=m_rect.width()) return;
392 316
393 if (point.x() != m_offsetX) {
394 m_offsetX = qBound(0.0, point.x(), m_width - m_rect.width());
317 if (x != m_offsetX) {
318 m_offsetX = qBound(0.0, x, m_width - m_rect.width());
395 319 m_markers->setPos(-m_offsetX,m_rect.top());
396 320 }
397 321 break;
398 322 }
399 case AlignmentLeft:
400 case AlignmentRight: {
323 case QLegend::AlignmentLeft:
324 case QLegend::AlignmentRight: {
401 325
402 326 if(m_height<=m_rect.height()) return;
403 327
404 if (point.y() != m_offsetY) {
405 m_offsetY = qBound(0.0, point.y(), m_height - m_rect.height());
328 if (y != m_offsetY) {
329 m_offsetY = qBound(0.0, y, m_height - m_rect.height());
406 330 m_markers->setPos(m_rect.left(),-m_offsetY);
407 331 }
408 332 break;
@@ -410,13 +334,8 void QLegend::setOffset(const QPointF& point)
410 334 }
411 335 }
412 336
413 QPointF QLegend::offset() const
414 {
415 return QPointF(m_offsetX,m_offsetY);
416 }
417 337
418 // this function runs first to set min max values
419 void QLegend::updateLayout()
338 void QLegendPrivate::updateLayout()
420 339 {
421 340 m_offsetX=0;
422 341 QList<QGraphicsItem *> items = m_markers->childItems();
@@ -428,8 +347,8 void QLegend::updateLayout()
428 347
429 348 switch(m_alignment) {
430 349
431 case AlignmentTop:
432 case AlignmentBottom: {
350 case QLegend::AlignmentTop:
351 case QLegend::AlignmentBottom: {
433 352 QPointF point = m_rect.topLeft();
434 353 m_width = 0;
435 354 foreach (QGraphicsItem *item, items) {
@@ -443,14 +362,15 void QLegend::updateLayout()
443 362 }
444 363 if(m_width<m_rect.width()){
445 364 m_markers->setPos(m_rect.width()/2-m_width/2,m_rect.top());
446 }else{
365 }
366 else {
447 367 m_markers->setPos(m_rect.topLeft());
448 368 }
449 369 m_height=m_minHeight;
450 370 }
451 371 break;
452 case AlignmentLeft:
453 case AlignmentRight:{
372 case QLegend::AlignmentLeft:
373 case QLegend::AlignmentRight: {
454 374 QPointF point = m_rect.topLeft();
455 375 m_height = 0;
456 376 foreach (QGraphicsItem *item, items) {
@@ -464,7 +384,8 void QLegend::updateLayout()
464 384 }
465 385 if(m_height<m_rect.height()){
466 386 m_markers->setPos(m_rect.left(),m_rect.height()/2-m_height/2);
467 }else{
387 }
388 else {
468 389 m_markers->setPos(m_rect.topLeft());
469 390 }
470 391 m_width=m_minWidth;
@@ -472,62 +393,36 void QLegend::updateLayout()
472 393 break;
473 394 }
474 395
475 m_chart->d_ptr->m_presenter->updateLayout(); //TODO fixme;
396 m_presenter->updateLayout();
476 397 }
477 398
478 /*!
479 Sets the visibility of legend background to \a visible
480 */
481 void QLegend::setBackgroundVisible(bool visible)
482 {
483 if(m_backgroundVisible!=visible)
399 void QLegendPrivate::handleSeriesAdded(QSeries *series, Domain *domain)
484 400 {
485 m_backgroundVisible=visible;
486 update();
487 }
488 }
401 Q_UNUSED(domain)
489 402
490 /*!
491 Returns the visibility of legend background
492 */
493 bool QLegend::isBackgroundVisible() const
494 {
495 return m_backgroundVisible;
496 }
403 QList<LegendMarker*> markers = series->d_ptr->createLegendMarker(q_ptr);
404 foreach(LegendMarker* marker , markers)
405 m_markers->addToGroup(marker);
497 406
498 /*!
499 \internal \a event see QGraphicsWidget for details
500 */
501 void QLegend::resizeEvent(QGraphicsSceneResizeEvent *event)
502 {
503 const QRectF& rect = QRectF(QPoint(0,0),event->newSize());
504 QGraphicsWidget::resizeEvent(event);
505 if(m_rect != rect){
506 m_rect = rect;
507 407 updateLayout();
508 408 }
509 }
510 409
511 /*!
512 \internal \a event see QGraphicsWidget for details
513 */
514 void QLegend::hideEvent(QHideEvent *event)
410 void QLegendPrivate::handleSeriesRemoved(QSeries *series)
515 411 {
516 QGraphicsWidget::hideEvent(event);
517 setEnabled(false);
518 updateLayout();
412
413 QList<QGraphicsItem *> items = m_markers->childItems();
414
415 foreach (QGraphicsItem *markers, items) {
416 LegendMarker *marker = static_cast<LegendMarker*>(markers);
417 if (marker->series() == series) {
418 delete marker;
419 }
519 420 }
520 421
521 /*!
522 \internal \a event see QGraphicsWidget for details
523 */
524 void QLegend::showEvent(QShowEvent *event)
525 {
526 QGraphicsWidget::showEvent(event);
527 setEnabled(true);
528 422 updateLayout();
529 423 }
530 424
531 425 #include "moc_qlegend.cpp"
426 #include "moc_qlegend_p.cpp"
532 427
533 428 QTCOMMERCIALCHART_END_NAMESPACE
@@ -25,7 +25,6
25 25 #include <QGraphicsWidget>
26 26 #include <QPen>
27 27 #include <QBrush>
28 #include "private/scroller_p.h" //TODO fixme
29 28
30 29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
31 30
@@ -39,7 +38,9 class QPieSeries;
39 38 class QAreaSeries;
40 39 class LegendScrollButton;
41 40 class QSeries;
41
42 42 class QChart;
43 class QLegendPrivate;
43 44
44 45 class QTCOMMERCIALCHART_EXPORT QLegend : public QGraphicsWidget
45 46 {
@@ -60,6 +61,8 private:
60 61 explicit QLegend(QChart *chart);
61 62
62 63 public:
64 ~QLegend();
65
63 66 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
64 67 QRectF boundingRect() const;
65 68
@@ -76,8 +79,8 public:
76 79 void attachToChart();
77 80 bool isAttachedToChart();
78 81
79 qreal minWidth() const { return m_minWidth;}
80 qreal minHeight() const { return m_minHeight;}
82 qreal minWidth() const;
83 qreal minHeight() const;
81 84
82 85 void setBackgroundVisible(bool visible = true);
83 86 bool isBackgroundVisible() const;
@@ -90,79 +93,10 protected:
90 93 void hideEvent(QHideEvent *event);
91 94 void showEvent(QShowEvent *event);
92 95
93 public Q_SLOTS:
94 // PIMPL --->
95 void handleSeriesAdded(QSeries *series, Domain *domain);
96 void handleSeriesRemoved(QSeries *series);
97 void handleAdded(QList<QPieSlice *> slices);
98 void handleRemoved(QList<QPieSlice *> slices);
99 // PIMPL <---
100
101 private:
102 // PIMPL --->
103 void appendMarkers(QAreaSeries *series);
104 void appendMarkers(QXYSeries *series);
105 void appendMarkers(QBarSeries *series);
106 void appendMarkers(QPieSeries *series);
107 void deleteMarkers(QSeries *series);
108 void updateLayout();
109
110 96 private:
111 qreal m_margin;
112
113 QRectF m_rect;
114 qreal m_offsetX;
115 qreal m_offsetY;
116
117 //QList<LegendMarker *> m_markers;
118
119 QBrush m_brush;
120 QPen m_pen;
121 QLegend::Alignments m_alignment;
122 QGraphicsItemGroup* m_markers;
123
124
125 bool m_attachedToChart;
126
127 QChart *m_chart;
128 qreal m_minWidth;
129 qreal m_minHeight;
130 qreal m_width;
131 qreal m_height;
132 bool m_backgroundVisible;
133 friend class ScrolledQLegend;
134 // <--- PIMPL
135 };
136
137 class ScrolledQLegend: public QLegend, public Scroller
138 {
139
140 public:
141 ScrolledQLegend(QChart *chart):QLegend(chart)
142 {
143 }
144
145 void setOffset(const QPointF& point)
146 {
147 QLegend::setOffset(point);
148 }
149 QPointF offset() const
150 {
151 return QLegend::offset();
152 }
153
154 void mousePressEvent(QGraphicsSceneMouseEvent* event){
155 Scroller::mousePressEvent(event);
156 //QLegend::mousePressEvent(event);
157 }
158 void mouseMoveEvent(QGraphicsSceneMouseEvent* event){
159 Scroller::mouseMoveEvent(event);
160 //QLegend::mouseMoveEvent(event);
161 }
162 void mouseReleaseEvent(QGraphicsSceneMouseEvent* event){
163 Scroller::mouseReleaseEvent(event);
164 //QLegend::mouseReleaseEvent(event);
165 }
97 QScopedPointer<QLegendPrivate> d_ptr;
98 Q_DISABLE_COPY(QLegend);
99 friend class LegendScroller;
166 100 };
167 101
168 102 QTCOMMERCIALCHART_END_NAMESPACE
@@ -65,6 +65,7 protected:
65 65 QScopedPointer<QSeriesPrivate> d_ptr;
66 66 friend class ChartDataSet;
67 67 friend class ChartPresenter;
68 friend class QLegendPrivate;
68 69 };
69 70
70 71 QTCOMMERCIALCHART_END_NAMESPACE
@@ -39,6 +39,8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
39 39 class Domain;
40 40 class ChartPresenter;
41 41 class Chart;
42 class LegendMarker;
43 class QLegend;
42 44
43 45 class QSeriesPrivate : public QObject
44 46 {
@@ -48,6 +50,7 public:
48 50
49 51 virtual void scaleDomain(Domain& domain) = 0;
50 52 virtual Chart* createGraphics(ChartPresenter* presenter) = 0;
53 virtual QList<LegendMarker*> createLegendMarker(QLegend* legend) = 0;
51 54
52 55 protected:
53 56 QSeries *q_ptr;
@@ -33,7 +33,9 PRIVATE_HEADERS += \
33 33 $$PWD/qchart_p.h \
34 34 $$PWD/qchartview_p.h \
35 35 $$PWD/scroller_p.h \
36 $$PWD/qseries_p.h
36 $$PWD/qseries_p.h \
37 $$PWD/qlegend_p.h \
38 $$PWD/legendscroller_p.h
37 39 PUBLIC_HEADERS += \
38 40 $$PWD/qchart.h \
39 41 $$PWD/qchartglobal.h \
@@ -21,6 +21,7
21 21 #include "qxyseries.h"
22 22 #include "qxyseries_p.h"
23 23 #include "domain_p.h"
24 #include "legendmarker_p.h"
24 25 #include <QAbstractItemModel>
25 26
26 27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
@@ -646,6 +647,13 void QXYSeriesPrivate::scaleDomain(Domain& domain)
646 647 domain.setRangeY(minY,maxY,tickYCount);
647 648 }
648 649
650 QList<LegendMarker*> QXYSeriesPrivate::createLegendMarker(QLegend* legend)
651 {
652 Q_Q(QXYSeries);
653 QList<LegendMarker*> list;
654 return list << new XYLegendMarker(q,legend);
655 }
656
649 657 #include "moc_qxyseries.cpp"
650 658 #include "moc_qxyseries_p.cpp"
651 659
@@ -44,6 +44,7 public:
44 44 QXYSeriesPrivate(QXYSeries* q);
45 45
46 46 void scaleDomain(Domain& domain);
47 QList<LegendMarker*> createLegendMarker(QLegend* legend);
47 48
48 49 Q_SIGNALS:
49 50 void updated();
General Comments 0
You need to be logged in to leave comments. Login now