##// END OF EJS Templates
Refactoring: QChartWidget is now a QGraphicsView
Tero Ahola -
r55:6d83ac09aba2
parent child
Show More
@@ -1,51 +1,49
1 #include "qchartwidget.h"
1 #include "qchartwidget.h"
2 #include "qchartseries.h"
2 #include "qchartseries.h"
3 #include <QGraphicsView>
3 #include <QGraphicsView>
4 #include <QGraphicsScene>
4 #include <QGraphicsScene>
5 #include <QResizeEvent>
5 #include <QResizeEvent>
6
6
7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8
8
9 QChartWidget::QChartWidget(QWidget *parent) :
9 QChartWidget::QChartWidget(QWidget *parent) :
10 QWidget(parent)
10 QGraphicsView(parent)
11 {
11 {
12 setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
12 setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
13 m_scene = new QGraphicsScene();
13 m_scene = new QGraphicsScene();
14 m_view = new QGraphicsView(parent);
14 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
15 m_view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
15 setScene(m_scene);
16 m_view->setScene(m_scene);
17 m_chart = new QChart();
16 m_chart = new QChart();
18 m_scene->addItem(m_chart);
17 m_scene->addItem(m_chart);
19 m_view->show();
18 show();
20 }
19 }
21
20
22 QChartWidget::~QChartWidget()
21 QChartWidget::~QChartWidget()
23 {
22 {
24 }
23 }
25
24
26 void QChartWidget::resizeEvent(QResizeEvent *event)
25 void QChartWidget::resizeEvent(QResizeEvent *event)
27 {
26 {
28 m_view->resize(size().width(),size().height());
29 m_scene->setSceneRect(0,0,size().width(),size().height());
27 m_scene->setSceneRect(0,0,size().width(),size().height());
30 m_chart->setSize(size());
28 m_chart->setSize(size());
31 QWidget::resizeEvent(event);
29 QWidget::resizeEvent(event);
32 }
30 }
33
31
34 QSize QChartWidget::sizeHint() const
32 QSize QChartWidget::sizeHint() const
35 {
33 {
36 // TODO: calculate size hint based on contents?
34 // TODO: calculate size hint based on contents?
37 return QSize(100, 100);
35 return QSize(100, 100);
38 }
36 }
39
37
40 void QChartWidget::addSeries(QChartSeries* series)
38 void QChartWidget::addSeries(QChartSeries* series)
41 {
39 {
42 m_chart->addSeries(series);
40 m_chart->addSeries(series);
43 }
41 }
44
42
45 QChartSeries* QChartWidget::createSeries(QList<qreal> x, QList<qreal> y, QChartSeries::QChartSeriesType type)
43 QChartSeries* QChartWidget::createSeries(QList<qreal> x, QList<qreal> y, QChartSeries::QChartSeriesType type)
46 {
44 {
47 return m_chart->createSeries(x, y, type);
45 return m_chart->createSeries(x, y, type);
48 }
46 }
49 #include "moc_qchartwidget.cpp"
47 #include "moc_qchartwidget.cpp"
50
48
51 QTCOMMERCIALCHART_END_NAMESPACE
49 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,41 +1,39
1 #ifndef QCHARTWIDGET_H
1 #ifndef QCHARTWIDGET_H
2 #define QCHARTWIDGET_H
2 #define QCHARTWIDGET_H
3
3
4 #include "qchartglobal.h"
4 #include "qchartglobal.h"
5 #include "qchart.h"
5 #include "qchart.h"
6 #include <QWidget>
6 #include <QGraphicsView>
7
7
8 class QGraphicsView;
9 class QGraphicsScene;
8 class QGraphicsScene;
10
9
11 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10 QTCOMMERCIALCHART_BEGIN_NAMESPACE
12
11
13 class QChartSeries;
12 class QChartSeries;
14 class QChartWidgetPrivate;
13 class QChartWidgetPrivate;
15
14
16 class QTCOMMERCIALCHART_EXPORT QChartWidget : public QWidget
15 class QTCOMMERCIALCHART_EXPORT QChartWidget : public QGraphicsView
17 {
16 {
18 Q_OBJECT
17 Q_OBJECT
19 public:
18 public:
20 explicit QChartWidget(QWidget *parent = 0);
19 explicit QChartWidget(QWidget *parent = 0);
21 ~QChartWidget();
20 ~QChartWidget();
22
21
23 //implement from QWidget
22 //implement from QWidget
24 void resizeEvent(QResizeEvent *event);
23 void resizeEvent(QResizeEvent *event);
25 QSize sizeHint() const;
24 QSize sizeHint() const;
26
25
27 // TODO: addSeries and createSeries are optional solutions
26 // TODO: addSeries and createSeries are optional solutions
28 void addSeries(QChartSeries* series);
27 void addSeries(QChartSeries* series);
29 QChartSeries* createSeries(QList<qreal> x, QList<qreal> y, QChartSeries::QChartSeriesType type);
28 QChartSeries* createSeries(QList<qreal> x, QList<qreal> y, QChartSeries::QChartSeriesType type);
30
29
31 private:
30 private:
32 Q_DISABLE_COPY(QChartWidget)
31 Q_DISABLE_COPY(QChartWidget)
33 QGraphicsView *m_view;
34 QGraphicsScene *m_scene;
32 QGraphicsScene *m_scene;
35 QChart* m_chart;
33 QChart* m_chart;
36
34
37 };
35 };
38
36
39 QTCOMMERCIALCHART_END_NAMESPACE
37 QTCOMMERCIALCHART_END_NAMESPACE
40
38
41 #endif // QCHARTWIDGET_H
39 #endif // QCHARTWIDGET_H
General Comments 0
You need to be logged in to leave comments. Login now