##// END OF EJS Templates
Multiaxis support...
Marek Rosa -
r2093:0a6899da1bbd
parent child
Show More
@@ -0,0 +1,84
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
14 **
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
18 **
19 ****************************************************************************/
20
21 #include <QApplication>
22 #include <QMainWindow>
23 #include <QChartView>
24 #include <QLineSeries>
25 #include <QValueAxis>
26 #include <QTime>
27 #include <QValueAxis>
28
29 QTCOMMERCIALCHART_USE_NAMESPACE
30
31 int main(int argc, char *argv[])
32 {
33 QApplication a(argc, argv);
34
35 qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
36
37 //![1]
38 QLineSeries* series;
39 QValueAxis *axisX;
40 QValueAxis *axisY;
41 QChart* chart = new QChart();
42 for (int i = 0; i < 5; i++) {
43 series = new QLineSeries;
44 for (int k(0); k < 8; k++)
45 series->append(i + k, qrand()%20);
46 chart->addSeries(series);
47
48 axisX = new QValueAxis;
49 axisX->setTickCount(7 + i);
50 axisX->setLinePenColor(series->pen().color());
51 if (i%2)
52 axisX->setAlternativePlacement(true);
53 axisY = new QValueAxis;
54 axisY->setTickCount(7 + i);
55 axisY->setLinePenColor(series->pen().color());
56 if (i%2)
57 axisY->setAlternativePlacement(true);
58
59 chart->setAxisX(axisX, series);
60 chart->setAxisY(axisY, series);
61 }
62 //![2]
63
64 //![3]
65 chart->legend()->hide();
66
67 chart->setTitle("Simple line chart example");
68 //![3]
69
70 //![4]
71 QChartView* chartView = new QChartView(chart);
72 chartView->setRenderHint(QPainter::Antialiasing);
73 //![4]
74
75
76 //![5]
77 QMainWindow window;
78 window.setCentralWidget(chartView);
79 window.resize(800, 600);
80 window.show();
81 //![5]
82
83 return a.exec();
84 }
@@ -0,0 +1,5
1 !include( ../examples.pri ) {
2 error( "Couldn't find the examples.pri file!" )
3 }
4 TARGET = multiaxis
5 SOURCES += main.cpp
@@ -1,38 +1,39
1 CURRENTLY_BUILDING_COMPONENTS = "examples"
1 CURRENTLY_BUILDING_COMPONENTS = "examples"
2 !include( ../config.pri ) {
2 !include( ../config.pri ) {
3 error( "Couldn't find the config.pri file!" )
3 error( "Couldn't find the config.pri file!" )
4 }
4 }
5
5
6 TEMPLATE = subdirs
6 TEMPLATE = subdirs
7 SUBDIRS += \
7 SUBDIRS += \
8 areachart \
8 areachart \
9 customchart \
9 customchart \
10 linechart \
10 linechart \
11 percentbarchart \
11 percentbarchart \
12 piechart \
12 piechart \
13 piechartdrilldown \
13 piechartdrilldown \
14 presenterchart \
14 presenterchart \
15 scatterchart \
15 scatterchart \
16 scatterinteractions \
16 scatterinteractions \
17 splinechart \
17 splinechart \
18 stackedbarchart \
18 stackedbarchart \
19 stackedbarchartdrilldown \
19 stackedbarchartdrilldown \
20 zoomlinechart \
20 zoomlinechart \
21 modeldata \
21 modeldata \
22 barchart \
22 barchart \
23 legend \
23 legend \
24 barmodelmapper \
24 barmodelmapper \
25 qmlpiechart \
25 qmlpiechart \
26 lineandbar \
26 lineandbar \
27 horizontalbarchart \
27 horizontalbarchart \
28 horizontalstackedbarchart \
28 horizontalstackedbarchart \
29 horizontalpercentbarchart \
29 horizontalpercentbarchart \
30 donutbreakdown \
30 donutbreakdown \
31 scrollchart \
31 scrollchart \
32 temperaturerecords \
32 temperaturerecords \
33 donutchart
33 donutchart \
34 multiaxis
34
35
35 !linux-arm*: {
36 !linux-arm*: {
36 SUBDIRS += \
37 SUBDIRS += \
37 datetimeaxis
38 datetimeaxis
38 }
39 }
@@ -1,487 +1,497
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 "qabstractaxis.h"
22 #include "qabstractaxis.h"
23 #include "qabstractaxis_p.h"
23 #include "qabstractaxis_p.h"
24 #include "chartpresenter_p.h"
24 #include "chartpresenter_p.h"
25 #include "domain_p.h"
25 #include "domain_p.h"
26 #include <qmath.h>
26 #include <qmath.h>
27 #include <QDateTime>
27 #include <QDateTime>
28 #include <QValueAxis>
28 #include <QValueAxis>
29 #include <QGraphicsLayout>
29 #include <QGraphicsLayout>
30 #include <QFontMetrics>
30 #include <QFontMetrics>
31
31
32 QTCOMMERCIALCHART_BEGIN_NAMESPACE
32 QTCOMMERCIALCHART_BEGIN_NAMESPACE
33
33
34 ChartAxis::ChartAxis(QAbstractAxis *axis,ChartPresenter *presenter) : ChartElement(presenter),
34 ChartAxis::ChartAxis(QAbstractAxis *axis,ChartPresenter *presenter) : ChartElement(presenter),
35 m_chartAxis(axis),
35 m_chartAxis(axis),
36 m_labelsAngle(0),
36 m_labelsAngle(0),
37 m_grid(new QGraphicsItemGroup(presenter->rootItem())),
37 m_grid(new QGraphicsItemGroup(presenter->rootItem())),
38 m_shades(new QGraphicsItemGroup(presenter->rootItem())),
38 m_shades(new QGraphicsItemGroup(presenter->rootItem())),
39 m_labels(new QGraphicsItemGroup(presenter->rootItem())),
39 m_labels(new QGraphicsItemGroup(presenter->rootItem())),
40 m_arrow(new QGraphicsItemGroup(presenter->rootItem())),
40 m_arrow(new QGraphicsItemGroup(presenter->rootItem())),
41 m_title(new QGraphicsSimpleTextItem(presenter->rootItem())),
41 m_title(new QGraphicsSimpleTextItem(presenter->rootItem())),
42 m_min(0),
42 m_min(0),
43 m_max(0),
43 m_max(0),
44 m_animation(0)
44 m_animation(0)
45 {
45 {
46 //initial initialization
46 //initial initialization
47 m_arrow->setZValue(ChartPresenter::AxisZValue);
47 m_arrow->setZValue(ChartPresenter::AxisZValue);
48 m_arrow->setHandlesChildEvents(false);
48 m_arrow->setHandlesChildEvents(false);
49 m_labels->setZValue(ChartPresenter::AxisZValue);
49 m_labels->setZValue(ChartPresenter::AxisZValue);
50 m_shades->setZValue(ChartPresenter::ShadesZValue);
50 m_shades->setZValue(ChartPresenter::ShadesZValue);
51 m_grid->setZValue(ChartPresenter::GridZValue);
51 m_grid->setZValue(ChartPresenter::GridZValue);
52
52
53 QObject::connect(m_chartAxis->d_ptr.data(),SIGNAL(updated()),this,SLOT(handleAxisUpdated()));
53 QObject::connect(m_chartAxis->d_ptr.data(),SIGNAL(updated()),this,SLOT(handleAxisUpdated()));
54
54
55 QGraphicsSimpleTextItem item;
55 QGraphicsSimpleTextItem item;
56 m_font = item.font();
56 m_font = item.font();
57
57
58 }
58 }
59
59
60 ChartAxis::~ChartAxis()
60 ChartAxis::~ChartAxis()
61 {
61 {
62 }
62 }
63
63
64 void ChartAxis::setAnimation(AxisAnimation* animation)
64 void ChartAxis::setAnimation(AxisAnimation* animation)
65 {
65 {
66 m_animation=animation;
66 m_animation=animation;
67 }
67 }
68
68
69 void ChartAxis::setLayout(QVector<qreal> &layout)
69 void ChartAxis::setLayout(QVector<qreal> &layout)
70 {
70 {
71 m_layoutVector=layout;
71 m_layoutVector=layout;
72 }
72 }
73
73
74 void ChartAxis::createItems(int count)
74 void ChartAxis::createItems(int count)
75 {
75 {
76 if (m_arrow->children().size() == 0)
76 if (m_arrow->children().size() == 0)
77 m_arrow->addToGroup(new AxisItem(this,presenter()->rootItem()));
77 m_arrow->addToGroup(new AxisItem(this,presenter()->rootItem()));
78 for (int i = 0; i < count; ++i) {
78 for (int i = 0; i < count; ++i) {
79 m_grid->addToGroup(new QGraphicsLineItem(presenter()->rootItem()));
79 m_grid->addToGroup(new QGraphicsLineItem(presenter()->rootItem()));
80 m_labels->addToGroup(new QGraphicsSimpleTextItem(presenter()->rootItem()));
80 m_labels->addToGroup(new QGraphicsSimpleTextItem(presenter()->rootItem()));
81 m_arrow->addToGroup(new QGraphicsLineItem(presenter()->rootItem()));
81 m_arrow->addToGroup(new QGraphicsLineItem(presenter()->rootItem()));
82 if ((m_grid->childItems().size())%2 && m_grid->childItems().size()>2) m_shades->addToGroup(new QGraphicsRectItem(presenter()->rootItem()));
82 if ((m_grid->childItems().size())%2 && m_grid->childItems().size()>2) m_shades->addToGroup(new QGraphicsRectItem(presenter()->rootItem()));
83 }
83 }
84 }
84 }
85
85
86 void ChartAxis::deleteItems(int count)
86 void ChartAxis::deleteItems(int count)
87 {
87 {
88 QList<QGraphicsItem *> lines = m_grid->childItems();
88 QList<QGraphicsItem *> lines = m_grid->childItems();
89 QList<QGraphicsItem *> labels = m_labels->childItems();
89 QList<QGraphicsItem *> labels = m_labels->childItems();
90 QList<QGraphicsItem *> shades = m_shades->childItems();
90 QList<QGraphicsItem *> shades = m_shades->childItems();
91 QList<QGraphicsItem *> axis = m_arrow->childItems();
91 QList<QGraphicsItem *> axis = m_arrow->childItems();
92
92
93 for (int i = 0; i < count; ++i) {
93 for (int i = 0; i < count; ++i) {
94 if (lines.size()%2 && lines.size() > 1) delete(shades.takeLast());
94 if (lines.size()%2 && lines.size() > 1) delete(shades.takeLast());
95 delete(lines.takeLast());
95 delete(lines.takeLast());
96 delete(labels.takeLast());
96 delete(labels.takeLast());
97 delete(axis.takeLast());
97 delete(axis.takeLast());
98 }
98 }
99 }
99 }
100
100
101 void ChartAxis::updateLayout(QVector<qreal> &layout)
101 void ChartAxis::updateLayout(QVector<qreal> &layout)
102 {
102 {
103 int diff = m_layoutVector.size() - layout.size();
103 int diff = m_layoutVector.size() - layout.size();
104
104
105 if (diff>0) {
105 if (diff>0) {
106 deleteItems(diff);
106 deleteItems(diff);
107 }
107 }
108 else if (diff<0) {
108 else if (diff<0) {
109 createItems(-diff);
109 createItems(-diff);
110 }
110 }
111
111
112 if(diff<0) handleAxisUpdated();
112 if(diff<0) handleAxisUpdated();
113
113
114 if (m_animation) {
114 if (m_animation) {
115 switch(presenter()->state()){
115 switch(presenter()->state()){
116 case ChartPresenter::ZoomInState:
116 case ChartPresenter::ZoomInState:
117 m_animation->setAnimationType(AxisAnimation::ZoomInAnimation);
117 m_animation->setAnimationType(AxisAnimation::ZoomInAnimation);
118 m_animation->setAnimationPoint(presenter()->statePoint());
118 m_animation->setAnimationPoint(presenter()->statePoint());
119 break;
119 break;
120 case ChartPresenter::ZoomOutState:
120 case ChartPresenter::ZoomOutState:
121 m_animation->setAnimationType(AxisAnimation::ZoomOutAnimation);
121 m_animation->setAnimationType(AxisAnimation::ZoomOutAnimation);
122 m_animation->setAnimationPoint(presenter()->statePoint());
122 m_animation->setAnimationPoint(presenter()->statePoint());
123 break;
123 break;
124 case ChartPresenter::ScrollUpState:
124 case ChartPresenter::ScrollUpState:
125 case ChartPresenter::ScrollLeftState:
125 case ChartPresenter::ScrollLeftState:
126 m_animation->setAnimationType(AxisAnimation::MoveBackwordAnimation);
126 m_animation->setAnimationType(AxisAnimation::MoveBackwordAnimation);
127 break;
127 break;
128 case ChartPresenter::ScrollDownState:
128 case ChartPresenter::ScrollDownState:
129 case ChartPresenter::ScrollRightState:
129 case ChartPresenter::ScrollRightState:
130 m_animation->setAnimationType(AxisAnimation::MoveForwardAnimation);
130 m_animation->setAnimationType(AxisAnimation::MoveForwardAnimation);
131 break;
131 break;
132 case ChartPresenter::ShowState:
132 case ChartPresenter::ShowState:
133 m_animation->setAnimationType(AxisAnimation::DefaultAnimation);
133 m_animation->setAnimationType(AxisAnimation::DefaultAnimation);
134 break;
134 break;
135 }
135 }
136 m_animation->setValues(m_layoutVector,layout);
136 m_animation->setValues(m_layoutVector,layout);
137 presenter()->startAnimation(m_animation);
137 presenter()->startAnimation(m_animation);
138 }
138 }
139 else {
139 else {
140 setLayout(layout);
140 setLayout(layout);
141 updateGeometry();
141 updateGeometry();
142 }
142 }
143 }
143 }
144
144
145 void ChartAxis::setArrowOpacity(qreal opacity)
145 void ChartAxis::setArrowOpacity(qreal opacity)
146 {
146 {
147 m_arrow->setOpacity(opacity);
147 m_arrow->setOpacity(opacity);
148 }
148 }
149
149
150 qreal ChartAxis::arrowOpacity() const
150 qreal ChartAxis::arrowOpacity() const
151 {
151 {
152 return m_arrow->opacity();
152 return m_arrow->opacity();
153 }
153 }
154
154
155 void ChartAxis::setArrowVisibility(bool visible)
155 void ChartAxis::setArrowVisibility(bool visible)
156 {
156 {
157 m_arrow->setOpacity(visible);
157 m_arrow->setOpacity(visible);
158 }
158 }
159
159
160 void ChartAxis::setGridOpacity(qreal opacity)
160 void ChartAxis::setGridOpacity(qreal opacity)
161 {
161 {
162 m_grid->setOpacity(opacity);
162 m_grid->setOpacity(opacity);
163 }
163 }
164
164
165 qreal ChartAxis::gridOpacity() const
165 qreal ChartAxis::gridOpacity() const
166 {
166 {
167 return m_grid->opacity();
167 return m_grid->opacity();
168 }
168 }
169
169
170 void ChartAxis::setGridVisibility(bool visible)
170 void ChartAxis::setGridVisibility(bool visible)
171 {
171 {
172 m_grid->setOpacity(visible);
172 m_grid->setOpacity(visible);
173 }
173 }
174
174
175 void ChartAxis::setLabelsOpacity(qreal opacity)
175 void ChartAxis::setLabelsOpacity(qreal opacity)
176 {
176 {
177 m_labels->setOpacity(opacity);
177 m_labels->setOpacity(opacity);
178 }
178 }
179
179
180 qreal ChartAxis::labelsOpacity() const
180 qreal ChartAxis::labelsOpacity() const
181 {
181 {
182 return m_labels->opacity();
182 return m_labels->opacity();
183 }
183 }
184
184
185 void ChartAxis::setLabelsVisibility(bool visible)
185 void ChartAxis::setLabelsVisibility(bool visible)
186 {
186 {
187 m_labels->setOpacity(visible);
187 m_labels->setOpacity(visible);
188 }
188 }
189
189
190 void ChartAxis::setShadesOpacity(qreal opacity)
190 void ChartAxis::setShadesOpacity(qreal opacity)
191 {
191 {
192 m_shades->setOpacity(opacity);
192 m_shades->setOpacity(opacity);
193 }
193 }
194
194
195 qreal ChartAxis::shadesOpacity() const
195 qreal ChartAxis::shadesOpacity() const
196 {
196 {
197 return m_shades->opacity();
197 return m_shades->opacity();
198 }
198 }
199
199
200 void ChartAxis::setShadesVisibility(bool visible)
200 void ChartAxis::setShadesVisibility(bool visible)
201 {
201 {
202 m_shades->setVisible(visible);
202 m_shades->setVisible(visible);
203 }
203 }
204
204
205 void ChartAxis::setLabelsAngle(int angle)
205 void ChartAxis::setLabelsAngle(int angle)
206 {
206 {
207 foreach(QGraphicsItem* item , m_labels->childItems()) {
207 foreach(QGraphicsItem* item , m_labels->childItems()) {
208 item->setRotation(angle);
208 item->setRotation(angle);
209 }
209 }
210
210
211 m_labelsAngle=angle;
211 m_labelsAngle=angle;
212 }
212 }
213
213
214 void ChartAxis::setLabelsPen(const QPen &pen)
214 void ChartAxis::setLabelsPen(const QPen &pen)
215 {
215 {
216 foreach(QGraphicsItem* item , m_labels->childItems()) {
216 foreach(QGraphicsItem* item , m_labels->childItems()) {
217 static_cast<QGraphicsSimpleTextItem*>(item)->setPen(pen);
217 static_cast<QGraphicsSimpleTextItem*>(item)->setPen(pen);
218 }
218 }
219 }
219 }
220
220
221 void ChartAxis::setLabelsBrush(const QBrush &brush)
221 void ChartAxis::setLabelsBrush(const QBrush &brush)
222 {
222 {
223 foreach(QGraphicsItem* item , m_labels->childItems()) {
223 foreach(QGraphicsItem* item , m_labels->childItems()) {
224 static_cast<QGraphicsSimpleTextItem*>(item)->setBrush(brush);
224 static_cast<QGraphicsSimpleTextItem*>(item)->setBrush(brush);
225 }
225 }
226 }
226 }
227
227
228 void ChartAxis::setLabelsFont(const QFont &font)
228 void ChartAxis::setLabelsFont(const QFont &font)
229 {
229 {
230 foreach(QGraphicsItem* item , m_labels->childItems()) {
230 foreach(QGraphicsItem* item , m_labels->childItems()) {
231 static_cast<QGraphicsSimpleTextItem*>(item)->setFont(font);
231 static_cast<QGraphicsSimpleTextItem*>(item)->setFont(font);
232 }
232 }
233 if(m_font!=font) {
233 if(m_font!=font) {
234 m_font = font;
234 m_font = font;
235 foreach(QGraphicsItem* item , m_labels->childItems()) {
235 foreach(QGraphicsItem* item , m_labels->childItems()) {
236 static_cast<QGraphicsSimpleTextItem*>(item)->setFont(font);
236 static_cast<QGraphicsSimpleTextItem*>(item)->setFont(font);
237 }
237 }
238 QGraphicsLayoutItem::updateGeometry();
238 QGraphicsLayoutItem::updateGeometry();
239 presenter()->layout()->invalidate();
239 presenter()->layout()->invalidate();
240
240
241 }
241 }
242 }
242 }
243
243
244 void ChartAxis::setShadesBrush(const QBrush &brush)
244 void ChartAxis::setShadesBrush(const QBrush &brush)
245 {
245 {
246 foreach(QGraphicsItem* item , m_shades->childItems()) {
246 foreach(QGraphicsItem* item , m_shades->childItems()) {
247 static_cast<QGraphicsRectItem*>(item)->setBrush(brush);
247 static_cast<QGraphicsRectItem*>(item)->setBrush(brush);
248 }
248 }
249 }
249 }
250
250
251 void ChartAxis::setShadesPen(const QPen &pen)
251 void ChartAxis::setShadesPen(const QPen &pen)
252 {
252 {
253 foreach(QGraphicsItem* item , m_shades->childItems()) {
253 foreach(QGraphicsItem* item , m_shades->childItems()) {
254 static_cast<QGraphicsRectItem*>(item)->setPen(pen);
254 static_cast<QGraphicsRectItem*>(item)->setPen(pen);
255 }
255 }
256 }
256 }
257
257
258 void ChartAxis::setArrowPen(const QPen &pen)
258 void ChartAxis::setArrowPen(const QPen &pen)
259 {
259 {
260 foreach(QGraphicsItem* item , m_arrow->childItems()) {
260 foreach(QGraphicsItem* item , m_arrow->childItems()) {
261 static_cast<QGraphicsLineItem*>(item)->setPen(pen);
261 static_cast<QGraphicsLineItem*>(item)->setPen(pen);
262 }
262 }
263 }
263 }
264
264
265 void ChartAxis::setGridPen(const QPen &pen)
265 void ChartAxis::setGridPen(const QPen &pen)
266 {
266 {
267 foreach(QGraphicsItem* item , m_grid->childItems()) {
267 foreach(QGraphicsItem* item , m_grid->childItems()) {
268 static_cast<QGraphicsLineItem*>(item)->setPen(pen);
268 static_cast<QGraphicsLineItem*>(item)->setPen(pen);
269 }
269 }
270 }
270 }
271
271
272 bool ChartAxis::isEmpty()
272 bool ChartAxis::isEmpty()
273 {
273 {
274 return !m_rect.isValid() || presenter()->chartsGeometry().isEmpty() || qFuzzyIsNull(m_min - m_max);
274 return !m_rect.isValid() || presenter()->chartsGeometry().isEmpty() || qFuzzyIsNull(m_min - m_max);
275 }
275 }
276
276
277 void ChartAxis::handleDomainUpdated()
277 void ChartAxis::handleDomainUpdated()
278 {
278 {
279 Domain* domain = qobject_cast<Domain*>(sender());
279 Domain* domain = qobject_cast<Domain*>(sender());
280 qreal min(0);
280 qreal min(0);
281 qreal max(0);
281 qreal max(0);
282
282
283 if(m_chartAxis->orientation()==Qt::Horizontal) {
283 if(m_chartAxis->orientation()==Qt::Horizontal) {
284 min = domain->minX();
284 min = domain->minX();
285 max = domain->maxX();
285 max = domain->maxX();
286 }
286 }
287 else if (m_chartAxis->orientation()==Qt::Vertical)
287 else if (m_chartAxis->orientation()==Qt::Vertical)
288 {
288 {
289 min = domain->minY();
289 min = domain->minY();
290 max = domain->maxY();
290 max = domain->maxY();
291 }
291 }
292
292
293 if (!qFuzzyIsNull(m_min - min) || !qFuzzyIsNull(m_max - max))
293 if (!qFuzzyIsNull(m_min - min) || !qFuzzyIsNull(m_max - max))
294 {
294 {
295 m_min = min;
295 m_min = min;
296 m_max = max;
296 m_max = max;
297
297
298 if (!isEmpty()) {
298 if (!isEmpty()) {
299
299
300 QVector<qreal> layout = calculateLayout();
300 QVector<qreal> layout = calculateLayout();
301 updateLayout(layout);
301 updateLayout(layout);
302 QSizeF before = effectiveSizeHint(Qt::MinimumSize);
302 QSizeF before = effectiveSizeHint(Qt::MinimumSize);
303
303
304 QSizeF after= sizeHint(Qt::MinimumSize);
304 QSizeF after= sizeHint(Qt::MinimumSize);
305
305
306 if(before!=after) {
306 if(before!=after) {
307 QGraphicsLayoutItem::updateGeometry();
307 QGraphicsLayoutItem::updateGeometry();
308 //we don't want to call invalidate on layout, since it will change minimum size of component,
308 //we don't want to call invalidate on layout, since it will change minimum size of component,
309 //which we would like to avoid since it causes nasty flips when scrolling or zooming,
309 //which we would like to avoid since it causes nasty flips when scrolling or zooming,
310 //instead recalculate layout and use plotArea for extra space.
310 //instead recalculate layout and use plotArea for extra space.
311 presenter()->layout()->setGeometry(presenter()->layout()->geometry());
311 presenter()->layout()->setGeometry(presenter()->layout()->geometry());
312 }
312 }
313 }
313 }
314 }
314 }
315 }
315 }
316
316
317 void ChartAxis::handleAxisUpdated()
317 void ChartAxis::handleAxisUpdated()
318 {
318 {
319 if(isEmpty()) return;
319 if(isEmpty()) return;
320
320
321
321
322 bool visible = m_chartAxis->isVisible();
322 bool visible = m_chartAxis->isVisible();
323
323
324 setArrowVisibility(visible && m_chartAxis->isLineVisible());
324 setArrowVisibility(visible && m_chartAxis->isLineVisible());
325 setGridVisibility(visible && m_chartAxis->isGridLineVisible());
325 setGridVisibility(visible && m_chartAxis->isGridLineVisible());
326 setLabelsVisibility(visible && m_chartAxis->labelsVisible());
326 setLabelsVisibility(visible && m_chartAxis->labelsVisible());
327 setShadesVisibility(visible && m_chartAxis->shadesVisible());
327 setShadesVisibility(visible && m_chartAxis->shadesVisible());
328 setLabelsAngle(m_chartAxis->labelsAngle());
328 setLabelsAngle(m_chartAxis->labelsAngle());
329 setArrowPen(m_chartAxis->linePen());
329 setArrowPen(m_chartAxis->linePen());
330 setLabelsPen(m_chartAxis->labelsPen());
330 setLabelsPen(m_chartAxis->labelsPen());
331 setLabelsBrush(m_chartAxis->labelsBrush());
331 setLabelsBrush(m_chartAxis->labelsBrush());
332 setLabelsFont(m_chartAxis->labelsFont());
332 setLabelsFont(m_chartAxis->labelsFont());
333 setGridPen(m_chartAxis->gridLinePen());
333 setGridPen(m_chartAxis->gridLinePen());
334 setShadesPen(m_chartAxis->shadesPen());
334 setShadesPen(m_chartAxis->shadesPen());
335 setShadesBrush(m_chartAxis->shadesBrush());
335 setShadesBrush(m_chartAxis->shadesBrush());
336 setTitleText(m_chartAxis->title());
336 setTitleText(m_chartAxis->title());
337 }
337 }
338
338
339 void ChartAxis::setTitleText(const QString& title)
339 void ChartAxis::setTitleText(const QString& title)
340 {
340 {
341 if(m_titleText!=title) {
341 if(m_titleText!=title) {
342 m_titleText = title;
342 m_titleText = title;
343 m_rect = QRect();
343 m_rect = QRect();
344 QGraphicsLayoutItem::updateGeometry();
344 QGraphicsLayoutItem::updateGeometry();
345 presenter()->layout()->invalidate();
345 presenter()->layout()->invalidate();
346 }
346 }
347 }
347 }
348
348
349 void ChartAxis::hide()
349 void ChartAxis::hide()
350 {
350 {
351 setArrowVisibility(false);
351 setArrowVisibility(false);
352 setGridVisibility(false);
352 setGridVisibility(false);
353 setLabelsVisibility(false);
353 setLabelsVisibility(false);
354 setShadesVisibility(false);
354 setShadesVisibility(false);
355 }
355 }
356
356
357 void ChartAxis::setGeometry(const QRectF &rect)
357 void ChartAxis::setGeometry(const QRectF &rect)
358 {
358 {
359
359
360 m_rect = rect;
360 m_rect = rect;
361
361
362 if (isEmpty()) return;
362 if (isEmpty()) return;
363
363
364 if(!m_titleText.isNull()) {
364 if(!m_titleText.isNull()) {
365 QFontMetrics fn(m_title->font());
365 QFontMetrics fn(m_title->font());
366
366
367 int size(0);
367 int size(0);
368
368
369 QRectF chartRect = presenter()->chartsGeometry();
369 QRectF chartRect = presenter()->chartsGeometry();
370
370
371 if(orientation()==Qt::Horizontal)
371 if(orientation()==Qt::Horizontal)
372 size = chartRect.width();
372 size = chartRect.width();
373 else if(orientation()==Qt::Vertical)
373 else if(orientation()==Qt::Vertical)
374 size = chartRect.height();
374 size = chartRect.height();
375
375
376 if (fn.boundingRect(m_titleText).width() > size)
376 if (fn.boundingRect(m_titleText).width() > size)
377 {
377 {
378 QString string = m_titleText + "...";
378 QString string = m_titleText + "...";
379 while (fn.boundingRect(string).width() > size && string.length() > 3)
379 while (fn.boundingRect(string).width() > size && string.length() > 3)
380 string.remove(string.length() - 4, 1);
380 string.remove(string.length() - 4, 1);
381 m_title->setText(string);
381 m_title->setText(string);
382 }
382 }
383 else
383 else
384 m_title->setText(m_titleText);
384 m_title->setText(m_titleText);
385
385
386 QPointF center = chartRect.center() - m_title->boundingRect().center();
386 QPointF center = chartRect.center() - m_title->boundingRect().center();
387 if(orientation()==Qt::Horizontal) {
387 if(orientation()==Qt::Horizontal) {
388 m_title->setPos(center.x(),m_rect.bottom()-m_title->boundingRect().height());
388 m_title->setPos(center.x(),m_rect.bottom()-m_title->boundingRect().height());
389 }
389 }
390 else if(orientation()==Qt::Vertical) {
390 else if(orientation()==Qt::Vertical) {
391 m_title->setTransformOriginPoint(m_title->boundingRect().center());
391 m_title->setTransformOriginPoint(m_title->boundingRect().center());
392 m_title->setRotation(270);
392 m_title->setRotation(270);
393 m_title->setPos(m_rect.left()- m_title->boundingRect().width()/2+m_title->boundingRect().height()/2,center.y());
393 m_title->setPos(m_rect.left()- m_title->boundingRect().width()/2+m_title->boundingRect().height()/2,center.y());
394 }
394 }
395 }
395 }
396
396
397 QVector<qreal> layout = calculateLayout();
397 QVector<qreal> layout = calculateLayout();
398 updateLayout(layout);
398 updateLayout(layout);
399
399
400 }
400 }
401
401
402 void ChartAxis::setInternalRect(const QRectF &size)
403 {
404 m_internalRect = size;
405 }
406
402 void ChartAxis::axisSelected()
407 void ChartAxis::axisSelected()
403 {
408 {
404 //TODO: axis clicked;
409 //TODO: axis clicked;
405 }
410 }
406
411
407
412
408 QStringList ChartAxis::createNumberLabels(qreal min, qreal max, int ticks) const
413 QStringList ChartAxis::createNumberLabels(qreal min, qreal max, int ticks) const
409 {
414 {
410 Q_ASSERT(max>min);
415 Q_ASSERT(max>min);
411 Q_ASSERT(ticks>1);
416 Q_ASSERT(ticks>1);
412
417
413 QStringList labels;
418 QStringList labels;
414
419
415 int n = qMax(int(-qFloor(log10((max-min)/(ticks-1)))),0);
420 int n = qMax(int(-qFloor(log10((max-min)/(ticks-1)))),0);
416 n++;
421 n++;
417
422
418 QValueAxis *axis = qobject_cast<QValueAxis *>(m_chartAxis);
423 QValueAxis *axis = qobject_cast<QValueAxis *>(m_chartAxis);
419
424
420 QString format = axis->labelFormat();
425 QString format = axis->labelFormat();
421
426
422 if(format.isNull()) {
427 if(format.isNull()) {
423 for (int i=0; i< ticks; i++) {
428 for (int i=0; i< ticks; i++) {
424 qreal value = min + (i * (max - min)/ (ticks-1));
429 qreal value = min + (i * (max - min)/ (ticks-1));
425 labels << QString::number(value,'f',n);
430 labels << QString::number(value,'f',n);
426 }
431 }
427 }
432 }
428 else {
433 else {
429 QByteArray array = format.toAscii();
434 QByteArray array = format.toAscii();
430 for (int i=0; i< ticks; i++) {
435 for (int i=0; i< ticks; i++) {
431 qreal value = min + (i * (max - min)/ (ticks-1));
436 qreal value = min + (i * (max - min)/ (ticks-1));
432 labels << QString().sprintf(array, value);
437 labels << QString().sprintf(array, value);
433 }
438 }
434 }
439 }
435
440
436 return labels;
441 return labels;
437 }
442 }
438
443
439 Qt::Orientation ChartAxis::orientation() const
444 Qt::Orientation ChartAxis::orientation() const
440 {
445 {
441 return m_chartAxis->orientation();
446 return m_chartAxis->orientation();
442 }
447 }
443
448
449 bool ChartAxis::alternativePlacement() const
450 {
451 return m_chartAxis->alternativePlacement();
452 }
453
444 bool ChartAxis::isVisible()
454 bool ChartAxis::isVisible()
445 {
455 {
446 return m_chartAxis->isVisible();
456 return m_chartAxis->isVisible();
447 }
457 }
448
458
449 QSizeF ChartAxis::sizeHint(Qt::SizeHint which, const QSizeF& constraint) const
459 QSizeF ChartAxis::sizeHint(Qt::SizeHint which, const QSizeF& constraint) const
450 {
460 {
451
461
452 Q_UNUSED(constraint);
462 Q_UNUSED(constraint);
453 QFontMetrics fn(m_title->font());
463 QFontMetrics fn(m_title->font());
454 QSizeF sh;
464 QSizeF sh;
455
465
456 if(m_titleText.isNull()) return sh;
466 if(m_titleText.isNull()) return sh;
457
467
458 switch(which) {
468 switch(which) {
459 case Qt::MinimumSize:
469 case Qt::MinimumSize:
460 if(orientation()==Qt::Horizontal) {
470 if(orientation()==Qt::Horizontal) {
461 sh = QSizeF(fn.boundingRect ("...").width(),fn.height());
471 sh = QSizeF(fn.boundingRect ("...").width(),fn.height());
462 }
472 }
463 else if(orientation()==Qt::Vertical) {
473 else if(orientation()==Qt::Vertical) {
464 sh = QSizeF(fn.height(),fn.boundingRect ("...").width());
474 sh = QSizeF(fn.height(),fn.boundingRect ("...").width());
465 }
475 }
466
476
467 break;
477 break;
468 case Qt::MaximumSize:
478 case Qt::MaximumSize:
469 case Qt::PreferredSize:
479 case Qt::PreferredSize:
470 if(orientation()==Qt::Horizontal) {
480 if(orientation()==Qt::Horizontal) {
471 sh = QSizeF(fn.boundingRect(m_chartAxis->title()).width(),fn.height());
481 sh = QSizeF(fn.boundingRect(m_chartAxis->title()).width(),fn.height());
472 }
482 }
473 else if(orientation()==Qt::Vertical) {
483 else if(orientation()==Qt::Vertical) {
474 sh = QSizeF(fn.height(),fn.boundingRect(m_chartAxis->title()).width());
484 sh = QSizeF(fn.height(),fn.boundingRect(m_chartAxis->title()).width());
475 }
485 }
476
486
477 break;
487 break;
478 default:
488 default:
479 break;
489 break;
480 }
490 }
481
491
482 return sh;
492 return sh;
483 }
493 }
484
494
485 #include "moc_chartaxis_p.cpp"
495 #include "moc_chartaxis_p.cpp"
486
496
487 QTCOMMERCIALCHART_END_NAMESPACE
497 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,181 +1,185
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 "chartelement_p.h"
34 #include "chartelement_p.h"
35 #include "axisanimation_p.h"
35 #include "axisanimation_p.h"
36 #include <QGraphicsItem>
36 #include <QGraphicsItem>
37 #include <QGraphicsLayoutItem>
37 #include <QGraphicsLayoutItem>
38 #include <QFont>
38 #include <QFont>
39
39
40 QTCOMMERCIALCHART_BEGIN_NAMESPACE
40 QTCOMMERCIALCHART_BEGIN_NAMESPACE
41
41
42 class QAbstractAxis;
42 class QAbstractAxis;
43 class ChartPresenter;
43 class ChartPresenter;
44
44
45 class ChartAxis : public ChartElement, public QGraphicsLayoutItem
45 class ChartAxis : public ChartElement, public QGraphicsLayoutItem
46 {
46 {
47 Q_OBJECT
47 Q_OBJECT
48 Q_INTERFACES(QGraphicsLayoutItem)
48 Q_INTERFACES(QGraphicsLayoutItem)
49 public:
49 public:
50 enum AxisType{ X_AXIS,Y_AXIS };
50 enum AxisType{ X_AXIS,Y_AXIS };
51
51
52 ChartAxis(QAbstractAxis *axis, ChartPresenter *presenter);
52 ChartAxis(QAbstractAxis *axis, ChartPresenter *presenter);
53 ~ChartAxis();
53 ~ChartAxis();
54
54
55 virtual AxisType axisType() const = 0;
55 virtual AxisType axisType() const = 0;
56
56
57 void setArrowOpacity(qreal opacity);
57 void setArrowOpacity(qreal opacity);
58 qreal arrowOpacity() const;
58 qreal arrowOpacity() const;
59 void setArrowVisibility(bool visible);
59 void setArrowVisibility(bool visible);
60
60
61 void setGridOpacity(qreal opacity);
61 void setGridOpacity(qreal opacity);
62 qreal gridOpacity() const;
62 qreal gridOpacity() const;
63 void setGridVisibility(bool visible);
63 void setGridVisibility(bool visible);
64
64
65 void setLabelsOpacity(qreal opacity);
65 void setLabelsOpacity(qreal opacity);
66 qreal labelsOpacity() const;
66 qreal labelsOpacity() const;
67 void setLabelsVisibility(bool visible);
67 void setLabelsVisibility(bool visible);
68
68
69 void setShadesOpacity(qreal opacity);
69 void setShadesOpacity(qreal opacity);
70 qreal shadesOpacity() const;
70 qreal shadesOpacity() const;
71 void setShadesVisibility(bool visible);
71 void setShadesVisibility(bool visible);
72
72
73 void setLabelsAngle(int angle);
73 void setLabelsAngle(int angle);
74 int labelsAngle()const { return m_labelsAngle; }
74 int labelsAngle()const { return m_labelsAngle; }
75
75
76 void setShadesBrush(const QBrush &brush);
76 void setShadesBrush(const QBrush &brush);
77 void setShadesPen(const QPen &pen);
77 void setShadesPen(const QPen &pen);
78
78
79 void setArrowPen(const QPen &pen);
79 void setArrowPen(const QPen &pen);
80 void setGridPen(const QPen &pen);
80 void setGridPen(const QPen &pen);
81
81
82 void setLabelsPen(const QPen &pen);
82 void setLabelsPen(const QPen &pen);
83 void setLabelsBrush(const QBrush &brush);
83 void setLabelsBrush(const QBrush &brush);
84 void setLabelsFont(const QFont &font);
84 void setLabelsFont(const QFont &font);
85
85
86 void setTitlePen(const QPen &pen);
86 void setTitlePen(const QPen &pen);
87 void setTitleBrush(const QBrush &brush);
87 void setTitleBrush(const QBrush &brush);
88 void setTitleFont(const QFont &font);
88 void setTitleFont(const QFont &font);
89 void setTitleText(const QString& title);
89 void setTitleText(const QString& title);
90
90
91
91
92 void setLayout(QVector<qreal> &layout);
92 void setLayout(QVector<qreal> &layout);
93 QVector<qreal> layout() const { return m_layoutVector; }
93 QVector<qreal> layout() const { return m_layoutVector; }
94
94
95 void setAnimation(AxisAnimation* animation);
95 void setAnimation(AxisAnimation* animation);
96 ChartAnimation* animation() const { return m_animation; };
96 ChartAnimation* animation() const { return m_animation; };
97
97
98 Qt::Orientation orientation() const;
98 Qt::Orientation orientation() const;
99 bool alternativePlacement() const;
99
100
100 bool isVisible();
101 bool isVisible();
101 void hide();
102 void hide();
102
103
103 void setGeometry(const QRectF &size);
104 void setGeometry(const QRectF &size);
104 QRectF geometry() const { return m_rect; }
105 QRectF geometry() const { return m_rect; }
105
106
107 void setInternalRect(const QRectF &size);
108
106 virtual QSizeF sizeHint(Qt::SizeHint which, const QSizeF& constraint = QSizeF()) const;
109 virtual QSizeF sizeHint(Qt::SizeHint which, const QSizeF& constraint = QSizeF()) const;
107
110
108 protected:
111 protected:
109 virtual void updateGeometry() = 0;
112 virtual void updateGeometry() = 0;
110 virtual QVector<qreal> calculateLayout() const = 0;
113 virtual QVector<qreal> calculateLayout() const = 0;
111 QStringList createNumberLabels(qreal min, qreal max,int ticks) const;
114 QStringList createNumberLabels(qreal min, qreal max,int ticks) const;
112
115
113
116
114 public Q_SLOTS:
117 public Q_SLOTS:
115 virtual void handleAxisUpdated();
118 virtual void handleAxisUpdated();
116 virtual void handleDomainUpdated();
119 virtual void handleDomainUpdated();
117
120
118
121
119 private:
122 private:
120 inline bool isEmpty();
123 inline bool isEmpty();
121 void createItems(int count);
124 void createItems(int count);
122 void deleteItems(int count);
125 void deleteItems(int count);
123 void updateLayout(QVector<qreal> &layout);
126 void updateLayout(QVector<qreal> &layout);
124 void axisSelected();
127 void axisSelected();
125
128
126 protected:
129 protected:
127 QAbstractAxis* m_chartAxis;
130 QAbstractAxis* m_chartAxis;
128 int m_labelsAngle;
131 int m_labelsAngle;
129 //TODO: to be removed
132 //TODO: to be removed
130 QRectF m_rect;
133 QRectF m_rect;
134 QRectF m_internalRect;
131 QScopedPointer<QGraphicsItemGroup> m_grid;
135 QScopedPointer<QGraphicsItemGroup> m_grid;
132 QScopedPointer<QGraphicsItemGroup> m_shades;
136 QScopedPointer<QGraphicsItemGroup> m_shades;
133 QScopedPointer<QGraphicsItemGroup> m_labels;
137 QScopedPointer<QGraphicsItemGroup> m_labels;
134 QScopedPointer<QGraphicsItemGroup> m_arrow;
138 QScopedPointer<QGraphicsItemGroup> m_arrow;
135 QGraphicsSimpleTextItem* m_title;
139 QGraphicsSimpleTextItem* m_title;
136 QVector<qreal> m_layoutVector;
140 QVector<qreal> m_layoutVector;
137 qreal m_min;
141 qreal m_min;
138 qreal m_max;
142 qreal m_max;
139 AxisAnimation *m_animation;
143 AxisAnimation *m_animation;
140 QFont m_font;
144 QFont m_font;
141 QString m_titleText;
145 QString m_titleText;
142
146
143 friend class AxisAnimation;
147 friend class AxisAnimation;
144 friend class AxisItem;
148 friend class AxisItem;
145
149
146 };
150 };
147
151
148 class AxisItem: public QGraphicsLineItem
152 class AxisItem: public QGraphicsLineItem
149 {
153 {
150
154
151 public:
155 public:
152 explicit AxisItem(ChartAxis *axis, QGraphicsItem *parent = 0) : QGraphicsLineItem(parent), m_axis(axis) {}
156 explicit AxisItem(ChartAxis *axis, QGraphicsItem *parent = 0) : QGraphicsLineItem(parent), m_axis(axis) {}
153
157
154 protected:
158 protected:
155 void mousePressEvent(QGraphicsSceneMouseEvent *event)
159 void mousePressEvent(QGraphicsSceneMouseEvent *event)
156 {
160 {
157 Q_UNUSED(event)
161 Q_UNUSED(event)
158 m_axis->axisSelected();
162 m_axis->axisSelected();
159 }
163 }
160
164
161 QRectF boundingRect() const
165 QRectF boundingRect() const
162 {
166 {
163 return shape().boundingRect();
167 return shape().boundingRect();
164 }
168 }
165
169
166 QPainterPath shape() const
170 QPainterPath shape() const
167 {
171 {
168 QPainterPath path = QGraphicsLineItem::shape();
172 QPainterPath path = QGraphicsLineItem::shape();
169 QRectF rect = path.boundingRect();
173 QRectF rect = path.boundingRect();
170 path.addRect(rect.adjusted(0,0,m_axis->axisType()!=ChartAxis::X_AXIS?8:0,m_axis->axisType()!=ChartAxis::Y_AXIS?8:0));
174 path.addRect(rect.adjusted(0,0,m_axis->axisType()!=ChartAxis::X_AXIS?8:0,m_axis->axisType()!=ChartAxis::Y_AXIS?8:0));
171 return path;
175 return path;
172 }
176 }
173
177
174 private:
178 private:
175 ChartAxis* m_axis;
179 ChartAxis* m_axis;
176
180
177 };
181 };
178
182
179 QTCOMMERCIALCHART_END_NAMESPACE
183 QTCOMMERCIALCHART_END_NAMESPACE
180
184
181 #endif /* CHARTAXI_H */
185 #endif /* CHARTAXI_H */
@@ -1,742 +1,753
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 "qabstractaxis.h"
21 #include "qabstractaxis.h"
22 #include "qabstractaxis_p.h"
22 #include "qabstractaxis_p.h"
23
23
24 QTCOMMERCIALCHART_BEGIN_NAMESPACE
24 QTCOMMERCIALCHART_BEGIN_NAMESPACE
25
25
26 /*!
26 /*!
27 \class QAbstractAxis
27 \class QAbstractAxis
28 \brief The QAbstractAxis class is used for manipulating chart's axis.
28 \brief The QAbstractAxis class is used for manipulating chart's axis.
29 \mainclass
29 \mainclass
30
30
31 There is only one x Axis visible at the time, however there can be multiple y axes.
31 There is only one x Axis visible at the time, however there can be multiple y axes.
32 Each chart series can be bound to exactly one Y axis and the shared common X axis.
32 Each chart series can be bound to exactly one Y axis and the shared common X axis.
33 Axis can be setup to show axis line with tick marks, grid lines and shades.
33 Axis can be setup to show axis line with tick marks, grid lines and shades.
34 */
34 */
35
35
36 /*!
36 /*!
37 \qmlclass AbstractAxis QAbstractAxis
37 \qmlclass AbstractAxis QAbstractAxis
38 \brief The Axis element is used for manipulating chart's axes
38 \brief The Axis element is used for manipulating chart's axes
39
39
40 There is only one x Axis visible at the time, however there can be multiple y axes on a ChartView.
40 There is only one x Axis visible at the time, however there can be multiple y axes on a ChartView.
41 Each chart series can be bound to exactly one Y axis and the shared common X axis.
41 Each chart series can be bound to exactly one Y axis and the shared common X axis.
42 Axis can be setup to show axis line with tick marks, grid lines and shades.
42 Axis can be setup to show axis line with tick marks, grid lines and shades.
43
43
44 To access Axes you can use ChartView API. For example:
44 To access Axes you can use ChartView API. For example:
45 \code
45 \code
46 ChartView {
46 ChartView {
47 axisX.min: 0
47 axisX.min: 0
48 axisX.max: 3
48 axisX.max: 3
49 axisX.ticksCount: 4
49 axisX.ticksCount: 4
50 axisY.min: 0
50 axisY.min: 0
51 axisY.max: 4
51 axisY.max: 4
52 // Add a few series...
52 // Add a few series...
53 }
53 }
54 \endcode
54 \endcode
55 */
55 */
56
56
57 /*!
57 /*!
58 \enum QAbstractAxis::AxisType
58 \enum QAbstractAxis::AxisType
59
59
60 The type of the series object.
60 The type of the series object.
61
61
62 \value AxisTypeNoAxis
62 \value AxisTypeNoAxis
63 \value AxisTypeValue
63 \value AxisTypeValue
64 \value AxisTypeBarCategory
64 \value AxisTypeBarCategory
65 \value AxisTypeCategory
65 \value AxisTypeCategory
66 \value AxisTypeDateTime
66 \value AxisTypeDateTime
67 */
67 */
68
68
69 /*!
69 /*!
70 *\fn void QAbstractAxis::type() const
70 *\fn void QAbstractAxis::type() const
71 Returns the type of the axis
71 Returns the type of the axis
72 */
72 */
73
73
74 /*!
74 /*!
75 \property QAbstractAxis::lineVisible
75 \property QAbstractAxis::lineVisible
76 The visibility of the axis line
76 The visibility of the axis line
77 */
77 */
78 /*!
78 /*!
79 \qmlproperty bool AbstractAxis::lineVisible
79 \qmlproperty bool AbstractAxis::lineVisible
80 The visibility of the axis line
80 The visibility of the axis line
81 */
81 */
82
82
83 /*!
83 /*!
84 \property QAbstractAxis::labelsVisible
84 \property QAbstractAxis::labelsVisible
85 Defines if axis labels are visible.
85 Defines if axis labels are visible.
86 */
86 */
87 /*!
87 /*!
88 \qmlproperty bool AbstractAxis::labelsVisible
88 \qmlproperty bool AbstractAxis::labelsVisible
89 Defines if axis labels are visible.
89 Defines if axis labels are visible.
90 */
90 */
91
91
92 /*!
92 /*!
93 \property QAbstractAxis::visible
93 \property QAbstractAxis::visible
94 The visibility of the axis.
94 The visibility of the axis.
95 */
95 */
96 /*!
96 /*!
97 \qmlproperty bool AbstractAxis::visible
97 \qmlproperty bool AbstractAxis::visible
98 The visibility of the axis.
98 The visibility of the axis.
99 */
99 */
100
100
101 /*!
101 /*!
102 \property QAbstractAxis::gridVisible
102 \property QAbstractAxis::gridVisible
103 The visibility of the grid lines.
103 The visibility of the grid lines.
104 */
104 */
105 /*!
105 /*!
106 \qmlproperty bool AbstractAxis::gridVisible
106 \qmlproperty bool AbstractAxis::gridVisible
107 The visibility of the grid lines.
107 The visibility of the grid lines.
108 */
108 */
109
109
110 /*!
110 /*!
111 \property QAbstractAxis::color
111 \property QAbstractAxis::color
112 The color of the axis and ticks.
112 The color of the axis and ticks.
113 */
113 */
114 /*!
114 /*!
115 \qmlproperty color AbstractAxis::color
115 \qmlproperty color AbstractAxis::color
116 The color of the axis and ticks.
116 The color of the axis and ticks.
117 */
117 */
118
118
119 /*!
119 /*!
120 \property QAbstractAxis::labelsFont
120 \property QAbstractAxis::labelsFont
121 The font of the axis labels.
121 The font of the axis labels.
122 */
122 */
123
123
124 /*!
124 /*!
125 \qmlproperty Font AbstractAxis::labelsFont
125 \qmlproperty Font AbstractAxis::labelsFont
126 The font of the axis labels.
126 The font of the axis labels.
127
127
128 See the \l {Font} {QML Font Element} for detailed documentation.
128 See the \l {Font} {QML Font Element} for detailed documentation.
129 */
129 */
130
130
131 /*!
131 /*!
132 \property QAbstractAxis::labelsColor
132 \property QAbstractAxis::labelsColor
133 The color of the axis labels.
133 The color of the axis labels.
134 */
134 */
135 /*!
135 /*!
136 \qmlproperty color AbstractAxis::labelsColor
136 \qmlproperty color AbstractAxis::labelsColor
137 The color of the axis labels.
137 The color of the axis labels.
138 */
138 */
139
139
140 /*!
140 /*!
141 \property QAbstractAxis::labelsAngle
141 \property QAbstractAxis::labelsAngle
142 The angle of the axis labels in degrees.
142 The angle of the axis labels in degrees.
143 */
143 */
144 /*!
144 /*!
145 \qmlproperty int AbstractAxis::labelsAngle
145 \qmlproperty int AbstractAxis::labelsAngle
146 The angle of the axis labels in degrees.
146 The angle of the axis labels in degrees.
147 */
147 */
148
148
149 /*!
149 /*!
150 \property QAbstractAxis::shadesVisible
150 \property QAbstractAxis::shadesVisible
151 The visibility of the axis shades.
151 The visibility of the axis shades.
152 */
152 */
153 /*!
153 /*!
154 \qmlproperty bool AbstractAxis::shadesVisible
154 \qmlproperty bool AbstractAxis::shadesVisible
155 The visibility of the axis shades.
155 The visibility of the axis shades.
156 */
156 */
157
157
158 /*!
158 /*!
159 \property QAbstractAxis::shadesColor
159 \property QAbstractAxis::shadesColor
160 The fill (brush) color of the axis shades.
160 The fill (brush) color of the axis shades.
161 */
161 */
162 /*!
162 /*!
163 \qmlproperty color AbstractAxis::shadesColor
163 \qmlproperty color AbstractAxis::shadesColor
164 The fill (brush) color of the axis shades.
164 The fill (brush) color of the axis shades.
165 */
165 */
166
166
167 /*!
167 /*!
168 \property QAbstractAxis::shadesBorderColor
168 \property QAbstractAxis::shadesBorderColor
169 The border (pen) color of the axis shades.
169 The border (pen) color of the axis shades.
170 */
170 */
171 /*!
171 /*!
172 \qmlproperty color AbstractAxis::shadesBorderColor
172 \qmlproperty color AbstractAxis::shadesBorderColor
173 The border (pen) color of the axis shades.
173 The border (pen) color of the axis shades.
174 */
174 */
175
175
176 /*!
176 /*!
177 \fn void QAbstractAxis::visibleChanged(bool visible)
177 \fn void QAbstractAxis::visibleChanged(bool visible)
178 Visibility of the axis has changed to \a visible.
178 Visibility of the axis has changed to \a visible.
179 */
179 */
180 /*!
180 /*!
181 \qmlsignal AbstractAxis::onVisibleChanged(bool visible)
181 \qmlsignal AbstractAxis::onVisibleChanged(bool visible)
182 Visibility of the axis has changed to \a visible.
182 Visibility of the axis has changed to \a visible.
183 */
183 */
184
184
185 /*!
185 /*!
186 \fn void QAbstractAxis::lineVisibleChanged(bool visible)
186 \fn void QAbstractAxis::lineVisibleChanged(bool visible)
187 Visibility of the axis line has changed to \a visible.
187 Visibility of the axis line has changed to \a visible.
188 */
188 */
189 /*!
189 /*!
190 \qmlsignal AbstractAxis::onLineVisibleChanged(bool visible)
190 \qmlsignal AbstractAxis::onLineVisibleChanged(bool visible)
191 Visibility of the axis line has changed to \a visible.
191 Visibility of the axis line has changed to \a visible.
192 */
192 */
193
193
194 /*!
194 /*!
195 \fn void QAbstractAxis::labelsVisibleChanged(bool visible)
195 \fn void QAbstractAxis::labelsVisibleChanged(bool visible)
196 Visibility of the labels of the axis has changed to \a visible.
196 Visibility of the labels of the axis has changed to \a visible.
197 */
197 */
198 /*!
198 /*!
199 \qmlsignal AbstractAxis::onLabelsVisibleChanged(bool visible)
199 \qmlsignal AbstractAxis::onLabelsVisibleChanged(bool visible)
200 Visibility of the labels of the axis has changed to \a visible.
200 Visibility of the labels of the axis has changed to \a visible.
201 */
201 */
202
202
203 /*!
203 /*!
204 \fn void QAbstractAxis::gridVisibleChanged(bool visible)
204 \fn void QAbstractAxis::gridVisibleChanged(bool visible)
205 Visibility of the grid lines of the axis has changed to \a visible.
205 Visibility of the grid lines of the axis has changed to \a visible.
206 */
206 */
207 /*!
207 /*!
208 \qmlsignal AbstractAxis::onGridVisibleChanged(bool visible)
208 \qmlsignal AbstractAxis::onGridVisibleChanged(bool visible)
209 Visibility of the grid lines of the axis has changed to \a visible.
209 Visibility of the grid lines of the axis has changed to \a visible.
210 */
210 */
211
211
212 /*!
212 /*!
213 \fn void QAbstractAxis::colorChanged(QColor color)
213 \fn void QAbstractAxis::colorChanged(QColor color)
214 Emitted if the \a color of the axis is changed.
214 Emitted if the \a color of the axis is changed.
215 */
215 */
216 /*!
216 /*!
217 \qmlsignal AbstractAxis::onColorChanged(QColor color)
217 \qmlsignal AbstractAxis::onColorChanged(QColor color)
218 Emitted if the \a color of the axis is changed.
218 Emitted if the \a color of the axis is changed.
219 */
219 */
220
220
221 /*!
221 /*!
222 \fn void QAbstractAxis::labelsColorChanged(QColor color)
222 \fn void QAbstractAxis::labelsColorChanged(QColor color)
223 Emitted if the \a color of the axis labels is changed.
223 Emitted if the \a color of the axis labels is changed.
224 */
224 */
225 /*!
225 /*!
226 \qmlsignal AbstractAxis::onLabelsColorChanged(QColor color)
226 \qmlsignal AbstractAxis::onLabelsColorChanged(QColor color)
227 Emitted if the \a color of the axis labels is changed.
227 Emitted if the \a color of the axis labels is changed.
228 */
228 */
229
229
230 /*!
230 /*!
231 \fn void QAbstractAxis::shadesVisibleChanged(bool)
231 \fn void QAbstractAxis::shadesVisibleChanged(bool)
232 Emitted if the visibility of the axis shades is changed to \a visible.
232 Emitted if the visibility of the axis shades is changed to \a visible.
233 */
233 */
234 /*!
234 /*!
235 \qmlsignal AbstractAxis::onShadesVisibleChanged(bool visible)
235 \qmlsignal AbstractAxis::onShadesVisibleChanged(bool visible)
236 Emitted if the visibility of the axis shades is changed to \a visible.
236 Emitted if the visibility of the axis shades is changed to \a visible.
237 */
237 */
238
238
239 /*!
239 /*!
240 \fn void QAbstractAxis::shadesColorChanged(QColor color)
240 \fn void QAbstractAxis::shadesColorChanged(QColor color)
241 Emitted if the \a color of the axis shades is changed.
241 Emitted if the \a color of the axis shades is changed.
242 */
242 */
243 /*!
243 /*!
244 \qmlsignal AbstractAxis::onShadesColorChanged(QColor color)
244 \qmlsignal AbstractAxis::onShadesColorChanged(QColor color)
245 Emitted if the \a color of the axis shades is changed.
245 Emitted if the \a color of the axis shades is changed.
246 */
246 */
247
247
248 /*!
248 /*!
249 \fn void QAbstractAxis::shadesBorderColorChanged(QColor)
249 \fn void QAbstractAxis::shadesBorderColorChanged(QColor)
250 Emitted if the border \a color of the axis shades is changed.
250 Emitted if the border \a color of the axis shades is changed.
251 */
251 */
252 /*!
252 /*!
253 \qmlsignal AbstractAxis::onBorderColorChanged(QColor color)
253 \qmlsignal AbstractAxis::onBorderColorChanged(QColor color)
254 Emitted if the border \a color of the axis shades is changed.
254 Emitted if the border \a color of the axis shades is changed.
255 */
255 */
256
256
257 /*!
257 /*!
258 \internal
258 \internal
259 Constructs new axis object which is a child of \a parent. Ownership is taken by
259 Constructs new axis object which is a child of \a parent. Ownership is taken by
260 QChart when axis added.
260 QChart when axis added.
261 */
261 */
262
262
263 QAbstractAxis::QAbstractAxis(QAbstractAxisPrivate &d, QObject *parent) :
263 QAbstractAxis::QAbstractAxis(QAbstractAxisPrivate &d, QObject *parent) :
264 QObject(parent),
264 QObject(parent),
265 d_ptr(&d)
265 d_ptr(&d)
266 {
266 {
267 }
267 }
268
268
269 /*!
269 /*!
270 Destructor of the axis object. When axis is added to chart, chart object takes ownership.
270 Destructor of the axis object. When axis is added to chart, chart object takes ownership.
271 */
271 */
272
272
273 QAbstractAxis::~QAbstractAxis()
273 QAbstractAxis::~QAbstractAxis()
274 {
274 {
275 if(d_ptr->m_dataset) qFatal("Still binded axis detected !");
275 if(d_ptr->m_dataset) qFatal("Still binded axis detected !");
276 }
276 }
277
277
278 /*!
278 /*!
279 Sets \a pen used to draw axis line and ticks.
279 Sets \a pen used to draw axis line and ticks.
280 */
280 */
281 void QAbstractAxis::setLinePen(const QPen &pen)
281 void QAbstractAxis::setLinePen(const QPen &pen)
282 {
282 {
283 if (d_ptr->m_axisPen!=pen) {
283 if (d_ptr->m_axisPen!=pen) {
284 d_ptr->m_axisPen = pen;
284 d_ptr->m_axisPen = pen;
285 d_ptr->emitUpdated();
285 d_ptr->emitUpdated();
286 }
286 }
287 }
287 }
288
288
289 /*!
289 /*!
290 Returns pen used to draw axis and ticks.
290 Returns pen used to draw axis and ticks.
291 */
291 */
292 QPen QAbstractAxis::linePen() const
292 QPen QAbstractAxis::linePen() const
293 {
293 {
294 return d_ptr->m_axisPen;
294 return d_ptr->m_axisPen;
295 }
295 }
296
296
297 void QAbstractAxis::setLinePenColor(QColor color)
297 void QAbstractAxis::setLinePenColor(QColor color)
298 {
298 {
299 QPen p = d_ptr->m_axisPen;
299 QPen p = d_ptr->m_axisPen;
300 if (p.color() != color) {
300 if (p.color() != color) {
301 p.setColor(color);
301 p.setColor(color);
302 setLinePen(p);
302 setLinePen(p);
303 emit colorChanged(color);
303 emit colorChanged(color);
304 }
304 }
305 }
305 }
306
306
307 QColor QAbstractAxis::linePenColor() const
307 QColor QAbstractAxis::linePenColor() const
308 {
308 {
309 return d_ptr->m_axisPen.color();
309 return d_ptr->m_axisPen.color();
310 }
310 }
311
311
312 /*!
312 /*!
313 Sets if axis and ticks are \a visible.
313 Sets if axis and ticks are \a visible.
314 */
314 */
315 void QAbstractAxis::setLineVisible(bool visible)
315 void QAbstractAxis::setLineVisible(bool visible)
316 {
316 {
317 if (d_ptr->m_arrowVisible != visible) {
317 if (d_ptr->m_arrowVisible != visible) {
318 d_ptr->m_arrowVisible = visible;
318 d_ptr->m_arrowVisible = visible;
319 d_ptr->emitUpdated();
319 d_ptr->emitUpdated();
320 emit lineVisibleChanged(visible);
320 emit lineVisibleChanged(visible);
321 }
321 }
322 }
322 }
323
323
324 bool QAbstractAxis::isLineVisible() const
324 bool QAbstractAxis::isLineVisible() const
325 {
325 {
326 return d_ptr->m_arrowVisible;
326 return d_ptr->m_arrowVisible;
327 }
327 }
328
328
329 void QAbstractAxis::setGridLineVisible(bool visible)
329 void QAbstractAxis::setGridLineVisible(bool visible)
330 {
330 {
331 if (d_ptr->m_gridLineVisible != visible) {
331 if (d_ptr->m_gridLineVisible != visible) {
332 d_ptr->m_gridLineVisible = visible;
332 d_ptr->m_gridLineVisible = visible;
333 d_ptr->emitUpdated();
333 d_ptr->emitUpdated();
334 emit gridVisibleChanged(visible);
334 emit gridVisibleChanged(visible);
335 }
335 }
336 }
336 }
337
337
338 bool QAbstractAxis::isGridLineVisible() const
338 bool QAbstractAxis::isGridLineVisible() const
339 {
339 {
340 return d_ptr->m_gridLineVisible;
340 return d_ptr->m_gridLineVisible;
341 }
341 }
342
342
343 /*!
343 /*!
344 Sets \a pen used to draw grid line.
344 Sets \a pen used to draw grid line.
345 */
345 */
346 void QAbstractAxis::setGridLinePen(const QPen &pen)
346 void QAbstractAxis::setGridLinePen(const QPen &pen)
347 {
347 {
348 if (d_ptr->m_gridLinePen != pen) {
348 if (d_ptr->m_gridLinePen != pen) {
349 d_ptr->m_gridLinePen = pen;
349 d_ptr->m_gridLinePen = pen;
350 d_ptr->emitUpdated();
350 d_ptr->emitUpdated();
351 }
351 }
352 }
352 }
353
353
354 /*!
354 /*!
355 Returns pen used to draw grid.
355 Returns pen used to draw grid.
356 */
356 */
357 QPen QAbstractAxis::gridLinePen() const
357 QPen QAbstractAxis::gridLinePen() const
358 {
358 {
359 return d_ptr->m_gridLinePen;
359 return d_ptr->m_gridLinePen;
360 }
360 }
361
361
362 void QAbstractAxis::setLabelsVisible(bool visible)
362 void QAbstractAxis::setLabelsVisible(bool visible)
363 {
363 {
364 if (d_ptr->m_labelsVisible != visible) {
364 if (d_ptr->m_labelsVisible != visible) {
365 d_ptr->m_labelsVisible = visible;
365 d_ptr->m_labelsVisible = visible;
366 d_ptr->emitUpdated();
366 d_ptr->emitUpdated();
367 emit labelsVisibleChanged(visible);
367 emit labelsVisibleChanged(visible);
368 }
368 }
369 }
369 }
370
370
371 bool QAbstractAxis::labelsVisible() const
371 bool QAbstractAxis::labelsVisible() const
372 {
372 {
373 return d_ptr->m_labelsVisible;
373 return d_ptr->m_labelsVisible;
374 }
374 }
375
375
376 /*!
376 /*!
377 Sets \a pen used to draw labels.
377 Sets \a pen used to draw labels.
378 */
378 */
379 void QAbstractAxis::setLabelsPen(const QPen &pen)
379 void QAbstractAxis::setLabelsPen(const QPen &pen)
380 {
380 {
381 if (d_ptr->m_labelsPen != pen) {
381 if (d_ptr->m_labelsPen != pen) {
382 d_ptr->m_labelsPen = pen;
382 d_ptr->m_labelsPen = pen;
383 d_ptr->emitUpdated();
383 d_ptr->emitUpdated();
384 }
384 }
385 }
385 }
386
386
387 /*!
387 /*!
388 Returns the pen used to labels.
388 Returns the pen used to labels.
389 */
389 */
390 QPen QAbstractAxis::labelsPen() const
390 QPen QAbstractAxis::labelsPen() const
391 {
391 {
392 return d_ptr->m_labelsPen;
392 return d_ptr->m_labelsPen;
393 }
393 }
394
394
395 /*!
395 /*!
396 Sets \a brush used to draw labels.
396 Sets \a brush used to draw labels.
397 */
397 */
398 void QAbstractAxis::setLabelsBrush(const QBrush &brush)
398 void QAbstractAxis::setLabelsBrush(const QBrush &brush)
399 {
399 {
400 if (d_ptr->m_labelsBrush != brush) {
400 if (d_ptr->m_labelsBrush != brush) {
401 d_ptr->m_labelsBrush = brush;
401 d_ptr->m_labelsBrush = brush;
402 d_ptr->emitUpdated();
402 d_ptr->emitUpdated();
403 }
403 }
404 }
404 }
405
405
406 /*!
406 /*!
407 Returns brush used to draw labels.
407 Returns brush used to draw labels.
408 */
408 */
409 QBrush QAbstractAxis::labelsBrush() const
409 QBrush QAbstractAxis::labelsBrush() const
410 {
410 {
411 return d_ptr->m_labelsBrush;
411 return d_ptr->m_labelsBrush;
412 }
412 }
413
413
414 /*!
414 /*!
415 Sets \a font used to draw labels.
415 Sets \a font used to draw labels.
416 */
416 */
417 void QAbstractAxis::setLabelsFont(const QFont &font)
417 void QAbstractAxis::setLabelsFont(const QFont &font)
418 {
418 {
419 if (d_ptr->m_labelsFont != font) {
419 if (d_ptr->m_labelsFont != font) {
420 d_ptr->m_labelsFont = font;
420 d_ptr->m_labelsFont = font;
421 d_ptr->emitUpdated();
421 d_ptr->emitUpdated();
422 }
422 }
423 }
423 }
424
424
425 /*!
425 /*!
426 Returns font used to draw labels.
426 Returns font used to draw labels.
427 */
427 */
428 QFont QAbstractAxis::labelsFont() const
428 QFont QAbstractAxis::labelsFont() const
429 {
429 {
430 return d_ptr->m_labelsFont;
430 return d_ptr->m_labelsFont;
431 }
431 }
432
432
433 void QAbstractAxis::setLabelsAngle(int angle)
433 void QAbstractAxis::setLabelsAngle(int angle)
434 {
434 {
435 if (d_ptr->m_labelsAngle != angle) {
435 if (d_ptr->m_labelsAngle != angle) {
436 d_ptr->m_labelsAngle = angle;
436 d_ptr->m_labelsAngle = angle;
437 d_ptr->emitUpdated();
437 d_ptr->emitUpdated();
438 }
438 }
439 }
439 }
440
440
441 int QAbstractAxis::labelsAngle() const
441 int QAbstractAxis::labelsAngle() const
442 {
442 {
443 return d_ptr->m_labelsAngle;
443 return d_ptr->m_labelsAngle;
444 }
444 }
445
445
446 void QAbstractAxis::setLabelsColor(QColor color)
446 void QAbstractAxis::setLabelsColor(QColor color)
447 {
447 {
448 QBrush b = d_ptr->m_labelsBrush;
448 QBrush b = d_ptr->m_labelsBrush;
449 if (b.color() != color) {
449 if (b.color() != color) {
450 b.setColor(color);
450 b.setColor(color);
451 setLabelsBrush(b);
451 setLabelsBrush(b);
452 emit labelsColorChanged(color);
452 emit labelsColorChanged(color);
453 }
453 }
454 }
454 }
455
455
456 QColor QAbstractAxis::labelsColor() const
456 QColor QAbstractAxis::labelsColor() const
457 {
457 {
458 return d_ptr->m_labelsBrush.color();
458 return d_ptr->m_labelsBrush.color();
459 }
459 }
460
460
461 void QAbstractAxis::setTitleVisible(bool visible)
461 void QAbstractAxis::setTitleVisible(bool visible)
462 {
462 {
463 if (d_ptr->m_titleVisible != visible) {
463 if (d_ptr->m_titleVisible != visible) {
464 d_ptr->m_titleVisible = visible;
464 d_ptr->m_titleVisible = visible;
465 d_ptr->emitUpdated();
465 d_ptr->emitUpdated();
466 }
466 }
467 }
467 }
468
468
469 bool QAbstractAxis::titleVisible() const
469 bool QAbstractAxis::titleVisible() const
470 {
470 {
471 return d_ptr->m_titleVisible;
471 return d_ptr->m_titleVisible;
472 }
472 }
473
473
474 /*!
474 /*!
475 Sets \a pen used to draw title.
475 Sets \a pen used to draw title.
476 */
476 */
477 void QAbstractAxis::setTitlePen(const QPen &pen)
477 void QAbstractAxis::setTitlePen(const QPen &pen)
478 {
478 {
479 if (d_ptr->m_titlePen != pen) {
479 if (d_ptr->m_titlePen != pen) {
480 d_ptr->m_titlePen = pen;
480 d_ptr->m_titlePen = pen;
481 d_ptr->emitUpdated();
481 d_ptr->emitUpdated();
482 }
482 }
483 }
483 }
484
484
485 /*!
485 /*!
486 Returns the pen used to title.
486 Returns the pen used to title.
487 */
487 */
488 QPen QAbstractAxis::titlePen() const
488 QPen QAbstractAxis::titlePen() const
489 {
489 {
490 return d_ptr->m_titlePen;
490 return d_ptr->m_titlePen;
491 }
491 }
492
492
493 /*!
493 /*!
494 Sets \a brush used to draw title.
494 Sets \a brush used to draw title.
495 */
495 */
496 void QAbstractAxis::setTitleBrush(const QBrush &brush)
496 void QAbstractAxis::setTitleBrush(const QBrush &brush)
497 {
497 {
498 if (d_ptr->m_titleBrush != brush) {
498 if (d_ptr->m_titleBrush != brush) {
499 d_ptr->m_titleBrush = brush;
499 d_ptr->m_titleBrush = brush;
500 d_ptr->emitUpdated();
500 d_ptr->emitUpdated();
501 }
501 }
502 }
502 }
503
503
504 /*!
504 /*!
505 Returns brush used to draw title.
505 Returns brush used to draw title.
506 */
506 */
507 QBrush QAbstractAxis::titleBrush() const
507 QBrush QAbstractAxis::titleBrush() const
508 {
508 {
509 return d_ptr->m_titleBrush;
509 return d_ptr->m_titleBrush;
510 }
510 }
511
511
512 /*!
512 /*!
513 Sets \a font used to draw title.
513 Sets \a font used to draw title.
514 */
514 */
515 void QAbstractAxis::setTitleFont(const QFont &font)
515 void QAbstractAxis::setTitleFont(const QFont &font)
516 {
516 {
517 if (d_ptr->m_titleFont != font) {
517 if (d_ptr->m_titleFont != font) {
518 d_ptr->m_titleFont = font;
518 d_ptr->m_titleFont = font;
519 d_ptr->emitUpdated();
519 d_ptr->emitUpdated();
520 }
520 }
521 }
521 }
522
522
523 /*!
523 /*!
524 Returns font used to draw title.
524 Returns font used to draw title.
525 */
525 */
526 QFont QAbstractAxis::titleFont() const
526 QFont QAbstractAxis::titleFont() const
527 {
527 {
528 return d_ptr->m_titleFont;
528 return d_ptr->m_titleFont;
529 }
529 }
530
530
531 void QAbstractAxis::setTitle(const QString& title)
531 void QAbstractAxis::setTitle(const QString& title)
532 {
532 {
533 if (d_ptr->m_title != title) {
533 if (d_ptr->m_title != title) {
534 d_ptr->m_title = title;
534 d_ptr->m_title = title;
535 d_ptr->emitUpdated();
535 d_ptr->emitUpdated();
536 }
536 }
537 }
537 }
538
538
539 QString QAbstractAxis::title() const
539 QString QAbstractAxis::title() const
540 {
540 {
541 return d_ptr->m_title;
541 return d_ptr->m_title;
542 }
542 }
543
543
544
544
545 void QAbstractAxis::setShadesVisible(bool visible)
545 void QAbstractAxis::setShadesVisible(bool visible)
546 {
546 {
547 if (d_ptr->m_shadesVisible != visible) {
547 if (d_ptr->m_shadesVisible != visible) {
548 d_ptr->m_shadesVisible = visible;
548 d_ptr->m_shadesVisible = visible;
549 d_ptr->emitUpdated();
549 d_ptr->emitUpdated();
550 emit shadesVisibleChanged(visible);
550 emit shadesVisibleChanged(visible);
551 }
551 }
552 }
552 }
553
553
554 bool QAbstractAxis::shadesVisible() const
554 bool QAbstractAxis::shadesVisible() const
555 {
555 {
556 return d_ptr->m_shadesVisible;
556 return d_ptr->m_shadesVisible;
557 }
557 }
558
558
559 /*!
559 /*!
560 Sets \a pen used to draw shades.
560 Sets \a pen used to draw shades.
561 */
561 */
562 void QAbstractAxis::setShadesPen(const QPen &pen)
562 void QAbstractAxis::setShadesPen(const QPen &pen)
563 {
563 {
564 if (d_ptr->m_shadesPen != pen) {
564 if (d_ptr->m_shadesPen != pen) {
565 d_ptr->m_shadesPen = pen;
565 d_ptr->m_shadesPen = pen;
566 d_ptr->emitUpdated();
566 d_ptr->emitUpdated();
567 }
567 }
568 }
568 }
569
569
570 /*!
570 /*!
571 Returns pen used to draw shades.
571 Returns pen used to draw shades.
572 */
572 */
573 QPen QAbstractAxis::shadesPen() const
573 QPen QAbstractAxis::shadesPen() const
574 {
574 {
575 return d_ptr->m_shadesPen;
575 return d_ptr->m_shadesPen;
576 }
576 }
577
577
578 /*!
578 /*!
579 Sets \a brush used to draw shades.
579 Sets \a brush used to draw shades.
580 */
580 */
581 void QAbstractAxis::setShadesBrush(const QBrush &brush)
581 void QAbstractAxis::setShadesBrush(const QBrush &brush)
582 {
582 {
583 if (d_ptr->m_shadesBrush != brush) {
583 if (d_ptr->m_shadesBrush != brush) {
584 d_ptr->m_shadesBrush = brush;
584 d_ptr->m_shadesBrush = brush;
585 d_ptr->emitUpdated();
585 d_ptr->emitUpdated();
586 emit shadesColorChanged(brush.color());
586 emit shadesColorChanged(brush.color());
587 }
587 }
588 }
588 }
589
589
590 /*!
590 /*!
591 Returns brush used to draw shades.
591 Returns brush used to draw shades.
592 */
592 */
593 QBrush QAbstractAxis::shadesBrush() const
593 QBrush QAbstractAxis::shadesBrush() const
594 {
594 {
595 return d_ptr->m_shadesBrush;
595 return d_ptr->m_shadesBrush;
596 }
596 }
597
597
598 void QAbstractAxis::setShadesColor(QColor color)
598 void QAbstractAxis::setShadesColor(QColor color)
599 {
599 {
600 QBrush b = d_ptr->m_shadesBrush;
600 QBrush b = d_ptr->m_shadesBrush;
601 b.setColor(color);
601 b.setColor(color);
602 setShadesBrush(b);
602 setShadesBrush(b);
603 }
603 }
604
604
605 QColor QAbstractAxis::shadesColor() const
605 QColor QAbstractAxis::shadesColor() const
606 {
606 {
607 return d_ptr->m_shadesBrush.color();
607 return d_ptr->m_shadesBrush.color();
608 }
608 }
609
609
610 void QAbstractAxis::setShadesBorderColor(QColor color)
610 void QAbstractAxis::setShadesBorderColor(QColor color)
611 {
611 {
612 QPen p = d_ptr->m_shadesPen;
612 QPen p = d_ptr->m_shadesPen;
613 p.setColor(color);
613 p.setColor(color);
614 setShadesPen(p);
614 setShadesPen(p);
615 }
615 }
616
616
617 QColor QAbstractAxis::shadesBorderColor() const
617 QColor QAbstractAxis::shadesBorderColor() const
618 {
618 {
619 return d_ptr->m_shadesPen.color();
619 return d_ptr->m_shadesPen.color();
620 }
620 }
621
621
622
622
623 bool QAbstractAxis::isVisible() const
623 bool QAbstractAxis::isVisible() const
624 {
624 {
625 return d_ptr->m_visible;
625 return d_ptr->m_visible;
626 }
626 }
627
627
628 /*!
628 /*!
629 Sets axis, shades, labels and grid lines to be visible.
629 Sets axis, shades, labels and grid lines to be visible.
630 */
630 */
631 void QAbstractAxis::setVisible(bool visible)
631 void QAbstractAxis::setVisible(bool visible)
632 {
632 {
633 if(d_ptr->m_visible!=visible){
633 if(d_ptr->m_visible!=visible){
634 d_ptr->m_visible=visible;
634 d_ptr->m_visible=visible;
635 d_ptr->emitUpdated();
635 d_ptr->emitUpdated();
636 emit visibleChanged(visible);
636 emit visibleChanged(visible);
637 }
637 }
638 }
638 }
639
639
640
640
641 /*!
641 /*!
642 Sets axis, shades, labels and grid lines to be visible.
642 Sets axis, shades, labels and grid lines to be visible.
643 */
643 */
644 void QAbstractAxis::show()
644 void QAbstractAxis::show()
645 {
645 {
646 setVisible(true);
646 setVisible(true);
647 }
647 }
648
648
649 /*!
649 /*!
650 Sets axis, shades, labels and grid lines to not be visible.
650 Sets axis, shades, labels and grid lines to not be visible.
651 */
651 */
652 void QAbstractAxis::hide()
652 void QAbstractAxis::hide()
653 {
653 {
654 setVisible(false);
654 setVisible(false);
655 }
655 }
656
656
657 /*!
657 /*!
658 Sets the minimum value shown on the axis.
658 Sets the minimum value shown on the axis.
659 Depending on the actual axis type the \a min parameter is converted to appropriate type.
659 Depending on the actual axis type the \a min parameter is converted to appropriate type.
660 If the conversion is impossible then the function call does nothing
660 If the conversion is impossible then the function call does nothing
661 */
661 */
662 void QAbstractAxis::setMin(const QVariant &min)
662 void QAbstractAxis::setMin(const QVariant &min)
663 {
663 {
664 d_ptr->setMin(min);
664 d_ptr->setMin(min);
665 }
665 }
666
666
667 /*!
667 /*!
668 Sets the maximum value shown on the axis.
668 Sets the maximum value shown on the axis.
669 Depending on the actual axis type the \a max parameter is converted to appropriate type.
669 Depending on the actual axis type the \a max parameter is converted to appropriate type.
670 If the conversion is impossible then the function call does nothing
670 If the conversion is impossible then the function call does nothing
671 */
671 */
672 void QAbstractAxis::setMax(const QVariant &max)
672 void QAbstractAxis::setMax(const QVariant &max)
673 {
673 {
674 d_ptr->setMax(max);
674 d_ptr->setMax(max);
675 }
675 }
676
676
677 /*!
677 /*!
678 Sets the range shown on the axis.
678 Sets the range shown on the axis.
679 Depending on the actual axis type the \a min and \a max parameters are converted to appropriate types.
679 Depending on the actual axis type the \a min and \a max parameters are converted to appropriate types.
680 If the conversion is impossible then the function call does nothing.
680 If the conversion is impossible then the function call does nothing.
681 */
681 */
682 void QAbstractAxis::setRange(const QVariant &min, const QVariant &max)
682 void QAbstractAxis::setRange(const QVariant &min, const QVariant &max)
683 {
683 {
684 d_ptr->setRange(min,max);
684 d_ptr->setRange(min,max);
685 }
685 }
686
686
687
687
688 /*!
688 /*!
689 Returns the orientation in which the axis is being used (Vertical or Horizontal)
689 Returns the orientation in which the axis is being used (Vertical or Horizontal)
690 */
690 */
691 Qt::Orientation QAbstractAxis::orientation()
691 Qt::Orientation QAbstractAxis::orientation()
692 {
692 {
693 return d_ptr->m_orientation;
693 return d_ptr->m_orientation;
694 }
694 }
695
695
696 bool QAbstractAxis::alternativePlacement() const
697 {
698 return d_ptr->m_alternativePlacement;
699 }
700
701 void QAbstractAxis::setAlternativePlacement(bool placement)
702 {
703 d_ptr->m_alternativePlacement = placement;
704 }
705
696 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
706 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
697
707
698 QAbstractAxisPrivate::QAbstractAxisPrivate(QAbstractAxis* q):
708 QAbstractAxisPrivate::QAbstractAxisPrivate(QAbstractAxis* q):
699 q_ptr(q),
709 q_ptr(q),
700 m_orientation(Qt::Orientation(0)),
710 m_orientation(Qt::Orientation(0)),
701 m_dataset(0),
711 m_dataset(0),
702 m_visible(false),
712 m_visible(true),
703 m_arrowVisible(true),
713 m_arrowVisible(true),
704 m_gridLineVisible(true),
714 m_gridLineVisible(true),
705 m_labelsVisible(true),
715 m_labelsVisible(true),
706 m_labelsAngle(0),
716 m_labelsAngle(0),
707 m_shadesVisible(false),
717 m_shadesVisible(false),
708 m_shadesBrush(Qt::SolidPattern),
718 m_shadesBrush(Qt::SolidPattern),
709 m_shadesOpacity(1.0),
719 m_shadesOpacity(1.0),
710 m_dirty(false)
720 m_dirty(false),
721 m_alternativePlacement(false)
711 {
722 {
712
723
713 }
724 }
714
725
715 QAbstractAxisPrivate::~QAbstractAxisPrivate()
726 QAbstractAxisPrivate::~QAbstractAxisPrivate()
716 {
727 {
717
728
718 }
729 }
719
730
720 void QAbstractAxisPrivate::emitUpdated()
731 void QAbstractAxisPrivate::emitUpdated()
721 {
732 {
722 if(!m_dirty){
733 if(!m_dirty){
723 m_dirty=true;
734 m_dirty=true;
724 emit updated();
735 emit updated();
725 }
736 }
726 }
737 }
727
738
728 void QAbstractAxisPrivate::setDirty(bool dirty)
739 void QAbstractAxisPrivate::setDirty(bool dirty)
729 {
740 {
730 m_dirty=dirty;
741 m_dirty=dirty;
731 }
742 }
732
743
733 void QAbstractAxisPrivate::setOrientation(Qt::Orientation orientation)
744 void QAbstractAxisPrivate::setOrientation(Qt::Orientation orientation)
734 {
745 {
735 m_orientation=orientation;
746 m_orientation=orientation;
736 }
747 }
737
748
738
749
739 #include "moc_qabstractaxis.cpp"
750 #include "moc_qabstractaxis.cpp"
740 #include "moc_qabstractaxis_p.cpp"
751 #include "moc_qabstractaxis_p.cpp"
741
752
742 QTCOMMERCIALCHART_END_NAMESPACE
753 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,157 +1,159
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 QABSTRACTAXIS_H
21 #ifndef QABSTRACTAXIS_H
22 #define QABSTRACTAXIS_H
22 #define QABSTRACTAXIS_H
23
23
24 #include <qchartglobal.h>
24 #include <qchartglobal.h>
25 #include <QPen>
25 #include <QPen>
26 #include <QFont>
26 #include <QFont>
27 #include <QVariant>
27 #include <QVariant>
28
28
29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30
30
31 class QAbstractAxisPrivate;
31 class QAbstractAxisPrivate;
32
32
33 class QTCOMMERCIALCHART_EXPORT QAbstractAxis : public QObject
33 class QTCOMMERCIALCHART_EXPORT QAbstractAxis : public QObject
34 {
34 {
35 Q_OBJECT
35 Q_OBJECT
36 Q_PROPERTY(bool visible READ isVisible WRITE setVisible NOTIFY visibleChanged)
36 Q_PROPERTY(bool visible READ isVisible WRITE setVisible NOTIFY visibleChanged)
37 Q_PROPERTY(bool lineVisible READ isLineVisible WRITE setLineVisible NOTIFY lineVisibleChanged)
37 Q_PROPERTY(bool lineVisible READ isLineVisible WRITE setLineVisible NOTIFY lineVisibleChanged)
38 Q_PROPERTY(QColor color READ linePenColor WRITE setLinePenColor NOTIFY colorChanged)
38 Q_PROPERTY(QColor color READ linePenColor WRITE setLinePenColor NOTIFY colorChanged)
39 Q_PROPERTY(bool labelsVisible READ labelsVisible WRITE setLabelsVisible NOTIFY labelsVisibleChanged)
39 Q_PROPERTY(bool labelsVisible READ labelsVisible WRITE setLabelsVisible NOTIFY labelsVisibleChanged)
40 Q_PROPERTY(int labelsAngle READ labelsAngle WRITE setLabelsAngle)
40 Q_PROPERTY(int labelsAngle READ labelsAngle WRITE setLabelsAngle)
41 Q_PROPERTY(QFont labelsFont READ labelsFont WRITE setLabelsFont)
41 Q_PROPERTY(QFont labelsFont READ labelsFont WRITE setLabelsFont)
42 Q_PROPERTY(QColor labelsColor READ labelsColor WRITE setLabelsColor NOTIFY labelsColorChanged)
42 Q_PROPERTY(QColor labelsColor READ labelsColor WRITE setLabelsColor NOTIFY labelsColorChanged)
43 Q_PROPERTY(bool gridVisible READ isGridLineVisible WRITE setGridLineVisible NOTIFY gridVisibleChanged)
43 Q_PROPERTY(bool gridVisible READ isGridLineVisible WRITE setGridLineVisible NOTIFY gridVisibleChanged)
44 Q_PROPERTY(bool shadesVisible READ shadesVisible WRITE setShadesVisible NOTIFY shadesVisibleChanged)
44 Q_PROPERTY(bool shadesVisible READ shadesVisible WRITE setShadesVisible NOTIFY shadesVisibleChanged)
45 Q_PROPERTY(QColor shadesColor READ shadesColor WRITE setShadesColor NOTIFY shadesColorChanged)
45 Q_PROPERTY(QColor shadesColor READ shadesColor WRITE setShadesColor NOTIFY shadesColorChanged)
46 Q_PROPERTY(QColor shadesBorderColor READ shadesBorderColor WRITE setShadesBorderColor NOTIFY shadesBorderColorChanged)
46 Q_PROPERTY(QColor shadesBorderColor READ shadesBorderColor WRITE setShadesBorderColor NOTIFY shadesBorderColorChanged)
47
47
48 public:
48 public:
49
49
50 enum AxisType {
50 enum AxisType {
51 AxisTypeNoAxis = 0x0,
51 AxisTypeNoAxis = 0x0,
52 AxisTypeValue = 0x1,
52 AxisTypeValue = 0x1,
53 AxisTypeBarCategory = 0x2,
53 AxisTypeBarCategory = 0x2,
54 AxisTypeCategory = 0x3,
54 AxisTypeCategory = 0x3,
55 AxisTypeDateTime = 0x4
55 AxisTypeDateTime = 0x4
56 };
56 };
57
57
58 Q_DECLARE_FLAGS(AxisTypes, AxisType)
58 Q_DECLARE_FLAGS(AxisTypes, AxisType)
59
59
60 protected:
60 protected:
61 explicit QAbstractAxis(QAbstractAxisPrivate &d,QObject *parent = 0);
61 explicit QAbstractAxis(QAbstractAxisPrivate &d,QObject *parent = 0);
62
62
63 public:
63 public:
64 ~QAbstractAxis();
64 ~QAbstractAxis();
65
65
66 virtual AxisType type() const = 0;
66 virtual AxisType type() const = 0;
67
67
68 //visibilty handling
68 //visibilty handling
69 bool isVisible() const;
69 bool isVisible() const;
70 void setVisible(bool visible = true);
70 void setVisible(bool visible = true);
71
71
72
72
73 //axis handling
73 //axis handling
74 bool isLineVisible() const;
74 bool isLineVisible() const;
75 void setLineVisible(bool visible = true);
75 void setLineVisible(bool visible = true);
76 void setLinePen(const QPen &pen);
76 void setLinePen(const QPen &pen);
77 QPen linePen() const;
77 QPen linePen() const;
78 void setLinePenColor(QColor color);
78 void setLinePenColor(QColor color);
79 QColor linePenColor() const;
79 QColor linePenColor() const;
80
80
81 //grid handling
81 //grid handling
82 bool isGridLineVisible() const;
82 bool isGridLineVisible() const;
83 void setGridLineVisible(bool visible = true);
83 void setGridLineVisible(bool visible = true);
84 void setGridLinePen(const QPen &pen);
84 void setGridLinePen(const QPen &pen);
85 QPen gridLinePen() const;
85 QPen gridLinePen() const;
86
86
87 //labels handling
87 //labels handling
88 bool labelsVisible() const;
88 bool labelsVisible() const;
89 void setLabelsVisible(bool visible = true);
89 void setLabelsVisible(bool visible = true);
90 void setLabelsPen(const QPen &pen);
90 void setLabelsPen(const QPen &pen);
91 QPen labelsPen() const;
91 QPen labelsPen() const;
92 void setLabelsBrush(const QBrush &brush);
92 void setLabelsBrush(const QBrush &brush);
93 QBrush labelsBrush() const;
93 QBrush labelsBrush() const;
94 void setLabelsFont(const QFont &font);
94 void setLabelsFont(const QFont &font);
95 QFont labelsFont() const;
95 QFont labelsFont() const;
96 void setLabelsAngle(int angle);
96 void setLabelsAngle(int angle);
97 int labelsAngle() const;
97 int labelsAngle() const;
98 void setLabelsColor(QColor color);
98 void setLabelsColor(QColor color);
99 QColor labelsColor() const;
99 QColor labelsColor() const;
100
100
101 //title handling
101 //title handling
102 bool titleVisible() const;
102 bool titleVisible() const;
103 void setTitleVisible(bool visible = true);
103 void setTitleVisible(bool visible = true);
104 void setTitlePen(const QPen &pen);
104 void setTitlePen(const QPen &pen);
105 QPen titlePen() const;
105 QPen titlePen() const;
106 void setTitleBrush(const QBrush &brush);
106 void setTitleBrush(const QBrush &brush);
107 QBrush titleBrush() const;
107 QBrush titleBrush() const;
108 void setTitleFont(const QFont &font);
108 void setTitleFont(const QFont &font);
109 QFont titleFont() const;
109 QFont titleFont() const;
110 void setTitle(const QString& title);
110 void setTitle(const QString& title);
111 QString title() const;
111 QString title() const;
112
112
113
113
114 //shades handling
114 //shades handling
115 bool shadesVisible() const;
115 bool shadesVisible() const;
116 void setShadesVisible(bool visible = true);
116 void setShadesVisible(bool visible = true);
117 void setShadesPen(const QPen &pen);
117 void setShadesPen(const QPen &pen);
118 QPen shadesPen() const;
118 QPen shadesPen() const;
119 void setShadesBrush(const QBrush &brush);
119 void setShadesBrush(const QBrush &brush);
120 QBrush shadesBrush() const;
120 QBrush shadesBrush() const;
121 void setShadesColor(QColor color);
121 void setShadesColor(QColor color);
122 QColor shadesColor() const;
122 QColor shadesColor() const;
123 void setShadesBorderColor(QColor color);
123 void setShadesBorderColor(QColor color);
124 QColor shadesBorderColor() const;
124 QColor shadesBorderColor() const;
125
125
126 Qt::Orientation orientation();
126 Qt::Orientation orientation();
127 bool alternativePlacement() const;
128 void setAlternativePlacement(bool placement);
127
129
128 //range handling
130 //range handling
129 void setMin(const QVariant &min);
131 void setMin(const QVariant &min);
130 void setMax(const QVariant &max);
132 void setMax(const QVariant &max);
131 void setRange(const QVariant &min, const QVariant &max);
133 void setRange(const QVariant &min, const QVariant &max);
132
134
133 void show();
135 void show();
134 void hide();
136 void hide();
135
137
136 Q_SIGNALS:
138 Q_SIGNALS:
137 void visibleChanged(bool visible);
139 void visibleChanged(bool visible);
138 void lineVisibleChanged(bool visible);
140 void lineVisibleChanged(bool visible);
139 void labelsVisibleChanged(bool visible);
141 void labelsVisibleChanged(bool visible);
140 void gridVisibleChanged(bool visible);
142 void gridVisibleChanged(bool visible);
141 void colorChanged(QColor color);
143 void colorChanged(QColor color);
142 void labelsColorChanged(QColor color);
144 void labelsColorChanged(QColor color);
143 void shadesVisibleChanged(bool visible);
145 void shadesVisibleChanged(bool visible);
144 void shadesColorChanged(QColor color);
146 void shadesColorChanged(QColor color);
145 void shadesBorderColorChanged(QColor color);
147 void shadesBorderColorChanged(QColor color);
146
148
147 protected:
149 protected:
148 QScopedPointer<QAbstractAxisPrivate> d_ptr;
150 QScopedPointer<QAbstractAxisPrivate> d_ptr;
149 Q_DISABLE_COPY(QAbstractAxis)
151 Q_DISABLE_COPY(QAbstractAxis)
150 friend class ChartDataSet;
152 friend class ChartDataSet;
151 friend class ChartAxis;
153 friend class ChartAxis;
152 friend class ChartPresenter;
154 friend class ChartPresenter;
153 };
155 };
154
156
155 QTCOMMERCIALCHART_END_NAMESPACE
157 QTCOMMERCIALCHART_END_NAMESPACE
156
158
157 #endif // QABSTRACTAXIS_H
159 #endif // QABSTRACTAXIS_H
@@ -1,116 +1,118
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 QABSTRACTAXIS_P_H
30 #ifndef QABSTRACTAXIS_P_H
31 #define QABSTRACTAXIS_P_H
31 #define QABSTRACTAXIS_P_H
32
32
33 #include "qabstractaxis.h"
33 #include "qabstractaxis.h"
34
34
35 QTCOMMERCIALCHART_BEGIN_NAMESPACE
35 QTCOMMERCIALCHART_BEGIN_NAMESPACE
36
36
37 class ChartPresenter;
37 class ChartPresenter;
38 class ChartAxis;
38 class ChartAxis;
39 class Domain;
39 class Domain;
40 class ChartDataSet;
40 class ChartDataSet;
41
41
42 class QTCOMMERCIALCHART_AUTOTEST_EXPORT QAbstractAxisPrivate : public QObject
42 class QTCOMMERCIALCHART_AUTOTEST_EXPORT QAbstractAxisPrivate : public QObject
43 {
43 {
44 Q_OBJECT
44 Q_OBJECT
45 public:
45 public:
46 QAbstractAxisPrivate(QAbstractAxis *q);
46 QAbstractAxisPrivate(QAbstractAxis *q);
47 ~QAbstractAxisPrivate();
47 ~QAbstractAxisPrivate();
48
48
49 public:
49 public:
50
50
51 virtual ChartAxis* createGraphics(ChartPresenter* presenter) = 0;
51 virtual ChartAxis* createGraphics(ChartPresenter* presenter) = 0;
52 virtual void intializeDomain(Domain* domain) = 0;
52 virtual void intializeDomain(Domain* domain) = 0;
53
53
54 void emitUpdated();
54 void emitUpdated();
55 void setDirty(bool dirty);
55 void setDirty(bool dirty);
56 bool isDirty(){ return m_dirty; }
56 bool isDirty(){ return m_dirty; }
57 void setOrientation(Qt::Orientation orientation);
57 void setOrientation(Qt::Orientation orientation);
58 Qt::Orientation orientation() const { return m_orientation; }
58 Qt::Orientation orientation() const { return m_orientation; }
59
59
60 virtual void setMin(const QVariant &min) = 0;
60 virtual void setMin(const QVariant &min) = 0;
61 virtual qreal min() = 0;
61 virtual qreal min() = 0;
62
62
63 virtual void setMax(const QVariant &max) = 0;
63 virtual void setMax(const QVariant &max) = 0;
64 virtual qreal max() = 0;
64 virtual qreal max() = 0;
65
65
66 virtual int count() const = 0;
66 virtual int count() const = 0;
67
67
68 virtual void setRange(const QVariant &min, const QVariant &max) = 0;
68 virtual void setRange(const QVariant &min, const QVariant &max) = 0;
69
69
70 public Q_SLOTS:
70 public Q_SLOTS:
71 virtual void handleDomainUpdated() = 0;
71 virtual void handleDomainUpdated() = 0;
72
72
73 Q_SIGNALS:
73 Q_SIGNALS:
74 void updated();
74 void updated();
75
75
76 protected:
76 protected:
77 QAbstractAxis *q_ptr;
77 QAbstractAxis *q_ptr;
78 Qt::Orientation m_orientation;
78 Qt::Orientation m_orientation;
79 ChartDataSet *m_dataset;
79 ChartDataSet *m_dataset;
80
80
81 private:
81 private:
82 bool m_visible;
82 bool m_visible;
83
83
84 bool m_arrowVisible;
84 bool m_arrowVisible;
85 QPen m_axisPen;
85 QPen m_axisPen;
86 QBrush m_axisBrush;
86 QBrush m_axisBrush;
87
87
88 bool m_gridLineVisible;
88 bool m_gridLineVisible;
89 QPen m_gridLinePen;
89 QPen m_gridLinePen;
90
90
91 bool m_labelsVisible;
91 bool m_labelsVisible;
92 QPen m_labelsPen;
92 QPen m_labelsPen;
93 QBrush m_labelsBrush;
93 QBrush m_labelsBrush;
94 QFont m_labelsFont;
94 QFont m_labelsFont;
95 int m_labelsAngle;
95 int m_labelsAngle;
96
96
97 bool m_titleVisible;
97 bool m_titleVisible;
98 QPen m_titlePen;
98 QPen m_titlePen;
99 QBrush m_titleBrush;
99 QBrush m_titleBrush;
100 QFont m_titleFont;
100 QFont m_titleFont;
101 QString m_title;
101 QString m_title;
102
102
103 bool m_shadesVisible;
103 bool m_shadesVisible;
104 QPen m_shadesPen;
104 QPen m_shadesPen;
105 QBrush m_shadesBrush;
105 QBrush m_shadesBrush;
106 qreal m_shadesOpacity;
106 qreal m_shadesOpacity;
107
107
108 bool m_dirty;
108 bool m_dirty;
109
109
110 bool m_alternativePlacement;
111
110 friend class QAbstractAxis;
112 friend class QAbstractAxis;
111 friend class ChartDataSet;
113 friend class ChartDataSet;
112 };
114 };
113
115
114 QTCOMMERCIALCHART_END_NAMESPACE
116 QTCOMMERCIALCHART_END_NAMESPACE
115
117
116 #endif
118 #endif
@@ -1,160 +1,172
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 "chartvalueaxisx_p.h"
21 #include "chartvalueaxisx_p.h"
22 #include "qabstractaxis.h"
22 #include "qabstractaxis.h"
23 #include "chartpresenter_p.h"
23 #include "chartpresenter_p.h"
24 #include "qvalueaxis.h"
24 #include "qvalueaxis.h"
25 #include <QGraphicsLayout>
25 #include <QGraphicsLayout>
26 #include <QFontMetrics>
26 #include <QFontMetrics>
27 #include <qmath.h>
27 #include <qmath.h>
28 #include <QDebug>
28 #include <QDebug>
29
29
30 static int label_padding = 5;
30 static int label_padding = 5;
31
31
32 QTCOMMERCIALCHART_BEGIN_NAMESPACE
32 QTCOMMERCIALCHART_BEGIN_NAMESPACE
33
33
34 ChartValueAxisX::ChartValueAxisX(QAbstractAxis *axis,ChartPresenter *presenter) : ChartAxis(axis,presenter),
34 ChartValueAxisX::ChartValueAxisX(QAbstractAxis *axis,ChartPresenter *presenter) : ChartAxis(axis,presenter),
35 m_tickCount(0)
35 m_tickCount(0)
36 {
36 {
37 }
37 }
38
38
39 ChartValueAxisX::~ChartValueAxisX()
39 ChartValueAxisX::~ChartValueAxisX()
40 {
40 {
41 }
41 }
42
42
43 QVector<qreal> ChartValueAxisX::calculateLayout() const
43 QVector<qreal> ChartValueAxisX::calculateLayout() const
44 {
44 {
45 Q_ASSERT(m_tickCount>=2);
45 Q_ASSERT(m_tickCount>=2);
46
46
47 QVector<qreal> points;
47 QVector<qreal> points;
48 points.resize(m_tickCount);
48 points.resize(m_tickCount);
49
49
50 QRectF rect = presenter()->chartsGeometry();
50 QRectF rect = presenter()->chartsGeometry();
51
51
52 const qreal deltaX = rect.width()/(m_tickCount-1);
52 const qreal deltaX = rect.width()/(m_tickCount-1);
53 for (int i = 0; i < m_tickCount; ++i) {
53 for (int i = 0; i < m_tickCount; ++i) {
54 int x = i * deltaX + rect.left();
54 int x = i * deltaX + rect.left();
55 points[i] = x;
55 points[i] = x;
56 }
56 }
57 return points;
57 return points;
58 }
58 }
59
59
60 void ChartValueAxisX::updateGeometry()
60 void ChartValueAxisX::updateGeometry()
61 {
61 {
62 const QVector<qreal>& layout = ChartAxis::layout();
62 const QVector<qreal>& layout = ChartAxis::layout();
63
63
64 if(layout.isEmpty()) return;
64 if(layout.isEmpty()) return;
65
65
66 QStringList ticksList = createNumberLabels(m_min,m_max,layout.size());
66 QStringList ticksList = createNumberLabels(m_min,m_max,layout.size());
67
67
68 QList<QGraphicsItem *> lines = m_grid->childItems();
68 QList<QGraphicsItem *> lines = m_grid->childItems();
69 QList<QGraphicsItem *> labels = m_labels->childItems();
69 QList<QGraphicsItem *> labels = m_labels->childItems();
70 QList<QGraphicsItem *> shades = m_shades->childItems();
70 QList<QGraphicsItem *> shades = m_shades->childItems();
71 QList<QGraphicsItem *> axis = m_arrow->childItems();
71 QList<QGraphicsItem *> axis = m_arrow->childItems();
72
72
73 Q_ASSERT(labels.size() == ticksList.size());
73 Q_ASSERT(labels.size() == ticksList.size());
74 Q_ASSERT(layout.size() == ticksList.size());
74 Q_ASSERT(layout.size() == ticksList.size());
75
75
76 QRectF chartRrect = presenter()->chartsGeometry();
76 QRectF chartRrect = presenter()->chartsGeometry();
77
77
78 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(axis.at(0));
78 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(axis.at(0));
79 lineItem->setLine(chartRrect.left(), chartRrect.bottom(), chartRrect.right(), chartRrect.bottom());
79 // lineItem->setLine(chartRrect.left(), chartRrect.bottom(), chartRrect.right(), chartRrect.bottom());
80 if (m_chartAxis->alternativePlacement())
81 lineItem->setLine(chartRrect.left(), m_internalRect.top(), chartRrect.right(), m_internalRect.top());
82 else
83 lineItem->setLine(chartRrect.left(), m_internalRect.bottom(), chartRrect.right(), m_internalRect.bottom());
80
84
81 qreal width = 0;
85 qreal width = 0;
82 for (int i = 0; i < layout.size(); ++i) {
86 for (int i = 0; i < layout.size(); ++i) {
83 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i));
87 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i));
84 lineItem->setLine(layout[i], chartRrect.top(), layout[i], chartRrect.bottom());
88 lineItem->setLine(layout[i], chartRrect.top(), layout[i], chartRrect.bottom());
85 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i));
89 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i));
86 labelItem->setText(ticksList.at(i));
90 labelItem->setText(ticksList.at(i));
87 const QRectF& rect = labelItem->boundingRect();
91 const QRectF& rect = labelItem->boundingRect();
88 QPointF center = rect.center();
92 QPointF center = rect.center();
89 labelItem->setTransformOriginPoint(center.x(), center.y());
93 labelItem->setTransformOriginPoint(center.x(), center.y());
90 labelItem->setPos(layout[i] - center.x(), chartRrect.bottom() + label_padding);
94 // labelItem->setPos(layout[i] - center.x(), chartRrect.bottom() + label_padding);
91 if(labelItem->pos().x() <= width ||
95 if (m_chartAxis->alternativePlacement())
92 labelItem->pos().x() < m_rect.left() ||
96 labelItem->setPos(layout[i] - center.x(), m_internalRect.top() - rect.height() - label_padding);
93 labelItem->pos().x() + rect.width() > m_rect.right()){
97 else
94 labelItem->setVisible(false);
98 labelItem->setPos(layout[i] - center.x(), m_internalRect.bottom() + label_padding);
95 lineItem->setVisible(false);
99 if(labelItem->pos().x() <= width ||
96 }else{
100 labelItem->pos().x() < m_rect.left() ||
97 labelItem->setVisible(true);
101 labelItem->pos().x() + rect.width() > m_rect.right()){
98 lineItem->setVisible(true);
102 labelItem->setVisible(false);
99 width=rect.width()+labelItem->pos().x();
103 lineItem->setVisible(false);
100 }
104 }else{
105 labelItem->setVisible(true);
106 lineItem->setVisible(true);
107 width=rect.width()+labelItem->pos().x();
108 }
101
109
102 if ((i+1)%2 && i>1) {
110 if ((i+1)%2 && i>1) {
103 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2-1));
111 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2-1));
104 rectItem->setRect(layout[i-1],chartRrect.top(),layout[i]-layout[i-1],chartRrect.height());
112 rectItem->setRect(layout[i-1],chartRrect.top(),layout[i]-layout[i-1],chartRrect.height());
105 }
113 }
106 lineItem = static_cast<QGraphicsLineItem*>(axis.at(i+1));
114 lineItem = static_cast<QGraphicsLineItem*>(axis.at(i+1));
107 lineItem->setLine(layout[i],chartRrect.bottom(),layout[i],chartRrect.bottom()+5);
115 // lineItem->setLine(layout[i],chartRrect.bottom(),layout[i],chartRrect.bottom()+5);
116 if (m_chartAxis->alternativePlacement())
117 lineItem->setLine(layout[i],m_internalRect.top(),layout[i],m_internalRect.top()-5);
118 else
119 lineItem->setLine(layout[i],m_internalRect.bottom(),layout[i],m_internalRect.bottom()+5);
108 }
120 }
109 }
121 }
110
122
111 void ChartValueAxisX::handleAxisUpdated()
123 void ChartValueAxisX::handleAxisUpdated()
112 {
124 {
113 //TODO:: fix this
125 //TODO:: fix this
114 QValueAxis* axis = qobject_cast<QValueAxis*>(m_chartAxis);
126 QValueAxis* axis = qobject_cast<QValueAxis*>(m_chartAxis);
115 m_tickCount = axis->tickCount();
127 m_tickCount = axis->tickCount();
116 ChartAxis::handleAxisUpdated();
128 ChartAxis::handleAxisUpdated();
117 }
129 }
118
130
119 QSizeF ChartValueAxisX::sizeHint(Qt::SizeHint which, const QSizeF& constraint) const
131 QSizeF ChartValueAxisX::sizeHint(Qt::SizeHint which, const QSizeF& constraint) const
120 {
132 {
121 Q_UNUSED(constraint)
133 Q_UNUSED(constraint)
122
134
123 QFontMetrics fn(m_font);
135 QFontMetrics fn(m_font);
124 QSizeF sh;
136 QSizeF sh;
125
137
126 QSizeF base = ChartAxis::sizeHint(which, constraint);
138 QSizeF base = ChartAxis::sizeHint(which, constraint);
127 QStringList ticksList = createNumberLabels(m_min,m_max,m_tickCount);
139 QStringList ticksList = createNumberLabels(m_min,m_max,m_tickCount);
128 qreal width=0;
140 qreal width=0;
129 qreal height=0;
141 qreal height=0;
130
142
131 switch (which) {
143 switch (which) {
132 case Qt::MinimumSize:{
144 case Qt::MinimumSize:{
133 int count = qMax(ticksList.last().count(),ticksList.first().count());
145 int count = qMax(ticksList.last().count(),ticksList.first().count());
134 width=fn.averageCharWidth()*count;
146 width=fn.averageCharWidth()*count;
135 height=fn.height()+label_padding;
147 height=fn.height()+label_padding;
136 width=qMax(width,base.width());
148 width=qMax(width,base.width());
137 height+=base.height();
149 height+=base.height();
138 sh = QSizeF(width,height);
150 sh = QSizeF(width,height);
139 break;
151 break;
140 }
152 }
141 case Qt::PreferredSize:{
153 case Qt::PreferredSize:{
142 for (int i = 0; i < ticksList.size(); ++i)
154 for (int i = 0; i < ticksList.size(); ++i)
143 {
155 {
144 width+=fn.averageCharWidth()*ticksList.at(i).count();
156 width+=fn.averageCharWidth()*ticksList.at(i).count();
145
157
146 }
147 height=fn.height()+label_padding;
148 width=qMax(width,base.width());
149 height+=base.height();
150 sh = QSizeF(width,height);
151 break;
152 }
158 }
153 default:
159 height=fn.height()+label_padding;
154 break;
160 width=qMax(width,base.width());
155 }
161 height+=base.height();
162 sh = QSizeF(width,height);
163 break;
164 }
165 default:
166 break;
167 }
156
168
157 return sh;
169 return sh;
158 }
170 }
159
171
160 QTCOMMERCIALCHART_END_NAMESPACE
172 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,175 +1,187
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 "chartvalueaxisy_p.h"
21 #include "chartvalueaxisy_p.h"
22 #include "qabstractaxis.h"
22 #include "qabstractaxis.h"
23 #include "chartpresenter_p.h"
23 #include "chartpresenter_p.h"
24 #include "qvalueaxis.h"
24 #include "qvalueaxis.h"
25 #include <QGraphicsLayout>
25 #include <QGraphicsLayout>
26 #include <QFontMetrics>
26 #include <QFontMetrics>
27 #include <qmath.h>
27 #include <qmath.h>
28 #include <QDebug>
28 #include <QDebug>
29
29
30 static int label_padding = 5;
30 static int label_padding = 5;
31
31
32 QTCOMMERCIALCHART_BEGIN_NAMESPACE
32 QTCOMMERCIALCHART_BEGIN_NAMESPACE
33
33
34 ChartValueAxisY::ChartValueAxisY(QAbstractAxis *axis,ChartPresenter *presenter) : ChartAxis(axis,presenter),
34 ChartValueAxisY::ChartValueAxisY(QAbstractAxis *axis,ChartPresenter *presenter) : ChartAxis(axis,presenter),
35 m_tickCount(0)
35 m_tickCount(0)
36 {
36 {
37 }
37 }
38
38
39 ChartValueAxisY::~ChartValueAxisY()
39 ChartValueAxisY::~ChartValueAxisY()
40 {
40 {
41 }
41 }
42
42
43 QVector<qreal> ChartValueAxisY::calculateLayout() const
43 QVector<qreal> ChartValueAxisY::calculateLayout() const
44 {
44 {
45 Q_ASSERT(m_tickCount>=2);
45 Q_ASSERT(m_tickCount>=2);
46
46
47 QVector<qreal> points;
47 QVector<qreal> points;
48 points.resize(m_tickCount);
48 points.resize(m_tickCount);
49
49
50 QRectF rect = presenter()->chartsGeometry();
50 QRectF rect = presenter()->chartsGeometry();
51
51
52 const qreal deltaY = rect.height()/(m_tickCount-1);
52 const qreal deltaY = rect.height()/(m_tickCount-1);
53 for (int i = 0; i < m_tickCount; ++i) {
53 for (int i = 0; i < m_tickCount; ++i) {
54 int y = i * -deltaY + rect.bottom();
54 int y = i * -deltaY + rect.bottom();
55 points[i] = y;
55 points[i] = y;
56 }
56 }
57
57
58 return points;
58 return points;
59 }
59 }
60
60
61 void ChartValueAxisY::updateGeometry()
61 void ChartValueAxisY::updateGeometry()
62 {
62 {
63 const QVector<qreal> &layout = ChartAxis::layout();
63 const QVector<qreal> &layout = ChartAxis::layout();
64
64
65 if(layout.isEmpty()) return;
65 if(layout.isEmpty()) return;
66
66
67 QStringList ticksList = createNumberLabels(m_min,m_max,layout.size());
67 QStringList ticksList = createNumberLabels(m_min,m_max,layout.size());
68
68
69 QList<QGraphicsItem *> lines = m_grid->childItems();
69 QList<QGraphicsItem *> lines = m_grid->childItems();
70 QList<QGraphicsItem *> labels = m_labels->childItems();
70 QList<QGraphicsItem *> labels = m_labels->childItems();
71 QList<QGraphicsItem *> shades = m_shades->childItems();
71 QList<QGraphicsItem *> shades = m_shades->childItems();
72 QList<QGraphicsItem *> axis = m_arrow->childItems();
72 QList<QGraphicsItem *> axis = m_arrow->childItems();
73
73
74 Q_ASSERT(labels.size() == ticksList.size());
74 Q_ASSERT(labels.size() == ticksList.size());
75 Q_ASSERT(layout.size() == ticksList.size());
75 Q_ASSERT(layout.size() == ticksList.size());
76
76
77 QRectF chartRect = presenter()->chartsGeometry();
77 QRectF chartRect = presenter()->chartsGeometry();
78
78
79 qreal height = m_rect.bottom();
79 qreal height = m_rect.bottom();
80
80
81 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(axis.at(0));
81 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(axis.at(0));
82 lineItem->setLine( chartRect.left() , chartRect.top(), chartRect.left(), chartRect.bottom());
82 // lineItem->setLine( chartRect.left() , chartRect.top(), chartRect.left(), chartRect.bottom());
83 if (m_chartAxis->alternativePlacement())
84 lineItem->setLine( m_internalRect.right() , chartRect.top(), m_internalRect.right(), chartRect.bottom());
85 else
86 lineItem->setLine( m_internalRect.left() , chartRect.top(), m_internalRect.left(), chartRect.bottom());
83
87
84 QFontMetrics fn(m_font);
88 QFontMetrics fn(m_font);
85
89
86 for (int i = 0; i < layout.size(); ++i) {
90 for (int i = 0; i < layout.size(); ++i) {
87 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i));
91 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i));
88 lineItem->setLine( chartRect.left() , layout[i], chartRect.right(), layout[i]);
92 lineItem->setLine( chartRect.left() , layout[i], chartRect.right(), layout[i]);
89 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i));
93 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i));
90
94
91 QString text = ticksList.at(i);
95 QString text = ticksList.at(i);
92
96
93 if (fn.boundingRect(text).width() > chartRect.left() - m_rect.left() - label_padding )
97 if (fn.boundingRect(text).width() > chartRect.left() - m_rect.left() - label_padding )
94 {
98 {
95 QString label = text + "...";
99 QString label = text + "...";
96 while (fn.boundingRect(label).width() > chartRect.left() - m_rect.left() - label_padding && label.length() > 3)
100 while (fn.boundingRect(label).width() > chartRect.left() - m_rect.left() - label_padding && label.length() > 3)
97 label.remove(label.length() - 4, 1);
101 label.remove(label.length() - 4, 1);
98 labelItem->setText(label);
102 labelItem->setText(label);
99 }else{
103 }else{
100 labelItem->setText(text);
104 labelItem->setText(text);
101 }
105 }
102
106
103 const QRectF& rect = labelItem->boundingRect();
107 const QRectF& rect = labelItem->boundingRect();
104
108
105 QPointF center = rect.center();
109 QPointF center = rect.center();
106 labelItem->setTransformOriginPoint(center.x(), center.y());
110 labelItem->setTransformOriginPoint(center.x(), center.y());
107 labelItem->setPos( chartRect.left() - rect.width() - label_padding , layout[i]-center.y());
111 // labelItem->setPos( chartRect.left() - rect.width() - label_padding , layout[i]-center.y());
112 if (m_chartAxis->alternativePlacement())
113 labelItem->setPos( m_internalRect.right() + label_padding , layout[i]-center.y());
114 else
115 labelItem->setPos( m_internalRect.left() - rect.width() - label_padding , layout[i]-center.y());
108
116
109 if(labelItem->pos().y() + rect.height() > height ||
117 if(labelItem->pos().y() + rect.height() > height ||
110 labelItem->pos().y() < m_rect.top()) {
118 labelItem->pos().y() < m_rect.top()) {
111 labelItem->setVisible(false);
119 labelItem->setVisible(false);
112 lineItem->setVisible(false);
120 lineItem->setVisible(false);
113 }else{
121 }else{
114 labelItem->setVisible(true);
122 labelItem->setVisible(true);
115 lineItem->setVisible(true);
123 lineItem->setVisible(true);
116 height=labelItem->pos().y();
124 height=labelItem->pos().y();
117 }
125 }
118
126
119 if ((i+1)%2 && i>1) {
127 if ((i+1)%2 && i>1) {
120 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2-1));
128 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2-1));
121 rectItem->setRect( chartRect.left(),layout[i], chartRect.width(),layout[i-1]-layout[i]);
129 rectItem->setRect( chartRect.left(),layout[i], chartRect.width(),layout[i-1]-layout[i]);
122 }
130 }
123 lineItem = static_cast<QGraphicsLineItem*>(axis.at(i+1));
131 lineItem = static_cast<QGraphicsLineItem*>(axis.at(i+1));
124 lineItem->setLine( chartRect.left()-5,layout[i], chartRect.left(),layout[i]);
132 // lineItem->setLine( chartRect.left()-5,layout[i], chartRect.left(),layout[i]);
133 if (m_chartAxis->alternativePlacement())
134 lineItem->setLine( m_internalRect.right()+5,layout[i], m_internalRect.right(),layout[i]);
135 else
136 lineItem->setLine( m_internalRect.left()-5,layout[i], m_internalRect.left(),layout[i]);
125 }
137 }
126 }
138 }
127
139
128 void ChartValueAxisY::handleAxisUpdated()
140 void ChartValueAxisY::handleAxisUpdated()
129 {
141 {
130 //TODO:: fix this
142 //TODO:: fix this
131 QValueAxis* axis = qobject_cast<QValueAxis*>(m_chartAxis);
143 QValueAxis* axis = qobject_cast<QValueAxis*>(m_chartAxis);
132 m_tickCount = axis->tickCount();
144 m_tickCount = axis->tickCount();
133 ChartAxis::handleAxisUpdated();
145 ChartAxis::handleAxisUpdated();
134 }
146 }
135
147
136 QSizeF ChartValueAxisY::sizeHint(Qt::SizeHint which, const QSizeF& constraint) const
148 QSizeF ChartValueAxisY::sizeHint(Qt::SizeHint which, const QSizeF& constraint) const
137 {
149 {
138 Q_UNUSED(constraint)
150 Q_UNUSED(constraint)
139
151
140 QFontMetrics fn(m_font);
152 QFontMetrics fn(m_font);
141 QSizeF sh;
153 QSizeF sh;
142 QSizeF base = ChartAxis::sizeHint(which, constraint);
154 QSizeF base = ChartAxis::sizeHint(which, constraint);
143 QStringList ticksList = createNumberLabels(m_min,m_max,m_tickCount);
155 QStringList ticksList = createNumberLabels(m_min,m_max,m_tickCount);
144 qreal width=0;
156 qreal width=0;
145 qreal height=0;
157 qreal height=0;
146
158
147 switch (which) {
159 switch (which) {
148 case Qt::MinimumSize: {
160 case Qt::MinimumSize: {
149 int count = qMax(ticksList.first().count() , ticksList.last().count());
161 int count = qMax(ticksList.first().count() , ticksList.last().count());
150 width=fn.averageCharWidth()*count+label_padding;
162 width=fn.averageCharWidth()*count+label_padding;
151 height=fn.height();
163 height=fn.height();
152 height=qMax(height,base.height());
164 height=qMax(height,base.height());
153 width+=base.width();
165 width+=base.width();
154 sh = QSizeF(width,height);
166 sh = QSizeF(width,height);
155 break;
167 break;
156 }
168 }
157 case Qt::PreferredSize:
169 case Qt::PreferredSize:
170 {
171 for (int i = 0; i < ticksList.size(); ++i)
158 {
172 {
159 for (int i = 0; i < ticksList.size(); ++i)
173 width=qMax(qreal(fn.averageCharWidth()*ticksList.at(i).count())+label_padding,width);
160 {
174 height+=fn.height();
161 width=qMax(qreal(fn.averageCharWidth()*ticksList.at(i).count())+label_padding,width);
162 height+=fn.height();
163 }
164 height=qMax(height,base.height());
165 width+=base.width();
166 sh = QSizeF(width,height);
167 break;
168 }
175 }
169 default:
176 height=qMax(height,base.height());
177 width+=base.width();
178 sh = QSizeF(width,height);
179 break;
180 }
181 default:
170 break;
182 break;
171 }
183 }
172 return sh;
184 return sh;
173 }
185 }
174
186
175 QTCOMMERCIALCHART_END_NAMESPACE
187 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,260 +1,299
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 "chartlayout_p.h"
21 #include "chartlayout_p.h"
22 #include "chartpresenter_p.h"
22 #include "chartpresenter_p.h"
23 #include "qlegend_p.h"
23 #include "qlegend_p.h"
24 #include "chartaxis_p.h"
24 #include "chartaxis_p.h"
25 #include "charttitle_p.h"
25 #include "charttitle_p.h"
26 #include "chartbackground_p.h"
26 #include "chartbackground_p.h"
27 #include "legendmarker_p.h"
27 #include "legendmarker_p.h"
28
28
29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30
30
31 static const qreal golden_ratio = 0.25;
31 static const qreal golden_ratio = 0.25;
32
32
33 ChartLayout::ChartLayout(ChartPresenter* presenter):
33 ChartLayout::ChartLayout(ChartPresenter* presenter):
34 m_presenter(presenter),
34 m_presenter(presenter),
35 m_margins(20,20,20,20),
35 m_margins(20,20,20,20),
36 m_minChartRect(0,0,200,200)
36 m_minChartRect(0,0,200,200)
37 {
37 {
38
38
39 }
39 }
40
40
41 ChartLayout::~ChartLayout()
41 ChartLayout::~ChartLayout()
42 {
42 {
43
43
44 }
44 }
45
45
46 void ChartLayout::setGeometry(const QRectF& rect)
46 void ChartLayout::setGeometry(const QRectF& rect)
47 {
47 {
48 if (!rect.isValid())
48 if (!rect.isValid())
49 return;
49 return;
50
50
51 QList<ChartAxis*> axes = m_presenter->axisItems();
51 QList<ChartAxis*> axes = m_presenter->axisItems();
52 ChartTitle* title = m_presenter->titleElement();
52 ChartTitle* title = m_presenter->titleElement();
53 QLegend* legend = m_presenter->legend();
53 QLegend* legend = m_presenter->legend();
54 ChartBackground* background = m_presenter->backgroundElement();
54 ChartBackground* background = m_presenter->backgroundElement();
55
55
56 QRectF contentGeometry = calculateBackgroundGeometry(rect,background);
56 QRectF contentGeometry = calculateBackgroundGeometry(rect,background);
57
57
58 contentGeometry = calculateContentGeometry(contentGeometry);
58 contentGeometry = calculateContentGeometry(contentGeometry);
59
59
60 if (title && title->isVisible()) {
60 if (title && title->isVisible()) {
61 contentGeometry = calculateTitleGeometry(contentGeometry,title);
61 contentGeometry = calculateTitleGeometry(contentGeometry,title);
62 }
62 }
63
63
64 if (legend->isAttachedToChart() && legend->isVisible()) {
64 if (legend->isAttachedToChart() && legend->isVisible()) {
65 contentGeometry = calculateLegendGeometry(contentGeometry,legend);
65 contentGeometry = calculateLegendGeometry(contentGeometry,legend);
66 }
66 }
67
67
68 calculateChartGeometry(contentGeometry,axes);
68 calculateChartGeometry(contentGeometry,axes);
69
69
70 //TODO remove me
70 //TODO remove me
71 #ifdef SHOW_LAYOUT
71 #ifdef SHOW_LAYOUT
72 LayoutDebuger* debuger = LayoutDebuger::instance();
72 LayoutDebuger* debuger = LayoutDebuger::instance();
73 debuger->reset();
73 debuger->reset();
74 debuger->setPen(QPen(Qt::red));
74 debuger->setPen(QPen(Qt::red));
75 debuger->add(backgroundGeometry,m_presenter->rootItem());
75 debuger->add(backgroundGeometry,m_presenter->rootItem());
76 debuger->add(titleGeometry,m_presenter->rootItem());
76 debuger->add(titleGeometry,m_presenter->rootItem());
77 debuger->add(legendGeometry ,m_presenter->rootItem());
77 debuger->add(legendGeometry ,m_presenter->rootItem());
78 debuger->add(axisGeometry ,m_presenter->rootItem());
78 debuger->add(axisGeometry ,m_presenter->rootItem());
79 debuger->add(geometry,m_presenter->rootItem());
79 debuger->add(geometry,m_presenter->rootItem());
80 foreach(LegendMarker* marker,legend->d_ptr->markers()){
80 foreach(LegendMarker* marker,legend->d_ptr->markers()){
81 debuger->add(marker->mapRectToScene(marker->boundingRect()),m_presenter->rootItem());
81 debuger->add(marker->mapRectToScene(marker->boundingRect()),m_presenter->rootItem());
82 }
82 }
83 #endif
83 #endif
84
84
85 QGraphicsLayout::setGeometry(rect);
85 QGraphicsLayout::setGeometry(rect);
86 }
86 }
87
87
88 QRectF ChartLayout::calculateContentGeometry(const QRectF& geometry) const
88 QRectF ChartLayout::calculateContentGeometry(const QRectF& geometry) const
89 {
89 {
90 return geometry.adjusted(m_margins.left(),m_margins.top(),-m_margins.right(),-m_margins.bottom());
90 return geometry.adjusted(m_margins.left(),m_margins.top(),-m_margins.right(),-m_margins.bottom());
91 }
91 }
92
92
93 QRectF ChartLayout::calculateContentMinimum(const QRectF& minimum) const
93 QRectF ChartLayout::calculateContentMinimum(const QRectF& minimum) const
94 {
94 {
95 return minimum.adjusted(0,0,m_margins.left()+m_margins.right(),m_margins.top() + m_margins.bottom());
95 return minimum.adjusted(0,0,m_margins.left()+m_margins.right(),m_margins.top() + m_margins.bottom());
96 }
96 }
97
97
98
98
99 QRectF ChartLayout::calculateBackgroundGeometry(const QRectF& geometry,ChartBackground* background) const
99 QRectF ChartLayout::calculateBackgroundGeometry(const QRectF& geometry,ChartBackground* background) const
100 {
100 {
101 qreal left, top, right, bottom;
101 qreal left, top, right, bottom;
102 getContentsMargins(&left, &top, &right, &bottom);
102 getContentsMargins(&left, &top, &right, &bottom);
103 QRectF backgroundGeometry = geometry.adjusted(left,top,-right,-bottom);
103 QRectF backgroundGeometry = geometry.adjusted(left,top,-right,-bottom);
104 if(background) background->setRect(backgroundGeometry);
104 if(background) background->setRect(backgroundGeometry);
105 return backgroundGeometry;
105 return backgroundGeometry;
106 }
106 }
107
107
108 QRectF ChartLayout::calculateBackgroundMinimum(const QRectF& minimum) const
108 QRectF ChartLayout::calculateBackgroundMinimum(const QRectF& minimum) const
109 {
109 {
110 qreal left, top, right, bottom;
110 qreal left, top, right, bottom;
111 getContentsMargins(&left, &top, &right, &bottom);
111 getContentsMargins(&left, &top, &right, &bottom);
112 return minimum.adjusted(0,0,left + right,top+bottom);
112 return minimum.adjusted(0,0,left + right,top+bottom);
113 }
113 }
114
114
115 QRectF ChartLayout::calculateChartGeometry(const QRectF& geometry, const QList<ChartAxis*>& axes) const
115 QRectF ChartLayout::calculateChartGeometry(const QRectF& geometry, const QList<ChartAxis*>& axes) const
116 {
116 {
117
117
118 QSizeF vertical(0,0);
118 QSizeF vertical(0,0);
119 QSizeF horizontal(0,0);
119 QSizeF horizontal(0,0);
120
120
121 int topAxisCount = 0;
122 int bottomAxisCount = 0;
123 int leftAxisCount = 0;
124 int rightAxisCount = 0;
125
121 // check axis size
126 // check axis size
122 foreach(ChartAxis* axis , axes) {
127 foreach(ChartAxis* axis , axes) {
123 if(axis->orientation()==Qt::Vertical && axis->isVisible()) {
128 if(axis->orientation()==Qt::Vertical && axis->isVisible()) {
124 vertical = vertical.expandedTo(axis->effectiveSizeHint(Qt::MinimumSize));
129 vertical = vertical.expandedTo(axis->effectiveSizeHint(Qt::MinimumSize));
130 if (axis->alternativePlacement())
131 rightAxisCount++;
132 else
133 leftAxisCount++;
125 }
134 }
126 else if(axis->orientation()==Qt::Horizontal && axis->isVisible()) {
135 else if(axis->orientation()==Qt::Horizontal && axis->isVisible()) {
127 horizontal = horizontal.expandedTo(axis->effectiveSizeHint(Qt::MinimumSize));
136 horizontal = horizontal.expandedTo(axis->effectiveSizeHint(Qt::MinimumSize));
137 if (axis->alternativePlacement())
138 topAxisCount++;
139 else
140 bottomAxisCount++;
128 }
141 }
129
142
130 }
143 }
131
144
132 qreal width = qMin(vertical.width(),geometry.width() * golden_ratio);
145 qreal width = qMin(vertical.width(),geometry.width() * golden_ratio);
133
146
134 QRectF rect = geometry.adjusted(width,vertical.height()/2,-horizontal.width()/2,-horizontal.height());
147 QRectF rect = geometry.adjusted(width,vertical.height()/2,-horizontal.width()/2,-horizontal.height());
135
148
149 // axis area width
150 // TODO: replase with dynamic size code
151 int axisWidth = 35;
152
153 // get the final size of the plot rect
154 rect.adjust(leftAxisCount * axisWidth, topAxisCount * axisWidth, -rightAxisCount * axisWidth, -bottomAxisCount * axisWidth);
136 m_presenter->setChartsGeometry(rect);
155 m_presenter->setChartsGeometry(rect);
137
156
157 leftAxisCount = 0;
158 rightAxisCount = 0;
159 bottomAxisCount = 0;
160 topAxisCount = 0;
161
162 // adjust the axes internal and external rects
138 foreach(ChartAxis* axis , axes) {
163 foreach(ChartAxis* axis , axes) {
139 axis->setGeometry(geometry);
164 if(axis->orientation()==Qt::Vertical) {
165 axis->setInternalRect(rect.adjusted(-leftAxisCount * axisWidth, 0, rightAxisCount * axisWidth, 0));
166 axis->setGeometry(rect.adjusted(-(leftAxisCount + 1) * axisWidth, 0, (rightAxisCount + 1) * axisWidth, 0));
167 if (axis->alternativePlacement())
168 rightAxisCount++;
169 else
170 leftAxisCount++;
171 }
172 else if(axis->orientation()==Qt::Horizontal) {
173 axis->setInternalRect(rect.adjusted(0, -topAxisCount * axisWidth, 0, bottomAxisCount * axisWidth));
174 axis->setGeometry(rect.adjusted(0, -(topAxisCount + 1) * axisWidth, 0, (bottomAxisCount + 1) * axisWidth));
175 if (axis->alternativePlacement())
176 topAxisCount++;
177 else
178 bottomAxisCount++;
179 }
140 }
180 }
141
181
142 return rect;
182 return rect;
143 }
183 }
144
184
145 QRectF ChartLayout::calculateAxisMinimum(const QRectF& minimum, const QList<ChartAxis*>& axes) const
185 QRectF ChartLayout::calculateAxisMinimum(const QRectF& minimum, const QList<ChartAxis*>& axes) const
146 {
186 {
147 QSizeF vertical(0,0);
187 QSizeF vertical(0,0);
148 QSizeF horizontal(0,0);
188 QSizeF horizontal(0,0);
149
189
150 // check axis size
190 // check axis size
151 foreach(ChartAxis* axis , axes) {
191 foreach(ChartAxis* axis , axes) {
152 if(axis->orientation()==Qt::Vertical && axis->isVisible()){
192 if(axis->orientation()==Qt::Vertical && axis->isVisible()){
153 vertical = vertical.expandedTo(axis->effectiveSizeHint(Qt::MinimumSize));
193 vertical = vertical.expandedTo(axis->effectiveSizeHint(Qt::MinimumSize));
154 }else if(axis->orientation()==Qt::Horizontal && axis->isVisible()) {
194 }else if(axis->orientation()==Qt::Horizontal && axis->isVisible()) {
155 horizontal = horizontal.expandedTo(axis->effectiveSizeHint(Qt::MinimumSize));
195 horizontal = horizontal.expandedTo(axis->effectiveSizeHint(Qt::MinimumSize));
156 }
196 }
157 }
197 }
158 return minimum.adjusted(0,0,horizontal.width()+vertical.width(),horizontal.height() + vertical.height());
198 return minimum.adjusted(0,0,horizontal.width()+vertical.width(),horizontal.height() + vertical.height());
159 }
199 }
160
200
161 QRectF ChartLayout::calculateLegendGeometry(const QRectF& geometry,QLegend* legend) const
201 QRectF ChartLayout::calculateLegendGeometry(const QRectF& geometry,QLegend* legend) const
162 {
202 {
163 QSizeF size = legend->effectiveSizeHint(Qt::PreferredSize,QSizeF(-1,-1));
203 QSizeF size = legend->effectiveSizeHint(Qt::PreferredSize,QSizeF(-1,-1));
164 QRectF legendRect;
204 QRectF legendRect;
165 QRectF result;
205 QRectF result;
166
206
167 switch (legend->alignment()) {
207 switch (legend->alignment()) {
168
208 case Qt::AlignTop: {
169 case Qt::AlignTop: {
209 legendRect = QRectF(geometry.topLeft(),QSizeF(geometry.width(),size.height()));
170 legendRect = QRectF(geometry.topLeft(),QSizeF(geometry.width(),size.height()));
210 result = geometry.adjusted(0,legendRect.height(),0,0);
171 result = geometry.adjusted(0,legendRect.height(),0,0);
211 break;
172 break;
212 }
173 }
213 case Qt::AlignBottom: {
174 case Qt::AlignBottom: {
214 legendRect = QRectF(QPointF(geometry.left(),geometry.bottom()-size.height()),QSizeF(geometry.width(),size.height()));
175 legendRect = QRectF(QPointF(geometry.left(),geometry.bottom()-size.height()),QSizeF(geometry.width(),size.height()));
215 result = geometry.adjusted(0,0,0,-legendRect.height());
176 result = geometry.adjusted(0,0,0,-legendRect.height());
216 break;
177 break;
217 }
178 }
218 case Qt::AlignLeft: {
179 case Qt::AlignLeft: {
219 qreal width = qMin(size.width(),geometry.width()*golden_ratio);
180 qreal width = qMin(size.width(),geometry.width()*golden_ratio);
220 legendRect = QRectF(geometry.topLeft(),QSizeF(width,geometry.height()));
181 legendRect = QRectF(geometry.topLeft(),QSizeF(width,geometry.height()));
221 result = geometry.adjusted(width,0,0,0);
182 result = geometry.adjusted(width,0,0,0);
222 break;
183 break;
223 }
184 }
224 case Qt::AlignRight: {
185 case Qt::AlignRight: {
225 qreal width = qMin(size.width(),geometry.width()*golden_ratio);
186 qreal width = qMin(size.width(),geometry.width()*golden_ratio);
226 legendRect = QRectF(QPointF(geometry.right()-width,geometry.top()),QSizeF(width,geometry.height()));
187 legendRect = QRectF(QPointF(geometry.right()-width,geometry.top()),QSizeF(width,geometry.height()));
227 result = geometry.adjusted(0,0,-width,0);
188 result = geometry.adjusted(0,0,-width,0);
228 break;
189 break;
229 }
190 }
230 default: {
191 default: {
231 legendRect = QRectF(0,0,0,0);
192 legendRect = QRectF(0,0,0,0);
232 result = geometry;
193 result = geometry;
233 break;
194 break;
234 }
195 }
196 }
235 }
197
236
198 legend->setGeometry(legendRect);
237 legend->setGeometry(legendRect);
199
238
200 return result;
239 return result;
201 }
240 }
202
241
203 QRectF ChartLayout::calculateLegendMinimum(const QRectF& geometry,QLegend* legend) const
242 QRectF ChartLayout::calculateLegendMinimum(const QRectF& geometry,QLegend* legend) const
204 {
243 {
205 QSizeF minSize = legend->effectiveSizeHint(Qt::MinimumSize,QSizeF(-1,-1));
244 QSizeF minSize = legend->effectiveSizeHint(Qt::MinimumSize,QSizeF(-1,-1));
206 return geometry.adjusted(0,0,minSize.width(),minSize.height());
245 return geometry.adjusted(0,0,minSize.width(),minSize.height());
207 }
246 }
208
247
209 QRectF ChartLayout::calculateTitleGeometry(const QRectF& geometry,ChartTitle* title) const
248 QRectF ChartLayout::calculateTitleGeometry(const QRectF& geometry,ChartTitle* title) const
210 {
249 {
211 title->setGeometry(geometry);
250 title->setGeometry(geometry);
212 QPointF center = geometry.center() - title->boundingRect().center();
251 QPointF center = geometry.center() - title->boundingRect().center();
213 title->setPos(center.x(),title->pos().y());
252 title->setPos(center.x(),title->pos().y());
214 return geometry.adjusted(0,title->boundingRect().height(),0,0);
253 return geometry.adjusted(0,title->boundingRect().height(),0,0);
215 }
254 }
216
255
217 QRectF ChartLayout::calculateTitleMinimum(const QRectF& minimum,ChartTitle* title) const
256 QRectF ChartLayout::calculateTitleMinimum(const QRectF& minimum,ChartTitle* title) const
218 {
257 {
219 QSizeF min = title->sizeHint(Qt::MinimumSize);
258 QSizeF min = title->sizeHint(Qt::MinimumSize);
220 return minimum.adjusted(0,0,min.width(),min.height());
259 return minimum.adjusted(0,0,min.width(),min.height());
221 }
260 }
222
261
223 QSizeF ChartLayout::sizeHint ( Qt::SizeHint which, const QSizeF & constraint) const
262 QSizeF ChartLayout::sizeHint ( Qt::SizeHint which, const QSizeF & constraint) const
224 {
263 {
225 Q_UNUSED(constraint);
264 Q_UNUSED(constraint);
226 if(which == Qt::MinimumSize){
265 if(which == Qt::MinimumSize){
227 QList<ChartAxis*> axes = m_presenter->axisItems();
266 QList<ChartAxis*> axes = m_presenter->axisItems();
228 ChartTitle* title = m_presenter->titleElement();
267 ChartTitle* title = m_presenter->titleElement();
229 QLegend* legend = m_presenter->legend();
268 QLegend* legend = m_presenter->legend();
230 QRectF minimumRect(0,0,0,0);
269 QRectF minimumRect(0,0,0,0);
231 minimumRect = calculateBackgroundMinimum(minimumRect);
270 minimumRect = calculateBackgroundMinimum(minimumRect);
232 minimumRect = calculateContentMinimum(minimumRect);
271 minimumRect = calculateContentMinimum(minimumRect);
233 minimumRect = calculateTitleMinimum(minimumRect,title);
272 minimumRect = calculateTitleMinimum(minimumRect,title);
234 minimumRect = calculateLegendMinimum(minimumRect,legend);
273 minimumRect = calculateLegendMinimum(minimumRect,legend);
235 minimumRect = calculateAxisMinimum(minimumRect,axes);
274 minimumRect = calculateAxisMinimum(minimumRect,axes);
236 return minimumRect.united(m_minChartRect).size().toSize();
275 return minimumRect.united(m_minChartRect).size().toSize();
237 }else
276 }else
238 return QSize(-1,-1);
277 return QSize(-1,-1);
239 }
278 }
240
279
241 void ChartLayout::setMargins(const QMargins& margins)
280 void ChartLayout::setMargins(const QMargins& margins)
242 {
281 {
243
282
244 if(m_margins != margins){
283 if(m_margins != margins){
245 m_margins = margins;
284 m_margins = margins;
246 updateGeometry();
285 updateGeometry();
247 }
286 }
248 }
287 }
249
288
250 QMargins ChartLayout::margins() const
289 QMargins ChartLayout::margins() const
251 {
290 {
252 return m_margins;
291 return m_margins;
253 }
292 }
254
293
255 void ChartLayout::adjustChartGeometry()
294 void ChartLayout::adjustChartGeometry()
256 {
295 {
257 setGeometry(geometry());
296 setGeometry(geometry());
258 }
297 }
259
298
260 QTCOMMERCIALCHART_END_NAMESPACE
299 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,456 +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 #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 "qabstractaxis.h"
23 #include "qabstractaxis.h"
24 #include "qabstractaxis_p.h"
24 #include "qabstractaxis_p.h"
25 #include "chartdataset_p.h"
25 #include "chartdataset_p.h"
26 #include "charttheme_p.h"
26 #include "charttheme_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 "chartbackground_p.h"
31 #include "chartbackground_p.h"
32 #include "chartlayout_p.h"
32 #include "chartlayout_p.h"
33 #include "charttitle_p.h"
33 #include "charttitle_p.h"
34 #include <QTimer>
34 #include <QTimer>
35
35
36 QTCOMMERCIALCHART_BEGIN_NAMESPACE
36 QTCOMMERCIALCHART_BEGIN_NAMESPACE
37
37
38 ChartPresenter::ChartPresenter(QChart* chart,ChartDataSet* dataset):QObject(chart),
38 ChartPresenter::ChartPresenter(QChart* chart,ChartDataSet* dataset):QObject(chart),
39 m_chart(chart),
39 m_chart(chart),
40 m_dataset(dataset),
40 m_dataset(dataset),
41 m_chartTheme(0),
41 m_chartTheme(0),
42 m_options(QChart::NoAnimation),
42 m_options(QChart::NoAnimation),
43 m_state(ShowState),
43 m_state(ShowState),
44 m_layout(new ChartLayout(this)),
44 m_layout(new ChartLayout(this)),
45 m_background(0),
45 m_background(0),
46 m_title(0)
46 m_title(0)
47 {
47 {
48
48
49 }
49 }
50
50
51 ChartPresenter::~ChartPresenter()
51 ChartPresenter::~ChartPresenter()
52 {
52 {
53 delete m_chartTheme;
53 delete m_chartTheme;
54 }
54 }
55
55
56 void ChartPresenter::setChartsGeometry(const QRectF& rect)
56 void ChartPresenter::setChartsGeometry(const QRectF& rect)
57 {
57 {
58 Q_ASSERT(rect.isValid());
58 Q_ASSERT(rect.isValid());
59
59
60 if(m_chartsRect!=rect) {
60 if(m_chartsRect!=rect) {
61 m_chartsRect=rect;
61 m_chartsRect=rect;
62 foreach(ChartElement* chart, m_chartItems)
62 foreach(ChartElement* chart, m_chartItems)
63 {
63 {
64 chart->handleGeometryChanged(rect);
64 chart->handleGeometryChanged(rect);
65 }
65 }
66 }
66 }
67 }
67 }
68
68
69 QRectF ChartPresenter::chartsGeometry() const
69 QRectF ChartPresenter::chartsGeometry() const
70 {
70 {
71 return m_chartsRect;
71 return m_chartsRect;
72 }
72 }
73
73
74 void ChartPresenter::handleAxisAdded(QAbstractAxis* axis,Domain* domain)
74 void ChartPresenter::handleAxisAdded(QAbstractAxis* axis,Domain* domain)
75 {
75 {
76 ChartAxis* item = axis->d_ptr->createGraphics(this);
76 ChartAxis* item = axis->d_ptr->createGraphics(this);
77 item->setDomain(domain);
77 item->setDomain(domain);
78
78
79 if(m_options.testFlag(QChart::GridAxisAnimations)){
79 if(m_options.testFlag(QChart::GridAxisAnimations)){
80 item->setAnimation(new AxisAnimation(item));
80 item->setAnimation(new AxisAnimation(item));
81 }
81 }
82
82
83 QObject::connect(this,SIGNAL(geometryChanged(QRectF)),item,SLOT(handleGeometryChanged(QRectF)));
83 QObject::connect(this,SIGNAL(geometryChanged(QRectF)),item,SLOT(handleGeometryChanged(QRectF)));
84 QObject::connect(domain,SIGNAL(updated()),item,SLOT(handleDomainUpdated()));
84 QObject::connect(domain,SIGNAL(updated()),item,SLOT(handleDomainUpdated()));
85 QObject::connect(axis,SIGNAL(visibleChanged(bool)),this,SLOT(handleAxisVisibleChanged(bool)));
85 QObject::connect(axis,SIGNAL(visibleChanged(bool)),this,SLOT(handleAxisVisibleChanged(bool)));
86
86
87 //initialize
87 //initialize
88 domain->emitUpdated();
88 domain->emitUpdated();
89 m_chartTheme->decorate(axis);
89 m_chartTheme->decorate(axis);
90 axis->d_ptr->setDirty(false);
90 axis->d_ptr->setDirty(false);
91 axis->d_ptr->emitUpdated();
91 axis->d_ptr->emitUpdated();
92 if(m_chartsRect.isValid()) item->handleGeometryChanged(m_chartsRect);
92 if(m_chartsRect.isValid()) item->handleGeometryChanged(m_chartsRect);
93
93
94 m_axisItems.insert(axis, item);
94 m_axisItems.insert(axis, item);
95 selectVisibleAxis();
95 selectVisibleAxis();
96 }
96 }
97
97
98 void ChartPresenter::handleAxisRemoved(QAbstractAxis* axis)
98 void ChartPresenter::handleAxisRemoved(QAbstractAxis* axis)
99 {
99 {
100 ChartAxis* item = m_axisItems.take(axis);
100 ChartAxis* item = m_axisItems.take(axis);
101 Q_ASSERT(item);
101 Q_ASSERT(item);
102 selectVisibleAxis();
102 selectVisibleAxis();
103 item->hide();
103 item->hide();
104 item->disconnect();
104 item->disconnect();
105 QObject::disconnect(this,0,item,0);
105 QObject::disconnect(this,0,item,0);
106 item->deleteLater();
106 item->deleteLater();
107 }
107 }
108
108
109
109
110 void ChartPresenter::handleSeriesAdded(QAbstractSeries* series,Domain* domain)
110 void ChartPresenter::handleSeriesAdded(QAbstractSeries* series,Domain* domain)
111 {
111 {
112 ChartElement *item = series->d_ptr->createGraphics(this);
112 ChartElement *item = series->d_ptr->createGraphics(this);
113 Q_ASSERT(item);
113 Q_ASSERT(item);
114 item->setDomain(domain);
114 item->setDomain(domain);
115
115
116 QObject::connect(this,SIGNAL(geometryChanged(QRectF)),item,SLOT(handleGeometryChanged(QRectF)));
116 QObject::connect(this,SIGNAL(geometryChanged(QRectF)),item,SLOT(handleGeometryChanged(QRectF)));
117 QObject::connect(domain,SIGNAL(updated()),item,SLOT(handleDomainUpdated()));
117 QObject::connect(domain,SIGNAL(updated()),item,SLOT(handleDomainUpdated()));
118 //initialize
118 //initialize
119 item->handleDomainUpdated();
119 item->handleDomainUpdated();
120
120
121 if(m_chartsRect.isValid()) item->handleGeometryChanged(m_chartsRect);
121 if(m_chartsRect.isValid()) item->handleGeometryChanged(m_chartsRect);
122 m_chartItems.insert(series,item);
122 m_chartItems.insert(series,item);
123 }
123 }
124
124
125 void ChartPresenter::handleSeriesRemoved(QAbstractSeries* series)
125 void ChartPresenter::handleSeriesRemoved(QAbstractSeries* series)
126 {
126 {
127 ChartElement* item = m_chartItems.take(series);
127 ChartElement* item = m_chartItems.take(series);
128 Q_ASSERT(item);
128 Q_ASSERT(item);
129 item->deleteLater();
129 item->deleteLater();
130 }
130 }
131
131
132 void ChartPresenter::selectVisibleAxis()
132 void ChartPresenter::selectVisibleAxis()
133 {
133 {
134 QMapIterator<QAbstractAxis*, ChartAxis*> i(m_axisItems);
134 QMapIterator<QAbstractAxis*, ChartAxis*> i(m_axisItems);
135
135
136 while (i.hasNext()) {
136 while (i.hasNext()) {
137 i.next();
137 i.next();
138 i.key()->hide();
138 // i.key()->hide();
139 i.key()->show();
139 }
140 }
140
141
141 i.toFront();
142 // i.toFront();
142
143
143 bool axisX=false;
144 // bool axisX=false;
144 bool axisY=false;
145 // bool axisY=false;
145
146
146 while (i.hasNext()) {
147 // while (i.hasNext()) {
147 i.next();
148 // i.next();
148 if(i.key()->orientation()==Qt::Vertical && !axisY) {
149 // if(i.key()->orientation()==Qt::Vertical && !axisY) {
149 axisY=true;
150 // axisY=true;
150 i.key()->show();
151 // i.key()->show();
151 }
152 // }
152 if(i.key()->orientation()==Qt::Horizontal && !axisX) {
153 // if(i.key()->orientation()==Qt::Horizontal && !axisX) {
153 axisX=true;
154 // axisX=true;
154 i.key()->show();
155 // i.key()->show();
155 }
156 // }
156
157
157 }
158 // }
158 }
159 }
159
160
160
161
161 void ChartPresenter::handleAxisVisibleChanged(bool visible)
162 void ChartPresenter::handleAxisVisibleChanged(bool visible)
162 {
163 {
163 QAbstractAxis* axis = static_cast<QAbstractAxis*> (sender());
164 QAbstractAxis* axis = static_cast<QAbstractAxis*> (sender());
164 Q_ASSERT(axis);
165 Q_ASSERT(axis);
165 if(visible){
166 if(visible){
166
167
167 QMapIterator<QAbstractAxis*, ChartAxis*> i(m_axisItems);
168 QMapIterator<QAbstractAxis*, ChartAxis*> i(m_axisItems);
168
169
169 while (i.hasNext()) {
170 while (i.hasNext()) {
170 i.next();
171 i.next();
171 if(i.key()==axis) {
172 if(i.key()==axis) {
172 continue;
173 continue;
173 }
174 }
174 if(i.key()->orientation()==axis->orientation()) {
175 if(i.key()->orientation()==axis->orientation()) {
175 i.key()->setVisible(false);
176 i.key()->setVisible(false);
176 }
177 }
177 }
178 }
178 }
179 }
179 }
180 }
180
181
181 void ChartPresenter::setTheme(QChart::ChartTheme theme,bool force)
182 void ChartPresenter::setTheme(QChart::ChartTheme theme,bool force)
182 {
183 {
183 if(m_chartTheme && m_chartTheme->id() == theme) return;
184 if(m_chartTheme && m_chartTheme->id() == theme) return;
184 delete m_chartTheme;
185 delete m_chartTheme;
185 m_chartTheme = ChartTheme::createTheme(theme);
186 m_chartTheme = ChartTheme::createTheme(theme);
186 m_chartTheme->setForced(force);
187 m_chartTheme->setForced(force);
187 m_chartTheme->decorate(m_chart);
188 m_chartTheme->decorate(m_chart);
188 m_chartTheme->decorate(m_chart->legend());
189 m_chartTheme->decorate(m_chart->legend());
189 resetAllElements();
190 resetAllElements();
190
191
191 // We do not want "force" to stay on.
192 // We do not want "force" to stay on.
192 // Bar/pie are calling decorate when adding/removing slices/bars which means
193 // Bar/pie are calling decorate when adding/removing slices/bars which means
193 // that to preserve users colors "force" must not be on.
194 // that to preserve users colors "force" must not be on.
194 m_chartTheme->setForced(false);
195 m_chartTheme->setForced(false);
195 }
196 }
196
197
197 QChart::ChartTheme ChartPresenter::theme()
198 QChart::ChartTheme ChartPresenter::theme()
198 {
199 {
199 return m_chartTheme->id();
200 return m_chartTheme->id();
200 }
201 }
201
202
202 void ChartPresenter::setAnimationOptions(QChart::AnimationOptions options)
203 void ChartPresenter::setAnimationOptions(QChart::AnimationOptions options)
203 {
204 {
204 if(m_options!=options) {
205 if(m_options!=options) {
205 m_options=options;
206 m_options=options;
206 resetAllElements();
207 resetAllElements();
207 }
208 }
208
209
209 }
210 }
210
211
211 void ChartPresenter::resetAllElements()
212 void ChartPresenter::resetAllElements()
212 {
213 {
213 QMapIterator<QAbstractAxis*, ChartAxis*> i(m_axisItems);
214 QMapIterator<QAbstractAxis*, ChartAxis*> i(m_axisItems);
214 while (i.hasNext()) {
215 while (i.hasNext()) {
215 i.next();
216 i.next();
216 Domain* domain = i.value()->domain();
217 Domain* domain = i.value()->domain();
217 QAbstractAxis* axis = i.key();
218 QAbstractAxis* axis = i.key();
218 handleAxisRemoved(axis);
219 handleAxisRemoved(axis);
219 handleAxisAdded(axis,domain);
220 handleAxisAdded(axis,domain);
220 }
221 }
221
222
222 QMapIterator<QAbstractSeries*, ChartElement*> j(m_chartItems);
223 QMapIterator<QAbstractSeries*, ChartElement*> j(m_chartItems);
223 while (j.hasNext()) {
224 while (j.hasNext()) {
224 j.next();
225 j.next();
225 Domain* domain = j.value()->domain();
226 Domain* domain = j.value()->domain();
226 QAbstractSeries* series = j.key();
227 QAbstractSeries* series = j.key();
227 handleSeriesRemoved(series);
228 handleSeriesRemoved(series);
228 handleSeriesAdded(series,domain);
229 handleSeriesAdded(series,domain);
229 }
230 }
230
231
231 layout()->invalidate();
232 layout()->invalidate();
232 }
233 }
233
234
234 void ChartPresenter::zoomIn(qreal factor)
235 void ChartPresenter::zoomIn(qreal factor)
235 {
236 {
236 QRectF rect = chartsGeometry();
237 QRectF rect = chartsGeometry();
237 rect.setWidth(rect.width()/factor);
238 rect.setWidth(rect.width()/factor);
238 rect.setHeight(rect.height()/factor);
239 rect.setHeight(rect.height()/factor);
239 rect.moveCenter(chartsGeometry().center());
240 rect.moveCenter(chartsGeometry().center());
240 zoomIn(rect);
241 zoomIn(rect);
241 }
242 }
242
243
243 void ChartPresenter::zoomIn(const QRectF& rect)
244 void ChartPresenter::zoomIn(const QRectF& rect)
244 {
245 {
245 QRectF r = rect.normalized();
246 QRectF r = rect.normalized();
246 r.translate(-chartsGeometry().topLeft());
247 r.translate(-chartsGeometry().topLeft());
247 if (!r.isValid())
248 if (!r.isValid())
248 return;
249 return;
249
250
250 m_state = ZoomInState;
251 m_state = ZoomInState;
251 m_statePoint = QPointF(r.center().x()/chartsGeometry().width(),r.center().y()/chartsGeometry().height());
252 m_statePoint = QPointF(r.center().x()/chartsGeometry().width(),r.center().y()/chartsGeometry().height());
252 m_dataset->zoomInDomain(r,chartsGeometry().size());
253 m_dataset->zoomInDomain(r,chartsGeometry().size());
253 m_state = ShowState;
254 m_state = ShowState;
254 }
255 }
255
256
256 void ChartPresenter::zoomOut(qreal factor)
257 void ChartPresenter::zoomOut(qreal factor)
257 {
258 {
258 m_state = ZoomOutState;
259 m_state = ZoomOutState;
259
260
260 QRectF chartRect;
261 QRectF chartRect;
261 chartRect.setSize(chartsGeometry().size());
262 chartRect.setSize(chartsGeometry().size());
262
263
263 QRectF rect;
264 QRectF rect;
264 rect.setSize(chartRect.size()/factor);
265 rect.setSize(chartRect.size()/factor);
265 rect.moveCenter(chartRect.center());
266 rect.moveCenter(chartRect.center());
266 if (!rect.isValid())
267 if (!rect.isValid())
267 return;
268 return;
268 m_statePoint = QPointF(rect.center().x()/chartsGeometry().width(),rect.center().y()/chartsGeometry().height());
269 m_statePoint = QPointF(rect.center().x()/chartsGeometry().width(),rect.center().y()/chartsGeometry().height());
269 m_dataset->zoomOutDomain(rect, chartRect.size());
270 m_dataset->zoomOutDomain(rect, chartRect.size());
270 m_state = ShowState;
271 m_state = ShowState;
271 }
272 }
272
273
273 void ChartPresenter::scroll(qreal dx,qreal dy)
274 void ChartPresenter::scroll(qreal dx,qreal dy)
274 {
275 {
275 if(dx<0) m_state=ScrollLeftState;
276 if(dx<0) m_state=ScrollLeftState;
276 if(dx>0) m_state=ScrollRightState;
277 if(dx>0) m_state=ScrollRightState;
277 if(dy<0) m_state=ScrollUpState;
278 if(dy<0) m_state=ScrollUpState;
278 if(dy>0) m_state=ScrollDownState;
279 if(dy>0) m_state=ScrollDownState;
279
280
280 m_dataset->scrollDomain(dx,dy,chartsGeometry().size());
281 m_dataset->scrollDomain(dx,dy,chartsGeometry().size());
281 m_state = ShowState;
282 m_state = ShowState;
282 }
283 }
283
284
284 QChart::AnimationOptions ChartPresenter::animationOptions() const
285 QChart::AnimationOptions ChartPresenter::animationOptions() const
285 {
286 {
286 return m_options;
287 return m_options;
287 }
288 }
288
289
289 void ChartPresenter::createBackgroundItem()
290 void ChartPresenter::createBackgroundItem()
290 {
291 {
291 if (!m_background) {
292 if (!m_background) {
292 m_background = new ChartBackground(rootItem());
293 m_background = new ChartBackground(rootItem());
293 m_background->setPen(Qt::NoPen);
294 m_background->setPen(Qt::NoPen);
294 m_background->setZValue(ChartPresenter::BackgroundZValue);
295 m_background->setZValue(ChartPresenter::BackgroundZValue);
295 }
296 }
296 }
297 }
297
298
298 void ChartPresenter::createTitleItem()
299 void ChartPresenter::createTitleItem()
299 {
300 {
300 if (!m_title) {
301 if (!m_title) {
301 m_title = new ChartTitle(rootItem());
302 m_title = new ChartTitle(rootItem());
302 m_title->setZValue(ChartPresenter::BackgroundZValue);
303 m_title->setZValue(ChartPresenter::BackgroundZValue);
303 }
304 }
304 }
305 }
305
306
306
307
307 void ChartPresenter::handleAnimationFinished()
308 void ChartPresenter::handleAnimationFinished()
308 {
309 {
309 m_animations.removeAll(qobject_cast<ChartAnimation*>(sender()));
310 m_animations.removeAll(qobject_cast<ChartAnimation*>(sender()));
310 if(m_animations.empty()) emit animationsFinished();
311 if(m_animations.empty()) emit animationsFinished();
311 }
312 }
312
313
313 void ChartPresenter::startAnimation(ChartAnimation* animation)
314 void ChartPresenter::startAnimation(ChartAnimation* animation)
314 {
315 {
315 if (animation->state() != QAbstractAnimation::Stopped) animation->stop();
316 if (animation->state() != QAbstractAnimation::Stopped) animation->stop();
316 QObject::connect(animation, SIGNAL(finished()),this,SLOT(handleAnimationFinished()),Qt::UniqueConnection);
317 QObject::connect(animation, SIGNAL(finished()),this,SLOT(handleAnimationFinished()),Qt::UniqueConnection);
317 if(!m_animations.isEmpty()){
318 if(!m_animations.isEmpty()){
318 m_animations.append(animation);
319 m_animations.append(animation);
319 }
320 }
320 QTimer::singleShot(0, animation, SLOT(start()));
321 QTimer::singleShot(0, animation, SLOT(start()));
321 }
322 }
322
323
323 void ChartPresenter::setBackgroundBrush(const QBrush& brush)
324 void ChartPresenter::setBackgroundBrush(const QBrush& brush)
324 {
325 {
325 createBackgroundItem();
326 createBackgroundItem();
326 m_background->setBrush(brush);
327 m_background->setBrush(brush);
327 m_layout->invalidate();
328 m_layout->invalidate();
328 }
329 }
329
330
330 QBrush ChartPresenter::backgroundBrush() const
331 QBrush ChartPresenter::backgroundBrush() const
331 {
332 {
332 if (!m_background) return QBrush();
333 if (!m_background) return QBrush();
333 return m_background->brush();
334 return m_background->brush();
334 }
335 }
335
336
336 void ChartPresenter::setBackgroundPen(const QPen& pen)
337 void ChartPresenter::setBackgroundPen(const QPen& pen)
337 {
338 {
338 createBackgroundItem();
339 createBackgroundItem();
339 m_background->setPen(pen);
340 m_background->setPen(pen);
340 m_layout->invalidate();
341 m_layout->invalidate();
341 }
342 }
342
343
343 QPen ChartPresenter::backgroundPen() const
344 QPen ChartPresenter::backgroundPen() const
344 {
345 {
345 if (!m_background) return QPen();
346 if (!m_background) return QPen();
346 return m_background->pen();
347 return m_background->pen();
347 }
348 }
348
349
349 void ChartPresenter::setTitle(const QString& title)
350 void ChartPresenter::setTitle(const QString& title)
350 {
351 {
351 createTitleItem();
352 createTitleItem();
352 m_title->setText(title);
353 m_title->setText(title);
353 m_layout->invalidate();
354 m_layout->invalidate();
354 }
355 }
355
356
356 QString ChartPresenter::title() const
357 QString ChartPresenter::title() const
357 {
358 {
358 if (!m_title) return QString();
359 if (!m_title) return QString();
359 return m_title->text();
360 return m_title->text();
360 }
361 }
361
362
362 void ChartPresenter::setTitleFont(const QFont& font)
363 void ChartPresenter::setTitleFont(const QFont& font)
363 {
364 {
364 createTitleItem();
365 createTitleItem();
365 m_title->setFont(font);
366 m_title->setFont(font);
366 m_layout->invalidate();
367 m_layout->invalidate();
367 }
368 }
368
369
369 QFont ChartPresenter::titleFont() const
370 QFont ChartPresenter::titleFont() const
370 {
371 {
371 if (!m_title) return QFont();
372 if (!m_title) return QFont();
372 return m_title->font();
373 return m_title->font();
373 }
374 }
374
375
375 void ChartPresenter::setTitleBrush(const QBrush &brush)
376 void ChartPresenter::setTitleBrush(const QBrush &brush)
376 {
377 {
377 createTitleItem();
378 createTitleItem();
378 m_title->setBrush(brush);
379 m_title->setBrush(brush);
379 m_layout->invalidate();
380 m_layout->invalidate();
380 }
381 }
381
382
382 QBrush ChartPresenter::titleBrush() const
383 QBrush ChartPresenter::titleBrush() const
383 {
384 {
384 if (!m_title) return QBrush();
385 if (!m_title) return QBrush();
385 return m_title->brush();
386 return m_title->brush();
386 }
387 }
387
388
388 void ChartPresenter::setBackgroundVisible(bool visible)
389 void ChartPresenter::setBackgroundVisible(bool visible)
389 {
390 {
390 createBackgroundItem();
391 createBackgroundItem();
391 m_background->setVisible(visible);
392 m_background->setVisible(visible);
392 }
393 }
393
394
394
395
395 bool ChartPresenter::isBackgroundVisible() const
396 bool ChartPresenter::isBackgroundVisible() const
396 {
397 {
397 if (!m_background) return false;
398 if (!m_background) return false;
398 return m_background->isVisible();
399 return m_background->isVisible();
399 }
400 }
400
401
401 void ChartPresenter::setBackgroundDropShadowEnabled(bool enabled)
402 void ChartPresenter::setBackgroundDropShadowEnabled(bool enabled)
402 {
403 {
403 createBackgroundItem();
404 createBackgroundItem();
404 m_background->setDropShadowEnabled(enabled);
405 m_background->setDropShadowEnabled(enabled);
405 }
406 }
406
407
407 bool ChartPresenter::isBackgroundDropShadowEnabled() const
408 bool ChartPresenter::isBackgroundDropShadowEnabled() const
408 {
409 {
409 if (!m_background) return false;
410 if (!m_background) return false;
410 return m_background->isDropShadowEnabled();
411 return m_background->isDropShadowEnabled();
411 }
412 }
412
413
413
414
414 QGraphicsLayout* ChartPresenter::layout()
415 QGraphicsLayout* ChartPresenter::layout()
415 {
416 {
416 return m_layout;
417 return m_layout;
417 }
418 }
418
419
419 void ChartPresenter::setMargins(const QMargins& margins)
420 void ChartPresenter::setMargins(const QMargins& margins)
420 {
421 {
421 m_layout->setMargins(margins);
422 m_layout->setMargins(margins);
422 }
423 }
423
424
424 QMargins ChartPresenter::margins() const
425 QMargins ChartPresenter::margins() const
425 {
426 {
426 return m_layout->margins();
427 return m_layout->margins();
427 }
428 }
428
429
429 QLegend* ChartPresenter::legend()
430 QLegend* ChartPresenter::legend()
430 {
431 {
431 return m_chart->legend();
432 return m_chart->legend();
432 }
433 }
433
434
434 void ChartPresenter::setVisible(bool visible)
435 void ChartPresenter::setVisible(bool visible)
435 {
436 {
436 m_chart->setVisible(visible);
437 m_chart->setVisible(visible);
437 }
438 }
438
439
439 ChartBackground* ChartPresenter::backgroundElement()
440 ChartBackground* ChartPresenter::backgroundElement()
440 {
441 {
441 return m_background;
442 return m_background;
442 }
443 }
443
444
444 QList<ChartAxis*> ChartPresenter::axisItems() const
445 QList<ChartAxis*> ChartPresenter::axisItems() const
445 {
446 {
446 return m_axisItems.values();
447 return m_axisItems.values();
447 }
448 }
448
449
449 ChartTitle* ChartPresenter::titleElement()
450 ChartTitle* ChartPresenter::titleElement()
450 {
451 {
451 return m_title;
452 return m_title;
452 }
453 }
453
454
454 #include "moc_chartpresenter_p.cpp"
455 #include "moc_chartpresenter_p.cpp"
455
456
456 QTCOMMERCIALCHART_END_NAMESPACE
457 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now