@@ -32,7 +32,6 ChartPresenter::ChartPresenter(QChart* chart,ChartDataSet* dataset):QObject(char | |||
|
32 | 32 | m_dataset(dataset), |
|
33 | 33 | m_chartTheme(0), |
|
34 | 34 | m_zoomIndex(0), |
|
35 | m_marginSize(0), | |
|
36 | 35 | m_rect(QRectF(QPoint(0,0),m_chart->size())), |
|
37 | 36 | m_options(QChart::NoAnimation) |
|
38 | 37 | { |
@@ -63,7 +62,7 QRectF ChartPresenter::geometry() const | |||
|
63 | 62 | void ChartPresenter::handleGeometryChanged() |
|
64 | 63 | { |
|
65 | 64 | QRectF rect(QPoint(0,0),m_chart->size()); |
|
66 | rect.adjust(m_marginSize,m_marginSize, -m_marginSize, -m_marginSize); | |
|
65 | rect.adjust(m_chart->padding(),m_chart->padding(), -m_chart->padding(), -m_chart->padding()); | |
|
67 | 66 | |
|
68 | 67 | //rewrite zoom stack |
|
69 | 68 | for(int i=0;i<m_zoomStack.count();i++){ |
@@ -86,16 +85,6 void ChartPresenter::handleGeometryChanged() | |||
|
86 | 85 | emit geometryChanged(m_rect); |
|
87 | 86 | } |
|
88 | 87 | |
|
89 | int ChartPresenter::margin() const | |
|
90 | { | |
|
91 | return m_marginSize; | |
|
92 | } | |
|
93 | ||
|
94 | void ChartPresenter::setMargin(int margin) | |
|
95 | { | |
|
96 | m_marginSize = margin; | |
|
97 | } | |
|
98 | ||
|
99 | 88 | void ChartPresenter::handleAxisAdded(QChartAxis* axis,Domain* domain) |
|
100 | 89 | { |
|
101 | 90 | AxisItem* item = new AxisItem(axis,this,axis==m_dataset->axisX()?AxisItem::X_AXIS : AxisItem::Y_AXIS,m_chart); |
@@ -339,7 +328,7 void ChartPresenter::zoomIn() | |||
|
339 | 328 | void ChartPresenter::zoomIn(const QRectF& rect) |
|
340 | 329 | { |
|
341 | 330 | QRectF r = rect.normalized(); |
|
342 |
r.translate(-m_ |
|
|
331 | r.translate(-m_chart->padding(), -m_chart->padding()); | |
|
343 | 332 | if(m_animator) { |
|
344 | 333 | |
|
345 | 334 | QPointF point(r.center().x()/geometry().width(),r.center().y()/geometry().height()); |
@@ -72,7 +72,6 private: | |||
|
72 | 72 | ChartDataSet* m_dataset; |
|
73 | 73 | ChartTheme *m_chartTheme; |
|
74 | 74 | int m_zoomIndex; |
|
75 | int m_marginSize; | |
|
76 | 75 | QMap<QSeries*,ChartItem*> m_chartItems; |
|
77 | 76 | QMap<QChartAxis*,AxisItem*> m_axisItems; |
|
78 | 77 | QVector<QRectF> m_zoomStack; |
@@ -3,6 +3,7 | |||
|
3 | 3 | #include "qlegend.h" |
|
4 | 4 | #include "chartpresenter_p.h" |
|
5 | 5 | #include "chartdataset_p.h" |
|
6 | #include "chartbackground_p.h" | |
|
6 | 7 | #include <QGraphicsScene> |
|
7 | 8 | #include <QGraphicsSceneResizeEvent> |
|
8 | 9 | #include <QDebug> |
@@ -54,7 +55,9 QChart::QChart(QGraphicsItem *parent, Qt::WindowFlags wFlags) : QGraphicsWidget( | |||
|
54 | 55 | m_titleItem(0), |
|
55 | 56 | m_legend(new QLegend(this)), |
|
56 | 57 | m_dataset(new ChartDataSet(this)), |
|
57 | m_presenter(new ChartPresenter(this,m_dataset)) | |
|
58 | m_presenter(new ChartPresenter(this,m_dataset)), | |
|
59 | m_padding(50), | |
|
60 | m_backgroundPadding(10) | |
|
58 | 61 | { |
|
59 | 62 | connect(m_dataset,SIGNAL(seriesAdded(QSeries*,Domain*)),m_legend,SLOT(handleSeriesAdded(QSeries*,Domain*))); |
|
60 | 63 | connect(m_dataset,SIGNAL(seriesRemoved(QSeries*)),m_legend,SLOT(handleSeriesRemoved(QSeries*))); |
@@ -173,7 +176,7 QBrush QChart::chartTitleBrush() | |||
|
173 | 176 | void QChart::createChartBackgroundItem() |
|
174 | 177 | { |
|
175 | 178 | if(!m_backgroundItem) { |
|
176 |
m_backgroundItem = new |
|
|
179 | m_backgroundItem = new ChartBackground(this); | |
|
177 | 180 | m_backgroundItem->setPen(Qt::NoPen); |
|
178 | 181 | m_backgroundItem->setZValue(ChartPresenter::BackgroundZValue); |
|
179 | 182 | } |
@@ -188,25 +191,6 void QChart::createChartTitleItem() | |||
|
188 | 191 | } |
|
189 | 192 | |
|
190 | 193 | /*! |
|
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 | \sa setMargin() | |
|
193 | */ | |
|
194 | int QChart::margin() const | |
|
195 | { | |
|
196 | return m_presenter->margin(); | |
|
197 | } | |
|
198 | ||
|
199 | /*! | |
|
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 | \sa margin() | |
|
202 | */ | |
|
203 | void QChart::setMargin(int margin) | |
|
204 | { | |
|
205 | m_presenter->setMargin(margin); | |
|
206 | updateLayout(); | |
|
207 | } | |
|
208 | ||
|
209 | /*! | |
|
210 | 194 | Sets the \a theme used by the chart for rendering the graphical representation of the data |
|
211 | 195 | \sa ChartTheme, chartTheme() |
|
212 | 196 | */ |
@@ -332,17 +316,17 void QChart::updateLayout() | |||
|
332 | 316 | { |
|
333 | 317 | if(!m_rect.isValid()) return; |
|
334 | 318 | |
|
335 |
QRectF rect = m_rect.adjusted(m |
|
|
319 | QRectF rect = m_rect.adjusted(m_padding,m_padding, -m_padding, -m_padding); | |
|
336 | 320 | |
|
337 | 321 | // recalculate title position |
|
338 | 322 | if (m_titleItem) { |
|
339 | 323 | QPointF center = m_rect.center() -m_titleItem->boundingRect().center(); |
|
340 |
m_titleItem->setPos(center.x(),m_rect.top()/2 + m |
|
|
324 | m_titleItem->setPos(center.x(),m_rect.top()/2 + m_padding/2); | |
|
341 | 325 | } |
|
342 | 326 | |
|
343 | 327 | //recalculate background gradient |
|
344 | 328 | if (m_backgroundItem) { |
|
345 | m_backgroundItem->setRect(rect); | |
|
329 | m_backgroundItem->setRect(m_rect.adjusted(m_backgroundPadding,m_backgroundPadding, -m_backgroundPadding, -m_backgroundPadding)); | |
|
346 | 330 | } |
|
347 | 331 | |
|
348 | 332 | // recalculate legend position |
@@ -352,6 +336,37 void QChart::updateLayout() | |||
|
352 | 336 | m_legend->setPreferredLayout(QLegend::PreferredLayoutHorizontal); |
|
353 | 337 | } |
|
354 | 338 | } |
|
339 | ||
|
340 | ||
|
341 | int QChart::padding() const | |
|
342 | { | |
|
343 | return m_padding; | |
|
344 | } | |
|
345 | ||
|
346 | void QChart::setPadding(int padding) | |
|
347 | { | |
|
348 | if(m_padding==padding){ | |
|
349 | m_padding = padding; | |
|
350 | m_presenter->handleGeometryChanged(); | |
|
351 | updateLayout(); | |
|
352 | } | |
|
353 | } | |
|
354 | ||
|
355 | void QChart::setBackgroundPadding(int padding) | |
|
356 | { | |
|
357 | if(m_backgroundPadding!=padding){ | |
|
358 | m_backgroundPadding = padding; | |
|
359 | updateLayout(); | |
|
360 | } | |
|
361 | } | |
|
362 | ||
|
363 | void QChart::setBackgroundDiameter(int diameter) | |
|
364 | { | |
|
365 | createChartBackgroundItem(); | |
|
366 | m_backgroundItem->setDimeter(diameter); | |
|
367 | m_backgroundItem->update(); | |
|
368 | } | |
|
369 | ||
|
355 | 370 | #include "moc_qchart.cpp" |
|
356 | 371 | |
|
357 | 372 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -21,6 +21,8 class ChartItem; | |||
|
21 | 21 | class ChartDataSet; |
|
22 | 22 | class ChartPresenter; |
|
23 | 23 | class QLegend; |
|
24 | class ChartBackground; | |
|
25 | ||
|
24 | 26 | |
|
25 | 27 | class QTCOMMERCIALCHART_EXPORT QChart : public QGraphicsWidget |
|
26 | 28 | { |
@@ -53,8 +55,6 public: | |||
|
53 | 55 | void removeSeries(QSeries* series); //returns ownership , deletes axis if no series attached |
|
54 | 56 | void removeAllSeries(); // deletes series and axis |
|
55 | 57 | |
|
56 | void setMargin(int margin); | |
|
57 | int margin() const; | |
|
58 | 58 | void setChartTheme(QChart::ChartTheme theme); |
|
59 | 59 | QChart::ChartTheme chartTheme() const; |
|
60 | 60 | |
@@ -84,22 +84,30 public: | |||
|
84 | 84 | // TODO: take (and give) legend instead of this. |
|
85 | 85 | QLegend* legend(); |
|
86 | 86 | |
|
87 | ||
|
88 | int padding() const; | |
|
89 | ||
|
87 | 90 | protected: |
|
88 | 91 | void resizeEvent(QGraphicsSceneResizeEvent *event); |
|
89 | 92 | |
|
90 | 93 | private: |
|
91 | 94 | inline void createChartBackgroundItem(); |
|
92 | 95 | inline void createChartTitleItem(); |
|
96 | void setPadding(int padding); | |
|
97 | void setBackgroundPadding(int padding); | |
|
98 | void setBackgroundDiameter(int diameter); | |
|
93 | 99 | void updateLayout(); |
|
94 | 100 | |
|
95 | 101 | private: |
|
96 | 102 | Q_DISABLE_COPY(QChart) |
|
97 |
|
|
|
103 | ChartBackground* m_backgroundItem; | |
|
98 | 104 | QGraphicsSimpleTextItem* m_titleItem; |
|
99 | 105 | QRectF m_rect; |
|
100 | 106 | QLegend* m_legend; |
|
101 | 107 | ChartDataSet *m_dataset; |
|
102 | 108 | ChartPresenter *m_presenter; |
|
109 | int m_padding; | |
|
110 | int m_backgroundPadding; | |
|
103 | 111 | }; |
|
104 | 112 | |
|
105 | 113 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -46,7 +46,6 QChartView::QChartView(QWidget *parent) : | |||
|
46 | 46 | setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); |
|
47 | 47 | setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); |
|
48 | 48 | setScene(m_scene); |
|
49 | m_chart->setMargin(50); | |
|
50 | 49 | m_scene->addItem(m_chart); |
|
51 | 50 | setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); |
|
52 | 51 | } |
@@ -126,14 +125,6 void QChartView::zoomOut() | |||
|
126 | 125 | } |
|
127 | 126 | |
|
128 | 127 | /*! |
|
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 | */ | |
|
131 | int QChartView::margin() const | |
|
132 | { | |
|
133 | return m_chart->margin(); | |
|
134 | } | |
|
135 | ||
|
136 | /*! | |
|
137 | 128 | Sets the chart \a title. A description text that is drawn above the chart. |
|
138 | 129 | */ |
|
139 | 130 | void QChartView::setChartTitle(const QString& title) |
@@ -240,8 +231,8 void QChartView::mousePressEvent(QMouseEvent *event) | |||
|
240 | 231 | { |
|
241 | 232 | if(m_rubberBand && m_rubberBand->isEnabled() && event->button() == Qt::LeftButton) { |
|
242 | 233 | |
|
243 |
int |
|
|
244 |
QRect rect( |
|
|
234 | int padding = m_chart->padding(); | |
|
235 | QRect rect(padding, padding, width() - 2 * padding, height() - 2 * padding); | |
|
245 | 236 | |
|
246 | 237 | if (rect.contains(event->pos())) { |
|
247 | 238 | m_rubberBandOrigin = event->pos(); |
@@ -262,8 +253,8 void QChartView::mousePressEvent(QMouseEvent *event) | |||
|
262 | 253 | void QChartView::mouseMoveEvent(QMouseEvent *event) |
|
263 | 254 | { |
|
264 | 255 | if(m_rubberBand && m_rubberBand->isVisible()) { |
|
265 |
int |
|
|
266 |
QRect rect( |
|
|
256 | int padding = m_chart->padding(); | |
|
257 | QRect rect(padding, padding, width() - 2 * padding, height() - 2 * padding); | |
|
267 | 258 | int width = event->pos().x() - m_rubberBandOrigin.x(); |
|
268 | 259 | int height = event->pos().y() - m_rubberBandOrigin.y(); |
|
269 | 260 | if(!m_verticalRubberBand) { |
@@ -30,7 +30,6 public: | |||
|
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 | int margin() const; | |
|
34 | 33 | |
|
35 | 34 | void setChartTitle(const QString& title); |
|
36 | 35 | QString chartTitle() const; |
@@ -16,20 +16,22 SOURCES += \ | |||
|
16 | 16 | qchartview.cpp \ |
|
17 | 17 | qseries.cpp \ |
|
18 | 18 | qlegend.cpp \ |
|
19 | legendmarker.cpp | |
|
19 | legendmarker.cpp \ | |
|
20 | chartbackground.cpp | |
|
20 | 21 | PRIVATE_HEADERS += \ |
|
21 | 22 | chartdataset_p.h \ |
|
22 | 23 | chartitem_p.h \ |
|
23 | 24 | chartpresenter_p.h \ |
|
24 | 25 | charttheme_p.h \ |
|
25 | 26 | domain_p.h \ |
|
26 | legendmarker_p.h | |
|
27 | legendmarker_p.h \ | |
|
28 | chartbackground_p.h | |
|
27 | 29 | PUBLIC_HEADERS += \ |
|
28 | 30 | qchart.h \ |
|
29 | 31 | qchartglobal.h \ |
|
30 | 32 | qseries.h \ |
|
31 | 33 | qchartview.h \ |
|
32 | qlegend.h | |
|
34 | qlegend.h | |
|
33 | 35 | |
|
34 | 36 | include(animations/animations.pri) |
|
35 | 37 | include(axis/axis.pri) |
General Comments 0
You need to be logged in to leave comments.
Login now