@@ -1,121 +1,105 | |||
|
1 | 1 | #include "qchartglobal.h" |
|
2 | 2 | #include "legendmarker_p.h" |
|
3 | 3 | #include <QPainter> |
|
4 | 4 | #include <QGraphicsSceneEvent> |
|
5 | 5 | |
|
6 | 6 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
7 | 7 | |
|
8 | 8 | LegendMarker::LegendMarker(QSeries* series, QGraphicsItem *parent) |
|
9 | 9 | : QGraphicsObject(parent) |
|
10 | 10 | ,mBoundingRect(0,0,1,1) |
|
11 | 11 | ,mName("") |
|
12 | 12 | ,mSeries(series) |
|
13 | 13 | ,mBarset(0) |
|
14 | 14 | ,mPieslice(0) |
|
15 | 15 | ,mType(LegendMarkerTypeSeries) |
|
16 | 16 | { |
|
17 | 17 | setAcceptedMouseButtons(Qt::LeftButton|Qt::RightButton); |
|
18 | 18 | } |
|
19 | 19 | |
|
20 | 20 | LegendMarker::LegendMarker(QBarSet* barset, QGraphicsItem *parent) |
|
21 | 21 | : QGraphicsObject(parent) |
|
22 | 22 | ,mBoundingRect(0,0,1,1) |
|
23 | 23 | ,mName("") |
|
24 | 24 | ,mSeries(0) |
|
25 | 25 | ,mBarset(barset) |
|
26 | 26 | ,mPieslice(0) |
|
27 | 27 | ,mType(LegendMarkerTypeBarset) |
|
28 | 28 | { |
|
29 | 29 | setAcceptedMouseButtons(Qt::LeftButton|Qt::RightButton); |
|
30 | 30 | } |
|
31 | 31 | |
|
32 | 32 | LegendMarker::LegendMarker(QPieSlice* pieslice, QGraphicsItem *parent) |
|
33 | 33 | : QGraphicsObject(parent) |
|
34 | 34 | ,mBoundingRect(0,0,1,1) |
|
35 | 35 | ,mName("") |
|
36 | 36 | ,mSeries(0) |
|
37 | 37 | ,mBarset(0) |
|
38 | 38 | ,mPieslice(pieslice) |
|
39 | 39 | ,mType(LegendMarkerTypePieslice) |
|
40 | 40 | { |
|
41 | 41 | setAcceptedMouseButtons(Qt::LeftButton|Qt::RightButton); |
|
42 | 42 | } |
|
43 | 43 | |
|
44 | 44 | void LegendMarker::setBoundingRect(const QRectF rect) |
|
45 | 45 | { |
|
46 | 46 | mBoundingRect = rect; |
|
47 | 47 | } |
|
48 | 48 | |
|
49 | 49 | void LegendMarker::setBrush(const QBrush brush) |
|
50 | 50 | { |
|
51 | 51 | mBrush = brush; |
|
52 | 52 | } |
|
53 | 53 | |
|
54 | 54 | QBrush LegendMarker::brush() const |
|
55 | 55 | { |
|
56 | 56 | return mBrush; |
|
57 | 57 | } |
|
58 | 58 | |
|
59 | 59 | void LegendMarker::setName(const QString name) |
|
60 | 60 | { |
|
61 | 61 | mName = name; |
|
62 | 62 | } |
|
63 | 63 | |
|
64 | 64 | QString LegendMarker::name() const |
|
65 | 65 | { |
|
66 | 66 | return mName; |
|
67 | 67 | } |
|
68 | 68 | |
|
69 | 69 | void LegendMarker::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) |
|
70 | 70 | { |
|
71 | 71 | painter->setBrush(mBrush); |
|
72 | 72 | painter->drawRect(mBoundingRect); |
|
73 | 73 | } |
|
74 | 74 | |
|
75 | 75 | QRectF LegendMarker::boundingRect() const |
|
76 | 76 | { |
|
77 | 77 | return mBoundingRect; |
|
78 | 78 | } |
|
79 | 79 | |
|
80 | 80 | void LegendMarker::mousePressEvent(QGraphicsSceneMouseEvent *event) |
|
81 | 81 | { |
|
82 | 82 | switch (mType) |
|
83 | 83 | { |
|
84 | 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 | } | |
|
85 | emit clicked(mSeries,event->button()); | |
|
91 | 86 | break; |
|
92 | 87 | } |
|
93 | 88 | 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 | } | |
|
89 | emit clicked(mBarset,event->button()); | |
|
100 | 90 | break; |
|
101 | 91 | } |
|
102 | ||
|
103 | 92 | 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 | } | |
|
93 | emit clicked(mPieslice,event->button()); | |
|
110 | 94 | break; |
|
111 | 95 | } |
|
112 | 96 | default: { |
|
113 | 97 | break; |
|
114 | 98 | } |
|
115 | 99 | } |
|
116 | 100 | |
|
117 | 101 | } |
|
118 | 102 | |
|
119 | 103 | #include "moc_legendmarker_p.cpp" |
|
120 | 104 | |
|
121 | 105 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,65 +1,62 | |||
|
1 | 1 | #ifndef LEGENDMARKER_P_H |
|
2 | 2 | #define LEGENDMARKER_P_H |
|
3 | 3 | |
|
4 | 4 | #include "qchartglobal.h" |
|
5 | 5 | #include <QGraphicsObject> |
|
6 | 6 | #include <QBrush> |
|
7 | 7 | |
|
8 | 8 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
9 | 9 | |
|
10 | 10 | class QSeries; |
|
11 | 11 | class QBarSet; |
|
12 | 12 | class QPieSlice; |
|
13 | 13 | |
|
14 | 14 | // TODO: split this to 3 different markers for series, barset and pieslice. Current implementation is easier to misuse... |
|
15 | 15 | class LegendMarker : public QGraphicsObject |
|
16 | 16 | { |
|
17 | 17 | Q_OBJECT |
|
18 | 18 | |
|
19 | 19 | enum LegendMarkerType { |
|
20 | 20 | LegendMarkerTypeSeries, |
|
21 | 21 | LegendMarkerTypeBarset, |
|
22 | 22 | LegendMarkerTypePieslice |
|
23 | 23 | }; |
|
24 | 24 | |
|
25 | 25 | public: |
|
26 | 26 | LegendMarker(QSeries* series, QGraphicsItem *parent = 0); |
|
27 | 27 | LegendMarker(QBarSet* barset, QGraphicsItem *parent = 0); |
|
28 | 28 | LegendMarker(QPieSlice* pieslice, QGraphicsItem *parent = 0); |
|
29 | 29 | void setBoundingRect(const QRectF rect); |
|
30 | 30 | |
|
31 | 31 | void setBrush(const QBrush brush); |
|
32 | 32 | QBrush brush() const; |
|
33 | 33 | |
|
34 | 34 | void setName(const QString name); |
|
35 | 35 | QString name() const; |
|
36 | 36 | |
|
37 | 37 | void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0); |
|
38 | 38 | |
|
39 | 39 | QRectF boundingRect() const; |
|
40 | 40 | |
|
41 | 41 | public: |
|
42 | 42 | // From QGraphicsObject |
|
43 | 43 | void mousePressEvent(QGraphicsSceneMouseEvent *event); |
|
44 | 44 | |
|
45 | 45 | Q_SIGNALS: |
|
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); | |
|
46 | void clicked(QSeries* series, Qt::MouseButton button); | |
|
47 | void clicked(QBarSet* barset, Qt::MouseButton button); | |
|
48 | void clicked(QPieSlice* pieslice, Qt::MouseButton button); | |
|
52 | 49 | |
|
53 | 50 | private: |
|
54 | 51 | QRectF mBoundingRect; |
|
55 | 52 | QBrush mBrush; |
|
56 | 53 | QString mName; |
|
57 | 54 | QSeries* mSeries; |
|
58 | 55 | QBarSet* mBarset; |
|
59 | 56 | QPieSlice* mPieslice; |
|
60 | 57 | |
|
61 | 58 | LegendMarkerType mType; |
|
62 | 59 | }; |
|
63 | 60 | |
|
64 | 61 | QTCOMMERCIALCHART_END_NAMESPACE |
|
65 | 62 | #endif // LEGENDMARKER_P_H |
@@ -1,206 +1,203 | |||
|
1 | 1 | #include "qchartglobal.h" |
|
2 | 2 | #include "qlegend.h" |
|
3 | 3 | #include "qseries.h" |
|
4 | 4 | #include "legendmarker_p.h" |
|
5 | 5 | #include "qxyseries.h" |
|
6 | 6 | #include "qlineseries.h" |
|
7 | 7 | #include "qareaseries.h" |
|
8 | 8 | #include "qscatterseries.h" |
|
9 | 9 | #include "qsplineseries.h" |
|
10 | 10 | #include "qbarseries.h" |
|
11 | 11 | #include "qstackedbarseries.h" |
|
12 | 12 | #include "qpercentbarseries.h" |
|
13 | 13 | #include "qbarset.h" |
|
14 | 14 | #include "qpieseries.h" |
|
15 | 15 | #include "qpieslice.h" |
|
16 | 16 | #include <QPainter> |
|
17 | 17 | #include <QPen> |
|
18 | 18 | |
|
19 | 19 | #include <QGraphicsSceneEvent> |
|
20 | 20 | |
|
21 | 21 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
22 | 22 | |
|
23 | 23 | QLegend::QLegend(QGraphicsItem *parent) |
|
24 | 24 | : QGraphicsObject(parent) |
|
25 | 25 | ,mBoundingRect(0,0,1,1) |
|
26 | 26 | ,mBackgroundBrush(Qt::darkGray) // TODO: from theme? |
|
27 | 27 | { |
|
28 | 28 | } |
|
29 | 29 | |
|
30 | 30 | void QLegend::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) |
|
31 | 31 | { |
|
32 | 32 | painter->setBrush(mBackgroundBrush); |
|
33 | 33 | painter->drawRect(mBoundingRect); |
|
34 | 34 | |
|
35 | 35 | foreach(LegendMarker* m, mMarkers) { |
|
36 | 36 | QRectF r = m->boundingRect(); |
|
37 | 37 | painter->setBrush(m->brush()); |
|
38 | 38 | painter->drawText(r.x() + r.width()*2, r.y() + r.height(), m->name()); |
|
39 | 39 | } |
|
40 | 40 | } |
|
41 | 41 | |
|
42 | 42 | QRectF QLegend::boundingRect() const |
|
43 | 43 | { |
|
44 | 44 | return mBoundingRect; |
|
45 | 45 | } |
|
46 | 46 | |
|
47 | 47 | void QLegend::setBackgroundBrush(const QBrush& brush) |
|
48 | 48 | { |
|
49 | 49 | mBackgroundBrush = brush; |
|
50 | 50 | } |
|
51 | 51 | |
|
52 | 52 | QBrush QLegend::backgroundBrush() const |
|
53 | 53 | { |
|
54 | 54 | return mBackgroundBrush; |
|
55 | 55 | } |
|
56 | 56 | |
|
57 | 57 | void QLegend::handleSeriesAdded(QSeries* series,Domain* domain) |
|
58 | 58 | { |
|
59 | 59 | mSeriesList.append(series); |
|
60 | 60 | |
|
61 | 61 | switch (series->type()) |
|
62 | 62 | { |
|
63 | 63 | case QSeries::SeriesTypeLine: { |
|
64 | 64 | |
|
65 | 65 | QLineSeries* lineSeries = static_cast<QLineSeries*>(series); |
|
66 | 66 | createMarker(lineSeries); |
|
67 | 67 | break; |
|
68 | 68 | } |
|
69 | 69 | case QSeries::SeriesTypeArea: { |
|
70 | 70 | |
|
71 | 71 | QAreaSeries* areaSeries = static_cast<QAreaSeries*>(series); |
|
72 | 72 | createMarker(areaSeries->upperSeries()); |
|
73 | 73 | createMarker(areaSeries->lowerSeries()); |
|
74 | 74 | break; |
|
75 | 75 | } |
|
76 | 76 | |
|
77 | 77 | case QSeries::SeriesTypeBar: { |
|
78 | 78 | |
|
79 | 79 | QBarSeries* barSeries = static_cast<QBarSeries*>(series); |
|
80 | 80 | createMarkers(barSeries); |
|
81 | 81 | break; |
|
82 | 82 | } |
|
83 | 83 | |
|
84 | 84 | case QSeries::SeriesTypeStackedBar: { |
|
85 | 85 | |
|
86 | 86 | QStackedBarSeries* stackedBarSeries = static_cast<QStackedBarSeries*>(series); |
|
87 | 87 | createMarkers(stackedBarSeries); |
|
88 | 88 | break; |
|
89 | 89 | } |
|
90 | 90 | |
|
91 | 91 | case QSeries::SeriesTypePercentBar: { |
|
92 | 92 | |
|
93 | 93 | QPercentBarSeries* percentBarSeries = static_cast<QPercentBarSeries*>(series); |
|
94 | 94 | createMarkers(percentBarSeries); |
|
95 | 95 | break; |
|
96 | 96 | } |
|
97 | 97 | |
|
98 | 98 | case QSeries::SeriesTypeScatter: { |
|
99 | 99 | |
|
100 | 100 | QScatterSeries *scatterSeries = static_cast<QScatterSeries *>(series); |
|
101 | 101 | createMarker(scatterSeries); |
|
102 | 102 | break; |
|
103 | 103 | } |
|
104 | 104 | |
|
105 | 105 | case QSeries::SeriesTypePie: { |
|
106 | 106 | |
|
107 | 107 | QPieSeries *pieSeries = static_cast<QPieSeries *>(series); |
|
108 | 108 | createMarkers(pieSeries); |
|
109 | 109 | break; |
|
110 | 110 | } |
|
111 | 111 | |
|
112 | 112 | case QSeries::SeriesTypeSpline: { |
|
113 | 113 | |
|
114 | 114 | QSplineSeries* splineSeries = static_cast<QSplineSeries*>(series); |
|
115 | 115 | break; |
|
116 | 116 | } |
|
117 | 117 | default: { |
|
118 | 118 | qDebug()<< "QLegend::handleSeriesAdded" << series->type() << "not implemented."; |
|
119 | 119 | break; |
|
120 | 120 | } |
|
121 | 121 | } |
|
122 | 122 | |
|
123 | 123 | layoutChanged(); |
|
124 | 124 | } |
|
125 | 125 | |
|
126 | 126 | void QLegend::handleSeriesRemoved(QSeries* series) |
|
127 | 127 | { |
|
128 | 128 | // TODO: delete markers, disconnect. |
|
129 | 129 | mSeriesList.removeOne(series); |
|
130 | 130 | layoutChanged(); |
|
131 | 131 | } |
|
132 | 132 | |
|
133 | 133 | void QLegend::handleGeometryChanged(const QRectF& size) |
|
134 | 134 | { |
|
135 | 135 | mBoundingRect = size; |
|
136 | 136 | layoutChanged(); |
|
137 | 137 | } |
|
138 | 138 | |
|
139 | 139 | void QLegend::createMarker(QXYSeries* series) |
|
140 | 140 | { |
|
141 | 141 | LegendMarker* marker = new LegendMarker(series,this); |
|
142 | 142 | marker->setName(series->name()); |
|
143 | 143 | marker->setBrush(series->brush()); |
|
144 |
connect(marker,SIGNAL(clicked(QSeries*)),this,SIGNAL( |
|
|
145 | connect(marker,SIGNAL(rightClicked(QSeries*)),this,SIGNAL(markerRightClicked(QSeries*))); | |
|
144 | connect(marker,SIGNAL(clicked(QSeries*,Qt::MouseButton)),this,SIGNAL(clicked(QSeries*,Qt::MouseButton))); | |
|
146 | 145 | mMarkers.append(marker); |
|
147 | 146 | childItems().append(marker); |
|
148 | 147 | } |
|
149 | 148 | |
|
150 | 149 | void QLegend::createMarkers(QBarSeries *series) |
|
151 | 150 | { |
|
152 | 151 | foreach(QBarSet* s, series->barSets()) { |
|
153 | 152 | LegendMarker* marker = new LegendMarker(series,this); |
|
154 | 153 | marker->setName(s->name()); |
|
155 | 154 | marker->setBrush(s->brush()); |
|
156 |
connect(marker,SIGNAL(clicked(QBarSet*)),this,SIGNAL( |
|
|
157 | connect(marker,SIGNAL(rightClicked(QBarSet*)),this,SIGNAL(markerRightClicked(QBarSet*))); | |
|
155 | connect(marker,SIGNAL(clicked(QBarSet*,Qt::MouseButton)),this,SIGNAL(clicked(QBarSet*,Qt::MouseButton))); | |
|
158 | 156 | mMarkers.append(marker); |
|
159 | 157 | childItems().append(marker); |
|
160 | 158 | } |
|
161 | 159 | } |
|
162 | 160 | |
|
163 | 161 | void QLegend::createMarkers(QPieSeries *series) |
|
164 | 162 | { |
|
165 | 163 | foreach(QPieSlice* s, series->slices()) { |
|
166 | 164 | LegendMarker* marker = new LegendMarker(series,this); |
|
167 | 165 | marker->setName(s->label()); |
|
168 | 166 | marker->setBrush(s->sliceBrush()); |
|
169 |
connect(marker,SIGNAL(clicked(QPieSlice*)),this,SIGNAL( |
|
|
170 | connect(marker,SIGNAL(rightClicked(QPieSlice*)),this,SIGNAL(markerRightClicked(QPieSlice*))); | |
|
167 | connect(marker,SIGNAL(clicked(QPieSlice*,Qt::MouseButton)),this,SIGNAL(clicked(QPieSlice*,Qt::MouseButton))); | |
|
171 | 168 | mMarkers.append(marker); |
|
172 | 169 | childItems().append(marker); |
|
173 | 170 | } |
|
174 | 171 | } |
|
175 | 172 | |
|
176 | 173 | void QLegend::layoutChanged() |
|
177 | 174 | { |
|
178 | 175 | // Calculate layout for markers and text |
|
179 | 176 | if (mMarkers.count() <= 0) { |
|
180 | 177 | // Nothing to do |
|
181 | 178 | return; |
|
182 | 179 | } |
|
183 | 180 | |
|
184 | 181 | // TODO: marker defined by series. |
|
185 | 182 | QSizeF markerSize(10,10); |
|
186 | 183 | |
|
187 | 184 | // TODO: better layout, this is just concept. |
|
188 | 185 | // Leave some space around markers like this: | x x x x | |
|
189 | 186 | qreal steps = mMarkers.count(); |
|
190 | 187 | |
|
191 | 188 | qreal xStep = mBoundingRect.width() / steps; |
|
192 | 189 | qreal yStep = mBoundingRect.height() / steps; |
|
193 | 190 | qreal x = mBoundingRect.x() + 5; |
|
194 | 191 | qreal y = mBoundingRect.y() + (mBoundingRect.height() - markerSize.height())/2; |
|
195 | 192 | foreach (LegendMarker* m, mMarkers) { |
|
196 | 193 | qDebug() << "marker x:" << x; |
|
197 | 194 | qDebug() << "marker y:" << y; |
|
198 | 195 | m->setBoundingRect(QRectF(x,y,markerSize.width(),markerSize.height())); |
|
199 | 196 | x += xStep; |
|
200 | 197 | } |
|
201 | 198 | } |
|
202 | 199 | |
|
203 | 200 | |
|
204 | 201 | |
|
205 | 202 | #include "moc_qlegend.cpp" |
|
206 | 203 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,63 +1,60 | |||
|
1 | 1 | #ifndef QLEGEND_H |
|
2 | 2 | #define QLEGEND_H |
|
3 | 3 | |
|
4 | 4 | #include "qchartglobal.h" |
|
5 | 5 | #include "qseries.h" |
|
6 | 6 | #include <QGraphicsObject> |
|
7 | 7 | |
|
8 | 8 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
9 | 9 | |
|
10 | 10 | class Domain; |
|
11 | 11 | class LegendMarker; |
|
12 | 12 | class QPieSlice; |
|
13 | 13 | class QXYSeries; |
|
14 | 14 | class QBarSet; |
|
15 | 15 | class QBarSeries; |
|
16 | 16 | class QPieSeries; |
|
17 | 17 | |
|
18 | 18 | class QTCOMMERCIALCHART_EXPORT QLegend : public QGraphicsObject |
|
19 | 19 | { |
|
20 | 20 | Q_OBJECT |
|
21 | 21 | public: |
|
22 | 22 | |
|
23 | 23 | explicit QLegend(QGraphicsItem *parent = 0); |
|
24 | 24 | |
|
25 | 25 | void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0); |
|
26 | 26 | QRectF boundingRect() const; |
|
27 | 27 | |
|
28 | 28 | void setBackgroundBrush(const QBrush& brush); |
|
29 | 29 | QBrush backgroundBrush() const; |
|
30 | 30 | |
|
31 | 31 | signals: |
|
32 | 32 | // for interactions. |
|
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); | |
|
33 | void clicked(QSeries* series, Qt::MouseButton button); | |
|
34 | void clicked(QBarSet* barset, Qt::MouseButton button); | |
|
35 | void clicked(QPieSlice* slice, Qt::MouseButton button); | |
|
39 | 36 | |
|
40 | 37 | public slots: |
|
41 | 38 | void handleSeriesAdded(QSeries* series,Domain* domain); |
|
42 | 39 | void handleSeriesRemoved(QSeries* series); |
|
43 | 40 | void handleGeometryChanged(const QRectF& size); |
|
44 | 41 | |
|
45 | 42 | private: |
|
46 | 43 | // PIMPL ---> |
|
47 | 44 | void createMarker(QXYSeries* series); |
|
48 | 45 | void createMarkers(QBarSeries* series); |
|
49 | 46 | void createMarkers(QPieSeries* series); |
|
50 | 47 | void layoutChanged(); |
|
51 | 48 | // <--- PIMPL |
|
52 | 49 | |
|
53 | 50 | |
|
54 | 51 | QRectF mBoundingRect; |
|
55 | 52 | QList<QSeries*> mSeriesList; |
|
56 | 53 | QList<LegendMarker*> mMarkers; |
|
57 | 54 | |
|
58 | 55 | QBrush mBackgroundBrush; |
|
59 | 56 | }; |
|
60 | 57 | |
|
61 | 58 | QTCOMMERCIALCHART_END_NAMESPACE |
|
62 | 59 | |
|
63 | 60 | #endif // QLEGEND_H |
General Comments 0
You need to be logged in to leave comments.
Login now