##// END OF EJS Templates
barchart PIMPL part 2. Cleaning up leftovers in public api
sauimone -
r962:95a77193fc29
parent child
Show More
@@ -1,215 +1,217
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 "barchartitem_p.h"
21 #include "barchartitem_p.h"
22 #include "bar_p.h"
22 #include "bar_p.h"
23 #include "barlabel_p.h"
23 #include "barlabel_p.h"
24 #include "qbarset.h"
24 #include "qbarset.h"
25 #include "qbarset_p.h"
25 #include "qbarseries.h"
26 #include "qbarseries.h"
27 #include "qbarseries_p.h"
26 #include "qchart.h"
28 #include "qchart.h"
27 #include "qchartaxis.h"
29 #include "qchartaxis.h"
28 #include "qchartaxiscategories.h"
30 #include "qchartaxiscategories.h"
29 #include "chartpresenter_p.h"
31 #include "chartpresenter_p.h"
30 #include "chartanimator_p.h"
32 #include "chartanimator_p.h"
31 #include "chartdataset_p.h"
33 #include "chartdataset_p.h"
32 #include <QToolTip>
34 #include <QToolTip>
33 #include <QPainter>
35 #include <QPainter>
34
36
35 QTCOMMERCIALCHART_BEGIN_NAMESPACE
37 QTCOMMERCIALCHART_BEGIN_NAMESPACE
36
38
37 BarChartItem::BarChartItem(QBarSeries *series, ChartPresenter *presenter) :
39 BarChartItem::BarChartItem(QBarSeries *series, ChartPresenter *presenter) :
38 ChartItem(presenter),
40 ChartItem(presenter),
39 m_layoutSet(false),
41 m_layoutSet(false),
40 m_series(series)
42 m_series(series)
41 {
43 {
42 setFlag(ItemClipsChildrenToShape);
44 setFlag(ItemClipsChildrenToShape);
43 connect(series, SIGNAL(showToolTip(QPoint,QString)), this, SLOT(showToolTip(QPoint,QString)));
45 connect(series->d_func(), SIGNAL(showToolTip(QPoint,QString)), this, SLOT(showToolTip(QPoint,QString)));
44 connect(series, SIGNAL(updatedBars()), this, SLOT(handleLayoutChanged()));
46 connect(series->d_func(), SIGNAL(updatedBars()), this, SLOT(handleLayoutChanged()));
45 connect(series, SIGNAL(restructuredBars()), this, SLOT(handleModelChanged()));
47 connect(series->d_func(), SIGNAL(restructuredBars()), this, SLOT(handleModelChanged()));
46 setZValue(ChartPresenter::BarSeriesZValue);
48 setZValue(ChartPresenter::BarSeriesZValue);
47 dataChanged();
49 dataChanged();
48 }
50 }
49
51
50 BarChartItem::~BarChartItem()
52 BarChartItem::~BarChartItem()
51 {
53 {
52 disconnect(this,SLOT(showToolTip(QPoint,QString)));
54 disconnect(this,SLOT(showToolTip(QPoint,QString)));
53 }
55 }
54
56
55 void BarChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
57 void BarChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
56 {
58 {
57 Q_UNUSED(painter);
59 Q_UNUSED(painter);
58 Q_UNUSED(option);
60 Q_UNUSED(option);
59 Q_UNUSED(widget);
61 Q_UNUSED(widget);
60 }
62 }
61
63
62 QRectF BarChartItem::boundingRect() const
64 QRectF BarChartItem::boundingRect() const
63 {
65 {
64 return m_rect.translated(-m_rect.topLeft());
66 return m_rect.translated(-m_rect.topLeft());
65 }
67 }
66
68
67 void BarChartItem::dataChanged()
69 void BarChartItem::dataChanged()
68 {
70 {
69 // TODO: performance optimizations. Do we really need to delete and create items every time data is changed or can we reuse them?
71 // TODO: performance optimizations. Do we really need to delete and create items every time data is changed or can we reuse them?
70 foreach(QGraphicsItem *item, childItems()) {
72 foreach(QGraphicsItem *item, childItems()) {
71 delete item;
73 delete item;
72 }
74 }
73
75
74 m_bars.clear();
76 m_bars.clear();
75 m_labels.clear();
77 m_labels.clear();
76 m_layout.clear();
78 m_layout.clear();
77
79
78 // Create new graphic items for bars
80 // Create new graphic items for bars
79 for (int c = 0; c < m_series->categoryCount(); c++) {
81 for (int c = 0; c < m_series->categoryCount(); c++) {
80 QString category = m_series->categoryName(c);
82 QString category = m_series->d_func()->categoryName(c);
81 for (int s = 0; s < m_series->barsetCount(); s++) {
83 for (int s = 0; s < m_series->barsetCount(); s++) {
82 QBarSet *set = m_series->barsetAt(s);
84 QBarSet *set = m_series->d_func()->barsetAt(s);
83 Bar *bar = new Bar(category,this);
85 Bar *bar = new Bar(category,this);
84 m_bars.append(bar);
86 m_bars.append(bar);
85 connect(bar, SIGNAL(clicked(QString,Qt::MouseButtons)), set, SIGNAL(clicked(QString,Qt::MouseButtons)));
87 connect(bar, SIGNAL(clicked(QString,Qt::MouseButtons)), set, SIGNAL(clicked(QString,Qt::MouseButtons)));
86 connect(bar, SIGNAL(hoverEntered(QPoint)), set, SLOT(barHoverEnterEvent(QPoint)));
88 connect(bar, SIGNAL(hoverEntered(QPoint)), set->d_ptr.data(), SLOT(barHoverEnterEvent(QPoint)));
87 connect(bar, SIGNAL(hoverLeaved()), set, SLOT(barHoverLeaveEvent()));
89 connect(bar, SIGNAL(hoverLeaved()), set->d_ptr.data(), SLOT(barHoverLeaveEvent()));
88 m_layout.append(QRectF(0, 0, 0, 0));
90 m_layout.append(QRectF(0, 0, 0, 0));
89 }
91 }
90 }
92 }
91
93
92 // Create labels
94 // Create labels
93 for (int category = 0; category < m_series->categoryCount(); category++) {
95 for (int category = 0; category < m_series->categoryCount(); category++) {
94 for (int s = 0; s < m_series->barsetCount(); s++) {
96 for (int s = 0; s < m_series->barsetCount(); s++) {
95 QBarSet *set = m_series->barsetAt(s);
97 QBarSet *set = m_series->d_func()->barsetAt(s);
96 BarLabel *value = new BarLabel(*set, this);
98 BarLabel *value = new BarLabel(*set, this);
97 m_labels.append(value);
99 m_labels.append(value);
98 connect(set,SIGNAL(labelsVisibleChanged(bool)),value,SLOT(labelsVisibleChanged(bool)));
100 connect(set,SIGNAL(labelsVisibleChanged(bool)),value,SLOT(labelsVisibleChanged(bool)));
99 }
101 }
100 }
102 }
101 }
103 }
102
104
103 QVector<QRectF> BarChartItem::calculateLayout()
105 QVector<QRectF> BarChartItem::calculateLayout()
104 {
106 {
105 QVector<QRectF> layout;
107 QVector<QRectF> layout;
106
108
107 // Use temporary qreals for accurancy
109 // Use temporary qreals for accurancy
108 qreal categoryCount = m_series->categoryCount();
110 qreal categoryCount = m_series->categoryCount();
109 qreal setCount = m_series->barsetCount();
111 qreal setCount = m_series->barsetCount();
110
112
111 // Domain:
113 // Domain:
112 qreal width = geometry().width();
114 qreal width = geometry().width();
113 qreal height = geometry().height();
115 qreal height = geometry().height();
114 qreal range = m_domainMaxY - m_domainMinY;
116 qreal range = m_domainMaxY - m_domainMinY;
115 qreal scale = (height / range);
117 qreal scale = (height / range);
116 qreal categoryWidth = width / categoryCount;
118 qreal categoryWidth = width / categoryCount;
117 qreal barWidth = categoryWidth / (setCount+1);
119 qreal barWidth = categoryWidth / (setCount+1);
118
120
119 int itemIndex(0);
121 int itemIndex(0);
120 for (int category = 0; category < categoryCount; category++) {
122 for (int category = 0; category < categoryCount; category++) {
121 qreal xPos = categoryWidth * category + barWidth / 2;
123 qreal xPos = categoryWidth * category + barWidth / 2;
122 qreal yPos = height + scale * m_domainMinY;
124 qreal yPos = height + scale * m_domainMinY;
123 for (int set = 0; set < setCount; set++) {
125 for (int set = 0; set < setCount; set++) {
124 QBarSet* barSet = m_series->barsetAt(set);
126 QBarSet* barSet = m_series->d_func()->barsetAt(set);
125
127
126 qreal barHeight = barSet->valueAt(category) * scale;
128 qreal barHeight = barSet->valueAt(category) * scale;
127 Bar* bar = m_bars.at(itemIndex);
129 Bar* bar = m_bars.at(itemIndex);
128
130
129 QRectF rect(xPos, yPos - barHeight, barWidth, barHeight);
131 QRectF rect(xPos, yPos - barHeight, barWidth, barHeight);
130 layout.append(rect);
132 layout.append(rect);
131 bar->setPen(barSet->pen());
133 bar->setPen(barSet->pen());
132 bar->setBrush(barSet->brush());
134 bar->setBrush(barSet->brush());
133
135
134 BarLabel* label = m_labels.at(itemIndex);
136 BarLabel* label = m_labels.at(itemIndex);
135
137
136 if (!qFuzzyIsNull(barSet->valueAt(category))) {
138 if (!qFuzzyIsNull(barSet->valueAt(category))) {
137 label->setText(QString::number(barSet->valueAt(category)));
139 label->setText(QString::number(barSet->valueAt(category)));
138 } else {
140 } else {
139 label->setText(QString(""));
141 label->setText(QString(""));
140 }
142 }
141
143
142 label->setPos(xPos + (rect.width()/2 - label->boundingRect().width()/2)
144 label->setPos(xPos + (rect.width()/2 - label->boundingRect().width()/2)
143 ,yPos - barHeight/2 - label->boundingRect().height()/2);
145 ,yPos - barHeight/2 - label->boundingRect().height()/2);
144 label->setFont(barSet->labelFont());
146 label->setFont(barSet->labelFont());
145
147
146 itemIndex++;
148 itemIndex++;
147 xPos += barWidth;
149 xPos += barWidth;
148 }
150 }
149 }
151 }
150 return layout;
152 return layout;
151 }
153 }
152
154
153 void BarChartItem::applyLayout(const QVector<QRectF> &layout)
155 void BarChartItem::applyLayout(const QVector<QRectF> &layout)
154 {
156 {
155 if (animator())
157 if (animator())
156 animator()->updateLayout(this, m_layout, layout);
158 animator()->updateLayout(this, m_layout, layout);
157 else
159 else
158 setLayout(layout);
160 setLayout(layout);
159 }
161 }
160
162
161 void BarChartItem::setLayout(const QVector<QRectF> &layout)
163 void BarChartItem::setLayout(const QVector<QRectF> &layout)
162 {
164 {
163 m_layout = layout;
165 m_layout = layout;
164
166
165 for (int i=0; i < m_bars.count(); i++)
167 for (int i=0; i < m_bars.count(); i++)
166 m_bars.at(i)->setRect(layout.at(i));
168 m_bars.at(i)->setRect(layout.at(i));
167
169
168 update();
170 update();
169 }
171 }
170 //handlers
172 //handlers
171
173
172 void BarChartItem::handleModelChanged()
174 void BarChartItem::handleModelChanged()
173 {
175 {
174 dataChanged();
176 dataChanged();
175 }
177 }
176
178
177 void BarChartItem::handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY)
179 void BarChartItem::handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY)
178 {
180 {
179 m_domainMinX = minX;
181 m_domainMinX = minX;
180 m_domainMaxX = maxX;
182 m_domainMaxX = maxX;
181 m_domainMinY = minY;
183 m_domainMinY = minY;
182 m_domainMaxY = maxY;
184 m_domainMaxY = maxY;
183 handleLayoutChanged();
185 handleLayoutChanged();
184 }
186 }
185
187
186 void BarChartItem::handleGeometryChanged(const QRectF &rect)
188 void BarChartItem::handleGeometryChanged(const QRectF &rect)
187 {
189 {
188 prepareGeometryChange();
190 prepareGeometryChange();
189 m_clipRect = rect.translated(-rect.topLeft());
191 m_clipRect = rect.translated(-rect.topLeft());
190 m_rect = rect;
192 m_rect = rect;
191 handleLayoutChanged();
193 handleLayoutChanged();
192 m_layoutSet = true;
194 m_layoutSet = true;
193 setPos(rect.topLeft());
195 setPos(rect.topLeft());
194 }
196 }
195
197
196 void BarChartItem::handleLayoutChanged()
198 void BarChartItem::handleLayoutChanged()
197 {
199 {
198 if ((m_rect.width() <= 0) || (m_rect.height() <= 0)) {
200 if ((m_rect.width() <= 0) || (m_rect.height() <= 0)) {
199 // rect size zero.
201 // rect size zero.
200 return;
202 return;
201 }
203 }
202 QVector<QRectF> layout = calculateLayout();
204 QVector<QRectF> layout = calculateLayout();
203 applyLayout(layout);
205 applyLayout(layout);
204 update();
206 update();
205 }
207 }
206
208
207 void BarChartItem::showToolTip(QPoint pos, QString tip)
209 void BarChartItem::showToolTip(QPoint pos, QString tip)
208 {
210 {
209 // TODO: cool tooltip instead of default
211 // TODO: cool tooltip instead of default
210 QToolTip::showText(pos, tip);
212 QToolTip::showText(pos, tip);
211 }
213 }
212
214
213 #include "moc_barchartitem_p.cpp"
215 #include "moc_barchartitem_p.cpp"
214
216
215 QTCOMMERCIALCHART_END_NAMESPACE
217 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,89 +1,90
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 "percentbarchartitem_p.h"
21 #include "percentbarchartitem_p.h"
22 #include "bar_p.h"
22 #include "bar_p.h"
23 #include "barlabel_p.h"
23 #include "barlabel_p.h"
24 #include "qbarseries_p.h"
24 #include "qbarset.h"
25 #include "qbarset.h"
25 #include <QDebug>
26 #include <QDebug>
26
27
27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28
29
29 PercentBarChartItem::PercentBarChartItem(QBarSeries *series, ChartPresenter *presenter) :
30 PercentBarChartItem::PercentBarChartItem(QBarSeries *series, ChartPresenter *presenter) :
30 BarChartItem(series, presenter)
31 BarChartItem(series, presenter)
31 {
32 {
32 }
33 }
33
34
34 QVector<QRectF> PercentBarChartItem::calculateLayout()
35 QVector<QRectF> PercentBarChartItem::calculateLayout()
35 {
36 {
36 QVector<QRectF> layout;
37 QVector<QRectF> layout;
37
38
38 // Use temporary qreals for accurancy
39 // Use temporary qreals for accurancy
39 qreal width = geometry().width();
40 qreal width = geometry().width();
40 qreal height = geometry().height();
41 qreal height = geometry().height();
41
42
42 qreal categoryCount = m_series->categoryCount();
43 qreal categoryCount = m_series->categoryCount();
43 qreal barWidth = width / (m_series->categoryCount() * 2);
44 qreal barWidth = width / (m_series->categoryCount() * 2);
44 qreal xStep = width / categoryCount;
45 qreal xStep = width / categoryCount;
45 qreal xPos = xStep / 2 - barWidth / 2;
46 qreal xPos = xStep / 2 - barWidth / 2;
46
47
47 qreal range = m_domainMaxY - m_domainMinY;
48 qreal range = m_domainMaxY - m_domainMinY;
48 qreal domainScale = (height / range);
49 qreal domainScale = (height / range);
49
50
50 int itemIndex(0);
51 int itemIndex(0);
51 for (int category = 0; category < categoryCount; category++) {
52 for (int category = 0; category < categoryCount; category++) {
52 qreal colSum = m_series->categorySum(category);
53 qreal colSum = m_series->d_func()->categorySum(category);
53 qreal percentage = (100 / colSum);
54 qreal percentage = (100 / colSum);
54 qreal yPos = height + domainScale * m_domainMinY;
55 qreal yPos = height + domainScale * m_domainMinY;
55 for (int set=0; set < m_series->barsetCount(); set++) {
56 for (int set=0; set < m_series->barsetCount(); set++) {
56 QBarSet* barSet = m_series->barsetAt(set);
57 QBarSet* barSet = m_series->d_func()->barsetAt(set);
57 qreal barHeight = barSet->valueAt(category) * percentage * domainScale;
58 qreal barHeight = barSet->valueAt(category) * percentage * domainScale;
58 Bar* bar = m_bars.at(itemIndex);
59 Bar* bar = m_bars.at(itemIndex);
59 bar->setPen(barSet->pen());
60 bar->setPen(barSet->pen());
60 bar->setBrush(barSet->brush());
61 bar->setBrush(barSet->brush());
61 QRectF rect(xPos, yPos-barHeight, barWidth, barHeight);
62 QRectF rect(xPos, yPos-barHeight, barWidth, barHeight);
62 layout.append(rect);
63 layout.append(rect);
63
64
64 BarLabel* label = m_labels.at(itemIndex);
65 BarLabel* label = m_labels.at(itemIndex);
65
66
66 if (!qFuzzyIsNull(m_series->valueAt(set,category))) {
67 if (!qFuzzyIsNull(m_series->d_func()->valueAt(set,category))) {
67 int p = m_series->percentageAt(set,category) * 100;
68 int p = m_series->d_func()->percentageAt(set,category) * 100;
68 QString vString(QString::number(p));
69 QString vString(QString::number(p));
69 vString.truncate(3);
70 vString.truncate(3);
70 vString.append("%");
71 vString.append("%");
71 label->setText(vString);
72 label->setText(vString);
72 } else {
73 } else {
73 label->setText(QString(""));
74 label->setText(QString(""));
74 }
75 }
75
76
76 label->setPos(xPos + (rect.width()/2 - label->boundingRect().width()/2)
77 label->setPos(xPos + (rect.width()/2 - label->boundingRect().width()/2)
77 ,yPos - barHeight/2 - label->boundingRect().height()/2);
78 ,yPos - barHeight/2 - label->boundingRect().height()/2);
78 label->setFont(barSet->labelFont());
79 label->setFont(barSet->labelFont());
79 itemIndex++;
80 itemIndex++;
80 yPos -= barHeight;
81 yPos -= barHeight;
81 }
82 }
82 xPos += xStep;
83 xPos += xStep;
83 }
84 }
84 return layout;
85 return layout;
85 }
86 }
86
87
87 #include "moc_percentbarchartitem_p.cpp"
88 #include "moc_percentbarchartitem_p.cpp"
88
89
89 QTCOMMERCIALCHART_END_NAMESPACE
90 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,824 +1,535
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 "qbarseries.h"
21 #include "qbarseries.h"
22 #include "qbarseries_p.h"
22 #include "qbarseries_p.h"
23 #include "qbarset.h"
23 #include "qbarset.h"
24 #include "qbarset_p.h"
24 #include "qbarset_p.h"
25 #include "barchartmodel_p.h"
25 #include "barchartmodel_p.h"
26 #include "domain_p.h"
26 #include "domain_p.h"
27 #include "legendmarker_p.h"
27 #include "legendmarker_p.h"
28 #include "chartdataset_p.h"
28 #include "chartdataset_p.h"
29 #include "charttheme_p.h"
29 #include "charttheme_p.h"
30 #include "chartanimator_p.h"
30 #include "chartanimator_p.h"
31
31
32 #include <QAbstractItemModel>
32 #include <QAbstractItemModel>
33 #include <QModelIndex>
33 #include <QModelIndex>
34
34
35 QTCOMMERCIALCHART_BEGIN_NAMESPACE
35 QTCOMMERCIALCHART_BEGIN_NAMESPACE
36
36
37 /*!
37 /*!
38 \class QBarSeries
38 \class QBarSeries
39 \brief part of QtCommercial chart API.
39 \brief part of QtCommercial chart API.
40
40
41 QBarSeries represents a series of data shown as bars. One QBarSeries can contain multible
41 QBarSeries represents a series of data shown as bars. One QBarSeries can contain multible
42 QBarSet data sets. QBarSeries groups the data from sets to categories, which are defined
42 QBarSet data sets. QBarSeries groups the data from sets to categories, which are defined
43 by QStringList.
43 by QStringList.
44
44
45 \mainclass
45 \mainclass
46
46
47 \sa QBarSet, QStackedBarSeries, QPercentBarSeries
47 \sa QBarSet, QStackedBarSeries, QPercentBarSeries
48 */
48 */
49
49
50 /*!
50 /*!
51 \fn virtual QSeriesType QBarSeries::type() const
51 \fn virtual QSeriesType QBarSeries::type() const
52 \brief Returns type of series.
52 \brief Returns type of series.
53 \sa QSeries, QSeriesType
53 \sa QSeries, QSeriesType
54 */
54 */
55
55
56 /*!
56 /*!
57 \fn void QBarSeries::showToolTip(QPoint pos, QString tip)
57 \fn void QBarSeries::showToolTip(QPoint pos, QString tip)
58 \brief \internal \a pos \a tip
58 \brief \internal \a pos \a tip
59 */
59 */
60
60
61 /*!
61 /*!
62 Constructs empty QBarSeries. Parameter \a categories defines the categories for chart.
62 Constructs empty QBarSeries. Parameter \a categories defines the categories for chart.
63 QBarSeries is QObject which is a child of a \a parent.
63 QBarSeries is QObject which is a child of a \a parent.
64 */
64 */
65
65
66 QBarSeries::QBarSeries(QBarCategories categories, QObject *parent) : QSeries(*new QBarSeriesPrivate(categories, this),parent)
66 QBarSeries::QBarSeries(QBarCategories categories, QObject *parent) : QSeries(*new QBarSeriesPrivate(categories, this),parent)
67 {
67 {
68
68
69 }
69 }
70
70
71 QBarSeries::QBarSeries(QBarSeriesPrivate &d,QObject *parent) : QSeries(d,parent)
71 QBarSeries::QBarSeries(QBarSeriesPrivate &d,QObject *parent) : QSeries(d,parent)
72 {
72 {
73
73
74 }
74 }
75
75
76 QSeries::QSeriesType QBarSeries::type() const
76 QSeries::QSeriesType QBarSeries::type() const
77 {
77 {
78 return QSeries::SeriesTypeBar;
78 return QSeries::SeriesTypeBar;
79 }
79 }
80
80
81 /*!
81 /*!
82 Adds a set of bars to series. Takes ownership of \a set.
82 Adds a set of bars to series. Takes ownership of \a set.
83 Connects the clicked(QString, Qt::MouseButtons) signal
83 Connects the clicked(QString, Qt::MouseButtons) signal
84 of \a set to this series
84 of \a set to this series
85 */
85 */
86 void QBarSeries::appendBarSet(QBarSet *set)
86 void QBarSeries::appendBarSet(QBarSet *set)
87 {
87 {
88 Q_D(QBarSeries);
88 Q_D(QBarSeries);
89 d->m_internalModel->appendBarSet(set);
89 d->m_internalModel->appendBarSet(set);
90 QObject::connect(set->d_ptr.data(), SIGNAL(clicked(QString,Qt::MouseButtons)), d, SLOT(barsetClicked(QString,Qt::MouseButtons)));
90 QObject::connect(set->d_ptr.data(), SIGNAL(clicked(QString,Qt::MouseButtons)), d, SLOT(barsetClicked(QString,Qt::MouseButtons)));
91 QObject::connect(set->d_ptr.data(), SIGNAL(valueChanged()), d, SLOT(barsetChanged()));
91 QObject::connect(set->d_ptr.data(), SIGNAL(valueChanged()), d, SLOT(barsetChanged()));
92 emit d->restructuredBars();
92 emit d->restructuredBars();
93 }
93 }
94
94
95 /*!
95 /*!
96 Removes a set of bars from series. Releases ownership of \a set. Doesnt delete \a set.
96 Removes a set of bars from series. Releases ownership of \a set. Doesnt delete \a set.
97 Disconnects the clicked(QString, Qt::MouseButtons) signal
97 Disconnects the clicked(QString, Qt::MouseButtons) signal
98 of \a set from this series
98 of \a set from this series
99 */
99 */
100 void QBarSeries::removeBarSet(QBarSet *set)
100 void QBarSeries::removeBarSet(QBarSet *set)
101 {
101 {
102 Q_D(QBarSeries);
102 Q_D(QBarSeries);
103 QObject::disconnect(set->d_ptr.data(), SIGNAL(clicked(QString,Qt::MouseButtons)), d, SLOT(barsetClicked(QString,Qt::MouseButtons)));
103 QObject::disconnect(set->d_ptr.data(), SIGNAL(clicked(QString,Qt::MouseButtons)), d, SLOT(barsetClicked(QString,Qt::MouseButtons)));
104 d->m_internalModel->removeBarSet(set);
104 d->m_internalModel->removeBarSet(set);
105 emit d->restructuredBars();
105 emit d->restructuredBars();
106 }
106 }
107
107
108 /*!
108 /*!
109 Adds a list of barsets to series. Takes ownership of \a sets.
109 Adds a list of barsets to series. Takes ownership of \a sets.
110 Connects the clicked(QString, Qt::MouseButtons) signals
110 Connects the clicked(QString, Qt::MouseButtons) signals
111 of \a sets to this series
111 of \a sets to this series
112 */
112 */
113 void QBarSeries::appendBarSets(QList<QBarSet* > sets)
113 void QBarSeries::appendBarSets(QList<QBarSet* > sets)
114 {
114 {
115 Q_D(QBarSeries);
115 Q_D(QBarSeries);
116 foreach (QBarSet* barset, sets) {
116 foreach (QBarSet* barset, sets) {
117 d->m_internalModel->appendBarSet(barset);
117 d->m_internalModel->appendBarSet(barset);
118 QObject::connect(barset, SIGNAL(clicked(QString,Qt::MouseButtons)), this, SLOT(barsetClicked(QString,Qt::MouseButtons)));
118 QObject::connect(barset, SIGNAL(clicked(QString,Qt::MouseButtons)), this, SLOT(barsetClicked(QString,Qt::MouseButtons)));
119 QObject::connect(barset, SIGNAL(valueChanged()), this, SLOT(barsetChanged()));
119 QObject::connect(barset, SIGNAL(valueChanged()), this, SLOT(barsetChanged()));
120 }
120 }
121 emit d->restructuredBars();
121 emit d->restructuredBars();
122
122
123 }
123 }
124
124
125 /*!
125 /*!
126 Removes a list of barsets from series. Releases ownership of \a sets. Doesnt delete \a sets.
126 Removes a list of barsets from series. Releases ownership of \a sets. Doesnt delete \a sets.
127 Disconnects the clicked(QString, Qt::MouseButtons) signal
127 Disconnects the clicked(QString, Qt::MouseButtons) signal
128 of \a sets from this series
128 of \a sets from this series
129 */
129 */
130 void QBarSeries::removeBarSets(QList<QBarSet* > sets)
130 void QBarSeries::removeBarSets(QList<QBarSet* > sets)
131 {
131 {
132 Q_D(QBarSeries);
132 Q_D(QBarSeries);
133
133
134 foreach (QBarSet* barset, sets) {
134 foreach (QBarSet* barset, sets) {
135 QObject::disconnect(barset, SIGNAL(clicked(QString,Qt::MouseButtons)), this, SLOT(barsetClicked(QString,Qt::MouseButtons)));
135 QObject::disconnect(barset, SIGNAL(clicked(QString,Qt::MouseButtons)), this, SLOT(barsetClicked(QString,Qt::MouseButtons)));
136 d->m_internalModel->removeBarSet(barset);
136 d->m_internalModel->removeBarSet(barset);
137 }
137 }
138 emit d->restructuredBars();
138 emit d->restructuredBars();
139 }
139 }
140
140
141 /*!
141 /*!
142 Inserts new \a set on the \a i position.
142 Inserts new \a set on the \a i position.
143 The barset that is currently at this postion is moved to postion i + 1
143 The barset that is currently at this postion is moved to postion i + 1
144 */
144 */
145 void QBarSeries::insertBarSet(int i, QBarSet *set)
145 void QBarSeries::insertBarSet(int i, QBarSet *set)
146 {
146 {
147 Q_D(QBarSeries);
147 Q_D(QBarSeries);
148 d->m_internalModel->insertBarSet(i, set);
148 d->m_internalModel->insertBarSet(i, set);
149 emit d->barsetChanged();
149 emit d->barsetChanged();
150 }
150 }
151
151
152 /*!
152 /*!
153 Inserts new \a category on the \a i position.
153 Inserts new \a category on the \a i position.
154 The category that is currently at this postion is moved to postion i + 1
154 The category that is currently at this postion is moved to postion i + 1
155 \sa removeCategory()
155 \sa removeCategory()
156 */
156 */
157 void QBarSeries::insertCategory(int i, QString category)
157 void QBarSeries::insertCategory(int i, QString category)
158 {
158 {
159 Q_D(QBarSeries);
159 Q_D(QBarSeries);
160 d->m_internalModel->insertCategory(i, category);
160 d->m_internalModel->insertCategory(i, category);
161 }
161 }
162
162
163 /*!
163 /*!
164 Removes the category specified by \a i
164 Removes the category specified by \a i
165 \sa insertCategory()
165 \sa insertCategory()
166 */
166 */
167 void QBarSeries::removeCategory(int i)
167 void QBarSeries::removeCategory(int i)
168 {
168 {
169 Q_D(QBarSeries);
169 Q_D(QBarSeries);
170 d->m_internalModel->removeCategory(i);
170 d->m_internalModel->removeCategory(i);
171 }
171 }
172
172
173 /*!
173 /*!
174 Returns number of sets in series.
174 Returns number of sets in series.
175 */
175 */
176 int QBarSeries::barsetCount() const
176 int QBarSeries::barsetCount() const
177 {
177 {
178 Q_D(const QBarSeries);
178 Q_D(const QBarSeries);
179 /*
180 // if(m_model)
181 // return m_mapBarTop - m_mapBarBottom;
182 // else
183
184 */
185 return d->m_internalModel->barsetCount();
179 return d->m_internalModel->barsetCount();
186 }
180 }
187
181
188 /*!
182 /*!
189 Returns number of categories in series
183 Returns number of categories in series
190 */
184 */
191 int QBarSeries::categoryCount() const
185 int QBarSeries::categoryCount() const
192 {
186 {
193 Q_D(const QBarSeries);
187 Q_D(const QBarSeries);
194 return d->m_internalModel->categoryCount();
188 return d->m_internalModel->categoryCount();
195 }
189 }
196
190
197 /*!
191 /*!
198 Returns a list of sets in series. Keeps ownership of sets.
192 Returns a list of sets in series. Keeps ownership of sets.
199 */
193 */
200 QList<QBarSet*> QBarSeries::barSets() const
194 QList<QBarSet*> QBarSeries::barSets() const
201 {
195 {
202 Q_D(const QBarSeries);
196 Q_D(const QBarSeries);
203 return d->m_internalModel->barSets();
197 return d->m_internalModel->barSets();
204 }
198 }
205
199
206 /*!
200 /*!
207 \internal \a index
208 */
209 QBarSet* QBarSeries::barsetAt(int index)
210 {
211 Q_D(QBarSeries);
212 return d->barsetAt(index);
213 // return m_internalModel->barsetAt(index);
214 }
215
216 /*!
217 \internal \a category
218 */
219 QString QBarSeries::categoryName(int category)
220 {
221 Q_D(QBarSeries);
222 return d->categoryName(category);
223 // return m_internalModel->categoryName(category);
224 }
225
226 /*!
227 Enables or disables tooltip depending on parameter \a enabled.
201 Enables or disables tooltip depending on parameter \a enabled.
228 Tooltip shows the name of set, when mouse is hovering on top of bar.
202 Tooltip shows the name of set, when mouse is hovering on top of bar.
229 Calling without parameter \a enabled, enables the tooltip
203 Calling without parameter \a enabled, enables the tooltip
230 */
204 */
231 void QBarSeries::setToolTipEnabled(bool enabled)
205 void QBarSeries::setToolTipEnabled(bool enabled)
232 {
206 {
233 Q_D(QBarSeries);
207 Q_D(QBarSeries);
234 d->setToolTipEnabled(enabled);
208 d->setToolTipEnabled(enabled);
235 /*
236 // TODO: what if we add sets after call to this function? Those sets won't have tooltip enabled.
237 if (enabled) {
238 for (int i=0; i<m_internalModel->barsetCount(); i++) {
239 QBarSet *set = m_internalModel->barsetAt(i);
240 connect(set, SIGNAL(showToolTip(QPoint,QString)), this, SIGNAL(showToolTip(QPoint,QString)));
241 }
242 } else {
243 for (int i=0; i<m_internalModel->barsetCount(); i++) {
244 QBarSet *set = m_internalModel->barsetAt(i);
245 disconnect(set, SIGNAL(showToolTip(QPoint,QString)), this, SIGNAL(showToolTip(QPoint,QString)));
246 }
247 }
248 */
249 }
250
251
252 /*!
253 \internal \a category
254 */
255 void QBarSeries::barsetClicked(QString category, Qt::MouseButtons button)
256 {
257 Q_D(QBarSeries);
258 d->barsetClicked(category,button);
259 // emit clicked(qobject_cast<QBarSet*>(sender()), category, button);
260 }
261
262 /*!
263 \internal
264 */
265 qreal QBarSeries::min()
266 {
267 Q_D(QBarSeries);
268 return d->min();
269 //return m_internalModel->min();
270 }
271
272 /*!
273 \internal
274 */
275 qreal QBarSeries::max()
276 {
277 Q_D(QBarSeries);
278 return d->max();
279 // return m_internalModel->max();
280 }
281
282 /*!
283 \internal \a set \a category
284 */
285 qreal QBarSeries::valueAt(int set, int category)
286 {
287 Q_D(QBarSeries);
288 return d->valueAt(set,category);
289 // return m_internalModel->valueAt(set, category);
290 }
291
292 /*!
293 \internal \a set \a category
294 */
295 qreal QBarSeries::percentageAt(int set, int category)
296 {
297 Q_D(QBarSeries);
298 return d->percentageAt(set,category);
299 // return m_internalModel->percentageAt(set, category);
300 }
301
302 /*!
303 \internal \a category
304 */
305 qreal QBarSeries::categorySum(int category)
306 {
307 Q_D(QBarSeries);
308 return d->categorySum(category);
309 // return m_internalModel->categorySum(category);
310 }
311
312 /*!
313 \internal \a category
314 */
315 qreal QBarSeries::absoluteCategorySum(int category)
316 {
317 Q_D(QBarSeries);
318 return d->absoluteCategorySum(category);
319 // return m_internalModel->absoluteCategorySum(category);
320 }
321
322 /*!
323 \internal
324 */
325 qreal QBarSeries::maxCategorySum()
326 {
327 Q_D(QBarSeries);
328 return d->maxCategorySum();
329 // return m_internalModel->maxCategorySum();
330 }
331
332 /*!
333 \internal
334 */
335 BarChartModel& QBarSeries::modelInternal()
336 {
337 Q_D(QBarSeries);
338 return d->modelInternal();
339 // return *m_internalModel;
340 }
209 }
341
210
342 /*!
211 /*!
343 \fn bool QBarSeries::setModel(QAbstractItemModel *model)
212 \fn bool QBarSeries::setModel(QAbstractItemModel *model)
344 Sets the \a model to be used as a data source
213 Sets the \a model to be used as a data source
345 */
214 */
346 bool QBarSeries::setModel(QAbstractItemModel *model)
215 bool QBarSeries::setModel(QAbstractItemModel *model)
347 {
216 {
348 Q_D(QBarSeries);
217 Q_D(QBarSeries);
349 return d->setModel(model);
218 return d->setModel(model);
350 /*
351 // disconnect signals from old model
352 if(m_model)
353 {
354 disconnect(m_model, 0, this, 0);
355 m_mapCategories = -1;
356 m_mapBarBottom = -1;
357 m_mapBarTop = -1;
358 m_mapFirst = 0;
359 m_mapCount = 0;
360 m_mapOrientation = Qt::Vertical;
361 }
362
363 // set new model
364 if(model)
365 {
366 m_model = model;
367 return true;
368 }
369 else
370 {
371 m_model = 0;
372 return false;
373 }
374 */
375 }
219 }
376
220
377 /*!
221 /*!
378 \fn bool QBarSeries::setModelMapping(int categories, int bottomBoundry, int topBoundry, Qt::Orientation orientation)
222 \fn bool QBarSeries::setModelMapping(int categories, int bottomBoundry, int topBoundry, Qt::Orientation orientation)
379 Sets column/row specified by \a categories to be used as a list of bar series categories.
223 Sets column/row specified by \a categories to be used as a list of bar series categories.
380 Parameter \a bottomBoundry indicates the column/row where the first bar set is located in the model.
224 Parameter \a bottomBoundry indicates the column/row where the first bar set is located in the model.
381 Parameter \a topBoundry indicates the column/row where the last bar set is located in the model.
225 Parameter \a topBoundry indicates the column/row where the last bar set is located in the model.
382 All the columns/rows inbetween those two values are also used as data for bar sets.
226 All the columns/rows inbetween those two values are also used as data for bar sets.
383 The \a orientation paramater specifies whether the data is in columns or in rows.
227 The \a orientation paramater specifies whether the data is in columns or in rows.
384 */
228 */
385 void QBarSeries::setModelMapping(int categories, int bottomBoundary, int topBoundary, Qt::Orientation orientation)
229 void QBarSeries::setModelMapping(int categories, int bottomBoundary, int topBoundary, Qt::Orientation orientation)
386 {
230 {
387 Q_D(QBarSeries);
231 Q_D(QBarSeries);
388 d->setModelMapping(categories,bottomBoundary,topBoundary,orientation);
232 d->setModelMapping(categories,bottomBoundary,topBoundary,orientation);
389 /*
390 if (!m_model)
391 return;
392
393 m_mapCategories = categories;
394 m_mapBarBottom = bottomBoundry;
395 m_mapBarTop = topBoundry;
396 // m_mapFirst = 1;
397 m_mapOrientation = orientation;
398
399 // connect the signals
400 if (m_mapOrientation == Qt::Vertical) {
401 m_mapCount = m_model->rowCount() - m_mapFirst;
402 connect(m_model,SIGNAL(dataChanged(QModelIndex,QModelIndex)),
403 this, SLOT(modelUpdated(QModelIndex, QModelIndex)));
404 connect(m_model,SIGNAL(rowsInserted(QModelIndex, int, int)),
405 this, SLOT(modelDataAdded(QModelIndex,int,int)));
406 connect(m_model, SIGNAL(rowsRemoved(QModelIndex, int, int)),
407 this, SLOT(modelDataRemoved(QModelIndex,int,int)));
408 } else {
409 m_mapCount = m_model->columnCount() - m_mapFirst;
410 connect(m_model,SIGNAL(dataChanged(QModelIndex,QModelIndex)),
411 this, SLOT(modelUpdated(QModelIndex, QModelIndex)));
412 connect(m_model,SIGNAL(columnsInserted(QModelIndex, int, int)),
413 this, SLOT(modelDataAdded(QModelIndex,int,int)));
414 connect(m_model, SIGNAL(columnsRemoved(QModelIndex, int, int)),
415 this, SLOT(modelDataRemoved(QModelIndex,int,int)));
416 }
417
418 // create the initial bars
419 delete m_internalModel;
420 if (m_mapOrientation == Qt::Vertical) {
421 QStringList categories;
422 for (int k = m_mapFirst; k < m_mapFirst + m_mapCount; k++)
423 categories << m_model->data(m_model->index(k, m_mapCategories), Qt::DisplayRole).toString();
424 m_internalModel = new BarChartModel(categories, this);
425
426 for (int i = m_mapBarBottom; i <= m_mapBarTop; i++) {
427 QBarSet* barSet = new QBarSet(QString("Column: %1").arg(i + 1));
428 for(int m = m_mapFirst; m < m_mapFirst + m_mapCount; m++)
429 *barSet << m_model->data(m_model->index(m, i), Qt::DisplayRole).toDouble();
430 appendBarSet(barSet);
431 }
432 } else {
433 QStringList categories;
434 for (int k = m_mapFirst; k < m_mapFirst + m_mapCount; k++)
435 categories << m_model->data(m_model->index(m_mapCategories, k), Qt::DisplayRole).toString();
436 m_internalModel = new BarChartModel(categories, this);
437
438 for (int i = m_mapBarBottom; i <= m_mapBarTop; i++) {
439 QBarSet* barSet = new QBarSet(QString("Row: %1").arg(i + 1));
440 for(int m = m_mapFirst; m < m_mapFirst + m_mapCount; m++)
441 *barSet << m_model->data(m_model->index(i, m), Qt::DisplayRole).toDouble();
442 appendBarSet(barSet);
443 }
444 }
445 */
446 }
447
448 /*!
449 \internal
450 */
451 void QBarSeries::modelUpdated(QModelIndex topLeft, QModelIndex bottomRight)
452 {
453 Q_D(QBarSeries);
454 d->modelUpdated(topLeft,bottomRight);
455 /*
456 Q_UNUSED(bottomRight)
457
458 if (m_mapOrientation == Qt::Vertical)
459 {
460 // model update is relevant to BarSeries if the change was made to the part of the model that was mapped to BarSeries
461 if (topLeft.column() >= m_mapBarBottom && topLeft.column() <= m_mapBarTop && topLeft.row() >= m_mapFirst && topLeft.row() < m_mapFirst + m_mapCount)
462 barsetAt(topLeft.column() - m_mapBarBottom)->setValue(topLeft.row() - m_mapFirst, m_model->data(topLeft, Qt::DisplayRole).toDouble());
463 }
464 else
465 {
466 // model update is relevant to BarSeries if the change was made to the part of the model that was mapped to BarSeries
467 if (topLeft.row() >= m_mapBarBottom && topLeft.row() <= m_mapBarTop && topLeft.column() >= m_mapFirst && topLeft.column() < m_mapFirst + m_mapCount)
468 barsetAt(topLeft.row() - m_mapBarBottom)->setValue(topLeft.column() - m_mapFirst, m_model->data(topLeft, Qt::DisplayRole).toDouble());
469 }
470 */
471 }
472
473 /*!
474 \internal
475 */
476 void QBarSeries::modelDataAdded(QModelIndex parent, int start, int end)
477 {
478 Q_D(QBarSeries);
479 d->modelDataAdded(parent,start,end);
480 /*
481 if (m_mapOrientation == Qt::Vertical) {
482 insertCategory(start - m_mapFirst, QString("Row: %1").arg(start + 1));
483 for (int i = 0; i <= m_mapBarTop - m_mapBarBottom; i++) {
484 barsetAt(i)->insertValue(start - m_mapFirst, m_model->data(m_model->index(start, i), Qt::DisplayRole).toDouble());
485 }
486 } else {
487 insertCategory(start - m_mapFirst, QString("Column: %1").arg(start + 1));
488 for (int i = 0; i <= m_mapBarTop - m_mapBarBottom; i++) {
489 barsetAt(i)->insertValue(start - m_mapFirst, m_model->data(m_model->index(i, start), Qt::DisplayRole).toDouble());
490 }
491 }
492 emit restructuredBars();
493 */
494 }
495
496 /*!
497 \internal
498 */
499 void QBarSeries::modelDataRemoved(QModelIndex parent, int start, int end)
500 {
501 Q_D(QBarSeries);
502 d->modelDataRemoved(parent,start,end);
503 /*
504 Q_UNUSED(parent)
505 Q_UNUSED(end)
506
507 removeCategory(start - m_mapFirst);
508 for (int i = 0; i <= m_mapBarTop - m_mapBarBottom; i++)
509 {
510 barsetAt(i)->removeValue(start - m_mapFirst);
511 }
512 emit restructuredBars();
513 */
514 }
515
516 void QBarSeries::barsetChanged()
517 {
518 Q_D(QBarSeries);
519 d->barsetChanged();
520 // emit updatedBars();
521 }
233 }
522
234
523 QBarCategories QBarSeries::categories() const
235 QBarCategories QBarSeries::categories() const
524 {
236 {
525 Q_D(const QBarSeries);
237 Q_D(const QBarSeries);
526
238
527 QBarCategories categories;
239 QBarCategories categories;
528 int count = d->m_internalModel->categoryCount();
240 int count = d->m_internalModel->categoryCount();
529 for (int i=1; i <= count; i++) {
241 for (int i=1; i <= count; i++) {
530 categories.insert(i, d->m_internalModel->categoryName(i - 1));
242 categories.insert(i, d->m_internalModel->categoryName(i - 1));
531 }
243 }
532 return categories;
244 return categories;
533
245
534 }
246 }
535
247
536 /*!
248 /*!
537 Sets the visibility of labels in series to \a visible
249 Sets the visibility of labels in series to \a visible
538 */
250 */
539 void QBarSeries::setLabelsVisible(bool visible)
251 void QBarSeries::setLabelsVisible(bool visible)
540 {
252 {
541 foreach (QBarSet* s, barSets()) {
253 foreach (QBarSet* s, barSets()) {
542 s->setLabelsVisible(visible);
254 s->setLabelsVisible(visible);
543 }
255 }
544 }
256 }
545
257
546 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
258 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
547
259
548 QBarSeriesPrivate::QBarSeriesPrivate(QBarCategories categories, QBarSeries *q) : QSeriesPrivate(q),
260 QBarSeriesPrivate::QBarSeriesPrivate(QBarCategories categories, QBarSeries *q) : QSeriesPrivate(q),
549 m_internalModel(new BarChartModel(categories,this)),
261 m_internalModel(new BarChartModel(categories,this)),
550 m_model(0),
262 m_model(0),
551 m_mapCategories(-1),
263 m_mapCategories(-1),
552 m_mapBarBottom(-1),
264 m_mapBarBottom(-1),
553 m_mapBarTop(-1),
265 m_mapBarTop(-1),
554 m_mapFirst(0),
266 m_mapFirst(0),
555 m_mapCount(0),
267 m_mapCount(0),
556 m_mapOrientation(Qt::Vertical)
268 m_mapOrientation(Qt::Vertical)
557 {
269 {
558 }
270 }
559
271
560 QBarSet* QBarSeriesPrivate::barsetAt(int index)
272 QBarSet* QBarSeriesPrivate::barsetAt(int index)
561 {
273 {
562 return m_internalModel->barsetAt(index);
274 return m_internalModel->barsetAt(index);
563 }
275 }
564
276
565 QString QBarSeriesPrivate::categoryName(int category)
277 QString QBarSeriesPrivate::categoryName(int category)
566 {
278 {
567 return m_internalModel->categoryName(category);
279 return m_internalModel->categoryName(category);
568 }
280 }
569
281
570 void QBarSeriesPrivate::setToolTipEnabled(bool enabled)
282 void QBarSeriesPrivate::setToolTipEnabled(bool enabled)
571 {
283 {
572 // TODO: what if we add sets after call to this function? Those sets won't have tooltip enabled.
284 // TODO: what if we add sets after call to this function? Those sets won't have tooltip enabled.
573 if (enabled) {
285 if (enabled) {
574 for (int i=0; i<m_internalModel->barsetCount(); i++) {
286 for (int i=0; i<m_internalModel->barsetCount(); i++) {
575 QBarSet *set = m_internalModel->barsetAt(i);
287 QBarSet *set = m_internalModel->barsetAt(i);
576 connect(set, SIGNAL(showToolTip(QPoint,QString)), this, SIGNAL(showToolTip(QPoint,QString)));
288 connect(set, SIGNAL(showToolTip(QPoint,QString)), this, SIGNAL(showToolTip(QPoint,QString)));
577 }
289 }
578 } else {
290 } else {
579 for (int i=0; i<m_internalModel->barsetCount(); i++) {
291 for (int i=0; i<m_internalModel->barsetCount(); i++) {
580 QBarSet *set = m_internalModel->barsetAt(i);
292 QBarSet *set = m_internalModel->barsetAt(i);
581 disconnect(set, SIGNAL(showToolTip(QPoint,QString)), this, SIGNAL(showToolTip(QPoint,QString)));
293 disconnect(set, SIGNAL(showToolTip(QPoint,QString)), this, SIGNAL(showToolTip(QPoint,QString)));
582 }
294 }
583 }
295 }
584 }
296 }
585
297
586 void QBarSeriesPrivate::barsetClicked(QString category, Qt::MouseButtons button)
298 void QBarSeriesPrivate::barsetClicked(QString category, Qt::MouseButtons button)
587 {
299 {
588 emit clicked(qobject_cast<QBarSet*>(sender()), category, button);
300 emit clicked(qobject_cast<QBarSet*>(sender()), category, button);
589 }
301 }
590
302
591 qreal QBarSeriesPrivate::min()
303 qreal QBarSeriesPrivate::min()
592 {
304 {
593 return m_internalModel->min();
305 return m_internalModel->min();
594 }
306 }
595
307
596 qreal QBarSeriesPrivate::max()
308 qreal QBarSeriesPrivate::max()
597 {
309 {
598 return m_internalModel->max();
310 return m_internalModel->max();
599 }
311 }
600
312
601 qreal QBarSeriesPrivate::valueAt(int set, int category)
313 qreal QBarSeriesPrivate::valueAt(int set, int category)
602 {
314 {
603 return m_internalModel->valueAt(set, category);
315 return m_internalModel->valueAt(set, category);
604 }
316 }
605
317
606 qreal QBarSeriesPrivate::percentageAt(int set, int category)
318 qreal QBarSeriesPrivate::percentageAt(int set, int category)
607 {
319 {
608 return m_internalModel->percentageAt(set, category);
320 return m_internalModel->percentageAt(set, category);
609 }
321 }
610
322
611 qreal QBarSeriesPrivate::categorySum(int category)
323 qreal QBarSeriesPrivate::categorySum(int category)
612 {
324 {
613 return m_internalModel->categorySum(category);
325 return m_internalModel->categorySum(category);
614 }
326 }
615
327
616 qreal QBarSeriesPrivate::absoluteCategorySum(int category)
328 qreal QBarSeriesPrivate::absoluteCategorySum(int category)
617 {
329 {
618 return m_internalModel->absoluteCategorySum(category);
330 return m_internalModel->absoluteCategorySum(category);
619 }
331 }
620
332
621 qreal QBarSeriesPrivate::maxCategorySum()
333 qreal QBarSeriesPrivate::maxCategorySum()
622 {
334 {
623 return m_internalModel->maxCategorySum();
335 return m_internalModel->maxCategorySum();
624 }
336 }
625
337
626 BarChartModel& QBarSeriesPrivate::modelInternal()
338 BarChartModel& QBarSeriesPrivate::modelInternal()
627 {
339 {
628 return *m_internalModel;
340 return *m_internalModel;
629 }
341 }
630
342
631 bool QBarSeriesPrivate::setModel(QAbstractItemModel *model)
343 bool QBarSeriesPrivate::setModel(QAbstractItemModel *model)
632 {
344 {
633 // disconnect signals from old model
345 // disconnect signals from old model
634 if(m_model)
346 if(m_model)
635 {
347 {
636 disconnect(m_model, 0, this, 0);
348 disconnect(m_model, 0, this, 0);
637 m_mapCategories = -1;
349 m_mapCategories = -1;
638 m_mapBarBottom = -1;
350 m_mapBarBottom = -1;
639 m_mapBarTop = -1;
351 m_mapBarTop = -1;
640 m_mapFirst = 0;
352 m_mapFirst = 0;
641 m_mapCount = 0;
353 m_mapCount = 0;
642 m_mapOrientation = Qt::Vertical;
354 m_mapOrientation = Qt::Vertical;
643 }
355 }
644
356
645 // set new model
357 // set new model
646 if(model)
358 if(model)
647 {
359 {
648 m_model = model;
360 m_model = model;
649 return true;
361 return true;
650 }
362 }
651 else
363 else
652 {
364 {
653 m_model = 0;
365 m_model = 0;
654 return false;
366 return false;
655 }
367 }
656 }
368 }
657
369
658 void QBarSeriesPrivate::setModelMapping(int categories, int bottomBoundry, int topBoundry, Qt::Orientation orientation)
370 void QBarSeriesPrivate::setModelMapping(int categories, int bottomBoundry, int topBoundry, Qt::Orientation orientation)
659 {
371 {
660 Q_Q(QBarSeries);
372 Q_Q(QBarSeries);
661
373
662 if (m_model == 0)
374 if (m_model == 0)
663 return;
375 return;
664
376
665 m_mapCategories = categories;
377 m_mapCategories = categories;
666 m_mapBarBottom = bottomBoundry;
378 m_mapBarBottom = bottomBoundry;
667 m_mapBarTop = topBoundry;
379 m_mapBarTop = topBoundry;
668 // m_mapFirst = 1;
380 // m_mapFirst = 1;
669 m_mapOrientation = orientation;
381 m_mapOrientation = orientation;
670
382
671 // connect the signals
383 // connect the signals
672 if (m_mapOrientation == Qt::Vertical) {
384 if (m_mapOrientation == Qt::Vertical) {
673 m_mapCount = m_model->rowCount() - m_mapFirst;
385 m_mapCount = m_model->rowCount() - m_mapFirst;
674 connect(m_model,SIGNAL(dataChanged(QModelIndex,QModelIndex)),
386 connect(m_model,SIGNAL(dataChanged(QModelIndex,QModelIndex)),
675 this, SLOT(modelUpdated(QModelIndex, QModelIndex)));
387 this, SLOT(modelUpdated(QModelIndex, QModelIndex)));
676 connect(m_model,SIGNAL(rowsInserted(QModelIndex, int, int)),
388 connect(m_model,SIGNAL(rowsInserted(QModelIndex, int, int)),
677 this, SLOT(modelDataAdded(QModelIndex,int,int)));
389 this, SLOT(modelDataAdded(QModelIndex,int,int)));
678 connect(m_model, SIGNAL(rowsRemoved(QModelIndex, int, int)),
390 connect(m_model, SIGNAL(rowsRemoved(QModelIndex, int, int)),
679 this, SLOT(modelDataRemoved(QModelIndex,int,int)));
391 this, SLOT(modelDataRemoved(QModelIndex,int,int)));
680 } else {
392 } else {
681 m_mapCount = m_model->columnCount() - m_mapFirst;
393 m_mapCount = m_model->columnCount() - m_mapFirst;
682 connect(m_model,SIGNAL(dataChanged(QModelIndex,QModelIndex)),
394 connect(m_model,SIGNAL(dataChanged(QModelIndex,QModelIndex)),
683 this, SLOT(modelUpdated(QModelIndex, QModelIndex)));
395 this, SLOT(modelUpdated(QModelIndex, QModelIndex)));
684 connect(m_model,SIGNAL(columnsInserted(QModelIndex, int, int)),
396 connect(m_model,SIGNAL(columnsInserted(QModelIndex, int, int)),
685 this, SLOT(modelDataAdded(QModelIndex,int,int)));
397 this, SLOT(modelDataAdded(QModelIndex,int,int)));
686 connect(m_model, SIGNAL(columnsRemoved(QModelIndex, int, int)),
398 connect(m_model, SIGNAL(columnsRemoved(QModelIndex, int, int)),
687 this, SLOT(modelDataRemoved(QModelIndex,int,int)));
399 this, SLOT(modelDataRemoved(QModelIndex,int,int)));
688 }
400 }
689
401
690 // create the initial bars
402 // create the initial bars
691 delete m_internalModel;
403 delete m_internalModel;
692 if (m_mapOrientation == Qt::Vertical) {
404 if (m_mapOrientation == Qt::Vertical) {
693 QStringList categories;
405 QStringList categories;
694 for (int k = m_mapFirst; k < m_mapFirst + m_mapCount; k++)
406 for (int k = m_mapFirst; k < m_mapFirst + m_mapCount; k++)
695 categories << m_model->data(m_model->index(k, m_mapCategories), Qt::DisplayRole).toString();
407 categories << m_model->data(m_model->index(k, m_mapCategories), Qt::DisplayRole).toString();
696 m_internalModel = new BarChartModel(categories, this);
408 m_internalModel = new BarChartModel(categories, this);
697
409
698 for (int i = m_mapBarBottom; i <= m_mapBarTop; i++) {
410 for (int i = m_mapBarBottom; i <= m_mapBarTop; i++) {
699 QBarSet* barSet = new QBarSet(QString("Column: %1").arg(i + 1));
411 QBarSet* barSet = new QBarSet(QString("Column: %1").arg(i + 1));
700 for(int m = m_mapFirst; m < m_mapFirst + m_mapCount; m++)
412 for(int m = m_mapFirst; m < m_mapFirst + m_mapCount; m++)
701 *barSet << m_model->data(m_model->index(m, i), Qt::DisplayRole).toDouble();
413 *barSet << m_model->data(m_model->index(m, i), Qt::DisplayRole).toDouble();
702 q->appendBarSet(barSet);
414 q->appendBarSet(barSet);
703 }
415 }
704 } else {
416 } else {
705 QStringList categories;
417 QStringList categories;
706 for (int k = m_mapFirst; k < m_mapFirst + m_mapCount; k++)
418 for (int k = m_mapFirst; k < m_mapFirst + m_mapCount; k++)
707 categories << m_model->data(m_model->index(m_mapCategories, k), Qt::DisplayRole).toString();
419 categories << m_model->data(m_model->index(m_mapCategories, k), Qt::DisplayRole).toString();
708 m_internalModel = new BarChartModel(categories, this);
420 m_internalModel = new BarChartModel(categories, this);
709
421
710 for (int i = m_mapBarBottom; i <= m_mapBarTop; i++) {
422 for (int i = m_mapBarBottom; i <= m_mapBarTop; i++) {
711 QBarSet* barSet = new QBarSet(QString("Row: %1").arg(i + 1));
423 QBarSet* barSet = new QBarSet(QString("Row: %1").arg(i + 1));
712 for(int m = m_mapFirst; m < m_mapFirst + m_mapCount; m++)
424 for(int m = m_mapFirst; m < m_mapFirst + m_mapCount; m++)
713 *barSet << m_model->data(m_model->index(i, m), Qt::DisplayRole).toDouble();
425 *barSet << m_model->data(m_model->index(i, m), Qt::DisplayRole).toDouble();
714 q->appendBarSet(barSet);
426 q->appendBarSet(barSet);
715 }
427 }
716 }
428 }
717 }
429 }
718
430
719 void QBarSeriesPrivate::modelUpdated(QModelIndex topLeft, QModelIndex bottomRight)
431 void QBarSeriesPrivate::modelUpdated(QModelIndex topLeft, QModelIndex bottomRight)
720 {
432 {
721 Q_UNUSED(bottomRight)
433 Q_UNUSED(bottomRight)
722
434
723 if (m_mapOrientation == Qt::Vertical)
435 if (m_mapOrientation == Qt::Vertical)
724 {
436 {
725 // model update is relevant to BarSeries if the change was made to the part of the model that was mapped to BarSeries
437 // model update is relevant to BarSeries if the change was made to the part of the model that was mapped to BarSeries
726 if (topLeft.column() >= m_mapBarBottom && topLeft.column() <= m_mapBarTop && topLeft.row() >= m_mapFirst && topLeft.row() < m_mapFirst + m_mapCount)
438 if (topLeft.column() >= m_mapBarBottom && topLeft.column() <= m_mapBarTop && topLeft.row() >= m_mapFirst && topLeft.row() < m_mapFirst + m_mapCount)
727 barsetAt(topLeft.column() - m_mapBarBottom)->setValue(topLeft.row() - m_mapFirst, m_model->data(topLeft, Qt::DisplayRole).toDouble());
439 barsetAt(topLeft.column() - m_mapBarBottom)->setValue(topLeft.row() - m_mapFirst, m_model->data(topLeft, Qt::DisplayRole).toDouble());
728 }
440 }
729 else
441 else
730 {
442 {
731 // model update is relevant to BarSeries if the change was made to the part of the model that was mapped to BarSeries
443 // model update is relevant to BarSeries if the change was made to the part of the model that was mapped to BarSeries
732 if (topLeft.row() >= m_mapBarBottom && topLeft.row() <= m_mapBarTop && topLeft.column() >= m_mapFirst && topLeft.column() < m_mapFirst + m_mapCount)
444 if (topLeft.row() >= m_mapBarBottom && topLeft.row() <= m_mapBarTop && topLeft.column() >= m_mapFirst && topLeft.column() < m_mapFirst + m_mapCount)
733 barsetAt(topLeft.row() - m_mapBarBottom)->setValue(topLeft.column() - m_mapFirst, m_model->data(topLeft, Qt::DisplayRole).toDouble());
445 barsetAt(topLeft.row() - m_mapBarBottom)->setValue(topLeft.column() - m_mapFirst, m_model->data(topLeft, Qt::DisplayRole).toDouble());
734 }
446 }
735 }
447 }
736
448
737 void QBarSeriesPrivate::modelDataAdded(QModelIndex /*parent*/, int start, int /*end*/)
449 void QBarSeriesPrivate::modelDataAdded(QModelIndex /*parent*/, int start, int /*end*/)
738 {
450 {
739 Q_Q(QBarSeries);
451 Q_Q(QBarSeries);
740
452
741 if (m_mapOrientation == Qt::Vertical) {
453 if (m_mapOrientation == Qt::Vertical) {
742 q->insertCategory(start - m_mapFirst, QString("Row: %1").arg(start + 1));
454 q->insertCategory(start - m_mapFirst, QString("Row: %1").arg(start + 1));
743 for (int i = 0; i <= m_mapBarTop - m_mapBarBottom; i++) {
455 for (int i = 0; i <= m_mapBarTop - m_mapBarBottom; i++) {
744 barsetAt(i)->insertValue(start - m_mapFirst, m_model->data(m_model->index(start, i), Qt::DisplayRole).toDouble());
456 barsetAt(i)->insertValue(start - m_mapFirst, m_model->data(m_model->index(start, i), Qt::DisplayRole).toDouble());
745 }
457 }
746 } else {
458 } else {
747 q->insertCategory(start - m_mapFirst, QString("Column: %1").arg(start + 1));
459 q->insertCategory(start - m_mapFirst, QString("Column: %1").arg(start + 1));
748 for (int i = 0; i <= m_mapBarTop - m_mapBarBottom; i++) {
460 for (int i = 0; i <= m_mapBarTop - m_mapBarBottom; i++) {
749 barsetAt(i)->insertValue(start - m_mapFirst, m_model->data(m_model->index(i, start), Qt::DisplayRole).toDouble());
461 barsetAt(i)->insertValue(start - m_mapFirst, m_model->data(m_model->index(i, start), Qt::DisplayRole).toDouble());
750 }
462 }
751 }
463 }
752 emit restructuredBars();
464 emit restructuredBars();
753 }
465 }
754
466
755 void QBarSeriesPrivate::modelDataRemoved(QModelIndex parent, int start, int end)
467 void QBarSeriesPrivate::modelDataRemoved(QModelIndex parent, int start, int end)
756 {
468 {
757 Q_Q(QBarSeries);
469 Q_Q(QBarSeries);
758 Q_UNUSED(parent)
470 Q_UNUSED(parent)
759 Q_UNUSED(end)
471 Q_UNUSED(end)
760
472
761 q->removeCategory(start - m_mapFirst);
473 q->removeCategory(start - m_mapFirst);
762 for (int i = 0; i <= m_mapBarTop - m_mapBarBottom; i++)
474 for (int i = 0; i <= m_mapBarTop - m_mapBarBottom; i++)
763 {
475 {
764 barsetAt(i)->removeValue(start - m_mapFirst);
476 barsetAt(i)->removeValue(start - m_mapFirst);
765 }
477 }
766 emit restructuredBars();
478 emit restructuredBars();
767 }
479 }
768
480
769 void QBarSeriesPrivate::barsetChanged()
481 void QBarSeriesPrivate::barsetChanged()
770 {
482 {
771 emit updatedBars();
483 emit updatedBars();
772 }
484 }
773
485
774 void QBarSeriesPrivate::scaleDomain(Domain& domain)
486 void QBarSeriesPrivate::scaleDomain(Domain& domain)
775 {
487 {
776 Q_Q(QBarSeries);
777 qreal minX(domain.minX());
488 qreal minX(domain.minX());
778 qreal minY(domain.minY());
489 qreal minY(domain.minY());
779 qreal maxX(domain.maxX());
490 qreal maxX(domain.maxX());
780 qreal maxY(domain.maxY());
491 qreal maxY(domain.maxY());
781 int tickXCount(domain.tickXCount());
492 int tickXCount(domain.tickXCount());
782 int tickYCount(domain.tickYCount());
493 int tickYCount(domain.tickYCount());
783
494
784 qreal x = q->categoryCount();
495 qreal x = m_internalModel->categoryCount();
785 qreal y = q->max();
496 qreal y = max();
786 minX = qMin(minX, x);
497 minX = qMin(minX, x);
787 minY = qMin(minY, y);
498 minY = qMin(minY, y);
788 maxX = qMax(maxX, x);
499 maxX = qMax(maxX, x);
789 maxY = qMax(maxY, y);
500 maxY = qMax(maxY, y);
790 tickXCount = x+1;
501 tickXCount = x+1;
791
502
792 domain.setRangeX(minX,maxX,tickXCount);
503 domain.setRangeX(minX,maxX,tickXCount);
793 domain.setRangeY(minY,maxY,tickYCount);
504 domain.setRangeY(minY,maxY,tickYCount);
794 }
505 }
795
506
796 Chart* QBarSeriesPrivate::createGraphics(ChartPresenter* presenter)
507 Chart* QBarSeriesPrivate::createGraphics(ChartPresenter* presenter)
797 {
508 {
798 Q_Q(QBarSeries);
509 Q_Q(QBarSeries);
799
510
800 BarChartItem* bar = new BarChartItem(q,presenter);
511 BarChartItem* bar = new BarChartItem(q,presenter);
801 if(presenter->animationOptions().testFlag(QChart::SeriesAnimations)) {
512 if(presenter->animationOptions().testFlag(QChart::SeriesAnimations)) {
802 presenter->animator()->addAnimation(bar);
513 presenter->animator()->addAnimation(bar);
803 }
514 }
804 presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q));
515 presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q));
805 return bar;
516 return bar;
806
517
807 }
518 }
808
519
809 QList<LegendMarker*> QBarSeriesPrivate::createLegendMarker(QLegend* legend)
520 QList<LegendMarker*> QBarSeriesPrivate::createLegendMarker(QLegend* legend)
810 {
521 {
811 Q_Q(QBarSeries);
522 Q_Q(QBarSeries);
812 QList<LegendMarker*> markers;
523 QList<LegendMarker*> markers;
813 foreach(QBarSet* set, q->barSets()) {
524 foreach(QBarSet* set, q->barSets()) {
814 BarLegendMarker* marker = new BarLegendMarker(q,set,legend);
525 BarLegendMarker* marker = new BarLegendMarker(q,set,legend);
815 markers << marker;
526 markers << marker;
816 }
527 }
817
528
818 return markers;
529 return markers;
819 }
530 }
820
531
821 #include "moc_qbarseries.cpp"
532 #include "moc_qbarseries.cpp"
822 #include "moc_qbarseries_p.cpp"
533 #include "moc_qbarseries_p.cpp"
823
534
824 QTCOMMERCIALCHART_END_NAMESPACE
535 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,127 +1,83
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 BARSERIES_H
21 #ifndef BARSERIES_H
22 #define BARSERIES_H
22 #define BARSERIES_H
23
23
24 #include <qseries.h>
24 #include <qseries.h>
25 #include <QStringList>
25 #include <QStringList>
26
26
27 class QModelIndex;
27 class QModelIndex;
28
28
29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30
30
31 typedef QStringList QBarCategories;
31 typedef QStringList QBarCategories;
32
32
33 class QBarSet;
33 class QBarSet;
34 class BarChartModel;
34 class BarChartModel;
35 class BarCategory;
35 class BarCategory;
36 class QBarSeriesPrivate;
36 class QBarSeriesPrivate;
37
37
38 // Container for series
38 // Container for series
39 class QTCOMMERCIALCHART_EXPORT QBarSeries : public QSeries
39 class QTCOMMERCIALCHART_EXPORT QBarSeries : public QSeries
40 {
40 {
41 Q_OBJECT
41 Q_OBJECT
42 public:
42 public:
43 QBarSeries(QBarCategories categories, QObject *parent = 0);
43 QBarSeries(QBarCategories categories, QObject *parent = 0);
44
44
45 QSeries::QSeriesType type() const;
45 QSeries::QSeriesType type() const;
46
46
47 void appendBarSet(QBarSet *set); // Takes ownership of set
47 void appendBarSet(QBarSet *set); // Takes ownership of set
48 void removeBarSet(QBarSet *set); // Releases ownership, doesn't delete set
48 void removeBarSet(QBarSet *set); // Releases ownership, doesn't delete set
49 void appendBarSets(QList<QBarSet* > sets);
49 void appendBarSets(QList<QBarSet* > sets);
50 void removeBarSets(QList<QBarSet* > sets);
50 void removeBarSets(QList<QBarSet* > sets);
51 void insertBarSet(int i, QBarSet *set);
51 void insertBarSet(int i, QBarSet *set);
52 void insertCategory(int i, QString category);
52 void insertCategory(int i, QString category);
53 void removeCategory(int i);
53 void removeCategory(int i);
54 int barsetCount() const;
54 int barsetCount() const;
55 int categoryCount() const;
55 int categoryCount() const;
56 QList<QBarSet*> barSets() const;
56 QList<QBarSet*> barSets() const;
57 QBarCategories categories() const;
57 QBarCategories categories() const;
58
58
59 void setLabelsVisible(bool visible = true);
59 void setLabelsVisible(bool visible = true);
60
60
61 bool setModel(QAbstractItemModel *model);
61 bool setModel(QAbstractItemModel *model);
62 void setModelMapping(int categories, int bottomBoundary, int topBoundary, Qt::Orientation orientation = Qt::Vertical);
62 void setModelMapping(int categories, int bottomBoundary, int topBoundary, Qt::Orientation orientation = Qt::Vertical);
63
63
64 public:
65 // TODO: Functions below this are not part of api and will be moved
66 // to private implementation, when we start using it
67 // TODO: TO PIMPL --->
68 QBarSet* barsetAt(int index);
69 QString categoryName(int category);
70 qreal min();
71 qreal max();
72 qreal valueAt(int set, int category);
73 qreal percentageAt(int set, int category);
74 qreal categorySum(int category);
75 qreal absoluteCategorySum(int category);
76 qreal maxCategorySum();
77 BarChartModel& modelInternal();
78 // <--- TO PIMPL
79
80 protected:
64 protected:
81 explicit QBarSeries(QBarSeriesPrivate &d,QObject *parent = 0);
65 explicit QBarSeries(QBarSeriesPrivate &d,QObject *parent = 0);
82
66
83 Q_SIGNALS:
67 Q_SIGNALS:
84 void clicked(QBarSet *barset, QString category, Qt::MouseButtons button); // Up to user of api, what to do with these signals
68 void clicked(QBarSet *barset, QString category, Qt::MouseButtons button);
85 void selected();
69 void selected();
86 //
87 void updatedBars();
88 void restructuredBars();
89
90 // TODO: internal signals, these to private implementation.
91 // TODO: TO PIMPL --->
92 // void showToolTip(QPoint pos, QString tip);
93 // <--- TO PIMPL
94
70
95 public Q_SLOTS:
71 public Q_SLOTS:
96 void setToolTipEnabled(bool enabled = true); // enables tooltips
72 void setToolTipEnabled(bool enabled = true); // enables tooltips
97
73
98 // TODO: TO PIMPL --->
99 void barsetClicked(QString category, Qt::MouseButtons button);
100 // <--- TO PIMPL
101
102 private Q_SLOTS:
103 // slots for updating bars when data in model changes
104 void modelUpdated(QModelIndex topLeft, QModelIndex bottomRight);
105 void modelDataAdded(QModelIndex parent, int start, int end);
106 void modelDataRemoved(QModelIndex parent, int start, int end);
107 void barsetChanged();
108
109 protected:
74 protected:
110 Q_DECLARE_PRIVATE(QBarSeries)
75 Q_DECLARE_PRIVATE(QBarSeries)
111
76 friend class BarChartItem;
112 /*
77 friend class PercentBarChartItem;
113 BarChartModel *m_internalModel; // TODO: this may change... current "2 models" situation doesn't look good.
78 friend class StackedBarChartItem;
114
115 // QAbstractItemModel* m_model;
116 int m_mapCategories;
117 int m_mapBarBottom;
118 int m_mapBarTop;
119 int m_mapFirst;
120 int m_mapCount;
121 Qt::Orientation m_mapOrientation;
122 */
123 };
79 };
124
80
125 QTCOMMERCIALCHART_END_NAMESPACE
81 QTCOMMERCIALCHART_END_NAMESPACE
126
82
127 #endif // BARSERIES_H
83 #endif // BARSERIES_H
@@ -1,80 +1,81
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 QBARSET_H
21 #ifndef QBARSET_H
22 #define QBARSET_H
22 #define QBARSET_H
23
23
24 #include <qchartglobal.h>
24 #include <qchartglobal.h>
25 #include <QPen>
25 #include <QPen>
26 #include <QBrush>
26 #include <QBrush>
27 #include <QFont>
27 #include <QFont>
28
28
29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30 class QBarSetPrivate;
30 class QBarSetPrivate;
31
31
32 class QTCOMMERCIALCHART_EXPORT QBarSet : public QObject
32 class QTCOMMERCIALCHART_EXPORT QBarSet : public QObject
33 {
33 {
34 Q_OBJECT
34 Q_OBJECT
35
35
36 public:
36 public:
37 QBarSet(QString name, QObject *parent = 0);
37 QBarSet(QString name, QObject *parent = 0);
38 virtual ~QBarSet();
38 virtual ~QBarSet();
39
39
40 void setName(QString name);
40 void setName(QString name);
41 QString name() const;
41 QString name() const;
42 QBarSet& operator << (const qreal &value); // appends new value to set
42 QBarSet& operator << (const qreal &value); // appends new value to set
43 void insertValue(int i, qreal value);
43 void insertValue(int i, qreal value);
44 void removeValue(int i);
44 void removeValue(int i);
45 int count() const; // count of values in set
45 int count() const; // count of values in set
46 qreal valueAt(int index) const; // for modifying individual values
46 qreal valueAt(int index) const; // for modifying individual values
47 void setValue(int index, qreal value); // setter for individual value
47 void setValue(int index, qreal value); // setter for individual value
48 qreal sum() const; // sum of all values in the set
48 qreal sum() const; // sum of all values in the set
49
49
50 void setPen(const QPen &pen);
50 void setPen(const QPen &pen);
51 QPen pen() const;
51 QPen pen() const;
52
52
53 void setBrush(const QBrush &brush);
53 void setBrush(const QBrush &brush);
54 QBrush brush() const;
54 QBrush brush() const;
55
55
56 void setLabelPen(const QPen &pen);
56 void setLabelPen(const QPen &pen);
57 QPen labelPen() const;
57 QPen labelPen() const;
58
58
59 void setLabelBrush(const QBrush &brush);
59 void setLabelBrush(const QBrush &brush);
60 QBrush labelBrush() const;
60 QBrush labelBrush() const;
61
61
62 void setLabelFont(const QFont &font);
62 void setLabelFont(const QFont &font);
63 QFont labelFont() const;
63 QFont labelFont() const;
64
64
65 void setLabelsVisible(bool visible = true);
65 void setLabelsVisible(bool visible = true);
66 bool labelsVisible() const;
66 bool labelsVisible() const;
67
67
68 Q_SIGNALS:
68 Q_SIGNALS:
69 void clicked(QString category, Qt::MouseButtons button); // Clicked and hover signals exposed to user
69 void clicked(QString category, Qt::MouseButtons button); // Clicked and hover signals exposed to user
70
70
71 private:
71 private:
72 QScopedPointer<QBarSetPrivate> d_ptr;
72 QScopedPointer<QBarSetPrivate> d_ptr;
73 Q_DISABLE_COPY(QBarSet)
73 Q_DISABLE_COPY(QBarSet)
74 friend class QBarSeries;
74 friend class QBarSeries;
75 friend class BarLegendMarker;
75 friend class BarLegendMarker;
76 friend class BarChartItem;
76 };
77 };
77
78
78 QTCOMMERCIALCHART_END_NAMESPACE
79 QTCOMMERCIALCHART_END_NAMESPACE
79
80
80 #endif // QBARSET_H
81 #endif // QBARSET_H
@@ -1,108 +1,108
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 "qstackedbarseries.h"
21 #include "qstackedbarseries.h"
22 #include "qstackedbarseries_p.h"
22 #include "qstackedbarseries_p.h"
23 #include "stackedbarchartitem_p.h"
23 #include "stackedbarchartitem_p.h"
24 #include "barchartmodel_p.h"
24 #include "chartdataset_p.h"
25 #include "chartdataset_p.h"
25 #include "charttheme_p.h"
26 #include "charttheme_p.h"
26 #include "chartanimator_p.h"
27 #include "chartanimator_p.h"
27
28
28 QTCOMMERCIALCHART_BEGIN_NAMESPACE
29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
29
30
30 /*!
31 /*!
31 \class QStackedBarSeries
32 \class QStackedBarSeries
32 \brief part of QtCommercial chart API.
33 \brief part of QtCommercial chart API.
33
34
34 QStackedBarSeries represents a series of data shown as bars. All bars in same category are
35 QStackedBarSeries represents a series of data shown as bars. All bars in same category are
35 stacked on top of each other. One QStackedBarSeries can contain multible QBarSet data sets.
36 stacked on top of each other. One QStackedBarSeries can contain multible QBarSet data sets.
36 QStackedBarSeries groups the data from sets to categories, which are defined by QStringList.
37 QStackedBarSeries groups the data from sets to categories, which are defined by QStringList.
37
38
38 \mainclass
39 \mainclass
39
40
40 \sa QBarSet, QPercentBarSeries, QBarSeries
41 \sa QBarSet, QPercentBarSeries, QBarSeries
41 */
42 */
42
43
43 /*!
44 /*!
44 \fn virtual QSeriesType QStackedBarSeries::type() const
45 \fn virtual QSeriesType QStackedBarSeries::type() const
45 \brief Returns type of series.
46 \brief Returns type of series.
46 \sa QSeries, QSeriesType
47 \sa QSeries, QSeriesType
47 */
48 */
48
49
49 /*!
50 /*!
50 Constructs empty QStackedBarSeries. Parameter \a categories defines the categories for chart.
51 Constructs empty QStackedBarSeries. Parameter \a categories defines the categories for chart.
51 QStackedBarSeries is QObject which is a child of a \a parent.
52 QStackedBarSeries is QObject which is a child of a \a parent.
52 */
53 */
53 QStackedBarSeries::QStackedBarSeries(QBarCategories categories, QObject *parent)
54 QStackedBarSeries::QStackedBarSeries(QBarCategories categories, QObject *parent)
54 : QBarSeries(*new QStackedBarSeriesPrivate(categories,this), parent)
55 : QBarSeries(*new QStackedBarSeriesPrivate(categories,this), parent)
55 {
56 {
56 }
57 }
57
58
58 QSeries::QSeriesType QStackedBarSeries::type() const
59 QSeries::QSeriesType QStackedBarSeries::type() const
59 {
60 {
60 return QSeries::SeriesTypeStackedBar;
61 return QSeries::SeriesTypeStackedBar;
61 }
62 }
62
63
63 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
64 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
64
65
65 QStackedBarSeriesPrivate::QStackedBarSeriesPrivate(QBarCategories categories, QStackedBarSeries *q) : QBarSeriesPrivate(categories,q)
66 QStackedBarSeriesPrivate::QStackedBarSeriesPrivate(QBarCategories categories, QStackedBarSeries *q) : QBarSeriesPrivate(categories,q)
66 {
67 {
67
68
68 }
69 }
69
70
70 void QStackedBarSeriesPrivate::scaleDomain(Domain& domain)
71 void QStackedBarSeriesPrivate::scaleDomain(Domain& domain)
71 {
72 {
72 Q_Q(QStackedBarSeries);
73 qreal minX(domain.minX());
73 qreal minX(domain.minX());
74 qreal minY(domain.minY());
74 qreal minY(domain.minY());
75 qreal maxX(domain.maxX());
75 qreal maxX(domain.maxX());
76 qreal maxY(domain.maxY());
76 qreal maxY(domain.maxY());
77 int tickXCount(domain.tickXCount());
77 int tickXCount(domain.tickXCount());
78 int tickYCount(domain.tickYCount());
78 int tickYCount(domain.tickYCount());
79
79
80 qreal x = q->categoryCount();
80 qreal x = m_internalModel->categoryCount();
81 qreal y = q->maxCategorySum();
81 qreal y = maxCategorySum();
82 minX = qMin(minX, x);
82 minX = qMin(minX, x);
83 minY = qMin(minY, y);
83 minY = qMin(minY, y);
84 maxX = qMax(maxX, x);
84 maxX = qMax(maxX, x);
85 maxY = qMax(maxY, y);
85 maxY = qMax(maxY, y);
86 tickXCount = x+1;
86 tickXCount = x+1;
87
87
88 domain.setRangeX(minX,maxX,tickXCount);
88 domain.setRangeX(minX,maxX,tickXCount);
89 domain.setRangeY(minY,maxY,tickYCount);
89 domain.setRangeY(minY,maxY,tickYCount);
90 }
90 }
91
91
92
92
93 Chart* QStackedBarSeriesPrivate::createGraphics(ChartPresenter* presenter)
93 Chart* QStackedBarSeriesPrivate::createGraphics(ChartPresenter* presenter)
94 {
94 {
95 Q_Q(QStackedBarSeries);
95 Q_Q(QStackedBarSeries);
96
96
97 StackedBarChartItem* bar = new StackedBarChartItem(q,presenter);
97 StackedBarChartItem* bar = new StackedBarChartItem(q,presenter);
98 if(presenter->animationOptions().testFlag(QChart::SeriesAnimations)) {
98 if(presenter->animationOptions().testFlag(QChart::SeriesAnimations)) {
99 presenter->animator()->addAnimation(bar);
99 presenter->animator()->addAnimation(bar);
100 }
100 }
101 presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q));
101 presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q));
102 return bar;
102 return bar;
103 }
103 }
104
104
105 #include "moc_qstackedbarseries.cpp"
105 #include "moc_qstackedbarseries.cpp"
106
106
107 QTCOMMERCIALCHART_END_NAMESPACE
107 QTCOMMERCIALCHART_END_NAMESPACE
108
108
@@ -1,83 +1,85
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 "stackedbarchartitem_p.h"
21 #include "stackedbarchartitem_p.h"
22 #include "bar_p.h"
22 #include "bar_p.h"
23 #include "barlabel_p.h"
23 #include "barlabel_p.h"
24 #include "qbarset_p.h"
25 #include "qbarseries_p.h"
24 #include "qbarset.h"
26 #include "qbarset.h"
25
27
26 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28 QTCOMMERCIALCHART_BEGIN_NAMESPACE
27
29
28 StackedBarChartItem::StackedBarChartItem(QBarSeries *series, ChartPresenter *presenter) :
30 StackedBarChartItem::StackedBarChartItem(QBarSeries *series, ChartPresenter *presenter) :
29 BarChartItem(series, presenter)
31 BarChartItem(series, presenter)
30 {
32 {
31 }
33 }
32
34
33 QVector<QRectF> StackedBarChartItem::calculateLayout()
35 QVector<QRectF> StackedBarChartItem::calculateLayout()
34 {
36 {
35 QVector<QRectF> layout;
37 QVector<QRectF> layout;
36 // Use temporary qreals for accurancy
38 // Use temporary qreals for accurancy
37
39
38 // Domain:
40 // Domain:
39 qreal range = m_domainMaxY - m_domainMinY;
41 qreal range = m_domainMaxY - m_domainMinY;
40 qreal height = geometry().height();
42 qreal height = geometry().height();
41 qreal width = geometry().width();
43 qreal width = geometry().width();
42 qreal scale = (height / range);
44 qreal scale = (height / range);
43 qreal categotyCount = m_series->categoryCount();
45 qreal categotyCount = m_series->categoryCount();
44 qreal barWidth = width / (categotyCount * 2);
46 qreal barWidth = width / (categotyCount * 2);
45 qreal xStep = width / categotyCount;
47 qreal xStep = width / categotyCount;
46 qreal xPos = xStep / 2 - barWidth / 2;
48 qreal xPos = xStep / 2 - barWidth / 2;
47
49
48 int itemIndex(0);
50 int itemIndex(0);
49 for (int category = 0; category < categotyCount; category++) {
51 for (int category = 0; category < categotyCount; category++) {
50 qreal yPos = height + scale * m_domainMinY;
52 qreal yPos = height + scale * m_domainMinY;
51 for (int set=0; set < m_series->barsetCount(); set++) {
53 for (int set=0; set < m_series->barsetCount(); set++) {
52 QBarSet* barSet = m_series->barsetAt(set);
54 QBarSet* barSet = m_series->d_func()->barsetAt(set);
53
55
54 qreal barHeight = barSet->valueAt(category) * scale;
56 qreal barHeight = barSet->valueAt(category) * scale;
55 Bar* bar = m_bars.at(itemIndex);
57 Bar* bar = m_bars.at(itemIndex);
56 bar->setPen(barSet->pen());
58 bar->setPen(barSet->pen());
57 bar->setBrush(barSet->brush());
59 bar->setBrush(barSet->brush());
58 QRectF rect(xPos, yPos-barHeight, barWidth, barHeight);
60 QRectF rect(xPos, yPos-barHeight, barWidth, barHeight);
59 layout.append(rect);
61 layout.append(rect);
60
62
61 BarLabel* label = m_labels.at(itemIndex);
63 BarLabel* label = m_labels.at(itemIndex);
62
64
63 if (!qFuzzyIsNull(barSet->valueAt(category))) {
65 if (!qFuzzyIsNull(barSet->valueAt(category))) {
64 label->setText(QString::number(barSet->valueAt(category)));
66 label->setText(QString::number(barSet->valueAt(category)));
65 } else {
67 } else {
66 label->setText(QString(""));
68 label->setText(QString(""));
67 }
69 }
68
70
69 label->setPos(xPos + (rect.width()/2 - label->boundingRect().width()/2)
71 label->setPos(xPos + (rect.width()/2 - label->boundingRect().width()/2)
70 ,yPos - barHeight/2 - label->boundingRect().height()/2);
72 ,yPos - barHeight/2 - label->boundingRect().height()/2);
71 label->setFont(barSet->labelFont());
73 label->setFont(barSet->labelFont());
72 itemIndex++;
74 itemIndex++;
73 yPos -= barHeight;
75 yPos -= barHeight;
74 }
76 }
75 xPos += xStep;
77 xPos += xStep;
76 }
78 }
77
79
78 return layout;
80 return layout;
79 }
81 }
80
82
81 #include "moc_stackedbarchartitem_p.cpp"
83 #include "moc_stackedbarchartitem_p.cpp"
82
84
83 QTCOMMERCIALCHART_END_NAMESPACE
85 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,336 +1,337
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 "tablewidget.h"
21 #include "tablewidget.h"
22 #include <QGridLayout>
22 #include <QGridLayout>
23 #include <QTableView>
23 #include <QTableView>
24 #include <QStyledItemDelegate>
24 #include <QStyledItemDelegate>
25 #include <QLineSeries>
25 #include <QLineSeries>
26 #include <QSplineSeries>
26 #include <QSplineSeries>
27 #include <QScatterSeries>
27 #include <QScatterSeries>
28 #include "customtablemodel.h"
28 #include "customtablemodel.h"
29 #include <QPieSeries>
29 #include <QPieSeries>
30 #include <QPieSlice>
30 #include <QPieSlice>
31 #include <QAreaSeries>
31 #include <QAreaSeries>
32 #include <QBarSeries>
32 #include <QBarSeries>
33 #include <QBarSet>
33 #include <QBarSet>
34 #include <QPushButton>
34 #include <QPushButton>
35 #include <QRadioButton>
35 #include <QRadioButton>
36 #include <QLabel>
36 #include <QLabel>
37 #include <QSpinBox>
37 #include <QSpinBox>
38 #include <QTime>
38 #include <QTime>
39
39
40 TableWidget::TableWidget(QWidget *parent)
40 TableWidget::TableWidget(QWidget *parent)
41 : QWidget(parent)
41 : QWidget(parent)
42 {
42 {
43 setGeometry(100, 100, 1000, 600);
43 setGeometry(100, 100, 1000, 600);
44 qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
44 qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
45 // create simple model for storing data
45 // create simple model for storing data
46 // user's table data model
46 // user's table data model
47 m_model = new CustomTableModel;
47 m_model = new CustomTableModel;
48 m_tableView = new QTableView;
48 m_tableView = new QTableView;
49 m_tableView->setModel(m_model);
49 m_tableView->setModel(m_model);
50 m_tableView->setMinimumHeight(300);
50 m_tableView->setMinimumHeight(300);
51 // tableView->setMinimumSize(340, 480);
51 // tableView->setMinimumSize(340, 480);
52 // tableView->setItemDelegate(new QStyledItemDelegate);
52 // tableView->setItemDelegate(new QStyledItemDelegate);
53 m_chart = new QChart;
53 m_chart = new QChart;
54 m_chartView = new QChartView(m_chart);
54 m_chartView = new QChartView(m_chart);
55 m_chartView->setRenderHint(QPainter::Antialiasing);
55 m_chartView->setRenderHint(QPainter::Antialiasing);
56 m_chartView->setMinimumSize(640, 480);
56 m_chartView->setMinimumSize(640, 480);
57
57
58 // add, remove data buttons
58 // add, remove data buttons
59 QPushButton* addRowAboveButton = new QPushButton("Add row above");
59 QPushButton* addRowAboveButton = new QPushButton("Add row above");
60 connect(addRowAboveButton, SIGNAL(clicked()), this, SLOT(addRowAbove()));
60 connect(addRowAboveButton, SIGNAL(clicked()), this, SLOT(addRowAbove()));
61
61
62 QPushButton* addRowBelowButton = new QPushButton("Add row below");
62 QPushButton* addRowBelowButton = new QPushButton("Add row below");
63 connect(addRowBelowButton, SIGNAL(clicked()), this, SLOT(addRowBelow()));
63 connect(addRowBelowButton, SIGNAL(clicked()), this, SLOT(addRowBelow()));
64
64
65 QPushButton* removeRowButton = new QPushButton("Remove row");
65 QPushButton* removeRowButton = new QPushButton("Remove row");
66 connect(removeRowButton, SIGNAL(clicked()), this, SLOT(removeRow()));
66 connect(removeRowButton, SIGNAL(clicked()), this, SLOT(removeRow()));
67
67
68 QLabel *spinBoxLabel = new QLabel("Rows affected:");
68 QLabel *spinBoxLabel = new QLabel("Rows affected:");
69
69
70 // spin box for setting number of affected items (add, remove)
70 // spin box for setting number of affected items (add, remove)
71 m_linesCountSpinBox = new QSpinBox;
71 m_linesCountSpinBox = new QSpinBox;
72 m_linesCountSpinBox->setRange(1, 10);
72 m_linesCountSpinBox->setRange(1, 10);
73 m_linesCountSpinBox->setValue(1);
73 m_linesCountSpinBox->setValue(1);
74
74
75 // buttons layout
75 // buttons layout
76 QVBoxLayout* buttonsLayout = new QVBoxLayout;
76 QVBoxLayout* buttonsLayout = new QVBoxLayout;
77 buttonsLayout->addWidget(spinBoxLabel);
77 buttonsLayout->addWidget(spinBoxLabel);
78 buttonsLayout->addWidget(m_linesCountSpinBox);
78 buttonsLayout->addWidget(m_linesCountSpinBox);
79 buttonsLayout->addWidget(addRowAboveButton);
79 buttonsLayout->addWidget(addRowAboveButton);
80 buttonsLayout->addWidget(addRowBelowButton);
80 buttonsLayout->addWidget(addRowBelowButton);
81 buttonsLayout->addWidget(removeRowButton);
81 buttonsLayout->addWidget(removeRowButton);
82 buttonsLayout->addStretch();
82 buttonsLayout->addStretch();
83
83
84 // chart type radio buttons
84 // chart type radio buttons
85 m_lineRadioButton = new QRadioButton("Line");
85 m_lineRadioButton = new QRadioButton("Line");
86 m_splineRadioButton = new QRadioButton("Spline");
86 m_splineRadioButton = new QRadioButton("Spline");
87 m_scatterRadioButton = new QRadioButton("Scatter");
87 m_scatterRadioButton = new QRadioButton("Scatter");
88 m_pieRadioButton = new QRadioButton("Pie");
88 m_pieRadioButton = new QRadioButton("Pie");
89 m_areaRadioButton = new QRadioButton("Area");
89 m_areaRadioButton = new QRadioButton("Area");
90 m_barRadioButton = new QRadioButton("Bar");
90 m_barRadioButton = new QRadioButton("Bar");
91
91
92 connect(m_lineRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
92 connect(m_lineRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
93 connect(m_splineRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
93 connect(m_splineRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
94 connect(m_scatterRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
94 connect(m_scatterRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
95 connect(m_pieRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
95 connect(m_pieRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
96 connect(m_areaRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
96 connect(m_areaRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
97 connect(m_barRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
97 connect(m_barRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
98 m_lineRadioButton->setChecked(true);
98 m_lineRadioButton->setChecked(true);
99
99
100 // radio buttons layout
100 // radio buttons layout
101 QVBoxLayout* radioLayout = new QVBoxLayout;
101 QVBoxLayout* radioLayout = new QVBoxLayout;
102 radioLayout->addWidget(m_lineRadioButton);
102 radioLayout->addWidget(m_lineRadioButton);
103 radioLayout->addWidget(m_splineRadioButton);
103 radioLayout->addWidget(m_splineRadioButton);
104 radioLayout->addWidget(m_scatterRadioButton);
104 radioLayout->addWidget(m_scatterRadioButton);
105 radioLayout->addWidget(m_pieRadioButton);
105 radioLayout->addWidget(m_pieRadioButton);
106 radioLayout->addWidget(m_areaRadioButton);
106 radioLayout->addWidget(m_areaRadioButton);
107 radioLayout->addWidget(m_barRadioButton);
107 radioLayout->addWidget(m_barRadioButton);
108 radioLayout->addStretch();
108 radioLayout->addStretch();
109
109
110 // create main layout
110 // create main layout
111 QGridLayout* mainLayout = new QGridLayout;
111 QGridLayout* mainLayout = new QGridLayout;
112 mainLayout->addLayout(buttonsLayout, 1, 1);
112 mainLayout->addLayout(buttonsLayout, 1, 1);
113 mainLayout->addLayout(radioLayout, 2, 1);
113 mainLayout->addLayout(radioLayout, 2, 1);
114 mainLayout->addWidget(m_tableView, 1, 0);
114 mainLayout->addWidget(m_tableView, 1, 0);
115 mainLayout->addWidget(m_chartView, 2, 0);
115 mainLayout->addWidget(m_chartView, 2, 0);
116 setLayout(mainLayout);
116 setLayout(mainLayout);
117 m_lineRadioButton->setFocus();
117 m_lineRadioButton->setFocus();
118 }
118 }
119
119
120 void TableWidget::addRowAbove()
120 void TableWidget::addRowAbove()
121 {
121 {
122 m_model->insertRows(m_tableView->currentIndex().row(), m_linesCountSpinBox->value());
122 m_model->insertRows(m_tableView->currentIndex().row(), m_linesCountSpinBox->value());
123
123
124 }
124 }
125
125
126 void TableWidget::addRowBelow()
126 void TableWidget::addRowBelow()
127 {
127 {
128 m_model->insertRows(m_tableView->currentIndex().row() + 1, m_linesCountSpinBox->value());
128 m_model->insertRows(m_tableView->currentIndex().row() + 1, m_linesCountSpinBox->value());
129
129
130 }
130 }
131
131
132 void TableWidget::removeRow()
132 void TableWidget::removeRow()
133 {
133 {
134 m_model->removeRows(m_tableView->currentIndex().row(), qMin(m_model->rowCount() - m_tableView->currentIndex().row(), m_linesCountSpinBox->value()));
134 m_model->removeRows(m_tableView->currentIndex().row(), qMin(m_model->rowCount() - m_tableView->currentIndex().row(), m_linesCountSpinBox->value()));
135 }
135 }
136
136
137 void TableWidget::updateChartType(bool toggle)
137 void TableWidget::updateChartType(bool toggle)
138 {
138 {
139 // this if is needed, so that the function is only called once.
139 // this if is needed, so that the function is only called once.
140 // For the radioButton that was enabled.
140 // For the radioButton that was enabled.
141 if (toggle) {
141 if (toggle) {
142 m_chart->removeAllSeries();
142 m_chart->removeAllSeries();
143
143
144 // renable axes of the chart (pie hides them)
144 // renable axes of the chart (pie hides them)
145 // x axis
145 // x axis
146 QChartAxis *axis = m_chart->axisX();
146 QChartAxis *axis = m_chart->axisX();
147 axis->setAxisVisible(true);
147 axis->setAxisVisible(true);
148 axis->setGridLineVisible(true);
148 axis->setGridLineVisible(true);
149 axis->setLabelsVisible(true);
149 axis->setLabelsVisible(true);
150
150
151 // y axis
151 // y axis
152 axis = m_chart->axisY();
152 axis = m_chart->axisY();
153 axis->setAxisVisible(true);
153 axis->setAxisVisible(true);
154 axis->setGridLineVisible(true);
154 axis->setGridLineVisible(true);
155 axis->setLabelsVisible(true);
155 axis->setLabelsVisible(true);
156
156
157 m_model->clearMapping();
157 m_model->clearMapping();
158
158
159 QString seriesColorHex = "#000000";
159 QString seriesColorHex = "#000000";
160 QPen pen;
160 QPen pen;
161 pen.setWidth(2);
161 pen.setWidth(2);
162
162
163 if (m_lineRadioButton->isChecked())
163 if (m_lineRadioButton->isChecked())
164 {
164 {
165 // series 1
165 // series 1
166 m_series = new QLineSeries;
166 m_series = new QLineSeries;
167 m_series->setModel(m_model);
167 m_series->setModel(m_model);
168 m_series->setModelMapping(0,1, Qt::Vertical);
168 m_series->setModelMapping(0,1, Qt::Vertical);
169 m_series->setModelMappingRange(1, 4);
169 m_series->setModelMappingRange(1, 4);
170 m_chart->addSeries(m_series);
170 m_chart->addSeries(m_series);
171 seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
171 seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
172 m_model->addMapping(seriesColorHex, QRect(0, 1, 2, 4));
172 m_model->addMapping(seriesColorHex, QRect(0, 1, 2, 4));
173
173
174 // series 2
174 // series 2
175 m_series = new QLineSeries;
175 m_series = new QLineSeries;
176 m_series->setModel(m_model);
176 m_series->setModel(m_model);
177 m_series->setModelMapping(2,3, Qt::Vertical);
177 m_series->setModelMapping(2,3, Qt::Vertical);
178 m_chart->addSeries(m_series);
178 m_chart->addSeries(m_series);
179 seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
179 seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
180 m_model->addMapping(seriesColorHex, QRect(2, 0, 2, 1000));
180 m_model->addMapping(seriesColorHex, QRect(2, 0, 2, 1000));
181
181
182 // series 3
182 // series 3
183 m_series = new QLineSeries;
183 m_series = new QLineSeries;
184 m_series->setModel(m_model);
184 m_series->setModel(m_model);
185 m_series->setModelMapping(4,5, Qt::Vertical);
185 m_series->setModelMapping(4,5, Qt::Vertical);
186 m_series->setModelMappingRange(2, 0);
186 m_series->setModelMappingRange(2, 0);
187 m_chart->addSeries(m_series);
187 m_chart->addSeries(m_series);
188 seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
188 seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
189 m_model->addMapping(seriesColorHex, QRect(4, 2, 2, 1000));
189 m_model->addMapping(seriesColorHex, QRect(4, 2, 2, 1000));
190 }
190 }
191 else if (m_splineRadioButton->isChecked())
191 else if (m_splineRadioButton->isChecked())
192 {
192 {
193 // series 1
193 // series 1
194 m_series = new QSplineSeries;
194 m_series = new QSplineSeries;
195 m_series->setModel(m_model);
195 m_series->setModel(m_model);
196 m_series->setModelMapping(0,1, Qt::Vertical);
196 m_series->setModelMapping(0,1, Qt::Vertical);
197 m_series->setModelMappingRange(1, 4);
197 m_series->setModelMappingRange(1, 4);
198 // series->setModelMapping(0,1, Qt::Horizontal);
198 // series->setModelMapping(0,1, Qt::Horizontal);
199 m_chart->addSeries(m_series);
199 m_chart->addSeries(m_series);
200 seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
200 seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
201 m_model->addMapping(seriesColorHex, QRect(0, 1, 2, 4));
201 m_model->addMapping(seriesColorHex, QRect(0, 1, 2, 4));
202
202
203 // series 2
203 // series 2
204 m_series = new QSplineSeries;
204 m_series = new QSplineSeries;
205 m_series->setModel(m_model);
205 m_series->setModel(m_model);
206 m_series->setModelMapping(2,3, Qt::Vertical);
206 m_series->setModelMapping(2,3, Qt::Vertical);
207 m_series->setModelMappingRange(0, 0);
207 m_series->setModelMappingRange(0, 0);
208 // series->setModelMapping(2,3, Qt::Horizontal);
208 // series->setModelMapping(2,3, Qt::Horizontal);
209 m_chart->addSeries(m_series);
209 m_chart->addSeries(m_series);
210 seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
210 seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
211 m_model->addMapping(seriesColorHex, QRect(2, 0, 2, 1000));
211 m_model->addMapping(seriesColorHex, QRect(2, 0, 2, 1000));
212
212
213 // series 3
213 // series 3
214 m_series = new QSplineSeries;
214 m_series = new QSplineSeries;
215 m_series->setModel(m_model);
215 m_series->setModel(m_model);
216 m_series->setModelMapping(4,5, Qt::Vertical);
216 m_series->setModelMapping(4,5, Qt::Vertical);
217 m_series->setModelMappingRange(2, 0);
217 m_series->setModelMappingRange(2, 0);
218 // series->setModelMapping(4,5, Qt::Horizontal);
218 // series->setModelMapping(4,5, Qt::Horizontal);
219 m_chart->addSeries(m_series);
219 m_chart->addSeries(m_series);
220 seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
220 seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
221 m_model->addMapping(seriesColorHex, QRect(4, 2, 2, 1000));
221 m_model->addMapping(seriesColorHex, QRect(4, 2, 2, 1000));
222 }
222 }
223 else if (m_scatterRadioButton->isChecked())
223 else if (m_scatterRadioButton->isChecked())
224 {
224 {
225 // series 1
225 // series 1
226 m_series = new QScatterSeries;
226 m_series = new QScatterSeries;
227 m_series->setModel(m_model);
227 m_series->setModel(m_model);
228 m_series->setModelMapping(0,1, Qt::Vertical);
228 m_series->setModelMapping(0,1, Qt::Vertical);
229 m_series->setModelMappingRange(2, 0);
229 m_series->setModelMappingRange(2, 0);
230 // series->setModelMapping(0,1, Qt::Horizontal);
230 // series->setModelMapping(0,1, Qt::Horizontal);
231 m_chart->addSeries(m_series);
231 m_chart->addSeries(m_series);
232
232
233 seriesColorHex = "#" + QString::number(m_series->brush().color().rgb(), 16).right(6).toUpper();
233 seriesColorHex = "#" + QString::number(m_series->brush().color().rgb(), 16).right(6).toUpper();
234 m_model->addMapping(seriesColorHex, QRect(0, 2, 2, 1000));
234 m_model->addMapping(seriesColorHex, QRect(0, 2, 2, 1000));
235
235
236 // series 2
236 // series 2
237 m_series = new QScatterSeries;
237 m_series = new QScatterSeries;
238 m_series->setModel(m_model);
238 m_series->setModel(m_model);
239 m_series->setModelMapping(2,3, Qt::Vertical);
239 m_series->setModelMapping(2,3, Qt::Vertical);
240 m_series->setModelMappingRange(1, 6);
240 m_series->setModelMappingRange(1, 6);
241 // series->setModelMapping(2,3, Qt::Horizontal);
241 // series->setModelMapping(2,3, Qt::Horizontal);
242 m_chart->addSeries(m_series);
242 m_chart->addSeries(m_series);
243
243
244 seriesColorHex = "#" + QString::number(m_series->brush().color().rgb(), 16).right(6).toUpper();
244 seriesColorHex = "#" + QString::number(m_series->brush().color().rgb(), 16).right(6).toUpper();
245 m_model->addMapping(seriesColorHex, QRect(2, 1, 2, 6));
245 m_model->addMapping(seriesColorHex, QRect(2, 1, 2, 6));
246
246
247 // series 3
247 // series 3
248 m_series = new QScatterSeries;
248 m_series = new QScatterSeries;
249 m_series->setModel(m_model);
249 m_series->setModel(m_model);
250 m_series->setModelMapping(4,5, Qt::Vertical);
250 m_series->setModelMapping(4,5, Qt::Vertical);
251 // series->setModelMapping(4,5, Qt::Horizontal);
251 // series->setModelMapping(4,5, Qt::Horizontal);
252 m_chart->addSeries(m_series);
252 m_chart->addSeries(m_series);
253 seriesColorHex = "#" + QString::number(m_series->brush().color().rgb(), 16).right(6).toUpper();
253 seriesColorHex = "#" + QString::number(m_series->brush().color().rgb(), 16).right(6).toUpper();
254 m_model->addMapping(seriesColorHex, QRect(4, 0, 2, 1000));
254 m_model->addMapping(seriesColorHex, QRect(4, 0, 2, 1000));
255 }
255 }
256 else if (m_pieRadioButton->isChecked())
256 else if (m_pieRadioButton->isChecked())
257 {
257 {
258 // pie 1
258 // pie 1
259 QPieSeries* pieSeries = new QPieSeries;
259 QPieSeries* pieSeries = new QPieSeries;
260 pieSeries->setModel(m_model);
260 pieSeries->setModel(m_model);
261 pieSeries->setModelMapping(0,0, Qt::Vertical);
261 pieSeries->setModelMapping(0,0, Qt::Vertical);
262 pieSeries->setLabelsVisible(true);
262 pieSeries->setLabelsVisible(true);
263 pieSeries->setPieSize(0.4);
263 pieSeries->setPieSize(0.4);
264 pieSeries->setHorizontalPosition(0.2);
264 pieSeries->setHorizontalPosition(0.2);
265 pieSeries->setVerticalPosition(0.35);
265 pieSeries->setVerticalPosition(0.35);
266
266
267 m_chart->addSeries(pieSeries);
267 m_chart->addSeries(pieSeries);
268 seriesColorHex = "#" + QString::number(pieSeries->slices().at(pieSeries->slices().count()/2)->brush().color().rgb(), 16).right(6).toUpper();
268 seriesColorHex = "#" + QString::number(pieSeries->slices().at(pieSeries->slices().count()/2)->brush().color().rgb(), 16).right(6).toUpper();
269 m_model->addMapping(seriesColorHex, QRect(0, 0, 1, 1000));
269 m_model->addMapping(seriesColorHex, QRect(0, 0, 1, 1000));
270
270
271 // pie 2
271 // pie 2
272 pieSeries = new QPieSeries;
272 pieSeries = new QPieSeries;
273 pieSeries->setModel(m_model);
273 pieSeries->setModel(m_model);
274 pieSeries->setModelMapping(1,1, Qt::Vertical);
274 pieSeries->setModelMapping(1,1, Qt::Vertical);
275 pieSeries->setLabelsVisible(true);
275 pieSeries->setLabelsVisible(true);
276 pieSeries->setPieSize(0.4);
276 pieSeries->setPieSize(0.4);
277 pieSeries->setHorizontalPosition(0.8);
277 pieSeries->setHorizontalPosition(0.8);
278 pieSeries->setVerticalPosition(0.35);
278 pieSeries->setVerticalPosition(0.35);
279 m_chart->addSeries(pieSeries);
279 m_chart->addSeries(pieSeries);
280 seriesColorHex = "#" + QString::number(pieSeries->slices().at(pieSeries->slices().count()/2)->brush().color().rgb(), 16).right(6).toUpper();
280 seriesColorHex = "#" + QString::number(pieSeries->slices().at(pieSeries->slices().count()/2)->brush().color().rgb(), 16).right(6).toUpper();
281 m_model->addMapping(seriesColorHex, QRect(1, 0, 1, 1000));
281 m_model->addMapping(seriesColorHex, QRect(1, 0, 1, 1000));
282
282
283 // pie 3
283 // pie 3
284 pieSeries = new QPieSeries;
284 pieSeries = new QPieSeries;
285 pieSeries->setModel(m_model);
285 pieSeries->setModel(m_model);
286 pieSeries->setModelMapping(2,2, Qt::Vertical);
286 pieSeries->setModelMapping(2,2, Qt::Vertical);
287 pieSeries->setLabelsVisible(true);
287 pieSeries->setLabelsVisible(true);
288 pieSeries->setPieSize(0.4);
288 pieSeries->setPieSize(0.4);
289 pieSeries->setHorizontalPosition(0.5);
289 pieSeries->setHorizontalPosition(0.5);
290 pieSeries->setVerticalPosition(0.65);
290 pieSeries->setVerticalPosition(0.65);
291 m_chart->addSeries(pieSeries);
291 m_chart->addSeries(pieSeries);
292 seriesColorHex = "#" + QString::number(pieSeries->slices().at(pieSeries->slices().count()/2)->brush().color().rgb(), 16).right(6).toUpper();
292 seriesColorHex = "#" + QString::number(pieSeries->slices().at(pieSeries->slices().count()/2)->brush().color().rgb(), 16).right(6).toUpper();
293 m_model->addMapping(seriesColorHex, QRect(2, 0, 1, 1000));
293 m_model->addMapping(seriesColorHex, QRect(2, 0, 1, 1000));
294 }
294 }
295 else if (m_areaRadioButton->isChecked())
295 else if (m_areaRadioButton->isChecked())
296 {
296 {
297 QLineSeries* upperLineSeries = new QLineSeries;
297 QLineSeries* upperLineSeries = new QLineSeries;
298 upperLineSeries->setModel(m_model);
298 upperLineSeries->setModel(m_model);
299 upperLineSeries->setModelMapping(0, 1, Qt::Vertical);
299 upperLineSeries->setModelMapping(0, 1, Qt::Vertical);
300 upperLineSeries->setModelMappingRange(1, 5);
300 upperLineSeries->setModelMappingRange(1, 5);
301 QLineSeries* lowerLineSeries = new QLineSeries;
301 QLineSeries* lowerLineSeries = new QLineSeries;
302 lowerLineSeries->setModel(m_model);
302 lowerLineSeries->setModel(m_model);
303 lowerLineSeries->setModelMapping(2, 3, Qt::Vertical);
303 lowerLineSeries->setModelMapping(2, 3, Qt::Vertical);
304 QAreaSeries* areaSeries = new QAreaSeries(upperLineSeries, lowerLineSeries);
304 QAreaSeries* areaSeries = new QAreaSeries(upperLineSeries, lowerLineSeries);
305 m_chart->addSeries(areaSeries);
305 m_chart->addSeries(areaSeries);
306 seriesColorHex = "#" + QString::number(areaSeries->brush().color().rgb(), 16).right(6).toUpper();
306 seriesColorHex = "#" + QString::number(areaSeries->brush().color().rgb(), 16).right(6).toUpper();
307 m_model->addMapping(seriesColorHex, QRect(0, 1, 2, 5));
307 m_model->addMapping(seriesColorHex, QRect(0, 1, 2, 5));
308 m_model->addMapping(seriesColorHex, QRect(2, 0, 2, 1000));
308 m_model->addMapping(seriesColorHex, QRect(2, 0, 2, 1000));
309 }
309 }
310 else if (m_barRadioButton->isChecked())
310 else if (m_barRadioButton->isChecked())
311 {
311 {
312 QBarSeries* barSeries = new QBarSeries(QStringList());
312 QBarSeries* barSeries = new QBarSeries(QStringList());
313 barSeries->setModel(m_model);
313 barSeries->setModel(m_model);
314 barSeries->setModelMapping(5, 2, 4, Qt::Vertical);
314 barSeries->setModelMapping(5, 2, 4, Qt::Vertical);
315 barSeries->setToolTipEnabled(true);
315 barSeries->setToolTipEnabled(true);
316 m_chart->addSeries(barSeries);
316 m_chart->addSeries(barSeries);
317 for (int i = 0; i < barSeries->barsetCount(); i++) {
317 QList<QBarSet*> barsets = barSeries->barSets();
318 seriesColorHex = "#" + QString::number(barSeries->barsetAt(i)->brush().color().rgb(), 16).right(6).toUpper();
318 for (int i = 0; i < barsets.count(); i++) {
319 seriesColorHex = "#" + QString::number(barsets.at(i)->brush().color().rgb(), 16).right(6).toUpper();
319 m_model->addMapping(seriesColorHex, QRect(2 + i, 0, 1, 1000));
320 m_model->addMapping(seriesColorHex, QRect(2 + i, 0, 1, 1000));
320 }
321 }
321 }
322 }
322
323
323
324
324 m_chart->axisX()->setRange(0, 500);
325 m_chart->axisX()->setRange(0, 500);
325 m_chart->axisY()->setRange(0, 120);
326 m_chart->axisY()->setRange(0, 120);
326
327
327 // repaint table view colors
328 // repaint table view colors
328 m_tableView->repaint();
329 m_tableView->repaint();
329 m_tableView->setFocus();
330 m_tableView->setFocus();
330 }
331 }
331 }
332 }
332
333
333 TableWidget::~TableWidget()
334 TableWidget::~TableWidget()
334 {
335 {
335
336
336 }
337 }
General Comments 0
You need to be logged in to leave comments. Login now