##// END OF EJS Templates
Fixes to BarSeries and BarModelMapper
Marek Rosa -
r1356:2dd60111aef3
parent child
Show More
@@ -1,212 +1,212
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 "qbarset.h"
23 #include "qbarset.h"
24 #include "qbarset_p.h"
24 #include "qbarset_p.h"
25 #include "qbarseries.h"
25 #include "qbarseries.h"
26 #include "qbarseries_p.h"
26 #include "qbarseries_p.h"
27 #include "qchart.h"
27 #include "qchart.h"
28 #include "chartpresenter_p.h"
28 #include "chartpresenter_p.h"
29 #include "chartanimator_p.h"
29 #include "chartanimator_p.h"
30 #include "chartdataset_p.h"
30 #include "chartdataset_p.h"
31 #include <QPainter>
31 #include <QPainter>
32
32
33 QTCOMMERCIALCHART_BEGIN_NAMESPACE
33 QTCOMMERCIALCHART_BEGIN_NAMESPACE
34
34
35 BarChartItem::BarChartItem(QBarSeries *series, ChartPresenter *presenter) :
35 BarChartItem::BarChartItem(QBarSeries *series, ChartPresenter *presenter) :
36 ChartItem(presenter),
36 ChartItem(presenter),
37 m_series(series)
37 m_series(series)
38 {
38 {
39 setFlag(ItemClipsChildrenToShape);
39 setFlag(ItemClipsChildrenToShape);
40 connect(series->d_func(), SIGNAL(updatedBars()), this, SLOT(handleLayoutChanged()));
40 connect(series->d_func(), SIGNAL(updatedBars()), this, SLOT(handleLayoutChanged()));
41 connect(series->d_func(), SIGNAL(labelsVisibleChanged(bool)), this, SLOT(handleLabelsVisibleChanged(bool)));
41 connect(series->d_func(), SIGNAL(labelsVisibleChanged(bool)), this, SLOT(handleLabelsVisibleChanged(bool)));
42 connect(series->d_func(), SIGNAL(restructuredBars()), this, SLOT(handleDataStructureChanged()));
42 connect(series->d_func(), SIGNAL(restructuredBars()), this, SLOT(handleDataStructureChanged()));
43 connect(series, SIGNAL(visibleChanged()), this, SLOT(handleLayoutChanged()));
43 connect(series, SIGNAL(visibleChanged()), this, SLOT(handleLayoutChanged()));
44 setZValue(ChartPresenter::BarSeriesZValue);
44 setZValue(ChartPresenter::BarSeriesZValue);
45 handleDataStructureChanged();
45 handleDataStructureChanged();
46 }
46 }
47
47
48 BarChartItem::~BarChartItem()
48 BarChartItem::~BarChartItem()
49 {
49 {
50 }
50 }
51
51
52 void BarChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
52 void BarChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
53 {
53 {
54 Q_UNUSED(painter);
54 Q_UNUSED(painter);
55 Q_UNUSED(option);
55 Q_UNUSED(option);
56 Q_UNUSED(widget);
56 Q_UNUSED(widget);
57 }
57 }
58
58
59 QRectF BarChartItem::boundingRect() const
59 QRectF BarChartItem::boundingRect() const
60 {
60 {
61 return m_rect;
61 return m_rect;
62 }
62 }
63
63
64 void BarChartItem::handleDataStructureChanged()
64 void BarChartItem::handleDataStructureChanged()
65 {
65 {
66 foreach(QGraphicsItem *item, childItems()) {
66 foreach(QGraphicsItem *item, childItems()) {
67 delete item;
67 delete item;
68 }
68 }
69
69
70 m_bars.clear();
70 m_bars.clear();
71 m_labels.clear();
71 m_labels.clear();
72 m_layout.clear();
72 m_layout.clear();
73
73
74 bool labelsVisible = m_series->isLabelsVisible();
74 bool labelsVisible = m_series->isLabelsVisible();
75
75
76 // Create new graphic items for bars
76 // Create new graphic items for bars
77 for (int c = 0; c < m_series->d_func()->categoryCount(); c++) {
77 for (int c = 0; c < m_series->d_func()->categoryCount(); c++) {
78 for (int s = 0; s < m_series->barsetCount(); s++) {
78 for (int s = 0; s < m_series->barsetCount(); s++) {
79 QBarSet *set = m_series->d_func()->barsetAt(s);
79 QBarSet *set = m_series->d_func()->barsetAt(s);
80
80
81 // Bars
81 // Bars
82 Bar *bar = new Bar(set,c,this);
82 Bar *bar = new Bar(set,c,this);
83 m_bars.append(bar);
83 m_bars.append(bar);
84 connect(bar, SIGNAL(clicked(QBarSet*,int)), m_series, SIGNAL(clicked(QBarSet*,int)));
84 connect(bar, SIGNAL(clicked(QBarSet*,int)), m_series, SIGNAL(clicked(QBarSet*,int)));
85 connect(bar, SIGNAL(hovered(QBarSet*,bool)), m_series, SIGNAL(hovered(QBarSet*,bool)));
85 connect(bar, SIGNAL(hovered(QBarSet*,bool)), m_series, SIGNAL(hovered(QBarSet*,bool)));
86 m_layout.append(QRectF(0, 0, 0, 0));
86 m_layout.append(QRectF(0, 0, 0, 0));
87
87
88 // Labels
88 // Labels
89 QGraphicsSimpleTextItem *label = new QGraphicsSimpleTextItem(this);
89 QGraphicsSimpleTextItem *label = new QGraphicsSimpleTextItem(this);
90 label->setVisible(labelsVisible);
90 label->setVisible(labelsVisible);
91 m_labels.append(label);
91 m_labels.append(label);
92 }
92 }
93 }
93 }
94
94
95 // TODO: Is this the right place to call it?
95 // TODO: Is this the right place to call it?
96 // presenter()->chartTheme()->decorate(m_series, presenter()->dataSet()->seriesIndex(m_series));
96 presenter()->chartTheme()->decorate(m_series, presenter()->dataSet()->seriesIndex(m_series));
97 handleLayoutChanged();
97 handleLayoutChanged();
98 }
98 }
99
99
100 QVector<QRectF> BarChartItem::calculateLayout()
100 QVector<QRectF> BarChartItem::calculateLayout()
101 {
101 {
102 QVector<QRectF> layout;
102 QVector<QRectF> layout;
103
103
104 // Use temporary qreals for accuracy
104 // Use temporary qreals for accuracy
105 qreal categoryCount = m_series->d_func()->categoryCount();
105 qreal categoryCount = m_series->d_func()->categoryCount();
106 qreal setCount = m_series->barsetCount();
106 qreal setCount = m_series->barsetCount();
107 bool barsVisible = m_series->isVisible();
107 bool barsVisible = m_series->isVisible();
108
108
109 // Domain:
109 // Domain:
110 qreal width = geometry().width();
110 qreal width = geometry().width();
111 qreal height = geometry().height();
111 qreal height = geometry().height();
112 qreal rangeY = m_domainMaxY - m_domainMinY;
112 qreal rangeY = m_domainMaxY - m_domainMinY;
113 qreal rangeX = m_domainMaxX - m_domainMinX;
113 qreal rangeX = m_domainMaxX - m_domainMinX;
114 qreal scaleY = (height / rangeY);
114 qreal scaleY = (height / rangeY);
115 qreal scaleX = (width / rangeX);
115 qreal scaleX = (width / rangeX);
116 qreal barWidth = scaleX - scaleX * m_series->d_func()->barMargin();
116 qreal barWidth = scaleX - scaleX * m_series->d_func()->barMargin();
117
117
118 int itemIndex(0);
118 int itemIndex(0);
119 for (int category = 0; category < categoryCount; category++) {
119 for (int category = 0; category < categoryCount; category++) {
120 qreal yPos = height + scaleY * m_domainMinY + geometry().topLeft().y();
120 qreal yPos = height + scaleY * m_domainMinY + geometry().topLeft().y();
121 for (int set = 0; set < setCount; set++) {
121 for (int set = 0; set < setCount; set++) {
122 QBarSet* barSet = m_series->d_func()->barsetAt(set);
122 QBarSet* barSet = m_series->d_func()->barsetAt(set);
123 qreal xPos = (barSet->at(category).x() - m_domainMinX) * scaleX + m_rect.left() - barWidth/2;
123 qreal xPos = (barSet->at(category).x() - m_domainMinX) * scaleX + m_rect.left() - barWidth/2;
124 qreal barHeight = barSet->at(category).y() * scaleY;
124 qreal barHeight = barSet->at(category).y() * scaleY;
125
125
126 Bar* bar = m_bars.at(itemIndex);
126 Bar* bar = m_bars.at(itemIndex);
127 QRectF rect(xPos, yPos - barHeight, barWidth, barHeight);
127 QRectF rect(xPos, yPos - barHeight, barWidth, barHeight);
128
128
129 layout.append(rect);
129 layout.append(rect);
130 bar->setPen(barSet->pen());
130 bar->setPen(barSet->pen());
131 bar->setBrush(barSet->brush());
131 bar->setBrush(barSet->brush());
132 bar->setVisible(barsVisible);
132 bar->setVisible(barsVisible);
133
133
134 QGraphicsSimpleTextItem* label = m_labels.at(itemIndex);
134 QGraphicsSimpleTextItem* label = m_labels.at(itemIndex);
135
135
136 if (!qFuzzyIsNull(barSet->at(category).y())) {
136 if (!qFuzzyIsNull(barSet->at(category).y())) {
137 label->setText(QString::number(barSet->at(category).y()));
137 label->setText(QString::number(barSet->at(category).y()));
138 } else {
138 } else {
139 label->setText(QString(""));
139 label->setText(QString(""));
140 }
140 }
141
141
142 label->setPos(xPos + (rect.width()/2 - label->boundingRect().width()/2)
142 label->setPos(xPos + (rect.width()/2 - label->boundingRect().width()/2)
143 ,yPos - barHeight/2 - label->boundingRect().height()/2);
143 ,yPos - barHeight/2 - label->boundingRect().height()/2);
144 label->setFont(barSet->labelFont());
144 label->setFont(barSet->labelFont());
145 label->setBrush(barSet->labelBrush());
145 label->setBrush(barSet->labelBrush());
146
146
147 itemIndex++;
147 itemIndex++;
148 }
148 }
149 }
149 }
150
150
151 return layout;
151 return layout;
152 }
152 }
153
153
154 void BarChartItem::applyLayout(const QVector<QRectF> &layout)
154 void BarChartItem::applyLayout(const QVector<QRectF> &layout)
155 {
155 {
156 if (animator()) {
156 if (animator()) {
157 animator()->updateLayout(this, m_layout, layout);
157 animator()->updateLayout(this, m_layout, layout);
158 } else {
158 } else {
159 setLayout(layout);
159 setLayout(layout);
160 }
160 }
161 }
161 }
162
162
163 void BarChartItem::setLayout(const QVector<QRectF> &layout)
163 void BarChartItem::setLayout(const QVector<QRectF> &layout)
164 {
164 {
165 m_layout = layout;
165 m_layout = layout;
166
166
167 for (int i=0; i < m_bars.count(); i++) {
167 for (int i=0; i < m_bars.count(); i++) {
168 m_bars.at(i)->setRect(layout.at(i));
168 m_bars.at(i)->setRect(layout.at(i));
169 }
169 }
170
170
171 update();
171 update();
172 }
172 }
173 //handlers
173 //handlers
174
174
175 void BarChartItem::handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY)
175 void BarChartItem::handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY)
176 {
176 {
177 m_domainMinX = minX;
177 m_domainMinX = minX;
178 m_domainMaxX = maxX;
178 m_domainMaxX = maxX;
179 m_domainMinY = minY;
179 m_domainMinY = minY;
180 m_domainMaxY = maxY;
180 m_domainMaxY = maxY;
181 handleLayoutChanged();
181 handleLayoutChanged();
182 }
182 }
183
183
184 void BarChartItem::handleGeometryChanged(const QRectF &rect)
184 void BarChartItem::handleGeometryChanged(const QRectF &rect)
185 {
185 {
186 prepareGeometryChange();
186 prepareGeometryChange();
187 m_rect = rect;
187 m_rect = rect;
188 handleLayoutChanged();
188 handleLayoutChanged();
189 }
189 }
190
190
191 void BarChartItem::handleLayoutChanged()
191 void BarChartItem::handleLayoutChanged()
192 {
192 {
193 if ((m_rect.width() <= 0) || (m_rect.height() <= 0)) {
193 if ((m_rect.width() <= 0) || (m_rect.height() <= 0)) {
194 // rect size zero.
194 // rect size zero.
195 return;
195 return;
196 }
196 }
197 QVector<QRectF> layout = calculateLayout();
197 QVector<QRectF> layout = calculateLayout();
198 applyLayout(layout);
198 applyLayout(layout);
199 update();
199 update();
200 }
200 }
201
201
202 void BarChartItem::handleLabelsVisibleChanged(bool visible)
202 void BarChartItem::handleLabelsVisibleChanged(bool visible)
203 {
203 {
204 foreach (QGraphicsSimpleTextItem* label, m_labels) {
204 foreach (QGraphicsSimpleTextItem* label, m_labels) {
205 label->setVisible(visible);
205 label->setVisible(visible);
206 }
206 }
207 update();
207 update();
208 }
208 }
209
209
210 #include "moc_barchartitem_p.cpp"
210 #include "moc_barchartitem_p.cpp"
211
211
212 QTCOMMERCIALCHART_END_NAMESPACE
212 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,478 +1,483
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 "qbarmodelmapper.h"
21 #include "qbarmodelmapper.h"
22 #include "qbarmodelmapper_p.h"
22 #include "qbarmodelmapper_p.h"
23 #include "qbarseries.h"
23 #include "qbarseries.h"
24 #include "qbarset.h"
24 #include "qbarset.h"
25 #include "qchart.h"
25 #include "qchart.h"
26 #include "qaxis.h"
26 #include "qaxis.h"
27 #include <QAbstractItemModel>
27 #include <QAbstractItemModel>
28
28
29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30
30
31 /*!
31 /*!
32 \property QBarModelMapper::series
32 \property QBarModelMapper::series
33 \brief Defines the QPieSeries object that is used by the mapper.
33 \brief Defines the QPieSeries object that is used by the mapper.
34
34
35 All the data in the series in the series is discarded when it is set to the mapper.
35 All the data in the series in the series is discarded when it is set to the mapper.
36 When new series is specified the old series is disconnected (it preserves its data)
36 When new series is specified the old series is disconnected (it preserves its data)
37 */
37 */
38
38
39 /*!
39 /*!
40 \property QBarModelMapper::model
40 \property QBarModelMapper::model
41 \brief Defines the model that is used by the mapper.
41 \brief Defines the model that is used by the mapper.
42 */
42 */
43
43
44 /*!
44 /*!
45 \property QBarModelMapper::first
45 \property QBarModelMapper::first
46 \brief Defines which item of the model's row/column should be mapped as the value of the first QBarSet in the series.
46 \brief Defines which item of the model's row/column should be mapped as the value of the first QBarSet in the series.
47
47
48 Minimal and default value is: 0
48 Minimal and default value is: 0
49 */
49 */
50
50
51 /*!
51 /*!
52 \property QBarModelMapper::count
52 \property QBarModelMapper::count
53 \brief Defines the number of rows/columns of the model that are mapped as the data for QBarSeries
53 \brief Defines the number of rows/columns of the model that are mapped as the data for QBarSeries
54
54
55 Minimal and default value is: -1 (count limited by the number of rows/columns in the model)
55 Minimal and default value is: -1 (count limited by the number of rows/columns in the model)
56 */
56 */
57
57
58 /*!
58 /*!
59 \class QBarModelMapper
59 \class QBarModelMapper
60 \brief part of QtCommercial chart API.
60 \brief part of QtCommercial chart API.
61 \mainclass
61 \mainclass
62
62
63 The instance of this class cannot be created directly. QHBarModelMapper of QVBarModelMapper should be used instead. This class is used to create a connection between QBarSeries and QAbstractItemModel derived model object.
63 The instance of this class cannot be created directly. QHBarModelMapper of QVBarModelMapper should be used instead. This class is used to create a connection between QBarSeries and QAbstractItemModel derived model object.
64 Curently it is NOT possible to use both QAbstractItemModel and QXYSeries model API.
64 Curently it is NOT possible to use both QAbstractItemModel and QXYSeries model API.
65 When the series is set to the mapper the QBarSeries and QBarSet API that affect the data (append, setValue, remove) should not be used.
65 When the series is set to the mapper the QBarSeries and QBarSet API that affect the data (append, setValue, remove) should not be used.
66 The model and the QBarSeries won't be kept in sync. Model API should be used to insert,remove,modify BarSets.
66 The model and the QBarSeries won't be kept in sync. Model API should be used to insert,remove,modify BarSets.
67 NOTE: used model has to support adding/removing rows/columns and modifying the data of the cells.
67 NOTE: used model has to support adding/removing rows/columns and modifying the data of the cells.
68 */
68 */
69
69
70 /*!
70 /*!
71 Constructs a mapper object which is a child of \a parent.
71 Constructs a mapper object which is a child of \a parent.
72 */
72 */
73 QBarModelMapper::QBarModelMapper(QObject *parent) :
73 QBarModelMapper::QBarModelMapper(QObject *parent) :
74 QObject(parent),
74 QObject(parent),
75 d_ptr(new QBarModelMapperPrivate(this))
75 d_ptr(new QBarModelMapperPrivate(this))
76 {
76 {
77 }
77 }
78
78
79 QAbstractItemModel* QBarModelMapper::model() const
79 QAbstractItemModel* QBarModelMapper::model() const
80 {
80 {
81 Q_D(const QBarModelMapper);
81 Q_D(const QBarModelMapper);
82 return d->m_model;
82 return d->m_model;
83 }
83 }
84
84
85 void QBarModelMapper::setModel(QAbstractItemModel *model)
85 void QBarModelMapper::setModel(QAbstractItemModel *model)
86 {
86 {
87 if (model == 0)
87 if (model == 0)
88 return;
88 return;
89
89
90 Q_D(QBarModelMapper);
90 Q_D(QBarModelMapper);
91 if (d->m_model) {
91 if (d->m_model) {
92 disconnect(d->m_model, 0, d, 0);
92 disconnect(d->m_model, 0, d, 0);
93 }
93 }
94
94
95 d->m_model = model;
95 d->m_model = model;
96 d->initializeBarFromModel();
96 d->initializeBarFromModel();
97 // connect signals from the model
97 // connect signals from the model
98 connect(d->m_model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), d, SLOT(modelUpdated(QModelIndex,QModelIndex)));
98 connect(d->m_model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), d, SLOT(modelUpdated(QModelIndex,QModelIndex)));
99 connect(d->m_model, SIGNAL(rowsInserted(QModelIndex,int,int)), d, SLOT(modelRowsAdded(QModelIndex,int,int)));
99 connect(d->m_model, SIGNAL(rowsInserted(QModelIndex,int,int)), d, SLOT(modelRowsAdded(QModelIndex,int,int)));
100 connect(d->m_model, SIGNAL(rowsRemoved(QModelIndex,int,int)), d, SLOT(modelRowsRemoved(QModelIndex,int,int)));
100 connect(d->m_model, SIGNAL(rowsRemoved(QModelIndex,int,int)), d, SLOT(modelRowsRemoved(QModelIndex,int,int)));
101 connect(d->m_model, SIGNAL(columnsInserted(QModelIndex,int,int)), d, SLOT(modelColumnsAdded(QModelIndex,int,int)));
101 connect(d->m_model, SIGNAL(columnsInserted(QModelIndex,int,int)), d, SLOT(modelColumnsAdded(QModelIndex,int,int)));
102 connect(d->m_model, SIGNAL(columnsRemoved(QModelIndex,int,int)), d, SLOT(modelColumnsRemoved(QModelIndex,int,int)));
102 connect(d->m_model, SIGNAL(columnsRemoved(QModelIndex,int,int)), d, SLOT(modelColumnsRemoved(QModelIndex,int,int)));
103 }
103 }
104
104
105 QBarSeries* QBarModelMapper::series() const
105 QBarSeries* QBarModelMapper::series() const
106 {
106 {
107 Q_D(const QBarModelMapper);
107 Q_D(const QBarModelMapper);
108 return d->m_series;
108 return d->m_series;
109 }
109 }
110
110
111 void QBarModelMapper::setSeries(QBarSeries *series)
111 void QBarModelMapper::setSeries(QBarSeries *series)
112 {
112 {
113 Q_D(QBarModelMapper);
113 Q_D(QBarModelMapper);
114 if (d->m_series) {
114 if (d->m_series) {
115 disconnect(d->m_series, 0, d, 0);
115 disconnect(d->m_series, 0, d, 0);
116 }
116 }
117
117
118 if (series == 0)
118 if (series == 0)
119 return;
119 return;
120
120
121 d->m_series = series;
121 d->m_series = series;
122 d->initializeBarFromModel();
122 d->initializeBarFromModel();
123 // connect the signals from the series
123 // connect the signals from the series
124 // connect(d->m_series, SIGNAL(pointAdded(int)), d, SLOT(handlePointAdded(int)));
124 // connect(d->m_series, SIGNAL(pointAdded(int)), d, SLOT(handlePointAdded(int)));
125 // connect(d->m_series, SIGNAL(pointRemoved(int)), d, SLOT(handlePointRemoved(int)));
125 // connect(d->m_series, SIGNAL(pointRemoved(int)), d, SLOT(handlePointRemoved(int)));
126 // connect(d->m_series, SIGNAL(pointReplaced(int)), d, SLOT(handlePointReplaced(int)));
126 // connect(d->m_series, SIGNAL(pointReplaced(int)), d, SLOT(handlePointReplaced(int)));
127 }
127 }
128
128
129 int QBarModelMapper::first() const
129 int QBarModelMapper::first() const
130 {
130 {
131 Q_D(const QBarModelMapper);
131 Q_D(const QBarModelMapper);
132 return d->m_first;
132 return d->m_first;
133 }
133 }
134
134
135 void QBarModelMapper::setFirst(int first)
135 void QBarModelMapper::setFirst(int first)
136 {
136 {
137 Q_D(QBarModelMapper);
137 Q_D(QBarModelMapper);
138 d->m_first = qMax(first, 0);
138 d->m_first = qMax(first, 0);
139 d->initializeBarFromModel();
139 d->initializeBarFromModel();
140 }
140 }
141
141
142 int QBarModelMapper::count() const
142 int QBarModelMapper::count() const
143 {
143 {
144 Q_D(const QBarModelMapper);
144 Q_D(const QBarModelMapper);
145 return d->m_count;
145 return d->m_count;
146 }
146 }
147
147
148 void QBarModelMapper::setCount(int count)
148 void QBarModelMapper::setCount(int count)
149 {
149 {
150 Q_D(QBarModelMapper);
150 Q_D(QBarModelMapper);
151 d->m_count = qMax(count, -1);
151 d->m_count = qMax(count, -1);
152 d->initializeBarFromModel();
152 d->initializeBarFromModel();
153 }
153 }
154
154
155 /*!
155 /*!
156 Returns the orientation that is used when QBarModelMapper accesses the model.
156 Returns the orientation that is used when QBarModelMapper accesses the model.
157 This mean whether the consecutive values of the bar set are read from row (Qt::Horizontal)
157 This mean whether the consecutive values of the bar set are read from row (Qt::Horizontal)
158 or from columns (Qt::Vertical)
158 or from columns (Qt::Vertical)
159 */
159 */
160 Qt::Orientation QBarModelMapper::orientation() const
160 Qt::Orientation QBarModelMapper::orientation() const
161 {
161 {
162 Q_D(const QBarModelMapper);
162 Q_D(const QBarModelMapper);
163 return d->m_orientation;
163 return d->m_orientation;
164 }
164 }
165
165
166 /*!
166 /*!
167 Returns the \a orientation that is used when QBarModelMapper accesses the model.
167 Returns the \a orientation that is used when QBarModelMapper accesses the model.
168 This mean whether the consecutive values of the pie are read from row (Qt::Horizontal)
168 This mean whether the consecutive values of the pie are read from row (Qt::Horizontal)
169 or from columns (Qt::Vertical)
169 or from columns (Qt::Vertical)
170 */
170 */
171 void QBarModelMapper::setOrientation(Qt::Orientation orientation)
171 void QBarModelMapper::setOrientation(Qt::Orientation orientation)
172 {
172 {
173 Q_D(QBarModelMapper);
173 Q_D(QBarModelMapper);
174 d->m_orientation = orientation;
174 d->m_orientation = orientation;
175 d->initializeBarFromModel();
175 d->initializeBarFromModel();
176 }
176 }
177
177
178 /*!
178 /*!
179 Returns which section of the model is used as the data source for the first bar set
179 Returns which section of the model is used as the data source for the first bar set
180 */
180 */
181 int QBarModelMapper::firstBarSetSection() const
181 int QBarModelMapper::firstBarSetSection() const
182 {
182 {
183 Q_D(const QBarModelMapper);
183 Q_D(const QBarModelMapper);
184 return d->m_firstBarSetSection;
184 return d->m_firstBarSetSection;
185 }
185 }
186
186
187 /*!
187 /*!
188 Sets the model section that is used as the data source for the first bar set
188 Sets the model section that is used as the data source for the first bar set
189 Parameter \a firstBarSetSection specifies the section of the model.
189 Parameter \a firstBarSetSection specifies the section of the model.
190 */
190 */
191 void QBarModelMapper::setFirstBarSetSection(int firstBarSetSection)
191 void QBarModelMapper::setFirstBarSetSection(int firstBarSetSection)
192 {
192 {
193 Q_D(QBarModelMapper);
193 Q_D(QBarModelMapper);
194 d->m_firstBarSetSection = firstBarSetSection;
194 d->m_firstBarSetSection = firstBarSetSection;
195 d->initializeBarFromModel();
195 d->initializeBarFromModel();
196 }
196 }
197
197
198 /*!
198 /*!
199 Returns which section of the model is used as the data source for the last bar set
199 Returns which section of the model is used as the data source for the last bar set
200 */
200 */
201 int QBarModelMapper::lastBarSetSection() const
201 int QBarModelMapper::lastBarSetSection() const
202 {
202 {
203 Q_D(const QBarModelMapper);
203 Q_D(const QBarModelMapper);
204 return d->m_lastBarSetSection;
204 return d->m_lastBarSetSection;
205 }
205 }
206
206
207 /*!
207 /*!
208 Sets the model section that is used as the data source for the last bar set
208 Sets the model section that is used as the data source for the last bar set
209 Parameter \a lastBarSetSection specifies the section of the model.
209 Parameter \a lastBarSetSection specifies the section of the model.
210 */
210 */
211 void QBarModelMapper::setLastBarSetSection(int lastBarSetSection)
211 void QBarModelMapper::setLastBarSetSection(int lastBarSetSection)
212 {
212 {
213 Q_D(QBarModelMapper);
213 Q_D(QBarModelMapper);
214 d->m_lastBarSetSection = lastBarSetSection;
214 d->m_lastBarSetSection = lastBarSetSection;
215 d->initializeBarFromModel();
215 d->initializeBarFromModel();
216 }
216 }
217
217
218 /*!
218 /*!
219 Resets the QBarModelMapper to the default state.
219 Resets the QBarModelMapper to the default state.
220 first: 0; count: -1; firstBarSetSection: -1; lastBarSetSection: -1; categoriesSection: -1
220 first: 0; count: -1; firstBarSetSection: -1; lastBarSetSection: -1; categoriesSection: -1
221 */
221 */
222 void QBarModelMapper::reset()
222 void QBarModelMapper::reset()
223 {
223 {
224 Q_D(QBarModelMapper);
224 Q_D(QBarModelMapper);
225 d->m_first = 0;
225 d->m_first = 0;
226 d->m_count = -1;
226 d->m_count = -1;
227 d->m_firstBarSetSection = -1;
227 d->m_firstBarSetSection = -1;
228 d->m_lastBarSetSection = -1;
228 d->m_lastBarSetSection = -1;
229 d->initializeBarFromModel();
229 d->initializeBarFromModel();
230 }
230 }
231
231
232 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
232 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
233
233
234 QBarModelMapperPrivate::QBarModelMapperPrivate(QBarModelMapper *q) :
234 QBarModelMapperPrivate::QBarModelMapperPrivate(QBarModelMapper *q) :
235 m_series(0),
235 m_series(0),
236 m_model(0),
236 m_model(0),
237 m_first(0),
237 m_first(0),
238 m_count(-1),
238 m_count(-1),
239 m_orientation(Qt::Vertical),
239 m_orientation(Qt::Vertical),
240 m_firstBarSetSection(-1),
240 m_firstBarSetSection(-1),
241 m_lastBarSetSection(-1),
241 m_lastBarSetSection(-1),
242 m_seriesSignalsBlock(false),
242 m_seriesSignalsBlock(false),
243 m_modelSignalsBlock(false),
243 m_modelSignalsBlock(false),
244 q_ptr(q)
244 q_ptr(q)
245 {
245 {
246 }
246 }
247
247
248 void QBarModelMapperPrivate::blockModelSignals(bool block)
248 void QBarModelMapperPrivate::blockModelSignals(bool block)
249 {
249 {
250 m_modelSignalsBlock = block;
250 m_modelSignalsBlock = block;
251 }
251 }
252
252
253 void QBarModelMapperPrivate::blockSeriesSignals(bool block)
253 void QBarModelMapperPrivate::blockSeriesSignals(bool block)
254 {
254 {
255 m_seriesSignalsBlock = block;
255 m_seriesSignalsBlock = block;
256 }
256 }
257
257
258 QBarSet* QBarModelMapperPrivate::barSet(QModelIndex index)
258 QBarSet* QBarModelMapperPrivate::barSet(QModelIndex index)
259 {
259 {
260 if (!index.isValid())
260 if (!index.isValid())
261 return 0;
261 return 0;
262
262
263 if (m_orientation == Qt::Vertical && index.column() >= m_firstBarSetSection && index.column() <= m_lastBarSetSection) {
263 if (m_orientation == Qt::Vertical && index.column() >= m_firstBarSetSection && index.column() <= m_lastBarSetSection) {
264 if (index.row() >= m_first && (m_count == - 1 || index.row() < m_first + m_count)) {
264 if (index.row() >= m_first && (m_count == - 1 || index.row() < m_first + m_count)) {
265 // if (m_model->index(index.row(), m_valuesSection).isValid() && m_model->index(index.row(), m_labelsSection).isValid())
265 // if (m_model->index(index.row(), m_valuesSection).isValid() && m_model->index(index.row(), m_labelsSection).isValid())
266 return m_series->barSets().at(index.column() - m_firstBarSetSection);
266 return m_series->barSets().at(index.column() - m_firstBarSetSection);
267 // else
267 // else
268 // return 0;
268 // return 0;
269 }
269 }
270 } else if (m_orientation == Qt::Horizontal && index.row() >= m_firstBarSetSection && index.row() <= m_lastBarSetSection) {
270 } else if (m_orientation == Qt::Horizontal && index.row() >= m_firstBarSetSection && index.row() <= m_lastBarSetSection) {
271 if (index.column() >= m_first && (m_count == - 1 || index.column() < m_first + m_count))
271 if (index.column() >= m_first && (m_count == - 1 || index.column() < m_first + m_count))
272 return m_series->barSets().at(index.row() - m_firstBarSetSection);
272 return m_series->barSets().at(index.row() - m_firstBarSetSection);
273 }
273 }
274 return 0; // This part of model has not been mapped to any slice
274 return 0; // This part of model has not been mapped to any slice
275 }
275 }
276
276
277 QModelIndex QBarModelMapperPrivate::barModelIndex(int barSection, int posInBar)
277 QModelIndex QBarModelMapperPrivate::barModelIndex(int barSection, int posInBar)
278 {
278 {
279 if (m_count != -1 && posInBar >= m_count)
279 if (m_count != -1 && posInBar >= m_count)
280 return QModelIndex(); // invalid
280 return QModelIndex(); // invalid
281
281
282 if (barSection < m_firstBarSetSection || barSection > m_lastBarSetSection)
282 if (barSection < m_firstBarSetSection || barSection > m_lastBarSetSection)
283 return QModelIndex(); // invalid
283 return QModelIndex(); // invalid
284
284
285 if (m_orientation == Qt::Vertical)
285 if (m_orientation == Qt::Vertical)
286 return m_model->index(posInBar + m_first, barSection);
286 return m_model->index(posInBar + m_first, barSection);
287 else
287 else
288 return m_model->index(barSection, posInBar + m_first);
288 return m_model->index(barSection, posInBar + m_first);
289 }
289 }
290
290
291 void QBarModelMapperPrivate::modelUpdated(QModelIndex topLeft, QModelIndex bottomRight)
291 void QBarModelMapperPrivate::modelUpdated(QModelIndex topLeft, QModelIndex bottomRight)
292 {
292 {
293 Q_UNUSED(topLeft)
293 Q_UNUSED(topLeft)
294 Q_UNUSED(bottomRight)
294 Q_UNUSED(bottomRight)
295
295
296 if (m_model == 0 || m_series == 0)
296 if (m_model == 0 || m_series == 0)
297 return;
297 return;
298
298
299 if (m_modelSignalsBlock)
299 if (m_modelSignalsBlock)
300 return;
300 return;
301
301
302 blockSeriesSignals();
302 blockSeriesSignals();
303 QModelIndex index;
303 QModelIndex index;
304 for (int row = topLeft.row(); row <= bottomRight.row(); row++) {
304 for (int row = topLeft.row(); row <= bottomRight.row(); row++) {
305 for (int column = topLeft.column(); column <= bottomRight.column(); column++) {
305 for (int column = topLeft.column(); column <= bottomRight.column(); column++) {
306 index = topLeft.sibling(row, column);
306 index = topLeft.sibling(row, column);
307 QBarSet* bar = barSet(index);
307 QBarSet* bar = barSet(index);
308 if (bar) {
308 if (bar) {
309 if (m_orientation == Qt::Vertical)
309 if (m_orientation == Qt::Vertical)
310 bar->replace(row - m_first, m_model->data(index).toReal());
310 bar->replace(row - m_first, m_model->data(index).toReal());
311 else
311 else
312 bar->replace(column - m_first, m_model->data(index).toReal());
312 bar->replace(column - m_first, m_model->data(index).toReal());
313 }
313 }
314 }
314 }
315 }
315 }
316 blockSeriesSignals(false);
316 blockSeriesSignals(false);
317 }
317 }
318
318
319 void QBarModelMapperPrivate::modelRowsAdded(QModelIndex parent, int start, int end)
319 void QBarModelMapperPrivate::modelRowsAdded(QModelIndex parent, int start, int end)
320 {
320 {
321 Q_UNUSED(parent);
321 Q_UNUSED(parent);
322 Q_UNUSED(end)
322 Q_UNUSED(end)
323 if (m_modelSignalsBlock)
323 if (m_modelSignalsBlock)
324 return;
324 return;
325
325
326 blockSeriesSignals();
326 blockSeriesSignals();
327 if (m_orientation == Qt::Vertical)
327 if (m_orientation == Qt::Vertical)
328 // insertData(start, end);
328 // insertData(start, end);
329 initializeBarFromModel();
329 initializeBarFromModel();
330 else if (start <= m_firstBarSetSection || start <= m_lastBarSetSection) // if the changes affect the map - reinitialize
330 else if (start <= m_firstBarSetSection || start <= m_lastBarSetSection) // if the changes affect the map - reinitialize
331 initializeBarFromModel();
331 initializeBarFromModel();
332 blockSeriesSignals(false);
332 blockSeriesSignals(false);
333 }
333 }
334
334
335 void QBarModelMapperPrivate::modelRowsRemoved(QModelIndex parent, int start, int end)
335 void QBarModelMapperPrivate::modelRowsRemoved(QModelIndex parent, int start, int end)
336 {
336 {
337 Q_UNUSED(parent);
337 Q_UNUSED(parent);
338 Q_UNUSED(end)
338 Q_UNUSED(end)
339 if (m_modelSignalsBlock)
339 if (m_modelSignalsBlock)
340 return;
340 return;
341
341
342 blockSeriesSignals();
342 blockSeriesSignals();
343 if (m_orientation == Qt::Vertical)
343 if (m_orientation == Qt::Vertical)
344 // removeData(start, end);
344 // removeData(start, end);
345 initializeBarFromModel();
345 initializeBarFromModel();
346 else if (start <= m_firstBarSetSection || start <= m_lastBarSetSection) // if the changes affect the map - reinitialize
346 else if (start <= m_firstBarSetSection || start <= m_lastBarSetSection) // if the changes affect the map - reinitialize
347 initializeBarFromModel();
347 initializeBarFromModel();
348 blockSeriesSignals(false);
348 blockSeriesSignals(false);
349 }
349 }
350
350
351 void QBarModelMapperPrivate::modelColumnsAdded(QModelIndex parent, int start, int end)
351 void QBarModelMapperPrivate::modelColumnsAdded(QModelIndex parent, int start, int end)
352 {
352 {
353 Q_UNUSED(parent);
353 Q_UNUSED(parent);
354 Q_UNUSED(end)
354 Q_UNUSED(end)
355 if (m_modelSignalsBlock)
355 if (m_modelSignalsBlock)
356 return;
356 return;
357
357
358 blockSeriesSignals();
358 blockSeriesSignals();
359 if (m_orientation == Qt::Horizontal)
359 if (m_orientation == Qt::Horizontal)
360 // insertData(start, end);
360 // insertData(start, end);
361 initializeBarFromModel();
361 initializeBarFromModel();
362 else if (start <= m_firstBarSetSection || start <= m_lastBarSetSection) // if the changes affect the map - reinitialize
362 else if (start <= m_firstBarSetSection || start <= m_lastBarSetSection) // if the changes affect the map - reinitialize
363 initializeBarFromModel();
363 initializeBarFromModel();
364 blockSeriesSignals(false);
364 blockSeriesSignals(false);
365 }
365 }
366
366
367 void QBarModelMapperPrivate::modelColumnsRemoved(QModelIndex parent, int start, int end)
367 void QBarModelMapperPrivate::modelColumnsRemoved(QModelIndex parent, int start, int end)
368 {
368 {
369 Q_UNUSED(parent);
369 Q_UNUSED(parent);
370 Q_UNUSED(end)
370 Q_UNUSED(end)
371 if (m_modelSignalsBlock)
371 if (m_modelSignalsBlock)
372 return;
372 return;
373
373
374 blockSeriesSignals();
374 blockSeriesSignals();
375 if (m_orientation == Qt::Horizontal)
375 if (m_orientation == Qt::Horizontal)
376 // removeData(start, end);
376 // removeData(start, end);
377 initializeBarFromModel();
377 initializeBarFromModel();
378 else if (start <= m_firstBarSetSection || start <= m_lastBarSetSection) // if the changes affect the map - reinitialize
378 else if (start <= m_firstBarSetSection || start <= m_lastBarSetSection) // if the changes affect the map - reinitialize
379 initializeBarFromModel();
379 initializeBarFromModel();
380 blockSeriesSignals(false);
380 blockSeriesSignals(false);
381 }
381 }
382
382
383 void QBarModelMapperPrivate::insertData(int start, int end)
383 void QBarModelMapperPrivate::insertData(int start, int end)
384 {
384 {
385 Q_UNUSED(end)
385 Q_UNUSED(end)
386 if (m_model == 0 || m_series == 0)
386 if (m_model == 0 || m_series == 0)
387 return;
387 return;
388
388
389 if (m_count != -1 && start >= m_first + m_count) {
389 if (m_count != -1 && start >= m_first + m_count) {
390 return;
390 return;
391 } /*else {
391 } /*else {
392 int addedCount = end - start + 1;
392 int addedCount = end - start + 1;
393 if (m_count != -1 && addedCount > m_count)
393 if (m_count != -1 && addedCount > m_count)
394 addedCount = m_count;
394 addedCount = m_count;
395 int first = qMax(start, m_first);
395 int first = qMax(start, m_first);
396 int last = qMin(first + addedCount - 1, m_orientation == Qt::Vertical ? m_model->rowCount() - 1 : m_model->columnCount() - 1);
396 int last = qMin(first + addedCount - 1, m_orientation == Qt::Vertical ? m_model->rowCount() - 1 : m_model->columnCount() - 1);
397 for (int k = 0; k < m_series->barSets().count(); k++) {
397 for (int k = 0; k < m_series->barSets().count(); k++) {
398 for (int i = first; i <= last; i++) {
398 for (int i = first; i <= last; i++) {
399 QBar point;
399 QBar point;
400 point.setX(m_model->data(xModelIndex(i - m_first), Qt::DisplayRole).toDouble());
400 point.setX(m_model->data(xModelIndex(i - m_first), Qt::DisplayRole).toDouble());
401 point.setY(m_model->data(yModelIndex(i - m_first), Qt::DisplayRole).toDouble());
401 point.setY(m_model->data(yModelIndex(i - m_first), Qt::DisplayRole).toDouble());
402 m_series->insert(i - m_first, point);
402 m_series->insert(i - m_first, point);
403 }
403 }
404 >>>>>>> Stashed changes
404 >>>>>>> Stashed changes
405 }
405 }
406
406
407 // remove excess of slices (abouve m_count)
407 // remove excess of slices (abouve m_count)
408 if (m_count != -1 && m_series->points().size() > m_count)
408 if (m_count != -1 && m_series->points().size() > m_count)
409 for (int i = m_series->points().size() - 1; i >= m_count; i--) {
409 for (int i = m_series->points().size() - 1; i >= m_count; i--) {
410 m_series->remove(m_series->points().at(i));
410 m_series->remove(m_series->points().at(i));
411 }
411 }
412 }*/
412 }*/
413 }
413 }
414
414
415 void QBarModelMapperPrivate::removeData(int start, int end)
415 void QBarModelMapperPrivate::removeData(int start, int end)
416 {
416 {
417 Q_UNUSED(end)
417 Q_UNUSED(end)
418 if (m_model == 0 || m_series == 0)
418 if (m_model == 0 || m_series == 0)
419 return;
419 return;
420
420
421 // int removedCount = end - start + 1;
421 // int removedCount = end - start + 1;
422 if (m_count != -1 && start >= m_first + m_count) {
422 if (m_count != -1 && start >= m_first + m_count) {
423 return;
423 return;
424 } /*else {
424 } /*else {
425 int toRemove = qMin(m_series->count(), removedCount); // first find how many items can actually be removed
425 int toRemove = qMin(m_series->count(), removedCount); // first find how many items can actually be removed
426 int first = qMax(start, m_first); // get the index of the first item that will be removed.
426 int first = qMax(start, m_first); // get the index of the first item that will be removed.
427 int last = qMin(first + toRemove - 1, m_series->count() + m_first - 1); // get the index of the last item that will be removed.
427 int last = qMin(first + toRemove - 1, m_series->count() + m_first - 1); // get the index of the last item that will be removed.
428 for (int i = last; i >= first; i--) {
428 for (int i = last; i >= first; i--) {
429 m_series->remove(m_series->points().at(i - m_first));
429 m_series->remove(m_series->points().at(i - m_first));
430 }
430 }
431
431
432 if (m_count != -1) {
432 if (m_count != -1) {
433 int itemsAvailable; // check how many are available to be added
433 int itemsAvailable; // check how many are available to be added
434 if (m_orientation == Qt::Vertical)
434 if (m_orientation == Qt::Vertical)
435 itemsAvailable = m_model->rowCount() - m_first - m_series->count();
435 itemsAvailable = m_model->rowCount() - m_first - m_series->count();
436 else
436 else
437 itemsAvailable = m_model->columnCount() - m_first - m_series->count();
437 itemsAvailable = m_model->columnCount() - m_first - m_series->count();
438 int toBeAdded = qMin(itemsAvailable, m_count - m_series->count()); // add not more items than there is space left to be filled.
438 int toBeAdded = qMin(itemsAvailable, m_count - m_series->count()); // add not more items than there is space left to be filled.
439 int currentSize = m_series->count();
439 int currentSize = m_series->count();
440 if (toBeAdded > 0)
440 if (toBeAdded > 0)
441 for (int i = m_series->count(); i < currentSize + toBeAdded; i++) {
441 for (int i = m_series->count(); i < currentSize + toBeAdded; i++) {
442 QPointF point;
442 QPointF point;
443 point.setX(m_model->data(xModelIndex(i), Qt::DisplayRole).toDouble());
443 point.setX(m_model->data(xModelIndex(i), Qt::DisplayRole).toDouble());
444 point.setY(m_model->data(yModelIndex(i), Qt::DisplayRole).toDouble());
444 point.setY(m_model->data(yModelIndex(i), Qt::DisplayRole).toDouble());
445 m_series->insert(i, point);
445 m_series->insert(i, point);
446 }
446 }
447 }
447 }
448 }*/
448 }*/
449 }
449 }
450
450
451 void QBarModelMapperPrivate::initializeBarFromModel()
451 void QBarModelMapperPrivate::initializeBarFromModel()
452 {
452 {
453 if (m_model == 0 || m_series == 0)
453 if (m_model == 0 || m_series == 0)
454 return;
454 return;
455
455
456 blockSeriesSignals();
456 blockSeriesSignals();
457 // clear current content
457 // clear current content
458 m_series->clear();
458 m_series->clear();
459
459
460 // create the initial bar sets
460 // create the initial bar sets
461 for (int i = m_firstBarSetSection; i <= m_lastBarSetSection; i++) {
461 for (int i = m_firstBarSetSection; i <= m_lastBarSetSection; i++) {
462 int posInBar = 0;
462 int posInBar = 0;
463 QModelIndex barIndex = barModelIndex(i, posInBar);
463 QModelIndex barIndex = barModelIndex(i, posInBar);
464 QBarSet *barSet = new QBarSet(m_model->headerData(i, Qt::Horizontal).toString());
464 // check if there is such model index
465 while (barIndex.isValid()) {
465 if (barIndex.isValid()) {
466 barSet->append(m_model->data(barIndex, Qt::DisplayRole).toDouble());
466 QBarSet *barSet = new QBarSet(m_model->headerData(i, Qt::Horizontal).toString());
467 posInBar++;
467 while (barIndex.isValid()) {
468 barIndex = barModelIndex(i, posInBar);
468 barSet->append(m_model->data(barIndex, Qt::DisplayRole).toDouble());
469 posInBar++;
470 barIndex = barModelIndex(i, posInBar);
471 }
472 m_series->append(barSet);
473 } else {
474 break;
469 }
475 }
470 m_series->append(barSet);
471 }
476 }
472 blockSeriesSignals(false);
477 blockSeriesSignals(false);
473 }
478 }
474
479
475 #include "moc_qbarmodelmapper.cpp"
480 #include "moc_qbarmodelmapper.cpp"
476 #include "moc_qbarmodelmapper_p.cpp"
481 #include "moc_qbarmodelmapper_p.cpp"
477
482
478 QTCOMMERCIALCHART_END_NAMESPACE
483 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,567 +1,570
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 "domain_p.h"
25 #include "domain_p.h"
26 #include "legendmarker_p.h"
26 #include "legendmarker_p.h"
27 #include "chartdataset_p.h"
27 #include "chartdataset_p.h"
28 #include "charttheme_p.h"
28 #include "charttheme_p.h"
29 #include "chartanimator_p.h"
29 #include "chartanimator_p.h"
30
30
31 QTCOMMERCIALCHART_BEGIN_NAMESPACE
31 QTCOMMERCIALCHART_BEGIN_NAMESPACE
32
32
33 /*!
33 /*!
34 \class QBarSeries
34 \class QBarSeries
35 \brief part of QtCommercial chart API.
35 \brief part of QtCommercial chart API.
36 \mainclass
36 \mainclass
37
37
38 QBarSeries represents a series of data shown as bars. The purpose of this class is to draw bars to
38 QBarSeries represents a series of data shown as bars. The purpose of this class is to draw bars to
39 the position defined by data. Single bar is defined by QPointF, where x value is the x-coordinate of the bar
39 the position defined by data. Single bar is defined by QPointF, where x value is the x-coordinate of the bar
40 and y-value is the height of the bar. The category names are ignored with this series and x-axis
40 and y-value is the height of the bar. The category names are ignored with this series and x-axis
41 shows the x-values.
41 shows the x-values.
42
42
43 See the \l {BarChart Example} {bar chart example} to learn how to create a simple bar chart.
43 See the \l {BarChart Example} {bar chart example} to learn how to create a simple bar chart.
44 \image examples_barchart.png
44 \image examples_barchart.png
45
45
46 \sa QBarSet, QStackedBarSeries, QPercentBarSeries
46 \sa QBarSet, QStackedBarSeries, QPercentBarSeries
47 */
47 */
48
48
49 /*!
49 /*!
50 \fn void QBarSeries::clicked(QBarSet *barset, int index)
50 \fn void QBarSeries::clicked(QBarSet *barset, int index)
51
51
52 The signal is emitted if the user clicks with a mouse on top of QBarSet \a barset.
52 The signal is emitted if the user clicks with a mouse on top of QBarSet \a barset.
53 Clicked bar inside set is indexed by \a index
53 Clicked bar inside set is indexed by \a index
54 */
54 */
55
55
56 /*!
56 /*!
57 \fn void QBarSeries::hovered(QBarSet* barset, bool status)
57 \fn void QBarSeries::hovered(QBarSet* barset, bool status)
58
58
59 The signal is emitted if mouse is hovered on top of series.
59 The signal is emitted if mouse is hovered on top of series.
60 Parameter \a barset is the pointer of barset, where hover happened.
60 Parameter \a barset is the pointer of barset, where hover happened.
61 Parameter \a status is true, if mouse entered on top of series, false if mouse left from top of series.
61 Parameter \a status is true, if mouse entered on top of series, false if mouse left from top of series.
62 */
62 */
63
63
64 /*!
64 /*!
65 Constructs empty QBarSeries.
65 Constructs empty QBarSeries.
66 QBarSeries is QObject which is a child of a \a parent.
66 QBarSeries is QObject which is a child of a \a parent.
67 */
67 */
68 QBarSeries::QBarSeries(QObject *parent) :
68 QBarSeries::QBarSeries(QObject *parent) :
69 QAbstractSeries(*new QBarSeriesPrivate(this),parent)
69 QAbstractSeries(*new QBarSeriesPrivate(this),parent)
70 {
70 {
71 }
71 }
72
72
73 /*!
73 /*!
74 Destructs barseries and owned barsets.
74 Destructs barseries and owned barsets.
75 */
75 */
76 QBarSeries::~QBarSeries()
76 QBarSeries::~QBarSeries()
77 {
77 {
78 Q_D(QBarSeries);
78 Q_D(QBarSeries);
79 if(d->m_dataset){
79 if(d->m_dataset){
80 d->m_dataset->removeSeries(this);
80 d->m_dataset->removeSeries(this);
81 }
81 }
82 }
82 }
83
83
84 /*!
84 /*!
85 \internal
85 \internal
86 */
86 */
87 QBarSeries::QBarSeries(QBarSeriesPrivate &d, QObject *parent) :
87 QBarSeries::QBarSeries(QBarSeriesPrivate &d, QObject *parent) :
88 QAbstractSeries(d,parent)
88 QAbstractSeries(d,parent)
89 {
89 {
90 }
90 }
91
91
92 /*!
92 /*!
93 Returns the type of series. Derived classes override this.
93 Returns the type of series. Derived classes override this.
94 */
94 */
95 QAbstractSeries::SeriesType QBarSeries::type() const
95 QAbstractSeries::SeriesType QBarSeries::type() const
96 {
96 {
97 return QAbstractSeries::SeriesTypeBar;
97 return QAbstractSeries::SeriesTypeBar;
98 }
98 }
99
99
100 /*!
100 /*!
101 Sets the margin around bars. Parameter \a margin is from 0 to 1 and represents
101 Sets the margin around bars. Parameter \a margin is from 0 to 1 and represents
102 percentage of margin compared to bars
102 percentage of margin compared to bars
103 */
103 */
104 void QBarSeries::setBarMargin(qreal margin)
104 void QBarSeries::setBarMargin(qreal margin)
105 {
105 {
106 Q_D(QBarSeries);
106 Q_D(QBarSeries);
107 d->setBarMargin(margin);
107 d->setBarMargin(margin);
108 }
108 }
109
109
110 /*!
110 /*!
111 Returns the margin around bars
111 Returns the margin around bars
112 */
112 */
113 qreal QBarSeries::barMargin() const
113 qreal QBarSeries::barMargin() const
114 {
114 {
115 Q_D(const QBarSeries);
115 Q_D(const QBarSeries);
116 return d->barMargin();
116 return d->barMargin();
117 }
117 }
118
118
119 /*!
119 /*!
120 Adds a set of bars to series. Takes ownership of \a set. If the set is null or is already in series, it won't be appended.
120 Adds a set of bars to series. Takes ownership of \a set. If the set is null or is already in series, it won't be appended.
121 Returns true, if appending succeeded.
121 Returns true, if appending succeeded.
122
122
123 */
123 */
124 bool QBarSeries::append(QBarSet *set)
124 bool QBarSeries::append(QBarSet *set)
125 {
125 {
126 Q_D(QBarSeries);
126 Q_D(QBarSeries);
127 bool success = d->append(set);
127 bool success = d->append(set);
128 if (success) {
128 if (success) {
129 QList<QBarSet*> sets;
129 QList<QBarSet*> sets;
130 sets.append(set);
130 sets.append(set);
131 emit barsetsAdded(sets);
131 emit barsetsAdded(sets);
132 }
132 }
133 return success;
133 return success;
134 }
134 }
135
135
136 /*!
136 /*!
137 Removes a set of bars from series. Releases ownership of \a set. Doesn't delete \a set.
137 Removes a set of bars from series. Releases ownership of \a set. Doesn't delete \a set.
138 Returns true, if set was removed.
138 Returns true, if set was removed.
139 */
139 */
140 bool QBarSeries::remove(QBarSet *set)
140 bool QBarSeries::remove(QBarSet *set)
141 {
141 {
142 Q_D(QBarSeries);
142 Q_D(QBarSeries);
143 bool success = d->remove(set);
143 bool success = d->remove(set);
144 if (success) {
144 if (success) {
145 QList<QBarSet*> sets;
145 QList<QBarSet*> sets;
146 sets.append(set);
146 sets.append(set);
147 emit barsetsRemoved(sets);
147 emit barsetsRemoved(sets);
148 }
148 }
149 return success;
149 return success;
150 }
150 }
151
151
152 /*!
152 /*!
153 Adds a list of barsets to series. Takes ownership of \a sets.
153 Adds a list of barsets to series. Takes ownership of \a sets.
154 Returns true, if all sets were appended succesfully. If any of the sets is null or is already appended to series,
154 Returns true, if all sets were appended succesfully. If any of the sets is null or is already appended to series,
155 nothing is appended and function returns false. If any of the sets is in list more than once, nothing is appended
155 nothing is appended and function returns false. If any of the sets is in list more than once, nothing is appended
156 and function returns false.
156 and function returns false.
157 */
157 */
158 bool QBarSeries::append(QList<QBarSet* > sets)
158 bool QBarSeries::append(QList<QBarSet* > sets)
159 {
159 {
160 Q_D(QBarSeries);
160 Q_D(QBarSeries);
161 bool success = d->append(sets);
161 bool success = d->append(sets);
162 if (success) {
162 if (success) {
163 emit barsetsAdded(sets);
163 emit barsetsAdded(sets);
164 }
164 }
165 return success;
165 return success;
166 }
166 }
167
167
168 /*!
168 /*!
169 Removes a list of barsets from series. Releases ownership of \a sets. Doesn't delete \a sets.
169 Removes a list of barsets from series. Releases ownership of \a sets. Doesn't delete \a sets.
170 */
170 */
171 bool QBarSeries::remove(QList<QBarSet* > sets)
171 bool QBarSeries::remove(QList<QBarSet* > sets)
172 {
172 {
173 Q_D(QBarSeries);
173 Q_D(QBarSeries);
174 bool success = d->remove(sets);
174 bool success = d->remove(sets);
175 if (success) {
175 if (success) {
176 emit barsetsRemoved(sets);
176 emit barsetsRemoved(sets);
177 }
177 }
178 return success;
178 return success;
179 }
179 }
180
180
181 /*!
182 Removes all of the bar sets from the series
183 */
181 void QBarSeries::clear()
184 void QBarSeries::clear()
182 {
185 {
183 Q_D(QBarSeries);
186 Q_D(QBarSeries);
184 d->m_barSets.clear();
187 d->remove(barSets());
185 }
188 }
186
189
187 /*!
190 /*!
188 Returns number of sets in series.
191 Returns number of sets in series.
189 */
192 */
190 int QBarSeries::barsetCount() const
193 int QBarSeries::barsetCount() const
191 {
194 {
192 Q_D(const QBarSeries);
195 Q_D(const QBarSeries);
193 return d->m_barSets.count();
196 return d->m_barSets.count();
194 }
197 }
195
198
196 /*!
199 /*!
197 Returns a list of sets in series. Keeps ownership of sets.
200 Returns a list of sets in series. Keeps ownership of sets.
198 */
201 */
199 QList<QBarSet*> QBarSeries::barSets() const
202 QList<QBarSet*> QBarSeries::barSets() const
200 {
203 {
201 Q_D(const QBarSeries);
204 Q_D(const QBarSeries);
202 return d->m_barSets;
205 return d->m_barSets;
203 }
206 }
204
207
205 /*!
208 /*!
206 Sets the visibility of labels in series to \a visible
209 Sets the visibility of labels in series to \a visible
207 */
210 */
208 void QBarSeries::setLabelsVisible(bool visible)
211 void QBarSeries::setLabelsVisible(bool visible)
209 {
212 {
210 Q_D(QBarSeries);
213 Q_D(QBarSeries);
211 if (d->m_labelsVisible != visible) {
214 if (d->m_labelsVisible != visible) {
212 d->setLabelsVisible(visible);
215 d->setLabelsVisible(visible);
213 emit labelsVisibleChanged();
216 emit labelsVisibleChanged();
214 }
217 }
215 }
218 }
216
219
217 /*!
220 /*!
218 Returns the visibility of labels
221 Returns the visibility of labels
219 */
222 */
220 bool QBarSeries::isLabelsVisible() const
223 bool QBarSeries::isLabelsVisible() const
221 {
224 {
222 Q_D(const QBarSeries);
225 Q_D(const QBarSeries);
223 return d->m_labelsVisible;
226 return d->m_labelsVisible;
224 }
227 }
225
228
226 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
229 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
227
230
228 QBarSeriesPrivate::QBarSeriesPrivate(QBarSeries *q) :
231 QBarSeriesPrivate::QBarSeriesPrivate(QBarSeries *q) :
229 QAbstractSeriesPrivate(q),
232 QAbstractSeriesPrivate(q),
230 m_barMargin(0.5), // Default value is 50% of category width
233 m_barMargin(0.5), // Default value is 50% of category width
231 m_labelsVisible(false),
234 m_labelsVisible(false),
232 m_visible(true)
235 m_visible(true)
233 {
236 {
234 }
237 }
235
238
236 void QBarSeriesPrivate::setCategories(QStringList categories)
239 void QBarSeriesPrivate::setCategories(QStringList categories)
237 {
240 {
238 m_categories = categories;
241 m_categories = categories;
239 }
242 }
240
243
241 void QBarSeriesPrivate::insertCategory(int index, const QString category)
244 void QBarSeriesPrivate::insertCategory(int index, const QString category)
242 {
245 {
243 m_categories.insert(index, category);
246 m_categories.insert(index, category);
244 emit categoriesUpdated();
247 emit categoriesUpdated();
245 }
248 }
246
249
247 void QBarSeriesPrivate::removeCategory(int index)
250 void QBarSeriesPrivate::removeCategory(int index)
248 {
251 {
249 m_categories.removeAt(index);
252 m_categories.removeAt(index);
250 emit categoriesUpdated();
253 emit categoriesUpdated();
251 }
254 }
252
255
253 int QBarSeriesPrivate::categoryCount() const
256 int QBarSeriesPrivate::categoryCount() const
254 {
257 {
255 if (m_categories.count() > 0) {
258 if (m_categories.count() > 0) {
256 return m_categories.count();
259 return m_categories.count();
257 }
260 }
258
261
259 // No categories defined. return count of longest set.
262 // No categories defined. return count of longest set.
260 int count = 0;
263 int count = 0;
261 for (int i=0; i<m_barSets.count(); i++) {
264 for (int i=0; i<m_barSets.count(); i++) {
262 if (m_barSets.at(i)->count() > count) {
265 if (m_barSets.at(i)->count() > count) {
263 count = m_barSets.at(i)->count();
266 count = m_barSets.at(i)->count();
264 }
267 }
265 }
268 }
266
269
267 return count;
270 return count;
268 }
271 }
269
272
270 QStringList QBarSeriesPrivate::categories() const
273 QStringList QBarSeriesPrivate::categories() const
271 {
274 {
272 if (m_categories.count() > 0) {
275 if (m_categories.count() > 0) {
273 return m_categories;
276 return m_categories;
274 }
277 }
275
278
276 // No categories defined. retun list of indices.
279 // No categories defined. retun list of indices.
277 QStringList categories;
280 QStringList categories;
278
281
279 int count = categoryCount();
282 int count = categoryCount();
280 for (int i = 0; i < count; i++) {
283 for (int i = 0; i < count; i++) {
281 categories.append(QString::number(i));
284 categories.append(QString::number(i));
282 }
285 }
283 return categories;
286 return categories;
284 }
287 }
285
288
286 void QBarSeriesPrivate::setBarMargin(qreal margin)
289 void QBarSeriesPrivate::setBarMargin(qreal margin)
287 {
290 {
288 if (margin > 1.0) {
291 if (margin > 1.0) {
289 margin = 1.0;
292 margin = 1.0;
290 } else if (margin < 0.0) {
293 } else if (margin < 0.0) {
291 margin = 0.0;
294 margin = 0.0;
292 }
295 }
293
296
294 m_barMargin = margin;
297 m_barMargin = margin;
295 emit updatedBars();
298 emit updatedBars();
296 }
299 }
297
300
298 qreal QBarSeriesPrivate::barMargin() const
301 qreal QBarSeriesPrivate::barMargin() const
299 {
302 {
300 return m_barMargin;
303 return m_barMargin;
301 }
304 }
302
305
303 QBarSet* QBarSeriesPrivate::barsetAt(int index)
306 QBarSet* QBarSeriesPrivate::barsetAt(int index)
304 {
307 {
305 return m_barSets.at(index);
308 return m_barSets.at(index);
306 }
309 }
307
310
308 void QBarSeriesPrivate::setVisible(bool visible)
311 void QBarSeriesPrivate::setVisible(bool visible)
309 {
312 {
310 m_visible = visible;
313 m_visible = visible;
311 emit updatedBars();
314 emit updatedBars();
312 }
315 }
313
316
314 void QBarSeriesPrivate::setLabelsVisible(bool visible)
317 void QBarSeriesPrivate::setLabelsVisible(bool visible)
315 {
318 {
316 m_labelsVisible = visible;
319 m_labelsVisible = visible;
317 emit labelsVisibleChanged(visible);
320 emit labelsVisibleChanged(visible);
318 }
321 }
319
322
320 QString QBarSeriesPrivate::categoryName(int category)
323 QString QBarSeriesPrivate::categoryName(int category)
321 {
324 {
322 if ((category >= 0) && (category < m_categories.count())) {
325 if ((category >= 0) && (category < m_categories.count())) {
323 return m_categories.at(category);
326 return m_categories.at(category);
324 }
327 }
325
328
326 return QString::number(category);
329 return QString::number(category);
327 }
330 }
328
331
329 qreal QBarSeriesPrivate::min()
332 qreal QBarSeriesPrivate::min()
330 {
333 {
331 if (m_barSets.count() <= 0) {
334 if (m_barSets.count() <= 0) {
332 return 0;
335 return 0;
333 }
336 }
334 qreal min = INT_MAX;
337 qreal min = INT_MAX;
335
338
336 for (int i = 0; i < m_barSets.count(); i++) {
339 for (int i = 0; i < m_barSets.count(); i++) {
337 int categoryCount = m_barSets.at(i)->count();
340 int categoryCount = m_barSets.at(i)->count();
338 for (int j = 0; j < categoryCount; j++) {
341 for (int j = 0; j < categoryCount; j++) {
339 qreal temp = m_barSets.at(i)->at(j).y();
342 qreal temp = m_barSets.at(i)->at(j).y();
340 if (temp < min)
343 if (temp < min)
341 min = temp;
344 min = temp;
342 }
345 }
343 }
346 }
344 return min;
347 return min;
345 }
348 }
346
349
347 qreal QBarSeriesPrivate::max()
350 qreal QBarSeriesPrivate::max()
348 {
351 {
349 if (m_barSets.count() <= 0) {
352 if (m_barSets.count() <= 0) {
350 return 0;
353 return 0;
351 }
354 }
352 qreal max = INT_MIN;
355 qreal max = INT_MIN;
353
356
354 for (int i = 0; i < m_barSets.count(); i++) {
357 for (int i = 0; i < m_barSets.count(); i++) {
355 int categoryCount = m_barSets.at(i)->count();
358 int categoryCount = m_barSets.at(i)->count();
356 for (int j = 0; j < categoryCount; j++) {
359 for (int j = 0; j < categoryCount; j++) {
357 qreal temp = m_barSets.at(i)->at(j).y();
360 qreal temp = m_barSets.at(i)->at(j).y();
358 if (temp > max)
361 if (temp > max)
359 max = temp;
362 max = temp;
360 }
363 }
361 }
364 }
362
365
363 return max;
366 return max;
364 }
367 }
365
368
366 qreal QBarSeriesPrivate::valueAt(int set, int category)
369 qreal QBarSeriesPrivate::valueAt(int set, int category)
367 {
370 {
368 if ((set < 0) || (set >= m_barSets.count())) {
371 if ((set < 0) || (set >= m_barSets.count())) {
369 // No set, no value.
372 // No set, no value.
370 return 0;
373 return 0;
371 } else if ((category < 0) || (category >= m_barSets.at(set)->count())) {
374 } else if ((category < 0) || (category >= m_barSets.at(set)->count())) {
372 // No category, no value.
375 // No category, no value.
373 return 0;
376 return 0;
374 }
377 }
375
378
376 return m_barSets.at(set)->at(category).y();
379 return m_barSets.at(set)->at(category).y();
377 }
380 }
378
381
379 qreal QBarSeriesPrivate::percentageAt(int set, int category)
382 qreal QBarSeriesPrivate::percentageAt(int set, int category)
380 {
383 {
381 if ((set < 0) || (set >= m_barSets.count())) {
384 if ((set < 0) || (set >= m_barSets.count())) {
382 // No set, no value.
385 // No set, no value.
383 return 0;
386 return 0;
384 } else if ((category < 0) || (category >= m_barSets.at(set)->count())) {
387 } else if ((category < 0) || (category >= m_barSets.at(set)->count())) {
385 // No category, no value.
388 // No category, no value.
386 return 0;
389 return 0;
387 }
390 }
388
391
389 qreal value = m_barSets.at(set)->at(category).y();
392 qreal value = m_barSets.at(set)->at(category).y();
390 qreal sum = categorySum(category);
393 qreal sum = categorySum(category);
391 if ( qFuzzyIsNull(sum) ) {
394 if ( qFuzzyIsNull(sum) ) {
392 return 0;
395 return 0;
393 }
396 }
394
397
395 return value / sum;
398 return value / sum;
396 }
399 }
397
400
398 qreal QBarSeriesPrivate::categorySum(int category)
401 qreal QBarSeriesPrivate::categorySum(int category)
399 {
402 {
400 qreal sum(0);
403 qreal sum(0);
401 int count = m_barSets.count(); // Count sets
404 int count = m_barSets.count(); // Count sets
402 for (int set = 0; set < count; set++) {
405 for (int set = 0; set < count; set++) {
403 if (category < m_barSets.at(set)->count())
406 if (category < m_barSets.at(set)->count())
404 sum += m_barSets.at(set)->at(category).y();
407 sum += m_barSets.at(set)->at(category).y();
405 }
408 }
406 return sum;
409 return sum;
407 }
410 }
408
411
409 qreal QBarSeriesPrivate::absoluteCategorySum(int category)
412 qreal QBarSeriesPrivate::absoluteCategorySum(int category)
410 {
413 {
411 qreal sum(0);
414 qreal sum(0);
412 int count = m_barSets.count(); // Count sets
415 int count = m_barSets.count(); // Count sets
413 for (int set = 0; set < count; set++) {
416 for (int set = 0; set < count; set++) {
414 if (category < m_barSets.at(set)->count())
417 if (category < m_barSets.at(set)->count())
415 sum += qAbs(m_barSets.at(set)->at(category).y());
418 sum += qAbs(m_barSets.at(set)->at(category).y());
416 }
419 }
417 return sum;
420 return sum;
418 }
421 }
419
422
420 qreal QBarSeriesPrivate::maxCategorySum()
423 qreal QBarSeriesPrivate::maxCategorySum()
421 {
424 {
422 qreal max = INT_MIN;
425 qreal max = INT_MIN;
423 int count = categoryCount();
426 int count = categoryCount();
424 for (int i = 0; i < count; i++) {
427 for (int i = 0; i < count; i++) {
425 qreal sum = categorySum(i);
428 qreal sum = categorySum(i);
426 if (sum > max)
429 if (sum > max)
427 max = sum;
430 max = sum;
428 }
431 }
429 return max;
432 return max;
430 }
433 }
431
434
432 void QBarSeriesPrivate::scaleDomain(Domain& domain)
435 void QBarSeriesPrivate::scaleDomain(Domain& domain)
433 {
436 {
434 qreal minX(domain.minX());
437 qreal minX(domain.minX());
435 qreal minY(domain.minY());
438 qreal minY(domain.minY());
436 qreal maxX(domain.maxX());
439 qreal maxX(domain.maxX());
437 qreal maxY(domain.maxY());
440 qreal maxY(domain.maxY());
438 int tickXCount(domain.tickXCount());
441 int tickXCount(domain.tickXCount());
439 int tickYCount(domain.tickYCount());
442 int tickYCount(domain.tickYCount());
440
443
441 qreal x = categoryCount();
444 qreal x = categoryCount();
442 qreal y = max();
445 qreal y = max();
443 minX = qMin(minX, x) - 0.5;
446 minX = qMin(minX, x) - 0.5;
444 minY = qMin(minY, y);
447 minY = qMin(minY, y);
445 maxX = qMax(maxX, x) - 0.5;
448 maxX = qMax(maxX, x) - 0.5;
446 maxY = qMax(maxY, y);
449 maxY = qMax(maxY, y);
447 tickXCount = x+1;
450 tickXCount = x+1;
448
451
449 domain.setRange(minX,maxX,minY,maxY,tickXCount,tickYCount);
452 domain.setRange(minX,maxX,minY,maxY,tickXCount,tickYCount);
450 }
453 }
451
454
452 Chart* QBarSeriesPrivate::createGraphics(ChartPresenter* presenter)
455 Chart* QBarSeriesPrivate::createGraphics(ChartPresenter* presenter)
453 {
456 {
454 Q_Q(QBarSeries);
457 Q_Q(QBarSeries);
455
458
456 BarChartItem* bar = new BarChartItem(q,presenter);
459 BarChartItem* bar = new BarChartItem(q,presenter);
457 if(presenter->animationOptions().testFlag(QChart::SeriesAnimations)) {
460 if(presenter->animationOptions().testFlag(QChart::SeriesAnimations)) {
458 presenter->animator()->addAnimation(bar);
461 presenter->animator()->addAnimation(bar);
459 }
462 }
460 presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q));
463 presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q));
461 return bar;
464 return bar;
462
465
463 }
466 }
464
467
465 QList<LegendMarker*> QBarSeriesPrivate::createLegendMarker(QLegend* legend)
468 QList<LegendMarker*> QBarSeriesPrivate::createLegendMarker(QLegend* legend)
466 {
469 {
467 Q_Q(QBarSeries);
470 Q_Q(QBarSeries);
468 QList<LegendMarker*> markers;
471 QList<LegendMarker*> markers;
469 foreach(QBarSet* set, q->barSets()) {
472 foreach(QBarSet* set, q->barSets()) {
470 BarLegendMarker* marker = new BarLegendMarker(q,set,legend);
473 BarLegendMarker* marker = new BarLegendMarker(q,set,legend);
471 markers << marker;
474 markers << marker;
472 }
475 }
473
476
474 return markers;
477 return markers;
475 }
478 }
476
479
477 bool QBarSeriesPrivate::append(QBarSet *set)
480 bool QBarSeriesPrivate::append(QBarSet *set)
478 {
481 {
479 Q_Q(QBarSeries);
482 Q_Q(QBarSeries);
480 if ((m_barSets.contains(set)) || (set == 0)) {
483 if ((m_barSets.contains(set)) || (set == 0)) {
481 // Fail if set is already in list or set is null.
484 // Fail if set is already in list or set is null.
482 return false;
485 return false;
483 }
486 }
484 m_barSets.append(set);
487 m_barSets.append(set);
485 QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
488 QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
486 QObject::connect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
489 QObject::connect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
487 if (m_dataset) {
490 if (m_dataset) {
488 m_dataset->updateSeries(q); // this notifies legend
491 m_dataset->updateSeries(q); // this notifies legend
489 }
492 }
490 emit restructuredBars(); // this notifies barchartitem
493 emit restructuredBars(); // this notifies barchartitem
491 return true;
494 return true;
492 }
495 }
493
496
494 bool QBarSeriesPrivate::remove(QBarSet *set)
497 bool QBarSeriesPrivate::remove(QBarSet *set)
495 {
498 {
496 Q_Q(QBarSeries);
499 Q_Q(QBarSeries);
497 if (!m_barSets.contains(set)) {
500 if (!m_barSets.contains(set)) {
498 // Fail if set is not in list
501 // Fail if set is not in list
499 return false;
502 return false;
500 }
503 }
501 m_barSets.removeOne(set);
504 m_barSets.removeOne(set);
502 QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
505 QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
503 QObject::disconnect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
506 QObject::disconnect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
504 if (m_dataset) {
507 if (m_dataset) {
505 m_dataset->updateSeries(q); // this notifies legend
508 m_dataset->updateSeries(q); // this notifies legend
506 }
509 }
507 emit restructuredBars(); // this notifies barchartitem
510 emit restructuredBars(); // this notifies barchartitem
508 return true;
511 return true;
509 }
512 }
510
513
511 bool QBarSeriesPrivate::append(QList<QBarSet* > sets)
514 bool QBarSeriesPrivate::append(QList<QBarSet* > sets)
512 {
515 {
513 Q_Q(QBarSeries);
516 Q_Q(QBarSeries);
514 foreach (QBarSet* set, sets) {
517 foreach (QBarSet* set, sets) {
515 if ((set == 0) || (m_barSets.contains(set))) {
518 if ((set == 0) || (m_barSets.contains(set))) {
516 // Fail if any of the sets is null or is already appended.
519 // Fail if any of the sets is null or is already appended.
517 return false;
520 return false;
518 }
521 }
519 if (sets.count(set) != 1) {
522 if (sets.count(set) != 1) {
520 // Also fail if same set is more than once in given list.
523 // Also fail if same set is more than once in given list.
521 return false;
524 return false;
522 }
525 }
523 }
526 }
524
527
525 foreach (QBarSet* set, sets) {
528 foreach (QBarSet* set, sets) {
526 m_barSets.append(set);
529 m_barSets.append(set);
527 QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
530 QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
528 QObject::connect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
531 QObject::connect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
529 }
532 }
530 if (m_dataset) {
533 if (m_dataset) {
531 m_dataset->updateSeries(q); // this notifies legend
534 m_dataset->updateSeries(q); // this notifies legend
532 }
535 }
533 emit restructuredBars(); // this notifies barchartitem
536 emit restructuredBars(); // this notifies barchartitem
534 return true;
537 return true;
535 }
538 }
536
539
537 bool QBarSeriesPrivate::remove(QList<QBarSet* > sets)
540 bool QBarSeriesPrivate::remove(QList<QBarSet* > sets)
538 {
541 {
539 Q_Q(QBarSeries);
542 Q_Q(QBarSeries);
540 foreach (QBarSet* set, sets) {
543 foreach (QBarSet* set, sets) {
541 if ((set == 0) || (!m_barSets.contains(set))) {
544 if ((set == 0) || (!m_barSets.contains(set))) {
542 // Fail if any of the sets is null or is not in series
545 // Fail if any of the sets is null or is not in series
543 return false;
546 return false;
544 }
547 }
545 if (sets.count(set) != 1) {
548 if (sets.count(set) != 1) {
546 // Also fail if same set is more than once in given list.
549 // Also fail if same set is more than once in given list.
547 return false;
550 return false;
548 }
551 }
549 }
552 }
550
553
551 foreach (QBarSet* set, sets) {
554 foreach (QBarSet* set, sets) {
552 m_barSets.removeOne(set);
555 m_barSets.removeOne(set);
553 QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
556 QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
554 QObject::disconnect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
557 QObject::disconnect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
555 }
558 }
556
559
557 if (m_dataset) {
560 if (m_dataset) {
558 m_dataset->updateSeries(q); // this notifies legend
561 m_dataset->updateSeries(q); // this notifies legend
559 }
562 }
560 emit restructuredBars(); // this notifies barchartitem
563 emit restructuredBars(); // this notifies barchartitem
561 return true;
564 return true;
562 }
565 }
563
566
564 #include "moc_qbarseries.cpp"
567 #include "moc_qbarseries.cpp"
565 #include "moc_qbarseries_p.cpp"
568 #include "moc_qbarseries_p.cpp"
566
569
567 QTCOMMERCIALCHART_END_NAMESPACE
570 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,585 +1,590
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 <QChart>
24 #include <QChart>
25 #include <QStyledItemDelegate>
25 #include <QStyledItemDelegate>
26 #include <QLineSeries>
26 #include <QLineSeries>
27 #include <QSplineSeries>
27 #include <QSplineSeries>
28 #include <QScatterSeries>
28 #include <QScatterSeries>
29 #include <QVXYModelMapper>
29 #include <QVXYModelMapper>
30 #include <QHXYModelMapper>
30 #include <QHXYModelMapper>
31 #include "customtablemodel.h"
31 #include "customtablemodel.h"
32 #include <QPieSeries>
32 #include <QPieSeries>
33 #include <QVPieModelMapper>
33 #include <QVPieModelMapper>
34 #include <QPieSlice>
34 #include <QPieSlice>
35 #include <QAreaSeries>
35 #include <QAreaSeries>
36 #include <QBarSeries>
36 #include <QBarSeries>
37 #include <QGroupedBarSeries>
37 #include <QGroupedBarSeries>
38 #include <QBarSet>
38 #include <QBarSet>
39 #include <QVBarModelMapper>
39 #include <QVBarModelMapper>
40 #include <QPushButton>
40 #include <QPushButton>
41 #include <QRadioButton>
41 #include <QRadioButton>
42 #include <QLabel>
42 #include <QLabel>
43 #include <QSpinBox>
43 #include <QSpinBox>
44 #include <QTime>
44 #include <QTime>
45 #include <QHeaderView>
45 #include <QHeaderView>
46
46
47 TableWidget::TableWidget(QWidget *parent)
47 TableWidget::TableWidget(QWidget *parent)
48 : QWidget(parent),
48 : QWidget(parent),
49 m_series(0),
49 m_series(0),
50 m_mapper(0),
50 m_mapper(0),
51 m_model(0),
51 m_model(0),
52 m_pieMapper(0),
52 m_pieMapper(0),
53 m_pieMapper2(0)
53 m_pieMapper2(0),
54 m_barSeries(0),
55 m_barMapper(0)
54 // specialPie(0)
56 // specialPie(0)
55 {
57 {
56 setGeometry(1900, 100, 1000, 600);
58 setGeometry(1900, 100, 1000, 600);
57 qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
59 qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
58 // create simple model for storing data
60 // create simple model for storing data
59 // user's table data model
61 // user's table data model
60 m_model = new CustomTableModel;
62 m_model = new CustomTableModel;
61 m_tableView = new QTableView;
63 m_tableView = new QTableView;
62 m_tableView->setModel(m_model);
64 m_tableView->setModel(m_model);
63 // m_tableView->setMinimumHeight(300);
65 // m_tableView->setMinimumHeight(300);
64 m_tableView->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
66 m_tableView->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
65 m_tableView->verticalHeader()->setResizeMode(QHeaderView::Stretch);
67 m_tableView->verticalHeader()->setResizeMode(QHeaderView::Stretch);
66
68
67 m_chart = new QChart;
69 m_chart = new QChart;
68 m_chart->legend()->setVisible(true);
70 m_chart->legend()->setVisible(true);
69 m_chart->setAnimationOptions(QChart::SeriesAnimations);
71 m_chart->setAnimationOptions(QChart::SeriesAnimations);
70 m_chartView = new QChartView(m_chart);
72 m_chartView = new QChartView(m_chart);
71 m_chartView->setRenderHint(QPainter::Antialiasing);
73 m_chartView->setRenderHint(QPainter::Antialiasing);
72 m_chartView->setMinimumSize(640, 480);
74 m_chartView->setMinimumSize(640, 480);
73
75
74 // add, remove data buttons
76 // add, remove data buttons
75 QPushButton* addRowAboveButton = new QPushButton("Add row above");
77 QPushButton* addRowAboveButton = new QPushButton("Add row above");
76 connect(addRowAboveButton, SIGNAL(clicked()), this, SLOT(addRowAbove()));
78 connect(addRowAboveButton, SIGNAL(clicked()), this, SLOT(addRowAbove()));
77
79
78 QPushButton* addRowBelowButton = new QPushButton("Add row below");
80 QPushButton* addRowBelowButton = new QPushButton("Add row below");
79 connect(addRowBelowButton, SIGNAL(clicked()), this, SLOT(addRowBelow()));
81 connect(addRowBelowButton, SIGNAL(clicked()), this, SLOT(addRowBelow()));
80
82
81 QPushButton* removeRowButton = new QPushButton("Remove row");
83 QPushButton* removeRowButton = new QPushButton("Remove row");
82 connect(removeRowButton, SIGNAL(clicked()), this, SLOT(removeRow()));
84 connect(removeRowButton, SIGNAL(clicked()), this, SLOT(removeRow()));
83
85
84 QPushButton* addColumnRightButton = new QPushButton("Add column to the right");
86 QPushButton* addColumnRightButton = new QPushButton("Add column to the right");
85 connect(addColumnRightButton, SIGNAL(clicked()), this, SLOT(addColumnRight()));
87 connect(addColumnRightButton, SIGNAL(clicked()), this, SLOT(addColumnRight()));
86
88
87 QPushButton* removeColumnButton = new QPushButton("Remove column");
89 QPushButton* removeColumnButton = new QPushButton("Remove column");
88 connect(removeColumnButton, SIGNAL(clicked()), this, SLOT(removeColumn()));
90 connect(removeColumnButton, SIGNAL(clicked()), this, SLOT(removeColumn()));
89
91
90 QPushButton* specialPieButton = new QPushButton("Add slices using series API");
92 QPushButton* specialPieButton = new QPushButton("Add slices using series API");
91 connect(specialPieButton, SIGNAL(clicked()), this, SLOT(testPie()));
93 connect(specialPieButton, SIGNAL(clicked()), this, SLOT(testPie()));
92
94
93 QPushButton* specialPieButton2 = new QPushButton("Remove slices using series API");
95 QPushButton* specialPieButton2 = new QPushButton("Remove slices using series API");
94 connect(specialPieButton2, SIGNAL(clicked()), this, SLOT(testPie2()));
96 connect(specialPieButton2, SIGNAL(clicked()), this, SLOT(testPie2()));
95
97
96 QPushButton* specialPieButton3 = new QPushButton("Modify slices using series API");
98 QPushButton* specialPieButton3 = new QPushButton("Modify slices using series API");
97 connect(specialPieButton3, SIGNAL(clicked()), this, SLOT(testPie3()));
99 connect(specialPieButton3, SIGNAL(clicked()), this, SLOT(testPie3()));
98
100
99 QPushButton* xyTestButton = new QPushButton("Append XY point");
101 QPushButton* xyTestButton = new QPushButton("Append XY point");
100 connect(xyTestButton, SIGNAL(clicked()), this, SLOT(testXY()));
102 connect(xyTestButton, SIGNAL(clicked()), this, SLOT(testXY()));
101
103
102
104
103 QLabel *spinBoxLabel = new QLabel("Rows affected:");
105 QLabel *spinBoxLabel = new QLabel("Rows affected:");
104
106
105 // spin box for setting number of affected items (add, remove)
107 // spin box for setting number of affected items (add, remove)
106 m_linesCountSpinBox = new QSpinBox;
108 m_linesCountSpinBox = new QSpinBox;
107 m_linesCountSpinBox->setRange(1, 10);
109 m_linesCountSpinBox->setRange(1, 10);
108 m_linesCountSpinBox->setValue(1);
110 m_linesCountSpinBox->setValue(1);
109
111
110 // buttons layout
112 // buttons layout
111 QVBoxLayout* buttonsLayout = new QVBoxLayout;
113 QVBoxLayout* buttonsLayout = new QVBoxLayout;
112 buttonsLayout->addWidget(spinBoxLabel);
114 buttonsLayout->addWidget(spinBoxLabel);
113 buttonsLayout->addWidget(m_linesCountSpinBox);
115 buttonsLayout->addWidget(m_linesCountSpinBox);
114 // buttonsLayout->addWidget(addRowAboveButton);
116 // buttonsLayout->addWidget(addRowAboveButton);
115 buttonsLayout->addWidget(addRowBelowButton);
117 buttonsLayout->addWidget(addRowBelowButton);
116 buttonsLayout->addWidget(removeRowButton);
118 buttonsLayout->addWidget(removeRowButton);
117 // buttonsLayout->addWidget(addColumnRightButton);
119 // buttonsLayout->addWidget(addColumnRightButton);
118 // buttonsLayout->addWidget(removeColumnButton);
120 // buttonsLayout->addWidget(removeColumnButton);
119 buttonsLayout->addWidget(specialPieButton);
121 buttonsLayout->addWidget(specialPieButton);
120 buttonsLayout->addWidget(specialPieButton2);
122 buttonsLayout->addWidget(specialPieButton2);
121 buttonsLayout->addWidget(specialPieButton3);
123 buttonsLayout->addWidget(specialPieButton3);
122 buttonsLayout->addWidget(xyTestButton);
124 buttonsLayout->addWidget(xyTestButton);
123 buttonsLayout->addStretch();
125 buttonsLayout->addStretch();
124
126
125 // chart type radio buttons
127 // chart type radio buttons
126 m_lineRadioButton = new QRadioButton("Line");
128 m_lineRadioButton = new QRadioButton("Line");
127 m_splineRadioButton = new QRadioButton("Spline");
129 m_splineRadioButton = new QRadioButton("Spline");
128 m_scatterRadioButton = new QRadioButton("Scatter");
130 m_scatterRadioButton = new QRadioButton("Scatter");
129 m_pieRadioButton = new QRadioButton("Pie");
131 m_pieRadioButton = new QRadioButton("Pie");
130 m_areaRadioButton = new QRadioButton("Area");
132 m_areaRadioButton = new QRadioButton("Area");
131 m_barRadioButton = new QRadioButton("Bar");
133 m_barRadioButton = new QRadioButton("Bar");
132
134
133 connect(m_lineRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
135 connect(m_lineRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
134 connect(m_splineRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
136 connect(m_splineRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
135 connect(m_scatterRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
137 connect(m_scatterRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
136 connect(m_pieRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
138 connect(m_pieRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
137 connect(m_areaRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
139 connect(m_areaRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
138 connect(m_barRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
140 connect(m_barRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool)));
139 m_barRadioButton->setChecked(true);
141 m_barRadioButton->setChecked(true);
140
142
141 // radio buttons layout
143 // radio buttons layout
142 QVBoxLayout* radioLayout = new QVBoxLayout;
144 QVBoxLayout* radioLayout = new QVBoxLayout;
143 radioLayout->addWidget(m_lineRadioButton);
145 radioLayout->addWidget(m_lineRadioButton);
144 radioLayout->addWidget(m_splineRadioButton);
146 radioLayout->addWidget(m_splineRadioButton);
145 radioLayout->addWidget(m_scatterRadioButton);
147 radioLayout->addWidget(m_scatterRadioButton);
146 radioLayout->addWidget(m_pieRadioButton);
148 radioLayout->addWidget(m_pieRadioButton);
147 // radioLayout->addWidget(m_areaRadioButton);
149 // radioLayout->addWidget(m_areaRadioButton);
148 radioLayout->addWidget(m_barRadioButton);
150 radioLayout->addWidget(m_barRadioButton);
149 radioLayout->addStretch();
151 radioLayout->addStretch();
150
152
151 // create main layout
153 // create main layout
152 QGridLayout* mainLayout = new QGridLayout;
154 QGridLayout* mainLayout = new QGridLayout;
153 mainLayout->addLayout(buttonsLayout, 2, 0);
155 mainLayout->addLayout(buttonsLayout, 2, 0);
154 mainLayout->addLayout(radioLayout, 3, 0);
156 mainLayout->addLayout(radioLayout, 3, 0);
155 mainLayout->addWidget(m_tableView, 1, 0);
157 mainLayout->addWidget(m_tableView, 1, 0);
156 mainLayout->addWidget(m_chartView, 1, 1, 2, 1);
158 mainLayout->addWidget(m_chartView, 1, 1, 2, 1);
157 setLayout(mainLayout);
159 setLayout(mainLayout);
158 m_lineRadioButton->setFocus();
160 m_lineRadioButton->setFocus();
159 }
161 }
160
162
161 void TableWidget::addRowAbove()
163 void TableWidget::addRowAbove()
162 {
164 {
163 m_model->insertRows(m_tableView->currentIndex().row(), m_linesCountSpinBox->value());
165 m_model->insertRows(m_tableView->currentIndex().row(), m_linesCountSpinBox->value());
164
166
165 }
167 }
166
168
167 void TableWidget::addRowBelow()
169 void TableWidget::addRowBelow()
168 {
170 {
169 m_model->insertRows(m_tableView->currentIndex().row() + 1, m_linesCountSpinBox->value());
171 m_model->insertRows(m_tableView->currentIndex().row() + 1, m_linesCountSpinBox->value());
170
172
171 }
173 }
172
174
173 void TableWidget::removeRow()
175 void TableWidget::removeRow()
174 {
176 {
175 m_model->removeRows(m_tableView->currentIndex().row(), qMin(m_model->rowCount() - m_tableView->currentIndex().row(), m_linesCountSpinBox->value()));
177 m_model->removeRows(m_tableView->currentIndex().row(), qMin(m_model->rowCount() - m_tableView->currentIndex().row(), m_linesCountSpinBox->value()));
176 }
178 }
177
179
178 void TableWidget::addColumnRight()
180 void TableWidget::addColumnRight()
179 {
181 {
180 m_model->insertColumns(m_tableView->currentIndex().column() + 1, m_linesCountSpinBox->value());
182 m_model->insertColumns(m_tableView->currentIndex().column() + 1, m_linesCountSpinBox->value());
181 }
183 }
182
184
183 void TableWidget::removeColumn()
185 void TableWidget::removeColumn()
184 {
186 {
185 m_model->removeColumns(m_tableView->currentIndex().column(), qMin(m_model->columnCount() - m_tableView->currentIndex().column(), m_linesCountSpinBox->value()));
187 m_model->removeColumns(m_tableView->currentIndex().column(), qMin(m_model->columnCount() - m_tableView->currentIndex().column(), m_linesCountSpinBox->value()));
186 }
188 }
187
189
188 void TableWidget::updateChartType(bool toggle)
190 void TableWidget::updateChartType(bool toggle)
189 {
191 {
190 // this if is needed, so that the function is only called once.
192 // this if is needed, so that the function is only called once.
191 // For the radioButton that was enabled.
193 // For the radioButton that was enabled.
192 if (toggle) {
194 if (toggle) {
193 // specialPie = 0;
195 // specialPie = 0;
194 m_chart->removeAllSeries();
196 m_chart->removeAllSeries();
195 m_series = 0;
197 m_series = 0;
196 // m_chart->axisX()->setNiceNumbersEnabled(false);
198 // m_chart->axisX()->setNiceNumbersEnabled(false);
197 // m_chart->axisY()->setNiceNumbersEnabled(false);
199 // m_chart->axisY()->setNiceNumbersEnabled(false);
198 if (m_mapper) {
200 if (m_mapper) {
199 m_mapper->deleteLater();
201 m_mapper->deleteLater();
200 m_mapper = 0;
202 m_mapper = 0;
201 }
203 }
202
204
203 if (m_pieMapper) {
205 if (m_pieMapper) {
204 m_pieMapper->deleteLater();
206 m_pieMapper->deleteLater();
205 m_pieMapper = 0;
207 m_pieMapper = 0;
206 }
208 }
207
209
208 if (m_pieMapper2) {
210 if (m_pieMapper2) {
209 m_pieMapper2->deleteLater();
211 m_pieMapper2->deleteLater();
210 m_pieMapper2 = 0;
212 m_pieMapper2 = 0;
211 }
213 }
212
214
213 // if (m_series) {
215 // if (m_series) {
214 // delete m_series;
216 // delete m_series;
215 // m_series = 0;
217 // m_series = 0;
216 // }
218 // }
217
219
218 // renable axes of the chart (pie hides them)
220 // renable axes of the chart (pie hides them)
219 // x axis
221 // x axis
220 QAxis *axis = m_chart->axisX();
222 QAxis *axis = m_chart->axisX();
221 axis->setAxisVisible(true);
223 axis->setAxisVisible(true);
222 axis->setGridLineVisible(true);
224 axis->setGridLineVisible(true);
223 axis->setLabelsVisible(true);
225 axis->setLabelsVisible(true);
224
226
225 // y axis
227 // y axis
226 axis = m_chart->axisY();
228 axis = m_chart->axisY();
227 axis->setAxisVisible(true);
229 axis->setAxisVisible(true);
228 axis->setGridLineVisible(true);
230 axis->setGridLineVisible(true);
229 axis->setLabelsVisible(true);
231 axis->setLabelsVisible(true);
230
232
231 m_model->clearMapping();
233 m_model->clearMapping();
232
234
233 QString seriesColorHex = "#000000";
235 QString seriesColorHex = "#000000";
234 // QPen pen;
236 // QPen pen;
235 // pen.setWidth(2);
237 // pen.setWidth(2);
236
238
237 if (m_lineRadioButton->isChecked())
239 if (m_lineRadioButton->isChecked())
238 {
240 {
239 // m_chart->setAnimationOptions(QChart::NoAnimation);
241 // m_chart->setAnimationOptions(QChart::NoAnimation);
240
242
241 // series 1
243 // series 1
242 m_series = new QLineSeries;
244 m_series = new QLineSeries;
243
245
244 // m_mapper = new QHXYModelMapper;
246 // m_mapper = new QHXYModelMapper;
245 // m_mapper->setModel(m_model);
247 // m_mapper->setModel(m_model);
246 // m_mapper->setSeries(m_series);
248 // m_mapper->setSeries(m_series);
247 // m_mapper->setXRow(0);
249 // m_mapper->setXRow(0);
248 // m_mapper->setYRow(1);
250 // m_mapper->setYRow(1);
249 // m_mapper->setFirst(3);
251 // m_mapper->setFirst(3);
250 // m_mapper->setCount(4);
252 // m_mapper->setCount(4);
251
253
252 m_mapper = new QVXYModelMapper;
254 m_mapper = new QVXYModelMapper;
253 m_mapper->setModel(m_model);
255 m_mapper->setModel(m_model);
254 m_mapper->setSeries(m_series);
256 m_mapper->setSeries(m_series);
255 m_mapper->setXColumn(0);
257 m_mapper->setXColumn(0);
256 m_mapper->setYColumn(1);
258 m_mapper->setYColumn(1);
257 m_mapper->setFirst(3);
259 m_mapper->setFirst(3);
258 // m_mapper->setCount(4);
260 // m_mapper->setCount(4);
259
261
260 // m_series->setModelMapping(0,1, Qt::Vertical);
262 // m_series->setModelMapping(0,1, Qt::Vertical);
261 // m_series->setModelMappingRange(3, 4);
263 // m_series->setModelMappingRange(3, 4);
262 m_chart->addSeries(m_series);
264 m_chart->addSeries(m_series);
263 seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
265 seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
264 m_model->addMapping(seriesColorHex, QRect(0, 3, 2, 4));
266 m_model->addMapping(seriesColorHex, QRect(0, 3, 2, 4));
265
267
266 // // series 2
268 // // series 2
267 // m_series = new QLineSeries;
269 // m_series = new QLineSeries;
268 // m_series->setModel(m_model);
270 // m_series->setModel(m_model);
269
271
270 // mapper = new QXYModelMapper;
272 // mapper = new QXYModelMapper;
271 // mapper->setMapX(3);
273 // mapper->setMapX(3);
272 // mapper->setMapY(4);
274 // mapper->setMapY(4);
273 // // mapper->setFirst(3);
275 // // mapper->setFirst(3);
274 // // mapper->setCount(4);
276 // // mapper->setCount(4);
275 // m_series->setModelMapper(mapper);
277 // m_series->setModelMapper(mapper);
276 // // m_series->setModelMapping(2,3, Qt::Vertical);
278 // // m_series->setModelMapping(2,3, Qt::Vertical);
277 // m_chart->addSeries(m_series);
279 // m_chart->addSeries(m_series);
278 // seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
280 // seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
279 // m_model->addMapping(seriesColorHex, QRect(3, 0, 2, 1000));
281 // m_model->addMapping(seriesColorHex, QRect(3, 0, 2, 1000));
280
282
281 // // series 3
283 // // series 3
282 // m_series = new QLineSeries;
284 // m_series = new QLineSeries;
283 // m_series->setModel(m_model);
285 // m_series->setModel(m_model);
284
286
285 // mapper = new QXYModelMapper;
287 // mapper = new QXYModelMapper;
286 // mapper->setMapX(5);
288 // mapper->setMapX(5);
287 // mapper->setMapY(6);
289 // mapper->setMapY(6);
288 // mapper->setFirst(2);
290 // mapper->setFirst(2);
289 // mapper->setCount(-1);
291 // mapper->setCount(-1);
290 // m_series->setModelMapper(mapper);
292 // m_series->setModelMapper(mapper);
291 // // m_series->setModelMapping(4,5, Qt::Vertical);
293 // // m_series->setModelMapping(4,5, Qt::Vertical);
292 // // m_series->setModelMappingRange(2, -1);
294 // // m_series->setModelMappingRange(2, -1);
293 // m_chart->addSeries(m_series);
295 // m_chart->addSeries(m_series);
294 // seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
296 // seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
295 // m_model->addMapping(seriesColorHex, QRect(5, 2, 2, 1000));
297 // m_model->addMapping(seriesColorHex, QRect(5, 2, 2, 1000));
296 }
298 }
297 else if (m_splineRadioButton->isChecked())
299 else if (m_splineRadioButton->isChecked())
298 {
300 {
299 m_chart->setAnimationOptions(QChart::NoAnimation);
301 m_chart->setAnimationOptions(QChart::NoAnimation);
300
302
301 // series 1
303 // series 1
302 m_series = new QSplineSeries;
304 m_series = new QSplineSeries;
303 // m_series->setModel(m_model);
305 // m_series->setModel(m_model);
304
306
305 // m_mapper = new QVXYModelMapper;
307 // m_mapper = new QVXYModelMapper;
306 // m_mapper->setSeries(m_series);
308 // m_mapper->setSeries(m_series);
307 // m_mapper->setModel(m_model);
309 // m_mapper->setModel(m_model);
308 // m_mapper->setXColumn(0);
310 // m_mapper->setXColumn(0);
309 // m_mapper->setYColumn(1);
311 // m_mapper->setYColumn(1);
310 // m_mapper->setFirst(0);
312 // m_mapper->setFirst(0);
311 // m_mapper->setCount(-1);
313 // m_mapper->setCount(-1);
312
314
313 // m_series->setModelMapper(mapper);
315 // m_series->setModelMapper(mapper);
314
316
315 m_chart->addSeries(m_series);
317 m_chart->addSeries(m_series);
316 seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
318 seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
317 m_model->addMapping(seriesColorHex, QRect(0, 0, 2, 1000));
319 m_model->addMapping(seriesColorHex, QRect(0, 0, 2, 1000));
318
320
319 // // series 2
321 // // series 2
320 // m_series = new QSplineSeries;
322 // m_series = new QSplineSeries;
321 // m_series->setModel(m_model);
323 // m_series->setModel(m_model);
322
324
323 // mapper = new QXYModelMapper;
325 // mapper = new QXYModelMapper;
324 // mapper->setMapX(2);
326 // mapper->setMapX(2);
325 // mapper->setMapY(3);
327 // mapper->setMapY(3);
326 // mapper->setFirst(2);
328 // mapper->setFirst(2);
327 // mapper->setCount(4);
329 // mapper->setCount(4);
328
330
329 // m_series->setModelMapper(mapper);
331 // m_series->setModelMapper(mapper);
330
332
331 // m_chart->addSeries(m_series);
333 // m_chart->addSeries(m_series);
332 // seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
334 // seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
333 // m_model->addMapping(seriesColorHex, QRect(2, 2, 2, 4));
335 // m_model->addMapping(seriesColorHex, QRect(2, 2, 2, 4));
334
336
335 // // series 3
337 // // series 3
336 // m_series = new QSplineSeries;
338 // m_series = new QSplineSeries;
337 // m_series->setModel(m_model);
339 // m_series->setModel(m_model);
338
340
339 // mapper = new QXYModelMapper;
341 // mapper = new QXYModelMapper;
340 // mapper->setMapX(4);
342 // mapper->setMapX(4);
341 // mapper->setMapY(5);
343 // mapper->setMapY(5);
342 // mapper->setFirst(2);
344 // mapper->setFirst(2);
343 // mapper->setCount(-1);
345 // mapper->setCount(-1);
344
346
345 // m_series->setModelMapper(mapper);
347 // m_series->setModelMapper(mapper);
346
348
347 // m_chart->addSeries(m_series);
349 // m_chart->addSeries(m_series);
348 // seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
350 // seriesColorHex = "#" + QString::number(m_series->pen().color().rgb(), 16).right(6).toUpper();
349 // m_model->addMapping(seriesColorHex, QRect(4, 2, 2, 1000));
351 // m_model->addMapping(seriesColorHex, QRect(4, 2, 2, 1000));
350 } else if (m_scatterRadioButton->isChecked())
352 } else if (m_scatterRadioButton->isChecked())
351 {
353 {
352 m_chart->setAnimationOptions(QChart::NoAnimation);
354 m_chart->setAnimationOptions(QChart::NoAnimation);
353
355
354 // series 1
356 // series 1
355 m_series = new QScatterSeries;
357 m_series = new QScatterSeries;
356
358
357 // m_mapper = new QVXYModelMapper;
359 // m_mapper = new QVXYModelMapper;
358 // m_mapper->setSeries(m_series);
360 // m_mapper->setSeries(m_series);
359 // m_mapper->setModel(m_model);
361 // m_mapper->setModel(m_model);
360 // m_mapper->setXColumn(0);
362 // m_mapper->setXColumn(0);
361 // m_mapper->setYColumn(1);
363 // m_mapper->setYColumn(1);
362 // m_mapper->setFirst(0);
364 // m_mapper->setFirst(0);
363 // m_mapper->setCount(-1);
365 // m_mapper->setCount(-1);
364
366
365 m_chart->addSeries(m_series);
367 m_chart->addSeries(m_series);
366 seriesColorHex = "#" + QString::number(m_series->brush().color().rgb(), 16).right(6).toUpper();
368 seriesColorHex = "#" + QString::number(m_series->brush().color().rgb(), 16).right(6).toUpper();
367 m_model->addMapping(seriesColorHex, QRect(0, 2, 2, 1000));
369 m_model->addMapping(seriesColorHex, QRect(0, 2, 2, 1000));
368
370
369 // // series 2
371 // // series 2
370 // m_series = new QScatterSeries;
372 // m_series = new QScatterSeries;
371 // m_series->setModel(m_model);
373 // m_series->setModel(m_model);
372 // m_series->setModelMapping(2,3, Qt::Vertical);
374 // m_series->setModelMapping(2,3, Qt::Vertical);
373 // // m_series->setModelMappingRange(1, 6);
375 // // m_series->setModelMappingRange(1, 6);
374 // // series->setModelMapping(2,3, Qt::Horizontal);
376 // // series->setModelMapping(2,3, Qt::Horizontal);
375 // m_chart->addSeries(m_series);
377 // m_chart->addSeries(m_series);
376
378
377 // seriesColorHex = "#" + QString::number(m_series->brush().color().rgb(), 16).right(6).toUpper();
379 // seriesColorHex = "#" + QString::number(m_series->brush().color().rgb(), 16).right(6).toUpper();
378 // m_model->addMapping(seriesColorHex, QRect(2, 1, 2, 6));
380 // m_model->addMapping(seriesColorHex, QRect(2, 1, 2, 6));
379
381
380 // // series 3
382 // // series 3
381 // m_series = new QScatterSeries;
383 // m_series = new QScatterSeries;
382 // m_series->setModel(m_model);
384 // m_series->setModel(m_model);
383 // m_series->setModelMapping(4,5, Qt::Vertical);
385 // m_series->setModelMapping(4,5, Qt::Vertical);
384 // // series->setModelMapping(4,5, Qt::Horizontal);
386 // // series->setModelMapping(4,5, Qt::Horizontal);
385 // m_chart->addSeries(m_series);
387 // m_chart->addSeries(m_series);
386 // seriesColorHex = "#" + QString::number(m_series->brush().color().rgb(), 16).right(6).toUpper();
388 // seriesColorHex = "#" + QString::number(m_series->brush().color().rgb(), 16).right(6).toUpper();
387 // m_model->addMapping(seriesColorHex, QRect(4, 0, 2, 1000));
389 // m_model->addMapping(seriesColorHex, QRect(4, 0, 2, 1000));
388 } else if (m_pieRadioButton->isChecked()) {
390 } else if (m_pieRadioButton->isChecked()) {
389 m_chart->setAnimationOptions(QChart::SeriesAnimations);
391 m_chart->setAnimationOptions(QChart::SeriesAnimations);
390
392
391 // pie 1
393 // pie 1
392 m_pieSeries = new QPieSeries;
394 m_pieSeries = new QPieSeries;
393
395
394 m_pieMapper = new QVPieModelMapper;
396 m_pieMapper = new QVPieModelMapper;
395 m_pieMapper->setValuesColumn(1);
397 m_pieMapper->setValuesColumn(1);
396 m_pieMapper->setLabelsColumn(7);
398 m_pieMapper->setLabelsColumn(7);
397 m_pieMapper->setSeries(m_pieSeries);
399 m_pieMapper->setSeries(m_pieSeries);
398 m_pieMapper->setModel(m_model);
400 m_pieMapper->setModel(m_model);
399 m_pieMapper->setFirst(2);
401 m_pieMapper->setFirst(2);
400 // m_pieMapper->setCount(5);
402 // m_pieMapper->setCount(5);
401 // pieSeries->setModelMapper(mapper);
403 // pieSeries->setModelMapper(mapper);
402
404
403 m_pieSeries->setLabelsVisible(true);
405 m_pieSeries->setLabelsVisible(true);
404 m_pieSeries->setPieSize(0.35);
406 m_pieSeries->setPieSize(0.35);
405 m_pieSeries->setHorizontalPosition(0.25);
407 m_pieSeries->setHorizontalPosition(0.25);
406 m_pieSeries->setVerticalPosition(0.35);
408 m_pieSeries->setVerticalPosition(0.35);
407
409
408 m_chart->addSeries(m_pieSeries);
410 m_chart->addSeries(m_pieSeries);
409 seriesColorHex = "#" + QString::number(m_pieSeries->slices().at(m_pieSeries->slices().count()/2)->brush().color().rgb(), 16).right(6).toUpper();
411 seriesColorHex = "#" + QString::number(m_pieSeries->slices().at(m_pieSeries->slices().count()/2)->brush().color().rgb(), 16).right(6).toUpper();
410 m_model->addMapping(seriesColorHex, QRect(1, 2, 1, 50));
412 m_model->addMapping(seriesColorHex, QRect(1, 2, 1, 50));
411
413
412
414
413 // pieSeries->slices().at(0)->setValue(400);
415 // pieSeries->slices().at(0)->setValue(400);
414 // pieSeries->slices().at(0)->setLabel(QString("36"));
416 // pieSeries->slices().at(0)->setLabel(QString("36"));
415
417
416 // pie 2
418 // pie 2
417 m_pieSeries2 = new QPieSeries;
419 m_pieSeries2 = new QPieSeries;
418
420
419 m_pieMapper2 = new QVPieModelMapper;
421 m_pieMapper2 = new QVPieModelMapper;
420 m_pieMapper2->setValuesColumn(0);
422 m_pieMapper2->setValuesColumn(0);
421 m_pieMapper2->setLabelsColumn(7);
423 m_pieMapper2->setLabelsColumn(7);
422 m_pieMapper2->setModel(m_model);
424 m_pieMapper2->setModel(m_model);
423 m_pieMapper2->setSeries(m_pieSeries2);
425 m_pieMapper2->setSeries(m_pieSeries2);
424 m_pieMapper2->setFirst(2);
426 m_pieMapper2->setFirst(2);
425
427
426 m_pieSeries2->setLabelsVisible(true);
428 m_pieSeries2->setLabelsVisible(true);
427 m_pieSeries2->setPieSize(0.35);
429 m_pieSeries2->setPieSize(0.35);
428 m_pieSeries2->setHorizontalPosition(0.75);
430 m_pieSeries2->setHorizontalPosition(0.75);
429 m_pieSeries2->setVerticalPosition(0.65);
431 m_pieSeries2->setVerticalPosition(0.65);
430 m_chart->addSeries(m_pieSeries2);
432 m_chart->addSeries(m_pieSeries2);
431 seriesColorHex = "#" + QString::number(m_pieSeries2->slices().at(m_pieSeries2->slices().count()/2)->brush().color().rgb(), 16).right(6).toUpper();
433 seriesColorHex = "#" + QString::number(m_pieSeries2->slices().at(m_pieSeries2->slices().count()/2)->brush().color().rgb(), 16).right(6).toUpper();
432 m_model->addMapping(seriesColorHex, QRect(0, 2, 1, 1000));
434 m_model->addMapping(seriesColorHex, QRect(0, 2, 1, 1000));
433
435
434 // // pie 3
436 // // pie 3
435 // pieSeries = new QPieSeries;
437 // pieSeries = new QPieSeries;
436 // pieSeries->setModel(m_model);
438 // pieSeries->setModel(m_model);
437 // pieSeries->setModelMapping(2,2, Qt::Vertical);
439 // pieSeries->setModelMapping(2,2, Qt::Vertical);
438 // pieSeries->setLabelsVisible(true);
440 // pieSeries->setLabelsVisible(true);
439 // pieSeries->setPieSize(0.35);
441 // pieSeries->setPieSize(0.35);
440 // pieSeries->setHorizontalPosition(0.5);
442 // pieSeries->setHorizontalPosition(0.5);
441 // pieSeries->setVerticalPosition(0.75);
443 // pieSeries->setVerticalPosition(0.75);
442 // m_chart->addSeries(pieSeries);
444 // m_chart->addSeries(pieSeries);
443 // seriesColorHex = "#" + QString::number(pieSeries->slices().at(pieSeries->slices().count()/2)->brush().color().rgb(), 16).right(6).toUpper();
445 // seriesColorHex = "#" + QString::number(pieSeries->slices().at(pieSeries->slices().count()/2)->brush().color().rgb(), 16).right(6).toUpper();
444 // m_model->addMapping(seriesColorHex, QRect(2, 0, 1, 1000));
446 // m_model->addMapping(seriesColorHex, QRect(2, 0, 1, 1000));
445
447
446 // // special pie
448 // // special pie
447 // specialPie = new QPieSeries;
449 // specialPie = new QPieSeries;
448 // specialPie->append(17, "1");
450 // specialPie->append(17, "1");
449 // specialPie->append(45, "2");
451 // specialPie->append(45, "2");
450 // specialPie->append(77, "3");
452 // specialPie->append(77, "3");
451 // specialPie->append(37, "4");
453 // specialPie->append(37, "4");
452 // specialPie->append(27, "5");
454 // specialPie->append(27, "5");
453 // specialPie->append(47, "6");
455 // specialPie->append(47, "6");
454 // specialPie->setPieSize(0.35);
456 // specialPie->setPieSize(0.35);
455 // specialPie->setHorizontalPosition(0.8);
457 // specialPie->setHorizontalPosition(0.8);
456 // specialPie->setVerticalPosition(0.75);
458 // specialPie->setVerticalPosition(0.75);
457 // specialPie->setLabelsVisible(true);
459 // specialPie->setLabelsVisible(true);
458 // m_chart->addSeries(specialPie);
460 // m_chart->addSeries(specialPie);
459 }
461 }
460 // else if (m_areaRadioButton->isChecked())
462 // else if (m_areaRadioButton->isChecked())
461 // {
463 // {
462 // m_chart->setAnimationOptions(QChart::NoAnimation);
464 // m_chart->setAnimationOptions(QChart::NoAnimation);
463
465
464 // QLineSeries* upperLineSeries = new QLineSeries;
466 // QLineSeries* upperLineSeries = new QLineSeries;
465 // upperLineSeries->setModel(m_model);
467 // upperLineSeries->setModel(m_model);
466 // upperLineSeries->setModelMapping(0, 1, Qt::Vertical);
468 // upperLineSeries->setModelMapping(0, 1, Qt::Vertical);
467 // // upperLineSeries->setModelMappingRange(1, 5);
469 // // upperLineSeries->setModelMappingRange(1, 5);
468 // QLineSeries* lowerLineSeries = new QLineSeries;
470 // QLineSeries* lowerLineSeries = new QLineSeries;
469 // lowerLineSeries->setModel(m_model);
471 // lowerLineSeries->setModel(m_model);
470 // lowerLineSeries->setModelMapping(2, 3, Qt::Vertical);
472 // lowerLineSeries->setModelMapping(2, 3, Qt::Vertical);
471 // QAreaSeries* areaSeries = new QAreaSeries(upperLineSeries, lowerLineSeries);
473 // QAreaSeries* areaSeries = new QAreaSeries(upperLineSeries, lowerLineSeries);
472 // m_chart->addSeries(areaSeries);
474 // m_chart->addSeries(areaSeries);
473 // seriesColorHex = "#" + QString::number(areaSeries->brush().color().rgb(), 16).right(6).toUpper();
475 // seriesColorHex = "#" + QString::number(areaSeries->brush().color().rgb(), 16).right(6).toUpper();
474 // m_model->addMapping(seriesColorHex, QRect(0, 1, 2, 5));
476 // m_model->addMapping(seriesColorHex, QRect(0, 1, 2, 5));
475 // m_model->addMapping(seriesColorHex, QRect(2, 0, 2, 1000));
477 // m_model->addMapping(seriesColorHex, QRect(2, 0, 2, 1000));
476 // }
478 // }
477 else if (m_barRadioButton->isChecked())
479 else if (m_barRadioButton->isChecked())
478 {
480 {
479 m_chart->setAnimationOptions(QChart::SeriesAnimations);
481 // m_chart->setAnimationOptions(QChart::SeriesAnimations);
482 m_chart->setAnimationOptions(QChart::NoAnimation);
480
483
481 QGroupedBarSeries* barSeries = new QGroupedBarSeries();
484 m_barSeries = new QGroupedBarSeries();
482
485
483 int first = 3;
486 int first = 3;
484 int count = 6;
487 int count = 6;
485 QVBarModelMapper *mapper = new QVBarModelMapper;
488 m_barMapper = new QVBarModelMapper;
486 mapper->setFirstBarSetColumn(2);
489 m_barMapper->setFirstBarSetColumn(2);
487 mapper->setLastBarSetColumn(4);
490 m_barMapper->setLastBarSetColumn(4);
488 mapper->setFirst(first);
491 m_barMapper->setFirst(first);
489 mapper->setCount(count);
492 m_barMapper->setCount(count);
490 mapper->setSeries(barSeries);
493 m_barMapper->setSeries(m_barSeries);
491 mapper->setModel(m_model);
494 m_barMapper->setModel(m_model);
492 // barSeries->setModelMapper(mapper);
495 // barSeries->setModelMapper(mapper);
493 m_chart->addSeries(barSeries);
496 m_chart->addSeries(m_barSeries);
494
497
495 QStringList categories;
498 QStringList categories;
496 categories << "June" << "July" << "August" << "September" << "October" << "November";
499 categories << "June" << "July" << "August" << "September" << "October" << "November";
497
500
498 m_chart->axisX()->categories()->insert(categories);
501 m_chart->axisX()->categories()->insert(categories);
499
502
500 QList<QBarSet*> barsets = barSeries->barSets();
503 QList<QBarSet*> barsets = m_barSeries->barSets();
501 for (int i = 0; i < barsets.count(); i++) {
504 for (int i = 0; i < barsets.count(); i++) {
502 seriesColorHex = "#" + QString::number(barsets.at(i)->brush().color().rgb(), 16).right(6).toUpper();
505 seriesColorHex = "#" + QString::number(barsets.at(i)->brush().color().rgb(), 16).right(6).toUpper();
503 m_model->addMapping(seriesColorHex, QRect(2 + i, first, 1, barsets.at(i)->count()));
506 m_model->addMapping(seriesColorHex, QRect(2 + i, first, 1, barsets.at(i)->count()));
504 }
507 }
505
508
506
509
507 }
510 }
508
511
509
512
510 if (!m_barRadioButton->isChecked()) {
513 if (!m_barRadioButton->isChecked()) {
511 // m_chart->axisX()->setRange(0, 500);
514 // m_chart->axisX()->setRange(0, 500);
512 // m_chart->axisY()->setRange(0, 220);
515 // m_chart->axisY()->setRange(0, 220);
513 }
516 }
514 m_chart->legend()->setVisible(true);
517 m_chart->legend()->setVisible(true);
515
518
516 // repaint table view colors
519 // repaint table view colors
517 m_tableView->repaint();
520 m_tableView->repaint();
518 m_tableView->setFocus();
521 m_tableView->setFocus();
519 }
522 }
520 }
523 }
521
524
522 void TableWidget::testPie()
525 void TableWidget::testPie()
523 {
526 {
524 // m_pieMapper->setCount(-1);
527 // m_pieMapper->setCount(-1);
525 QPieSlice *slice = new QPieSlice("Hehe", 145);
528 QPieSlice *slice = new QPieSlice("Hehe", 145);
526 slice->setLabelVisible();
529 slice->setLabelVisible();
527 m_pieSeries->append(slice);
530 m_pieSeries->append(slice);
528
531
529 slice = new QPieSlice("Hoho", 34);
532 slice = new QPieSlice("Hoho", 34);
530 slice->setLabelVisible();
533 slice->setLabelVisible();
531 m_pieSeries->append(slice);
534 m_pieSeries->append(slice);
532 // m_series->modelMapper()->setMapX(4);
535 // m_series->modelMapper()->setMapX(4);
533 // m_tableView->setColumnWidth(10, 250);
536 // m_tableView->setColumnWidth(10, 250);
534 // if (specialPie) {
537 // if (specialPie) {
535 // specialPie->remove(specialPie->slices().at(2));
538 // specialPie->remove(specialPie->slices().at(2));
536 // // specialPie->insert(4, new QPieSlice(45, "Hello"));//specialPie->slices.at(2));
539 // // specialPie->insert(4, new QPieSlice(45, "Hello"));//specialPie->slices.at(2));
537 // specialPie->append(4, "heloo");
540 // specialPie->append(4, "heloo");
538 // }
541 // }
539 }
542 }
540
543
541 void TableWidget::testPie2()
544 void TableWidget::testPie2()
542 {
545 {
543 QPieSlice *slice;
546 QPieSlice *slice;
544 if (m_pieSeries->count() > 0) {
547 if (m_pieSeries->count() > 0) {
545 slice = m_pieSeries->slices().last();
548 slice = m_pieSeries->slices().last();
546 m_pieSeries->remove(slice);
549 m_pieSeries->remove(slice);
547 }
550 }
548
551
549 if (m_pieSeries->count() > 0) {
552 if (m_pieSeries->count() > 0) {
550 slice = m_pieSeries->slices().first();
553 slice = m_pieSeries->slices().first();
551 m_pieSeries->remove(slice);
554 m_pieSeries->remove(slice);
552 }
555 }
553 }
556 }
554
557
555 void TableWidget::testPie3()
558 void TableWidget::testPie3()
556 {
559 {
557 QPieSlice *slice;
560 QPieSlice *slice;
558 if (m_pieSeries->count() > 0) {
561 if (m_pieSeries->count() > 0) {
559 slice = m_pieSeries->slices().last();
562 slice = m_pieSeries->slices().last();
560 slice->setLabel("Dalej");
563 slice->setLabel("Dalej");
561 slice->setValue(222);
564 slice->setValue(222);
562 }
565 }
563
566
564 if (m_pieSeries->count() > 0) {
567 if (m_pieSeries->count() > 0) {
565 slice = m_pieSeries->slices().first();
568 slice = m_pieSeries->slices().first();
566 slice->setLabel("Prawie");
569 slice->setLabel("Prawie");
567 slice->setValue(111);
570 slice->setValue(111);
568 }
571 }
569 }
572 }
570
573
571 void TableWidget::testXY()
574 void TableWidget::testXY()
572 {
575 {
576 if (m_barMapper)
577 m_barMapper->setLastBarSetColumn(m_barMapper->lastBarSetColumn() + 1);
573 // if (m_series->type() != QAbstractSeries::SeriesTypeLine) {
578 // if (m_series->type() != QAbstractSeries::SeriesTypeLine) {
574 // m_series->append(QPointF(150, 75));
579 // m_series->append(QPointF(150, 75));
575 // }
580 // }
576
581
577 if (m_series->count() > 0) {
582 // if (m_series->count() > 0) {
578 m_series->remove(m_series->points().last());
583 // m_series->remove(m_series->points().last());
579 }
584 // }
580 }
585 }
581
586
582 TableWidget::~TableWidget()
587 TableWidget::~TableWidget()
583 {
588 {
584
589
585 }
590 }
@@ -1,83 +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 #ifndef TABLEWIDGET_H
21 #ifndef TABLEWIDGET_H
22 #define TABLEWIDGET_H
22 #define TABLEWIDGET_H
23
23
24 #include <QtGui/QWidget>
24 #include <QtGui/QWidget>
25 //#include <QChartGlobal>
25 //#include <QChartGlobal>
26 #include "qchartview.h"
26 #include "qchartview.h"
27 //#include "qxyseries.h"
27 //#include "qxyseries.h"
28 #include <QPieSeries>
28 #include <QPieSeries>
29 #include <QVPieModelMapper>
29 #include <QVPieModelMapper>
30 #include <QVXYModelMapper>
30 #include <QVXYModelMapper>
31 #include <QHXYModelMapper>
31 #include <QHXYModelMapper>
32 #include <QGroupedBarSeries>
33 #include <QVBarModelMapper>
32
34
33 class CustomTableModel;
35 class CustomTableModel;
34 class QTableView;
36 class QTableView;
35 class QRadioButton;
37 class QRadioButton;
36 class QSpinBox;
38 class QSpinBox;
37
39
38 QTCOMMERCIALCHART_USE_NAMESPACE
40 QTCOMMERCIALCHART_USE_NAMESPACE
39
41
40 class TableWidget : public QWidget
42 class TableWidget : public QWidget
41 {
43 {
42 Q_OBJECT
44 Q_OBJECT
43
45
44 public:
46 public:
45 TableWidget(QWidget *parent = 0);
47 TableWidget(QWidget *parent = 0);
46 ~TableWidget();
48 ~TableWidget();
47
49
48
50
49 public slots:
51 public slots:
50 void addRowAbove();
52 void addRowAbove();
51 void addRowBelow();
53 void addRowBelow();
52 void removeRow();
54 void removeRow();
53 void addColumnRight();
55 void addColumnRight();
54 void removeColumn();
56 void removeColumn();
55 void updateChartType(bool toggle);
57 void updateChartType(bool toggle);
56 void testPie();
58 void testPie();
57 void testPie2();
59 void testPie2();
58 void testPie3();
60 void testPie3();
59
61
60 void testXY();
62 void testXY();
61
63
62 private:
64 private:
63 QChartView* m_chartView;
65 QChartView* m_chartView;
64 QChart* m_chart;
66 QChart* m_chart;
65 QXYSeries* m_series;
67 QXYSeries* m_series;
66 QVXYModelMapper *m_mapper;
68 QVXYModelMapper *m_mapper;
67 CustomTableModel* m_model;
69 CustomTableModel* m_model;
68 QTableView* m_tableView;
70 QTableView* m_tableView;
71
69 QRadioButton* m_lineRadioButton;
72 QRadioButton* m_lineRadioButton;
70 QRadioButton* m_splineRadioButton;
73 QRadioButton* m_splineRadioButton;
71 QRadioButton* m_scatterRadioButton;
74 QRadioButton* m_scatterRadioButton;
72 QRadioButton* m_pieRadioButton;
75 QRadioButton* m_pieRadioButton;
73 QRadioButton* m_areaRadioButton;
76 QRadioButton* m_areaRadioButton;
74 QRadioButton* m_barRadioButton;
77 QRadioButton* m_barRadioButton;
75 QSpinBox* m_linesCountSpinBox;
78 QSpinBox* m_linesCountSpinBox;
79
76 QVPieModelMapper *m_pieMapper;
80 QVPieModelMapper *m_pieMapper;
77 QPieSeries* m_pieSeries;
81 QPieSeries* m_pieSeries;
78 QVPieModelMapper *m_pieMapper2;
82 QVPieModelMapper *m_pieMapper2;
79 QPieSeries* m_pieSeries2;
83 QPieSeries* m_pieSeries2;
84
85 QGroupedBarSeries* m_barSeries;
86 QVBarModelMapper* m_barMapper;
80 // QPieSeries* specialPie;
87 // QPieSeries* specialPie;
81 };
88 };
82
89
83 #endif // TABLEWIDGET_H
90 #endif // TABLEWIDGET_H
General Comments 0
You need to be logged in to leave comments. Login now