@@ -1,213 +1,224 | |||
|
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 "qbarset.h" |
|
24 | 24 | #include "qbarset_p.h" |
|
25 | 25 | #include "qbarseries.h" |
|
26 | 26 | #include "qbarseries_p.h" |
|
27 | 27 | #include "qchart.h" |
|
28 | 28 | #include "chartpresenter_p.h" |
|
29 | 29 | #include "chartanimator_p.h" |
|
30 | 30 | #include "chartdataset_p.h" |
|
31 | 31 | #include <QPainter> |
|
32 | 32 | |
|
33 | 33 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
34 | 34 | |
|
35 | 35 | BarChartItem::BarChartItem(QBarSeries *series, ChartPresenter *presenter) : |
|
36 | 36 | ChartItem(presenter), |
|
37 | 37 | m_series(series) |
|
38 | 38 | { |
|
39 | 39 | setFlag(ItemClipsChildrenToShape); |
|
40 | 40 | connect(series->d_func(), SIGNAL(updatedBars()), this, SLOT(handleLayoutChanged())); |
|
41 | 41 | connect(series->d_func(), SIGNAL(labelsVisibleChanged(bool)), this, SLOT(handleLabelsVisibleChanged(bool))); |
|
42 | 42 | connect(series->d_func(), SIGNAL(restructuredBars()), this, SLOT(handleDataStructureChanged())); |
|
43 |
connect(series, SIGNAL(visibleChanged()), this, SLOT(handle |
|
|
43 | connect(series, SIGNAL(visibleChanged()), this, SLOT(handleVisibleChanged())); | |
|
44 | 44 | setZValue(ChartPresenter::BarSeriesZValue); |
|
45 | 45 | handleDataStructureChanged(); |
|
46 | 46 | } |
|
47 | 47 | |
|
48 | 48 | BarChartItem::~BarChartItem() |
|
49 | 49 | { |
|
50 | 50 | } |
|
51 | 51 | |
|
52 | 52 | void BarChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) |
|
53 | 53 | { |
|
54 | 54 | Q_UNUSED(painter); |
|
55 | 55 | Q_UNUSED(option); |
|
56 | 56 | Q_UNUSED(widget); |
|
57 | 57 | } |
|
58 | 58 | |
|
59 | 59 | QRectF BarChartItem::boundingRect() const |
|
60 | 60 | { |
|
61 | 61 | return m_rect; |
|
62 | 62 | } |
|
63 | 63 | |
|
64 | void BarChartItem::handleDataStructureChanged() | |
|
65 | { | |
|
66 | foreach(QGraphicsItem *item, childItems()) { | |
|
67 | delete item; | |
|
68 | } | |
|
69 | ||
|
70 | m_bars.clear(); | |
|
71 | m_labels.clear(); | |
|
72 | m_layout.clear(); | |
|
73 | ||
|
74 | bool labelsVisible = m_series->isLabelsVisible(); | |
|
75 | ||
|
76 | // Create new graphic items for bars | |
|
77 | for (int c = 0; c < m_series->d_func()->categoryCount(); c++) { | |
|
78 | for (int s = 0; s < m_series->count(); s++) { | |
|
79 | QBarSet *set = m_series->d_func()->barsetAt(s); | |
|
80 | ||
|
81 | // Bars | |
|
82 | Bar *bar = new Bar(set,c,this); | |
|
83 | m_bars.append(bar); | |
|
84 | connect(bar, SIGNAL(clicked(QBarSet*,int)), m_series, SIGNAL(clicked(QBarSet*,int))); | |
|
85 | connect(bar, SIGNAL(hovered(QBarSet*,bool)), m_series, SIGNAL(hovered(QBarSet*,bool))); | |
|
86 | m_layout.append(QRectF(0, 0, 0, 0)); | |
|
87 | ||
|
88 | // Labels | |
|
89 | QGraphicsSimpleTextItem *label = new QGraphicsSimpleTextItem(this); | |
|
90 | label->setVisible(labelsVisible); | |
|
91 | m_labels.append(label); | |
|
92 | } | |
|
93 | } | |
|
94 | ||
|
95 | // TODO: Is this the right place to call it? | |
|
96 | presenter()->chartTheme()->decorate(m_series, presenter()->dataSet()->seriesIndex(m_series)); | |
|
97 | handleLayoutChanged(); | |
|
98 | } | |
|
99 | ||
|
100 | 64 | QVector<QRectF> BarChartItem::calculateLayout() |
|
101 | 65 | { |
|
102 | 66 | QVector<QRectF> layout; |
|
103 | 67 | |
|
104 | 68 | // Use temporary qreals for accuracy |
|
105 | 69 | qreal categoryCount = m_series->d_func()->categoryCount(); |
|
106 | 70 | qreal setCount = m_series->count(); |
|
107 | 71 | bool barsVisible = m_series->isVisible(); |
|
108 | 72 | |
|
109 | 73 | // Domain: |
|
110 | 74 | qreal width = geometry().width(); |
|
111 | 75 | qreal height = geometry().height(); |
|
112 | 76 | qreal rangeY = m_domainMaxY - m_domainMinY; |
|
113 | 77 | qreal rangeX = m_domainMaxX - m_domainMinX; |
|
114 | 78 | qreal scaleY = (height / rangeY); |
|
115 | 79 | qreal scaleX = (width / rangeX); |
|
116 | 80 | qreal barWidth = scaleX * m_series->d_func()->barWidth(); |
|
117 | 81 | |
|
118 | 82 | int itemIndex(0); |
|
119 | 83 | for (int category = 0; category < categoryCount; category++) { |
|
120 | 84 | qreal yPos = height + scaleY * m_domainMinY + geometry().topLeft().y(); |
|
121 | 85 | for (int set = 0; set < setCount; set++) { |
|
122 | 86 | QBarSet* barSet = m_series->d_func()->barsetAt(set); |
|
123 | 87 | qreal xPos = (barSet->at(category).x() - m_domainMinX) * scaleX + m_rect.left() - barWidth/2; |
|
124 | 88 | qreal barHeight = barSet->at(category).y() * scaleY; |
|
125 | 89 | |
|
126 | 90 | Bar* bar = m_bars.at(itemIndex); |
|
127 | 91 | QRectF rect(xPos, yPos - barHeight, barWidth, barHeight); |
|
128 | 92 | |
|
129 | 93 | layout.append(rect); |
|
130 | 94 | bar->setPen(barSet->pen()); |
|
131 | 95 | bar->setBrush(barSet->brush()); |
|
132 | 96 | bar->setVisible(barsVisible); |
|
133 | 97 | |
|
134 | 98 | QGraphicsSimpleTextItem* label = m_labels.at(itemIndex); |
|
135 | 99 | |
|
136 | 100 | if (!qFuzzyIsNull(barSet->at(category).y())) { |
|
137 | 101 | label->setText(QString::number(barSet->at(category).y())); |
|
138 | 102 | } else { |
|
139 | 103 | label->setText(QString("")); |
|
140 | 104 | } |
|
141 | 105 | |
|
142 | 106 | label->setPos(xPos + (rect.width()/2 - label->boundingRect().width()/2) |
|
143 | 107 | ,yPos - barHeight/2 - label->boundingRect().height()/2); |
|
144 | 108 | label->setFont(barSet->labelFont()); |
|
145 | 109 | label->setBrush(barSet->labelBrush()); |
|
146 | 110 | |
|
147 | 111 | itemIndex++; |
|
148 | 112 | } |
|
149 | 113 | } |
|
150 | 114 | |
|
151 | 115 | return layout; |
|
152 | 116 | } |
|
153 | 117 | |
|
154 | 118 | void BarChartItem::applyLayout(const QVector<QRectF> &layout) |
|
155 | 119 | { |
|
156 | 120 | if (animator()) { |
|
157 | 121 | animator()->updateLayout(this, m_layout, layout); |
|
158 | 122 | } else { |
|
159 | 123 | setLayout(layout); |
|
160 | 124 | update(); |
|
161 | 125 | } |
|
162 | 126 | } |
|
163 | 127 | |
|
164 | 128 | void BarChartItem::setLayout(const QVector<QRectF> &layout) |
|
165 | 129 | { |
|
166 | 130 | if (layout.count() != m_bars.count()) |
|
167 | 131 | return; |
|
168 | 132 | |
|
169 | 133 | m_layout = layout; |
|
170 | 134 | |
|
171 | 135 | for (int i=0; i < m_bars.count(); i++) { |
|
172 | 136 | m_bars.at(i)->setRect(layout.at(i)); |
|
173 | 137 | } |
|
174 | 138 | } |
|
175 | 139 | //handlers |
|
176 | 140 | |
|
177 | 141 | void BarChartItem::handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY) |
|
178 | 142 | { |
|
179 | 143 | m_domainMinX = minX; |
|
180 | 144 | m_domainMaxX = maxX; |
|
181 | 145 | m_domainMinY = minY; |
|
182 | 146 | m_domainMaxY = maxY; |
|
183 | 147 | handleLayoutChanged(); |
|
184 | 148 | } |
|
185 | 149 | |
|
186 | 150 | void BarChartItem::handleGeometryChanged(const QRectF &rect) |
|
187 | 151 | { |
|
188 | 152 | prepareGeometryChange(); |
|
189 | 153 | m_rect = rect; |
|
190 | 154 | handleLayoutChanged(); |
|
191 | 155 | } |
|
192 | 156 | |
|
193 | 157 | void BarChartItem::handleLayoutChanged() |
|
194 | 158 | { |
|
195 | 159 | if ((m_rect.width() <= 0) || (m_rect.height() <= 0)) { |
|
196 | 160 | // rect size zero. |
|
197 | 161 | return; |
|
198 | 162 | } |
|
199 | 163 | QVector<QRectF> layout = calculateLayout(); |
|
200 | 164 | applyLayout(layout); |
|
201 | 165 | } |
|
202 | 166 | |
|
167 | ||
|
168 | ||
|
203 | 169 | void BarChartItem::handleLabelsVisibleChanged(bool visible) |
|
204 | 170 | { |
|
205 | 171 | foreach (QGraphicsSimpleTextItem* label, m_labels) { |
|
206 | 172 | label->setVisible(visible); |
|
207 | 173 | } |
|
208 | 174 | update(); |
|
209 | 175 | } |
|
210 | 176 | |
|
177 | void BarChartItem::handleDataStructureChanged() | |
|
178 | { | |
|
179 | foreach(QGraphicsItem *item, childItems()) { | |
|
180 | delete item; | |
|
181 | } | |
|
182 | ||
|
183 | m_bars.clear(); | |
|
184 | m_labels.clear(); | |
|
185 | m_layout.clear(); | |
|
186 | ||
|
187 | bool labelsVisible = m_series->isLabelsVisible(); | |
|
188 | ||
|
189 | // Create new graphic items for bars | |
|
190 | for (int c = 0; c < m_series->d_func()->categoryCount(); c++) { | |
|
191 | for (int s = 0; s < m_series->count(); s++) { | |
|
192 | QBarSet *set = m_series->d_func()->barsetAt(s); | |
|
193 | ||
|
194 | // Bars | |
|
195 | Bar *bar = new Bar(set,c,this); | |
|
196 | m_bars.append(bar); | |
|
197 | connect(bar, SIGNAL(clicked(QBarSet*,int)), m_series, SIGNAL(clicked(QBarSet*,int))); | |
|
198 | connect(bar, SIGNAL(hovered(QBarSet*,bool)), m_series, SIGNAL(hovered(QBarSet*,bool))); | |
|
199 | m_layout.append(QRectF(0, 0, 0, 0)); | |
|
200 | ||
|
201 | // Labels | |
|
202 | QGraphicsSimpleTextItem *label = new QGraphicsSimpleTextItem(this); | |
|
203 | label->setVisible(labelsVisible); | |
|
204 | m_labels.append(label); | |
|
205 | } | |
|
206 | } | |
|
207 | ||
|
208 | // TODO: Is this the right place to call it? | |
|
209 | presenter()->chartTheme()->decorate(m_series, presenter()->dataSet()->seriesIndex(m_series)); | |
|
210 | handleLayoutChanged(); | |
|
211 | } | |
|
212 | ||
|
213 | void BarChartItem::handleVisibleChanged() | |
|
214 | { | |
|
215 | bool visible = m_series->isVisible(); | |
|
216 | handleLabelsVisibleChanged(visible); | |
|
217 | foreach(QGraphicsItem *item, childItems()) { | |
|
218 | item->setVisible(visible); | |
|
219 | } | |
|
220 | } | |
|
221 | ||
|
211 | 222 | #include "moc_barchartitem_p.cpp" |
|
212 | 223 | |
|
213 | 224 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,88 +1,89 | |||
|
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 | // W A R N I N G |
|
22 | 22 | // ------------- |
|
23 | 23 | // |
|
24 | 24 | // This file is not part of the QtCommercial Chart API. It exists purely as an |
|
25 | 25 | // implementation detail. This header file may change from version to |
|
26 | 26 | // version without notice, or even be removed. |
|
27 | 27 | // |
|
28 | 28 | // We mean it. |
|
29 | 29 | |
|
30 | 30 | |
|
31 | 31 | #ifndef BARCHARTITEM_H |
|
32 | 32 | #define BARCHARTITEM_H |
|
33 | 33 | |
|
34 | 34 | #include "chartitem_p.h" |
|
35 | 35 | #include "qbarseries.h" |
|
36 | 36 | #include <QPen> |
|
37 | 37 | #include <QBrush> |
|
38 | 38 | |
|
39 | 39 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
40 | 40 | |
|
41 | 41 | class Bar; |
|
42 | 42 | class QAxisCategories; |
|
43 | 43 | class QChart; |
|
44 | 44 | |
|
45 | 45 | class BarChartItem : public ChartItem |
|
46 | 46 | { |
|
47 | 47 | Q_OBJECT |
|
48 | 48 | public: |
|
49 | 49 | BarChartItem(QBarSeries *series, ChartPresenter *presenter); |
|
50 | 50 | virtual ~BarChartItem(); |
|
51 | 51 | |
|
52 | 52 | public: |
|
53 | 53 | void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); |
|
54 | 54 | QRectF boundingRect() const; |
|
55 | 55 | |
|
56 | 56 | virtual QVector<QRectF> calculateLayout(); |
|
57 | 57 | virtual 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 handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY); |
|
65 | 65 | void handleGeometryChanged(const QRectF &size); |
|
66 | 66 | void handleLayoutChanged(); |
|
67 | 67 | void handleLabelsVisibleChanged(bool visible); |
|
68 | 68 | void handleDataStructureChanged(); // structure of of series has changed, recreate graphic items |
|
69 | void handleVisibleChanged(); | |
|
69 | 70 | |
|
70 | 71 | protected: |
|
71 | 72 | |
|
72 | 73 | qreal m_domainMinX; |
|
73 | 74 | qreal m_domainMaxX; |
|
74 | 75 | qreal m_domainMinY; |
|
75 | 76 | qreal m_domainMaxY; |
|
76 | 77 | |
|
77 | 78 | QRectF m_rect; |
|
78 | 79 | QVector<QRectF> m_layout; |
|
79 | 80 | |
|
80 | 81 | // Not owned. |
|
81 | 82 | QBarSeries *m_series; |
|
82 | 83 | QList<Bar *> m_bars; |
|
83 | 84 | QList<QGraphicsSimpleTextItem *> m_labels; |
|
84 | 85 | }; |
|
85 | 86 | |
|
86 | 87 | QTCOMMERCIALCHART_END_NAMESPACE |
|
87 | 88 | |
|
88 | 89 | #endif // BARCHARTITEM_H |
General Comments 0
You need to be logged in to leave comments.
Login now