##// END OF EJS Templates
Refcator scrol() to scrollLeft,Right,Up,Down
Michal Klocek -
r600:db004721a4d5
parent child
Show More
@@ -1,355 +1,360
1 1 #include "qchart.h"
2 2 #include "qchartaxis.h"
3 3 #include "qlegend.h"
4 4 #include "chartpresenter_p.h"
5 5 #include "chartdataset_p.h"
6 6 #include <QGraphicsScene>
7 7 #include <QGraphicsSceneResizeEvent>
8 8 #include <QDebug>
9 9
10 10 QTCOMMERCIALCHART_BEGIN_NAMESPACE
11 11
12 12 /*!
13 13 \enum QChart::ChartTheme
14 14
15 15 This enum describes the theme used by the chart.
16 16
17 17 \value ChartThemeDefault Follows the GUI style of the Operating System
18 18 \value ChartThemeVanilla
19 19 \value ChartThemeIcy
20 20 \value ChartThemeGrayscale
21 21 \value ChartThemeScientific
22 22 \value ChartThemeBlueCerulean
23 23 \value ChartThemeLight
24 24 \value ChartThemeCount Not really a theme; the total count of themes.
25 25 */
26 26
27 27 /*!
28 28 \enum QChart::AnimationOption
29 29
30 30 For enabling/disabling animations. Defaults to NoAnimation.
31 31
32 32 \value NoAnimation
33 33 \value GridAxisAnimations
34 34 \value SeriesAnimations
35 35 \value AllAnimations
36 36 */
37 37
38 38 /*!
39 39 \class QChart
40 40 \brief QtCommercial chart API.
41 41
42 42 QChart is a QGraphicsWidget that you can show in a QGraphicsScene. It manages the graphical
43 43 representation of different types of QChartSeries and other chart related objects like
44 44 QChartAxis and QChartLegend. If you simply want to show a chart in a layout, you can use the
45 45 convenience class QChartView instead of QChart.
46 46 \sa QChartView
47 47 */
48 48
49 49 /*!
50 50 Constructs a chart object which is a child of a\a parent. Parameter \a wFlags is passed to the QGraphicsWidget constructor.
51 51 */
52 52 QChart::QChart(QGraphicsItem *parent, Qt::WindowFlags wFlags) : QGraphicsWidget(parent,wFlags),
53 53 m_backgroundItem(0),
54 54 m_titleItem(0),
55 55 m_legend(new QLegend(this)),
56 56 m_dataset(new ChartDataSet(this)),
57 57 m_presenter(new ChartPresenter(this,m_dataset))
58 58 {
59 59 connect(m_dataset,SIGNAL(seriesAdded(QSeries*,Domain*)),m_legend,SLOT(handleSeriesAdded(QSeries*,Domain*)));
60 60 connect(m_dataset,SIGNAL(seriesRemoved(QSeries*)),m_legend,SLOT(handleSeriesRemoved(QSeries*)));
61 61 }
62 62
63 63 /*!
64 64 Destroys the object and it's children, like QChartSeries and QChartAxis object added to it.
65 65 */
66 66 QChart::~QChart()
67 67 {
68 68 disconnect(m_dataset,SIGNAL(seriesAdded(QSeries*,Domain*)),m_legend,SLOT(handleSeriesAdded(QSeries*,Domain*)));
69 69 disconnect(m_dataset,SIGNAL(seriesRemoved(QSeries*)),m_legend,SLOT(handleSeriesRemoved(QSeries*)));
70 70 }
71 71
72 72 /*!
73 73 Adds the \a series and optional \a axisY onto the chart and takes the ownership of the objects.
74 74 If auto scaling is enabled, re-scales the axes the series is bound to (both the x axis and
75 75 the y axis).
76 76 */
77 77 void QChart::addSeries(QSeries* series, QChartAxis* axisY)
78 78 {
79 79 m_dataset->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 QChart::removeSeries(QSeries* series)
89 89 {
90 90 m_dataset->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 QChart::removeAllSeries()
99 99 {
100 100 m_dataset->removeAllSeries();
101 101 }
102 102
103 103 /*!
104 104 Sets the \a brush that is used for painting the background of the chart area.
105 105 */
106 106 void QChart::setChartBackgroundBrush(const QBrush& brush)
107 107 {
108 108 createChartBackgroundItem();
109 109 m_backgroundItem->setBrush(brush);
110 110 m_backgroundItem->update();
111 111 }
112 112
113 113 /*!
114 114 Sets the \a pen that is used for painting the background of the chart area.
115 115 */
116 116 void QChart::setChartBackgroundPen(const QPen& pen)
117 117 {
118 118 createChartBackgroundItem();
119 119 m_backgroundItem->setPen(pen);
120 120 m_backgroundItem->update();
121 121 }
122 122
123 123 /*!
124 124 Sets the chart \a title. The description text that is drawn above the chart.
125 125 */
126 126 void QChart::setChartTitle(const QString& title)
127 127 {
128 128 createChartTitleItem();
129 129 m_titleItem->setText(title);
130 130 updateLayout();
131 131 }
132 132
133 133 /*!
134 134 Returns the chart title. The description text that is drawn above the chart.
135 135 */
136 136 QString QChart::chartTitle() const
137 137 {
138 138 if(m_titleItem)
139 139 return m_titleItem->text();
140 140 else
141 141 return QString();
142 142 }
143 143
144 144 /*!
145 145 Sets the \a font that is used for rendering the description text that is rendered above the chart.
146 146 */
147 147 void QChart::setChartTitleFont(const QFont& font)
148 148 {
149 149 createChartTitleItem();
150 150 m_titleItem->setFont(font);
151 151 updateLayout();
152 152 }
153 153
154 154 /*!
155 155 Sets the \a brush used for rendering the title text.
156 156 */
157 157 void QChart::setChartTitleBrush(const QBrush &brush)
158 158 {
159 159 createChartTitleItem();
160 160 m_titleItem->setBrush(brush);
161 161 updateLayout();
162 162 }
163 163
164 164 /*!
165 165 Returns the brush used for rendering the title text.
166 166 */
167 167 QBrush QChart::chartTitleBrush()
168 168 {
169 169 createChartTitleItem();
170 170 return m_titleItem->brush();
171 171 }
172 172
173 173 void QChart::createChartBackgroundItem()
174 174 {
175 175 if(!m_backgroundItem) {
176 176 m_backgroundItem = new QGraphicsRectItem(this);
177 177 m_backgroundItem->setPen(Qt::NoPen);
178 178 m_backgroundItem->setZValue(ChartPresenter::BackgroundZValue);
179 179 }
180 180 }
181 181
182 182 void QChart::createChartTitleItem()
183 183 {
184 184 if(!m_titleItem) {
185 185 m_titleItem = new QGraphicsSimpleTextItem(this);
186 186 m_titleItem->setZValue(ChartPresenter::BackgroundZValue);
187 187 }
188 188 }
189 189
190 190 /*!
191 191 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.
192 192 \sa setMargin()
193 193 */
194 194 int QChart::margin() const
195 195 {
196 196 return m_presenter->margin();
197 197 }
198 198
199 199 /*!
200 200 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.
201 201 \sa margin()
202 202 */
203 203 void QChart::setMargin(int margin)
204 204 {
205 205 m_presenter->setMargin(margin);
206 206 updateLayout();
207 207 }
208 208
209 209 /*!
210 210 Sets the \a theme used by the chart for rendering the graphical representation of the data
211 211 \sa ChartTheme, chartTheme()
212 212 */
213 213 void QChart::setChartTheme(QChart::ChartTheme theme)
214 214 {
215 215 m_presenter->setChartTheme(theme);
216 216 }
217 217
218 218 /*!
219 219 Returns the theme enum used by the chart.
220 220 \sa ChartTheme, setChartTheme()
221 221 */
222 222 QChart::ChartTheme QChart::chartTheme() const
223 223 {
224 224 return m_presenter->chartTheme();
225 225 }
226 226
227 227 /*!
228 228 Zooms in the view by a factor of 2
229 229 */
230 230 void QChart::zoomIn()
231 231 {
232 232 m_presenter->zoomIn();
233 233 }
234 234
235 235 /*!
236 236 Zooms in the view to a maximum level at which \a rect is still fully visible.
237 237 */
238 238 void QChart::zoomIn(const QRectF& rect)
239 239 {
240 240
241 241 if(!rect.isValid()) return;
242 242 m_presenter->zoomIn(rect);
243 243 }
244 244
245 245 /*!
246 246 Restores the view zoom level to the previous one.
247 247 */
248 248 void QChart::zoomOut()
249 249 {
250 250 m_presenter->zoomOut();
251 251 }
252 252
253 253 /*!
254 254 Resets to the default view.
255 255 */
256 256 void QChart::zoomReset()
257 257 {
258 258 m_presenter->zoomReset();
259 259 }
260 260
261 261 /*!
262 262 Returns the pointer to the x axis object of the chart
263 263 */
264 264 QChartAxis* QChart::axisX() const
265 265 {
266 266 return m_dataset->axisX();
267 267 }
268 268
269 269 /*!
270 270 Returns the pointer to the y axis object of the chart
271 271 */
272 272 QChartAxis* QChart::axisY() const
273 273 {
274 274 return m_dataset->axisY();
275 275 }
276 276
277 277 /*!
278 278 Returns the legend object of the chart
279 279 */
280 280 QLegend* QChart::legend()
281 281 {
282 282 return m_legend;
283 283 }
284 284
285 285 /*!
286 286 Resizes and updates the chart area using the \a event data
287 287 */
288 288 void QChart::resizeEvent(QGraphicsSceneResizeEvent *event)
289 289 {
290 290
291 291 m_rect = QRectF(QPoint(0,0),event->newSize());
292 292 updateLayout();
293 293 QGraphicsWidget::resizeEvent(event);
294 294 update();
295 295 }
296 296
297 297 /*!
298 298 Sets animation \a options for the chart
299 299 */
300 300 void QChart::setAnimationOptions(AnimationOptions options)
301 301 {
302 302 m_presenter->setAnimationOptions(options);
303 303 }
304 304
305 305 /*!
306 306 Returns animation options for the chart
307 307 */
308 308 QChart::AnimationOptions QChart::animationOptions() const
309 309 {
310 310 return m_presenter->animationOptions();
311 311 }
312 312
313 void QChart::scroll(int dx,int dy)
314 {
315 //temporary
316 if(dx>0)
317 m_presenter->scroll(m_presenter->geometry().width()/(axisX()->ticksCount()-1),0);
318 if(dx<0)
319 m_presenter->scroll(-m_presenter->geometry().width()/(axisX()->ticksCount()-1),0);
320 if(dy>0)
321 m_presenter->scroll(0,m_presenter->geometry().width()/(axisY()->ticksCount()-1));
322 if(dy<0)
313 void QChart::scrollLeft()
314 {
315 m_presenter->scroll(-m_presenter->geometry().width()/(axisX()->ticksCount()-1),0);
316 }
317
318 void QChart::scrollRight()
319 {
320 m_presenter->scroll(m_presenter->geometry().width()/(axisX()->ticksCount()-1),0);
321 }
322 void QChart::scrollUp()
323 {
324 m_presenter->scroll(0,m_presenter->geometry().width()/(axisY()->ticksCount()-1));
325 }
326 void QChart::scrollDown()
327 {
323 328 m_presenter->scroll(0,-m_presenter->geometry().width()/(axisY()->ticksCount()-1));
324 329 }
325 330
326 331 void QChart::updateLayout()
327 332 {
328 333 if(!m_rect.isValid()) return;
329 334
330 335 QRectF rect = m_rect.adjusted(margin(),margin(), -margin(), -margin());
331 336
332 337 // recalculate title position
333 338 if (m_titleItem) {
334 339 QPointF center = m_rect.center() -m_titleItem->boundingRect().center();
335 340 m_titleItem->setPos(center.x(),m_rect.top()/2 + margin()/2);
336 341 }
337 342
338 343 //recalculate background gradient
339 344 if (m_backgroundItem) {
340 345 m_backgroundItem->setRect(rect);
341 346 }
342 347
343 348 // recalculate legend position
344 349 // TODO: better layout
345 350 if (m_legend) {
346 351 QRectF boundingRect(m_rect.adjusted(margin(),
347 352 rect.height() + margin() + margin()/2,
348 353 -margin(),
349 354 -margin()/2 + m_legend->minimumSize().height()));
350 355 m_legend->handleGeometryChanged(boundingRect);
351 356 }
352 357 }
353 358 #include "moc_qchart.cpp"
354 359
355 360 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,105 +1,108
1 1 #ifndef QCHART_H
2 2 #define QCHART_H
3 3
4 4 #include <qchartglobal.h>
5 5 #include <qseries.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 QSeries;
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 class QLegend;
24 24
25 25 class QTCOMMERCIALCHART_EXPORT QChart : public QGraphicsWidget
26 26 {
27 27 Q_OBJECT
28 28 public:
29 29 enum ChartTheme {
30 30 ChartThemeDefault,
31 31 ChartThemeVanilla,
32 32 ChartThemeIcy,
33 33 ChartThemeGrayscale,
34 34 ChartThemeScientific,
35 35 ChartThemeBlueCerulean,
36 36 ChartThemeLight,
37 37 ChartThemeCount
38 38 };
39 39
40 40 enum AnimationOption {
41 41 NoAnimation = 0x0,
42 42 GridAxisAnimations = 0x1,
43 43 SeriesAnimations =0x2,
44 44 AllAnimations = 0x3
45 45 };
46 46 Q_DECLARE_FLAGS(AnimationOptions, AnimationOption)
47 47
48 48 public:
49 49 QChart(QGraphicsItem *parent = 0, Qt::WindowFlags wFlags = 0);
50 50 ~QChart();
51 51
52 52 void addSeries(QSeries* series, QChartAxis* axisY = 0);
53 53 void removeSeries(QSeries* series); //returns ownership , deletes axis if no series attached
54 54 void removeAllSeries(); // deletes series and axis
55 55
56 56 void setMargin(int margin);
57 57 int margin() const;
58 58 void setChartTheme(QChart::ChartTheme theme);
59 59 QChart::ChartTheme chartTheme() const;
60 60
61 61 void setChartTitle(const QString& title);
62 62 QString chartTitle() const;
63 63 void setChartTitleFont(const QFont& font);
64 64 void setChartTitleBrush(const QBrush &brush);
65 65 QBrush chartTitleBrush();
66 66 void setChartBackgroundBrush(const QBrush& brush);
67 67 void setChartBackgroundPen(const QPen& pen);
68 68
69 69 void setAnimationOptions(AnimationOptions options);
70 70 AnimationOptions animationOptions() const;
71 71
72 72 void zoomIn();
73 73 void zoomIn(const QRectF& rect);
74 74 void zoomOut();
75 75 void zoomReset();
76 void scroll(int dx,int dy);
76 void scrollLeft();
77 void scrollRight();
78 void scrollUp();
79 void scrollDown();
77 80
78 81 QChartAxis* axisX() const;
79 82 QChartAxis* axisY() const;
80 83
81 84 QLegend* legend();
82 85
83 86 protected:
84 87 void resizeEvent(QGraphicsSceneResizeEvent *event);
85 88
86 89 private:
87 90 inline void createChartBackgroundItem();
88 91 inline void createChartTitleItem();
89 92 void updateLayout();
90 93
91 94 private:
92 95 Q_DISABLE_COPY(QChart)
93 96 QGraphicsRectItem* m_backgroundItem;
94 97 QGraphicsSimpleTextItem* m_titleItem;
95 98 QRectF m_rect;
96 99 QLegend* m_legend;
97 100 ChartDataSet *m_dataset;
98 101 ChartPresenter *m_presenter;
99 102 };
100 103
101 104 QTCOMMERCIALCHART_END_NAMESPACE
102 105
103 106 Q_DECLARE_OPERATORS_FOR_FLAGS(QTCOMMERCIALCHART_NAMESPACE::QChart::AnimationOptions)
104 107
105 108 #endif
@@ -1,390 +1,406
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
26 26 QGraphicsScene to work. It manages the graphical representation of different types of
27 27 QChartSeries and other chart related objects like QChartAxis and QChartLegend. If you want to
28 28 display a chart in your existing QGraphicsScene, you can use the QChart class instead.
29 29
30 30 \sa QChart
31 31 */
32 32
33 33 QTCOMMERCIALCHART_BEGIN_NAMESPACE
34 34
35 35 /*!
36 36 Constructs a chartView object which is a child of a\a parent.
37 37 */
38 38 QChartView::QChartView(QWidget *parent) :
39 39 QGraphicsView(parent),
40 40 m_scene(new QGraphicsScene(this)),
41 41 m_chart(new QChart()),
42 42 m_rubberBand(0),
43 43 m_verticalRubberBand(false),
44 44 m_horizonalRubberBand(false)
45 45 {
46 46 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
47 47 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
48 48 setScene(m_scene);
49 49 m_chart->setMargin(50);
50 50 m_scene->addItem(m_chart);
51 51 setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
52 52 }
53 53
54 54
55 55 /*!
56 56 Destroys the object and it's children, like QChartSeries and QChartAxis object added to it.
57 57 */
58 58 QChartView::~QChartView()
59 59 {
60 60 }
61 61
62 62 /*!
63 63 Resizes and updates the chart area using the \a event data
64 64 */
65 65 void QChartView::resizeEvent(QResizeEvent *event)
66 66 {
67 67 m_scene->setSceneRect(0,0,size().width(),size().height());
68 68 m_chart->resize(size());
69 69 QWidget::resizeEvent(event);
70 70 }
71 71
72 72 /*!
73 73 Adds the \a series and optional \a axisY onto the chart and takes the ownership of the objects.
74 74 If auto scaling is enabled, re-scales the axes the series is bound to (both the x axis and
75 75 the y axis).
76 76 \sa removeSeries(), removeAllSeries()
77 77 */
78 78 void QChartView::addSeries(QSeries* series,QChartAxis *axisY)
79 79 {
80 80 m_chart->addSeries(series,axisY);
81 81 }
82 82
83 83 /*!
84 84 Removes the \a series specified in a perameter from the QChartView.
85 85 It releses its ownership of the specified QChartSeries object.
86 86 It does not delete the pointed QChartSeries data object
87 87 \sa addSeries(), removeAllSeries()
88 88 */
89 89 void QChartView::removeSeries(QSeries* series)
90 90 {
91 91 m_chart->removeSeries(series);
92 92 }
93 93
94 94 /*!
95 95 Removes all the QChartSeries that have been added to the QChartView
96 96 It also deletes the pointed QChartSeries data objects
97 97 \sa addSeries(), removeSeries()
98 98 */
99 99 void QChartView::removeAllSeries()
100 100 {
101 101 m_chart->removeAllSeries();
102 102 }
103 103
104 104 /*!
105 105 Zooms in the view by a factor of 2
106 106 */
107 107 void QChartView::zoomIn()
108 108 {
109 109 m_chart->zoomIn();
110 110 }
111 111
112 112 /*!
113 113 Zooms in the view to a maximum level at which \a rect is still fully visible.
114 114 */
115 115 void QChartView::zoomIn(const QRect& rect)
116 116 {
117 117 m_chart->zoomIn(rect);
118 118 }
119 119
120 120 /*!
121 121 Restores the view zoom level to the previous one.
122 122 */
123 123 void QChartView::zoomOut()
124 124 {
125 125 m_chart->zoomOut();
126 126 }
127 127
128 128 /*!
129 129 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.
130 130 */
131 131 int QChartView::margin() const
132 132 {
133 133 return m_chart->margin();
134 134 }
135 135
136 136 /*!
137 137 Sets the chart \a title. A description text that is drawn above the chart.
138 138 */
139 139 void QChartView::setChartTitle(const QString& title)
140 140 {
141 141 m_chart->setChartTitle(title);
142 142 }
143 143
144 144 /*!
145 145 Returns the chart's title. A description text that is drawn above the chart.
146 146 */
147 147 QString QChartView::chartTitle() const
148 148 {
149 149 return m_chart->chartTitle();
150 150 }
151 151
152 152 /*!
153 153 Sets the \a font that is used for rendering the description text that is rendered above the chart.
154 154 */
155 155 void QChartView::setChartTitleFont(const QFont& font)
156 156 {
157 157 m_chart->setChartTitleFont(font);
158 158 }
159 159
160 160 /*!
161 161 Sets the \a brush used for rendering the title text.
162 162 */
163 163 void QChartView::setChartTitleBrush(const QBrush &brush)
164 164 {
165 165 m_chart->setChartTitleBrush(brush);
166 166 }
167 167
168 168 /*!
169 169 Returns the brush used for rendering the title text.
170 170 */
171 171 QBrush QChartView::chartTitleBrush()
172 172 {
173 173 return m_chart->chartTitleBrush();
174 174 }
175 175
176 176 /*!
177 177 Sets the \a brush that is used for painting the background of the chart area of the QChartView widget.
178 178 */
179 179 void QChartView::setChartBackgroundBrush(const QBrush& brush)
180 180 {
181 181 m_chart->setChartBackgroundBrush(brush);
182 182 }
183 183
184 184 /*!
185 185 Sets the \a pen that is used for painting the background of the chart area of the QChartView widget.
186 186 */
187 187 void QChartView::setChartBackgroundPen(const QPen& pen)
188 188 {
189 189 m_chart->setChartBackgroundPen(pen);
190 190 }
191 191
192 192 /*!
193 193 Sets the RubberBandPlicy to \a policy. Selected policy determines the way zooming is performed.
194 194 */
195 195 void QChartView::setRubberBandPolicy(const RubberBandPolicy policy)
196 196 {
197 197 switch(policy) {
198 198 case VerticalRubberBand:
199 199 m_verticalRubberBand = true;
200 200 m_horizonalRubberBand = false;
201 201 break;
202 202 case HorizonalRubberBand:
203 203 m_verticalRubberBand = false;
204 204 m_horizonalRubberBand = true;
205 205 break;
206 206 case RectangleRubberBand:
207 207 m_verticalRubberBand = true;
208 208 m_horizonalRubberBand = true;
209 209 break;
210 210 case NoRubberBand:
211 211 default:
212 212 delete m_rubberBand;
213 213 m_rubberBand=0;
214 214 m_horizonalRubberBand = false;
215 215 m_verticalRubberBand = false;
216 216 return;
217 217 }
218 218 if(!m_rubberBand) {
219 219 m_rubberBand = new QRubberBand(QRubberBand::Rectangle, this);
220 220 m_rubberBand->setEnabled(true);
221 221 }
222 222 }
223 223
224 224 /*!
225 225 Returns the RubberBandPolicy that is currently being used by the widget.
226 226 */
227 227 QChartView::RubberBandPolicy QChartView::rubberBandPolicy() const
228 228 {
229 229 if(m_horizonalRubberBand && m_verticalRubberBand) return RectangleRubberBand;
230 230 if(m_horizonalRubberBand) return HorizonalRubberBand;
231 231 if(m_verticalRubberBand) return VerticalRubberBand;
232 232 return NoRubberBand;
233 233 }
234 234
235 235 /*!
236 236 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.
237 237 If different mouse button is pressed and/or the RubberBandPolicy is disabled then the \a event is passed to QGraphicsView::mousePressEvent() implementation.
238 238 */
239 239 void QChartView::mousePressEvent(QMouseEvent *event)
240 240 {
241 241 if(m_rubberBand && m_rubberBand->isEnabled() && event->button() == Qt::LeftButton) {
242 242
243 243 int margin = m_chart->margin();
244 244 QRect rect(margin, margin, width() - 2 * margin, height() - 2 * margin);
245 245
246 246 if (rect.contains(event->pos())) {
247 247 m_rubberBandOrigin = event->pos();
248 248 m_rubberBand->setGeometry(QRect(m_rubberBandOrigin, QSize()));
249 249 m_rubberBand->show();
250 250 event->accept();
251 251 }
252 252 }
253 253 else {
254 254 QGraphicsView::mousePressEvent(event);
255 255 }
256 256 }
257 257
258 258 /*!
259 259 If RubberBand rectange specification has been initiated in pressEvent then \a event data is used to update RubberBand geometry.
260 260 In other case the defualt QGraphicsView::mouseMoveEvent implementation is called.
261 261 */
262 262 void QChartView::mouseMoveEvent(QMouseEvent *event)
263 263 {
264 264 if(m_rubberBand && m_rubberBand->isVisible()) {
265 265 int margin = m_chart->margin();
266 266 QRect rect(margin, margin, width() - 2 * margin, height() - 2 * margin);
267 267 int width = event->pos().x() - m_rubberBandOrigin.x();
268 268 int height = event->pos().y() - m_rubberBandOrigin.y();
269 269 if(!m_verticalRubberBand) {
270 270 m_rubberBandOrigin.setY(rect.top());
271 271 height = rect.height();
272 272 }
273 273 if(!m_horizonalRubberBand) {
274 274 m_rubberBandOrigin.setX(rect.left());
275 275 width= rect.width();
276 276 }
277 277 m_rubberBand->setGeometry(QRect(m_rubberBandOrigin.x(),m_rubberBandOrigin.y(), width,height).normalized());
278 278 }
279 279 else {
280 280 QGraphicsView::mouseMoveEvent(event);
281 281 }
282 282 }
283 283
284 284 /*!
285 285 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
286 286 If it is the right mouse button \a event then RubberBand is dissmissed and zoom is canceled.
287 287 */
288 288 void QChartView::mouseReleaseEvent(QMouseEvent *event)
289 289 {
290 290 if(m_rubberBand) {
291 291 if (event->button() == Qt::LeftButton && m_rubberBand->isVisible()) {
292 292 m_rubberBand->hide();
293 293 QRect rect = m_rubberBand->geometry();
294 294 m_chart->zoomIn(rect);
295 295 event->accept();
296 296 }
297 297
298 298 if(event->button()==Qt::RightButton)
299 299 m_chart->zoomReset();
300 300 }
301 301 else {
302 302 QGraphicsView::mouseReleaseEvent(event);
303 303 }
304 304 }
305 305
306 306 /*!
307 307 Pressing + and - keys performs zoomIn() and zoomOut() respectivly.
308 308 In other \a event is passed to the QGraphicsView::keyPressEvent() implementation
309 309 */
310 310 void QChartView::keyPressEvent(QKeyEvent *event)
311 311 {
312 312 switch (event->key()) {
313 313 case Qt::Key_Plus:
314 314 zoomIn();
315 315 break;
316 316 case Qt::Key_Minus:
317 317 zoomOut();
318 318 break;
319 319 default:
320 320 QGraphicsView::keyPressEvent(event);
321 321 break;
322 322 }
323 323 }
324 324
325 325 /*!
326 326 Sets the \a theme used by the chart for rendering the graphical representation of the data
327 327 \sa QChart::ChartTheme, chartTheme()
328 328 */
329 329 void QChartView::setChartTheme(QChart::ChartTheme theme)
330 330 {
331 331 m_chart->setChartTheme(theme);
332 332 }
333 333
334 334 /*!
335 335 Returns the theme enum used by the chart.
336 336 \sa setChartTheme()
337 337 */
338 338 QChart::ChartTheme QChartView::chartTheme() const
339 339 {
340 340 return m_chart->chartTheme();
341 341 }
342 342
343 343 /*!
344 344 Returns the pointer to the x axis object of the chart
345 345 */
346 346 QChartAxis* QChartView::axisX() const
347 347 {
348 348 return m_chart->axisX();
349 349 }
350 350
351 351 /*!
352 352 Returns the pointer to the y axis object of the chart
353 353 */
354 354 QChartAxis* QChartView::axisY() const
355 355 {
356 356 return m_chart->axisY();
357 357 }
358 358
359 359 /*!
360 360 Returns the pointer to legend object of the chart
361 361 */
362 362 QLegend* QChartView::legend() const
363 363 {
364 364 return m_chart->legend();
365 365 }
366 366
367 367 /*!
368 368 Sets animation \a options for the chart
369 369 */
370 370 void QChartView::setAnimationOptions(QChart::AnimationOptions options)
371 371 {
372 372 m_chart->setAnimationOptions(options);
373 373 }
374 374
375 375 /*!
376 376 Returns animation options for the chart
377 377 */
378 378 QChart::AnimationOptions QChartView::animationOptions() const
379 379 {
380 380 return m_chart->animationOptions();
381 381 }
382 382
383 void QChartView::scroll(int dx,int dy)
383 void QChartView::scrollLeft()
384 384 {
385 m_chart->scroll(dx,dy);
385 m_chart->scrollLeft();
386 386 }
387 387
388 void QChartView::scrollRight()
389 {
390 m_chart->scrollRight();
391 }
392
393 void QChartView::scrollUp()
394 {
395 m_chart->scrollUp();
396 }
397
398 void QChartView::scrollDown()
399 {
400 m_chart->scrollDown();
401 }
402
403
388 404 #include "moc_qchartview.cpp"
389 405
390 406 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,81 +1,83
1 1 #ifndef QCHARTWIDGET_H
2 2 #define QCHARTWIDGET_H
3 3
4 4 #include "qchartglobal.h"
5 5 #include "qchartaxis.h"
6 6 #include "qseries.h"
7 7 #include "qchart.h"
8 8 #include <QGraphicsView>
9 9
10 10 class QGraphicsScene;
11 11 class QRubberBand;
12 12
13 13 QTCOMMERCIALCHART_BEGIN_NAMESPACE
14 14
15 15 class QChart;
16 16
17 17 class QTCOMMERCIALCHART_EXPORT QChartView : public QGraphicsView
18 18 {
19 19 Q_OBJECT
20 20
21 21 public:
22 22 enum RubberBandPolicy { NoRubberBand, VerticalRubberBand, HorizonalRubberBand, RectangleRubberBand };
23 23
24 24 explicit QChartView(QWidget *parent = 0);
25 25 ~QChartView();
26 26
27 27 //implement from QWidget
28 28 void resizeEvent(QResizeEvent *event);
29 29
30 30 void addSeries(QSeries* series,QChartAxis* axisY=0);// takes series ownership , takes axis ownership
31 31 void removeSeries(QSeries* series); //returns ownership , deletes axis if no series attached
32 32 void removeAllSeries(); // deletes series and axis
33 33 int margin() const;
34 34
35 35 void setChartTitle(const QString& title);
36 36 QString chartTitle() const;
37 37 void setChartTitleFont(const QFont& font);
38 38 void setChartTitleBrush(const QBrush &brush);
39 39 QBrush chartTitleBrush();
40 40 void setChartBackgroundBrush(const QBrush& brush);
41 41 void setChartBackgroundPen(const QPen& pen);
42 42
43 43 void zoomIn();
44 44 void zoomIn(const QRect& rect);
45 45 void zoomOut();
46
47 void scroll(int dx,int dy);
46 void scrollLeft();
47 void scrollRight();
48 void scrollUp();
49 void scrollDown();
48 50
49 51 void setRubberBandPolicy(const RubberBandPolicy );
50 52 RubberBandPolicy rubberBandPolicy() const;
51 53
52 54 void setChartTheme(QChart::ChartTheme theme);
53 55 QChart::ChartTheme chartTheme() const;
54 56
55 57 void setAnimationOptions(QChart::AnimationOptions options);
56 58 QChart::AnimationOptions animationOptions() const;
57 59
58 60 QChartAxis* axisX() const;
59 61 QChartAxis* axisY() const;
60 62
61 63 QLegend* legend() const;
62 64
63 65 protected:
64 66 void mousePressEvent(QMouseEvent *event);
65 67 void mouseMoveEvent(QMouseEvent *event);
66 68 void mouseReleaseEvent(QMouseEvent *event);
67 69 void keyPressEvent(QKeyEvent *event);
68 70
69 71 private:
70 72 QGraphicsScene *m_scene;
71 73 QChart* m_chart;
72 74 QPoint m_rubberBandOrigin;
73 75 QRubberBand* m_rubberBand;
74 76 bool m_verticalRubberBand;
75 77 bool m_horizonalRubberBand;
76 78 Q_DISABLE_COPY(QChartView)
77 79 };
78 80
79 81 QTCOMMERCIALCHART_END_NAMESPACE
80 82
81 83 #endif // QCHARTWIDGET_H
General Comments 0
You need to be logged in to leave comments. Login now