##// END OF EJS Templates
Marek Rosa -
r275:e59660aaa64a merge
parent child
Show More
@@ -1,49 +1,49
1 #include <QApplication>
1 #include <QApplication>
2 #include <QMainWindow>
2 #include <QMainWindow>
3 #include <QStandardItemModel>
3 #include <QStandardItemModel>
4 #include <qstackedbarchartseries.h>
4 #include <qstackedbarchartseries.h>
5 #include "chartwidget.h"
5 #include "chartwidget.h"
6 #include <qbarset.h>
6 #include <qbarset.h>
7 #include <qbarcategory.h>
7 #include <qbarcategory.h>
8
8
9 QTCOMMERCIALCHART_USE_NAMESPACE
9 QTCOMMERCIALCHART_USE_NAMESPACE
10
10
11 int main(int argc, char *argv[])
11 int main(int argc, char *argv[])
12 {
12 {
13 QApplication a(argc, argv);
13 QApplication a(argc, argv);
14 QMainWindow window;
14 QMainWindow window;
15
15
16 QBarCategory *category = new QBarCategory;
16 QBarCategory *category = new QBarCategory;
17 *category << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "June" << "Jul" << "Aug" << "Sep" << "Oct" << "Nov" << "Dec";
17 *category << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "June" << "Jul" << "Aug" << "Sep" << "Oct" << "Nov" << "Dec";
18
18
19 QStackedBarChartSeries* series0 = new QStackedBarChartSeries(category);
19 QStackedBarChartSeries* series0 = new QStackedBarChartSeries(category);
20
20
21 QBarSet *set0 = new QBarSet;
21 QBarSet *set0 = new QBarSet;
22 QBarSet *set1 = new QBarSet;
22 QBarSet *set1 = new QBarSet;
23 QBarSet *set2 = new QBarSet;
23 QBarSet *set2 = new QBarSet;
24 QBarSet *set3 = new QBarSet;
24 QBarSet *set3 = new QBarSet;
25 QBarSet *set4 = new QBarSet;
25 QBarSet *set4 = new QBarSet;
26
26
27 // Create some test data to chart
27 // Create some test data to chart
28 *set0 << 1 << 2 << 3 << 4 << 5 << 6 << 7 << 8 << 9 << 10 << 11 << 12;
28 *set0 << 1 << 2 << 3 << 4 << 5 << 6 << 7 << 8 << 9 << 10 << 11 << 12;
29 *set1 << 5 << 0 << 0 << 4 << 0 << 7 << 8 << 9 << 9 << 0 << 4 << 2;
29 // *set1 << 5 << 0 << 0 << 4 << 0 << 7 << 8 << 9 << 9 << 0 << 4 << 2;
30 *set2 << 3 << 5 << 8 << 13 << 8 << 5 << 3 << 2 << 1 << 1 << 3 << 5;
30 // *set2 << 3 << 5 << 8 << 13 << 8 << 5 << 3 << 2 << 1 << 1 << 3 << 5;
31 *set3 << 5 << 6 << 7 << 3 << 4 << 5 << 8 << 9 << 10 << 5 << 2 << 7;
31 // *set3 << 5 << 6 << 7 << 3 << 4 << 5 << 8 << 9 << 10 << 5 << 2 << 7;
32 *set4 << 9 << 7 << 5 << 3 << 1 << 2 << 4 << 6 << 8 << 10 << 1 << 6;
32 *set4 << 9 << 7 << 5 << 3 << 1 << 2 << 4 << 6 << 8 << 10 << 1 << 6;
33
33
34 series0->addBarSet(set0);
34 series0->addBarSet(set0);
35 series0->addBarSet(set1);
35 // series0->addBarSet(set1);
36 series0->addBarSet(set2);
36 // series0->addBarSet(set2);
37 series0->addBarSet(set3);
37 // series0->addBarSet(set3);
38 series0->addBarSet(set4);
38 series0->addBarSet(set4);
39
39
40 ChartWidget* chartWidget = new ChartWidget(&window);
40 ChartWidget* chartWidget = new ChartWidget(&window);
41 chartWidget->addSeries(series0);
41 chartWidget->addSeries(series0);
42
42
43 window.setCentralWidget(chartWidget);
43 window.setCentralWidget(chartWidget);
44 window.resize(400, 300);
44 window.resize(400, 300);
45 window.show();
45 window.show();
46
46
47 return a.exec();
47 return a.exec();
48 }
48 }
49
49
@@ -1,300 +1,317
1 #include "axisitem_p.h"
1 #include "axisitem_p.h"
2 #include "qchartaxis.h"
2 #include "qchartaxis.h"
3 #include "chartpresenter_p.h"
3 #include "chartpresenter_p.h"
4 #include <QPainter>
4 #include <QPainter>
5 #include <QDebug>
5 #include <QDebug>
6
6
7 static int label_padding = 5;
7 static int label_padding = 5;
8
8
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10
10
11 AxisItem::AxisItem(AxisType type,QGraphicsItem* parent) :
11 AxisItem::AxisItem(AxisType type,QGraphicsItem* parent) :
12 ChartItem(parent),
12 ChartItem(parent),
13 m_type(type),
13 m_type(type),
14 m_labelsAngle(0),
14 m_labelsAngle(0),
15 m_shadesEnabled(true),
15 m_shadesEnabled(true),
16 m_grid(parent),
16 m_grid(parent),
17 m_shades(parent),
17 m_shades(parent),
18 m_labels(parent),
18 m_labels(parent),
19 m_origin(0,0)
19 m_axis(parent)
20 {
20 {
21 //initial initialization
21 //initial initialization
22 m_axis.setZValue(ChartPresenter::AxisZValue);
22 m_shades.setZValue(ChartPresenter::ShadesZValue);
23 m_shades.setZValue(ChartPresenter::ShadesZValue);
23 m_grid.setZValue(ChartPresenter::GridZValue);
24 m_grid.setZValue(ChartPresenter::GridZValue);
25 setFlags(QGraphicsItem::ItemHasNoContents);
24 }
26 }
25
27
26 AxisItem::~AxisItem()
28 AxisItem::~AxisItem()
27 {
29 {
28 }
30 }
29
31
30 QRectF AxisItem::boundingRect() const
32 QRectF AxisItem::boundingRect() const
31 {
33 {
32 return m_rect;
34 return m_rect;
33 }
35 }
34
36
35 void AxisItem::createItems(int count)
37 void AxisItem::createItems(int count)
36 {
38 {
39 m_axis.addToGroup(new QGraphicsLineItem(this));
37 for (int i = 0; i < count; ++i) {
40 for (int i = 0; i < count; ++i) {
38 m_grid.addToGroup(new QGraphicsLineItem(this));
41 m_grid.addToGroup(new QGraphicsLineItem(this));
39 m_labels.addToGroup(new QGraphicsSimpleTextItem(this));
42 m_labels.addToGroup(new QGraphicsSimpleTextItem(this));
40 if(i%2) m_shades.addToGroup(new QGraphicsRectItem(this));
43 if(i%2) m_shades.addToGroup(new QGraphicsRectItem(this));
44 m_axis.addToGroup(new QGraphicsLineItem(this));
41 }
45 }
42 }
46 }
43
47
44 void AxisItem::clear()
48 void AxisItem::clear()
45 {
49 {
46 foreach(QGraphicsItem* item , m_shades.childItems()) {
50 foreach(QGraphicsItem* item , m_shades.childItems()) {
47 delete item;
51 delete item;
48 }
52 }
49
53
50 foreach(QGraphicsItem* item , m_grid.childItems()) {
54 foreach(QGraphicsItem* item , m_grid.childItems()) {
51 delete item;
55 delete item;
52 }
56 }
53
57
54 foreach(QGraphicsItem* item , m_labels.childItems()) {
58 foreach(QGraphicsItem* item , m_labels.childItems()) {
55 delete item;
59 delete item;
56 }
60 }
57
61
62 foreach(QGraphicsItem* item , m_axis.childItems()) {
63 delete item;
64 }
65
58 m_thicksList.clear();
66 m_thicksList.clear();
59
67
60 }
68 }
61
69
62 void AxisItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
70 void AxisItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
63 {
71 {
64
72
65 }
73 }
66
74
67 void AxisItem::updateItem(int count)
75 void AxisItem::updateItem(int count)
68 {
76 {
77 if(count ==0) return;
69
78
70 QList<QGraphicsItem *> lines = m_grid.childItems();
79 QList<QGraphicsItem *> lines = m_grid.childItems();
71 QList<QGraphicsItem *> labels = m_labels.childItems();
80 QList<QGraphicsItem *> labels = m_labels.childItems();
72 QList<QGraphicsItem *> shades = m_shades.childItems();
81 QList<QGraphicsItem *> shades = m_shades.childItems();
82 QList<QGraphicsItem *> axis = m_axis.childItems();
73
83
74 switch (m_type)
84 switch (m_type)
75 {
85 {
76 case X_AXIS:
86 case X_AXIS:
77 {
87 {
78 const qreal deltaX = m_rect.width() / (count-1);
88 const qreal deltaX = m_rect.width() / (count-1);
79
89
80 m_axis.setLine(m_rect.left(), m_rect.bottom(), m_rect.right(), m_rect.bottom());
90 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(axis.at(0));
91 lineItem->setLine(m_rect.left(), m_rect.bottom(), m_rect.right(), m_rect.bottom());
81
92
82 for (int i = 0; i < count; ++i) {
93 for (int i = 0; i < count; ++i) {
83 int x = i * deltaX + m_rect.left();
94 int x = i * deltaX + m_rect.left();
84 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i));
95 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i));
85 lineItem->setLine(x, m_rect.top(), x, m_rect.bottom());
96 lineItem->setLine(x, m_rect.top(), x, m_rect.bottom());
86 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i));
97 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i));
87 labelItem->setText(m_thicksList.at(i));
98 labelItem->setText(m_thicksList.at(i));
88 QPointF center = labelItem->boundingRect().center();
99 QPointF center = labelItem->boundingRect().center();
89 labelItem->setTransformOriginPoint(center.x(), center.y());
100 labelItem->setTransformOriginPoint(center.x(), center.y());
90 labelItem->setPos(x - center.x(), m_rect.bottom() + label_padding);
101 labelItem->setPos(x - center.x(), m_rect.bottom() + label_padding);
91
92 if(i%2){
102 if(i%2){
93 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2));
103 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2));
94 rectItem->setRect(x,m_rect.top(),deltaX,m_rect.height());
104 rectItem->setRect(x,m_rect.top(),deltaX,m_rect.height());
95 }
105 }
106 lineItem = static_cast<QGraphicsLineItem*>(axis.at(i+1));
107 lineItem->setLine(x,m_rect.bottom(),x,m_rect.bottom()+5);
96 }
108 }
97 }
109 }
98 break;
110 break;
99
111
100 case Y_AXIS:
112 case Y_AXIS:
101 {
113 {
102 const qreal deltaY = m_rect.height()/ (count-1);
114 const qreal deltaY = m_rect.height()/ (count-1);
103
115
104 m_axis.setLine(m_rect.left() , m_rect.top(), m_rect.left(), m_rect.bottom());
116 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(axis.at(0));
117 lineItem->setLine(m_rect.left() , m_rect.top(), m_rect.left(), m_rect.bottom());
105
118
106 for (int i = 0; i < count; ++i) {
119 for (int i = 0; i < count; ++i) {
107 int y = i * -deltaY + m_rect.bottom();
120 int y = i * -deltaY + m_rect.bottom();
108 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i));
121 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i));
109 lineItem->setLine(m_rect.left() , y, m_rect.right(), y);
122 lineItem->setLine(m_rect.left() , y, m_rect.right(), y);
110 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i));
123 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i));
111 labelItem->setText(m_thicksList.at(i));
124 labelItem->setText(m_thicksList.at(i));
112 QPointF center = labelItem->boundingRect().center();
125 QPointF center = labelItem->boundingRect().center();
113 labelItem->setTransformOriginPoint(center.x(), center.y());
126 labelItem->setTransformOriginPoint(center.x(), center.y());
114 labelItem->setPos(m_rect.left() - labelItem->boundingRect().width() - label_padding , y-center.y());
127 labelItem->setPos(m_rect.left() - labelItem->boundingRect().width() - label_padding , y-center.y());
115 if(i%2){
128 if(i%2){
116 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2));
129 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2));
117 rectItem->setRect(m_rect.left(),y,m_rect.width(),deltaY);
130 rectItem->setRect(m_rect.left(),y,m_rect.width(),deltaY);
118 }
131 }
132 lineItem = static_cast<QGraphicsLineItem*>(axis.at(i+1));
133 lineItem->setLine(m_rect.left()-5,y,m_rect.left(),y);
119 }
134 }
120 }
135 }
121 break;
136 break;
122 default:
137 default:
123 qDebug()<<"Unknown axis type";
138 qDebug()<<"Unknown axis type";
124 break;
139 break;
125 }
140 }
126 }
141 }
127
142
128 void AxisItem::handleAxisUpdate(QChartAxis* axis)
143 void AxisItem::handleAxisUpdate(QChartAxis* axis)
129 {
144 {
130 if(axis->isAxisVisible()) {
145 if(axis->isAxisVisible()) {
131 setAxisOpacity(100);
146 setAxisOpacity(100);
132 }
147 }
133 else {
148 else {
134 setAxisOpacity(0);
149 setAxisOpacity(0);
135 }
150 }
136
151
137 if(axis->isGridVisible()) {
152 if(axis->isGridVisible()) {
138 setGridOpacity(100);
153 setGridOpacity(100);
139 }
154 }
140 else {
155 else {
141 setGridOpacity(0);
156 setGridOpacity(0);
142 }
157 }
143
158
144 if(axis->isLabelsVisible())
159 if(axis->isLabelsVisible())
145 {
160 {
146 setLabelsOpacity(100);
161 setLabelsOpacity(100);
147 }
162 }
148 else {
163 else {
149 setLabelsOpacity(0);
164 setLabelsOpacity(0);
150 }
165 }
151
166
152 if(axis->isShadesVisible()) {
167 if(axis->isShadesVisible()) {
153 setShadesOpacity(axis->shadesOpacity());
168 setShadesOpacity(axis->shadesOpacity());
154 }
169 }
155 else {
170 else {
156 setShadesOpacity(0);
171 setShadesOpacity(0);
157 }
172 }
158
173
159 setLabelsAngle(axis->labelsAngle());
174 setLabelsAngle(axis->labelsAngle());
160 setAxisPen(axis->axisPen());
175 setAxisPen(axis->axisPen());
161 setLabelsPen(axis->labelsPen());
176 setLabelsPen(axis->labelsPen());
162 setLabelsBrush(axis->labelsBrush());
177 setLabelsBrush(axis->labelsBrush());
163 setLabelsFont(axis->labelFont());
178 setLabelsFont(axis->labelFont());
164 setGridPen(axis->gridPen());
179 setGridPen(axis->gridPen());
165 setShadesPen(axis->shadesPen());
180 setShadesPen(axis->shadesPen());
166 setShadesBrush(axis->shadesBrush());
181 setShadesBrush(axis->shadesBrush());
167 }
182 }
168
183
169 void AxisItem::handleLabelsChanged(QChartAxis* axis,const QStringList& labels)
184 void AxisItem::handleLabelsChanged(QChartAxis* axis,const QStringList& labels)
170 {
185 {
171 m_thicksList=labels;
186 m_thicksList=labels;
172 QList<QGraphicsItem*> items = m_labels.childItems();
187 QList<QGraphicsItem*> items = m_labels.childItems();
173 if(items.size()!=m_thicksList.size()){
188 //if(items.size()!=m_thicksList.size()){
174 clear();
189 clear();
175 m_thicksList=labels;
190 m_thicksList=labels;
176 createItems(m_thicksList.size());
191 createItems(m_thicksList.size());
177 updateItem(m_thicksList.size());
192 updateItem(m_thicksList.size());
178 items = m_labels.childItems();
193 items = m_labels.childItems();
179 handleAxisUpdate(axis);
194 handleAxisUpdate(axis);
180 }
195 // }
181
196
182 Q_ASSERT(items.size()==m_thicksList.size());
197 Q_ASSERT(items.size()==m_thicksList.size());
183
198
184 int i=0;
199 int i=0;
185 foreach(QGraphicsItem* item, items){
200 foreach(QGraphicsItem* item, items){
186 static_cast<QGraphicsSimpleTextItem*>(item)->setText(m_thicksList.at(i));
201 static_cast<QGraphicsSimpleTextItem*>(item)->setText(m_thicksList.at(i));
187 i++;
202 i++;
188 }
203 }
189 update();
204 update();
190 }
205 }
191
206
192 void AxisItem::handleGeometryChanged(const QRectF& rect)
207 void AxisItem::handleGeometryChanged(const QRectF& rect)
193 {
208 {
194 m_rect = rect;
209 m_rect = rect;
195 updateItem(m_thicksList.size());
210 updateItem(m_thicksList.size());
196 update();
211 update();
197 }
212 }
198
213
199 void AxisItem::setAxisOpacity(qreal opacity)
214 void AxisItem::setAxisOpacity(qreal opacity)
200 {
215 {
201 m_axis.setOpacity(opacity);
216 m_axis.setOpacity(opacity);
202 }
217 }
203
218
204 qreal AxisItem::axisOpacity() const
219 qreal AxisItem::axisOpacity() const
205 {
220 {
206 return m_axis.opacity();
221 return m_axis.opacity();
207 }
222 }
208
223
209 void AxisItem::setGridOpacity(qreal opacity)
224 void AxisItem::setGridOpacity(qreal opacity)
210 {
225 {
211 m_grid.setOpacity(opacity);
226 m_grid.setOpacity(opacity);
212 }
227 }
213
228
214 qreal AxisItem::gridOpacity() const
229 qreal AxisItem::gridOpacity() const
215 {
230 {
216 return m_grid.opacity();
231 return m_grid.opacity();
217 }
232 }
218
233
219 void AxisItem::setLabelsOpacity(qreal opacity)
234 void AxisItem::setLabelsOpacity(qreal opacity)
220 {
235 {
221 m_labels.setOpacity(opacity);
236 m_labels.setOpacity(opacity);
222 }
237 }
223
238
224 qreal AxisItem::labelsOpacity() const
239 qreal AxisItem::labelsOpacity() const
225 {
240 {
226 return m_labels.opacity();
241 return m_labels.opacity();
227 }
242 }
228
243
229 void AxisItem::setShadesOpacity(qreal opacity)
244 void AxisItem::setShadesOpacity(qreal opacity)
230 {
245 {
231 m_shades.setOpacity(opacity);
246 m_shades.setOpacity(opacity);
232 }
247 }
233
248
234 qreal AxisItem::shadesOpacity() const
249 qreal AxisItem::shadesOpacity() const
235 {
250 {
236 return m_shades.opacity();
251 return m_shades.opacity();
237 }
252 }
238
253
239 void AxisItem::setLabelsAngle(int angle)
254 void AxisItem::setLabelsAngle(int angle)
240 {
255 {
241 foreach(QGraphicsItem* item , m_labels.childItems()) {
256 foreach(QGraphicsItem* item , m_labels.childItems()) {
242 QPointF center = item->boundingRect().center();
257 QPointF center = item->boundingRect().center();
243 item->setRotation(angle);
258 item->setRotation(angle);
244 }
259 }
245
260
246 m_labelsAngle=angle;
261 m_labelsAngle=angle;
247 }
262 }
248
263
249 void AxisItem::setLabelsPen(const QPen& pen)
264 void AxisItem::setLabelsPen(const QPen& pen)
250 {
265 {
251 foreach(QGraphicsItem* item , m_labels.childItems()) {
266 foreach(QGraphicsItem* item , m_labels.childItems()) {
252 static_cast<QGraphicsSimpleTextItem*>(item)->setPen(pen);
267 static_cast<QGraphicsSimpleTextItem*>(item)->setPen(pen);
253 }
268 }
254 }
269 }
255
270
256 void AxisItem::setLabelsBrush(const QBrush& brush)
271 void AxisItem::setLabelsBrush(const QBrush& brush)
257 {
272 {
258 foreach(QGraphicsItem* item , m_labels.childItems()) {
273 foreach(QGraphicsItem* item , m_labels.childItems()) {
259 static_cast<QGraphicsSimpleTextItem*>(item)->setBrush(brush);
274 static_cast<QGraphicsSimpleTextItem*>(item)->setBrush(brush);
260 }
275 }
261 }
276 }
262
277
263 void AxisItem::setLabelsFont(const QFont& font)
278 void AxisItem::setLabelsFont(const QFont& font)
264 {
279 {
265 foreach(QGraphicsItem* item , m_labels.childItems()) {
280 foreach(QGraphicsItem* item , m_labels.childItems()) {
266 static_cast<QGraphicsSimpleTextItem*>(item)->setFont(font);
281 static_cast<QGraphicsSimpleTextItem*>(item)->setFont(font);
267 }
282 }
268 }
283 }
269
284
270 void AxisItem::setShadesBrush(const QBrush& brush)
285 void AxisItem::setShadesBrush(const QBrush& brush)
271 {
286 {
272 foreach(QGraphicsItem* item , m_shades.childItems()) {
287 foreach(QGraphicsItem* item , m_shades.childItems()) {
273 static_cast<QGraphicsRectItem*>(item)->setBrush(brush);
288 static_cast<QGraphicsRectItem*>(item)->setBrush(brush);
274 }
289 }
275 }
290 }
276
291
277 void AxisItem::setShadesPen(const QPen& pen)
292 void AxisItem::setShadesPen(const QPen& pen)
278 {
293 {
279 foreach(QGraphicsItem* item , m_shades.childItems()) {
294 foreach(QGraphicsItem* item , m_shades.childItems()) {
280 static_cast<QGraphicsRectItem*>(item)->setPen(pen);
295 static_cast<QGraphicsRectItem*>(item)->setPen(pen);
281 }
296 }
282 }
297 }
283
298
284 void AxisItem::setAxisPen(const QPen& pen)
299 void AxisItem::setAxisPen(const QPen& pen)
285 {
300 {
286 m_axis.setPen(pen);
301 foreach(QGraphicsItem* item , m_axis.childItems()) {
302 static_cast<QGraphicsLineItem*>(item)->setPen(pen);
303 }
287 }
304 }
288
305
289 void AxisItem::setGridPen(const QPen& pen)
306 void AxisItem::setGridPen(const QPen& pen)
290 {
307 {
291 foreach(QGraphicsItem* item , m_grid.childItems()) {
308 foreach(QGraphicsItem* item , m_grid.childItems()) {
292 static_cast<QGraphicsLineItem*>(item)->setPen(pen);
309 static_cast<QGraphicsLineItem*>(item)->setPen(pen);
293 }
310 }
294 }
311 }
295
312
296
313
297 //TODO "nice numbers algorithm"
314 //TODO "nice numbers algorithm"
298 #include "moc_axisitem_p.cpp"
315 #include "moc_axisitem_p.cpp"
299
316
300 QTCOMMERCIALCHART_END_NAMESPACE
317 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,77 +1,76
1 #ifndef AXISITEM_H_
1 #ifndef AXISITEM_H_
2 #define AXISITEM_H_
2 #define AXISITEM_H_
3
3
4 #include "domain_p.h"
4 #include "domain_p.h"
5 #include "chartitem_p.h"
5 #include "chartitem_p.h"
6 #include <QGraphicsItem>
6 #include <QGraphicsItem>
7
7
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9
9
10 class QChartAxis;
10 class QChartAxis;
11
11
12 class AxisItem : public QObject, public ChartItem
12 class AxisItem : public QObject, public ChartItem
13 {
13 {
14 Q_OBJECT
14 Q_OBJECT
15 public:
15 public:
16 enum AxisType{X_AXIS,Y_AXIS};
16 enum AxisType{X_AXIS,Y_AXIS};
17
17
18 AxisItem(AxisType type = X_AXIS,QGraphicsItem* parent = 0);
18 AxisItem(AxisType type = X_AXIS,QGraphicsItem* parent = 0);
19 ~AxisItem();
19 ~AxisItem();
20
20
21 //from QGraphicsItem
21 //from QGraphicsItem
22 QRectF boundingRect() const;
22 QRectF boundingRect() const;
23 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
23 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
24
24
25 AxisType axisType() const {return m_type;};
25 AxisType axisType() const {return m_type;};
26
26
27 void setAxisOpacity(qreal opacity);
27 void setAxisOpacity(qreal opacity);
28 qreal axisOpacity() const;
28 qreal axisOpacity() const;
29
29
30 void setGridOpacity(qreal opacity);
30 void setGridOpacity(qreal opacity);
31 qreal gridOpacity() const;
31 qreal gridOpacity() const;
32
32
33 void setLabelsOpacity(qreal opacity);
33 void setLabelsOpacity(qreal opacity);
34 qreal labelsOpacity() const;
34 qreal labelsOpacity() const;
35
35
36 void setShadesOpacity(qreal opacity);
36 void setShadesOpacity(qreal opacity);
37 qreal shadesOpacity() const;
37 qreal shadesOpacity() const;
38
38
39 void setLabelsAngle(int angle);
39 void setLabelsAngle(int angle);
40 int labelsAngle()const { return m_labelsAngle; }
40 int labelsAngle()const { return m_labelsAngle; }
41
41
42 void setShadesBrush(const QBrush& brush);
42 void setShadesBrush(const QBrush& brush);
43 void setShadesPen(const QPen& pen);
43 void setShadesPen(const QPen& pen);
44
44
45 void setAxisPen(const QPen& pen);
45 void setAxisPen(const QPen& pen);
46 void setGridPen(const QPen& pen);
46 void setGridPen(const QPen& pen);
47
47
48 void setLabelsPen(const QPen& pen);
48 void setLabelsPen(const QPen& pen);
49 void setLabelsBrush(const QBrush& brush);
49 void setLabelsBrush(const QBrush& brush);
50 void setLabelsFont(const QFont& font);
50 void setLabelsFont(const QFont& font);
51
51
52 public slots:
52 public slots:
53 void handleAxisUpdate(QChartAxis* axis); //look and feel
53 void handleAxisUpdate(QChartAxis* axis); //look and feel
54 void handleLabelsChanged(QChartAxis* axis,const QStringList& labels); //labels from dataset
54 void handleLabelsChanged(QChartAxis* axis,const QStringList& labels); //labels from dataset
55 void handleGeometryChanged(const QRectF& size); // geometry from presenter
55 void handleGeometryChanged(const QRectF& size); // geometry from presenter
56 protected:
56 protected:
57 void updateItem(int count);
57 void updateItem(int count);
58 private:
58 private:
59 void clear();
59 void clear();
60 void createItems(int count);
60 void createItems(int count);
61 private:
61 private:
62 AxisType m_type;
62 AxisType m_type;
63 QRectF m_rect;
63 QRectF m_rect;
64 int m_labelsAngle;
64 int m_labelsAngle;
65 bool m_shadesEnabled;
65 bool m_shadesEnabled;
66 QGraphicsItemGroup m_grid;
66 QGraphicsItemGroup m_grid;
67 QGraphicsItemGroup m_shades;
67 QGraphicsItemGroup m_shades;
68 QGraphicsItemGroup m_labels;
68 QGraphicsItemGroup m_labels;
69 QGraphicsLineItem m_axis;
69 QGraphicsItemGroup m_axis;
70 QStringList m_thicksList;
70 QStringList m_thicksList;
71 QPointF m_origin;
72
71
73 };
72 };
74
73
75 QTCOMMERCIALCHART_END_NAMESPACE
74 QTCOMMERCIALCHART_END_NAMESPACE
76
75
77 #endif /* AXISITEM_H_ */
76 #endif /* AXISITEM_H_ */
@@ -1,77 +1,77
1 #include "bar_p.h"
1 #include "bar_p.h"
2 #include <QDebug>
2 #include <QDebug>
3 #include <QPainter>
3 #include <QPainter>
4
4
5 QTCOMMERCIALCHART_BEGIN_NAMESPACE
5 QTCOMMERCIALCHART_BEGIN_NAMESPACE
6
6
7 Bar::Bar(QGraphicsItem *parent)
7 Bar::Bar(QGraphicsItem *parent)
8 : QGraphicsObject(parent)
8 : QGraphicsObject(parent)
9 {
9 {
10 setAcceptedMouseButtons(Qt::LeftButton);
10 setAcceptedMouseButtons(Qt::LeftButton);
11 }
11 }
12
12
13 void Bar::setSize(const QSizeF& size)
13 void Bar::setSize(const QSizeF& size)
14 {
14 {
15 mWidth = size.width();
15 mWidth = size.width();
16 mHeight = size.height();
16 mHeight = size.height();
17 }
17 }
18
18
19
19
20 void Bar::resize( qreal w, qreal h )
20 void Bar::resize( qreal w, qreal h )
21 {
21 {
22 mWidth = w;
22 mWidth = w;
23 mHeight = h;
23 mHeight = h;
24 }
24 }
25
25
26 void Bar::setColor( QColor col )
26 void Bar::setColor( QColor col )
27 {
27 {
28 mColor = col;
28 mColor = col;
29 }
29 }
30
30
31 void Bar::setPos(qreal x, qreal y)
31 void Bar::setPos(qreal x, qreal y)
32 {
32 {
33 mXpos = x;
33 mXpos = x;
34 mYpos = y;
34 mYpos = y;
35 }
35 }
36
36
37 void Bar::setPen(QPen pen)
37 void Bar::setPen(QPen pen)
38 {
38 {
39 mPen = pen;
39 mPen = pen;
40 }
40 }
41
41
42 void Bar::setBrush(QBrush brush)
42 void Bar::setBrush(QBrush brush)
43 {
43 {
44 mBrush = brush;
44 mBrush = brush;
45 }
45 }
46
46
47 void Bar::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
47 void Bar::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
48 {
48 {
49 if (0 == mHeight) {
49 if (0 == mHeight) {
50 return;
50 return;
51 }
51 }
52 painter->setBrush(mBrush);
52 painter->setBrush(mBrush);
53
53
54 // This compensates for rounding errors. drawRect takes ints and cumulative error of pos + size may be over 1.
54 // This compensates for rounding errors. drawRect takes ints and cumulative error of pos + size may be over 1.
55 int x0 = mXpos;
55 int x0 = mXpos;
56 int x1 = (mXpos + mWidth);
56 int x1 = (mXpos + mWidth);
57 int w = x1-x0;
57 int w = x1-x0;
58 int y0 = mYpos;
58 int y0 = mYpos;
59 int y1 = (mYpos + mHeight);
59 int y1 = (mYpos + mHeight);
60 int h = y1-y0;
60 int h = y1-y0;
61 painter->drawRect(x0, y0 ,w ,h);
61 painter->drawRect(x0, y0 ,w ,h);
62 }
62 }
63
63
64 QRectF Bar::boundingRect() const
64 QRectF Bar::boundingRect() const
65 {
65 {
66 QRectF r(mXpos, mYpos, mXpos + mWidth, mYpos + mHeight);
66 QRectF r(mXpos, mYpos, mWidth, mHeight);
67 return r;
67 return r;
68 }
68 }
69
69
70 void Bar::mousePressEvent(QGraphicsSceneMouseEvent* /*event*/)
70 void Bar::mousePressEvent(QGraphicsSceneMouseEvent* /*event*/)
71 {
71 {
72 emit clicked();
72 emit clicked();
73 }
73 }
74
74
75 #include "moc_bar_p.cpp"
75 #include "moc_bar_p.cpp"
76
76
77 QTCOMMERCIALCHART_END_NAMESPACE
77 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,191 +1,190
1 #include <limits.h>
1 #include <limits.h>
2 #include <QVector>
2 #include <QVector>
3 #include <QDebug>
3 #include <QDebug>
4 #include "barchartmodel_p.h"
4 #include "barchartmodel_p.h"
5 #include "qbarcategory.h"
5 #include "qbarcategory.h"
6 #include "qbarset.h"
6 #include "qbarset.h"
7
7
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9
9
10 BarChartModel::BarChartModel(QBarCategory *category, QObject *parent) :
10 BarChartModel::BarChartModel(QBarCategory *category, QObject *parent) :
11 QObject(parent)
11 QObject(parent)
12 ,mCategory(category)
12 ,mCategory(category)
13 {
13 {
14 }
14 }
15
15
16 BarChartModel::~BarChartModel()
16 BarChartModel::~BarChartModel()
17 {
17 {
18 delete mCategory;
18 delete mCategory;
19 }
19 }
20
20
21
21
22 QBarCategory& BarChartModel::category()
22 QBarCategory& BarChartModel::category()
23 {
23 {
24 return *mCategory;
24 return *mCategory;
25 }
25 }
26
26
27 void BarChartModel::addBarSet(QBarSet *set)
27 void BarChartModel::addBarSet(QBarSet *set)
28 {
28 {
29 mDataModel.append(set);
29 mDataModel.append(set);
30 }
30 }
31
31
32 void BarChartModel::removeBarSet(QBarSet *set)
32 void BarChartModel::removeBarSet(QBarSet *set)
33 {
33 {
34 if (mDataModel.contains(set)) {
34 if (mDataModel.contains(set)) {
35 mDataModel.removeOne(set);
35 mDataModel.removeOne(set);
36 }
36 }
37 }
37 }
38
38
39 QBarSet* BarChartModel::nextSet(bool getFirst)
39 QBarSet* BarChartModel::nextSet(bool getFirst)
40 {
40 {
41 if (getFirst) {
41 if (getFirst) {
42 mCurrentSet = 0;
42 mCurrentSet = 0;
43 }
43 }
44
44
45 if ((mDataModel.count() <= 0) || (mDataModel.count() <= mCurrentSet)) {
45 if ((mDataModel.count() <= 0) || (mDataModel.count() <= mCurrentSet)) {
46 return 0;
46 return 0;
47 }
47 }
48
48
49 QBarSet* set = mDataModel.at(mCurrentSet);
49 QBarSet* set = mDataModel.at(mCurrentSet);
50 mCurrentSet++;
50 mCurrentSet++;
51 return set;
51 return set;
52 }
52 }
53
53
54
54 QBarSet* BarChartModel::setAt(int index)
55 QBarSet& BarChartModel::setAt(int index)
56 {
55 {
57 return *mDataModel.at(index);
56 return mDataModel.at(index);
58 }
57 }
59
58
60 int BarChartModel::countSets()
59 int BarChartModel::countSets()
61 {
60 {
62 return mDataModel.count();
61 return mDataModel.count();
63 }
62 }
64
63
65 int BarChartModel::countCategories()
64 int BarChartModel::countCategories()
66 {
65 {
67 int count(0);
66 int count(0);
68 for (int i=0; i<mDataModel.count(); i++){
67 for (int i=0; i<mDataModel.count(); i++){
69 // TODO: can we assume that all series have same number of values? If not. then which values are empty?
68 // TODO: can we assume that all series have same number of values? If not. then which values are empty?
70 int temp = mDataModel.at(i)->count();
69 int temp = mDataModel.at(i)->count();
71 if (temp > count) {
70 if (temp > count) {
72 count = temp;
71 count = temp;
73 }
72 }
74 }
73 }
75 return count;
74 return count;
76 }
75 }
77
76
78 int BarChartModel::countTotalItems()
77 int BarChartModel::countTotalItems()
79 {
78 {
80 int total = mDataModel.count() * countCategories();
79 int total = mDataModel.count() * countCategories();
81 return total;
80 return total;
82 }
81 }
83
82
84 qreal BarChartModel::min()
83 qreal BarChartModel::min()
85 {
84 {
86 Q_ASSERT(mDataModel.count() > 0);
85 Q_ASSERT(mDataModel.count() > 0);
87 // TODO: make min and max members and update them when data changes.
86 // TODO: make min and max members and update them when data changes.
88 // This is slower since they are checked every time, even if data is same since previous call.
87 // This is slower since they are checked every time, even if data is same since previous call.
89 qreal min = INT_MAX;
88 qreal min = INT_MAX;
90
89
91 for (int i=0; i <mDataModel.count(); i++) {
90 for (int i=0; i <mDataModel.count(); i++) {
92 int itemCount = mDataModel.at(i)->count();
91 int itemCount = mDataModel.at(i)->count();
93 for (int j=0; j<itemCount; j++) {
92 for (int j=0; j<itemCount; j++) {
94 qreal temp = mDataModel.at(i)->valueAt(j);
93 qreal temp = mDataModel.at(i)->valueAt(j);
95 if (temp < min) {
94 if (temp < min) {
96 min = temp;
95 min = temp;
97 }
96 }
98 }
97 }
99 }
98 }
100 return min;
99 return min;
101 }
100 }
102
101
103 qreal BarChartModel::max()
102 qreal BarChartModel::max()
104 {
103 {
105 Q_ASSERT(mDataModel.count() > 0);
104 Q_ASSERT(mDataModel.count() > 0);
106
105
107 // TODO: make min and max members and update them when data changes.
106 // TODO: make min and max members and update them when data changes.
108 // This is slower since they are checked every time, even if data is same since previous call.
107 // This is slower since they are checked every time, even if data is same since previous call.
109 qreal max = INT_MIN;
108 qreal max = INT_MIN;
110
109
111 for (int i=0; i <mDataModel.count(); i++) {
110 for (int i=0; i <mDataModel.count(); i++) {
112 int itemCount = mDataModel.at(i)->count();
111 int itemCount = mDataModel.at(i)->count();
113 for (int j=0; j<itemCount; j++) {
112 for (int j=0; j<itemCount; j++) {
114 qreal temp = mDataModel.at(i)->valueAt(j);
113 qreal temp = mDataModel.at(i)->valueAt(j);
115 if (temp > max) {
114 if (temp > max) {
116 max = temp;
115 max = temp;
117 }
116 }
118 }
117 }
119 }
118 }
120
119
121 return max;
120 return max;
122 }
121 }
123
122
124 qreal BarChartModel::valueAt(int set, int category)
123 qreal BarChartModel::valueAt(int set, int category)
125 {
124 {
126 if ((set < 0) || (set >= mDataModel.count())) {
125 if ((set < 0) || (set >= mDataModel.count())) {
127 // No set, no value.
126 // No set, no value.
128 return 0;
127 return 0;
129 } else if ((category < 0) || (category >= mDataModel.at(set)->count())) {
128 } else if ((category < 0) || (category >= mDataModel.at(set)->count())) {
130 // No category, no value.
129 // No category, no value.
131 return 0;
130 return 0;
132 }
131 }
133
132
134 return mDataModel.at(set)->valueAt(category);
133 return mDataModel.at(set)->valueAt(category);
135 }
134 }
136
135
137 qreal BarChartModel::percentageAt(int set, int category)
136 qreal BarChartModel::percentageAt(int set, int category)
138 {
137 {
139 if ((set < 0) || (set >= mDataModel.count())) {
138 if ((set < 0) || (set >= mDataModel.count())) {
140 // No set, no value.
139 // No set, no value.
141 return 0;
140 return 0;
142 } else if ((category < 0) || (category >= mDataModel.at(set)->count())) {
141 } else if ((category < 0) || (category >= mDataModel.at(set)->count())) {
143 // No category, no value.
142 // No category, no value.
144 return 0;
143 return 0;
145 }
144 }
146
145
147 qreal value = mDataModel.at(set)->valueAt(category);
146 qreal value = mDataModel.at(set)->valueAt(category);
148 qreal total = categorySum(category);
147 qreal total = categorySum(category);
149 if (0 == total) {
148 if (0 == total) {
150 return 100.0;
149 return 100.0;
151 }
150 }
152
151
153 return value / total;
152 return value / total;
154 }
153 }
155
154
156
155
157 qreal BarChartModel::categorySum(int category)
156 qreal BarChartModel::categorySum(int category)
158 {
157 {
159 qreal sum(0);
158 qreal sum(0);
160 int count = mDataModel.count(); // Count sets
159 int count = mDataModel.count(); // Count sets
161
160
162 for (int set = 0; set < count; set++) {
161 for (int set = 0; set < count; set++) {
163 if (category < mDataModel.at(set)->count()) {
162 if (category < mDataModel.at(set)->count()) {
164 sum += mDataModel.at(set)->valueAt(category);
163 sum += mDataModel.at(set)->valueAt(category);
165 }
164 }
166 }
165 }
167 return sum;
166 return sum;
168 }
167 }
169
168
170 qreal BarChartModel::maxCategorySum()
169 qreal BarChartModel::maxCategorySum()
171 {
170 {
172 qreal max = INT_MIN;
171 qreal max = INT_MIN;
173 int count = countCategories();
172 int count = countCategories();
174
173
175 for (int col=0; col<count; col++) {
174 for (int col=0; col<count; col++) {
176 qreal sum = categorySum(col);
175 qreal sum = categorySum(col);
177 if (sum > max) {
176 if (sum > max) {
178 max = sum;
177 max = sum;
179 }
178 }
180 }
179 }
181 return max;
180 return max;
182 }
181 }
183
182
184 QString BarChartModel::label(int category)
183 QString BarChartModel::label(int category)
185 {
184 {
186 return mCategory->label(category);
185 return mCategory->label(category);
187 }
186 }
188
187
189 #include "moc_barchartmodel_p.cpp"
188 #include "moc_barchartmodel_p.cpp"
190
189
191 QTCOMMERCIALCHART_END_NAMESPACE
190 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,59 +1,58
1 #ifndef BARCHARTMODEL_H
1 #ifndef BARCHARTMODEL_H
2 #define BARCHARTMODEL_H
2 #define BARCHARTMODEL_H
3
3
4 #include <QObject>
4 #include <QObject>
5 #include "qchartglobal.h"
5 #include "qchartglobal.h"
6
6
7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8
8
9 // Model for bar chart. Internal class.
9 // Model for bar chart. Internal class.
10 // TODO: Implement as QAbstractItemModel?
10 // TODO: Implement as QAbstractItemModel?
11
11
12 class QBarSet;
12 class QBarSet;
13 class QBarCategory;
13 class QBarCategory;
14
14
15 class BarChartModel : public QObject //, public QAbstractItemModel
15 class BarChartModel : public QObject //, public QAbstractItemModel
16 {
16 {
17 Q_OBJECT
17 Q_OBJECT
18 public:
18 public:
19 explicit BarChartModel(QBarCategory *category, QObject *parent = 0);
19 explicit BarChartModel(QBarCategory *category, QObject *parent = 0);
20 ~BarChartModel();
20 ~BarChartModel();
21
21
22 QBarCategory& category();
22 QBarCategory& category();
23 void addBarSet(QBarSet *set);
23 void addBarSet(QBarSet *set);
24 void removeBarSet(QBarSet *set);
24 void removeBarSet(QBarSet *set);
25 QBarSet* nextSet(bool getFirst);
25 QBarSet* nextSet(bool getFirst);
26
26 QBarSet *setAt(int index);
27 QBarSet& setAt(int index); // Internal
28
27
29 int countSets(); // Number of sets in model
28 int countSets(); // Number of sets in model
30 int countCategories(); // Number of categories
29 int countCategories(); // Number of categories
31 int countTotalItems(); // Total items in all sets. Includes empty items.
30 int countTotalItems(); // Total items in all sets. Includes empty items.
32
31
33 qreal max(); // Maximum value of all sets
32 qreal max(); // Maximum value of all sets
34 qreal min(); // Minimum value of all sets
33 qreal min(); // Minimum value of all sets
35 qreal valueAt(int set, int category);
34 qreal valueAt(int set, int category);
36 qreal percentageAt(int set, int category);
35 qreal percentageAt(int set, int category);
37
36
38 qreal categorySum(int category);
37 qreal categorySum(int category);
39 qreal maxCategorySum(); // returns maximum sum of sets in all categories.
38 qreal maxCategorySum(); // returns maximum sum of sets in all categories.
40
39
41 QString label(int category);
40 QString label(int category);
42
41
43 signals:
42 signals:
44 void modelUpdated();
43 void modelUpdated();
45
44
46 public slots:
45 public slots:
47
46
48 private:
47 private:
49
48
50 QList<QBarSet*> mDataModel;
49 QList<QBarSet*> mDataModel;
51 QBarCategory* mCategory; // Owned
50 QBarCategory* mCategory; // Owned
52
51
53 int mCurrentSet;
52 int mCurrentSet;
54
53
55 };
54 };
56
55
57 QTCOMMERCIALCHART_END_NAMESPACE
56 QTCOMMERCIALCHART_END_NAMESPACE
58
57
59 #endif // BARCHARTMODEL_H
58 #endif // BARCHARTMODEL_H
@@ -1,36 +1,36
1 #include "barlabel_p.h"
1 #include "barlabel_p.h"
2 #include <QPainter>
2 #include <QPainter>
3 QTCOMMERCIALCHART_BEGIN_NAMESPACE
3 QTCOMMERCIALCHART_BEGIN_NAMESPACE
4
4
5 BarLabel::BarLabel(QGraphicsItem* parent) : ChartItem(parent)
5 BarLabel::BarLabel(QGraphicsItem* parent) : ChartItem(parent)
6 {
6 {
7 }
7 }
8
8
9 void BarLabel::set(QString label)
9 void BarLabel::set(QString label)
10 {
10 {
11 mLabel = label;
11 mLabel = label;
12 }
12 }
13
13
14 void BarLabel::setPos(qreal x, qreal y)
14 void BarLabel::setPos(qreal x, qreal y)
15 {
15 {
16 mXpos = x;
16 mXpos = x;
17 mYpos = y;
17 mYpos = y;
18 }
18 }
19
19
20 void BarLabel::setSize(const QSizeF &size)
20 void BarLabel::setSize(const QSizeF &size)
21 {
21 {
22 mSize = size;
22 mSize = size;
23 }
23 }
24
24
25 void BarLabel::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
25 void BarLabel::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
26 {
26 {
27 painter->drawText(boundingRect(),mLabel);
27 painter->drawText(boundingRect(),mLabel);
28 }
28 }
29
29
30 QRectF BarLabel::boundingRect() const
30 QRectF BarLabel::boundingRect() const
31 {
31 {
32 QRectF r(mXpos, mYpos, mXpos + mSize.width(), mYpos + mSize.height());
32 QRectF r(mXpos, mYpos, mSize.width(), mSize.height());
33 return r;
33 return r;
34 }
34 }
35
35
36 QTCOMMERCIALCHART_END_NAMESPACE
36 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,92 +1,92
1 #include "barpresenter.h"
1 #include "barpresenter.h"
2 #include "bar_p.h"
2 #include "bar_p.h"
3 #include "barlabel_p.h"
3 #include "barlabel_p.h"
4 #include "barvalue_p.h"
4 #include "barvalue_p.h"
5 #include "qbarset.h"
5 #include "qbarset.h"
6 #include <QDebug>
6 #include <QDebug>
7
7
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9
9
10 BarPresenter::BarPresenter(BarChartModel& model, QGraphicsItem *parent) :
10 BarPresenter::BarPresenter(BarChartModel& model, QGraphicsItem *parent) :
11 BarPresenterBase(model,parent)
11 BarPresenterBase(model,parent)
12 {
12 {
13 mBarDefaultWidth = 15;
13 mBarDefaultWidth = 15;
14 }
14 }
15
15
16 void BarPresenter::layoutChanged()
16 void BarPresenter::layoutChanged()
17 {
17 {
18 // Scale bars to new layout
18 // Scale bars to new layout
19 // Layout for bars:
19 // Layout for bars:
20 if (mModel.countSets() <= 0) {
20 if (mModel.countSets() <= 0) {
21 qDebug() << "No sets in model!";
21 qDebug() << "No sets in model!";
22 return;
22 return;
23 }
23 }
24
24
25 if (childItems().count() == 0) {
25 if (childItems().count() == 0) {
26 qDebug() << "WARNING: BarPresenter::layoutChanged called before graphics items are created!";
26 qDebug() << "WARNING: BarPresenter::layoutChanged called before graphics items are created!";
27 return;
27 return;
28 }
28 }
29
29
30 // TODO: better way to auto-layout?
30 // TODO: better way to auto-layout?
31 // Use reals for accurancy (we might get some compiler warnings... :)
31 // Use reals for accurancy (we might get some compiler warnings... :)
32 int categoryCount = mModel.countCategories();
32 int categoryCount = mModel.countCategories();
33 int setCount = mModel.countSets();
33 int setCount = mModel.countSets();
34
34
35 qreal tW = mWidth;
35 qreal tW = mWidth;
36 qreal tH = mHeight;
36 qreal tH = mHeight;
37 qreal tM = mModel.max();
37 qreal tM = mModel.max();
38 qreal scale = (tH/tM);
38 qreal scale = (tH/tM);
39 qreal tC = categoryCount+1;
39 qreal tC = categoryCount+1;
40 qreal xStepPerSet = (tW/tC);
40 qreal xStepPerSet = (tW/tC);
41
41
42 // Scaling.
42 // Scaling.
43 int itemIndex(0);
43 int itemIndex(0);
44 int labelIndex(0);
44 int labelIndex(0);
45
45
46 for (int category=0; category < categoryCount; category++) {
46 for (int category=0; category < categoryCount; category++) {
47 qreal xPos = xStepPerSet * category + ((tW + mBarDefaultWidth*setCount)/(categoryCount*2));
47 qreal xPos = xStepPerSet * category + ((tW + mBarDefaultWidth*setCount)/(categoryCount*2));
48 qreal yPos = mHeight;
48 qreal yPos = mHeight;
49 for (int set = 0; set < setCount; set++) {
49 for (int set = 0; set < setCount; set++) {
50 qreal barHeight = mModel.valueAt(set, category) * scale;
50 qreal barHeight = mModel.valueAt(set, category) * scale;
51 Bar* bar = mBars.at(itemIndex);
51 Bar* bar = mBars.at(itemIndex);
52
52
53 // TODO: width settable per bar?
53 // TODO: width settable per bar?
54 bar->resize(mBarDefaultWidth, barHeight);
54 bar->resize(mBarDefaultWidth, barHeight);
55 bar->setBrush(mModel.setAt(set).brush());
55 bar->setBrush(mModel.setAt(set)->brush());
56 bar->setPos(xPos, yPos-barHeight); // item*posStep+startPos + set * mBarDefaultWidth, mHeight);
56 bar->setPos(xPos, yPos-barHeight); // item*posStep+startPos + set * mBarDefaultWidth, mHeight);
57 itemIndex++;
57 itemIndex++;
58 xPos += mBarDefaultWidth;
58 xPos += mBarDefaultWidth;
59 }
59 }
60
60
61 // TODO: Layout for labels, remove magic number
61 // TODO: Layout for labels, remove magic number
62 xPos = xStepPerSet * category + ((tW + mBarDefaultWidth*setCount)/(categoryCount*2));
62 xPos = xStepPerSet * category + ((tW + mBarDefaultWidth*setCount)/(categoryCount*2));
63 BarLabel* label = mLabels.at(labelIndex);
63 BarLabel* label = mLabels.at(labelIndex);
64 label->setPos(xPos, mHeight + 20);
64 label->setPos(xPos, mHeight + 20);
65 labelIndex++;
65 labelIndex++;
66 }
66 }
67
67
68 // Position floating values
68 // Position floating values
69 itemIndex = 0;
69 itemIndex = 0;
70 for (int category=0; category < mModel.countCategories(); category++) {
70 for (int category=0; category < mModel.countCategories(); category++) {
71 qreal xPos = xStepPerSet * category + ((tW + mBarDefaultWidth*setCount)/(categoryCount*2));
71 qreal xPos = xStepPerSet * category + ((tW + mBarDefaultWidth*setCount)/(categoryCount*2));
72 qreal yPos = mHeight;
72 qreal yPos = mHeight;
73 for (int set=0; set < mModel.countSets(); set++) {
73 for (int set=0; set < mModel.countSets(); set++) {
74 qreal barHeight = mModel.valueAt(set,category) * scale;
74 qreal barHeight = mModel.valueAt(set,category) * scale;
75 BarValue* value = mFloatingValues.at(itemIndex);
75 BarValue* value = mFloatingValues.at(itemIndex);
76
76
77 // TODO: remove hard coding, apply layout
77 // TODO: remove hard coding, apply layout
78 value->resize(100,50);
78 value->resize(100,50);
79 value->setPos(xPos + mBarDefaultWidth/2, yPos-barHeight/2);
79 value->setPos(xPos + mBarDefaultWidth/2, yPos-barHeight/2);
80 value->setPen(QPen(QColor(255,255,255,255)));
80 value->setPen(QPen(QColor(255,255,255,255)));
81
81
82 QString vString(QString::number(mModel.valueAt(set,category)));
82 QString vString(QString::number(mModel.valueAt(set,category)));
83 value->setValueString(vString);
83 value->setValueString(vString);
84
84
85 itemIndex++;
85 itemIndex++;
86 xPos += mBarDefaultWidth;
86 xPos += mBarDefaultWidth;
87 }
87 }
88 }
88 }
89 mLayoutDirty = true;
89 mLayoutDirty = true;
90 }
90 }
91
91
92 QTCOMMERCIALCHART_END_NAMESPACE
92 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,147 +1,136
1 #include "barpresenterbase.h"
1 #include "barpresenterbase.h"
2 #include "bar_p.h"
2 #include "bar_p.h"
3 #include "barvalue_p.h"
3 #include "barvalue_p.h"
4 #include "barlabel_p.h"
4 #include "barlabel_p.h"
5 #include "separator_p.h"
5 #include "separator_p.h"
6 #include "qbarset.h"
6 #include "qbarset.h"
7 #include <QDebug>
7 #include <QDebug>
8
8
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10
10
11 BarPresenterBase::BarPresenterBase(BarChartModel& model, QGraphicsItem *parent)
11 BarPresenterBase::BarPresenterBase(BarChartModel& model, QGraphicsItem *parent)
12 : ChartItem(parent)
12 : ChartItem(parent)
13 ,mBarDefaultWidth(20) // TODO: remove hard coding, when we have layout code ready
13 ,mBarDefaultWidth(20) // TODO: remove hard coding, when we have layout code ready
14 ,mLayoutSet(false)
14 ,mLayoutSet(false)
15 ,mLayoutDirty(true)
15 ,mLayoutDirty(true)
16 ,mSeparatorsVisible(false)
16 ,mSeparatorsVisible(false)
17 ,mModel(model)
17 ,mModel(model)
18 {
18 {
19 dataChanged();
19 dataChanged();
20 }
20 }
21
21
22 void BarPresenterBase::setSeparatorsVisible(bool visible)
22 void BarPresenterBase::setSeparatorsVisible(bool visible)
23 {
23 {
24 mSeparatorsVisible = visible;
24 mSeparatorsVisible = visible;
25 }
25 }
26
26
27 void BarPresenterBase::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
27 void BarPresenterBase::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
28 {
28 {
29 if (!mLayoutSet) {
29 if (!mLayoutSet) {
30 qDebug() << "BarPresenterBase::paint called without layout set. Aborting.";
30 qDebug() << "BarPresenterBase::paint called without layout set. Aborting.";
31 return;
31 return;
32 }
32 }
33 // if (mLayoutDirty) {
33 // if (mLayoutDirty) {
34 // Layout or data has changed. Need to redraw.
34 // Layout or data has changed. Need to redraw.
35 foreach(QGraphicsItem* i, childItems()) {
35 foreach(QGraphicsItem* i, childItems()) {
36 i->paint(painter,option,widget);
36 i->paint(painter,option,widget);
37 }
37 }
38 // }
38 // }
39 }
39 }
40
40
41 QRectF BarPresenterBase::boundingRect() const
41 QRectF BarPresenterBase::boundingRect() const
42 {
42 {
43 return QRectF(0,0,mWidth,mHeight);
43 return QRectF(0,0,mWidth,mHeight);
44 }
44 }
45
45
46 void BarPresenterBase::setBarWidth( int w )
46 void BarPresenterBase::setBarWidth( int w )
47 {
47 {
48 mBarDefaultWidth = w;
48 mBarDefaultWidth = w;
49 }
49 }
50
50
51 void BarPresenterBase::dataChanged()
51 void BarPresenterBase::dataChanged()
52 {
52 {
53 // TODO: performance optimizations. Do we really need to delete and create items every time data is changed or can we reuse them?
53 // TODO: performance optimizations. Do we really need to delete and create items every time data is changed or can we reuse them?
54 qDebug() << "datachanged";
54 qDebug() << "datachanged";
55 // Delete old bars
55 // Delete old bars
56 foreach (QGraphicsItem* item, childItems()) {
56 foreach (QGraphicsItem* item, childItems()) {
57 delete item;
57 delete item;
58 }
58 }
59
59
60 mBars.clear();
60 mBars.clear();
61 mLabels.clear();
61 mLabels.clear();
62 mSeparators.clear();
62 mSeparators.clear();
63 mFloatingValues.clear();
63 mFloatingValues.clear();
64
64
65 // Create new graphic items for bars
65 // Create new graphic items for bars
66 for (int s=0; s<mModel.countSets(); s++) {
66 for (int c=0; c<mModel.countCategories(); c++) {
67 QBarSet *set = mModel.nextSet(0==s);
67 for (int s=0; s<mModel.countSets(); s++) {
68 for (int c=0; c<mModel.countCategories(); c++) {
68 QBarSet *set = mModel.setAt(s);
69 Bar *bar = new Bar(this);
69 Bar *bar = new Bar(this);
70 childItems().append(bar);
70 childItems().append(bar);
71 mBars.append(bar);
71 mBars.append(bar);
72 connect(bar,SIGNAL(clicked()),set,SLOT(toggleFloatingValuesVisible()));
72 connect(bar,SIGNAL(clicked()),set,SLOT(barClicked()));
73 }
73 }
74 }
74 }
75
75
76 // Create labels
76 // Create labels
77 int count = mModel.countCategories();
77 int count = mModel.countCategories();
78 for (int i=0; i<count; i++) {
78 for (int i=0; i<count; i++) {
79 BarLabel* label = new BarLabel(this);
79 BarLabel* label = new BarLabel(this);
80 label->set(mModel.label(i));
80 label->set(mModel.label(i));
81 childItems().append(label);
81 childItems().append(label);
82 mLabels.append(label);
82 mLabels.append(label);
83 }
83 }
84
84
85 // Create separators
85 // Create separators
86 count = mModel.countCategories() - 1; // There is one less separator than columns
86 count = mModel.countCategories() - 1; // There is one less separator than columns
87 for (int i=0; i<count; i++) {
87 for (int i=0; i<count; i++) {
88 Separator* sep = new Separator(this);
88 Separator* sep = new Separator(this);
89 sep->setColor(QColor(255,0,0,255)); // TODO: color for separations from theme
89 sep->setColor(QColor(255,0,0,255)); // TODO: color for separations from theme
90 childItems().append(sep);
90 childItems().append(sep);
91 mSeparators.append(sep);
91 mSeparators.append(sep);
92 }
92 }
93
93
94 // Create floating values
94 // Create floating values
95 for (int s=0; s<mModel.countSets(); s++) {
95 for (int category=0; category<mModel.countCategories(); category++) {
96 QBarSet *set = mModel.nextSet(0==s);
96 for (int s=0; s<mModel.countSets(); s++) {
97 for (int category=0; category<mModel.countCategories(); category++) {
97 QBarSet *set = mModel.setAt(s);
98 BarValue *value = new BarValue(*set, this);
98 BarValue *value = new BarValue(*set, this);
99 childItems().append(value);
99 childItems().append(value);
100 mFloatingValues.append(value);
100 mFloatingValues.append(value);
101 connect(set,SIGNAL(clicked()),value,SLOT(toggleVisible()));
101 }
102 }
102 connect(set,SIGNAL(setFloatingValuesVisible(QBarSet*)),this,SLOT(setFloatingValues(QBarSet*)));
103 }
103 }
104
104
105 // TODO: if (autolayout) { layoutChanged() } or something
105 // TODO: if (autolayout) { layoutChanged() } or something
106 mLayoutDirty = true;
106 mLayoutDirty = true;
107 }
107 }
108
108
109 void BarPresenterBase::setFloatingValues(QBarSet *set)
110 {
111 qDebug() << "BarPresenterBase::setFloatingValues";
112 // TODO: better way to map set to BarValues?
113 for (int i=0; i<mFloatingValues.count(); i++) {
114 if (mFloatingValues.at(i)->belongsToSet(set)) {
115 mFloatingValues.at(i)->setVisible(set->isFloatingValuesVisible());
116 }
117 }
118 }
119
120 //handlers
109 //handlers
121
110
122 void BarPresenterBase::handleModelChanged(int index)
111 void BarPresenterBase::handleModelChanged(int index)
123 {
112 {
124 // qDebug() << "BarPresenterBase::handleModelChanged" << index;
113 // qDebug() << "BarPresenterBase::handleModelChanged" << index;
125 dataChanged();
114 dataChanged();
126 }
115 }
127
116
128 void BarPresenterBase::handleDomainChanged(const Domain& domain)
117 void BarPresenterBase::handleDomainChanged(const Domain& domain)
129 {
118 {
130 // qDebug() << "BarPresenterBase::handleDomainChanged";
119 // qDebug() << "BarPresenterBase::handleDomainChanged";
131 // TODO: Figure out the use case for this.
120 // TODO: Figure out the use case for this.
132 // Affects the size of visible item, so layout is changed.
121 // Affects the size of visible item, so layout is changed.
133 // layoutChanged();
122 // layoutChanged();
134 }
123 }
135
124
136 void BarPresenterBase::handleGeometryChanged(const QRectF& rect)
125 void BarPresenterBase::handleGeometryChanged(const QRectF& rect)
137 {
126 {
138 mWidth = rect.width();
127 mWidth = rect.width();
139 mHeight = rect.height();
128 mHeight = rect.height();
140 layoutChanged();
129 layoutChanged();
141 mLayoutSet = true;
130 mLayoutSet = true;
142 setPos(rect.topLeft());
131 setPos(rect.topLeft());
143 }
132 }
144
133
145 #include "moc_barpresenterbase.cpp"
134 #include "moc_barpresenterbase.cpp"
146
135
147 QTCOMMERCIALCHART_END_NAMESPACE
136 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,76 +1,73
1 #ifndef BARPRESENTERBASE_H
1 #ifndef BARPRESENTERBASE_H
2 #define BARPRESENTERBASE_H
2 #define BARPRESENTERBASE_H
3
3
4 #include "chartitem_p.h"
4 #include "chartitem_p.h"
5 #include "barchartmodel_p.h"
5 #include "barchartmodel_p.h"
6 #include <QPen>
6 #include <QPen>
7 #include <QBrush>
7 #include <QBrush>
8 #include <QGraphicsItem>
8 #include <QGraphicsItem>
9
9
10 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10 QTCOMMERCIALCHART_BEGIN_NAMESPACE
11
11
12 class Bar;
12 class Bar;
13 class BarLabel;
13 class BarLabel;
14 class Separator;
14 class Separator;
15 class BarValue;
15 class BarValue;
16
16
17 // Common implemantation of different presenters. Not to be instantiated.
17 // Common implemantation of different presenters. Not to be instantiated.
18 class BarPresenterBase : public QObject, public ChartItem
18 class BarPresenterBase : public QObject, public ChartItem
19 {
19 {
20 Q_OBJECT
20 Q_OBJECT
21 public:
21 public:
22 BarPresenterBase(BarChartModel& model, QGraphicsItem *parent = 0);
22 BarPresenterBase(BarChartModel& model, QGraphicsItem *parent = 0);
23 void setSeparatorsVisible(bool visible = true);
23 void setSeparatorsVisible(bool visible = true);
24
24
25 public:
25 public:
26
26
27 // From QGraphicsItem
27 // From QGraphicsItem
28 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
28 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
29 QRectF boundingRect() const;
29 QRectF boundingRect() const;
30
30
31 // TODO: these may change with layout awarness.
31 // TODO: these may change with layout awarness.
32 void setBarWidth( int w );
32 void setBarWidth( int w );
33
33
34 void setPen(QPen pen);
34 void setPen(QPen pen);
35 QPen pen();
35 QPen pen();
36
36
37 void setBrush(QBrush brush);
37 void setBrush(QBrush brush);
38 QBrush brush();
38 QBrush brush();
39
39
40 // TODO: Consider the domain for layoutChanged. May be use case, may not be. If it is, then the derived classes need to implement it
40 // TODO: Consider the domain for layoutChanged. May be use case, may not be. If it is, then the derived classes need to implement it
41 virtual void dataChanged(); // data of series has changed -> need to recalculate bar sizes
41 virtual void dataChanged(); // data of series has changed -> need to recalculate bar sizes
42 virtual void layoutChanged() = 0; // layout has changed -> need to recalculate bar sizes
42 virtual void layoutChanged() = 0; // layout has changed -> need to recalculate bar sizes
43
43
44 public Q_SLOTS:
45 void setFloatingValues(QBarSet *set);
46
47 protected slots:
44 protected slots:
48 void handleModelChanged(int index);
45 void handleModelChanged(int index);
49 void handleDomainChanged(const Domain& domain);
46 void handleDomainChanged(const Domain& domain);
50 void handleGeometryChanged(const QRectF& size);
47 void handleGeometryChanged(const QRectF& size);
51
48
52 protected:
49 protected:
53
50
54 // TODO: consider these.
51 // TODO: consider these.
55 int mHeight; // Layout spesific
52 int mHeight; // Layout spesific
56 int mWidth;
53 int mWidth;
57 int mBarDefaultWidth;
54 int mBarDefaultWidth;
58
55
59 bool mLayoutSet; // True, if component has been laid out.
56 bool mLayoutSet; // True, if component has been laid out.
60 bool mLayoutDirty;
57 bool mLayoutDirty;
61
58
62 bool mSeparatorsVisible;
59 bool mSeparatorsVisible;
63 BarChartModel& mModel;
60 BarChartModel& mModel;
64
61
65 // Not owned.
62 // Not owned.
66 QList<Bar*> mBars;
63 QList<Bar*> mBars;
67 QList<BarLabel*> mLabels;
64 QList<BarLabel*> mLabels;
68 QList<Separator*> mSeparators;
65 QList<Separator*> mSeparators;
69 QList<BarValue*> mFloatingValues;
66 QList<BarValue*> mFloatingValues;
70
67
71 QPen mPen;
68 QPen mPen;
72 };
69 };
73
70
74 QTCOMMERCIALCHART_END_NAMESPACE
71 QTCOMMERCIALCHART_END_NAMESPACE
75
72
76 #endif // BARPRESENTERBASE_H
73 #endif // BARPRESENTERBASE_H
@@ -1,64 +1,69
1 #include "barvalue_p.h"
1 #include "barvalue_p.h"
2 #include <QPainter>
2 #include <QPainter>
3 #include <QPen>
3 #include <QPen>
4
4
5 QTCOMMERCIALCHART_BEGIN_NAMESPACE
5 QTCOMMERCIALCHART_BEGIN_NAMESPACE
6
6
7 BarValue::BarValue(QBarSet &set, QGraphicsItem *parent)
7 BarValue::BarValue(QBarSet &set, QGraphicsItem *parent)
8 : QGraphicsItem(parent)
8 : QGraphicsObject(parent)
9 ,mBarSet(set)
9 ,mBarSet(set)
10 {
10 {
11 // setVisible(false);
11 setVisible(false);
12 }
12 }
13
13
14 void BarValue::setValueString(QString str)
14 void BarValue::setValueString(QString str)
15 {
15 {
16 mValueString = str;
16 mValueString = str;
17 }
17 }
18
18
19 QString BarValue::valueString()
19 QString BarValue::valueString()
20 {
20 {
21 return mValueString;
21 return mValueString;
22 }
22 }
23
23
24 void BarValue::setPen(const QPen& pen)
24 void BarValue::setPen(const QPen& pen)
25 {
25 {
26 mPen = pen;
26 mPen = pen;
27 }
27 }
28
28
29 const QPen& BarValue::pen()
29 const QPen& BarValue::pen()
30 {
30 {
31 return mPen;
31 return mPen;
32 }
32 }
33
33
34 void BarValue::resize(qreal w, qreal h)
34 void BarValue::resize(qreal w, qreal h)
35 {
35 {
36 mWidth = w;
36 mWidth = w;
37 mHeight = h;
37 mHeight = h;
38 }
38 }
39
39
40 void BarValue::setPos(qreal x, qreal y)
40 void BarValue::setPos(qreal x, qreal y)
41 {
41 {
42 mXpos = x;
42 mXpos = x;
43 mYpos = y;
43 mYpos = y;
44 }
44 }
45
45
46 bool BarValue::belongsToSet(QBarSet *set)
46 bool BarValue::belongsToSet(QBarSet *set)
47 {
47 {
48 return (&mBarSet == set);
48 return (&mBarSet == set);
49 }
49 }
50
50
51 void BarValue::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
51 void BarValue::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
52 {
52 {
53 painter->setPen(mPen);
53 painter->setPen(mPen);
54 painter->drawText(boundingRect(),mValueString);
54 painter->drawText(boundingRect(),mValueString);
55 }
55 }
56
56
57 QRectF BarValue::boundingRect() const
57 QRectF BarValue::boundingRect() const
58 {
58 {
59 QRectF r(mXpos, mYpos, mXpos + mWidth, mYpos + mHeight);
59 QRectF r(mXpos, mYpos, mWidth, mHeight);
60 return r;
60 return r;
61 }
61 }
62
62
63 void BarValue::toggleVisible()
64 {
65 setVisible(!isVisible());
66 }
63
67
68 #include "moc_barvalue_p.cpp"
64 QTCOMMERCIALCHART_END_NAMESPACE
69 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,49 +1,53
1 #ifndef BARVALUE_P_H
1 #ifndef BARVALUE_P_H
2 #define BARVALUE_P_H
2 #define BARVALUE_P_H
3
3
4 #include "qchartglobal.h"
4 #include "qchartglobal.h"
5 #include <QGraphicsItem>
5 #include <QGraphicsObject>
6 #include <QPen>
6 #include <QPen>
7
7
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9
9
10 class QBarSet;
10 class QBarSet;
11
11
12 // Visual class for floating bar values
12 // Visual class for floating bar values
13 // TODO: fonts, colors etc.
13 // TODO: fonts, colors etc.
14 // By default these are not visible.
14 // By default these are not visible.
15 class BarValue : public QGraphicsItem // TODO: public QGraphicsObject for signals?
15 class BarValue : public QGraphicsObject
16 {
16 {
17 Q_OBJECT
17 public:
18 public:
18 BarValue(QBarSet &set, QGraphicsItem *parent = 0);
19 BarValue(QBarSet &set, QGraphicsItem *parent = 0);
19
20
20 void setValueString(QString str);
21 void setValueString(QString str);
21 QString valueString();
22 QString valueString();
22
23
23 void setPen(const QPen& pen);
24 void setPen(const QPen& pen);
24 const QPen& pen();
25 const QPen& pen();
25
26
26 void resize(qreal w, qreal h);
27 void resize(qreal w, qreal h);
27 void setPos(qreal x, qreal y);
28 void setPos(qreal x, qreal y);
28
29
29 bool belongsToSet(QBarSet *set);
30 bool belongsToSet(QBarSet *set);
30
31
31 // From QGraphicsItem
32 // From QGraphicsItem
32 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
33 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
33 QRectF boundingRect() const;
34 QRectF boundingRect() const;
34
35
36 public Q_SLOTS:
37 void toggleVisible();
38
35 private:
39 private:
36
40
37 QBarSet& mBarSet;
41 QBarSet& mBarSet;
38 QPen mPen;
42 QPen mPen;
39 QString mValueString;
43 QString mValueString;
40
44
41 qreal mXpos;
45 qreal mXpos;
42 qreal mYpos;
46 qreal mYpos;
43 qreal mWidth;
47 qreal mWidth;
44 qreal mHeight;
48 qreal mHeight;
45 };
49 };
46
50
47 QTCOMMERCIALCHART_END_NAMESPACE
51 QTCOMMERCIALCHART_END_NAMESPACE
48
52
49 #endif // BARVALUE_P_H
53 #endif // BARVALUE_P_H
@@ -1,103 +1,103
1 #include "percentbarpresenter.h"
1 #include "percentbarpresenter.h"
2 #include "bar_p.h"
2 #include "bar_p.h"
3 #include "barlabel_p.h"
3 #include "barlabel_p.h"
4 #include "barvalue_p.h"
4 #include "barvalue_p.h"
5 #include "separator_p.h"
5 #include "separator_p.h"
6 #include "qbarset.h"
6 #include "qbarset.h"
7 #include <QDebug>
7 #include <QDebug>
8
8
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10
10
11
11
12 PercentBarPresenter::PercentBarPresenter(BarChartModel& model, QGraphicsItem *parent) :
12 PercentBarPresenter::PercentBarPresenter(BarChartModel& model, QGraphicsItem *parent) :
13 BarPresenterBase(model, parent)
13 BarPresenterBase(model, parent)
14 {
14 {
15 }
15 }
16
16
17 void PercentBarPresenter::layoutChanged()
17 void PercentBarPresenter::layoutChanged()
18 {
18 {
19 // Scale bars to new layout
19 // Scale bars to new layout
20 // Layout for bars:
20 // Layout for bars:
21 if (mModel.countSets() <= 0) {
21 if (mModel.countSets() <= 0) {
22 qDebug() << "No sets in model!";
22 qDebug() << "No sets in model!";
23 // Nothing to do.
23 // Nothing to do.
24 return;
24 return;
25 }
25 }
26
26
27 if (childItems().count() == 0) {
27 if (childItems().count() == 0) {
28 qDebug() << "WARNING: PercentBarPresenter::layoutChanged called before graphics items are created!";
28 qDebug() << "WARNING: PercentBarPresenter::layoutChanged called before graphics items are created!";
29 return;
29 return;
30 }
30 }
31
31
32 // TODO: better way to auto-layout
32 // TODO: better way to auto-layout
33 // Use reals for accurancy (we might get some compiler warnings... :)
33 // Use reals for accurancy (we might get some compiler warnings... :)
34 int count = mModel.countCategories();
34 int count = mModel.countCategories();
35 int itemIndex(0);
35 int itemIndex(0);
36 int labelIndex(0);
36 int labelIndex(0);
37 qreal tW = mWidth;
37 qreal tW = mWidth;
38 qreal tC = count+1;
38 qreal tC = count+1;
39 qreal xStep = (tW/tC);
39 qreal xStep = (tW/tC);
40 qreal xPos = ((tW/tC) - mBarDefaultWidth / 2);
40 qreal xPos = ((tW/tC) - mBarDefaultWidth / 2);
41 qreal h = mHeight;
41 qreal h = mHeight;
42
42
43 for (int category = 0; category < mModel.countCategories(); category++) {
43 for (int category = 0; category < mModel.countCategories(); category++) {
44 qreal colSum = mModel.categorySum(category);
44 qreal colSum = mModel.categorySum(category);
45 qreal scale = (h / colSum);
45 qreal scale = (h / colSum);
46 qreal yPos = h;
46 qreal yPos = h;
47 for (int set=0; set < mModel.countSets(); set++) {
47 for (int set=0; set < mModel.countSets(); set++) {
48 qreal barHeight = mModel.valueAt(set, category) * scale;
48 qreal barHeight = mModel.valueAt(set, category) * scale;
49 Bar* bar = mBars.at(itemIndex);
49 Bar* bar = mBars.at(itemIndex);
50
50
51 // TODO: width settable per bar?
51 // TODO: width settable per bar?
52 bar->resize(mBarDefaultWidth, barHeight);
52 bar->resize(mBarDefaultWidth, barHeight);
53 bar->setBrush(mModel.setAt(set).brush());
53 bar->setBrush(mModel.setAt(set)->brush());
54 bar->setPos(xPos, yPos-barHeight);
54 bar->setPos(xPos, yPos-barHeight);
55 itemIndex++;
55 itemIndex++;
56 yPos -= barHeight;
56 yPos -= barHeight;
57 }
57 }
58
58
59 // TODO: Layout for labels, remove magic number
59 // TODO: Layout for labels, remove magic number
60 BarLabel* label = mLabels.at(labelIndex);
60 BarLabel* label = mLabels.at(labelIndex);
61 label->setPos(xPos, mHeight + 20);
61 label->setPos(xPos, mHeight + 20);
62 labelIndex++;
62 labelIndex++;
63 xPos += xStep;
63 xPos += xStep;
64 }
64 }
65
65
66 // Position separators
66 // Position separators
67 xPos = xStep + xStep/2;
67 xPos = xStep + xStep/2;
68 for (int s=0; s < mModel.countCategories() - 1; s++) {
68 for (int s=0; s < mModel.countCategories() - 1; s++) {
69 Separator* sep = mSeparators.at(s);
69 Separator* sep = mSeparators.at(s);
70 sep->setPos(xPos,0);
70 sep->setPos(xPos,0);
71 sep->setSize(QSizeF(1,mHeight));
71 sep->setSize(QSizeF(1,mHeight));
72 xPos += xStep;
72 xPos += xStep;
73 }
73 }
74
74
75 // Position floating values
75 // Position floating values
76 itemIndex = 0;
76 itemIndex = 0;
77 xPos = ((tW/tC) - mBarDefaultWidth / 2);
77 xPos = ((tW/tC) - mBarDefaultWidth / 2);
78 for (int category=0; category < mModel.countCategories(); category++) {
78 for (int category=0; category < mModel.countCategories(); category++) {
79 qreal yPos = h;
79 qreal yPos = h;
80 qreal colSum = mModel.categorySum(category);
80 qreal colSum = mModel.categorySum(category);
81 qreal scale = (h / colSum);
81 qreal scale = (h / colSum);
82 for (int set=0; set < mModel.countSets(); set++) {
82 for (int set=0; set < mModel.countSets(); set++) {
83 qreal barHeight = mModel.valueAt(set,category) * scale;
83 qreal barHeight = mModel.valueAt(set,category) * scale;
84 BarValue* value = mFloatingValues.at(itemIndex);
84 BarValue* value = mFloatingValues.at(itemIndex);
85
85
86 // TODO: remove hard coding, apply layout
86 // TODO: remove hard coding, apply layout
87 value->setPos(xPos + mBarDefaultWidth/2, yPos-barHeight/2);
87 value->setPos(xPos + mBarDefaultWidth/2, yPos-barHeight/2);
88 value->setPen(QPen(QColor(255,255,255,255)));
88 value->setPen(QPen(QColor(255,255,255,255)));
89
89
90 QString vString(QString::number(mModel.percentageAt(set,category) * 100));
90 QString vString(QString::number(mModel.percentageAt(set,category) * 100));
91 vString.append("%");
91 vString.append("%");
92 value->setValueString(vString);
92 value->setValueString(vString);
93
93
94 itemIndex++;
94 itemIndex++;
95 yPos -= barHeight;
95 yPos -= barHeight;
96 }
96 }
97 xPos += xStep;
97 xPos += xStep;
98 }
98 }
99
99
100 mLayoutDirty = true;
100 mLayoutDirty = true;
101 }
101 }
102
102
103 QTCOMMERCIALCHART_END_NAMESPACE
103 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,88 +1,75
1 #include "qbarset.h"
1 #include "qbarset.h"
2 #include <QDebug>
2 #include <QDebug>
3
3
4 QTCOMMERCIALCHART_BEGIN_NAMESPACE
4 QTCOMMERCIALCHART_BEGIN_NAMESPACE
5
5
6 QBarSet::QBarSet()
6 QBarSet::QBarSet()
7 {
7 {
8 mFloatingValuesVisible = false;
8 mFloatingValuesVisible = false;
9 }
9 }
10
10
11 void QBarSet::setName(QString name)
11 void QBarSet::setName(QString name)
12 {
12 {
13 mName = name;
13 mName = name;
14 }
14 }
15 QString QBarSet::name()
15 QString QBarSet::name()
16 {
16 {
17 return mName;
17 return mName;
18 }
18 }
19
19
20 QBarSet& QBarSet::operator << (const qreal &value)
20 QBarSet& QBarSet::operator << (const qreal &value)
21 {
21 {
22 mValues.append(value);
22 mValues.append(value);
23 return *this;
23 return *this;
24 }
24 }
25
25
26 int QBarSet::count()
26 int QBarSet::count()
27 {
27 {
28 return mValues.count();
28 return mValues.count();
29 }
29 }
30
30
31 qreal QBarSet::valueAt(int index)
31 qreal QBarSet::valueAt(int index)
32 {
32 {
33 return mValues.at(index);
33 return mValues.at(index);
34 }
34 }
35
35
36 void QBarSet::setValue(int index, qreal value)
36 void QBarSet::setValue(int index, qreal value)
37 {
37 {
38 mValues.replace(index,value);
38 mValues.replace(index,value);
39 }
39 }
40
40
41 void QBarSet::setPen(const QPen& pen)
41 void QBarSet::setPen(const QPen& pen)
42 {
42 {
43 mPen = pen;
43 mPen = pen;
44 }
44 }
45
45
46 const QPen& QBarSet::pen() const
46 const QPen& QBarSet::pen() const
47 {
47 {
48 return mPen;
48 return mPen;
49 }
49 }
50
50
51 void QBarSet::setBrush(const QBrush& brush)
51 void QBarSet::setBrush(const QBrush& brush)
52 {
52 {
53 mBrush = brush;
53 mBrush = brush;
54 }
54 }
55
55
56 const QBrush& QBarSet::brush() const
56 const QBrush& QBarSet::brush() const
57 {
57 {
58 return mBrush;
58 return mBrush;
59 }
59 }
60
60
61 bool QBarSet::isFloatingValuesVisible()
61 bool QBarSet::isFloatingValuesVisible()
62 {
62 {
63 return mFloatingValuesVisible;
63 return mFloatingValuesVisible;
64 }
64 }
65
65
66 void QBarSet::barClicked()
66 void QBarSet::barClicked()
67 {
67 {
68 qDebug() << "QBarset::barClicked";
68 qDebug() << "QBarset::barClicked" << this;
69 // Some bar of this set has been clicked
69 // Some bar of this set has been clicked
70 // TODO: What happens then?
70 // TODO: What happens then?
71 emit clicked(); // Notify that set has been clicked
71 emit clicked(); // Notify that set has been clicked
72 }
72 }
73
73
74 void QBarSet::toggleFloatingValuesVisible()
75 {
76 qDebug() << "QBarset::toggleFloatingValuesVisible";
77 // TODO: toggle vs explicit set?
78 if (mFloatingValuesVisible) {
79 mFloatingValuesVisible=false;
80 } else {
81 mFloatingValuesVisible=true;
82 }
83 emit setFloatingValuesVisible(this);
84 }
85
86
87 #include "moc_qbarset.cpp"
74 #include "moc_qbarset.cpp"
88 QTCOMMERCIALCHART_END_NAMESPACE
75 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,56 +1,54
1 #ifndef QBARSET_H
1 #ifndef QBARSET_H
2 #define QBARSET_H
2 #define QBARSET_H
3
3
4 #include "qchartglobal.h"
4 #include "qchartglobal.h"
5 #include <QPen>
5 #include <QPen>
6 #include <QBrush>
6 #include <QBrush>
7
7
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9
9
10 class QTCOMMERCIALCHART_EXPORT QBarSet : public QObject
10 class QTCOMMERCIALCHART_EXPORT QBarSet : public QObject
11 {
11 {
12 Q_OBJECT
12 Q_OBJECT
13 public:
13 public:
14 QBarSet();
14 QBarSet();
15
15
16 void setName(QString name);
16 void setName(QString name);
17 QString name();
17 QString name();
18 QBarSet& operator << (const qreal &value); // appends new value to set
18 QBarSet& operator << (const qreal &value); // appends new value to set
19
19
20 int count(); // count of values in set
20 int count(); // count of values in set
21 qreal valueAt(int index); // for modifying individual values
21 qreal valueAt(int index); // for modifying individual values
22 void setValue(int index, qreal value); //
22 void setValue(int index, qreal value); //
23
23
24 void setPen(const QPen& pen);
24 void setPen(const QPen& pen);
25 const QPen& pen() const;
25 const QPen& pen() const;
26
26
27 void setBrush(const QBrush& brush);
27 void setBrush(const QBrush& brush);
28 const QBrush& brush() const;
28 const QBrush& brush() const;
29
29
30 bool isFloatingValuesVisible();
30 bool isFloatingValuesVisible();
31
31
32 Q_SIGNALS:
32 Q_SIGNALS:
33 void clicked();
33 void clicked();
34 void setFloatingValuesVisible(QBarSet* set);
35 /*
34 /*
36 void hoverEnter();
35 void hoverEnter();
37 void hoverLeave();
36 void hoverLeave();
38 */
37 */
39
38
40 public Q_SLOTS:
39 public Q_SLOTS:
41 void barClicked();
40 void barClicked();
42 void toggleFloatingValuesVisible();
43
41
44 private:
42 private:
45
43
46 QString mName;
44 QString mName;
47 QList<qreal> mValues;
45 QList<qreal> mValues;
48 QPen mPen;
46 QPen mPen;
49 QBrush mBrush;
47 QBrush mBrush;
50
48
51 bool mFloatingValuesVisible;
49 bool mFloatingValuesVisible;
52 };
50 };
53
51
54 QTCOMMERCIALCHART_END_NAMESPACE
52 QTCOMMERCIALCHART_END_NAMESPACE
55
53
56 #endif // QBARSET_H
54 #endif // QBARSET_H
@@ -1,106 +1,106
1 #include "stackedbarpresenter.h"
1 #include "stackedbarpresenter.h"
2 #include "bar_p.h"
2 #include "bar_p.h"
3 #include "barlabel_p.h"
3 #include "barlabel_p.h"
4 #include "barvalue_p.h"
4 #include "barvalue_p.h"
5 #include "separator_p.h"
5 #include "separator_p.h"
6 #include "qbarset.h"
6 #include "qbarset.h"
7 #include <QDebug>
7 #include <QDebug>
8
8
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10
10
11 StackedBarPresenter::StackedBarPresenter(BarChartModel& model, QGraphicsItem *parent) :
11 StackedBarPresenter::StackedBarPresenter(BarChartModel& model, QGraphicsItem *parent) :
12 BarPresenterBase(model,parent)
12 BarPresenterBase(model,parent)
13 {
13 {
14 }
14 }
15
15
16 void StackedBarPresenter::layoutChanged()
16 void StackedBarPresenter::layoutChanged()
17 {
17 {
18 // Scale bars to new layout
18 // Scale bars to new layout
19 // Layout for bars:
19 // Layout for bars:
20 if (mModel.countSets() <= 0) {
20 if (mModel.countSets() <= 0) {
21 qDebug() << "No sets in model!";
21 qDebug() << "No sets in model!";
22 // Nothing to do.
22 // Nothing to do.
23 return;
23 return;
24 }
24 }
25
25
26 if (mModel.countCategories() == 0) {
26 if (mModel.countCategories() == 0) {
27 qDebug() << "No categories in model!";
27 qDebug() << "No categories in model!";
28 // Nothing to do
28 // Nothing to do
29 return;
29 return;
30 }
30 }
31
31
32 if (childItems().count() == 0) {
32 if (childItems().count() == 0) {
33 qDebug() << "WARNING: StackedBarPresenter::layoutChanged called before graphics items are created!";
33 qDebug() << "WARNING: StackedBarPresenter::layoutChanged called before graphics items are created!";
34 return;
34 return;
35 }
35 }
36
36
37 // TODO: better way to auto-layout
37 // TODO: better way to auto-layout
38 // Use reals for accurancy (we might get some compiler warnings... :)
38 // Use reals for accurancy (we might get some compiler warnings... :)
39 // TODO: use temp variable for category count...
39 // TODO: use temp variable for category count...
40 qreal maxSum = mModel.maxCategorySum();
40 qreal maxSum = mModel.maxCategorySum();
41 qreal h = mHeight;
41 qreal h = mHeight;
42 qreal scale = (h / maxSum);
42 qreal scale = (h / maxSum);
43
43
44 int itemIndex(0);
44 int itemIndex(0);
45 int labelIndex(0);
45 int labelIndex(0);
46 qreal tW = mWidth;
46 qreal tW = mWidth;
47 qreal tC = mModel.countCategories() + 1;
47 qreal tC = mModel.countCategories() + 1;
48 qreal xStep = (tW/tC);
48 qreal xStep = (tW/tC);
49 qreal xPos = ((tW/tC) - mBarDefaultWidth / 2);
49 qreal xPos = ((tW/tC) - mBarDefaultWidth / 2);
50
50
51 for (int category = 0; category < mModel.countCategories(); category++) {
51 for (int category = 0; category < mModel.countCategories(); category++) {
52 qreal yPos = h;
52 qreal yPos = h;
53 for (int set=0; set < mModel.countSets(); set++) {
53 for (int set=0; set < mModel.countSets(); set++) {
54 qreal barHeight = mModel.valueAt(set, category) * scale;
54 qreal barHeight = mModel.valueAt(set, category) * scale;
55 Bar* bar = mBars.at(itemIndex);
55 Bar* bar = mBars.at(itemIndex);
56
56
57 bar->resize(mBarDefaultWidth, barHeight);
57 bar->resize(mBarDefaultWidth, barHeight);
58 bar->setBrush(mModel.setAt(set).brush());
58 bar->setBrush(mModel.setAt(set)->brush());
59 bar->setPos(xPos, yPos-barHeight);
59 bar->setPos(xPos, yPos-barHeight);
60 itemIndex++;
60 itemIndex++;
61 yPos -= barHeight;
61 yPos -= barHeight;
62 }
62 }
63
63
64 // TODO: Layout for labels, remove magic number
64 // TODO: Layout for labels, remove magic number
65 BarLabel* label = mLabels.at(labelIndex);
65 BarLabel* label = mLabels.at(labelIndex);
66 label->setPos(xPos, mHeight + 20);
66 label->setPos(xPos, mHeight + 20);
67 labelIndex++;
67 labelIndex++;
68 xPos += xStep;
68 xPos += xStep;
69 }
69 }
70
70
71 // Position separators
71 // Position separators
72 xPos = xStep + xStep/2;
72 xPos = xStep + xStep/2;
73 for (int s=0; s < mModel.countCategories() - 1; s++) {
73 for (int s=0; s < mModel.countCategories() - 1; s++) {
74 Separator* sep = mSeparators.at(s);
74 Separator* sep = mSeparators.at(s);
75 sep->setPos(xPos,0);
75 sep->setPos(xPos,0);
76 sep->setSize(QSizeF(1,mHeight));
76 sep->setSize(QSizeF(1,mHeight));
77 xPos += xStep;
77 xPos += xStep;
78 }
78 }
79
79
80 // Position floating values
80 // Position floating values
81 itemIndex = 0;
81 itemIndex = 0;
82 xPos = ((tW/tC) - mBarDefaultWidth / 2);
82 xPos = ((tW/tC) - mBarDefaultWidth / 2);
83 for (int category=0; category < mModel.countCategories(); category++) {
83 for (int category=0; category < mModel.countCategories(); category++) {
84 qreal yPos = h;
84 qreal yPos = h;
85 for (int set=0; set < mModel.countSets(); set++) {
85 for (int set=0; set < mModel.countSets(); set++) {
86 qreal barHeight = mModel.valueAt(set,category) * scale;
86 qreal barHeight = mModel.valueAt(set,category) * scale;
87 BarValue* value = mFloatingValues.at(itemIndex);
87 BarValue* value = mFloatingValues.at(itemIndex);
88
88
89 // TODO: remove hard coding, apply layout
89 // TODO: remove hard coding, apply layout
90 value->resize(100,50);
90 value->resize(100,50);
91 value->setPos(xPos + mBarDefaultWidth/2, yPos-barHeight/2);
91 value->setPos(xPos + mBarDefaultWidth/2, yPos-barHeight/2);
92 value->setPen(QPen(QColor(255,255,255,255)));
92 value->setPen(QPen(QColor(255,255,255,255)));
93
93
94 QString vString(QString::number(mModel.valueAt(set,category)));
94 QString vString(QString::number(mModel.valueAt(set,category)));
95 value->setValueString(vString);
95 value->setValueString(vString);
96
96
97 itemIndex++;
97 itemIndex++;
98 yPos -= barHeight;
98 yPos -= barHeight;
99 }
99 }
100 xPos += xStep;
100 xPos += xStep;
101 }
101 }
102
102
103 mLayoutDirty = true;
103 mLayoutDirty = true;
104 }
104 }
105
105
106 QTCOMMERCIALCHART_END_NAMESPACE
106 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,202 +1,203
1 #include "qchart.h"
1 #include "qchart.h"
2 #include "qchartaxis.h"
2 #include "qchartaxis.h"
3 #include "chartpresenter_p.h"
3 #include "chartpresenter_p.h"
4 #include "chartdataset_p.h"
4 #include "chartdataset_p.h"
5 #include <QGraphicsScene>
5 #include <QGraphicsScene>
6 #include <QGraphicsSceneResizeEvent>
6 #include <QGraphicsSceneResizeEvent>
7 #include <QDebug>
7 #include <QDebug>
8
8
9 /*!
9 /*!
10 \class QChart
10 \class QChart
11 \brief QtCommercial chart API.
11 \brief QtCommercial chart API.
12
12
13 QChart is a QGraphicsWidget that you can show in a QGraphicsScene. It manages the graphical
13 QChart is a QGraphicsWidget that you can show in a QGraphicsScene. It manages the graphical
14 representation of different types of QChartSeries and other chart related objects like
14 representation of different types of QChartSeries and other chart related objects like
15 QChartAxis and QChartLegend. If you simply want to show a chart in a layout, you can use the
15 QChartAxis and QChartLegend. If you simply want to show a chart in a layout, you can use the
16 convenience class QChartView instead of QChart.
16 convenience class QChartView instead of QChart.
17 \sa QChartView
17 \sa QChartView
18 */
18 */
19
19
20 QTCOMMERCIALCHART_BEGIN_NAMESPACE
20 QTCOMMERCIALCHART_BEGIN_NAMESPACE
21
21
22 /*!
22 /*!
23 Constructs a chart object which is a child of parent.
23 Constructs a chart object which is a child of parent.
24 */
24 */
25 QChart::QChart(QGraphicsItem *parent, Qt::WindowFlags wFlags) : QGraphicsWidget(parent,wFlags),
25 QChart::QChart(QGraphicsItem *parent, Qt::WindowFlags wFlags) : QGraphicsWidget(parent,wFlags),
26 m_backgroundItem(0),
26 m_backgroundItem(0),
27 m_titleItem(0),
27 m_titleItem(0),
28 m_dataset(new ChartDataSet(this)),
28 m_dataset(new ChartDataSet(this)),
29 m_presenter(new ChartPresenter(this,m_dataset))
29 m_presenter(new ChartPresenter(this,m_dataset))
30 {
30 {
31 }
31 }
32
32
33 /*!
33 /*!
34 Destroys the object and it's children, like QChartSeries and QChartAxis object added to it.
34 Destroys the object and it's children, like QChartSeries and QChartAxis object added to it.
35 */
35 */
36 QChart::~QChart()
36 QChart::~QChart()
37 {
37 {
38 }
38 }
39
39
40 /*!
40 /*!
41 Adds the series and optional y axis onto the chart and takes the ownership of the objects.
41 Adds the series and optional y axis onto the chart and takes the ownership of the objects.
42 If auto scaling is enabled, re-scales the axes the series is bound to (both the x axis and
42 If auto scaling is enabled, re-scales the axes the series is bound to (both the x axis and
43 the y axis).
43 the y axis).
44 */
44 */
45 void QChart::addSeries(QChartSeries* series, QChartAxis* axisY)
45 void QChart::addSeries(QChartSeries* series, QChartAxis* axisY)
46 {
46 {
47 m_dataset->addSeries(series, axisY);
47 m_dataset->addSeries(series, axisY);
48 }
48 }
49
49
50 /*!
50 /*!
51 Removes the QChartSeries specified in a perameter from the QChartView.
51 Removes the QChartSeries specified in a perameter from the QChartView.
52 It releses its ownership of the specified QChartSeries object.
52 It releses its ownership of the specified QChartSeries object.
53 It does not delete the pointed QChartSeries data object
53 It does not delete the pointed QChartSeries data object
54 \sa removeSeries(), removeAllSeries()
54 \sa removeSeries(), removeAllSeries()
55 */
55 */
56 void QChart::removeSeries(QChartSeries* series)
56 void QChart::removeSeries(QChartSeries* series)
57 {
57 {
58 m_dataset->removeSeries(series);
58 m_dataset->removeSeries(series);
59 }
59 }
60
60
61 /*!
61 /*!
62 Removes all the QChartSeries that have been added to the QChartView
62 Removes all the QChartSeries that have been added to the QChartView
63 It also deletes the pointed QChartSeries data objects
63 It also deletes the pointed QChartSeries data objects
64 \sa addSeries(), removeSeries()
64 \sa addSeries(), removeSeries()
65 */
65 */
66 void QChart::removeAllSeries()
66 void QChart::removeAllSeries()
67 {
67 {
68 m_dataset->removeAllSeries();
68 m_dataset->removeAllSeries();
69 }
69 }
70
70
71 void QChart::setChartBackgroundBrush(const QBrush& brush)
71 void QChart::setChartBackgroundBrush(const QBrush& brush)
72 {
72 {
73 createChartBackgroundItem();
73 createChartBackgroundItem();
74 m_backgroundItem->setBrush(brush);
74 m_backgroundItem->setBrush(brush);
75 m_backgroundItem->update();
75 m_backgroundItem->update();
76 }
76 }
77
77
78 void QChart::setChartBackgroundPen(const QPen& pen)
78 void QChart::setChartBackgroundPen(const QPen& pen)
79 {
79 {
80 createChartBackgroundItem();
80 createChartBackgroundItem();
81 m_backgroundItem->setPen(pen);
81 m_backgroundItem->setPen(pen);
82 m_backgroundItem->update();
82 m_backgroundItem->update();
83 }
83 }
84
84
85 /*!
85 /*!
86 Sets the title description text that is rendered above the chart.
86 Sets the title description text that is rendered above the chart.
87 */
87 */
88 void QChart::setChartTitle(const QString& title)
88 void QChart::setChartTitle(const QString& title)
89 {
89 {
90 createChartTitleItem();
90 createChartTitleItem();
91 m_titleItem->setPlainText(title);
91 m_titleItem->setPlainText(title);
92 }
92 }
93
93
94 /*!
94 /*!
95 Sets the font that is used for rendering the description text that is rendered above the chart.
95 Sets the font that is used for rendering the description text that is rendered above the chart.
96 */
96 */
97 void QChart::setChartTitleFont(const QFont& font)
97 void QChart::setChartTitleFont(const QFont& font)
98 {
98 {
99 createChartTitleItem();
99 createChartTitleItem();
100 m_titleItem->setFont(font);
100 m_titleItem->setFont(font);
101 }
101 }
102
102
103 void QChart::createChartBackgroundItem()
103 void QChart::createChartBackgroundItem()
104 {
104 {
105 if(!m_backgroundItem) {
105 if(!m_backgroundItem) {
106 m_backgroundItem = new QGraphicsRectItem(this);
106 m_backgroundItem = new QGraphicsRectItem(this);
107 m_backgroundItem->setPen(Qt::NoPen);
107 m_backgroundItem->setZValue(ChartPresenter::BackgroundZValue);
108 m_backgroundItem->setZValue(ChartPresenter::BackgroundZValue);
108 }
109 }
109 }
110 }
110
111
111 void QChart::createChartTitleItem()
112 void QChart::createChartTitleItem()
112 {
113 {
113 if(!m_titleItem) {
114 if(!m_titleItem) {
114 m_titleItem = new QGraphicsTextItem(this);
115 m_titleItem = new QGraphicsTextItem(this);
115 m_titleItem->setZValue(ChartPresenter::BackgroundZValue);
116 m_titleItem->setZValue(ChartPresenter::BackgroundZValue);
116 }
117 }
117 }
118 }
118
119
119 int QChart::margin() const
120 int QChart::margin() const
120 {
121 {
121 return m_presenter->margin();
122 return m_presenter->margin();
122 }
123 }
123
124
124 void QChart::setMargin(int margin)
125 void QChart::setMargin(int margin)
125 {
126 {
126 m_presenter->setMargin(margin);
127 m_presenter->setMargin(margin);
127 }
128 }
128
129
129 void QChart::setChartTheme(QChart::ChartTheme theme)
130 void QChart::setChartTheme(QChart::ChartTheme theme)
130 {
131 {
131 m_presenter->setChartTheme(theme);
132 m_presenter->setChartTheme(theme);
132 }
133 }
133
134
134 QChart::ChartTheme QChart::chartTheme() const
135 QChart::ChartTheme QChart::chartTheme() const
135 {
136 {
136 return m_presenter->chartTheme();
137 return m_presenter->chartTheme();
137 }
138 }
138
139
139 void QChart::zoomIn()
140 void QChart::zoomIn()
140 {
141 {
141 if (!m_dataset->nextDomain()) {
142 if (!m_dataset->nextDomain()) {
142 QRectF rect = m_presenter->geometry();
143 QRectF rect = m_presenter->geometry();
143 rect.setWidth(rect.width()/2);
144 rect.setWidth(rect.width()/2);
144 rect.setHeight(rect.height()/2);
145 rect.setHeight(rect.height()/2);
145 rect.moveCenter(m_presenter->geometry().center());
146 rect.moveCenter(m_presenter->geometry().center());
146 zoomIn(rect);
147 zoomIn(rect);
147 }
148 }
148 }
149 }
149
150
150 void QChart::zoomIn(const QRectF& rect)
151 void QChart::zoomIn(const QRectF& rect)
151 {
152 {
152 if(!rect.isValid()) return;
153 if(!rect.isValid()) return;
153 QRectF r = rect.normalized();
154 QRectF r = rect.normalized();
154 int margin = m_presenter->margin();
155 int margin = m_presenter->margin();
155 r.translate(-margin, -margin);
156 r.translate(-margin, -margin);
156 m_dataset->addDomain(r,m_presenter->geometry());
157 m_dataset->addDomain(r,m_presenter->geometry());
157 }
158 }
158
159
159 void QChart::zoomOut()
160 void QChart::zoomOut()
160 {
161 {
161 m_dataset->previousDomain();
162 m_dataset->previousDomain();
162 }
163 }
163
164
164 void QChart::zoomReset()
165 void QChart::zoomReset()
165 {
166 {
166 m_dataset->clearDomains();
167 m_dataset->clearDomains();
167 }
168 }
168
169
169 QChartAxis* QChart::axisX() const
170 QChartAxis* QChart::axisX() const
170 {
171 {
171 return m_dataset->axisX();
172 return m_dataset->axisX();
172 }
173 }
173
174
174 QChartAxis* QChart::axisY() const
175 QChartAxis* QChart::axisY() const
175 {
176 {
176 return m_dataset->axisY();
177 return m_dataset->axisY();
177 }
178 }
178
179
179 void QChart::resizeEvent(QGraphicsSceneResizeEvent *event)
180 void QChart::resizeEvent(QGraphicsSceneResizeEvent *event)
180 {
181 {
181
182
182 m_rect = QRectF(QPoint(0,0),event->newSize());
183 m_rect = QRectF(QPoint(0,0),event->newSize());
183 QRectF rect = m_rect.adjusted(margin(),margin(), -margin(), -margin());
184 QRectF rect = m_rect.adjusted(margin(),margin(), -margin(), -margin());
184
185
185 // recalculate title position
186 // recalculate title position
186 if (m_titleItem) {
187 if (m_titleItem) {
187 QPointF center = m_rect.center() -m_titleItem->boundingRect().center();
188 QPointF center = m_rect.center() -m_titleItem->boundingRect().center();
188 m_titleItem->setPos(center.x(),m_rect.top()/2 + margin()/2);
189 m_titleItem->setPos(center.x(),m_rect.top()/2 + margin()/2);
189 }
190 }
190
191
191 //recalculate background gradient
192 //recalculate background gradient
192 if (m_backgroundItem) {
193 if (m_backgroundItem) {
193 m_backgroundItem->setRect(rect);
194 m_backgroundItem->setRect(rect);
194 }
195 }
195
196
196 QGraphicsWidget::resizeEvent(event);
197 QGraphicsWidget::resizeEvent(event);
197 update();
198 update();
198 }
199 }
199
200
200 #include "moc_qchart.cpp"
201 #include "moc_qchart.cpp"
201
202
202 QTCOMMERCIALCHART_END_NAMESPACE
203 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now