##// END OF EJS Templates
Adding data to BarSet through model added
Marek Rosa -
r662:c9f8f8ef7809
parent child
Show More
@@ -1,159 +1,169
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 void BarChartModel::insertBarSet(int i, QBarSet *set)
33 {
34 mDataModel.insert(i, set);
35 }
36
37 void BarChartModel::insertCategory(int i, QString category)
38 {
39 mCategory.insert(i, category);
40 }
41
32 42 QBarSet* BarChartModel::setAt(int index)
33 43 {
34 44 return mDataModel.at(index);
35 45 }
36 46
37 47 QList<QBarSet*> BarChartModel::barSets()
38 48 {
39 49 return mDataModel;
40 50 }
41 51
42 52 int BarChartModel::barsetCount()
43 53 {
44 54 return mDataModel.count();
45 55 }
46 56
47 57 int BarChartModel::categoryCount()
48 58 {
49 59 return mCategory.count();
50 60 }
51 61
52 62 qreal BarChartModel::min()
53 63 {
54 64 Q_ASSERT(mDataModel.count() > 0);
55 65 // TODO: make min and max members and update them when data changes.
56 66 // This is slower since they are checked every time, even if data is same since previous call.
57 67 qreal min = INT_MAX;
58 68
59 69 for (int i=0; i <mDataModel.count(); i++) {
60 70 int itemCount = mDataModel.at(i)->count();
61 71 for (int j=0; j<itemCount; j++) {
62 72 qreal temp = mDataModel.at(i)->valueAt(j);
63 73 if (temp < min) {
64 74 min = temp;
65 75 }
66 76 }
67 77 }
68 78 return min;
69 79 }
70 80
71 81 qreal BarChartModel::max()
72 82 {
73 83 Q_ASSERT(mDataModel.count() > 0);
74 84
75 85 // TODO: make min and max members and update them when data changes.
76 86 // This is slower since they are checked every time, even if data is same since previous call.
77 87 qreal max = INT_MIN;
78 88
79 89 for (int i=0; i <mDataModel.count(); i++) {
80 90 int itemCount = mDataModel.at(i)->count();
81 91 for (int j=0; j<itemCount; j++) {
82 92 qreal temp = mDataModel.at(i)->valueAt(j);
83 93 if (temp > max) {
84 94 max = temp;
85 95 }
86 96 }
87 97 }
88 98
89 99 return max;
90 100 }
91 101
92 102 qreal BarChartModel::valueAt(int set, int category)
93 103 {
94 104 if ((set < 0) || (set >= mDataModel.count())) {
95 105 // No set, no value.
96 106 return 0;
97 107 } else if ((category < 0) || (category >= mDataModel.at(set)->count())) {
98 108 // No category, no value.
99 109 return 0;
100 110 }
101 111
102 112 return mDataModel.at(set)->valueAt(category);
103 113 }
104 114
105 115 qreal BarChartModel::percentageAt(int set, int category)
106 116 {
107 117 if ((set < 0) || (set >= mDataModel.count())) {
108 118 // No set, no value.
109 119 return 0;
110 120 } else if ((category < 0) || (category >= mDataModel.at(set)->count())) {
111 121 // No category, no value.
112 122 return 0;
113 123 }
114 124
115 125 qreal value = mDataModel.at(set)->valueAt(category);
116 126 qreal total = categorySum(category);
117 127 if (0 == total) {
118 128 return 100.0;
119 129 }
120 130
121 131 return value / total;
122 132 }
123 133
124 134
125 135 qreal BarChartModel::categorySum(int category)
126 136 {
127 137 qreal sum(0);
128 138 int count = mDataModel.count(); // Count sets
129 139
130 140 for (int set = 0; set < count; set++) {
131 141 if (category < mDataModel.at(set)->count()) {
132 142 sum += mDataModel.at(set)->valueAt(category);
133 143 }
134 144 }
135 145 return sum;
136 146 }
137 147
138 148 qreal BarChartModel::maxCategorySum()
139 149 {
140 150 qreal max = INT_MIN;
141 151 int count = categoryCount();
142 152
143 153 for (int col=0; col<count; col++) {
144 154 qreal sum = categorySum(col);
145 155 if (sum > max) {
146 156 max = sum;
147 157 }
148 158 }
149 159 return max;
150 160 }
151 161
152 162 QString BarChartModel::categoryName(int category)
153 163 {
154 164 return mCategory.at(category);
155 165 }
156 166
157 167 #include "moc_barchartmodel_p.cpp"
158 168
159 169 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,57 +1,59
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 void insertBarSet(int i, QBarSet *set);
26 void insertCategory(int i, QString category);
25 27 QBarSet *setAt(int index);
26 28 QList<QBarSet*> barSets();
27 29
28 30 int barsetCount(); // Number of sets in model
29 31 int categoryCount(); // Number of categories
30 32
31 33 qreal max(); // Maximum value of all sets
32 34 qreal min(); // Minimum value of all sets
33 35 qreal valueAt(int set, int category);
34 36 qreal percentageAt(int set, int category);
35 37
36 38 qreal categorySum(int category);
37 39 qreal maxCategorySum(); // returns maximum sum of sets in all categories.
38 40
39 41 QString categoryName(int category);
40 42
41 43 signals:
42 44 void modelUpdated();
43 45
44 46 public slots:
45 47
46 48 private:
47 49
48 50 QList<QBarSet*> mDataModel;
49 51 QStringList mCategory;
50 52
51 53 int mCurrentSet;
52 54
53 55 };
54 56
55 57 QTCOMMERCIALCHART_END_NAMESPACE
56 58
57 59 #endif // BARCHARTMODEL_H
@@ -1,88 +1,89
1 1 #include "barpresenter_p.h"
2 2 #include "bar_p.h"
3 3 #include "barvalue_p.h"
4 4 #include "qbarset.h"
5 5 #include <QDebug>
6 6
7 7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 8
9 9 BarPresenter::BarPresenter(QBarSeries *series, QChart *parent) :
10 10 BarPresenterBase(series, parent)
11 11 {
12 12 connect(series, SIGNAL(updatedBars()), this, SLOT(layoutChanged()));
13 connect(series, SIGNAL(restructuredBar(int)), this, SLOT(handleModelChanged(int)));
13 14 }
14 15
15 16 void BarPresenter::layoutChanged()
16 17 {
17 18 // Scale bars to new layout
18 19 // Layout for bars:
19 20 if (mSeries->barsetCount() <= 0) {
20 21 qDebug() << "No sets in model!";
21 22 return;
22 23 }
23 24
24 25 if (childItems().count() == 0) {
25 26 qDebug() << "WARNING: BarPresenter::layoutChanged called before graphics items are created!";
26 27 return;
27 28 }
28 29
29 30 // Use temporary qreals for accurancy (we might get some compiler warnings... :)
30 31 int categoryCount = mSeries->categoryCount();
31 32 int setCount = mSeries->barsetCount();
32 33
33 34 qreal tW = mWidth;
34 35 qreal tH = mHeight;
35 36 qreal tM = mSeries->max();
36 37 qreal scale = (tH/tM);
37 38 qreal tC = categoryCount + 1;
38 39 qreal categoryWidth = tW/tC;
39 40 mBarWidth = categoryWidth / (setCount+1);
40 41
41 42 int itemIndex(0);
42 43 for (int category=0; category < categoryCount; category++) {
43 44 qreal xPos = categoryWidth * category + categoryWidth /2 + mBarWidth/2;
44 45 qreal yPos = mHeight;
45 46 for (int set = 0; set < setCount; set++) {
46 47 qreal barHeight = mSeries->valueAt(set,category) * scale;
47 48 Bar* bar = mBars.at(itemIndex);
48 49
49 50 // TODO: width settable per bar?
50 51 bar->resize(mBarWidth, barHeight);
51 52 bar->setPen(mSeries->barsetAt(set)->pen());
52 53 bar->setBrush(mSeries->barsetAt(set)->brush());
53 54 bar->setPos(xPos, yPos-barHeight);
54 55 itemIndex++;
55 56 xPos += mBarWidth;
56 57 }
57 58 }
58 59
59 60 // Position floating values
60 61 itemIndex = 0;
61 62 for (int category=0; category < mSeries->categoryCount(); category++) {
62 63 qreal xPos = categoryWidth * category + categoryWidth/2 + mBarWidth;
63 64 qreal yPos = mHeight;
64 65 for (int set=0; set < mSeries->barsetCount(); set++) {
65 66 qreal barHeight = mSeries->valueAt(set,category) * scale;
66 67 BarValue* value = mFloatingValues.at(itemIndex);
67 68
68 69 QBarSet* barSet = mSeries->barsetAt(set);
69 70 value->resize(100,50); // TODO: proper layout for this.
70 71 value->setPos(xPos, yPos-barHeight/2);
71 72 value->setPen(barSet->floatingValuePen());
72 73
73 74 if (mSeries->valueAt(set,category) != 0) {
74 75 value->setValueString(QString::number(mSeries->valueAt(set,category)));
75 76 } else {
76 77 value->setValueString(QString(""));
77 78 }
78 79
79 80 itemIndex++;
80 81 xPos += mBarWidth;
81 82 }
82 83 }
83 84 update();
84 85 }
85 86
86 87 #include "moc_barpresenter_p.cpp"
87 88
88 89 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,331 +1,358
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 void QBarSeries::insertBarSet(int i, QBarSet *set)
73 {
74 mModel->insertBarSet(i, set);
75 // emit barsetChanged();
76 }
77
78 void QBarSeries::insertCategory(int i, QString category)
79 {
80 mModel->insertCategory(i, category);
81 }
82
72 83 /*!
73 84 Returns number of sets in series.
74 85 */
75 86 int QBarSeries::barsetCount()
76 87 {
77 // if(m_model)
78 // return m_mapBarTop - m_mapBarBottom;
79 // else
80 return mModel->barsetCount();
88 // if(m_model)
89 // return m_mapBarTop - m_mapBarBottom;
90 // else
91 return mModel->barsetCount();
81 92 }
82 93
83 94 /*!
84 95 Returns number of categories in series
85 96 */
86 97 int QBarSeries::categoryCount()
87 98 {
88 99 return mModel->categoryCount();
89 100 }
90 101
91 102 /*!
92 103 Returns a list of sets in series. Keeps ownership of sets.
93 104 */
94 105 QList<QBarSet*> QBarSeries::barSets()
95 106 {
96 107 return mModel->barSets();
97 108 }
98 109
99 110 /*!
100 111 \internal \a index
101 112 */
102 113 QBarSet* QBarSeries::barsetAt(int index)
103 114 {
104 115 return mModel->setAt(index);
105 116 }
106 117
107 118 /*!
108 119 \internal \a category
109 120 */
110 121 QString QBarSeries::categoryName(int category)
111 122 {
112 123 return mModel->categoryName(category);
113 124 }
114 125
115 126 /*!
116 127 Enables or disables tooltip depending on parameter \a enabled.
117 128 Tooltip shows the name of set, when mouse is hovering on top of bar.
118 129 Calling without parameter \a enabled, enables the tooltip
119 130 */
120 131 void QBarSeries::setToolTipEnabled(bool enabled)
121 132 {
122 133 // TODO: what if we add sets after call to this function? Those sets won't have tooltip enabled.
123 134 if (enabled) {
124 135 for (int i=0; i<mModel->barsetCount(); i++) {
125 136 QBarSet *set = mModel->setAt(i);
126 137 connect(set,SIGNAL(showToolTip(QPoint,QString)),this,SIGNAL(showToolTip(QPoint,QString)));
127 138 }
128 139 } else {
129 140 for (int i=0; i<mModel->barsetCount(); i++) {
130 141 QBarSet *set = mModel->setAt(i);
131 142 disconnect(set,SIGNAL(showToolTip(QPoint,QString)),this,SIGNAL(showToolTip(QPoint,QString)));
132 143 }
133 144 }
134 145 }
135 146
136 147
137 148 /*!
138 149 \internal \a category
139 150 */
140 151 void QBarSeries::barsetClicked(QString category)
141 152 {
142 153 emit clicked(qobject_cast<QBarSet*>(sender()), category);
143 154 }
144 155
145 156 /*!
146 157 \internal \a category
147 158 */
148 159 void QBarSeries::barsetRightClicked(QString category)
149 160 {
150 161 emit rightClicked(qobject_cast<QBarSet*>(sender()), category);
151 162 }
152 163
153 164
154 165 /*!
155 166 \internal
156 167 */
157 168 qreal QBarSeries::min()
158 169 {
159 170 return mModel->min();
160 171 }
161 172
162 173 /*!
163 174 \internal
164 175 */
165 176 qreal QBarSeries::max()
166 177 {
167 178 return mModel->max();
168 179 }
169 180
170 181 /*!
171 182 \internal \a set \a category
172 183 */
173 184 qreal QBarSeries::valueAt(int set, int category)
174 185 {
175 186 return mModel->valueAt(set,category);
176 187 }
177 188
178 189 /*!
179 190 \internal \a set \a category
180 191 */
181 192 qreal QBarSeries::percentageAt(int set, int category)
182 193 {
183 194 return mModel->percentageAt(set,category);
184 195 }
185 196
186 197 /*!
187 198 \internal \a category
188 199 */
189 200 qreal QBarSeries::categorySum(int category)
190 201 {
191 202 return mModel->categorySum(category);
192 203 }
193 204
194 205 /*!
195 206 \internal
196 207 */
197 208 qreal QBarSeries::maxCategorySum()
198 209 {
199 210 return mModel->maxCategorySum();
200 211 }
201 212
202 213 /*!
203 214 \internal
204 215 */
205 216 BarChartModel& QBarSeries::model()
206 217 {
207 218 return *mModel;
208 219 }
209 220
210 221 bool QBarSeries::setModel(QAbstractItemModel* model)
211 222 {
212 223 // disconnect signals from old model
213 224 if(m_model)
214 225 {
215 226 disconnect(m_model, 0, this, 0);
216 227 m_mapCategories = -1;
217 228 m_mapBarBottom = -1;
218 229 m_mapBarTop = -1;
219 230 m_mapOrientation = Qt::Vertical;
220 231 }
221 232
222 233 // set new model
223 234 if(model)
224 235 {
225 236 m_model = model;
226 237 return true;
227 238 }
228 239 else
229 240 {
230 241 m_model = NULL;
231 242 return false;
232 243 }
233 244 }
234 245
235 246 // TODO
236 247 void QBarSeries::setModelMapping(int categories, int bottomBoundry, int topBoundry, Qt::Orientation orientation)
237 248 {
238 249 if (m_model == NULL)
239 250 return;
240 251 m_mapCategories = categories;
241 252 m_mapBarBottom = bottomBoundry;
242 253 m_mapBarTop = topBoundry;
243 254 m_mapOrientation = orientation;
244 255
245 256 // connect the signals
246 257 if (m_mapOrientation == Qt::Vertical)
247 258 {
248 259 connect(m_model,SIGNAL(dataChanged(QModelIndex,QModelIndex)), this, SLOT(modelUpdated(QModelIndex, QModelIndex)));
249 260 connect(m_model,SIGNAL(rowsInserted(QModelIndex, int, int)), this, SLOT(modelDataAdded(QModelIndex,int,int)));
250 261 connect(m_model, SIGNAL(rowsRemoved(QModelIndex, int, int)), this, SLOT(modelDataRemoved(QModelIndex,int,int)));
251 262 }
252 263 else
253 264 {
254 265 connect(m_model,SIGNAL(dataChanged(QModelIndex,QModelIndex)), this, SLOT(modelUpdated(QModelIndex, QModelIndex)));
255 266 connect(m_model,SIGNAL(columnsInserted(QModelIndex, int, int)), this, SLOT(modelDataAdded(QModelIndex,int,int)));
256 267 connect(m_model, SIGNAL(columnsRemoved(QModelIndex, int, int)), this, SLOT(modelDataRemoved(QModelIndex,int,int)));
257 268 }
258 269
259 270
260 271 // create the initial bars
261 272 delete mModel;
262 273 if (m_mapOrientation == Qt::Vertical)
263 274 {
264 275 QStringList categories;
265 276 for (int k = 0; k < m_model->rowCount(); k++)
266 277 categories << m_model->data(m_model->index(k, m_mapCategories), Qt::DisplayRole).toString();
267 278 mModel = new BarChartModel(categories, this);
268 279
269 280 for (int i = m_mapBarBottom; i <= m_mapBarTop; i++)
270 281 {
271 282 QBarSet* barSet = new QBarSet(QString("Column: %1").arg(i + 1));
272 283 for(int m = 0; m < m_model->rowCount(); m++)
273 284 *barSet << m_model->data(m_model->index(m, i), Qt::DisplayRole).toDouble();
274 285 addBarSet(barSet);
275 286 }
276 287 }
277 288 else
278 289 {
279 290 QStringList categories;
280 291 for (int k = 0; k < m_model->columnCount(); k++)
281 292 categories << m_model->data(m_model->index(m_mapCategories, k), Qt::DisplayRole).toString();
282 293 mModel = new BarChartModel(categories, this);
283 294
284 295 for (int i = m_mapBarBottom; i <= m_mapBarTop; i++)
285 296 {
286 297 QBarSet* barSet = new QBarSet(QString("Row: %1").arg(i + 1));
287 298 for(int m = 0; m < m_model->columnCount(); m++)
288 299 *barSet << m_model->data(m_model->index(i, m), Qt::DisplayRole).toDouble();
289 300 addBarSet(barSet);
290 301 }
291 302 }
292 303 }
293 304
294 305 void QBarSeries::modelUpdated(QModelIndex topLeft, QModelIndex bottomRight)
295 306 {
296 307 Q_UNUSED(bottomRight)
297 308
298 309 if (m_mapOrientation == Qt::Vertical)
299 310 {
300 311 if (topLeft.column() >= m_mapBarBottom && topLeft.column() <= m_mapBarTop)
301 312 barsetAt(topLeft.column() - m_mapBarBottom)->setValue(topLeft.row(), m_model->data(topLeft, Qt::DisplayRole).toDouble());
302 // else if (topLeft.column() == m_mapCategories)
303 // slices().at(topLeft.row())->setLabel(m_model->data(topLeft, Qt::DisplayRole).toString());
313 // else if (topLeft.column() == m_mapCategories)
314 // slices().at(topLeft.row())->setLabel(m_model->data(topLeft, Qt::DisplayRole).toString());
304 315 }
305 316 else
306 317 {
307 318 if (topLeft.row() >= m_mapBarBottom && topLeft.row() <= m_mapBarTop)
308 319 barsetAt(topLeft.row() - m_mapBarBottom)->setValue(topLeft.column(), m_model->data(topLeft, Qt::DisplayRole).toDouble());
309 // else if (topLeft.row() == m_mapCategories)
310 // slices().at(topLeft.column())->setLabel(m_model->data(topLeft, Qt::DisplayRole).toString());
320 // else if (topLeft.row() == m_mapCategories)
321 // slices().at(topLeft.column())->setLabel(m_model->data(topLeft, Qt::DisplayRole).toString());
311 322 }
312 323 }
313 324
314 void QBarSeries::modelDataAdded(QModelIndex /*parent*/, int /*start*/, int /*end*/)
325 void QBarSeries::modelDataAdded(QModelIndex /*parent*/, int start, int /*end*/)
315 326 {
316 //
327 if (m_mapOrientation == Qt::Vertical)
328 {
329 insertCategory(start, QString("Row: %1").arg(start + 1));
330 for (int i = 0; i <= m_mapBarTop - m_mapBarBottom; i++)
331 {
332 barsetAt(i)->insertValue(start, m_model->data(m_model->index(start, i), Qt::DisplayRole).toDouble());
333 }
334 }
335 else
336 {
337 insertCategory(start, QString("Column: %1").arg(start + 1));
338 for (int i = 0; i <= m_mapBarTop - m_mapBarBottom; i++)
339 {
340 barsetAt(i)->insertValue(start, m_model->data(m_model->index(i, start), Qt::DisplayRole).toDouble());
341 }
342 }
343 emit restructuredBar(1);
317 344 }
318 345
319 346 void QBarSeries::modelDataRemoved(QModelIndex /*parent*/, int /*start*/, int /*end*/)
320 347 {
321 348 //
322 349 }
323 350
324 351 void QBarSeries::barsetChanged()
325 352 {
326 353 emit updatedBars();
327 354 }
328 355
329 356 #include "moc_qbarseries.cpp"
330 357
331 358 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,87 +1,90
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 void insertBarSet(int i, QBarSet *set);
25 void insertCategory(int i, QString category);
24 26 int barsetCount();
25 27 int categoryCount();
26 28 QList<QBarSet*> barSets();
27 29
28 30 bool setModel(QAbstractItemModel* model);
29 31 QAbstractItemModel* modelExt() {return m_model;}
30 32 void setModelMapping(int categories, int bottomBoundry, int topBoundry, Qt::Orientation orientation = Qt::Vertical);
31 33
32 34 public:
33 35 // TODO: Functions below this are not part of api and will be moved
34 36 // to private implementation, when we start using it
35 37 // TODO: TO PIMPL --->
36 38 QBarSet* barsetAt(int index);
37 39 QString categoryName(int category);
38 40 qreal min();
39 41 qreal max();
40 42 qreal valueAt(int set, int category);
41 43 qreal percentageAt(int set, int category);
42 44 qreal categorySum(int category);
43 45 qreal maxCategorySum();
44 46 BarChartModel& model();
45 47 // <--- TO PIMPL
46 48
47 49 signals:
48 50 //void changed(int index);
49 51 void clicked(QBarSet* barset, QString category); // Up to user of api, what to do with these signals
50 52 void rightClicked(QBarSet* barset, QString category);
51 53
52 54 //
53 55 void updatedBars();
56 void restructuredBar(int);
54 57
55 58 // TODO: internal signals, these to private implementation.
56 59 // TODO: TO PIMPL --->
57 60 void showToolTip(QPoint pos, QString tip);
58 61 // <--- TO PIMPL
59 62
60 63 public Q_SLOTS:
61 64 void setToolTipEnabled(bool enabled=true); // enables tooltips
62 65
63 66 // TODO: TO PIMPL --->
64 67 void barsetClicked(QString category);
65 68 void barsetRightClicked(QString category);
66 69 // <--- TO PIMPL
67 70
68 71 private Q_SLOTS:
69 72 // slots for updating bars when data in model changes
70 73 void modelUpdated(QModelIndex topLeft, QModelIndex bottomRight);
71 74 void modelDataAdded(QModelIndex parent, int start, int end);
72 75 void modelDataRemoved(QModelIndex parent, int start, int end);
73 76 void barsetChanged();
74 77
75 78 protected:
76 79 BarChartModel* mModel;
77 80
78 81 // QAbstractItemModel* m_model;
79 82 int m_mapCategories;
80 83 int m_mapBarBottom;
81 84 int m_mapBarTop;
82 85 Qt::Orientation m_mapOrientation;
83 86 };
84 87
85 88 QTCOMMERCIALCHART_END_NAMESPACE
86 89
87 90 #endif // BARSERIES_H
@@ -1,195 +1,200
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 void QBarSet::insertValue(int i, qreal value)
90 {
91 mValues.insert(i, value);
92 }
93
89 94 /*!
90 95 Returns count of values in set.
91 96 */
92 97 int QBarSet::count()
93 98 {
94 99 return mValues.count();
95 100 }
96 101
97 102 /*!
98 103 Returns value of set indexed by \a index
99 104 */
100 105 qreal QBarSet::valueAt(int index)
101 106 {
102 107 return mValues.at(index);
103 108 }
104 109
105 110 /*!
106 111 Sets a new value \a value to set, indexed by \a index
107 112 */
108 113 void QBarSet::setValue(int index, qreal value)
109 114 {
110 115 mValues.replace(index,value);
111 116 emit valueChanged();
112 117 }
113 118
114 119 /*!
115 120 Returns total sum of all values in barset.
116 121 */
117 122 qreal QBarSet::total()
118 123 {
119 124 qreal total(0);
120 125 for (int i=0; i<mValues.count(); i++) {
121 126 total += mValues.at(i);
122 127 }
123 128 return total;
124 129 }
125 130
126 131 /*!
127 132 Sets pen for set. Bars of this set are drawn using \a pen
128 133 */
129 134 void QBarSet::setPen(const QPen pen)
130 135 {
131 136 mPen = pen;
132 137 emit valueChanged();
133 138 }
134 139
135 140 /*!
136 141 Returns pen of the set.
137 142 */
138 143 QPen QBarSet::pen() const
139 144 {
140 145 return mPen;
141 146 }
142 147
143 148 /*!
144 149 Sets brush for the set. Bars of this set are drawn using \a brush
145 150 */
146 151 void QBarSet::setBrush(const QBrush brush)
147 152 {
148 153 mBrush = brush;
149 154 emit valueChanged();
150 155 }
151 156
152 157 /*!
153 158 Returns brush of the set.
154 159 */
155 160 QBrush QBarSet::brush() const
156 161 {
157 162 return mBrush;
158 163 }
159 164
160 165 /*!
161 166 Sets the pen for floating values that are drawn on top of this set
162 167 */
163 168 void QBarSet::setFloatingValuePen(const QPen pen)
164 169 {
165 170 mFloatingValuePen = pen;
166 171 }
167 172
168 173 /*!
169 174 Returns the pen for floating values that are drawn on top of this set
170 175 */
171 176 QPen QBarSet::floatingValuePen() const
172 177 {
173 178 return mFloatingValuePen;
174 179 }
175 180
176 181 /*!
177 182 \internal \a pos
178 183 */
179 184 void QBarSet::barHoverEnterEvent(QPoint pos)
180 185 {
181 186 emit showToolTip(pos, mName);
182 187 emit hoverEnter(pos);
183 188 }
184 189
185 190 /*!
186 191 \internal
187 192 */
188 193 void QBarSet::barHoverLeaveEvent()
189 194 {
190 195 // Emit signal to user of charts
191 196 emit hoverLeave();
192 197 }
193 198
194 199 #include "moc_qbarset.cpp"
195 200 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,72 +1,73
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 void insertValue(int i, qreal value);
19 20
20 21 // TODO: remove indices eventually. Use as internal?
21 22 int count(); // count of values in set
22 23 qreal valueAt(int index); // for modifying individual values
23 24 void setValue(int index, qreal value); // setter for individual value
24 25 qreal total(); // total values in the set
25 26
26 27 // TODO:
27 28 //qreal value(QString category);
28 29 //void setValue(QString category, qreal value);
29 30
30 31 void setPen(const QPen pen);
31 32 QPen pen() const;
32 33
33 34 void setBrush(const QBrush brush);
34 35 QBrush brush() const;
35 36
36 37 void setFloatingValuePen(const QPen pen);
37 38 QPen floatingValuePen() const;
38 39
39 40 Q_SIGNALS:
40 41 void clicked(QString category); // Clicked and hover signals exposed to user
41 42 void rightClicked(QString category);
42 43 void toggleFloatingValues();
43 44
44 45 // TODO: Expose this to user or not?
45 46 // TODO: TO PIMPL --->
46 47 void structureChanged();
47 48 void valueChanged();
48 49 void hoverEnter(QPoint pos);
49 50 void hoverLeave();
50 51 void showToolTip(QPoint pos, QString tip); // Private signal
51 52 // <--- TO PIMPL
52 53
53 54 public Q_SLOTS:
54 55 // These are for internal communication
55 56 // TODO: TO PIMPL --->
56 57 void barHoverEnterEvent(QPoint pos);
57 58 void barHoverLeaveEvent();
58 59 // <--- TO PIMPL
59 60
60 61 private:
61 62
62 63 QString mName;
63 64 QList<qreal> mValues; // TODO: replace with map (category, value)
64 65 QMap<QString,qreal> mMappedValues;
65 66 QPen mPen;
66 67 QBrush mBrush;
67 68 QPen mFloatingValuePen;
68 69 };
69 70
70 71 QTCOMMERCIALCHART_END_NAMESPACE
71 72
72 73 #endif // QBARSET_H
General Comments 0
You need to be logged in to leave comments. Login now