##// END OF EJS Templates
barchart mvp model integrating
sauimone -
r160:f044c4ee95db
parent child
Show More
@@ -1,99 +1,99
1 1 #include <limits.h>
2 2 #include "barchartmodel_p.h"
3 3
4 4 QTCOMMERCIALCHART_BEGIN_NAMESPACE
5 5
6 6 BarChartModel::BarChartModel(QObject *parent) :
7 7 QObject(parent)
8 8 {
9 9 }
10 10
11 11 void BarChartModel::addSeries(BarChartSeriesBase& series)
12 12 {
13 13 mSeries.append(&series);
14 14 emit modelUpdated();
15 15 }
16 16
17 17 void BarChartModel::removeSeries(BarChartSeriesBase& series)
18 18 {
19 19 int i = mSeries.indexOf(&series);
20 20 if (-1 == i) {
21 21 return;
22 22 }
23 23 mSeries.removeAt(i);
24 24 emit modelUpdated();
25 25 }
26 26
27 27 int BarChartModel::countSeries()
28 28 {
29 29 return mSeries.count();
30 30 }
31 31
32 32 int BarChartModel::countItemsInSeries()
33 33 {
34 34 int count(0);
35 35 for (int i=0; i<mSeries.count(); i++){
36 // TODO: can we assume that all series have same number of items? If not. then which items are empty.
37 int temp = mSeries.at(i)->countItems();
36 // TODO: can we assume that all series have same number of values? If not. then which values are empty?
37 int temp = mSeries.at(i)->countValues();
38 38 if (temp > count) {
39 39 count = temp;
40 40 }
41 41 }
42 42 return count;
43 43 }
44 44
45 45 int BarChartModel::countTotalItems()
46 46 {
47 47 int total = mSeries.count() * countItemsInSeries();
48 48 return total;
49 49 }
50 50
51 51 int BarChartModel::min()
52 52 {
53 53 Q_ASSERT(mSeries.count() > 0);
54 54 // TODO: make min and max members and update them when data changes.
55 55 // This is slower since they are checked every time, even if data is same since previous call.
56 56 int min = INT_MAX;
57 57
58 58 for (int i=0; i <mSeries.count(); i++) {
59 59 int temp = mSeries.at(i)->min();
60 60 if (temp < min) {
61 61 min = temp;
62 62 }
63 63 }
64 64 return min;
65 65 }
66 66
67 67 int BarChartModel::max()
68 68 {
69 69 Q_ASSERT(mSeries.count() > 0);
70 70
71 71 // TODO: make min and max members and update them when data changes.
72 72 // This is slower since they are checked every time, even if data is same since previous call.
73 73 int max = INT_MIN;
74 74
75 75 for (int i=0; i <mSeries.count(); i++) {
76 76 int temp = mSeries.at(i)->min();
77 77 if (temp > max) {
78 78 max = temp;
79 79 }
80 80 }
81 81 return max;
82 82 }
83 83
84 84 qreal BarChartModel::valueAt(int series, int item)
85 85 {
86 86 if ((series < 0) || (series >= mSeries.count())) {
87 87 // No series, no value.
88 88 return 0;
89 } else if ((item < 0) || (item >= mSeries.at(series)->countItems())) {
89 } else if ((item < 0) || (item >= mSeries.at(series)->countValues())) {
90 90 // No item, no value.
91 91 return 0;
92 92 }
93 93
94 94 return mSeries.at(series)->valueAt(item);
95 95 }
96 96
97 97 #include "moc_barchartmodel_p.cpp"
98 98
99 99 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,158 +1,158
1 1 #include <limits.h>
2 2 #include <QDebug>
3 3 #include "barchartseriesbase.h"
4 4 #include "bargroup.h"
5 5
6 6 QTCOMMERCIALCHART_BEGIN_NAMESPACE
7 7
8 8 BarChartSeriesBase::BarChartSeriesBase(QObject *parent)
9 9 : QChartSeries(parent)
10 10 ,mData(0)
11 11 {
12 12 }
13 13 /*
14 14 bool BarChartSeriesBase::setModel(QAbstractItemModel* model)
15 15 {
16 16 mModel = model;
17 17 return true;
18 18 }
19 19 */
20 20 bool BarChartSeriesBase::setData(QList<qreal>& data)
21 21 {
22 22 mData = &data;
23 23 return true;
24 24 }
25 25 /*
26 26 int BarChartSeriesBase::min()
27 27 {
28 28 Q_ASSERT(mModel->rowCount() > 0);
29 29 Q_ASSERT(mModel->columnCount() > 0);
30 30
31 31 // TODO: make min and max members and update them when data changes.
32 32 // This is slower since they are checked every time, even if data is same since previous call.
33 33 int min = INT_MAX;
34 34
35 35 for (int i=0; i <mModel->rowCount(); i++) {
36 36 for(int j=0; j<mModel->columnCount(); j++) {
37 37 int temp = mModel->data(mModel->index(i,j)).toInt();
38 38 if (temp < min) {
39 39 min = temp;
40 40 }
41 41 }
42 42 }
43 43 return min;
44 44 }
45 45
46 46 int BarChartSeriesBase::max()
47 47 {
48 48 Q_ASSERT(mModel->rowCount() > 0);
49 49 Q_ASSERT(mModel->columnCount() > 0);
50 50
51 51 // TODO: make min and max members and update them when data changes.
52 52 // This is slower since they are checked every time, even if data is same since previous call.
53 53 int max = INT_MIN;
54 54
55 55 for (int i=0; i <mModel->rowCount(); i++) {
56 56 for(int j=0; j<mModel->columnCount(); j++) {
57 57 int temp = mModel->data(mModel->index(i,j)).toInt();
58 58 if (temp > max) {
59 59 max = temp;
60 60 }
61 61 }
62 62 }
63 63 return max;
64 64 }
65 65
66 66 int BarChartSeriesBase::maxColumnSum()
67 67 {
68 68 Q_ASSERT(mModel->rowCount() > 0);
69 69 Q_ASSERT(mModel->columnCount() > 0);
70 70
71 71 int max = INT_MIN;
72 72
73 73 for (int col=0; col <mModel->columnCount(); col++) {
74 74 int sum = columnSum(col);
75 75 if (sum > max) {
76 76 max = sum;
77 77 }
78 78 }
79 79 return max;
80 80 }
81 81
82 82 int BarChartSeriesBase::countRows()
83 83 {
84 84 return mModel->rowCount();
85 85 }
86 86
87 87 int BarChartSeriesBase::countColumns()
88 88 {
89 89 return mModel->columnCount();
90 90 }
91 91
92 92 int BarChartSeriesBase::countTotalItems()
93 93 {
94 94 return mModel->rowCount() * mModel->columnCount();
95 95 }
96 96
97 97 int BarChartSeriesBase::valueAt(int row, int column)
98 98 {
99 99 QModelIndex index = mModel->index(row,column);
100 100 return mModel->data(index).toInt();
101 101 }
102 102
103 103 int BarChartSeriesBase::columnSum(int column)
104 104 {
105 105 int sum(0);
106 106 int count = mModel->rowCount();
107 107
108 108 for (int row = 0; row < count; row++) {
109 109 sum += mModel->data(mModel->index(row,column)).toInt();
110 110 }
111 111 return sum;
112 112 }
113 113 */
114 114 qreal BarChartSeriesBase::min()
115 115 {
116 116 Q_ASSERT(mData != 0);
117 117
118 118 int count = mData->count();
119 119 int min = INT_MAX;
120 120
121 121 for (int i=0; i<count; i++) {
122 122 if (mData->at(i) < min) {
123 123 min = mData->at(i);
124 124 }
125 125 }
126 126 return min;
127 127 }
128 128
129 129 qreal BarChartSeriesBase::max()
130 130 {
131 131 Q_ASSERT(mData != 0);
132 132
133 133 int count = mData->count();
134 134 int max = INT_MIN;
135 135
136 136 for (int i=0; i<count; i++) {
137 137 if (mData->at(i) > max) {
138 138 max = mData->at(i);
139 139 }
140 140 }
141 141 return max;
142 142 }
143 143
144 int BarChartSeriesBase::countItems()
144 int BarChartSeriesBase::countValues()
145 145 {
146 146 Q_ASSERT(mData != 0);
147 147 return mData->count();
148 148 }
149 149
150 150 qreal BarChartSeriesBase::valueAt(int item)
151 151 {
152 152 Q_ASSERT(mData != 0);
153 153 return mData->at(item);
154 154 }
155 155
156 156 #include "moc_barchartseriesbase.cpp"
157 157
158 158 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,58 +1,58
1 1 #ifndef BARCHARTSERIESBASE_H
2 2 #define BARCHARTSERIESBASE_H
3 3
4 4 #include <QList>
5 5 #include <QAbstractItemModel>
6 6 #include "qchartseries.h"
7 7 #include "qchartglobal.h"
8 8
9 9 class BarGroupBase;
10 10
11 11 QTCOMMERCIALCHART_BEGIN_NAMESPACE
12 12
13 13 // Container for series
14 14 class QTCOMMERCIALCHART_EXPORT BarChartSeriesBase : public QChartSeries
15 15 {
16 16 Q_OBJECT
17 17 protected:
18 18 BarChartSeriesBase(QObject* parent=0);
19 19
20 20 public:
21 21 // from QChartSeries
22 22 virtual QChartSeriesType type() const { return QChartSeries::SeriesTypeInvalid; }
23 23
24 24 // TODO: Better data model?
25 25 // virtual bool setModel(QAbstractItemModel* model);
26 26 virtual bool setData(QList<qreal>& data);
27 27
28 28 // Methods to find out minimum and maximum values of data
29 29 // int min(); // TODO: remove
30 30 // int max(); // TODO: remove
31 31 // int maxColumnSum(); // TODO: move to model. returns maximum sum of items in all columns.
32 32
33 33 // int countRows(); // TODO: remove.
34 34 // int countColumns(); // TODO: remove. Count items in one series.
35 35 // int countTotalItems(); // TODO: move to model
36 36 // int valueAt(int row, int column); // TODO: move to model
37 37
38 38 // int columnSum(int column); // TODO: move to model
39 39
40 40 qreal min();
41 41 qreal max();
42 int countItems();
42 int countValues();
43 43 qreal valueAt(int item);
44 44
45 45 public Q_SLOTS:
46 46
47 47 private:
48 48
49 49 QAbstractItemModel* mModel;
50 50 BarGroupBase* mBarGroup;
51 51
52 52 QList<qreal>* mData;
53 53
54 54 };
55 55
56 56 QTCOMMERCIALCHART_END_NAMESPACE
57 57
58 58 #endif // BARCHARTSERIESBASE_H
@@ -1,160 +1,165
1 1 #include "chartdataset_p.h"
2 2 //series
3 3 #include "qlinechartseries.h"
4 4 #include "barchartseries.h"
5 5 #include "stackedbarchartseries.h"
6 6 #include "percentbarchartseries.h"
7 7 #include "qpieseries.h"
8 8 #include "qscatterseries.h"
9 9
10 10 QTCOMMERCIALCHART_BEGIN_NAMESPACE
11 11
12 12 ChartDataSet::ChartDataSet(QObject *parent):QObject(parent)
13 13 {
14 14 Domain domain;
15 15 m_domains<<domain;
16 16 }
17 17
18 18 ChartDataSet::~ChartDataSet()
19 19 {
20 20 // TODO Auto-generated destructor stub
21 21 }
22 22
23 23 const Domain& ChartDataSet::domain() const
24 24 {
25 25 return m_domains[m_domainIndex];
26 26 }
27 27
28 28 void ChartDataSet::addSeries(QChartSeries* series)
29 29 {
30 30 // TODO: we should check the series not already added
31 31 m_chartSeries << series;
32 32 m_domainIndex = 0;
33 33 m_domains.resize(1);
34 34
35 35 Domain& domain = m_domains[m_domainIndex];
36 36
37 37 switch(series->type())
38 38 {
39 39 case QChartSeries::SeriesTypeLine: {
40 40
41 41 QLineChartSeries* xyseries = static_cast<QLineChartSeries*>(series);
42 42
43 43 for (int i = 0; i < xyseries->count(); i++)
44 44 {
45 45 qreal x = xyseries->x(i);
46 46 qreal y = xyseries->y(i);
47 47 domain.m_minX = qMin(domain.m_minX,x);
48 48 domain.m_minY = qMin(domain.m_minY,y);
49 49 domain.m_maxX = qMax(domain.m_maxX,x);
50 50 domain.m_maxY = qMax(domain.m_maxY,y);
51 51 }
52 52 break;
53 53 }
54 54 case QChartSeries::SeriesTypeBar: {
55 55
56 56 BarChartSeries* barSeries = static_cast<BarChartSeries*>(series);
57 qreal x = barSeries->countColumns();
57 qreal x = barSeries->countValues();
58 58 qreal y = barSeries->max();
59 59 domain.m_minX = qMin(domain.m_minX,x);
60 60 domain.m_minY = qMin(domain.m_minY,y);
61 61 domain.m_maxX = qMax(domain.m_maxX,x);
62 62 domain.m_maxY = qMax(domain.m_maxY,y);
63
63 64 }
64 65 break;
65 66 case QChartSeries::SeriesTypeStackedBar: {
66 67
67 68 StackedBarChartSeries* stackedBarSeries = static_cast<StackedBarChartSeries*>(series);
69 /*
68 70 qreal x = stackedBarSeries->countColumns();
69 71 qreal y = stackedBarSeries->maxColumnSum();
70 72 domain.m_minX = qMin(domain.m_minX,x);
71 73 domain.m_minY = qMin(domain.m_minY,y);
72 74 domain.m_maxX = qMax(domain.m_maxX,x);
73 75 domain.m_maxY = qMax(domain.m_maxY,y);
76 */
74 77 }
75 78 break;
76 79 case QChartSeries::SeriesTypePercentBar: {
77 80
78 81 PercentBarChartSeries* percentBarSeries = static_cast<PercentBarChartSeries*>(series);
82 /*
79 83 qreal x = percentBarSeries->countColumns();
80 84 domain.m_minX = qMin(domain.m_minX,x);
81 85 domain.m_minY = 0;
82 86 domain.m_maxX = qMax(domain.m_maxX,x);
83 87 domain.m_maxY = 100;
88 */
84 89 }
85 90 break;
86 91
87 92 case QChartSeries::SeriesTypePie: {
88 93 QPieSeries *pieSeries = static_cast<QPieSeries *>(series);
89 94 // TODO: domain stuff
90 95 break;
91 96 }
92 97
93 98 case QChartSeries::SeriesTypeScatter: {
94 99 QScatterSeries *scatterSeries = qobject_cast<QScatterSeries *>(series);
95 100 Q_ASSERT(scatterSeries);
96 101 foreach (QPointF point, scatterSeries->data()) {
97 102 domain.m_minX = qMin(domain.m_minX, point.x());
98 103 domain.m_maxX = qMax(domain.m_maxX, point.x());
99 104 domain.m_minY = qMin(domain.m_minY, point.y());
100 105 domain.m_maxY = qMax(domain.m_maxY, point.y());
101 106 }
102 107 break;
103 108 }
104 109
105 110 default: {
106 111 qDebug()<<__FUNCTION__<<"type" << series->type()<<"not supported";
107 112 return;
108 113 break;
109 114 }
110 115
111 116 }
112 117
113 118 emit seriesAdded(series);
114 119 emit domainChanged(domain);
115 120 }
116 121
117 122 bool ChartDataSet::nextDomain()
118 123 {
119 124 if (m_domainIndex < m_domains.count() - 1) {
120 125 m_domainIndex++;
121 126 emit domainChanged(m_domains[m_domainIndex]);
122 127 return true;
123 128 }
124 129 else {
125 130 return false;
126 131 }
127 132 }
128 133
129 134 bool ChartDataSet::previousDomain()
130 135 {
131 136 if (m_domainIndex > 0) {
132 137 m_domainIndex--;
133 138 emit domainChanged(m_domains[m_domainIndex]);
134 139 return true;
135 140 }
136 141 else {
137 142 return false;
138 143 }
139 144 }
140 145
141 146 void ChartDataSet::clearDomains()
142 147 {
143 148 if (m_domainIndex > 0) {
144 149 m_domainIndex = 0;
145 150 emit domainChanged(m_domains[m_domainIndex]);
146 151 }
147 152 }
148 153
149 154 void ChartDataSet::addDomain(const Domain& domain)
150 155 {
151 156 m_domains.resize(m_domainIndex + 1);
152 157 m_domains << domain;
153 158 m_domainIndex++;
154 159
155 160 emit domainChanged(domain);
156 161 }
157 162
158 163 #include "moc_chartdataset_p.cpp"
159 164
160 165 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now