@@ -1,274 +1,285 | |||
|
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 | \enum QChart::AnimationOption | |
|
25 | ||
|
26 | For enabling/disabling animations. Defaults to NoAnimation. | |
|
27 | ||
|
28 | \value NoAnimation | |
|
29 | \value GridAxisAnimations | |
|
30 | \value SeriesAnimations | |
|
31 | \value AllAnimations | |
|
32 | */ | |
|
33 | ||
|
34 | /*! | |
|
24 | 35 | \class QChart |
|
25 | 36 | \brief QtCommercial chart API. |
|
26 | 37 | |
|
27 | 38 | QChart is a QGraphicsWidget that you can show in a QGraphicsScene. It manages the graphical |
|
28 | 39 | representation of different types of QChartSeries and other chart related objects like |
|
29 | 40 | QChartAxis and QChartLegend. If you simply want to show a chart in a layout, you can use the |
|
30 | 41 | convenience class QChartView instead of QChart. |
|
31 | 42 | \sa QChartView |
|
32 | 43 | */ |
|
33 | 44 | |
|
34 | 45 | /*! |
|
35 | 46 | Constructs a chart object which is a child of a\a parent. Parameter \a wFlags is passed to the QGraphicsWidget constructor. |
|
36 | 47 | */ |
|
37 | 48 | QChart::QChart(QGraphicsItem *parent, Qt::WindowFlags wFlags) : QGraphicsWidget(parent,wFlags), |
|
38 | 49 | m_backgroundItem(0), |
|
39 | 50 | m_titleItem(0), |
|
40 | 51 | m_dataset(new ChartDataSet(this)), |
|
41 | 52 | m_presenter(new ChartPresenter(this,m_dataset)) |
|
42 | 53 | { |
|
43 | 54 | } |
|
44 | 55 | |
|
45 | 56 | /*! |
|
46 | 57 | Destroys the object and it's children, like QChartSeries and QChartAxis object added to it. |
|
47 | 58 | */ |
|
48 | 59 | QChart::~QChart() |
|
49 | 60 | { |
|
50 | 61 | } |
|
51 | 62 | |
|
52 | 63 | /*! |
|
53 | 64 | Adds the \a series and optional \a axisY onto the chart and takes the ownership of the objects. |
|
54 | 65 | If auto scaling is enabled, re-scales the axes the series is bound to (both the x axis and |
|
55 | 66 | the y axis). |
|
56 | 67 | */ |
|
57 | 68 | void QChart::addSeries(QChartSeries* series, QChartAxis* axisY) |
|
58 | 69 | { |
|
59 | 70 | m_dataset->addSeries(series, axisY); |
|
60 | 71 | } |
|
61 | 72 | |
|
62 | 73 | /*! |
|
63 | 74 | Removes the \a series specified in a perameter from the QChartView. |
|
64 | 75 | It releses its ownership of the specified QChartSeries object. |
|
65 | 76 | It does not delete the pointed QChartSeries data object |
|
66 | 77 | \sa addSeries(), removeAllSeries() |
|
67 | 78 | */ |
|
68 | 79 | void QChart::removeSeries(QChartSeries* series) |
|
69 | 80 | { |
|
70 | 81 | m_dataset->removeSeries(series); |
|
71 | 82 | } |
|
72 | 83 | |
|
73 | 84 | /*! |
|
74 | 85 | Removes all the QChartSeries that have been added to the QChartView |
|
75 | 86 | It also deletes the pointed QChartSeries data objects |
|
76 | 87 | \sa addSeries(), removeSeries() |
|
77 | 88 | */ |
|
78 | 89 | void QChart::removeAllSeries() |
|
79 | 90 | { |
|
80 | 91 | m_dataset->removeAllSeries(); |
|
81 | 92 | } |
|
82 | 93 | |
|
83 | 94 | /*! |
|
84 | 95 | Sets the \a brush that is used for painting the background of the chart area. |
|
85 | 96 | */ |
|
86 | 97 | void QChart::setChartBackgroundBrush(const QBrush& brush) |
|
87 | 98 | { |
|
88 | 99 | createChartBackgroundItem(); |
|
89 | 100 | m_backgroundItem->setBrush(brush); |
|
90 | 101 | m_backgroundItem->update(); |
|
91 | 102 | } |
|
92 | 103 | |
|
93 | 104 | /*! |
|
94 | 105 | Sets the \a pen that is used for painting the background of the chart area. |
|
95 | 106 | */ |
|
96 | 107 | void QChart::setChartBackgroundPen(const QPen& pen) |
|
97 | 108 | { |
|
98 | 109 | createChartBackgroundItem(); |
|
99 | 110 | m_backgroundItem->setPen(pen); |
|
100 | 111 | m_backgroundItem->update(); |
|
101 | 112 | } |
|
102 | 113 | |
|
103 | 114 | /*! |
|
104 | 115 | Sets the chart \a title. The description text that is rendered above the chart. |
|
105 | 116 | */ |
|
106 | 117 | void QChart::setChartTitle(const QString& title) |
|
107 | 118 | { |
|
108 | 119 | createChartTitleItem(); |
|
109 | 120 | m_titleItem->setPlainText(title); |
|
110 | 121 | } |
|
111 | 122 | |
|
112 | 123 | /*! |
|
113 | 124 | Sets the \a font that is used for rendering the description text that is rendered above the chart. |
|
114 | 125 | */ |
|
115 | 126 | void QChart::setChartTitleFont(const QFont& font) |
|
116 | 127 | { |
|
117 | 128 | createChartTitleItem(); |
|
118 | 129 | m_titleItem->setFont(font); |
|
119 | 130 | } |
|
120 | 131 | |
|
121 | 132 | void QChart::createChartBackgroundItem() |
|
122 | 133 | { |
|
123 | 134 | if(!m_backgroundItem) { |
|
124 | 135 | m_backgroundItem = new QGraphicsRectItem(this); |
|
125 | 136 | m_backgroundItem->setPen(Qt::NoPen); |
|
126 | 137 | m_backgroundItem->setZValue(ChartPresenter::BackgroundZValue); |
|
127 | 138 | } |
|
128 | 139 | } |
|
129 | 140 | |
|
130 | 141 | void QChart::createChartTitleItem() |
|
131 | 142 | { |
|
132 | 143 | if(!m_titleItem) { |
|
133 | 144 | m_titleItem = new QGraphicsTextItem(this); |
|
134 | 145 | m_titleItem->setZValue(ChartPresenter::BackgroundZValue); |
|
135 | 146 | } |
|
136 | 147 | } |
|
137 | 148 | |
|
138 | 149 | /*! |
|
139 | 150 | 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 | 151 | \sa setMargin() |
|
141 | 152 | */ |
|
142 | 153 | int QChart::margin() const |
|
143 | 154 | { |
|
144 | 155 | return m_presenter->margin(); |
|
145 | 156 | } |
|
146 | 157 | |
|
147 | 158 | /*! |
|
148 | 159 | 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 | 160 | \sa margin() |
|
150 | 161 | */ |
|
151 | 162 | void QChart::setMargin(int margin) |
|
152 | 163 | { |
|
153 | 164 | m_presenter->setMargin(margin); |
|
154 | 165 | } |
|
155 | 166 | |
|
156 | 167 | /*! |
|
157 | 168 | Sets the \a theme used by the chart for rendering the graphical representation of the data |
|
158 | 169 | \sa ChartTheme, chartTheme() |
|
159 | 170 | */ |
|
160 | 171 | void QChart::setChartTheme(QChart::ChartTheme theme) |
|
161 | 172 | { |
|
162 | 173 | m_presenter->setChartTheme(theme); |
|
163 | 174 | } |
|
164 | 175 | |
|
165 | 176 | /*! |
|
166 | 177 | Returns the theme enum used by the chart. |
|
167 | 178 | \sa ChartTheme, setChartTheme() |
|
168 | 179 | */ |
|
169 | 180 | QChart::ChartTheme QChart::chartTheme() const |
|
170 | 181 | { |
|
171 | 182 | return m_presenter->chartTheme(); |
|
172 | 183 | } |
|
173 | 184 | |
|
174 | 185 | /*! |
|
175 | 186 | Zooms in the view by a factor of 2 |
|
176 | 187 | */ |
|
177 | 188 | void QChart::zoomIn() |
|
178 | 189 | { |
|
179 | 190 | if (!m_dataset->nextDomain()) { |
|
180 | 191 | QRectF rect = m_presenter->geometry(); |
|
181 | 192 | rect.setWidth(rect.width()/2); |
|
182 | 193 | rect.setHeight(rect.height()/2); |
|
183 | 194 | rect.moveCenter(m_presenter->geometry().center()); |
|
184 | 195 | zoomIn(rect); |
|
185 | 196 | } |
|
186 | 197 | } |
|
187 | 198 | |
|
188 | 199 | /*! |
|
189 | 200 | Zooms in the view to a maximum level at which \a rect is still fully visible. |
|
190 | 201 | */ |
|
191 | 202 | void QChart::zoomIn(const QRectF& rect) |
|
192 | 203 | { |
|
193 | 204 | if(!rect.isValid()) return; |
|
194 | 205 | QRectF r = rect.normalized(); |
|
195 | 206 | int margin = m_presenter->margin(); |
|
196 | 207 | r.translate(-margin, -margin); |
|
197 | 208 | m_dataset->addDomain(r,m_presenter->geometry()); |
|
198 | 209 | } |
|
199 | 210 | |
|
200 | 211 | /*! |
|
201 | 212 | Restores the view zoom level to the previous one. |
|
202 | 213 | */ |
|
203 | 214 | void QChart::zoomOut() |
|
204 | 215 | { |
|
205 | 216 | m_dataset->previousDomain(); |
|
206 | 217 | } |
|
207 | 218 | |
|
208 | 219 | /*! |
|
209 | 220 | Resets to the default view. |
|
210 | 221 | */ |
|
211 | 222 | void QChart::zoomReset() |
|
212 | 223 | { |
|
213 | 224 | m_dataset->clearDomains(); |
|
214 | 225 | } |
|
215 | 226 | |
|
216 | 227 | /*! |
|
217 | 228 | Returns the pointer to the x axis object of the chart |
|
218 | 229 | */ |
|
219 | 230 | QChartAxis* QChart::axisX() const |
|
220 | 231 | { |
|
221 | 232 | return m_dataset->axisX(); |
|
222 | 233 | } |
|
223 | 234 | |
|
224 | 235 | /*! |
|
225 | 236 | Returns the pointer to the y axis object of the chart |
|
226 | 237 | */ |
|
227 | 238 | QChartAxis* QChart::axisY() const |
|
228 | 239 | { |
|
229 | 240 | return m_dataset->axisY(); |
|
230 | 241 | } |
|
231 | 242 | |
|
232 | 243 | /*! |
|
233 | 244 | Resizes and updates the chart area using the \a event data |
|
234 | 245 | */ |
|
235 | 246 | void QChart::resizeEvent(QGraphicsSceneResizeEvent *event) |
|
236 | 247 | { |
|
237 | 248 | |
|
238 | 249 | m_rect = QRectF(QPoint(0,0),event->newSize()); |
|
239 | 250 | QRectF rect = m_rect.adjusted(margin(),margin(), -margin(), -margin()); |
|
240 | 251 | |
|
241 | 252 | // recalculate title position |
|
242 | 253 | if (m_titleItem) { |
|
243 | 254 | QPointF center = m_rect.center() -m_titleItem->boundingRect().center(); |
|
244 | 255 | m_titleItem->setPos(center.x(),m_rect.top()/2 + margin()/2); |
|
245 | 256 | } |
|
246 | 257 | |
|
247 | 258 | //recalculate background gradient |
|
248 | 259 | if (m_backgroundItem) { |
|
249 | 260 | m_backgroundItem->setRect(rect); |
|
250 | 261 | } |
|
251 | 262 | |
|
252 | 263 | QGraphicsWidget::resizeEvent(event); |
|
253 | 264 | update(); |
|
254 | 265 | } |
|
255 | 266 | |
|
256 | 267 | /*! |
|
257 | Sets animation options for the chart | |
|
268 | Sets animation \a options for the chart | |
|
258 | 269 | */ |
|
259 | 270 | void QChart::setAnimationOptions(AnimationOptions options) |
|
260 | 271 | { |
|
261 | 272 | m_presenter->setAnimationOptions(options); |
|
262 | 273 | } |
|
263 | 274 | |
|
264 | 275 | /*! |
|
265 | 276 | Returns animation options for the chart |
|
266 | 277 | */ |
|
267 | 278 | QChart::AnimationOptions QChart::animationOptions() const |
|
268 | 279 | { |
|
269 | 280 | return m_presenter->animationOptions(); |
|
270 | 281 | } |
|
271 | 282 | |
|
272 | 283 | #include "moc_qchart.cpp" |
|
273 | 284 | |
|
274 | 285 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,350 +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 | 334 | /*! |
|
335 | Sets animation options for the chart | |
|
335 | Sets animation \a options for the chart | |
|
336 | 336 | */ |
|
337 | 337 | void QChartView::setAnimationOptions(QChart::AnimationOptions options) |
|
338 | 338 | { |
|
339 | 339 | m_chart->setAnimationOptions(options); |
|
340 | 340 | } |
|
341 | 341 | |
|
342 | 342 | /*! |
|
343 | 343 | Returns animation options for the chart |
|
344 | 344 | */ |
|
345 | 345 | QChart::AnimationOptions QChartView::animationOptions() const |
|
346 | 346 | { |
|
347 | 347 | return m_chart->animationOptions(); |
|
348 | 348 | } |
|
349 | 349 | |
|
350 | 350 | QTCOMMERCIALCHART_END_NAMESPACE |
General Comments 0
You need to be logged in to leave comments.
Login now