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