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