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