##// END OF EJS Templates
barchart pimpl part 1
sauimone -
r934:284eb2f28726
parent child
Show More
@@ -0,0 +1,96
1 #ifndef QBARSERIESPRIVATE_P_H
2 #define QBARSERIESPRIVATE_P_H
3
4 #include "qbarseries.h"
5 #include <QStringList>
6 #include <QSeries>
7
8 class QModelIndex;
9
10 QTCOMMERCIALCHART_BEGIN_NAMESPACE
11
12 typedef QStringList QBarCategories;
13
14 class QBarSeries;
15 class QBarSet;
16 class BarChartModel;
17 class BarCategory;
18
19 // Container for series
20 class QBarSeriesPrivate : public QObject
21 {
22 Q_OBJECT
23 Q_DECLARE_PUBLIC(QBarSeries)
24 public:
25 QBarSeriesPrivate(QStringList categories, QBarSeries *parent);
26
27 virtual QSeries::QSeriesType type() const { return QSeries::SeriesTypeBar; }
28
29 void appendBarSet(QBarSet *set); // Takes ownership of set
30 void removeBarSet(QBarSet *set); // Releases ownership, doesn't delete set
31 void appendBarSets(QList<QBarSet* > sets);
32 void removeBarSets(QList<QBarSet* > sets);
33 void insertBarSet(int i, QBarSet *set);
34 void insertCategory(int i, QString category);
35 void removeCategory(int i);
36 int barsetCount() const;
37 int categoryCount() const;
38 QList<QBarSet*> barSets() const;
39 QBarCategories categories() const;
40
41 void setLabelsVisible(bool visible = true);
42
43 bool setModel(QAbstractItemModel *model);
44 void setModelMapping(int categories, int bottomBoundry, int topBoundry, Qt::Orientation orientation = Qt::Vertical);
45
46 QBarSet* barsetAt(int index);
47 QString categoryName(int category);
48 qreal min();
49 qreal max();
50 qreal valueAt(int set, int category);
51 qreal percentageAt(int set, int category);
52 qreal categorySum(int category);
53 qreal absoluteCategorySum(int category);
54 qreal maxCategorySum();
55 BarChartModel& modelInternal();
56
57 static QBarSeriesPrivate &data(QBarSeries *barseries)
58 {
59 Q_ASSERT(barseries);
60 return *barseries->d_ptr;
61 }
62
63 Q_SIGNALS:
64 void clicked(QBarSet *barset, QString category, Qt::MouseButtons button); // Up to user of api, what to do with these signals
65 void selected();
66 void updatedBars();
67 void restructuredBars();
68 void showToolTip(QPoint pos, QString tip);
69
70 public Q_SLOTS:
71 void setToolTipEnabled(bool enabled = true); // enables tooltips
72 void barsetClicked(QString category, Qt::MouseButtons button);
73
74 private Q_SLOTS:
75 // slots for updating bars when data in model changes
76 void modelUpdated(QModelIndex topLeft, QModelIndex bottomRight);
77 void modelDataAdded(QModelIndex parent, int start, int end);
78 void modelDataRemoved(QModelIndex parent, int start, int end);
79 void barsetChanged();
80
81 protected:
82 QBarSeries * const q_ptr;
83
84 BarChartModel *m_internalModel; // TODO: this may change... current "2 models" situation doesn't look good.
85 QAbstractItemModel* m_model;
86 int m_mapCategories;
87 int m_mapBarBottom;
88 int m_mapBarTop;
89 int m_mapFirst;
90 int m_mapCount;
91 Qt::Orientation m_mapOrientation;
92 };
93
94 QTCOMMERCIALCHART_END_NAMESPACE
95
96 #endif // QBARSERIESPRIVATE_P_H
@@ -0,0 +1,80
1 #ifndef QBARSETPRIVATE_P_H
2 #define QBARSETPRIVATE_P_H
3
4 #include "qbarset.h"
5
6 QTCOMMERCIALCHART_BEGIN_NAMESPACE
7
8 class QBarSetPrivate : public QObject
9 {
10 Q_OBJECT
11 Q_DECLARE_PUBLIC(QBarSet)
12
13 public:
14 QBarSetPrivate(QString name, QBarSet *parent);
15 ~QBarSetPrivate();
16
17 void setName(QString name);
18 QString name() const;
19 QBarSetPrivate& operator << (const qreal &value); // appends new value to set
20 void insertValue(int i, qreal value);
21 void removeValue(int i);
22 int count() const;
23 qreal valueAt(int index) const;
24 void setValue(int index, qreal value);
25 qreal sum() const;
26
27 void setPen(const QPen &pen);
28 QPen pen() const;
29
30 void setBrush(const QBrush &brush);
31 QBrush brush() const;
32
33 void setLabelPen(const QPen &pen);
34 QPen labelPen() const;
35
36 void setLabelBrush(const QBrush &brush);
37 QBrush labelBrush() const;
38
39 void setLabelFont(const QFont &font);
40 QFont labelFont() const;
41
42 void setLabelsVisible(bool visible = true);
43 bool labelsVisible() const;
44
45 static QBarSetPrivate &pimpl(QBarSet *barset)
46 {
47 Q_ASSERT(barset);
48 return *barset->d_ptr;
49 }
50
51 Q_SIGNALS:
52 void clicked(QString category, Qt::MouseButtons button);
53 void structureChanged();
54 void valueChanged();
55 void hoverEnter(QPoint pos);
56 void hoverLeave();
57 void showToolTip(QPoint pos, QString tip);
58 void labelsVisibleChanged(bool visible);
59
60 public Q_SLOTS:
61 void barHoverEnterEvent(QPoint pos);
62 void barHoverLeaveEvent();
63
64 public:
65 QBarSet * const q_ptr;
66
67 QString m_name;
68 QList<qreal> m_values; // TODO: replace with map (category, value)
69 QMap<QString, qreal> m_mappedValues;
70 QPen m_pen;
71 QBrush m_brush;
72 QPen m_labelPen;
73 QBrush m_labelBrush;
74 QFont m_labelFont;
75 bool m_labelsVisible;
76 };
77
78 QTCOMMERCIALCHART_END_NAMESPACE
79
80 #endif // QBARSETPRIVATE_P_H
@@ -1,117 +1,117
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #include <QtGui/QApplication>
22 22 #include <QMainWindow>
23 23 #include <QChartView>
24 24 #include <QBarSet>
25 25 #include <QLegend>
26 26 #include "drilldownseries.h"
27 27 #include "drilldownchart.h"
28 28
29 29 QTCOMMERCIALCHART_USE_NAMESPACE
30 30
31 31 int main(int argc, char *argv[])
32 32 {
33 33 //! [1]
34 34 QApplication a(argc, argv);
35 35 QMainWindow window;
36 36 //! [1]
37 37
38 38 //! [2]
39 39 DrilldownChart* drilldownChart = new DrilldownChart();
40 40 drilldownChart->setTheme(QChart::ChartThemeBlueIcy);
41 41 drilldownChart->setAnimationOptions(QChart::SeriesAnimations);
42 42 //! [2]
43 43
44 44 //! [3]
45 45 // Define categories
46 46 QStringList months;
47 47 months << "May" << "Jun" << "Jul" << "Aug" << "Sep";
48 48 QStringList weeks;
49 49 weeks << "week 1" << "week 2" << "week 3" << "week 4";
50 50 QStringList plants;
51 51 plants << "Habanero" << "Lemon Drop" << "Starfish" << "Aji Amarillo";
52 52 //! [3]
53 53
54 54 //! [4]
55 55 // Create drilldown structure
56 56 DrilldownBarSeries* seasonSeries = new DrilldownBarSeries(months, drilldownChart);
57 57 seasonSeries->setName("Crop by month - Season");
58 58
59 59 // Each month in season series has drilldown series for weekly data
60 60 foreach (QString month, months) {
61 61
62 62 // Create drilldown series for every week
63 63 DrilldownBarSeries* weeklySeries = new DrilldownBarSeries(weeks, drilldownChart);
64 64 seasonSeries->mapDrilldownSeries(month, weeklySeries);
65 65
66 66 // Drilling down from weekly data brings us back to season data.
67 67 foreach (QString week, weeks) {
68 68 weeklySeries->mapDrilldownSeries(week, seasonSeries);
69 69 weeklySeries->setName(QString("Crop by week - " + month));
70 70 }
71 71
72 72 // Use right click signal to implement drilldown
73 73 QObject::connect(weeklySeries, SIGNAL(clicked(QBarSet*,QString,Qt::MouseButtons)), drilldownChart, SLOT(handleClicked(QBarSet*,QString,Qt::MouseButtons)));
74 74 }
75 75
76 76 // Enable drilldown from season series using right click.
77 77 QObject::connect(seasonSeries, SIGNAL(clicked(QBarSet*,QString,Qt::MouseButtons)), drilldownChart, SLOT(handleClicked(QBarSet*,QString,Qt::MouseButtons)));
78 78 //! [4]
79 79
80 80 //! [5]
81 81 // Fill monthly and weekly series with data
82 82 foreach (QString plant, plants) {
83 83 QBarSet* monthlyCrop = new QBarSet(plant);
84 84 foreach (QString month, months) {
85 85 QBarSet* weeklyCrop = new QBarSet(plant);
86 86 foreach (QString week, weeks )
87 87 *weeklyCrop << (qrand() % 20);
88 88 // Get the drilldown series from season series and add crop to it.
89 89 seasonSeries->drilldownSeries(month)->appendBarSet(weeklyCrop);
90 90 seasonSeries->drilldownSeries(month)->setToolTipEnabled(true);
91 *monthlyCrop << weeklyCrop->total();
91 *monthlyCrop << weeklyCrop->sum();
92 92 }
93 93 seasonSeries->appendBarSet(monthlyCrop);
94 94 }
95 95 //! [5]
96 96
97 97 //! [6]
98 98 // Show season series in initial view
99 99 drilldownChart->changeSeries(seasonSeries);
100 100 drilldownChart->setTitle(seasonSeries->name());
101 101 //! [6]
102 102
103 103 //! [7]
104 104 drilldownChart->axisX()->setGridLineVisible(false);
105 105 drilldownChart->axisY()->setNiceNumbers(true);
106 106 drilldownChart->legend()->setVisible(true);
107 107 drilldownChart->legend()->setAlignment(QLegend::AlignmentBottom);
108 108 //! [7]
109 109
110 110 QChartView *chartView = new QChartView(drilldownChart);
111 111 window.setCentralWidget(chartView);
112 112 window.resize(400, 300);
113 113 window.show();
114 114
115 115 return a.exec();
116 116 }
117 117
@@ -1,29 +1,31
1 1 INCLUDEPATH += $$PWD
2 2 DEPENDPATH += $$PWD
3 3
4 4 SOURCES += \
5 5 $$PWD/bar.cpp \
6 6 $$PWD/barchartmodel.cpp \
7 7 $$PWD/barchartitem.cpp \
8 8 $$PWD/percentbarchartitem.cpp \
9 9 $$PWD/qbarseries.cpp \
10 10 $$PWD/qbarset.cpp \
11 11 $$PWD/qpercentbarseries.cpp \
12 12 $$PWD/qstackedbarseries.cpp \
13 13 $$PWD/stackedbarchartitem.cpp \
14 14 $$PWD/barlabel.cpp
15 15
16 16 PRIVATE_HEADERS += \
17 17 $$PWD/bar_p.h \
18 18 $$PWD/barchartmodel_p.h \
19 19 $$PWD/barchartitem_p.h \
20 20 $$PWD/percentbarchartitem_p.h \
21 21 $$PWD/stackedbarchartitem_p.h \
22 $$PWD/barlabel_p.h
22 $$PWD/barlabel_p.h \
23 $$PWD/qbarsetprivate_p.h \
24 $$PWD/qbarseriesprivate_p.h
23 25
24 26 PUBLIC_HEADERS += \
25 27 $$PWD/qbarseries.h \
26 28 $$PWD/qbarset.h \
27 29 $$PWD/qpercentbarseries.h \
28 30 $$PWD/qstackedbarseries.h
29 31
@@ -1,472 +1,874
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #include <QDebug>
22 22 #include "qbarseries.h"
23 23 #include "qbarset.h"
24 24 #include "barchartmodel_p.h"
25 #include "qbarseriesprivate_p.h"
26 #include "qbarsetprivate_p.h"
25 27 #include <QAbstractItemModel>
26 28 #include <QModelIndex>
27 29
28 30 QTCOMMERCIALCHART_BEGIN_NAMESPACE
29 31
32 QBarSeriesPrivate::QBarSeriesPrivate(QBarCategories categories, QBarSeries *parent) : QObject(parent),
33 q_ptr(parent),
34 m_internalModel(new BarChartModel(categories,this))
35 {
36 m_model = 0;
37 m_mapCategories = -1;
38 m_mapBarBottom = -1;
39 m_mapBarTop = -1;
40 m_mapFirst = 0;
41 m_mapCount = 0;
42 m_mapOrientation = Qt::Vertical;
43 }
44
45 void QBarSeriesPrivate::appendBarSet(QBarSet *set)
46 {
47 m_internalModel->appendBarSet(set);
48 // connect(set, SIGNAL(clicked(QString,Qt::MouseButtons)), this, SLOT(barsetClicked(QString,Qt::MouseButtons)));
49 // connect(set, SIGNAL(valueChanged()), this, SLOT(barsetChanged()));
50 emit restructuredBars();
51 }
52
53 void QBarSeriesPrivate::removeBarSet(QBarSet *set)
54 {
55 // disconnect(set, SIGNAL(clicked(QString,Qt::MouseButtons)), this, SLOT(barsetClicked(QString,Qt::MouseButtons)));
56 m_internalModel->removeBarSet(set);
57 emit restructuredBars();
58 }
59 void QBarSeriesPrivate::appendBarSets(QList<QBarSet* > sets)
60 {
61 foreach (QBarSet* barset, sets) {
62 m_internalModel->appendBarSet(barset);
63 connect(barset, SIGNAL(clicked(QString,Qt::MouseButtons)), this, SLOT(barsetClicked(QString,Qt::MouseButtons)));
64 connect(barset, SIGNAL(valueChanged()), this, SLOT(barsetChanged()));
65 }
66 emit restructuredBars();
67 }
68
69 void QBarSeriesPrivate::removeBarSets(QList<QBarSet* > sets)
70 {
71 foreach (QBarSet* barset, sets) {
72 disconnect(barset, SIGNAL(clicked(QString,Qt::MouseButtons)), this, SLOT(barsetClicked(QString,Qt::MouseButtons)));
73 m_internalModel->removeBarSet(barset);
74 }
75 emit restructuredBars();
76 }
77
78 void QBarSeriesPrivate::insertBarSet(int i, QBarSet *set)
79 {
80 m_internalModel->insertBarSet(i, set);
81 // emit barsetChanged();
82 }
83
84 void QBarSeriesPrivate::insertCategory(int i, QString category)
85 {
86 m_internalModel->insertCategory(i, category);
87 }
88
89 void QBarSeriesPrivate::removeCategory(int i)
90 {
91 m_internalModel->removeCategory(i);
92 }
93
94 int QBarSeriesPrivate::barsetCount() const
95 {
96 // if(m_model)
97 // return m_mapBarTop - m_mapBarBottom;
98 // else
99 return m_internalModel->barsetCount();
100 }
101
102 int QBarSeriesPrivate::categoryCount() const
103 {
104 return m_internalModel->categoryCount();
105 }
106
107 QList<QBarSet*> QBarSeriesPrivate::barSets() const
108 {
109 return m_internalModel->barSets();
110 }
111
112 QBarSet* QBarSeriesPrivate::barsetAt(int index)
113 {
114 return m_internalModel->barsetAt(index);
115 }
116
117 QString QBarSeriesPrivate::categoryName(int category)
118 {
119 return m_internalModel->categoryName(category);
120 }
121
122 void QBarSeriesPrivate::setToolTipEnabled(bool enabled)
123 {
124 // TODO: what if we add sets after call to this function? Those sets won't have tooltip enabled.
125 if (enabled) {
126 for (int i=0; i<m_internalModel->barsetCount(); i++) {
127 QBarSet *set = m_internalModel->barsetAt(i);
128 connect(set, SIGNAL(showToolTip(QPoint,QString)), this, SIGNAL(showToolTip(QPoint,QString)));
129 }
130 } else {
131 for (int i=0; i<m_internalModel->barsetCount(); i++) {
132 QBarSet *set = m_internalModel->barsetAt(i);
133 disconnect(set, SIGNAL(showToolTip(QPoint,QString)), this, SIGNAL(showToolTip(QPoint,QString)));
134 }
135 }
136 }
137
138 void QBarSeriesPrivate::barsetClicked(QString category, Qt::MouseButtons button)
139 {
140 emit clicked(qobject_cast<QBarSet*>(sender()), category, button);
141 }
142
143 qreal QBarSeriesPrivate::min()
144 {
145 return m_internalModel->min();
146 }
147
148 qreal QBarSeriesPrivate::max()
149 {
150 return m_internalModel->max();
151 }
152
153 qreal QBarSeriesPrivate::valueAt(int set, int category)
154 {
155 return m_internalModel->valueAt(set, category);
156 }
157
158 qreal QBarSeriesPrivate::percentageAt(int set, int category)
159 {
160 return m_internalModel->percentageAt(set, category);
161 }
162
163 qreal QBarSeriesPrivate::categorySum(int category)
164 {
165 return m_internalModel->categorySum(category);
166 }
167
168 qreal QBarSeriesPrivate::absoluteCategorySum(int category)
169 {
170 return m_internalModel->absoluteCategorySum(category);
171 }
172
173 qreal QBarSeriesPrivate::maxCategorySum()
174 {
175 return m_internalModel->maxCategorySum();
176 }
177
178 BarChartModel& QBarSeriesPrivate::modelInternal()
179 {
180 return *m_internalModel;
181 }
182
183 bool QBarSeriesPrivate::setModel(QAbstractItemModel *model)
184 {
185 // disconnect signals from old model
186 if(m_model)
187 {
188 disconnect(m_model, 0, this, 0);
189 m_mapCategories = -1;
190 m_mapBarBottom = -1;
191 m_mapBarTop = -1;
192 m_mapFirst = 0;
193 m_mapCount = 0;
194 m_mapOrientation = Qt::Vertical;
195 }
196
197 // set new model
198 if(model)
199 {
200 m_model = model;
201 return true;
202 }
203 else
204 {
205 m_model = 0;
206 return false;
207 }
208 }
209
210 void QBarSeriesPrivate::setModelMapping(int categories, int bottomBoundry, int topBoundry, Qt::Orientation orientation)
211 {
212 if (!m_model)
213 return;
214
215 m_mapCategories = categories;
216 m_mapBarBottom = bottomBoundry;
217 m_mapBarTop = topBoundry;
218 // m_mapFirst = 1;
219 m_mapOrientation = orientation;
220
221 // connect the signals
222 if (m_mapOrientation == Qt::Vertical) {
223 m_mapCount = m_model->rowCount() - m_mapFirst;
224 connect(m_model,SIGNAL(dataChanged(QModelIndex,QModelIndex)),
225 this, SLOT(modelUpdated(QModelIndex, QModelIndex)));
226 connect(m_model,SIGNAL(rowsInserted(QModelIndex, int, int)),
227 this, SLOT(modelDataAdded(QModelIndex,int,int)));
228 connect(m_model, SIGNAL(rowsRemoved(QModelIndex, int, int)),
229 this, SLOT(modelDataRemoved(QModelIndex,int,int)));
230 } else {
231 m_mapCount = m_model->columnCount() - m_mapFirst;
232 connect(m_model,SIGNAL(dataChanged(QModelIndex,QModelIndex)),
233 this, SLOT(modelUpdated(QModelIndex, QModelIndex)));
234 connect(m_model,SIGNAL(columnsInserted(QModelIndex, int, int)),
235 this, SLOT(modelDataAdded(QModelIndex,int,int)));
236 connect(m_model, SIGNAL(columnsRemoved(QModelIndex, int, int)),
237 this, SLOT(modelDataRemoved(QModelIndex,int,int)));
238 }
239
240 // create the initial bars
241 delete m_internalModel;
242 if (m_mapOrientation == Qt::Vertical) {
243 QStringList categories;
244 for (int k = m_mapFirst; k < m_mapFirst + m_mapCount; k++)
245 categories << m_model->data(m_model->index(k, m_mapCategories), Qt::DisplayRole).toString();
246 m_internalModel = new BarChartModel(categories, this);
247
248 for (int i = m_mapBarBottom; i <= m_mapBarTop; i++) {
249 QBarSet* barSet = new QBarSet(QString("Column: %1").arg(i + 1));
250 for(int m = m_mapFirst; m < m_mapFirst + m_mapCount; m++)
251 *barSet << m_model->data(m_model->index(m, i), Qt::DisplayRole).toDouble();
252 appendBarSet(barSet);
253 }
254 } else {
255 QStringList categories;
256 for (int k = m_mapFirst; k < m_mapFirst + m_mapCount; k++)
257 categories << m_model->data(m_model->index(m_mapCategories, k), Qt::DisplayRole).toString();
258 m_internalModel = new BarChartModel(categories, this);
259
260 for (int i = m_mapBarBottom; i <= m_mapBarTop; i++) {
261 QBarSet* barSet = new QBarSet(QString("Row: %1").arg(i + 1));
262 for(int m = m_mapFirst; m < m_mapFirst + m_mapCount; m++)
263 *barSet << m_model->data(m_model->index(i, m), Qt::DisplayRole).toDouble();
264 appendBarSet(barSet);
265 }
266 }
267 }
268
269 void QBarSeriesPrivate::modelUpdated(QModelIndex topLeft, QModelIndex bottomRight)
270 {
271 Q_UNUSED(bottomRight)
272
273 if (m_mapOrientation == Qt::Vertical)
274 {
275 // model update is relevant to BarSeries if the change was made to the part of the model that was mapped to BarSeries
276 if (topLeft.column() >= m_mapBarBottom && topLeft.column() <= m_mapBarTop && topLeft.row() >= m_mapFirst && topLeft.row() < m_mapFirst + m_mapCount)
277 barsetAt(topLeft.column() - m_mapBarBottom)->setValue(topLeft.row() - m_mapFirst, m_model->data(topLeft, Qt::DisplayRole).toDouble());
278 }
279 else
280 {
281 // model update is relevant to BarSeries if the change was made to the part of the model that was mapped to BarSeries
282 if (topLeft.row() >= m_mapBarBottom && topLeft.row() <= m_mapBarTop && topLeft.column() >= m_mapFirst && topLeft.column() < m_mapFirst + m_mapCount)
283 barsetAt(topLeft.row() - m_mapBarBottom)->setValue(topLeft.column() - m_mapFirst, m_model->data(topLeft, Qt::DisplayRole).toDouble());
284 }
285 }
286
287 void QBarSeriesPrivate::modelDataAdded(QModelIndex /*parent*/, int start, int /*end*/)
288 {
289 if (m_mapOrientation == Qt::Vertical) {
290 insertCategory(start - m_mapFirst, QString("Row: %1").arg(start + 1));
291 for (int i = 0; i <= m_mapBarTop - m_mapBarBottom; i++) {
292 barsetAt(i)->insertValue(start - m_mapFirst, m_model->data(m_model->index(start, i), Qt::DisplayRole).toDouble());
293 }
294 } else {
295 insertCategory(start - m_mapFirst, QString("Column: %1").arg(start + 1));
296 for (int i = 0; i <= m_mapBarTop - m_mapBarBottom; i++) {
297 barsetAt(i)->insertValue(start - m_mapFirst, m_model->data(m_model->index(i, start), Qt::DisplayRole).toDouble());
298 }
299 }
300 emit restructuredBars();
301 }
302
303 void QBarSeriesPrivate::modelDataRemoved(QModelIndex parent, int start, int end)
304 {
305 Q_UNUSED(parent)
306 Q_UNUSED(end)
307
308 removeCategory(start - m_mapFirst);
309 for (int i = 0; i <= m_mapBarTop - m_mapBarBottom; i++)
310 {
311 barsetAt(i)->removeValue(start - m_mapFirst);
312 }
313 emit restructuredBars();
314 }
315
316 void QBarSeriesPrivate::barsetChanged()
317 {
318 emit updatedBars();
319 }
320
321 QBarCategories QBarSeriesPrivate::categories() const
322 {
323 QBarCategories categories;
324 int count = m_internalModel->categoryCount();
325 for (int i=1; i <= count; i++) {
326 categories.insert(i, m_internalModel->categoryName(i - 1));
327 }
328 return categories;
329 }
330
331 void QBarSeriesPrivate::setLabelsVisible(bool visible)
332 {
333 foreach (QBarSet* s, barSets()) {
334 s->setLabelsVisible(visible);
335 }
336 }
337
338
30 339 /*!
31 340 \class QBarSeries
32 341 \brief part of QtCommercial chart API.
33 342
34 343 QBarSeries represents a series of data shown as bars. One QBarSeries can contain multible
35 344 QBarSet data sets. QBarSeries groups the data from sets to categories, which are defined
36 345 by QStringList.
37 346
38 347 \mainclass
39 348
40 349 \sa QBarSet, QStackedBarSeries, QPercentBarSeries
41 350 */
42 351
43 352 /*!
44 353 \fn virtual QSeriesType QBarSeries::type() const
45 354 \brief Returns type of series.
46 355 \sa QSeries, QSeriesType
47 356 */
48 357
49 358 /*!
50 359 \fn void QBarSeries::showToolTip(QPoint pos, QString tip)
51 360 \brief \internal \a pos \a tip
52 361 */
53 362
54 363 /*!
55 364 Constructs empty QBarSeries. Parameter \a categories defines the categories for chart.
56 365 QBarSeries is QObject which is a child of a \a parent.
57 366 */
58 367 QBarSeries::QBarSeries(QBarCategories categories, QObject *parent) : QSeries(parent),
59 m_internalModel(new BarChartModel(categories, this))
368 d_ptr(new QBarSeriesPrivate(categories, this))
369 // m_internalModel(new BarChartModel(categories, this))
60 370 {
61 m_model = 0;
62 m_mapCategories = -1;
63 m_mapBarBottom = -1;
64 m_mapBarTop = -1;
65 m_mapFirst = 0;
66 m_mapCount = 0;
67 m_mapOrientation = Qt::Vertical;
371 // m_model = 0;
372 // m_mapCategories = -1;
373 // m_mapBarBottom = -1;
374 // m_mapBarTop = -1;
375 // m_mapFirst = 0;
376 // m_mapCount = 0;
377 // m_mapOrientation = Qt::Vertical;
68 378 }
69 379
70 380 /*!
71 381 Adds a set of bars to series. Takes ownership of \a set.
72 382 Connects the clicked(QString, Qt::MouseButtons) signal
73 383 of \a set to this series
74 384 */
75 385 void QBarSeries::appendBarSet(QBarSet *set)
76 386 {
387 Q_D(QBarSeries);
388 connect(&QBarSetPrivate::pimpl(set), SIGNAL(clicked(QString,Qt::MouseButtons)), d_ptr, SLOT(barsetClicked(QString,Qt::MouseButtons)));
389 connect(&QBarSetPrivate::pimpl(set), SIGNAL(valueChanged()), d_ptr, SLOT(barsetChanged()));
390 d->appendBarSet(set);
391 /*
77 392 m_internalModel->appendBarSet(set);
78 393 connect(set, SIGNAL(clicked(QString,Qt::MouseButtons)), this, SLOT(barsetClicked(QString,Qt::MouseButtons)));
79 394 connect(set, SIGNAL(valueChanged()), this, SLOT(barsetChanged()));
80 395 emit restructuredBars();
396 */
81 397 }
82 398
83 399 /*!
84 400 Removes a set of bars from series. Releases ownership of \a set. Doesnt delete \a set.
85 401 Disconnects the clicked(QString, Qt::MouseButtons) signal
86 402 of \a set from this series
87 403 */
88 404 void QBarSeries::removeBarSet(QBarSet *set)
89 405 {
406 Q_D(QBarSeries);
407 disconnect(&QBarSetPrivate::pimpl(set), SIGNAL(clicked(QString,Qt::MouseButtons)), d_ptr, SLOT(barsetClicked(QString,Qt::MouseButtons)));
408 d->removeBarSet(set);
409 /*
90 410 disconnect(set, SIGNAL(clicked(QString,Qt::MouseButtons)), this, SLOT(barsetClicked(QString,Qt::MouseButtons)));
91 411 m_internalModel->removeBarSet(set);
92 412 emit restructuredBars();
413 */
93 414 }
94 415
95 416 /*!
96 417 Adds a list of barsets to series. Takes ownership of \a sets.
97 418 Connects the clicked(QString, Qt::MouseButtons) signals
98 419 of \a sets to this series
99 420 */
100 421 void QBarSeries::appendBarSets(QList<QBarSet* > sets)
101 422 {
423 Q_D(QBarSeries);
424 d->appendBarSets(sets);
425 /*
102 426 foreach (QBarSet* barset, sets) {
103 427 m_internalModel->appendBarSet(barset);
104 428 connect(barset, SIGNAL(clicked(QString,Qt::MouseButtons)), this, SLOT(barsetClicked(QString,Qt::MouseButtons)));
105 429 connect(barset, SIGNAL(valueChanged()), this, SLOT(barsetChanged()));
106 430 }
107 431 emit restructuredBars();
432 */
108 433 }
109 434
110 435 /*!
111 436 Removes a list of barsets from series. Releases ownership of \a sets. Doesnt delete \a sets.
112 437 Disconnects the clicked(QString, Qt::MouseButtons) signal
113 438 of \a sets from this series
114 439 */
115 440 void QBarSeries::removeBarSets(QList<QBarSet* > sets)
116 441 {
442 Q_D(QBarSeries);
443 d->removeBarSets(sets);
444 /*
117 445 foreach (QBarSet* barset, sets) {
118 446 disconnect(barset, SIGNAL(clicked(QString,Qt::MouseButtons)), this, SLOT(barsetClicked(QString,Qt::MouseButtons)));
119 447 m_internalModel->removeBarSet(barset);
120 448 }
121 449 emit restructuredBars();
450 */
122 451 }
123 452
124 453 /*!
125 454 Inserts new \a set on the \a i position.
126 455 The barset that is currently at this postion is moved to postion i + 1
127 456 */
128 457 void QBarSeries::insertBarSet(int i, QBarSet *set)
129 458 {
459 Q_D(QBarSeries);
460 d->insertBarSet(i,set);
461 /*
130 462 m_internalModel->insertBarSet(i, set);
131 463 // emit barsetChanged();
464 */
132 465 }
133 466
134 467 /*!
135 468 Inserts new \a category on the \a i position.
136 469 The category that is currently at this postion is moved to postion i + 1
137 470 \sa removeCategory()
138 471 */
139 472 void QBarSeries::insertCategory(int i, QString category)
140 473 {
141 m_internalModel->insertCategory(i, category);
474 Q_D(QBarSeries);
475 d->insertCategory(i,category);
476 //m_internalModel->insertCategory(i, category);
142 477 }
143 478
144 479 /*!
145 480 Removes the category specified by \a i
146 481 \sa insertCategory()
147 482 */
148 483 void QBarSeries::removeCategory(int i)
149 484 {
150 m_internalModel->removeCategory(i);
485 Q_D(QBarSeries);
486 d->removeCategory(i);
487 //m_internalModel->removeCategory(i);
151 488 }
152 489
153 490 /*!
154 491 Returns number of sets in series.
155 492 */
156 493 int QBarSeries::barsetCount() const
157 494 {
495 Q_D(const QBarSeries);
496 return d->barsetCount();
497 /*
158 498 // if(m_model)
159 499 // return m_mapBarTop - m_mapBarBottom;
160 500 // else
161 501 return m_internalModel->barsetCount();
502 */
162 503 }
163 504
164 505 /*!
165 506 Returns number of categories in series
166 507 */
167 508 int QBarSeries::categoryCount() const
168 509 {
169 return m_internalModel->categoryCount();
510 Q_D(const QBarSeries);
511 return d->categoryCount();
512 // return m_internalModel->categoryCount();
170 513 }
171 514
172 515 /*!
173 516 Returns a list of sets in series. Keeps ownership of sets.
174 517 */
175 518 QList<QBarSet*> QBarSeries::barSets() const
176 519 {
177 return m_internalModel->barSets();
520 Q_D(const QBarSeries);
521 return d->barSets();
522 // return m_internalModel->barSets();
178 523 }
179 524
180 525 /*!
181 526 \internal \a index
182 527 */
183 528 QBarSet* QBarSeries::barsetAt(int index)
184 529 {
185 return m_internalModel->barsetAt(index);
530 Q_D(QBarSeries);
531 return d->barsetAt(index);
532 // return m_internalModel->barsetAt(index);
186 533 }
187 534
188 535 /*!
189 536 \internal \a category
190 537 */
191 538 QString QBarSeries::categoryName(int category)
192 539 {
193 return m_internalModel->categoryName(category);
540 Q_D(QBarSeries);
541 return d->categoryName(category);
542 // return m_internalModel->categoryName(category);
194 543 }
195 544
196 545 /*!
197 546 Enables or disables tooltip depending on parameter \a enabled.
198 547 Tooltip shows the name of set, when mouse is hovering on top of bar.
199 548 Calling without parameter \a enabled, enables the tooltip
200 549 */
201 550 void QBarSeries::setToolTipEnabled(bool enabled)
202 551 {
552 Q_D(QBarSeries);
553 d->setToolTipEnabled(enabled);
554 /*
203 555 // TODO: what if we add sets after call to this function? Those sets won't have tooltip enabled.
204 556 if (enabled) {
205 557 for (int i=0; i<m_internalModel->barsetCount(); i++) {
206 558 QBarSet *set = m_internalModel->barsetAt(i);
207 559 connect(set, SIGNAL(showToolTip(QPoint,QString)), this, SIGNAL(showToolTip(QPoint,QString)));
208 560 }
209 561 } else {
210 562 for (int i=0; i<m_internalModel->barsetCount(); i++) {
211 563 QBarSet *set = m_internalModel->barsetAt(i);
212 564 disconnect(set, SIGNAL(showToolTip(QPoint,QString)), this, SIGNAL(showToolTip(QPoint,QString)));
213 565 }
214 566 }
567 */
215 568 }
216 569
217 570
218 571 /*!
219 572 \internal \a category
220 573 */
221 574 void QBarSeries::barsetClicked(QString category, Qt::MouseButtons button)
222 575 {
223 emit clicked(qobject_cast<QBarSet*>(sender()), category, button);
576 Q_D(QBarSeries);
577 d->barsetClicked(category,button);
578 // emit clicked(qobject_cast<QBarSet*>(sender()), category, button);
224 579 }
225 580
226 581 /*!
227 582 \internal
228 583 */
229 584 qreal QBarSeries::min()
230 585 {
231 return m_internalModel->min();
586 Q_D(QBarSeries);
587 return d->min();
588 //return m_internalModel->min();
232 589 }
233 590
234 591 /*!
235 592 \internal
236 593 */
237 594 qreal QBarSeries::max()
238 595 {
239 return m_internalModel->max();
596 Q_D(QBarSeries);
597 return d->max();
598 // return m_internalModel->max();
240 599 }
241 600
242 601 /*!
243 602 \internal \a set \a category
244 603 */
245 604 qreal QBarSeries::valueAt(int set, int category)
246 605 {
247 return m_internalModel->valueAt(set, category);
606 Q_D(QBarSeries);
607 return d->valueAt(set,category);
608 // return m_internalModel->valueAt(set, category);
248 609 }
249 610
250 611 /*!
251 612 \internal \a set \a category
252 613 */
253 614 qreal QBarSeries::percentageAt(int set, int category)
254 615 {
255 return m_internalModel->percentageAt(set, category);
616 Q_D(QBarSeries);
617 return d->percentageAt(set,category);
618 // return m_internalModel->percentageAt(set, category);
256 619 }
257 620
258 621 /*!
259 622 \internal \a category
260 623 */
261 624 qreal QBarSeries::categorySum(int category)
262 625 {
263 return m_internalModel->categorySum(category);
626 Q_D(QBarSeries);
627 return d->categorySum(category);
628 // return m_internalModel->categorySum(category);
264 629 }
265 630
266 631 /*!
267 632 \internal \a category
268 633 */
269 634 qreal QBarSeries::absoluteCategorySum(int category)
270 635 {
271 return m_internalModel->absoluteCategorySum(category);
636 Q_D(QBarSeries);
637 return d->absoluteCategorySum(category);
638 // return m_internalModel->absoluteCategorySum(category);
272 639 }
273 640
274 641 /*!
275 642 \internal
276 643 */
277 644 qreal QBarSeries::maxCategorySum()
278 645 {
279 return m_internalModel->maxCategorySum();
646 Q_D(QBarSeries);
647 return d->maxCategorySum();
648 // return m_internalModel->maxCategorySum();
280 649 }
281 650
282 651 /*!
283 652 \internal
284 653 */
285 654 BarChartModel& QBarSeries::modelInternal()
286 655 {
287 return *m_internalModel;
656 Q_D(QBarSeries);
657 return d->modelInternal();
658 // return *m_internalModel;
288 659 }
289 660
290 661 /*!
291 662 \fn bool QBarSeries::setModel(QAbstractItemModel *model)
292 663 Sets the \a model to be used as a data source
293 664 */
294 665 bool QBarSeries::setModel(QAbstractItemModel *model)
295 666 {
667 Q_D(QBarSeries);
668 return d->setModel(model);
669 /*
296 670 // disconnect signals from old model
297 671 if(m_model)
298 672 {
299 673 disconnect(m_model, 0, this, 0);
300 674 m_mapCategories = -1;
301 675 m_mapBarBottom = -1;
302 676 m_mapBarTop = -1;
303 677 m_mapFirst = 0;
304 678 m_mapCount = 0;
305 679 m_mapOrientation = Qt::Vertical;
306 680 }
307 681
308 682 // set new model
309 683 if(model)
310 684 {
311 685 m_model = model;
312 686 return true;
313 687 }
314 688 else
315 689 {
316 690 m_model = 0;
317 691 return false;
318 692 }
693 */
319 694 }
320 695
321 696 /*!
322 697 \fn bool QBarSeries::setModelMapping(int categories, int bottomBoundry, int topBoundry, Qt::Orientation orientation)
323 698 Sets column/row specified by \a categories to be used as a list of bar series categories.
324 699 Parameter \a bottomBoundry indicates the column/row where the first bar set is located in the model.
325 700 Parameter \a topBoundry indicates the column/row where the last bar set is located in the model.
326 701 All the columns/rows inbetween those two values are also used as data for bar sets.
327 702 The \a orientation paramater specifies whether the data is in columns or in rows.
328 703 */
329 void QBarSeries::setModelMapping(int categories, int bottomBoundry, int topBoundry, Qt::Orientation orientation)
704 void QBarSeries::setModelMapping(int categories, int bottomBoundary, int topBoundary, Qt::Orientation orientation)
330 705 {
706 Q_D(QBarSeries);
707 d->setModelMapping(categories,bottomBoundary,topBoundary,orientation);
708 /*
331 709 if (!m_model)
332 710 return;
333 711
334 712 m_mapCategories = categories;
335 713 m_mapBarBottom = bottomBoundry;
336 714 m_mapBarTop = topBoundry;
337 715 // m_mapFirst = 1;
338 716 m_mapOrientation = orientation;
339 717
340 718 // connect the signals
341 719 if (m_mapOrientation == Qt::Vertical) {
342 720 m_mapCount = m_model->rowCount() - m_mapFirst;
343 721 connect(m_model,SIGNAL(dataChanged(QModelIndex,QModelIndex)),
344 722 this, SLOT(modelUpdated(QModelIndex, QModelIndex)));
345 723 connect(m_model,SIGNAL(rowsInserted(QModelIndex, int, int)),
346 724 this, SLOT(modelDataAdded(QModelIndex,int,int)));
347 725 connect(m_model, SIGNAL(rowsRemoved(QModelIndex, int, int)),
348 726 this, SLOT(modelDataRemoved(QModelIndex,int,int)));
349 727 } else {
350 728 m_mapCount = m_model->columnCount() - m_mapFirst;
351 729 connect(m_model,SIGNAL(dataChanged(QModelIndex,QModelIndex)),
352 730 this, SLOT(modelUpdated(QModelIndex, QModelIndex)));
353 731 connect(m_model,SIGNAL(columnsInserted(QModelIndex, int, int)),
354 732 this, SLOT(modelDataAdded(QModelIndex,int,int)));
355 733 connect(m_model, SIGNAL(columnsRemoved(QModelIndex, int, int)),
356 734 this, SLOT(modelDataRemoved(QModelIndex,int,int)));
357 735 }
358 736
359 737 // create the initial bars
360 738 delete m_internalModel;
361 739 if (m_mapOrientation == Qt::Vertical) {
362 740 QStringList categories;
363 741 for (int k = m_mapFirst; k < m_mapFirst + m_mapCount; k++)
364 742 categories << m_model->data(m_model->index(k, m_mapCategories), Qt::DisplayRole).toString();
365 743 m_internalModel = new BarChartModel(categories, this);
366 744
367 745 for (int i = m_mapBarBottom; i <= m_mapBarTop; i++) {
368 746 QBarSet* barSet = new QBarSet(QString("Column: %1").arg(i + 1));
369 747 for(int m = m_mapFirst; m < m_mapFirst + m_mapCount; m++)
370 748 *barSet << m_model->data(m_model->index(m, i), Qt::DisplayRole).toDouble();
371 749 appendBarSet(barSet);
372 750 }
373 751 } else {
374 752 QStringList categories;
375 753 for (int k = m_mapFirst; k < m_mapFirst + m_mapCount; k++)
376 754 categories << m_model->data(m_model->index(m_mapCategories, k), Qt::DisplayRole).toString();
377 755 m_internalModel = new BarChartModel(categories, this);
378 756
379 757 for (int i = m_mapBarBottom; i <= m_mapBarTop; i++) {
380 758 QBarSet* barSet = new QBarSet(QString("Row: %1").arg(i + 1));
381 759 for(int m = m_mapFirst; m < m_mapFirst + m_mapCount; m++)
382 760 *barSet << m_model->data(m_model->index(i, m), Qt::DisplayRole).toDouble();
383 761 appendBarSet(barSet);
384 762 }
385 763 }
764 */
386 765 }
387 766
388 767 /*!
389 768 \internal
390 769 */
391 770 void QBarSeries::modelUpdated(QModelIndex topLeft, QModelIndex bottomRight)
392 771 {
772 Q_D(QBarSeries);
773 d->modelUpdated(topLeft,bottomRight);
774 /*
393 775 Q_UNUSED(bottomRight)
394 776
395 777 if (m_mapOrientation == Qt::Vertical)
396 778 {
397 779 // model update is relevant to BarSeries if the change was made to the part of the model that was mapped to BarSeries
398 780 if (topLeft.column() >= m_mapBarBottom && topLeft.column() <= m_mapBarTop && topLeft.row() >= m_mapFirst && topLeft.row() < m_mapFirst + m_mapCount)
399 781 barsetAt(topLeft.column() - m_mapBarBottom)->setValue(topLeft.row() - m_mapFirst, m_model->data(topLeft, Qt::DisplayRole).toDouble());
400 782 }
401 783 else
402 784 {
403 785 // model update is relevant to BarSeries if the change was made to the part of the model that was mapped to BarSeries
404 786 if (topLeft.row() >= m_mapBarBottom && topLeft.row() <= m_mapBarTop && topLeft.column() >= m_mapFirst && topLeft.column() < m_mapFirst + m_mapCount)
405 787 barsetAt(topLeft.row() - m_mapBarBottom)->setValue(topLeft.column() - m_mapFirst, m_model->data(topLeft, Qt::DisplayRole).toDouble());
406 788 }
789 */
407 790 }
408 791
409 792 /*!
410 793 \internal
411 794 */
412 void QBarSeries::modelDataAdded(QModelIndex /*parent*/, int start, int /*end*/)
795 void QBarSeries::modelDataAdded(QModelIndex parent, int start, int end)
413 796 {
797 Q_D(QBarSeries);
798 d->modelDataAdded(parent,start,end);
799 /*
414 800 if (m_mapOrientation == Qt::Vertical) {
415 801 insertCategory(start - m_mapFirst, QString("Row: %1").arg(start + 1));
416 802 for (int i = 0; i <= m_mapBarTop - m_mapBarBottom; i++) {
417 803 barsetAt(i)->insertValue(start - m_mapFirst, m_model->data(m_model->index(start, i), Qt::DisplayRole).toDouble());
418 804 }
419 805 } else {
420 806 insertCategory(start - m_mapFirst, QString("Column: %1").arg(start + 1));
421 807 for (int i = 0; i <= m_mapBarTop - m_mapBarBottom; i++) {
422 808 barsetAt(i)->insertValue(start - m_mapFirst, m_model->data(m_model->index(i, start), Qt::DisplayRole).toDouble());
423 809 }
424 810 }
425 811 emit restructuredBars();
812 */
426 813 }
427 814
428 815 /*!
429 816 \internal
430 817 */
431 818 void QBarSeries::modelDataRemoved(QModelIndex parent, int start, int end)
432 819 {
820 Q_D(QBarSeries);
821 d->modelDataRemoved(parent,start,end);
822 /*
433 823 Q_UNUSED(parent)
434 824 Q_UNUSED(end)
435 825
436 826 removeCategory(start - m_mapFirst);
437 827 for (int i = 0; i <= m_mapBarTop - m_mapBarBottom; i++)
438 828 {
439 829 barsetAt(i)->removeValue(start - m_mapFirst);
440 830 }
441 831 emit restructuredBars();
832 */
442 833 }
443 834
444 835 void QBarSeries::barsetChanged()
445 836 {
446 emit updatedBars();
837 Q_D(QBarSeries);
838 d->barsetChanged();
839 // emit updatedBars();
447 840 }
448 841
449 842 QBarCategories QBarSeries::categories() const
450 843 {
844 Q_D(const QBarSeries);
845 return d->categories();
846 /*
451 847 QBarCategories categories;
452 848 int count = m_internalModel->categoryCount();
453 849 for (int i=1; i <= count; i++) {
454 850 categories.insert(i, m_internalModel->categoryName(i - 1));
455 851 }
456 852 return categories;
853 */
457 854 }
458 855
459 856 /*!
460 857 Sets the visibility of labels in series to \a visible
461 858 */
462 859 void QBarSeries::setLabelsVisible(bool visible)
463 860 {
861 Q_D(QBarSeries);
862 d->setLabelsVisible(visible);
863 /*
464 864 foreach (QBarSet* s, barSets()) {
465 865 s->setLabelsVisible(visible);
466 866 }
867 */
467 868 }
468 869
469 870
470 871 #include "moc_qbarseries.cpp"
872 #include "moc_qbarseriesprivate_p.cpp"
471 873
472 874 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,119 +1,126
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #ifndef BARSERIES_H
22 22 #define BARSERIES_H
23 23
24 24 #include <qseries.h>
25 25 #include <QStringList>
26 26
27 27 class QModelIndex;
28 28
29 29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30 30
31 31 typedef QStringList QBarCategories;
32 32
33 33 class QBarSet;
34 34 class BarChartModel;
35 35 class BarCategory;
36 class QBarSeriesPrivate;
36 37
37 38 // Container for series
38 39 class QTCOMMERCIALCHART_EXPORT QBarSeries : public QSeries
39 40 {
40 41 Q_OBJECT
41 42 public:
42 43 QBarSeries(QStringList categories, QObject *parent = 0);
43 44
44 45 virtual QSeriesType type() const { return QSeries::SeriesTypeBar; }
45 46
46 47 void appendBarSet(QBarSet *set); // Takes ownership of set
47 48 void removeBarSet(QBarSet *set); // Releases ownership, doesn't delete set
48 49 void appendBarSets(QList<QBarSet* > sets);
49 50 void removeBarSets(QList<QBarSet* > sets);
50 51 void insertBarSet(int i, QBarSet *set);
51 52 void insertCategory(int i, QString category);
52 53 void removeCategory(int i);
53 54 int barsetCount() const;
54 55 int categoryCount() const;
55 56 QList<QBarSet*> barSets() const;
56 57 QBarCategories categories() const;
57 58
58 59 void setLabelsVisible(bool visible = true);
59 60
60 61 bool setModel(QAbstractItemModel *model);
61 void setModelMapping(int categories, int bottomBoundry, int topBoundry, Qt::Orientation orientation = Qt::Vertical);
62 void setModelMapping(int categories, int bottomBoundary, int topBoundary, Qt::Orientation orientation = Qt::Vertical);
62 63
63 64 public:
64 65 // TODO: Functions below this are not part of api and will be moved
65 66 // to private implementation, when we start using it
66 67 // TODO: TO PIMPL --->
67 68 QBarSet* barsetAt(int index);
68 69 QString categoryName(int category);
69 70 qreal min();
70 71 qreal max();
71 72 qreal valueAt(int set, int category);
72 73 qreal percentageAt(int set, int category);
73 74 qreal categorySum(int category);
74 75 qreal absoluteCategorySum(int category);
75 76 qreal maxCategorySum();
76 77 BarChartModel& modelInternal();
77 78 // <--- TO PIMPL
78 79
79 80 Q_SIGNALS:
80 81 void clicked(QBarSet *barset, QString category, Qt::MouseButtons button); // Up to user of api, what to do with these signals
81 82 void selected();
82 83 //
83 84 void updatedBars();
84 85 void restructuredBars();
85 86
86 87 // TODO: internal signals, these to private implementation.
87 88 // TODO: TO PIMPL --->
88 void showToolTip(QPoint pos, QString tip);
89 // void showToolTip(QPoint pos, QString tip);
89 90 // <--- TO PIMPL
90 91
91 92 public Q_SLOTS:
92 93 void setToolTipEnabled(bool enabled = true); // enables tooltips
93 94
94 95 // TODO: TO PIMPL --->
95 96 void barsetClicked(QString category, Qt::MouseButtons button);
96 97 // <--- TO PIMPL
97 98
98 99 private Q_SLOTS:
99 100 // slots for updating bars when data in model changes
100 101 void modelUpdated(QModelIndex topLeft, QModelIndex bottomRight);
101 102 void modelDataAdded(QModelIndex parent, int start, int end);
102 103 void modelDataRemoved(QModelIndex parent, int start, int end);
103 104 void barsetChanged();
104 105
105 106 protected:
107 QBarSeriesPrivate * const d_ptr;
108 Q_DECLARE_PRIVATE(QBarSeries)
109 Q_DISABLE_COPY(QBarSeries)
110
111 /*
106 112 BarChartModel *m_internalModel; // TODO: this may change... current "2 models" situation doesn't look good.
107 113
108 114 // QAbstractItemModel* m_model;
109 115 int m_mapCategories;
110 116 int m_mapBarBottom;
111 117 int m_mapBarTop;
112 118 int m_mapFirst;
113 119 int m_mapCount;
114 120 Qt::Orientation m_mapOrientation;
121 */
115 122 };
116 123
117 124 QTCOMMERCIALCHART_END_NAMESPACE
118 125
119 126 #endif // BARSERIES_H
@@ -1,279 +1,467
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #include "qbarset.h"
22 #include <QDebug>
22 #include "qbarsetprivate_p.h"
23 //#include <QDebug>
23 24 #include <QToolTip>
24 25
25 26 QTCOMMERCIALCHART_BEGIN_NAMESPACE
26 27
28
29 QBarSetPrivate::QBarSetPrivate(QString name, QBarSet *parent) : QObject(parent),
30 q_ptr(parent),
31 m_name(name),
32 m_labelsVisible(false)
33 {
34
35 }
36
37 QBarSetPrivate::~QBarSetPrivate()
38 {
39
40 }
41
42 void QBarSetPrivate::setName(QString name)
43 {
44 m_name = name;
45 }
46
47 QString QBarSetPrivate::name() const
48 {
49 return m_name;
50 }
51
52 QBarSetPrivate& QBarSetPrivate::operator << (const qreal &value)
53 {
54 m_values.append(value);
55 emit structureChanged();
56 return *this;
57 }
58
59 void QBarSetPrivate::insertValue(int i, qreal value)
60 {
61 m_values.insert(i, value);
62 }
63
64 void QBarSetPrivate::removeValue(int i)
65 {
66 m_values.removeAt(i);
67 }
68
69 int QBarSetPrivate::count() const
70 {
71 return m_values.count();
72 }
73
74 qreal QBarSetPrivate::valueAt(int index) const
75 {
76 return m_values.at(index);
77 }
78
79 void QBarSetPrivate::setValue(int index, qreal value)
80 {
81 m_values.replace(index,value);
82 emit valueChanged();
83 }
84
85 qreal QBarSetPrivate::sum() const
86 {
87 qreal sum(0);
88 for (int i=0; i < m_values.count(); i++) {
89 sum += m_values.at(i);
90 }
91 return sum;
92 }
93
94 void QBarSetPrivate::setPen(const QPen &pen)
95 {
96 m_pen = pen;
97 emit valueChanged();
98 }
99
100 QPen QBarSetPrivate::pen() const
101 {
102 return m_pen;
103 }
104
105 void QBarSetPrivate::setBrush(const QBrush &brush)
106 {
107 m_brush = brush;
108 emit valueChanged();
109 }
110
111 QBrush QBarSetPrivate::brush() const
112 {
113 return m_brush;
114 }
115
116 void QBarSetPrivate::setLabelPen(const QPen &pen)
117 {
118 m_labelPen = pen;
119 emit valueChanged();
120 }
121
122 QPen QBarSetPrivate::labelPen() const
123 {
124 return m_labelPen;
125 }
126
127 void QBarSetPrivate::setLabelBrush(const QBrush &brush)
128 {
129 m_labelBrush = brush;
130 emit valueChanged();
131 }
132
133 QBrush QBarSetPrivate::labelBrush() const
134 {
135 return m_labelBrush;
136 }
137
138 void QBarSetPrivate::setLabelFont(const QFont &font)
139 {
140 m_labelFont = font;
141 emit valueChanged();
142 }
143
144 QFont QBarSetPrivate::labelFont() const
145 {
146 return m_labelFont;
147 }
148
149 void QBarSetPrivate::setLabelsVisible(bool visible)
150 {
151 m_labelsVisible = visible;
152 emit labelsVisibleChanged(visible);
153 }
154
155 bool QBarSetPrivate::labelsVisible() const
156 {
157 return m_labelsVisible;
158 }
159
160 void QBarSetPrivate::barHoverEnterEvent(QPoint pos)
161 {
162 emit showToolTip(pos, m_name);
163 emit hoverEnter(pos);
164 }
165
166 void QBarSetPrivate::barHoverLeaveEvent()
167 {
168 // Emit signal to user of charts
169 emit hoverLeave();
170 }
171
172
27 173 /*!
28 174 \class QBarSet
29 175 \brief part of QtCommercial chart API.
30 176
31 177 QBarSet represents one set of bars. Set of bars contains one data value for each category.
32 178 First value of set is assumed to belong to first category, second to second category and so on.
33 179 If set has fewer values than there are categories, then the missing values are assumed to be
34 180 at the end of set. For missing values in middle of a set, numerical value of zero is used.
35 181
36 182 \mainclass
37 183
38 184 \sa QBarSeries, QStackedBarSeries, QPercentBarSeries
39 185 */
40 186
41 187 /*!
42 188 \fn void QBarSet::clicked(QString category, Qt::MouseButtons button)
43 189 \brief signals that set has been clicked
44 190 Parameter \a category describes on which category was clicked
45 191 Parameter \a button mouse button
46 192 */
47 193
48 194 /*!
49 195 \fn void QBarSet::hoverEnter(QPoint pos)
50 196 \brief signals that mouse has entered over the set at position \a pos.
51 197 */
52 198
53 199 /*!
54 200 \fn void QBarSet::hoverLeave()
55 201 \brief signals that mouse has left from the set.
56 202 */
57 203
58 204 /*!
59 205 \fn void QBarSet::showToolTip(QPoint pos, QString tip)
60 206 \brief \internal \a pos \a tip
61 207 */
62 208
63 209
64 210 /*!
65 211 Constructs QBarSet with a name of \a name and with parent of \a parent
66 212 */
67 213 QBarSet::QBarSet(QString name, QObject *parent)
68 214 : QObject(parent)
69 ,m_name(name)
70 ,m_labelsVisible(false)
215 ,d_ptr(new QBarSetPrivate(name,this))
216 // ,m_name(name)
217 // ,m_labelsVisible(false)
71 218 {
72 219 }
73 220
74 221 /*!
75 222 Sets new \a name for set.
76 223 */
77 224 void QBarSet::setName(QString name)
78 225 {
79 m_name = name;
226 Q_D(QBarSet);
227 d->setName(name);
80 228 }
81 229
82 230 /*!
83 231 Returns name of the set.
84 232 */
85 233 QString QBarSet::name() const
86 234 {
87 return m_name;
235 Q_D(const QBarSet);
236 return d->name();
88 237 }
89 238
90 239 /*!
91 240 Appends new value \a value to the end of set.
92 241 */
93 242 QBarSet& QBarSet::operator << (const qreal &value)
94 243 {
95 m_values.append(value);
96 emit structureChanged();
244 Q_D(QBarSet);
245 d->operator <<(value);
97 246 return *this;
247
248 // m_values.append(value);
249 // emit structureChanged();
250 // return *this;
98 251 }
99 252
100 253 /*!
101 254 Inserts new \a value on the \a i position.
102 255 The value that is currently at this postion is moved to postion i + 1
103 256 \sa removeValue()
104 257 */
105 258 void QBarSet::insertValue(int i, qreal value)
106 259 {
107 m_values.insert(i, value);
260 Q_D(QBarSet);
261 d->insertValue(i,value);
262 // m_values.insert(i, value);
108 263 }
109 264
110 265 /*!
111 266 Removes the value specified by \a i
112 267 \sa insertValue()
113 268 */
114 269 void QBarSet::removeValue(int i)
115 270 {
116 m_values.removeAt(i);
271 Q_D(QBarSet);
272 d->removeValue(i);
273 // m_values.removeAt(i);
117 274 }
118 275
119 276 /*!
120 277 Returns count of values in set.
121 278 */
122 279 int QBarSet::count() const
123 280 {
124 return m_values.count();
281 Q_D(const QBarSet);
282 return d->count();
283 // return m_values.count();
125 284 }
126 285
127 286 /*!
128 287 Returns value of set indexed by \a index
129 288 */
130 289 qreal QBarSet::valueAt(int index) const
131 290 {
132 return m_values.at(index);
291 Q_D(const QBarSet);
292 return d->valueAt(index);
293 // return m_values.at(index);
133 294 }
134 295
135 296 /*!
136 297 Sets a new value \a value to set, indexed by \a index
137 298 */
138 299 void QBarSet::setValue(int index, qreal value)
139 300 {
140 m_values.replace(index,value);
141 emit valueChanged();
301 Q_D(QBarSet);
302 d->setValue(index,value);
303 // m_values.replace(index,value);
304 // emit valueChanged();
142 305 }
143 306
144 307 /*!
145 Returns total sum of all values in barset.
308 Returns sum of all values in barset.
146 309 */
147 qreal QBarSet::total() const
310 qreal QBarSet::sum() const
148 311 {
312 Q_D(const QBarSet);
313 return d->sum();
314 /*
149 315 qreal total(0);
150 316 for (int i=0; i < m_values.count(); i++) {
151 317 total += m_values.at(i);
152 318 }
153 319 return total;
320 */
154 321 }
155 322
156 323 /*!
157 324 Sets pen for set. Bars of this set are drawn using \a pen
158 325 */
159 326 void QBarSet::setPen(const QPen &pen)
160 327 {
161 m_pen = pen;
162 emit valueChanged();
328 Q_D(QBarSet);
329 d->setPen(pen);
330 // m_pen = pen;
331 // emit valueChanged();
163 332 }
164 333
165 334 /*!
166 335 Returns pen of the set.
167 336 */
168 337 QPen QBarSet::pen() const
169 338 {
170 return m_pen;
339 Q_D(const QBarSet);
340 return d->pen();
341 // return m_pen;
171 342 }
172 343
173 344 /*!
174 345 Sets brush for the set. Bars of this set are drawn using \a brush
175 346 */
176 347 void QBarSet::setBrush(const QBrush &brush)
177 348 {
178 m_brush = brush;
179 emit valueChanged();
349 Q_D(QBarSet);
350 d->setBrush(brush);
351 // m_brush = brush;
352 // emit valueChanged();
180 353 }
181 354
182 355 /*!
183 356 Returns brush of the set.
184 357 */
185 358 QBrush QBarSet::brush() const
186 359 {
187 return m_brush;
360 Q_D(const QBarSet);
361 return d->brush();
362 // return m_brush;
188 363 }
189 364
190 365 /*!
191 366 Sets \a pen of the values that are drawn on top of this barset
192 367 */
193 368 void QBarSet::setLabelPen(const QPen &pen)
194 369 {
195 m_labelPen = pen;
196 emit valueChanged();
370 Q_D(QBarSet);
371 d->setLabelPen(pen);
372 // m_labelPen = pen;
373 // emit valueChanged();
197 374 }
198 375
199 376 /*!
200 377 Returns pen of the values that are drawn on top of this barset
201 378 */
202 379 QPen QBarSet::labelPen() const
203 380 {
204 return m_labelPen;
381 Q_D(const QBarSet);
382 return d->labelPen();
383 // return m_labelPen;
205 384 }
206 385
207 386 /*!
208 387 Sets \a brush of the values that are drawn on top of this barset
209 388 */
210 389 void QBarSet::setLabelBrush(const QBrush &brush)
211 390 {
212 m_labelBrush = brush;
213 emit valueChanged();
391 Q_D(QBarSet);
392 d->setLabelBrush(brush);
393 // m_labelBrush = brush;
394 // emit valueChanged();
214 395 }
215 396
216 397 /*!
217 398 Returns brush of the values that are drawn on top of this barset
218 399 */
219 400 QBrush QBarSet::labelBrush() const
220 401 {
221 return m_labelBrush;
402 Q_D(const QBarSet);
403 return d->labelBrush();
404 // return m_labelBrush;
222 405 }
223 406
224 407 /*!
225 408 Sets the \a font for values that are drawn on top of this barset
226 409 */
227 410 void QBarSet::setLabelFont(const QFont &font)
228 411 {
229 m_labelFont = font;
230 emit valueChanged();
412 Q_D(QBarSet);
413 d->setLabelFont(font);
414 // m_labelFont = font;
415 // emit valueChanged();
231 416 }
232 417
233 418 /*!
234 419 Returns the pen for values that are drawn on top of this set
235 420 */
236 421 QFont QBarSet::labelFont() const
237 422 {
238 return m_labelFont;
423 Q_D(const QBarSet);
424 return d->labelFont();
425 // return m_labelFont;
239 426 }
240 427
241 428 /*!
242 429 Sets visibility of bar labels. If \a visible is true, labels are drawn on top of barsets.
243 430 */
244 431
245 432 void QBarSet::setLabelsVisible(bool visible)
246 433 {
247 m_labelsVisible = visible;
248 emit labelsVisibleChanged(visible);
434 Q_D(QBarSet);
435 d->setLabelsVisible(visible);
436 // m_labelsVisible = visible;
437 // emit labelsVisibleChanged(visible);
249 438 }
250 439
251 440 /*!
252 441 Returns the visibility of values
253 442 */
254 443 bool QBarSet::labelsVisible() const
255 444 {
256 return m_labelsVisible;
445 Q_D(const QBarSet);
446 return d->labelsVisible();
447 // return m_labelsVisible;
257 448 }
258 449
259 /*!
260 \internal \a pos
261 */
450 /*
262 451 void QBarSet::barHoverEnterEvent(QPoint pos)
263 452 {
264 453 emit showToolTip(pos, m_name);
265 454 emit hoverEnter(pos);
266 455 }
267
268 /*!
269 \internal
270 456 */
457 /*
271 458 void QBarSet::barHoverLeaveEvent()
272 459 {
273 460 // Emit signal to user of charts
274 461 emit hoverLeave();
275 462 }
276
463 */
277 464 #include "moc_qbarset.cpp"
465 #include "moc_qbarsetprivate_p.cpp"
278 466
279 467 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,105 +1,104
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #ifndef QBARSET_H
22 22 #define QBARSET_H
23 23
24 24 #include <qchartglobal.h>
25 25 #include <QPen>
26 26 #include <QBrush>
27 27 #include <QFont>
28 28
29 29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30 class QBarSetPrivate;
30 31
31 32 class QTCOMMERCIALCHART_EXPORT QBarSet : public QObject
32 33 {
33 34 Q_OBJECT
34 35 public:
35 36 QBarSet(QString name, QObject *parent = 0);
36 37
37 38 void setName(QString name);
38 39 QString name() const;
39 40 QBarSet& operator << (const qreal &value); // appends new value to set
40 41 void insertValue(int i, qreal value);
41 42 void removeValue(int i);
42
43 // TODO: remove indices eventually. Use as internal?
44 43 int count() const; // count of values in set
45 qreal valueAt(int index) const; // for modifying individual values
44 qreal valueAt(int index) const; // for modifying individual values
46 45 void setValue(int index, qreal value); // setter for individual value
47 qreal total() const; // total values in the set
48
49 // TODO:
50 //qreal value(QString category);
51 //void setValue(QString category, qreal value);
46 qreal sum() const; // sum of all values in the set
52 47
53 48 void setPen(const QPen &pen);
54 49 QPen pen() const;
55 50
56 51 void setBrush(const QBrush &brush);
57 52 QBrush brush() const;
58 53
59 54 void setLabelPen(const QPen &pen);
60 55 QPen labelPen() const;
61 56
62 57 void setLabelBrush(const QBrush &brush);
63 58 QBrush labelBrush() const;
64 59
65 60 void setLabelFont(const QFont &font);
66 61 QFont labelFont() const;
67 62
68 63 void setLabelsVisible(bool visible = true);
69 64 bool labelsVisible() const;
70 65
71 66 Q_SIGNALS:
72 67 void clicked(QString category, Qt::MouseButtons button); // Clicked and hover signals exposed to user
73 68
74 69 // TODO: TO PIMPL --->
75 void structureChanged();
76 void valueChanged();
77 void hoverEnter(QPoint pos);
78 void hoverLeave();
79 void showToolTip(QPoint pos, QString tip); // Private signal
80 void labelsVisibleChanged(bool visible);
70 // void structureChanged();
71 // void valueChanged();
72 // void hoverEnter(QPoint pos);
73 // void hoverLeave();
74 // void showToolTip(QPoint pos, QString tip); // Private signal
75 // void labelsVisibleChanged(bool visible);
81 76 // <--- TO PIMPL
82 77
83 78 public Q_SLOTS:
84 79 // These are for internal communication
85 80 // TODO: TO PIMPL --->
86 void barHoverEnterEvent(QPoint pos);
87 void barHoverLeaveEvent();
81 // void barHoverEnterEvent(QPoint pos);
82 // void barHoverLeaveEvent();
88 83 // <--- TO PIMPL
89 84
90 85 private:
91
86 QBarSetPrivate * const d_ptr;
87 Q_DECLARE_PRIVATE(QBarSet)
88 Q_DISABLE_COPY(QBarSet)
89 /*
92 90 QString m_name;
93 91 QList<qreal> m_values; // TODO: replace with map (category, value)
94 92 QMap<QString, qreal> m_mappedValues;
95 93 QPen m_pen;
96 94 QBrush m_brush;
97 95 QPen m_labelPen;
98 96 QBrush m_labelBrush;
99 97 QFont m_labelFont;
100 98 bool m_labelsVisible;
99 */
101 100 };
102 101
103 102 QTCOMMERCIALCHART_END_NAMESPACE
104 103
105 104 #endif // QBARSET_H
General Comments 0
You need to be logged in to leave comments. Login now