@@ -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 | NO CONTENT: modified file |
|
NO CONTENT: modified file |
@@ -1,38 +1,13 | |||||
1 | #include "qchartglobal.h" |
|
1 | #include "qchartglobal.h" | |
2 | #include "qlegend.h" |
|
2 | #include "qlegend.h" | |
3 | #include "qseries.h" |
|
3 | #include "qseries.h" | |
|
4 | #include "legendmarker_p.h" | |||
4 | #include <QPainter> |
|
5 | #include <QPainter> | |
5 | #include <QPen> |
|
6 | #include <QPen> | |
6 |
|
7 | |||
7 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
8 | #include <QGraphicsSceneEvent> | |
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 |
|
9 | |||
23 | void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0) |
|
10 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
24 | { |
|
|||
25 | painter->setBrush(mBrush); |
|
|||
26 | painter->drawRect(mBoundingRect); |
|
|||
27 | } |
|
|||
28 |
|
||||
29 | QRectF boundingRect() const { return mBoundingRect; } |
|
|||
30 |
|
||||
31 | private: |
|
|||
32 | QRectF mBoundingRect; |
|
|||
33 | QBrush mBrush; |
|
|||
34 | QString mName; |
|
|||
35 | }; |
|
|||
36 |
|
11 | |||
37 | QLegend::QLegend(QGraphicsItem *parent) |
|
12 | QLegend::QLegend(QGraphicsItem *parent) | |
38 | : QGraphicsObject(parent) |
|
13 | : QGraphicsObject(parent) | |
@@ -49,7 +24,7 void QLegend::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, Q | |||||
49 | foreach(LegendMarker* m, mMarkers) { |
|
24 | foreach(LegendMarker* m, mMarkers) { | |
50 | QRectF r = m->boundingRect(); |
|
25 | QRectF r = m->boundingRect(); | |
51 | painter->setPen(m->color()); |
|
26 | painter->setPen(m->color()); | |
52 |
painter->drawText(r.x() + 2 |
|
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 | void QLegend::dataChanged() |
|
66 | void QLegend::dataChanged() | |
92 | { |
|
67 | { | |
93 | foreach (QGraphicsItem* i, childItems()) { |
|
68 | foreach (LegendMarker* marker, mMarkers) { | |
94 | delete i; |
|
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 | mMarkers.clear(); |
|
74 | mMarkers.clear(); | |
98 |
|
75 | |||
99 | foreach (QSeries* s, mSeriesList) { |
|
76 | foreach (QSeries* s, mSeriesList) { | |
100 | for (int i=0; i<s->legendEntries().count(); i++) { |
|
77 | for (int i=0; i<s->legendEntries().count(); i++) { | |
101 | LegendMarker *marker = new LegendMarker(this); |
|
78 | LegendMarker *marker = new LegendMarker(s, this); | |
102 | marker->setBrush(s->legendEntries().at(i).mBrush); |
|
79 | marker->setBrush(s->legendEntries().at(i).mBrush); | |
103 | marker->setName(s->legendEntries().at(i).mName); |
|
80 | marker->setName(s->legendEntries().at(i).mName); | |
104 | mMarkers.append(marker); |
|
81 | mMarkers.append(marker); | |
105 |
|
|
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,6 +24,9 public: | |||||
24 | QBrush backgroundBrush() const; |
|
24 | QBrush backgroundBrush() const; | |
25 |
|
25 | |||
26 | signals: |
|
26 | signals: | |
|
27 | // for interactions. | |||
|
28 | void clicked(QSeries* series, QString name); | |||
|
29 | void rightClicked(QSeries* series, QString name); | |||
27 |
|
30 | |||
28 | public slots: |
|
31 | public slots: | |
29 | void handleSeriesAdded(QSeries* series,Domain* domain); |
|
32 | void handleSeriesAdded(QSeries* series,Domain* domain); |
@@ -14,13 +14,15 SOURCES += \ | |||||
14 | qchart.cpp \ |
|
14 | qchart.cpp \ | |
15 | qchartview.cpp \ |
|
15 | qchartview.cpp \ | |
16 | qseries.cpp \ |
|
16 | qseries.cpp \ | |
17 | qlegend.cpp |
|
17 | qlegend.cpp \ | |
|
18 | legendmarker.cpp | |||
18 | PRIVATE_HEADERS += \ |
|
19 | PRIVATE_HEADERS += \ | |
19 | chartdataset_p.h \ |
|
20 | chartdataset_p.h \ | |
20 | chartitem_p.h \ |
|
21 | chartitem_p.h \ | |
21 | chartpresenter_p.h \ |
|
22 | chartpresenter_p.h \ | |
22 | charttheme_p.h \ |
|
23 | charttheme_p.h \ | |
23 | domain_p.h |
|
24 | domain_p.h \ | |
|
25 | legendmarker_p.h | |||
24 |
PUBLIC_HEADERS += \ |
|
26 | PUBLIC_HEADERS += \ | |
25 | qchart.h \ |
|
27 | qchart.h \ | |
26 | qchartglobal.h \ |
|
28 | qchartglobal.h \ |
General Comments 0
You need to be logged in to leave comments.
Login now