@@ -1,169 +1,174 | |||
|
1 | 1 | #include <limits.h> |
|
2 | 2 | #include <QVector> |
|
3 | 3 | #include <QDebug> |
|
4 | 4 | #include "barchartmodel_p.h" |
|
5 | 5 | #include "qbarset.h" |
|
6 | 6 | |
|
7 | 7 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
8 | 8 | |
|
9 | 9 | BarChartModel::BarChartModel(QStringList categories, QObject *parent) : |
|
10 | 10 | QObject(parent) |
|
11 | 11 | ,mCategory(categories) |
|
12 | 12 | { |
|
13 | 13 | } |
|
14 | 14 | |
|
15 | 15 | QStringList BarChartModel::category() |
|
16 | 16 | { |
|
17 | 17 | return mCategory; |
|
18 | 18 | } |
|
19 | 19 | |
|
20 | 20 | void BarChartModel::addBarSet(QBarSet *set) |
|
21 | 21 | { |
|
22 | 22 | mDataModel.append(set); |
|
23 | 23 | } |
|
24 | 24 | |
|
25 | 25 | void BarChartModel::removeBarSet(QBarSet *set) |
|
26 | 26 | { |
|
27 | 27 | if (mDataModel.contains(set)) { |
|
28 | 28 | mDataModel.removeOne(set); |
|
29 | 29 | } |
|
30 | 30 | } |
|
31 | 31 | |
|
32 | 32 | void BarChartModel::insertBarSet(int i, QBarSet *set) |
|
33 | 33 | { |
|
34 | 34 | mDataModel.insert(i, set); |
|
35 | 35 | } |
|
36 | 36 | |
|
37 | 37 | void BarChartModel::insertCategory(int i, QString category) |
|
38 | 38 | { |
|
39 | 39 | mCategory.insert(i, category); |
|
40 | 40 | } |
|
41 | 41 | |
|
42 | void BarChartModel::removeCategory(int i) | |
|
43 | { | |
|
44 | mCategory.removeAt(i); | |
|
45 | } | |
|
46 | ||
|
42 | 47 | QBarSet* BarChartModel::setAt(int index) |
|
43 | 48 | { |
|
44 | 49 | return mDataModel.at(index); |
|
45 | 50 | } |
|
46 | 51 | |
|
47 | 52 | QList<QBarSet*> BarChartModel::barSets() |
|
48 | 53 | { |
|
49 | 54 | return mDataModel; |
|
50 | 55 | } |
|
51 | 56 | |
|
52 | 57 | int BarChartModel::barsetCount() |
|
53 | 58 | { |
|
54 | 59 | return mDataModel.count(); |
|
55 | 60 | } |
|
56 | 61 | |
|
57 | 62 | int BarChartModel::categoryCount() |
|
58 | 63 | { |
|
59 | 64 | return mCategory.count(); |
|
60 | 65 | } |
|
61 | 66 | |
|
62 | 67 | qreal BarChartModel::min() |
|
63 | 68 | { |
|
64 | 69 | Q_ASSERT(mDataModel.count() > 0); |
|
65 | 70 | // TODO: make min and max members and update them when data changes. |
|
66 | 71 | // This is slower since they are checked every time, even if data is same since previous call. |
|
67 | 72 | qreal min = INT_MAX; |
|
68 | 73 | |
|
69 | 74 | for (int i=0; i <mDataModel.count(); i++) { |
|
70 | 75 | int itemCount = mDataModel.at(i)->count(); |
|
71 | 76 | for (int j=0; j<itemCount; j++) { |
|
72 | 77 | qreal temp = mDataModel.at(i)->valueAt(j); |
|
73 | 78 | if (temp < min) { |
|
74 | 79 | min = temp; |
|
75 | 80 | } |
|
76 | 81 | } |
|
77 | 82 | } |
|
78 | 83 | return min; |
|
79 | 84 | } |
|
80 | 85 | |
|
81 | 86 | qreal BarChartModel::max() |
|
82 | 87 | { |
|
83 | 88 | Q_ASSERT(mDataModel.count() > 0); |
|
84 | 89 | |
|
85 | 90 | // TODO: make min and max members and update them when data changes. |
|
86 | 91 | // This is slower since they are checked every time, even if data is same since previous call. |
|
87 | 92 | qreal max = INT_MIN; |
|
88 | 93 | |
|
89 | 94 | for (int i=0; i <mDataModel.count(); i++) { |
|
90 | 95 | int itemCount = mDataModel.at(i)->count(); |
|
91 | 96 | for (int j=0; j<itemCount; j++) { |
|
92 | 97 | qreal temp = mDataModel.at(i)->valueAt(j); |
|
93 | 98 | if (temp > max) { |
|
94 | 99 | max = temp; |
|
95 | 100 | } |
|
96 | 101 | } |
|
97 | 102 | } |
|
98 | 103 | |
|
99 | 104 | return max; |
|
100 | 105 | } |
|
101 | 106 | |
|
102 | 107 | qreal BarChartModel::valueAt(int set, int category) |
|
103 | 108 | { |
|
104 | 109 | if ((set < 0) || (set >= mDataModel.count())) { |
|
105 | 110 | // No set, no value. |
|
106 | 111 | return 0; |
|
107 | 112 | } else if ((category < 0) || (category >= mDataModel.at(set)->count())) { |
|
108 | 113 | // No category, no value. |
|
109 | 114 | return 0; |
|
110 | 115 | } |
|
111 | 116 | |
|
112 | 117 | return mDataModel.at(set)->valueAt(category); |
|
113 | 118 | } |
|
114 | 119 | |
|
115 | 120 | qreal BarChartModel::percentageAt(int set, int category) |
|
116 | 121 | { |
|
117 | 122 | if ((set < 0) || (set >= mDataModel.count())) { |
|
118 | 123 | // No set, no value. |
|
119 | 124 | return 0; |
|
120 | 125 | } else if ((category < 0) || (category >= mDataModel.at(set)->count())) { |
|
121 | 126 | // No category, no value. |
|
122 | 127 | return 0; |
|
123 | 128 | } |
|
124 | 129 | |
|
125 | 130 | qreal value = mDataModel.at(set)->valueAt(category); |
|
126 | 131 | qreal total = categorySum(category); |
|
127 | 132 | if (0 == total) { |
|
128 | 133 | return 100.0; |
|
129 | 134 | } |
|
130 | 135 | |
|
131 | 136 | return value / total; |
|
132 | 137 | } |
|
133 | 138 | |
|
134 | 139 | |
|
135 | 140 | qreal BarChartModel::categorySum(int category) |
|
136 | 141 | { |
|
137 | 142 | qreal sum(0); |
|
138 | 143 | int count = mDataModel.count(); // Count sets |
|
139 | 144 | |
|
140 | 145 | for (int set = 0; set < count; set++) { |
|
141 | 146 | if (category < mDataModel.at(set)->count()) { |
|
142 | 147 | sum += mDataModel.at(set)->valueAt(category); |
|
143 | 148 | } |
|
144 | 149 | } |
|
145 | 150 | return sum; |
|
146 | 151 | } |
|
147 | 152 | |
|
148 | 153 | qreal BarChartModel::maxCategorySum() |
|
149 | 154 | { |
|
150 | 155 | qreal max = INT_MIN; |
|
151 | 156 | int count = categoryCount(); |
|
152 | 157 | |
|
153 | 158 | for (int col=0; col<count; col++) { |
|
154 | 159 | qreal sum = categorySum(col); |
|
155 | 160 | if (sum > max) { |
|
156 | 161 | max = sum; |
|
157 | 162 | } |
|
158 | 163 | } |
|
159 | 164 | return max; |
|
160 | 165 | } |
|
161 | 166 | |
|
162 | 167 | QString BarChartModel::categoryName(int category) |
|
163 | 168 | { |
|
164 | 169 | return mCategory.at(category); |
|
165 | 170 | } |
|
166 | 171 | |
|
167 | 172 | #include "moc_barchartmodel_p.cpp" |
|
168 | 173 | |
|
169 | 174 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,59 +1,60 | |||
|
1 | 1 | #ifndef BARCHARTMODEL_H |
|
2 | 2 | #define BARCHARTMODEL_H |
|
3 | 3 | |
|
4 | 4 | #include <QObject> |
|
5 | 5 | #include <QStringList> |
|
6 | 6 | #include "qchartglobal.h" |
|
7 | 7 | #include <qseries.h> |
|
8 | 8 | |
|
9 | 9 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
10 | 10 | |
|
11 | 11 | // Model for bar chart. Internal class. |
|
12 | 12 | // TODO: Implement as QAbstractItemModel? |
|
13 | 13 | |
|
14 | 14 | class QBarSet; |
|
15 | 15 | |
|
16 | 16 | class BarChartModel : public QObject //, public QAbstractItemModel |
|
17 | 17 | { |
|
18 | 18 | Q_OBJECT |
|
19 | 19 | public: |
|
20 | 20 | explicit BarChartModel(QStringList categories, QObject *parent = 0); |
|
21 | 21 | |
|
22 | 22 | QStringList category(); |
|
23 | 23 | void addBarSet(QBarSet *set); |
|
24 | 24 | void removeBarSet(QBarSet *set); |
|
25 | 25 | void insertBarSet(int i, QBarSet *set); |
|
26 | 26 | void insertCategory(int i, QString category); |
|
27 | void removeCategory(int i); | |
|
27 | 28 | QBarSet *setAt(int index); |
|
28 | 29 | QList<QBarSet*> barSets(); |
|
29 | 30 | |
|
30 | 31 | int barsetCount(); // Number of sets in model |
|
31 | 32 | int categoryCount(); // Number of categories |
|
32 | 33 | |
|
33 | 34 | qreal max(); // Maximum value of all sets |
|
34 | 35 | qreal min(); // Minimum value of all sets |
|
35 | 36 | qreal valueAt(int set, int category); |
|
36 | 37 | qreal percentageAt(int set, int category); |
|
37 | 38 | |
|
38 | 39 | qreal categorySum(int category); |
|
39 | 40 | qreal maxCategorySum(); // returns maximum sum of sets in all categories. |
|
40 | 41 | |
|
41 | 42 | QString categoryName(int category); |
|
42 | 43 | |
|
43 | 44 | signals: |
|
44 | 45 | void modelUpdated(); |
|
45 | 46 | |
|
46 | 47 | public slots: |
|
47 | 48 | |
|
48 | 49 | private: |
|
49 | 50 | |
|
50 | 51 | QList<QBarSet*> mDataModel; |
|
51 | 52 | QStringList mCategory; |
|
52 | 53 | |
|
53 | 54 | int mCurrentSet; |
|
54 | 55 | |
|
55 | 56 | }; |
|
56 | 57 | |
|
57 | 58 | QTCOMMERCIALCHART_END_NAMESPACE |
|
58 | 59 | |
|
59 | 60 | #endif // BARCHARTMODEL_H |
@@ -1,358 +1,368 | |||
|
1 | 1 | #include <QDebug> |
|
2 | 2 | #include "qbarseries.h" |
|
3 | 3 | #include "qbarset.h" |
|
4 | 4 | #include "barchartmodel_p.h" |
|
5 | 5 | |
|
6 | 6 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
7 | 7 | |
|
8 | 8 | /*! |
|
9 | 9 | \class QBarSeries |
|
10 | 10 | \brief part of QtCommercial chart API. |
|
11 | 11 | |
|
12 | 12 | QBarSeries represents a series of data shown as bars. One QBarSeries can contain multible |
|
13 | 13 | QBarSet data sets. QBarSeries groups the data from sets to categories, which are defined |
|
14 | 14 | by QStringList. |
|
15 | 15 | |
|
16 | 16 | \mainclass |
|
17 | 17 | |
|
18 | 18 | \sa QBarSet, QStackedBarSeries, QPercentBarSeries |
|
19 | 19 | */ |
|
20 | 20 | |
|
21 | 21 | /*! |
|
22 | 22 | \fn virtual QSeriesType QBarSeries::type() const |
|
23 | 23 | \brief Returns type of series. |
|
24 | 24 | \sa QSeries, QSeriesType |
|
25 | 25 | */ |
|
26 | 26 | |
|
27 | 27 | /*! |
|
28 | 28 | \fn void QBarSeries::showToolTip(QPoint pos, QString tip) |
|
29 | 29 | \brief \internal \a pos \a tip |
|
30 | 30 | */ |
|
31 | 31 | |
|
32 | 32 | /*! |
|
33 | 33 | Constructs empty QBarSeries. Parameter \a categories defines the categories for chart. |
|
34 | 34 | QBarSeries is QObject which is a child of a \a parent. |
|
35 | 35 | */ |
|
36 | 36 | QBarSeries::QBarSeries(QStringList categories, QObject *parent) |
|
37 | 37 | : QSeries(parent) |
|
38 | 38 | ,mModel(new BarChartModel(categories, this)) |
|
39 | 39 | { |
|
40 | 40 | m_model = NULL; |
|
41 | 41 | m_mapCategories = -1; |
|
42 | 42 | m_mapBarBottom = -1; |
|
43 | 43 | m_mapBarTop = -1; |
|
44 | 44 | m_mapOrientation = Qt::Vertical; |
|
45 | 45 | } |
|
46 | 46 | |
|
47 | 47 | /*! |
|
48 | 48 | Adds a set of bars to series. Takes ownership of \a set. |
|
49 | 49 | Connects the clicked(QString) and rightClicked(QString) signals |
|
50 | 50 | of \a set to this series |
|
51 | 51 | */ |
|
52 | 52 | void QBarSeries::addBarSet(QBarSet *set) |
|
53 | 53 | { |
|
54 | 54 | mModel->addBarSet(set); |
|
55 | 55 | connect(set,SIGNAL(clicked(QString)),this,SLOT(barsetClicked(QString))); |
|
56 | 56 | connect(set,SIGNAL(rightClicked(QString)),this,SLOT(barsetRightClicked(QString))); |
|
57 | 57 | connect(set, SIGNAL(valueChanged()), this, SLOT(barsetChanged())); |
|
58 | 58 | } |
|
59 | 59 | |
|
60 | 60 | /*! |
|
61 | 61 | Removes a set of bars from series. Releases ownership of \a set. Doesnt delete \a set. |
|
62 | 62 | Disconnects the clicked(QString) and rightClicked(QString) signals |
|
63 | 63 | of \a set from this series |
|
64 | 64 | */ |
|
65 | 65 | void QBarSeries::removeBarSet(QBarSet *set) |
|
66 | 66 | { |
|
67 | 67 | disconnect(set,SIGNAL(clicked(QString)),this,SLOT(barsetClicked(QString))); |
|
68 | 68 | disconnect(set,SIGNAL(rightClicked(QString)),this,SLOT(barsetRightClicked(QString))); |
|
69 | 69 | mModel->removeBarSet(set); |
|
70 | 70 | } |
|
71 | 71 | |
|
72 | 72 | void QBarSeries::insertBarSet(int i, QBarSet *set) |
|
73 | 73 | { |
|
74 | 74 | mModel->insertBarSet(i, set); |
|
75 | 75 | // emit barsetChanged(); |
|
76 | 76 | } |
|
77 | 77 | |
|
78 | 78 | void QBarSeries::insertCategory(int i, QString category) |
|
79 | 79 | { |
|
80 | 80 | mModel->insertCategory(i, category); |
|
81 | 81 | } |
|
82 | 82 | |
|
83 | void QBarSeries::removeCategory(int i) | |
|
84 | { | |
|
85 | mModel->removeCategory(i); | |
|
86 | } | |
|
87 | ||
|
83 | 88 | /*! |
|
84 | 89 | Returns number of sets in series. |
|
85 | 90 | */ |
|
86 | 91 | int QBarSeries::barsetCount() |
|
87 | 92 | { |
|
88 | 93 | // if(m_model) |
|
89 | 94 | // return m_mapBarTop - m_mapBarBottom; |
|
90 | 95 | // else |
|
91 | 96 | return mModel->barsetCount(); |
|
92 | 97 | } |
|
93 | 98 | |
|
94 | 99 | /*! |
|
95 | 100 | Returns number of categories in series |
|
96 | 101 | */ |
|
97 | 102 | int QBarSeries::categoryCount() |
|
98 | 103 | { |
|
99 | 104 | return mModel->categoryCount(); |
|
100 | 105 | } |
|
101 | 106 | |
|
102 | 107 | /*! |
|
103 | 108 | Returns a list of sets in series. Keeps ownership of sets. |
|
104 | 109 | */ |
|
105 | 110 | QList<QBarSet*> QBarSeries::barSets() |
|
106 | 111 | { |
|
107 | 112 | return mModel->barSets(); |
|
108 | 113 | } |
|
109 | 114 | |
|
110 | 115 | /*! |
|
111 | 116 | \internal \a index |
|
112 | 117 | */ |
|
113 | 118 | QBarSet* QBarSeries::barsetAt(int index) |
|
114 | 119 | { |
|
115 | 120 | return mModel->setAt(index); |
|
116 | 121 | } |
|
117 | 122 | |
|
118 | 123 | /*! |
|
119 | 124 | \internal \a category |
|
120 | 125 | */ |
|
121 | 126 | QString QBarSeries::categoryName(int category) |
|
122 | 127 | { |
|
123 | 128 | return mModel->categoryName(category); |
|
124 | 129 | } |
|
125 | 130 | |
|
126 | 131 | /*! |
|
127 | 132 | Enables or disables tooltip depending on parameter \a enabled. |
|
128 | 133 | Tooltip shows the name of set, when mouse is hovering on top of bar. |
|
129 | 134 | Calling without parameter \a enabled, enables the tooltip |
|
130 | 135 | */ |
|
131 | 136 | void QBarSeries::setToolTipEnabled(bool enabled) |
|
132 | 137 | { |
|
133 | 138 | // TODO: what if we add sets after call to this function? Those sets won't have tooltip enabled. |
|
134 | 139 | if (enabled) { |
|
135 | 140 | for (int i=0; i<mModel->barsetCount(); i++) { |
|
136 | 141 | QBarSet *set = mModel->setAt(i); |
|
137 | 142 | connect(set,SIGNAL(showToolTip(QPoint,QString)),this,SIGNAL(showToolTip(QPoint,QString))); |
|
138 | 143 | } |
|
139 | 144 | } else { |
|
140 | 145 | for (int i=0; i<mModel->barsetCount(); i++) { |
|
141 | 146 | QBarSet *set = mModel->setAt(i); |
|
142 | 147 | disconnect(set,SIGNAL(showToolTip(QPoint,QString)),this,SIGNAL(showToolTip(QPoint,QString))); |
|
143 | 148 | } |
|
144 | 149 | } |
|
145 | 150 | } |
|
146 | 151 | |
|
147 | 152 | |
|
148 | 153 | /*! |
|
149 | 154 | \internal \a category |
|
150 | 155 | */ |
|
151 | 156 | void QBarSeries::barsetClicked(QString category) |
|
152 | 157 | { |
|
153 | 158 | emit clicked(qobject_cast<QBarSet*>(sender()), category); |
|
154 | 159 | } |
|
155 | 160 | |
|
156 | 161 | /*! |
|
157 | 162 | \internal \a category |
|
158 | 163 | */ |
|
159 | 164 | void QBarSeries::barsetRightClicked(QString category) |
|
160 | 165 | { |
|
161 | 166 | emit rightClicked(qobject_cast<QBarSet*>(sender()), category); |
|
162 | 167 | } |
|
163 | 168 | |
|
164 | 169 | |
|
165 | 170 | /*! |
|
166 | 171 | \internal |
|
167 | 172 | */ |
|
168 | 173 | qreal QBarSeries::min() |
|
169 | 174 | { |
|
170 | 175 | return mModel->min(); |
|
171 | 176 | } |
|
172 | 177 | |
|
173 | 178 | /*! |
|
174 | 179 | \internal |
|
175 | 180 | */ |
|
176 | 181 | qreal QBarSeries::max() |
|
177 | 182 | { |
|
178 | 183 | return mModel->max(); |
|
179 | 184 | } |
|
180 | 185 | |
|
181 | 186 | /*! |
|
182 | 187 | \internal \a set \a category |
|
183 | 188 | */ |
|
184 | 189 | qreal QBarSeries::valueAt(int set, int category) |
|
185 | 190 | { |
|
186 | 191 | return mModel->valueAt(set,category); |
|
187 | 192 | } |
|
188 | 193 | |
|
189 | 194 | /*! |
|
190 | 195 | \internal \a set \a category |
|
191 | 196 | */ |
|
192 | 197 | qreal QBarSeries::percentageAt(int set, int category) |
|
193 | 198 | { |
|
194 | 199 | return mModel->percentageAt(set,category); |
|
195 | 200 | } |
|
196 | 201 | |
|
197 | 202 | /*! |
|
198 | 203 | \internal \a category |
|
199 | 204 | */ |
|
200 | 205 | qreal QBarSeries::categorySum(int category) |
|
201 | 206 | { |
|
202 | 207 | return mModel->categorySum(category); |
|
203 | 208 | } |
|
204 | 209 | |
|
205 | 210 | /*! |
|
206 | 211 | \internal |
|
207 | 212 | */ |
|
208 | 213 | qreal QBarSeries::maxCategorySum() |
|
209 | 214 | { |
|
210 | 215 | return mModel->maxCategorySum(); |
|
211 | 216 | } |
|
212 | 217 | |
|
213 | 218 | /*! |
|
214 | 219 | \internal |
|
215 | 220 | */ |
|
216 | 221 | BarChartModel& QBarSeries::model() |
|
217 | 222 | { |
|
218 | 223 | return *mModel; |
|
219 | 224 | } |
|
220 | 225 | |
|
221 | 226 | bool QBarSeries::setModel(QAbstractItemModel* model) |
|
222 | 227 | { |
|
223 | 228 | // disconnect signals from old model |
|
224 | 229 | if(m_model) |
|
225 | 230 | { |
|
226 | 231 | disconnect(m_model, 0, this, 0); |
|
227 | 232 | m_mapCategories = -1; |
|
228 | 233 | m_mapBarBottom = -1; |
|
229 | 234 | m_mapBarTop = -1; |
|
230 | 235 | m_mapOrientation = Qt::Vertical; |
|
231 | 236 | } |
|
232 | 237 | |
|
233 | 238 | // set new model |
|
234 | 239 | if(model) |
|
235 | 240 | { |
|
236 | 241 | m_model = model; |
|
237 | 242 | return true; |
|
238 | 243 | } |
|
239 | 244 | else |
|
240 | 245 | { |
|
241 | 246 | m_model = NULL; |
|
242 | 247 | return false; |
|
243 | 248 | } |
|
244 | 249 | } |
|
245 | 250 | |
|
246 | 251 | // TODO |
|
247 | 252 | void QBarSeries::setModelMapping(int categories, int bottomBoundry, int topBoundry, Qt::Orientation orientation) |
|
248 | 253 | { |
|
249 | 254 | if (m_model == NULL) |
|
250 | 255 | return; |
|
251 | 256 | m_mapCategories = categories; |
|
252 | 257 | m_mapBarBottom = bottomBoundry; |
|
253 | 258 | m_mapBarTop = topBoundry; |
|
254 | 259 | m_mapOrientation = orientation; |
|
255 | 260 | |
|
256 | 261 | // connect the signals |
|
257 | 262 | if (m_mapOrientation == Qt::Vertical) |
|
258 | 263 | { |
|
259 | 264 | connect(m_model,SIGNAL(dataChanged(QModelIndex,QModelIndex)), this, SLOT(modelUpdated(QModelIndex, QModelIndex))); |
|
260 | 265 | connect(m_model,SIGNAL(rowsInserted(QModelIndex, int, int)), this, SLOT(modelDataAdded(QModelIndex,int,int))); |
|
261 | 266 | connect(m_model, SIGNAL(rowsRemoved(QModelIndex, int, int)), this, SLOT(modelDataRemoved(QModelIndex,int,int))); |
|
262 | 267 | } |
|
263 | 268 | else |
|
264 | 269 | { |
|
265 | 270 | connect(m_model,SIGNAL(dataChanged(QModelIndex,QModelIndex)), this, SLOT(modelUpdated(QModelIndex, QModelIndex))); |
|
266 | 271 | connect(m_model,SIGNAL(columnsInserted(QModelIndex, int, int)), this, SLOT(modelDataAdded(QModelIndex,int,int))); |
|
267 | 272 | connect(m_model, SIGNAL(columnsRemoved(QModelIndex, int, int)), this, SLOT(modelDataRemoved(QModelIndex,int,int))); |
|
268 | 273 | } |
|
269 | 274 | |
|
270 | 275 | |
|
271 | 276 | // create the initial bars |
|
272 | 277 | delete mModel; |
|
273 | 278 | if (m_mapOrientation == Qt::Vertical) |
|
274 | 279 | { |
|
275 | 280 | QStringList categories; |
|
276 | 281 | for (int k = 0; k < m_model->rowCount(); k++) |
|
277 | 282 | categories << m_model->data(m_model->index(k, m_mapCategories), Qt::DisplayRole).toString(); |
|
278 | 283 | mModel = new BarChartModel(categories, this); |
|
279 | 284 | |
|
280 | 285 | for (int i = m_mapBarBottom; i <= m_mapBarTop; i++) |
|
281 | 286 | { |
|
282 | 287 | QBarSet* barSet = new QBarSet(QString("Column: %1").arg(i + 1)); |
|
283 | 288 | for(int m = 0; m < m_model->rowCount(); m++) |
|
284 | 289 | *barSet << m_model->data(m_model->index(m, i), Qt::DisplayRole).toDouble(); |
|
285 | 290 | addBarSet(barSet); |
|
286 | 291 | } |
|
287 | 292 | } |
|
288 | 293 | else |
|
289 | 294 | { |
|
290 | 295 | QStringList categories; |
|
291 | 296 | for (int k = 0; k < m_model->columnCount(); k++) |
|
292 | 297 | categories << m_model->data(m_model->index(m_mapCategories, k), Qt::DisplayRole).toString(); |
|
293 | 298 | mModel = new BarChartModel(categories, this); |
|
294 | 299 | |
|
295 | 300 | for (int i = m_mapBarBottom; i <= m_mapBarTop; i++) |
|
296 | 301 | { |
|
297 | 302 | QBarSet* barSet = new QBarSet(QString("Row: %1").arg(i + 1)); |
|
298 | 303 | for(int m = 0; m < m_model->columnCount(); m++) |
|
299 | 304 | *barSet << m_model->data(m_model->index(i, m), Qt::DisplayRole).toDouble(); |
|
300 | 305 | addBarSet(barSet); |
|
301 | 306 | } |
|
302 | 307 | } |
|
303 | 308 | } |
|
304 | 309 | |
|
305 | 310 | void QBarSeries::modelUpdated(QModelIndex topLeft, QModelIndex bottomRight) |
|
306 | 311 | { |
|
307 | 312 | Q_UNUSED(bottomRight) |
|
308 | 313 | |
|
309 | 314 | if (m_mapOrientation == Qt::Vertical) |
|
310 | 315 | { |
|
311 | 316 | if (topLeft.column() >= m_mapBarBottom && topLeft.column() <= m_mapBarTop) |
|
312 | 317 | barsetAt(topLeft.column() - m_mapBarBottom)->setValue(topLeft.row(), m_model->data(topLeft, Qt::DisplayRole).toDouble()); |
|
313 | 318 | // else if (topLeft.column() == m_mapCategories) |
|
314 | 319 | // slices().at(topLeft.row())->setLabel(m_model->data(topLeft, Qt::DisplayRole).toString()); |
|
315 | 320 | } |
|
316 | 321 | else |
|
317 | 322 | { |
|
318 | 323 | if (topLeft.row() >= m_mapBarBottom && topLeft.row() <= m_mapBarTop) |
|
319 | 324 | barsetAt(topLeft.row() - m_mapBarBottom)->setValue(topLeft.column(), m_model->data(topLeft, Qt::DisplayRole).toDouble()); |
|
320 | 325 | // else if (topLeft.row() == m_mapCategories) |
|
321 | 326 | // slices().at(topLeft.column())->setLabel(m_model->data(topLeft, Qt::DisplayRole).toString()); |
|
322 | 327 | } |
|
323 | 328 | } |
|
324 | 329 | |
|
325 | 330 | void QBarSeries::modelDataAdded(QModelIndex /*parent*/, int start, int /*end*/) |
|
326 | 331 | { |
|
327 | 332 | if (m_mapOrientation == Qt::Vertical) |
|
328 | 333 | { |
|
329 | 334 | insertCategory(start, QString("Row: %1").arg(start + 1)); |
|
330 | 335 | for (int i = 0; i <= m_mapBarTop - m_mapBarBottom; i++) |
|
331 | 336 | { |
|
332 | 337 | barsetAt(i)->insertValue(start, m_model->data(m_model->index(start, i), Qt::DisplayRole).toDouble()); |
|
333 | 338 | } |
|
334 | 339 | } |
|
335 | 340 | else |
|
336 | 341 | { |
|
337 | 342 | insertCategory(start, QString("Column: %1").arg(start + 1)); |
|
338 | 343 | for (int i = 0; i <= m_mapBarTop - m_mapBarBottom; i++) |
|
339 | 344 | { |
|
340 | 345 | barsetAt(i)->insertValue(start, m_model->data(m_model->index(i, start), Qt::DisplayRole).toDouble()); |
|
341 | 346 | } |
|
342 | 347 | } |
|
343 | 348 | emit restructuredBar(1); |
|
344 | 349 | } |
|
345 | 350 | |
|
346 |
void QBarSeries::modelDataRemoved(QModelIndex /*parent*/, int |
|
|
351 | void QBarSeries::modelDataRemoved(QModelIndex /*parent*/, int start, int /*end*/) | |
|
347 | 352 | { |
|
348 | // | |
|
353 | removeCategory(start); | |
|
354 | for (int i = 0; i <= m_mapBarTop - m_mapBarBottom; i++) | |
|
355 | { | |
|
356 | barsetAt(i)->removeValue(start); | |
|
357 | } | |
|
358 | emit restructuredBar(1); | |
|
349 | 359 | } |
|
350 | 360 | |
|
351 | 361 | void QBarSeries::barsetChanged() |
|
352 | 362 | { |
|
353 | 363 | emit updatedBars(); |
|
354 | 364 | } |
|
355 | 365 | |
|
356 | 366 | #include "moc_qbarseries.cpp" |
|
357 | 367 | |
|
358 | 368 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,90 +1,91 | |||
|
1 | 1 | #ifndef BARSERIES_H |
|
2 | 2 | #define BARSERIES_H |
|
3 | 3 | |
|
4 | 4 | #include "qseries.h" |
|
5 | 5 | #include <QStringList> |
|
6 | 6 | |
|
7 | 7 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
8 | 8 | |
|
9 | 9 | class QBarSet; |
|
10 | 10 | class BarChartModel; |
|
11 | 11 | class BarCategory; |
|
12 | 12 | |
|
13 | 13 | // Container for series |
|
14 | 14 | class QTCOMMERCIALCHART_EXPORT QBarSeries : public QSeries |
|
15 | 15 | { |
|
16 | 16 | Q_OBJECT |
|
17 | 17 | public: |
|
18 | 18 | QBarSeries(QStringList categories, QObject* parent=0); |
|
19 | 19 | |
|
20 | 20 | virtual QSeriesType type() const { return QSeries::SeriesTypeBar; } |
|
21 | 21 | |
|
22 | 22 | void addBarSet(QBarSet *set); // Takes ownership of set |
|
23 | 23 | void removeBarSet(QBarSet *set); // Releases ownership, doesn't delete set |
|
24 | 24 | void insertBarSet(int i, QBarSet *set); |
|
25 | 25 | void insertCategory(int i, QString category); |
|
26 | void removeCategory(int i); | |
|
26 | 27 | int barsetCount(); |
|
27 | 28 | int categoryCount(); |
|
28 | 29 | QList<QBarSet*> barSets(); |
|
29 | 30 | |
|
30 | 31 | bool setModel(QAbstractItemModel* model); |
|
31 | 32 | QAbstractItemModel* modelExt() {return m_model;} |
|
32 | 33 | void setModelMapping(int categories, int bottomBoundry, int topBoundry, Qt::Orientation orientation = Qt::Vertical); |
|
33 | 34 | |
|
34 | 35 | public: |
|
35 | 36 | // TODO: Functions below this are not part of api and will be moved |
|
36 | 37 | // to private implementation, when we start using it |
|
37 | 38 | // TODO: TO PIMPL ---> |
|
38 | 39 | QBarSet* barsetAt(int index); |
|
39 | 40 | QString categoryName(int category); |
|
40 | 41 | qreal min(); |
|
41 | 42 | qreal max(); |
|
42 | 43 | qreal valueAt(int set, int category); |
|
43 | 44 | qreal percentageAt(int set, int category); |
|
44 | 45 | qreal categorySum(int category); |
|
45 | 46 | qreal maxCategorySum(); |
|
46 | 47 | BarChartModel& model(); |
|
47 | 48 | // <--- TO PIMPL |
|
48 | 49 | |
|
49 | 50 | signals: |
|
50 | 51 | //void changed(int index); |
|
51 | 52 | void clicked(QBarSet* barset, QString category); // Up to user of api, what to do with these signals |
|
52 | 53 | void rightClicked(QBarSet* barset, QString category); |
|
53 | 54 | |
|
54 | 55 | // |
|
55 | 56 | void updatedBars(); |
|
56 | 57 | void restructuredBar(int); |
|
57 | 58 | |
|
58 | 59 | // TODO: internal signals, these to private implementation. |
|
59 | 60 | // TODO: TO PIMPL ---> |
|
60 | 61 | void showToolTip(QPoint pos, QString tip); |
|
61 | 62 | // <--- TO PIMPL |
|
62 | 63 | |
|
63 | 64 | public Q_SLOTS: |
|
64 | 65 | void setToolTipEnabled(bool enabled=true); // enables tooltips |
|
65 | 66 | |
|
66 | 67 | // TODO: TO PIMPL ---> |
|
67 | 68 | void barsetClicked(QString category); |
|
68 | 69 | void barsetRightClicked(QString category); |
|
69 | 70 | // <--- TO PIMPL |
|
70 | 71 | |
|
71 | 72 | private Q_SLOTS: |
|
72 | 73 | // slots for updating bars when data in model changes |
|
73 | 74 | void modelUpdated(QModelIndex topLeft, QModelIndex bottomRight); |
|
74 | 75 | void modelDataAdded(QModelIndex parent, int start, int end); |
|
75 | 76 | void modelDataRemoved(QModelIndex parent, int start, int end); |
|
76 | 77 | void barsetChanged(); |
|
77 | 78 | |
|
78 | 79 | protected: |
|
79 | 80 | BarChartModel* mModel; |
|
80 | 81 | |
|
81 | 82 | // QAbstractItemModel* m_model; |
|
82 | 83 | int m_mapCategories; |
|
83 | 84 | int m_mapBarBottom; |
|
84 | 85 | int m_mapBarTop; |
|
85 | 86 | Qt::Orientation m_mapOrientation; |
|
86 | 87 | }; |
|
87 | 88 | |
|
88 | 89 | QTCOMMERCIALCHART_END_NAMESPACE |
|
89 | 90 | |
|
90 | 91 | #endif // BARSERIES_H |
@@ -1,200 +1,205 | |||
|
1 | 1 | #include "qbarset.h" |
|
2 | 2 | #include <QDebug> |
|
3 | 3 | #include <QToolTip> |
|
4 | 4 | |
|
5 | 5 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
6 | 6 | |
|
7 | 7 | /*! |
|
8 | 8 | \class QBarSet |
|
9 | 9 | \brief part of QtCommercial chart API. |
|
10 | 10 | |
|
11 | 11 | QBarSet represents one set of bars. Set of bars contains one data value for each category. |
|
12 | 12 | First value of set is assumed to belong to first category, second to second category and so on. |
|
13 | 13 | If set has fewer values than there are categories, then the missing values are assumed to be |
|
14 | 14 | at the end of set. For missing values in middle of a set, numerical value of zero is used. |
|
15 | 15 | |
|
16 | 16 | \mainclass |
|
17 | 17 | |
|
18 | 18 | \sa QBarSeries, QStackedBarSeries, QPercentBarSeries |
|
19 | 19 | */ |
|
20 | 20 | |
|
21 | 21 | /*! |
|
22 | 22 | \fn void QBarSet::clicked(QString category) |
|
23 | 23 | \brief signals that set has been clicked |
|
24 | 24 | Parameter \a category describes on which category was clicked |
|
25 | 25 | */ |
|
26 | 26 | |
|
27 | 27 | /*! |
|
28 | 28 | \fn void QBarSet::rightClicked(QString category) |
|
29 | 29 | \brief signals that set has been clicked with right mouse button |
|
30 | 30 | Parameter \a category describes on which category was clicked |
|
31 | 31 | */ |
|
32 | 32 | |
|
33 | 33 | /*! |
|
34 | 34 | \fn void QBarSet::hoverEnter(QPoint pos) |
|
35 | 35 | \brief signals that mouse has entered over the set at position \a pos. |
|
36 | 36 | */ |
|
37 | 37 | |
|
38 | 38 | /*! |
|
39 | 39 | \fn void QBarSet::hoverLeave() |
|
40 | 40 | \brief signals that mouse has left from the set. |
|
41 | 41 | */ |
|
42 | 42 | |
|
43 | 43 | /*! |
|
44 | 44 | \fn void QBarSet::toggleFloatingValues() |
|
45 | 45 | \brief \internal |
|
46 | 46 | */ |
|
47 | 47 | |
|
48 | 48 | /*! |
|
49 | 49 | \fn void QBarSet::showToolTip(QPoint pos, QString tip) |
|
50 | 50 | \brief \internal \a pos \a tip |
|
51 | 51 | */ |
|
52 | 52 | |
|
53 | 53 | |
|
54 | 54 | /*! |
|
55 | 55 | Constructs QBarSet with a name of \a name and with parent of \a parent |
|
56 | 56 | */ |
|
57 | 57 | QBarSet::QBarSet(QString name, QObject *parent) |
|
58 | 58 | : QObject(parent) |
|
59 | 59 | ,mName(name) |
|
60 | 60 | { |
|
61 | 61 | } |
|
62 | 62 | |
|
63 | 63 | /*! |
|
64 | 64 | Sets new \a name for set. |
|
65 | 65 | */ |
|
66 | 66 | void QBarSet::setName(QString name) |
|
67 | 67 | { |
|
68 | 68 | mName = name; |
|
69 | 69 | } |
|
70 | 70 | |
|
71 | 71 | /*! |
|
72 | 72 | Returns name of the set. |
|
73 | 73 | */ |
|
74 | 74 | QString QBarSet::name() |
|
75 | 75 | { |
|
76 | 76 | return mName; |
|
77 | 77 | } |
|
78 | 78 | |
|
79 | 79 | /*! |
|
80 | 80 | Appends new value \a value to the end of set. |
|
81 | 81 | */ |
|
82 | 82 | QBarSet& QBarSet::operator << (const qreal &value) |
|
83 | 83 | { |
|
84 | 84 | mValues.append(value); |
|
85 | 85 | emit structureChanged(); |
|
86 | 86 | return *this; |
|
87 | 87 | } |
|
88 | 88 | |
|
89 | 89 | void QBarSet::insertValue(int i, qreal value) |
|
90 | 90 | { |
|
91 | 91 | mValues.insert(i, value); |
|
92 | 92 | } |
|
93 | 93 | |
|
94 | void QBarSet::removeValue(int i) | |
|
95 | { | |
|
96 | mValues.removeAt(i); | |
|
97 | } | |
|
98 | ||
|
94 | 99 | /*! |
|
95 | 100 | Returns count of values in set. |
|
96 | 101 | */ |
|
97 | 102 | int QBarSet::count() |
|
98 | 103 | { |
|
99 | 104 | return mValues.count(); |
|
100 | 105 | } |
|
101 | 106 | |
|
102 | 107 | /*! |
|
103 | 108 | Returns value of set indexed by \a index |
|
104 | 109 | */ |
|
105 | 110 | qreal QBarSet::valueAt(int index) |
|
106 | 111 | { |
|
107 | 112 | return mValues.at(index); |
|
108 | 113 | } |
|
109 | 114 | |
|
110 | 115 | /*! |
|
111 | 116 | Sets a new value \a value to set, indexed by \a index |
|
112 | 117 | */ |
|
113 | 118 | void QBarSet::setValue(int index, qreal value) |
|
114 | 119 | { |
|
115 | 120 | mValues.replace(index,value); |
|
116 | 121 | emit valueChanged(); |
|
117 | 122 | } |
|
118 | 123 | |
|
119 | 124 | /*! |
|
120 | 125 | Returns total sum of all values in barset. |
|
121 | 126 | */ |
|
122 | 127 | qreal QBarSet::total() |
|
123 | 128 | { |
|
124 | 129 | qreal total(0); |
|
125 | 130 | for (int i=0; i<mValues.count(); i++) { |
|
126 | 131 | total += mValues.at(i); |
|
127 | 132 | } |
|
128 | 133 | return total; |
|
129 | 134 | } |
|
130 | 135 | |
|
131 | 136 | /*! |
|
132 | 137 | Sets pen for set. Bars of this set are drawn using \a pen |
|
133 | 138 | */ |
|
134 | 139 | void QBarSet::setPen(const QPen pen) |
|
135 | 140 | { |
|
136 | 141 | mPen = pen; |
|
137 | 142 | emit valueChanged(); |
|
138 | 143 | } |
|
139 | 144 | |
|
140 | 145 | /*! |
|
141 | 146 | Returns pen of the set. |
|
142 | 147 | */ |
|
143 | 148 | QPen QBarSet::pen() const |
|
144 | 149 | { |
|
145 | 150 | return mPen; |
|
146 | 151 | } |
|
147 | 152 | |
|
148 | 153 | /*! |
|
149 | 154 | Sets brush for the set. Bars of this set are drawn using \a brush |
|
150 | 155 | */ |
|
151 | 156 | void QBarSet::setBrush(const QBrush brush) |
|
152 | 157 | { |
|
153 | 158 | mBrush = brush; |
|
154 | 159 | emit valueChanged(); |
|
155 | 160 | } |
|
156 | 161 | |
|
157 | 162 | /*! |
|
158 | 163 | Returns brush of the set. |
|
159 | 164 | */ |
|
160 | 165 | QBrush QBarSet::brush() const |
|
161 | 166 | { |
|
162 | 167 | return mBrush; |
|
163 | 168 | } |
|
164 | 169 | |
|
165 | 170 | /*! |
|
166 | 171 | Sets the pen for floating values that are drawn on top of this set |
|
167 | 172 | */ |
|
168 | 173 | void QBarSet::setFloatingValuePen(const QPen pen) |
|
169 | 174 | { |
|
170 | 175 | mFloatingValuePen = pen; |
|
171 | 176 | } |
|
172 | 177 | |
|
173 | 178 | /*! |
|
174 | 179 | Returns the pen for floating values that are drawn on top of this set |
|
175 | 180 | */ |
|
176 | 181 | QPen QBarSet::floatingValuePen() const |
|
177 | 182 | { |
|
178 | 183 | return mFloatingValuePen; |
|
179 | 184 | } |
|
180 | 185 | |
|
181 | 186 | /*! |
|
182 | 187 | \internal \a pos |
|
183 | 188 | */ |
|
184 | 189 | void QBarSet::barHoverEnterEvent(QPoint pos) |
|
185 | 190 | { |
|
186 | 191 | emit showToolTip(pos, mName); |
|
187 | 192 | emit hoverEnter(pos); |
|
188 | 193 | } |
|
189 | 194 | |
|
190 | 195 | /*! |
|
191 | 196 | \internal |
|
192 | 197 | */ |
|
193 | 198 | void QBarSet::barHoverLeaveEvent() |
|
194 | 199 | { |
|
195 | 200 | // Emit signal to user of charts |
|
196 | 201 | emit hoverLeave(); |
|
197 | 202 | } |
|
198 | 203 | |
|
199 | 204 | #include "moc_qbarset.cpp" |
|
200 | 205 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,73 +1,74 | |||
|
1 | 1 | #ifndef QBARSET_H |
|
2 | 2 | #define QBARSET_H |
|
3 | 3 | |
|
4 | 4 | #include <qchartglobal.h> |
|
5 | 5 | #include <QPen> |
|
6 | 6 | #include <QBrush> |
|
7 | 7 | |
|
8 | 8 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
9 | 9 | |
|
10 | 10 | class QTCOMMERCIALCHART_EXPORT QBarSet : public QObject |
|
11 | 11 | { |
|
12 | 12 | Q_OBJECT |
|
13 | 13 | public: |
|
14 | 14 | QBarSet(QString name, QObject *parent = 0); |
|
15 | 15 | |
|
16 | 16 | void setName(QString name); |
|
17 | 17 | QString name(); |
|
18 | 18 | QBarSet& operator << (const qreal &value); // appends new value to set |
|
19 | 19 | void insertValue(int i, qreal value); |
|
20 | void removeValue(int i); | |
|
20 | 21 | |
|
21 | 22 | // TODO: remove indices eventually. Use as internal? |
|
22 | 23 | int count(); // count of values in set |
|
23 | 24 | qreal valueAt(int index); // for modifying individual values |
|
24 | 25 | void setValue(int index, qreal value); // setter for individual value |
|
25 | 26 | qreal total(); // total values in the set |
|
26 | 27 | |
|
27 | 28 | // TODO: |
|
28 | 29 | //qreal value(QString category); |
|
29 | 30 | //void setValue(QString category, qreal value); |
|
30 | 31 | |
|
31 | 32 | void setPen(const QPen pen); |
|
32 | 33 | QPen pen() const; |
|
33 | 34 | |
|
34 | 35 | void setBrush(const QBrush brush); |
|
35 | 36 | QBrush brush() const; |
|
36 | 37 | |
|
37 | 38 | void setFloatingValuePen(const QPen pen); |
|
38 | 39 | QPen floatingValuePen() const; |
|
39 | 40 | |
|
40 | 41 | Q_SIGNALS: |
|
41 | 42 | void clicked(QString category); // Clicked and hover signals exposed to user |
|
42 | 43 | void rightClicked(QString category); |
|
43 | 44 | void toggleFloatingValues(); |
|
44 | 45 | |
|
45 | 46 | // TODO: Expose this to user or not? |
|
46 | 47 | // TODO: TO PIMPL ---> |
|
47 | 48 | void structureChanged(); |
|
48 | 49 | void valueChanged(); |
|
49 | 50 | void hoverEnter(QPoint pos); |
|
50 | 51 | void hoverLeave(); |
|
51 | 52 | void showToolTip(QPoint pos, QString tip); // Private signal |
|
52 | 53 | // <--- TO PIMPL |
|
53 | 54 | |
|
54 | 55 | public Q_SLOTS: |
|
55 | 56 | // These are for internal communication |
|
56 | 57 | // TODO: TO PIMPL ---> |
|
57 | 58 | void barHoverEnterEvent(QPoint pos); |
|
58 | 59 | void barHoverLeaveEvent(); |
|
59 | 60 | // <--- TO PIMPL |
|
60 | 61 | |
|
61 | 62 | private: |
|
62 | 63 | |
|
63 | 64 | QString mName; |
|
64 | 65 | QList<qreal> mValues; // TODO: replace with map (category, value) |
|
65 | 66 | QMap<QString,qreal> mMappedValues; |
|
66 | 67 | QPen mPen; |
|
67 | 68 | QBrush mBrush; |
|
68 | 69 | QPen mFloatingValuePen; |
|
69 | 70 | }; |
|
70 | 71 | |
|
71 | 72 | QTCOMMERCIALCHART_END_NAMESPACE |
|
72 | 73 | |
|
73 | 74 | #endif // QBARSET_H |
General Comments 0
You need to be logged in to leave comments.
Login now