##// END OF EJS Templates
Refactors layout managment...
Michal Klocek -
r1534:d9bcc3bd9d82
parent child
Show More
@@ -0,0 +1,144
1 #include "chartlayout_p.h"
2 #include "chartpresenter_p.h"
3 #include "chartaxis_p.h"
4 #include <QDebug>
5 QTCOMMERCIALCHART_BEGIN_NAMESPACE
6
7 ChartLayout::ChartLayout(ChartPresenter* presenter):
8 m_presenter(presenter),
9 m_marginBig(60),
10 m_marginSmall(20),
11 m_marginTiny(10),
12 m_chartMargins(QPointF(m_marginBig,m_marginBig),QPointF(m_marginBig,m_marginBig))
13 {
14
15 }
16
17 ChartLayout::~ChartLayout()
18 {
19
20 }
21
22 void ChartLayout::setGeometry(const QRectF& rect)
23 {
24 if (!rect.isValid()) return;
25
26 QGraphicsLayout::setGeometry(rect);
27
28 // check title size
29
30 QSize titleSize = QSize(0,0);
31
32 if (m_presenter->titleItem()) {
33 titleSize= m_presenter->titleItem()->boundingRect().size().toSize();
34 }
35
36 qreal axisHeight = 0;
37 qreal axisWidth = 0;
38
39 // check axis size
40
41 foreach (ChartAxis* axis,m_presenter->axisItems()){
42 if(axis->axisType() == ChartAxis::X_AXIS)
43 axisHeight = qMax(axis->minimumHeight(),axisHeight);
44 else
45 axisWidth = qMax(axis->minimumWidth(),axisWidth);
46 }
47
48 QLegend* legend = m_presenter->legend();
49
50 qreal titlePadding = m_chartMargins.top()/2;
51
52 QRectF chartMargins = m_chartMargins;
53
54 // recalculate legend position
55 if (legend != 0 && legend->isVisible() && legend->isAttachedToChart()) {
56
57 // Reserve some space for legend
58 switch (legend->alignment()) {
59
60 case Qt::AlignTop: {
61 QSizeF legendSize = legend->effectiveSizeHint(Qt::PreferredSize,QSizeF(rect.width(),-1));
62 int topMargin = 2*m_marginTiny + titleSize.height() + legendSize.height() + m_marginTiny;
63 chartMargins = QRect(QPoint(m_chartMargins.left(),topMargin),QPoint(m_chartMargins.right(),m_chartMargins.bottom()));
64 m_legendMargins = QRect(QPoint(chartMargins.left(),topMargin - (legendSize.height() + m_marginTiny)),QPoint(chartMargins.right(),rect.height()-topMargin + m_marginTiny));
65 titlePadding = m_marginTiny + m_marginTiny;
66 break;
67 }
68 case Qt::AlignBottom: {
69 QSizeF legendSize = legend->effectiveSizeHint(Qt::PreferredSize,QSizeF(rect.width(),-1));
70 int bottomMargin = m_marginTiny + m_marginSmall + legendSize.height() + m_marginTiny + axisHeight;
71 chartMargins = QRect(QPoint(m_chartMargins.left(),m_chartMargins.top()),QPoint(m_chartMargins.right(),bottomMargin));
72 m_legendMargins = QRect(QPoint(chartMargins.left(),rect.height()-bottomMargin + m_marginTiny + axisHeight),QPoint(chartMargins.right(),m_marginTiny + m_marginSmall));
73 titlePadding = chartMargins.top()/2;
74 break;
75 }
76 case Qt::AlignLeft: {
77 QSizeF legendSize = legend->effectiveSizeHint(Qt::PreferredSize,QSizeF(-1,rect.height()));
78 int leftPadding = m_marginTiny + m_marginSmall + legendSize.width() + m_marginTiny + axisWidth;
79 chartMargins = QRect(QPoint(leftPadding,m_chartMargins.top()),QPoint(m_chartMargins.right(),m_chartMargins.bottom()));
80 m_legendMargins = QRect(QPoint(m_marginTiny + m_marginSmall,chartMargins.top()),QPoint(rect.width()-leftPadding + m_marginTiny + axisWidth,chartMargins.bottom()));
81 titlePadding = chartMargins.top()/2;
82 break;
83 }
84 case Qt::AlignRight: {
85 QSizeF legendSize = legend->effectiveSizeHint(Qt::PreferredSize,QSizeF(-1,rect.height()));
86 int rightPadding = m_marginTiny + m_marginSmall + legendSize.width() + m_marginTiny;
87 chartMargins = QRect(QPoint(m_chartMargins.left(),m_chartMargins.top()),QPoint(rightPadding,m_chartMargins.bottom()));
88 m_legendMargins = QRect(QPoint(rect.width()- rightPadding+ m_marginTiny ,chartMargins.top()),QPoint(m_marginTiny + m_marginSmall,chartMargins.bottom()));
89 titlePadding = chartMargins.top()/2;
90 break;
91 }
92 default: {
93 break;
94 }
95 }
96
97 legend->setGeometry(rect.adjusted(m_legendMargins.left(),m_legendMargins.top(),-m_legendMargins.right(),-m_legendMargins.bottom()));
98 }
99
100 // recalculate title position
101 if (m_presenter->titleItem()) {
102 QPointF center = rect.center() - m_presenter->titleItem()->boundingRect().center();
103 m_presenter->titleItem()->setPos(center.x(),titlePadding);
104 }
105
106 //recalculate background gradient
107 if (m_presenter->backgroundItem()) {
108 m_presenter->backgroundItem()->setRect(rect.adjusted(m_marginTiny,m_marginTiny, -m_marginTiny, -m_marginTiny));
109 }
110
111 QRectF chartRect = rect.adjusted(chartMargins.left(),chartMargins.top(),-chartMargins.right(),-chartMargins.bottom());
112
113 if(m_presenter->geometry()!=chartRect && chartRect.isValid()){
114 m_presenter->setGeometry(chartRect);
115 }else if(chartRect.size().isEmpty()){
116 m_presenter->setGeometry(QRect(rect.width()/2,rect.height()/2,1,1));
117 }
118
119 }
120
121
122 QSizeF ChartLayout::sizeHint ( Qt::SizeHint which, const QSizeF & constraint) const
123 {
124 Q_UNUSED(constraint);
125 if(which == Qt::MinimumSize)
126 return QSize(2*(m_chartMargins.top()+m_chartMargins.bottom()),2*(m_chartMargins.top() + m_chartMargins.bottom()));
127 else
128 return QSize(-1,-1);
129 }
130
131 void ChartLayout::setMarginsMinimum(const QRectF& margins)
132 {
133 if(m_chartMargins != margins){
134 m_chartMargins = margins;
135 updateGeometry();
136 }
137 }
138
139 QRectF ChartLayout::margins() const
140 {
141 return m_chartMargins;
142 }
143
144 QTCOMMERCIALCHART_END_NAMESPACE
@@ -0,0 +1,43
1 #ifndef CHARTLAYOUT_H_
2 #define CHARTLAYOUT_H_
3 #include <QGraphicsLayout>
4 #include "qchartglobal.h"
5
6 QTCOMMERCIALCHART_BEGIN_NAMESPACE
7
8 class ChartPresenter;
9
10 class ChartLayout : public QGraphicsLayout
11 {
12 public:
13
14 ChartLayout(ChartPresenter* presenter);
15 virtual ~ChartLayout();
16
17 void setMarginsMinimum(const QRectF& margins);
18 QRectF margins() const;
19
20 void setGeometry(const QRectF& rect);
21
22 protected:
23 QSizeF sizeHint ( Qt::SizeHint which, const QSizeF & constraint = QSizeF() ) const;
24 int count() const { return 0; }
25 QGraphicsLayoutItem* itemAt(int) const { return 0; };
26 void removeAt(int){};
27
28 private:
29 ChartPresenter* m_presenter;
30 int m_marginBig;
31 int m_marginSmall;
32 int m_marginTiny;
33
34 QRectF m_chartMargins;
35 QRectF m_legendMargins;
36
37
38
39 };
40
41 QTCOMMERCIALCHART_END_NAMESPACE
42
43 #endif
@@ -0,0 +1,348
1 #include "legendlayout_p.h"
2 #include "chartpresenter_p.h"
3 #include "legendmarker_p.h"
4 #include "qlegend_p.h"
5 #include <QDebug>
6 QTCOMMERCIALCHART_BEGIN_NAMESPACE
7
8 LegendLayout::LegendLayout(QLegend* legend):
9 m_legend(legend)
10 {
11
12 }
13
14 LegendLayout::~LegendLayout()
15 {
16
17 }
18
19 void LegendLayout::setOffset(qreal x, qreal y)
20 {
21 bool scrollHorizontal = true;
22 switch(m_legend->alignment()) {
23 case Qt::AlignTop:
24 case Qt::AlignBottom: {
25 scrollHorizontal = true;
26 break;
27 }
28 case Qt::AlignLeft:
29 case Qt::AlignRight: {
30 scrollHorizontal = false;
31 break;
32 }
33 }
34
35 // If detached, the scrolling direction is vertical instead of horizontal and vice versa.
36 if (!m_legend->isAttachedToChart()) {
37 scrollHorizontal = !scrollHorizontal;
38 }
39
40 QRectF boundingRect = geometry();
41
42 // Limit offset between m_minOffset and m_maxOffset
43 if (scrollHorizontal) {
44 if(m_width<=boundingRect.width()) return;
45
46 if (x != m_offsetX) {
47 m_offsetX = qBound(m_minOffsetX, x, m_maxOffsetX);
48 m_legend->d_ptr->items()->setPos(-m_offsetX,boundingRect.top());
49 }
50 }
51 else {
52 if(m_height<=boundingRect.height()) return;
53
54 if (y != m_offsetY) {
55 m_offsetY = qBound(m_minOffsetY, y, m_maxOffsetY);
56 m_legend->d_ptr->items()->setPos(boundingRect.left(),-m_offsetY);
57 }
58 }
59 }
60
61 QPointF LegendLayout::offset() const
62 {
63 return QPointF(m_offsetX,m_offsetY);
64 }
65
66 void LegendLayout::setGeometry(const QRectF& rect)
67 {
68 if (!rect.isValid()) return;
69
70 QGraphicsLayout::setGeometry(rect);
71
72 if(m_legend->isAttachedToChart()) {
73 setAttachedGeometry(rect);
74 }
75 else {
76 setDettachedGeometry(rect);
77 }
78 }
79
80 void LegendLayout::setAttachedGeometry(const QRectF& rect)
81 {
82
83 m_offsetX=0;
84 m_offsetY=0;
85
86 QSizeF size(0,0);
87
88 if( m_legend->d_ptr->markers().isEmpty()) return;
89
90 m_width=0;
91 m_height=0;
92
93 switch(m_legend->alignment()) {
94
95 case Qt::AlignTop:
96
97 case Qt::AlignBottom: {
98 QPointF point(0,0);
99 foreach (LegendMarker* marker, m_legend->d_ptr->markers()) {
100 if (marker->isVisible()) {
101 marker->setGeometry(QRectF(QPoint(0,0),marker->effectiveSizeHint(Qt::PreferredSize)));
102 marker->setPos(point.x(),rect.height()/2 - marker->boundingRect().height()/2);
103 const QRectF& rect = marker->boundingRect();
104 size = size.expandedTo(rect.size());
105 qreal w = rect.width();
106 m_width+=w;
107 point.setX(point.x() + w);
108 }
109 }
110 if(m_width<rect.width()) {
111 m_legend->d_ptr->items()->setPos(rect.width()/2-m_width/2,rect.top());
112
113 }
114 else {
115 m_legend->d_ptr->items()->setPos(rect.topLeft());
116 }
117 m_height=size.height();
118 }
119 break;
120 case Qt::AlignLeft:
121 case Qt::AlignRight: {
122 QPointF point(0,0);
123 foreach (LegendMarker* marker, m_legend->d_ptr->markers()) {
124 if (marker->isVisible()) {
125 marker->setGeometry(QRectF(QPoint(0,0),marker->effectiveSizeHint(Qt::PreferredSize)));
126 marker->setPos(point);
127 const QRectF& rect = marker->boundingRect();
128 qreal h = rect.height();
129 size = size.expandedTo(rect.size());
130 m_height+=h;
131 point.setY(point.y() + h);
132 }
133 }
134 if(m_height<rect.height()) {
135 m_legend->d_ptr->items()->setPos(rect.left(),rect.height()/2-m_height/2);
136 }
137 else {
138 m_legend->d_ptr->items()->setPos(rect.topLeft());
139 }
140 m_width=size.width();
141 }
142 break;
143 }
144
145 m_minOffsetX = 0;
146 m_minOffsetY = 0;
147 m_maxOffsetX = m_width - rect.width();
148 m_maxOffsetY = m_height - rect.height();
149 }
150
151 void LegendLayout::setDettachedGeometry(const QRectF& rect)
152 {
153 // Detached layout is different.
154 // In detached mode legend may have multiple rows and columns, so layout calculations
155 // differ a log from attached mode.
156 // Also the scrolling logic is bit different.
157
158 m_offsetX=0;
159 m_offsetY=0;
160
161 QSizeF size(0,0);
162
163 if( m_legend->d_ptr->markers().isEmpty()) return;
164
165 QList<QGraphicsItem *> items = m_legend->d_ptr->items()->childItems();
166
167 switch (m_legend->alignment()) {
168 case Qt::AlignTop: {
169 QPointF point = rect.topLeft();
170 m_width = 0;
171 m_height = 0;
172 for (int i=0; i<items.count(); i++) {
173 QGraphicsItem *item = items.at(i);
174 if (item->isVisible()) {
175 const QRectF& boundingRect = item->boundingRect();
176 qreal w = boundingRect.width();
177 qreal h = boundingRect.height();
178 m_width = qMax(m_width,w);
179 m_height = qMax(m_height,h);
180 item->setPos(point.x(),point.y());
181 point.setX(point.x() + w);
182 if (point.x() + w > rect.topLeft().x() + rect.width()) {
183 // Next item would go off rect.
184 point.setX(rect.topLeft().x());
185 point.setY(point.y() + h);
186 if (i+1 < items.count()) {
187 m_height += h;
188 }
189 }
190 }
191 }
192 m_legend->d_ptr->items()->setPos(rect.topLeft());
193
194 m_minOffsetX = 0;
195 m_minOffsetY = 0;
196 m_maxOffsetX = m_width - rect.width();
197 m_maxOffsetY = m_height - rect.height();
198 }
199 break;
200 case Qt::AlignBottom: {
201 QPointF point = rect.bottomLeft();
202 m_width = 0;
203 m_height = 0;
204 for (int i=0; i<items.count(); i++) {
205 QGraphicsItem *item = items.at(i);
206 if (item->isVisible()) {
207 const QRectF& boundingRect = item->boundingRect();
208 qreal w = boundingRect.width();
209 qreal h = boundingRect.height();
210 m_width = qMax(m_width,w);
211 m_height = qMax(m_height,h);
212 item->setPos(point.x(),point.y() - h);
213 point.setX(point.x() + w);
214 if (point.x() + w > rect.bottomLeft().x() + rect.width()) {
215 // Next item would go off rect.
216 point.setX(rect.bottomLeft().x());
217 point.setY(point.y() - h);
218 if (i+1 < items.count()) {
219 m_height += h;
220 }
221 }
222 }
223 }
224 m_legend->d_ptr->items()->setPos(rect.topLeft());
225
226 m_minOffsetX = 0;
227 m_minOffsetY = qMin(rect.topLeft().y(), rect.topLeft().y() - m_height + rect.height());
228 m_maxOffsetX = m_width - rect.width();
229 m_maxOffsetY = 0;
230 }
231 break;
232 case Qt::AlignLeft: {
233 QPointF point = rect.topLeft();
234 m_width = 0;
235 m_height = 0;
236 qreal maxWidth = 0;
237 for (int i=0; i<items.count(); i++) {
238 QGraphicsItem *item = items.at(i);
239 if (item->isVisible()) {
240 const QRectF& boundingRect = item->boundingRect();
241 qreal w = boundingRect.width();
242 qreal h = boundingRect.height();
243 m_height = qMax(m_height,h);
244 maxWidth = qMax(maxWidth,w);
245 item->setPos(point.x(),point.y());
246 point.setY(point.y() + h);
247 if (point.y() + h > rect.topLeft().y() + rect.height()) {
248 // Next item would go off rect.
249 point.setX(point.x() + maxWidth);
250 point.setY(rect.topLeft().y());
251 if (i+1 < items.count()) {
252 m_width += maxWidth;
253 maxWidth = 0;
254 }
255 }
256 }
257 }
258 m_width += maxWidth;
259 m_legend->d_ptr->items()->setPos(rect.topLeft());
260
261 m_minOffsetX = 0;
262 m_minOffsetY = 0;
263 m_maxOffsetX = m_width - rect.width();
264 m_maxOffsetY = m_height - rect.height();
265 }
266 break;
267 case Qt::AlignRight: {
268 QPointF point = rect.topRight();
269 m_width = 0;
270 m_height = 0;
271 qreal maxWidth = 0;
272 for (int i=0; i<items.count(); i++) {
273 QGraphicsItem *item = items.at(i);
274 if (item->isVisible()) {
275 const QRectF& boundingRect = item->boundingRect();
276 qreal w = boundingRect.width();
277 qreal h = boundingRect.height();
278 m_height = qMax(m_height,h);
279 maxWidth = qMax(maxWidth,w);
280 item->setPos(point.x() - w,point.y());
281 point.setY(point.y() + h);
282 if (point.y() + h > rect.topLeft().y() + rect.height()) {
283 // Next item would go off rect.
284 point.setX(point.x() - maxWidth);
285 point.setY(rect.topLeft().y());
286 if (i+1 < items.count()) {
287 m_width += maxWidth;
288 maxWidth = 0;
289 }
290 }
291 }
292 }
293 m_width += maxWidth;
294 m_legend->d_ptr->items()->setPos(rect.topLeft());
295
296 m_minOffsetX = qMin(rect.topLeft().x(), rect.topLeft().x() - m_width + rect.width());
297 m_minOffsetY = 0;
298 m_maxOffsetX = 0;
299 m_maxOffsetY = m_height - rect.height();
300 }
301 break;
302 default:
303 break;
304 }
305
306 }
307
308 QSizeF LegendLayout::sizeHint ( Qt::SizeHint which, const QSizeF & constraint) const
309 {
310 QSizeF size(0, 0);
311 qreal left, top, right, bottom;
312 getContentsMargins(&left, &top, &right, &bottom);
313
314 if(constraint.isValid()) {
315 foreach(LegendMarker* marker, m_legend->d_ptr->markers()) {
316 size = size.expandedTo(marker->effectiveSizeHint(which));
317 }
318 size = size.boundedTo(constraint);
319 }
320 else if (constraint.width() >= 0) {
321 qreal width = 0;
322 qreal height = 0;
323 foreach(LegendMarker* marker, m_legend->d_ptr->markers()) {
324 width+=marker->effectiveSizeHint(which).width();
325 height=qMax(height,marker->effectiveSizeHint(which).height());
326 }
327
328 size = QSizeF(qMin(constraint.width(),width), height);
329 }
330 else if (constraint.height() >= 0) {
331 qreal width = 0;
332 qreal height = 0;
333 foreach(LegendMarker* marker, m_legend->d_ptr->markers()) {
334 width=qMax(width,marker->effectiveSizeHint(which).width());
335 height+=height,marker->effectiveSizeHint(which).height();
336 }
337 size = QSizeF(width,qMin(constraint.height(),height));
338 }
339 else {
340 foreach(LegendMarker* marker, m_legend->d_ptr->markers()) {
341 size = size.expandedTo(marker->effectiveSizeHint(which));
342 }
343 }
344 size += QSize(left + right, top + bottom);
345 return size;
346 }
347
348 QTCOMMERCIALCHART_END_NAMESPACE
@@ -0,0 +1,50
1 #ifndef LEGENDLAYOUT_H_
2 #define LEGENDLAYOUT_H_
3 #include <QGraphicsLayout>
4 #include "qchartglobal.h"
5
6 QTCOMMERCIALCHART_BEGIN_NAMESPACE
7
8 class QLegend;
9
10 class LegendLayout : public QGraphicsLayout
11 {
12 public:
13
14 LegendLayout(QLegend* legend);
15 virtual ~LegendLayout();
16
17 void setGeometry(const QRectF& rect);
18
19 void setOffset(qreal x, qreal y);
20 QPointF offset() const;
21
22 protected:
23 QSizeF sizeHint ( Qt::SizeHint which, const QSizeF & constraint = QSizeF() ) const;
24 int count() const { return 0; }
25 QGraphicsLayoutItem* itemAt(int) const { return 0; };
26 void removeAt(int){};
27
28 private:
29 void setAttachedGeometry(const QRectF& rect);
30 void setDettachedGeometry(const QRectF& rect);
31
32 private:
33 QLegend* m_legend;
34 int m_marginBig;
35 int m_marginSmall;
36 int m_marginTiny;
37 qreal m_offsetX;
38 qreal m_offsetY;
39 qreal m_minOffsetX;
40 qreal m_minOffsetY;
41 qreal m_maxOffsetX;
42 qreal m_maxOffsetY;
43 qreal m_width;
44 qreal m_height;
45
46 };
47
48 QTCOMMERCIALCHART_END_NAMESPACE
49
50 #endif
@@ -1,261 +1,261
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "mainwidget.h"
21 #include "mainwidget.h"
22 #include <QChart>
22 #include <QChart>
23 #include <QChartView>
23 #include <QChartView>
24 #include <QPushButton>
24 #include <QPushButton>
25 #include <QLabel>
25 #include <QLabel>
26 #include <QDebug>
26 #include <QDebug>
27 #include <QBarSet>
27 #include <QBarSet>
28 #include <QBarSeries>
28 #include <QBarSeries>
29 #include <QLegend>
29 #include <QLegend>
30 #include <QFormLayout>
30 #include <QFormLayout>
31
31
32 QTCOMMERCIALCHART_USE_NAMESPACE
32 QTCOMMERCIALCHART_USE_NAMESPACE
33
33
34 MainWidget::MainWidget(QWidget *parent) :
34 MainWidget::MainWidget(QWidget *parent) :
35 QWidget(parent)
35 QWidget(parent)
36 {
36 {
37 // Create buttons for ui
37 // Create buttons for ui
38 m_buttonLayout = new QGridLayout();
38 m_buttonLayout = new QGridLayout();
39 QPushButton *detachLegendButton = new QPushButton("detach legend");
39 QPushButton *detachLegendButton = new QPushButton("detach legend");
40 connect(detachLegendButton, SIGNAL(clicked()), this, SLOT(detachLegend()));
40 connect(detachLegendButton, SIGNAL(clicked()), this, SLOT(detachLegend()));
41 m_buttonLayout->addWidget(detachLegendButton, 0, 0);
41 m_buttonLayout->addWidget(detachLegendButton, 0, 0);
42 QPushButton *attachLegendButton = new QPushButton("attach legend");
42 QPushButton *attachLegendButton = new QPushButton("attach legend");
43 connect(attachLegendButton, SIGNAL(clicked()), this, SLOT(attachLegend()));
43 connect(attachLegendButton, SIGNAL(clicked()), this, SLOT(attachLegend()));
44 m_buttonLayout->addWidget(attachLegendButton, 1, 0);
44 m_buttonLayout->addWidget(attachLegendButton, 1, 0);
45
45
46 QPushButton *addSetButton = new QPushButton("add barset");
46 QPushButton *addSetButton = new QPushButton("add barset");
47 connect(addSetButton, SIGNAL(clicked()), this, SLOT(addBarset()));
47 connect(addSetButton, SIGNAL(clicked()), this, SLOT(addBarset()));
48 m_buttonLayout->addWidget(addSetButton, 2, 0);
48 m_buttonLayout->addWidget(addSetButton, 2, 0);
49 QPushButton *removeBarsetButton = new QPushButton("remove barset");
49 QPushButton *removeBarsetButton = new QPushButton("remove barset");
50 connect(removeBarsetButton, SIGNAL(clicked()), this, SLOT(removeBarset()));
50 connect(removeBarsetButton, SIGNAL(clicked()), this, SLOT(removeBarset()));
51 m_buttonLayout->addWidget(removeBarsetButton, 3, 0);
51 m_buttonLayout->addWidget(removeBarsetButton, 3, 0);
52
52
53 QPushButton *leftButton = new QPushButton("Align legend left");
53 QPushButton *leftButton = new QPushButton("Align legend left");
54 connect(leftButton, SIGNAL(clicked()), this, SLOT(setLegendLeft()));
54 connect(leftButton, SIGNAL(clicked()), this, SLOT(setLegendLeft()));
55 m_buttonLayout->addWidget(leftButton, 4, 0);
55 m_buttonLayout->addWidget(leftButton, 4, 0);
56
56
57 QPushButton *rightButton = new QPushButton("Align legend right");
57 QPushButton *rightButton = new QPushButton("Align legend right");
58 connect(rightButton, SIGNAL(clicked()), this, SLOT(setLegendRight()));
58 connect(rightButton, SIGNAL(clicked()), this, SLOT(setLegendRight()));
59 m_buttonLayout->addWidget(rightButton, 5, 0);
59 m_buttonLayout->addWidget(rightButton, 5, 0);
60
60
61 QPushButton *topButton = new QPushButton("Align legend top");
61 QPushButton *topButton = new QPushButton("Align legend top");
62 connect(topButton, SIGNAL(clicked()), this, SLOT(setLegendTop()));
62 connect(topButton, SIGNAL(clicked()), this, SLOT(setLegendTop()));
63 m_buttonLayout->addWidget(topButton, 6, 0);
63 m_buttonLayout->addWidget(topButton, 6, 0);
64
64
65 QPushButton *bottomButton = new QPushButton("Align legend bottom");
65 QPushButton *bottomButton = new QPushButton("Align legend bottom");
66 connect(bottomButton, SIGNAL(clicked()), this, SLOT(setLegendBottom()));
66 connect(bottomButton, SIGNAL(clicked()), this, SLOT(setLegendBottom()));
67 m_buttonLayout->addWidget(bottomButton, 7, 0);
67 m_buttonLayout->addWidget(bottomButton, 7, 0);
68
68
69 QPushButton *boldButton = new QPushButton("Toggle bold");
69 QPushButton *boldButton = new QPushButton("Toggle bold");
70 connect(boldButton, SIGNAL(clicked()), this, SLOT(toggleBold()));
70 connect(boldButton, SIGNAL(clicked()), this, SLOT(toggleBold()));
71 m_buttonLayout->addWidget(boldButton, 8, 0);
71 m_buttonLayout->addWidget(boldButton, 8, 0);
72
72
73 QPushButton *italicButton = new QPushButton("Toggle italic");
73 QPushButton *italicButton = new QPushButton("Toggle italic");
74 connect(italicButton, SIGNAL(clicked()), this, SLOT(toggleItalic()));
74 connect(italicButton, SIGNAL(clicked()), this, SLOT(toggleItalic()));
75 m_buttonLayout->addWidget(italicButton, 9, 0);
75 m_buttonLayout->addWidget(italicButton, 9, 0);
76
76
77 m_legendPosX = new QDoubleSpinBox();
77 m_legendPosX = new QDoubleSpinBox();
78 m_legendPosY = new QDoubleSpinBox();
78 m_legendPosY = new QDoubleSpinBox();
79 m_legendWidth = new QDoubleSpinBox();
79 m_legendWidth = new QDoubleSpinBox();
80 m_legendHeight = new QDoubleSpinBox();
80 m_legendHeight = new QDoubleSpinBox();
81
81
82 connect(m_legendPosX, SIGNAL(valueChanged(double)), this, SLOT(updateLegendLayout()));
82 connect(m_legendPosX, SIGNAL(valueChanged(double)), this, SLOT(updateLegendLayout()));
83 connect(m_legendPosY, SIGNAL(valueChanged(double)), this, SLOT(updateLegendLayout()));
83 connect(m_legendPosY, SIGNAL(valueChanged(double)), this, SLOT(updateLegendLayout()));
84 connect(m_legendWidth, SIGNAL(valueChanged(double)), this, SLOT(updateLegendLayout()));
84 connect(m_legendWidth, SIGNAL(valueChanged(double)), this, SLOT(updateLegendLayout()));
85 connect(m_legendHeight, SIGNAL(valueChanged(double)), this, SLOT(updateLegendLayout()));
85 connect(m_legendHeight, SIGNAL(valueChanged(double)), this, SLOT(updateLegendLayout()));
86
86
87 QFormLayout* legendLayout = new QFormLayout();
87 QFormLayout* legendLayout = new QFormLayout();
88 legendLayout->addRow("Horizontal position", m_legendPosX);
88 legendLayout->addRow("Horizontal position", m_legendPosX);
89 legendLayout->addRow("Vertical position", m_legendPosY);
89 legendLayout->addRow("Vertical position", m_legendPosY);
90 legendLayout->addRow("Width", m_legendWidth);
90 legendLayout->addRow("Width", m_legendWidth);
91 legendLayout->addRow("Height", m_legendHeight);
91 legendLayout->addRow("Height", m_legendHeight);
92 m_legendSettings = new QGroupBox("Detached legend");
92 m_legendSettings = new QGroupBox("Detached legend");
93 m_legendSettings->setLayout(legendLayout);
93 m_legendSettings->setLayout(legendLayout);
94 m_buttonLayout->addWidget(m_legendSettings);
94 m_buttonLayout->addWidget(m_legendSettings);
95 m_legendSettings->setVisible(false);
95 m_legendSettings->setVisible(false);
96
96
97 // Create chart view with the chart
97 // Create chart view with the chart
98 m_chart = new QChart();
98 m_chart = new QChart();
99 m_chartView = new QChartView(m_chart, this);
99 m_chartView = new QChartView(m_chart, this);
100
100
101 // Create spinbox to modify font size
101 // Create spinbox to modify font size
102 m_fontSize = new QDoubleSpinBox();
102 m_fontSize = new QDoubleSpinBox();
103 m_fontSize->setValue(m_chart->legend()->font().pointSizeF());
103 m_fontSize->setValue(m_chart->legend()->font().pointSizeF());
104 connect(m_fontSize, SIGNAL(valueChanged(double)), this, SLOT(fontSizeChanged()));
104 connect(m_fontSize, SIGNAL(valueChanged(double)), this, SLOT(fontSizeChanged()));
105
105
106 QFormLayout* fontLayout = new QFormLayout();
106 QFormLayout* fontLayout = new QFormLayout();
107 fontLayout->addRow("Legend font size", m_fontSize);
107 fontLayout->addRow("Legend font size", m_fontSize);
108
108
109 // Create layout for grid and detached legend
109 // Create layout for grid and detached legend
110 m_mainLayout = new QGridLayout();
110 m_mainLayout = new QGridLayout();
111 m_mainLayout->addLayout(m_buttonLayout, 0, 0);
111 m_mainLayout->addLayout(m_buttonLayout, 0, 0);
112 m_mainLayout->addLayout(fontLayout,1,0);
112 m_mainLayout->addLayout(fontLayout,1,0);
113 m_mainLayout->addWidget(m_chartView, 0, 1, 3, 1);
113 m_mainLayout->addWidget(m_chartView, 0, 1, 3, 1);
114 setLayout(m_mainLayout);
114 setLayout(m_mainLayout);
115
115
116 createSeries();
116 createSeries();
117 }
117 }
118
118
119 void MainWidget::createSeries()
119 void MainWidget::createSeries()
120 {
120 {
121 m_series = new QBarSeries();
121 m_series = new QBarSeries();
122 addBarset();
122 addBarset();
123 addBarset();
123 addBarset();
124 addBarset();
124 addBarset();
125 addBarset();
125 addBarset();
126
126
127 m_chart->addSeries(m_series);
127 m_chart->addSeries(m_series);
128 m_chart->setTitle("Legend detach example");
128 m_chart->setTitle("Legend detach example");
129 //![1]
129 //![1]
130 m_chart->legend()->setVisible(true);
130 m_chart->legend()->setVisible(true);
131 m_chart->legend()->setAlignment(Qt::AlignBottom);
131 m_chart->legend()->setAlignment(Qt::AlignBottom);
132 //![1]
132 //![1]
133
133
134 m_chart->axisY()->setNiceNumbersEnabled(true);
134 m_chart->axisY()->setNiceNumbersEnabled(true);
135 m_chartView->setRenderHint(QPainter::Antialiasing);
135 m_chartView->setRenderHint(QPainter::Antialiasing);
136 }
136 }
137
137
138 void MainWidget::showLegendSpinbox()
138 void MainWidget::showLegendSpinbox()
139 {
139 {
140 m_legendSettings->setVisible(true);
140 m_legendSettings->setVisible(true);
141 QRectF chartViewRect = m_chartView->rect();
141 QRectF chartViewRect = m_chartView->rect();
142
142
143 m_legendPosX->setMinimum(0);
143 m_legendPosX->setMinimum(0);
144 m_legendPosX->setMaximum(chartViewRect.width());
144 m_legendPosX->setMaximum(chartViewRect.width());
145 m_legendPosX->setValue(150);
145 m_legendPosX->setValue(150);
146
146
147 m_legendPosY->setMinimum(0);
147 m_legendPosY->setMinimum(0);
148 m_legendPosY->setMaximum(chartViewRect.height());
148 m_legendPosY->setMaximum(chartViewRect.height());
149 m_legendPosY->setValue(150);
149 m_legendPosY->setValue(150);
150
150
151 m_legendWidth->setMinimum(0);
151 m_legendWidth->setMinimum(0);
152 m_legendWidth->setMaximum(chartViewRect.width());
152 m_legendWidth->setMaximum(chartViewRect.width());
153 m_legendWidth->setValue(150);
153 m_legendWidth->setValue(150);
154
154
155 m_legendHeight->setMinimum(0);
155 m_legendHeight->setMinimum(0);
156 m_legendHeight->setMaximum(chartViewRect.height());
156 m_legendHeight->setMaximum(chartViewRect.height());
157 m_legendHeight->setValue(75);
157 m_legendHeight->setValue(75);
158 }
158 }
159
159
160 void MainWidget::hideLegendSpinbox()
160 void MainWidget::hideLegendSpinbox()
161 {
161 {
162 m_legendSettings->setVisible(false);
162 m_legendSettings->setVisible(false);
163 }
163 }
164
164
165
165
166 void MainWidget::detachLegend()
166 void MainWidget::detachLegend()
167 {
167 {
168 //![2]
168 //![2]
169 QLegend *legend = m_chart->legend();
169 QLegend *legend = m_chart->legend();
170 legend->detachFromChart();
170 legend->detachFromChart();
171
171
172 m_chart->legend()->setBackgroundVisible(true);
172 m_chart->legend()->setBackgroundVisible(true);
173 m_chart->legend()->setBrush(QBrush(QColor(128,128,128,128)));
173 m_chart->legend()->setBrush(QBrush(QColor(128,128,128,128)));
174 m_chart->legend()->setPen(QPen(QColor(192,192,192,192)));
174 m_chart->legend()->setPen(QPen(QColor(192,192,192,192)));
175 //![2]
175 //![2]
176
176
177 showLegendSpinbox();
177 showLegendSpinbox();
178 updateLegendLayout();
178 updateLegendLayout();
179 update();
179 update();
180 }
180 }
181
181
182
182
183 void MainWidget::attachLegend()
183 void MainWidget::attachLegend()
184 {
184 {
185 //![3]
185 //![3]
186 QLegend *legend = m_chart->legend();
186 QLegend *legend = m_chart->legend();
187 legend->attachToChart();
187 legend->attachToChart();
188 m_chart->legend()->setBackgroundVisible(false);
188 m_chart->legend()->setBackgroundVisible(false);
189 //![3]
189 //![3]
190
190
191 hideLegendSpinbox();
191 hideLegendSpinbox();
192 update();
192 update();
193 }
193 }
194
194
195 void MainWidget::addBarset()
195 void MainWidget::addBarset()
196 {
196 {
197 QBarSet *barSet = new QBarSet(QString("set ") + QString::number(m_series->count()));
197 QBarSet *barSet = new QBarSet(QString("set ") + QString::number(m_series->count()));
198 qreal delta = m_series->count() * 0.1;
198 qreal delta = m_series->count() * 0.1;
199 *barSet << QPointF(0.0 + delta, 1 + delta) << QPointF(1.0 + delta, 2 + delta) << QPointF(2.0 + delta, 3 + delta) << QPointF(3.0 + delta, 4 + delta);
199 *barSet << QPointF(0.0 + delta, 1 + delta) << QPointF(1.0 + delta, 2 + delta) << QPointF(2.0 + delta, 3 + delta) << QPointF(3.0 + delta, 4 + delta);
200 m_series->append(barSet);
200 m_series->append(barSet);
201 }
201 }
202
202
203 void MainWidget::removeBarset()
203 void MainWidget::removeBarset()
204 {
204 {
205 QList<QBarSet*> sets = m_series->barSets();
205 QList<QBarSet*> sets = m_series->barSets();
206 if (sets.count() > 0) {
206 if (sets.count() > 0) {
207 m_series->remove(sets.at(sets.count()-1));
207 m_series->remove(sets.at(sets.count()-1));
208 }
208 }
209 }
209 }
210
210
211 void MainWidget::setLegendLeft()
211 void MainWidget::setLegendLeft()
212 {
212 {
213 m_chart->legend()->setAlignment(Qt::AlignLeft);
213 m_chart->legend()->setAlignment(Qt::AlignLeft);
214 }
214 }
215
215
216 void MainWidget::setLegendRight()
216 void MainWidget::setLegendRight()
217 {
217 {
218 m_chart->legend()->setAlignment(Qt::AlignRight);
218 m_chart->legend()->setAlignment(Qt::AlignRight);
219 }
219 }
220
220
221 void MainWidget::setLegendTop()
221 void MainWidget::setLegendTop()
222 {
222 {
223 m_chart->legend()->setAlignment(Qt::AlignTop);
223 m_chart->legend()->setAlignment(Qt::AlignTop);
224 }
224 }
225
225
226 void MainWidget::setLegendBottom()
226 void MainWidget::setLegendBottom()
227 {
227 {
228 m_chart->legend()->setAlignment(Qt::AlignBottom);
228 m_chart->legend()->setAlignment(Qt::AlignBottom);
229 }
229 }
230
230
231 void MainWidget::toggleBold()
231 void MainWidget::toggleBold()
232 {
232 {
233 QFont font = m_chart->legend()->font();
233 QFont font = m_chart->legend()->font();
234 font.setBold(!font.bold());
234 font.setBold(!font.bold());
235 m_chart->legend()->setFont(font);
235 m_chart->legend()->setFont(font);
236 }
236 }
237
237
238 void MainWidget::toggleItalic()
238 void MainWidget::toggleItalic()
239 {
239 {
240 QFont font = m_chart->legend()->font();
240 QFont font = m_chart->legend()->font();
241 font.setItalic(!font.italic());
241 font.setItalic(!font.italic());
242 m_chart->legend()->setFont(font);
242 m_chart->legend()->setFont(font);
243 }
243 }
244
244
245 void MainWidget::fontSizeChanged()
245 void MainWidget::fontSizeChanged()
246 {
246 {
247 QFont font = m_chart->legend()->font();
247 QFont font = m_chart->legend()->font();
248 font.setPointSizeF(m_fontSize->value());
248 font.setPointSizeF(m_fontSize->value());
249 m_chart->legend()->setFont(font);
249 m_chart->legend()->setFont(font);
250 }
250 }
251
251
252 void MainWidget::updateLegendLayout()
252 void MainWidget::updateLegendLayout()
253 {
253 {
254 //![4]
254 //![4]
255 m_chart->legend()->setGeometry(m_legendPosX->value()
255 m_chart->legend()->setGeometry(QRectF(m_legendPosX->value()
256 ,m_legendPosY->value()
256 ,m_legendPosY->value()
257 ,m_legendWidth->value()
257 ,m_legendWidth->value()
258 ,m_legendHeight->value());
258 ,m_legendHeight->value()));
259 m_chart->legend()->update();
259 m_chart->legend()->update();
260 //![4]
260 //![4]
261 }
261 }
@@ -1,361 +1,381
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "chartaxis_p.h"
21 #include "chartaxis_p.h"
22 #include "qaxis.h"
22 #include "qaxis.h"
23 #include "qaxis_p.h"
23 #include "qaxis_p.h"
24 #include "qaxiscategories_p.h"
24 #include "qaxiscategories_p.h"
25 #include "chartpresenter_p.h"
25 #include "chartpresenter_p.h"
26 #include "chartanimator_p.h"
26 #include "chartanimator_p.h"
27 #include <QPainter>
27 #include <QPainter>
28 #include <QDebug>
28 #include <QDebug>
29 #include <cmath>
29 #include <cmath>
30
30
31 QTCOMMERCIALCHART_BEGIN_NAMESPACE
31 QTCOMMERCIALCHART_BEGIN_NAMESPACE
32
32
33 ChartAxis::ChartAxis(QAxis *axis,ChartPresenter *presenter) : Chart(presenter),
33 ChartAxis::ChartAxis(QAxis *axis,ChartPresenter *presenter) : Chart(presenter),
34 m_chartAxis(axis),
34 m_chartAxis(axis),
35 m_labelsAngle(0),
35 m_labelsAngle(0),
36 m_grid(new QGraphicsItemGroup(presenter->rootItem())),
36 m_grid(new QGraphicsItemGroup(presenter->rootItem())),
37 m_shades(new QGraphicsItemGroup(presenter->rootItem())),
37 m_shades(new QGraphicsItemGroup(presenter->rootItem())),
38 m_labels(new QGraphicsItemGroup(presenter->rootItem())),
38 m_labels(new QGraphicsItemGroup(presenter->rootItem())),
39 m_axis(new QGraphicsItemGroup(presenter->rootItem())),
39 m_axis(new QGraphicsItemGroup(presenter->rootItem())),
40 m_min(0),
40 m_min(0),
41 m_max(0),
41 m_max(0),
42 m_ticksCount(0),
42 m_ticksCount(0),
43 m_animation(0)
43 m_animation(0),
44 m_minWidth(0),
45 m_minHeight(0)
44 {
46 {
45 //initial initialization
47 //initial initialization
46 m_axis->setZValue(ChartPresenter::AxisZValue);
48 m_axis->setZValue(ChartPresenter::AxisZValue);
47 m_axis->setHandlesChildEvents(false);
49 m_axis->setHandlesChildEvents(false);
48
50
49 m_shades->setZValue(ChartPresenter::ShadesZValue);
51 m_shades->setZValue(ChartPresenter::ShadesZValue);
50 m_grid->setZValue(ChartPresenter::GridZValue);
52 m_grid->setZValue(ChartPresenter::GridZValue);
51
53
52 QObject::connect(m_chartAxis->d_ptr.data(),SIGNAL(updated()),this,SLOT(handleAxisUpdated()));
54 QObject::connect(m_chartAxis->d_ptr.data(),SIGNAL(updated()),this,SLOT(handleAxisUpdated()));
53 QObject::connect(m_chartAxis->categories()->d_ptr.data(),SIGNAL(updated()),this,SLOT(handleAxisCategoriesUpdated()));
55 QObject::connect(m_chartAxis->categories()->d_ptr.data(),SIGNAL(updated()),this,SLOT(handleAxisCategoriesUpdated()));
54
56
57 QGraphicsSimpleTextItem item;
58 m_font = item.font();
59
55 handleAxisUpdated();
60 handleAxisUpdated();
56 }
61 }
57
62
58 ChartAxis::~ChartAxis()
63 ChartAxis::~ChartAxis()
59 {
64 {
60 }
65 }
61
66
62 void ChartAxis::setAnimation(AxisAnimation* animation)
67 void ChartAxis::setAnimation(AxisAnimation* animation)
63 {
68 {
64 m_animation=animation;
69 m_animation=animation;
65 }
70 }
66
71
67 void ChartAxis::setLayout(QVector<qreal> &layout)
72 void ChartAxis::setLayout(QVector<qreal> &layout)
68 {
73 {
69 m_layoutVector=layout;
74 m_layoutVector=layout;
70 }
75 }
71
76
72 void ChartAxis::createItems(int count)
77 void ChartAxis::createItems(int count)
73 {
78 {
74 if (m_axis->children().size() == 0)
79 if (m_axis->children().size() == 0)
75 m_axis->addToGroup(new AxisItem(this));
80 m_axis->addToGroup(new AxisItem(this));
76 for (int i = 0; i < count; ++i) {
81 for (int i = 0; i < count; ++i) {
77 m_grid->addToGroup(new QGraphicsLineItem());
82 m_grid->addToGroup(new QGraphicsLineItem());
78 m_labels->addToGroup(new QGraphicsSimpleTextItem());
83 m_labels->addToGroup(new QGraphicsSimpleTextItem());
79 m_axis->addToGroup(new QGraphicsLineItem());
84 m_axis->addToGroup(new QGraphicsLineItem());
80 if ((m_grid->childItems().size())%2 && m_grid->childItems().size()>2) m_shades->addToGroup(new QGraphicsRectItem());
85 if ((m_grid->childItems().size())%2 && m_grid->childItems().size()>2) m_shades->addToGroup(new QGraphicsRectItem());
81 }
86 }
82 }
87 }
83
88
84 void ChartAxis::deleteItems(int count)
89 void ChartAxis::deleteItems(int count)
85 {
90 {
86 QList<QGraphicsItem *> lines = m_grid->childItems();
91 QList<QGraphicsItem *> lines = m_grid->childItems();
87 QList<QGraphicsItem *> labels = m_labels->childItems();
92 QList<QGraphicsItem *> labels = m_labels->childItems();
88 QList<QGraphicsItem *> shades = m_shades->childItems();
93 QList<QGraphicsItem *> shades = m_shades->childItems();
89 QList<QGraphicsItem *> axis = m_axis->childItems();
94 QList<QGraphicsItem *> axis = m_axis->childItems();
90
95
91 for (int i = 0; i < count; ++i) {
96 for (int i = 0; i < count; ++i) {
92 if (lines.size()%2 && lines.size() > 1) delete(shades.takeLast());
97 if (lines.size()%2 && lines.size() > 1) delete(shades.takeLast());
93 delete(lines.takeLast());
98 delete(lines.takeLast());
94 delete(labels.takeLast());
99 delete(labels.takeLast());
95 delete(axis.takeLast());
100 delete(axis.takeLast());
96 }
101 }
97 }
102 }
98
103
99 void ChartAxis::updateLayout(QVector<qreal> &layout)
104 void ChartAxis::updateLayout(QVector<qreal> &layout)
100 {
105 {
101 int diff = m_layoutVector.size() - layout.size();
106 int diff = m_layoutVector.size() - layout.size();
102
107
103 if (diff>0) {
108 if (diff>0) {
104 deleteItems(diff);
109 deleteItems(diff);
105 }
110 }
106 else if (diff<0) {
111 else if (diff<0) {
107 createItems(-diff);
112 createItems(-diff);
108 }
113 }
109
114
110 if( diff!=0) handleAxisUpdated();
115 if( diff!=0) handleAxisUpdated();
111
116
112 if (m_animation) {
117 if (m_animation) {
113 switch(presenter()->state()){
118 switch(presenter()->state()){
114 case ChartPresenter::ZoomInState:
119 case ChartPresenter::ZoomInState:
115 m_animation->setAnimationType(AxisAnimation::ZoomInAnimation);
120 m_animation->setAnimationType(AxisAnimation::ZoomInAnimation);
116 m_animation->setAnimationPoint(presenter()->statePoint());
121 m_animation->setAnimationPoint(presenter()->statePoint());
117 break;
122 break;
118 case ChartPresenter::ZoomOutState:
123 case ChartPresenter::ZoomOutState:
119 m_animation->setAnimationType(AxisAnimation::ZoomOutAnimation);
124 m_animation->setAnimationType(AxisAnimation::ZoomOutAnimation);
120 m_animation->setAnimationPoint(presenter()->statePoint());
125 m_animation->setAnimationPoint(presenter()->statePoint());
121 break;
126 break;
122 case ChartPresenter::ScrollUpState:
127 case ChartPresenter::ScrollUpState:
123 case ChartPresenter::ScrollLeftState:
128 case ChartPresenter::ScrollLeftState:
124 m_animation->setAnimationType(AxisAnimation::MoveBackwordAnimation);
129 m_animation->setAnimationType(AxisAnimation::MoveBackwordAnimation);
125 break;
130 break;
126 case ChartPresenter::ScrollDownState:
131 case ChartPresenter::ScrollDownState:
127 case ChartPresenter::ScrollRightState:
132 case ChartPresenter::ScrollRightState:
128 m_animation->setAnimationType(AxisAnimation::MoveForwardAnimation);
133 m_animation->setAnimationType(AxisAnimation::MoveForwardAnimation);
129 break;
134 break;
130 case ChartPresenter::ShowState:
135 case ChartPresenter::ShowState:
131 m_animation->setAnimationType(AxisAnimation::DefaultAnimation);
136 m_animation->setAnimationType(AxisAnimation::DefaultAnimation);
132 break;
137 break;
133 }
138 }
134 m_animation->setValues(m_layoutVector,layout);
139 m_animation->setValues(m_layoutVector,layout);
135 presenter()->startAnimation(m_animation);
140 presenter()->startAnimation(m_animation);
136 }
141 }
137 else {
142 else {
138 setLayout(layout);
143 setLayout(layout);
139 updateGeometry();
144 updateGeometry();
140 }
145 }
141 }
146 }
142
147
143 bool ChartAxis::createLabels(QStringList &labels,qreal min, qreal max,int ticks) const
148 bool ChartAxis::createLabels(QStringList &labels,qreal min, qreal max,int ticks) const
144 {
149 {
145 Q_ASSERT(max>min);
150 Q_ASSERT(max>min);
146 Q_ASSERT(ticks>1);
151 Q_ASSERT(ticks>1);
147
152
148 QAxisCategories* categories = m_chartAxis->categories();
153 QAxisCategories* categories = m_chartAxis->categories();
149
154
150 bool category = categories->count()>0;
155 bool category = categories->count()>0;
151
156
152 if (!category) {
157 if (!category) {
153 int n = qMax(int(-floor(log10((max-min)/(ticks-1)))),0);
158 int n = qMax(int(-floor(log10((max-min)/(ticks-1)))),0);
154 n++;
159 n++;
155 for (int i=0; i< ticks; i++) {
160 for (int i=0; i< ticks; i++) {
156 qreal value = min + (i * (max - min)/ (ticks-1));
161 qreal value = min + (i * (max - min)/ (ticks-1));
157 Q_UNUSED(value);
162 Q_UNUSED(value);
158 labels << QString::number(value,'f',n);
163 labels << QString::number(value,'f',n);
159 }
164 }
160 } else {
165 } else {
161 QList<qreal> values = categories->values();
166 QList<qreal> values = categories->values();
162 for (int i=0; i< ticks; i++) {
167 for (int i=0; i< ticks; i++) {
163 qreal value = (min + (i * (max - min)/ (ticks-1)));
168 qreal value = (min + (i * (max - min)/ (ticks-1)));
164 int j=0;
169 int j=0;
165 for (; j<values.count(); j++) {
170 for (; j<values.count(); j++) {
166 if (values.at(j) > value) break;
171 if (values.at(j) > value) break;
167 }
172 }
168 if (j!=0) value=values.at(j-1);
173 if (j!=0) value=values.at(j-1);
169
174
170 QString label = categories->label(value);
175 QString label = categories->label(value);
171 labels << label;
176 labels << label;
172 }
177 }
173 }
178 }
174
179
175 return category;
180 return category;
176 }
181 }
177
182
178 void ChartAxis::setAxisOpacity(qreal opacity)
183 void ChartAxis::setAxisOpacity(qreal opacity)
179 {
184 {
180 m_axis->setOpacity(opacity);
185 m_axis->setOpacity(opacity);
181 }
186 }
182
187
183 qreal ChartAxis::axisOpacity() const
188 qreal ChartAxis::axisOpacity() const
184 {
189 {
185 return m_axis->opacity();
190 return m_axis->opacity();
186 }
191 }
187
192
188 void ChartAxis::setGridOpacity(qreal opacity)
193 void ChartAxis::setGridOpacity(qreal opacity)
189 {
194 {
190 m_grid->setOpacity(opacity);
195 m_grid->setOpacity(opacity);
191 }
196 }
192
197
193 qreal ChartAxis::gridOpacity() const
198 qreal ChartAxis::gridOpacity() const
194 {
199 {
195 return m_grid->opacity();
200 return m_grid->opacity();
196 }
201 }
197
202
198 void ChartAxis::setLabelsOpacity(qreal opacity)
203 void ChartAxis::setLabelsOpacity(qreal opacity)
199 {
204 {
200 m_labels->setOpacity(opacity);
205 m_labels->setOpacity(opacity);
201 }
206 }
202
207
203 qreal ChartAxis::labelsOpacity() const
208 qreal ChartAxis::labelsOpacity() const
204 {
209 {
205 return m_labels->opacity();
210 return m_labels->opacity();
206 }
211 }
207
212
208 void ChartAxis::setShadesOpacity(qreal opacity)
213 void ChartAxis::setShadesOpacity(qreal opacity)
209 {
214 {
210 m_shades->setOpacity(opacity);
215 m_shades->setOpacity(opacity);
211 }
216 }
212
217
213 qreal ChartAxis::shadesOpacity() const
218 qreal ChartAxis::shadesOpacity() const
214 {
219 {
215 return m_shades->opacity();
220 return m_shades->opacity();
216 }
221 }
217
222
218 void ChartAxis::setLabelsAngle(int angle)
223 void ChartAxis::setLabelsAngle(int angle)
219 {
224 {
220 foreach(QGraphicsItem* item , m_labels->childItems()) {
225 foreach(QGraphicsItem* item , m_labels->childItems()) {
221 item->setRotation(angle);
226 item->setRotation(angle);
222 }
227 }
223
228
224 m_labelsAngle=angle;
229 m_labelsAngle=angle;
225 }
230 }
226
231
227 void ChartAxis::setLabelsPen(const QPen &pen)
232 void ChartAxis::setLabelsPen(const QPen &pen)
228 {
233 {
229 foreach(QGraphicsItem* item , m_labels->childItems()) {
234 foreach(QGraphicsItem* item , m_labels->childItems()) {
230 static_cast<QGraphicsSimpleTextItem*>(item)->setPen(pen);
235 static_cast<QGraphicsSimpleTextItem*>(item)->setPen(pen);
231 }
236 }
232 }
237 }
233
238
234 void ChartAxis::setLabelsBrush(const QBrush &brush)
239 void ChartAxis::setLabelsBrush(const QBrush &brush)
235 {
240 {
236 foreach(QGraphicsItem* item , m_labels->childItems()) {
241 foreach(QGraphicsItem* item , m_labels->childItems()) {
237 static_cast<QGraphicsSimpleTextItem*>(item)->setBrush(brush);
242 static_cast<QGraphicsSimpleTextItem*>(item)->setBrush(brush);
238 }
243 }
239 }
244 }
240
245
241 void ChartAxis::setLabelsFont(const QFont &font)
246 void ChartAxis::setLabelsFont(const QFont &font)
242 {
247 {
243 foreach(QGraphicsItem* item , m_labels->childItems()) {
248 foreach(QGraphicsItem* item , m_labels->childItems()) {
244 static_cast<QGraphicsSimpleTextItem*>(item)->setFont(font);
249 static_cast<QGraphicsSimpleTextItem*>(item)->setFont(font);
245 }
250 }
251 m_font = font;
246 }
252 }
247
253
248 void ChartAxis::setShadesBrush(const QBrush &brush)
254 void ChartAxis::setShadesBrush(const QBrush &brush)
249 {
255 {
250 foreach(QGraphicsItem* item , m_shades->childItems()) {
256 foreach(QGraphicsItem* item , m_shades->childItems()) {
251 static_cast<QGraphicsRectItem*>(item)->setBrush(brush);
257 static_cast<QGraphicsRectItem*>(item)->setBrush(brush);
252 }
258 }
253 }
259 }
254
260
255 void ChartAxis::setShadesPen(const QPen &pen)
261 void ChartAxis::setShadesPen(const QPen &pen)
256 {
262 {
257 foreach(QGraphicsItem* item , m_shades->childItems()) {
263 foreach(QGraphicsItem* item , m_shades->childItems()) {
258 static_cast<QGraphicsRectItem*>(item)->setPen(pen);
264 static_cast<QGraphicsRectItem*>(item)->setPen(pen);
259 }
265 }
260 }
266 }
261
267
262 void ChartAxis::setAxisPen(const QPen &pen)
268 void ChartAxis::setAxisPen(const QPen &pen)
263 {
269 {
264 foreach(QGraphicsItem* item , m_axis->childItems()) {
270 foreach(QGraphicsItem* item , m_axis->childItems()) {
265 static_cast<QGraphicsLineItem*>(item)->setPen(pen);
271 static_cast<QGraphicsLineItem*>(item)->setPen(pen);
266 }
272 }
267 }
273 }
268
274
269 void ChartAxis::setGridPen(const QPen &pen)
275 void ChartAxis::setGridPen(const QPen &pen)
270 {
276 {
271 foreach(QGraphicsItem* item , m_grid->childItems()) {
277 foreach(QGraphicsItem* item , m_grid->childItems()) {
272 static_cast<QGraphicsLineItem*>(item)->setPen(pen);
278 static_cast<QGraphicsLineItem*>(item)->setPen(pen);
273 }
279 }
274 }
280 }
275
281
276 bool ChartAxis::isEmpty()
282 bool ChartAxis::isEmpty()
277 {
283 {
278 return m_rect.isEmpty() || qFuzzyIsNull(m_min - m_max) || m_ticksCount==0;
284 return m_rect.isEmpty() || qFuzzyIsNull(m_min - m_max) || m_ticksCount==0;
279 }
285 }
280
286
281 //handlers
287 //handlers
282
288
283 void ChartAxis::handleAxisCategoriesUpdated()
289 void ChartAxis::handleAxisCategoriesUpdated()
284 {
290 {
285 if (isEmpty()) return;
291 if (isEmpty()) return;
286 updateLayout(m_layoutVector);
292 updateLayout(m_layoutVector);
287 }
293 }
288
294
289 void ChartAxis::handleAxisUpdated()
295 void ChartAxis::handleAxisUpdated()
290 {
296 {
291
297
292 if (isEmpty()) return;
298 if (isEmpty()) return;
293
299
294 if (m_chartAxis->isAxisVisible()) {
300 if (m_chartAxis->isAxisVisible()) {
295 setAxisOpacity(100);
301 setAxisOpacity(100);
296 } else {
302 } else {
297 setAxisOpacity(0);
303 setAxisOpacity(0);
298 }
304 }
299
305
300 if (m_chartAxis->isGridLineVisible()) {
306 if (m_chartAxis->isGridLineVisible()) {
301 setGridOpacity(100);
307 setGridOpacity(100);
302 } else {
308 } else {
303 setGridOpacity(0);
309 setGridOpacity(0);
304 }
310 }
305
311
306 if (m_chartAxis->labelsVisible()) {
312 if (m_chartAxis->labelsVisible()) {
307 setLabelsOpacity(100);
313 setLabelsOpacity(100);
308 } else {
314 } else {
309 setLabelsOpacity(0);
315 setLabelsOpacity(0);
310 }
316 }
311
317
312 if (m_chartAxis->shadesVisible()) {
318 if (m_chartAxis->shadesVisible()) {
313 setShadesOpacity(100);
319 setShadesOpacity(100);
314 } else {
320 } else {
315 setShadesOpacity(0);
321 setShadesOpacity(0);
316 }
322 }
317
323
318 setLabelsAngle(m_chartAxis->labelsAngle());
324 setLabelsAngle(m_chartAxis->labelsAngle());
319 setAxisPen(m_chartAxis->axisPen());
325 setAxisPen(m_chartAxis->axisPen());
320 setLabelsPen(m_chartAxis->labelsPen());
326 setLabelsPen(m_chartAxis->labelsPen());
321 setLabelsBrush(m_chartAxis->labelsBrush());
327 setLabelsBrush(m_chartAxis->labelsBrush());
322 setLabelsFont(m_chartAxis->labelsFont());
328 setLabelsFont(m_chartAxis->labelsFont());
323 setGridPen(m_chartAxis->gridLinePen());
329 setGridPen(m_chartAxis->gridLinePen());
324 setShadesPen(m_chartAxis->shadesPen());
330 setShadesPen(m_chartAxis->shadesPen());
325 setShadesBrush(m_chartAxis->shadesBrush());
331 setShadesBrush(m_chartAxis->shadesBrush());
326
332
327 }
333 }
328
334
329 void ChartAxis::handleRangeChanged(qreal min, qreal max,int tickCount)
335 void ChartAxis::handleRangeChanged(qreal min, qreal max,int tickCount)
330 {
336 {
331 if (qFuzzyIsNull(min - max) || tickCount < 2)
337 if (qFuzzyIsNull(min - max) || tickCount < 2)
332 return;
338 return;
333
339
334 m_min = min;
340 m_min = min;
335 m_max = max;
341 m_max = max;
336 m_ticksCount= tickCount;
342 m_ticksCount= tickCount;
337
343
338 if (isEmpty()) return;
344 if (isEmpty()) return;
339 QVector<qreal> layout = calculateLayout();
345 QVector<qreal> layout = calculateLayout();
340 updateLayout(layout);
346 updateLayout(layout);
341 }
347 }
342
348
343 void ChartAxis::handleGeometryChanged(const QRectF &rect)
349 void ChartAxis::handleGeometryChanged(const QRectF &rect)
344 {
350 {
345 if(m_rect != rect)
351 if(m_rect != rect)
346 {
352 {
347 m_rect = rect;
353 m_rect = rect;
348 if (isEmpty()) return;
354 if (isEmpty()) return;
349 QVector<qreal> layout = calculateLayout();
355 QVector<qreal> layout = calculateLayout();
350 updateLayout(layout);
356 updateLayout(layout);
351 }
357 }
352 }
358 }
353
359
360
361 qreal ChartAxis::minimumWidth()
362 {
363 if(m_minWidth == 0) updateGeometry();
364 return m_minWidth;
365 }
366
367 qreal ChartAxis::minimumHeight()
368 {
369 if(m_minHeight == 0) updateGeometry();
370 return m_minHeight;
371 }
372
373
354 void ChartAxis::axisSelected()
374 void ChartAxis::axisSelected()
355 {
375 {
356 qDebug()<<"TODO: axis clicked";
376 qDebug()<<"TODO: axis clicked";
357 }
377 }
358
378
359 #include "moc_chartaxis_p.cpp"
379 #include "moc_chartaxis_p.cpp"
360
380
361 QTCOMMERCIALCHART_END_NAMESPACE
381 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,157 +1,164
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 // W A R N I N G
21 // W A R N I N G
22 // -------------
22 // -------------
23 //
23 //
24 // This file is not part of the QtCommercial Chart API. It exists purely as an
24 // This file is not part of the QtCommercial Chart API. It exists purely as an
25 // implementation detail. This header file may change from version to
25 // implementation detail. This header file may change from version to
26 // version without notice, or even be removed.
26 // version without notice, or even be removed.
27 //
27 //
28 // We mean it.
28 // We mean it.
29
29
30 #ifndef CHARTAXIS_H
30 #ifndef CHARTAXIS_H
31 #define CHARTAXIS_H
31 #define CHARTAXIS_H
32
32
33 #include "qchartglobal.h"
33 #include "qchartglobal.h"
34 #include "chart_p.h"
34 #include "chart_p.h"
35 #include "axisanimation_p.h"
35 #include "axisanimation_p.h"
36 #include <QGraphicsItem>
36 #include <QGraphicsItem>
37 #include <QFont>
37
38
38 QTCOMMERCIALCHART_BEGIN_NAMESPACE
39 QTCOMMERCIALCHART_BEGIN_NAMESPACE
39
40
40 class QAxis;
41 class QAxis;
41 class ChartPresenter;
42 class ChartPresenter;
42
43
43 class ChartAxis : public Chart
44 class ChartAxis : public Chart
44 {
45 {
45 Q_OBJECT
46 Q_OBJECT
46 public:
47 public:
47 enum AxisType{ X_AXIS,Y_AXIS };
48 enum AxisType{ X_AXIS,Y_AXIS };
48
49
49 ChartAxis(QAxis *axis, ChartPresenter *presenter);
50 ChartAxis(QAxis *axis, ChartPresenter *presenter);
50 ~ChartAxis();
51 ~ChartAxis();
51
52
52 virtual AxisType axisType() const = 0;
53 virtual AxisType axisType() const = 0;
53
54
54 void setAxisOpacity(qreal opacity);
55 void setAxisOpacity(qreal opacity);
55 qreal axisOpacity() const;
56 qreal axisOpacity() const;
56
57
57 void setGridOpacity(qreal opacity);
58 void setGridOpacity(qreal opacity);
58 qreal gridOpacity() const;
59 qreal gridOpacity() const;
59
60
60 void setLabelsOpacity(qreal opacity);
61 void setLabelsOpacity(qreal opacity);
61 qreal labelsOpacity() const;
62 qreal labelsOpacity() const;
62
63
63 void setShadesOpacity(qreal opacity);
64 void setShadesOpacity(qreal opacity);
64 qreal shadesOpacity() const;
65 qreal shadesOpacity() const;
65
66
66 void setLabelsAngle(int angle);
67 void setLabelsAngle(int angle);
67 int labelsAngle()const { return m_labelsAngle; }
68 int labelsAngle()const { return m_labelsAngle; }
68
69
69 void setShadesBrush(const QBrush &brush);
70 void setShadesBrush(const QBrush &brush);
70 void setShadesPen(const QPen &pen);
71 void setShadesPen(const QPen &pen);
71
72
72 void setAxisPen(const QPen &pen);
73 void setAxisPen(const QPen &pen);
73 void setGridPen(const QPen &pen);
74 void setGridPen(const QPen &pen);
74
75
75 void setLabelsPen(const QPen &pen);
76 void setLabelsPen(const QPen &pen);
76 void setLabelsBrush(const QBrush &brush);
77 void setLabelsBrush(const QBrush &brush);
77 void setLabelsFont(const QFont &font);
78 void setLabelsFont(const QFont &font);
78
79
79 void setLayout(QVector<qreal> &layout);
80 void setLayout(QVector<qreal> &layout);
80 QVector<qreal> layout() const { return m_layoutVector; }
81 QVector<qreal> layout() const { return m_layoutVector; }
81
82
82 void setAnimation(AxisAnimation* animation);
83 void setAnimation(AxisAnimation* animation);
83 ChartAnimation* animation() const { return m_animation; };
84 ChartAnimation* animation() const { return m_animation; };
84
85
85 QRectF geometry() const { return m_rect; }
86 QRectF geometry() const { return m_rect; }
86
87
88 qreal minimumWidth();
89 qreal minimumHeight();
90
87 protected:
91 protected:
88 virtual void updateGeometry() = 0;
92 virtual void updateGeometry() = 0;
89 virtual QVector<qreal> calculateLayout() const = 0;
93 virtual QVector<qreal> calculateLayout() const = 0;
90 bool createLabels(QStringList &labels,qreal min, qreal max,int ticks) const;
94 bool createLabels(QStringList &labels,qreal min, qreal max,int ticks) const;
91
95
92 public Q_SLOTS:
96 public Q_SLOTS:
93 void handleAxisUpdated();
97 void handleAxisUpdated();
94 void handleAxisCategoriesUpdated();
98 void handleAxisCategoriesUpdated();
95 void handleRangeChanged(qreal min , qreal max,int tickCount);
99 void handleRangeChanged(qreal min , qreal max,int tickCount);
96 void handleGeometryChanged(const QRectF &size);
100 void handleGeometryChanged(const QRectF &size);
97
101
98 private:
102 private:
99 inline bool isEmpty();
103 inline bool isEmpty();
100 void createItems(int count);
104 void createItems(int count);
101 void deleteItems(int count);
105 void deleteItems(int count);
102 void updateLayout(QVector<qreal> &layout);
106 void updateLayout(QVector<qreal> &layout);
103 void axisSelected();
107 void axisSelected();
104
108
105 protected:
109 protected:
106 QAxis* m_chartAxis;
110 QAxis* m_chartAxis;
107 QRectF m_rect;
111 QRectF m_rect;
108 int m_labelsAngle;
112 int m_labelsAngle;
109 QScopedPointer<QGraphicsItemGroup> m_grid;
113 QScopedPointer<QGraphicsItemGroup> m_grid;
110 QScopedPointer<QGraphicsItemGroup> m_shades;
114 QScopedPointer<QGraphicsItemGroup> m_shades;
111 QScopedPointer<QGraphicsItemGroup> m_labels;
115 QScopedPointer<QGraphicsItemGroup> m_labels;
112 QScopedPointer<QGraphicsItemGroup> m_axis;
116 QScopedPointer<QGraphicsItemGroup> m_axis;
113 QVector<qreal> m_layoutVector;
117 QVector<qreal> m_layoutVector;
114 qreal m_min;
118 qreal m_min;
115 qreal m_max;
119 qreal m_max;
116 int m_ticksCount;
120 int m_ticksCount;
117 AxisAnimation *m_animation;
121 AxisAnimation *m_animation;
122 qreal m_minWidth;
123 qreal m_minHeight;
124 QFont m_font;
118
125
119 friend class AxisAnimation;
126 friend class AxisAnimation;
120 friend class AxisItem;
127 friend class AxisItem;
121
128
122 };
129 };
123
130
124 class AxisItem: public QGraphicsLineItem
131 class AxisItem: public QGraphicsLineItem
125 {
132 {
126
133
127 public:
134 public:
128 explicit AxisItem(ChartAxis *axis, QGraphicsItem *parent = 0) : QGraphicsLineItem(parent), m_axis(axis) {}
135 explicit AxisItem(ChartAxis *axis, QGraphicsItem *parent = 0) : QGraphicsLineItem(parent), m_axis(axis) {}
129
136
130 protected:
137 protected:
131 void mousePressEvent(QGraphicsSceneMouseEvent *event)
138 void mousePressEvent(QGraphicsSceneMouseEvent *event)
132 {
139 {
133 Q_UNUSED(event)
140 Q_UNUSED(event)
134 m_axis->axisSelected();
141 m_axis->axisSelected();
135 }
142 }
136
143
137 QRectF boundingRect() const
144 QRectF boundingRect() const
138 {
145 {
139 return shape().boundingRect();
146 return shape().boundingRect();
140 }
147 }
141
148
142 QPainterPath shape() const
149 QPainterPath shape() const
143 {
150 {
144 QPainterPath path = QGraphicsLineItem::shape();
151 QPainterPath path = QGraphicsLineItem::shape();
145 QRectF rect = path.boundingRect();
152 QRectF rect = path.boundingRect();
146 path.addRect(rect.adjusted(0,0,m_axis->axisType()!=ChartAxis::X_AXIS?8:0,m_axis->axisType()!=ChartAxis::Y_AXIS?8:0));
153 path.addRect(rect.adjusted(0,0,m_axis->axisType()!=ChartAxis::X_AXIS?8:0,m_axis->axisType()!=ChartAxis::Y_AXIS?8:0));
147 return path;
154 return path;
148 }
155 }
149
156
150 private:
157 private:
151 ChartAxis* m_axis;
158 ChartAxis* m_axis;
152
159
153 };
160 };
154
161
155 QTCOMMERCIALCHART_END_NAMESPACE
162 QTCOMMERCIALCHART_END_NAMESPACE
156
163
157 #endif /* AXISITEM_H_ */
164 #endif /* AXISITEM_H_ */
@@ -1,112 +1,124
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "chartaxisx_p.h"
21 #include "chartaxisx_p.h"
22 #include "qaxis.h"
22 #include "qaxis.h"
23 #include "qaxis_p.h"
23 #include "qaxis_p.h"
24 #include "qaxiscategories_p.h"
24 #include "qaxiscategories_p.h"
25 #include "chartpresenter_p.h"
25 #include "chartpresenter_p.h"
26 #include "chartanimator_p.h"
26 #include "chartanimator_p.h"
27 #include <QGraphicsLayout>
28 #include <QDebug>
29 #include <QFontMetrics>
27
30
28 static int label_padding = 5;
31 static int label_padding = 5;
29
32
30 QTCOMMERCIALCHART_BEGIN_NAMESPACE
33 QTCOMMERCIALCHART_BEGIN_NAMESPACE
31
34
32 ChartAxisX::ChartAxisX(QAxis *axis,ChartPresenter *presenter) : ChartAxis(axis,presenter)
35 ChartAxisX::ChartAxisX(QAxis *axis,ChartPresenter *presenter) : ChartAxis(axis,presenter)
33 {
36 {
34 }
37 }
35
38
36 ChartAxisX::~ChartAxisX()
39 ChartAxisX::~ChartAxisX()
37 {
40 {
38 }
41 }
39
42
40 QVector<qreal> ChartAxisX::calculateLayout() const
43 QVector<qreal> ChartAxisX::calculateLayout() const
41 {
44 {
42 Q_ASSERT(m_ticksCount>=2);
45 Q_ASSERT(m_ticksCount>=2);
43
46
44 QVector<qreal> points;
47 QVector<qreal> points;
45 points.resize(m_ticksCount);
48 points.resize(m_ticksCount);
46
49
47 const qreal deltaX = m_rect.width()/(m_ticksCount-1);
50 const qreal deltaX = m_rect.width()/(m_ticksCount-1);
48 for (int i = 0; i < m_ticksCount; ++i) {
51 for (int i = 0; i < m_ticksCount; ++i) {
49 int x = i * deltaX + m_rect.left();
52 int x = i * deltaX + m_rect.left();
50 points[i] = x;
53 points[i] = x;
51 }
54 }
52 return points;
55 return points;
53 }
56 }
54
57
55 void ChartAxisX::updateGeometry()
58 void ChartAxisX::updateGeometry()
56 {
59 {
57 const QVector<qreal>& layout = ChartAxis::layout();
60 const QVector<qreal>& layout = ChartAxis::layout();
58
61
62 m_minWidth = 0;
63 m_minHeight = 0;
64
65 if(layout.isEmpty()) return;
66
59 QStringList ticksList;
67 QStringList ticksList;
60
68
61 bool categories = createLabels(ticksList,m_min,m_max,layout.size());
69 bool categories = createLabels(ticksList,m_min,m_max,layout.size());
62
70
63 QList<QGraphicsItem *> lines = m_grid->childItems();
71 QList<QGraphicsItem *> lines = m_grid->childItems();
64 QList<QGraphicsItem *> labels = m_labels->childItems();
72 QList<QGraphicsItem *> labels = m_labels->childItems();
65 QList<QGraphicsItem *> shades = m_shades->childItems();
73 QList<QGraphicsItem *> shades = m_shades->childItems();
66 QList<QGraphicsItem *> axis = m_axis->childItems();
74 QList<QGraphicsItem *> axis = m_axis->childItems();
67
75
68 Q_ASSERT(labels.size() == ticksList.size());
76 Q_ASSERT(labels.size() == ticksList.size());
69 Q_ASSERT(layout.size() == ticksList.size());
77 Q_ASSERT(layout.size() == ticksList.size());
70
78
71 qreal minWidth = 0;
72 qreal minHeight = 0;
73
74 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(axis.at(0));
79 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(axis.at(0));
75 lineItem->setLine(m_rect.left(), m_rect.bottom(), m_rect.right(), m_rect.bottom());
80 lineItem->setLine(m_rect.left(), m_rect.bottom(), m_rect.right(), m_rect.bottom());
76
81
82 qreal width = 0;
77 for (int i = 0; i < layout.size(); ++i) {
83 for (int i = 0; i < layout.size(); ++i) {
78 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i));
84 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i));
79 lineItem->setLine(layout[i], m_rect.top(), layout[i], m_rect.bottom());
85 lineItem->setLine(layout[i], m_rect.top(), layout[i], m_rect.bottom());
80 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i));
86 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i));
81 if (!categories || i<1) {
87 if (!categories || i<1) {
82 labelItem->setText(ticksList.at(i));
88 labelItem->setText(ticksList.at(i));
83 const QRectF& rect = labelItem->boundingRect();
89 const QRectF& rect = labelItem->boundingRect();
84 minWidth+=rect.width();
85 minHeight=qMax(rect.height(),minHeight);
86 QPointF center = rect.center();
90 QPointF center = rect.center();
87 labelItem->setTransformOriginPoint(center.x(), center.y());
91 labelItem->setTransformOriginPoint(center.x(), center.y());
88 labelItem->setPos(layout[i] - center.x(), m_rect.bottom() + label_padding);
92 labelItem->setPos(layout[i] - center.x(), m_rect.bottom() + label_padding);
93
94 if(labelItem->pos().x()<=width){
95 labelItem->setVisible(false);
96 lineItem->setVisible(false);
97 }else{
98 labelItem->setVisible(true);
99 lineItem->setVisible(true);
100 width=rect.width()+labelItem->pos().x();
101 }
102 m_minWidth+=rect.width();
103 m_minHeight=qMax(rect.height(),m_minHeight);
89 }
104 }
90 else {
105 else {
91 labelItem->setText(ticksList.at(i));
106 labelItem->setText(ticksList.at(i));
92 const QRectF& rect = labelItem->boundingRect();
107 const QRectF& rect = labelItem->boundingRect();
93 minWidth+=rect.width();
94 minHeight=qMax(rect.height()+label_padding,minHeight);
95 QPointF center = rect.center();
108 QPointF center = rect.center();
96 labelItem->setTransformOriginPoint(center.x(), center.y());
109 labelItem->setTransformOriginPoint(center.x(), center.y());
97 labelItem->setPos(layout[i] - (layout[i] - layout[i-1])/2 - center.x(), m_rect.bottom() + label_padding);
110 labelItem->setPos(layout[i] - (layout[i] - layout[i-1])/2 - center.x(), m_rect.bottom() + label_padding);
111 m_minWidth+=rect.width();
112 m_minHeight=qMax(rect.height()+label_padding,m_minHeight);
98 }
113 }
99
114
100 if ((i+1)%2 && i>1) {
115 if ((i+1)%2 && i>1) {
101 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2-1));
116 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2-1));
102 rectItem->setRect(layout[i-1],m_rect.top(),layout[i]-layout[i-1],m_rect.height());
117 rectItem->setRect(layout[i-1],m_rect.top(),layout[i]-layout[i-1],m_rect.height());
103 }
118 }
104 lineItem = static_cast<QGraphicsLineItem*>(axis.at(i+1));
119 lineItem = static_cast<QGraphicsLineItem*>(axis.at(i+1));
105 lineItem->setLine(layout[i],m_rect.bottom(),layout[i],m_rect.bottom()+5);
120 lineItem->setLine(layout[i],m_rect.bottom(),layout[i],m_rect.bottom()+5);
106 }
121 }
107
108 presenter()->setMinimumMarginWidth(this,minWidth);
109 presenter()->setMinimumMarginHeight(this,minHeight);
110 }
122 }
111
123
112 QTCOMMERCIALCHART_END_NAMESPACE
124 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,116 +1,130
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "chartaxisy_p.h"
21 #include "chartaxisy_p.h"
22 #include "qaxis.h"
22 #include "qaxis.h"
23 #include "qaxis_p.h"
23 #include "qaxis_p.h"
24 #include "qaxiscategories_p.h"
24 #include "qaxiscategories_p.h"
25 #include "chartpresenter_p.h"
25 #include "chartpresenter_p.h"
26 #include "chartanimator_p.h"
26 #include "chartanimator_p.h"
27
27 #include <QGraphicsLayout>
28 #include <QFontMetrics>
29 #include <QDebug>
28
30
29 static int label_padding = 5;
31 static int label_padding = 5;
30
32
31 QTCOMMERCIALCHART_BEGIN_NAMESPACE
33 QTCOMMERCIALCHART_BEGIN_NAMESPACE
32
34
33 ChartAxisY::ChartAxisY(QAxis *axis,ChartPresenter *presenter) : ChartAxis(axis,presenter)
35 ChartAxisY::ChartAxisY(QAxis *axis,ChartPresenter *presenter) : ChartAxis(axis,presenter)
34 {
36 {
35 }
37 }
36
38
37 ChartAxisY::~ChartAxisY()
39 ChartAxisY::~ChartAxisY()
38 {
40 {
39 }
41 }
40
42
41 QVector<qreal> ChartAxisY::calculateLayout() const
43 QVector<qreal> ChartAxisY::calculateLayout() const
42 {
44 {
43 Q_ASSERT(m_ticksCount>=2);
45 Q_ASSERT(m_ticksCount>=2);
44
46
45 QVector<qreal> points;
47 QVector<qreal> points;
46 points.resize(m_ticksCount);
48 points.resize(m_ticksCount);
47
49
48 const qreal deltaY = m_rect.height()/(m_ticksCount-1);
50 const qreal deltaY = m_rect.height()/(m_ticksCount-1);
49 for (int i = 0; i < m_ticksCount; ++i) {
51 for (int i = 0; i < m_ticksCount; ++i) {
50 int y = i * -deltaY + m_rect.bottom();
52 int y = i * -deltaY + m_rect.bottom();
51 points[i] = y;
53 points[i] = y;
52 }
54 }
53
55
54 return points;
56 return points;
55 }
57 }
56
58
57 void ChartAxisY::updateGeometry()
59 void ChartAxisY::updateGeometry()
58 {
60 {
59 const QVector<qreal> &layout = ChartAxis::layout();
61 const QVector<qreal> &layout = ChartAxis::layout();
62 m_minWidth = 0;
63 m_minHeight = 0;
64
65 if(layout.isEmpty()) return;
60
66
61 QStringList ticksList;
67 QStringList ticksList;
62
68
63 bool categories = createLabels(ticksList,m_min,m_max,layout.size());
69 bool categories = createLabels(ticksList,m_min,m_max,layout.size());
64
70
65 QList<QGraphicsItem *> lines = m_grid->childItems();
71 QList<QGraphicsItem *> lines = m_grid->childItems();
66 QList<QGraphicsItem *> labels = m_labels->childItems();
72 QList<QGraphicsItem *> labels = m_labels->childItems();
67 QList<QGraphicsItem *> shades = m_shades->childItems();
73 QList<QGraphicsItem *> shades = m_shades->childItems();
68 QList<QGraphicsItem *> axis = m_axis->childItems();
74 QList<QGraphicsItem *> axis = m_axis->childItems();
69
75
70 Q_ASSERT(labels.size() == ticksList.size());
76 Q_ASSERT(labels.size() == ticksList.size());
71 Q_ASSERT(layout.size() == ticksList.size());
77 Q_ASSERT(layout.size() == ticksList.size());
72
78
73 qreal minWidth = 0;
79 qreal height = 2*m_rect.bottom();
74 qreal minHeight = 0;
75
80
76 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(axis.at(0));
81 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(axis.at(0));
77 lineItem->setLine(m_rect.left() , m_rect.top(), m_rect.left(), m_rect.bottom());
82 lineItem->setLine(m_rect.left() , m_rect.top(), m_rect.left(), m_rect.bottom());
78
83
79 for (int i = 0; i < layout.size(); ++i) {
84 for (int i = 0; i < layout.size(); ++i) {
80 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i));
85 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i));
81 lineItem->setLine(m_rect.left() , layout[i], m_rect.right(), layout[i]);
86 lineItem->setLine(m_rect.left() , layout[i], m_rect.right(), layout[i]);
82 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i));
87 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i));
83
88
84 if (!categories || i<1) {
89 if (!categories || i<1) {
85 labelItem->setText(ticksList.at(i));
90 labelItem->setText(ticksList.at(i));
86 const QRectF& rect = labelItem->boundingRect();
91 const QRectF& rect = labelItem->boundingRect();
87 minWidth=qMax(rect.width()+label_padding,minWidth);
92
88 minHeight+=rect.height();
89 QPointF center = rect.center();
93 QPointF center = rect.center();
90 labelItem->setTransformOriginPoint(center.x(), center.y());
94 labelItem->setTransformOriginPoint(center.x(), center.y());
91 labelItem->setPos(m_rect.left() - rect.width() - label_padding , layout[i]-center.y());
95 labelItem->setPos(m_rect.left() - rect.width() - label_padding , layout[i]-center.y());
96
97 if(labelItem->pos().y()+rect.height()>height) {
98 labelItem->setVisible(false);
99 lineItem->setVisible(false);
100 }
101 else {
102 labelItem->setVisible(true);
103 lineItem->setVisible(true);
104 height=labelItem->pos().y();
105 }
106
107 m_minWidth=qMax(rect.width()+label_padding,m_minWidth);
108 m_minHeight+=rect.height();
92 }
109 }
93 else {
110 else {
94 labelItem->setText(ticksList.at(i));
111 labelItem->setText(ticksList.at(i));
95 const QRectF& rect = labelItem->boundingRect();
112 const QRectF& rect = labelItem->boundingRect();
96 minWidth=qMax(rect.width(),minWidth);
113 m_minWidth=qMax(rect.width(),m_minWidth);
97 minHeight+=rect.height();
114 m_minHeight+=rect.height();
98 QPointF center = rect.center();
115 QPointF center = rect.center();
99 labelItem->setTransformOriginPoint(center.x(), center.y());
116 labelItem->setTransformOriginPoint(center.x(), center.y());
100 labelItem->setPos(m_rect.left() - rect.width() - label_padding , layout[i] - (layout[i] - layout[i-1])/2 -center.y());
117 labelItem->setPos(m_rect.left() - rect.width() - label_padding , layout[i] - (layout[i] - layout[i-1])/2 -center.y());
101 }
118 }
102
119
103 if ((i+1)%2 && i>1) {
120 if ((i+1)%2 && i>1) {
104 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2-1));
121 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2-1));
105 rectItem->setRect(m_rect.left(),layout[i],m_rect.width(),layout[i-1]-layout[i]);
122 rectItem->setRect(m_rect.left(),layout[i],m_rect.width(),layout[i-1]-layout[i]);
106 }
123 }
107 lineItem = static_cast<QGraphicsLineItem*>(axis.at(i+1));
124 lineItem = static_cast<QGraphicsLineItem*>(axis.at(i+1));
108 lineItem->setLine(m_rect.left()-5,layout[i],m_rect.left(),layout[i]);
125 lineItem->setLine(m_rect.left()-5,layout[i],m_rect.left(),layout[i]);
109 }
126 }
110
111 presenter()->setMinimumMarginWidth(this,minWidth);
112 presenter()->setMinimumMarginHeight(this,minHeight);
113
114 }
127 }
115
128
129
116 QTCOMMERCIALCHART_END_NAMESPACE
130 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,55 +1,56
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 // W A R N I N G
21 // W A R N I N G
22 // -------------
22 // -------------
23 //
23 //
24 // This file is not part of the QtCommercial Chart API. It exists purely as an
24 // This file is not part of the QtCommercial Chart API. It exists purely as an
25 // implementation detail. This header file may change from version to
25 // implementation detail. This header file may change from version to
26 // version without notice, or even be removed.
26 // version without notice, or even be removed.
27 //
27 //
28 // We mean it.
28 // We mean it.
29
29
30 #ifndef CHARTAXISY_H
30 #ifndef CHARTAXISY_H
31 #define CHARTAXISY_H
31 #define CHARTAXISY_H
32
32
33 #include "chartaxis_p.h"
33 #include "chartaxis_p.h"
34
34
35 QTCOMMERCIALCHART_BEGIN_NAMESPACE
35 QTCOMMERCIALCHART_BEGIN_NAMESPACE
36
36
37 class QAxis;
37 class QAxis;
38 class ChartPresenter;
38 class ChartPresenter;
39
39
40 class ChartAxisY : public ChartAxis
40 class ChartAxisY : public ChartAxis
41 {
41 {
42 public:
42 public:
43 ChartAxisY(QAxis *axis, ChartPresenter *presenter);
43 ChartAxisY(QAxis *axis, ChartPresenter *presenter);
44 ~ChartAxisY();
44 ~ChartAxisY();
45
45
46 AxisType axisType() const { return Y_AXIS;}
46 AxisType axisType() const { return Y_AXIS;}
47
47
48 protected:
48 protected:
49 QVector<qreal> calculateLayout() const;
49 QVector<qreal> calculateLayout() const;
50 void updateGeometry();
50 void updateGeometry();
51
51 };
52 };
52
53
53 QTCOMMERCIALCHART_END_NAMESPACE
54 QTCOMMERCIALCHART_END_NAMESPACE
54
55
55 #endif /* AXISITEM_H_ */
56 #endif /* AXISITEM_H_ */
@@ -1,433 +1,418
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20 #include "chartpresenter_p.h"
20 #include "chartpresenter_p.h"
21 #include "qchart.h"
21 #include "qchart.h"
22 #include "qchart_p.h"
22 #include "qchart_p.h"
23 #include "qaxis.h"
23 #include "qaxis.h"
24 #include "chartdataset_p.h"
24 #include "chartdataset_p.h"
25 #include "charttheme_p.h"
25 #include "charttheme_p.h"
26 #include "chartanimator_p.h"
26 #include "chartanimator_p.h"
27 #include "chartanimation_p.h"
27 #include "chartanimation_p.h"
28 #include "qabstractseries_p.h"
28 #include "qabstractseries_p.h"
29 #include "qareaseries.h"
29 #include "qareaseries.h"
30 #include "chartaxis_p.h"
30 #include "chartaxis_p.h"
31 #include "chartaxisx_p.h"
31 #include "chartaxisx_p.h"
32 #include "chartaxisy_p.h"
32 #include "chartaxisy_p.h"
33 #include "areachartitem_p.h"
33 #include "areachartitem_p.h"
34 #include "chartbackground_p.h"
34 #include "chartbackground_p.h"
35 #include "chartlayout_p.h"
35 #include <QTimer>
36 #include <QTimer>
36
37
37 QTCOMMERCIALCHART_BEGIN_NAMESPACE
38 QTCOMMERCIALCHART_BEGIN_NAMESPACE
38
39
39 ChartPresenter::ChartPresenter(QChart* chart,ChartDataSet* dataset):QObject(chart),
40 ChartPresenter::ChartPresenter(QChart* chart,ChartDataSet* dataset):QObject(chart),
40 m_chart(chart),
41 m_chart(chart),
41 m_animator(0),
42 m_animator(0),
42 m_dataset(dataset),
43 m_dataset(dataset),
43 m_chartTheme(0),
44 m_chartTheme(0),
44 m_chartRect(QRectF(QPoint(0,0),m_chart->size())),
45 m_options(QChart::NoAnimation),
45 m_options(QChart::NoAnimation),
46 m_minLeftMargin(0),
47 m_minBottomMargin(0),
48 m_state(ShowState),
46 m_state(ShowState),
47 m_layout(new ChartLayout(this)),
49 m_backgroundItem(0),
48 m_backgroundItem(0),
50 m_titleItem(0),
49 m_titleItem(0)
51 m_marginBig(60),
52 m_marginSmall(20),
53 m_marginTiny(10),
54 m_chartMargins(QRect(m_marginBig,m_marginBig,0,0))
55 {
50 {
56
51
57 }
52 }
58
53
59 ChartPresenter::~ChartPresenter()
54 ChartPresenter::~ChartPresenter()
60 {
55 {
61 delete m_chartTheme;
56 delete m_chartTheme;
62 }
57 }
63
58
64 void ChartPresenter::setGeometry(const QRectF& rect)
59 void ChartPresenter::setGeometry(const QRectF& rect)
65 {
60 {
66 m_rect = rect;
67 Q_ASSERT(m_rect.isValid());
68 updateLayout();
69 }
70
71 void ChartPresenter::setMinimumMarginWidth(ChartAxis* axis, qreal width)
72 {
73 switch(axis->axisType()){
74 case ChartAxis::X_AXIS:
75 {
76 if(width>m_chartRect.width()+ m_chartMargins.left()) {
77 m_minLeftMargin= width - m_chartRect.width();
78 updateLayout();
79 }
80 break;
81 }
82 case ChartAxis::Y_AXIS:
83 {
84
61
85 if(m_minLeftMargin!=width){
62 Q_ASSERT(rect.isValid());
86 m_minLeftMargin= width;
87 updateLayout();
88 }
89 break;
90 }
91
92 }
93 }
94
95 void ChartPresenter::setMinimumMarginHeight(ChartAxis* axis, qreal height)
96 {
97 switch(axis->axisType()){
98 case ChartAxis::X_AXIS:
99 {
100 if(m_minBottomMargin!=height) {
101 m_minBottomMargin= height;
102 updateLayout();
103 }
104 break;
105 }
106 case ChartAxis::Y_AXIS:
107 {
108
109 if(height>m_chartMargins.bottom()+m_chartRect.height()){
110 m_minBottomMargin= height - m_chartRect.height();
111 updateLayout();
112 }
113 break;
114 }
115
63
64 if(m_rect!=rect) {
65 m_rect=rect;
66 emit geometryChanged(m_rect);
116 }
67 }
117 }
68 }
118
69
119 void ChartPresenter::handleAxisAdded(QAxis* axis,Domain* domain)
70 void ChartPresenter::handleAxisAdded(QAxis* axis,Domain* domain)
120 {
71 {
121 ChartAxis* item;
72 ChartAxis* item;
122
73
123 if(axis == m_dataset->axisX()){
74 if(axis == m_dataset->axisX()){
124 item = new ChartAxisX(axis,this);
75 item = new ChartAxisX(axis,this);
125 }else{
76 }else{
126 item = new ChartAxisY(axis,this);
77 item = new ChartAxisY(axis,this);
127 }
78 }
128
79
129 if(m_options.testFlag(QChart::GridAxisAnimations)){
80 if(m_options.testFlag(QChart::GridAxisAnimations)){
130 item->setAnimator(m_animator);
81 item->setAnimator(m_animator);
131 item->setAnimation(new AxisAnimation(item));
82 item->setAnimation(new AxisAnimation(item));
132 }
83 }
133
84
134 if(axis==m_dataset->axisX()){
85 if(axis==m_dataset->axisX()){
135 m_chartTheme->decorate(axis,true);
86 m_chartTheme->decorate(axis,true);
136 QObject::connect(domain,SIGNAL(rangeXChanged(qreal,qreal,int)),item,SLOT(handleRangeChanged(qreal,qreal,int)));
87 QObject::connect(domain,SIGNAL(rangeXChanged(qreal,qreal,int)),item,SLOT(handleRangeChanged(qreal,qreal,int)));
137 //initialize
88 //initialize
138 item->handleRangeChanged(domain->minX(),domain->maxX(),domain->tickXCount());
89 item->handleRangeChanged(domain->minX(),domain->maxX(),domain->tickXCount());
139
90
140 }
91 }
141 else{
92 else{
142 m_chartTheme->decorate(axis,false);
93 m_chartTheme->decorate(axis,false);
143 QObject::connect(domain,SIGNAL(rangeYChanged(qreal,qreal,int)),item,SLOT(handleRangeChanged(qreal,qreal,int)));
94 QObject::connect(domain,SIGNAL(rangeYChanged(qreal,qreal,int)),item,SLOT(handleRangeChanged(qreal,qreal,int)));
144 //initialize
95 //initialize
145 item->handleRangeChanged(domain->minY(),domain->maxY(),domain->tickYCount());
96 item->handleRangeChanged(domain->minY(),domain->maxY(),domain->tickYCount());
146 }
97 }
147
98
148 QObject::connect(this,SIGNAL(geometryChanged(QRectF)),item,SLOT(handleGeometryChanged(QRectF)));
99 QObject::connect(this,SIGNAL(geometryChanged(QRectF)),item,SLOT(handleGeometryChanged(QRectF)));
149 //initialize
100 //initialize
150 if(m_chartRect.isValid()) item->handleGeometryChanged(m_chartRect);
101 if(m_rect.isValid()) item->handleGeometryChanged(m_rect);
151 m_axisItems.insert(axis, item);
102 m_axisItems.insert(axis, item);
152 }
103 }
153
104
154 void ChartPresenter::handleAxisRemoved(QAxis* axis)
105 void ChartPresenter::handleAxisRemoved(QAxis* axis)
155 {
106 {
156 ChartAxis* item = m_axisItems.take(axis);
107 ChartAxis* item = m_axisItems.take(axis);
157 Q_ASSERT(item);
108 Q_ASSERT(item);
158 if(m_animator) m_animator->removeAnimation(item);
109 if(m_animator) m_animator->removeAnimation(item);
159 delete item;
110 delete item;
160 }
111 }
161
112
162
113
163 void ChartPresenter::handleSeriesAdded(QAbstractSeries* series,Domain* domain)
114 void ChartPresenter::handleSeriesAdded(QAbstractSeries* series,Domain* domain)
164 {
115 {
165 Chart *item = series->d_ptr->createGraphics(this);
116 Chart *item = series->d_ptr->createGraphics(this);
166 Q_ASSERT(item);
117 Q_ASSERT(item);
167 QObject::connect(this,SIGNAL(geometryChanged(QRectF)),item,SLOT(handleGeometryChanged(QRectF)));
118 QObject::connect(this,SIGNAL(geometryChanged(QRectF)),item,SLOT(handleGeometryChanged(QRectF)));
168 QObject::connect(domain,SIGNAL(domainChanged(qreal,qreal,qreal,qreal)),item,SLOT(handleDomainChanged(qreal,qreal,qreal,qreal)));
119 QObject::connect(domain,SIGNAL(domainChanged(qreal,qreal,qreal,qreal)),item,SLOT(handleDomainChanged(qreal,qreal,qreal,qreal)));
169 //initialize
120 //initialize
170 item->handleDomainChanged(domain->minX(),domain->maxX(),domain->minY(),domain->maxY());
121 item->handleDomainChanged(domain->minX(),domain->maxX(),domain->minY(),domain->maxY());
171 if(m_chartRect.isValid()) item->handleGeometryChanged(m_chartRect);
122 if(m_rect.isValid()) item->handleGeometryChanged(m_rect);
172 m_chartItems.insert(series,item);
123 m_chartItems.insert(series,item);
173 }
124 }
174
125
175 void ChartPresenter::handleSeriesRemoved(QAbstractSeries* series)
126 void ChartPresenter::handleSeriesRemoved(QAbstractSeries* series)
176 {
127 {
177 Chart* item = m_chartItems.take(series);
128 Chart* item = m_chartItems.take(series);
178 Q_ASSERT(item);
129 Q_ASSERT(item);
179 if(m_animator) {
130 if(m_animator) {
180 //small hack to handle area animations
131 //small hack to handle area animations
181 if(series->type() == QAbstractSeries::SeriesTypeArea){
132 if(series->type() == QAbstractSeries::SeriesTypeArea){
182 QAreaSeries* areaSeries = static_cast<QAreaSeries*>(series);
133 QAreaSeries* areaSeries = static_cast<QAreaSeries*>(series);
183 AreaChartItem* area = static_cast<AreaChartItem*>(item);
134 AreaChartItem* area = static_cast<AreaChartItem*>(item);
184 m_animator->removeAnimation(area->upperLineItem());
135 m_animator->removeAnimation(area->upperLineItem());
185 if(areaSeries->lowerSeries()) m_animator->removeAnimation(area->lowerLineItem());
136 if(areaSeries->lowerSeries()) m_animator->removeAnimation(area->lowerLineItem());
186 }else
137 }else
187 m_animator->removeAnimation(item);
138 m_animator->removeAnimation(item);
188 }
139 }
189 delete item;
140 delete item;
190 }
141 }
191
142
192 void ChartPresenter::setTheme(QChart::ChartTheme theme,bool force)
143 void ChartPresenter::setTheme(QChart::ChartTheme theme,bool force)
193 {
144 {
194 if(m_chartTheme && m_chartTheme->id() == theme) return;
145 if(m_chartTheme && m_chartTheme->id() == theme) return;
195 delete m_chartTheme;
146 delete m_chartTheme;
196 m_chartTheme = ChartTheme::createTheme(theme);
147 m_chartTheme = ChartTheme::createTheme(theme);
197 m_chartTheme->setForced(force);
148 m_chartTheme->setForced(force);
198 m_chartTheme->decorate(m_chart);
149 m_chartTheme->decorate(m_chart);
199 m_chartTheme->decorate(m_chart->legend());
150 m_chartTheme->decorate(m_chart->legend());
200 resetAllElements();
151 resetAllElements();
201
152
202 // We do not want "force" to stay on.
153 // We do not want "force" to stay on.
203 // Bar/pie are calling decorate when adding/removing slices/bars which means
154 // Bar/pie are calling decorate when adding/removing slices/bars which means
204 // that to preserve users colors "force" must not be on.
155 // that to preserve users colors "force" must not be on.
205 m_chartTheme->setForced(false);
156 m_chartTheme->setForced(false);
206 }
157 }
207
158
208 QChart::ChartTheme ChartPresenter::theme()
159 QChart::ChartTheme ChartPresenter::theme()
209 {
160 {
210 return m_chartTheme->id();
161 return m_chartTheme->id();
211 }
162 }
212
163
213 void ChartPresenter::setAnimationOptions(QChart::AnimationOptions options)
164 void ChartPresenter::setAnimationOptions(QChart::AnimationOptions options)
214 {
165 {
215 if(m_options!=options) {
166 if(m_options!=options) {
216
167
217 m_options=options;
168 m_options=options;
218
169
219 if(m_options!=QChart::NoAnimation && !m_animator) {
170 if(m_options!=QChart::NoAnimation && !m_animator) {
220 m_animator= new ChartAnimator(this);
171 m_animator= new ChartAnimator(this);
221 }
172 }
222 resetAllElements();
173 resetAllElements();
223 }
174 }
224
175
225 }
176 }
226
177
227 void ChartPresenter::resetAllElements()
178 void ChartPresenter::resetAllElements()
228 {
179 {
229 QList<QAxis *> axisList = m_axisItems.uniqueKeys();
180 QList<QAxis *> axisList = m_axisItems.uniqueKeys();
230 QList<QAbstractSeries *> seriesList = m_chartItems.uniqueKeys();
181 QList<QAbstractSeries *> seriesList = m_chartItems.uniqueKeys();
231
182
232 foreach(QAxis *axis, axisList) {
183 foreach(QAxis *axis, axisList) {
233 handleAxisRemoved(axis);
184 handleAxisRemoved(axis);
234 handleAxisAdded(axis,m_dataset->domain(axis));
185 handleAxisAdded(axis,m_dataset->domain(axis));
235 }
186 }
236 foreach(QAbstractSeries *series, seriesList) {
187 foreach(QAbstractSeries *series, seriesList) {
237 handleSeriesRemoved(series);
188 handleSeriesRemoved(series);
238 handleSeriesAdded(series,m_dataset->domain(series));
189 handleSeriesAdded(series,m_dataset->domain(series));
239 // m_dataset->removeSeries(series);
190 // m_dataset->removeSeries(series);
240 // m_dataset->addSeries(series);
191 // m_dataset->addSeries(series);
241 }
192 }
242 }
193 }
243
194
244 void ChartPresenter::zoomIn(qreal factor)
195 void ChartPresenter::zoomIn(qreal factor)
245 {
196 {
246 QRectF rect = chartGeometry();
197 QRectF rect = geometry();
247 rect.setWidth(rect.width()/factor);
198 rect.setWidth(rect.width()/factor);
248 rect.setHeight(rect.height()/factor);
199 rect.setHeight(rect.height()/factor);
249 rect.moveCenter(chartGeometry().center());
200 rect.moveCenter(geometry().center());
250 zoomIn(rect);
201 zoomIn(rect);
251 }
202 }
252
203
253 void ChartPresenter::zoomIn(const QRectF& rect)
204 void ChartPresenter::zoomIn(const QRectF& rect)
254 {
205 {
255 QRectF r = rect.normalized();
206 QRectF r = rect.normalized();
256 r.translate(-m_chartMargins.topLeft());
207 r.translate(-geometry().topLeft());
257 if (!r.isValid())
208 if (!r.isValid())
258 return;
209 return;
259
210
260 m_state = ZoomInState;
211 m_state = ZoomInState;
261 m_statePoint = QPointF(r.center().x()/chartGeometry().width(),r.center().y()/chartGeometry().height());
212 m_statePoint = QPointF(r.center().x()/geometry().width(),r.center().y()/geometry().height());
262 m_dataset->zoomInDomain(r,chartGeometry().size());
213 m_dataset->zoomInDomain(r,geometry().size());
263 m_state = ShowState;
214 m_state = ShowState;
264 }
215 }
265
216
266 void ChartPresenter::zoomOut(qreal factor)
217 void ChartPresenter::zoomOut(qreal factor)
267 {
218 {
268 m_state = ZoomOutState;
219 m_state = ZoomOutState;
269
220
270 QRectF chartRect;
221 QRectF chartRect;
271 chartRect.setSize(chartGeometry().size());
222 chartRect.setSize(geometry().size());
272
223
273 QRectF rect;
224 QRectF rect;
274 rect.setSize(chartRect.size()/factor);
225 rect.setSize(chartRect.size()/factor);
275 rect.moveCenter(chartRect.center());
226 rect.moveCenter(chartRect.center());
276 if (!rect.isValid())
227 if (!rect.isValid())
277 return;
228 return;
278 m_statePoint = QPointF(rect.center().x()/chartGeometry().width(),rect.center().y()/chartGeometry().height());
229 m_statePoint = QPointF(rect.center().x()/geometry().width(),rect.center().y()/geometry().height());
279 m_dataset->zoomOutDomain(rect, chartRect.size());
230 m_dataset->zoomOutDomain(rect, chartRect.size());
280 m_state = ShowState;
231 m_state = ShowState;
281 }
232 }
282
233
283 void ChartPresenter::scroll(qreal dx,qreal dy)
234 void ChartPresenter::scroll(qreal dx,qreal dy)
284 {
235 {
285 if(dx<0) m_state=ScrollLeftState;
236 if(dx<0) m_state=ScrollLeftState;
286 if(dx>0) m_state=ScrollRightState;
237 if(dx>0) m_state=ScrollRightState;
287 if(dy<0) m_state=ScrollUpState;
238 if(dy<0) m_state=ScrollUpState;
288 if(dy>0) m_state=ScrollDownState;
239 if(dy>0) m_state=ScrollDownState;
289
240
290 m_dataset->scrollDomain(dx,dy,chartGeometry().size());
241 m_dataset->scrollDomain(dx,dy,geometry().size());
291 m_state = ShowState;
242 m_state = ShowState;
292 }
243 }
293
244
294 QChart::AnimationOptions ChartPresenter::animationOptions() const
245 QChart::AnimationOptions ChartPresenter::animationOptions() const
295 {
246 {
296 return m_options;
247 return m_options;
297 }
248 }
298
249
299 void ChartPresenter::updateLayout()
250 void ChartPresenter::createBackgroundItem()
300 {
251 {
301 if (!m_rect.isValid()) return;
252 if (!m_backgroundItem) {
253 m_backgroundItem = new ChartBackground(rootItem());
254 m_backgroundItem->setPen(Qt::NoPen);
255 m_backgroundItem->setZValue(ChartPresenter::BackgroundZValue);
256 }
257 }
302
258
303 QRectF oldChargMargins = m_chartMargins;
259 void ChartPresenter::createTitleItem()
260 {
261 if (!m_titleItem) {
262 m_titleItem = new QGraphicsSimpleTextItem(rootItem());
263 m_titleItem->setZValue(ChartPresenter::BackgroundZValue);
264 }
265 }
304
266
305 // recalculate title size
306
267
307 QSize titleSize;
268 void ChartPresenter::handleAnimationFinished()
308 int titlePadding=0;
269 {
270 m_animations.removeAll(qobject_cast<ChartAnimation*>(sender()));
271 if(m_animations.empty()) emit animationsFinished();
272 }
309
273
310 if (m_titleItem) {
274 void ChartPresenter::startAnimation(ChartAnimation* animation)
311 titleSize= m_titleItem->boundingRect().size().toSize();
275 {
276 if (animation->state() != QAbstractAnimation::Stopped) animation->stop();
277 QObject::connect(animation, SIGNAL(finished()),this,SLOT(handleAnimationFinished()),Qt::UniqueConnection);
278 if(!m_animations.isEmpty()){
279 m_animations.append(animation);
312 }
280 }
281 QTimer::singleShot(0, animation, SLOT(start()));
282 }
313
283
314 //defaults
284 QGraphicsRectItem* ChartPresenter::backgroundItem()
315 m_chartMargins = QRect(QPoint(m_minLeftMargin>m_marginBig?m_minLeftMargin:m_marginBig,m_marginBig),QPoint(m_marginBig,m_minBottomMargin>m_marginBig?m_minBottomMargin:m_marginBig));
285 {
316 titlePadding = m_chartMargins.top()/2;
286 return m_backgroundItem;
287 }
317
288
318 QLegend* legend = m_chart->d_ptr->m_legend;
289 void ChartPresenter::setBackgroundBrush(const QBrush& brush)
290 {
291 createBackgroundItem();
292 m_backgroundItem->setBrush(brush);
293 m_layout->invalidate();
294 }
319
295
320 // recalculate legend position
296 QBrush ChartPresenter::backgroundBrush() const
321 if (legend != 0 && legend->isAttachedToChart() && legend->isEnabled()) {
297 {
298 if (!m_backgroundItem) return QBrush();
299 return m_backgroundItem->brush();
300 }
322
301
323 // Reserve some space for legend
302 void ChartPresenter::setBackgroundPen(const QPen& pen)
324 switch (legend->alignment()) {
303 {
304 createBackgroundItem();
305 m_backgroundItem->setPen(pen);
306 m_layout->invalidate();
307 }
325
308
326 case Qt::AlignTop: {
309 QPen ChartPresenter::backgroundPen() const
327 int ledgendSize = legend->minHeight();
310 {
328 int topPadding = 2*m_marginTiny + titleSize.height() + ledgendSize + m_marginTiny;
311 if (!m_backgroundItem) return QPen();
329 m_chartMargins = QRect(QPoint(m_chartMargins.left(),topPadding),QPoint(m_chartMargins.right(),m_chartMargins.bottom()));
312 return m_backgroundItem->pen();
330 m_legendMargins = QRect(QPoint(m_chartMargins.left(),topPadding - (ledgendSize + m_marginTiny)),QPoint(m_chartMargins.right(),m_rect.height()-topPadding + m_marginTiny));
313 }
331 titlePadding = m_marginTiny + m_marginTiny;
332 break;
333 }
334 case Qt::AlignBottom: {
335 int ledgendSize = legend->minHeight();
336 int bottomPadding = m_marginTiny + m_marginSmall + ledgendSize + m_marginTiny + m_minBottomMargin;
337 m_chartMargins = QRect(QPoint(m_chartMargins.left(),m_chartMargins.top()),QPoint(m_chartMargins.right(),bottomPadding));
338 m_legendMargins = QRect(QPoint(m_chartMargins.left(),m_rect.height()-bottomPadding + m_marginTiny + m_minBottomMargin),QPoint(m_chartMargins.right(),m_marginTiny + m_marginSmall));
339 titlePadding = m_chartMargins.top()/2;
340 break;
341 }
342 case Qt::AlignLeft: {
343 int ledgendSize = legend->minWidth();
344 int leftPadding = m_marginTiny + m_marginSmall + ledgendSize + m_marginTiny + m_minLeftMargin;
345 m_chartMargins = QRect(QPoint(leftPadding,m_chartMargins.top()),QPoint(m_chartMargins.right(),m_chartMargins.bottom()));
346 m_legendMargins = QRect(QPoint(m_marginTiny + m_marginSmall,m_chartMargins.top()),QPoint(m_rect.width()-leftPadding + m_marginTiny + m_minLeftMargin,m_chartMargins.bottom()));
347 titlePadding = m_chartMargins.top()/2;
348 break;
349 }
350 case Qt::AlignRight: {
351 int ledgendSize = legend->minWidth();
352 int rightPadding = m_marginTiny + m_marginSmall + ledgendSize + m_marginTiny;
353 m_chartMargins = QRect(QPoint(m_chartMargins.left(),m_chartMargins.top()),QPoint(rightPadding,m_chartMargins.bottom()));
354 m_legendMargins = QRect(QPoint(m_rect.width()- rightPadding+ m_marginTiny ,m_chartMargins.top()),QPoint(m_marginTiny + m_marginSmall,m_chartMargins.bottom()));
355 titlePadding = m_chartMargins.top()/2;
356 break;
357 }
358 default: {
359 break;
360 }
361 }
362 }
363
314
364 if(m_rect.width()<2*(m_chartMargins.top()+m_chartMargins.bottom()) || m_rect.height()< 2*(m_chartMargins.top() + m_chartMargins.bottom()))
315 QGraphicsItem* ChartPresenter::titleItem()
365 {
316 {
366 m_chart->setMinimumSize(2*(m_chartMargins.top()+m_chartMargins.bottom()),2*(m_chartMargins.top() + m_chartMargins.bottom()));
317 return m_titleItem;
367 return;
318 }
368 }
369
319
320 void ChartPresenter::setTitle(const QString& title)
321 {
322 createTitleItem();
323 m_titleItem->setText(title);
324 m_layout->invalidate();
325 }
370
326
371 // recalculate title position
327 QString ChartPresenter::title() const
372 if (m_titleItem) {
328 {
373 QPointF center = m_rect.center() -m_titleItem->boundingRect().center();
329 if (!m_titleItem) return QString();
374 m_titleItem->setPos(center.x(),titlePadding);
330 return m_titleItem->text();
375 }
331 }
376
332
377 //recalculate background gradient
333 void ChartPresenter::setTitleFont(const QFont& font)
378 if (m_backgroundItem) {
334 {
379 m_backgroundItem->setRect(m_rect.adjusted(m_marginTiny,m_marginTiny, -m_marginTiny, -m_marginTiny));
335 createTitleItem();
380 }
336 m_titleItem->setFont(font);
337 m_layout->invalidate();
338 }
381
339
340 QFont ChartPresenter::titleFont() const
341 {
342 if (!m_titleItem) return QFont();
343 return m_titleItem->font();
344 }
382
345
383 QRectF chartRect = m_rect.adjusted(m_chartMargins.left(),m_chartMargins.top(),-m_chartMargins.right(),-m_chartMargins.bottom());
346 void ChartPresenter::setTitleBrush(const QBrush &brush)
347 {
348 createTitleItem();
349 m_titleItem->setBrush(brush);
350 m_layout->invalidate();
351 }
384
352
385 if (legend != 0 && legend->isAttachedToChart() && legend->isEnabled()) {
353 QBrush ChartPresenter::titleBrush() const
386 legend->setGeometry(m_rect.adjusted(m_legendMargins.left(),m_legendMargins.top(),-m_legendMargins.right(),-m_legendMargins.bottom()));
354 {
387 }
355 if (!m_titleItem) return QBrush();
356 return m_titleItem->brush();
357 }
358
359 void ChartPresenter::setBackgroundVisible(bool visible)
360 {
361 createBackgroundItem();
362 m_backgroundItem->setVisible(visible);
363 }
388
364
389 if(m_chartRect!=chartRect && chartRect.isValid()){
390 m_chartRect=chartRect;
391 emit geometryChanged(m_chartRect);
392 }
393
365
394 if (oldChargMargins != m_chartMargins)
366 bool ChartPresenter::isBackgroundVisible() const
395 emit marginsChanged(m_chartMargins);
367 {
368 if (!m_backgroundItem) return false;
369 return m_backgroundItem->isVisible();
396 }
370 }
397
371
398 void ChartPresenter::createChartBackgroundItem()
372 void ChartPresenter::setBackgroundDropShadowEnabled(bool enabled)
399 {
373 {
400 if (!m_backgroundItem) {
374 createBackgroundItem();
401 m_backgroundItem = new ChartBackground(rootItem());
375 m_backgroundItem->setDropShadowEnabled(enabled);
402 m_backgroundItem->setPen(Qt::NoPen);
403 m_backgroundItem->setZValue(ChartPresenter::BackgroundZValue);
404 }
405 }
376 }
406
377
407 void ChartPresenter::createChartTitleItem()
378 bool ChartPresenter::isBackgroundDropShadowEnabled() const
408 {
379 {
409 if (!m_titleItem) {
380 if (!m_backgroundItem) return false;
410 m_titleItem = new QGraphicsSimpleTextItem(rootItem());
381 return m_backgroundItem->isDropShadowEnabled();
411 m_titleItem->setZValue(ChartPresenter::BackgroundZValue);
412 }
413 }
382 }
414
383
415 void ChartPresenter::handleAnimationFinished()
384
385 QGraphicsLayout* ChartPresenter::layout()
416 {
386 {
417 m_animations.removeAll(qobject_cast<ChartAnimation*>(sender()));
387 return m_layout;
418 if(m_animations.empty()) emit animationsFinished();
419 }
388 }
420
389
421 void ChartPresenter::startAnimation(ChartAnimation* animation)
390 void ChartPresenter::setMarginsMinimum(const QRectF& margins)
422 {
391 {
423 if (animation->state() != QAbstractAnimation::Stopped) animation->stop();
392 Q_UNUSED(margins);
424 QObject::connect(animation, SIGNAL(finished()),this,SLOT(handleAnimationFinished()),Qt::UniqueConnection);
393 // m_layout->setMarginsMinimum(margins);
425 if(!m_animations.isEmpty()){
394 }
426 m_animations.append(animation);
395
427 }
396 QRectF ChartPresenter::margins() const
428 QTimer::singleShot(0, animation, SLOT(start()));
397 {
398 return QRectF();//m_layout->margins();
399 }
400
401 QLegend* ChartPresenter::legend()
402 {
403 return m_chart->legend();
404 }
405
406 QList<ChartAxis*> ChartPresenter::axisItems() const
407 {
408 return m_axisItems.values();
409 }
410
411 void ChartPresenter::setVisible(bool visible)
412 {
413 m_chart->setVisible(visible);
429 }
414 }
430
415
431 #include "moc_chartpresenter_p.cpp"
416 #include "moc_chartpresenter_p.cpp"
432
417
433 QTCOMMERCIALCHART_END_NAMESPACE
418 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,155 +1,174
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 // W A R N I N G
21 // W A R N I N G
22 // -------------
22 // -------------
23 //
23 //
24 // This file is not part of the QtCommercial Chart API. It exists purely as an
24 // This file is not part of the QtCommercial Chart API. It exists purely as an
25 // implementation detail. This header file may change from version to
25 // implementation detail. This header file may change from version to
26 // version without notice, or even be removed.
26 // version without notice, or even be removed.
27 //
27 //
28 // We mean it.
28 // We mean it.
29
29
30 #ifndef CHARTPRESENTER_H
30 #ifndef CHARTPRESENTER_H
31 #define CHARTPRESENTER_H
31 #define CHARTPRESENTER_H
32
32
33 #include "qchartglobal.h"
33 #include "qchartglobal.h"
34 #include "qchart.h" //becouse of QChart::ChartThemeId //TODO
34 #include "qchart.h" //becouse of QChart::ChartThemeId //TODO
35 #include <QRectF>
35 #include <QRectF>
36
36
37 QTCOMMERCIALCHART_BEGIN_NAMESPACE
37 QTCOMMERCIALCHART_BEGIN_NAMESPACE
38
38
39 class Chart;
39 class Chart;
40 class QAbstractSeries;
40 class QAbstractSeries;
41 class ChartDataSet;
41 class ChartDataSet;
42 class Domain;
42 class Domain;
43 class ChartAxis;
43 class ChartAxis;
44 class ChartTheme;
44 class ChartTheme;
45 class ChartAnimator;
45 class ChartAnimator;
46 class ChartBackground;
46 class ChartBackground;
47 class ChartAnimation;
47 class ChartAnimation;
48 class ChartLayout;
48
49
49 class ChartPresenter: public QObject
50 class ChartPresenter: public QObject
50 {
51 {
51 Q_OBJECT
52 Q_OBJECT
52 public:
53 public:
53 enum ZValues {
54 enum ZValues {
54 BackgroundZValue = -1,
55 BackgroundZValue = -1,
55 ShadesZValue,
56 ShadesZValue,
56 GridZValue,
57 GridZValue,
57 SeriesZValue,
58 SeriesZValue,
58 LineChartZValue = SeriesZValue,
59 LineChartZValue = SeriesZValue,
59 BarSeriesZValue = SeriesZValue,
60 BarSeriesZValue = SeriesZValue,
60 ScatterSeriesZValue = SeriesZValue,
61 ScatterSeriesZValue = SeriesZValue,
61 PieSeriesZValue = SeriesZValue,
62 PieSeriesZValue = SeriesZValue,
62 AxisZValue,
63 AxisZValue,
63 LegendZValue
64 LegendZValue
64 };
65 };
65
66
66 enum State {
67 enum State {
67 ShowState,
68 ShowState,
68 ScrollUpState,
69 ScrollUpState,
69 ScrollDownState,
70 ScrollDownState,
70 ScrollLeftState,
71 ScrollLeftState,
71 ScrollRightState,
72 ScrollRightState,
72 ZoomInState,
73 ZoomInState,
73 ZoomOutState
74 ZoomOutState
74 };
75 };
75
76
76 ChartPresenter(QChart* chart,ChartDataSet *dataset);
77 ChartPresenter(QChart* chart,ChartDataSet *dataset);
77 virtual ~ChartPresenter();
78 virtual ~ChartPresenter();
78
79
79 ChartAnimator* animator() const { return m_animator; }
80 ChartAnimator* animator() const { return m_animator; }
80 ChartTheme *chartTheme() const { return m_chartTheme; }
81 ChartTheme *chartTheme() const { return m_chartTheme; }
81 ChartDataSet *dataSet() const { return m_dataset; }
82 ChartDataSet *dataSet() const { return m_dataset; }
82 QGraphicsItem* rootItem() const { return m_chart; }
83 QGraphicsItem* rootItem() const { return m_chart; }
84 QGraphicsRectItem* backgroundItem();
85 QGraphicsItem* titleItem();
86 QList<ChartAxis*> axisItems() const;
87
88 QLegend* legend();
89
90 void setBackgroundBrush(const QBrush& brush);
91 QBrush backgroundBrush() const;
92
93 void setBackgroundPen(const QPen& pen);
94 QPen backgroundPen() const;
95
96 void setTitle(const QString& title);
97 QString title() const;
98
99 void setTitleFont(const QFont& font);
100 QFont titleFont() const;
101
102 void setTitleBrush(const QBrush &brush);
103 QBrush titleBrush() const;
104
105 void setBackgroundVisible(bool visible);
106 bool isBackgroundVisible() const;
107
108 void setBackgroundDropShadowEnabled(bool enabled);
109 bool isBackgroundDropShadowEnabled() const;
110
111 void setVisible(bool visible);
83
112
84 void setTheme(QChart::ChartTheme theme,bool force = true);
113 void setTheme(QChart::ChartTheme theme,bool force = true);
85 QChart::ChartTheme theme();
114 QChart::ChartTheme theme();
86
115
87 void setAnimationOptions(QChart::AnimationOptions options);
116 void setAnimationOptions(QChart::AnimationOptions options);
88 QChart::AnimationOptions animationOptions() const;
117 QChart::AnimationOptions animationOptions() const;
89
118
90 void zoomIn(qreal factor);
119 void zoomIn(qreal factor);
91 void zoomIn(const QRectF& rect);
120 void zoomIn(const QRectF& rect);
92 void zoomOut(qreal factor);
121 void zoomOut(qreal factor);
93 void scroll(qreal dx,qreal dy);
122 void scroll(qreal dx,qreal dy);
94
123
95 void setGeometry(const QRectF& rect);
124 void setGeometry(const QRectF& rect);
96 QRectF chartGeometry() const { return m_chartRect; }
125 QRectF geometry() { return m_rect; }
97
98 void setMinimumMarginHeight(ChartAxis* axis, qreal height);
99 void setMinimumMarginWidth(ChartAxis* axis, qreal width);
100 qreal minimumLeftMargin() const { return m_minLeftMargin; }
101 qreal minimumBottomMargin() const { return m_minBottomMargin; }
102
126
103 void startAnimation(ChartAnimation* animation);
127 void startAnimation(ChartAnimation* animation);
104 State state() const { return m_state; }
128 State state() const { return m_state; }
105 QPointF statePoint() const { return m_statePoint; }
129 QPointF statePoint() const { return m_statePoint; }
106 public: //TODO: fix me
130
107 void resetAllElements();
131 void resetAllElements();
108 void createChartBackgroundItem();
132
109 void createChartTitleItem();
133 void setMarginsMinimum(const QRectF& margins);
110 QRectF margins() const { return m_chartMargins;}
134 QRectF margins() const;
135 QGraphicsLayout* layout();
136
137 private:
138 void createBackgroundItem();
139 void createTitleItem();
111
140
112 public Q_SLOTS:
141 public Q_SLOTS:
113 void handleSeriesAdded(QAbstractSeries* series,Domain* domain);
142 void handleSeriesAdded(QAbstractSeries* series,Domain* domain);
114 void handleSeriesRemoved(QAbstractSeries* series);
143 void handleSeriesRemoved(QAbstractSeries* series);
115 void handleAxisAdded(QAxis* axis,Domain* domain);
144 void handleAxisAdded(QAxis* axis,Domain* domain);
116 void handleAxisRemoved(QAxis* axis);
145 void handleAxisRemoved(QAxis* axis);
117 void updateLayout();
118
146
119 private Q_SLOTS:
147 private Q_SLOTS:
120 void handleAnimationFinished();
148 void handleAnimationFinished();
121
149
122 Q_SIGNALS:
150 Q_SIGNALS:
123 void geometryChanged(const QRectF& rect);
151 void geometryChanged(const QRectF& rect);
124 void animationsFinished();
152 void animationsFinished();
125 void marginsChanged(QRectF margins);
153 void marginsChanged(QRectF margins);
126
154
127 private:
155 private:
128 QChart* m_chart;
156 QChart* m_chart;
129 ChartAnimator* m_animator;
157 ChartAnimator* m_animator;
130 ChartDataSet* m_dataset;
158 ChartDataSet* m_dataset;
131 ChartTheme *m_chartTheme;
159 ChartTheme *m_chartTheme;
132 QMap<QAbstractSeries *, Chart *> m_chartItems;
160 QMap<QAbstractSeries *, Chart *> m_chartItems;
133 QMap<QAxis *, ChartAxis *> m_axisItems;
161 QMap<QAxis *, ChartAxis *> m_axisItems;
134 QRectF m_rect;
162 QRectF m_rect;
135 QRectF m_chartRect;
136 QChart::AnimationOptions m_options;
163 QChart::AnimationOptions m_options;
137 qreal m_minLeftMargin;
138 qreal m_minBottomMargin;
139 State m_state;
164 State m_state;
140 QPointF m_statePoint;
165 QPointF m_statePoint;
141 QList<ChartAnimation*> m_animations;
166 QList<ChartAnimation*> m_animations;
142
167 ChartLayout* m_layout;
143 public: //TODO: fixme
144 ChartBackground* m_backgroundItem;
168 ChartBackground* m_backgroundItem;
145 QGraphicsSimpleTextItem* m_titleItem;
169 QGraphicsSimpleTextItem* m_titleItem;
146 int m_marginBig;
147 int m_marginSmall;
148 int m_marginTiny;
149 QRectF m_chartMargins;
150 QRectF m_legendMargins;
151 };
170 };
152
171
153 QTCOMMERCIALCHART_END_NAMESPACE
172 QTCOMMERCIALCHART_END_NAMESPACE
154
173
155 #endif /* CHARTPRESENTER_H_ */
174 #endif /* CHARTPRESENTER_H_ */
@@ -1,15 +1,17
1 INCLUDEPATH += $$PWD
1 INCLUDEPATH += $$PWD
2 DEPENDPATH += $$PWD
2 DEPENDPATH += $$PWD
3
3
4 SOURCES += \
4 SOURCES += \
5 $$PWD/qlegend.cpp \
5 $$PWD/qlegend.cpp \
6 $$PWD/legendmarker.cpp
6 $$PWD/legendmarker.cpp \
7 $$PWD/legendlayout.cpp
7
8
8 PRIVATE_HEADERS += \
9 PRIVATE_HEADERS += \
9 $$PWD/legendmarker_p.h \
10 $$PWD/legendmarker_p.h \
10 $$PWD/legendscroller_p.h \
11 $$PWD/legendscroller_p.h \
11 $$PWD/qlegend_p.h
12 $$PWD/qlegend_p.h \
13 $$PWD/legendlayout_p.h
12
14
13
15
14 PUBLIC_HEADERS += \
16 PUBLIC_HEADERS += \
15 $$PWD/qlegend.h No newline at end of file
17 $$PWD/qlegend.h
@@ -1,220 +1,233
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "legendmarker_p.h"
21 #include "legendmarker_p.h"
22 #include "qxyseries.h"
22 #include "qxyseries.h"
23 #include "qxyseries_p.h"
23 #include "qxyseries_p.h"
24 #include "qlegend.h"
24 #include "qlegend.h"
25 #include "qbarseries.h"
25 #include "qbarseries.h"
26 #include "qpieseries.h"
26 #include "qpieseries.h"
27 #include "qpieslice.h"
27 #include "qpieslice.h"
28 #include "qbarset.h"
28 #include "qbarset.h"
29 #include "qbarset_p.h"
29 #include "qbarset_p.h"
30 #include "qareaseries.h"
30 #include "qareaseries.h"
31 #include "qareaseries_p.h"
31 #include "qareaseries_p.h"
32 #include <QPainter>
32 #include <QPainter>
33 #include <QGraphicsSceneEvent>
33 #include <QGraphicsSceneEvent>
34 #include <QGraphicsSimpleTextItem>
34 #include <QGraphicsSimpleTextItem>
35 #include <QDebug>
35 #include <QDebug>
36
36
37 QTCOMMERCIALCHART_BEGIN_NAMESPACE
37 QTCOMMERCIALCHART_BEGIN_NAMESPACE
38
38
39 LegendMarker::LegendMarker(QAbstractSeries *series, QLegend *legend) :
39 LegendMarker::LegendMarker(QAbstractSeries *series, QLegend *legend) :
40 QGraphicsObject(legend),
40 QGraphicsObject(legend),
41 m_series(series),
41 m_series(series),
42 m_markerRect(0,0,10.0,10.0),
42 m_markerRect(0,0,10.0,10.0),
43 m_boundingRect(0,0,0,0),
43 m_boundingRect(0,0,0,0),
44 m_legend(legend),
44 m_legend(legend),
45 m_textItem(new QGraphicsSimpleTextItem(this)),
45 m_textItem(new QGraphicsSimpleTextItem(this)),
46 m_rectItem(new QGraphicsRectItem(this))
46 m_rectItem(new QGraphicsRectItem(this)),
47 m_margin(2),
48 m_space(4)
47 {
49 {
48 //setAcceptedMouseButtons(Qt::LeftButton|Qt::RightButton);
50 //setAcceptedMouseButtons(Qt::LeftButton|Qt::RightButton);
49 m_rectItem->setRect(m_markerRect);
51 m_rectItem->setRect(m_markerRect);
50 updateLayout();
51 }
52 }
52
53
53 void LegendMarker::setPen(const QPen &pen)
54 void LegendMarker::setPen(const QPen &pen)
54 {
55 {
55 m_rectItem->setPen(pen);
56 m_rectItem->setPen(pen);
56 updateLayout();
57 m_textItem->setPen(pen);
57 }
58 }
58
59
59 QPen LegendMarker::pen() const
60 QPen LegendMarker::pen() const
60 {
61 {
61 return m_rectItem->pen();
62 return m_rectItem->pen();
62 }
63 }
63
64
64 void LegendMarker::setBrush(const QBrush &brush)
65 void LegendMarker::setBrush(const QBrush &brush)
65 {
66 {
66 m_rectItem->setBrush(brush);
67 m_rectItem->setBrush(brush);
67 }
68 }
68
69
69 QBrush LegendMarker::brush() const
70 QBrush LegendMarker::brush() const
70 {
71 {
71 return m_rectItem->brush();
72 return m_rectItem->brush();
72 }
73 }
73
74
74 void LegendMarker::setFont(const QFont &font)
75 void LegendMarker::setFont(const QFont &font)
75 {
76 {
76 m_textItem->setFont(font);
77 m_textItem->setFont(font);
77 updateLayout();
78 }
78 }
79
79
80 QFont LegendMarker::font() const
80 QFont LegendMarker::font() const
81 {
81 {
82 return m_textItem->font();
82 return m_textItem->font();
83 }
83 }
84
84
85 void LegendMarker::setLabel(const QString label)
85 void LegendMarker::setLabel(const QString label)
86 {
86 {
87 m_textItem->setText(label);
87 m_textItem->setText(label);
88 updateLayout();
89 }
88 }
90
89
91 void LegendMarker::setSize(const QSize& size)
90 QString LegendMarker::label() const
92 {
91 {
93 m_markerRect = QRectF(0,0,size.width(),size.height());
92 return m_textItem->text();
94 }
93 }
95
94
96 QString LegendMarker::label() const
95 QRectF LegendMarker::boundingRect() const
97 {
96 {
98 return m_textItem->text();
97 return m_boundingRect;
99 }
98 }
100
99
101 void LegendMarker::setLabelBrush(const QBrush &brush)
100 void LegendMarker::setLabelBrush(const QBrush &brush)
102 {
101 {
103 m_textItem->setBrush(brush);
102 m_textItem->setBrush(brush);
104 updateLayout();
105 }
103 }
106
104
107 QBrush LegendMarker::labelBrush() const
105 QBrush LegendMarker::labelBrush() const
108 {
106 {
109 return m_textItem->brush();
107 return m_textItem->brush();
110 }
108 }
111
109
110
111 void LegendMarker::setGeometry(const QRectF& rect)
112 {
113 const QRectF& textRect = m_textItem->boundingRect();
114
115 m_textItem->setPos(m_markerRect.width() + m_space + m_margin,rect.height()/2 - textRect.height()/2);
116 m_rectItem->setPos(m_margin,rect.height()/2 - m_markerRect.height()/2);
117
118 prepareGeometryChange();
119 m_boundingRect = rect;
120 }
121
112 void LegendMarker::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
122 void LegendMarker::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
113 {
123 {
114 Q_UNUSED(option)
124 Q_UNUSED(option)
115 Q_UNUSED(widget)
125 Q_UNUSED(widget)
116 Q_UNUSED(painter)
126 Q_UNUSED(painter)
117 }
127 }
118
128
119 QRectF LegendMarker::boundingRect() const
120 {
121 return m_boundingRect;
122 }
123
129
124 void LegendMarker::updateLayout()
130 QSizeF LegendMarker::sizeHint(Qt::SizeHint which, const QSizeF& constraint) const
125 {
131 {
132 Q_UNUSED(constraint)
126
133
127 static const qreal margin = 2;
134 QFontMetrics fn(m_textItem->font());
128 static const qreal space = 4;
135 QSizeF sh;
129
136
130 const QRectF& textRect = m_textItem->boundingRect();
137 switch (which) {
131 prepareGeometryChange();
138 case Qt::MinimumSize:
132 m_boundingRect = QRectF(0,0,m_markerRect.width() + 2*margin + space + textRect.width(),qMax(m_markerRect.height()+2*margin,textRect.height()+2*margin));
139 sh = QSizeF(fn.boundingRect("...").width(),fn.height());
133 m_textItem->setPos(m_markerRect.width() + space + margin,m_boundingRect.height()/2 - textRect.height()/2);
140 break;
134 m_rectItem->setPos(margin,m_boundingRect.height()/2 - m_markerRect.height()/2);
141 case Qt::PreferredSize:
142 sh = QSizeF(fn.boundingRect(m_textItem->text()).width() + 2*m_margin + m_space +m_markerRect.width(),qMax(m_markerRect.height()+2*m_margin,fn.height()+2*m_margin));
143 break;
144 default:
145 break;
146 }
135
147
148 return sh;
136 }
149 }
137
150
138 void LegendMarker::mousePressEvent(QGraphicsSceneMouseEvent *event)
151 void LegendMarker::mousePressEvent(QGraphicsSceneMouseEvent *event)
139 {
152 {
140 QGraphicsObject::mousePressEvent(event);
153 QGraphicsObject::mousePressEvent(event);
141 qDebug()<<"Not implemented"; //TODO: selected signal removed for now
154 qDebug()<<"Not implemented"; //TODO: selected signal removed for now
142 }
155 }
143
156
144 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
157 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
145
158
146 AreaLegendMarker::AreaLegendMarker(QAreaSeries *series,QLegend *legend) : LegendMarker(series,legend),
159 AreaLegendMarker::AreaLegendMarker(QAreaSeries *series,QLegend *legend) : LegendMarker(series,legend),
147 m_series(series)
160 m_series(series)
148 {
161 {
149 //QObject::connect(this, SIGNAL(selected()), series, SIGNAL(selected()));
162 //QObject::connect(this, SIGNAL(selected()), series, SIGNAL(selected()));
150 QObject::connect(series->d_func(),SIGNAL(updated()), this, SLOT(updated()));
163 QObject::connect(series->d_func(),SIGNAL(updated()), this, SLOT(updated()));
151 QObject::connect(series, SIGNAL(nameChanged()), this, SLOT(updated()));
164 QObject::connect(series, SIGNAL(nameChanged()), this, SLOT(updated()));
152 updated();
165 updated();
153 }
166 }
154
167
155 void AreaLegendMarker::updated()
168 void AreaLegendMarker::updated()
156 {
169 {
157 setBrush(m_series->brush());
170 setBrush(m_series->brush());
158 setLabel(m_series->name());
171 setLabel(m_series->name());
159 }
172 }
160
173
161 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
174 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
162
175
163 BarLegendMarker::BarLegendMarker(QBarSeries *barseries,QBarSet *barset, QLegend *legend) : LegendMarker(barseries,legend),
176 BarLegendMarker::BarLegendMarker(QBarSeries *barseries,QBarSet *barset, QLegend *legend) : LegendMarker(barseries,legend),
164 m_barset(barset)
177 m_barset(barset)
165 {
178 {
166 //QObject::connect(this, SIGNAL(selected()),barset->d_ptr.data(), SIGNAL(selected()));
179 //QObject::connect(this, SIGNAL(selected()),barset->d_ptr.data(), SIGNAL(selected()));
167 QObject::connect(barset->d_ptr.data(), SIGNAL(updatedBars()), this, SLOT(updated()));
180 QObject::connect(barset->d_ptr.data(), SIGNAL(updatedBars()), this, SLOT(updated()));
168 updated();
181 updated();
169 }
182 }
170
183
171 void BarLegendMarker::updated()
184 void BarLegendMarker::updated()
172 {
185 {
173 setBrush(m_barset->brush());
186 setBrush(m_barset->brush());
174 setLabel(m_barset->label());
187 setLabel(m_barset->label());
175 }
188 }
176
189
177 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
190 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
178
191
179 PieLegendMarker::PieLegendMarker(QPieSeries* series,QPieSlice *pieslice, QLegend *legend) : LegendMarker(series,legend),
192 PieLegendMarker::PieLegendMarker(QPieSeries* series,QPieSlice *pieslice, QLegend *legend) : LegendMarker(series,legend),
180 m_pieslice(pieslice)
193 m_pieslice(pieslice)
181 {
194 {
182 QObject::connect(pieslice, SIGNAL(labelChanged()), this, SLOT(updated()));
195 QObject::connect(pieslice, SIGNAL(labelChanged()), this, SLOT(updated()));
183 QObject::connect(pieslice, SIGNAL(brushChanged()), this, SLOT(updated()));
196 QObject::connect(pieslice, SIGNAL(brushChanged()), this, SLOT(updated()));
184 updated();
197 updated();
185 }
198 }
186
199
187 void PieLegendMarker::updated()
200 void PieLegendMarker::updated()
188 {
201 {
189 setBrush(m_pieslice->brush());
202 setBrush(m_pieslice->brush());
190 setLabel(m_pieslice->label());
203 setLabel(m_pieslice->label());
191 }
204 }
192
205
193 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
206 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
194
207
195 XYLegendMarker::XYLegendMarker(QXYSeries *series, QLegend *legend) : LegendMarker(series,legend),
208 XYLegendMarker::XYLegendMarker(QXYSeries *series, QLegend *legend) : LegendMarker(series,legend),
196 m_series(series)
209 m_series(series)
197 {
210 {
198 //QObject::connect(this, SIGNAL(selected()), series, SIGNAL(selected()));
211 //QObject::connect(this, SIGNAL(selected()), series, SIGNAL(selected()));
199 QObject::connect(series->d_func(),SIGNAL(updated()), this, SLOT(updated()));
212 QObject::connect(series->d_func(),SIGNAL(updated()), this, SLOT(updated()));
200 QObject::connect(series, SIGNAL(nameChanged()), this, SLOT(updated()));
213 QObject::connect(series, SIGNAL(nameChanged()), this, SLOT(updated()));
201 updated();
214 updated();
202 }
215 }
203
216
204 void XYLegendMarker::updated()
217 void XYLegendMarker::updated()
205 {
218 {
206 setLabel(m_series->name());
219 setLabel(m_series->name());
207
220
208 if(m_series->type()== QAbstractSeries::SeriesTypeScatter)
221 if(m_series->type()== QAbstractSeries::SeriesTypeScatter)
209 {
222 {
210 setBrush(m_series->brush());
223 setBrush(m_series->brush());
211
224
212 }
225 }
213 else {
226 else {
214 setBrush(QBrush(m_series->pen().color()));
227 setBrush(QBrush(m_series->pen().color()));
215 }
228 }
216 }
229 }
217
230
218 #include "moc_legendmarker_p.cpp"
231 #include "moc_legendmarker_p.cpp"
219
232
220 QTCOMMERCIALCHART_END_NAMESPACE
233 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,139 +1,144
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 // W A R N I N G
21 // W A R N I N G
22 // -------------
22 // -------------
23 //
23 //
24 // This file is not part of the QtCommercial Chart API. It exists purely as an
24 // This file is not part of the QtCommercial Chart API. It exists purely as an
25 // implementation detail. This header file may change from version to
25 // implementation detail. This header file may change from version to
26 // version without notice, or even be removed.
26 // version without notice, or even be removed.
27 //
27 //
28 // We mean it.
28 // We mean it.
29
29
30 #ifndef LEGENDMARKER_P_H
30 #ifndef LEGENDMARKER_P_H
31 #define LEGENDMARKER_P_H
31 #define LEGENDMARKER_P_H
32
32
33 #include "qchartglobal.h"
33 #include "qchartglobal.h"
34 #include <QGraphicsObject>
34 #include <QGraphicsObject>
35 #include <QBrush>
35 #include <QBrush>
36 #include <QPen>
36 #include <QPen>
37 #include <QGraphicsSimpleTextItem>
37 #include <QGraphicsSimpleTextItem>
38 #include <QGraphicsLayoutItem>
38
39
39 QTCOMMERCIALCHART_BEGIN_NAMESPACE
40 QTCOMMERCIALCHART_BEGIN_NAMESPACE
40
41
41 class QAbstractSeries;
42 class QAbstractSeries;
42 class QAreaSeries;
43 class QAreaSeries;
43 class QXYSeries;
44 class QXYSeries;
44 class QBarSet;
45 class QBarSet;
45 class QBarSeries;
46 class QBarSeries;
46 class QPieSlice;
47 class QPieSlice;
47 class QLegend;
48 class QLegend;
48 class QPieSeries;
49 class QPieSeries;
49
50
50 class LegendMarker : public QGraphicsObject
51 class LegendMarker : public QGraphicsObject, public QGraphicsLayoutItem
51 {
52 {
52 Q_OBJECT
53 Q_OBJECT
53
54 Q_INTERFACES(QGraphicsLayoutItem)
54 public:
55 public:
55 explicit LegendMarker(QAbstractSeries *m_series, QLegend *parent);
56 explicit LegendMarker(QAbstractSeries *m_series, QLegend *parent);
56
57
57 void setPen(const QPen &pen);
58 void setPen(const QPen &pen);
58 QPen pen() const;
59 QPen pen() const;
60
59 void setBrush(const QBrush &brush);
61 void setBrush(const QBrush &brush);
60 QBrush brush() const;
62 QBrush brush() const;
61
63
62 void setFont(const QFont &font);
64 void setFont(const QFont &font);
63 QFont font() const;
65 QFont font() const;
64
66
65 void setSize(const QSize& size);
66
67 void setLabel(const QString label);
67 void setLabel(const QString label);
68 QString label() const;
68 QString label() const;
69
69 void setLabelBrush(const QBrush &brush);
70 void setLabelBrush(const QBrush &brush);
70 QBrush labelBrush() const;
71 QBrush labelBrush() const;
71
72
72 QAbstractSeries *series() const { return m_series;}
73 QAbstractSeries *series() const { return m_series;}
73
74
74 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
75 void setGeometry(const QRectF& rect);
75
76
76 QRectF boundingRect() const;
77 QRectF boundingRect() const;
77
78
78 void updateLayout();
79 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
80
81 QSizeF sizeHint (Qt::SizeHint which, const QSizeF& constraint) const;
79
82
80 protected:
83 protected:
81 // From QGraphicsObject
84 // From QGraphicsObject
82 void mousePressEvent(QGraphicsSceneMouseEvent *event);
85 void mousePressEvent(QGraphicsSceneMouseEvent *event);
83
86
84 public Q_SLOTS:
87 public Q_SLOTS:
85 virtual void updated() = 0;
88 virtual void updated() = 0;
86
89
87 protected:
90 protected:
88 QAbstractSeries *m_series;
91 QAbstractSeries *m_series;
89 QRectF m_markerRect;
92 QRectF m_markerRect;
90 QRectF m_boundingRect;
93 QRectF m_boundingRect;
91 QLegend* m_legend;
94 QLegend* m_legend;
92 QGraphicsSimpleTextItem *m_textItem;
95 QGraphicsSimpleTextItem *m_textItem;
93 QGraphicsRectItem *m_rectItem;
96 QGraphicsRectItem *m_rectItem;
97 qreal m_margin;
98 qreal m_space;
94
99
95 };
100 };
96
101
97 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
102 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
98 class XYLegendMarker : public LegendMarker
103 class XYLegendMarker : public LegendMarker
99 {
104 {
100 public:
105 public:
101 XYLegendMarker(QXYSeries *series, QLegend *legend);
106 XYLegendMarker(QXYSeries *series, QLegend *legend);
102 protected:
107 protected:
103 void updated();
108 void updated();
104 private:
109 private:
105 QXYSeries *m_series;
110 QXYSeries *m_series;
106 };
111 };
107 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
112 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
108 class AreaLegendMarker : public LegendMarker
113 class AreaLegendMarker : public LegendMarker
109 {
114 {
110 public:
115 public:
111 AreaLegendMarker(QAreaSeries *series, QLegend *legend);
116 AreaLegendMarker(QAreaSeries *series, QLegend *legend);
112 protected:
117 protected:
113 void updated();
118 void updated();
114 private:
119 private:
115 QAreaSeries *m_series;
120 QAreaSeries *m_series;
116 };
121 };
117 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
122 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
118 class BarLegendMarker : public LegendMarker
123 class BarLegendMarker : public LegendMarker
119 {
124 {
120 public:
125 public:
121 BarLegendMarker(QBarSeries *barseries, QBarSet *barset,QLegend *legend);
126 BarLegendMarker(QBarSeries *barseries, QBarSet *barset,QLegend *legend);
122 protected:
127 protected:
123 void updated();
128 void updated();
124 private:
129 private:
125 QBarSet *m_barset;
130 QBarSet *m_barset;
126 };
131 };
127 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
132 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
128 class PieLegendMarker : public LegendMarker
133 class PieLegendMarker : public LegendMarker
129 {
134 {
130 public:
135 public:
131 PieLegendMarker(QPieSeries *pieSeries, QPieSlice *pieslice, QLegend *legend);
136 PieLegendMarker(QPieSeries *pieSeries, QPieSlice *pieslice, QLegend *legend);
132 protected:
137 protected:
133 void updated();
138 void updated();
134 private:
139 private:
135 QPieSlice *m_pieslice;
140 QPieSlice *m_pieslice;
136 };
141 };
137
142
138 QTCOMMERCIALCHART_END_NAMESPACE
143 QTCOMMERCIALCHART_END_NAMESPACE
139 #endif // LEGENDMARKER_P_H
144 #endif // LEGENDMARKER_P_H
@@ -1,869 +1,549
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "qlegend.h"
21 #include "qlegend.h"
22 #include "qlegend_p.h"
22 #include "qlegend_p.h"
23 #include "qabstractseries.h"
23 #include "qabstractseries.h"
24 #include "qabstractseries_p.h"
24 #include "qabstractseries_p.h"
25 #include "qchart_p.h"
25 #include "qchart_p.h"
26
26 #include "legendlayout_p.h"
27 #include "legendmarker_p.h"
27 #include "legendmarker_p.h"
28 #include "qxyseries.h"
28 #include "qxyseries.h"
29 #include "qlineseries.h"
29 #include "qlineseries.h"
30 #include "qareaseries.h"
30 #include "qareaseries.h"
31 #include "qscatterseries.h"
31 #include "qscatterseries.h"
32 #include "qsplineseries.h"
32 #include "qsplineseries.h"
33 #include "qbarseries.h"
33 #include "qbarseries.h"
34 #include "qstackedbarseries.h"
34 #include "qstackedbarseries.h"
35 #include "qpercentbarseries.h"
35 #include "qpercentbarseries.h"
36 #include "qbarset.h"
36 #include "qbarset.h"
37 #include "qpieseries.h"
37 #include "qpieseries.h"
38 #include "qpieseries_p.h"
38 #include "qpieseries_p.h"
39 #include "qpieslice.h"
39 #include "qpieslice.h"
40 #include "chartpresenter_p.h"
40 #include "chartpresenter_p.h"
41 #include <QPainter>
41 #include <QPainter>
42 #include <QPen>
42 #include <QPen>
43 #include <QTimer>
43 #include <QTimer>
44
44 #include <QGraphicsLayout>
45 #include <QGraphicsSceneEvent>
45 #include <QGraphicsSceneEvent>
46
46
47 QTCOMMERCIALCHART_BEGIN_NAMESPACE
47 QTCOMMERCIALCHART_BEGIN_NAMESPACE
48
48
49 /*!
49 /*!
50 \class QLegend
50 \class QLegend
51 \brief Legend object
51 \brief Legend object
52 \mainclass
52 \mainclass
53
53
54 QLegend is a graphical object, whics displays legend of the chart. Legend state is updated by QChart, when
54 QLegend is a graphical object, whics displays legend of the chart. Legend state is updated by QChart, when
55 series have been changed. By default, legend is drawn by QChart, but user can set a new parent to legend and
55 series have been changed. By default, legend is drawn by QChart, but user can set a new parent to legend and
56 handle the drawing manually.
56 handle the drawing manually.
57 User isn't supposed to create or delete legend objects, but can reference it via QChart class.
57 User isn't supposed to create or delete legend objects, but can reference it via QChart class.
58
58
59 \image examples_percentbarchart_legend.png
59 \image examples_percentbarchart_legend.png
60
60
61 \sa QChart
61 \sa QChart
62 */
62 */
63 /*!
63 /*!
64 \qmlclass Legend QLegend
64 \qmlclass Legend QLegend
65 \brief Legend is part of QtCommercial Chart QML API.
65 \brief Legend is part of QtCommercial Chart QML API.
66
66
67 Legend is a graphical object, whics displays legend of the chart. Legend state is updated by ChartView, when
67 Legend is a graphical object, whics displays legend of the chart. Legend state is updated by ChartView, when
68 series have been changed. Legend is used via ChartView class. For example:
68 series have been changed. Legend is used via ChartView class. For example:
69 \code
69 \code
70 ChartView {
70 ChartView {
71 legend.visible: true
71 legend.visible: true
72 legend.alignment: Qt.AlignBottom
72 legend.alignment: Qt.AlignBottom
73 // Add a few series...
73 // Add a few series...
74 }
74 }
75 \endcode
75 \endcode
76
76
77 \image examples_percentbarchart_legend.png
77 \image examples_percentbarchart_legend.png
78 */
78 */
79
79
80 /*!
80 /*!
81 \property QLegend::alignment
81 \property QLegend::alignment
82 \brief The alignment of the legend.
82 \brief The alignment of the legend.
83
83
84 Legend paints on the defined position in the chart. The following alignments are supported:
84 Legend paints on the defined position in the chart. The following alignments are supported:
85 Qt::AlignTop, Qt::AlignBottom, Qt::AlignLeft, Qt::AlignRight. If you set more than one flag the result is undefined.
85 Qt::AlignTop, Qt::AlignBottom, Qt::AlignLeft, Qt::AlignRight. If you set more than one flag the result is undefined.
86 */
86 */
87 /*!
87 /*!
88 \qmlproperty Qt.Alignment Legend::alignment
88 \qmlproperty Qt.Alignment Legend::alignment
89 \brief The alignment of the legend.
89 \brief The alignment of the legend.
90
90
91 Legend paints on the defined position in the chart. The following alignments are supported:
91 Legend paints on the defined position in the chart. The following alignments are supported:
92 Qt.AlignTop, Qt.AlignBottom, Qt.AlignLeft, Qt.AlignRight. If you set more than one flag the result is undefined.
92 Qt.AlignTop, Qt.AlignBottom, Qt.AlignLeft, Qt.AlignRight. If you set more than one flag the result is undefined.
93 */
93 */
94
94
95 /*!
95 /*!
96 \property QLegend::backgroundVisible
96 \property QLegend::backgroundVisible
97 Whether the legend background is visible or not.
97 Whether the legend background is visible or not.
98 */
98 */
99 /*!
99 /*!
100 \qmlproperty bool Legend::backgroundVisible
100 \qmlproperty bool Legend::backgroundVisible
101 Whether the legend background is visible or not.
101 Whether the legend background is visible or not.
102 */
102 */
103
103
104 /*!
104 /*!
105 \property QLegend::color
105 \property QLegend::color
106 The color of the legend, i.e. the background (brush) color. Note that if you change the color
106 The color of the legend, i.e. the background (brush) color. Note that if you change the color
107 of the legend, the style of the legend brush is set to Qt::SolidPattern.
107 of the legend, the style of the legend brush is set to Qt::SolidPattern.
108 */
108 */
109 /*!
109 /*!
110 \qmlproperty color Legend::color
110 \qmlproperty color Legend::color
111 The color of the legend, i.e. the background (brush) color.
111 The color of the legend, i.e. the background (brush) color.
112 */
112 */
113
113
114 /*!
114 /*!
115 \property QLegend::borderColor
115 \property QLegend::borderColor
116 The border color of the legend, i.e. the line color.
116 The border color of the legend, i.e. the line color.
117 */
117 */
118 /*!
118 /*!
119 \qmlproperty color Legend::borderColor
119 \qmlproperty color Legend::borderColor
120 The border color of the legend, i.e. the line color.
120 The border color of the legend, i.e. the line color.
121 */
121 */
122
122
123 /*!
123 /*!
124 \property QLegend::font
124 \property QLegend::font
125 The font of markers used by legend
125 The font of markers used by legend
126 */
126 */
127 /*!
127 /*!
128 \qmlproperty color Legend::font
128 \qmlproperty color Legend::font
129 The font of markers used by legend
129 The font of markers used by legend
130 */
130 */
131
131
132 /*!
132 /*!
133 \property QLegend::labelColor
133 \property QLegend::labelColor
134 The color of brush used to draw labels.
134 The color of brush used to draw labels.
135 */
135 */
136 /*!
136 /*!
137 \qmlproperty color QLegend::labelColor
137 \qmlproperty color QLegend::labelColor
138 The color of brush used to draw labels.
138 The color of brush used to draw labels.
139 */
139 */
140
140
141 /*!
141 /*!
142 \fn void QLegend::backgroundVisibleChanged(bool)
142 \fn void QLegend::backgroundVisibleChanged(bool)
143 The visibility of the legend background changed to \a visible.
143 The visibility of the legend background changed to \a visible.
144 */
144 */
145
145
146 /*!
146 /*!
147 \fn void QLegend::colorChanged(QColor)
147 \fn void QLegend::colorChanged(QColor)
148 The color of the legend background changed to \a color.
148 The color of the legend background changed to \a color.
149 */
149 */
150
150
151 /*!
151 /*!
152 \fn void QLegend::borderColorChanged(QColor)
152 \fn void QLegend::borderColorChanged(QColor)
153 The border color of the legend background changed to \a color.
153 The border color of the legend background changed to \a color.
154 */
154 */
155
155
156 /*!
156 /*!
157 \fn void QLegend::fontChanged(QFont)
157 \fn void QLegend::fontChanged(QFont)
158 The font of markers of the legend changed to \a font.
158 The font of markers of the legend changed to \a font.
159 */
159 */
160
160
161 /*!
161 /*!
162 \fn void QLegend::labelBrushChanged(QBrush brush)
162 \fn void QLegend::labelBrushChanged(QBrush brush)
163 This signal is emitted when the brush used to draw labels has changed to \a brush.
163 This signal is emitted when the brush used to draw labels has changed to \a brush.
164 */
164 */
165
165
166 /*!
166 /*!
167 \fn void QLegend::labelColorChanged(QColor color)
167 \fn void QLegend::labelColorChanged(QColor color)
168 This signal is emitted when the color of brush used to draw labels has changed to \a color.
168 This signal is emitted when the color of brush used to draw labels has changed to \a color.
169 */
169 */
170
170
171 /*!
171 /*!
172 \fn qreal QLegend::minWidth() const
172 \fn qreal QLegend::minWidth() const
173 Returns minimum width of the legend
173 Returns minimum width of the legend
174 */
174 */
175
175
176 /*!
176 /*!
177 \fn qreal QLegend::minHeight() const
177 \fn qreal QLegend::minHeight() const
178 Returns minimum height of the legend
178 Returns minimum height of the legend
179 */
179 */
180
180
181 /*!
181 /*!
182 Constructs the legend object and sets the parent to \a parent
182 Constructs the legend object and sets the parent to \a parent
183 */
183 */
184
184
185 QLegend::QLegend(QChart *chart):QGraphicsWidget(chart),
185 QLegend::QLegend(QChart *chart):QGraphicsWidget(chart),
186 d_ptr(new QLegendPrivate(chart->d_ptr->m_presenter,chart,this))
186 d_ptr(new QLegendPrivate(chart->d_ptr->m_presenter,chart,this))
187 {
187 {
188 setZValue(ChartPresenter::LegendZValue);
188 setZValue(ChartPresenter::LegendZValue);
189 setFlags(QGraphicsItem::ItemClipsChildrenToShape);
189 setFlags(QGraphicsItem::ItemClipsChildrenToShape);
190 QObject::connect(chart->d_ptr->m_dataset,SIGNAL(seriesAdded(QAbstractSeries*,Domain*)),d_ptr.data(),SLOT(handleSeriesAdded(QAbstractSeries*,Domain*)));
190 QObject::connect(chart->d_ptr->m_dataset,SIGNAL(seriesAdded(QAbstractSeries*,Domain*)),d_ptr.data(),SLOT(handleSeriesAdded(QAbstractSeries*,Domain*)));
191 QObject::connect(chart->d_ptr->m_dataset,SIGNAL(seriesRemoved(QAbstractSeries*)),d_ptr.data(),SLOT(handleSeriesRemoved(QAbstractSeries*)));
191 QObject::connect(chart->d_ptr->m_dataset,SIGNAL(seriesRemoved(QAbstractSeries*)),d_ptr.data(),SLOT(handleSeriesRemoved(QAbstractSeries*)));
192 QObject::connect(chart->d_ptr->m_dataset,SIGNAL(seriesUpdated(QAbstractSeries*)),d_ptr.data(),SLOT(handleSeriesUpdated(QAbstractSeries*)));
192 QObject::connect(chart->d_ptr->m_dataset,SIGNAL(seriesUpdated(QAbstractSeries*)),d_ptr.data(),SLOT(handleSeriesUpdated(QAbstractSeries*)));
193 setLayout(d_ptr->m_layout);
193 }
194 }
194
195
195 /*!
196 /*!
196 Destroys the legend object. Legend is always owned by a QChart, so an application should never call this.
197 Destroys the legend object. Legend is always owned by a QChart, so an application should never call this.
197 */
198 */
198 QLegend::~QLegend()
199 QLegend::~QLegend()
199 {
200 {
200 }
201 }
201
202
202 /*!
203 /*!
203 \internal
204 \internal
204 */
205 */
205 void QLegend::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
206 void QLegend::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
206 {
207 {
207 Q_UNUSED(option)
208 Q_UNUSED(option)
208 Q_UNUSED(widget)
209 Q_UNUSED(widget)
209 if(!d_ptr->m_backgroundVisible) return;
210 if(!d_ptr->m_backgroundVisible) return;
210
211
211 painter->setOpacity(opacity());
212 painter->setOpacity(opacity());
212 painter->setPen(d_ptr->m_pen);
213 painter->setPen(d_ptr->m_pen);
213 painter->setBrush(d_ptr->m_brush);
214 painter->setBrush(d_ptr->m_brush);
214 painter->drawRoundRect(rect(),d_ptr->roundness(rect().width()),d_ptr->roundness(rect().height()));
215 painter->drawRoundRect(rect(),d_ptr->roundness(rect().width()),d_ptr->roundness(rect().height()));
215 }
216
216
217 /*!
218 \internal
219 */
220 QRectF QLegend::boundingRect() const
221 {
222 return d_ptr->m_rect;
223 }
217 }
224
218
219
225 /*!
220 /*!
226 Sets the \a brush of legend. Brush affects the background of legend.
221 Sets the \a brush of legend. Brush affects the background of legend.
227 */
222 */
228 void QLegend::setBrush(const QBrush &brush)
223 void QLegend::setBrush(const QBrush &brush)
229 {
224 {
230 if (d_ptr->m_brush != brush) {
225 if (d_ptr->m_brush != brush) {
231 d_ptr->m_brush = brush;
226 d_ptr->m_brush = brush;
232 update();
227 update();
233 }
228 }
234 }
229 }
235
230
236 /*!
231 /*!
237 Returns the brush used by legend.
232 Returns the brush used by legend.
238 */
233 */
239 QBrush QLegend::brush() const
234 QBrush QLegend::brush() const
240 {
235 {
241 return d_ptr->m_brush;
236 return d_ptr->m_brush;
242 }
237 }
243
238
244 void QLegend::setColor(QColor color)
239 void QLegend::setColor(QColor color)
245 {
240 {
246 QBrush b = d_ptr->m_brush;
241 QBrush b = d_ptr->m_brush;
247 if (b.style() != Qt::SolidPattern || b.color() != color) {
242 if (b.style() != Qt::SolidPattern || b.color() != color) {
248 b.setStyle(Qt::SolidPattern);
243 b.setStyle(Qt::SolidPattern);
249 b.setColor(color);
244 b.setColor(color);
250 setBrush(b);
245 setBrush(b);
251 emit colorChanged(color);
246 emit colorChanged(color);
252 }
247 }
253 }
248 }
254
249
255 QColor QLegend::color()
250 QColor QLegend::color()
256 {
251 {
257 return d_ptr->m_brush.color();
252 return d_ptr->m_brush.color();
258 }
253 }
259
254
260 /*!
255 /*!
261 Sets the \a pen of legend. Pen affects the legend borders.
256 Sets the \a pen of legend. Pen affects the legend borders.
262 */
257 */
263 void QLegend::setPen(const QPen &pen)
258 void QLegend::setPen(const QPen &pen)
264 {
259 {
265 if (d_ptr->m_pen != pen) {
260 if (d_ptr->m_pen != pen) {
266 d_ptr->m_pen = pen;
261 d_ptr->m_pen = pen;
267 update();
262 update();
268 }
263 }
269 }
264 }
270
265
271 /*!
266 /*!
272 Returns the pen used by legend
267 Returns the pen used by legend
273 */
268 */
274
269
275 QPen QLegend::pen() const
270 QPen QLegend::pen() const
276 {
271 {
277 return d_ptr->m_pen;
272 return d_ptr->m_pen;
278 }
273 }
279
274
275 void QLegend::setFont(const QFont &font)
276 {
277 if (d_ptr->m_font != font) {
278 d_ptr->m_font = font;
279
280 foreach (LegendMarker *marker, d_ptr->markers()) {
281 marker->setFont(d_ptr->m_font);
282 }
283 emit fontChanged(font);
284 }
285 }
286
287 QFont QLegend::font() const
288 {
289 return d_ptr->m_font;
290 }
291
280 void QLegend::setBorderColor(QColor color)
292 void QLegend::setBorderColor(QColor color)
281 {
293 {
282 QPen p = d_ptr->m_pen;
294 QPen p = d_ptr->m_pen;
283 if (p.color() != color) {
295 if (p.color() != color) {
284 p.setColor(color);
296 p.setColor(color);
285 setPen(p);
297 setPen(p);
286 emit borderColorChanged(color);
298 emit borderColorChanged(color);
287 }
299 }
288 }
300 }
289
301
290 QColor QLegend::borderColor()
302 QColor QLegend::borderColor()
291 {
303 {
292 return d_ptr->m_pen.color();
304 return d_ptr->m_pen.color();
293 }
305 }
294
306
295 void QLegend::setFont(const QFont &font)
296 {
297 if (d_ptr->m_font != font) {
298 d_ptr->setFont(font);
299 emit fontChanged(font);
300 }
301 }
302
303 QFont QLegend::font() const
304 {
305 return d_ptr->m_font;
306 }
307
308 /*!
307 /*!
309 Set brush used to draw labels to \a brush.
308 Set brush used to draw labels to \a brush.
310 */
309 */
311 void QLegend::setLabelBrush(const QBrush &brush)
310 void QLegend::setLabelBrush(const QBrush &brush)
312 {
311 {
313 if (d_ptr->m_labelBrush != brush) {
312 if (d_ptr->m_labelBrush != brush) {
314 d_ptr->setLabelBrush(brush);
313
314 d_ptr->m_labelBrush = brush;
315
316 foreach (LegendMarker *marker, d_ptr->markers()) {
317 marker->setLabelBrush(d_ptr->m_labelBrush);
318 }
315 emit labelBrushChanged(brush);
319 emit labelBrushChanged(brush);
316 }
320 }
317 }
321 }
318
322
319 /*!
323 /*!
320 Brush used to draw labels.
324 Brush used to draw labels.
321 */
325 */
322 QBrush QLegend::labelBrush() const
326 QBrush QLegend::labelBrush() const
323 {
327 {
324 return d_ptr->m_labelBrush;
328 return d_ptr->m_labelBrush;
325 }
329 }
326
330
327 void QLegend::setLabelColor(QColor color)
331 void QLegend::setLabelColor(QColor color)
328 {
332 {
329 QBrush b = d_ptr->m_labelBrush;
333 QBrush b = d_ptr->m_labelBrush;
330 if (b.style() != Qt::SolidPattern || b.color() != color) {
334 if (b.style() != Qt::SolidPattern || b.color() != color) {
331 b.setStyle(Qt::SolidPattern);
335 b.setStyle(Qt::SolidPattern);
332 b.setColor(color);
336 b.setColor(color);
333 setLabelBrush(b);
337 setLabelBrush(b);
334 emit labelColorChanged(color);
338 emit labelColorChanged(color);
335 }
339 }
336 }
340 }
337
341
338 QColor QLegend::labelColor() const
342 QColor QLegend::labelColor() const
339 {
343 {
340 return d_ptr->m_labelBrush.color();
344 return d_ptr->m_labelBrush.color();
341 }
345 }
342
346
347
343 void QLegend::setAlignment(Qt::Alignment alignment)
348 void QLegend::setAlignment(Qt::Alignment alignment)
344 {
349 {
345 if(d_ptr->m_alignment!=alignment) {
350 if(d_ptr->m_alignment!=alignment) {
346 d_ptr->m_alignment = alignment;
351 d_ptr->m_alignment = alignment;
347 d_ptr->updateLayout();
352 updateGeometry();
353 if(isAttachedToChart()){
354 d_ptr->m_presenter->layout()->invalidate();
355 }else{
356 layout()->invalidate();
357 }
348 }
358 }
349 }
359 }
350
360
351 Qt::Alignment QLegend::alignment() const
361 Qt::Alignment QLegend::alignment() const
352 {
362 {
353 return d_ptr->m_alignment;
363 return d_ptr->m_alignment;
354 }
364 }
355
365
356 /*!
366 /*!
357 Detaches the legend from chart. Chart won't change layout of the legend.
367 Detaches the legend from chart. Chart won't change layout of the legend.
358 */
368 */
359 void QLegend::detachFromChart()
369 void QLegend::detachFromChart()
360 {
370 {
361 d_ptr->m_attachedToChart = false;
371 d_ptr->m_attachedToChart = false;
372 d_ptr->m_layout->invalidate();
373 setParent(0);
374
362 }
375 }
363
376
364 /*!
377 /*!
365 Attaches the legend to chart. Chart may change layout of the legend.
378 Attaches the legend to chart. Chart may change layout of the legend.
366 */
379 */
367 void QLegend::attachToChart()
380 void QLegend::attachToChart()
368 {
381 {
369 d_ptr->attachToChart();
382 d_ptr->m_attachedToChart = true;
383 d_ptr->m_layout->invalidate();
384 setParent(d_ptr->m_chart);
370 }
385 }
371
386
372 /*!
387 /*!
373 Returns true, if legend is attached to chart.
388 Returns true, if legend is attached to chart.
374 */
389 */
375 bool QLegend::isAttachedToChart()
390 bool QLegend::isAttachedToChart()
376 {
391 {
377 return d_ptr->m_attachedToChart;
392 return d_ptr->m_attachedToChart;
378 }
393 }
379
394
380 /*!
395 /*!
381 Sets the visibility of legend background to \a visible
396 Sets the visibility of legend background to \a visible
382 */
397 */
383 void QLegend::setBackgroundVisible(bool visible)
398 void QLegend::setBackgroundVisible(bool visible)
384 {
399 {
385 if(d_ptr->m_backgroundVisible != visible) {
400 if(d_ptr->m_backgroundVisible != visible) {
386 d_ptr->m_backgroundVisible = visible;
401 d_ptr->m_backgroundVisible = visible;
387 update();
402 update();
388 emit backgroundVisibleChanged(visible);
403 emit backgroundVisibleChanged(visible);
389 }
404 }
390 }
405 }
391
406
392 /*!
407 /*!
393 Returns the visibility of legend background
408 Returns the visibility of legend background
394 */
409 */
395 bool QLegend::isBackgroundVisible() const
410 bool QLegend::isBackgroundVisible() const
396 {
411 {
397 return d_ptr->m_backgroundVisible;
412 return d_ptr->m_backgroundVisible;
398 }
413 }
399
414
400 /*!
415 /*!
401 \internal \a event see QGraphicsWidget for details
416 \internal \a event see QGraphicsWidget for details
402 */
417 */
403 void QLegend::resizeEvent(QGraphicsSceneResizeEvent *event)
404 {
405 const QRectF& rect = QRectF(QPoint(0,0),event->newSize());
406 QGraphicsWidget::resizeEvent(event);
407 if(d_ptr->m_rect != rect) {
408 d_ptr->m_rect = rect;
409 d_ptr->updateLayout();
410 }
411 }
412
413 /*!
414 \internal \a event see QGraphicsWidget for details
415 */
416 void QLegend::hideEvent(QHideEvent *event)
418 void QLegend::hideEvent(QHideEvent *event)
417 {
419 {
418 QGraphicsWidget::hideEvent(event);
420 QGraphicsWidget::hideEvent(event);
419 setEnabled(false);
421 d_ptr->m_presenter->layout()->invalidate();
420 d_ptr->updateLayout();
421 }
422 }
422
423
423 /*!
424 /*!
424 \internal \a event see QGraphicsWidget for details
425 \internal \a event see QGraphicsWidget for details
425 */
426 */
426 void QLegend::showEvent(QShowEvent *event)
427 void QLegend::showEvent(QShowEvent *event)
427 {
428 {
428 QGraphicsWidget::showEvent(event);
429 QGraphicsWidget::showEvent(event);
429 setEnabled(true);
430 d_ptr->m_presenter->layout()->invalidate();
430 d_ptr->updateLayout();
431 }
432
433 qreal QLegend::minWidth() const
434 {
435 return d_ptr->m_minWidth;
436 }
437
438 qreal QLegend::minHeight() const
439 {
440 return d_ptr->m_minHeight;
441 }
431 }
442
432
443 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
433 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
444
434
445 QLegendPrivate::QLegendPrivate(ChartPresenter* presenter, QChart *chart, QLegend *q):
435 QLegendPrivate::QLegendPrivate(ChartPresenter* presenter, QChart *chart, QLegend *q):
446 q_ptr(q),
436 q_ptr(q),
447 m_presenter(presenter),
437 m_presenter(presenter),
438 m_layout(new LegendLayout(q)),
448 m_chart(chart),
439 m_chart(chart),
449 m_markers(new QGraphicsItemGroup(q)),
440 m_items(new QGraphicsItemGroup(q)),
450 m_alignment(Qt::AlignTop),
441 m_alignment(Qt::AlignTop),
451 m_brush(QBrush()),
442 m_brush(QBrush()),
452 m_pen(QPen()),
443 m_pen(QPen()),
453 m_labelBrush(QBrush()),
444 m_labelBrush(QBrush()),
454 m_offsetX(0),
455 m_offsetY(0),
456 m_minWidth(0),
457 m_minHeight(0),
458 m_width(0),
459 m_height(0),
460 m_diameter(5),
445 m_diameter(5),
461 m_attachedToChart(true),
446 m_attachedToChart(true),
462 m_backgroundVisible(false)
447 m_backgroundVisible(false)
463 {
448 {
464
449
465 }
450 }
466
451
467 QLegendPrivate::~QLegendPrivate()
452 QLegendPrivate::~QLegendPrivate()
468 {
453 {
469
454
470 }
455 }
471
456
472 void QLegendPrivate::setOffset(qreal x, qreal y)
457 void QLegendPrivate::setOffset(qreal x, qreal y)
473 {
458 {
474 bool scrollHorizontal = true;
459 m_layout->setOffset(x,y);
475 switch(m_alignment) {
476 case Qt::AlignTop:
477 case Qt::AlignBottom: {
478 scrollHorizontal = true;
479 break;
480 }
481 case Qt::AlignLeft:
482 case Qt::AlignRight: {
483 scrollHorizontal = false;
484 break;
485 }
486 }
487
488 // If detached, the scrolling direction is vertical instead of horizontal and vice versa.
489 if (!m_attachedToChart) {
490 scrollHorizontal = !scrollHorizontal;
491 }
492
493 // Limit offset between m_minOffset and m_maxOffset
494 if (scrollHorizontal) {
495 if(m_width<=m_rect.width()) return;
496
497 if (x != m_offsetX) {
498 m_offsetX = qBound(m_minOffsetX, x, m_maxOffsetX);
499 m_markers->setPos(-m_offsetX,m_rect.top());
500 }
501 } else {
502 if(m_height<=m_rect.height()) return;
503
504 if (y != m_offsetY) {
505 m_offsetY = qBound(m_minOffsetY, y, m_maxOffsetY);
506 m_markers->setPos(m_rect.left(),-m_offsetY);
507 }
508 }
509 }
460 }
510
461
511 QPointF QLegendPrivate::offset() const
462 QPointF QLegendPrivate::offset() const
512 {
463 {
513 return QPointF(m_offsetX,m_offsetY);
464 return m_layout->offset();
514 }
515
516 void QLegendPrivate::updateLayout()
517 {
518 if (!m_attachedToChart) {
519 updateDetachedLayout();
520 return;
521 }
522
523 m_offsetX=0;
524 QList<QGraphicsItem *> items = m_markers->childItems();
525
526 if(items.isEmpty()) return;
527
528 m_minWidth=0;
529 m_minHeight=0;
530
531 switch(m_alignment) {
532
533 case Qt::AlignTop:
534 case Qt::AlignBottom: {
535 QPointF point = m_rect.topLeft();
536 m_width = 0;
537 foreach (QGraphicsItem *item, items) {
538 if (item->isVisible()) {
539 item->setPos(point.x(),m_rect.height()/2 -item->boundingRect().height()/2);
540 const QRectF& rect = item->boundingRect();
541 qreal w = rect.width();
542 m_minWidth=qMax(m_minWidth,w);
543 m_minHeight=qMax(m_minHeight,rect.height());
544 m_width+=w;
545 point.setX(point.x() + w);
546 }
547 }
548 if(m_width<m_rect.width()) {
549 m_markers->setPos(m_rect.width()/2-m_width/2,m_rect.top());
550 }
551 else {
552 m_markers->setPos(m_rect.topLeft());
553 }
554 m_height=m_minHeight;
555 }
556 break;
557 case Qt::AlignLeft:
558 case Qt::AlignRight: {
559 QPointF point = m_rect.topLeft();
560 m_height = 0;
561 foreach (QGraphicsItem *item, items) {
562 if (item->isVisible()) {
563 item->setPos(point);
564 const QRectF& rect = item->boundingRect();
565 qreal h = rect.height();
566 m_minWidth=qMax(m_minWidth,rect.width());
567 m_minHeight=qMax(m_minHeight,h);
568 m_height+=h;
569 point.setY(point.y() + h);
570 }
571 }
572 if(m_height<m_rect.height()) {
573 m_markers->setPos(m_rect.left(),m_rect.height()/2-m_height/2);
574 }
575 else {
576 m_markers->setPos(m_rect.topLeft());
577 }
578 m_width=m_minWidth;
579 }
580 break;
581 }
582
583 m_minOffsetX = 0;
584 m_minOffsetY = 0;
585 m_maxOffsetX = m_width - m_rect.width();
586 m_maxOffsetY = m_height - m_rect.height();
587
588 m_presenter->updateLayout();
589 }
590
591 void QLegendPrivate::updateDetachedLayout()
592 {
593 // Detached layout is different.
594 // In detached mode legend may have multiple rows and columns, so layout calculations
595 // differ a log from attached mode.
596 // Also the scrolling logic is bit different.
597 m_offsetX=0;
598 m_offsetY=0;
599 QList<QGraphicsItem *> items = m_markers->childItems();
600
601 if(items.isEmpty()) return;
602
603 m_minWidth = 0;
604 m_minHeight = 0;
605
606 switch (m_alignment) {
607 case Qt::AlignTop: {
608 QPointF point = m_rect.topLeft();
609 m_width = 0;
610 m_height = 0;
611 for (int i=0; i<items.count(); i++) {
612 QGraphicsItem *item = items.at(i);
613 if (item->isVisible()) {
614 const QRectF& rect = item->boundingRect();
615 qreal w = rect.width();
616 qreal h = rect.height();
617 m_minWidth = qMax(m_minWidth,w);
618 m_minHeight = qMax(m_minHeight,rect.height());
619 m_height = qMax(m_height,h);
620 item->setPos(point.x(),point.y());
621 point.setX(point.x() + w);
622 if (point.x() + w > m_rect.topLeft().x() + m_rect.width()) {
623 // Next item would go off rect.
624 point.setX(m_rect.topLeft().x());
625 point.setY(point.y() + h);
626 if (i+1 < items.count()) {
627 m_height += h;
628 }
629 }
630 }
631 }
632 m_markers->setPos(m_rect.topLeft());
633 m_width = m_minWidth;
634
635 m_minOffsetX = 0;
636 m_minOffsetY = 0;
637 m_maxOffsetX = m_width - m_rect.width();
638 m_maxOffsetY = m_height - m_rect.height();
639 }
640 break;
641 case Qt::AlignBottom: {
642 QPointF point = m_rect.bottomLeft();
643 m_width = 0;
644 m_height = 0;
645 for (int i=0; i<items.count(); i++) {
646 QGraphicsItem *item = items.at(i);
647 if (item->isVisible()) {
648 const QRectF& rect = item->boundingRect();
649 qreal w = rect.width();
650 qreal h = rect.height();
651 m_minWidth = qMax(m_minWidth,w);
652 m_minHeight = qMax(m_minHeight,rect.height());
653 m_height = qMax(m_height,h);
654 item->setPos(point.x(),point.y() - h);
655 point.setX(point.x() + w);
656 if (point.x() + w > m_rect.bottomLeft().x() + m_rect.width()) {
657 // Next item would go off rect.
658 point.setX(m_rect.bottomLeft().x());
659 point.setY(point.y() - h);
660 if (i+1 < items.count()) {
661 m_height += h;
662 }
663 }
664 }
665 }
666 m_markers->setPos(m_rect.topLeft());
667 m_width = m_minWidth;
668
669 m_minOffsetX = 0;
670 m_minOffsetY = qMin(m_rect.topLeft().y(), m_rect.topLeft().y() - m_height + m_rect.height());
671 m_maxOffsetX = m_width - m_rect.width();
672 m_maxOffsetY = 0;
673 }
674 break;
675 case Qt::AlignLeft: {
676 QPointF point = m_rect.topLeft();
677 m_width = 0;
678 m_height = 0;
679 qreal maxWidth = 0;
680 for (int i=0; i<items.count(); i++) {
681 QGraphicsItem *item = items.at(i);
682 if (item->isVisible()) {
683 const QRectF& rect = item->boundingRect();
684 qreal w = rect.width();
685 qreal h = rect.height();
686 m_minWidth = qMax(m_minWidth,rect.width());
687 m_minHeight = qMax(m_minHeight,h);
688 maxWidth = qMax(maxWidth,w);
689 item->setPos(point.x(),point.y());
690 point.setY(point.y() + h);
691 if (point.y() + h > m_rect.topLeft().y() + m_rect.height()) {
692 // Next item would go off rect.
693 point.setX(point.x() + maxWidth);
694 point.setY(m_rect.topLeft().y());
695 if (i+1 < items.count()) {
696 m_width += maxWidth;
697 maxWidth = 0;
698 }
699 }
700 }
701 }
702 m_width += maxWidth;
703 m_markers->setPos(m_rect.topLeft());
704 m_height = m_minHeight;
705
706 m_minOffsetX = 0;
707 m_minOffsetY = 0;
708 m_maxOffsetX = m_width - m_rect.width();
709 m_maxOffsetY = m_height - m_rect.height();
710 }
711 break;
712 case Qt::AlignRight: {
713 QPointF point = m_rect.topRight();
714 m_width = 0;
715 m_height = 0;
716 qreal maxWidth = 0;
717 for (int i=0; i<items.count(); i++) {
718 QGraphicsItem *item = items.at(i);
719 if (item->isVisible()) {
720 const QRectF& rect = item->boundingRect();
721 qreal w = rect.width();
722 qreal h = rect.height();
723 m_minWidth = qMax(m_minWidth,rect.width());
724 m_minHeight = qMax(m_minHeight,h);
725 maxWidth = qMax(maxWidth,w);
726 item->setPos(point.x() - w,point.y());
727 point.setY(point.y() + h);
728 if (point.y() + h > m_rect.topLeft().y() + m_rect.height()) {
729 // Next item would go off rect.
730 point.setX(point.x() - maxWidth);
731 point.setY(m_rect.topLeft().y());
732 if (i+1 < items.count()) {
733 m_width += maxWidth;
734 maxWidth = 0;
735 }
736 }
737 }
738 }
739 m_width += maxWidth;
740 m_markers->setPos(m_rect.topLeft());
741 m_height = m_minHeight;
742
743 m_minOffsetX = qMin(m_rect.topLeft().x(), m_rect.topLeft().x() - m_width + m_rect.width());
744 m_minOffsetY = 0;
745 m_maxOffsetX = 0;
746 m_maxOffsetY = m_height - m_rect.height();
747 }
748 break;
749 default:
750 break;
751 }
752 }
753
754 void QLegendPrivate::attachToChart()
755 {
756 m_attachedToChart = true;
757 q_ptr->setParent(m_chart);
758 }
465 }
759
466
760 int QLegendPrivate::roundness(qreal size)
467 int QLegendPrivate::roundness(qreal size)
761 {
468 {
762 return 100*m_diameter/int(size);
469 return 100*m_diameter/int(size);
763 }
470 }
764
471
765 void QLegendPrivate::setFont(const QFont &font)
766 {
767 m_font = font;
768 QList<QGraphicsItem *> items = m_markers->childItems();
769
770 foreach (QGraphicsItem *markers, items) {
771 LegendMarker *marker = static_cast<LegendMarker*>(markers);
772 marker->setFont(m_font);
773 }
774 updateLayout();
775 }
776
777 void QLegendPrivate::setLabelBrush(const QBrush &brush)
778 {
779 m_labelBrush = brush;
780 QList<QGraphicsItem *> items = m_markers->childItems();
781
782 foreach (QGraphicsItem *markers, items) {
783 LegendMarker *marker = static_cast<LegendMarker*>(markers);
784 marker->setLabelBrush(m_labelBrush);
785 }
786 updateLayout();
787 }
788
789 void QLegendPrivate::handleSeriesAdded(QAbstractSeries *series, Domain *domain)
472 void QLegendPrivate::handleSeriesAdded(QAbstractSeries *series, Domain *domain)
790 {
473 {
791 Q_UNUSED(domain)
474 Q_UNUSED(domain)
792
475
793 QList<LegendMarker*> markers = series->d_ptr->createLegendMarker(q_ptr);
476 QList<LegendMarker*> markers = series->d_ptr->createLegendMarker(q_ptr);
477
794 foreach(LegendMarker* marker, markers) {
478 foreach(LegendMarker* marker, markers) {
795 marker->setFont(m_font);
479 marker->setFont(m_font);
796 marker->setLabelBrush(m_labelBrush);
480 marker->setLabelBrush(m_labelBrush);
797 m_markers->addToGroup(marker);
481 m_items->addToGroup(marker);
482 m_markers<<marker;
798 }
483 }
799
484
800 QObject::connect(series, SIGNAL(visibleChanged()), this, SLOT(handleSeriesVisibleChanged()));
485 QObject::connect(series, SIGNAL(visibleChanged()), this, SLOT(handleSeriesVisibleChanged()));
801
486
802 if(series->type() == QAbstractSeries::SeriesTypePie) {
487 if(series->type() == QAbstractSeries::SeriesTypePie) {
803 QPieSeries *pieSeries = static_cast<QPieSeries *>(series);
488 QPieSeries *pieSeries = static_cast<QPieSeries *>(series);
804 QObject::connect(pieSeries, SIGNAL(added(QList<QPieSlice*>)), this, SLOT(handleUpdatePieSeries()));
489 QObject::connect(pieSeries, SIGNAL(added(QList<QPieSlice*>)), this, SLOT(handleUpdatePieSeries()));
805 QObject::connect(pieSeries, SIGNAL(removed(QList<QPieSlice*>)), this, SLOT(handleUpdatePieSeries()));
490 QObject::connect(pieSeries, SIGNAL(removed(QList<QPieSlice*>)), this, SLOT(handleUpdatePieSeries()));
806 }
491 }
807
492
808 updateLayout();
493 q_ptr->layout()->invalidate();
809 }
494 }
810
495
811 void QLegendPrivate::handleSeriesRemoved(QAbstractSeries *series)
496 void QLegendPrivate::handleSeriesRemoved(QAbstractSeries *series)
812 {
497 {
813 QList<QGraphicsItem *> items = m_markers->childItems();
498 foreach (LegendMarker *marker, m_markers) {
814
815 foreach (QGraphicsItem *markers, items) {
816 LegendMarker *marker = static_cast<LegendMarker*>(markers);
817 if (marker->series() == series) {
499 if (marker->series() == series) {
818 delete marker;
500 delete marker;
501 m_markers.removeAll(marker);
819 }
502 }
820 }
503 }
821
504
822 if(series->type() == QAbstractSeries::SeriesTypePie)
505 if(series->type() == QAbstractSeries::SeriesTypePie)
823 {
506 {
824 QPieSeries *pieSeries = static_cast<QPieSeries *>(series);
507 QPieSeries *pieSeries = static_cast<QPieSeries *>(series);
825 QObject::disconnect(pieSeries, SIGNAL(added(QList<QPieSlice*>)), this, SLOT(handleUpdatePieSeries()));
508 QObject::disconnect(pieSeries, SIGNAL(added(QList<QPieSlice*>)), this, SLOT(handleUpdatePieSeries()));
826 QObject::disconnect(pieSeries, SIGNAL(removed(QList<QPieSlice*>)), this, SLOT(handleUpdatePieSeries()));
509 QObject::disconnect(pieSeries, SIGNAL(removed(QList<QPieSlice*>)), this, SLOT(handleUpdatePieSeries()));
827 }
510 }
828
511
829 updateLayout();
512 q_ptr->layout()->invalidate();
830 }
513 }
831
514
832 void QLegendPrivate::handleSeriesUpdated(QAbstractSeries *series)
515 void QLegendPrivate::handleSeriesUpdated(QAbstractSeries *series)
833 {
516 {
834 // TODO: find out which markers are are added or removed. Update them
517 // TODO: find out which markers are are added or removed. Update them
835 // TODO: better implementation
518 // TODO: better implementation
836 handleSeriesRemoved(series);
519 handleSeriesRemoved(series);
837 Domain domain;
520 Domain domain;
838 handleSeriesAdded(series, &domain);
521 handleSeriesAdded(series, &domain);
839 }
522 }
840
523
841 void QLegendPrivate::handleUpdatePieSeries()
524 void QLegendPrivate::handleUpdatePieSeries()
842 {
525 {
843 //TODO: reimplement to be optimal
526 //TODO: reimplement to be optimal
844 QPieSeries* series = qobject_cast<QPieSeries *> (sender());
527 QPieSeries* series = qobject_cast<QPieSeries *> (sender());
845 Q_ASSERT(series);
528 Q_ASSERT(series);
846 handleSeriesRemoved(series);
529 handleSeriesRemoved(series);
847 handleSeriesAdded(series, 0);
530 handleSeriesAdded(series, 0);
848 }
531 }
849
532
850 void QLegendPrivate::handleSeriesVisibleChanged()
533 void QLegendPrivate::handleSeriesVisibleChanged()
851 {
534 {
852 QAbstractSeries* series = qobject_cast<QAbstractSeries *> (sender());
535 QAbstractSeries* series = qobject_cast<QAbstractSeries *> (sender());
853 QList<QGraphicsItem *> items = m_markers->childItems();
854
536
855 foreach (QGraphicsItem *markers, items) {
537 foreach (LegendMarker* marker, m_markers) {
856 LegendMarker *marker = static_cast<LegendMarker*>(markers);
857 if (marker->series() == series) {
538 if (marker->series() == series) {
858 marker->setVisible(!marker->isVisible());
539 marker->setVisible(!marker->isVisible());
859 }
540 }
860 }
541 }
861
542
862 updateLayout();
543 q_ptr->layout()->invalidate();
863 }
544 }
864
545
865
866 #include "moc_qlegend.cpp"
546 #include "moc_qlegend.cpp"
867 #include "moc_qlegend_p.cpp"
547 #include "moc_qlegend_p.cpp"
868
548
869 QTCOMMERCIALCHART_END_NAMESPACE
549 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,114 +1,112
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #ifndef QLEGEND_H
21 #ifndef QLEGEND_H
22 #define QLEGEND_H
22 #define QLEGEND_H
23
23
24 #include <QChartGlobal>
24 #include <QChartGlobal>
25 #include <QGraphicsWidget>
25 #include <QGraphicsWidget>
26 #include <QPen>
26 #include <QPen>
27 #include <QBrush>
27 #include <QBrush>
28
28
29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30
30
31 class Domain;
31 class Domain;
32 class LegendMarker;
32 class LegendMarker;
33 class QPieSlice;
33 class QPieSlice;
34 class QXYSeries;
34 class QXYSeries;
35 class QBarSet;
35 class QBarSet;
36 class QBarSeries;
36 class QBarSeries;
37 class QPieSeries;
37 class QPieSeries;
38 class QAreaSeries;
38 class QAreaSeries;
39 class LegendScrollButton;
39 class LegendScrollButton;
40 class QChart;
40 class QChart;
41 class QLegendPrivate;
41 class QLegendPrivate;
42
42
43 class QTCOMMERCIALCHART_EXPORT QLegend : public QGraphicsWidget
43 class QTCOMMERCIALCHART_EXPORT QLegend : public QGraphicsWidget
44 {
44 {
45 Q_OBJECT
45 Q_OBJECT
46 Q_PROPERTY(Qt::Alignment alignment READ alignment WRITE setAlignment)
46 Q_PROPERTY(Qt::Alignment alignment READ alignment WRITE setAlignment)
47 Q_PROPERTY(bool backgroundVisible READ isBackgroundVisible WRITE setBackgroundVisible NOTIFY backgroundVisibleChanged)
47 Q_PROPERTY(bool backgroundVisible READ isBackgroundVisible WRITE setBackgroundVisible NOTIFY backgroundVisibleChanged)
48 Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY colorChanged)
48 Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY colorChanged)
49 Q_PROPERTY(QColor borderColor READ borderColor WRITE setBorderColor NOTIFY borderColorChanged)
49 Q_PROPERTY(QColor borderColor READ borderColor WRITE setBorderColor NOTIFY borderColorChanged)
50 Q_PROPERTY(QFont font READ font WRITE setFont NOTIFY fontChanged)
50 Q_PROPERTY(QFont font READ font WRITE setFont NOTIFY fontChanged)
51 Q_PROPERTY(QColor labelColor READ labelColor WRITE setLabelColor NOTIFY labelColorChanged)
51 Q_PROPERTY(QColor labelColor READ labelColor WRITE setLabelColor NOTIFY labelColorChanged)
52
52
53 private:
53 private:
54 explicit QLegend(QChart *chart);
54 explicit QLegend(QChart *chart);
55
55
56 public:
56 public:
57 ~QLegend();
57 ~QLegend();
58
58
59 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
59 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
60 QRectF boundingRect() const;
61
60
62 void setBrush(const QBrush &brush);
61 void setBrush(const QBrush &brush);
63 QBrush brush() const;
62 QBrush brush() const;
64 void setColor(QColor color);
63 void setColor(QColor color);
65 QColor color();
64 QColor color();
66
65
67 void setPen(const QPen &pen);
66 void setPen(const QPen &pen);
68 QPen pen() const;
67 QPen pen() const;
69 void setBorderColor(QColor color);
68 void setBorderColor(QColor color);
70 QColor borderColor();
69 QColor borderColor();
71
70
72 void setFont(const QFont &font);
71 void setFont(const QFont &font);
73 QFont font() const;
72 QFont font() const;
74 void setLabelBrush(const QBrush &brush);
73 void setLabelBrush(const QBrush &brush);
75 QBrush labelBrush() const;
74 QBrush labelBrush() const;
76
75
77 void setLabelColor(QColor color);
76 void setLabelColor(QColor color);
78 QColor labelColor() const;
77 QColor labelColor() const;
79
78
80 void setAlignment(Qt::Alignment alignment);
79 void setAlignment(Qt::Alignment alignment);
81 Qt::Alignment alignment() const;
80 Qt::Alignment alignment() const;
82
81
83 void detachFromChart();
82 void detachFromChart();
84 void attachToChart();
83 void attachToChart();
85 bool isAttachedToChart();
84 bool isAttachedToChart();
86
85
87 qreal minWidth() const;
88 qreal minHeight() const;
89
90 void setBackgroundVisible(bool visible = true);
86 void setBackgroundVisible(bool visible = true);
91 bool isBackgroundVisible() const;
87 bool isBackgroundVisible() const;
92
88
89
93 protected:
90 protected:
94 void resizeEvent(QGraphicsSceneResizeEvent *event);
95 void hideEvent(QHideEvent *event);
91 void hideEvent(QHideEvent *event);
96 void showEvent(QShowEvent *event);
92 void showEvent(QShowEvent *event);
97
93
98 Q_SIGNALS:
94 Q_SIGNALS:
99 void backgroundVisibleChanged(bool visible);
95 void backgroundVisibleChanged(bool visible);
100 void colorChanged(QColor color);
96 void colorChanged(QColor color);
101 void borderColorChanged(QColor color);
97 void borderColorChanged(QColor color);
102 void fontChanged(QFont font);
98 void fontChanged(QFont font);
103 void labelBrushChanged(QBrush brush);
99 void labelBrushChanged(QBrush brush);
104 void labelColorChanged(QColor color);
100 void labelColorChanged(QColor color);
105
101
106 private:
102 private:
107 QScopedPointer<QLegendPrivate> d_ptr;
103 QScopedPointer<QLegendPrivate> d_ptr;
108 Q_DISABLE_COPY(QLegend)
104 Q_DISABLE_COPY(QLegend)
109 friend class LegendScroller;
105 friend class LegendScroller;
106 friend class LegendLayout;
107 friend class ChartLayout;
110 };
108 };
111
109
112 QTCOMMERCIALCHART_END_NAMESPACE
110 QTCOMMERCIALCHART_END_NAMESPACE
113
111
114 #endif // QLEGEND_H
112 #endif // QLEGEND_H
@@ -1,95 +1,86
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 // W A R N I N G
21 // W A R N I N G
22 // -------------
22 // -------------
23 //
23 //
24 // This file is not part of the QtCommercial Chart API. It exists purely as an
24 // This file is not part of the QtCommercial Chart API. It exists purely as an
25 // implementation detail. This header file may change from version to
25 // implementation detail. This header file may change from version to
26 // version without notice, or even be removed.
26 // version without notice, or even be removed.
27 //
27 //
28 // We mean it.
28 // We mean it.
29
29
30 #ifndef QLEGEND_P_H
30 #ifndef QLEGEND_P_H
31 #define QLEGEND_P_H
31 #define QLEGEND_P_H
32
32
33 #include "qlegend.h"
33 #include "qlegend.h"
34
34
35 QTCOMMERCIALCHART_BEGIN_NAMESPACE
35 QTCOMMERCIALCHART_BEGIN_NAMESPACE
36
36
37 class QChart;
37 class QChart;
38 class ChartPresenter;
38 class ChartPresenter;
39 class QAbstractSeries;
39 class QAbstractSeries;
40 class LegendLayout;
40
41
41 class QLegendPrivate : public QObject
42 class QLegendPrivate : public QObject
42 {
43 {
43 Q_OBJECT
44 Q_OBJECT
44 public:
45 public:
45 QLegendPrivate(ChartPresenter *presenter, QChart *chart, QLegend *q);
46 QLegendPrivate(ChartPresenter *presenter, QChart *chart, QLegend *q);
46 ~QLegendPrivate();
47 ~QLegendPrivate();
47
48
48 void setOffset(qreal x, qreal y);
49 void setOffset(qreal x, qreal y);
49 QPointF offset() const;
50 QPointF offset() const;
50 void updateLayout();
51 void updateDetachedLayout();
52 void attachToChart();
53 int roundness(qreal size);
51 int roundness(qreal size);
54 void setFont(const QFont &font);
52
55 void setLabelBrush(const QBrush &brush);
53 QList<LegendMarker*> markers() { return m_markers; }
54 QGraphicsItemGroup* items() { return m_items; }
56
55
57 public Q_SLOTS:
56 public Q_SLOTS:
58 void handleSeriesAdded(QAbstractSeries *series, Domain *domain);
57 void handleSeriesAdded(QAbstractSeries *series, Domain *domain);
59 void handleSeriesRemoved(QAbstractSeries *series);
58 void handleSeriesRemoved(QAbstractSeries *series);
60 void handleSeriesUpdated(QAbstractSeries *series);
59 void handleSeriesUpdated(QAbstractSeries *series);
61 void handleUpdatePieSeries(); //TODO remove this function
60 void handleUpdatePieSeries(); //TODO remove this function
62 void handleSeriesVisibleChanged();
61 void handleSeriesVisibleChanged();
63
62
64 private:
63 private:
65 QLegend *q_ptr;
64 QLegend *q_ptr;
66 ChartPresenter *m_presenter;
65 ChartPresenter *m_presenter;
66 LegendLayout *m_layout;
67 QChart* m_chart;
67 QChart* m_chart;
68 QGraphicsItemGroup* m_markers;
68 QGraphicsItemGroup* m_items;
69 QList<LegendMarker*> m_markers;
69 Qt::Alignment m_alignment;
70 Qt::Alignment m_alignment;
70 QBrush m_brush;
71 QBrush m_brush;
71 QPen m_pen;
72 QPen m_pen;
72 QFont m_font;
73 QFont m_font;
73 QBrush m_labelBrush;
74 QBrush m_labelBrush;
74 QRectF m_rect;
75
75 qreal m_offsetX;
76 qreal m_offsetY;
77 qreal m_minOffsetX;
78 qreal m_minOffsetY;
79 qreal m_maxOffsetX;
80 qreal m_maxOffsetY;
81 qreal m_minWidth;
82 qreal m_minHeight;
83 qreal m_width;
84 qreal m_height;
85 qreal m_diameter;
76 qreal m_diameter;
86 bool m_attachedToChart;
77 bool m_attachedToChart;
87 bool m_backgroundVisible;
78 bool m_backgroundVisible;
88
79
89 friend class QLegend;
80 friend class QLegend;
90
81
91 };
82 };
92
83
93 QTCOMMERCIALCHART_END_NAMESPACE
84 QTCOMMERCIALCHART_END_NAMESPACE
94
85
95 #endif
86 #endif
@@ -1,493 +1,457
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "qchart.h"
21 #include "qchart.h"
22 #include "qchart_p.h"
22 #include "qchart_p.h"
23 #include "legendscroller_p.h"
23 #include "legendscroller_p.h"
24 #include "qlegend_p.h"
24 #include "qlegend_p.h"
25 #include "chartbackground_p.h"
25 #include "chartbackground_p.h"
26 #include "qaxis.h"
26 #include "qaxis.h"
27 #include <QGraphicsScene>
27 #include <QGraphicsScene>
28 #include <QGraphicsSceneResizeEvent>
28 #include <QGraphicsSceneResizeEvent>
29 #include <QGraphicsLayout>
29
30
30 QTCOMMERCIALCHART_BEGIN_NAMESPACE
31 QTCOMMERCIALCHART_BEGIN_NAMESPACE
31
32
32 /*!
33 /*!
33 \enum QChart::ChartTheme
34 \enum QChart::ChartTheme
34
35
35 This enum describes the theme used by the chart.
36 This enum describes the theme used by the chart.
36
37
37 \value ChartThemeLight The default theme
38 \value ChartThemeLight The default theme
38 \value ChartThemeBlueCerulean
39 \value ChartThemeBlueCerulean
39 \value ChartThemeDark
40 \value ChartThemeDark
40 \value ChartThemeBrownSand
41 \value ChartThemeBrownSand
41 \value ChartThemeBlueNcs
42 \value ChartThemeBlueNcs
42 \value ChartThemeHighContrast
43 \value ChartThemeHighContrast
43 \value ChartThemeBlueIcy
44 \value ChartThemeBlueIcy
44 */
45 */
45
46
46 /*!
47 /*!
47 \enum QChart::AnimationOption
48 \enum QChart::AnimationOption
48
49
49 For enabling/disabling animations. Defaults to NoAnimation.
50 For enabling/disabling animations. Defaults to NoAnimation.
50
51
51 \value NoAnimation
52 \value NoAnimation
52 \value GridAxisAnimations
53 \value GridAxisAnimations
53 \value SeriesAnimations
54 \value SeriesAnimations
54 \value AllAnimations
55 \value AllAnimations
55 */
56 */
56
57
57 /*!
58 /*!
58 \class QChart
59 \class QChart
59 \brief QtCommercial chart API.
60 \brief QtCommercial chart API.
60
61
61 QChart is a QGraphicsWidget that you can show in a QGraphicsScene. It manages the graphical
62 QChart is a QGraphicsWidget that you can show in a QGraphicsScene. It manages the graphical
62 representation of different types of series and other chart related objects like
63 representation of different types of series and other chart related objects like
63 QAxis and QLegend. If you simply want to show a chart in a layout, you can use the
64 QAxis and QLegend. If you simply want to show a chart in a layout, you can use the
64 convenience class QChartView instead of QChart.
65 convenience class QChartView instead of QChart.
65 \sa QChartView
66 \sa QChartView
66 */
67 */
67
68
68 /*!
69 /*!
69 \property QChart::animationOptions
70 \property QChart::animationOptions
70 The animation \a options for the chart. Animations are enabled/disabled based on this setting.
71 The animation \a options for the chart. Animations are enabled/disabled based on this setting.
71 */
72 */
72
73
73 /*!
74 /*!
74 \property QChart::backgroundVisible
75 \property QChart::backgroundVisible
75 Whether the chart background is visible or not.
76 Whether the chart background is visible or not.
76 \sa setBackgroundBrush(), setBackgroundPen()
77 \sa setBackgroundBrush(), setBackgroundPen()
77 */
78 */
78
79
79 /*!
80 /*!
80 \property QChart::dropShadowEnabled
81 \property QChart::dropShadowEnabled
81 If set to true, the background drop shadow effect is enabled. If set to false, it is disabled. Note that the drop
82 If set to true, the background drop shadow effect is enabled. If set to false, it is disabled. Note that the drop
82 shadow effect depends on theme, which means the setting may be changed if you switch to another theme.
83 shadow effect depends on theme, which means the setting may be changed if you switch to another theme.
83 */
84 */
84
85
85 /*!
86 /*!
86 \property QChart::margins
87 \property QChart::margins
87 Margins around the plot area. Note that the margin area is used for drawing chart title, legend and axes.
88 Margins around the plot area. Note that the margin area is used for drawing chart title, legend and axes.
88 */
89 */
89
90
90 /*!
91 /*!
91 \property QChart::theme
92 \property QChart::theme
92 Theme is a built-in collection of UI style related settings applied for all visual elements of a chart, like colors,
93 Theme is a built-in collection of UI style related settings applied for all visual elements of a chart, like colors,
93 pens, brushes and fonts of series, axes, title and legend. \l {Chart themes demo} shows an example with a few
94 pens, brushes and fonts of series, axes, title and legend. \l {Chart themes demo} shows an example with a few
94 different themes.
95 different themes.
95 Note: changing the theme will overwrite all customizations previously applied to the series.
96 Note: changing the theme will overwrite all customizations previously applied to the series.
96 */
97 */
97
98
98 /*!
99 /*!
99 \property QChart::title
100 \property QChart::title
100 Title is the name (label) of a chart. It is shown as a headline on top of the chart.
101 Title is the name (label) of a chart. It is shown as a headline on top of the chart.
101 */
102 */
102
103
103 /*!
104 /*!
104 \fn void QChart::marginsChanged(QRectF newMargins)
105 \fn void QChart::marginsChanged(QRectF newMargins)
105 The margins around plot area have changed to \a newMargins. This may happen for example if you change title font size,
106 The margins around plot area have changed to \a newMargins. This may happen for example if you change title font size,
106 modify axes or hide/show legend.
107 modify axes or hide/show legend.
107 */
108 */
108
109
109 /*!
110 /*!
110 Constructs a chart object which is a child of a\a parent. Parameter \a wFlags is passed to the QGraphicsWidget constructor.
111 Constructs a chart object which is a child of a\a parent. Parameter \a wFlags is passed to the QGraphicsWidget constructor.
111 */
112 */
112 QChart::QChart(QGraphicsItem *parent, Qt::WindowFlags wFlags) : QGraphicsWidget(parent,wFlags),
113 QChart::QChart(QGraphicsItem *parent, Qt::WindowFlags wFlags) : QGraphicsWidget(parent,wFlags),
113 d_ptr(new QChartPrivate())
114 d_ptr(new QChartPrivate())
114 {
115 {
115 d_ptr->m_dataset = new ChartDataSet(this);
116 d_ptr->m_dataset = new ChartDataSet(this);
116 d_ptr->m_presenter = new ChartPresenter(this,d_ptr->m_dataset);
117 d_ptr->m_presenter = new ChartPresenter(this,d_ptr->m_dataset);
117 d_ptr->createConnections();
118 d_ptr->createConnections();
118 d_ptr->m_legend = new LegendScroller(this);
119 d_ptr->m_legend = new LegendScroller(this);
119 d_ptr->m_presenter->setTheme(QChart::ChartThemeLight, false);
120 d_ptr->m_presenter->setTheme(QChart::ChartThemeLight, false);
120 connect(d_ptr->m_presenter, SIGNAL(marginsChanged(QRectF)), this, SIGNAL(marginsChanged(QRectF)));
121 //connect(d_ptr->m_presenter, SIGNAL(marginsChanged(QRectF)), this, SIGNAL(marginsChanged(QRectF)));
122 setLayout(d_ptr->m_presenter->layout());
121 }
123 }
122
124
123 /*!
125 /*!
124 Destroys the object and it's children, like series and axis objects added to it.
126 Destroys the object and it's children, like series and axis objects added to it.
125 */
127 */
126 QChart::~QChart()
128 QChart::~QChart()
127 {
129 {
128 //delete first presenter , since this is a root of all the graphical items
130 //delete first presenter , since this is a root of all the graphical items
131 setLayout(0);
129 delete d_ptr->m_presenter;
132 delete d_ptr->m_presenter;
130 d_ptr->m_presenter=0;
133 d_ptr->m_presenter=0;
131 }
134 }
132
135
133 /*!
136 /*!
134 Adds the \a series and optional \a axisY onto the chart and takes the ownership of the objects.
137 Adds the \a series and optional \a axisY onto the chart and takes the ownership of the objects.
135 If auto scaling is enabled, re-scales the axes the series is bound to (both the x axis and
138 If auto scaling is enabled, re-scales the axes the series is bound to (both the x axis and
136 the y axis).
139 the y axis).
137
140
138 \sa removeSeries(), removeAllSeries()
141 \sa removeSeries(), removeAllSeries()
139 */
142 */
140 void QChart::addSeries(QAbstractSeries *series, QAxis *axisY)
143 void QChart::addSeries(QAbstractSeries *series, QAxis *axisY)
141 {
144 {
142 Q_ASSERT(series);
145 Q_ASSERT(series);
143 d_ptr->m_dataset->addSeries(series, axisY);
146 d_ptr->m_dataset->addSeries(series, axisY);
144 }
147 }
145
148
146 /*!
149 /*!
147 Removes the \a series specified in a perameter from the QChartView.
150 Removes the \a series specified in a perameter from the QChartView.
148 It releses its ownership of the specified QChartSeries object.
151 It releses its ownership of the specified QChartSeries object.
149 It does not delete the pointed QChartSeries data object
152 It does not delete the pointed QChartSeries data object
150 \sa addSeries(), removeAllSeries()
153 \sa addSeries(), removeAllSeries()
151 */
154 */
152 void QChart::removeSeries(QAbstractSeries *series)
155 void QChart::removeSeries(QAbstractSeries *series)
153 {
156 {
154 Q_ASSERT(series);
157 Q_ASSERT(series);
155 d_ptr->m_dataset->removeSeries(series);
158 d_ptr->m_dataset->removeSeries(series);
156 }
159 }
157
160
158 /*!
161 /*!
159 Removes all the QChartSeries that have been added to the QChartView
162 Removes all the QChartSeries that have been added to the QChartView
160 It also deletes the pointed QChartSeries data objects
163 It also deletes the pointed QChartSeries data objects
161 \sa addSeries(), removeSeries()
164 \sa addSeries(), removeSeries()
162 */
165 */
163 void QChart::removeAllSeries()
166 void QChart::removeAllSeries()
164 {
167 {
165 d_ptr->m_dataset->removeAllSeries();
168 d_ptr->m_dataset->removeAllSeries();
166 }
169 }
167
170
168 /*!
171 /*!
169 Sets the \a brush that is used for painting the background of the chart area.
172 Sets the \a brush that is used for painting the background of the chart area.
170 */
173 */
171 void QChart::setBackgroundBrush(const QBrush& brush)
174 void QChart::setBackgroundBrush(const QBrush& brush)
172 {
175 {
173 //TODO: refactor me
176 d_ptr->m_presenter->setBackgroundBrush(brush);
174 d_ptr->m_presenter->createChartBackgroundItem();
175 d_ptr->m_presenter->m_backgroundItem->setBrush(brush);
176 d_ptr->m_presenter->m_backgroundItem->update();
177 }
177 }
178
178
179 /*!
179 /*!
180 Gets the brush that is used for painting the background of the chart area.
180 Gets the brush that is used for painting the background of the chart area.
181 */
181 */
182 QBrush QChart::backgroundBrush() const
182 QBrush QChart::backgroundBrush() const
183 {
183 {
184 //TODO: refactor me
184 return d_ptr->m_presenter->backgroundBrush();
185 if (!d_ptr->m_presenter->m_backgroundItem) return QBrush();
186 return (d_ptr->m_presenter->m_backgroundItem)->brush();
187 }
185 }
188
186
189 /*!
187 /*!
190 Sets the \a pen that is used for painting the background of the chart area.
188 Sets the \a pen that is used for painting the background of the chart area.
191 */
189 */
192 void QChart::setBackgroundPen(const QPen& pen)
190 void QChart::setBackgroundPen(const QPen& pen)
193 {
191 {
194 //TODO: refactor me
192 d_ptr->m_presenter->setBackgroundPen(pen);
195 d_ptr->m_presenter->createChartBackgroundItem();
196 d_ptr->m_presenter->m_backgroundItem->setPen(pen);
197 d_ptr->m_presenter->m_backgroundItem->update();
198 }
193 }
199
194
200 /*!
195 /*!
201 Gets the pen that is used for painting the background of the chart area.
196 Gets the pen that is used for painting the background of the chart area.
202 */
197 */
203 QPen QChart::backgroundPen() const
198 QPen QChart::backgroundPen() const
204 {
199 {
205 //TODO: refactor me
200 return d_ptr->m_presenter->backgroundPen();
206 if (!d_ptr->m_presenter->m_backgroundItem) return QPen();
207 return d_ptr->m_presenter->m_backgroundItem->pen();
208 }
201 }
209
202
210 /*!
203 /*!
211 Sets the chart \a title. The description text that is drawn above the chart.
204 Sets the chart \a title. The description text that is drawn above the chart.
212 */
205 */
213 void QChart::setTitle(const QString& title)
206 void QChart::setTitle(const QString& title)
214 {
207 {
215 //TODO: refactor me
208 d_ptr->m_presenter->setTitle(title);
216 d_ptr->m_presenter->createChartTitleItem();
217 d_ptr->m_presenter->m_titleItem->setText(title);
218 d_ptr->m_presenter->updateLayout();
219 }
209 }
220
210
221 /*!
211 /*!
222 Returns the chart title. The description text that is drawn above the chart.
212 Returns the chart title. The description text that is drawn above the chart.
223 */
213 */
224 QString QChart::title() const
214 QString QChart::title() const
225 {
215 {
226 //TODO: refactor me
216 return d_ptr->m_presenter->title();
227 if (d_ptr->m_presenter->m_titleItem)
228 return d_ptr->m_presenter->m_titleItem->text();
229 else
230 return QString();
231 }
217 }
232
218
233 /*!
219 /*!
234 Sets the \a font that is used for drawing the chart description text that is rendered above the chart.
220 Sets the \a font that is used for drawing the chart description text that is rendered above the chart.
235 */
221 */
236 void QChart::setTitleFont(const QFont& font)
222 void QChart::setTitleFont(const QFont& font)
237 {
223 {
238 //TODO: refactor me
224 d_ptr->m_presenter->setTitleFont(font);
239 d_ptr->m_presenter->createChartTitleItem();
240 d_ptr->m_presenter->m_titleItem->setFont(font);
241 d_ptr->m_presenter->updateLayout();
242 }
225 }
243
226
244 /*!
227 /*!
245 Gets the font that is used for drawing the chart description text that is rendered above the chart.
228 Gets the font that is used for drawing the chart description text that is rendered above the chart.
246 */
229 */
247 QFont QChart::titleFont() const
230 QFont QChart::titleFont() const
248 {
231 {
249 if (d_ptr->m_presenter->m_titleItem)
232 return d_ptr->m_presenter->titleFont();
250 return d_ptr->m_presenter->m_titleItem->font();
251 else
252 return QFont();
253 }
233 }
254
234
255 /*!
235 /*!
256 Sets the \a brush used for rendering the title text.
236 Sets the \a brush used for rendering the title text.
257 */
237 */
258 void QChart::setTitleBrush(const QBrush &brush)
238 void QChart::setTitleBrush(const QBrush &brush)
259 {
239 {
260 //TODO: refactor me
240 d_ptr->m_presenter->setTitleBrush(brush);
261 d_ptr->m_presenter->createChartTitleItem();
262 d_ptr->m_presenter->m_titleItem->setBrush(brush);
263 d_ptr->m_presenter->updateLayout();
264 }
241 }
265
242
266 /*!
243 /*!
267 Returns the brush used for rendering the title text.
244 Returns the brush used for rendering the title text.
268 */
245 */
269 QBrush QChart::titleBrush() const
246 QBrush QChart::titleBrush() const
270 {
247 {
271 //TODO: refactor me
248 return d_ptr->m_presenter->titleBrush();
272 if (!d_ptr->m_presenter->m_titleItem) return QBrush();
273 return d_ptr->m_presenter->m_titleItem->brush();
274 }
249 }
275
250
276 void QChart::setTheme(QChart::ChartTheme theme)
251 void QChart::setTheme(QChart::ChartTheme theme)
277 {
252 {
278 d_ptr->m_presenter->setTheme(theme);
253 d_ptr->m_presenter->setTheme(theme);
279 }
254 }
280
255
281 QChart::ChartTheme QChart::theme() const
256 QChart::ChartTheme QChart::theme() const
282 {
257 {
283 return d_ptr->m_presenter->theme();
258 return d_ptr->m_presenter->theme();
284 }
259 }
285
260
286 /*!
261 /*!
287 Zooms in the view by a factor of 2
262 Zooms in the view by a factor of 2
288 */
263 */
289 void QChart::zoomIn()
264 void QChart::zoomIn()
290 {
265 {
291 d_ptr->m_presenter->zoomIn(2.0);
266 d_ptr->m_presenter->zoomIn(2.0);
292 }
267 }
293
268
294 /*!
269 /*!
295 Zooms in the view to a maximum level at which \a rect is still fully visible.
270 Zooms in the view to a maximum level at which \a rect is still fully visible.
296 */
271 */
297 void QChart::zoomIn(const QRectF& rect)
272 void QChart::zoomIn(const QRectF& rect)
298 {
273 {
299 if (!rect.isValid()) return;
274 if (!rect.isValid()) return;
300 d_ptr->m_presenter->zoomIn(rect);
275 d_ptr->m_presenter->zoomIn(rect);
301 }
276 }
302
277
303 /*!
278 /*!
304 Restores the view zoom level to the previous one.
279 Restores the view zoom level to the previous one.
305 */
280 */
306 void QChart::zoomOut()
281 void QChart::zoomOut()
307 {
282 {
308 d_ptr->m_presenter->zoomOut(2.0);
283 d_ptr->m_presenter->zoomOut(2.0);
309 }
284 }
310
285
311 /*!
286 /*!
312 Zooms in the view by a \a factor.
287 Zooms in the view by a \a factor.
313
288
314 A factor over 1.0 zooms the view in and factor between 0.0 and 1.0 zooms out.
289 A factor over 1.0 zooms the view in and factor between 0.0 and 1.0 zooms out.
315 */
290 */
316 void QChart::zoom(qreal factor)
291 void QChart::zoom(qreal factor)
317 {
292 {
318 if (qFuzzyIsNull(factor))
293 if (qFuzzyIsNull(factor))
319 return;
294 return;
320
295
321 if (qFuzzyCompare(factor, 1.0))
296 if (qFuzzyCompare(factor, 1.0))
322 return;
297 return;
323
298
324 if (factor < 0)
299 if (factor < 0)
325 return;
300 return;
326
301
327 if (factor > 1.0)
302 if (factor > 1.0)
328 d_ptr->m_presenter->zoomIn(factor);
303 d_ptr->m_presenter->zoomIn(factor);
329 else
304 else
330 d_ptr->m_presenter->zoomOut(1.0 / factor);
305 d_ptr->m_presenter->zoomOut(1.0 / factor);
331 }
306 }
332
307
333 /*!
308 /*!
334 Returns the pointer to the x axis object of the chart
309 Returns the pointer to the x axis object of the chart
335 */
310 */
336 QAxis* QChart::axisX() const
311 QAxis* QChart::axisX() const
337 {
312 {
338 return d_ptr->m_dataset->axisX();
313 return d_ptr->m_dataset->axisX();
339 }
314 }
340
315
341 /*!
316 /*!
342 Returns the pointer to the y axis object of the \a series
317 Returns the pointer to the y axis object of the \a series
343 If no \a series is provided then default Y axis of the chart is returned.
318 If no \a series is provided then default Y axis of the chart is returned.
344 */
319 */
345 QAxis* QChart::axisY(QAbstractSeries *series) const
320 QAxis* QChart::axisY(QAbstractSeries *series) const
346 {
321 {
347 return d_ptr->m_dataset->axisY(series);
322 return d_ptr->m_dataset->axisY(series);
348 }
323 }
349
324
350 /*!
325 /*!
351 Returns the legend object of the chart. Ownership stays in chart.
326 Returns the legend object of the chart. Ownership stays in chart.
352 */
327 */
353 QLegend* QChart::legend() const
328 QLegend* QChart::legend() const
354 {
329 {
355 return d_ptr->m_legend;
330 return d_ptr->m_legend;
356 }
331 }
357
332
358 /*!
333 /*!
359 Returns the rect that contains information about margins (distance between chart widget edge and axes).
334 Returns the rect that contains information about margins (distance between chart widget edge and axes).
360 Individual margins can be obtained by calling left, top, right, bottom on the returned rect.
335 Individual margins can be obtained by calling left, top, right, bottom on the returned rect.
361 */
336 */
362 QRectF QChart::margins() const
337 QRectF QChart::margins() const
363 {
338 {
364 return d_ptr->m_presenter->margins();
339 return d_ptr->m_presenter->margins();
365 }
340 }
366
341
367
368 /*!
342 /*!
369 Resizes and updates the chart area using the \a event data
343 Sets animation \a options for the chart
370 */
344 */
371 void QChart::resizeEvent(QGraphicsSceneResizeEvent *event)
372 {
373 d_ptr->m_rect = QRectF(QPoint(0,0),event->newSize());
374 QGraphicsWidget::resizeEvent(event);
375 d_ptr->m_presenter->setGeometry(d_ptr->m_rect);
376 }
377
345
378 void QChart::setAnimationOptions(AnimationOptions options)
346 void QChart::setAnimationOptions(AnimationOptions options)
379 {
347 {
380 d_ptr->m_presenter->setAnimationOptions(options);
348 d_ptr->m_presenter->setAnimationOptions(options);
381 }
349 }
382
350
383 QChart::AnimationOptions QChart::animationOptions() const
351 QChart::AnimationOptions QChart::animationOptions() const
384 {
352 {
385 return d_ptr->m_presenter->animationOptions();
353 return d_ptr->m_presenter->animationOptions();
386 }
354 }
387
355
388 /*!
356 /*!
389 Scrolls the visible area of the chart to the left by the distance between two x axis ticks
357 Scrolls the visible area of the chart to the left by the distance between two x axis ticks
390 */
358 */
391 void QChart::scrollLeft()
359 void QChart::scrollLeft()
392 {
360 {
393 d_ptr->m_presenter->scroll(-d_ptr->m_presenter->chartGeometry().width()/(axisX()->ticksCount()-1),0);
361 d_ptr->m_presenter->scroll(-d_ptr->m_presenter->geometry().width()/(axisX()->ticksCount()-1),0);
394 }
362 }
395
363
396 /*!
364 /*!
397 Scrolls the visible area of the chart to the right by the distance between two x axis ticks
365 Scrolls the visible area of the chart to the right by the distance between two x axis ticks
398 */
366 */
399 void QChart::scrollRight()
367 void QChart::scrollRight()
400 {
368 {
401 d_ptr->m_presenter->scroll(d_ptr->m_presenter->chartGeometry().width()/(axisX()->ticksCount()-1),0);
369 d_ptr->m_presenter->scroll(d_ptr->m_presenter->geometry().width()/(axisX()->ticksCount()-1),0);
402 }
370 }
403
371
404 /*!
372 /*!
405 Scrolls the visible area of the chart up by the distance between two y axis ticks
373 Scrolls the visible area of the chart up by the distance between two y axis ticks
406 */
374 */
407 void QChart::scrollUp()
375 void QChart::scrollUp()
408 {
376 {
409 d_ptr->m_presenter->scroll(0,d_ptr->m_presenter->chartGeometry().width()/(axisY()->ticksCount()-1));
377 d_ptr->m_presenter->scroll(0,d_ptr->m_presenter->geometry().width()/(axisY()->ticksCount()-1));
410 }
378 }
411
379
412 /*!
380 /*!
413 Scrolls the visible area of the chart down by the distance between two y axis ticks
381 Scrolls the visible area of the chart down by the distance between two y axis ticks
414 */
382 */
415 void QChart::scrollDown()
383 void QChart::scrollDown()
416 {
384 {
417 d_ptr->m_presenter->scroll(0,-d_ptr->m_presenter->chartGeometry().width()/(axisY()->ticksCount()-1));
385 d_ptr->m_presenter->scroll(0,-d_ptr->m_presenter->geometry().width()/(axisY()->ticksCount()-1));
418 }
386 }
419
387
420 /*!
388 /*!
421 Scrolls the visible area of the chart by the distance defined in the \a delta.
389 Scrolls the visible area of the chart by the distance defined in the \a delta.
422 */
390 */
423 void QChart::scroll(const QPointF &delta)
391 void QChart::scroll(const QPointF &delta)
424 {
392 {
425 d_ptr->m_presenter->scroll(-delta.x(), delta.y());
393 d_ptr->m_presenter->scroll(-delta.x(), delta.y());
426 }
394 }
427
395
428 void QChart::setBackgroundVisible(bool visible)
396 void QChart::setBackgroundVisible(bool visible)
429 {
397 {
430 //TODO: refactor me
398 d_ptr->m_presenter->setBackgroundVisible(visible);
431 d_ptr->m_presenter->createChartBackgroundItem();
432 d_ptr->m_presenter->m_backgroundItem->setVisible(visible);
433 }
399 }
434
400
435 bool QChart::isBackgroundVisible() const
401 bool QChart::isBackgroundVisible() const
436 {
402 {
437 //TODO: refactor me
403 return d_ptr->m_presenter->isBackgroundVisible();
438 if (!d_ptr->m_presenter->m_backgroundItem)
439 return false;
440
441 return d_ptr->m_presenter->m_backgroundItem->isVisible();
442 }
404 }
443
405
444 void QChart::setDropShadowEnabled(bool enabled)
406 void QChart::setDropShadowEnabled(bool enabled)
445 {
407 {
446 d_ptr->m_presenter->createChartBackgroundItem();
408 d_ptr->m_presenter->setBackgroundDropShadowEnabled(enabled);
447 d_ptr->m_presenter->m_backgroundItem->setDropShadowEnabled(enabled);
448 }
409 }
449
410
450 bool QChart::isDropShadowEnabled() const
411 bool QChart::isDropShadowEnabled() const
451 {
412 {
452 if (!d_ptr->m_presenter->m_backgroundItem)
413 return d_ptr->m_presenter->isBackgroundDropShadowEnabled();
453 return false;
454
455 return d_ptr->m_presenter->m_backgroundItem->isDropShadowEnabled();
456 }
414 }
457
415
458 /*!
416 /*!
459 Returns all the series that are added to the chart.
417 Returns all the series that are added to the chart.
460
418
461 \sa addSeries(), removeSeries(), removeAllSeries()
419 \sa addSeries(), removeSeries(), removeAllSeries()
462 */
420 */
463 QList<QAbstractSeries*> QChart::series() const
421 QList<QAbstractSeries*> QChart::series() const
464 {
422 {
465 return d_ptr->m_dataset->series();
423 return d_ptr->m_dataset->series();
466 }
424 }
467
425
426 void QChart::setMarginsMinimum(const QRectF& margins)
427 {
428 d_ptr->m_presenter->setMarginsMinimum(margins);
429 }
430
468 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
431 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
469
432
470 QChartPrivate::QChartPrivate():
433 QChartPrivate::QChartPrivate():
471 m_legend(0),
434 m_legend(0),
472 m_dataset(0),
435 m_dataset(0),
473 m_presenter(0)
436 m_presenter(0)
474 {
437 {
475
438
476 }
439 }
477
440
478 QChartPrivate::~QChartPrivate()
441 QChartPrivate::~QChartPrivate()
479 {
442 {
480
443
481 }
444 }
482
445
483 void QChartPrivate::createConnections()
446 void QChartPrivate::createConnections()
484 {
447 {
485 QObject::connect(m_dataset,SIGNAL(seriesAdded(QAbstractSeries*,Domain*)),m_presenter,SLOT(handleSeriesAdded(QAbstractSeries*,Domain*)));
448 QObject::connect(m_dataset,SIGNAL(seriesAdded(QAbstractSeries*,Domain*)),m_presenter,SLOT(handleSeriesAdded(QAbstractSeries*,Domain*)));
486 QObject::connect(m_dataset,SIGNAL(seriesRemoved(QAbstractSeries*)),m_presenter,SLOT(handleSeriesRemoved(QAbstractSeries*)));
449 QObject::connect(m_dataset,SIGNAL(seriesRemoved(QAbstractSeries*)),m_presenter,SLOT(handleSeriesRemoved(QAbstractSeries*)));
487 QObject::connect(m_dataset,SIGNAL(axisAdded(QAxis*,Domain*)),m_presenter,SLOT(handleAxisAdded(QAxis*,Domain*)));
450 QObject::connect(m_dataset,SIGNAL(axisAdded(QAxis*,Domain*)),m_presenter,SLOT(handleAxisAdded(QAxis*,Domain*)));
488 QObject::connect(m_dataset,SIGNAL(axisRemoved(QAxis*)),m_presenter,SLOT(handleAxisRemoved(QAxis*)));
451 QObject::connect(m_dataset,SIGNAL(axisRemoved(QAxis*)),m_presenter,SLOT(handleAxisRemoved(QAxis*)));
452 //QObject::connect(m_presenter, SIGNAL(marginsChanged(QRectF)), q_ptr, SIGNAL(marginsChanged(QRectF)));
489 }
453 }
490
454
491 #include "moc_qchart.cpp"
455 #include "moc_qchart.cpp"
492
456
493 QTCOMMERCIALCHART_END_NAMESPACE
457 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,133 +1,132
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #ifndef QCHART_H
21 #ifndef QCHART_H
22 #define QCHART_H
22 #define QCHART_H
23
23
24 #include <QAbstractSeries>
24 #include <QAbstractSeries>
25 #include <QLegend>
25 #include <QLegend>
26 #include <QGraphicsWidget>
26 #include <QGraphicsWidget>
27
27
28 class QGraphicsSceneResizeEvent;
28 class QGraphicsSceneResizeEvent;
29
29
30 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30 QTCOMMERCIALCHART_BEGIN_NAMESPACE
31
31
32 class QAbstractSeries;
32 class QAbstractSeries;
33 class QAxis;
33 class QAxis;
34 class QLegend;
34 class QLegend;
35 struct QChartPrivate;
35 struct QChartPrivate;
36
36
37 class QTCOMMERCIALCHART_EXPORT QChart : public QGraphicsWidget
37 class QTCOMMERCIALCHART_EXPORT QChart : public QGraphicsWidget
38 {
38 {
39 Q_OBJECT
39 Q_OBJECT
40 Q_PROPERTY(QChart::ChartTheme theme READ theme WRITE setTheme)
40 Q_PROPERTY(QChart::ChartTheme theme READ theme WRITE setTheme)
41 Q_PROPERTY(QString title READ title WRITE setTitle)
41 Q_PROPERTY(QString title READ title WRITE setTitle)
42 Q_PROPERTY(bool backgroundVisible READ isBackgroundVisible WRITE setBackgroundVisible)
42 Q_PROPERTY(bool backgroundVisible READ isBackgroundVisible WRITE setBackgroundVisible)
43 Q_PROPERTY(bool dropShadowEnabled READ isDropShadowEnabled WRITE setDropShadowEnabled)
43 Q_PROPERTY(bool dropShadowEnabled READ isDropShadowEnabled WRITE setDropShadowEnabled)
44 Q_PROPERTY(QChart::AnimationOptions animationOptions READ animationOptions WRITE setAnimationOptions)
44 Q_PROPERTY(QChart::AnimationOptions animationOptions READ animationOptions WRITE setAnimationOptions)
45 Q_PROPERTY(QRectF margins READ margins NOTIFY marginsChanged)
45 Q_PROPERTY(QRectF margins READ margins NOTIFY marginsChanged)
46 Q_ENUMS(ChartTheme)
46 Q_ENUMS(ChartTheme)
47 Q_ENUMS(AnimationOption)
47 Q_ENUMS(AnimationOption)
48
48
49 public:
49 public:
50 enum ChartTheme {
50 enum ChartTheme {
51 ChartThemeLight = 0,
51 ChartThemeLight = 0,
52 ChartThemeBlueCerulean,
52 ChartThemeBlueCerulean,
53 ChartThemeDark,
53 ChartThemeDark,
54 ChartThemeBrownSand,
54 ChartThemeBrownSand,
55 ChartThemeBlueNcs,
55 ChartThemeBlueNcs,
56 ChartThemeHighContrast,
56 ChartThemeHighContrast,
57 ChartThemeBlueIcy
57 ChartThemeBlueIcy
58 };
58 };
59
59
60 enum AnimationOption {
60 enum AnimationOption {
61 NoAnimation = 0x0,
61 NoAnimation = 0x0,
62 GridAxisAnimations = 0x1,
62 GridAxisAnimations = 0x1,
63 SeriesAnimations =0x2,
63 SeriesAnimations =0x2,
64 AllAnimations = 0x3
64 AllAnimations = 0x3
65 };
65 };
66
66
67 Q_DECLARE_FLAGS(AnimationOptions, AnimationOption)
67 Q_DECLARE_FLAGS(AnimationOptions, AnimationOption)
68
68
69 public:
69 public:
70 explicit QChart(QGraphicsItem *parent = 0, Qt::WindowFlags wFlags = 0);
70 explicit QChart(QGraphicsItem *parent = 0, Qt::WindowFlags wFlags = 0);
71 ~QChart();
71 ~QChart();
72
72
73 void addSeries(QAbstractSeries *series, QAxis *axisY = 0);
73 void addSeries(QAbstractSeries *series, QAxis *axisY = 0);
74 void removeSeries(QAbstractSeries *series);
74 void removeSeries(QAbstractSeries *series);
75 void removeAllSeries();
75 void removeAllSeries();
76 QList<QAbstractSeries*> series() const;
76 QList<QAbstractSeries*> series() const;
77
77
78 void setTheme(QChart::ChartTheme theme);
78 void setTheme(QChart::ChartTheme theme);
79 QChart::ChartTheme theme() const;
79 QChart::ChartTheme theme() const;
80
80
81 void setTitle(const QString& title);
81 void setTitle(const QString& title);
82 QString title() const;
82 QString title() const;
83 void setTitleFont(const QFont& font);
83 void setTitleFont(const QFont& font);
84 QFont titleFont() const;
84 QFont titleFont() const;
85 void setTitleBrush(const QBrush &brush);
85 void setTitleBrush(const QBrush &brush);
86 QBrush titleBrush() const;
86 QBrush titleBrush() const;
87
87
88 void setBackgroundBrush(const QBrush &brush);
88 void setBackgroundBrush(const QBrush &brush);
89 QBrush backgroundBrush() const;
89 QBrush backgroundBrush() const;
90 void setBackgroundPen(const QPen &pen);
90 void setBackgroundPen(const QPen &pen);
91 QPen backgroundPen() const;
91 QPen backgroundPen() const;
92 void setBackgroundVisible(bool visible = true);
92 void setBackgroundVisible(bool visible = true);
93 bool isBackgroundVisible() const;
93 bool isBackgroundVisible() const;
94
94
95 void setDropShadowEnabled(bool enabled = true);
95 void setDropShadowEnabled(bool enabled = true);
96 bool isDropShadowEnabled() const;
96 bool isDropShadowEnabled() const;
97 void setAnimationOptions(AnimationOptions options);
97 void setAnimationOptions(AnimationOptions options);
98 AnimationOptions animationOptions() const;
98 AnimationOptions animationOptions() const;
99
99
100 void zoomIn();
100 void zoomIn();
101 void zoomIn(const QRectF &rect);
101 void zoomIn(const QRectF &rect);
102 void zoomOut();
102 void zoomOut();
103 void zoom(qreal factor);
103 void zoom(qreal factor);
104 void scrollLeft();
104 void scrollLeft();
105 void scrollRight();
105 void scrollRight();
106 void scrollUp();
106 void scrollUp();
107 void scrollDown();
107 void scrollDown();
108 void scroll(const QPointF &delta);
108 void scroll(const QPointF &delta);
109
109
110 QAxis* axisX() const;
110 QAxis* axisX() const;
111 QAxis* axisY(QAbstractSeries* series = 0) const;
111 QAxis* axisY(QAbstractSeries* series = 0) const;
112
112
113 QLegend* legend() const;
113 QLegend* legend() const;
114
115 void setMarginsMinimum(const QRectF& margins);
114 QRectF margins() const;
116 QRectF margins() const;
115
117
116 Q_SIGNALS:
118 Q_SIGNALS:
117 void marginsChanged(QRectF newMargins);
119 void marginsChanged(QRectF newMargins);
118
120
119 protected:
121 protected:
120 void resizeEvent(QGraphicsSceneResizeEvent *event);
121
122 protected:
123 QScopedPointer<QChartPrivate> d_ptr;
122 QScopedPointer<QChartPrivate> d_ptr;
124 friend class QLegend;
123 friend class QLegend;
125 friend class ChartPresenter;
124 friend class ChartPresenter;
126 Q_DISABLE_COPY(QChart)
125 Q_DISABLE_COPY(QChart)
127 };
126 };
128
127
129 QTCOMMERCIALCHART_END_NAMESPACE
128 QTCOMMERCIALCHART_END_NAMESPACE
130
129
131 Q_DECLARE_OPERATORS_FOR_FLAGS(QTCOMMERCIALCHART_NAMESPACE::QChart::AnimationOptions)
130 Q_DECLARE_OPERATORS_FOR_FLAGS(QTCOMMERCIALCHART_NAMESPACE::QChart::AnimationOptions)
132
131
133 #endif
132 #endif
@@ -1,55 +1,54
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 // W A R N I N G
21 // W A R N I N G
22 // -------------
22 // -------------
23 //
23 //
24 // This file is not part of the QtCommercial Chart API. It exists purely as an
24 // This file is not part of the QtCommercial Chart API. It exists purely as an
25 // implementation detail. This header file may change from version to
25 // implementation detail. This header file may change from version to
26 // version without notice, or even be removed.
26 // version without notice, or even be removed.
27 //
27 //
28 // We mean it.
28 // We mean it.
29
29
30 #ifndef QCHART_P_H
30 #ifndef QCHART_P_H
31 #define QCHART_P_H
31 #define QCHART_P_H
32
32
33 #include "qlegend.h"
33 #include "qlegend.h"
34 #include "chartpresenter_p.h"
34 #include "chartpresenter_p.h"
35 #include "chartdataset_p.h"
35 #include "chartdataset_p.h"
36
36
37 QTCOMMERCIALCHART_BEGIN_NAMESPACE
37 QTCOMMERCIALCHART_BEGIN_NAMESPACE
38
38
39 class QChart;
39 class QChart;
40
40
41 struct QChartPrivate
41 struct QChartPrivate
42 {
42 {
43 QChartPrivate();
43 QChartPrivate();
44 ~QChartPrivate();
44 ~QChartPrivate();
45 QRectF m_rect;
46 QLegend* m_legend;
45 QLegend* m_legend;
47 ChartDataSet *m_dataset;
46 ChartDataSet *m_dataset;
48 ChartPresenter *m_presenter;
47 ChartPresenter *m_presenter;
49
48
50 void createConnections();
49 void createConnections();
51
50
52 };
51 };
53
52
54 QTCOMMERCIALCHART_END_NAMESPACE
53 QTCOMMERCIALCHART_END_NAMESPACE
55 #endif
54 #endif
@@ -1,255 +1,257
1 !include( ../config.pri ):error( "Couldn't find the config.pri file!" )
1 !include( ../config.pri ):error( "Couldn't find the config.pri file!" )
2
2
3 ############################# BUILD CONFIG ######################################
3 ############################# BUILD CONFIG ######################################
4
4
5 TARGET = $$LIBRARY_NAME
5 TARGET = $$LIBRARY_NAME
6 DESTDIR = $$CHART_BUILD_LIB_DIR
6 DESTDIR = $$CHART_BUILD_LIB_DIR
7 TEMPLATE = lib
7 TEMPLATE = lib
8 QT = core gui
8 QT = core gui
9 DEFINES += QTCOMMERCIALCHART_LIBRARY
9 DEFINES += QTCOMMERCIALCHART_LIBRARY
10 win32:CONFIG += create_prl
10 win32:CONFIG += create_prl
11 # treat warnings as errors
11 # treat warnings as errors
12 win32-msvc*: {
12 win32-msvc*: {
13 QMAKE_CXXFLAGS += /WX
13 QMAKE_CXXFLAGS += /WX
14 } else {
14 } else {
15 QMAKE_CXXFLAGS += -Werror
15 QMAKE_CXXFLAGS += -Werror
16 }
16 }
17
17
18 unix:{
18 unix:{
19 QMAKE_CXXFLAGS += -fvisibility=hidden -fvisibility-inlines-hidden
19 QMAKE_CXXFLAGS += -fvisibility=hidden -fvisibility-inlines-hidden
20 }
20 }
21
21
22 # enable building debug and release at the same time
22 # enable building debug and release at the same time
23 CONFIG += debug_and_release
23 CONFIG += debug_and_release
24
24
25 # On windows by default build both debug and release at the same time
25 # On windows by default build both debug and release at the same time
26 win32:CONFIG += build_all
26 win32:CONFIG += build_all
27
27
28 ############################# DEPEDENCES ########################################
28 ############################# DEPEDENCES ########################################
29
29
30 win32-msvc*: LIBS += User32.lib
30 win32-msvc*: LIBS += User32.lib
31 LIBS -= -l$$LIBRARY_NAME
31 LIBS -= -l$$LIBRARY_NAME
32 INCLUDEPATH += ../include .
32 INCLUDEPATH += ../include .
33
33
34 ############################# SOURCES ##########################################
34 ############################# SOURCES ##########################################
35
35
36 SOURCES += \
36 SOURCES += \
37 $$PWD/chartdataset.cpp \
37 $$PWD/chartdataset.cpp \
38 $$PWD/chartpresenter.cpp \
38 $$PWD/chartpresenter.cpp \
39 $$PWD/charttheme.cpp \
39 $$PWD/charttheme.cpp \
40 $$PWD/domain.cpp \
40 $$PWD/domain.cpp \
41 $$PWD/qchart.cpp \
41 $$PWD/qchart.cpp \
42 $$PWD/qchartview.cpp \
42 $$PWD/qchartview.cpp \
43 $$PWD/qabstractseries.cpp \
43 $$PWD/qabstractseries.cpp \
44 $$PWD/chartbackground.cpp \
44 $$PWD/chartbackground.cpp \
45 $$PWD/chart.cpp \
45 $$PWD/chart.cpp \
46 $$PWD/scroller.cpp
46 $$PWD/scroller.cpp \
47 $$PWD/chartlayout.cpp
47 PRIVATE_HEADERS += \
48 PRIVATE_HEADERS += \
48 $$PWD/chartdataset_p.h \
49 $$PWD/chartdataset_p.h \
49 $$PWD/chartitem_p.h \
50 $$PWD/chartitem_p.h \
50 $$PWD/chartpresenter_p.h \
51 $$PWD/chartpresenter_p.h \
51 $$PWD/charttheme_p.h \
52 $$PWD/charttheme_p.h \
52 $$PWD/domain_p.h \
53 $$PWD/domain_p.h \
53 $$PWD/chartbackground_p.h \
54 $$PWD/chartbackground_p.h \
54 $$PWD/chart_p.h \
55 $$PWD/chart_p.h \
55 $$PWD/chartconfig_p.h \
56 $$PWD/chartconfig_p.h \
56 $$PWD/qchart_p.h \
57 $$PWD/qchart_p.h \
57 $$PWD/qchartview_p.h \
58 $$PWD/qchartview_p.h \
58 $$PWD/scroller_p.h \
59 $$PWD/scroller_p.h \
59 $$PWD/qabstractseries_p.h
60 $$PWD/qabstractseries_p.h \
61 $$PWD/chartlayout_p.h
60 PUBLIC_HEADERS += \
62 PUBLIC_HEADERS += \
61 $$PWD/qchart.h \
63 $$PWD/qchart.h \
62 $$PWD/qchartglobal.h \
64 $$PWD/qchartglobal.h \
63 $$PWD/qabstractseries.h \
65 $$PWD/qabstractseries.h \
64 $$PWD/qchartview.h
66 $$PWD/qchartview.h
65
67
66 include(animations/animations.pri)
68 include(animations/animations.pri)
67 include(areachart/areachart.pri)
69 include(areachart/areachart.pri)
68 include(axis/axis.pri)
70 include(axis/axis.pri)
69 include(barchart/barchart.pri)
71 include(barchart/barchart.pri)
70 include(legend/legend.pri)
72 include(legend/legend.pri)
71 include(linechart/linechart.pri)
73 include(linechart/linechart.pri)
72 include(piechart/piechart.pri)
74 include(piechart/piechart.pri)
73 include(scatterchart/scatter.pri)
75 include(scatterchart/scatter.pri)
74 include(splinechart/splinechart.pri)
76 include(splinechart/splinechart.pri)
75 include(themes/themes.pri)
77 include(themes/themes.pri)
76 include(xychart/xychart.pri)
78 include(xychart/xychart.pri)
77
79
78 HEADERS += $$PUBLIC_HEADERS
80 HEADERS += $$PUBLIC_HEADERS
79 HEADERS += $$PRIVATE_HEADERS
81 HEADERS += $$PRIVATE_HEADERS
80 HEADERS += $$THEMES
82 HEADERS += $$THEMES
81
83
82 ############################# BUILD PATH ##########################################
84 ############################# BUILD PATH ##########################################
83
85
84 OBJECTS_DIR = $$CHART_BUILD_DIR/lib
86 OBJECTS_DIR = $$CHART_BUILD_DIR/lib
85 MOC_DIR = $$CHART_BUILD_DIR/lib
87 MOC_DIR = $$CHART_BUILD_DIR/lib
86 UI_DIR = $$CHART_BUILD_DIR/lib
88 UI_DIR = $$CHART_BUILD_DIR/lib
87 RCC_DIR = $$CHART_BUILD_DIR/lib
89 RCC_DIR = $$CHART_BUILD_DIR/lib
88
90
89 ############################# PUBLIC HEADERS GENERTOR ##########################################
91 ############################# PUBLIC HEADERS GENERTOR ##########################################
90
92
91 #this is very primitive and lame parser , TODO: make perl script insted
93 #this is very primitive and lame parser , TODO: make perl script insted
92 !exists($$CHART_BUILD_PUBLIC_HEADER_DIR/QChartGlobal)
94 !exists($$CHART_BUILD_PUBLIC_HEADER_DIR/QChartGlobal)
93 {
95 {
94 system($$QMAKE_MKDIR $$CHART_BUILD_PUBLIC_HEADER_DIR)
96 system($$QMAKE_MKDIR $$CHART_BUILD_PUBLIC_HEADER_DIR)
95 win32:{
97 win32:{
96 command = "echo $${LITERAL_HASH}include \"qchartglobal.h\" > $$CHART_BUILD_PUBLIC_HEADER_DIR/QChartGlobal"
98 command = "echo $${LITERAL_HASH}include \"qchartglobal.h\" > $$CHART_BUILD_PUBLIC_HEADER_DIR/QChartGlobal"
97 }else{
99 }else{
98 command = "echo \"$${LITERAL_HASH}include \\\"qchartglobal.h\\\"\" > $$CHART_BUILD_PUBLIC_HEADER_DIR/QChartGlobal"
100 command = "echo \"$${LITERAL_HASH}include \\\"qchartglobal.h\\\"\" > $$CHART_BUILD_PUBLIC_HEADER_DIR/QChartGlobal"
99 }
101 }
100 PUBLIC_QT_HEADERS += $$CHART_BUILD_PUBLIC_HEADER_DIR/QChartGlobal
102 PUBLIC_QT_HEADERS += $$CHART_BUILD_PUBLIC_HEADER_DIR/QChartGlobal
101 system($$command)
103 system($$command)
102 }
104 }
103
105
104 for(file, PUBLIC_HEADERS) {
106 for(file, PUBLIC_HEADERS) {
105 name = $$split(file,'/')
107 name = $$split(file,'/')
106 name = $$last(name)
108 name = $$last(name)
107 class = "$$cat($$file)"
109 class = "$$cat($$file)"
108 class = $$find(class,class)
110 class = $$find(class,class)
109 !isEmpty(class){
111 !isEmpty(class){
110 class = $$split(class,QTCOMMERCIALCHART_EXPORT)
112 class = $$split(class,QTCOMMERCIALCHART_EXPORT)
111 class = $$member(class,1)
113 class = $$member(class,1)
112 class = $$split(class,' ')
114 class = $$split(class,' ')
113 class = $$replace(class,' ','')
115 class = $$replace(class,' ','')
114 class = $$member(class,0)
116 class = $$member(class,0)
115 win32:{
117 win32:{
116 command = "echo $${LITERAL_HASH}include \"$$name\" > $$CHART_BUILD_PUBLIC_HEADER_DIR/$$class"
118 command = "echo $${LITERAL_HASH}include \"$$name\" > $$CHART_BUILD_PUBLIC_HEADER_DIR/$$class"
117 }else{
119 }else{
118 command = "echo \"$${LITERAL_HASH}include \\\"$$name\\\"\" > $$CHART_BUILD_PUBLIC_HEADER_DIR/$$class"
120 command = "echo \"$${LITERAL_HASH}include \\\"$$name\\\"\" > $$CHART_BUILD_PUBLIC_HEADER_DIR/$$class"
119 }
121 }
120 PUBLIC_QT_HEADERS += $$CHART_BUILD_PUBLIC_HEADER_DIR/$$class
122 PUBLIC_QT_HEADERS += $$CHART_BUILD_PUBLIC_HEADER_DIR/$$class
121 system($$command)
123 system($$command)
122 }
124 }
123 }
125 }
124
126
125 ############################# INSTALLERS ##########################################
127 ############################# INSTALLERS ##########################################
126
128
127 public_headers.path = $$[QT_INSTALL_HEADERS]/QtCommercialChart
129 public_headers.path = $$[QT_INSTALL_HEADERS]/QtCommercialChart
128 public_headers.files = $$PUBLIC_HEADERS $$PUBLIC_QT_HEADERS
130 public_headers.files = $$PUBLIC_HEADERS $$PUBLIC_QT_HEADERS
129 INSTALLS += public_headers
131 INSTALLS += public_headers
130
132
131 install_build_public_headers.name = build_public_headers
133 install_build_public_headers.name = build_public_headers
132 install_build_public_headers.output = $$CHART_BUILD_PUBLIC_HEADER_DIR/${QMAKE_FILE_BASE}.h
134 install_build_public_headers.output = $$CHART_BUILD_PUBLIC_HEADER_DIR/${QMAKE_FILE_BASE}.h
133 install_build_public_headers.input = PUBLIC_HEADERS
135 install_build_public_headers.input = PUBLIC_HEADERS
134 install_build_public_headers.commands = $$QMAKE_COPY \
136 install_build_public_headers.commands = $$QMAKE_COPY \
135 ${QMAKE_FILE_NAME} \
137 ${QMAKE_FILE_NAME} \
136 $$CHART_BUILD_PUBLIC_HEADER_DIR
138 $$CHART_BUILD_PUBLIC_HEADER_DIR
137 install_build_public_headers.CONFIG += target_predeps \
139 install_build_public_headers.CONFIG += target_predeps \
138 no_link
140 no_link
139
141
140 install_build_private_headers.name = buld_private_headers
142 install_build_private_headers.name = buld_private_headers
141 install_build_private_headers.output = $$CHART_BUILD_PRIVATE_HEADER_DIR/${QMAKE_FILE_BASE}.h
143 install_build_private_headers.output = $$CHART_BUILD_PRIVATE_HEADER_DIR/${QMAKE_FILE_BASE}.h
142 install_build_private_headers.input = PRIVATE_HEADERS
144 install_build_private_headers.input = PRIVATE_HEADERS
143 install_build_private_headers.commands = $$QMAKE_COPY \
145 install_build_private_headers.commands = $$QMAKE_COPY \
144 ${QMAKE_FILE_NAME} \
146 ${QMAKE_FILE_NAME} \
145 $$CHART_BUILD_PRIVATE_HEADER_DIR
147 $$CHART_BUILD_PRIVATE_HEADER_DIR
146 install_build_private_headers.CONFIG += target_predeps \
148 install_build_private_headers.CONFIG += target_predeps \
147 no_link
149 no_link
148
150
149 QMAKE_EXTRA_COMPILERS += install_build_public_headers \
151 QMAKE_EXTRA_COMPILERS += install_build_public_headers \
150 install_build_private_headers \
152 install_build_private_headers \
151
153
152 win32:{
154 win32:{
153 bintarget.CONFIG += no_check_exist
155 bintarget.CONFIG += no_check_exist
154 !staticlib: {
156 !staticlib: {
155 bintarget.files += $$CHART_BUILD_LIB_DIR\\$${TARGET}.dll
157 bintarget.files += $$CHART_BUILD_LIB_DIR\\$${TARGET}.dll
156 }
158 }
157 win32-msvc*:CONFIG(debug, debug|release): {
159 win32-msvc*:CONFIG(debug, debug|release): {
158 bintarget.files += $$CHART_BUILD_LIB_DIR\\$${TARGET}.pdb
160 bintarget.files += $$CHART_BUILD_LIB_DIR\\$${TARGET}.pdb
159 }
161 }
160 bintarget.path = $$[QT_INSTALL_BINS]
162 bintarget.path = $$[QT_INSTALL_BINS]
161
163
162 libtarget.CONFIG += no_check_exist
164 libtarget.CONFIG += no_check_exist
163 libtarget.files = $$CHART_BUILD_LIB_DIR\\$${TARGET}.prl
165 libtarget.files = $$CHART_BUILD_LIB_DIR\\$${TARGET}.prl
164 win32-msvc*: {
166 win32-msvc*: {
165 libtarget.files += $$CHART_BUILD_LIB_DIR\\$${TARGET}.lib
167 libtarget.files += $$CHART_BUILD_LIB_DIR\\$${TARGET}.lib
166 } else {
168 } else {
167 libtarget.files += $$CHART_BUILD_LIB_DIR\\lib$${TARGET}.a
169 libtarget.files += $$CHART_BUILD_LIB_DIR\\lib$${TARGET}.a
168 }
170 }
169 libtarget.path = $$[QT_INSTALL_LIBS]
171 libtarget.path = $$[QT_INSTALL_LIBS]
170
172
171 DLLDESTDIR = $$CHART_BUILD_BIN_DIR
173 DLLDESTDIR = $$CHART_BUILD_BIN_DIR
172 INSTALLS += bintarget libtarget
174 INSTALLS += bintarget libtarget
173 }else{
175 }else{
174 target.path=$$[QT_INSTALL_LIBS]
176 target.path=$$[QT_INSTALL_LIBS]
175 INSTALLS += target
177 INSTALLS += target
176 }
178 }
177
179
178 mac: !staticlib: {
180 mac: !staticlib: {
179 # Update the name (id) of the library on OSX to point to the lib path
181 # Update the name (id) of the library on OSX to point to the lib path
180 MAC_CHARTS_LIB_NAME = "lib"$$LIBRARY_NAME".1.dylib"
182 MAC_CHARTS_LIB_NAME = "lib"$$LIBRARY_NAME".1.dylib"
181 QMAKE_POST_LINK += "install_name_tool -id $$CHART_BUILD_LIB_DIR"/"$$MAC_CHARTS_LIB_NAME $$CHART_BUILD_LIB_DIR"/"$$MAC_CHARTS_LIB_NAME"
183 QMAKE_POST_LINK += "install_name_tool -id $$CHART_BUILD_LIB_DIR"/"$$MAC_CHARTS_LIB_NAME $$CHART_BUILD_LIB_DIR"/"$$MAC_CHARTS_LIB_NAME"
182
184
183 # Update the name (id) of the installed library on OSX to point to the installation path
185 # Update the name (id) of the installed library on OSX to point to the installation path
184 postinstall.path = $$[QT_INSTALL_LIBS]
186 postinstall.path = $$[QT_INSTALL_LIBS]
185 postinstall.extra = "install_name_tool -id $$[QT_INSTALL_LIBS]"/"$$MAC_CHARTS_LIB_NAME $$[QT_INSTALL_LIBS]"/"$$MAC_CHARTS_LIB_NAME"
187 postinstall.extra = "install_name_tool -id $$[QT_INSTALL_LIBS]"/"$$MAC_CHARTS_LIB_NAME $$[QT_INSTALL_LIBS]"/"$$MAC_CHARTS_LIB_NAME"
186 INSTALLS += postinstall
188 INSTALLS += postinstall
187 }
189 }
188
190
189 ################################ DEVELOPMENT BUILD ##########################################
191 ################################ DEVELOPMENT BUILD ##########################################
190 # There is a problem with jom.exe currently. It does not seem to understand QMAKE_EXTRA_TARGETS properly.
192 # There is a problem with jom.exe currently. It does not seem to understand QMAKE_EXTRA_TARGETS properly.
191 # This is the case at least with shadow builds.
193 # This is the case at least with shadow builds.
192 # http://qt-project.org/wiki/jom
194 # http://qt-project.org/wiki/jom
193
195
194 development_build:!win32-msvc*:{
196 development_build:!win32-msvc*:{
195 chartversion.target = $$PWD/qchartversion_p.h
197 chartversion.target = $$PWD/qchartversion_p.h
196
198
197 unix:{
199 unix:{
198 chartversion.commands = @echo \
200 chartversion.commands = @echo \
199 \" $${LITERAL_HASH}ifndef QCHARTVERSION_P_H\\n\
201 \" $${LITERAL_HASH}ifndef QCHARTVERSION_P_H\\n\
200 $${LITERAL_HASH}define QCHARTVERSION_P_H\\n\
202 $${LITERAL_HASH}define QCHARTVERSION_P_H\\n\
201 const char *buildTime = \\\"`date +'%y%m%d%H%M'`\\\" ; \\n\
203 const char *buildTime = \\\"`date +'%y%m%d%H%M'`\\\" ; \\n\
202 const char *gitHead = \\\"`git rev-parse HEAD`\\\" ; \\n \
204 const char *gitHead = \\\"`git rev-parse HEAD`\\\" ; \\n \
203 $${LITERAL_HASH}endif \" \
205 $${LITERAL_HASH}endif \" \
204 > \
206 > \
205 $$chartversion.target;
207 $$chartversion.target;
206 }else{
208 }else{
207 chartversion.commands = @echo \
209 chartversion.commands = @echo \
208 "const char *buildTime = \"%date%_%time%\" ; \
210 "const char *buildTime = \"%date%_%time%\" ; \
209 const char *gitHead = \"unknown\" ; " \
211 const char *gitHead = \"unknown\" ; " \
210 > \
212 > \
211 $$chartversion.target
213 $$chartversion.target
212 }
214 }
213
215
214 chartversion.depends = $$HEADERS \
216 chartversion.depends = $$HEADERS \
215 $$SOURCES
217 $$SOURCES
216
218
217 PRE_TARGETDEPS += $$chartversion.target
219 PRE_TARGETDEPS += $$chartversion.target
218 QMAKE_CLEAN += $$PWD/qchartversion_p.h
220 QMAKE_CLEAN += $$PWD/qchartversion_p.h
219 QMAKE_EXTRA_TARGETS += chartversion
221 QMAKE_EXTRA_TARGETS += chartversion
220 }
222 }
221
223
222 ############################### CLEAN ###########################################
224 ############################### CLEAN ###########################################
223
225
224 unix:QMAKE_DISTCLEAN += -r \
226 unix:QMAKE_DISTCLEAN += -r \
225 $$CHART_BUILD_HEADER_DIR \
227 $$CHART_BUILD_HEADER_DIR \
226 $$CHART_BUILD_LIB_DIR
228 $$CHART_BUILD_LIB_DIR
227 win32:QMAKE_DISTCLEAN += /Q \
229 win32:QMAKE_DISTCLEAN += /Q \
228 $$CHART_BUILD_HEADER_DIR \
230 $$CHART_BUILD_HEADER_DIR \
229 $$CHART_BUILD_LIB_DIR
231 $$CHART_BUILD_LIB_DIR
230
232
231 ############################## COVERAGE #########################################
233 ############################## COVERAGE #########################################
232
234
233 unix:coverage:{
235 unix:coverage:{
234
236
235 QMAKE_CXXFLAGS += -fprofile-arcs -ftest-coverage
237 QMAKE_CXXFLAGS += -fprofile-arcs -ftest-coverage
236 QMAKE_LDFLAGS += -fprofile-arcs -ftest-coverage
238 QMAKE_LDFLAGS += -fprofile-arcs -ftest-coverage
237
239
238 LIBS += -lgcov
240 LIBS += -lgcov
239
241
240 QMAKE_CLEAN += $$OBJECTS_DIR/*.gcda $$OBJECTS_DIR/*.gcno $$PWD/*.gcov ../coverage/*.info
242 QMAKE_CLEAN += $$OBJECTS_DIR/*.gcda $$OBJECTS_DIR/*.gcno $$PWD/*.gcov ../coverage/*.info
241 QMAKE_EXTRA_TARGETS += preparecoverage gencoverage
243 QMAKE_EXTRA_TARGETS += preparecoverage gencoverage
242
244
243 preparecoverage.target = prepare_coverage
245 preparecoverage.target = prepare_coverage
244 preparecoverage.depends = all
246 preparecoverage.depends = all
245 preparecoverage.commands = lcov --directory $$OBJECTS_DIR --zerocounters ;\
247 preparecoverage.commands = lcov --directory $$OBJECTS_DIR --zerocounters ;\
246 lcov -i -d $$OBJECTS_DIR -c -o ../coverage/base.info -b $$PWD;
248 lcov -i -d $$OBJECTS_DIR -c -o ../coverage/base.info -b $$PWD;
247
249
248 gencoverage.target = gen_coverage
250 gencoverage.target = gen_coverage
249 gencoverage.depends = all
251 gencoverage.depends = all
250 gencoverage.commands = lcov -d $$OBJECTS_DIR -c -o ../coverage/src.info -b $$PWD;\
252 gencoverage.commands = lcov -d $$OBJECTS_DIR -c -o ../coverage/src.info -b $$PWD;\
251 lcov -e ../coverage/base.info $$PWD/* $$PWD/animations/* $$PWD/areachart/* $$PWD/axis/* $$PWD/barchart/* $$PWD/legend/* $$PWD/linechart/* $$PWD/piechart/* $$PWD/scatterchart/* $$PWD/splinechart/* $$PWD/themes/* $$PWD/xychart/* -o ../coverage/base.info;\
253 lcov -e ../coverage/base.info $$PWD/* $$PWD/animations/* $$PWD/areachart/* $$PWD/axis/* $$PWD/barchart/* $$PWD/legend/* $$PWD/linechart/* $$PWD/piechart/* $$PWD/scatterchart/* $$PWD/splinechart/* $$PWD/themes/* $$PWD/xychart/* -o ../coverage/base.info;\
252 lcov -e ../coverage/src.info $$PWD/* $$PWD/animations/* $$PWD/areachart/* $$PWD/axis/* $$PWD/barchart/* $$PWD/legend/* $$PWD/linechart/* $$PWD/piechart/* $$PWD/scatterchart/* $$PWD/splinechart/* $$PWD/themes/* $$PWD/xychart/* -o ../coverage/src.info;\
254 lcov -e ../coverage/src.info $$PWD/* $$PWD/animations/* $$PWD/areachart/* $$PWD/axis/* $$PWD/barchart/* $$PWD/legend/* $$PWD/linechart/* $$PWD/piechart/* $$PWD/scatterchart/* $$PWD/splinechart/* $$PWD/themes/* $$PWD/xychart/* -o ../coverage/src.info;\
253 lcov -a ../coverage/base.info -a ../coverage/src.info -o ../coverage/coverage.info;
255 lcov -a ../coverage/base.info -a ../coverage/src.info -o ../coverage/coverage.info;
254 }
256 }
255
257
General Comments 0
You need to be logged in to leave comments. Login now