@@ -1,182 +1,184 | |||||
1 | #include "barpresenterbase_p.h" |
|
1 | #include "barpresenterbase_p.h" | |
2 | #include "bar_p.h" |
|
2 | #include "bar_p.h" | |
3 | #include "barvalue_p.h" |
|
3 | #include "barvalue_p.h" | |
4 | #include "separator_p.h" |
|
4 | #include "separator_p.h" | |
5 | #include "qbarset.h" |
|
5 | #include "qbarset.h" | |
6 | #include "qbarseries.h" |
|
6 | #include "qbarseries.h" | |
7 | #include "qchart.h" |
|
7 | #include "qchart.h" | |
8 | #include "qchartaxis.h" |
|
8 | #include "qchartaxis.h" | |
9 | #include "qchartaxiscategories.h" |
|
9 | #include "qchartaxiscategories.h" | |
|
10 | #include "chartpresenter_p.h" | |||
10 | #include <QDebug> |
|
11 | #include <QDebug> | |
11 | #include <QToolTip> |
|
12 | #include <QToolTip> | |
12 |
|
13 | |||
13 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
14 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
14 |
|
15 | |||
15 | BarPresenterBase::BarPresenterBase(QBarSeries *series, QChart *parent) : |
|
16 | BarPresenterBase::BarPresenterBase(QBarSeries *series, QChart *parent) : | |
16 | ChartItem(parent), |
|
17 | ChartItem(parent), | |
17 | mLayoutSet(false), |
|
18 | mLayoutSet(false), | |
18 | mSeries(series), |
|
19 | mSeries(series), | |
19 | mChart(parent), |
|
20 | mChart(parent), | |
20 | mWidth(0), |
|
21 | mWidth(0), | |
21 | mHeight(0) |
|
22 | mHeight(0) | |
22 | { |
|
23 | { | |
23 | connect(series,SIGNAL(showToolTip(QPoint,QString)),this,SLOT(showToolTip(QPoint,QString))); |
|
24 | connect(series,SIGNAL(showToolTip(QPoint,QString)),this,SLOT(showToolTip(QPoint,QString))); | |
24 | // connect(series,SIGNAL(enableSeparators(bool)),this,SLOT(enableSeparators(bool))); |
|
25 | // connect(series,SIGNAL(enableSeparators(bool)),this,SLOT(enableSeparators(bool))); | |
25 | // enableSeparators(series->separatorsVisible()); |
|
26 | // enableSeparators(series->separatorsVisible()); | |
|
27 | setZValue(ChartPresenter::BarSeriesZValue); | |||
26 | initAxisLabels(); |
|
28 | initAxisLabels(); | |
27 | dataChanged(); |
|
29 | dataChanged(); | |
28 | } |
|
30 | } | |
29 |
|
31 | |||
30 | BarPresenterBase::~BarPresenterBase() |
|
32 | BarPresenterBase::~BarPresenterBase() | |
31 | { |
|
33 | { | |
32 | disconnect(this,SLOT(showToolTip(QPoint,QString))); |
|
34 | disconnect(this,SLOT(showToolTip(QPoint,QString))); | |
33 | disconnect(this,SLOT(enableSeparators(bool))); |
|
35 | disconnect(this,SLOT(enableSeparators(bool))); | |
34 | } |
|
36 | } | |
35 |
|
37 | |||
36 | void BarPresenterBase::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) |
|
38 | void BarPresenterBase::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) | |
37 | { |
|
39 | { | |
38 | if (!mLayoutSet) { |
|
40 | if (!mLayoutSet) { | |
39 | qDebug() << "BarPresenterBase::paint called without layout set. Aborting."; |
|
41 | qDebug() << "BarPresenterBase::paint called without layout set. Aborting."; | |
40 | return; |
|
42 | return; | |
41 | } |
|
43 | } | |
42 | foreach(QGraphicsItem* i, childItems()) { |
|
44 | foreach(QGraphicsItem* i, childItems()) { | |
43 | i->paint(painter,option,widget); |
|
45 | i->paint(painter,option,widget); | |
44 | } |
|
46 | } | |
45 | } |
|
47 | } | |
46 |
|
48 | |||
47 | QRectF BarPresenterBase::boundingRect() const |
|
49 | QRectF BarPresenterBase::boundingRect() const | |
48 | { |
|
50 | { | |
49 | return QRectF(0, 0, mWidth, mHeight); |
|
51 | return QRectF(0, 0, mWidth, mHeight); | |
50 | } |
|
52 | } | |
51 |
|
53 | |||
52 | void BarPresenterBase::dataChanged() |
|
54 | void BarPresenterBase::dataChanged() | |
53 | { |
|
55 | { | |
54 | // TODO: performance optimizations. Do we really need to delete and create items every time data is changed or can we reuse them? |
|
56 | // TODO: performance optimizations. Do we really need to delete and create items every time data is changed or can we reuse them? | |
55 | // Delete old bars |
|
57 | // Delete old bars | |
56 | foreach (QGraphicsItem* item, childItems()) { |
|
58 | foreach (QGraphicsItem* item, childItems()) { | |
57 | delete item; |
|
59 | delete item; | |
58 | } |
|
60 | } | |
59 |
|
61 | |||
60 | mBars.clear(); |
|
62 | mBars.clear(); | |
61 | // mSeparators.clear(); |
|
63 | // mSeparators.clear(); | |
62 | mFloatingValues.clear(); |
|
64 | mFloatingValues.clear(); | |
63 |
|
65 | |||
64 | // Create new graphic items for bars |
|
66 | // Create new graphic items for bars | |
65 | for (int c=0; c<mSeries->categoryCount(); c++) { |
|
67 | for (int c=0; c<mSeries->categoryCount(); c++) { | |
66 | QString category = mSeries->categoryName(c); |
|
68 | QString category = mSeries->categoryName(c); | |
67 | for (int s=0; s<mSeries->barsetCount(); s++) { |
|
69 | for (int s=0; s<mSeries->barsetCount(); s++) { | |
68 | QBarSet *set = mSeries->barsetAt(s); |
|
70 | QBarSet *set = mSeries->barsetAt(s); | |
69 | Bar *bar = new Bar(category,this); |
|
71 | Bar *bar = new Bar(category,this); | |
70 | childItems().append(bar); |
|
72 | childItems().append(bar); | |
71 | mBars.append(bar); |
|
73 | mBars.append(bar); | |
72 | connect(bar,SIGNAL(clicked(QString)),set,SIGNAL(clicked(QString))); |
|
74 | connect(bar,SIGNAL(clicked(QString)),set,SIGNAL(clicked(QString))); | |
73 | connect(bar,SIGNAL(rightClicked(QString)),set,SIGNAL(rightClicked(QString))); |
|
75 | connect(bar,SIGNAL(rightClicked(QString)),set,SIGNAL(rightClicked(QString))); | |
74 | connect(bar,SIGNAL(hoverEntered(QPoint)),set,SLOT(barHoverEnterEvent(QPoint))); |
|
76 | connect(bar,SIGNAL(hoverEntered(QPoint)),set,SLOT(barHoverEnterEvent(QPoint))); | |
75 | connect(bar,SIGNAL(hoverLeaved()),set,SLOT(barHoverLeaveEvent())); |
|
77 | connect(bar,SIGNAL(hoverLeaved()),set,SLOT(barHoverLeaveEvent())); | |
76 | } |
|
78 | } | |
77 | } |
|
79 | } | |
78 | /* |
|
80 | /* | |
79 | // Create separators |
|
81 | // Create separators | |
80 | int count = mSeries->categoryCount() - 1; // There is one less separator than columns |
|
82 | int count = mSeries->categoryCount() - 1; // There is one less separator than columns | |
81 | for (int i=0; i<count; i++) { |
|
83 | for (int i=0; i<count; i++) { | |
82 | Separator* sep = new Separator(this); |
|
84 | Separator* sep = new Separator(this); | |
83 | sep->setVisible(mSeries->separatorsVisible()); |
|
85 | sep->setVisible(mSeries->separatorsVisible()); | |
84 | childItems().append(sep); |
|
86 | childItems().append(sep); | |
85 | mSeparators.append(sep); |
|
87 | mSeparators.append(sep); | |
86 | } |
|
88 | } | |
87 | */ |
|
89 | */ | |
88 | // Create floating values |
|
90 | // Create floating values | |
89 | for (int category=0; category<mSeries->categoryCount(); category++) { |
|
91 | for (int category=0; category<mSeries->categoryCount(); category++) { | |
90 | for (int s=0; s<mSeries->barsetCount(); s++) { |
|
92 | for (int s=0; s<mSeries->barsetCount(); s++) { | |
91 | QBarSet *set = mSeries->barsetAt(s); |
|
93 | QBarSet *set = mSeries->barsetAt(s); | |
92 | BarValue *value = new BarValue(*set, this); |
|
94 | BarValue *value = new BarValue(*set, this); | |
93 | childItems().append(value); |
|
95 | childItems().append(value); | |
94 | mFloatingValues.append(value); |
|
96 | mFloatingValues.append(value); | |
95 | connect(set,SIGNAL(toggleFloatingValues()),value,SLOT(toggleVisible())); |
|
97 | connect(set,SIGNAL(toggleFloatingValues()),value,SLOT(toggleVisible())); | |
96 | } |
|
98 | } | |
97 | } |
|
99 | } | |
98 | } |
|
100 | } | |
99 |
|
101 | |||
100 | void BarPresenterBase::initAxisLabels() |
|
102 | void BarPresenterBase::initAxisLabels() | |
101 | { |
|
103 | { | |
102 | int count = mSeries->categoryCount(); |
|
104 | int count = mSeries->categoryCount(); | |
103 | if (0 == count) { |
|
105 | if (0 == count) { | |
104 | return; |
|
106 | return; | |
105 | } |
|
107 | } | |
106 |
|
108 | |||
107 | mChart->axisX()->setTicksCount(count+2); |
|
109 | mChart->axisX()->setTicksCount(count+2); | |
108 |
|
110 | |||
109 | qreal min = 0; |
|
111 | qreal min = 0; | |
110 | qreal max = count+1; |
|
112 | qreal max = count+1; | |
111 |
|
113 | |||
112 | mChart->axisX()->setMin(min); |
|
114 | mChart->axisX()->setMin(min); | |
113 | mChart->axisX()->setMax(max); |
|
115 | mChart->axisX()->setMax(max); | |
114 |
|
116 | |||
115 | QChartAxisCategories* categories = mChart->axisX()->categories(); |
|
117 | QChartAxisCategories* categories = mChart->axisX()->categories(); | |
116 | categories->clear(); |
|
118 | categories->clear(); | |
117 | for (int i=0; i<count; i++) { |
|
119 | for (int i=0; i<count; i++) { | |
118 | categories->insert(i+1,mSeries->categoryName(i)); |
|
120 | categories->insert(i+1,mSeries->categoryName(i)); | |
119 | } |
|
121 | } | |
120 |
|
122 | |||
121 |
|
123 | |||
122 |
|
124 | |||
123 | mChart->axisX()->setLabelsVisible(true); |
|
125 | mChart->axisX()->setLabelsVisible(true); | |
124 | } |
|
126 | } | |
125 |
|
127 | |||
126 | //handlers |
|
128 | //handlers | |
127 |
|
129 | |||
128 | void BarPresenterBase::handleModelChanged(int index) |
|
130 | void BarPresenterBase::handleModelChanged(int index) | |
129 | { |
|
131 | { | |
130 | // qDebug() << "BarPresenterBase::handleModelChanged" << index; |
|
132 | // qDebug() << "BarPresenterBase::handleModelChanged" << index; | |
131 | dataChanged(); |
|
133 | dataChanged(); | |
132 | } |
|
134 | } | |
133 |
|
135 | |||
134 | void BarPresenterBase::handleDomainChanged(const Domain& domain) |
|
136 | void BarPresenterBase::handleDomainChanged(const Domain& domain) | |
135 | { |
|
137 | { | |
136 | qDebug() << "BarPresenterBase::handleDomainChanged"; |
|
138 | qDebug() << "BarPresenterBase::handleDomainChanged"; | |
137 | /* |
|
139 | /* | |
138 | int count = mSeries->categoryCount(); |
|
140 | int count = mSeries->categoryCount(); | |
139 | if (0 == count) { |
|
141 | if (0 == count) { | |
140 | return; |
|
142 | return; | |
141 | } |
|
143 | } | |
142 |
|
144 | |||
143 | // Position labels to domain |
|
145 | // Position labels to domain | |
144 | qreal min = domain.minX(); |
|
146 | qreal min = domain.minX(); | |
145 | qreal max = domain.maxX(); |
|
147 | qreal max = domain.maxX(); | |
146 | qreal step = (max-min)/count; |
|
148 | qreal step = (max-min)/count; | |
147 | QChartAxisCategories& categories = mChart->axisX()->categories(); |
|
149 | QChartAxisCategories& categories = mChart->axisX()->categories(); | |
148 | categories.clear(); |
|
150 | categories.clear(); | |
149 | for (int i=0; i<count; i++) { |
|
151 | for (int i=0; i<count; i++) { | |
150 | categories.insert(min,mSeries->categoryName(i)); |
|
152 | categories.insert(min,mSeries->categoryName(i)); | |
151 | min += step; |
|
153 | min += step; | |
152 | } |
|
154 | } | |
153 | */ |
|
155 | */ | |
154 | } |
|
156 | } | |
155 |
|
157 | |||
156 | void BarPresenterBase::handleGeometryChanged(const QRectF& rect) |
|
158 | void BarPresenterBase::handleGeometryChanged(const QRectF& rect) | |
157 | { |
|
159 | { | |
158 | mWidth = rect.width(); |
|
160 | mWidth = rect.width(); | |
159 | mHeight = rect.height(); |
|
161 | mHeight = rect.height(); | |
160 | layoutChanged(); |
|
162 | layoutChanged(); | |
161 | mLayoutSet = true; |
|
163 | mLayoutSet = true; | |
162 | setPos(rect.topLeft()); |
|
164 | setPos(rect.topLeft()); | |
163 | } |
|
165 | } | |
164 |
|
166 | |||
165 | void BarPresenterBase::showToolTip(QPoint pos, QString tip) |
|
167 | void BarPresenterBase::showToolTip(QPoint pos, QString tip) | |
166 | { |
|
168 | { | |
167 | // TODO: cool tooltip instead of default |
|
169 | // TODO: cool tooltip instead of default | |
168 | QToolTip::showText(pos,tip); |
|
170 | QToolTip::showText(pos,tip); | |
169 | } |
|
171 | } | |
170 |
|
172 | |||
171 | /* |
|
173 | /* | |
172 | void BarPresenterBase::enableSeparators(bool enabled) |
|
174 | void BarPresenterBase::enableSeparators(bool enabled) | |
173 | { |
|
175 | { | |
174 | for (int i=0; i<mSeparators.count(); i++) { |
|
176 | for (int i=0; i<mSeparators.count(); i++) { | |
175 | mSeparators.at(i)->setVisible(enabled); |
|
177 | mSeparators.at(i)->setVisible(enabled); | |
176 | } |
|
178 | } | |
177 | } |
|
179 | } | |
178 | */ |
|
180 | */ | |
179 |
|
181 | |||
180 | #include "moc_barpresenterbase_p.cpp" |
|
182 | #include "moc_barpresenterbase_p.cpp" | |
181 |
|
183 | |||
182 | QTCOMMERCIALCHART_END_NAMESPACE |
|
184 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,85 +1,86 | |||||
1 | #ifndef CHARTPRESENTER_H_ |
|
1 | #ifndef CHARTPRESENTER_H_ | |
2 | #define CHARTPRESENTER_H_ |
|
2 | #define CHARTPRESENTER_H_ | |
3 |
|
3 | |||
4 | #include "qchartglobal.h" |
|
4 | #include "qchartglobal.h" | |
5 | #include "qchart.h" //becouse of QChart::ChartThemeId //TODO |
|
5 | #include "qchart.h" //becouse of QChart::ChartThemeId //TODO | |
6 | #include "qchartaxis.h" |
|
6 | #include "qchartaxis.h" | |
7 | #include <QRectF> |
|
7 | #include <QRectF> | |
8 |
|
8 | |||
9 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
9 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
10 |
|
10 | |||
11 | class ChartItem; |
|
11 | class ChartItem; | |
12 | class QSeries; |
|
12 | class QSeries; | |
13 | class ChartDataSet; |
|
13 | class ChartDataSet; | |
14 | class Domain; |
|
14 | class Domain; | |
15 | class AxisItem; |
|
15 | class AxisItem; | |
16 | class ChartTheme; |
|
16 | class ChartTheme; | |
17 | class ChartAnimator; |
|
17 | class ChartAnimator; | |
18 |
|
18 | |||
19 | class ChartPresenter: public QObject |
|
19 | class ChartPresenter: public QObject | |
20 | { |
|
20 | { | |
21 | Q_OBJECT |
|
21 | Q_OBJECT | |
22 | public: |
|
22 | public: | |
23 | enum ZValues { |
|
23 | enum ZValues { | |
24 | BackgroundZValue = -1, |
|
24 | BackgroundZValue = -1, | |
25 | ShadesZValue, |
|
25 | ShadesZValue, | |
26 | GridZValue, |
|
26 | GridZValue, | |
27 | AxisZValue, |
|
27 | AxisZValue, | |
28 | LineChartZValue, |
|
28 | LineChartZValue, | |
|
29 | BarSeriesZValue, | |||
29 | ScatterSeriesZValue, |
|
30 | ScatterSeriesZValue, | |
30 | PieSeriesZValue, |
|
31 | PieSeriesZValue, | |
31 | LegendZValue |
|
32 | LegendZValue | |
32 | }; |
|
33 | }; | |
33 |
|
34 | |||
34 | ChartPresenter(QChart* chart,ChartDataSet *dataset); |
|
35 | ChartPresenter(QChart* chart,ChartDataSet *dataset); | |
35 | virtual ~ChartPresenter(); |
|
36 | virtual ~ChartPresenter(); | |
36 |
|
37 | |||
37 | void setMargin(int margin); |
|
38 | void setMargin(int margin); | |
38 | int margin() const; |
|
39 | int margin() const; | |
39 |
|
40 | |||
40 | QRectF geometry() const; |
|
41 | QRectF geometry() const; | |
41 | ChartAnimator* animator() const {return m_animator;}; |
|
42 | ChartAnimator* animator() const {return m_animator;}; | |
42 |
|
43 | |||
43 | void setChartTheme(QChart::ChartTheme theme); |
|
44 | void setChartTheme(QChart::ChartTheme theme); | |
44 | QChart::ChartTheme chartTheme(); |
|
45 | QChart::ChartTheme chartTheme(); | |
45 |
|
46 | |||
46 | void setAnimationOptions(QChart::AnimationOptions options); |
|
47 | void setAnimationOptions(QChart::AnimationOptions options); | |
47 | QChart::AnimationOptions animationOptions() const; |
|
48 | QChart::AnimationOptions animationOptions() const; | |
48 |
|
49 | |||
49 | void zoomIn(); |
|
50 | void zoomIn(); | |
50 | void zoomIn(const QRectF& rect); |
|
51 | void zoomIn(const QRectF& rect); | |
51 | void zoomOut(); |
|
52 | void zoomOut(); | |
52 | void zoomReset(); |
|
53 | void zoomReset(); | |
53 | void scroll(int dx,int dy); |
|
54 | void scroll(int dx,int dy); | |
54 | private: |
|
55 | private: | |
55 | void createConnections(); |
|
56 | void createConnections(); | |
56 | void resetAllElements(); |
|
57 | void resetAllElements(); | |
57 |
|
58 | |||
58 | public slots: |
|
59 | public slots: | |
59 | void handleSeriesAdded(QSeries* series,Domain* domain); |
|
60 | void handleSeriesAdded(QSeries* series,Domain* domain); | |
60 | void handleSeriesRemoved(QSeries* series); |
|
61 | void handleSeriesRemoved(QSeries* series); | |
61 | void handleAxisAdded(QChartAxis* axis,Domain* domain); |
|
62 | void handleAxisAdded(QChartAxis* axis,Domain* domain); | |
62 | void handleAxisRemoved(QChartAxis* axis); |
|
63 | void handleAxisRemoved(QChartAxis* axis); | |
63 | void handleGeometryChanged(); |
|
64 | void handleGeometryChanged(); | |
64 |
|
65 | |||
65 | signals: |
|
66 | signals: | |
66 | void geometryChanged(const QRectF& rect); |
|
67 | void geometryChanged(const QRectF& rect); | |
67 |
|
68 | |||
68 | private: |
|
69 | private: | |
69 | QChart* m_chart; |
|
70 | QChart* m_chart; | |
70 | ChartAnimator* m_animator; |
|
71 | ChartAnimator* m_animator; | |
71 | ChartDataSet* m_dataset; |
|
72 | ChartDataSet* m_dataset; | |
72 | ChartTheme *m_chartTheme; |
|
73 | ChartTheme *m_chartTheme; | |
73 | int m_zoomIndex; |
|
74 | int m_zoomIndex; | |
74 | int m_marginSize; |
|
75 | int m_marginSize; | |
75 | QMap<QSeries*,ChartItem*> m_chartItems; |
|
76 | QMap<QSeries*,ChartItem*> m_chartItems; | |
76 | QMap<QChartAxis*,AxisItem*> m_axisItems; |
|
77 | QMap<QChartAxis*,AxisItem*> m_axisItems; | |
77 | QVector<QRectF> m_zoomStack; |
|
78 | QVector<QRectF> m_zoomStack; | |
78 | QRectF m_rect; |
|
79 | QRectF m_rect; | |
79 | QChart::AnimationOptions m_options; |
|
80 | QChart::AnimationOptions m_options; | |
80 |
|
81 | |||
81 | }; |
|
82 | }; | |
82 |
|
83 | |||
83 | QTCOMMERCIALCHART_END_NAMESPACE |
|
84 | QTCOMMERCIALCHART_END_NAMESPACE | |
84 |
|
85 | |||
85 | #endif /* CHARTPRESENTER_H_ */ |
|
86 | #endif /* CHARTPRESENTER_H_ */ |
General Comments 0
You need to be logged in to leave comments.
Login now