##// END OF EJS Templates
background to legend, theme applies
sauimone -
r540:7d8a0757e05d
parent child
Show More
@@ -138,13 +138,10 int main(int argc, char *argv[])
138 drilldownChart->setChartTitle(seasonSeries->title());
138 drilldownChart->setChartTitle(seasonSeries->title());
139 //! [6]
139 //! [6]
140
140
141 // Disable axis, since they don't really apply to bar chart
142 // drilldownChart->axisX()->setAxisVisible(false);
143 drilldownChart->axisX()->setGridLineVisible(false);
141 drilldownChart->axisX()->setGridLineVisible(false);
144 // drilldownChart->axisX()->setLabelsVisible(false);
145
142
146 QLegend* l = drilldownChart->legend();
143 QLegend* l = drilldownChart->legend();
147 l->handleGeometryChanged(QRectF(20,20,100,100));
144 l->handleGeometryChanged(QRectF(50,270,300,20));
148
145
149 window.setCentralWidget(drilldownChart);
146 window.setCentralWidget(drilldownChart);
150 window.resize(400, 300);
147 window.resize(400, 300);
@@ -277,6 +277,7 void ChartPresenter::setChartTheme(QChart::ChartTheme theme)
277 delete m_chartTheme;
277 delete m_chartTheme;
278 m_chartTheme = ChartTheme::createTheme(theme);
278 m_chartTheme = ChartTheme::createTheme(theme);
279 m_chartTheme->decorate(m_chart);
279 m_chartTheme->decorate(m_chart);
280 m_chartTheme->decorate(m_chart->legend());
280 resetAllElements();
281 resetAllElements();
281 }
282 }
282
283
@@ -27,7 +27,8 public:
27 AxisZValue,
27 AxisZValue,
28 LineChartZValue,
28 LineChartZValue,
29 ScatterSeriesZValue,
29 ScatterSeriesZValue,
30 PieSeriesZValue
30 PieSeriesZValue,
31 LegendZValue
31 };
32 };
32
33
33 ChartPresenter(QChart* chart,ChartDataSet *dataset);
34 ChartPresenter(QChart* chart,ChartDataSet *dataset);
@@ -1,5 +1,6
1 #include "charttheme_p.h"
1 #include "charttheme_p.h"
2 #include "qchart.h"
2 #include "qchart.h"
3 #include "qlegend.h"
3 #include "qchartaxis.h"
4 #include "qchartaxis.h"
4 #include <QTime>
5 #include <QTime>
5
6
@@ -64,6 +65,11 void ChartTheme::decorate(QChart* chart)
64 chart->setChartBackgroundBrush(m_backgroundGradient);
65 chart->setChartBackgroundBrush(m_backgroundGradient);
65 }
66 }
66
67
68 void ChartTheme::decorate(QLegend* legend)
69 {
70 legend->setBackgroundBrush(m_backgroundGradient);
71 }
72
67 void ChartTheme::decorate(AreaChartItem* item, QAreaSeries* series, int index)
73 void ChartTheme::decorate(AreaChartItem* item, QAreaSeries* series, int index)
68 {
74 {
69 QPen pen;
75 QPen pen;
@@ -35,6 +35,7 public:
35 static ChartTheme* createTheme(QChart::ChartTheme theme);
35 static ChartTheme* createTheme(QChart::ChartTheme theme);
36 QChart::ChartTheme id() const {return m_id;}
36 QChart::ChartTheme id() const {return m_id;}
37 void decorate(QChart* chart);
37 void decorate(QChart* chart);
38 void decorate(QLegend* legend);
38 //void decorate(ChartItem* item, QSeries* series,int index);
39 //void decorate(ChartItem* item, QSeries* series,int index);
39 void decorate(BarPresenter* item, QBarSeries* series, int index);
40 void decorate(BarPresenter* item, QBarSeries* series, int index);
40 void decorate(StackedBarPresenter* item, QStackedBarSeries* series, int index);
41 void decorate(StackedBarPresenter* item, QStackedBarSeries* series, int index);
@@ -50,9 +50,9 QChart::QChart(QGraphicsItem *parent, Qt::WindowFlags wFlags) : QGraphicsWidget(
50 m_backgroundItem(0),
50 m_backgroundItem(0),
51 m_titleItem(0),
51 m_titleItem(0),
52 m_dataset(new ChartDataSet(this)),
52 m_dataset(new ChartDataSet(this)),
53 m_presenter(new ChartPresenter(this,m_dataset)),
53 m_presenter(new ChartPresenter(this,m_dataset))
54 m_legend(new QLegend(this)) // TODO: is this the parent we want the legend to have?
55 {
54 {
55 createLegend();
56 connect(m_dataset,SIGNAL(seriesAdded(QSeries*,Domain*)),m_legend,SLOT(handleSeriesAdded(QSeries*,Domain*)));
56 connect(m_dataset,SIGNAL(seriesAdded(QSeries*,Domain*)),m_legend,SLOT(handleSeriesAdded(QSeries*,Domain*)));
57 connect(m_dataset,SIGNAL(seriesRemoved(QSeries*)),m_legend,SLOT(handleSeriesRemoved(QSeries*)));
57 connect(m_dataset,SIGNAL(seriesRemoved(QSeries*)),m_legend,SLOT(handleSeriesRemoved(QSeries*)));
58 }
58 }
@@ -181,6 +181,16 void QChart::createChartTitleItem()
181 }
181 }
182 }
182 }
183
183
184 void QChart::createLegend()
185 {
186 // TODO: Why we have null pointer, even if this is created in constructor?
187 if(!m_legend) {
188 m_legend = new QLegend(this);
189 m_legend->setZValue(ChartPresenter::LegendZValue);
190 }
191 }
192
193
184 /*!
194 /*!
185 Returns the chart margin, which is the distance between the widget edge and the part of the chart where the actual data can be displayed.
195 Returns the chart margin, which is the distance between the widget edge and the part of the chart where the actual data can be displayed.
186 \sa setMargin()
196 \sa setMargin()
@@ -270,12 +280,12 QChartAxis* QChart::axisY() const
270 /*!
280 /*!
271 Returns the legend object of the chart
281 Returns the legend object of the chart
272 */
282 */
273 QLegend* QChart::legend() const
283 QLegend* QChart::legend()
274 {
284 {
285 createLegend();
275 return m_legend;
286 return m_legend;
276 }
287 }
277
288
278
279 /*!
289 /*!
280 Resizes and updates the chart area using the \a event data
290 Resizes and updates the chart area using the \a event data
281 */
291 */
@@ -77,7 +77,7 public:
77 QChartAxis* axisX() const;
77 QChartAxis* axisX() const;
78 QChartAxis* axisY() const;
78 QChartAxis* axisY() const;
79
79
80 QLegend* legend() const;
80 QLegend* legend();
81
81
82 protected:
82 protected:
83 void resizeEvent(QGraphicsSceneResizeEvent *event);
83 void resizeEvent(QGraphicsSceneResizeEvent *event);
@@ -85,6 +85,7 protected:
85 private:
85 private:
86 inline void createChartBackgroundItem();
86 inline void createChartBackgroundItem();
87 inline void createChartTitleItem();
87 inline void createChartTitleItem();
88 inline void createLegend();
88
89
89 private:
90 private:
90 Q_DISABLE_COPY(QChart)
91 Q_DISABLE_COPY(QChart)
@@ -364,7 +364,6 QLegend* QChartView::legend() const
364 return m_chart->legend();
364 return m_chart->legend();
365 }
365 }
366
366
367
368 /*!
367 /*!
369 Sets animation \a options for the chart
368 Sets animation \a options for the chart
370 */
369 */
@@ -22,7 +22,6 public:
22
22
23 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0)
23 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0)
24 {
24 {
25 qDebug() << "LegendMarker::paint" << mBoundingRect;
26 painter->setBrush(mBrush);
25 painter->setBrush(mBrush);
27 painter->drawRect(mBoundingRect);
26 painter->drawRect(mBoundingRect);
28 }
27 }
@@ -38,17 +37,19 private:
38 QLegend::QLegend(QGraphicsItem *parent)
37 QLegend::QLegend(QGraphicsItem *parent)
39 : QGraphicsObject(parent)
38 : QGraphicsObject(parent)
40 ,mBoundingRect(0,0,1,1)
39 ,mBoundingRect(0,0,1,1)
40 ,mBackgroundBrush(Qt::darkGray) // TODO: from theme?
41 {
41 {
42 }
42 }
43
43
44 void QLegend::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
44 void QLegend::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
45 {
45 {
46 // TODO: layout for text. using marker layout + magic for now.
46 painter->setBrush(mBackgroundBrush);
47 painter->drawRect(mBoundingRect);
48
47 foreach(LegendMarker* m, mMarkers) {
49 foreach(LegendMarker* m, mMarkers) {
48 QRectF r = m->boundingRect();
50 QRectF r = m->boundingRect();
49 painter->setPen(m->color());
51 painter->setPen(m->color());
50 // TODO: r.y + r.height is incorrect. should be r.y. Find the bug, and remove the hack
52 painter->drawText(r.x() + 20, r.y() + r.height(), m->name());
51 painter->drawText(r.x() + 20, r.y()+r.height(), m->name());
52 }
53 }
53 }
54 }
54
55
@@ -57,6 +58,16 QRectF QLegend::boundingRect() const
57 return mBoundingRect;
58 return mBoundingRect;
58 }
59 }
59
60
61 void QLegend::setBackgroundBrush(const QBrush& brush)
62 {
63 mBackgroundBrush = brush;
64 }
65
66 QBrush QLegend::backgroundBrush() const
67 {
68 return mBackgroundBrush;
69 }
70
60 void QLegend::handleSeriesAdded(QSeries* series,Domain* domain)
71 void QLegend::handleSeriesAdded(QSeries* series,Domain* domain)
61 {
72 {
62 mSeriesList.append(series);
73 mSeriesList.append(series);
@@ -91,12 +102,11 void QLegend::dataChanged()
91 marker->setBrush(s->legendEntries().at(i).mBrush);
102 marker->setBrush(s->legendEntries().at(i).mBrush);
92 marker->setName(s->legendEntries().at(i).mName);
103 marker->setName(s->legendEntries().at(i).mName);
93 mMarkers.append(marker);
104 mMarkers.append(marker);
94 childItems().append(marker);
105 // childItems().append(marker);
95 }
106 }
96 }
107 }
97 }
108 }
98
109
99
100 void QLegend::layoutChanged()
110 void QLegend::layoutChanged()
101 {
111 {
102 // Calculate layout for markers and text
112 // Calculate layout for markers and text
@@ -110,14 +120,17 void QLegend::layoutChanged()
110
120
111 // TODO: better layout, this is just concept.
121 // TODO: better layout, this is just concept.
112 // Leave some space around markers like this: | x x x x |
122 // Leave some space around markers like this: | x x x x |
113 qreal steps = mMarkers.count() * 2 + 1;
123 qreal steps = mMarkers.count();
114
124
125 qreal xStep = mBoundingRect.width() / steps;
115 qreal yStep = mBoundingRect.height() / steps;
126 qreal yStep = mBoundingRect.height() / steps;
116 qreal x = 0;
127 qreal x = mBoundingRect.x() + 5;
117 qreal y = yStep; // first step is empty
128 qreal y = mBoundingRect.y() + (mBoundingRect.height() - markerSize.height())/2;
118 foreach (LegendMarker* m, mMarkers) {
129 foreach (LegendMarker* m, mMarkers) {
130 qDebug() << "marker x:" << x;
131 qDebug() << "marker y:" << y;
119 m->setBoundingRect(QRectF(x,y,markerSize.width(),markerSize.height()));
132 m->setBoundingRect(QRectF(x,y,markerSize.width(),markerSize.height()));
120 y += yStep*2; // 2 steps per marker (marker and empty space)
133 x += xStep;
121 }
134 }
122 }
135 }
123
136
@@ -20,6 +20,9 public:
20 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
20 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
21 QRectF boundingRect() const;
21 QRectF boundingRect() const;
22
22
23 void setBackgroundBrush(const QBrush& brush);
24 QBrush backgroundBrush() const;
25
23 signals:
26 signals:
24
27
25 public slots:
28 public slots:
@@ -34,6 +37,8 private:
34 QRectF mBoundingRect;
37 QRectF mBoundingRect;
35 QList<QSeries*> mSeriesList;
38 QList<QSeries*> mSeriesList;
36 QList<LegendMarker*> mMarkers;
39 QList<LegendMarker*> mMarkers;
40
41 QBrush mBackgroundBrush;
37 };
42 };
38
43
39 QTCOMMERCIALCHART_END_NAMESPACE
44 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now