##// END OF EJS Templates
more intelligent legend. fixed compiler warning in bar.cpp
sauimone -
r565:b4a66ce9ee2b
parent child
Show More
@@ -40,10 +40,10 Q_SIGNALS:
40
40
41 private:
41 private:
42
42
43 qreal mHeight;
44 qreal mWidth;
45 qreal mXpos;
43 qreal mXpos;
46 qreal mYpos;
44 qreal mYpos;
45 qreal mWidth;
46 qreal mHeight;
47
47
48 QBrush mBrush;
48 QBrush mBrush;
49 QPen mPen;
49 QPen mPen;
@@ -7,8 +7,36 QTCOMMERCIALCHART_BEGIN_NAMESPACE
7
7
8 LegendMarker::LegendMarker(QSeries* series, QGraphicsItem *parent)
8 LegendMarker::LegendMarker(QSeries* series, QGraphicsItem *parent)
9 : QGraphicsObject(parent)
9 : QGraphicsObject(parent)
10 ,mBoundingRect(0,0,1,1)
11 ,mName("")
10 ,mSeries(series)
12 ,mSeries(series)
13 ,mBarset(0)
14 ,mPieslice(0)
15 ,mType(LegendMarkerTypeSeries)
16 {
17 setAcceptedMouseButtons(Qt::LeftButton|Qt::RightButton);
18 }
19
20 LegendMarker::LegendMarker(QBarSet* barset, QGraphicsItem *parent)
21 : QGraphicsObject(parent)
22 ,mBoundingRect(0,0,1,1)
23 ,mName("")
24 ,mSeries(0)
25 ,mBarset(barset)
26 ,mPieslice(0)
27 ,mType(LegendMarkerTypeBarset)
28 {
29 setAcceptedMouseButtons(Qt::LeftButton|Qt::RightButton);
30 }
31
32 LegendMarker::LegendMarker(QPieSlice* pieslice, QGraphicsItem *parent)
33 : QGraphicsObject(parent)
11 ,mBoundingRect(0,0,1,1)
34 ,mBoundingRect(0,0,1,1)
35 ,mName("")
36 ,mSeries(0)
37 ,mBarset(0)
38 ,mPieslice(pieslice)
39 ,mType(LegendMarkerTypePieslice)
12 {
40 {
13 setAcceptedMouseButtons(Qt::LeftButton|Qt::RightButton);
41 setAcceptedMouseButtons(Qt::LeftButton|Qt::RightButton);
14 }
42 }
@@ -23,6 +51,11 void LegendMarker::setBrush(const QBrush brush)
23 mBrush = brush;
51 mBrush = brush;
24 }
52 }
25
53
54 QBrush LegendMarker::brush() const
55 {
56 return mBrush;
57 }
58
26 void LegendMarker::setName(const QString name)
59 void LegendMarker::setName(const QString name)
27 {
60 {
28 mName = name;
61 mName = name;
@@ -33,11 +66,6 QString LegendMarker::name() const
33 return mName;
66 return mName;
34 }
67 }
35
68
36 QColor LegendMarker::color() const
37 {
38 return mBrush.color();
39 }
40
41 void LegendMarker::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
69 void LegendMarker::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
42 {
70 {
43 painter->setBrush(mBrush);
71 painter->setBrush(mBrush);
@@ -51,11 +79,41 QRectF LegendMarker::boundingRect() const
51
79
52 void LegendMarker::mousePressEvent(QGraphicsSceneMouseEvent *event)
80 void LegendMarker::mousePressEvent(QGraphicsSceneMouseEvent *event)
53 {
81 {
54 if (event->button() == Qt::LeftButton) {
82 switch (mType)
55 emit clicked(mSeries, mName);
83 {
56 } else if (event->button() == Qt::RightButton) {
84 case LegendMarkerTypeSeries: {
57 emit rightClicked(mSeries, mName);
85
86 if (event->button() == Qt::LeftButton) {
87 emit clicked(mSeries);
88 } else if (event->button() == Qt::RightButton) {
89 emit rightClicked(mSeries);
90 }
91 break;
58 }
92 }
93 case LegendMarkerTypeBarset: {
94
95 if (event->button() == Qt::LeftButton) {
96 emit clicked(mBarset);
97 } else if (event->button() == Qt::RightButton) {
98 emit rightClicked(mBarset);
99 }
100 break;
101 }
102
103 case LegendMarkerTypePieslice: {
104
105 if (event->button() == Qt::LeftButton) {
106 emit clicked(mPieslice);
107 } else if (event->button() == Qt::RightButton) {
108 emit rightClicked(mPieslice);
109 }
110 break;
111 }
112 default: {
113 break;
114 }
115 }
116
59 }
117 }
60
118
61 #include "moc_legendmarker_p.cpp"
119 #include "moc_legendmarker_p.cpp"
@@ -8,18 +8,31
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9
9
10 class QSeries;
10 class QSeries;
11 class QBarSet;
12 class QPieSlice;
11
13
12
14 // TODO: split this to 3 different markers for series, barset and pieslice. Current implementation is easier to misuse...
13 class LegendMarker : public QGraphicsObject
15 class LegendMarker : public QGraphicsObject
14 {
16 {
15 Q_OBJECT
17 Q_OBJECT
18
19 enum LegendMarkerType {
20 LegendMarkerTypeSeries,
21 LegendMarkerTypeBarset,
22 LegendMarkerTypePieslice
23 };
24
16 public:
25 public:
17 LegendMarker(QSeries* series, QGraphicsItem *parent = 0);
26 LegendMarker(QSeries* series, QGraphicsItem *parent = 0);
27 LegendMarker(QBarSet* barset, QGraphicsItem *parent = 0);
28 LegendMarker(QPieSlice* pieslice, QGraphicsItem *parent = 0);
18 void setBoundingRect(const QRectF rect);
29 void setBoundingRect(const QRectF rect);
30
19 void setBrush(const QBrush brush);
31 void setBrush(const QBrush brush);
32 QBrush brush() const;
33
20 void setName(const QString name);
34 void setName(const QString name);
21 QString name() const;
35 QString name() const;
22 QColor color() const;
23
36
24 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
37 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
25
38
@@ -30,14 +43,22 public:
30 void mousePressEvent(QGraphicsSceneMouseEvent *event);
43 void mousePressEvent(QGraphicsSceneMouseEvent *event);
31
44
32 Q_SIGNALS:
45 Q_SIGNALS:
33 void clicked(QSeries* series, QString name);
46 void clicked(QSeries* series);
34 void rightClicked(QSeries* series, QString name);
47 void rightClicked(QSeries* series);
48 void clicked(QBarSet* barset);
49 void rightClicked(QBarSet* barset);
50 void clicked(QPieSlice* pieslice);
51 void rightClicked(QPieSlice* pieslice);
35
52
36 private:
53 private:
37 QRectF mBoundingRect;
54 QRectF mBoundingRect;
38 QBrush mBrush;
55 QBrush mBrush;
39 QString mName;
56 QString mName;
40 QSeries* mSeries;
57 QSeries* mSeries;
58 QBarSet* mBarset;
59 QPieSlice* mPieslice;
60
61 LegendMarkerType mType;
41 };
62 };
42
63
43 QTCOMMERCIALCHART_END_NAMESPACE
64 QTCOMMERCIALCHART_END_NAMESPACE
@@ -2,6 +2,17
2 #include "qlegend.h"
2 #include "qlegend.h"
3 #include "qseries.h"
3 #include "qseries.h"
4 #include "legendmarker_p.h"
4 #include "legendmarker_p.h"
5 #include "qxyseries.h"
6 #include "qlineseries.h"
7 #include "qareaseries.h"
8 #include "qscatterseries.h"
9 #include "qsplineseries.h"
10 #include "qbarseries.h"
11 #include "qstackedbarseries.h"
12 #include "qpercentbarseries.h"
13 #include "qbarset.h"
14 #include "qpieseries.h"
15 #include "qpieslice.h"
5 #include <QPainter>
16 #include <QPainter>
6 #include <QPen>
17 #include <QPen>
7
18
@@ -23,7 +34,7 void QLegend::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, Q
23
34
24 foreach(LegendMarker* m, mMarkers) {
35 foreach(LegendMarker* m, mMarkers) {
25 QRectF r = m->boundingRect();
36 QRectF r = m->boundingRect();
26 painter->setPen(m->color());
37 painter->setBrush(m->brush());
27 painter->drawText(r.x() + r.width()*2, r.y() + r.height(), m->name());
38 painter->drawText(r.x() + r.width()*2, r.y() + r.height(), m->name());
28 }
39 }
29 }
40 }
@@ -46,14 +57,76 QBrush QLegend::backgroundBrush() const
46 void QLegend::handleSeriesAdded(QSeries* series,Domain* domain)
57 void QLegend::handleSeriesAdded(QSeries* series,Domain* domain)
47 {
58 {
48 mSeriesList.append(series);
59 mSeriesList.append(series);
49 dataChanged();
60
61 switch (series->type())
62 {
63 case QSeries::SeriesTypeLine: {
64
65 QLineSeries* lineSeries = static_cast<QLineSeries*>(series);
66 createMarker(lineSeries);
67 break;
68 }
69 case QSeries::SeriesTypeArea: {
70
71 QAreaSeries* areaSeries = static_cast<QAreaSeries*>(series);
72 createMarker(areaSeries->upperSeries());
73 createMarker(areaSeries->lowerSeries());
74 break;
75 }
76
77 case QSeries::SeriesTypeBar: {
78
79 QBarSeries* barSeries = static_cast<QBarSeries*>(series);
80 createMarkers(barSeries);
81 break;
82 }
83
84 case QSeries::SeriesTypeStackedBar: {
85
86 QStackedBarSeries* stackedBarSeries = static_cast<QStackedBarSeries*>(series);
87 createMarkers(stackedBarSeries);
88 break;
89 }
90
91 case QSeries::SeriesTypePercentBar: {
92
93 QPercentBarSeries* percentBarSeries = static_cast<QPercentBarSeries*>(series);
94 createMarkers(percentBarSeries);
95 break;
96 }
97
98 case QSeries::SeriesTypeScatter: {
99
100 QScatterSeries *scatterSeries = static_cast<QScatterSeries *>(series);
101 createMarker(scatterSeries);
102 break;
103 }
104
105 case QSeries::SeriesTypePie: {
106
107 QPieSeries *pieSeries = static_cast<QPieSeries *>(series);
108 createMarkers(pieSeries);
109 break;
110 }
111
112 case QSeries::SeriesTypeSpline: {
113
114 QSplineSeries* splineSeries = static_cast<QSplineSeries*>(series);
115 break;
116 }
117 default: {
118 qDebug()<< "QLegend::handleSeriesAdded" << series->type() << "not implemented.";
119 break;
120 }
121 }
122
50 layoutChanged();
123 layoutChanged();
51 }
124 }
52
125
53 void QLegend::handleSeriesRemoved(QSeries* series)
126 void QLegend::handleSeriesRemoved(QSeries* series)
54 {
127 {
128 // TODO: delete markers, disconnect.
55 mSeriesList.removeOne(series);
129 mSeriesList.removeOne(series);
56 dataChanged();
57 layoutChanged();
130 layoutChanged();
58 }
131 }
59
132
@@ -63,26 +136,40 void QLegend::handleGeometryChanged(const QRectF& size)
63 layoutChanged();
136 layoutChanged();
64 }
137 }
65
138
66 void QLegend::dataChanged()
139 void QLegend::createMarker(QXYSeries* series)
67 {
140 {
68 foreach (LegendMarker* marker, mMarkers) {
141 LegendMarker* marker = new LegendMarker(series,this);
69 disconnect(marker,SIGNAL(clicked(QSeries*,QString)),this,SIGNAL(clicked(QSeries*,QString)));
142 marker->setName(series->name());
70 disconnect(marker,SIGNAL(rightClicked(QSeries*,QString)),this,SIGNAL(rightClicked(QSeries*,QString)));
143 marker->setBrush(series->brush());
71 delete marker;
144 connect(marker,SIGNAL(clicked(QSeries*)),this,SIGNAL(markerClicked(QSeries*)));
145 connect(marker,SIGNAL(rightClicked(QSeries*)),this,SIGNAL(markerRightClicked(QSeries*)));
146 mMarkers.append(marker);
147 childItems().append(marker);
148 }
149
150 void QLegend::createMarkers(QBarSeries *series)
151 {
152 foreach(QBarSet* s, series->barSets()) {
153 LegendMarker* marker = new LegendMarker(series,this);
154 marker->setName(s->name());
155 marker->setBrush(s->brush());
156 connect(marker,SIGNAL(clicked(QBarSet*)),this,SIGNAL(markerClicked(QBarSet*)));
157 connect(marker,SIGNAL(rightClicked(QBarSet*)),this,SIGNAL(markerRightClicked(QBarSet*)));
158 mMarkers.append(marker);
159 childItems().append(marker);
72 }
160 }
161 }
73
162
74 mMarkers.clear();
163 void QLegend::createMarkers(QPieSeries *series)
75
164 {
76 foreach (QSeries* s, mSeriesList) {
165 foreach(QPieSlice* s, series->slices()) {
77 for (int i=0; i<s->legendEntries().count(); i++) {
166 LegendMarker* marker = new LegendMarker(series,this);
78 LegendMarker *marker = new LegendMarker(s, this);
167 marker->setName(s->label());
79 marker->setBrush(s->legendEntries().at(i).mBrush);
168 marker->setBrush(s->sliceBrush());
80 marker->setName(s->legendEntries().at(i).mName);
169 connect(marker,SIGNAL(clicked(QPieSlice*)),this,SIGNAL(markerClicked(QPieSlice*)));
81 mMarkers.append(marker);
170 connect(marker,SIGNAL(rightClicked(QPieSlice*)),this,SIGNAL(markerRightClicked(QPieSlice*)));
82 childItems().append(marker);
171 mMarkers.append(marker);
83 connect(marker,SIGNAL(clicked(QSeries*,QString)),this,SIGNAL(clicked(QSeries*,QString)));
172 childItems().append(marker);
84 connect(marker,SIGNAL(rightClicked(QSeries*,QString)),this,SIGNAL(rightClicked(QSeries*,QString)));
85 }
86 }
173 }
87 }
174 }
88
175
@@ -9,6 +9,11 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9
9
10 class Domain;
10 class Domain;
11 class LegendMarker;
11 class LegendMarker;
12 class QPieSlice;
13 class QXYSeries;
14 class QBarSet;
15 class QBarSeries;
16 class QPieSeries;
12
17
13 class QTCOMMERCIALCHART_EXPORT QLegend : public QGraphicsObject
18 class QTCOMMERCIALCHART_EXPORT QLegend : public QGraphicsObject
14 {
19 {
@@ -25,8 +30,12 public:
25
30
26 signals:
31 signals:
27 // for interactions.
32 // for interactions.
28 void clicked(QSeries* series, QString name);
33 void clicked(QSeries* series);
29 void rightClicked(QSeries* series, QString name);
34 void rightClicked(QSeries* series);
35 void clicked(QBarSet* barset);
36 void rightClicked(QBarSet* series);
37 void clicked(QPieSlice* slice);
38 void rightClicked(QPieSlice* series);
30
39
31 public slots:
40 public slots:
32 void handleSeriesAdded(QSeries* series,Domain* domain);
41 void handleSeriesAdded(QSeries* series,Domain* domain);
@@ -34,8 +43,13 public slots:
34 void handleGeometryChanged(const QRectF& size);
43 void handleGeometryChanged(const QRectF& size);
35
44
36 private:
45 private:
37 void dataChanged();
46 // PIMPL --->
47 void createMarker(QXYSeries* series);
48 void createMarkers(QBarSeries* series);
49 void createMarkers(QPieSeries* series);
38 void layoutChanged();
50 void layoutChanged();
51 // <--- PIMPL
52
39
53
40 QRectF mBoundingRect;
54 QRectF mBoundingRect;
41 QList<QSeries*> mSeriesList;
55 QList<QSeries*> mSeriesList;
@@ -37,6 +37,7 protected:
37 public:
37 public:
38 virtual ~QSeries() {}
38 virtual ~QSeries() {}
39 virtual QSeriesType type() const = 0;
39 virtual QSeriesType type() const = 0;
40 QString name() { return QString("TODO: Name QSeries"); }
40 // TODO
41 // TODO
41 virtual bool setModel(QAbstractItemModel* /*model*/) { return false; }
42 virtual bool setModel(QAbstractItemModel* /*model*/) { return false; }
42
43
General Comments 0
You need to be logged in to leave comments. Login now