##// END OF EJS Templates
simple text item for barvalue
sauimone -
r811:04debc5ee52b
parent child
Show More
@@ -1,236 +1,235
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 "barvalue_p.h"
23 #include "barvalue_p.h"
24 #include "qbarset.h"
24 #include "qbarset.h"
25 #include "qbarseries.h"
25 #include "qbarseries.h"
26 #include "qchart.h"
26 #include "qchart.h"
27 #include "qchartaxis.h"
27 #include "qchartaxis.h"
28 #include "qchartaxiscategories.h"
28 #include "qchartaxiscategories.h"
29 #include "chartpresenter_p.h"
29 #include "chartpresenter_p.h"
30 #include "chartanimator_p.h"
30 #include "chartanimator_p.h"
31 #include "chartdataset_p.h"
31 #include "chartdataset_p.h"
32 #include <QDebug>
32 #include <QDebug>
33 #include <QToolTip>
33 #include <QToolTip>
34
34
35 QTCOMMERCIALCHART_BEGIN_NAMESPACE
35 QTCOMMERCIALCHART_BEGIN_NAMESPACE
36
36
37 BarChartItem::BarChartItem(QBarSeries *series, ChartPresenter *presenter) :
37 BarChartItem::BarChartItem(QBarSeries *series, ChartPresenter *presenter) :
38 ChartItem(presenter),
38 ChartItem(presenter),
39 m_layoutSet(false),
39 m_layoutSet(false),
40 m_series(series)
40 m_series(series)
41 {
41 {
42 connect(series, SIGNAL(showToolTip(QPoint,QString)), this, SLOT(showToolTip(QPoint,QString)));
42 connect(series, SIGNAL(showToolTip(QPoint,QString)), this, SLOT(showToolTip(QPoint,QString)));
43 connect(series, SIGNAL(updatedBars()), this, SLOT(handleLayoutChanged()));
43 connect(series, SIGNAL(updatedBars()), this, SLOT(handleLayoutChanged()));
44 //TODO: connect(series,SIGNAL("position or size has changed"), this, SLOT(handleLayoutChanged()));
44 //TODO: connect(series,SIGNAL("position or size has changed"), this, SLOT(handleLayoutChanged()));
45 connect(series, SIGNAL(restructuredBar(int)), this, SLOT(handleModelChanged(int)));
45 connect(series, SIGNAL(restructuredBar(int)), this, SLOT(handleModelChanged(int)));
46 setZValue(ChartPresenter::BarSeriesZValue);
46 setZValue(ChartPresenter::BarSeriesZValue);
47 dataChanged();
47 dataChanged();
48 }
48 }
49
49
50 BarChartItem::~BarChartItem()
50 BarChartItem::~BarChartItem()
51 {
51 {
52 disconnect(this,SLOT(showToolTip(QPoint,QString)));
52 disconnect(this,SLOT(showToolTip(QPoint,QString)));
53 }
53 }
54
54
55 void BarChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
55 void BarChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
56 {
56 {
57 if (!m_layoutSet) {
57 if (!m_layoutSet) {
58 qWarning() << "BarChartItem::paint called without layout set. Aborting.";
58 qWarning() << "BarChartItem::paint called without layout set. Aborting.";
59 return;
59 return;
60 }
60 }
61
61
62 foreach(QGraphicsItem* i, childItems())
62 foreach(QGraphicsItem* i, childItems())
63 i->paint(painter,option,widget);
63 i->paint(painter,option,widget);
64 }
64 }
65
65
66 QRectF BarChartItem::boundingRect() const
66 QRectF BarChartItem::boundingRect() const
67 {
67 {
68 return m_rect;
68 return m_rect;
69 }
69 }
70
70
71 void BarChartItem::dataChanged()
71 void BarChartItem::dataChanged()
72 {
72 {
73 // TODO: performance optimizations. Do we really need to delete and create items every time data is changed or can we reuse them?
73 // TODO: performance optimizations. Do we really need to delete and create items every time data is changed or can we reuse them?
74 // Delete old bars
74 // Delete old bars
75 foreach (QGraphicsItem *item, childItems())
75 foreach (QGraphicsItem *item, childItems())
76 delete item;
76 delete item;
77
77
78 m_bars.clear();
78 m_bars.clear();
79 m_floatingValues.clear();
79 m_floatingValues.clear();
80 m_layout.clear();
80 m_layout.clear();
81
81
82 // Create new graphic items for bars
82 // Create new graphic items for bars
83 for (int c = 0; c < m_series->categoryCount(); c++) {
83 for (int c = 0; c < m_series->categoryCount(); c++) {
84 QString category = m_series->categoryName(c);
84 QString category = m_series->categoryName(c);
85 for (int s = 0; s < m_series->barsetCount(); s++) {
85 for (int s = 0; s < m_series->barsetCount(); s++) {
86 QBarSet *set = m_series->barsetAt(s);
86 QBarSet *set = m_series->barsetAt(s);
87 Bar *bar = new Bar(category,this);
87 Bar *bar = new Bar(category,this);
88 childItems().append(bar);
88 childItems().append(bar);
89 m_bars.append(bar);
89 m_bars.append(bar);
90 connect(bar, SIGNAL(clicked(QString)), set, SIGNAL(clicked(QString)));
90 connect(bar, SIGNAL(clicked(QString)), set, SIGNAL(clicked(QString)));
91 connect(bar, SIGNAL(rightClicked(QString)), set, SIGNAL(rightClicked(QString)));
91 connect(bar, SIGNAL(rightClicked(QString)), set, SIGNAL(rightClicked(QString)));
92 connect(bar, SIGNAL(hoverEntered(QPoint)), set, SLOT(barHoverEnterEvent(QPoint)));
92 connect(bar, SIGNAL(hoverEntered(QPoint)), set, SLOT(barHoverEnterEvent(QPoint)));
93 connect(bar, SIGNAL(hoverLeaved()), set, SLOT(barHoverLeaveEvent()));
93 connect(bar, SIGNAL(hoverLeaved()), set, SLOT(barHoverLeaveEvent()));
94 m_layout.append(QRectF(0, 0, 0, 0));
94 m_layout.append(QRectF(0, 0, 0, 0));
95 }
95 }
96 }
96 }
97
97
98 // Create floating values
98 // Create floating values
99 for (int category = 0; category < m_series->categoryCount(); category++) {
99 for (int category = 0; category < m_series->categoryCount(); category++) {
100 for (int s = 0; s < m_series->barsetCount(); s++) {
100 for (int s = 0; s < m_series->barsetCount(); s++) {
101 QBarSet *set = m_series->barsetAt(s);
101 QBarSet *set = m_series->barsetAt(s);
102 BarValue *value = new BarValue(*set, this);
102 BarValue *value = new BarValue(*set, this);
103 childItems().append(value);
103 childItems().append(value);
104 m_floatingValues.append(value);
104 m_floatingValues.append(value);
105 connect(set, SIGNAL(toggleFloatingValues()), value, SLOT(toggleVisible()));
105 connect(set, SIGNAL(toggleFloatingValues()), value, SLOT(toggleVisible()));
106 }
106 }
107 }
107 }
108 }
108 }
109
109
110 QVector<QRectF> BarChartItem::calculateLayout()
110 QVector<QRectF> BarChartItem::calculateLayout()
111 {
111 {
112 QVector<QRectF> layout;
112 QVector<QRectF> layout;
113
113
114 // Use temporary qreals for accurancy (we might get some compiler warnings... :)
114 // Use temporary qreals for accurancy (we might get some compiler warnings... :)
115 qreal categoryCount = m_series->categoryCount();
115 qreal categoryCount = m_series->categoryCount();
116 qreal setCount = m_series->barsetCount();
116 qreal setCount = m_series->barsetCount();
117
117
118 qreal width = geometry().width();
118 qreal width = geometry().width();
119 qreal height = geometry().height();
119 qreal height = geometry().height();
120
120
121 qreal max = m_series->max();
121 qreal max = m_series->max();
122
122
123 // Domain:
123 // Domain:
124 if (m_domainMaxY > max) {
124 if (m_domainMaxY > max) {
125 max = m_domainMaxY;
125 max = m_domainMaxY;
126 }
126 }
127
127
128 qreal scale = (height / max);
128 qreal scale = (height / max);
129 qreal categoryWidth = width / categoryCount;
129 qreal categoryWidth = width / categoryCount;
130 qreal barWidth = categoryWidth / (setCount+1);
130 qreal barWidth = categoryWidth / (setCount+1);
131
131
132 int itemIndex(0);
132 int itemIndex(0);
133 for (int category = 0; category < categoryCount; category++) {
133 for (int category = 0; category < categoryCount; category++) {
134 qreal xPos = categoryWidth * category + barWidth / 2;
134 qreal xPos = categoryWidth * category + barWidth / 2;
135 qreal yPos = height;
135 qreal yPos = height;
136 for (int set = 0; set < setCount; set++) {
136 for (int set = 0; set < setCount; set++) {
137 qreal barHeight = m_series->valueAt(set, category) * scale;
137 qreal barHeight = m_series->valueAt(set, category) * scale;
138 Bar* bar = m_bars.at(itemIndex);
138 Bar* bar = m_bars.at(itemIndex);
139
139
140 QRectF rect(xPos, yPos - barHeight, barWidth, barHeight);
140 QRectF rect(xPos, yPos - barHeight, barWidth, barHeight);
141 layout.append(rect);
141 layout.append(rect);
142 bar->setPen(m_series->barsetAt(set)->pen());
142 bar->setPen(m_series->barsetAt(set)->pen());
143 bar->setBrush(m_series->barsetAt(set)->brush());
143 bar->setBrush(m_series->barsetAt(set)->brush());
144 itemIndex++;
144 itemIndex++;
145 xPos += barWidth;
145 xPos += barWidth;
146 }
146 }
147 }
147 }
148
148
149 // Position floating values
149 // Position floating values
150 itemIndex = 0;
150 itemIndex = 0;
151 for (int category = 0; category < m_series->categoryCount(); category++) {
151 for (int category = 0; category < m_series->categoryCount(); category++) {
152 qreal xPos = categoryWidth * category + barWidth;
152 qreal xPos = categoryWidth * category + barWidth;
153 qreal yPos = height;
153 qreal yPos = height;
154 for (int set=0; set < m_series->barsetCount(); set++) {
154 for (int set=0; set < m_series->barsetCount(); set++) {
155 qreal barHeight = m_series->valueAt(set, category) * scale;
155 qreal barHeight = m_series->valueAt(set, category) * scale;
156 BarValue* value = m_floatingValues.at(itemIndex);
156 BarValue* value = m_floatingValues.at(itemIndex);
157
157
158 QBarSet* barSet = m_series->barsetAt(set);
158 QBarSet* barSet = m_series->barsetAt(set);
159 value->resize(100, 50); // TODO: proper layout for this.
160 value->setPos(xPos, yPos-barHeight / 2);
161 value->setPen(barSet->floatingValuePen());
162
159
163 if (!qFuzzyIsNull(m_series->valueAt(set,category))) {
160 if (!qFuzzyIsNull(m_series->valueAt(set,category))) {
164 value->setText(QString::number(m_series->valueAt(set, category)));
161 value->setText(QString::number(m_series->valueAt(set, category)));
165 } else {
162 } else {
166 value->setText(QString(""));
163 value->setText(QString(""));
167 }
164 }
168
165
166 value->setPos(xPos, yPos-barHeight / 2);
167 value->setPen(barSet->floatingValuePen());
168
169 itemIndex++;
169 itemIndex++;
170 xPos += barWidth;
170 xPos += barWidth;
171 }
171 }
172 }
172 }
173
173
174 return layout;
174 return layout;
175 }
175 }
176
176
177 void BarChartItem::applyLayout(const QVector<QRectF> &layout)
177 void BarChartItem::applyLayout(const QVector<QRectF> &layout)
178 {
178 {
179 if (animator())
179 if (animator())
180 animator()->updateLayout(this, m_layout, layout);
180 animator()->updateLayout(this, m_layout, layout);
181 else
181 else
182 setLayout(layout);
182 setLayout(layout);
183 }
183 }
184
184
185 void BarChartItem::setLayout(const QVector<QRectF> &layout)
185 void BarChartItem::setLayout(const QVector<QRectF> &layout)
186 {
186 {
187 m_layout = layout;
187 m_layout = layout;
188
188
189 for (int i=0; i < m_bars.count(); i++)
189 for (int i=0; i < m_bars.count(); i++)
190 m_bars.at(i)->setRect(layout.at(i));
190 m_bars.at(i)->setRect(layout.at(i));
191
191
192 update();
192 update();
193 }
193 }
194
194
195 //handlers
195 //handlers
196
196
197 void BarChartItem::handleModelChanged(int index)
197 void BarChartItem::handleModelChanged(int index)
198 {
198 {
199 Q_UNUSED(index)
199 Q_UNUSED(index)
200 dataChanged();
200 dataChanged();
201 }
201 }
202
202
203 void BarChartItem::handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY)
203 void BarChartItem::handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY)
204 {
204 {
205 m_domainMinX = minX;
205 m_domainMinX = minX;
206 m_domainMaxX = maxX;
206 m_domainMaxX = maxX;
207 m_domainMinY = minY;
207 m_domainMinY = minY;
208 m_domainMaxY = maxY;
208 m_domainMaxY = maxY;
209 handleLayoutChanged();
209 handleLayoutChanged();
210 }
210 }
211
211
212 void BarChartItem::handleGeometryChanged(const QRectF &rect)
212 void BarChartItem::handleGeometryChanged(const QRectF &rect)
213 {
213 {
214 m_rect = rect;
214 m_rect = rect;
215 handleLayoutChanged();
215 handleLayoutChanged();
216 m_layoutSet = true;
216 m_layoutSet = true;
217 setPos(rect.topLeft());
217 setPos(rect.topLeft());
218 }
218 }
219
219
220 void BarChartItem::handleLayoutChanged()
220 void BarChartItem::handleLayoutChanged()
221 {
221 {
222 QVector<QRectF> layout = calculateLayout();
222 QVector<QRectF> layout = calculateLayout();
223 applyLayout(layout);
223 applyLayout(layout);
224 update();
224 update();
225 }
225 }
226
226
227
228 void BarChartItem::showToolTip(QPoint pos, QString tip)
227 void BarChartItem::showToolTip(QPoint pos, QString tip)
229 {
228 {
230 // TODO: cool tooltip instead of default
229 // TODO: cool tooltip instead of default
231 QToolTip::showText(pos, tip);
230 QToolTip::showText(pos, tip);
232 }
231 }
233
232
234 #include "moc_barchartitem_p.cpp"
233 #include "moc_barchartitem_p.cpp"
235
234
236 QTCOMMERCIALCHART_END_NAMESPACE
235 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,93 +1,92
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 BARCHARTITEM_H
21 #ifndef BARCHARTITEM_H
22 #define BARCHARTITEM_H
22 #define BARCHARTITEM_H
23
23
24 #include "chartitem_p.h"
24 #include "chartitem_p.h"
25 #include "qbarseries.h"
25 #include "qbarseries.h"
26 #include <QPen>
26 #include <QPen>
27 #include <QBrush>
27 #include <QBrush>
28 #include <QGraphicsItem>
29
28
30 QTCOMMERCIALCHART_BEGIN_NAMESPACE
29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
31
30
32 class Bar;
31 class Bar;
33 class BarValue;
32 class BarValue;
34 class QChartAxisCategories;
33 class QChartAxisCategories;
35 class QChart;
34 class QChart;
36
35
37 //typedef QVector<QRectF> BarLayout;
36 //typedef QVector<QRectF> BarLayout;
38
37
39 class BarChartItem : public ChartItem
38 class BarChartItem : public ChartItem
40 {
39 {
41 Q_OBJECT
40 Q_OBJECT
42 public:
41 public:
43 BarChartItem(QBarSeries *series, ChartPresenter *presenter);
42 BarChartItem(QBarSeries *series, ChartPresenter *presenter);
44 virtual ~BarChartItem();
43 virtual ~BarChartItem();
45
44
46 // Common implemantation of different presenters. Not to be instantiated.
45 // Common implemantation of different presenters. Not to be instantiated.
47 // TODO: combine this with BarPresenter and derive other presenters from it?
46 // TODO: combine this with BarPresenter and derive other presenters from it?
48
47
49 public:
48 public:
50 // From QGraphicsItem
49 // From QGraphicsItem
51 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
50 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
52 QRectF boundingRect() const;
51 QRectF boundingRect() const;
53
52
54 // TODO: Consider the domain for layoutChanged. May be use case, may not be. If it is, then the derived classes need to implement it
53 // TODO: Consider the domain for layoutChanged. May be use case, may not be. If it is, then the derived classes need to implement it
55 virtual void dataChanged(); // data of series has changed -> need to recalculate bar sizes
54 virtual void dataChanged(); // data of series has changed -> need to recalculate bar sizes
56
55
57 virtual QVector<QRectF> calculateLayout();
56 virtual QVector<QRectF> calculateLayout();
58 void applyLayout(const QVector<QRectF> &layout);
57 void applyLayout(const QVector<QRectF> &layout);
59 void setLayout(const QVector<QRectF> &layout);
58 void setLayout(const QVector<QRectF> &layout);
60 void updateLayout(const QVector<QRectF> &layout);
59 void updateLayout(const QVector<QRectF> &layout);
61
60
62 QRectF geometry() const { return m_rect;}
61 QRectF geometry() const { return m_rect;}
63
62
64 public Q_SLOTS:
63 public Q_SLOTS:
65 void handleModelChanged(int index);
64 void handleModelChanged(int index);
66 void handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY);
65 void handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY);
67 void handleGeometryChanged(const QRectF &size);
66 void handleGeometryChanged(const QRectF &size);
68 void handleLayoutChanged();
67 void handleLayoutChanged();
69
68
70 // Internal slots
69 // Internal slots
71 void showToolTip(QPoint pos, QString tip); // shows tooltip (if enabled)
70 void showToolTip(QPoint pos, QString tip); // shows tooltip (if enabled)
72
71
73 protected:
72 protected:
74
73
75 // TODO: consider these.
74 // TODO: consider these.
76 qreal m_domainMinX;
75 qreal m_domainMinX;
77 qreal m_domainMaxX;
76 qreal m_domainMaxX;
78 qreal m_domainMinY;
77 qreal m_domainMinY;
79 qreal m_domainMaxY;
78 qreal m_domainMaxY;
80
79
81 QRectF m_rect;
80 QRectF m_rect;
82 bool m_layoutSet; // True, if component has been laid out.
81 bool m_layoutSet; // True, if component has been laid out.
83 QVector<QRectF> m_layout;
82 QVector<QRectF> m_layout;
84
83
85 // Not owned.
84 // Not owned.
86 QBarSeries *m_series;
85 QBarSeries *m_series;
87 QList<Bar *> m_bars;
86 QList<Bar *> m_bars;
88 QList<BarValue *> m_floatingValues;
87 QList<BarValue *> m_floatingValues;
89 };
88 };
90
89
91 QTCOMMERCIALCHART_END_NAMESPACE
90 QTCOMMERCIALCHART_END_NAMESPACE
92
91
93 #endif // BARCHARTITEM_H
92 #endif // BARCHARTITEM_H
@@ -1,93 +1,88
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 "barvalue_p.h"
21 #include "barvalue_p.h"
22 #include <QPainter>
22 #include <QPainter>
23 #include <QPen>
23 #include <QPen>
24 #include <QGraphicsSimpleTextItem>
24
25
25 QTCOMMERCIALCHART_BEGIN_NAMESPACE
26 QTCOMMERCIALCHART_BEGIN_NAMESPACE
26
27
27 BarValue::BarValue(QBarSet &set, QGraphicsItem *parent)
28 BarValue::BarValue(QBarSet &set, QGraphicsItem *parent) : QGraphicsObject(parent),
28 : QGraphicsObject(parent),
29 m_barSet(set),
29 m_barSet(set),
30 m_xPos(0),
30 m_textItem(new QGraphicsSimpleTextItem(this))
31 m_yPos(0),
32 m_width(0),
33 m_height(0)
34 {
31 {
35 setVisible(false);
32 setVisible(false);
36 }
33 }
37
34
38 void BarValue::setText(QString str)
35 void BarValue::setText(QString str)
39 {
36 {
40 m_valueString = str;
37 m_textItem->setText(str);
41 }
38 }
42
39
43 QString BarValue::text() const
40 QString BarValue::text() const
44 {
41 {
45 return m_valueString;
42 return m_textItem->text();
46 }
43 }
47
44
48 void BarValue::setPen(const QPen &pen)
45 void BarValue::setPen(const QPen &pen)
49 {
46 {
50 m_pen = pen;
47 m_textItem->setPen(pen);
51 }
48 }
52
49
53 QPen BarValue::pen() const
50 QPen BarValue::pen() const
54 {
51 {
55 return m_pen;
52 return m_textItem->pen();
56 }
53 }
57
54
58 void BarValue::resize(qreal w, qreal h)
55 void BarValue::setFont(const QFont &font)
59 {
56 {
60 m_width = w;
57 m_textItem->setFont(font);
61 m_height = h;
58 }
59
60 QFont BarValue::font() const
61 {
62 return m_textItem->font();
62 }
63 }
63
64
64 void BarValue::setPos(qreal x, qreal y)
65 void BarValue::setPos(qreal x, qreal y)
65 {
66 {
66 m_xPos = x;
67 m_textItem->setPos(x,y);
67 m_yPos = y;
68 }
68 }
69
69
70 void BarValue::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
70 void BarValue::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
71 {
71 {
72 Q_UNUSED(option)
72 Q_UNUSED(option)
73 Q_UNUSED(widget)
73 Q_UNUSED(widget)
74
75 if (isVisible()) {
76 painter->setPen(m_pen);
77 painter->drawText(boundingRect(), m_valueString);
78 }
79 }
74 }
80
75
81 QRectF BarValue::boundingRect() const
76 QRectF BarValue::boundingRect() const
82 {
77 {
83 QRectF r(m_xPos, m_yPos, m_width, m_height);
78 return m_textItem->boundingRect();
84 return r;
85 }
79 }
86
80
87 void BarValue::toggleVisible()
81 void BarValue::toggleVisible()
88 {
82 {
83 qDebug() << "toggle visible";
89 setVisible(!isVisible());
84 setVisible(!isVisible());
90 }
85 }
91
86
92 #include "moc_barvalue_p.cpp"
87 #include "moc_barvalue_p.cpp"
93 QTCOMMERCIALCHART_END_NAMESPACE
88 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,70 +1,67
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 BARVALUE_P_H
21 #ifndef BARVALUE_P_H
22 #define BARVALUE_P_H
22 #define BARVALUE_P_H
23
23
24 #include "qchartglobal.h"
24 #include "qchartglobal.h"
25 #include <QGraphicsObject>
25 #include <QGraphicsObject>
26 #include <QPen>
26 #include <QPen>
27 class QGraphicsSimpleTextItem;
27
28
28 QTCOMMERCIALCHART_BEGIN_NAMESPACE
29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
29
30
30 class QBarSet;
31 class QBarSet;
31
32
32 // Visual class for floating bar values
33 // Visual class for floating bar values
33 // By default these are not visible.
34 // By default these are not visible.
34 class BarValue : public QGraphicsObject
35 class BarValue : public QGraphicsObject
35 {
36 {
36 Q_OBJECT
37 Q_OBJECT
37 public:
38 public:
38 BarValue(QBarSet &set, QGraphicsItem *parent = 0);
39 BarValue(QBarSet &set, QGraphicsItem *parent = 0);
39
40
40 void setText(QString str);
41 void setText(QString str);
41 QString text() const;
42 QString text() const;
42
43
43 void setPen(const QPen &pen);
44 void setPen(const QPen &pen);
44 QPen pen() const;
45 QPen pen() const;
45
46
46 void resize(qreal w, qreal h);
47 void setFont(const QFont &font);
48 QFont font() const;
49
47 void setPos(qreal x, qreal y);
50 void setPos(qreal x, qreal y);
48
51
49 // From QGraphicsItem
52 // From QGraphicsItem
50 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
53 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
51 QRectF boundingRect() const;
54 QRectF boundingRect() const;
52
55
53 public Q_SLOTS:
56 public Q_SLOTS:
54 void toggleVisible();
57 void toggleVisible();
55
58
56 private:
59 private:
57
60
58 QBarSet &m_barSet;
61 QBarSet &m_barSet;
59 QPen m_pen;
62 QGraphicsSimpleTextItem *m_textItem;
60 QString m_valueString;
61
62 qreal m_xPos;
63 qreal m_yPos;
64 qreal m_width;
65 qreal m_height;
66 };
63 };
67
64
68 QTCOMMERCIALCHART_END_NAMESPACE
65 QTCOMMERCIALCHART_END_NAMESPACE
69
66
70 #endif // BARVALUE_P_H
67 #endif // BARVALUE_P_H
@@ -1,101 +1,101
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 "percentbarchartitem_p.h"
21 #include "percentbarchartitem_p.h"
22 #include "bar_p.h"
22 #include "bar_p.h"
23 #include "barvalue_p.h"
23 #include "barvalue_p.h"
24 #include "qbarset.h"
24 #include "qbarset.h"
25 #include <QDebug>
25 #include <QDebug>
26
26
27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28
28
29 PercentBarChartItem::PercentBarChartItem(QBarSeries *series, ChartPresenter *presenter) :
29 PercentBarChartItem::PercentBarChartItem(QBarSeries *series, ChartPresenter *presenter) :
30 BarChartItem(series, presenter)
30 BarChartItem(series, presenter)
31 {
31 {
32 }
32 }
33
33
34 QVector<QRectF> PercentBarChartItem::calculateLayout()
34 QVector<QRectF> PercentBarChartItem::calculateLayout()
35 {
35 {
36 QVector<QRectF> layout;
36 QVector<QRectF> layout;
37
37
38 // Use temporary qreals for accurancy (we might get some compiler warnings... :)
38 // Use temporary qreals for accurancy (we might get some compiler warnings... :)
39 qreal width = geometry().width();
39 qreal width = geometry().width();
40 qreal height = geometry().height();
40 qreal height = geometry().height();
41
41
42 qreal categoryCount = m_series->categoryCount();
42 qreal categoryCount = m_series->categoryCount();
43 qreal barWidth = width / (m_series->categoryCount() * 2);
43 qreal barWidth = width / (m_series->categoryCount() * 2);
44 qreal xStep = width / categoryCount;
44 qreal xStep = width / categoryCount;
45 qreal xPos = xStep / 2 - barWidth / 2;
45 qreal xPos = xStep / 2 - barWidth / 2;
46
46
47 int itemIndex(0);
47 int itemIndex(0);
48 for (int category = 0; category < categoryCount; category++) {
48 for (int category = 0; category < categoryCount; category++) {
49 qreal colSum = m_series->categorySum(category);
49 qreal colSum = m_series->categorySum(category);
50 qreal scale = (height / colSum);
50 qreal scale = (height / colSum);
51 qreal yPos = height;
51 qreal yPos = height;
52 for (int set=0; set < m_series->barsetCount(); set++) {
52 for (int set=0; set < m_series->barsetCount(); set++) {
53 qreal barHeight = m_series->valueAt(set, category) * scale;
53 qreal barHeight = m_series->valueAt(set, category) * scale;
54 Bar* bar = m_bars.at(itemIndex);
54 Bar* bar = m_bars.at(itemIndex);
55 bar->setPen(m_series->barsetAt(set)->pen());
55 bar->setPen(m_series->barsetAt(set)->pen());
56 bar->setBrush(m_series->barsetAt(set)->brush());
56 bar->setBrush(m_series->barsetAt(set)->brush());
57 QRectF rect(xPos, yPos-barHeight, barWidth, barHeight);
57 QRectF rect(xPos, yPos-barHeight, barWidth, barHeight);
58 layout.append(rect);
58 layout.append(rect);
59 itemIndex++;
59 itemIndex++;
60 yPos -= barHeight;
60 yPos -= barHeight;
61 }
61 }
62 xPos += xStep;
62 xPos += xStep;
63 }
63 }
64
64
65 // Position floating values
65 // Position floating values
66 itemIndex = 0;
66 itemIndex = 0;
67 xPos = (width/categoryCount);
67 xPos = (width/categoryCount);
68 for (int category=0; category < m_series->categoryCount(); category++) {
68 for (int category=0; category < m_series->categoryCount(); category++) {
69 qreal yPos = height;
69 qreal yPos = height;
70 qreal colSum = m_series->categorySum(category);
70 qreal colSum = m_series->categorySum(category);
71 qreal scale = (height / colSum);
71 qreal scale = (height / colSum);
72 for (int set=0; set < m_series->barsetCount(); set++) {
72 for (int set=0; set < m_series->barsetCount(); set++) {
73 qreal barHeight = m_series->valueAt(set,category) * scale;
73 qreal barHeight = m_series->valueAt(set,category) * scale;
74 BarValue* value = m_floatingValues.at(itemIndex);
74 BarValue* value = m_floatingValues.at(itemIndex);
75
75
76 QBarSet* barSet = m_series->barsetAt(set);
76 QBarSet* barSet = m_series->barsetAt(set);
77 value->resize(100, 50); // TODO: proper layout for this.
78 value->setPos(xPos, yPos-barHeight / 2);
79 value->setPen(barSet->floatingValuePen());
80
77
81 if (!qFuzzyIsNull(m_series->valueAt(set,category))) {
78 if (!qFuzzyIsNull(m_series->valueAt(set,category))) {
82 int p = m_series->percentageAt(set,category) * 100;
79 int p = m_series->percentageAt(set,category) * 100;
83 QString vString(QString::number(p));
80 QString vString(QString::number(p));
84 vString.truncate(3);
81 vString.truncate(3);
85 vString.append("%");
82 vString.append("%");
86 value->setText(vString);
83 value->setText(vString);
87 } else {
84 } else {
88 value->setText(QString(""));
85 value->setText(QString(""));
89 }
86 }
90
87
88 value->setPos(xPos, yPos-barHeight / 2);
89 value->setPen(barSet->floatingValuePen());
90
91 itemIndex++;
91 itemIndex++;
92 yPos -= barHeight;
92 yPos -= barHeight;
93 }
93 }
94 xPos += xStep;
94 xPos += xStep;
95 }
95 }
96 return layout;
96 return layout;
97 }
97 }
98
98
99 #include "moc_percentbarchartitem_p.cpp"
99 #include "moc_percentbarchartitem_p.cpp"
100
100
101 QTCOMMERCIALCHART_END_NAMESPACE
101 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,103 +1,103
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 "stackedbarchartitem_p.h"
21 #include "stackedbarchartitem_p.h"
22 #include "bar_p.h"
22 #include "bar_p.h"
23 #include "barvalue_p.h"
23 #include "barvalue_p.h"
24 #include "qbarset.h"
24 #include "qbarset.h"
25 #include <QDebug>
25 #include <QDebug>
26
26
27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28
28
29 StackedBarChartItem::StackedBarChartItem(QBarSeries *series, ChartPresenter *presenter) :
29 StackedBarChartItem::StackedBarChartItem(QBarSeries *series, ChartPresenter *presenter) :
30 BarChartItem(series, presenter)
30 BarChartItem(series, presenter)
31 {
31 {
32 }
32 }
33
33
34 StackedBarChartItem::~StackedBarChartItem()
34 StackedBarChartItem::~StackedBarChartItem()
35 {
35 {
36 }
36 }
37
37
38 QVector<QRectF> StackedBarChartItem::calculateLayout()
38 QVector<QRectF> StackedBarChartItem::calculateLayout()
39 {
39 {
40 QVector<QRectF> layout;
40 QVector<QRectF> layout;
41 // Use temporary qreals for accurancy (we might get some compiler warnings... :)
41 // Use temporary qreals for accurancy (we might get some compiler warnings... :)
42
42
43 qreal maxSum = m_series->maxCategorySum();
43 qreal maxSum = m_series->maxCategorySum();
44 // Domain:
44 // Domain:
45 if (m_domainMaxY > maxSum) {
45 if (m_domainMaxY > maxSum) {
46 maxSum = m_domainMaxY;
46 maxSum = m_domainMaxY;
47 }
47 }
48
48
49 qreal height = geometry().height();
49 qreal height = geometry().height();
50 qreal width = geometry().width();
50 qreal width = geometry().width();
51 qreal scale = (height / m_series->maxCategorySum());
51 qreal scale = (height / m_series->maxCategorySum());
52 qreal categotyCount = m_series->categoryCount();
52 qreal categotyCount = m_series->categoryCount();
53 qreal barWidth = width / (categotyCount * 2);
53 qreal barWidth = width / (categotyCount * 2);
54 qreal xStep = width / categotyCount;
54 qreal xStep = width / categotyCount;
55 qreal xPos = xStep / 2 - barWidth / 2;
55 qreal xPos = xStep / 2 - barWidth / 2;
56
56
57 int itemIndex(0);
57 int itemIndex(0);
58 for (int category = 0; category < categotyCount; category++) {
58 for (int category = 0; category < categotyCount; category++) {
59 qreal yPos = height;
59 qreal yPos = height;
60 for (int set=0; set < m_series->barsetCount(); set++) {
60 for (int set=0; set < m_series->barsetCount(); set++) {
61 qreal barHeight = m_series->valueAt(set, category) * scale;
61 qreal barHeight = m_series->valueAt(set, category) * scale;
62 Bar* bar = m_bars.at(itemIndex);
62 Bar* bar = m_bars.at(itemIndex);
63 bar->setPen(m_series->barsetAt(set)->pen());
63 bar->setPen(m_series->barsetAt(set)->pen());
64 bar->setBrush(m_series->barsetAt(set)->brush());
64 bar->setBrush(m_series->barsetAt(set)->brush());
65 QRectF rect(xPos, yPos-barHeight, barWidth, barHeight);
65 QRectF rect(xPos, yPos-barHeight, barWidth, barHeight);
66 layout.append(rect);
66 layout.append(rect);
67 itemIndex++;
67 itemIndex++;
68 yPos -= barHeight;
68 yPos -= barHeight;
69 }
69 }
70 xPos += xStep;
70 xPos += xStep;
71 }
71 }
72
72
73 // Position floating values
73 // Position floating values
74 itemIndex = 0;
74 itemIndex = 0;
75 xPos = (width/categotyCount);
75 xPos = (width/categotyCount);
76 for (int category=0; category < m_series->categoryCount(); category++) {
76 for (int category=0; category < m_series->categoryCount(); category++) {
77 qreal yPos = height;
77 qreal yPos = height;
78 for (int set=0; set < m_series->barsetCount(); set++) {
78 for (int set=0; set < m_series->barsetCount(); set++) {
79 qreal barHeight = m_series->valueAt(set, category) * scale;
79 qreal barHeight = m_series->valueAt(set, category) * scale;
80 BarValue* value = m_floatingValues.at(itemIndex);
80 BarValue* value = m_floatingValues.at(itemIndex);
81
81
82 QBarSet* barSet = m_series->barsetAt(set);
82 QBarSet* barSet = m_series->barsetAt(set);
83 value->resize(100, 50); // TODO: proper layout for this.
84 value->setPos(xPos, yPos-barHeight / 2);
85 value->setPen(barSet->floatingValuePen());
86
83
87 if (!qFuzzyIsNull(m_series->valueAt(set, category))) {
84 if (!qFuzzyIsNull(m_series->valueAt(set, category))) {
88 value->setText(QString::number(m_series->valueAt(set,category)));
85 value->setText(QString::number(m_series->valueAt(set,category)));
89 } else {
86 } else {
90 value->setText(QString(""));
87 value->setText(QString(""));
91 }
88 }
92
89
90 value->setPos(xPos, yPos-barHeight / 2);
91 value->setPen(barSet->floatingValuePen());
92
93 itemIndex++;
93 itemIndex++;
94 yPos -= barHeight;
94 yPos -= barHeight;
95 }
95 }
96 xPos += xStep;
96 xPos += xStep;
97 }
97 }
98 return layout;
98 return layout;
99 }
99 }
100
100
101 #include "moc_stackedbarchartitem_p.cpp"
101 #include "moc_stackedbarchartitem_p.cpp"
102
102
103 QTCOMMERCIALCHART_END_NAMESPACE
103 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now