@@ -1,210 +1,210 | |||
|
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 "barlabel_p.h" |
|
24 | 24 | #include "qbarset.h" |
|
25 | 25 | #include "qbarset_p.h" |
|
26 | 26 | #include "qbarseries.h" |
|
27 | 27 | #include "qbarseries_p.h" |
|
28 | 28 | #include "qchart.h" |
|
29 | 29 | #include "chartpresenter_p.h" |
|
30 | 30 | #include "chartanimator_p.h" |
|
31 | 31 | #include "chartdataset_p.h" |
|
32 | 32 | #include <QPainter> |
|
33 | 33 | |
|
34 | 34 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
35 | 35 | |
|
36 | 36 | BarChartItem::BarChartItem(QBarSeries *series, ChartPresenter *presenter) : |
|
37 | 37 | ChartItem(presenter), |
|
38 | 38 | m_layoutSet(false), |
|
39 | 39 | m_series(series) |
|
40 | 40 | { |
|
41 | 41 | setFlag(ItemClipsChildrenToShape); |
|
42 | 42 | connect(series->d_func(), SIGNAL(updatedBars()), this, SLOT(handleLayoutChanged())); |
|
43 | 43 | connect(series->d_func(), SIGNAL(restructuredBars()), this, SLOT(handleModelChanged())); |
|
44 | 44 | setZValue(ChartPresenter::BarSeriesZValue); |
|
45 | 45 | dataChanged(); |
|
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 | 64 | void BarChartItem::dataChanged() |
|
65 | 65 | { |
|
66 | 66 | foreach(QGraphicsItem *item, childItems()) { |
|
67 | 67 | delete item; |
|
68 | 68 | } |
|
69 | 69 | |
|
70 | 70 | m_bars.clear(); |
|
71 | 71 | m_labels.clear(); |
|
72 | 72 | m_layout.clear(); |
|
73 | 73 | |
|
74 | 74 | // Create new graphic items for bars |
|
75 | 75 | for (int c = 0; c < m_series->categoryCount(); c++) { |
|
76 | 76 | QString category = m_series->d_func()->categoryName(c); |
|
77 | 77 | for (int s = 0; s < m_series->barsetCount(); s++) { |
|
78 | 78 | QBarSet *set = m_series->d_func()->barsetAt(s); |
|
79 | 79 | Bar *bar = new Bar(set,category,this); |
|
80 | 80 | m_bars.append(bar); |
|
81 | 81 | connect(bar, SIGNAL(clicked(QString)), set, SIGNAL(clicked(QString))); |
|
82 | 82 | connect(bar, SIGNAL(clicked(QBarSet*,QString)), m_series, SIGNAL(clicked(QBarSet*,QString))); |
|
83 | 83 | connect(bar, SIGNAL(hovered(bool)), set, SIGNAL(hovered(bool))); |
|
84 | 84 | connect(bar, SIGNAL(hovered(QBarSet*,bool)), m_series, SIGNAL(hovered(QBarSet*,bool))); |
|
85 | 85 | m_layout.append(QRectF(0, 0, 0, 0)); |
|
86 | 86 | } |
|
87 | 87 | } |
|
88 | 88 | |
|
89 | 89 | // Create labels |
|
90 | 90 | for (int category = 0; category < m_series->categoryCount(); category++) { |
|
91 | 91 | for (int s = 0; s < m_series->barsetCount(); s++) { |
|
92 | 92 | QBarSet *set = m_series->d_func()->barsetAt(s); |
|
93 | 93 | BarLabel *value = new BarLabel(*set, this); |
|
94 | 94 | m_labels.append(value); |
|
95 | 95 | connect(set->d_ptr.data(),SIGNAL(labelsVisibleChanged(bool)),value,SLOT(labelsVisibleChanged(bool))); |
|
96 | 96 | } |
|
97 | 97 | } |
|
98 | 98 | |
|
99 | 99 | // TODO: Is this the right place to call it? |
|
100 | 100 | // presenter()->chartTheme()->decorate(m_series, presenter()->dataSet()->seriesIndex(m_series)); |
|
101 | 101 | } |
|
102 | 102 | |
|
103 | 103 | QVector<QRectF> BarChartItem::calculateLayout() |
|
104 | 104 | { |
|
105 | 105 | QVector<QRectF> layout; |
|
106 | 106 | |
|
107 | 107 | // Use temporary qreals for accurancy |
|
108 | 108 | qreal categoryCount = m_series->categoryCount(); |
|
109 | 109 | qreal setCount = m_series->barsetCount(); |
|
110 | 110 | |
|
111 | 111 | // Domain: |
|
112 | 112 | qreal width = geometry().width(); |
|
113 | 113 | qreal height = geometry().height(); |
|
114 | 114 | qreal rangeY = m_domainMaxY - m_domainMinY; |
|
115 | 115 | qreal rangeX = m_domainMaxX - m_domainMinX; |
|
116 | 116 | qreal scaleY = (height / rangeY); |
|
117 | 117 | qreal scaleX = (width / rangeX); |
|
118 | 118 | qreal categoryWidth = width / categoryCount; |
|
119 | qreal barWidth = categoryWidth - categoryWidth * m_series->d_func()->barMargin(); | |
|
119 | qreal barWidth = categoryWidth / setCount - categoryWidth * m_series->d_func()->barMargin(); | |
|
120 | 120 | |
|
121 | 121 | int itemIndex(0); |
|
122 | 122 | for (int category = 0; category < categoryCount; category++) { |
|
123 | 123 | qreal yPos = height + scaleY * m_domainMinY + geometry().topLeft().y(); |
|
124 | 124 | for (int set = 0; set < setCount; set++) { |
|
125 | 125 | QBarSet* barSet = m_series->d_func()->barsetAt(set); |
|
126 | 126 | qreal xPos = (barSet->at(category).x() - m_domainMinX) * scaleX + m_rect.left() - barWidth/2; |
|
127 | 127 | qreal barHeight = barSet->at(category).y() * scaleY; |
|
128 | 128 | |
|
129 | 129 | Bar* bar = m_bars.at(itemIndex); |
|
130 | 130 | QRectF rect(xPos, yPos - barHeight, barWidth, barHeight); |
|
131 | 131 | |
|
132 | 132 | layout.append(rect); |
|
133 | 133 | bar->setPen(barSet->pen()); |
|
134 | 134 | bar->setBrush(barSet->brush()); |
|
135 | 135 | |
|
136 | 136 | BarLabel* label = m_labels.at(itemIndex); |
|
137 | 137 | |
|
138 | 138 | if (!qFuzzyIsNull(barSet->at(category).y())) { |
|
139 | 139 | label->setText(QString::number(barSet->at(category).y())); |
|
140 | 140 | } else { |
|
141 | 141 | label->setText(QString("")); |
|
142 | 142 | } |
|
143 | 143 | |
|
144 | 144 | label->setPos(xPos + (rect.width()/2 - label->boundingRect().width()/2) |
|
145 | 145 | ,yPos - barHeight/2 - label->boundingRect().height()/2); |
|
146 | 146 | label->setFont(barSet->labelFont()); |
|
147 | 147 | |
|
148 | 148 | itemIndex++; |
|
149 | 149 | } |
|
150 | 150 | } |
|
151 | 151 | |
|
152 | 152 | return layout; |
|
153 | 153 | } |
|
154 | 154 | |
|
155 | 155 | void BarChartItem::applyLayout(const QVector<QRectF> &layout) |
|
156 | 156 | { |
|
157 | 157 | if (animator()) |
|
158 | 158 | animator()->updateLayout(this, m_layout, layout); |
|
159 | 159 | else |
|
160 | 160 | setLayout(layout); |
|
161 | 161 | } |
|
162 | 162 | |
|
163 | 163 | void BarChartItem::setLayout(const QVector<QRectF> &layout) |
|
164 | 164 | { |
|
165 | 165 | m_layout = layout; |
|
166 | 166 | |
|
167 | 167 | for (int i=0; i < m_bars.count(); i++) |
|
168 | 168 | m_bars.at(i)->setRect(layout.at(i)); |
|
169 | 169 | |
|
170 | 170 | update(); |
|
171 | 171 | } |
|
172 | 172 | //handlers |
|
173 | 173 | |
|
174 | 174 | void BarChartItem::handleModelChanged() |
|
175 | 175 | { |
|
176 | 176 | // dataChanged(); |
|
177 | 177 | presenter()->resetAllElements(); |
|
178 | 178 | } |
|
179 | 179 | |
|
180 | 180 | void BarChartItem::handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY) |
|
181 | 181 | { |
|
182 | 182 | m_domainMinX = minX; |
|
183 | 183 | m_domainMaxX = maxX; |
|
184 | 184 | m_domainMinY = minY; |
|
185 | 185 | m_domainMaxY = maxY; |
|
186 | 186 | handleLayoutChanged(); |
|
187 | 187 | } |
|
188 | 188 | |
|
189 | 189 | void BarChartItem::handleGeometryChanged(const QRectF &rect) |
|
190 | 190 | { |
|
191 | 191 | prepareGeometryChange(); |
|
192 | 192 | m_rect = rect; |
|
193 | 193 | handleLayoutChanged(); |
|
194 | 194 | m_layoutSet = true; |
|
195 | 195 | } |
|
196 | 196 | |
|
197 | 197 | void BarChartItem::handleLayoutChanged() |
|
198 | 198 | { |
|
199 | 199 | if ((m_rect.width() <= 0) || (m_rect.height() <= 0)) { |
|
200 | 200 | // rect size zero. |
|
201 | 201 | return; |
|
202 | 202 | } |
|
203 | 203 | QVector<QRectF> layout = calculateLayout(); |
|
204 | 204 | applyLayout(layout); |
|
205 | 205 | update(); |
|
206 | 206 | } |
|
207 | 207 | |
|
208 | 208 | #include "moc_barchartitem_p.cpp" |
|
209 | 209 | |
|
210 | 210 | QTCOMMERCIALCHART_END_NAMESPACE |
General Comments 0
You need to be logged in to leave comments.
Login now