##// END OF EJS Templates
fixed barchart signals with pimpl. drilldown works now
sauimone -
r968:ecf1d49ffcb6
parent child
Show More
@@ -1,54 +1,55
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "bar_p.h"
21 #include "bar_p.h"
22 #include <QDebug>
22 #include <QDebug>
23 #include <QPainter>
23 #include <QPainter>
24 #include <QGraphicsSceneEvent>
24 #include <QGraphicsSceneEvent>
25
25
26 QTCOMMERCIALCHART_BEGIN_NAMESPACE
26 QTCOMMERCIALCHART_BEGIN_NAMESPACE
27
27
28 Bar::Bar(QString category, QGraphicsItem *parent)
28 Bar::Bar(QBarSet *barset, QString category, QGraphicsItem *parent) : QGraphicsRectItem(parent),
29 : QGraphicsRectItem(parent),
29 m_category(category),
30 m_category(category)
30 m_barset(barset)
31 {
31 {
32 setAcceptedMouseButtons(Qt::LeftButton | Qt::RightButton);
32 setAcceptedMouseButtons(Qt::LeftButton | Qt::RightButton);
33 setAcceptHoverEvents(true);
33 setAcceptHoverEvents(true);
34 }
34 }
35
35
36 void Bar::mousePressEvent(QGraphicsSceneMouseEvent *event)
36 void Bar::mousePressEvent(QGraphicsSceneMouseEvent *event)
37 {
37 {
38 emit clicked(m_category, event->button());
38 emit clicked(m_category, event->button());
39 emit clicked(m_barset, m_category, event->button());
39 }
40 }
40
41
41 void Bar::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
42 void Bar::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
42 {
43 {
43 emit hoverEntered(event->lastScreenPos());
44 emit hoverEntered(event->lastScreenPos());
44 }
45 }
45
46
46 void Bar::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
47 void Bar::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
47 {
48 {
48 Q_UNUSED(event)
49 Q_UNUSED(event)
49 emit hoverLeaved();
50 emit hoverLeaved();
50 }
51 }
51
52
52 #include "moc_bar_p.cpp"
53 #include "moc_bar_p.cpp"
53
54
54 QTCOMMERCIALCHART_END_NAMESPACE
55 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,52 +1,56
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #ifndef BAR_H
21 #ifndef BAR_H
22 #define BAR_H
22 #define BAR_H
23
23
24 #include "qchartglobal.h"
24 #include "qchartglobal.h"
25 #include <QGraphicsRectItem>
25 #include <QGraphicsRectItem>
26
26
27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28
28
29 class QBarSet;
30
29 // Single visual bar item of chart
31 // Single visual bar item of chart
30 class Bar : public QObject, public QGraphicsRectItem
32 class Bar : public QObject, public QGraphicsRectItem
31 {
33 {
32 Q_OBJECT
34 Q_OBJECT
33 public:
35 public:
34 Bar(QString category, QGraphicsItem *parent = 0);
36 Bar(QBarSet *barset, QString category, QGraphicsItem *parent = 0);
35
37
36 public:
38 public:
37 void mousePressEvent(QGraphicsSceneMouseEvent *event);
39 void mousePressEvent(QGraphicsSceneMouseEvent *event);
38 void hoverEnterEvent(QGraphicsSceneHoverEvent *event);
40 void hoverEnterEvent(QGraphicsSceneHoverEvent *event);
39 void hoverLeaveEvent(QGraphicsSceneHoverEvent *event);
41 void hoverLeaveEvent(QGraphicsSceneHoverEvent *event);
40
42
41 Q_SIGNALS:
43 Q_SIGNALS:
42 void clicked(QString category, Qt::MouseButtons button);
44 void clicked(QString category, Qt::MouseButtons button);
45 void clicked(QBarSet *barset, QString category, Qt::MouseButtons button);
43 void hoverEntered(QPoint pos);
46 void hoverEntered(QPoint pos);
44 void hoverLeaved();
47 void hoverLeaved();
45
48
46 private:
49 private:
47 QString m_category;
50 QString m_category;
51 QBarSet *m_barset;
48 };
52 };
49
53
50 QTCOMMERCIALCHART_END_NAMESPACE
54 QTCOMMERCIALCHART_END_NAMESPACE
51
55
52 #endif // BAR_H
56 #endif // BAR_H
@@ -1,217 +1,218
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "barchartitem_p.h"
21 #include "barchartitem_p.h"
22 #include "bar_p.h"
22 #include "bar_p.h"
23 #include "barlabel_p.h"
23 #include "barlabel_p.h"
24 #include "qbarset.h"
24 #include "qbarset.h"
25 #include "qbarset_p.h"
25 #include "qbarset_p.h"
26 #include "qbarseries.h"
26 #include "qbarseries.h"
27 #include "qbarseries_p.h"
27 #include "qbarseries_p.h"
28 #include "qchart.h"
28 #include "qchart.h"
29 #include "qchartaxis.h"
29 #include "qchartaxis.h"
30 #include "qchartaxiscategories.h"
30 #include "qchartaxiscategories.h"
31 #include "chartpresenter_p.h"
31 #include "chartpresenter_p.h"
32 #include "chartanimator_p.h"
32 #include "chartanimator_p.h"
33 #include "chartdataset_p.h"
33 #include "chartdataset_p.h"
34 #include <QToolTip>
34 #include <QToolTip>
35 #include <QPainter>
35 #include <QPainter>
36
36
37 QTCOMMERCIALCHART_BEGIN_NAMESPACE
37 QTCOMMERCIALCHART_BEGIN_NAMESPACE
38
38
39 BarChartItem::BarChartItem(QBarSeries *series, ChartPresenter *presenter) :
39 BarChartItem::BarChartItem(QBarSeries *series, ChartPresenter *presenter) :
40 ChartItem(presenter),
40 ChartItem(presenter),
41 m_layoutSet(false),
41 m_layoutSet(false),
42 m_series(series)
42 m_series(series)
43 {
43 {
44 setFlag(ItemClipsChildrenToShape);
44 setFlag(ItemClipsChildrenToShape);
45 connect(series->d_func(), SIGNAL(showToolTip(QPoint,QString)), this, SLOT(showToolTip(QPoint,QString)));
45 connect(series->d_func(), SIGNAL(showToolTip(QPoint,QString)), this, SLOT(showToolTip(QPoint,QString)));
46 connect(series->d_func(), SIGNAL(updatedBars()), this, SLOT(handleLayoutChanged()));
46 connect(series->d_func(), SIGNAL(updatedBars()), this, SLOT(handleLayoutChanged()));
47 connect(series->d_func(), SIGNAL(restructuredBars()), this, SLOT(handleModelChanged()));
47 connect(series->d_func(), SIGNAL(restructuredBars()), this, SLOT(handleModelChanged()));
48 setZValue(ChartPresenter::BarSeriesZValue);
48 setZValue(ChartPresenter::BarSeriesZValue);
49 dataChanged();
49 dataChanged();
50 }
50 }
51
51
52 BarChartItem::~BarChartItem()
52 BarChartItem::~BarChartItem()
53 {
53 {
54 disconnect(this,SLOT(showToolTip(QPoint,QString)));
54 disconnect(this,SLOT(showToolTip(QPoint,QString)));
55 }
55 }
56
56
57 void BarChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
57 void BarChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
58 {
58 {
59 Q_UNUSED(painter);
59 Q_UNUSED(painter);
60 Q_UNUSED(option);
60 Q_UNUSED(option);
61 Q_UNUSED(widget);
61 Q_UNUSED(widget);
62 }
62 }
63
63
64 QRectF BarChartItem::boundingRect() const
64 QRectF BarChartItem::boundingRect() const
65 {
65 {
66 return m_rect.translated(-m_rect.topLeft());
66 return m_rect.translated(-m_rect.topLeft());
67 }
67 }
68
68
69 void BarChartItem::dataChanged()
69 void BarChartItem::dataChanged()
70 {
70 {
71 // TODO: performance optimizations. Do we really need to delete and create items every time data is changed or can we reuse them?
71 // TODO: performance optimizations. Do we really need to delete and create items every time data is changed or can we reuse them?
72 foreach(QGraphicsItem *item, childItems()) {
72 foreach(QGraphicsItem *item, childItems()) {
73 delete item;
73 delete item;
74 }
74 }
75
75
76 m_bars.clear();
76 m_bars.clear();
77 m_labels.clear();
77 m_labels.clear();
78 m_layout.clear();
78 m_layout.clear();
79
79
80 // Create new graphic items for bars
80 // Create new graphic items for bars
81 for (int c = 0; c < m_series->categoryCount(); c++) {
81 for (int c = 0; c < m_series->categoryCount(); c++) {
82 QString category = m_series->d_func()->categoryName(c);
82 QString category = m_series->d_func()->categoryName(c);
83 for (int s = 0; s < m_series->barsetCount(); s++) {
83 for (int s = 0; s < m_series->barsetCount(); s++) {
84 QBarSet *set = m_series->d_func()->barsetAt(s);
84 QBarSet *set = m_series->d_func()->barsetAt(s);
85 Bar *bar = new Bar(category,this);
85 Bar *bar = new Bar(set,category,this);
86 m_bars.append(bar);
86 m_bars.append(bar);
87 connect(bar, SIGNAL(clicked(QString,Qt::MouseButtons)), set, SIGNAL(clicked(QString,Qt::MouseButtons)));
87 connect(bar, SIGNAL(clicked(QString,Qt::MouseButtons)), set, SIGNAL(clicked(QString,Qt::MouseButtons)));
88 connect(bar, SIGNAL(clicked(QBarSet*,QString,Qt::MouseButtons)), m_series, SIGNAL(clicked(QBarSet*,QString,Qt::MouseButtons)));
88 connect(bar, SIGNAL(hoverEntered(QPoint)), set->d_ptr.data(), SLOT(barHoverEnterEvent(QPoint)));
89 connect(bar, SIGNAL(hoverEntered(QPoint)), set->d_ptr.data(), SLOT(barHoverEnterEvent(QPoint)));
89 connect(bar, SIGNAL(hoverLeaved()), set->d_ptr.data(), SLOT(barHoverLeaveEvent()));
90 connect(bar, SIGNAL(hoverLeaved()), set->d_ptr.data(), SLOT(barHoverLeaveEvent()));
90 m_layout.append(QRectF(0, 0, 0, 0));
91 m_layout.append(QRectF(0, 0, 0, 0));
91 }
92 }
92 }
93 }
93
94
94 // Create labels
95 // Create labels
95 for (int category = 0; category < m_series->categoryCount(); category++) {
96 for (int category = 0; category < m_series->categoryCount(); category++) {
96 for (int s = 0; s < m_series->barsetCount(); s++) {
97 for (int s = 0; s < m_series->barsetCount(); s++) {
97 QBarSet *set = m_series->d_func()->barsetAt(s);
98 QBarSet *set = m_series->d_func()->barsetAt(s);
98 BarLabel *value = new BarLabel(*set, this);
99 BarLabel *value = new BarLabel(*set, this);
99 m_labels.append(value);
100 m_labels.append(value);
100 connect(set,SIGNAL(labelsVisibleChanged(bool)),value,SLOT(labelsVisibleChanged(bool)));
101 connect(set->d_ptr.data(),SIGNAL(labelsVisibleChanged(bool)),value,SLOT(labelsVisibleChanged(bool)));
101 }
102 }
102 }
103 }
103 }
104 }
104
105
105 QVector<QRectF> BarChartItem::calculateLayout()
106 QVector<QRectF> BarChartItem::calculateLayout()
106 {
107 {
107 QVector<QRectF> layout;
108 QVector<QRectF> layout;
108
109
109 // Use temporary qreals for accurancy
110 // Use temporary qreals for accurancy
110 qreal categoryCount = m_series->categoryCount();
111 qreal categoryCount = m_series->categoryCount();
111 qreal setCount = m_series->barsetCount();
112 qreal setCount = m_series->barsetCount();
112
113
113 // Domain:
114 // Domain:
114 qreal width = geometry().width();
115 qreal width = geometry().width();
115 qreal height = geometry().height();
116 qreal height = geometry().height();
116 qreal range = m_domainMaxY - m_domainMinY;
117 qreal range = m_domainMaxY - m_domainMinY;
117 qreal scale = (height / range);
118 qreal scale = (height / range);
118 qreal categoryWidth = width / categoryCount;
119 qreal categoryWidth = width / categoryCount;
119 qreal barWidth = categoryWidth / (setCount+1);
120 qreal barWidth = categoryWidth / (setCount+1);
120
121
121 int itemIndex(0);
122 int itemIndex(0);
122 for (int category = 0; category < categoryCount; category++) {
123 for (int category = 0; category < categoryCount; category++) {
123 qreal xPos = categoryWidth * category + barWidth / 2;
124 qreal xPos = categoryWidth * category + barWidth / 2;
124 qreal yPos = height + scale * m_domainMinY;
125 qreal yPos = height + scale * m_domainMinY;
125 for (int set = 0; set < setCount; set++) {
126 for (int set = 0; set < setCount; set++) {
126 QBarSet* barSet = m_series->d_func()->barsetAt(set);
127 QBarSet* barSet = m_series->d_func()->barsetAt(set);
127
128
128 qreal barHeight = barSet->valueAt(category) * scale;
129 qreal barHeight = barSet->valueAt(category) * scale;
129 Bar* bar = m_bars.at(itemIndex);
130 Bar* bar = m_bars.at(itemIndex);
130
131
131 QRectF rect(xPos, yPos - barHeight, barWidth, barHeight);
132 QRectF rect(xPos, yPos - barHeight, barWidth, barHeight);
132 layout.append(rect);
133 layout.append(rect);
133 bar->setPen(barSet->pen());
134 bar->setPen(barSet->pen());
134 bar->setBrush(barSet->brush());
135 bar->setBrush(barSet->brush());
135
136
136 BarLabel* label = m_labels.at(itemIndex);
137 BarLabel* label = m_labels.at(itemIndex);
137
138
138 if (!qFuzzyIsNull(barSet->valueAt(category))) {
139 if (!qFuzzyIsNull(barSet->valueAt(category))) {
139 label->setText(QString::number(barSet->valueAt(category)));
140 label->setText(QString::number(barSet->valueAt(category)));
140 } else {
141 } else {
141 label->setText(QString(""));
142 label->setText(QString(""));
142 }
143 }
143
144
144 label->setPos(xPos + (rect.width()/2 - label->boundingRect().width()/2)
145 label->setPos(xPos + (rect.width()/2 - label->boundingRect().width()/2)
145 ,yPos - barHeight/2 - label->boundingRect().height()/2);
146 ,yPos - barHeight/2 - label->boundingRect().height()/2);
146 label->setFont(barSet->labelFont());
147 label->setFont(barSet->labelFont());
147
148
148 itemIndex++;
149 itemIndex++;
149 xPos += barWidth;
150 xPos += barWidth;
150 }
151 }
151 }
152 }
152 return layout;
153 return layout;
153 }
154 }
154
155
155 void BarChartItem::applyLayout(const QVector<QRectF> &layout)
156 void BarChartItem::applyLayout(const QVector<QRectF> &layout)
156 {
157 {
157 if (animator())
158 if (animator())
158 animator()->updateLayout(this, m_layout, layout);
159 animator()->updateLayout(this, m_layout, layout);
159 else
160 else
160 setLayout(layout);
161 setLayout(layout);
161 }
162 }
162
163
163 void BarChartItem::setLayout(const QVector<QRectF> &layout)
164 void BarChartItem::setLayout(const QVector<QRectF> &layout)
164 {
165 {
165 m_layout = layout;
166 m_layout = layout;
166
167
167 for (int i=0; i < m_bars.count(); i++)
168 for (int i=0; i < m_bars.count(); i++)
168 m_bars.at(i)->setRect(layout.at(i));
169 m_bars.at(i)->setRect(layout.at(i));
169
170
170 update();
171 update();
171 }
172 }
172 //handlers
173 //handlers
173
174
174 void BarChartItem::handleModelChanged()
175 void BarChartItem::handleModelChanged()
175 {
176 {
176 dataChanged();
177 dataChanged();
177 }
178 }
178
179
179 void BarChartItem::handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY)
180 void BarChartItem::handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY)
180 {
181 {
181 m_domainMinX = minX;
182 m_domainMinX = minX;
182 m_domainMaxX = maxX;
183 m_domainMaxX = maxX;
183 m_domainMinY = minY;
184 m_domainMinY = minY;
184 m_domainMaxY = maxY;
185 m_domainMaxY = maxY;
185 handleLayoutChanged();
186 handleLayoutChanged();
186 }
187 }
187
188
188 void BarChartItem::handleGeometryChanged(const QRectF &rect)
189 void BarChartItem::handleGeometryChanged(const QRectF &rect)
189 {
190 {
190 prepareGeometryChange();
191 prepareGeometryChange();
191 m_clipRect = rect.translated(-rect.topLeft());
192 m_clipRect = rect.translated(-rect.topLeft());
192 m_rect = rect;
193 m_rect = rect;
193 handleLayoutChanged();
194 handleLayoutChanged();
194 m_layoutSet = true;
195 m_layoutSet = true;
195 setPos(rect.topLeft());
196 setPos(rect.topLeft());
196 }
197 }
197
198
198 void BarChartItem::handleLayoutChanged()
199 void BarChartItem::handleLayoutChanged()
199 {
200 {
200 if ((m_rect.width() <= 0) || (m_rect.height() <= 0)) {
201 if ((m_rect.width() <= 0) || (m_rect.height() <= 0)) {
201 // rect size zero.
202 // rect size zero.
202 return;
203 return;
203 }
204 }
204 QVector<QRectF> layout = calculateLayout();
205 QVector<QRectF> layout = calculateLayout();
205 applyLayout(layout);
206 applyLayout(layout);
206 update();
207 update();
207 }
208 }
208
209
209 void BarChartItem::showToolTip(QPoint pos, QString tip)
210 void BarChartItem::showToolTip(QPoint pos, QString tip)
210 {
211 {
211 // TODO: cool tooltip instead of default
212 // TODO: cool tooltip instead of default
212 QToolTip::showText(pos, tip);
213 QToolTip::showText(pos, tip);
213 }
214 }
214
215
215 #include "moc_barchartitem_p.cpp"
216 #include "moc_barchartitem_p.cpp"
216
217
217 QTCOMMERCIALCHART_END_NAMESPACE
218 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,535 +1,526
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "qbarseries.h"
21 #include "qbarseries.h"
22 #include "qbarseries_p.h"
22 #include "qbarseries_p.h"
23 #include "qbarset.h"
23 #include "qbarset.h"
24 #include "qbarset_p.h"
24 #include "qbarset_p.h"
25 #include "barchartmodel_p.h"
25 #include "barchartmodel_p.h"
26 #include "domain_p.h"
26 #include "domain_p.h"
27 #include "legendmarker_p.h"
27 #include "legendmarker_p.h"
28 #include "chartdataset_p.h"
28 #include "chartdataset_p.h"
29 #include "charttheme_p.h"
29 #include "charttheme_p.h"
30 #include "chartanimator_p.h"
30 #include "chartanimator_p.h"
31
31
32 #include <QAbstractItemModel>
32 #include <QAbstractItemModel>
33 #include <QModelIndex>
33 #include <QModelIndex>
34
34
35 QTCOMMERCIALCHART_BEGIN_NAMESPACE
35 QTCOMMERCIALCHART_BEGIN_NAMESPACE
36
36
37 /*!
37 /*!
38 \class QBarSeries
38 \class QBarSeries
39 \brief part of QtCommercial chart API.
39 \brief part of QtCommercial chart API.
40
40
41 QBarSeries represents a series of data shown as bars. One QBarSeries can contain multible
41 QBarSeries represents a series of data shown as bars. One QBarSeries can contain multible
42 QBarSet data sets. QBarSeries groups the data from sets to categories, which are defined
42 QBarSet data sets. QBarSeries groups the data from sets to categories, which are defined
43 by QStringList.
43 by QStringList.
44
44
45 \mainclass
45 \mainclass
46
46
47 \sa QBarSet, QStackedBarSeries, QPercentBarSeries
47 \sa QBarSet, QStackedBarSeries, QPercentBarSeries
48 */
48 */
49
49
50 /*!
50 /*!
51 \fn virtual QSeriesType QBarSeries::type() const
51 \fn virtual QSeriesType QBarSeries::type() const
52 \brief Returns type of series.
52 \brief Returns type of series.
53 \sa QSeries, QSeriesType
53 \sa QSeries, QSeriesType
54 */
54 */
55
55
56 /*!
56 /*!
57 \fn void QBarSeries::showToolTip(QPoint pos, QString tip)
57 \fn void QBarSeries::showToolTip(QPoint pos, QString tip)
58 \brief \internal \a pos \a tip
58 \brief \internal \a pos \a tip
59 */
59 */
60
60
61 /*!
61 /*!
62 Constructs empty QBarSeries. Parameter \a categories defines the categories for chart.
62 Constructs empty QBarSeries. Parameter \a categories defines the categories for chart.
63 QBarSeries is QObject which is a child of a \a parent.
63 QBarSeries is QObject which is a child of a \a parent.
64 */
64 */
65
65
66 QBarSeries::QBarSeries(QBarCategories categories, QObject *parent) : QSeries(*new QBarSeriesPrivate(categories, this),parent)
66 QBarSeries::QBarSeries(QBarCategories categories, QObject *parent) : QSeries(*new QBarSeriesPrivate(categories, this),parent)
67 {
67 {
68
68
69 }
69 }
70
70
71 QBarSeries::QBarSeries(QBarSeriesPrivate &d,QObject *parent) : QSeries(d,parent)
71 QBarSeries::QBarSeries(QBarSeriesPrivate &d,QObject *parent) : QSeries(d,parent)
72 {
72 {
73
73
74 }
74 }
75
75
76 QSeries::QSeriesType QBarSeries::type() const
76 QSeries::QSeriesType QBarSeries::type() const
77 {
77 {
78 return QSeries::SeriesTypeBar;
78 return QSeries::SeriesTypeBar;
79 }
79 }
80
80
81 /*!
81 /*!
82 Adds a set of bars to series. Takes ownership of \a set.
82 Adds a set of bars to series. Takes ownership of \a set.
83 Connects the clicked(QString, Qt::MouseButtons) signal
83 Connects the clicked(QString, Qt::MouseButtons) signal
84 of \a set to this series
84 of \a set to this series
85 */
85 */
86 void QBarSeries::appendBarSet(QBarSet *set)
86 void QBarSeries::appendBarSet(QBarSet *set)
87 {
87 {
88 Q_D(QBarSeries);
88 Q_D(QBarSeries);
89 d->m_internalModel->appendBarSet(set);
89 d->m_internalModel->appendBarSet(set);
90 QObject::connect(set->d_ptr.data(), SIGNAL(clicked(QString,Qt::MouseButtons)), d, SLOT(barsetClicked(QString,Qt::MouseButtons)));
91 QObject::connect(set->d_ptr.data(), SIGNAL(valueChanged()), d, SLOT(barsetChanged()));
90 QObject::connect(set->d_ptr.data(), SIGNAL(valueChanged()), d, SLOT(barsetChanged()));
92 emit d->restructuredBars();
91 emit d->restructuredBars();
93 }
92 }
94
93
95 /*!
94 /*!
96 Removes a set of bars from series. Releases ownership of \a set. Doesnt delete \a set.
95 Removes a set of bars from series. Releases ownership of \a set. Doesnt delete \a set.
97 Disconnects the clicked(QString, Qt::MouseButtons) signal
96 Disconnects the clicked(QString, Qt::MouseButtons) signal
98 of \a set from this series
97 of \a set from this series
99 */
98 */
100 void QBarSeries::removeBarSet(QBarSet *set)
99 void QBarSeries::removeBarSet(QBarSet *set)
101 {
100 {
102 Q_D(QBarSeries);
101 Q_D(QBarSeries);
103 QObject::disconnect(set->d_ptr.data(), SIGNAL(clicked(QString,Qt::MouseButtons)), d, SLOT(barsetClicked(QString,Qt::MouseButtons)));
104 d->m_internalModel->removeBarSet(set);
102 d->m_internalModel->removeBarSet(set);
105 emit d->restructuredBars();
103 emit d->restructuredBars();
106 }
104 }
107
105
108 /*!
106 /*!
109 Adds a list of barsets to series. Takes ownership of \a sets.
107 Adds a list of barsets to series. Takes ownership of \a sets.
110 Connects the clicked(QString, Qt::MouseButtons) signals
108 Connects the clicked(QString, Qt::MouseButtons) signals
111 of \a sets to this series
109 of \a sets to this series
112 */
110 */
113 void QBarSeries::appendBarSets(QList<QBarSet* > sets)
111 void QBarSeries::appendBarSets(QList<QBarSet* > sets)
114 {
112 {
115 Q_D(QBarSeries);
113 Q_D(QBarSeries);
116 foreach (QBarSet* barset, sets) {
114 foreach (QBarSet* barset, sets) {
117 d->m_internalModel->appendBarSet(barset);
115 d->m_internalModel->appendBarSet(barset);
118 QObject::connect(barset, SIGNAL(clicked(QString,Qt::MouseButtons)), this, SLOT(barsetClicked(QString,Qt::MouseButtons)));
119 QObject::connect(barset, SIGNAL(valueChanged()), this, SLOT(barsetChanged()));
116 QObject::connect(barset, SIGNAL(valueChanged()), this, SLOT(barsetChanged()));
120 }
117 }
121 emit d->restructuredBars();
118 emit d->restructuredBars();
122
119
123 }
120 }
124
121
125 /*!
122 /*!
126 Removes a list of barsets from series. Releases ownership of \a sets. Doesnt delete \a sets.
123 Removes a list of barsets from series. Releases ownership of \a sets. Doesnt delete \a sets.
127 Disconnects the clicked(QString, Qt::MouseButtons) signal
124 Disconnects the clicked(QString, Qt::MouseButtons) signal
128 of \a sets from this series
125 of \a sets from this series
129 */
126 */
130 void QBarSeries::removeBarSets(QList<QBarSet* > sets)
127 void QBarSeries::removeBarSets(QList<QBarSet* > sets)
131 {
128 {
132 Q_D(QBarSeries);
129 Q_D(QBarSeries);
133
130
134 foreach (QBarSet* barset, sets) {
131 foreach (QBarSet* barset, sets) {
135 QObject::disconnect(barset, SIGNAL(clicked(QString,Qt::MouseButtons)), this, SLOT(barsetClicked(QString,Qt::MouseButtons)));
136 d->m_internalModel->removeBarSet(barset);
132 d->m_internalModel->removeBarSet(barset);
137 }
133 }
138 emit d->restructuredBars();
134 emit d->restructuredBars();
139 }
135 }
140
136
141 /*!
137 /*!
142 Inserts new \a set on the \a i position.
138 Inserts new \a set on the \a i position.
143 The barset that is currently at this postion is moved to postion i + 1
139 The barset that is currently at this postion is moved to postion i + 1
144 */
140 */
145 void QBarSeries::insertBarSet(int i, QBarSet *set)
141 void QBarSeries::insertBarSet(int i, QBarSet *set)
146 {
142 {
147 Q_D(QBarSeries);
143 Q_D(QBarSeries);
148 d->m_internalModel->insertBarSet(i, set);
144 d->m_internalModel->insertBarSet(i, set);
149 emit d->barsetChanged();
145 emit d->barsetChanged();
150 }
146 }
151
147
152 /*!
148 /*!
153 Inserts new \a category on the \a i position.
149 Inserts new \a category on the \a i position.
154 The category that is currently at this postion is moved to postion i + 1
150 The category that is currently at this postion is moved to postion i + 1
155 \sa removeCategory()
151 \sa removeCategory()
156 */
152 */
157 void QBarSeries::insertCategory(int i, QString category)
153 void QBarSeries::insertCategory(int i, QString category)
158 {
154 {
159 Q_D(QBarSeries);
155 Q_D(QBarSeries);
160 d->m_internalModel->insertCategory(i, category);
156 d->m_internalModel->insertCategory(i, category);
161 }
157 }
162
158
163 /*!
159 /*!
164 Removes the category specified by \a i
160 Removes the category specified by \a i
165 \sa insertCategory()
161 \sa insertCategory()
166 */
162 */
167 void QBarSeries::removeCategory(int i)
163 void QBarSeries::removeCategory(int i)
168 {
164 {
169 Q_D(QBarSeries);
165 Q_D(QBarSeries);
170 d->m_internalModel->removeCategory(i);
166 d->m_internalModel->removeCategory(i);
171 }
167 }
172
168
173 /*!
169 /*!
174 Returns number of sets in series.
170 Returns number of sets in series.
175 */
171 */
176 int QBarSeries::barsetCount() const
172 int QBarSeries::barsetCount() const
177 {
173 {
178 Q_D(const QBarSeries);
174 Q_D(const QBarSeries);
179 return d->m_internalModel->barsetCount();
175 return d->m_internalModel->barsetCount();
180 }
176 }
181
177
182 /*!
178 /*!
183 Returns number of categories in series
179 Returns number of categories in series
184 */
180 */
185 int QBarSeries::categoryCount() const
181 int QBarSeries::categoryCount() const
186 {
182 {
187 Q_D(const QBarSeries);
183 Q_D(const QBarSeries);
188 return d->m_internalModel->categoryCount();
184 return d->m_internalModel->categoryCount();
189 }
185 }
190
186
191 /*!
187 /*!
192 Returns a list of sets in series. Keeps ownership of sets.
188 Returns a list of sets in series. Keeps ownership of sets.
193 */
189 */
194 QList<QBarSet*> QBarSeries::barSets() const
190 QList<QBarSet*> QBarSeries::barSets() const
195 {
191 {
196 Q_D(const QBarSeries);
192 Q_D(const QBarSeries);
197 return d->m_internalModel->barSets();
193 return d->m_internalModel->barSets();
198 }
194 }
199
195
200 /*!
196 /*!
201 Enables or disables tooltip depending on parameter \a enabled.
197 Enables or disables tooltip depending on parameter \a enabled.
202 Tooltip shows the name of set, when mouse is hovering on top of bar.
198 Tooltip shows the name of set, when mouse is hovering on top of bar.
203 Calling without parameter \a enabled, enables the tooltip
199 Calling without parameter \a enabled, enables the tooltip
204 */
200 */
205 void QBarSeries::setToolTipEnabled(bool enabled)
201 void QBarSeries::setToolTipEnabled(bool enabled)
206 {
202 {
207 Q_D(QBarSeries);
203 Q_D(QBarSeries);
208 d->setToolTipEnabled(enabled);
204 d->setToolTipEnabled(enabled);
209 }
205 }
210
206
211 /*!
207 /*!
212 \fn bool QBarSeries::setModel(QAbstractItemModel *model)
208 \fn bool QBarSeries::setModel(QAbstractItemModel *model)
213 Sets the \a model to be used as a data source
209 Sets the \a model to be used as a data source
214 */
210 */
215 bool QBarSeries::setModel(QAbstractItemModel *model)
211 bool QBarSeries::setModel(QAbstractItemModel *model)
216 {
212 {
217 Q_D(QBarSeries);
213 Q_D(QBarSeries);
218 return d->setModel(model);
214 return d->setModel(model);
219 }
215 }
220
216
221 /*!
217 /*!
222 \fn bool QBarSeries::setModelMapping(int categories, int bottomBoundry, int topBoundry, Qt::Orientation orientation)
218 \fn bool QBarSeries::setModelMapping(int categories, int bottomBoundry, int topBoundry, Qt::Orientation orientation)
223 Sets column/row specified by \a categories to be used as a list of bar series categories.
219 Sets column/row specified by \a categories to be used as a list of bar series categories.
224 Parameter \a bottomBoundry indicates the column/row where the first bar set is located in the model.
220 Parameter \a bottomBoundry indicates the column/row where the first bar set is located in the model.
225 Parameter \a topBoundry indicates the column/row where the last bar set is located in the model.
221 Parameter \a topBoundry indicates the column/row where the last bar set is located in the model.
226 All the columns/rows inbetween those two values are also used as data for bar sets.
222 All the columns/rows inbetween those two values are also used as data for bar sets.
227 The \a orientation paramater specifies whether the data is in columns or in rows.
223 The \a orientation paramater specifies whether the data is in columns or in rows.
228 */
224 */
229 void QBarSeries::setModelMapping(int categories, int bottomBoundary, int topBoundary, Qt::Orientation orientation)
225 void QBarSeries::setModelMapping(int categories, int bottomBoundary, int topBoundary, Qt::Orientation orientation)
230 {
226 {
231 Q_D(QBarSeries);
227 Q_D(QBarSeries);
232 d->setModelMapping(categories,bottomBoundary,topBoundary,orientation);
228 d->setModelMapping(categories,bottomBoundary,topBoundary,orientation);
233 }
229 }
234
230
235 QBarCategories QBarSeries::categories() const
231 QBarCategories QBarSeries::categories() const
236 {
232 {
237 Q_D(const QBarSeries);
233 Q_D(const QBarSeries);
238
234
239 QBarCategories categories;
235 QBarCategories categories;
240 int count = d->m_internalModel->categoryCount();
236 int count = d->m_internalModel->categoryCount();
241 for (int i=1; i <= count; i++) {
237 for (int i=1; i <= count; i++) {
242 categories.insert(i, d->m_internalModel->categoryName(i - 1));
238 categories.insert(i, d->m_internalModel->categoryName(i - 1));
243 }
239 }
244 return categories;
240 return categories;
245
241
246 }
242 }
247
243
248 /*!
244 /*!
249 Sets the visibility of labels in series to \a visible
245 Sets the visibility of labels in series to \a visible
250 */
246 */
251 void QBarSeries::setLabelsVisible(bool visible)
247 void QBarSeries::setLabelsVisible(bool visible)
252 {
248 {
253 foreach (QBarSet* s, barSets()) {
249 foreach (QBarSet* s, barSets()) {
254 s->setLabelsVisible(visible);
250 s->setLabelsVisible(visible);
255 }
251 }
256 }
252 }
257
253
258 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
254 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
259
255
260 QBarSeriesPrivate::QBarSeriesPrivate(QBarCategories categories, QBarSeries *q) : QSeriesPrivate(q),
256 QBarSeriesPrivate::QBarSeriesPrivate(QBarCategories categories, QBarSeries *q) : QSeriesPrivate(q),
261 m_internalModel(new BarChartModel(categories,this)),
257 m_internalModel(new BarChartModel(categories,this)),
262 m_model(0),
258 m_model(0),
263 m_mapCategories(-1),
259 m_mapCategories(-1),
264 m_mapBarBottom(-1),
260 m_mapBarBottom(-1),
265 m_mapBarTop(-1),
261 m_mapBarTop(-1),
266 m_mapFirst(0),
262 m_mapFirst(0),
267 m_mapCount(0),
263 m_mapCount(0),
268 m_mapOrientation(Qt::Vertical)
264 m_mapOrientation(Qt::Vertical)
269 {
265 {
270 }
266 }
271
267
272 QBarSet* QBarSeriesPrivate::barsetAt(int index)
268 QBarSet* QBarSeriesPrivate::barsetAt(int index)
273 {
269 {
274 return m_internalModel->barsetAt(index);
270 return m_internalModel->barsetAt(index);
275 }
271 }
276
272
277 QString QBarSeriesPrivate::categoryName(int category)
273 QString QBarSeriesPrivate::categoryName(int category)
278 {
274 {
279 return m_internalModel->categoryName(category);
275 return m_internalModel->categoryName(category);
280 }
276 }
281
277
282 void QBarSeriesPrivate::setToolTipEnabled(bool enabled)
278 void QBarSeriesPrivate::setToolTipEnabled(bool enabled)
283 {
279 {
284 // TODO: what if we add sets after call to this function? Those sets won't have tooltip enabled.
280 // TODO: what if we add sets after call to this function? Those sets won't have tooltip enabled.
285 if (enabled) {
281 if (enabled) {
286 for (int i=0; i<m_internalModel->barsetCount(); i++) {
282 for (int i=0; i<m_internalModel->barsetCount(); i++) {
287 QBarSet *set = m_internalModel->barsetAt(i);
283 QBarSet *set = m_internalModel->barsetAt(i);
288 connect(set, SIGNAL(showToolTip(QPoint,QString)), this, SIGNAL(showToolTip(QPoint,QString)));
284 connect(set->d_ptr.data(), SIGNAL(showToolTip(QPoint,QString)), this, SIGNAL(showToolTip(QPoint,QString)));
289 }
285 }
290 } else {
286 } else {
291 for (int i=0; i<m_internalModel->barsetCount(); i++) {
287 for (int i=0; i<m_internalModel->barsetCount(); i++) {
292 QBarSet *set = m_internalModel->barsetAt(i);
288 QBarSet *set = m_internalModel->barsetAt(i);
293 disconnect(set, SIGNAL(showToolTip(QPoint,QString)), this, SIGNAL(showToolTip(QPoint,QString)));
289 disconnect(set->d_ptr.data(), SIGNAL(showToolTip(QPoint,QString)), this, SIGNAL(showToolTip(QPoint,QString)));
294 }
290 }
295 }
291 }
296 }
292 }
297
293
298 void QBarSeriesPrivate::barsetClicked(QString category, Qt::MouseButtons button)
299 {
300 emit clicked(qobject_cast<QBarSet*>(sender()), category, button);
301 }
302
303 qreal QBarSeriesPrivate::min()
294 qreal QBarSeriesPrivate::min()
304 {
295 {
305 return m_internalModel->min();
296 return m_internalModel->min();
306 }
297 }
307
298
308 qreal QBarSeriesPrivate::max()
299 qreal QBarSeriesPrivate::max()
309 {
300 {
310 return m_internalModel->max();
301 return m_internalModel->max();
311 }
302 }
312
303
313 qreal QBarSeriesPrivate::valueAt(int set, int category)
304 qreal QBarSeriesPrivate::valueAt(int set, int category)
314 {
305 {
315 return m_internalModel->valueAt(set, category);
306 return m_internalModel->valueAt(set, category);
316 }
307 }
317
308
318 qreal QBarSeriesPrivate::percentageAt(int set, int category)
309 qreal QBarSeriesPrivate::percentageAt(int set, int category)
319 {
310 {
320 return m_internalModel->percentageAt(set, category);
311 return m_internalModel->percentageAt(set, category);
321 }
312 }
322
313
323 qreal QBarSeriesPrivate::categorySum(int category)
314 qreal QBarSeriesPrivate::categorySum(int category)
324 {
315 {
325 return m_internalModel->categorySum(category);
316 return m_internalModel->categorySum(category);
326 }
317 }
327
318
328 qreal QBarSeriesPrivate::absoluteCategorySum(int category)
319 qreal QBarSeriesPrivate::absoluteCategorySum(int category)
329 {
320 {
330 return m_internalModel->absoluteCategorySum(category);
321 return m_internalModel->absoluteCategorySum(category);
331 }
322 }
332
323
333 qreal QBarSeriesPrivate::maxCategorySum()
324 qreal QBarSeriesPrivate::maxCategorySum()
334 {
325 {
335 return m_internalModel->maxCategorySum();
326 return m_internalModel->maxCategorySum();
336 }
327 }
337
328
338 BarChartModel& QBarSeriesPrivate::modelInternal()
329 BarChartModel& QBarSeriesPrivate::modelInternal()
339 {
330 {
340 return *m_internalModel;
331 return *m_internalModel;
341 }
332 }
342
333
343 bool QBarSeriesPrivate::setModel(QAbstractItemModel *model)
334 bool QBarSeriesPrivate::setModel(QAbstractItemModel *model)
344 {
335 {
345 // disconnect signals from old model
336 // disconnect signals from old model
346 if(m_model)
337 if(m_model)
347 {
338 {
348 disconnect(m_model, 0, this, 0);
339 disconnect(m_model, 0, this, 0);
349 m_mapCategories = -1;
340 m_mapCategories = -1;
350 m_mapBarBottom = -1;
341 m_mapBarBottom = -1;
351 m_mapBarTop = -1;
342 m_mapBarTop = -1;
352 m_mapFirst = 0;
343 m_mapFirst = 0;
353 m_mapCount = 0;
344 m_mapCount = 0;
354 m_mapOrientation = Qt::Vertical;
345 m_mapOrientation = Qt::Vertical;
355 }
346 }
356
347
357 // set new model
348 // set new model
358 if(model)
349 if(model)
359 {
350 {
360 m_model = model;
351 m_model = model;
361 return true;
352 return true;
362 }
353 }
363 else
354 else
364 {
355 {
365 m_model = 0;
356 m_model = 0;
366 return false;
357 return false;
367 }
358 }
368 }
359 }
369
360
370 void QBarSeriesPrivate::setModelMapping(int categories, int bottomBoundry, int topBoundry, Qt::Orientation orientation)
361 void QBarSeriesPrivate::setModelMapping(int categories, int bottomBoundry, int topBoundry, Qt::Orientation orientation)
371 {
362 {
372 Q_Q(QBarSeries);
363 Q_Q(QBarSeries);
373
364
374 if (m_model == 0)
365 if (m_model == 0)
375 return;
366 return;
376
367
377 m_mapCategories = categories;
368 m_mapCategories = categories;
378 m_mapBarBottom = bottomBoundry;
369 m_mapBarBottom = bottomBoundry;
379 m_mapBarTop = topBoundry;
370 m_mapBarTop = topBoundry;
380 // m_mapFirst = 1;
371 // m_mapFirst = 1;
381 m_mapOrientation = orientation;
372 m_mapOrientation = orientation;
382
373
383 // connect the signals
374 // connect the signals
384 if (m_mapOrientation == Qt::Vertical) {
375 if (m_mapOrientation == Qt::Vertical) {
385 m_mapCount = m_model->rowCount() - m_mapFirst;
376 m_mapCount = m_model->rowCount() - m_mapFirst;
386 connect(m_model,SIGNAL(dataChanged(QModelIndex,QModelIndex)),
377 connect(m_model,SIGNAL(dataChanged(QModelIndex,QModelIndex)),
387 this, SLOT(modelUpdated(QModelIndex,QModelIndex)));
378 this, SLOT(modelUpdated(QModelIndex,QModelIndex)));
388 connect(m_model,SIGNAL(rowsInserted(QModelIndex,int,int)),
379 connect(m_model,SIGNAL(rowsInserted(QModelIndex,int,int)),
389 this, SLOT(modelDataAdded(QModelIndex,int,int)));
380 this, SLOT(modelDataAdded(QModelIndex,int,int)));
390 connect(m_model, SIGNAL(rowsRemoved(QModelIndex,int,int)),
381 connect(m_model, SIGNAL(rowsRemoved(QModelIndex,int,int)),
391 this, SLOT(modelDataRemoved(QModelIndex,int,int)));
382 this, SLOT(modelDataRemoved(QModelIndex,int,int)));
392 } else {
383 } else {
393 m_mapCount = m_model->columnCount() - m_mapFirst;
384 m_mapCount = m_model->columnCount() - m_mapFirst;
394 connect(m_model,SIGNAL(dataChanged(QModelIndex,QModelIndex)),
385 connect(m_model,SIGNAL(dataChanged(QModelIndex,QModelIndex)),
395 this, SLOT(modelUpdated(QModelIndex,QModelIndex)));
386 this, SLOT(modelUpdated(QModelIndex,QModelIndex)));
396 connect(m_model,SIGNAL(columnsInserted(QModelIndex,int,int)),
387 connect(m_model,SIGNAL(columnsInserted(QModelIndex,int,int)),
397 this, SLOT(modelDataAdded(QModelIndex,int,int)));
388 this, SLOT(modelDataAdded(QModelIndex,int,int)));
398 connect(m_model, SIGNAL(columnsRemoved(QModelIndex,int,int)),
389 connect(m_model, SIGNAL(columnsRemoved(QModelIndex,int,int)),
399 this, SLOT(modelDataRemoved(QModelIndex,int,int)));
390 this, SLOT(modelDataRemoved(QModelIndex,int,int)));
400 }
391 }
401
392
402 // create the initial bars
393 // create the initial bars
403 delete m_internalModel;
394 delete m_internalModel;
404 if (m_mapOrientation == Qt::Vertical) {
395 if (m_mapOrientation == Qt::Vertical) {
405 QStringList categories;
396 QStringList categories;
406 for (int k = m_mapFirst; k < m_mapFirst + m_mapCount; k++)
397 for (int k = m_mapFirst; k < m_mapFirst + m_mapCount; k++)
407 categories << m_model->data(m_model->index(k, m_mapCategories), Qt::DisplayRole).toString();
398 categories << m_model->data(m_model->index(k, m_mapCategories), Qt::DisplayRole).toString();
408 m_internalModel = new BarChartModel(categories, this);
399 m_internalModel = new BarChartModel(categories, this);
409
400
410 for (int i = m_mapBarBottom; i <= m_mapBarTop; i++) {
401 for (int i = m_mapBarBottom; i <= m_mapBarTop; i++) {
411 QBarSet* barSet = new QBarSet(QString("Column: %1").arg(i + 1));
402 QBarSet* barSet = new QBarSet(QString("Column: %1").arg(i + 1));
412 for(int m = m_mapFirst; m < m_mapFirst + m_mapCount; m++)
403 for(int m = m_mapFirst; m < m_mapFirst + m_mapCount; m++)
413 *barSet << m_model->data(m_model->index(m, i), Qt::DisplayRole).toDouble();
404 *barSet << m_model->data(m_model->index(m, i), Qt::DisplayRole).toDouble();
414 q->appendBarSet(barSet);
405 q->appendBarSet(barSet);
415 }
406 }
416 } else {
407 } else {
417 QStringList categories;
408 QStringList categories;
418 for (int k = m_mapFirst; k < m_mapFirst + m_mapCount; k++)
409 for (int k = m_mapFirst; k < m_mapFirst + m_mapCount; k++)
419 categories << m_model->data(m_model->index(m_mapCategories, k), Qt::DisplayRole).toString();
410 categories << m_model->data(m_model->index(m_mapCategories, k), Qt::DisplayRole).toString();
420 m_internalModel = new BarChartModel(categories, this);
411 m_internalModel = new BarChartModel(categories, this);
421
412
422 for (int i = m_mapBarBottom; i <= m_mapBarTop; i++) {
413 for (int i = m_mapBarBottom; i <= m_mapBarTop; i++) {
423 QBarSet* barSet = new QBarSet(QString("Row: %1").arg(i + 1));
414 QBarSet* barSet = new QBarSet(QString("Row: %1").arg(i + 1));
424 for(int m = m_mapFirst; m < m_mapFirst + m_mapCount; m++)
415 for(int m = m_mapFirst; m < m_mapFirst + m_mapCount; m++)
425 *barSet << m_model->data(m_model->index(i, m), Qt::DisplayRole).toDouble();
416 *barSet << m_model->data(m_model->index(i, m), Qt::DisplayRole).toDouble();
426 q->appendBarSet(barSet);
417 q->appendBarSet(barSet);
427 }
418 }
428 }
419 }
429 }
420 }
430
421
431 void QBarSeriesPrivate::modelUpdated(QModelIndex topLeft, QModelIndex bottomRight)
422 void QBarSeriesPrivate::modelUpdated(QModelIndex topLeft, QModelIndex bottomRight)
432 {
423 {
433 Q_UNUSED(bottomRight)
424 Q_UNUSED(bottomRight)
434
425
435 if (m_mapOrientation == Qt::Vertical)
426 if (m_mapOrientation == Qt::Vertical)
436 {
427 {
437 // model update is relevant to BarSeries if the change was made to the part of the model that was mapped to BarSeries
428 // model update is relevant to BarSeries if the change was made to the part of the model that was mapped to BarSeries
438 if (topLeft.column() >= m_mapBarBottom && topLeft.column() <= m_mapBarTop && topLeft.row() >= m_mapFirst && topLeft.row() < m_mapFirst + m_mapCount)
429 if (topLeft.column() >= m_mapBarBottom && topLeft.column() <= m_mapBarTop && topLeft.row() >= m_mapFirst && topLeft.row() < m_mapFirst + m_mapCount)
439 barsetAt(topLeft.column() - m_mapBarBottom)->setValue(topLeft.row() - m_mapFirst, m_model->data(topLeft, Qt::DisplayRole).toDouble());
430 barsetAt(topLeft.column() - m_mapBarBottom)->setValue(topLeft.row() - m_mapFirst, m_model->data(topLeft, Qt::DisplayRole).toDouble());
440 }
431 }
441 else
432 else
442 {
433 {
443 // model update is relevant to BarSeries if the change was made to the part of the model that was mapped to BarSeries
434 // model update is relevant to BarSeries if the change was made to the part of the model that was mapped to BarSeries
444 if (topLeft.row() >= m_mapBarBottom && topLeft.row() <= m_mapBarTop && topLeft.column() >= m_mapFirst && topLeft.column() < m_mapFirst + m_mapCount)
435 if (topLeft.row() >= m_mapBarBottom && topLeft.row() <= m_mapBarTop && topLeft.column() >= m_mapFirst && topLeft.column() < m_mapFirst + m_mapCount)
445 barsetAt(topLeft.row() - m_mapBarBottom)->setValue(topLeft.column() - m_mapFirst, m_model->data(topLeft, Qt::DisplayRole).toDouble());
436 barsetAt(topLeft.row() - m_mapBarBottom)->setValue(topLeft.column() - m_mapFirst, m_model->data(topLeft, Qt::DisplayRole).toDouble());
446 }
437 }
447 }
438 }
448
439
449 void QBarSeriesPrivate::modelDataAdded(QModelIndex /*parent*/, int start, int /*end*/)
440 void QBarSeriesPrivate::modelDataAdded(QModelIndex /*parent*/, int start, int /*end*/)
450 {
441 {
451 Q_Q(QBarSeries);
442 Q_Q(QBarSeries);
452
443
453 if (m_mapOrientation == Qt::Vertical) {
444 if (m_mapOrientation == Qt::Vertical) {
454 q->insertCategory(start - m_mapFirst, QString("Row: %1").arg(start + 1));
445 q->insertCategory(start - m_mapFirst, QString("Row: %1").arg(start + 1));
455 for (int i = 0; i <= m_mapBarTop - m_mapBarBottom; i++) {
446 for (int i = 0; i <= m_mapBarTop - m_mapBarBottom; i++) {
456 barsetAt(i)->insertValue(start - m_mapFirst, m_model->data(m_model->index(start, i), Qt::DisplayRole).toDouble());
447 barsetAt(i)->insertValue(start - m_mapFirst, m_model->data(m_model->index(start, i), Qt::DisplayRole).toDouble());
457 }
448 }
458 } else {
449 } else {
459 q->insertCategory(start - m_mapFirst, QString("Column: %1").arg(start + 1));
450 q->insertCategory(start - m_mapFirst, QString("Column: %1").arg(start + 1));
460 for (int i = 0; i <= m_mapBarTop - m_mapBarBottom; i++) {
451 for (int i = 0; i <= m_mapBarTop - m_mapBarBottom; i++) {
461 barsetAt(i)->insertValue(start - m_mapFirst, m_model->data(m_model->index(i, start), Qt::DisplayRole).toDouble());
452 barsetAt(i)->insertValue(start - m_mapFirst, m_model->data(m_model->index(i, start), Qt::DisplayRole).toDouble());
462 }
453 }
463 }
454 }
464 emit restructuredBars();
455 emit restructuredBars();
465 }
456 }
466
457
467 void QBarSeriesPrivate::modelDataRemoved(QModelIndex parent, int start, int end)
458 void QBarSeriesPrivate::modelDataRemoved(QModelIndex parent, int start, int end)
468 {
459 {
469 Q_Q(QBarSeries);
460 Q_Q(QBarSeries);
470 Q_UNUSED(parent)
461 Q_UNUSED(parent)
471 Q_UNUSED(end)
462 Q_UNUSED(end)
472
463
473 q->removeCategory(start - m_mapFirst);
464 q->removeCategory(start - m_mapFirst);
474 for (int i = 0; i <= m_mapBarTop - m_mapBarBottom; i++)
465 for (int i = 0; i <= m_mapBarTop - m_mapBarBottom; i++)
475 {
466 {
476 barsetAt(i)->removeValue(start - m_mapFirst);
467 barsetAt(i)->removeValue(start - m_mapFirst);
477 }
468 }
478 emit restructuredBars();
469 emit restructuredBars();
479 }
470 }
480
471
481 void QBarSeriesPrivate::barsetChanged()
472 void QBarSeriesPrivate::barsetChanged()
482 {
473 {
483 emit updatedBars();
474 emit updatedBars();
484 }
475 }
485
476
486 void QBarSeriesPrivate::scaleDomain(Domain& domain)
477 void QBarSeriesPrivate::scaleDomain(Domain& domain)
487 {
478 {
488 qreal minX(domain.minX());
479 qreal minX(domain.minX());
489 qreal minY(domain.minY());
480 qreal minY(domain.minY());
490 qreal maxX(domain.maxX());
481 qreal maxX(domain.maxX());
491 qreal maxY(domain.maxY());
482 qreal maxY(domain.maxY());
492 int tickXCount(domain.tickXCount());
483 int tickXCount(domain.tickXCount());
493 int tickYCount(domain.tickYCount());
484 int tickYCount(domain.tickYCount());
494
485
495 qreal x = m_internalModel->categoryCount();
486 qreal x = m_internalModel->categoryCount();
496 qreal y = max();
487 qreal y = max();
497 minX = qMin(minX, x);
488 minX = qMin(minX, x);
498 minY = qMin(minY, y);
489 minY = qMin(minY, y);
499 maxX = qMax(maxX, x);
490 maxX = qMax(maxX, x);
500 maxY = qMax(maxY, y);
491 maxY = qMax(maxY, y);
501 tickXCount = x+1;
492 tickXCount = x+1;
502
493
503 domain.setRangeX(minX,maxX,tickXCount);
494 domain.setRangeX(minX,maxX,tickXCount);
504 domain.setRangeY(minY,maxY,tickYCount);
495 domain.setRangeY(minY,maxY,tickYCount);
505 }
496 }
506
497
507 Chart* QBarSeriesPrivate::createGraphics(ChartPresenter* presenter)
498 Chart* QBarSeriesPrivate::createGraphics(ChartPresenter* presenter)
508 {
499 {
509 Q_Q(QBarSeries);
500 Q_Q(QBarSeries);
510
501
511 BarChartItem* bar = new BarChartItem(q,presenter);
502 BarChartItem* bar = new BarChartItem(q,presenter);
512 if(presenter->animationOptions().testFlag(QChart::SeriesAnimations)) {
503 if(presenter->animationOptions().testFlag(QChart::SeriesAnimations)) {
513 presenter->animator()->addAnimation(bar);
504 presenter->animator()->addAnimation(bar);
514 }
505 }
515 presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q));
506 presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q));
516 return bar;
507 return bar;
517
508
518 }
509 }
519
510
520 QList<LegendMarker*> QBarSeriesPrivate::createLegendMarker(QLegend* legend)
511 QList<LegendMarker*> QBarSeriesPrivate::createLegendMarker(QLegend* legend)
521 {
512 {
522 Q_Q(QBarSeries);
513 Q_Q(QBarSeries);
523 QList<LegendMarker*> markers;
514 QList<LegendMarker*> markers;
524 foreach(QBarSet* set, q->barSets()) {
515 foreach(QBarSet* set, q->barSets()) {
525 BarLegendMarker* marker = new BarLegendMarker(q,set,legend);
516 BarLegendMarker* marker = new BarLegendMarker(q,set,legend);
526 markers << marker;
517 markers << marker;
527 }
518 }
528
519
529 return markers;
520 return markers;
530 }
521 }
531
522
532 #include "moc_qbarseries.cpp"
523 #include "moc_qbarseries.cpp"
533 #include "moc_qbarseries_p.cpp"
524 #include "moc_qbarseries_p.cpp"
534
525
535 QTCOMMERCIALCHART_END_NAMESPACE
526 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,71 +1,70
1 #ifndef QBARSERIES_P_H
1 #ifndef QBARSERIES_P_H
2 #define QBARSERIES_P_H
2 #define QBARSERIES_P_H
3
3
4 #include "qbarseries.h"
4 #include "qbarseries.h"
5 #include "qseries_p.h"
5 #include "qseries_p.h"
6 #include <QStringList>
6 #include <QStringList>
7 #include <QSeries>
7 #include <QSeries>
8
8
9 class QModelIndex;
9 class QModelIndex;
10
10
11 QTCOMMERCIALCHART_BEGIN_NAMESPACE
11 QTCOMMERCIALCHART_BEGIN_NAMESPACE
12
12
13 // Container for series
13 // Container for series
14 class QBarSeriesPrivate : public QSeriesPrivate
14 class QBarSeriesPrivate : public QSeriesPrivate
15 {
15 {
16 Q_OBJECT
16 Q_OBJECT
17 public:
17 public:
18 QBarSeriesPrivate(QBarCategories categories, QBarSeries *parent);
18 QBarSeriesPrivate(QBarCategories categories, QBarSeries *parent);
19
19
20 void scaleDomain(Domain& domain);
20 void scaleDomain(Domain& domain);
21 Chart* createGraphics(ChartPresenter* presenter);
21 Chart* createGraphics(ChartPresenter* presenter);
22 QList<LegendMarker*> createLegendMarker(QLegend* legend);
22 QList<LegendMarker*> createLegendMarker(QLegend* legend);
23
23
24 bool setModel(QAbstractItemModel *model);
24 bool setModel(QAbstractItemModel *model);
25 void setModelMapping(int categories, int bottomBoundry, int topBoundry, Qt::Orientation orientation = Qt::Vertical);
25 void setModelMapping(int categories, int bottomBoundry, int topBoundry, Qt::Orientation orientation = Qt::Vertical);
26
26
27 QBarSet* barsetAt(int index);
27 QBarSet* barsetAt(int index);
28 QString categoryName(int category);
28 QString categoryName(int category);
29 qreal min();
29 qreal min();
30 qreal max();
30 qreal max();
31 qreal valueAt(int set, int category);
31 qreal valueAt(int set, int category);
32 qreal percentageAt(int set, int category);
32 qreal percentageAt(int set, int category);
33 qreal categorySum(int category);
33 qreal categorySum(int category);
34 qreal absoluteCategorySum(int category);
34 qreal absoluteCategorySum(int category);
35 qreal maxCategorySum();
35 qreal maxCategorySum();
36 BarChartModel& modelInternal();
36 BarChartModel& modelInternal();
37
37
38 Q_SIGNALS:
38 Q_SIGNALS:
39 void clicked(QBarSet *barset, QString category, Qt::MouseButtons button); // Up to user of api, what to do with these signals
39 void clicked(QBarSet *barset, QString category, Qt::MouseButtons button);
40 void selected();
40 void selected();
41 void updatedBars();
41 void updatedBars();
42 void restructuredBars();
42 void restructuredBars();
43 void showToolTip(QPoint pos, QString tip);
43 void showToolTip(QPoint pos, QString tip);
44
44
45 public Q_SLOTS:
45 public Q_SLOTS:
46 void setToolTipEnabled(bool enabled = true); // enables tooltips
46 void setToolTipEnabled(bool enabled = true); // enables tooltips
47 void barsetClicked(QString category, Qt::MouseButtons button);
48
47
49 private Q_SLOTS:
48 private Q_SLOTS:
50 // slots for updating bars when data in model changes
49 // slots for updating bars when data in model changes
51 void modelUpdated(QModelIndex topLeft, QModelIndex bottomRight);
50 void modelUpdated(QModelIndex topLeft, QModelIndex bottomRight);
52 void modelDataAdded(QModelIndex parent, int start, int end);
51 void modelDataAdded(QModelIndex parent, int start, int end);
53 void modelDataRemoved(QModelIndex parent, int start, int end);
52 void modelDataRemoved(QModelIndex parent, int start, int end);
54 void barsetChanged();
53 void barsetChanged();
55
54
56 protected:
55 protected:
57 BarChartModel *m_internalModel; // TODO: this may change... current "2 models" situation doesn't look good.
56 BarChartModel *m_internalModel; // TODO: this may change... current "2 models" situation doesn't look good.
58 QAbstractItemModel* m_model;
57 QAbstractItemModel* m_model;
59 int m_mapCategories;
58 int m_mapCategories;
60 int m_mapBarBottom;
59 int m_mapBarBottom;
61 int m_mapBarTop;
60 int m_mapBarTop;
62 int m_mapFirst;
61 int m_mapFirst;
63 int m_mapCount;
62 int m_mapCount;
64 Qt::Orientation m_mapOrientation;
63 Qt::Orientation m_mapOrientation;
65 private:
64 private:
66 Q_DECLARE_PUBLIC(QBarSeries)
65 Q_DECLARE_PUBLIC(QBarSeries)
67 };
66 };
68
67
69 QTCOMMERCIALCHART_END_NAMESPACE
68 QTCOMMERCIALCHART_END_NAMESPACE
70
69
71 #endif // QBARSERIESPRIVATE_P_H
70 #endif // QBARSERIESPRIVATE_P_H
@@ -1,326 +1,324
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "qbarset.h"
21 #include "qbarset.h"
22 #include "qbarset_p.h"
22 #include "qbarset_p.h"
23 #include <QToolTip>
23 #include <QToolTip>
24
24
25 QTCOMMERCIALCHART_BEGIN_NAMESPACE
25 QTCOMMERCIALCHART_BEGIN_NAMESPACE
26
26
27 /*!
27 /*!
28 \class QBarSet
28 \class QBarSet
29 \brief part of QtCommercial chart API.
29 \brief part of QtCommercial chart API.
30
30
31 QBarSet represents one set of bars. Set of bars contains one data value for each category.
31 QBarSet represents one set of bars. Set of bars contains one data value for each category.
32 First value of set is assumed to belong to first category, second to second category and so on.
32 First value of set is assumed to belong to first category, second to second category and so on.
33 If set has fewer values than there are categories, then the missing values are assumed to be
33 If set has fewer values than there are categories, then the missing values are assumed to be
34 at the end of set. For missing values in middle of a set, numerical value of zero is used.
34 at the end of set. For missing values in middle of a set, numerical value of zero is used.
35
35
36 \mainclass
36 \mainclass
37
37
38 \sa QBarSeries, QStackedBarSeries, QPercentBarSeries
38 \sa QBarSeries, QStackedBarSeries, QPercentBarSeries
39 */
39 */
40
40
41 /*!
41 /*!
42 \fn void QBarSet::clicked(QString category, Qt::MouseButtons button)
42 \fn void QBarSet::clicked(QString category, Qt::MouseButtons button)
43 \brief signals that set has been clicked
43 \brief signals that set has been clicked
44 Parameter \a category describes on which category was clicked
44 Parameter \a category describes on which category was clicked
45 Parameter \a button mouse button
45 Parameter \a button mouse button
46 */
46 */
47
47
48 /*!
48 /*!
49 \fn void QBarSet::hoverEnter(QPoint pos)
49 \fn void QBarSet::hoverEnter(QPoint pos)
50 \brief signals that mouse has entered over the set at position \a pos.
50 \brief signals that mouse has entered over the set at position \a pos.
51 */
51 */
52
52
53 /*!
53 /*!
54 \fn void QBarSet::hoverLeave()
54 \fn void QBarSet::hoverLeave()
55 \brief signals that mouse has left from the set.
55 \brief signals that mouse has left from the set.
56 */
56 */
57
57
58 /*!
58 /*!
59 \fn void QBarSetPrivate::showToolTip(QPoint pos, QString tip)
59 \fn void QBarSetPrivate::showToolTip(QPoint pos, QString tip)
60 \brief \internal \a pos \a tip
60 \brief \internal \a pos \a tip
61 */
61 */
62
62
63
63
64 /*!
64 /*!
65 Constructs QBarSet with a name of \a name and with parent of \a parent
65 Constructs QBarSet with a name of \a name and with parent of \a parent
66 */
66 */
67 QBarSet::QBarSet(QString name, QObject *parent)
67 QBarSet::QBarSet(QString name, QObject *parent)
68 : QObject(parent)
68 : QObject(parent)
69 ,d_ptr(new QBarSetPrivate(name,this))
69 ,d_ptr(new QBarSetPrivate(name,this))
70 // ,m_name(name)
71 // ,m_labelsVisible(false)
72 {
70 {
73 }
71 }
74
72
75 QBarSet::~QBarSet()
73 QBarSet::~QBarSet()
76 {
74 {
77
75
78 }
76 }
79
77
80 /*!
78 /*!
81 Sets new \a name for set.
79 Sets new \a name for set.
82 */
80 */
83 void QBarSet::setName(QString name)
81 void QBarSet::setName(QString name)
84 {
82 {
85 d_ptr->m_name = name;
83 d_ptr->m_name = name;
86 }
84 }
87
85
88 /*!
86 /*!
89 Returns name of the set.
87 Returns name of the set.
90 */
88 */
91 QString QBarSet::name() const
89 QString QBarSet::name() const
92 {
90 {
93 return d_ptr->m_name;
91 return d_ptr->m_name;
94 }
92 }
95
93
96 /*!
94 /*!
97 Appends new value \a value to the end of set.
95 Appends new value \a value to the end of set.
98 */
96 */
99 QBarSet& QBarSet::operator << (const qreal &value)
97 QBarSet& QBarSet::operator << (const qreal &value)
100 {
98 {
101 d_ptr->m_values.append(value);
99 d_ptr->m_values.append(value);
102 emit d_ptr->structureChanged();
100 emit d_ptr->structureChanged();
103 return *this;
101 return *this;
104 }
102 }
105
103
106 /*!
104 /*!
107 Inserts new \a value on the \a i position.
105 Inserts new \a value on the \a i position.
108 The value that is currently at this postion is moved to postion i + 1
106 The value that is currently at this postion is moved to postion i + 1
109 \sa removeValue()
107 \sa removeValue()
110 */
108 */
111 void QBarSet::insertValue(int i, qreal value)
109 void QBarSet::insertValue(int i, qreal value)
112 {
110 {
113 d_ptr->m_values.insert(i, value);
111 d_ptr->m_values.insert(i, value);
114 }
112 }
115
113
116 /*!
114 /*!
117 Removes the value specified by \a i
115 Removes the value specified by \a i
118 \sa insertValue()
116 \sa insertValue()
119 */
117 */
120 void QBarSet::removeValue(int i)
118 void QBarSet::removeValue(int i)
121 {
119 {
122 d_ptr->m_values.removeAt(i);
120 d_ptr->m_values.removeAt(i);
123 }
121 }
124
122
125 /*!
123 /*!
126 Returns count of values in set.
124 Returns count of values in set.
127 */
125 */
128 int QBarSet::count() const
126 int QBarSet::count() const
129 {
127 {
130 return d_ptr->m_values.count();
128 return d_ptr->m_values.count();
131 }
129 }
132
130
133 /*!
131 /*!
134 Returns value of set indexed by \a index
132 Returns value of set indexed by \a index
135 */
133 */
136 qreal QBarSet::valueAt(int index) const
134 qreal QBarSet::valueAt(int index) const
137 {
135 {
138 return d_ptr->m_values.at(index);
136 return d_ptr->m_values.at(index);
139 }
137 }
140
138
141 /*!
139 /*!
142 Sets a new value \a value to set, indexed by \a index
140 Sets a new value \a value to set, indexed by \a index
143 */
141 */
144 void QBarSet::setValue(int index, qreal value)
142 void QBarSet::setValue(int index, qreal value)
145 {
143 {
146 d_ptr->m_values.replace(index,value);
144 d_ptr->m_values.replace(index,value);
147 emit d_ptr->valueChanged();
145 emit d_ptr->valueChanged();
148 }
146 }
149
147
150 /*!
148 /*!
151 Returns sum of all values in barset.
149 Returns sum of all values in barset.
152 */
150 */
153 qreal QBarSet::sum() const
151 qreal QBarSet::sum() const
154 {
152 {
155 qreal total(0);
153 qreal total(0);
156 for (int i=0; i < d_ptr->m_values.count(); i++) {
154 for (int i=0; i < d_ptr->m_values.count(); i++) {
157 total += d_ptr->m_values.at(i);
155 total += d_ptr->m_values.at(i);
158 }
156 }
159 return total;
157 return total;
160 }
158 }
161
159
162 /*!
160 /*!
163 Sets pen for set. Bars of this set are drawn using \a pen
161 Sets pen for set. Bars of this set are drawn using \a pen
164 */
162 */
165 void QBarSet::setPen(const QPen &pen)
163 void QBarSet::setPen(const QPen &pen)
166 {
164 {
167 if(d_ptr->m_pen!=pen){
165 if(d_ptr->m_pen!=pen){
168 d_ptr->m_pen = pen;
166 d_ptr->m_pen = pen;
169 emit d_ptr->valueChanged();
167 emit d_ptr->valueChanged();
170 }
168 }
171 }
169 }
172
170
173 /*!
171 /*!
174 Returns pen of the set.
172 Returns pen of the set.
175 */
173 */
176 QPen QBarSet::pen() const
174 QPen QBarSet::pen() const
177 {
175 {
178 return d_ptr->m_pen;
176 return d_ptr->m_pen;
179 }
177 }
180
178
181 /*!
179 /*!
182 Sets brush for the set. Bars of this set are drawn using \a brush
180 Sets brush for the set. Bars of this set are drawn using \a brush
183 */
181 */
184 void QBarSet::setBrush(const QBrush &brush)
182 void QBarSet::setBrush(const QBrush &brush)
185 {
183 {
186 if(d_ptr->m_brush!=brush){
184 if(d_ptr->m_brush!=brush){
187 d_ptr->m_brush = brush;
185 d_ptr->m_brush = brush;
188 emit d_ptr->valueChanged();
186 emit d_ptr->valueChanged();
189 }
187 }
190 }
188 }
191
189
192 /*!
190 /*!
193 Returns brush of the set.
191 Returns brush of the set.
194 */
192 */
195 QBrush QBarSet::brush() const
193 QBrush QBarSet::brush() const
196 {
194 {
197 return d_ptr->m_brush;
195 return d_ptr->m_brush;
198 }
196 }
199
197
200 /*!
198 /*!
201 Sets \a pen of the values that are drawn on top of this barset
199 Sets \a pen of the values that are drawn on top of this barset
202 */
200 */
203 void QBarSet::setLabelPen(const QPen &pen)
201 void QBarSet::setLabelPen(const QPen &pen)
204 {
202 {
205 if(d_ptr->m_labelPen!=pen){
203 if(d_ptr->m_labelPen!=pen){
206 d_ptr->m_labelPen = pen;
204 d_ptr->m_labelPen = pen;
207 emit d_ptr->valueChanged();
205 emit d_ptr->valueChanged();
208 }
206 }
209 }
207 }
210
208
211 /*!
209 /*!
212 Returns pen of the values that are drawn on top of this barset
210 Returns pen of the values that are drawn on top of this barset
213 */
211 */
214 QPen QBarSet::labelPen() const
212 QPen QBarSet::labelPen() const
215 {
213 {
216 return d_ptr->m_labelPen;
214 return d_ptr->m_labelPen;
217 }
215 }
218
216
219 /*!
217 /*!
220 Sets \a brush of the values that are drawn on top of this barset
218 Sets \a brush of the values that are drawn on top of this barset
221 */
219 */
222 void QBarSet::setLabelBrush(const QBrush &brush)
220 void QBarSet::setLabelBrush(const QBrush &brush)
223 {
221 {
224 if(d_ptr->m_labelBrush!=brush){
222 if(d_ptr->m_labelBrush!=brush){
225 d_ptr->m_labelBrush = brush;
223 d_ptr->m_labelBrush = brush;
226 emit d_ptr->valueChanged();
224 emit d_ptr->valueChanged();
227 }
225 }
228 }
226 }
229
227
230 /*!
228 /*!
231 Returns brush of the values that are drawn on top of this barset
229 Returns brush of the values that are drawn on top of this barset
232 */
230 */
233 QBrush QBarSet::labelBrush() const
231 QBrush QBarSet::labelBrush() const
234 {
232 {
235 return d_ptr->m_labelBrush;
233 return d_ptr->m_labelBrush;
236 }
234 }
237
235
238 /*!
236 /*!
239 Sets the \a font for values that are drawn on top of this barset
237 Sets the \a font for values that are drawn on top of this barset
240 */
238 */
241 void QBarSet::setLabelFont(const QFont &font)
239 void QBarSet::setLabelFont(const QFont &font)
242 {
240 {
243 if(d_ptr->m_labelFont!=font) {
241 if(d_ptr->m_labelFont!=font) {
244 d_ptr->m_labelFont = font;
242 d_ptr->m_labelFont = font;
245 emit d_ptr->valueChanged();
243 emit d_ptr->valueChanged();
246 }
244 }
247
245
248 }
246 }
249
247
250 /*!
248 /*!
251 Returns the pen for values that are drawn on top of this set
249 Returns the pen for values that are drawn on top of this set
252 */
250 */
253 QFont QBarSet::labelFont() const
251 QFont QBarSet::labelFont() const
254 {
252 {
255 return d_ptr->m_labelFont;
253 return d_ptr->m_labelFont;
256 }
254 }
257
255
258 /*!
256 /*!
259 Sets visibility of bar labels. If \a visible is true, labels are drawn on top of barsets.
257 Sets visibility of bar labels. If \a visible is true, labels are drawn on top of barsets.
260 */
258 */
261
259
262 void QBarSet::setLabelsVisible(bool visible)
260 void QBarSet::setLabelsVisible(bool visible)
263 {
261 {
264 if(d_ptr->m_labelsVisible!=visible) {
262 if(d_ptr->m_labelsVisible!=visible) {
265 d_ptr->m_labelsVisible = visible;
263 d_ptr->m_labelsVisible = visible;
266 emit d_ptr->labelsVisibleChanged(visible);
264 emit d_ptr->labelsVisibleChanged(visible);
267 }
265 }
268 }
266 }
269
267
270 /*!
268 /*!
271 Returns the visibility of values
269 Returns the visibility of values
272 */
270 */
273 bool QBarSet::labelsVisible() const
271 bool QBarSet::labelsVisible() const
274 {
272 {
275 return d_ptr->m_labelsVisible;
273 return d_ptr->m_labelsVisible;
276 }
274 }
277
275
278 /*
276 /*
279 void QBarSet::barHoverEnterEvent(QPoint pos)
277 void QBarSet::barHoverEnterEvent(QPoint pos)
280 {
278 {
281 emit showToolTip(pos, m_name);
279 emit showToolTip(pos, m_name);
282 emit hoverEnter(pos);
280 emit hoverEnter(pos);
283 }
281 }
284 */
282 */
285 /*
283 /*
286 void QBarSet::barHoverLeaveEvent()
284 void QBarSet::barHoverLeaveEvent()
287 {
285 {
288 // Emit signal to user of charts
286 // Emit signal to user of charts
289 emit hoverLeave();
287 emit hoverLeave();
290 }
288 }
291 */
289 */
292
290
293 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
291 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
294
292
295 QBarSetPrivate::QBarSetPrivate(QString name, QBarSet *parent) : QObject(parent),
293 QBarSetPrivate::QBarSetPrivate(QString name, QBarSet *parent) : QObject(parent),
296 q_ptr(parent),
294 q_ptr(parent),
297 m_name(name),
295 m_name(name),
298 m_labelsVisible(false)
296 m_labelsVisible(false)
299 {
297 {
300
298
301 }
299 }
302
300
303 QBarSetPrivate::~QBarSetPrivate()
301 QBarSetPrivate::~QBarSetPrivate()
304 {
302 {
305
303
306 }
304 }
307
305
308
306
309 //TODO: fixme , refactor it and get rid of it
307 //TODO: fixme , refactor it and get rid of it
310 void QBarSetPrivate::barHoverEnterEvent(QPoint pos)
308 void QBarSetPrivate::barHoverEnterEvent(QPoint pos)
311 {
309 {
312 emit showToolTip(pos, m_name);
310 emit showToolTip(pos, m_name);
313 emit hoverEnter(pos);
311 emit hoverEnter(pos);
314 }
312 }
315
313
316 //TODO: fixme , refactor it and get rid of it
314 //TODO: fixme , refactor it and get rid of it
317 void QBarSetPrivate::barHoverLeaveEvent()
315 void QBarSetPrivate::barHoverLeaveEvent()
318 {
316 {
319 // Emit signal to user of charts
317 // Emit signal to user of charts
320 emit hoverLeave();
318 emit hoverLeave();
321 }
319 }
322
320
323 #include "moc_qbarset.cpp"
321 #include "moc_qbarset.cpp"
324 #include "moc_qbarset_p.cpp"
322 #include "moc_qbarset_p.cpp"
325
323
326 QTCOMMERCIALCHART_END_NAMESPACE
324 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,81 +1,82
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #ifndef QBARSET_H
21 #ifndef QBARSET_H
22 #define QBARSET_H
22 #define QBARSET_H
23
23
24 #include <qchartglobal.h>
24 #include <qchartglobal.h>
25 #include <QPen>
25 #include <QPen>
26 #include <QBrush>
26 #include <QBrush>
27 #include <QFont>
27 #include <QFont>
28
28
29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30 class QBarSetPrivate;
30 class QBarSetPrivate;
31
31
32 class QTCOMMERCIALCHART_EXPORT QBarSet : public QObject
32 class QTCOMMERCIALCHART_EXPORT QBarSet : public QObject
33 {
33 {
34 Q_OBJECT
34 Q_OBJECT
35
35
36 public:
36 public:
37 QBarSet(QString name, QObject *parent = 0);
37 QBarSet(QString name, QObject *parent = 0);
38 virtual ~QBarSet();
38 virtual ~QBarSet();
39
39
40 void setName(QString name);
40 void setName(QString name);
41 QString name() const;
41 QString name() const;
42 QBarSet& operator << (const qreal &value); // appends new value to set
42 QBarSet& operator << (const qreal &value); // appends new value to set
43 void insertValue(int i, qreal value);
43 void insertValue(int i, qreal value);
44 void removeValue(int i);
44 void removeValue(int i);
45 int count() const; // count of values in set
45 int count() const; // count of values in set
46 qreal valueAt(int index) const; // for modifying individual values
46 qreal valueAt(int index) const; // for modifying individual values
47 void setValue(int index, qreal value); // setter for individual value
47 void setValue(int index, qreal value); // setter for individual value
48 qreal sum() const; // sum of all values in the set
48 qreal sum() const; // sum of all values in the set
49
49
50 void setPen(const QPen &pen);
50 void setPen(const QPen &pen);
51 QPen pen() const;
51 QPen pen() const;
52
52
53 void setBrush(const QBrush &brush);
53 void setBrush(const QBrush &brush);
54 QBrush brush() const;
54 QBrush brush() const;
55
55
56 void setLabelPen(const QPen &pen);
56 void setLabelPen(const QPen &pen);
57 QPen labelPen() const;
57 QPen labelPen() const;
58
58
59 void setLabelBrush(const QBrush &brush);
59 void setLabelBrush(const QBrush &brush);
60 QBrush labelBrush() const;
60 QBrush labelBrush() const;
61
61
62 void setLabelFont(const QFont &font);
62 void setLabelFont(const QFont &font);
63 QFont labelFont() const;
63 QFont labelFont() const;
64
64
65 void setLabelsVisible(bool visible = true);
65 void setLabelsVisible(bool visible = true);
66 bool labelsVisible() const;
66 bool labelsVisible() const;
67
67
68 Q_SIGNALS:
68 Q_SIGNALS:
69 void clicked(QString category, Qt::MouseButtons button); // Clicked and hover signals exposed to user
69 void clicked(QString category, Qt::MouseButtons button); // Clicked and hover signals exposed to user
70
70
71 private:
71 private:
72 QScopedPointer<QBarSetPrivate> d_ptr;
72 QScopedPointer<QBarSetPrivate> d_ptr;
73 Q_DISABLE_COPY(QBarSet)
73 Q_DISABLE_COPY(QBarSet)
74 friend class QBarSeries;
74 friend class QBarSeries;
75 friend class BarLegendMarker;
75 friend class BarLegendMarker;
76 friend class BarChartItem;
76 friend class BarChartItem;
77 friend class QBarSeriesPrivate;
77 };
78 };
78
79
79 QTCOMMERCIALCHART_END_NAMESPACE
80 QTCOMMERCIALCHART_END_NAMESPACE
80
81
81 #endif // QBARSET_H
82 #endif // QBARSET_H
General Comments 0
You need to be logged in to leave comments. Login now