##// END OF EJS Templates
legend detach example. Bug fixes to legend.
sauimone -
r1263:d5f432b5ffc0
parent child
Show More
@@ -0,0 +1,12
1 !include( ../examples.pri ) {
2 error( "Couldn't find the examples.pri file!" )
3 }
4
5 TARGET = legend
6 SOURCES += main.cpp \
7 mainwidget.cpp
8
9 !system_build:mac: QMAKE_POST_LINK += "$$MAC_POST_LINK_PREFIX $$MAC_EXAMPLES_BIN_DIR"
10
11 HEADERS += \
12 mainwidget.h
@@ -0,0 +1,41
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 #include "mainwidget.h"
22
23 #include <QApplication>
24 #include <QMainWindow>
25 #include <QChartView>
26 #include <QBarSeries>
27 #include <QBarSet>
28 #include <QLegend>
29
30 QTCOMMERCIALCHART_USE_NAMESPACE
31
32 int main(int argc, char *argv[])
33 {
34 QApplication a(argc, argv);
35
36 MainWidget w;
37 w.resize(1024,768);
38 w.show();
39
40 return a.exec();
41 }
@@ -0,0 +1,134
1 #include "mainwidget.h"
2 #include <QChart>
3 #include <QChartView>
4 #include <QPushButton>
5 #include <QLabel>
6 #include <QDebug>
7 #include <QBarSet>
8 #include <QBarSeries>
9 #include <QLegend>
10
11 QTCOMMERCIALCHART_USE_NAMESPACE
12
13 MainWidget::MainWidget(QWidget *parent) :
14 QWidget(parent)
15 {
16 m_setCount = 0;
17 m_chart = new QChart();
18
19 m_buttonLayout = new QGridLayout();
20 QPushButton *detachLegendButton = new QPushButton("detach legend");
21 connect(detachLegendButton, SIGNAL(clicked()), this, SLOT(detachLegend()));
22 m_buttonLayout->addWidget(detachLegendButton, 0, 0);
23 QPushButton *attachLegendButton = new QPushButton("attach legend");
24 connect(attachLegendButton, SIGNAL(clicked()), this, SLOT(attachLegend()));
25 m_buttonLayout->addWidget(attachLegendButton, 1, 0);
26
27 QPushButton *addSetButton = new QPushButton("add barset");
28 connect(addSetButton, SIGNAL(clicked()), this, SLOT(addBarset()));
29 m_buttonLayout->addWidget(addSetButton, 2, 0);
30 QPushButton *removeBarsetButton = new QPushButton("remove barset");
31 connect(removeBarsetButton, SIGNAL(clicked()), this, SLOT(removeBarset()));
32 m_buttonLayout->addWidget(removeBarsetButton, 3, 0);
33
34 // add row with empty label to make all the other rows static
35 m_buttonLayout->addWidget(new QLabel(""), m_buttonLayout->rowCount(), 0);
36 m_buttonLayout->setRowStretch(m_buttonLayout->rowCount() - 1, 1);
37
38 // Create chart view with the chart
39 m_chartView = new QChartView(m_chart, this);
40 m_chartView->setRubberBand(QChartView::HorizonalRubberBand);
41
42 m_customView = new QGraphicsView(this);
43 m_customScene = new QGraphicsScene(this);
44 m_customView->setScene(m_customScene);
45
46 // Create layout for grid and detached legend
47 m_mainLayout = new QGridLayout();
48 m_mainLayout->addLayout(m_buttonLayout, 0, 0);
49 m_mainLayout->addWidget(m_chartView, 0, 1, 3, 1);
50 m_mainLayout->addWidget(m_customView, 0, 2, 3, 1);
51 setLayout(m_mainLayout);
52
53 createSeries();
54 }
55
56 void MainWidget::createSeries()
57 {
58 //![1]
59 m_series = new QBarSeries();
60
61 addBarset();
62 addBarset();
63 addBarset();
64 addBarset();
65 //![1]
66
67 //![2]
68 m_chart->addSeries(m_series);
69 m_chart->setTitle("Legend detach example");
70 //![2]
71
72 //![3]
73 m_chart->legend()->setVisible(true);
74 m_chart->legend()->setAlignment(QLegend::AlignmentBottom);
75 m_chart->axisY()->setNiceNumbersEnabled(true);
76 //![3]
77
78 //![4]
79 m_chartView->setRenderHint(QPainter::Antialiasing);
80 //![4]
81 }
82
83 void MainWidget::attachLegend()
84 {
85 qDebug() << "attach legend";
86 QLegend *legend = m_chart->legend();
87
88 if (m_customScene->items().contains(legend)) {
89 qDebug() << "legend removed from other scene";
90 m_customScene->removeItem(legend);
91 legend->setParent(m_chart);
92 legend->attachToChart();
93 }
94
95 // legend->attachToChart();
96 // This causes redraw
97 QSize size(1,1);
98 this->resize(this->size() + size);
99 this->resize(this->size() - size);
100 }
101
102 void MainWidget::detachLegend()
103 {
104 qDebug() << "detach legend";
105 QLegend *legend = m_chart->legend();
106 legend->detachFromChart();
107
108 m_customScene->addItem(legend);
109 // m_mainLayout->addWidget(legend,0,2,3,1);
110 // setLayout(m_layout);
111
112 // TODO: layout legend to somewhere else
113 // This causes redraw
114 QSize size(1,1);
115 this->resize(this->size() + size);
116 this->resize(this->size() - size);
117 }
118
119 void MainWidget::addBarset()
120 {
121 QBarSet *barSet = new QBarSet(QString("set ") + QString::number(m_setCount));
122 m_setCount++;
123 qreal delta = m_series->barsetCount() * 0.1;
124 *barSet << QPointF(0.0 + delta, 1 + delta) << QPointF(1.0 + delta, 2 + delta) << QPointF(2.0 + delta, 3 + delta) << QPointF(3.0 + delta, 4 + delta);
125 m_series->append(barSet);
126 }
127
128 void MainWidget::removeBarset()
129 {
130 QList<QBarSet*> sets = m_series->barSets();
131 if (sets.count() > 0) {
132 m_series->remove(sets.at(sets.count()-1));
133 }
134 }
@@ -0,0 +1,49
1 #ifndef MAINWIDGET_H
2 #define MAINWIDGET_H
3
4 #include "qchartglobal.h"
5 #include "qchart.h"
6 #include "qchartview.h"
7 #include <QWidget>
8 #include <QGraphicsWidget>
9 #include <QGridLayout>
10 #include <QGraphicsGridLayout>
11
12 QTCOMMERCIALCHART_USE_NAMESPACE
13
14 class MainWidget : public QWidget
15 //class MainWidget : public QGraphicsWidget
16 {
17 Q_OBJECT
18 public:
19 explicit MainWidget(QWidget *parent = 0);
20
21 void createSeries();
22
23 signals:
24
25 public slots:
26 void attachLegend();
27 void detachLegend();
28 void addBarset();
29 void removeBarset();
30
31 private:
32
33 QChart *m_chart;
34 QBarSeries *m_series;
35
36 QGraphicsScene *m_scene;
37 QChartView *m_chartView;
38 QGridLayout *m_mainLayout;
39 QGridLayout *m_buttonLayout;
40
41 QGraphicsView *m_customView;
42 QGraphicsScene *m_customScene;
43 QGraphicsGridLayout *m_customLayout;
44
45
46 int m_setCount;
47 };
48
49 #endif // MAINWIDGET_H
@@ -20,4 +20,5 SUBDIRS += \
20 20 stackedbarchartdrilldown \
21 21 zoomlinechart \
22 22 modeldata \
23 groupedbarchart
23 groupedbarchart \
24 legend
@@ -75,7 +75,10 QBarSeries::QBarSeries(QObject *parent) :
75 75 */
76 76 QBarSeries::~QBarSeries()
77 77 {
78 // NOTE: d_ptr destroyed by QObject
78 Q_D(QBarSeries);
79 if(d->m_dataset){
80 d->m_dataset->removeSeries(this);
81 }
79 82 }
80 83
81 84 /*!
@@ -112,14 +115,7 void QBarSeries::setCategories(QBarCategories categories)
112 115 bool QBarSeries::append(QBarSet *set)
113 116 {
114 117 Q_D(QBarSeries);
115 if ((d->m_barSets.contains(set)) || (set == 0)) {
116 // Fail if set is already in list or set is null.
117 return false;
118 }
119 d->m_barSets.append(set);
120 QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), d, SLOT(barsetChanged()));
121 emit d->restructuredBars();
122 return true;
118 return d->append(set);
123 119 }
124 120
125 121 /*!
@@ -129,14 +125,7 bool QBarSeries::append(QBarSet *set)
129 125 bool QBarSeries::remove(QBarSet *set)
130 126 {
131 127 Q_D(QBarSeries);
132 if (!d->m_barSets.contains(set)) {
133 // Fail if set is not in list
134 return false;
135 }
136 d->m_barSets.removeOne(set);
137 QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedBars()), d, SLOT(barsetChanged()));
138 emit d->restructuredBars();
139 return true;
128 return d->remove(set);
140 129 }
141 130
142 131 /*!
@@ -148,23 +137,7 bool QBarSeries::remove(QBarSet *set)
148 137 bool QBarSeries::append(QList<QBarSet* > sets)
149 138 {
150 139 Q_D(QBarSeries);
151 foreach (QBarSet* set, sets) {
152 if ((set == 0) || (d->m_barSets.contains(set))) {
153 // Fail if any of the sets is null or is already appended.
154 return false;
155 }
156 if (sets.count(set) != 1) {
157 // Also fail if same set is more than once in given list.
158 return false;
159 }
160 }
161
162 foreach (QBarSet* set, sets) {
163 d->m_barSets.append(set);
164 QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), d, SLOT(barsetChanged()));
165 }
166 emit d->restructuredBars();
167 return true;
140 return d->append(sets);
168 141 }
169 142
170 143 /*!
@@ -173,20 +146,7 bool QBarSeries::append(QList<QBarSet* > sets)
173 146 bool QBarSeries::remove(QList<QBarSet* > sets)
174 147 {
175 148 Q_D(QBarSeries);
176
177 bool setsRemoved = false;
178 foreach (QBarSet* set, sets) {
179 if (d->m_barSets.contains(set)) {
180 d->m_barSets.removeOne(set);
181 QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedBars()), d, SLOT(barsetChanged()));
182 setsRemoved = true;
183 }
184 }
185
186 if (setsRemoved) {
187 emit d->restructuredBars();
188 }
189 return setsRemoved;
149 return d->remove(sets);
190 150 }
191 151
192 152 /*!
@@ -454,7 +414,7 void QBarSeriesPrivate::scaleDomain(Domain& domain)
454 414 qreal y = max();
455 415 minX = qMin(minX, x) - 0.5;
456 416 minY = qMin(minY, y);
457 maxX = qMax(maxX, x) - 0.5;
417 maxX = qMax(maxX, x) + 0.5;
458 418 maxY = qMax(maxY, y);
459 419 tickXCount = x+1;
460 420
@@ -486,6 +446,84 QList<LegendMarker*> QBarSeriesPrivate::createLegendMarker(QLegend* legend)
486 446 return markers;
487 447 }
488 448
449 bool QBarSeriesPrivate::append(QBarSet *set)
450 {
451 Q_Q(QBarSeries);
452 if ((m_barSets.contains(set)) || (set == 0)) {
453 // Fail if set is already in list or set is null.
454 return false;
455 }
456 m_barSets.append(set);
457 QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SLOT(barsetChanged()));
458 if (m_dataset) {
459 m_dataset->updateSeries(q); // this notifies legend
460 }
461 emit restructuredBars(); // this notifies barchartitem
462 return true;
463 }
464
465 bool QBarSeriesPrivate::remove(QBarSet *set)
466 {
467 Q_Q(QBarSeries);
468 if (!m_barSets.contains(set)) {
469 // Fail if set is not in list
470 return false;
471 }
472 m_barSets.removeOne(set);
473 QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SLOT(barsetChanged()));
474 if (m_dataset) {
475 m_dataset->updateSeries(q); // this notifies legend
476 }
477 emit restructuredBars(); // this notifies barchartitem
478 return true;
479 }
480
481 bool QBarSeriesPrivate::append(QList<QBarSet* > sets)
482 {
483 Q_Q(QBarSeries);
484 foreach (QBarSet* set, sets) {
485 if ((set == 0) || (m_barSets.contains(set))) {
486 // Fail if any of the sets is null or is already appended.
487 return false;
488 }
489 if (sets.count(set) != 1) {
490 // Also fail if same set is more than once in given list.
491 return false;
492 }
493 }
494
495 foreach (QBarSet* set, sets) {
496 m_barSets.append(set);
497 QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SLOT(barsetChanged()));
498 }
499 if (m_dataset) {
500 m_dataset->updateSeries(q); // this notifies legend
501 }
502 emit restructuredBars(); // this notifies barchartitem
503 return true;
504 }
505
506 bool QBarSeriesPrivate::remove(QList<QBarSet* > sets)
507 {
508 Q_Q(QBarSeries);
509 bool setsRemoved = false;
510 foreach (QBarSet* set, sets) {
511 if (m_barSets.contains(set)) {
512 m_barSets.removeOne(set);
513 QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SLOT(barsetChanged()));
514 setsRemoved = true;
515 }
516 }
517
518 if (setsRemoved) {
519 if (m_dataset) {
520 m_dataset->updateSeries(q); // this notifies legend
521 }
522 emit restructuredBars(); // this notifies barchartitem
523 }
524 return setsRemoved;
525 }
526
489 527 #include "moc_qbarseries.cpp"
490 528 #include "moc_qbarseries_p.cpp"
491 529
@@ -57,6 +57,11 public:
57 57 Chart* createGraphics(ChartPresenter* presenter);
58 58 QList<LegendMarker*> createLegendMarker(QLegend* legend);
59 59
60 bool append(QBarSet *set);
61 bool remove(QBarSet *set);
62 bool append(QList<QBarSet* > sets);
63 bool remove(QList<QBarSet* > sets);
64
60 65 QBarSet* barsetAt(int index);
61 66 QString categoryName(int category);
62 67 qreal min();
@@ -286,6 +286,11 QList<QAbstractSeries*> ChartDataSet::series() const
286 286 return m_seriesAxisMap.keys();
287 287 }
288 288
289 void ChartDataSet::updateSeries(QAbstractSeries *series)
290 {
291 emit seriesUpdated(series);
292 }
293
289 294 #include "moc_chartdataset_p.cpp"
290 295
291 296 QTCOMMERCIALCHART_END_NAMESPACE
@@ -49,6 +49,7 public:
49 49 void addSeries(QAbstractSeries* series,QAxis *axisY = 0);
50 50 QAxis* removeSeries(QAbstractSeries* series);
51 51 void removeAllSeries();
52 void updateSeries(QAbstractSeries* series);
52 53
53 54 void zoomInDomain(const QRectF& rect, const QSizeF& size);
54 55 void zoomOutDomain(const QRectF& rect, const QSizeF& size);
@@ -68,6 +69,7 public:
68 69 Q_SIGNALS:
69 70 void seriesAdded(QAbstractSeries* series, Domain* domain);
70 71 void seriesRemoved(QAbstractSeries* series);
72 void seriesUpdated(QAbstractSeries* series);
71 73 void axisAdded(QAxis* axis,Domain* domain);
72 74 void axisRemoved(QAxis* axis);
73 75
@@ -93,6 +93,7 d_ptr(new QLegendPrivate(chart->d_ptr->m_presenter,this))
93 93 setFlags(QGraphicsItem::ItemClipsChildrenToShape);
94 94 QObject::connect(chart->d_ptr->m_dataset,SIGNAL(seriesAdded(QAbstractSeries*,Domain*)),d_ptr.data(),SLOT(handleSeriesAdded(QAbstractSeries*,Domain*)));
95 95 QObject::connect(chart->d_ptr->m_dataset,SIGNAL(seriesRemoved(QAbstractSeries*)),d_ptr.data(),SLOT(handleSeriesRemoved(QAbstractSeries*)));
96 QObject::connect(chart->d_ptr->m_dataset,SIGNAL(seriesUpdated(QAbstractSeries*)),d_ptr.data(),SLOT(handleSeriesUpdated(QAbstractSeries*)));
96 97 }
97 98
98 99 /*!
@@ -444,6 +445,15 void QLegendPrivate::handleSeriesRemoved(QAbstractSeries *series)
444 445 updateLayout();
445 446 }
446 447
448 void QLegendPrivate::handleSeriesUpdated(QAbstractSeries *series)
449 {
450 // TODO: find out which markers are are added or removed. Update them
451 // TODO: better implementation
452 handleSeriesRemoved(series);
453 Domain domain;
454 handleSeriesAdded(series, &domain);
455 }
456
447 457 void QLegendPrivate::handleUpdatePieSeries()
448 458 {
449 459 //TODO: reimplement to be optimal
@@ -50,6 +50,7 public:
50 50 public Q_SLOTS:
51 51 void handleSeriesAdded(QAbstractSeries *series, Domain *domain);
52 52 void handleSeriesRemoved(QAbstractSeries *series);
53 void handleSeriesUpdated(QAbstractSeries *series);
53 54 void handleUpdatePieSeries(); //TODO remove this function
54 55
55 56 private:
General Comments 0
You need to be logged in to leave comments. Login now