##// END OF EJS Templates
Separated legend marker to private header. Added signals for left and right mouse click
sauimone -
r547:2c194d26bbea
parent child
Show More
@@ -0,0 +1,63
1 #include "qchartglobal.h"
2 #include "legendmarker_p.h"
3 #include <QPainter>
4 #include <QGraphicsSceneEvent>
5
6 QTCOMMERCIALCHART_BEGIN_NAMESPACE
7
8 LegendMarker::LegendMarker(QSeries* series, QGraphicsItem *parent)
9 : QGraphicsObject(parent)
10 ,mSeries(series)
11 ,mBoundingRect(0,0,1,1)
12 {
13 setAcceptedMouseButtons(Qt::LeftButton|Qt::RightButton);
14 }
15
16 void LegendMarker::setBoundingRect(const QRectF rect)
17 {
18 mBoundingRect = rect;
19 }
20
21 void LegendMarker::setBrush(const QBrush brush)
22 {
23 mBrush = brush;
24 }
25
26 void LegendMarker::setName(const QString name)
27 {
28 mName = name;
29 }
30
31 QString LegendMarker::name() const
32 {
33 return mName;
34 }
35
36 QColor LegendMarker::color() const
37 {
38 return mBrush.color();
39 }
40
41 void LegendMarker::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
42 {
43 painter->setBrush(mBrush);
44 painter->drawRect(mBoundingRect);
45 }
46
47 QRectF LegendMarker::boundingRect() const
48 {
49 return mBoundingRect;
50 }
51
52 void LegendMarker::mousePressEvent(QGraphicsSceneMouseEvent *event)
53 {
54 if (event->button() == Qt::LeftButton) {
55 emit clicked(mSeries, mName);
56 } else if (event->button() == Qt::RightButton) {
57 emit rightClicked(mSeries, mName);
58 }
59 }
60
61 #include "moc_legendmarker_p.cpp"
62
63 QTCOMMERCIALCHART_END_NAMESPACE
@@ -0,0 +1,44
1 #ifndef LEGENDMARKER_P_H
2 #define LEGENDMARKER_P_H
3
4 #include "qchartglobal.h"
5 #include <QGraphicsObject>
6 #include <QBrush>
7
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9
10 class QSeries;
11
12
13 class LegendMarker : public QGraphicsObject
14 {
15 Q_OBJECT
16 public:
17 LegendMarker(QSeries* series, QGraphicsItem *parent = 0);
18 void setBoundingRect(const QRectF rect);
19 void setBrush(const QBrush brush);
20 void setName(const QString name);
21 QString name() const;
22 QColor color() const;
23
24 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
25
26 QRectF boundingRect() const;
27
28 public:
29 // From QGraphicsObject
30 void mousePressEvent(QGraphicsSceneMouseEvent *event);
31
32 Q_SIGNALS:
33 void clicked(QSeries* series, QString name);
34 void rightClicked(QSeries* series, QString name);
35
36 private:
37 QRectF mBoundingRect;
38 QBrush mBrush;
39 QString mName;
40 QSeries* mSeries;
41 };
42
43 QTCOMMERCIALCHART_END_NAMESPACE
44 #endif // LEGENDMARKER_P_H
@@ -1,390 +1,390
1 1 #include "qchart.h"
2 2 #include "qchartaxis.h"
3 3 #include "chartpresenter_p.h"
4 4 #include "chartdataset_p.h"
5 5 #include "charttheme_p.h"
6 6 #include "chartanimator_p.h"
7 7 //series
8 8 #include "qbarseries.h"
9 9 #include "qstackedbarseries.h"
10 10 #include "qpercentbarseries.h"
11 11 #include "qlineseries.h"
12 12 #include "qareaseries.h"
13 13 #include "qpieseries.h"
14 14 #include "qscatterseries.h"
15 15 #include "qsplineseries.h"
16 16 //items
17 17 #include "axisitem_p.h"
18 18 #include "areachartitem_p.h"
19 19 #include "barpresenter_p.h"
20 20 #include "stackedbarpresenter_p.h"
21 21 #include "percentbarpresenter_p.h"
22 22 #include "linechartitem_p.h"
23 23 #include "piepresenter_p.h"
24 24 #include "scatterchartitem_p.h"
25 25 #include "splinechartitem_p.h"
26 26
27 27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28 28
29 29 ChartPresenter::ChartPresenter(QChart* chart,ChartDataSet* dataset):QObject(chart),
30 30 m_chart(chart),
31 31 m_animator(0),
32 32 m_dataset(dataset),
33 33 m_chartTheme(0),
34 34 m_zoomIndex(0),
35 35 m_marginSize(0),
36 36 m_rect(QRectF(QPoint(0,0),m_chart->size())),
37 37 m_options(QChart::NoAnimation)
38 38 {
39 39 createConnections();
40 40 setChartTheme(QChart::ChartThemeDefault);
41 41 }
42 42
43 43 ChartPresenter::~ChartPresenter()
44 44 {
45 45 delete m_chartTheme;
46 46 }
47 47
48 48 void ChartPresenter::createConnections()
49 49 {
50 50 QObject::connect(m_chart,SIGNAL(geometryChanged()),this,SLOT(handleGeometryChanged()));
51 51 QObject::connect(m_dataset,SIGNAL(seriesAdded(QSeries*,Domain*)),this,SLOT(handleSeriesAdded(QSeries*,Domain*)));
52 52 QObject::connect(m_dataset,SIGNAL(seriesRemoved(QSeries*)),this,SLOT(handleSeriesRemoved(QSeries*)));
53 53 QObject::connect(m_dataset,SIGNAL(axisAdded(QChartAxis*,Domain*)),this,SLOT(handleAxisAdded(QChartAxis*,Domain*)));
54 54 QObject::connect(m_dataset,SIGNAL(axisRemoved(QChartAxis*)),this,SLOT(handleAxisRemoved(QChartAxis*)));
55 55 }
56 56
57 57
58 58 QRectF ChartPresenter::geometry() const
59 59 {
60 60 return m_rect;
61 61 }
62 62
63 63 void ChartPresenter::handleGeometryChanged()
64 64 {
65 65 QRectF rect(QPoint(0,0),m_chart->size());
66 66 rect.adjust(m_marginSize,m_marginSize, -m_marginSize, -m_marginSize);
67 67
68 68 //rewrite zoom stack
69 69 for(int i=0;i<m_zoomStack.count();i++){
70 70 QRectF r = m_zoomStack[i];
71 71 qreal w = rect.width()/m_rect.width();
72 72 qreal h = rect.height()/m_rect.height();
73 73 QPointF tl = r.topLeft();
74 74 tl.setX(tl.x()*w);
75 75 tl.setY(tl.y()*h);
76 76 QPointF br = r.bottomRight();
77 77 br.setX(br.x()*w);
78 78 br.setY(br.y()*h);
79 79 r.setTopLeft(tl);
80 80 r.setBottomRight(br);
81 81 m_zoomStack[i]=r;
82 82 }
83 83
84 84 m_rect = rect;
85 85 Q_ASSERT(m_rect.isValid());
86 86 emit geometryChanged(m_rect);
87 87 }
88 88
89 89 int ChartPresenter::margin() const
90 90 {
91 91 return m_marginSize;
92 92 }
93 93
94 94 void ChartPresenter::setMargin(int margin)
95 95 {
96 96 m_marginSize = margin;
97 97 }
98 98
99 99 void ChartPresenter::handleAxisAdded(QChartAxis* axis,Domain* domain)
100 100 {
101 101 AxisItem* item = new AxisItem(axis,this,axis==m_dataset->axisX()?AxisItem::X_AXIS : AxisItem::Y_AXIS,m_chart);
102 102
103 103 if(m_options.testFlag(QChart::GridAxisAnimations)){
104 104 m_animator->addAnimation(item);
105 105 }
106 106
107 107 if(axis==m_dataset->axisX()){
108 108 QObject::connect(domain,SIGNAL(rangeXChanged(qreal,qreal,int)),item,SLOT(handleRangeChanged(qreal,qreal,int)));
109 109 //initialize
110 110 item->handleRangeChanged(domain->minX(),domain->maxX(),domain->tickXCount());
111 111 }
112 112 else{
113 113 QObject::connect(domain,SIGNAL(rangeYChanged(qreal,qreal,int)),item,SLOT(handleRangeChanged(qreal,qreal,int)));
114 114 //initialize
115 115 item->handleRangeChanged(domain->minY(),domain->maxY(),domain->tickYCount());
116 116 }
117 117
118 118 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
119 119 //initialize
120 120 item->handleGeometryChanged(m_rect);
121 121 m_chartTheme->decorate(axis,item);
122 122 m_axisItems.insert(axis,item);
123 123
124 124 }
125 125
126 126 void ChartPresenter::handleAxisRemoved(QChartAxis* axis)
127 127 {
128 128 AxisItem* item = m_axisItems.take(axis);
129 129 Q_ASSERT(item);
130 130 if(m_animator) m_animator->removeAnimation(item);
131 131 delete item;
132 132 }
133 133
134 134
135 135 void ChartPresenter::handleSeriesAdded(QSeries* series,Domain* domain)
136 136 {
137 137 ChartItem *item = 0 ;
138 138
139 139 switch(series->type())
140 140 {
141 141 case QSeries::SeriesTypeLine: {
142 142
143 143 QLineSeries* lineSeries = static_cast<QLineSeries*>(series);
144 144 LineChartItem* line = new LineChartItem(lineSeries,m_chart);
145 145 if(m_options.testFlag(QChart::SeriesAnimations)) {
146 146 m_animator->addAnimation(line);
147 147 }
148 148 m_chartTheme->decorate(line, lineSeries, m_dataset->seriesIndex(series));
149 149 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),line,SLOT(handleGeometryChanged(const QRectF&)));
150 150 QObject::connect(domain,SIGNAL(domainChanged(qreal,qreal,qreal,qreal)),line,SLOT(handleDomainChanged(qreal,qreal,qreal,qreal)));
151 151 item = line;
152 152 break;
153 153 }
154 154
155 155 case QSeries::SeriesTypeArea: {
156 156
157 157 QAreaSeries* areaSeries = static_cast<QAreaSeries*>(series);
158 158 AreaChartItem* area = new AreaChartItem(areaSeries,m_chart);
159 159 if(m_options.testFlag(QChart::SeriesAnimations)) {
160 160 // m_animator->addAnimation(area);
161 161 }
162 162 m_chartTheme->decorate(area, areaSeries, m_dataset->seriesIndex(series));
163 163 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),area,SLOT(handleGeometryChanged(const QRectF&)));
164 164 QObject::connect(domain,SIGNAL(domainChanged(qreal,qreal,qreal,qreal)),area,SLOT(handleDomainChanged(qreal,qreal,qreal,qreal)));
165 165 item=area;
166 166 break;
167 167 }
168 168
169 169 case QSeries::SeriesTypeBar: {
170 170 QBarSeries* barSeries = static_cast<QBarSeries*>(series);
171 171 BarPresenter* bar = new BarPresenter(barSeries,m_chart);
172 172 if(m_options.testFlag(QChart::SeriesAnimations)) {
173 173 // m_animator->addAnimation(bar);
174 174 }
175 175 m_chartTheme->decorate(bar, barSeries, m_dataset->seriesIndex(barSeries));
176 176 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),bar,SLOT(handleGeometryChanged(const QRectF&)));
177 177 QObject::connect(domain,SIGNAL(domainChanged(qreal,qreal,qreal,qreal)),bar,SLOT(handleDomainChanged(qreal,qreal,qreal,qreal)));
178 178 item=bar;
179 179 break;
180 180 }
181 181
182 182 case QSeries::SeriesTypeStackedBar: {
183 183 QStackedBarSeries* stackedBarSeries = static_cast<QStackedBarSeries*>(series);
184 184 StackedBarPresenter* bar = new StackedBarPresenter(stackedBarSeries,m_chart);
185 185 if(m_options.testFlag(QChart::SeriesAnimations)) {
186 186 // m_animator->addAnimation(bar);
187 187 }
188 188 m_chartTheme->decorate(bar, stackedBarSeries, m_dataset->seriesIndex(stackedBarSeries));
189 189 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),bar,SLOT(handleGeometryChanged(const QRectF&)));
190 190 QObject::connect(domain,SIGNAL(domainChanged(qreal,qreal,qreal,qreal)),bar,SLOT(handleDomainChanged(qreal,qreal,qreal,qreal)));
191 191 item=bar;
192 192 break;
193 193 }
194 194
195 195 case QSeries::SeriesTypePercentBar: {
196 196 QPercentBarSeries* percentBarSeries = static_cast<QPercentBarSeries*>(series);
197 197 PercentBarPresenter* bar = new PercentBarPresenter(percentBarSeries,m_chart);
198 198 if(m_options.testFlag(QChart::SeriesAnimations)) {
199 199 // m_animator->addAnimation(bar);
200 200 }
201 201 m_chartTheme->decorate(bar, percentBarSeries, m_dataset->seriesIndex(percentBarSeries));
202 202 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),bar,SLOT(handleGeometryChanged(const QRectF&)));
203 203 QObject::connect(domain,SIGNAL(domainChanged(qreal,qreal,qreal,qreal)),bar,SLOT(handleDomainChanged(qreal,qreal,qreal,qreal)));
204 204 item=bar;
205 205 break;
206 206 }
207 207
208 208 case QSeries::SeriesTypeScatter: {
209 209 QScatterSeries *scatterSeries = static_cast<QScatterSeries *>(series);
210 210 ScatterChartItem *scatter = new ScatterChartItem(scatterSeries, m_chart);
211 211 if(m_options.testFlag(QChart::SeriesAnimations)) {
212 212 m_animator->addAnimation(scatter);
213 213 }
214 214 m_chartTheme->decorate(scatter, scatterSeries, m_dataset->seriesIndex(series));
215 215 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),scatter,SLOT(handleGeometryChanged(const QRectF&)));
216 216 QObject::connect(domain,SIGNAL(domainChanged(qreal,qreal,qreal,qreal)),scatter,SLOT(handleDomainChanged(qreal,qreal,qreal,qreal)));
217 217 item = scatter;
218 218 break;
219 219 }
220 220
221 221 case QSeries::SeriesTypePie: {
222 222 QPieSeries *pieSeries = static_cast<QPieSeries *>(series);
223 223 PiePresenter* pie = new PiePresenter(m_chart, pieSeries);
224 224 if(m_options.testFlag(QChart::SeriesAnimations)) {
225 225 // m_animator->addAnimation(pie);
226 226 }
227 227 m_chartTheme->decorate(pie, pieSeries, m_dataset->seriesIndex(series));
228 228 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),pie,SLOT(handleGeometryChanged(const QRectF&)));
229 229 QObject::connect(domain,SIGNAL(domainChanged(qreal,qreal,qreal,qreal)),pie,SLOT(handleDomainChanged(qreal,qreal,qreal,qreal)));
230 230 // Hide all from background when there is only piechart
231 231 // TODO: refactor this ugly code... should be one setting for this
232 232 if (m_chartItems.count() == 0) {
233 233 m_chart->axisX()->hide();
234 234 m_chart->axisY()->hide();
235 235 m_chart->setChartBackgroundBrush(Qt::transparent);
236 236 }
237 237 item=pie;
238 238 break;
239 239 }
240 240
241 241 case QSeries::SeriesTypeSpline: {
242 242 QSplineSeries* splineSeries = static_cast<QSplineSeries*>(series);
243 243 SplineChartItem* spline = new SplineChartItem(splineSeries, m_chart);
244 244 if(m_options.testFlag(QChart::SeriesAnimations)) {
245 245 m_animator->addAnimation(spline);
246 246 }
247 247 m_chartTheme->decorate(spline, splineSeries, m_dataset->seriesIndex(series));
248 248 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),spline,SLOT(handleGeometryChanged(const QRectF&)));
249 249 QObject::connect(domain,SIGNAL(domainChanged(qreal,qreal,qreal,qreal)),spline,SLOT(handleDomainChanged(qreal,qreal,qreal,qreal)));
250 250 item=spline;
251 251 break;
252 252 }
253 253 default: {
254 254 qDebug()<< "Series type" << series->type() << "not implemented.";
255 255 break;
256 256 }
257 257 }
258 258
259 259 //initialize
260 260 item->handleDomainChanged(domain->minX(),domain->maxX(),domain->minY(),domain->maxY());
261 261 if(m_rect.isValid()) item->handleGeometryChanged(m_rect);
262 262 m_chartItems.insert(series,item);
263 263 zoomReset();
264 264 }
265 265
266 266 void ChartPresenter::handleSeriesRemoved(QSeries* series)
267 267 {
268 268 ChartItem* item = m_chartItems.take(series);
269 269 Q_ASSERT(item);
270 270 if(m_animator) m_animator->removeAnimation(item);
271 271 delete item;
272 272 }
273 273
274 274 void ChartPresenter::setChartTheme(QChart::ChartTheme theme)
275 275 {
276 276 if(m_chartTheme && m_chartTheme->id() == theme) return;
277 277 delete m_chartTheme;
278 278 m_chartTheme = ChartTheme::createTheme(theme);
279 279 m_chartTheme->decorate(m_chart);
280 280 m_chartTheme->decorate(m_chart->legend());
281 281 resetAllElements();
282 282 }
283 283
284 284 QChart::ChartTheme ChartPresenter::chartTheme()
285 285 {
286 286 return m_chartTheme->id();
287 287 }
288 288
289 289 void ChartPresenter::setAnimationOptions(QChart::AnimationOptions options)
290 290 {
291 291 if(m_options!=options) {
292 292
293 293 m_options=options;
294 294
295 295 if(m_options!=QChart::NoAnimation && !m_animator) {
296 296 m_animator= new ChartAnimator(this);
297 297
298 298 }
299 299 resetAllElements();
300 300 }
301 301
302 302 }
303 303
304 304 void ChartPresenter::resetAllElements()
305 305 {
306 306 QList<QChartAxis*> axisList = m_axisItems.uniqueKeys();
307 307 QList<QSeries*> seriesList = m_chartItems.uniqueKeys();
308 308
309 309 foreach(QChartAxis* axis, axisList) {
310 310 handleAxisRemoved(axis);
311 311 handleAxisAdded(axis,m_dataset->domain(axis));
312 312 }
313 313 foreach(QSeries* series, seriesList) {
314 314 handleSeriesRemoved(series);
315 315 handleSeriesAdded(series,m_dataset->domain(series));
316 316 }
317 317 }
318 318
319 319 void ChartPresenter::zoomIn()
320 320 {
321 321 QRectF rect = geometry();
322 322 rect.setWidth(rect.width()/2);
323 323 rect.setHeight(rect.height()/2);
324 324 rect.moveCenter(geometry().center());
325 325 zoomIn(rect);
326 326 }
327 327
328 328 void ChartPresenter::zoomIn(const QRectF& rect)
329 329 {
330 330 QRectF r = rect.normalized();
331 331 r.translate(-m_marginSize, -m_marginSize);
332 332 if(m_animator) {
333 333
334 334 QPointF point(r.center().x()/geometry().width(),r.center().y()/geometry().height());
335 335 m_animator->setState(ChartAnimator::ZoomInState,point);
336 336 }
337 337 m_dataset->zoomInDomain(r,geometry().size());
338 338 m_zoomStack<<r;
339 339 m_zoomIndex++;
340 340 if(m_animator) {
341 341 m_animator->setState(ChartAnimator::ShowState);
342 342 }
343 343 }
344 344
345 345 void ChartPresenter::zoomOut()
346 346 {
347 347 if(m_zoomIndex==0) return;
348 348 if(m_animator)
349 349 {
350 350 m_animator->setState(ChartAnimator::ZoomOutState);
351 351 }
352 352 m_dataset->zoomOutDomain(m_zoomStack[m_zoomIndex-1],geometry().size());
353 353 m_zoomIndex--;
354 354 m_zoomStack.resize(m_zoomIndex);
355 355 if(m_animator){
356 356 m_animator->setState(ChartAnimator::ShowState);
357 357 }
358 358 }
359 359
360 360 void ChartPresenter::zoomReset()
361 361 {
362 362 m_zoomIndex=0;
363 363 m_zoomStack.resize(m_zoomIndex);
364 364 }
365 365
366 366 void ChartPresenter::scroll(int dx,int dy)
367 367 {
368 368 if(m_animator){
369 369 if(dx<0) m_animator->setState(ChartAnimator::ScrollLeftState,QPointF());
370 370 if(dx>0) m_animator->setState(ChartAnimator::ScrollRightState,QPointF());
371 371 if(dy<0) m_animator->setState(ChartAnimator::ScrollUpState,QPointF());
372 372 if(dy>0) m_animator->setState(ChartAnimator::ScrollDownState,QPointF());
373 373 }
374 374
375 375 m_dataset->scrollDomain(dx,dy,geometry().size());
376 376
377 if(m_animator){
377 if(m_animator){
378 378 m_animator->setState(ChartAnimator::ShowState);
379 379 }
380 380 }
381 381
382 382 QChart::AnimationOptions ChartPresenter::animationOptions() const
383 383 {
384 384 return m_options;
385 385 }
386 386
387 387
388 388 #include "moc_chartpresenter_p.cpp"
389 389
390 390 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,140 +1,119
1 1 #include "qchartglobal.h"
2 2 #include "qlegend.h"
3 3 #include "qseries.h"
4 #include "legendmarker_p.h"
4 5 #include <QPainter>
5 6 #include <QPen>
6 7
7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8
9 // TODO: this to legendmarker_p.h header
10 class LegendMarker : public QGraphicsItem
11 {
12 public:
13 LegendMarker(QGraphicsItem *parent = 0) : QGraphicsItem(parent)
14 ,mBoundingRect(0,0,1,1)
15 {}
16
17 void setBoundingRect(const QRectF rect) { mBoundingRect = rect; }
18 void setBrush(const QBrush brush) { mBrush = brush; }
19 void setName(const QString name) { mName = name; }
20 QString name() const { return mName; }
21 QColor color() const { return mBrush.color(); }
22
23 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0)
24 {
25 painter->setBrush(mBrush);
26 painter->drawRect(mBoundingRect);
27 }
28
29 QRectF boundingRect() const { return mBoundingRect; }
8 #include <QGraphicsSceneEvent>
30 9
31 private:
32 QRectF mBoundingRect;
33 QBrush mBrush;
34 QString mName;
35 };
10 QTCOMMERCIALCHART_BEGIN_NAMESPACE
36 11
37 12 QLegend::QLegend(QGraphicsItem *parent)
38 13 : QGraphicsObject(parent)
39 14 ,mBoundingRect(0,0,1,1)
40 15 ,mBackgroundBrush(Qt::darkGray) // TODO: from theme?
41 16 {
42 17 }
43 18
44 19 void QLegend::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
45 20 {
46 21 painter->setBrush(mBackgroundBrush);
47 22 painter->drawRect(mBoundingRect);
48 23
49 24 foreach(LegendMarker* m, mMarkers) {
50 25 QRectF r = m->boundingRect();
51 26 painter->setPen(m->color());
52 painter->drawText(r.x() + 20, r.y() + r.height(), m->name());
27 painter->drawText(r.x() + r.width()*2, r.y() + r.height(), m->name());
53 28 }
54 29 }
55 30
56 31 QRectF QLegend::boundingRect() const
57 32 {
58 33 return mBoundingRect;
59 34 }
60 35
61 36 void QLegend::setBackgroundBrush(const QBrush& brush)
62 37 {
63 38 mBackgroundBrush = brush;
64 39 }
65 40
66 41 QBrush QLegend::backgroundBrush() const
67 42 {
68 43 return mBackgroundBrush;
69 44 }
70 45
71 46 void QLegend::handleSeriesAdded(QSeries* series,Domain* domain)
72 47 {
73 48 mSeriesList.append(series);
74 49 dataChanged();
75 50 layoutChanged();
76 51 }
77 52
78 53 void QLegend::handleSeriesRemoved(QSeries* series)
79 54 {
80 55 mSeriesList.removeOne(series);
81 56 dataChanged();
82 57 layoutChanged();
83 58 }
84 59
85 60 void QLegend::handleGeometryChanged(const QRectF& size)
86 61 {
87 62 mBoundingRect = size;
88 63 layoutChanged();
89 64 }
90 65
91 66 void QLegend::dataChanged()
92 67 {
93 foreach (QGraphicsItem* i, childItems()) {
94 delete i;
68 foreach (LegendMarker* marker, mMarkers) {
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;
95 72 }
96 73
97 74 mMarkers.clear();
98 75
99 76 foreach (QSeries* s, mSeriesList) {
100 77 for (int i=0; i<s->legendEntries().count(); i++) {
101 LegendMarker *marker = new LegendMarker(this);
78 LegendMarker *marker = new LegendMarker(s, this);
102 79 marker->setBrush(s->legendEntries().at(i).mBrush);
103 80 marker->setName(s->legendEntries().at(i).mName);
104 81 mMarkers.append(marker);
105 // childItems().append(marker);
82 childItems().append(marker);
83 connect(marker,SIGNAL(clicked(QSeries*,QString)),this,SIGNAL(clicked(QSeries*,QString)));
84 connect(marker,SIGNAL(rightClicked(QSeries*,QString)),this,SIGNAL(rightClicked(QSeries*,QString)));
106 85 }
107 86 }
108 87 }
109 88
110 89 void QLegend::layoutChanged()
111 90 {
112 91 // Calculate layout for markers and text
113 92 if (mMarkers.count() <= 0) {
114 93 // Nothing to do
115 94 return;
116 95 }
117 96
118 97 // TODO: marker defined by series.
119 98 QSizeF markerSize(10,10);
120 99
121 100 // TODO: better layout, this is just concept.
122 101 // Leave some space around markers like this: | x x x x |
123 102 qreal steps = mMarkers.count();
124 103
125 104 qreal xStep = mBoundingRect.width() / steps;
126 105 qreal yStep = mBoundingRect.height() / steps;
127 106 qreal x = mBoundingRect.x() + 5;
128 107 qreal y = mBoundingRect.y() + (mBoundingRect.height() - markerSize.height())/2;
129 108 foreach (LegendMarker* m, mMarkers) {
130 109 qDebug() << "marker x:" << x;
131 110 qDebug() << "marker y:" << y;
132 111 m->setBoundingRect(QRectF(x,y,markerSize.width(),markerSize.height()));
133 112 x += xStep;
134 113 }
135 114 }
136 115
137 116
138 117
139 118 #include "moc_qlegend.cpp"
140 119 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,46 +1,49
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
13 13 class QTCOMMERCIALCHART_EXPORT QLegend : public QGraphicsObject
14 14 {
15 15 Q_OBJECT
16 16 public:
17 17
18 18 explicit QLegend(QGraphicsItem *parent = 0);
19 19
20 20 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
21 21 QRectF boundingRect() const;
22 22
23 23 void setBackgroundBrush(const QBrush& brush);
24 24 QBrush backgroundBrush() const;
25 25
26 26 signals:
27
27 // for interactions.
28 void clicked(QSeries* series, QString name);
29 void rightClicked(QSeries* series, QString name);
30
28 31 public slots:
29 32 void handleSeriesAdded(QSeries* series,Domain* domain);
30 33 void handleSeriesRemoved(QSeries* series);
31 34 void handleGeometryChanged(const QRectF& size);
32 35
33 36 private:
34 37 void dataChanged();
35 38 void layoutChanged();
36 39
37 40 QRectF mBoundingRect;
38 41 QList<QSeries*> mSeriesList;
39 42 QList<LegendMarker*> mMarkers;
40 43
41 44 QBrush mBackgroundBrush;
42 45 };
43 46
44 47 QTCOMMERCIALCHART_END_NAMESPACE
45 48
46 49 #endif // QLEGEND_H
@@ -1,96 +1,98
1 1 !include( ../common.pri ):error( Couldn't find the common.pri file! )
2 2 TARGET = QtCommercialChart
3 3 DESTDIR = $$CHART_BUILD_LIB_DIR
4 4 TEMPLATE = lib
5 5 QT += core \
6 6 gui
7 7 CONFIG += debug_and_release
8 8 CONFIG(debug, debug|release):TARGET = QtCommercialChartd
9 9 SOURCES += \
10 10 chartdataset.cpp \
11 11 chartpresenter.cpp \
12 12 charttheme.cpp \
13 13 domain.cpp \
14 14 qchart.cpp \
15 15 qchartview.cpp \
16 16 qseries.cpp \
17 qlegend.cpp
17 qlegend.cpp \
18 legendmarker.cpp
18 19 PRIVATE_HEADERS += \
19 20 chartdataset_p.h \
20 21 chartitem_p.h \
21 22 chartpresenter_p.h \
22 23 charttheme_p.h \
23 domain_p.h
24 PUBLIC_HEADERS += \
24 domain_p.h \
25 legendmarker_p.h
26 PUBLIC_HEADERS += \
25 27 qchart.h \
26 28 qchartglobal.h \
27 29 qseries.h \
28 30 qchartview.h \
29 31 qlegend.h
30 32
31 33 include(animations/animations.pri)
32 34 include(axis/axis.pri)
33 35 include(xychart/xychart.pri)
34 36 include(linechart/linechart.pri)
35 37 include(areachart/areachart.pri)
36 38 include(barchart/barchart.pri)
37 39 include(piechart/piechart.pri)
38 40 include(scatterseries/scatter.pri)
39 41 include(splinechart/splinechart.pri)
40 42
41 43 THEMES += themes/chartthemedefault_p.h \
42 44 themes/chartthemeicy_p.h \
43 45 themes/chartthemegrayscale_p.h \
44 46 themes/chartthemescientific_p.h \
45 47 themes/chartthemevanilla_p.h
46 48 HEADERS += $$PUBLIC_HEADERS
47 49 HEADERS += $$PRIVATE_HEADERS
48 50 HEADERS += $$THEMES
49 51 INCLUDEPATH += linechart \
50 52 barchart \
51 53 themes \
52 54 .
53 55 OBJECTS_DIR = $$CHART_BUILD_DIR/lib
54 56 MOC_DIR = $$CHART_BUILD_DIR/lib
55 57 UI_DIR = $$CHART_BUILD_DIR/lib
56 58 RCC_DIR = $$CHART_BUILD_DIR/lib
57 59 DEFINES += QTCOMMERCIALCHART_LIBRARY
58 60 public_headers.path = $$[QT_INSTALL_HEADERS]/QtCommercialChart
59 61 public_headers.files = $$PUBLIC_HEADERS
60 62 target.path = $$[QT_INSTALL_LIBS]
61 63 INSTALLS += target \
62 64 public_headers
63 65 install_build_public_headers.name = bild_public_headers
64 66 install_build_public_headers.output = $$CHART_BUILD_PUBLIC_HEADER_DIR/${QMAKE_FILE_BASE}.h
65 67 install_build_public_headers.input = PUBLIC_HEADERS
66 68 install_build_public_headers.commands = $$QMAKE_COPY \
67 69 ${QMAKE_FILE_NAME} \
68 70 $$CHART_BUILD_PUBLIC_HEADER_DIR
69 71 install_build_public_headers.CONFIG += target_predeps \
70 72 no_link
71 73 install_build_private_headers.name = bild_private_headers
72 74 install_build_private_headers.output = $$CHART_BUILD_PRIVATE_HEADER_DIR/${QMAKE_FILE_BASE}.h
73 75 install_build_private_headers.input = PRIVATE_HEADERS
74 76 install_build_private_headers.commands = $$QMAKE_COPY \
75 77 ${QMAKE_FILE_NAME} \
76 78 $$CHART_BUILD_PRIVATE_HEADER_DIR
77 79 install_build_private_headers.CONFIG += target_predeps \
78 80 no_link
79 81 QMAKE_EXTRA_COMPILERS += install_build_public_headers \
80 82 install_build_private_headers
81 83 chartversion.target = qchartversion_p.h
82 84 chartversion.commands = @echo \
83 85 "build_time" \
84 86 > \
85 87 $$chartversion.target;
86 88 chartversion.depends = $$HEADERS \
87 89 $$SOURCES
88 90 PRE_TARGETDEPS += qchartversion_p.h
89 91 QMAKE_CLEAN += qchartversion_p.h
90 92 QMAKE_EXTRA_TARGETS += chartversion
91 93 unix:QMAKE_DISTCLEAN += -r \
92 94 $$CHART_BUILD_HEADER_DIR \
93 95 $$CHART_BUILD_LIB_DIR
94 96 win32:QMAKE_DISTCLEAN += /Q \
95 97 $$CHART_BUILD_HEADER_DIR \
96 98 $$CHART_BUILD_LIB_DIR
General Comments 0
You need to be logged in to leave comments. Login now