##// END OF EJS Templates
combined clicked and rightclicked signals in barchart
sauimone -
r812:2e82e33f659e
parent child
Show More
@@ -1,58 +1,54
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 "bar_p.h"
22 22 #include <QDebug>
23 23 #include <QPainter>
24 24 #include <QGraphicsSceneEvent>
25 25
26 26 QTCOMMERCIALCHART_BEGIN_NAMESPACE
27 27
28 28 Bar::Bar(QString category, QGraphicsItem *parent)
29 29 : QGraphicsRectItem(parent),
30 30 m_category(category)
31 31 {
32 32 setAcceptedMouseButtons(Qt::LeftButton | Qt::RightButton);
33 33 setAcceptHoverEvents(true);
34 34 }
35 35
36 36 void Bar::mousePressEvent(QGraphicsSceneMouseEvent *event)
37 37 {
38 if (event->button() == Qt::LeftButton) {
39 emit clicked(m_category);
40 } else if (event->button() == Qt::RightButton) {
41 emit rightClicked(m_category);
42 }
38 emit clicked(m_category, event->button());
43 39 }
44 40
45 41 void Bar::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
46 42 {
47 43 emit hoverEntered(event->lastScreenPos());
48 44 }
49 45
50 46 void Bar::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
51 47 {
52 48 Q_UNUSED(event)
53 49 emit hoverLeaved();
54 50 }
55 51
56 52 #include "moc_bar_p.cpp"
57 53
58 54 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,53 +1,52
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 BAR_H
22 22 #define BAR_H
23 23
24 24 #include "qchartglobal.h"
25 25 #include <QGraphicsRectItem>
26 26
27 27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28 28
29 29 // Single visual bar item of chart
30 30 class Bar : public QObject, public QGraphicsRectItem
31 31 {
32 32 Q_OBJECT
33 33 public:
34 34 Bar(QString category, QGraphicsItem *parent = 0);
35 35
36 36 public:
37 37 void mousePressEvent(QGraphicsSceneMouseEvent *event);
38 38 void hoverEnterEvent(QGraphicsSceneHoverEvent *event);
39 39 void hoverLeaveEvent(QGraphicsSceneHoverEvent *event);
40 40
41 41 Q_SIGNALS:
42 void clicked(QString category);
43 void rightClicked(QString category);
42 void clicked(QString category, Qt::MouseButtons button);
44 43 void hoverEntered(QPoint pos);
45 44 void hoverLeaved();
46 45
47 46 private:
48 47 QString m_category;
49 48 };
50 49
51 50 QTCOMMERCIALCHART_END_NAMESPACE
52 51
53 52 #endif // BAR_H
@@ -1,235 +1,234
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 "barchartitem_p.h"
22 22 #include "bar_p.h"
23 23 #include "barvalue_p.h"
24 24 #include "qbarset.h"
25 25 #include "qbarseries.h"
26 26 #include "qchart.h"
27 27 #include "qchartaxis.h"
28 28 #include "qchartaxiscategories.h"
29 29 #include "chartpresenter_p.h"
30 30 #include "chartanimator_p.h"
31 31 #include "chartdataset_p.h"
32 32 #include <QDebug>
33 33 #include <QToolTip>
34 34
35 35 QTCOMMERCIALCHART_BEGIN_NAMESPACE
36 36
37 37 BarChartItem::BarChartItem(QBarSeries *series, ChartPresenter *presenter) :
38 38 ChartItem(presenter),
39 39 m_layoutSet(false),
40 40 m_series(series)
41 41 {
42 42 connect(series, SIGNAL(showToolTip(QPoint,QString)), this, SLOT(showToolTip(QPoint,QString)));
43 43 connect(series, SIGNAL(updatedBars()), this, SLOT(handleLayoutChanged()));
44 44 //TODO: connect(series,SIGNAL("position or size has changed"), this, SLOT(handleLayoutChanged()));
45 45 connect(series, SIGNAL(restructuredBar(int)), this, SLOT(handleModelChanged(int)));
46 46 setZValue(ChartPresenter::BarSeriesZValue);
47 47 dataChanged();
48 48 }
49 49
50 50 BarChartItem::~BarChartItem()
51 51 {
52 52 disconnect(this,SLOT(showToolTip(QPoint,QString)));
53 53 }
54 54
55 55 void BarChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
56 56 {
57 57 if (!m_layoutSet) {
58 58 qWarning() << "BarChartItem::paint called without layout set. Aborting.";
59 59 return;
60 60 }
61 61
62 62 foreach(QGraphicsItem* i, childItems())
63 63 i->paint(painter,option,widget);
64 64 }
65 65
66 66 QRectF BarChartItem::boundingRect() const
67 67 {
68 68 return m_rect;
69 69 }
70 70
71 71 void BarChartItem::dataChanged()
72 72 {
73 73 // TODO: performance optimizations. Do we really need to delete and create items every time data is changed or can we reuse them?
74 74 // Delete old bars
75 75 foreach (QGraphicsItem *item, childItems())
76 76 delete item;
77 77
78 78 m_bars.clear();
79 79 m_floatingValues.clear();
80 80 m_layout.clear();
81 81
82 82 // Create new graphic items for bars
83 83 for (int c = 0; c < m_series->categoryCount(); c++) {
84 84 QString category = m_series->categoryName(c);
85 85 for (int s = 0; s < m_series->barsetCount(); s++) {
86 86 QBarSet *set = m_series->barsetAt(s);
87 87 Bar *bar = new Bar(category,this);
88 88 childItems().append(bar);
89 89 m_bars.append(bar);
90 connect(bar, SIGNAL(clicked(QString)), set, SIGNAL(clicked(QString)));
91 connect(bar, SIGNAL(rightClicked(QString)), set, SIGNAL(rightClicked(QString)));
90 connect(bar, SIGNAL(clicked(QString,Qt::MouseButtons)), set, SIGNAL(clicked(QString,Qt::MouseButtons)));
92 91 connect(bar, SIGNAL(hoverEntered(QPoint)), set, SLOT(barHoverEnterEvent(QPoint)));
93 92 connect(bar, SIGNAL(hoverLeaved()), set, SLOT(barHoverLeaveEvent()));
94 93 m_layout.append(QRectF(0, 0, 0, 0));
95 94 }
96 95 }
97 96
98 97 // Create floating values
99 98 for (int category = 0; category < m_series->categoryCount(); category++) {
100 99 for (int s = 0; s < m_series->barsetCount(); s++) {
101 100 QBarSet *set = m_series->barsetAt(s);
102 101 BarValue *value = new BarValue(*set, this);
103 102 childItems().append(value);
104 103 m_floatingValues.append(value);
105 104 connect(set, SIGNAL(toggleFloatingValues()), value, SLOT(toggleVisible()));
106 105 }
107 106 }
108 107 }
109 108
110 109 QVector<QRectF> BarChartItem::calculateLayout()
111 110 {
112 111 QVector<QRectF> layout;
113 112
114 113 // Use temporary qreals for accurancy (we might get some compiler warnings... :)
115 114 qreal categoryCount = m_series->categoryCount();
116 115 qreal setCount = m_series->barsetCount();
117 116
118 117 qreal width = geometry().width();
119 118 qreal height = geometry().height();
120 119
121 120 qreal max = m_series->max();
122 121
123 122 // Domain:
124 123 if (m_domainMaxY > max) {
125 124 max = m_domainMaxY;
126 125 }
127 126
128 127 qreal scale = (height / max);
129 128 qreal categoryWidth = width / categoryCount;
130 129 qreal barWidth = categoryWidth / (setCount+1);
131 130
132 131 int itemIndex(0);
133 132 for (int category = 0; category < categoryCount; category++) {
134 133 qreal xPos = categoryWidth * category + barWidth / 2;
135 134 qreal yPos = height;
136 135 for (int set = 0; set < setCount; set++) {
137 136 qreal barHeight = m_series->valueAt(set, category) * scale;
138 137 Bar* bar = m_bars.at(itemIndex);
139 138
140 139 QRectF rect(xPos, yPos - barHeight, barWidth, barHeight);
141 140 layout.append(rect);
142 141 bar->setPen(m_series->barsetAt(set)->pen());
143 142 bar->setBrush(m_series->barsetAt(set)->brush());
144 143 itemIndex++;
145 144 xPos += barWidth;
146 145 }
147 146 }
148 147
149 148 // Position floating values
150 149 itemIndex = 0;
151 150 for (int category = 0; category < m_series->categoryCount(); category++) {
152 151 qreal xPos = categoryWidth * category + barWidth;
153 152 qreal yPos = height;
154 153 for (int set=0; set < m_series->barsetCount(); set++) {
155 154 qreal barHeight = m_series->valueAt(set, category) * scale;
156 155 BarValue* value = m_floatingValues.at(itemIndex);
157 156
158 157 QBarSet* barSet = m_series->barsetAt(set);
159 158
160 159 if (!qFuzzyIsNull(m_series->valueAt(set,category))) {
161 160 value->setText(QString::number(m_series->valueAt(set, category)));
162 161 } else {
163 162 value->setText(QString(""));
164 163 }
165 164
166 165 value->setPos(xPos, yPos-barHeight / 2);
167 166 value->setPen(barSet->floatingValuePen());
168 167
169 168 itemIndex++;
170 169 xPos += barWidth;
171 170 }
172 171 }
173 172
174 173 return layout;
175 174 }
176 175
177 176 void BarChartItem::applyLayout(const QVector<QRectF> &layout)
178 177 {
179 178 if (animator())
180 179 animator()->updateLayout(this, m_layout, layout);
181 180 else
182 181 setLayout(layout);
183 182 }
184 183
185 184 void BarChartItem::setLayout(const QVector<QRectF> &layout)
186 185 {
187 186 m_layout = layout;
188 187
189 188 for (int i=0; i < m_bars.count(); i++)
190 189 m_bars.at(i)->setRect(layout.at(i));
191 190
192 191 update();
193 192 }
194 193
195 194 //handlers
196 195
197 196 void BarChartItem::handleModelChanged(int index)
198 197 {
199 198 Q_UNUSED(index)
200 199 dataChanged();
201 200 }
202 201
203 202 void BarChartItem::handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY)
204 203 {
205 204 m_domainMinX = minX;
206 205 m_domainMaxX = maxX;
207 206 m_domainMinY = minY;
208 207 m_domainMaxY = maxY;
209 208 handleLayoutChanged();
210 209 }
211 210
212 211 void BarChartItem::handleGeometryChanged(const QRectF &rect)
213 212 {
214 213 m_rect = rect;
215 214 handleLayoutChanged();
216 215 m_layoutSet = true;
217 216 setPos(rect.topLeft());
218 217 }
219 218
220 219 void BarChartItem::handleLayoutChanged()
221 220 {
222 221 QVector<QRectF> layout = calculateLayout();
223 222 applyLayout(layout);
224 223 update();
225 224 }
226 225
227 226 void BarChartItem::showToolTip(QPoint pos, QString tip)
228 227 {
229 228 // TODO: cool tooltip instead of default
230 229 QToolTip::showText(pos, tip);
231 230 }
232 231
233 232 #include "moc_barchartitem_p.cpp"
234 233
235 234 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,407 +1,396
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 25
26 26 QTCOMMERCIALCHART_BEGIN_NAMESPACE
27 27
28 28 /*!
29 29 \class QBarSeries
30 30 \brief part of QtCommercial chart API.
31 31
32 32 QBarSeries represents a series of data shown as bars. One QBarSeries can contain multible
33 33 QBarSet data sets. QBarSeries groups the data from sets to categories, which are defined
34 34 by QStringList.
35 35
36 36 \mainclass
37 37
38 38 \sa QBarSet, QStackedBarSeries, QPercentBarSeries
39 39 */
40 40
41 41 /*!
42 42 \fn virtual QSeriesType QBarSeries::type() const
43 43 \brief Returns type of series.
44 44 \sa QSeries, QSeriesType
45 45 */
46 46
47 47 /*!
48 48 \fn void QBarSeries::showToolTip(QPoint pos, QString tip)
49 49 \brief \internal \a pos \a tip
50 50 */
51 51
52 52 /*!
53 53 Constructs empty QBarSeries. Parameter \a categories defines the categories for chart.
54 54 QBarSeries is QObject which is a child of a \a parent.
55 55 */
56 56 QBarSeries::QBarSeries(QBarCategories categories, QObject *parent) : QSeries(parent),
57 57 m_internalModel(new BarChartModel(categories, this))
58 58 {
59 59 m_model = 0;
60 60 m_mapCategories = -1;
61 61 m_mapBarBottom = -1;
62 62 m_mapBarTop = -1;
63 63 m_mapFirst = 0;
64 64 m_mapCount = 0;
65 65 m_mapOrientation = Qt::Vertical;
66 66 }
67 67
68 68 /*!
69 69 Adds a set of bars to series. Takes ownership of \a set.
70 Connects the clicked(QString) and rightClicked(QString) signals
70 Connects the clicked(QString, Qt::MouseButtons) signal
71 71 of \a set to this series
72 72 */
73 73 void QBarSeries::appendBarSet(QBarSet *set)
74 74 {
75 75 m_internalModel->appendBarSet(set);
76 connect(set, SIGNAL(clicked(QString)), this, SLOT(barsetClicked(QString)));
77 connect(set, SIGNAL(rightClicked(QString)), this, SLOT(barsetRightClicked(QString)));
76 connect(set, SIGNAL(clicked(QString,Qt::MouseButtons)), this, SLOT(barsetClicked(QString,Qt::MouseButtons)));
78 77 connect(set, SIGNAL(valueChanged()), this, SLOT(barsetChanged()));
79 78 emit updatedBars();
80 79 }
81 80
82 81 /*!
83 82 Removes a set of bars from series. Releases ownership of \a set. Doesnt delete \a set.
84 Disconnects the clicked(QString) and rightClicked(QString) signals
83 Disconnects the clicked(QString, Qt::MouseButtons) signal
85 84 of \a set from this series
86 85 */
87 86 void QBarSeries::removeBarSet(QBarSet *set)
88 87 {
89 disconnect(set, SIGNAL(clicked(QString)), this, SLOT(barsetClicked(QString)));
90 disconnect(set, SIGNAL(rightClicked(QString)), this, SLOT(barsetRightClicked(QString)));
88 disconnect(set, SIGNAL(clicked(QString,Qt::MouseButtons)), this, SLOT(barsetClicked(QString,Qt::MouseButtons)));
91 89 m_internalModel->removeBarSet(set);
92 90 emit updatedBars();
93 91 }
94 92
95 93 void QBarSeries::insertBarSet(int i, QBarSet *set)
96 94 {
97 95 m_internalModel->insertBarSet(i, set);
98 96 // emit barsetChanged();
99 97 }
100 98
101 99 void QBarSeries::insertCategory(int i, QString category)
102 100 {
103 101 m_internalModel->insertCategory(i, category);
104 102 }
105 103
106 104 void QBarSeries::removeCategory(int i)
107 105 {
108 106 m_internalModel->removeCategory(i);
109 107 }
110 108
111 109 /*!
112 110 Returns number of sets in series.
113 111 */
114 112 int QBarSeries::barsetCount() const
115 113 {
116 114 // if(m_model)
117 115 // return m_mapBarTop - m_mapBarBottom;
118 116 // else
119 117 return m_internalModel->barsetCount();
120 118 }
121 119
122 120 /*!
123 121 Returns number of categories in series
124 122 */
125 123 int QBarSeries::categoryCount() const
126 124 {
127 125 return m_internalModel->categoryCount();
128 126 }
129 127
130 128 /*!
131 129 Returns a list of sets in series. Keeps ownership of sets.
132 130 */
133 131 QList<QBarSet*> QBarSeries::barSets() const
134 132 {
135 133 return m_internalModel->barSets();
136 134 }
137 135
138 136 /*!
139 137 \internal \a index
140 138 */
141 139 QBarSet* QBarSeries::barsetAt(int index)
142 140 {
143 141 return m_internalModel->barsetAt(index);
144 142 }
145 143
146 144 /*!
147 145 \internal \a category
148 146 */
149 147 QString QBarSeries::categoryName(int category)
150 148 {
151 149 return m_internalModel->categoryName(category);
152 150 }
153 151
154 152 /*!
155 153 Enables or disables tooltip depending on parameter \a enabled.
156 154 Tooltip shows the name of set, when mouse is hovering on top of bar.
157 155 Calling without parameter \a enabled, enables the tooltip
158 156 */
159 157 void QBarSeries::setToolTipEnabled(bool enabled)
160 158 {
161 159 // TODO: what if we add sets after call to this function? Those sets won't have tooltip enabled.
162 160 if (enabled) {
163 161 for (int i=0; i<m_internalModel->barsetCount(); i++) {
164 162 QBarSet *set = m_internalModel->barsetAt(i);
165 163 connect(set, SIGNAL(showToolTip(QPoint,QString)), this, SIGNAL(showToolTip(QPoint,QString)));
166 164 }
167 165 } else {
168 166 for (int i=0; i<m_internalModel->barsetCount(); i++) {
169 167 QBarSet *set = m_internalModel->barsetAt(i);
170 168 disconnect(set, SIGNAL(showToolTip(QPoint,QString)), this, SIGNAL(showToolTip(QPoint,QString)));
171 169 }
172 170 }
173 171 }
174 172
175 173
176 174 /*!
177 175 \internal \a category
178 176 */
179 void QBarSeries::barsetClicked(QString category)
177 void QBarSeries::barsetClicked(QString category, Qt::MouseButtons button)
180 178 {
181 emit clicked(qobject_cast<QBarSet*>(sender()), category);
179 emit clicked(qobject_cast<QBarSet*>(sender()), category, button);
182 180 }
183 181
184 182 /*!
185 \internal \a category
186 */
187 void QBarSeries::barsetRightClicked(QString category)
188 {
189 emit rightClicked(qobject_cast<QBarSet*>(sender()), category);
190 }
191
192
193 /*!
194 183 \internal
195 184 */
196 185 qreal QBarSeries::min()
197 186 {
198 187 return m_internalModel->min();
199 188 }
200 189
201 190 /*!
202 191 \internal
203 192 */
204 193 qreal QBarSeries::max()
205 194 {
206 195 return m_internalModel->max();
207 196 }
208 197
209 198 /*!
210 199 \internal \a set \a category
211 200 */
212 201 qreal QBarSeries::valueAt(int set, int category)
213 202 {
214 203 return m_internalModel->valueAt(set, category);
215 204 }
216 205
217 206 /*!
218 207 \internal \a set \a category
219 208 */
220 209 qreal QBarSeries::percentageAt(int set, int category)
221 210 {
222 211 return m_internalModel->percentageAt(set, category);
223 212 }
224 213
225 214 /*!
226 215 \internal \a category
227 216 */
228 217 qreal QBarSeries::categorySum(int category)
229 218 {
230 219 return m_internalModel->categorySum(category);
231 220 }
232 221
233 222 /*!
234 223 \internal
235 224 */
236 225 qreal QBarSeries::maxCategorySum()
237 226 {
238 227 return m_internalModel->maxCategorySum();
239 228 }
240 229
241 230 /*!
242 231 \internal
243 232 */
244 233 BarChartModel& QBarSeries::model()
245 234 {
246 235 return *m_internalModel;
247 236 }
248 237
249 238 bool QBarSeries::setModel(QAbstractItemModel *model)
250 239 {
251 240 // disconnect signals from old model
252 241 if(m_model)
253 242 {
254 243 disconnect(m_model, 0, this, 0);
255 244 m_mapCategories = -1;
256 245 m_mapBarBottom = -1;
257 246 m_mapBarTop = -1;
258 247 m_mapFirst = 0;
259 248 m_mapCount = 0;
260 249 m_mapOrientation = Qt::Vertical;
261 250 }
262 251
263 252 // set new model
264 253 if(model)
265 254 {
266 255 m_model = model;
267 256 return true;
268 257 }
269 258 else
270 259 {
271 260 m_model = 0;
272 261 return false;
273 262 }
274 263 }
275 264
276 265 // TODO
277 266 void QBarSeries::setModelMapping(int categories, int bottomBoundry, int topBoundry, Qt::Orientation orientation)
278 267 {
279 268 if (!m_model)
280 269 return;
281 270
282 271 m_mapCategories = categories;
283 272 m_mapBarBottom = bottomBoundry;
284 273 m_mapBarTop = topBoundry;
285 274 // m_mapFirst = 1;
286 275 m_mapOrientation = orientation;
287 276
288 277 // connect the signals
289 278 if (m_mapOrientation == Qt::Vertical) {
290 279 m_mapCount = m_model->rowCount() - m_mapFirst;
291 280 connect(m_model,SIGNAL(dataChanged(QModelIndex,QModelIndex)),
292 281 this, SLOT(modelUpdated(QModelIndex, QModelIndex)));
293 282 connect(m_model,SIGNAL(rowsInserted(QModelIndex, int, int)),
294 283 this, SLOT(modelDataAdded(QModelIndex,int,int)));
295 284 connect(m_model, SIGNAL(rowsRemoved(QModelIndex, int, int)),
296 285 this, SLOT(modelDataRemoved(QModelIndex,int,int)));
297 286 } else {
298 287 m_mapCount = m_model->columnCount() - m_mapFirst;
299 288 connect(m_model,SIGNAL(dataChanged(QModelIndex,QModelIndex)),
300 289 this, SLOT(modelUpdated(QModelIndex, QModelIndex)));
301 290 connect(m_model,SIGNAL(columnsInserted(QModelIndex, int, int)),
302 291 this, SLOT(modelDataAdded(QModelIndex,int,int)));
303 292 connect(m_model, SIGNAL(columnsRemoved(QModelIndex, int, int)),
304 293 this, SLOT(modelDataRemoved(QModelIndex,int,int)));
305 294 }
306 295
307 296
308 297 // create the initial bars
309 298 delete m_internalModel;
310 299 if (m_mapOrientation == Qt::Vertical) {
311 300 QStringList categories;
312 301 for (int k = m_mapFirst; k < m_mapFirst + m_mapCount; k++)
313 302 categories << m_model->data(m_model->index(k, m_mapCategories), Qt::DisplayRole).toString();
314 303 m_internalModel = new BarChartModel(categories, this);
315 304
316 305 for (int i = m_mapBarBottom; i <= m_mapBarTop; i++) {
317 306 QBarSet* barSet = new QBarSet(QString("Column: %1").arg(i + 1));
318 307 for(int m = m_mapFirst; m < m_mapFirst + m_mapCount; m++)
319 308 *barSet << m_model->data(m_model->index(m, i), Qt::DisplayRole).toDouble();
320 309 appendBarSet(barSet);
321 310 }
322 311 } else {
323 312 QStringList categories;
324 313 for (int k = m_mapFirst; k < m_mapFirst + m_mapCount; k++)
325 314 categories << m_model->data(m_model->index(m_mapCategories, k), Qt::DisplayRole).toString();
326 315 m_internalModel = new BarChartModel(categories, this);
327 316
328 317 for (int i = m_mapBarBottom; i <= m_mapBarTop; i++) {
329 318 QBarSet* barSet = new QBarSet(QString("Row: %1").arg(i + 1));
330 319 for(int m = m_mapFirst; m < m_mapFirst + m_mapCount; m++)
331 320 *barSet << m_model->data(m_model->index(i, m), Qt::DisplayRole).toDouble();
332 321 appendBarSet(barSet);
333 322 }
334 323 }
335 324 }
336 325
337 326 void QBarSeries::setModelMappingShift(int first, int count)
338 327 {
339 328 m_mapFirst = first;
340 329 m_mapCount = count;
341 330 }
342 331
343 332 void QBarSeries::modelUpdated(QModelIndex topLeft, QModelIndex bottomRight)
344 333 {
345 334 Q_UNUSED(bottomRight)
346 335
347 336 if (m_mapOrientation == Qt::Vertical)
348 337 {
349 338 // model update is relevant to BarSeries if the change was made to the part of the model that was mapped to BarSeries
350 339 if (topLeft.column() >= m_mapBarBottom && topLeft.column() <= m_mapBarTop && topLeft.row() >= m_mapFirst && topLeft.row() < m_mapFirst + m_mapCount)
351 340 barsetAt(topLeft.column() - m_mapBarBottom)->setValue(topLeft.row() - m_mapFirst, m_model->data(topLeft, Qt::DisplayRole).toDouble());
352 341 }
353 342 else
354 343 {
355 344 // model update is relevant to BarSeries if the change was made to the part of the model that was mapped to BarSeries
356 345 if (topLeft.row() >= m_mapBarBottom && topLeft.row() <= m_mapBarTop && topLeft.column() >= m_mapFirst && topLeft.column() < m_mapFirst + m_mapCount)
357 346 barsetAt(topLeft.row() - m_mapBarBottom)->setValue(topLeft.column() - m_mapFirst, m_model->data(topLeft, Qt::DisplayRole).toDouble());
358 347 }
359 348 }
360 349
361 350 void QBarSeries::modelDataAdded(QModelIndex /*parent*/, int start, int /*end*/)
362 351 {
363 352 if (m_mapOrientation == Qt::Vertical) {
364 353 insertCategory(start - m_mapFirst, QString("Row: %1").arg(start + 1));
365 354 for (int i = 0; i <= m_mapBarTop - m_mapBarBottom; i++) {
366 355 barsetAt(i)->insertValue(start - m_mapFirst, m_model->data(m_model->index(start, i), Qt::DisplayRole).toDouble());
367 356 }
368 357 } else {
369 358 insertCategory(start - m_mapFirst, QString("Column: %1").arg(start + 1));
370 359 for (int i = 0; i <= m_mapBarTop - m_mapBarBottom; i++) {
371 360 barsetAt(i)->insertValue(start - m_mapFirst, m_model->data(m_model->index(i, start), Qt::DisplayRole).toDouble());
372 361 }
373 362 }
374 363 emit restructuredBar(1);
375 364 }
376 365
377 366 void QBarSeries::modelDataRemoved(QModelIndex parent, int start, int end)
378 367 {
379 368 Q_UNUSED(parent)
380 369 Q_UNUSED(end)
381 370
382 371 removeCategory(start - m_mapFirst);
383 372 for (int i = 0; i <= m_mapBarTop - m_mapBarBottom; i++)
384 373 {
385 374 barsetAt(i)->removeValue(start - m_mapFirst);
386 375 }
387 376 emit restructuredBar(1);
388 377 }
389 378
390 379 void QBarSeries::barsetChanged()
391 380 {
392 381 emit updatedBars();
393 382 }
394 383
395 384 QBarCategories QBarSeries::categories() const
396 385 {
397 386 QBarCategories categories;
398 387 int count = m_internalModel->categoryCount();
399 388 for (int i=1; i <= count; i++) {
400 389 categories.insert(i, m_internalModel->categoryName(i - 1));
401 390 }
402 391 return categories;
403 392 }
404 393
405 394 #include "moc_qbarseries.cpp"
406 395
407 396 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,117 +1,114
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 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28 28
29 29 typedef QStringList QBarCategories;
30 30
31 31 class QBarSet;
32 32 class BarChartModel;
33 33 class BarCategory;
34 34
35 35 // Container for series
36 36 class QTCOMMERCIALCHART_EXPORT QBarSeries : public QSeries
37 37 {
38 38 Q_OBJECT
39 39 public:
40 40 QBarSeries(QStringList categories, QObject *parent = 0);
41 41
42 42 virtual QSeriesType type() const { return QSeries::SeriesTypeBar; }
43 43
44 44 void appendBarSet(QBarSet *set); // Takes ownership of set
45 45 void removeBarSet(QBarSet *set); // Releases ownership, doesn't delete set
46 46 void insertBarSet(int i, QBarSet *set);
47 47 void insertCategory(int i, QString category);
48 48 void removeCategory(int i);
49 49 int barsetCount() const;
50 50 int categoryCount() const;
51 51 QList<QBarSet*> barSets() const;
52 52 QBarCategories categories() const;
53 53
54 54 bool setModel(QAbstractItemModel *model);
55 55 QAbstractItemModel *modelExt() { return m_model; }
56 56 void setModelMapping(int categories, int bottomBoundry, int topBoundry, Qt::Orientation orientation = Qt::Vertical);
57 57 void setModelMappingShift(int first, int count);
58 58
59 59 public:
60 60 // TODO: Functions below this are not part of api and will be moved
61 61 // to private implementation, when we start using it
62 62 // TODO: TO PIMPL --->
63 63 QBarSet* barsetAt(int index);
64 64 QString categoryName(int category);
65 65 qreal min();
66 66 qreal max();
67 67 qreal valueAt(int set, int category);
68 68 qreal percentageAt(int set, int category);
69 69 qreal categorySum(int category);
70 70 qreal maxCategorySum();
71 71 BarChartModel& model();
72 72 // <--- TO PIMPL
73 73
74 74 Q_SIGNALS:
75 //void changed(int index);
76 void clicked(QBarSet *barset, QString category); // Up to user of api, what to do with these signals
77 void rightClicked(QBarSet *barset, QString category);
75 void clicked(QBarSet *barset, QString category, Qt::MouseButtons button); // Up to user of api, what to do with these signals
78 76
79 77 //
80 78 void updatedBars();
81 79 void restructuredBar(int);
82 80
83 81 // TODO: internal signals, these to private implementation.
84 82 // TODO: TO PIMPL --->
85 83 void showToolTip(QPoint pos, QString tip);
86 84 // <--- TO PIMPL
87 85
88 86 public Q_SLOTS:
89 87 void setToolTipEnabled(bool enabled = true); // enables tooltips
90 88
91 89 // TODO: TO PIMPL --->
92 void barsetClicked(QString category);
93 void barsetRightClicked(QString category);
90 void barsetClicked(QString category, Qt::MouseButtons button);
94 91 // <--- TO PIMPL
95 92
96 93 private Q_SLOTS:
97 94 // slots for updating bars when data in model changes
98 95 void modelUpdated(QModelIndex topLeft, QModelIndex bottomRight);
99 96 void modelDataAdded(QModelIndex parent, int start, int end);
100 97 void modelDataRemoved(QModelIndex parent, int start, int end);
101 98 void barsetChanged();
102 99
103 100 protected:
104 101 BarChartModel *m_internalModel; // TODO: this may change... current "2 models" situation doesn't look good.
105 102
106 103 QAbstractItemModel* m_model;
107 104 int m_mapCategories;
108 105 int m_mapBarBottom;
109 106 int m_mapBarTop;
110 107 int m_mapFirst;
111 108 int m_mapCount;
112 109 Qt::Orientation m_mapOrientation;
113 110 };
114 111
115 112 QTCOMMERCIALCHART_END_NAMESPACE
116 113
117 114 #endif // BARSERIES_H
@@ -1,226 +1,221
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 22 #include <QDebug>
23 23 #include <QToolTip>
24 24
25 25 QTCOMMERCIALCHART_BEGIN_NAMESPACE
26 26
27 27 /*!
28 28 \class QBarSet
29 29 \brief part of QtCommercial chart API.
30 30
31 31 QBarSet represents one set of bars. Set of bars contains one data value for each category.
32 32 First value of set is assumed to belong to first category, second to second category and so on.
33 33 If set has fewer values than there are categories, then the missing values are assumed to be
34 34 at the end of set. For missing values in middle of a set, numerical value of zero is used.
35 35
36 36 \mainclass
37 37
38 38 \sa QBarSeries, QStackedBarSeries, QPercentBarSeries
39 39 */
40 40
41 41 /*!
42 \fn void QBarSet::clicked(QString category)
42 \fn void QBarSet::clicked(QString category, Qt::MouseButtons button)
43 43 \brief signals that set has been clicked
44 44 Parameter \a category describes on which category was clicked
45 */
46
47 /*!
48 \fn void QBarSet::rightClicked(QString category)
49 \brief signals that set has been clicked with right mouse button
50 Parameter \a category describes on which category was clicked
45 Parameter \a button mouse button
51 46 */
52 47
53 48 /*!
54 49 \fn void QBarSet::hoverEnter(QPoint pos)
55 50 \brief signals that mouse has entered over the set at position \a pos.
56 51 */
57 52
58 53 /*!
59 54 \fn void QBarSet::hoverLeave()
60 55 \brief signals that mouse has left from the set.
61 56 */
62 57
63 58 /*!
64 59 \fn void QBarSet::toggleFloatingValues()
65 60 \brief \internal
66 61 */
67 62
68 63 /*!
69 64 \fn void QBarSet::showToolTip(QPoint pos, QString tip)
70 65 \brief \internal \a pos \a tip
71 66 */
72 67
73 68
74 69 /*!
75 70 Constructs QBarSet with a name of \a name and with parent of \a parent
76 71 */
77 72 QBarSet::QBarSet(QString name, QObject *parent)
78 73 : QObject(parent)
79 74 ,m_name(name)
80 75 {
81 76 }
82 77
83 78 /*!
84 79 Sets new \a name for set.
85 80 */
86 81 void QBarSet::setName(QString name)
87 82 {
88 83 m_name = name;
89 84 }
90 85
91 86 /*!
92 87 Returns name of the set.
93 88 */
94 89 QString QBarSet::name() const
95 90 {
96 91 return m_name;
97 92 }
98 93
99 94 /*!
100 95 Appends new value \a value to the end of set.
101 96 */
102 97 QBarSet& QBarSet::operator << (const qreal &value)
103 98 {
104 99 m_values.append(value);
105 100 emit structureChanged();
106 101 return *this;
107 102 }
108 103
109 104 void QBarSet::insertValue(int i, qreal value)
110 105 {
111 106 m_values.insert(i, value);
112 107 }
113 108
114 109 void QBarSet::removeValue(int i)
115 110 {
116 111 m_values.removeAt(i);
117 112 }
118 113
119 114 /*!
120 115 Returns count of values in set.
121 116 */
122 117 int QBarSet::count() const
123 118 {
124 119 return m_values.count();
125 120 }
126 121
127 122 /*!
128 123 Returns value of set indexed by \a index
129 124 */
130 125 qreal QBarSet::valueAt(int index) const
131 126 {
132 127 return m_values.at(index);
133 128 }
134 129
135 130 /*!
136 131 Sets a new value \a value to set, indexed by \a index
137 132 */
138 133 void QBarSet::setValue(int index, qreal value)
139 134 {
140 135 m_values.replace(index,value);
141 136 emit valueChanged();
142 137 }
143 138
144 139 /*!
145 140 Returns total sum of all values in barset.
146 141 */
147 142 qreal QBarSet::total() const
148 143 {
149 144 qreal total(0);
150 145 for (int i=0; i < m_values.count(); i++) {
151 146 total += m_values.at(i);
152 147 }
153 148 return total;
154 149 }
155 150
156 151 /*!
157 152 Sets pen for set. Bars of this set are drawn using \a pen
158 153 */
159 154 void QBarSet::setPen(const QPen &pen)
160 155 {
161 156 m_pen = pen;
162 157 emit valueChanged();
163 158 }
164 159
165 160 /*!
166 161 Returns pen of the set.
167 162 */
168 163 QPen QBarSet::pen() const
169 164 {
170 165 return m_pen;
171 166 }
172 167
173 168 /*!
174 169 Sets brush for the set. Bars of this set are drawn using \a brush
175 170 */
176 171 void QBarSet::setBrush(const QBrush &brush)
177 172 {
178 173 m_brush = brush;
179 174 emit valueChanged();
180 175 }
181 176
182 177 /*!
183 178 Returns brush of the set.
184 179 */
185 180 QBrush QBarSet::brush() const
186 181 {
187 182 return m_brush;
188 183 }
189 184
190 185 /*!
191 186 Sets the pen for floating values that are drawn on top of this set
192 187 */
193 188 void QBarSet::setFloatingValuePen(const QPen &pen)
194 189 {
195 190 m_floatingValuePen = pen;
196 191 }
197 192
198 193 /*!
199 194 Returns the pen for floating values that are drawn on top of this set
200 195 */
201 196 QPen QBarSet::floatingValuePen() const
202 197 {
203 198 return m_floatingValuePen;
204 199 }
205 200
206 201 /*!
207 202 \internal \a pos
208 203 */
209 204 void QBarSet::barHoverEnterEvent(QPoint pos)
210 205 {
211 206 emit showToolTip(pos, m_name);
212 207 emit hoverEnter(pos);
213 208 }
214 209
215 210 /*!
216 211 \internal
217 212 */
218 213 void QBarSet::barHoverLeaveEvent()
219 214 {
220 215 // Emit signal to user of charts
221 216 emit hoverLeave();
222 217 }
223 218
224 219 #include "moc_qbarset.cpp"
225 220
226 221 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,94 +1,93
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
28 28 QTCOMMERCIALCHART_BEGIN_NAMESPACE
29 29
30 30 class QTCOMMERCIALCHART_EXPORT QBarSet : public QObject
31 31 {
32 32 Q_OBJECT
33 33 public:
34 34 QBarSet(QString name, QObject *parent = 0);
35 35
36 36 void setName(QString name);
37 37 QString name() const;
38 38 QBarSet& operator << (const qreal &value); // appends new value to set
39 39 void insertValue(int i, qreal value);
40 40 void removeValue(int i);
41 41
42 42 // TODO: remove indices eventually. Use as internal?
43 43 int count() const; // count of values in set
44 44 qreal valueAt(int index) const; // for modifying individual values
45 45 void setValue(int index, qreal value); // setter for individual value
46 46 qreal total() const; // total values in the set
47 47
48 48 // TODO:
49 49 //qreal value(QString category);
50 50 //void setValue(QString category, qreal value);
51 51
52 52 void setPen(const QPen &pen);
53 53 QPen pen() const;
54 54
55 55 void setBrush(const QBrush &brush);
56 56 QBrush brush() const;
57 57
58 58 void setFloatingValuePen(const QPen &pen);
59 59 QPen floatingValuePen() const;
60 60
61 61 Q_SIGNALS:
62 void clicked(QString category); // Clicked and hover signals exposed to user
63 void rightClicked(QString category);
62 void clicked(QString category, Qt::MouseButtons button); // Clicked and hover signals exposed to user
64 63 void toggleFloatingValues();
65 64
66 65 // TODO: Expose this to user or not?
67 66 // TODO: TO PIMPL --->
68 67 void structureChanged();
69 68 void valueChanged();
70 69 void hoverEnter(QPoint pos);
71 70 void hoverLeave();
72 71 void showToolTip(QPoint pos, QString tip); // Private signal
73 72 // <--- TO PIMPL
74 73
75 74 public Q_SLOTS:
76 75 // These are for internal communication
77 76 // TODO: TO PIMPL --->
78 77 void barHoverEnterEvent(QPoint pos);
79 78 void barHoverLeaveEvent();
80 79 // <--- TO PIMPL
81 80
82 81 private:
83 82
84 83 QString m_name;
85 84 QList<qreal> m_values; // TODO: replace with map (category, value)
86 85 QMap<QString, qreal> m_mappedValues;
87 86 QPen m_pen;
88 87 QBrush m_brush;
89 88 QPen m_floatingValuePen;
90 89 };
91 90
92 91 QTCOMMERCIALCHART_END_NAMESPACE
93 92
94 93 #endif // QBARSET_H
General Comments 0
You need to be logged in to leave comments. Login now