##// 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
@@ -374,7 +374,7 void ChartPresenter::scroll(int dx,int dy)
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 }
@@ -1,38 +1,13
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)
@@ -49,7 +24,7 void QLegend::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, Q
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
@@ -90,19 +65,23 void QLegend::handleGeometryChanged(const QRectF& size)
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 }
@@ -24,7 +24,10 public:
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);
@@ -14,14 +14,16 SOURCES += \
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 \
General Comments 0
You need to be logged in to leave comments. Login now