##// END OF EJS Templates
fixed bug in legend name drawing
sauimone -
r585:b761105be1ea
parent child
Show More
@@ -1,109 +1,121
1 1 #include "qchartglobal.h"
2 2 #include "legendmarker_p.h"
3 3 #include <QPainter>
4 4 #include <QGraphicsSceneEvent>
5 5
6 6 QTCOMMERCIALCHART_BEGIN_NAMESPACE
7 7
8 8 LegendMarker::LegendMarker(QSeries* series, QGraphicsItem *parent)
9 9 : QGraphicsObject(parent)
10 10 ,mBoundingRect(0,0,1,1)
11 ,mName("")
11 ,mMarkerBoundingRect(0,0,1,1)
12 12 ,mSeries(series)
13 13 ,mBarset(0)
14 14 ,mPieslice(0)
15 15 ,mType(LegendMarkerTypeSeries)
16 ,mTextItem(new QGraphicsSimpleTextItem(this))
16 17 {
17 18 setAcceptedMouseButtons(Qt::LeftButton|Qt::RightButton);
18 19 }
19 20
20 21 LegendMarker::LegendMarker(QSeries *series, QBarSet *barset, QGraphicsItem *parent)
21 22 : QGraphicsObject(parent)
22 23 ,mBoundingRect(0,0,1,1)
23 ,mName("")
24 ,mMarkerBoundingRect(0,0,1,1)
24 25 ,mSeries(series)
25 26 ,mBarset(barset)
26 27 ,mPieslice(0)
27 28 ,mType(LegendMarkerTypeBarset)
29 ,mTextItem(new QGraphicsSimpleTextItem(this))
28 30 {
29 31 setAcceptedMouseButtons(Qt::LeftButton|Qt::RightButton);
30 32 }
31 33
32 34 LegendMarker::LegendMarker(QSeries *series, QPieSlice *pieslice, QGraphicsItem *parent)
33 35 : QGraphicsObject(parent)
34 36 ,mBoundingRect(0,0,1,1)
35 ,mName("")
37 ,mMarkerBoundingRect(0,0,1,1)
36 38 ,mSeries(series)
37 39 ,mBarset(0)
38 40 ,mPieslice(pieslice)
39 41 ,mType(LegendMarkerTypePieslice)
42 ,mTextItem(new QGraphicsSimpleTextItem(this))
40 43 {
41 44 setAcceptedMouseButtons(Qt::LeftButton|Qt::RightButton);
42 45 }
43 46
44 47 void LegendMarker::setBoundingRect(const QRectF rect)
45 48 {
46 49 mBoundingRect = rect;
50 // Calculate Marker pos
51
52 // TODO: remove hard coding. 5 is marigin around marker
53 QSizeF markerSize(10,10);
54 qreal x = mBoundingRect.x() + 5;
55 qreal y = mBoundingRect.y() + (mBoundingRect.height() - markerSize.height())/2;
56 mMarkerBoundingRect = QRectF(x,y,markerSize.width(),markerSize.height());
57
58 mTextItem.setPos(mBoundingRect.x() + markerSize.width() + 10, y );
47 59 }
48 60
49 61 void LegendMarker::setBrush(const QBrush brush)
50 62 {
51 63 mBrush = brush;
52 64 }
53 65
54 66 QBrush LegendMarker::brush() const
55 67 {
56 68 return mBrush;
57 69 }
58 70
59 71 void LegendMarker::setName(const QString name)
60 72 {
61 mName = name;
73 mTextItem.setText(name);
62 74 }
63 75
64 76 QString LegendMarker::name() const
65 77 {
66 return mName;
78 return mTextItem.text();
67 79 }
68 80
69 81 QSeries* LegendMarker::series() const
70 82 {
71 83 return mSeries;
72 84 }
73 85
74 86 void LegendMarker::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
75 87 {
76 88 painter->setBrush(mBrush);
77 painter->drawRect(mBoundingRect);
89 painter->drawRect(mMarkerBoundingRect);
78 90 }
79 91
80 92 QRectF LegendMarker::boundingRect() const
81 93 {
82 94 return mBoundingRect;
83 95 }
84 96
85 97 void LegendMarker::mousePressEvent(QGraphicsSceneMouseEvent *event)
86 98 {
87 99 switch (mType)
88 100 {
89 101 case LegendMarkerTypeSeries: {
90 102 emit clicked(mSeries,event->button());
91 103 break;
92 104 }
93 105 case LegendMarkerTypeBarset: {
94 106 emit clicked(mBarset,event->button());
95 107 break;
96 108 }
97 109 case LegendMarkerTypePieslice: {
98 110 emit clicked(mPieslice,event->button());
99 111 break;
100 112 }
101 113 default: {
102 114 break;
103 115 }
104 116 }
105 117 }
106 118
107 119 #include "moc_legendmarker_p.cpp"
108 120
109 121 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,64 +1,66
1 1 #ifndef LEGENDMARKER_P_H
2 2 #define LEGENDMARKER_P_H
3 3
4 4 #include "qchartglobal.h"
5 5 #include <QGraphicsObject>
6 6 #include <QBrush>
7 7
8 8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 9
10 10 class QSeries;
11 11 class QBarSet;
12 12 class QPieSlice;
13 13
14 14 // TODO: split this to 3 different markers for series, barset and pieslice. Current implementation is easier to misuse...
15 15 class LegendMarker : public QGraphicsObject
16 16 {
17 17 Q_OBJECT
18 18
19 19 enum LegendMarkerType {
20 20 LegendMarkerTypeSeries,
21 21 LegendMarkerTypeBarset,
22 22 LegendMarkerTypePieslice
23 23 };
24 24
25 25 public:
26 26 LegendMarker(QSeries* series, QGraphicsItem *parent = 0);
27 27 LegendMarker(QSeries* series, QBarSet* barset, QGraphicsItem *parent = 0);
28 28 LegendMarker(QSeries* series, QPieSlice* pieslice, QGraphicsItem *parent = 0);
29 29 void setBoundingRect(const QRectF rect);
30 30
31 31 void setBrush(const QBrush brush);
32 32 QBrush brush() const;
33 33
34 34 void setName(const QString name);
35 35 QString name() const;
36 36
37 37 QSeries* series() const;
38 38
39 39 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
40 40
41 41 QRectF boundingRect() const;
42 42
43 43 public:
44 44 // From QGraphicsObject
45 45 void mousePressEvent(QGraphicsSceneMouseEvent *event);
46 46
47 47 Q_SIGNALS:
48 48 void clicked(QSeries* series, Qt::MouseButton button);
49 49 void clicked(QBarSet* barset, Qt::MouseButton button);
50 50 void clicked(QPieSlice* pieslice, Qt::MouseButton button);
51 51
52 52 private:
53 53 QRectF mBoundingRect;
54 QRectF mMarkerBoundingRect;
54 55 QBrush mBrush;
55 QString mName;
56 56 QSeries* mSeries;
57 57 QBarSet* mBarset;
58 58 QPieSlice* mPieslice;
59 59
60 60 LegendMarkerType mType;
61 QGraphicsSimpleTextItem mTextItem;
62
61 63 };
62 64
63 65 QTCOMMERCIALCHART_END_NAMESPACE
64 66 #endif // LEGENDMARKER_P_H
@@ -1,356 +1,355
1 1 #include "qchart.h"
2 2 #include "qchartaxis.h"
3 3 #include "qlegend.h"
4 4 #include "chartpresenter_p.h"
5 5 #include "chartdataset_p.h"
6 6 #include <QGraphicsScene>
7 7 #include <QGraphicsSceneResizeEvent>
8 8 #include <QDebug>
9 9
10 10 QTCOMMERCIALCHART_BEGIN_NAMESPACE
11 11
12 12 /*!
13 13 \enum QChart::ChartTheme
14 14
15 15 This enum describes the theme used by the chart.
16 16
17 17 \value ChartThemeDefault Follows the GUI style of the Operating System
18 18 \value ChartThemeVanilla
19 19 \value ChartThemeIcy
20 20 \value ChartThemeGrayscale
21 21 \value ChartThemeScientific
22 22 \value ChartThemeBlueCerulean
23 23 \value ChartThemeLight
24 24 \value ChartThemeCount Not really a theme; the total count of themes.
25 25 */
26 26
27 27 /*!
28 28 \enum QChart::AnimationOption
29 29
30 30 For enabling/disabling animations. Defaults to NoAnimation.
31 31
32 32 \value NoAnimation
33 33 \value GridAxisAnimations
34 34 \value SeriesAnimations
35 35 \value AllAnimations
36 36 */
37 37
38 38 /*!
39 39 \class QChart
40 40 \brief QtCommercial chart API.
41 41
42 42 QChart is a QGraphicsWidget that you can show in a QGraphicsScene. It manages the graphical
43 43 representation of different types of QChartSeries and other chart related objects like
44 44 QChartAxis and QChartLegend. If you simply want to show a chart in a layout, you can use the
45 45 convenience class QChartView instead of QChart.
46 46 \sa QChartView
47 47 */
48 48
49 49 /*!
50 50 Constructs a chart object which is a child of a\a parent. Parameter \a wFlags is passed to the QGraphicsWidget constructor.
51 51 */
52 52 QChart::QChart(QGraphicsItem *parent, Qt::WindowFlags wFlags) : QGraphicsWidget(parent,wFlags),
53 53 m_backgroundItem(0),
54 54 m_titleItem(0),
55 55 m_legend(new QLegend(this)),
56 56 m_dataset(new ChartDataSet(this)),
57 57 m_presenter(new ChartPresenter(this,m_dataset))
58 58 {
59 59 connect(m_dataset,SIGNAL(seriesAdded(QSeries*,Domain*)),m_legend,SLOT(handleSeriesAdded(QSeries*,Domain*)));
60 60 connect(m_dataset,SIGNAL(seriesRemoved(QSeries*)),m_legend,SLOT(handleSeriesRemoved(QSeries*)));
61 61 }
62 62
63 63 /*!
64 64 Destroys the object and it's children, like QChartSeries and QChartAxis object added to it.
65 65 */
66 66 QChart::~QChart()
67 67 {
68 68 disconnect(m_dataset,SIGNAL(seriesAdded(QSeries*,Domain*)),m_legend,SLOT(handleSeriesAdded(QSeries*,Domain*)));
69 69 disconnect(m_dataset,SIGNAL(seriesRemoved(QSeries*)),m_legend,SLOT(handleSeriesRemoved(QSeries*)));
70 70 }
71 71
72 72 /*!
73 73 Adds the \a series and optional \a axisY onto the chart and takes the ownership of the objects.
74 74 If auto scaling is enabled, re-scales the axes the series is bound to (both the x axis and
75 75 the y axis).
76 76 */
77 77 void QChart::addSeries(QSeries* series, QChartAxis* axisY)
78 78 {
79 79 m_dataset->addSeries(series, axisY);
80 80 }
81 81
82 82 /*!
83 83 Removes the \a series specified in a perameter from the QChartView.
84 84 It releses its ownership of the specified QChartSeries object.
85 85 It does not delete the pointed QChartSeries data object
86 86 \sa addSeries(), removeAllSeries()
87 87 */
88 88 void QChart::removeSeries(QSeries* series)
89 89 {
90 90 m_dataset->removeSeries(series);
91 91 }
92 92
93 93 /*!
94 94 Removes all the QChartSeries that have been added to the QChartView
95 95 It also deletes the pointed QChartSeries data objects
96 96 \sa addSeries(), removeSeries()
97 97 */
98 98 void QChart::removeAllSeries()
99 99 {
100 100 m_dataset->removeAllSeries();
101 101 }
102 102
103 103 /*!
104 104 Sets the \a brush that is used for painting the background of the chart area.
105 105 */
106 106 void QChart::setChartBackgroundBrush(const QBrush& brush)
107 107 {
108 108 createChartBackgroundItem();
109 109 m_backgroundItem->setBrush(brush);
110 110 m_backgroundItem->update();
111 111 }
112 112
113 113 /*!
114 114 Sets the \a pen that is used for painting the background of the chart area.
115 115 */
116 116 void QChart::setChartBackgroundPen(const QPen& pen)
117 117 {
118 118 createChartBackgroundItem();
119 119 m_backgroundItem->setPen(pen);
120 120 m_backgroundItem->update();
121 121 }
122 122
123 123 /*!
124 124 Sets the chart \a title. The description text that is drawn above the chart.
125 125 */
126 126 void QChart::setChartTitle(const QString& title)
127 127 {
128 128 createChartTitleItem();
129 129 m_titleItem->setText(title);
130 130 updateLayout();
131 131 }
132 132
133 133 /*!
134 134 Returns the chart title. The description text that is drawn above the chart.
135 135 */
136 136 QString QChart::chartTitle() const
137 137 {
138 138 if(m_titleItem)
139 139 return m_titleItem->text();
140 140 else
141 141 return QString();
142 142 }
143 143
144 144 /*!
145 145 Sets the \a font that is used for rendering the description text that is rendered above the chart.
146 146 */
147 147 void QChart::setChartTitleFont(const QFont& font)
148 148 {
149 149 createChartTitleItem();
150 150 m_titleItem->setFont(font);
151 151 updateLayout();
152 152 }
153 153
154 154 /*!
155 155 Sets the \a brush used for rendering the title text.
156 156 */
157 157 void QChart::setChartTitleBrush(const QBrush &brush)
158 158 {
159 159 createChartTitleItem();
160 160 m_titleItem->setBrush(brush);
161 161 updateLayout();
162 162 }
163 163
164 164 /*!
165 165 Returns the brush used for rendering the title text.
166 166 */
167 167 QBrush QChart::chartTitleBrush()
168 168 {
169 169 createChartTitleItem();
170 170 return m_titleItem->brush();
171 171 }
172 172
173 173 void QChart::createChartBackgroundItem()
174 174 {
175 175 if(!m_backgroundItem) {
176 176 m_backgroundItem = new QGraphicsRectItem(this);
177 177 m_backgroundItem->setPen(Qt::NoPen);
178 178 m_backgroundItem->setZValue(ChartPresenter::BackgroundZValue);
179 179 }
180 180 }
181 181
182 182 void QChart::createChartTitleItem()
183 183 {
184 184 if(!m_titleItem) {
185 185 m_titleItem = new QGraphicsSimpleTextItem(this);
186 186 m_titleItem->setZValue(ChartPresenter::BackgroundZValue);
187 187 }
188 188 }
189 189
190 190 /*!
191 191 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.
192 192 \sa setMargin()
193 193 */
194 194 int QChart::margin() const
195 195 {
196 196 return m_presenter->margin();
197 197 }
198 198
199 199 /*!
200 200 Sets the chart \a margin, which is the distance between the widget edge and the part of the chart where the actual data can be displayed.
201 201 \sa margin()
202 202 */
203 203 void QChart::setMargin(int margin)
204 204 {
205 205 m_presenter->setMargin(margin);
206 206 updateLayout();
207 207 }
208 208
209 209 /*!
210 210 Sets the \a theme used by the chart for rendering the graphical representation of the data
211 211 \sa ChartTheme, chartTheme()
212 212 */
213 213 void QChart::setChartTheme(QChart::ChartTheme theme)
214 214 {
215 215 m_presenter->setChartTheme(theme);
216 216 }
217 217
218 218 /*!
219 219 Returns the theme enum used by the chart.
220 220 \sa ChartTheme, setChartTheme()
221 221 */
222 222 QChart::ChartTheme QChart::chartTheme() const
223 223 {
224 224 return m_presenter->chartTheme();
225 225 }
226 226
227 227 /*!
228 228 Zooms in the view by a factor of 2
229 229 */
230 230 void QChart::zoomIn()
231 231 {
232 232 m_presenter->zoomIn();
233 233 }
234 234
235 235 /*!
236 236 Zooms in the view to a maximum level at which \a rect is still fully visible.
237 237 */
238 238 void QChart::zoomIn(const QRectF& rect)
239 239 {
240 240
241 241 if(!rect.isValid()) return;
242 242 m_presenter->zoomIn(rect);
243 243 }
244 244
245 245 /*!
246 246 Restores the view zoom level to the previous one.
247 247 */
248 248 void QChart::zoomOut()
249 249 {
250 250 m_presenter->zoomOut();
251 251 }
252 252
253 253 /*!
254 254 Resets to the default view.
255 255 */
256 256 void QChart::zoomReset()
257 257 {
258 258 m_presenter->zoomReset();
259 259 }
260 260
261 261 /*!
262 262 Returns the pointer to the x axis object of the chart
263 263 */
264 264 QChartAxis* QChart::axisX() const
265 265 {
266 266 return m_dataset->axisX();
267 267 }
268 268
269 269 /*!
270 270 Returns the pointer to the y axis object of the chart
271 271 */
272 272 QChartAxis* QChart::axisY() const
273 273 {
274 274 return m_dataset->axisY();
275 275 }
276 276
277 277 /*!
278 278 Returns the legend object of the chart
279 279 */
280 280 QLegend* QChart::legend()
281 281 {
282 282 return m_legend;
283 283 }
284 284
285 285 /*!
286 286 Resizes and updates the chart area using the \a event data
287 287 */
288 288 void QChart::resizeEvent(QGraphicsSceneResizeEvent *event)
289 289 {
290 290
291 291 m_rect = QRectF(QPoint(0,0),event->newSize());
292 292 updateLayout();
293 293 QGraphicsWidget::resizeEvent(event);
294 294 update();
295 295 }
296 296
297 297 /*!
298 298 Sets animation \a options for the chart
299 299 */
300 300 void QChart::setAnimationOptions(AnimationOptions options)
301 301 {
302 302 m_presenter->setAnimationOptions(options);
303 303 }
304 304
305 305 /*!
306 306 Returns animation options for the chart
307 307 */
308 308 QChart::AnimationOptions QChart::animationOptions() const
309 309 {
310 310 return m_presenter->animationOptions();
311 311 }
312 312
313 313 void QChart::scroll(int dx,int dy)
314 314 {
315 315 //temporary
316 316 if(dx>0)
317 317 m_presenter->scroll(m_presenter->geometry().width()/(axisX()->ticksCount()-1),0);
318 318 if(dx<0)
319 319 m_presenter->scroll(-m_presenter->geometry().width()/(axisX()->ticksCount()-1),0);
320 320 if(dy>0)
321 321 m_presenter->scroll(0,m_presenter->geometry().width()/(axisY()->ticksCount()-1));
322 322 if(dy<0)
323 323 m_presenter->scroll(0,-m_presenter->geometry().width()/(axisY()->ticksCount()-1));
324 324 }
325 325
326 326 void QChart::updateLayout()
327 327 {
328 328 if(!m_rect.isValid()) return;
329 329
330 330 QRectF rect = m_rect.adjusted(margin(),margin(), -margin(), -margin());
331 331
332 332 // recalculate title position
333 333 if (m_titleItem) {
334 334 QPointF center = m_rect.center() -m_titleItem->boundingRect().center();
335 335 m_titleItem->setPos(center.x(),m_rect.top()/2 + margin()/2);
336 336 }
337 337
338 338 //recalculate background gradient
339 339 if (m_backgroundItem) {
340 340 m_backgroundItem->setRect(rect);
341 341 }
342 342
343 343 // recalculate legend position
344 344 // TODO: better layout
345 345 if (m_legend) {
346 346 QRectF boundingRect(m_rect.adjusted(margin(),
347 rect.height() + margin() + margin()/2 - m_legend->minimumSize().height()/2,
347 rect.height() + margin() + margin()/2,
348 348 -margin(),
349 -margin()/2 + m_legend->minimumSize().height()/2));
349 -margin()/2 + m_legend->minimumSize().height()));
350 350 m_legend->handleGeometryChanged(boundingRect);
351 qDebug() << "legend rect:" << m_legend->boundingRect();
352 351 }
353 352 }
354 353 #include "moc_qchart.cpp"
355 354
356 355 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,232 +1,219
1 1 #include "qchartglobal.h"
2 2 #include "qlegend.h"
3 3 #include "qseries.h"
4 4 #include "legendmarker_p.h"
5 5 #include "qxyseries.h"
6 6 #include "qlineseries.h"
7 7 #include "qareaseries.h"
8 8 #include "qscatterseries.h"
9 9 #include "qsplineseries.h"
10 10 #include "qbarseries.h"
11 11 #include "qstackedbarseries.h"
12 12 #include "qpercentbarseries.h"
13 13 #include "qbarset.h"
14 14 #include "qpieseries.h"
15 15 #include "qpieslice.h"
16 16 #include <QPainter>
17 17 #include <QPen>
18 18
19 19 #include <QGraphicsSceneEvent>
20 20
21 21 QTCOMMERCIALCHART_BEGIN_NAMESPACE
22 22
23 23 QLegend::QLegend(QGraphicsItem *parent)
24 24 : QGraphicsObject(parent)
25 25 ,mBoundingRect(0,0,1,1)
26 26 ,mBackgroundBrush(Qt::darkGray) // TODO: from theme?
27 27 ,mMinimumSize(50,20) // TODO: magic numbers
28 28 {
29 29 }
30 30
31 31 void QLegend::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
32 32 {
33 33 painter->setBrush(mBackgroundBrush);
34 34 painter->drawRect(mBoundingRect);
35
36 foreach(LegendMarker* m, mMarkers) {
37 QRectF r = m->boundingRect();
38 painter->setBrush(m->brush());
39 painter->drawText(r.x() + r.width()*2, r.y() + r.height(), m->name());
40 }
41 35 }
42 36
43 37 QRectF QLegend::boundingRect() const
44 38 {
45 39 return mBoundingRect;
46 40 }
47 41
48 42 void QLegend::setBackgroundBrush(const QBrush& brush)
49 43 {
50 44 mBackgroundBrush = brush;
51 45 }
52 46
53 47 QBrush QLegend::backgroundBrush() const
54 48 {
55 49 return mBackgroundBrush;
56 50 }
57 51
58 52 QSizeF QLegend::minimumSize() const
59 53 {
60 54 return mMinimumSize;
61 55 }
62 56
63 57 void QLegend::setMinimumSize(const QSizeF size)
64 58 {
65 59 mMinimumSize = size;
66 60 }
67 61
68 62 void QLegend::handleSeriesAdded(QSeries* series,Domain* domain)
69 63 {
70 64 mSeriesList.append(series);
71 65
72 66 switch (series->type())
73 67 {
74 68 case QSeries::SeriesTypeLine: {
75 69
76 70 QLineSeries* lineSeries = static_cast<QLineSeries*>(series);
77 71 createMarker(lineSeries);
78 72 break;
79 73 }
80 74 case QSeries::SeriesTypeArea: {
81 75
82 76 QAreaSeries* areaSeries = static_cast<QAreaSeries*>(series);
83 77 createMarker(areaSeries->upperSeries());
84 78 if(areaSeries->lowerSeries())
85 79 createMarker(areaSeries->lowerSeries());
86 80 break;
87 81 }
88 82
89 83 case QSeries::SeriesTypeBar: {
90 84
91 85 QBarSeries* barSeries = static_cast<QBarSeries*>(series);
92 86 createMarkers(barSeries);
93 87 break;
94 88 }
95 89
96 90 case QSeries::SeriesTypeStackedBar: {
97 91
98 92 QStackedBarSeries* stackedBarSeries = static_cast<QStackedBarSeries*>(series);
99 93 createMarkers(stackedBarSeries);
100 94 break;
101 95 }
102 96
103 97 case QSeries::SeriesTypePercentBar: {
104 98
105 99 QPercentBarSeries* percentBarSeries = static_cast<QPercentBarSeries*>(series);
106 100 createMarkers(percentBarSeries);
107 101 break;
108 102 }
109 103
110 104 case QSeries::SeriesTypeScatter: {
111 105
112 106 QScatterSeries *scatterSeries = static_cast<QScatterSeries *>(series);
113 107 createMarker(scatterSeries);
114 108 break;
115 109 }
116 110
117 111 case QSeries::SeriesTypePie: {
118 112
119 113 QPieSeries *pieSeries = static_cast<QPieSeries *>(series);
120 114 createMarkers(pieSeries);
121 115 break;
122 116 }
123 117
124 118 case QSeries::SeriesTypeSpline: {
125 119
126 120 QSplineSeries* splineSeries = static_cast<QSplineSeries*>(series);
127 121 createMarker(splineSeries);
128 122 break;
129 123 }
130 124 default: {
131 125 qDebug()<< "QLegend::handleSeriesAdded" << series->type() << "not implemented.";
132 126 break;
133 127 }
134 128 }
135 129
136 130 layoutChanged();
137 131 }
138 132
139 133 void QLegend::handleSeriesRemoved(QSeries* series)
140 134 {
141 135 if (series->type() == QSeries::SeriesTypeArea)
142 136 {
143 137 // This is special case. Area series has upper and lower series, which each have markers
144 138 QAreaSeries* s = static_cast<QAreaSeries*> (series);
145 139 deleteMarkers(s->upperSeries());
146 140 deleteMarkers(s->lowerSeries());
147 141 } else {
148 142 deleteMarkers(series);
149 143 }
150 144
151 145 mSeriesList.removeOne(series);
152 146 layoutChanged();
153 147 }
154 148
155 149 void QLegend::handleGeometryChanged(const QRectF& size)
156 150 {
157 151 mBoundingRect = size;
158 152 layoutChanged();
159 153 }
160 154
161 155 void QLegend::deleteMarkers(QSeries *series)
162 156 {
163 157 // Search all markers that belong to given series and delete them.
164 158 foreach (LegendMarker *m, mMarkers) {
165 159 if (m->series() == series) {
166 160 mMarkers.removeOne(m);
167 161 delete m;
168 162 }
169 163 }
170 164 }
171 165
172 166 void QLegend::createMarker(QXYSeries* series)
173 167 {
174 168 LegendMarker* marker = new LegendMarker(series,this);
175 169 marker->setName(series->name());
176 170 marker->setBrush(series->brush());
177 171 connect(marker,SIGNAL(clicked(QSeries*,Qt::MouseButton)),this,SIGNAL(clicked(QSeries*,Qt::MouseButton)));
178 172 mMarkers.append(marker);
179 173 childItems().append(marker);
180 174 }
181 175
182 176 void QLegend::createMarkers(QBarSeries *series)
183 177 {
184 178 foreach(QBarSet* s, series->barSets()) {
185 179 LegendMarker* marker = new LegendMarker(series,s,this);
186 180 marker->setName(s->name());
187 181 marker->setBrush(s->brush());
188 182 connect(marker,SIGNAL(clicked(QBarSet*,Qt::MouseButton)),this,SIGNAL(clicked(QBarSet*,Qt::MouseButton)));
189 183 mMarkers.append(marker);
190 184 childItems().append(marker);
191 185 }
192 186 }
193 187
194 188 void QLegend::createMarkers(QPieSeries *series)
195 189 {
196 190 foreach(QPieSlice* s, series->slices()) {
197 191 LegendMarker* marker = new LegendMarker(series,s,this);
198 192 marker->setName(s->label());
199 193 marker->setBrush(s->sliceBrush());
200 194 connect(marker,SIGNAL(clicked(QPieSlice*,Qt::MouseButton)),this,SIGNAL(clicked(QPieSlice*,Qt::MouseButton)));
201 195 mMarkers.append(marker);
202 196 childItems().append(marker);
203 197 }
204 198 }
205 199
206 200 void QLegend::layoutChanged()
207 201 {
208 202 // Calculate layout for markers and text
209 203 if (mMarkers.count() <= 0) {
210 204 // Nothing to do
211 205 return;
212 206 }
213 207
214 // TODO: marker defined by series.
215 QSizeF markerSize(10,10);
216
217 // TODO: better layout, this is just concept.
218 // Leave some space around markers like this: | x x x x |
219 208 qreal steps = mMarkers.count();
220
221 209 qreal xStep = mBoundingRect.width() / steps;
222 qreal yStep = mBoundingRect.height() / steps;
223 qreal x = mBoundingRect.x() + 5;
224 qreal y = mBoundingRect.y() + (mBoundingRect.height() - markerSize.height())/2;
210 qreal x=mBoundingRect.x();
211 qreal y = mBoundingRect.y() + (mBoundingRect.height()/4);
225 212 foreach (LegendMarker* m, mMarkers) {
226 m->setBoundingRect(QRectF(x,y,markerSize.width(),markerSize.height()));
213 m->setBoundingRect(QRectF(x,y,xStep,mBoundingRect.height()/2));
227 214 x += xStep;
228 215 }
229 216 }
230 217
231 218 #include "moc_qlegend.cpp"
232 219 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now