##// END OF EJS Templates
removed m_layoutSet from barchartitem
sauimone -
r1247:0108313386d5
parent child
Show More
@@ -1,217 +1,215
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 m_layoutSet(false),
38 37 m_series(series)
39 38 {
40 39 setFlag(ItemClipsChildrenToShape);
41 40 connect(series->d_func(), SIGNAL(updatedBars()), this, SLOT(handleLayoutChanged()));
42 41 connect(series->d_func(), SIGNAL(restructuredBars()), this, SLOT(handleModelChanged()));
43 42 connect(series->d_func(), SIGNAL(labelsVisibleChanged(bool)), this, SLOT(labelsVisibleChanged(bool)));
44 43 setZValue(ChartPresenter::BarSeriesZValue);
45 44 dataChanged();
46 45 }
47 46
48 47 BarChartItem::~BarChartItem()
49 48 {
50 49 }
51 50
52 51 void BarChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
53 52 {
54 53 Q_UNUSED(painter);
55 54 Q_UNUSED(option);
56 55 Q_UNUSED(widget);
57 56 }
58 57
59 58 QRectF BarChartItem::boundingRect() const
60 59 {
61 60 return m_rect;
62 61 }
63 62
64 63 void BarChartItem::dataChanged()
65 64 {
66 65 foreach(QGraphicsItem *item, childItems()) {
67 66 delete item;
68 67 }
69 68
70 69 m_bars.clear();
71 70 m_labels.clear();
72 71 m_layout.clear();
73 72
74 73 bool labelsVisible = m_series->isLabelsVisible();
75 74
76 75 // Create new graphic items for bars
77 76 for (int c = 0; c < m_series->categoryCount(); c++) {
78 77 QString category = m_series->d_func()->categoryName(c);
79 78 for (int s = 0; s < m_series->barsetCount(); s++) {
80 79 QBarSet *set = m_series->d_func()->barsetAt(s);
81 80
82 81 // Bars
83 82 Bar *bar = new Bar(set,category,this);
84 83 m_bars.append(bar);
85 84 connect(bar, SIGNAL(clicked(QString)), set, SIGNAL(clicked(QString)));
86 85 connect(bar, SIGNAL(clicked(QBarSet*,QString)), m_series, SIGNAL(clicked(QBarSet*,QString)));
87 86 connect(bar, SIGNAL(hovered(bool)), set, SIGNAL(hovered(bool)));
88 87 connect(bar, SIGNAL(hovered(QBarSet*,bool)), m_series, SIGNAL(hovered(QBarSet*,bool)));
89 88 m_layout.append(QRectF(0, 0, 0, 0));
90 89
91 90 // Labels
92 91 QGraphicsSimpleTextItem *label = new QGraphicsSimpleTextItem(this);
93 92 label->setVisible(labelsVisible);
94 93 m_labels.append(label);
95 94 }
96 95 }
97 96
98 97 // TODO: Is this the right place to call it?
99 98 // presenter()->chartTheme()->decorate(m_series, presenter()->dataSet()->seriesIndex(m_series));
100 99 }
101 100
102 101 QVector<QRectF> BarChartItem::calculateLayout()
103 102 {
104 103 QVector<QRectF> layout;
105 104
106 105 // Use temporary qreals for accurancy
107 106 qreal categoryCount = m_series->categoryCount();
108 107 qreal setCount = m_series->barsetCount();
109 108
110 109 // Domain:
111 110 qreal width = geometry().width();
112 111 qreal height = geometry().height();
113 112 qreal rangeY = m_domainMaxY - m_domainMinY;
114 113 qreal rangeX = m_domainMaxX - m_domainMinX;
115 114 qreal scaleY = (height / rangeY);
116 115 qreal scaleX = (width / rangeX);
117 116 qreal categoryWidth = width / categoryCount;
118 117 qreal barWidth = categoryWidth / setCount - categoryWidth * m_series->d_func()->barMargin();
119 118
120 119 int itemIndex(0);
121 120 for (int category = 0; category < categoryCount; category++) {
122 121 qreal yPos = height + scaleY * m_domainMinY + geometry().topLeft().y();
123 122 for (int set = 0; set < setCount; set++) {
124 123 QBarSet* barSet = m_series->d_func()->barsetAt(set);
125 124 qreal xPos = (barSet->at(category).x() - m_domainMinX) * scaleX + m_rect.left() - barWidth/2;
126 125 qreal barHeight = barSet->at(category).y() * scaleY;
127 126
128 127 Bar* bar = m_bars.at(itemIndex);
129 128 QRectF rect(xPos, yPos - barHeight, barWidth, barHeight);
130 129
131 130 layout.append(rect);
132 131 bar->setPen(barSet->pen());
133 132 bar->setBrush(barSet->brush());
134 133
135 134 QGraphicsSimpleTextItem* label = m_labels.at(itemIndex);
136 135
137 136 if (!qFuzzyIsNull(barSet->at(category).y())) {
138 137 label->setText(QString::number(barSet->at(category).y()));
139 138 } else {
140 139 label->setText(QString(""));
141 140 }
142 141
143 142 label->setPos(xPos + (rect.width()/2 - label->boundingRect().width()/2)
144 143 ,yPos - barHeight/2 - label->boundingRect().height()/2);
145 144 label->setFont(barSet->labelFont());
146 145
147 146 itemIndex++;
148 147 }
149 148 }
150 149
151 150 return layout;
152 151 }
153 152
154 153 void BarChartItem::applyLayout(const QVector<QRectF> &layout)
155 154 {
156 155 if (animator())
157 156 animator()->updateLayout(this, m_layout, layout);
158 157 else
159 158 setLayout(layout);
160 159 }
161 160
162 161 void BarChartItem::setLayout(const QVector<QRectF> &layout)
163 162 {
164 163 m_layout = layout;
165 164
166 165 for (int i=0; i < m_bars.count(); i++)
167 166 m_bars.at(i)->setRect(layout.at(i));
168 167
169 168 update();
170 169 }
171 170 //handlers
172 171
173 172 void BarChartItem::handleModelChanged()
174 173 {
175 174 // dataChanged();
176 175 presenter()->resetAllElements();
177 176 }
178 177
179 178 void BarChartItem::handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY)
180 179 {
181 180 m_domainMinX = minX;
182 181 m_domainMaxX = maxX;
183 182 m_domainMinY = minY;
184 183 m_domainMaxY = maxY;
185 184 handleLayoutChanged();
186 185 }
187 186
188 187 void BarChartItem::handleGeometryChanged(const QRectF &rect)
189 188 {
190 189 prepareGeometryChange();
191 190 m_rect = rect;
192 191 handleLayoutChanged();
193 m_layoutSet = true;
194 192 }
195 193
196 194 void BarChartItem::handleLayoutChanged()
197 195 {
198 196 if ((m_rect.width() <= 0) || (m_rect.height() <= 0)) {
199 197 // rect size zero.
200 198 return;
201 199 }
202 200 QVector<QRectF> layout = calculateLayout();
203 201 applyLayout(layout);
204 202 update();
205 203 }
206 204
207 205 void BarChartItem::labelsVisibleChanged(bool visible)
208 206 {
209 207 foreach (QGraphicsSimpleTextItem* label, m_labels) {
210 208 label->setVisible(visible);
211 209 }
212 210 update();
213 211 }
214 212
215 213 #include "moc_barchartitem_p.cpp"
216 214
217 215 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,84 +1,83
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 QAxisCategories;
33 33 class QChart;
34 34
35 35 //typedef QVector<QRectF> BarLayout;
36 36
37 37 class BarChartItem : public ChartItem
38 38 {
39 39 Q_OBJECT
40 40 public:
41 41 BarChartItem(QBarSeries *series, ChartPresenter *presenter);
42 42 virtual ~BarChartItem();
43 43
44 44 public:
45 45 // From QGraphicsItem
46 46 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
47 47 QRectF boundingRect() const;
48 48
49 49 virtual void dataChanged(); // data of series has changed -> need to recalculate bar sizes
50 50
51 51 virtual QVector<QRectF> calculateLayout();
52 52 void applyLayout(const QVector<QRectF> &layout);
53 53 void setLayout(const QVector<QRectF> &layout);
54 54 void updateLayout(const QVector<QRectF> &layout);
55 55
56 56 QRectF geometry() const { return m_rect;}
57 57
58 58 public Q_SLOTS:
59 59 void handleModelChanged();
60 60 void handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY);
61 61 void handleGeometryChanged(const QRectF &size);
62 62 void handleLayoutChanged();
63 63 void labelsVisibleChanged(bool visible);
64 64
65 65 protected:
66 66
67 67 qreal m_domainMinX;
68 68 qreal m_domainMaxX;
69 69 qreal m_domainMinY;
70 70 qreal m_domainMaxY;
71 71
72 72 QRectF m_rect;
73 bool m_layoutSet; // True, if component has been laid out.
74 73 QVector<QRectF> m_layout;
75 74
76 75 // Not owned.
77 76 QBarSeries *m_series;
78 77 QList<Bar *> m_bars;
79 78 QList<QGraphicsSimpleTextItem *> m_labels;
80 79 };
81 80
82 81 QTCOMMERCIALCHART_END_NAMESPACE
83 82
84 83 #endif // BARCHARTITEM_H
General Comments 0
You need to be logged in to leave comments. Login now