@@ -40,10 +40,10 Q_SIGNALS: | |||
|
40 | 40 | |
|
41 | 41 | private: |
|
42 | 42 | |
|
43 | qreal mHeight; | |
|
44 | qreal mWidth; | |
|
45 | 43 | qreal mXpos; |
|
46 | 44 | qreal mYpos; |
|
45 | qreal mWidth; | |
|
46 | qreal mHeight; | |
|
47 | 47 | |
|
48 | 48 | QBrush mBrush; |
|
49 | 49 | QPen mPen; |
@@ -7,8 +7,36 QTCOMMERCIALCHART_BEGIN_NAMESPACE | |||
|
7 | 7 | |
|
8 | 8 | LegendMarker::LegendMarker(QSeries* series, QGraphicsItem *parent) |
|
9 | 9 | : QGraphicsObject(parent) |
|
10 | ,mBoundingRect(0,0,1,1) | |
|
11 | ,mName("") | |
|
10 | 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 | 34 | ,mBoundingRect(0,0,1,1) |
|
35 | ,mName("") | |
|
36 | ,mSeries(0) | |
|
37 | ,mBarset(0) | |
|
38 | ,mPieslice(pieslice) | |
|
39 | ,mType(LegendMarkerTypePieslice) | |
|
12 | 40 | { |
|
13 | 41 | setAcceptedMouseButtons(Qt::LeftButton|Qt::RightButton); |
|
14 | 42 | } |
@@ -23,6 +51,11 void LegendMarker::setBrush(const QBrush brush) | |||
|
23 | 51 | mBrush = brush; |
|
24 | 52 | } |
|
25 | 53 | |
|
54 | QBrush LegendMarker::brush() const | |
|
55 | { | |
|
56 | return mBrush; | |
|
57 | } | |
|
58 | ||
|
26 | 59 | void LegendMarker::setName(const QString name) |
|
27 | 60 | { |
|
28 | 61 | mName = name; |
@@ -33,11 +66,6 QString LegendMarker::name() const | |||
|
33 | 66 | return mName; |
|
34 | 67 | } |
|
35 | 68 | |
|
36 | QColor LegendMarker::color() const | |
|
37 | { | |
|
38 | return mBrush.color(); | |
|
39 | } | |
|
40 | ||
|
41 | 69 | void LegendMarker::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) |
|
42 | 70 | { |
|
43 | 71 | painter->setBrush(mBrush); |
@@ -51,11 +79,41 QRectF LegendMarker::boundingRect() const | |||
|
51 | 79 | |
|
52 | 80 | void LegendMarker::mousePressEvent(QGraphicsSceneMouseEvent *event) |
|
53 | 81 | { |
|
54 | if (event->button() == Qt::LeftButton) { | |
|
55 | emit clicked(mSeries, mName); | |
|
56 | } else if (event->button() == Qt::RightButton) { | |
|
57 | emit rightClicked(mSeries, mName); | |
|
82 | switch (mType) | |
|
83 | { | |
|
84 | case LegendMarkerTypeSeries: { | |
|
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 | 119 | #include "moc_legendmarker_p.cpp" |
@@ -8,18 +8,31 | |||
|
8 | 8 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
9 | 9 | |
|
10 | 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 | 15 | class LegendMarker : public QGraphicsObject |
|
14 | 16 | { |
|
15 | 17 | Q_OBJECT |
|
18 | ||
|
19 | enum LegendMarkerType { | |
|
20 | LegendMarkerTypeSeries, | |
|
21 | LegendMarkerTypeBarset, | |
|
22 | LegendMarkerTypePieslice | |
|
23 | }; | |
|
24 | ||
|
16 | 25 | public: |
|
17 | 26 | LegendMarker(QSeries* series, QGraphicsItem *parent = 0); |
|
27 | LegendMarker(QBarSet* barset, QGraphicsItem *parent = 0); | |
|
28 | LegendMarker(QPieSlice* pieslice, QGraphicsItem *parent = 0); | |
|
18 | 29 | void setBoundingRect(const QRectF rect); |
|
30 | ||
|
19 | 31 | void setBrush(const QBrush brush); |
|
32 | QBrush brush() const; | |
|
33 | ||
|
20 | 34 | void setName(const QString name); |
|
21 | 35 | QString name() const; |
|
22 | QColor color() const; | |
|
23 | 36 | |
|
24 | 37 | void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0); |
|
25 | 38 | |
@@ -30,14 +43,22 public: | |||
|
30 | 43 | void mousePressEvent(QGraphicsSceneMouseEvent *event); |
|
31 | 44 | |
|
32 | 45 | Q_SIGNALS: |
|
33 |
void clicked(QSeries* series |
|
|
34 |
void rightClicked(QSeries* series |
|
|
46 | void clicked(QSeries* series); | |
|
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 | 53 | private: |
|
37 | 54 | QRectF mBoundingRect; |
|
38 | 55 | QBrush mBrush; |
|
39 | 56 | QString mName; |
|
40 | 57 | QSeries* mSeries; |
|
58 | QBarSet* mBarset; | |
|
59 | QPieSlice* mPieslice; | |
|
60 | ||
|
61 | LegendMarkerType mType; | |
|
41 | 62 | }; |
|
42 | 63 | |
|
43 | 64 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -2,6 +2,17 | |||
|
2 | 2 | #include "qlegend.h" |
|
3 | 3 | #include "qseries.h" |
|
4 | 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 | 16 | #include <QPainter> |
|
6 | 17 | #include <QPen> |
|
7 | 18 | |
@@ -23,7 +34,7 void QLegend::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, Q | |||
|
23 | 34 | |
|
24 | 35 | foreach(LegendMarker* m, mMarkers) { |
|
25 | 36 | QRectF r = m->boundingRect(); |
|
26 |
painter->set |
|
|
37 | painter->setBrush(m->brush()); | |
|
27 | 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 | 57 | void QLegend::handleSeriesAdded(QSeries* series,Domain* domain) |
|
47 | 58 | { |
|
48 | 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 | 123 | layoutChanged(); |
|
51 | 124 | } |
|
52 | 125 | |
|
53 | 126 | void QLegend::handleSeriesRemoved(QSeries* series) |
|
54 | 127 | { |
|
128 | // TODO: delete markers, disconnect. | |
|
55 | 129 | mSeriesList.removeOne(series); |
|
56 | dataChanged(); | |
|
57 | 130 | layoutChanged(); |
|
58 | 131 | } |
|
59 | 132 | |
@@ -63,26 +136,40 void QLegend::handleGeometryChanged(const QRectF& size) | |||
|
63 | 136 | layoutChanged(); |
|
64 | 137 | } |
|
65 | 138 | |
|
66 | void QLegend::dataChanged() | |
|
139 | void QLegend::createMarker(QXYSeries* series) | |
|
67 | 140 | { |
|
68 |
|
|
|
69 | disconnect(marker,SIGNAL(clicked(QSeries*,QString)),this,SIGNAL(clicked(QSeries*,QString))); | |
|
70 | disconnect(marker,SIGNAL(rightClicked(QSeries*,QString)),this,SIGNAL(rightClicked(QSeries*,QString))); | |
|
71 | delete marker; | |
|
141 | LegendMarker* marker = new LegendMarker(series,this); | |
|
142 | marker->setName(series->name()); | |
|
143 | marker->setBrush(series->brush()); | |
|
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(); | |
|
75 | ||
|
76 |
foreach |
|
|
77 | for (int i=0; i<s->legendEntries().count(); i++) { | |
|
78 | LegendMarker *marker = new LegendMarker(s, this); | |
|
79 |
|
|
|
80 | marker->setName(s->legendEntries().at(i).mName); | |
|
81 | mMarkers.append(marker); | |
|
82 |
|
|
|
83 | connect(marker,SIGNAL(clicked(QSeries*,QString)),this,SIGNAL(clicked(QSeries*,QString))); | |
|
84 | connect(marker,SIGNAL(rightClicked(QSeries*,QString)),this,SIGNAL(rightClicked(QSeries*,QString))); | |
|
85 | } | |
|
163 | void QLegend::createMarkers(QPieSeries *series) | |
|
164 | { | |
|
165 | foreach(QPieSlice* s, series->slices()) { | |
|
166 | LegendMarker* marker = new LegendMarker(series,this); | |
|
167 | marker->setName(s->label()); | |
|
168 | marker->setBrush(s->sliceBrush()); | |
|
169 | connect(marker,SIGNAL(clicked(QPieSlice*)),this,SIGNAL(markerClicked(QPieSlice*))); | |
|
170 | connect(marker,SIGNAL(rightClicked(QPieSlice*)),this,SIGNAL(markerRightClicked(QPieSlice*))); | |
|
171 | mMarkers.append(marker); | |
|
172 | childItems().append(marker); | |
|
86 | 173 | } |
|
87 | 174 | } |
|
88 | 175 |
@@ -9,6 +9,11 QTCOMMERCIALCHART_BEGIN_NAMESPACE | |||
|
9 | 9 | |
|
10 | 10 | class Domain; |
|
11 | 11 | class LegendMarker; |
|
12 | class QPieSlice; | |
|
13 | class QXYSeries; | |
|
14 | class QBarSet; | |
|
15 | class QBarSeries; | |
|
16 | class QPieSeries; | |
|
12 | 17 | |
|
13 | 18 | class QTCOMMERCIALCHART_EXPORT QLegend : public QGraphicsObject |
|
14 | 19 | { |
@@ -25,8 +30,12 public: | |||
|
25 | 30 | |
|
26 | 31 | signals: |
|
27 | 32 | // for interactions. |
|
28 |
void clicked(QSeries* series |
|
|
29 |
void rightClicked(QSeries* series |
|
|
33 | void clicked(QSeries* series); | |
|
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 | 40 | public slots: |
|
32 | 41 | void handleSeriesAdded(QSeries* series,Domain* domain); |
@@ -34,8 +43,13 public slots: | |||
|
34 | 43 | void handleGeometryChanged(const QRectF& size); |
|
35 | 44 | |
|
36 | 45 | private: |
|
37 | void dataChanged(); | |
|
46 | // PIMPL ---> | |
|
47 | void createMarker(QXYSeries* series); | |
|
48 | void createMarkers(QBarSeries* series); | |
|
49 | void createMarkers(QPieSeries* series); | |
|
38 | 50 | void layoutChanged(); |
|
51 | // <--- PIMPL | |
|
52 | ||
|
39 | 53 | |
|
40 | 54 | QRectF mBoundingRect; |
|
41 | 55 | QList<QSeries*> mSeriesList; |
General Comments 0
You need to be logged in to leave comments.
Login now