@@ -5,6 +5,7 | |||||
5 | #include <qstackedbarseries.h> |
|
5 | #include <qstackedbarseries.h> | |
6 | #include <qbarset.h> |
|
6 | #include <qbarset.h> | |
7 | #include <qchartaxis.h> |
|
7 | #include <qchartaxis.h> | |
|
8 | #include <qlegend.h> | |||
8 | #include <QStringList> |
|
9 | #include <QStringList> | |
9 | #include <QDebug> |
|
10 | #include <QDebug> | |
10 |
|
11 | |||
@@ -142,6 +143,9 int main(int argc, char *argv[]) | |||||
142 | drilldownChart->axisX()->setGridVisible(false); |
|
143 | drilldownChart->axisX()->setGridVisible(false); | |
143 | // drilldownChart->axisX()->setLabelsVisible(false); |
|
144 | // drilldownChart->axisX()->setLabelsVisible(false); | |
144 |
|
145 | |||
|
146 | QLegend* l = drilldownChart->legend(); | |||
|
147 | l->handleGeometryChanged(QRectF(20,20,100,100)); | |||
|
148 | ||||
145 | window.setCentralWidget(drilldownChart); |
|
149 | window.setCentralWidget(drilldownChart); | |
146 | window.resize(400, 300); |
|
150 | window.resize(400, 300); | |
147 | window.show(); |
|
151 | window.show(); |
@@ -46,7 +46,7 QList<QSeries::LegendEntry> BarChartModel::legendEntries() | |||||
46 | for (int i=0; i<mDataModel.count(); i++) { |
|
46 | for (int i=0; i<mDataModel.count(); i++) { | |
47 | QSeries::LegendEntry l; |
|
47 | QSeries::LegendEntry l; | |
48 | l.mName = mDataModel.at(i)->name(); |
|
48 | l.mName = mDataModel.at(i)->name(); | |
49 |
l.m |
|
49 | l.mBrush = mDataModel.at(i)->brush(); | |
50 | legend.append(l); |
|
50 | legend.append(l); | |
51 | } |
|
51 | } | |
52 | return legend; |
|
52 | return legend; |
@@ -357,6 +357,15 QChartAxis* QChartView::axisY() const | |||||
357 | } |
|
357 | } | |
358 |
|
358 | |||
359 | /*! |
|
359 | /*! | |
|
360 | Returns the pointer to legend object of the chart | |||
|
361 | */ | |||
|
362 | QLegend* QChartView::legend() const | |||
|
363 | { | |||
|
364 | return m_chart->legend(); | |||
|
365 | } | |||
|
366 | ||||
|
367 | ||||
|
368 | /*! | |||
360 | Sets animation \a options for the chart |
|
369 | Sets animation \a options for the chart | |
361 | */ |
|
370 | */ | |
362 | void QChartView::setAnimationOptions(QChart::AnimationOptions options) |
|
371 | void QChartView::setAnimationOptions(QChart::AnimationOptions options) |
@@ -53,6 +53,8 public: | |||||
53 | QChartAxis* axisX() const; |
|
53 | QChartAxis* axisX() const; | |
54 | QChartAxis* axisY() const; |
|
54 | QChartAxis* axisY() const; | |
55 |
|
55 | |||
|
56 | QLegend* legend() const; | |||
|
57 | ||||
56 | protected: |
|
58 | protected: | |
57 | void mousePressEvent(QMouseEvent *event); |
|
59 | void mousePressEvent(QMouseEvent *event); | |
58 | void mouseMoveEvent(QMouseEvent *event); |
|
60 | void mouseMoveEvent(QMouseEvent *event); |
@@ -1,24 +1,54 | |||||
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 <QPainter> | |||
|
5 | #include <QPen> | |||
4 |
|
6 | |||
5 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
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 | qDebug() << "LegendMarker::paint" << mBoundingRect; | |||
|
26 | painter->setBrush(mBrush); | |||
|
27 | painter->drawRect(mBoundingRect); | |||
|
28 | } | |||
|
29 | ||||
|
30 | QRectF boundingRect() const { return mBoundingRect; } | |||
|
31 | ||||
|
32 | private: | |||
|
33 | QRectF mBoundingRect; | |||
|
34 | QBrush mBrush; | |||
|
35 | QString mName; | |||
|
36 | }; | |||
|
37 | ||||
6 | QLegend::QLegend(QGraphicsItem *parent) |
|
38 | QLegend::QLegend(QGraphicsItem *parent) | |
7 | : QGraphicsObject(parent) |
|
39 | : QGraphicsObject(parent) | |
8 |
,mBoundingRect(0,0, |
|
40 | ,mBoundingRect(0,0,1,1) | |
9 | { |
|
41 | { | |
10 | } |
|
42 | } | |
11 |
|
43 | |||
12 |
|
||||
13 | void QLegend::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) |
|
44 | void QLegend::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) | |
14 | { |
|
45 | { | |
15 | // TODO: |
|
46 | // TODO: layout for text. using marker layout + magic for now. | |
16 | qDebug() << "QLegend::paint"; |
|
47 | foreach(LegendMarker* m, mMarkers) { | |
17 | foreach(QSeries* s, mSeriesList) { |
|
48 | QRectF r = m->boundingRect(); | |
18 | for (int i=0; i<s->legendEntries().count(); i++) { |
|
49 | painter->setPen(m->color()); | |
19 | // Paint it... |
|
50 | // TODO: r.y + r.height is incorrect. should be r.y. Find the bug, and remove the hack | |
20 | //qDebug() << s->legendEntries().at(i).mName; |
|
51 | painter->drawText(r.x() + 20, r.y()+r.height(), m->name()); | |
21 | } |
|
|||
22 | } |
|
52 | } | |
23 | } |
|
53 | } | |
24 |
|
54 | |||
@@ -29,16 +59,69 QRectF QLegend::boundingRect() const | |||||
29 |
|
59 | |||
30 | void QLegend::handleSeriesAdded(QSeries* series,Domain* domain) |
|
60 | void QLegend::handleSeriesAdded(QSeries* series,Domain* domain) | |
31 | { |
|
61 | { | |
32 | // TODO: append entries |
|
|||
33 | mSeriesList.append(series); |
|
62 | mSeriesList.append(series); | |
|
63 | dataChanged(); | |||
|
64 | layoutChanged(); | |||
34 | } |
|
65 | } | |
35 |
|
66 | |||
36 | void QLegend::handleSeriesRemoved(QSeries* series) |
|
67 | void QLegend::handleSeriesRemoved(QSeries* series) | |
37 | { |
|
68 | { | |
38 | // TODO: remove entries |
|
|||
39 | mSeriesList.removeOne(series); |
|
69 | mSeriesList.removeOne(series); | |
|
70 | dataChanged(); | |||
|
71 | layoutChanged(); | |||
|
72 | } | |||
|
73 | ||||
|
74 | void QLegend::handleGeometryChanged(const QRectF& size) | |||
|
75 | { | |||
|
76 | mBoundingRect = size; | |||
|
77 | layoutChanged(); | |||
40 | } |
|
78 | } | |
41 |
|
79 | |||
|
80 | void QLegend::dataChanged() | |||
|
81 | { | |||
|
82 | foreach (QGraphicsItem* i, childItems()) { | |||
|
83 | delete i; | |||
|
84 | } | |||
|
85 | ||||
|
86 | mMarkers.clear(); | |||
|
87 | ||||
|
88 | foreach (QSeries* s, mSeriesList) { | |||
|
89 | for (int i=0; i<s->legendEntries().count(); i++) { | |||
|
90 | LegendMarker *marker = new LegendMarker(this); | |||
|
91 | marker->setBrush(s->legendEntries().at(i).mBrush); | |||
|
92 | marker->setName(s->legendEntries().at(i).mName); | |||
|
93 | mMarkers.append(marker); | |||
|
94 | childItems().append(marker); | |||
|
95 | } | |||
|
96 | } | |||
|
97 | } | |||
|
98 | ||||
|
99 | ||||
|
100 | void QLegend::layoutChanged() | |||
|
101 | { | |||
|
102 | // Calculate layout for markers and text | |||
|
103 | if (mMarkers.count() <= 0) { | |||
|
104 | // Nothing to do | |||
|
105 | return; | |||
|
106 | } | |||
|
107 | ||||
|
108 | // TODO: marker defined by series. | |||
|
109 | QSizeF markerSize(10,10); | |||
|
110 | ||||
|
111 | // TODO: better layout, this is just concept. | |||
|
112 | // Leave some space around markers like this: | x x x x | | |||
|
113 | qreal steps = mMarkers.count() * 2 + 1; | |||
|
114 | ||||
|
115 | qreal yStep = mBoundingRect.height() / steps; | |||
|
116 | qreal x = 0; | |||
|
117 | qreal y = yStep; // first step is empty | |||
|
118 | foreach (LegendMarker* m, mMarkers) { | |||
|
119 | m->setBoundingRect(QRectF(x,y,markerSize.width(),markerSize.height())); | |||
|
120 | y += yStep*2; // 2 steps per marker (marker and empty space) | |||
|
121 | } | |||
|
122 | } | |||
|
123 | ||||
|
124 | ||||
42 |
|
125 | |||
43 | #include "moc_qlegend.cpp" |
|
126 | #include "moc_qlegend.cpp" | |
44 | QTCOMMERCIALCHART_END_NAMESPACE |
|
127 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -8,8 +8,9 | |||||
8 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
8 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
9 |
|
9 | |||
10 | class Domain; |
|
10 | class Domain; | |
|
11 | class LegendMarker; | |||
11 |
|
12 | |||
12 | class QLegend : public QGraphicsObject |
|
13 | class QTCOMMERCIALCHART_EXPORT QLegend : public QGraphicsObject | |
13 | { |
|
14 | { | |
14 | Q_OBJECT |
|
15 | Q_OBJECT | |
15 | public: |
|
16 | public: | |
@@ -24,11 +25,15 signals: | |||||
24 | public slots: |
|
25 | public slots: | |
25 | void handleSeriesAdded(QSeries* series,Domain* domain); |
|
26 | void handleSeriesAdded(QSeries* series,Domain* domain); | |
26 | void handleSeriesRemoved(QSeries* series); |
|
27 | void handleSeriesRemoved(QSeries* series); | |
|
28 | void handleGeometryChanged(const QRectF& size); | |||
27 |
|
29 | |||
28 | private: |
|
30 | private: | |
|
31 | void dataChanged(); | |||
|
32 | void layoutChanged(); | |||
29 |
|
33 | |||
30 | QRectF mBoundingRect; |
|
34 | QRectF mBoundingRect; | |
31 | QList<QSeries*> mSeriesList; |
|
35 | QList<QSeries*> mSeriesList; | |
|
36 | QList<LegendMarker*> mMarkers; | |||
32 | }; |
|
37 | }; | |
33 |
|
38 | |||
34 | QTCOMMERCIALCHART_END_NAMESPACE |
|
39 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -24,11 +24,11 public: | |||||
24 | }; |
|
24 | }; | |
25 |
|
25 | |||
26 | // Helper class to contain legend and color for it |
|
26 | // Helper class to contain legend and color for it | |
27 | // TODO: This as private class? Or should we expose this to user of API |
|
27 | // TODO: This is actually quite close to current LegendMarker.. combine them? | |
28 | class LegendEntry { |
|
28 | class LegendEntry { | |
29 | public: |
|
29 | public: | |
30 | QString mName; |
|
30 | QString mName; | |
31 |
Q |
|
31 | QBrush mBrush; | |
32 | }; |
|
32 | }; | |
33 |
|
33 | |||
34 | protected: |
|
34 | protected: |
General Comments 0
You need to be logged in to leave comments.
Login now