##// END OF EJS Templates
Adds animation settings handling
Michal Klocek -
r298:cf230c2f88fb
parent child
Show More
@@ -1,333 +1,335
1 1 #include "axisitem_p.h"
2 2 #include "qchartaxis.h"
3 3 #include "chartpresenter_p.h"
4 4 #include <QPainter>
5 5 #include <QDebug>
6 6
7 7 static int label_padding = 5;
8 8
9 9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10 10
11 11 AxisItem::AxisItem(AxisType type,QGraphicsItem* parent) :
12 12 ChartItem(parent),
13 13 m_type(type),
14 14 m_labelsAngle(0),
15 15 m_grid(parent),
16 16 m_shades(parent),
17 17 m_labels(parent),
18 18 m_axis(parent)
19 19 {
20 20 //initial initialization
21 21 m_axis.setZValue(ChartPresenter::AxisZValue);
22 22 m_shades.setZValue(ChartPresenter::ShadesZValue);
23 23 m_grid.setZValue(ChartPresenter::GridZValue);
24 24 setFlags(QGraphicsItem::ItemHasNoContents);
25 25 }
26 26
27 27 AxisItem::~AxisItem()
28 28 {
29 29 }
30 30
31 31 QRectF AxisItem::boundingRect() const
32 32 {
33 33 return QRectF();
34 34 }
35 35
36 36 void AxisItem::createItems(int count)
37 37 {
38 38 if(m_axis.children().size()==0)
39 39 m_axis.addToGroup(new QGraphicsLineItem());
40 40 for (int i = 0; i < count; ++i) {
41 41 m_grid.addToGroup(new QGraphicsLineItem());
42 42 m_labels.addToGroup(new QGraphicsSimpleTextItem());
43 43 if(m_grid.childItems().size()%2) m_shades.addToGroup(new QGraphicsRectItem());
44 44 m_axis.addToGroup(new QGraphicsLineItem());
45 45 }
46 46 }
47 47
48 48 void AxisItem::clear(int count)
49 49 {
50 50 QList<QGraphicsItem *> lines = m_grid.childItems();
51 51 QList<QGraphicsItem *> labels = m_labels.childItems();
52 52 QList<QGraphicsItem *> shades = m_shades.childItems();
53 53 QList<QGraphicsItem *> axis = m_axis.childItems();
54 54
55 55 for (int i = 0; i < count; ++i) {
56 56 delete(lines.takeLast());
57 57 delete(labels.takeLast());
58 58 if(lines.size()%2) delete(shades.takeLast());
59 59 delete(axis.takeLast());
60 60 }
61 61
62 62 m_thicksList.clear();
63 63
64 64 }
65 65
66 66 void AxisItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
67 67 {
68 68 Q_UNUSED(painter);
69 69 Q_UNUSED(option);
70 70 Q_UNUSED(widget);
71 71 }
72 72
73 73 void AxisItem::updateItems(QVector<qreal>& vector)
74 74 {
75 75 calculateLayout(vector);
76 76 if(vector.count()==0) return;
77 77 applyLayout(vector);
78 78 }
79 79
80 80 void AxisItem::handleAxisUpdate(QChartAxis* axis)
81 81 {
82 if(m_layoutVector.count()==0) return;
83
82 84 if(axis->isAxisVisible()) {
83 85 setAxisOpacity(100);
84 86 }
85 87 else {
86 88 setAxisOpacity(0);
87 89 }
88 90
89 91 if(axis->isGridVisible()) {
90 92 setGridOpacity(100);
91 93 }
92 94 else {
93 95 setGridOpacity(0);
94 96 }
95 97
96 98 if(axis->isLabelsVisible())
97 99 {
98 100 setLabelsOpacity(100);
99 101 }
100 102 else {
101 103 setLabelsOpacity(0);
102 104 }
103 105
104 106 if(axis->isShadesVisible()) {
105 107 setShadesOpacity(axis->shadesOpacity());
106 108 }
107 109 else {
108 110 setShadesOpacity(0);
109 111 }
110 112
111 113 setLabelsAngle(axis->labelsAngle());
112 114 setAxisPen(axis->axisPen());
113 115 setLabelsPen(axis->labelsPen());
114 116 setLabelsBrush(axis->labelsBrush());
115 117 setLabelsFont(axis->labelFont());
116 118 setGridPen(axis->gridPen());
117 119 setShadesPen(axis->shadesPen());
118 120 setShadesBrush(axis->shadesBrush());
119 121 }
120 122
121 123 void AxisItem::handleLabelsChanged(QChartAxis* axis,const QStringList& labels)
122 124 {
123 125 int diff = m_thicksList.size() - labels.size();
124 126
125 127 if(diff>0){
126 128 clear(diff);
127 129 }else if(diff<0){
128 130 createItems(-diff);
129 131 }
130 132 m_thicksList=labels;
131 133 m_layoutVector.resize(m_thicksList.size());
132 134 updateItems(m_layoutVector);
133 handleAxisUpdate(axis);
135 if(diff!=0) handleAxisUpdate(axis);
134 136 }
135 137
136 138 void AxisItem::handleGeometryChanged(const QRectF& rect)
137 139 {
138 140 m_rect = rect;
139 141 updateItems(m_layoutVector);
140 142 }
141 143
142 144 void AxisItem::setAxisOpacity(qreal opacity)
143 145 {
144 146 m_axis.setOpacity(opacity);
145 147 }
146 148
147 149 qreal AxisItem::axisOpacity() const
148 150 {
149 151 return m_axis.opacity();
150 152 }
151 153
152 154 void AxisItem::setGridOpacity(qreal opacity)
153 155 {
154 156 m_grid.setOpacity(opacity);
155 157 }
156 158
157 159 qreal AxisItem::gridOpacity() const
158 160 {
159 161 return m_grid.opacity();
160 162 }
161 163
162 164 void AxisItem::setLabelsOpacity(qreal opacity)
163 165 {
164 166 m_labels.setOpacity(opacity);
165 167 }
166 168
167 169 qreal AxisItem::labelsOpacity() const
168 170 {
169 171 return m_labels.opacity();
170 172 }
171 173
172 174 void AxisItem::setShadesOpacity(qreal opacity)
173 175 {
174 176 m_shades.setOpacity(opacity);
175 177 }
176 178
177 179 qreal AxisItem::shadesOpacity() const
178 180 {
179 181 return m_shades.opacity();
180 182 }
181 183
182 184 void AxisItem::setLabelsAngle(int angle)
183 185 {
184 186 foreach(QGraphicsItem* item , m_labels.childItems()) {
185 187 QPointF center = item->boundingRect().center();
186 188 item->setRotation(angle);
187 189 }
188 190
189 191 m_labelsAngle=angle;
190 192 }
191 193
192 194 void AxisItem::setLabelsPen(const QPen& pen)
193 195 {
194 196 foreach(QGraphicsItem* item , m_labels.childItems()) {
195 197 static_cast<QGraphicsSimpleTextItem*>(item)->setPen(pen);
196 198 }
197 199 }
198 200
199 201 void AxisItem::setLabelsBrush(const QBrush& brush)
200 202 {
201 203 foreach(QGraphicsItem* item , m_labels.childItems()) {
202 204 static_cast<QGraphicsSimpleTextItem*>(item)->setBrush(brush);
203 205 }
204 206 }
205 207
206 208 void AxisItem::setLabelsFont(const QFont& font)
207 209 {
208 210 foreach(QGraphicsItem* item , m_labels.childItems()) {
209 211 static_cast<QGraphicsSimpleTextItem*>(item)->setFont(font);
210 212 }
211 213 }
212 214
213 215 void AxisItem::setShadesBrush(const QBrush& brush)
214 216 {
215 217 foreach(QGraphicsItem* item , m_shades.childItems()) {
216 218 static_cast<QGraphicsRectItem*>(item)->setBrush(brush);
217 219 }
218 220 }
219 221
220 222 void AxisItem::setShadesPen(const QPen& pen)
221 223 {
222 224 foreach(QGraphicsItem* item , m_shades.childItems()) {
223 225 static_cast<QGraphicsRectItem*>(item)->setPen(pen);
224 226 }
225 227 }
226 228
227 229 void AxisItem::setAxisPen(const QPen& pen)
228 230 {
229 231 foreach(QGraphicsItem* item , m_axis.childItems()) {
230 232 static_cast<QGraphicsLineItem*>(item)->setPen(pen);
231 233 }
232 234 }
233 235
234 236 void AxisItem::setGridPen(const QPen& pen)
235 237 {
236 238 foreach(QGraphicsItem* item , m_grid.childItems()) {
237 239 static_cast<QGraphicsLineItem*>(item)->setPen(pen);
238 240 }
239 241 }
240 242
241 243 void AxisItem::calculateLayout(QVector<qreal>& points)
242 244 {
243 245 switch (m_type)
244 246 {
245 247 case X_AXIS:
246 248 {
247 249 const qreal deltaX = m_rect.width()/(m_thicksList.size()-1);
248 250 for (int i = 0; i < m_thicksList.size(); ++i) {
249 251 int x = i * deltaX + m_rect.left();
250 252 points[i]=x;
251 253 }
252 254 }
253 255 break;
254 256 case Y_AXIS:
255 257 {
256 258 const qreal deltaY = m_rect.height()/(m_thicksList.size()-1);
257 259 for (int i = 0; i < m_thicksList.size(); ++i) {
258 260 int y = i * -deltaY + m_rect.bottom();
259 261 points[i]=y;
260 262 }
261 263 }
262 264 break;
263 265 }
264 266 }
265 267
266 268 void AxisItem::applyLayout(const QVector<qreal>& points)
267 269 {
268 270 Q_ASSERT(points.size() == m_thicksList.size());
269 271
270 272 QList<QGraphicsItem *> lines = m_grid.childItems();
271 273 QList<QGraphicsItem *> labels = m_labels.childItems();
272 274 QList<QGraphicsItem *> shades = m_shades.childItems();
273 275 QList<QGraphicsItem *> axis = m_axis.childItems();
274 276
275 277 Q_ASSERT(labels.size() == m_thicksList.size());
276 278
277 279 switch (m_type)
278 280 {
279 281 case X_AXIS:
280 282 {
281 283 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(axis.at(0));
282 284 lineItem->setLine(m_rect.left(), m_rect.bottom(), m_rect.right(), m_rect.bottom());
283 285
284 286 for (int i = 0; i < points.size(); ++i) {
285 287 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i));
286 288 lineItem->setLine(points[i], m_rect.top(), points[i], m_rect.bottom());
287 289 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i));
288 290 labelItem->setText(m_thicksList.at(i));
289 291 QPointF center = labelItem->boundingRect().center();
290 292 labelItem->setTransformOriginPoint(center.x(), center.y());
291 293 labelItem->setPos(points[i] - center.x(), m_rect.bottom() + label_padding);
292 294 if(i%2){
293 295 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2));
294 296 rectItem->setRect(points[i],m_rect.top(),points[i+1]-points[i],m_rect.height());
295 297 }
296 298 lineItem = static_cast<QGraphicsLineItem*>(axis.at(i+1));
297 299 lineItem->setLine(points[i],m_rect.bottom(),points[i],m_rect.bottom()+5);
298 300 }
299 301 }
300 302 break;
301 303
302 304 case Y_AXIS:
303 305 {
304 306 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(axis.at(0));
305 307 lineItem->setLine(m_rect.left() , m_rect.top(), m_rect.left(), m_rect.bottom());
306 308
307 309 for (int i = 0; i < points.size(); ++i) {
308 310 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i));
309 311 lineItem->setLine(m_rect.left() , points[i], m_rect.right(), points[i]);
310 312 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i));
311 313 labelItem->setText(m_thicksList.at(i));
312 314 QPointF center = labelItem->boundingRect().center();
313 315 labelItem->setTransformOriginPoint(center.x(), center.y());
314 316 labelItem->setPos(m_rect.left() - labelItem->boundingRect().width() - label_padding , points[i]-center.y());
315 317 if(i%2){
316 318 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2));
317 319 rectItem->setRect(m_rect.left(),points[i],m_rect.width(),points[i]-points[i+1]);
318 320 }
319 321 lineItem = static_cast<QGraphicsLineItem*>(axis.at(i+1));
320 322 lineItem->setLine(m_rect.left()-5,points[i],m_rect.left(),points[i]);
321 323 }
322 324 }
323 325 break;
324 326 default:
325 327 qDebug()<<"Unknown axis type";
326 328 break;
327 329 }
328 330 }
329 331
330 332 //TODO "nice numbers algorithm"
331 333 #include "moc_axisitem_p.cpp"
332 334
333 335 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,60 +1,60
1 1 #ifndef CHARTDATASET_P_H_
2 2 #define CHARTDATASET_P_H_
3 3
4 4 #include "qchartseries.h"
5 5 #include "domain_p.h"
6 6 #include <QVector>
7 7
8 8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 9
10 10 class QChartAxis;
11 11
12 12 class ChartDataSet : public QObject
13 13 {
14 14 Q_OBJECT
15 15 public:
16 16 ChartDataSet(QObject* parent=0);
17 17 virtual ~ChartDataSet();
18 18
19 19 void addSeries(QChartSeries* series,QChartAxis *axisY = 0);
20 20 void removeSeries(QChartSeries* series);
21 21 void removeAllSeries();
22 22 void addDomain(const QRectF& rect, const QRectF& viewport);
23 23 bool nextDomain();
24 24 bool previousDomain();
25 25 void clearDomains(int toIndex =0);
26 26 const Domain domain(QChartAxis *axisY) const;
27 27 int domainIndex() const {return m_domainIndex;}
28 void setDomain(int index);
28 29
29 30 QChartAxis* axisX() const { return m_axisX;};
30 31 QChartAxis* axisY(QChartSeries* series = 0) const;
31 32
32 33 signals:
33 34 void seriesAdded(QChartSeries* series);
34 35 void seriesRemoved(QChartSeries* series);
35 36 void axisAdded(QChartAxis* axis);
36 37 void axisRemoved(QChartAxis* axis);
37 38 void axisLabelsChanged(QChartAxis* axis, const QStringList& labels);
38 39 void seriesDomainChanged(QChartSeries* series,const Domain& domain);
39 40
40 41 private slots:
41 42 void handleMinChanged(qreal min);
42 43 void handleMaxChanged(qreal max);
43 44 void handleTickChanged(QChartAxis*);
44 45
45 46 private:
46 void setDomain(int index);
47 47 QStringList createLabels(QChartAxis* axis,qreal min, qreal max);
48 48
49 49 private:
50 50 QMultiMap<QChartAxis*, Domain> m_domainMap;
51 51 QMultiMap<QChartAxis*, QChartSeries*> m_seriesMap;
52 52 QChartAxis* m_axisX;
53 53 QChartAxis* m_axisY;
54 54 int m_domainIndex;
55 55 bool m_axisXInitialized;
56 56 };
57 57
58 58 QTCOMMERCIALCHART_END_NAMESPACE
59 59
60 60 #endif /* CHARTENGINE_P_H_ */
@@ -1,241 +1,284
1 1 #include "qchart.h"
2 2 #include "qchartaxis.h"
3 3 #include "chartpresenter_p.h"
4 4 #include "chartdataset_p.h"
5 5 #include "charttheme_p.h"
6 6 //series
7 7 #include "qbarchartseries.h"
8 8 #include "qstackedbarchartseries.h"
9 9 #include "qpercentbarchartseries.h"
10 10 #include "qlinechartseries.h"
11 11 #include "qpieseries.h"
12 12 #include "qscatterseries.h"
13 13 //items
14 14 #include "axisitem_p.h"
15 #include "axisanimationitem_p.h"
15 16 #include "barpresenter.h"
16 17 #include "stackedbarpresenter.h"
17 18 #include "linechartitem_p.h"
18 19 #include "percentbarpresenter.h"
19 20 #include "linechartanimationitem_p.h"
20 21 #include "piepresenter.h"
21 22 #include "scatterpresenter_p.h"
22 23
23 24 QTCOMMERCIALCHART_BEGIN_NAMESPACE
24 25
25 26 ChartPresenter::ChartPresenter(QChart* chart,ChartDataSet* dataset):QObject(chart),
26 27 m_chart(chart),
27 28 m_dataset(dataset),
28 29 m_chartTheme(0),
29 30 m_marginSize(0),
30 m_rect(QRectF(QPoint(0,0),m_chart->size()))
31 m_rect(QRectF(QPoint(0,0),m_chart->size())),
32 m_options(0)
31 33 {
32 34 createConnections();
33 35 setChartTheme(QChart::ChartThemeDefault);
34 36
35 37 }
36 38
37 39 ChartPresenter::~ChartPresenter()
38 40 {
39 41 }
40 42
41 43 void ChartPresenter::createConnections()
42 44 {
43 45 QObject::connect(m_chart,SIGNAL(geometryChanged()),this,SLOT(handleGeometryChanged()));
44 46 QObject::connect(m_dataset,SIGNAL(seriesAdded(QChartSeries*)),this,SLOT(handleSeriesAdded(QChartSeries*)));
45 47 QObject::connect(m_dataset,SIGNAL(seriesRemoved(QChartSeries*)),this,SLOT(handleSeriesRemoved(QChartSeries*)));
46 48 QObject::connect(m_dataset,SIGNAL(axisAdded(QChartAxis*)),this,SLOT(handleAxisAdded(QChartAxis*)));
47 49 QObject::connect(m_dataset,SIGNAL(axisRemoved(QChartAxis*)),this,SLOT(handleAxisRemoved(QChartAxis*)));
48 50 QObject::connect(m_dataset,SIGNAL(seriesDomainChanged(QChartSeries*,const Domain&)),this,SLOT(handleSeriesDomainChanged(QChartSeries*,const Domain&)));
49 51 QObject::connect(m_dataset,SIGNAL(axisLabelsChanged(QChartAxis*,const QStringList&)),this,SLOT(handleAxisLabelsChanged(QChartAxis*,const QStringList&)));
50 52 }
51 53
52 54
53 55 QRectF ChartPresenter::geometry() const
54 56 {
55 57 return m_rect;
56 58 }
57 59
58 60 void ChartPresenter::handleGeometryChanged()
59 61 {
60 62 m_rect = QRectF(QPoint(0,0),m_chart->size());
61 63 m_rect.adjust(m_marginSize,m_marginSize, -m_marginSize, -m_marginSize);
62 64 Q_ASSERT(m_rect.isValid());
63 65 emit geometryChanged(m_rect);
64 66 }
65 67
66 68 int ChartPresenter::margin() const
67 69 {
68 70 return m_marginSize;
69 71 }
70 72
71 73 void ChartPresenter::setMargin(int margin)
72 74 {
73 75 m_marginSize = margin;
74 76 }
75 77
76 78 void ChartPresenter::handleAxisAdded(QChartAxis* axis)
77 79 {
80
78 81 AxisItem* item ;
79 82
80 if(axis==m_dataset->axisX()){
81 item = new AxisItem(AxisItem::X_AXIS,m_chart);
83 if(!m_options.testFlag(QChart::GridAxisAnimations))
84 {
85 item = new AxisItem(axis==m_dataset->axisX()?AxisItem::X_AXIS : AxisItem::Y_AXIS,m_chart);
86 qDebug()<<"kuku1";
82 87 }else{
83 item = new AxisItem(AxisItem::Y_AXIS,m_chart);
88 item = new AxisAnimationItem(axis==m_dataset->axisX()?AxisItem::X_AXIS : AxisItem::Y_AXIS,m_chart);
89 qDebug()<<"kuku2";
84 90 }
91
85 92 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
86 93 QObject::connect(axis,SIGNAL(update(QChartAxis*)),item,SLOT(handleAxisUpdate(QChartAxis*)));
87 94
88 95 item->handleAxisUpdate(axis);
89 96 item->handleGeometryChanged(m_rect);
90 97 m_chartTheme->decorate(axis,item);
91 98 m_axisItems.insert(axis,item);
92 99 }
93 100
94 101 void ChartPresenter::handleAxisRemoved(QChartAxis* axis)
95 102 {
96 103 AxisItem* item = m_axisItems.take(axis);
97 104 Q_ASSERT(item);
98 105 delete item;
99 106 }
100 107
101 108
102 109 void ChartPresenter::handleSeriesAdded(QChartSeries* series)
103 110 {
104 111 switch(series->type())
105 112 {
106 113 case QChartSeries::SeriesTypeLine: {
107 114 QLineChartSeries* lineSeries = static_cast<QLineChartSeries*>(series);
108 LineChartItem* item = new LineChartAnimationItem(this,lineSeries,m_chart);
115 LineChartItem* item;
116 if(m_options.testFlag(QChart::SeriesAnimations)){
117 item = new LineChartAnimationItem(this,lineSeries,m_chart);
118 }else{
119 item = new LineChartItem(this,lineSeries,m_chart);
120 }
109 121 m_chartTheme->decorate(item,lineSeries,m_chartItems.count());
110 122 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
111 123 QObject::connect(lineSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int)));
112 124 m_chartItems.insert(series,item);
113 125 break;
114 126 }
115 127
116 128 case QChartSeries::SeriesTypeBar: {
117 129 QBarChartSeries* barSeries = static_cast<QBarChartSeries*>(series);
118 130 BarPresenter* item = new BarPresenter(barSeries,m_chart);
119 131 m_chartTheme->decorate(item,barSeries,m_chartItems.count());
120 132 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
121 133 QObject::connect(barSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int)));
122 134 m_chartItems.insert(series,item);
123 135 // m_axisXItem->setVisible(false);
124 136 break;
125 137 }
126 138
127 139 case QChartSeries::SeriesTypeStackedBar: {
128 140
129 141 QStackedBarChartSeries* stackedBarSeries = static_cast<QStackedBarChartSeries*>(series);
130 142 StackedBarPresenter* item = new StackedBarPresenter(stackedBarSeries,m_chart);
131 143 m_chartTheme->decorate(item,stackedBarSeries,m_chartItems.count());
132 144 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
133 145 QObject::connect(stackedBarSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int)));
134 146 m_chartItems.insert(series,item);
135 147 break;
136 148 }
137 149
138 150 case QChartSeries::SeriesTypePercentBar: {
139 151
140 152 QPercentBarChartSeries* percentBarSeries = static_cast<QPercentBarChartSeries*>(series);
141 153 PercentBarPresenter* item = new PercentBarPresenter(percentBarSeries,m_chart);
142 154 m_chartTheme->decorate(item,percentBarSeries ,m_chartItems.count());
143 155 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
144 156 QObject::connect(percentBarSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int)));
145 157 m_chartItems.insert(series,item);
146 158 break;
147 159 }
148 160 case QChartSeries::SeriesTypeScatter: {
149 161 QScatterSeries *scatterSeries = qobject_cast<QScatterSeries *>(series);
150 162 ScatterPresenter *scatterPresenter = new ScatterPresenter(scatterSeries, m_chart);
151 163 QObject::connect(this, SIGNAL(geometryChanged(const QRectF&)),
152 164 scatterPresenter, SLOT(handleGeometryChanged(const QRectF&)));
153 165 m_chartTheme->decorate(scatterPresenter, scatterSeries, m_chartItems.count());
154 166 m_chartItems.insert(scatterSeries, scatterPresenter);
155 167 break;
156 168 }
157 169 case QChartSeries::SeriesTypePie: {
158 170 QPieSeries *s = qobject_cast<QPieSeries *>(series);
159 171 PiePresenter* pie = new PiePresenter(m_chart, s);
160 172 m_chartTheme->decorate(pie, s, m_chartItems.count());
161 173 QObject::connect(this, SIGNAL(geometryChanged(const QRectF&)), pie, SLOT(handleGeometryChanged(const QRectF&)));
162 174
163 175 // Hide all from background when there is only piechart
164 176 // TODO: refactor this ugly code... should be one setting for this
165 177 if (m_chartItems.count() == 0) {
166 178 m_chart->axisX()->setAxisVisible(false);
167 179 m_chart->axisY()->setAxisVisible(false);
168 180 m_chart->axisX()->setGridVisible(false);
169 181 m_chart->axisY()->setGridVisible(false);
170 182 m_chart->axisX()->setLabelsVisible(false);
171 183 m_chart->axisY()->setLabelsVisible(false);
172 184 m_chart->axisX()->setShadesVisible(false);
173 185 m_chart->axisY()->setShadesVisible(false);
174 186 m_chart->setChartBackgroundBrush(Qt::transparent);
175 187 }
176 188
177 189 m_chartItems.insert(series, pie);
178 190 break;
179 191 }
180 192 default: {
181 193 qDebug()<< "Series type" << series->type() << "not implemented.";
182 194 break;
183 195 }
184 196 }
185 197
186 198 if(m_rect.isValid()) emit geometryChanged(m_rect);
187 199 }
188 200
189 201 void ChartPresenter::handleSeriesRemoved(QChartSeries* series)
190 202 {
191 203 ChartItem* item = m_chartItems.take(series);
192 204 delete item;
193 205 }
194 206
195 207 void ChartPresenter::handleSeriesChanged(QChartSeries* series)
196 208 {
197 209 //TODO:
198 210 }
199 211
200 212 void ChartPresenter::handleSeriesDomainChanged(QChartSeries* series, const Domain& domain)
201 213 {
202 214 m_chartItems.value(series)->handleDomainChanged(domain);
203 215 }
204 216
205 217 void ChartPresenter::handleAxisLabelsChanged(QChartAxis* axis,const QStringList& labels)
206 218 {
207 219 m_axisItems.value(axis)->handleLabelsChanged(axis,labels);
208 220 }
209 221
210 222 void ChartPresenter::setChartTheme(QChart::ChartTheme theme)
211 223 {
212 224 delete m_chartTheme;
213 225
214 226 m_chartTheme = ChartTheme::createTheme(theme);
215 227
216 228 m_chartTheme->decorate(m_chart);
217 229 QMapIterator<QChartSeries*,ChartItem*> i(m_chartItems);
218 230
219 231 int index=0;
220 232 while (i.hasNext()) {
221 233 i.next();
222 234 index++;
223 235 m_chartTheme->decorate(i.value(),i.key(),index);
224 236 }
225 237
226 238 QMapIterator<QChartAxis*,AxisItem*> j(m_axisItems);
227 239 while (j.hasNext()) {
228 240 j.next();
229 241 m_chartTheme->decorate(j.key(),j.value());
230 242 }
231 243 }
232 244
233 245 QChart::ChartTheme ChartPresenter::chartTheme()
234 246 {
235 247 return m_chartTheme->id();
236 248 }
237 249
250 void ChartPresenter::setAnimationOptions(QChart::AnimationOptions options)
251 {
252 if(m_options!=options) {
253
254 m_options=options;
255
256 //recreate elements
257
258 QList<QChartAxis*> axisList = m_axisItems.uniqueKeys();
259 QList<QChartSeries*> seriesList = m_chartItems.uniqueKeys();
260
261 foreach(QChartAxis* axis, axisList) {
262 handleAxisRemoved(axis);
263 handleAxisAdded(axis);
264 }
265 foreach(QChartSeries* series, seriesList) {
266 handleSeriesRemoved(series);
267 handleSeriesAdded(series);
268 }
269
270 //now reintialize view data
271 //TODO: make it more nice
272 m_dataset->setDomain(m_dataset->domainIndex());
273 }
274 }
275
276 QChart::AnimationOptions ChartPresenter::animationOptions() const
277 {
278 return m_options;
279 }
280
238 281
239 282 #include "moc_chartpresenter_p.cpp"
240 283
241 284 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,63 +1,67
1 1 #ifndef CHARTPRESENTER_H_
2 2 #define CHARTPRESENTER_H_
3 3
4 4 #include "qchartglobal.h"
5 5 #include "qchart.h" //becouse of QChart::ChartThemeId //TODO
6 6 #include "qchartaxis.h"
7 7 #include <QRectF>
8 8
9 9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10 10
11 11 class ChartItem;
12 12 class QChartSeries;
13 13 class ChartDataSet;
14 14 //class QChart;
15 15 class Domain;
16 16 class AxisItem;
17 17 class ChartTheme;
18 18
19 19 class ChartPresenter: public QObject
20 20 {
21 21 Q_OBJECT
22 22 public:
23 23 enum ZValues { BackgroundZValue = -1 , ShadesZValue, GridZValue, AxisZValue , LineChartZValue };
24 24
25 25 ChartPresenter(QChart* chart,ChartDataSet *dataset);
26 26 virtual ~ChartPresenter();
27 27
28 28 void setMargin(int margin);
29 29 int margin() const;
30 30
31 31 QRectF geometry() const;
32 32
33 33 void setChartTheme(QChart::ChartTheme theme);
34 34 QChart::ChartTheme chartTheme();
35 35
36 void setAnimationOptions(QChart::AnimationOptions options);
37 QChart::AnimationOptions animationOptions() const;
38
36 39 private:
37 40 void createConnections();
38 41
39 42 public slots:
40 43 void handleSeriesAdded(QChartSeries* series);
41 44 void handleSeriesRemoved(QChartSeries* series);
42 45 void handleAxisAdded(QChartAxis* axis);
43 46 void handleAxisRemoved(QChartAxis* axis);
44 47 void handleSeriesDomainChanged(QChartSeries* series, const Domain& domain);
45 48 void handleAxisLabelsChanged(QChartAxis* axis, const QStringList& labels);
46 49 void handleSeriesChanged(QChartSeries* series);
47 50 void handleGeometryChanged();
48 51 signals:
49 52 void geometryChanged(const QRectF& rect);
50 53 private:
51 54 QMap<QChartSeries*,ChartItem*> m_chartItems;
52 55 QMap<QChartAxis*,AxisItem*> m_axisItems;
53 56 QChart* m_chart;
54 57 ChartDataSet* m_dataset;
55 58 ChartTheme *m_chartTheme;
56 59 int m_marginSize;
57 60 QRectF m_rect;
61 QChart::AnimationOptions m_options;
58 62
59 63 };
60 64
61 65 QTCOMMERCIALCHART_END_NAMESPACE
62 66
63 67 #endif /* CHARTPRESENTER_H_ */
@@ -1,258 +1,274
1 1 #include "qchart.h"
2 2 #include "qchartaxis.h"
3 3 #include "chartpresenter_p.h"
4 4 #include "chartdataset_p.h"
5 5 #include <QGraphicsScene>
6 6 #include <QGraphicsSceneResizeEvent>
7 7 #include <QDebug>
8 8
9 9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10 10
11 11 /*!
12 12 \enum QChart::ChartTheme
13 13
14 14 This enum describes the theme used by the chart.
15 15
16 16 \value ChartThemeDefault
17 17 \value ChartThemeVanilla
18 18 \value ChartThemeIcy
19 19 \value ChartThemeGrayscale
20 20 \value ChartThemeScientific
21 21 */
22 22
23 23 /*!
24 24 \class QChart
25 25 \brief QtCommercial chart API.
26 26
27 27 QChart is a QGraphicsWidget that you can show in a QGraphicsScene. It manages the graphical
28 28 representation of different types of QChartSeries and other chart related objects like
29 29 QChartAxis and QChartLegend. If you simply want to show a chart in a layout, you can use the
30 30 convenience class QChartView instead of QChart.
31 31 \sa QChartView
32 32 */
33 33
34 34 /*!
35 35 Constructs a chart object which is a child of a\a parent. Parameter \a wFlags is passed to the QGraphicsWidget constructor.
36 36 */
37 37 QChart::QChart(QGraphicsItem *parent, Qt::WindowFlags wFlags) : QGraphicsWidget(parent,wFlags),
38 38 m_backgroundItem(0),
39 39 m_titleItem(0),
40 40 m_dataset(new ChartDataSet(this)),
41 41 m_presenter(new ChartPresenter(this,m_dataset))
42 42 {
43 43 }
44 44
45 45 /*!
46 46 Destroys the object and it's children, like QChartSeries and QChartAxis object added to it.
47 47 */
48 48 QChart::~QChart()
49 49 {
50 50 }
51 51
52 52 /*!
53 53 Adds the \a series and optional \a axisY onto the chart and takes the ownership of the objects.
54 54 If auto scaling is enabled, re-scales the axes the series is bound to (both the x axis and
55 55 the y axis).
56 56 */
57 57 void QChart::addSeries(QChartSeries* series, QChartAxis* axisY)
58 58 {
59 59 m_dataset->addSeries(series, axisY);
60 60 }
61 61
62 62 /*!
63 63 Removes the \a series specified in a perameter from the QChartView.
64 64 It releses its ownership of the specified QChartSeries object.
65 65 It does not delete the pointed QChartSeries data object
66 66 \sa addSeries(), removeAllSeries()
67 67 */
68 68 void QChart::removeSeries(QChartSeries* series)
69 69 {
70 70 m_dataset->removeSeries(series);
71 71 }
72 72
73 73 /*!
74 74 Removes all the QChartSeries that have been added to the QChartView
75 75 It also deletes the pointed QChartSeries data objects
76 76 \sa addSeries(), removeSeries()
77 77 */
78 78 void QChart::removeAllSeries()
79 79 {
80 80 m_dataset->removeAllSeries();
81 81 }
82 82
83 83 /*!
84 84 Sets the \a brush that is used for painting the background of the chart area.
85 85 */
86 86 void QChart::setChartBackgroundBrush(const QBrush& brush)
87 87 {
88 88 createChartBackgroundItem();
89 89 m_backgroundItem->setBrush(brush);
90 90 m_backgroundItem->update();
91 91 }
92 92
93 93 /*!
94 94 Sets the \a pen that is used for painting the background of the chart area.
95 95 */
96 96 void QChart::setChartBackgroundPen(const QPen& pen)
97 97 {
98 98 createChartBackgroundItem();
99 99 m_backgroundItem->setPen(pen);
100 100 m_backgroundItem->update();
101 101 }
102 102
103 103 /*!
104 104 Sets the chart \a title. The description text that is rendered above the chart.
105 105 */
106 106 void QChart::setChartTitle(const QString& title)
107 107 {
108 108 createChartTitleItem();
109 109 m_titleItem->setPlainText(title);
110 110 }
111 111
112 112 /*!
113 113 Sets the \a font that is used for rendering the description text that is rendered above the chart.
114 114 */
115 115 void QChart::setChartTitleFont(const QFont& font)
116 116 {
117 117 createChartTitleItem();
118 118 m_titleItem->setFont(font);
119 119 }
120 120
121 121 void QChart::createChartBackgroundItem()
122 122 {
123 123 if(!m_backgroundItem) {
124 124 m_backgroundItem = new QGraphicsRectItem(this);
125 125 m_backgroundItem->setPen(Qt::NoPen);
126 126 m_backgroundItem->setZValue(ChartPresenter::BackgroundZValue);
127 127 }
128 128 }
129 129
130 130 void QChart::createChartTitleItem()
131 131 {
132 132 if(!m_titleItem) {
133 133 m_titleItem = new QGraphicsTextItem(this);
134 134 m_titleItem->setZValue(ChartPresenter::BackgroundZValue);
135 135 }
136 136 }
137 137
138 138 /*!
139 139 Returns the chart margin, which is the distance between the widget edge and the part of the chart where the actual data can be displayed.
140 140 \sa setMargin()
141 141 */
142 142 int QChart::margin() const
143 143 {
144 144 return m_presenter->margin();
145 145 }
146 146
147 147 /*!
148 148 Sets the chart \a margin, which is the distance between the widget edge and the part of the chart where the actual data can be displayed.
149 149 \sa margin()
150 150 */
151 151 void QChart::setMargin(int margin)
152 152 {
153 153 m_presenter->setMargin(margin);
154 154 }
155 155
156 156 /*!
157 157 Sets the \a theme used by the chart for rendering the graphical representation of the data
158 158 \sa ChartTheme, chartTheme()
159 159 */
160 160 void QChart::setChartTheme(QChart::ChartTheme theme)
161 161 {
162 162 m_presenter->setChartTheme(theme);
163 163 }
164 164
165 165 /*!
166 166 Returns the theme enum used by the chart.
167 167 \sa ChartTheme, setChartTheme()
168 168 */
169 169 QChart::ChartTheme QChart::chartTheme() const
170 170 {
171 171 return m_presenter->chartTheme();
172 172 }
173 173
174 174 /*!
175 175 Zooms in the view by a factor of 2
176 176 */
177 177 void QChart::zoomIn()
178 178 {
179 179 if (!m_dataset->nextDomain()) {
180 180 QRectF rect = m_presenter->geometry();
181 181 rect.setWidth(rect.width()/2);
182 182 rect.setHeight(rect.height()/2);
183 183 rect.moveCenter(m_presenter->geometry().center());
184 184 zoomIn(rect);
185 185 }
186 186 }
187 187
188 188 /*!
189 189 Zooms in the view to a maximum level at which \a rect is still fully visible.
190 190 */
191 191 void QChart::zoomIn(const QRectF& rect)
192 192 {
193 193 if(!rect.isValid()) return;
194 194 QRectF r = rect.normalized();
195 195 int margin = m_presenter->margin();
196 196 r.translate(-margin, -margin);
197 197 m_dataset->addDomain(r,m_presenter->geometry());
198 198 }
199 199
200 200 /*!
201 201 Restores the view zoom level to the previous one.
202 202 */
203 203 void QChart::zoomOut()
204 204 {
205 205 m_dataset->previousDomain();
206 206 }
207 207
208 208 /*!
209 209 Resets to the default view.
210 210 */
211 211 void QChart::zoomReset()
212 212 {
213 213 m_dataset->clearDomains();
214 214 }
215 215
216 216 /*!
217 217 Returns the pointer to the x axis object of the chart
218 218 */
219 219 QChartAxis* QChart::axisX() const
220 220 {
221 221 return m_dataset->axisX();
222 222 }
223 223
224 224 /*!
225 225 Returns the pointer to the y axis object of the chart
226 226 */
227 227 QChartAxis* QChart::axisY() const
228 228 {
229 229 return m_dataset->axisY();
230 230 }
231 231
232 232 /*!
233 233 Resizes and updates the chart area using the \a event data
234 234 */
235 235 void QChart::resizeEvent(QGraphicsSceneResizeEvent *event)
236 236 {
237 237
238 238 m_rect = QRectF(QPoint(0,0),event->newSize());
239 239 QRectF rect = m_rect.adjusted(margin(),margin(), -margin(), -margin());
240 240
241 241 // recalculate title position
242 242 if (m_titleItem) {
243 243 QPointF center = m_rect.center() -m_titleItem->boundingRect().center();
244 244 m_titleItem->setPos(center.x(),m_rect.top()/2 + margin()/2);
245 245 }
246 246
247 247 //recalculate background gradient
248 248 if (m_backgroundItem) {
249 249 m_backgroundItem->setRect(rect);
250 250 }
251 251
252 252 QGraphicsWidget::resizeEvent(event);
253 253 update();
254 254 }
255 255
256 /*!
257 Sets animation options for the chart
258 */
259 void QChart::setAnimationOptions(AnimationOptions options)
260 {
261 m_presenter->setAnimationOptions(options);
262 }
263
264 /*!
265 Returns animation options for the chart
266 */
267 QChart::AnimationOptions QChart::animationOptions() const
268 {
269 return m_presenter->animationOptions();
270 }
271
256 272 #include "moc_qchart.cpp"
257 273
258 274 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,82 +1,95
1 1 #ifndef QCHART_H
2 2 #define QCHART_H
3 3
4 4 #include <qchartglobal.h>
5 5 #include <qchartseries.h>
6 6 #include <QGraphicsWidget>
7 7 #include <QLinearGradient>
8 8 #include <QFont>
9 9
10 10 class QGraphicsSceneResizeEvent;
11 11
12 12 QTCOMMERCIALCHART_BEGIN_NAMESPACE
13 13
14 14 class AxisItem;
15 15 class QChartSeries;
16 16 class PlotDomain;
17 17 class BarPresenter;
18 18 class QChartAxis;
19 19 class ChartTheme;
20 20 class ChartItem;
21 21 class ChartDataSet;
22 22 class ChartPresenter;
23 23
24 24 class QTCOMMERCIALCHART_EXPORT QChart : public QGraphicsWidget
25 25 {
26 26 Q_OBJECT
27 27 public:
28 enum ChartTheme {
28 enum ChartTheme {
29 29 ChartThemeDefault,
30 30 ChartThemeVanilla,
31 31 ChartThemeIcy,
32 32 ChartThemeGrayscale,
33 33 ChartThemeScientific
34 34 //ChartThemeUnnamed1
35 35 /*! The default theme follows the GUI style of the Operating System */
36 36 };
37 37
38 enum AnimationOption {
39 NoAnimation = 0x0,
40 GridAxisAnimations = 0x1,
41 SeriesAnimations =0x2,
42 AllAnimations = 0x3
43 };
44 Q_DECLARE_FLAGS(AnimationOptions, AnimationOption)
45
38 46 public:
39 47 QChart(QGraphicsItem *parent = 0, Qt::WindowFlags wFlags = 0);
40 48 ~QChart();
41 49
42 50 void addSeries(QChartSeries* series, QChartAxis* axisY = 0);
43 51 void removeSeries(QChartSeries* series); //returns ownership , deletes axis if no series attached
44 52 void removeAllSeries(); // deletes series and axis
45 53
46 54 void setMargin(int margin);
47 55 int margin() const;
48 56 void setChartTheme(QChart::ChartTheme theme);
49 57 QChart::ChartTheme chartTheme() const;
50 58
51 59 void setChartTitle(const QString& title);
52 60 void setChartTitleFont(const QFont& font);
53 61 void setChartBackgroundBrush(const QBrush& brush);
54 62 void setChartBackgroundPen(const QPen& pen);
55 63
64 void setAnimationOptions(AnimationOptions options);
65 AnimationOptions animationOptions() const;
66
56 67 void zoomIn();
57 68 void zoomIn(const QRectF& rect);
58 69 void zoomOut();
59 70 void zoomReset();
60 71
61 72 QChartAxis* axisX() const;
62 73 QChartAxis* axisY() const;
63 74
64 75 protected:
65 76 void resizeEvent(QGraphicsSceneResizeEvent *event);
66 77
67 78 private:
68 79 inline void createChartBackgroundItem();
69 80 inline void createChartTitleItem();
70 81
71 82 private:
72 83 Q_DISABLE_COPY(QChart)
73 84 QGraphicsRectItem* m_backgroundItem;
74 85 QGraphicsTextItem* m_titleItem;
75 86 QRectF m_rect;
76 87 ChartDataSet *m_dataset;
77 88 ChartPresenter *m_presenter;
78 89 };
79 90
80 91 QTCOMMERCIALCHART_END_NAMESPACE
81 92
93 Q_DECLARE_OPERATORS_FOR_FLAGS(QTCOMMERCIALCHART_NAMESPACE::QChart::AnimationOptions)
94
82 95 #endif
@@ -1,334 +1,350
1 1 #include "qchartview.h"
2 2 #include "qchart.h"
3 3 #include "qchartaxis.h"
4 4 #include <QGraphicsView>
5 5 #include <QGraphicsScene>
6 6 #include <QRubberBand>
7 7 #include <QResizeEvent>
8 8 #include <QDebug>
9 9
10 10 /*!
11 11 \enum QChartView::RubberBandPolicy
12 12
13 13 This enum describes the different types of rubber bands that can be used for zoom rect selection
14 14
15 15 \value NoRubberBand
16 16 \value VerticalRubberBand
17 17 \value HorizonalRubberBand
18 18 \value RectangleRubberBand
19 19 */
20 20
21 21 /*!
22 22 \class QChartView
23 23 \brief Standalone charting widget.
24 24
25 25 QChartView is a standalone widget that can display charts. It does not require separate QGraphicsScene to work. It manages the graphical
26 26 representation of different types of QChartSeries and other chart related objects like
27 27 QChartAxis and QChartLegend. If you want to display a chart in your existing QGraphicsScene, you can use the QChart class instead.
28 28
29 29 \sa QChart
30 30 */
31 31
32 32 QTCOMMERCIALCHART_BEGIN_NAMESPACE
33 33
34 34 /*!
35 35 Constructs a chartView object which is a child of a\a parent.
36 36 */
37 37 QChartView::QChartView(QWidget *parent) :
38 38 QGraphicsView(parent),
39 39 m_scene(new QGraphicsScene(this)),
40 40 m_chart(new QChart()),
41 41 m_rubberBand(0),
42 42 m_verticalRubberBand(false),
43 43 m_horizonalRubberBand(false)
44 44 {
45 45 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
46 46 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
47 47 setScene(m_scene);
48 48 m_chart->setMargin(50);
49 49 m_scene->addItem(m_chart);
50 50 setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
51 51 }
52 52
53 53
54 54 /*!
55 55 Destroys the object and it's children, like QChartSeries and QChartAxis object added to it.
56 56 */
57 57 QChartView::~QChartView()
58 58 {
59 59 }
60 60
61 61 /*!
62 62 Resizes and updates the chart area using the \a event data
63 63 */
64 64 void QChartView::resizeEvent(QResizeEvent *event)
65 65 {
66 66 m_scene->setSceneRect(0,0,size().width(),size().height());
67 67 m_chart->resize(size());
68 68 QWidget::resizeEvent(event);
69 69 }
70 70
71 71 /*!
72 72 Adds the \a series and optional \a axisY onto the chart and takes the ownership of the objects.
73 73 If auto scaling is enabled, re-scales the axes the series is bound to (both the x axis and
74 74 the y axis).
75 75 \sa removeSeries(), removeAllSeries()
76 76 */
77 77 void QChartView::addSeries(QChartSeries* series,QChartAxis *axisY)
78 78 {
79 79 m_chart->addSeries(series,axisY);
80 80 }
81 81
82 82 /*!
83 83 Removes the \a series specified in a perameter from the QChartView.
84 84 It releses its ownership of the specified QChartSeries object.
85 85 It does not delete the pointed QChartSeries data object
86 86 \sa addSeries(), removeAllSeries()
87 87 */
88 88 void QChartView::removeSeries(QChartSeries* series)
89 89 {
90 90 m_chart->removeSeries(series);
91 91 }
92 92
93 93 /*!
94 94 Removes all the QChartSeries that have been added to the QChartView
95 95 It also deletes the pointed QChartSeries data objects
96 96 \sa addSeries(), removeSeries()
97 97 */
98 98 void QChartView::removeAllSeries()
99 99 {
100 100 m_chart->removeAllSeries();
101 101 }
102 102
103 103 /*!
104 104 Zooms in the view by a factor of 2
105 105 */
106 106 void QChartView::zoomIn()
107 107 {
108 108 m_chart->zoomIn();
109 109 }
110 110
111 111 /*!
112 112 Zooms in the view to a maximum level at which \a rect is still fully visible.
113 113 */
114 114 void QChartView::zoomIn(const QRect& rect)
115 115 {
116 116 m_chart->zoomIn(rect);
117 117 }
118 118
119 119 /*!
120 120 Restores the view zoom level to the previous one.
121 121 */
122 122 void QChartView::zoomOut()
123 123 {
124 124 m_chart->zoomOut();
125 125 }
126 126
127 127 /*!
128 128 Returns the chart margin, which is the distance between the widget edge and the part of the chart where the actual data can be displayed.
129 129 */
130 130 int QChartView::margin() const
131 131 {
132 132 return m_chart->margin();
133 133 }
134 134
135 135 /*!
136 136 Sets the chart \a title. A description text that is rendered above the chart.
137 137 */
138 138 void QChartView::setChartTitle(const QString& title)
139 139 {
140 140 m_chart->setChartTitle(title);
141 141 }
142 142
143 143 /*!
144 144 Sets the \a font that is used for rendering the description text that is rendered above the chart.
145 145 */
146 146 void QChartView::setChartTitleFont(const QFont& font)
147 147 {
148 148 m_chart->setChartTitleFont(font);
149 149 }
150 150
151 151 /*!
152 152 Sets the \a brush that is used for painting the background of the chart area of the QChartView widget.
153 153 */
154 154 void QChartView::setChartBackgroundBrush(const QBrush& brush)
155 155 {
156 156 m_chart->setChartBackgroundBrush(brush);
157 157 }
158 158
159 159 /*!
160 160 Sets the \a pen that is used for painting the background of the chart area of the QChartView widget.
161 161 */
162 162 void QChartView::setChartBackgroundPen(const QPen& pen)
163 163 {
164 164 m_chart->setChartBackgroundPen(pen);
165 165 }
166 166
167 167 /*!
168 168 Sets the RubberBandPlicy to \a policy. Selected policy determines the way zooming is performed.
169 169 */
170 170 void QChartView::setRubberBandPolicy(const RubberBandPolicy policy)
171 171 {
172 172 switch(policy) {
173 173 case VerticalRubberBand:
174 174 m_verticalRubberBand = true;
175 175 m_horizonalRubberBand = false;
176 176 break;
177 177 case HorizonalRubberBand:
178 178 m_verticalRubberBand = false;
179 179 m_horizonalRubberBand = true;
180 180 break;
181 181 case RectangleRubberBand:
182 182 m_verticalRubberBand = true;
183 183 m_horizonalRubberBand = true;
184 184 break;
185 185 case NoRubberBand:
186 186 default:
187 187 delete m_rubberBand;
188 188 m_rubberBand=0;
189 189 m_horizonalRubberBand = false;
190 190 m_verticalRubberBand = false;
191 191 return;
192 192 }
193 193 if(!m_rubberBand) {
194 194 m_rubberBand = new QRubberBand(QRubberBand::Rectangle, this);
195 195 m_rubberBand->setEnabled(true);
196 196 }
197 197 }
198 198
199 199 /*!
200 200 Returns the RubberBandPolicy that is currently being used by the widget.
201 201 */
202 202 QChartView::RubberBandPolicy QChartView::rubberBandPolicy() const
203 203 {
204 204 if(m_horizonalRubberBand && m_verticalRubberBand) return RectangleRubberBand;
205 205 if(m_horizonalRubberBand) return HorizonalRubberBand;
206 206 if(m_verticalRubberBand) return VerticalRubberBand;
207 207 return NoRubberBand;
208 208 }
209 209
210 210 /*!
211 211 If Left mouse button is pressed and the RubberBandPolicy is enabled the \a event is accepted and the rubber band is displayed on the screen allowing the user to select the zoom area.
212 212 If different mouse button is pressed and/or the RubberBandPolicy is disabled then the \a event is passed to QGraphicsView::mousePressEvent() implementation is called.
213 213 */
214 214 void QChartView::mousePressEvent(QMouseEvent *event)
215 215 {
216 216 if(m_rubberBand && m_rubberBand->isEnabled() && event->button() == Qt::LeftButton) {
217 217
218 218 int margin = m_chart->margin();
219 219 QRect rect(margin, margin, width() - 2 * margin, height() - 2 * margin);
220 220
221 221 if (rect.contains(event->pos())) {
222 222 m_rubberBandOrigin = event->pos();
223 223 m_rubberBand->setGeometry(QRect(m_rubberBandOrigin, QSize()));
224 224 m_rubberBand->show();
225 225 event->accept();
226 226 }
227 227 }
228 228 else {
229 229 QGraphicsView::mousePressEvent(event);
230 230 }
231 231 }
232 232
233 233 /*!
234 234 If RubberBand rectange specification has been initiated in pressEvent then \a event data is used to update RubberBand geometry.
235 235 In other case the defualt QGraphicsView::mouseMoveEvent implementation is called.
236 236 */
237 237 void QChartView::mouseMoveEvent(QMouseEvent *event)
238 238 {
239 239 if(m_rubberBand && m_rubberBand->isVisible()) {
240 240 int margin = m_chart->margin();
241 241 QRect rect(margin, margin, width() - 2 * margin, height() - 2 * margin);
242 242 int width = event->pos().x() - m_rubberBandOrigin.x();
243 243 int height = event->pos().y() - m_rubberBandOrigin.y();
244 244 if(!m_verticalRubberBand) {
245 245 m_rubberBandOrigin.setY(rect.top());
246 246 height = rect.height();
247 247 }
248 248 if(!m_horizonalRubberBand) {
249 249 m_rubberBandOrigin.setX(rect.left());
250 250 width= rect.width();
251 251 }
252 252 m_rubberBand->setGeometry(QRect(m_rubberBandOrigin.x(),m_rubberBandOrigin.y(), width,height).normalized());
253 253 }
254 254 else {
255 255 QGraphicsView::mouseMoveEvent(event);
256 256 }
257 257 }
258 258
259 259 /*!
260 260 If left mouse button is release and RubberBand is enabled then \a event is accepted and the view is zoomed in to rect specified by RubberBand
261 261 If it is the right mouse button \a event then RubberBand is dissmissed and zoom is canceled.
262 262 */
263 263 void QChartView::mouseReleaseEvent(QMouseEvent *event)
264 264 {
265 265 if(m_rubberBand) {
266 266 if (event->button() == Qt::LeftButton && m_rubberBand->isVisible()) {
267 267 m_rubberBand->hide();
268 268 QRect rect = m_rubberBand->geometry();
269 269 m_chart->zoomIn(rect);
270 270 event->accept();
271 271 }
272 272
273 273 if(event->button()==Qt::RightButton)
274 274 m_chart->zoomReset();
275 275 }
276 276 else {
277 277 QGraphicsView::mouseReleaseEvent(event);
278 278 }
279 279 }
280 280
281 281 /*!
282 282 Pressing + and - keys performs zoomIn() and zoomOut() respectivly.
283 283 In other \a event is passed to the QGraphicsView::keyPressEvent() implementation
284 284 */
285 285 void QChartView::keyPressEvent(QKeyEvent *event)
286 286 {
287 287 switch (event->key()) {
288 288 case Qt::Key_Plus:
289 289 zoomIn();
290 290 break;
291 291 case Qt::Key_Minus:
292 292 zoomOut();
293 293 break;
294 294 default:
295 295 QGraphicsView::keyPressEvent(event);
296 296 break;
297 297 }
298 298 }
299 299
300 300 /*!
301 301 Sets the \a theme used by the chart for rendering the graphical representation of the data
302 302 \sa QChart::ChartTheme, chartTheme()
303 303 */
304 304 void QChartView::setChartTheme(QChart::ChartTheme theme)
305 305 {
306 306 m_chart->setChartTheme(theme);
307 307 }
308 308
309 309 /*!
310 310 Returns the theme enum used by the chart.
311 311 \sa setChartTheme()
312 312 */
313 313 QChart::ChartTheme QChartView::chartTheme() const
314 314 {
315 315 return m_chart->chartTheme();
316 316 }
317 317
318 318 /*!
319 319 Returns the pointer to the x axis object of the chart
320 320 */
321 321 QChartAxis* QChartView::axisX() const
322 322 {
323 323 return m_chart->axisX();
324 324 }
325 325
326 326 /*!
327 327 Returns the pointer to the y axis object of the chart
328 328 */
329 329 QChartAxis* QChartView::axisY() const
330 330 {
331 331 return m_chart->axisY();
332 332 }
333 333
334 /*!
335 Sets animation options for the chart
336 */
337 void QChartView::setAnimationOptions(QChart::AnimationOptions options)
338 {
339 m_chart->setAnimationOptions(options);
340 }
341
342 /*!
343 Returns animation options for the chart
344 */
345 QChart::AnimationOptions QChartView::animationOptions() const
346 {
347 return m_chart->animationOptions();
348 }
349
334 350 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,71 +1,74
1 1 #ifndef QCHARTWIDGET_H
2 2 #define QCHARTWIDGET_H
3 3
4 4 #include "qchartglobal.h"
5 5 #include "qchartseries.h"
6 6 #include "qchart.h"
7 7 #include <QGraphicsView>
8 8
9 9 class QGraphicsScene;
10 10 class QRubberBand;
11 11
12 12 QTCOMMERCIALCHART_BEGIN_NAMESPACE
13 13
14 14 class QChart;
15 15
16 16 class QTCOMMERCIALCHART_EXPORT QChartView : public QGraphicsView
17 17 {
18 18 public:
19 19 enum RubberBandPolicy { NoRubberBand, VerticalRubberBand, HorizonalRubberBand, RectangleRubberBand };
20 20
21 21 explicit QChartView(QWidget *parent = 0);
22 22 ~QChartView();
23 23
24 24 //implement from QWidget
25 25 void resizeEvent(QResizeEvent *event);
26 26
27 27 void addSeries(QChartSeries* series,QChartAxis* axisY=0);// takes series ownership , takes axis ownership
28 28 void removeSeries(QChartSeries* series); //returns ownership , deletes axis if no series attached
29 29 void removeAllSeries(); // deletes series and axis
30 30 int margin() const;
31 31
32 32 void setChartTitle(const QString& title);
33 33 void setChartTitleFont(const QFont& font);
34 34 void setChartBackgroundBrush(const QBrush& brush);
35 35 void setChartBackgroundPen(const QPen& pen);
36 36
37 37 void zoomIn();
38 38 void zoomIn(const QRect& rect);
39 39 void zoomOut();
40 40
41 41 void setRubberBandPolicy(const RubberBandPolicy );
42 42 RubberBandPolicy rubberBandPolicy() const;
43 43
44 44 void setChartTheme(QChart::ChartTheme theme);
45 45 QChart::ChartTheme chartTheme() const;
46 46
47 void setAnimationOptions(QChart::AnimationOptions options);
48 QChart::AnimationOptions animationOptions() const;
49
47 50 QChartAxis* axisX() const;
48 51 QChartAxis* axisY() const;
49 52
50 53 protected:
51 54 void mousePressEvent(QMouseEvent *event);
52 55 void mouseMoveEvent(QMouseEvent *event);
53 56 void mouseReleaseEvent(QMouseEvent *event);
54 57 void keyPressEvent(QKeyEvent *event);
55 58
56 59
57 60 private:
58 61 QGraphicsScene *m_scene;
59 62 QChart* m_chart;
60 63 QPoint m_rubberBandOrigin;
61 64 QRubberBand* m_rubberBand;
62 65 bool m_verticalRubberBand;
63 66 bool m_horizonalRubberBand;
64 67 Q_DISABLE_COPY(QChartView)
65 68
66 69
67 70 };
68 71
69 72 QTCOMMERCIALCHART_END_NAMESPACE
70 73
71 74 #endif // QCHARTWIDGET_H
General Comments 0
You need to be logged in to leave comments. Login now