@@ -1,354 +1,357 | |||||
1 | #include "barchartitem_p.h" |
|
1 | #include "barchartitem_p.h" | |
2 | #include "bar_p.h" |
|
2 | #include "bar_p.h" | |
3 | #include "barvalue_p.h" |
|
3 | #include "barvalue_p.h" | |
4 | #include "qbarset.h" |
|
4 | #include "qbarset.h" | |
5 | #include "qbarseries.h" |
|
5 | #include "qbarseries.h" | |
6 | #include "qchart.h" |
|
6 | #include "qchart.h" | |
7 | #include "qchartaxis.h" |
|
7 | #include "qchartaxis.h" | |
8 | #include "qchartaxiscategories.h" |
|
8 | #include "qchartaxiscategories.h" | |
9 | #include "chartpresenter_p.h" |
|
9 | #include "chartpresenter_p.h" | |
10 | #include "chartanimator_p.h" |
|
10 | #include "chartanimator_p.h" | |
11 | #include "chartdataset_p.h" |
|
11 | #include "chartdataset_p.h" | |
12 | #include <QDebug> |
|
12 | #include <QDebug> | |
13 | #include <QToolTip> |
|
13 | #include <QToolTip> | |
14 |
|
14 | |||
15 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
15 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
16 |
|
16 | |||
17 | BarChartItem::BarChartItem(QBarSeries *series, ChartPresenter *presenter) : |
|
17 | BarChartItem::BarChartItem(QBarSeries *series, ChartPresenter *presenter) : | |
18 | ChartItem(presenter), |
|
18 | ChartItem(presenter), | |
19 | mLayoutSet(false), |
|
19 | mLayoutSet(false), | |
20 | mSeries(series) |
|
20 | mSeries(series) | |
21 | { |
|
21 | { | |
22 | connect(series,SIGNAL(showToolTip(QPoint,QString)),this,SLOT(showToolTip(QPoint,QString))); |
|
22 | connect(series,SIGNAL(showToolTip(QPoint,QString)),this,SLOT(showToolTip(QPoint,QString))); | |
23 | connect(series, SIGNAL(updatedBars()), this, SLOT(handleLayoutChanged())); |
|
23 | connect(series, SIGNAL(updatedBars()), this, SLOT(handleLayoutChanged())); | |
24 | //TODO: connect(series,SIGNAL("position or size has changed"), this, SLOT(handleLayoutChanged())); |
|
24 | //TODO: connect(series,SIGNAL("position or size has changed"), this, SLOT(handleLayoutChanged())); | |
25 | connect(series, SIGNAL(restructuredBar(int)), this, SLOT(handleModelChanged(int))); |
|
25 | connect(series, SIGNAL(restructuredBar(int)), this, SLOT(handleModelChanged(int))); | |
26 | setZValue(ChartPresenter::BarSeriesZValue); |
|
26 | setZValue(ChartPresenter::BarSeriesZValue); | |
27 | initAxisLabels(); |
|
27 | initAxisLabels(); | |
28 | dataChanged(); |
|
28 | dataChanged(); | |
29 | } |
|
29 | } | |
30 |
|
30 | |||
31 | BarChartItem::~BarChartItem() |
|
31 | BarChartItem::~BarChartItem() | |
32 | { |
|
32 | { | |
33 | disconnect(this,SLOT(showToolTip(QPoint,QString))); |
|
33 | disconnect(this,SLOT(showToolTip(QPoint,QString))); | |
34 | } |
|
34 | } | |
35 |
|
35 | |||
36 | void BarChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) |
|
36 | void BarChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) | |
37 | { |
|
37 | { | |
38 | if (!mLayoutSet) { |
|
38 | if (!mLayoutSet) { | |
39 | qDebug() << "BarChartItem::paint called without layout set. Aborting."; |
|
39 | qDebug() << "BarChartItem::paint called without layout set. Aborting."; | |
40 | return; |
|
40 | return; | |
41 | } |
|
41 | } | |
42 | foreach(QGraphicsItem* i, childItems()) { |
|
42 | foreach(QGraphicsItem* i, childItems()) { | |
43 | i->paint(painter,option,widget); |
|
43 | i->paint(painter,option,widget); | |
44 | } |
|
44 | } | |
45 | } |
|
45 | } | |
46 |
|
46 | |||
47 | QRectF BarChartItem::boundingRect() const |
|
47 | QRectF BarChartItem::boundingRect() const | |
48 | { |
|
48 | { | |
49 | return m_rect; |
|
49 | return m_rect; | |
50 | } |
|
50 | } | |
51 |
|
51 | |||
52 | void BarChartItem::dataChanged() |
|
52 | void BarChartItem::dataChanged() | |
53 | { |
|
53 | { | |
54 | // TODO: performance optimizations. Do we really need to delete and create items every time data is changed or can we reuse them? |
|
54 | // TODO: performance optimizations. Do we really need to delete and create items every time data is changed or can we reuse them? | |
55 | // Delete old bars |
|
55 | // Delete old bars | |
56 | foreach (QGraphicsItem* item, childItems()) { |
|
56 | foreach (QGraphicsItem* item, childItems()) { | |
57 | delete item; |
|
57 | delete item; | |
58 | } |
|
58 | } | |
59 |
|
59 | |||
60 | mBars.clear(); |
|
60 | mBars.clear(); | |
61 | mFloatingValues.clear(); |
|
61 | mFloatingValues.clear(); | |
62 | mLayout.clear(); |
|
62 | mLayout.clear(); | |
63 |
|
63 | |||
64 | // Create new graphic items for bars |
|
64 | // Create new graphic items for bars | |
65 | for (int c=0; c<mSeries->categoryCount(); c++) { |
|
65 | for (int c=0; c<mSeries->categoryCount(); c++) { | |
66 | QString category = mSeries->categoryName(c); |
|
66 | QString category = mSeries->categoryName(c); | |
67 | for (int s=0; s<mSeries->barsetCount(); s++) { |
|
67 | for (int s=0; s<mSeries->barsetCount(); s++) { | |
68 | QBarSet *set = mSeries->barsetAt(s); |
|
68 | QBarSet *set = mSeries->barsetAt(s); | |
69 | Bar *bar = new Bar(category,this); |
|
69 | Bar *bar = new Bar(category,this); | |
70 | childItems().append(bar); |
|
70 | childItems().append(bar); | |
71 | mBars.append(bar); |
|
71 | mBars.append(bar); | |
72 | connect(bar,SIGNAL(clicked(QString)),set,SIGNAL(clicked(QString))); |
|
72 | connect(bar,SIGNAL(clicked(QString)),set,SIGNAL(clicked(QString))); | |
73 | connect(bar,SIGNAL(rightClicked(QString)),set,SIGNAL(rightClicked(QString))); |
|
73 | connect(bar,SIGNAL(rightClicked(QString)),set,SIGNAL(rightClicked(QString))); | |
74 | connect(bar,SIGNAL(hoverEntered(QPoint)),set,SLOT(barHoverEnterEvent(QPoint))); |
|
74 | connect(bar,SIGNAL(hoverEntered(QPoint)),set,SLOT(barHoverEnterEvent(QPoint))); | |
75 | connect(bar,SIGNAL(hoverLeaved()),set,SLOT(barHoverLeaveEvent())); |
|
75 | connect(bar,SIGNAL(hoverLeaved()),set,SLOT(barHoverLeaveEvent())); | |
76 | mLayout.append(QRectF(0,0,0,0)); |
|
76 | mLayout.append(QRectF(0,0,0,0)); | |
77 | } |
|
77 | } | |
78 | } |
|
78 | } | |
79 |
|
79 | |||
80 | // Create floating values |
|
80 | // Create floating values | |
81 | for (int category=0; category<mSeries->categoryCount(); category++) { |
|
81 | for (int category=0; category<mSeries->categoryCount(); category++) { | |
82 | for (int s=0; s<mSeries->barsetCount(); s++) { |
|
82 | for (int s=0; s<mSeries->barsetCount(); s++) { | |
83 | QBarSet *set = mSeries->barsetAt(s); |
|
83 | QBarSet *set = mSeries->barsetAt(s); | |
84 | BarValue *value = new BarValue(*set, this); |
|
84 | BarValue *value = new BarValue(*set, this); | |
85 | childItems().append(value); |
|
85 | childItems().append(value); | |
86 | mFloatingValues.append(value); |
|
86 | mFloatingValues.append(value); | |
87 | connect(set,SIGNAL(toggleFloatingValues()),value,SLOT(toggleVisible())); |
|
87 | connect(set,SIGNAL(toggleFloatingValues()),value,SLOT(toggleVisible())); | |
88 | } |
|
88 | } | |
89 | } |
|
89 | } | |
90 | } |
|
90 | } | |
91 | /* |
|
91 | /* | |
92 | void BarChartItem::layoutChanged() |
|
92 | void BarChartItem::layoutChanged() | |
93 | { |
|
93 | { | |
94 | qDebug() << "Deprecated BarChartItem::layoutChanged called. aborting"; |
|
94 | qDebug() << "Deprecated BarChartItem::layoutChanged called. aborting"; | |
95 | return; |
|
95 | return; | |
96 | // Scale bars to new layout |
|
96 | // Scale bars to new layout | |
97 | // Layout for bars: |
|
97 | // Layout for bars: | |
98 | if (mSeries->barsetCount() <= 0) { |
|
98 | if (mSeries->barsetCount() <= 0) { | |
99 | qDebug() << "No sets in model!"; |
|
99 | qDebug() << "No sets in model!"; | |
100 | return; |
|
100 | return; | |
101 | } |
|
101 | } | |
102 |
|
102 | |||
103 | if (childItems().count() == 0) { |
|
103 | if (childItems().count() == 0) { | |
104 | qDebug() << "WARNING: BarChartitem::layoutChanged called before graphics items are created!"; |
|
104 | qDebug() << "WARNING: BarChartitem::layoutChanged called before graphics items are created!"; | |
105 | return; |
|
105 | return; | |
106 | } |
|
106 | } | |
107 |
|
107 | |||
108 |
|
108 | |||
109 | // Use temporary qreals for accurancy (we might get some compiler warnings... :) |
|
109 | // Use temporary qreals for accurancy (we might get some compiler warnings... :) | |
110 | qreal categoryCount = mSeries->categoryCount(); |
|
110 | qreal categoryCount = mSeries->categoryCount(); | |
111 | qreal setCount = mSeries->barsetCount(); |
|
111 | qreal setCount = mSeries->barsetCount(); | |
112 | qreal max = mSeries->max(); |
|
112 | qreal max = mSeries->max(); | |
113 |
|
113 | |||
114 | // Domain: |
|
114 | // Domain: | |
115 | if (mDomainMaxY > max) { |
|
115 | if (mDomainMaxY > max) { | |
116 | max = mDomainMaxY; |
|
116 | max = mDomainMaxY; | |
117 | } |
|
117 | } | |
118 |
|
118 | |||
119 | qreal width = geometry().width(); |
|
119 | qreal width = geometry().width(); | |
120 | qreal height = geometry().height(); |
|
120 | qreal height = geometry().height(); | |
121 | qreal scale = (height/max); |
|
121 | qreal scale = (height/max); | |
122 | qreal categoryWidth = width/categoryCount; |
|
122 | qreal categoryWidth = width/categoryCount; | |
123 | qreal barWidth = categoryWidth / (setCount+1); |
|
123 | qreal barWidth = categoryWidth / (setCount+1); | |
124 |
|
124 | |||
125 | BarLayout layout; |
|
125 | BarLayout layout; | |
126 |
|
126 | |||
127 | int itemIndex(0); |
|
127 | int itemIndex(0); | |
128 | for (int category=0; category < categoryCount; category++) { |
|
128 | for (int category=0; category < categoryCount; category++) { | |
129 | qreal xPos = categoryWidth * category + barWidth/2; |
|
129 | qreal xPos = categoryWidth * category + barWidth/2; | |
130 | qreal yPos = height; |
|
130 | qreal yPos = height; | |
131 | for (int set = 0; set < setCount; set++) { |
|
131 | for (int set = 0; set < setCount; set++) { | |
132 | qreal barHeight = mSeries->valueAt(set,category) * scale; |
|
132 | qreal barHeight = mSeries->valueAt(set,category) * scale; | |
133 | Bar* bar = mBars.at(itemIndex); |
|
133 | Bar* bar = mBars.at(itemIndex); | |
134 |
|
134 | |||
135 | QRectF rect(xPos,yPos-barHeight,mBarWidth,barHeight); |
|
135 | QRectF rect(xPos,yPos-barHeight,mBarWidth,barHeight); | |
136 | layout.insert(bar,rect); |
|
136 | layout.insert(bar,rect); | |
137 | // TODO: width settable per bar? |
|
137 | // TODO: width settable per bar? | |
138 | bar->setRect(xPos, yPos-barHeight,barWidth, barHeight); |
|
138 | bar->setRect(xPos, yPos-barHeight,barWidth, barHeight); | |
139 | bar->setPen(mSeries->barsetAt(set)->pen()); |
|
139 | bar->setPen(mSeries->barsetAt(set)->pen()); | |
140 | bar->setBrush(mSeries->barsetAt(set)->brush()); |
|
140 | bar->setBrush(mSeries->barsetAt(set)->brush()); | |
141 |
|
141 | |||
142 | // bar->resize(mBarWidth, barHeight); |
|
142 | // bar->resize(mBarWidth, barHeight); | |
143 | // layout.insert(bar,QSizeF(mBarWidth,barHeight)); |
|
143 | // layout.insert(bar,QSizeF(mBarWidth,barHeight)); | |
144 | bar->setPen(mSeries->barsetAt(set)->pen()); |
|
144 | bar->setPen(mSeries->barsetAt(set)->pen()); | |
145 | bar->setBrush(mSeries->barsetAt(set)->brush()); |
|
145 | bar->setBrush(mSeries->barsetAt(set)->brush()); | |
146 | // bar->setPos(xPos, yPos-barHeight); |
|
146 | // bar->setPos(xPos, yPos-barHeight); | |
147 | itemIndex++; |
|
147 | itemIndex++; | |
148 | xPos += barWidth; |
|
148 | xPos += barWidth; | |
149 | } |
|
149 | } | |
150 | } |
|
150 | } | |
151 |
|
151 | |||
152 | // Position floating values |
|
152 | // Position floating values | |
153 | itemIndex = 0; |
|
153 | itemIndex = 0; | |
154 | for (int category=0; category < mSeries->categoryCount(); category++) { |
|
154 | for (int category=0; category < mSeries->categoryCount(); category++) { | |
155 | qreal xPos = categoryWidth * category + categoryWidth/2 + barWidth; |
|
155 | qreal xPos = categoryWidth * category + categoryWidth/2 + barWidth; | |
156 | qreal yPos = height; |
|
156 | qreal yPos = height; | |
157 | for (int set=0; set < mSeries->barsetCount(); set++) { |
|
157 | for (int set=0; set < mSeries->barsetCount(); set++) { | |
158 | qreal barHeight = mSeries->valueAt(set,category) * scale; |
|
158 | qreal barHeight = mSeries->valueAt(set,category) * scale; | |
159 | BarValue* value = mFloatingValues.at(itemIndex); |
|
159 | BarValue* value = mFloatingValues.at(itemIndex); | |
160 |
|
160 | |||
161 | QBarSet* barSet = mSeries->barsetAt(set); |
|
161 | QBarSet* barSet = mSeries->barsetAt(set); | |
162 | value->resize(100,50); // TODO: proper layout for this. |
|
162 | value->resize(100,50); // TODO: proper layout for this. | |
163 | value->setPos(xPos, yPos-barHeight/2); |
|
163 | value->setPos(xPos, yPos-barHeight/2); | |
164 | value->setPen(barSet->floatingValuePen()); |
|
164 | value->setPen(barSet->floatingValuePen()); | |
165 |
|
165 | |||
166 | if (mSeries->valueAt(set,category) != 0) { |
|
166 | if (mSeries->valueAt(set,category) != 0) { | |
167 | value->setValueString(QString::number(mSeries->valueAt(set,category))); |
|
167 | value->setValueString(QString::number(mSeries->valueAt(set,category))); | |
168 | } else { |
|
168 | } else { | |
169 | value->setValueString(QString("")); |
|
169 | value->setValueString(QString("")); | |
170 | } |
|
170 | } | |
171 |
|
171 | |||
172 | itemIndex++; |
|
172 | itemIndex++; | |
173 | xPos += barWidth; |
|
173 | xPos += barWidth; | |
174 | } |
|
174 | } | |
175 | } |
|
175 | } | |
176 | // update(); |
|
176 | // update(); | |
177 | } |
|
177 | } | |
178 | */ |
|
178 | */ | |
179 | QVector<QRectF> BarChartItem::calculateLayout() |
|
179 | QVector<QRectF> BarChartItem::calculateLayout() | |
180 | { |
|
180 | { | |
181 | // layoutChanged(); |
|
181 | // layoutChanged(); | |
182 | /* |
|
182 | /* | |
183 | BarLayout layout; |
|
183 | BarLayout layout; | |
184 | foreach(Bar* bar, mBars) { |
|
184 | foreach(Bar* bar, mBars) { | |
185 | layout.insert(bar,bar->boundingRect()); |
|
185 | layout.insert(bar,bar->boundingRect()); | |
186 | } |
|
186 | } | |
187 | */ |
|
187 | */ | |
188 | QVector<QRectF> layout; |
|
188 | QVector<QRectF> layout; | |
189 |
|
189 | |||
190 | // Use temporary qreals for accurancy (we might get some compiler warnings... :) |
|
190 | // Use temporary qreals for accurancy (we might get some compiler warnings... :) | |
191 | int categoryCount = mSeries->categoryCount(); |
|
191 | int categoryCount = mSeries->categoryCount(); | |
192 | int setCount = mSeries->barsetCount(); |
|
192 | int setCount = mSeries->barsetCount(); | |
193 |
|
193 | |||
194 | qreal tW = mWidth; |
|
194 | // qreal tW = mWidth; | |
195 | qreal tH = mHeight; |
|
195 | // qreal tH = mHeight; | |
|
196 | qreal width = geometry().width(); | |||
|
197 | qreal height = geometry().height(); | |||
|
198 | ||||
196 | qreal tM = mSeries->max(); |
|
199 | qreal tM = mSeries->max(); | |
197 |
|
200 | |||
198 | // Domain: |
|
201 | // Domain: | |
199 | if (mDomainMaxY > tM) { |
|
202 | if (mDomainMaxY > tM) { | |
200 | tM = mDomainMaxY; |
|
203 | tM = mDomainMaxY; | |
201 | } |
|
204 | } | |
202 |
|
205 | |||
203 |
qreal scale = (t |
|
206 | qreal scale = (height/tM); | |
204 | qreal tC = categoryCount + 1; |
|
207 | qreal tC = categoryCount + 1; | |
205 |
qreal categoryWidth = |
|
208 | qreal categoryWidth = width/tC; | |
206 |
|
|
209 | qreal barWidth = categoryWidth / (setCount+1); | |
207 |
|
210 | |||
208 | int itemIndex(0); |
|
211 | int itemIndex(0); | |
209 | for (int category=0; category < categoryCount; category++) { |
|
212 | for (int category=0; category < categoryCount; category++) { | |
210 |
qreal xPos = categoryWidth * category + categoryWidth /2 + |
|
213 | qreal xPos = categoryWidth * category + categoryWidth /2 + barWidth/2; | |
211 |
qreal yPos = |
|
214 | qreal yPos = height; | |
212 | for (int set = 0; set < setCount; set++) { |
|
215 | for (int set = 0; set < setCount; set++) { | |
213 | qreal barHeight = mSeries->valueAt(set,category) * scale; |
|
216 | qreal barHeight = mSeries->valueAt(set,category) * scale; | |
214 | Bar* bar = mBars.at(itemIndex); |
|
217 | Bar* bar = mBars.at(itemIndex); | |
215 |
|
218 | |||
216 |
QRectF rect(xPos,yPos-barHeight, |
|
219 | QRectF rect(xPos,yPos-barHeight,barWidth,barHeight); | |
217 | //layout.insert(bar,rect); |
|
220 | //layout.insert(bar,rect); | |
218 | layout.append(rect); |
|
221 | layout.append(rect); | |
219 | // TODO: width settable per bar? |
|
222 | // TODO: width settable per bar? | |
220 | // bar->resize(mBarWidth, barHeight); |
|
223 | // bar->resize(mBarWidth, barHeight); | |
221 | // layout.insert(bar,QSizeF(mBarWidth,barHeight)); |
|
224 | // layout.insert(bar,QSizeF(mBarWidth,barHeight)); | |
222 | bar->setPen(mSeries->barsetAt(set)->pen()); |
|
225 | bar->setPen(mSeries->barsetAt(set)->pen()); | |
223 | bar->setBrush(mSeries->barsetAt(set)->brush()); |
|
226 | bar->setBrush(mSeries->barsetAt(set)->brush()); | |
224 | // bar->setPos(xPos, yPos-barHeight); |
|
227 | // bar->setPos(xPos, yPos-barHeight); | |
225 | itemIndex++; |
|
228 | itemIndex++; | |
226 |
xPos += |
|
229 | xPos += barWidth; | |
227 | } |
|
230 | } | |
228 | } |
|
231 | } | |
229 |
|
232 | |||
230 | // Position floating values |
|
233 | // Position floating values | |
231 | itemIndex = 0; |
|
234 | itemIndex = 0; | |
232 | for (int category=0; category < mSeries->categoryCount(); category++) { |
|
235 | for (int category=0; category < mSeries->categoryCount(); category++) { | |
233 |
qreal xPos = categoryWidth * category + categoryWidth/2 + |
|
236 | qreal xPos = categoryWidth * category + categoryWidth/2 + barWidth; | |
234 |
qreal yPos = |
|
237 | qreal yPos = height; | |
235 | for (int set=0; set < mSeries->barsetCount(); set++) { |
|
238 | for (int set=0; set < mSeries->barsetCount(); set++) { | |
236 | qreal barHeight = mSeries->valueAt(set,category) * scale; |
|
239 | qreal barHeight = mSeries->valueAt(set,category) * scale; | |
237 | BarValue* value = mFloatingValues.at(itemIndex); |
|
240 | BarValue* value = mFloatingValues.at(itemIndex); | |
238 |
|
241 | |||
239 | QBarSet* barSet = mSeries->barsetAt(set); |
|
242 | QBarSet* barSet = mSeries->barsetAt(set); | |
240 | value->resize(100,50); // TODO: proper layout for this. |
|
243 | value->resize(100,50); // TODO: proper layout for this. | |
241 | value->setPos(xPos, yPos-barHeight/2); |
|
244 | value->setPos(xPos, yPos-barHeight/2); | |
242 | value->setPen(barSet->floatingValuePen()); |
|
245 | value->setPen(barSet->floatingValuePen()); | |
243 |
|
246 | |||
244 | if (mSeries->valueAt(set,category) != 0) { |
|
247 | if (mSeries->valueAt(set,category) != 0) { | |
245 | value->setValueString(QString::number(mSeries->valueAt(set,category))); |
|
248 | value->setValueString(QString::number(mSeries->valueAt(set,category))); | |
246 | } else { |
|
249 | } else { | |
247 | value->setValueString(QString("")); |
|
250 | value->setValueString(QString("")); | |
248 | } |
|
251 | } | |
249 |
|
252 | |||
250 | itemIndex++; |
|
253 | itemIndex++; | |
251 |
xPos += |
|
254 | xPos += barWidth; | |
252 | } |
|
255 | } | |
253 | } |
|
256 | } | |
254 |
|
257 | |||
255 | return layout; |
|
258 | return layout; | |
256 | } |
|
259 | } | |
257 |
|
260 | |||
258 | void BarChartItem::applyLayout(const QVector<QRectF> &layout) |
|
261 | void BarChartItem::applyLayout(const QVector<QRectF> &layout) | |
259 | { |
|
262 | { | |
260 | if (animator()) |
|
263 | if (animator()) | |
261 | animator()->updateLayout(this, mLayout, layout); |
|
264 | animator()->updateLayout(this, mLayout, layout); | |
262 | else |
|
265 | else | |
263 | setLayout(layout); |
|
266 | setLayout(layout); | |
264 | } |
|
267 | } | |
265 |
|
268 | |||
266 | void BarChartItem::setLayout(const QVector<QRectF> &layout) |
|
269 | void BarChartItem::setLayout(const QVector<QRectF> &layout) | |
267 | { |
|
270 | { | |
268 | mLayout = layout; |
|
271 | mLayout = layout; | |
269 |
|
272 | |||
270 | for (int i=0; i<mBars.count(); i++) { |
|
273 | for (int i=0; i<mBars.count(); i++) { | |
271 | //mBars.at(i)->setSize(layout.at(i).size()); |
|
274 | //mBars.at(i)->setSize(layout.at(i).size()); | |
272 | //mBars.at(i)->setPos(layout.at(i).topLeft()); |
|
275 | //mBars.at(i)->setPos(layout.at(i).topLeft()); | |
273 | mBars.at(i)->setRect(layout.at(i)); |
|
276 | mBars.at(i)->setRect(layout.at(i)); | |
274 | } |
|
277 | } | |
275 |
|
278 | |||
276 | update(); |
|
279 | update(); | |
277 | } |
|
280 | } | |
278 |
|
281 | |||
279 | void BarChartItem::initAxisLabels() |
|
282 | void BarChartItem::initAxisLabels() | |
280 | { |
|
283 | { | |
281 | int count = mSeries->categoryCount(); |
|
284 | int count = mSeries->categoryCount(); | |
282 | if (0 == count) { |
|
285 | if (0 == count) { | |
283 | return; |
|
286 | return; | |
284 | } |
|
287 | } | |
285 |
|
288 | |||
286 | Domain* domain = presenter()->dataSet()->domain(mSeries); |
|
289 | Domain* domain = presenter()->dataSet()->domain(mSeries); | |
287 |
|
290 | |||
288 | qreal min = 0; |
|
291 | qreal min = 0; | |
289 | qreal max = count+1; |
|
292 | qreal max = count+1; | |
290 |
|
293 | |||
291 | domain->setRangeX(min,max,count+1); |
|
294 | domain->setRangeX(min,max,count+1); | |
292 | } |
|
295 | } | |
293 |
|
296 | |||
294 | //handlers |
|
297 | //handlers | |
295 |
|
298 | |||
296 | void BarChartItem::handleModelChanged(int index) |
|
299 | void BarChartItem::handleModelChanged(int index) | |
297 | { |
|
300 | { | |
298 | Q_UNUSED(index) |
|
301 | Q_UNUSED(index) | |
299 | dataChanged(); |
|
302 | dataChanged(); | |
300 | } |
|
303 | } | |
301 |
|
304 | |||
302 | void BarChartItem::handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY) |
|
305 | void BarChartItem::handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY) | |
303 | { |
|
306 | { | |
304 | mDomainMinX = minX; |
|
307 | mDomainMinX = minX; | |
305 | mDomainMaxX = maxX; |
|
308 | mDomainMaxX = maxX; | |
306 | mDomainMinY = minY; |
|
309 | mDomainMinY = minY; | |
307 | mDomainMaxY = maxY; |
|
310 | mDomainMaxY = maxY; | |
308 | handleLayoutChanged(); |
|
311 | handleLayoutChanged(); | |
309 |
|
312 | |||
310 | /* |
|
313 | /* | |
311 | int count = mSeries->categoryCount(); |
|
314 | int count = mSeries->categoryCount(); | |
312 | if (0 == count) { |
|
315 | if (0 == count) { | |
313 | return; |
|
316 | return; | |
314 | } |
|
317 | } | |
315 |
|
318 | |||
316 | // Position labels to domain |
|
319 | // Position labels to domain | |
317 | qreal min = domain.minX(); |
|
320 | qreal min = domain.minX(); | |
318 | qreal max = domain.maxX(); |
|
321 | qreal max = domain.maxX(); | |
319 | qreal step = (max-min)/count; |
|
322 | qreal step = (max-min)/count; | |
320 | QChartAxisCategories& categories = mChart->axisX()->categories(); |
|
323 | QChartAxisCategories& categories = mChart->axisX()->categories(); | |
321 | categories.clear(); |
|
324 | categories.clear(); | |
322 | for (int i=0; i<count; i++) { |
|
325 | for (int i=0; i<count; i++) { | |
323 | categories.insert(min,mSeries->categoryName(i)); |
|
326 | categories.insert(min,mSeries->categoryName(i)); | |
324 | min += step; |
|
327 | min += step; | |
325 | } |
|
328 | } | |
326 | */ |
|
329 | */ | |
327 | } |
|
330 | } | |
328 |
|
331 | |||
329 | void BarChartItem::handleGeometryChanged(const QRectF& rect) |
|
332 | void BarChartItem::handleGeometryChanged(const QRectF& rect) | |
330 | { |
|
333 | { | |
331 | m_rect=rect; |
|
334 | m_rect=rect; | |
332 | layoutChanged(); |
|
335 | handleLayoutChanged(); | |
333 | mLayoutSet = true; |
|
336 | mLayoutSet = true; | |
334 | setPos(rect.topLeft()); |
|
337 | setPos(rect.topLeft()); | |
335 | } |
|
338 | } | |
336 |
|
339 | |||
337 | void BarChartItem::handleLayoutChanged() |
|
340 | void BarChartItem::handleLayoutChanged() | |
338 | { |
|
341 | { | |
339 | qDebug() << "BarChartItem::handleLayoutChanged"; |
|
342 | qDebug() << "BarChartItem::handleLayoutChanged"; | |
340 | QVector<QRectF> layout = calculateLayout(); |
|
343 | QVector<QRectF> layout = calculateLayout(); | |
341 | applyLayout(layout); |
|
344 | applyLayout(layout); | |
342 | update(); |
|
345 | update(); | |
343 | } |
|
346 | } | |
344 |
|
347 | |||
345 |
|
348 | |||
346 | void BarChartItem::showToolTip(QPoint pos, QString tip) |
|
349 | void BarChartItem::showToolTip(QPoint pos, QString tip) | |
347 | { |
|
350 | { | |
348 | // TODO: cool tooltip instead of default |
|
351 | // TODO: cool tooltip instead of default | |
349 | QToolTip::showText(pos,tip); |
|
352 | QToolTip::showText(pos,tip); | |
350 | } |
|
353 | } | |
351 |
|
354 | |||
352 | #include "moc_barchartitem_p.cpp" |
|
355 | #include "moc_barchartitem_p.cpp" | |
353 |
|
356 | |||
354 | QTCOMMERCIALCHART_END_NAMESPACE |
|
357 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,35 +1,36 | |||||
1 | #ifndef CHART_H_ |
|
1 | #ifndef CHART_H_ | |
2 | #define CHART_H_ |
|
2 | #define CHART_H_ | |
3 |
|
3 | |||
4 | #include "qchartglobal.h" |
|
4 | #include "qchartglobal.h" | |
5 | #include <QObject> |
|
5 | #include <QObject> | |
|
6 | #include <QRect> | |||
6 |
|
7 | |||
7 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
8 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
8 |
|
9 | |||
9 | class ChartAnimator; |
|
10 | class ChartAnimator; | |
10 | class ChartPresenter; |
|
11 | class ChartPresenter; | |
11 |
|
12 | |||
12 | class Chart: public QObject |
|
13 | class Chart: public QObject | |
13 | { |
|
14 | { | |
14 | Q_OBJECT |
|
15 | Q_OBJECT | |
15 | public: |
|
16 | public: | |
16 | explicit Chart(ChartPresenter *presenter); |
|
17 | explicit Chart(ChartPresenter *presenter); | |
17 |
|
18 | |||
18 | public slots: |
|
19 | public slots: | |
19 | virtual void handleGeometryChanged(const QRectF& rect); |
|
20 | virtual void handleGeometryChanged(const QRectF& rect); | |
20 | virtual void handleDomainChanged(qreal minX,qreal maxX,qreal minY,qreal maxY); |
|
21 | virtual void handleDomainChanged(qreal minX,qreal maxX,qreal minY,qreal maxY); | |
21 | virtual void rangeXChanged(qreal min, qreal max, int tickXCount); |
|
22 | virtual void rangeXChanged(qreal min, qreal max, int tickXCount); | |
22 | virtual void rangeYChanged(qreal min, qreal max, int tickYCount); |
|
23 | virtual void rangeYChanged(qreal min, qreal max, int tickYCount); | |
23 |
|
24 | |||
24 | void setAnimator(ChartAnimator* animator); |
|
25 | void setAnimator(ChartAnimator* animator); | |
25 | ChartAnimator* animator() const; |
|
26 | ChartAnimator* animator() const; | |
26 | ChartPresenter* presenter() const; |
|
27 | ChartPresenter* presenter() const; | |
27 |
|
28 | |||
28 | private: |
|
29 | private: | |
29 | ChartAnimator* m_animator; |
|
30 | ChartAnimator* m_animator; | |
30 | ChartPresenter* m_presenter; |
|
31 | ChartPresenter* m_presenter; | |
31 | }; |
|
32 | }; | |
32 |
|
33 | |||
33 | QTCOMMERCIALCHART_END_NAMESPACE |
|
34 | QTCOMMERCIALCHART_END_NAMESPACE | |
34 |
|
35 | |||
35 | #endif |
|
36 | #endif |
General Comments 0
You need to be logged in to leave comments.
Login now