##// END OF EJS Templates
Adds missing ids to theme classes
Michal Klocek -
r153:04be7b528347
parent child
Show More
@@ -1,251 +1,261
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 "charttheme_p.h"
6 6 //series
7 7 #include "barchartseries.h"
8 8 #include "stackedbarchartseries.h"
9 9 #include "percentbarchartseries.h"
10 10 #include "qlinechartseries.h"
11 11 #include "qpieseries.h"
12 12 //items
13 13 #include "axisitem_p.h"
14 14 #include "bargroup.h"
15 15 #include "stackedbargroup.h"
16 16 #include "linechartitem_p.h"
17 17 #include "percentbargroup.h"
18 18 #include "linechartanimationitem_p.h"
19 19 #include "piepresenter.h"
20 20
21 21 QTCOMMERCIALCHART_BEGIN_NAMESPACE
22 22
23 23 ChartPresenter::ChartPresenter(QChart* chart,ChartDataSet* dataset):QObject(chart),
24 24 m_chart(chart),
25 25 m_dataset(dataset),
26 26 m_chartTheme(0),
27 27 m_domainIndex(0),
28 28 m_marginSize(0),
29 29 m_rect(QRectF(QPoint(0,0),m_chart->size()))
30 30 {
31 setTheme(QChart::ChartThemeDefault);
31 setChartTheme(QChart::ChartThemeDefault);
32 32 createConnections();
33 33 createDeafultAxis();
34 34 }
35 35
36 36 ChartPresenter::~ChartPresenter()
37 37 {
38 38 }
39 39
40 40 void ChartPresenter::createDeafultAxis()
41 41 {
42 42 //default axis
43 43 QChartAxis* axisX = new QChartAxis(this);
44 44 QChartAxis* axisY = new QChartAxis(this);
45 45
46 46 m_axis << new AxisItem(axisX,AxisItem::X_AXIS,m_chart);
47 47 m_axis << new AxisItem(axisY,AxisItem::Y_AXIS,m_chart);
48 48
49 49 foreach(AxisItem* item, m_axis) {
50 50 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
51 51 QObject::connect(m_dataset,SIGNAL(domainChanged(const Domain&)),item,SLOT(handleDomainChanged(const Domain&)));
52 52 }
53 53 }
54 54
55 55 void ChartPresenter::createConnections()
56 56 {
57 57 QObject::connect(m_chart,SIGNAL(geometryChanged()),this,SLOT(handleGeometryChanged()));
58 58 QObject::connect(m_dataset,SIGNAL(seriesAdded(QChartSeries*)),this,SLOT(handleSeriesAdded(QChartSeries*)));
59 59 }
60 60
61 61 void ChartPresenter::handleGeometryChanged()
62 62 {
63 63 m_rect = QRectF(QPoint(0,0),m_chart->size());
64 64 m_rect.adjust(m_marginSize,m_marginSize, -m_marginSize, -m_marginSize);
65 65 Q_ASSERT(m_rect.isValid());
66 66 emit geometryChanged(m_rect);
67 67 }
68 68
69 69 int ChartPresenter::margin() const
70 70 {
71 71 return m_marginSize;
72 72 }
73 73
74 74 void ChartPresenter::setMargin(int margin)
75 75 {
76 76 m_marginSize = margin;
77 77 }
78 78
79 79 void ChartPresenter::handleSeriesAdded(QChartSeries* series)
80 80 {
81 81 switch(series->type())
82 82 {
83 83 case QChartSeries::SeriesTypeLine: {
84 84 QLineChartSeries* lineSeries = static_cast<QLineChartSeries*>(series);
85 85 LineChartItem* item = new LineChartAnimationItem(this,lineSeries,m_chart);
86 86 m_chartTheme->decorate(item,lineSeries,m_chartItems.count());
87 87 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
88 88 QObject::connect(m_dataset,SIGNAL(domainChanged(const Domain&)),item,SLOT(handleDomainChanged(const Domain&)));
89 89 QObject::connect(lineSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int)));
90 90 m_chartItems.insert(series,item);
91 91 break;
92 92 }
93 93
94 94 case QChartSeries::SeriesTypeBar: {
95 95 BarChartSeries* barSeries = static_cast<BarChartSeries*>(series);
96 96 BarGroup* item = new BarGroup(*barSeries,m_chart);
97 97 m_chartTheme->decorate(item,barSeries,m_chartItems.count());
98 98 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
99 99 QObject::connect(m_dataset,SIGNAL(domainChanged(const Domain&)),item,SLOT(handleDomainChanged(const Domain&)));
100 100 QObject::connect(barSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int)));
101 101 m_chartItems.insert(series,item);
102 102 // m_axisXItem->setVisible(false);
103 103 break;
104 104 }
105 105
106 106 case QChartSeries::SeriesTypeStackedBar: {
107 107
108 108 StackedBarChartSeries* stackedBarSeries = static_cast<StackedBarChartSeries*>(series);
109 109 StackedBarGroup* item = new StackedBarGroup(*stackedBarSeries,m_chart);
110 110 m_chartTheme->decorate(item,stackedBarSeries,m_chartItems.count());
111 111 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
112 112 QObject::connect(m_dataset,SIGNAL(domainChanged(const Domain&)),item,SLOT(handleDomainChanged(const Domain&)));
113 113 QObject::connect(stackedBarSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int)));
114 114 m_chartItems.insert(series,item);
115 115 break;
116 116 }
117 117
118 118 case QChartSeries::SeriesTypePercentBar: {
119 119
120 120 PercentBarChartSeries* percentBarSeries = static_cast<PercentBarChartSeries*>(series);
121 121 PercentBarGroup* item = new PercentBarGroup(*percentBarSeries,m_chart);
122 122 m_chartTheme->decorate(item,percentBarSeries ,m_chartItems.count());
123 123 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
124 124 QObject::connect(m_dataset,SIGNAL(domainChanged(const Domain&)),item,SLOT(handleDomainChanged(const Domain&)));
125 125 QObject::connect(percentBarSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int)));
126 126 m_chartItems.insert(series,item);
127 127 break;
128 128 }
129 129 /*
130 130 case QChartSeries::SeriesTypeScatter: {
131 131 QScatterSeries *scatterSeries = qobject_cast<QScatterSeries *>(series);
132 132 scatterSeries->d->m_theme = m_chartTheme->themeForSeries();
133 133 scatterSeries->d->setParentItem(this);
134 134 scatterSeries->d->m_boundingRect = m_rect.adjusted(margin(),margin(), -margin(), -margin());
135 135 m_chartItems << scatterSeries->d;
136 136 m_chartTheme->addObserver(scatterSeries->d);
137 137
138 138 foreach (qreal x, scatterSeries->d->m_x) {
139 139 domain.m_minX = qMin(domain.m_minX, x);
140 140 domain.m_maxX = qMax(domain.m_maxX, x);
141 141 }
142 142 foreach (qreal y, scatterSeries->d->m_y) {
143 143 domain.m_minY = qMin(domain.m_minY, y);
144 144 domain.m_maxY = qMax(domain.m_maxY, y);
145 145 }
146 146
147 147 break;
148 148 }
149 149 */
150 150
151 151 case QChartSeries::SeriesTypePie: {
152 152 QPieSeries *pieSeries = qobject_cast<QPieSeries *>(series);
153 153 PiePresenter* pie = new PiePresenter(m_chart, pieSeries);
154 154 pieSeries->m_piePresenter = pie; // TODO: remove this pointer passing use signals&slots
155 155 QObject::connect(this, SIGNAL(geometryChanged(const QRectF&)), pie, SLOT(handleGeometryChanged(const QRectF&)));
156 156 QObject::connect(m_dataset, SIGNAL(domainChanged(const Domain&)), pie, SLOT(handleDomainChanged(const Domain&)));
157 157 m_chartItems.insert(series, pie);
158 158 break;
159 159 }
160 160
161 161 default: {
162 162 qDebug()<< "Series type" << series->type() << "not implemented.";
163 163 break;
164 164 }
165 165 }
166 166
167 167 if(m_rect.isValid()) emit geometryChanged(m_rect);
168 168 }
169 169
170 170 void ChartPresenter::handleSeriesChanged(QChartSeries* series)
171 171 {
172 172 //TODO:
173 173 }
174 174
175 175 void ChartPresenter::zoomInToRect(const QRectF& rect)
176 176 {
177 177 if(!rect.isValid()) return;
178 178 QRectF r = rect.normalized();
179 179 r.translate(-m_marginSize, -m_marginSize);
180 180 Domain domain (m_dataset->domain().subDomain(r,m_rect.width(),m_rect.height()));
181 181 m_dataset->addDomain(domain);
182 182 }
183 183
184 184 void ChartPresenter::zoomIn()
185 185 {
186 186 if (!m_dataset->nextDomain()) {
187 187 QRectF rect = m_rect;
188 188 rect.setWidth(rect.width()/2);
189 189 rect.setHeight(rect.height()/2);
190 190 rect.moveCenter(m_rect.center());
191 191 Domain domain (m_dataset->domain().subDomain(rect,m_rect.width(),m_rect.height()));
192 192 m_dataset->addDomain(domain);
193 193 }
194 194 }
195 195
196 196 void ChartPresenter::zoomOut()
197 197 {
198 198 m_dataset->previousDomain();
199 199 }
200 200
201 201 void ChartPresenter::zoomReset()
202 202 {
203 203 m_dataset->clearDomains();
204 204 }
205 205
206 void ChartPresenter::setTheme(QChart::ChartThemeId theme)
206 void ChartPresenter::setChartTheme(QChart::ChartTheme theme)
207 207 {
208 208 delete m_chartTheme;
209 209
210 210 m_chartTheme = ChartTheme::createTheme(theme);
211 211
212 212 m_chartTheme->decorate(m_chart);
213 213 QMapIterator<QChartSeries*,ChartItem*> i(m_chartItems);
214 214
215 215 int index=0;
216 216 while (i.hasNext()) {
217 217 i.next();
218 218 index++;
219 219 m_chartTheme->decorate(i.value(),i.key(),index);
220 220 }
221 221 }
222 222
223 223
224 QChart::ChartThemeId ChartPresenter::theme()
224 QChart::ChartTheme ChartPresenter::chartTheme()
225 225 {
226 return (QChart::ChartThemeId) 0;
226 return m_chartTheme->id();
227 }
228
229 void ChartPresenter::setAxisX(QChartAxis* axis)
230 {
231
232 }
233
234 void ChartPresenter::addAxisY(QChartAxis* axis)
235 {
236
227 237 }
228 238
229 239 /*
230 240 void ChartPresenter::setAxisX(const QChartAxis& axis)
231 241 {
232 242 setAxis(m_axisXItem,axis);
233 243 }
234 244 void ChartPresenter::setAxisY(const QChartAxis& axis)
235 245 {
236 246 setAxis(m_axisYItem.at(0),axis);
237 247 }
238 248
239 249 void ChartPresenter::setAxisY(const QList<QChartAxis>& axis)
240 250 {
241 251 //TODO not implemented
242 252 }
243 253
244 254 void ChartPresenter::setAxis(AxisItem *item, const QChartAxis& axis)
245 255 {
246 256 item->setVisible(axis.isAxisVisible());
247 257 }
248 258 */
249 259 #include "moc_chartpresenter_p.cpp"
250 260
251 261 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,72 +1,67
1 1 #ifndef CHARTPRESENTER_H_
2 2 #define CHARTPRESENTER_H_
3 3
4 4 #include "qchartglobal.h"
5 5 #include "qchart.h" //becouse of QChart::ChartThemeId //TODO
6 6 #include <QRectF>
7 7
8 8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 9
10 10 class ChartItem;
11 11 class QChartSeries;
12 12 class ChartDataSet;
13 13 //class QChart;
14 14 class Domain;
15 15 class AxisItem;
16 16 class ChartTheme;
17 17
18 18 class ChartPresenter: public QObject
19 19 {
20 20 Q_OBJECT
21 21 public:
22 22 ChartPresenter(QChart* chart,ChartDataSet *dataset);
23 23 virtual ~ChartPresenter();
24 /*
25 void setAxisX(const QChartAxis& axis);
26 void setAxisY(const QChartAxis& axis);
27 void setAxisY(const QList<QChartAxis>& axis);
28
29
30 */
31 24
32 25 void setMargin(int margin);
33 26 int margin() const;
34 27
35 28 void zoomInToRect(const QRectF& rectangle);
36 29 void zoomIn();
37 30 void zoomOut();
38 31 void zoomReset();
39 32
40 void setTheme(QChart::ChartThemeId theme);
41 QChart::ChartThemeId theme();
33 void setChartTheme(QChart::ChartTheme theme);
34 QChart::ChartTheme chartTheme();
42 35
36 void setAxisX(QChartAxis* axis);
37 void addAxisY(QChartAxis* axis);
43 38 private:
44 39 void createConnections();
45 40 void createDeafultAxis();
46 41
47 42 public slots:
48 43 void handleSeriesAdded(QChartSeries* series);
49 44 void handleSeriesRemoved(QChartSeries* series){};
50 45 void handleSeriesChanged(QChartSeries* series);
51 46 //void handleDomainChanged(Domain oldDomain,Domain newDomain);
52 47 void handleGeometryChanged();
53 48
54 49 signals:
55 50 void geometryChanged(const QRectF& rect);
56 51
57 52 private:
58 53 QMap<QChartSeries*,ChartItem*> m_chartItems;
59 54 QChart* m_chart;
60 55 ChartDataSet* m_dataset;
61 56 QVector<Domain> m_domains;
62 57 QList<AxisItem*> m_axis;
63 58 ChartTheme *m_chartTheme;
64 59 int m_domainIndex;
65 60 int m_marginSize;
66 61 QRectF m_rect;
67 62
68 63 };
69 64
70 65 QTCOMMERCIALCHART_END_NAMESPACE
71 66
72 67 #endif /* CHARTPRESENTER_H_ */
@@ -1,149 +1,150
1 1 #include "charttheme_p.h"
2 2 #include "qchart.h"
3 3
4 4
5 5 //series
6 6 #include "barchartseries.h"
7 7 #include "stackedbarchartseries.h"
8 8 #include "percentbarchartseries.h"
9 9 #include "qlinechartseries.h"
10 10 //items
11 11 #include "axisitem_p.h"
12 12 #include "bargroup.h"
13 13 #include "stackedbargroup.h"
14 14 #include "linechartitem_p.h"
15 15 #include "percentbargroup.h"
16 16
17 17 //themes
18 18 #include "chartthemevanilla_p.h"
19 19 #include "chartthemeicy_p.h"
20 20 #include "chartthemegrayscale_p.h"
21 21 #include "chartthemescientific_p.h"
22 22
23 23
24 24 QTCOMMERCIALCHART_BEGIN_NAMESPACE
25 25
26 26 /* TODO
27 27 case QChart::ChartThemeUnnamed1:
28 28 m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xff3fa9f5)), 2));
29 29 m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xff7AC943)), 2));
30 30 m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xffFF931E)), 2));
31 31 m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xffFF1D25)), 2));
32 32 m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xffFF7BAC)), 2));
33 33
34 34 m_gradientStartColor = QColor(QRgb(0xfff3dc9e));
35 35 m_gradientEndColor = QColor(QRgb(0xffafafaf));
36 36 */
37 37
38 ChartTheme::ChartTheme()
38 ChartTheme::ChartTheme(QChart::ChartTheme id)
39 39 {
40 m_id = id;
40 41 m_seriesColor.append(QRgb(0xff000000));
41 42 m_seriesColor.append(QRgb(0xff707070));
42 43 m_gradientStartColor = QColor(QRgb(0xffffffff));
43 44 m_gradientEndColor = QColor(QRgb(0xffafafaf));
44 45 }
45 46
46 47
47 ChartTheme* ChartTheme::createTheme(QChart::ChartThemeId theme)
48 ChartTheme* ChartTheme::createTheme(QChart::ChartTheme theme)
48 49 {
49 50 switch(theme) {
50 51 case QChart::ChartThemeDefault:
51 52 return new ChartTheme();
52 53 case QChart::ChartThemeVanilla:
53 54 return new ChartThemeVanilla();
54 55 case QChart::ChartThemeIcy:
55 56 return new ChartThemeIcy();
56 57 case QChart::ChartThemeGrayscale:
57 58 return new ChartThemeGrayscale();
58 59 case QChart::ChartThemeScientific:
59 60 return new ChartThemeScientific();
60 61 }
61 62 }
62 63
63 64 void ChartTheme::decorate(QChart* chart)
64 65 {
65 66 QLinearGradient backgroundGradient;
66 67 backgroundGradient.setColorAt(0.0, m_gradientStartColor);
67 68 backgroundGradient.setColorAt(1.0, m_gradientEndColor);
68 69 backgroundGradient.setCoordinateMode(QGradient::ObjectBoundingMode);
69 70 chart->setChartBackgroundBrush(backgroundGradient);
70 71 }
71 72 //TODO helper to by removed later
72 73 void ChartTheme::decorate(ChartItem* item, QChartSeries* series,int count)
73 74 {
74 75 switch(series->type())
75 76 {
76 77 case QChartSeries::SeriesTypeLine: {
77 78 QLineChartSeries* s = static_cast<QLineChartSeries*>(series);
78 79 LineChartItem* i = static_cast<LineChartItem*>(item);
79 80 decorate(i,s,count);
80 81 break;
81 82 }
82 83 case QChartSeries::SeriesTypeBar: {
83 84 BarChartSeries* b = static_cast<BarChartSeries*>(series);
84 85 BarGroup* i = static_cast<BarGroup*>(item);
85 86 decorate(i,b,count);
86 87 break;
87 88 }
88 89 case QChartSeries::SeriesTypeStackedBar: {
89 90 StackedBarChartSeries* s = static_cast<StackedBarChartSeries*>(series);
90 91 StackedBarGroup* i = static_cast<StackedBarGroup*>(item);
91 92 decorate(i,s,count);
92 93 break;
93 94 }
94 95 case QChartSeries::SeriesTypePercentBar: {
95 96 PercentBarChartSeries* s = static_cast<PercentBarChartSeries*>(series);
96 97 PercentBarGroup* i = static_cast<PercentBarGroup*>(item);
97 98 decorate(i,s,count);
98 99 break;
99 100 }
100 101 default:
101 102 qDebug()<<"Wrong item to be decorated by theme";
102 103 break;
103 104 }
104 105
105 106 }
106 107
107 108 void ChartTheme::decorate(LineChartItem* item, QLineChartSeries* series,int count)
108 109 {
109 110 QPen pen;
110 111 if(pen != series->pen()){
111 112 item->setPen(series->pen());
112 113 return;
113 114 }
114 115 pen.setColor(m_seriesColor.at(count%m_seriesColor.size()));
115 116 pen.setWidthF(2);
116 117 item->setPen(pen);
117 118 }
118 119
119 120 void ChartTheme::decorate(BarGroup* item, BarChartSeries* series,int count)
120 121 {
121 122 item->addColor(QColor(255,0,0,128));
122 123 item->addColor(QColor(255,255,0,128));
123 124 item->addColor(QColor(0,255,0,128));
124 125 item->addColor(QColor(0,0,255,128));
125 126 item->addColor(QColor(255,128,0,128));
126 127 }
127 128
128 129 void ChartTheme::decorate(StackedBarGroup* item, StackedBarChartSeries* series,int count)
129 130 {
130 131 // Add some fugly colors for 5 fist series...
131 132 item->addColor(QColor(255,0,0,128));
132 133 item->addColor(QColor(255,255,0,128));
133 134 item->addColor(QColor(0,255,0,128));
134 135 item->addColor(QColor(0,0,255,128));
135 136 item->addColor(QColor(255,128,0,128));
136 137 }
137 138
138 139 void ChartTheme::decorate(PercentBarGroup* item, PercentBarChartSeries* series,int count)
139 140 {
140 141 // Add some fugly colors for 5 fist series...
141 142 item->addColor(QColor(255,0,0,128));
142 143 item->addColor(QColor(255,255,0,128));
143 144 item->addColor(QColor(0,255,0,128));
144 145 item->addColor(QColor(0,0,255,128));
145 146 item->addColor(QColor(255,128,0,128));
146 147 }
147 148
148 149
149 150 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,43 +1,45
1 1 #ifndef CHARTTHEME_H
2 2 #define CHARTTHEME_H
3 3
4 4 #include "qchartglobal.h"
5 5 #include "qchart.h"
6 6 #include <QColor>
7 7
8 8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 9
10 10 class ChartItem;
11 11 class QChartSeries;
12 12 class LineChartItem;
13 13 class QLineChartSeries;
14 14 class BarGroup;
15 15 class BarChartSeries;
16 16 class StackedBarGroup;
17 17 class StackedBarChartSeries;
18 18 class PercentBarChartSeries;
19 19 class PercentBarGroup;
20 20
21 21 class ChartTheme
22 22 {
23 23 protected:
24 explicit ChartTheme();
24 explicit ChartTheme(QChart::ChartTheme id = QChart::ChartThemeDefault);
25 25 public:
26 static ChartTheme* createTheme(QChart::ChartThemeId theme);
26 static ChartTheme* createTheme(QChart::ChartTheme theme);
27 QChart::ChartTheme id() const {return m_id;}
27 28 void decorate(QChart* chart);
28 29 void decorate(ChartItem* item, QChartSeries* series,int count);
29 30 void decorate(LineChartItem* item, QLineChartSeries*, int count);
30 31 void decorate(BarGroup* item, BarChartSeries* series,int count);
31 32 void decorate(StackedBarGroup* item, StackedBarChartSeries* series,int count);
32 33 void decorate(PercentBarGroup* item, PercentBarChartSeries* series,int count);
33 34
34 35 protected:
36 QChart::ChartTheme m_id;
35 37 QColor m_gradientStartColor;
36 38 QColor m_gradientEndColor;
37 39 QList<QColor> m_seriesColor;
38 40
39 41 };
40 42
41 43 QTCOMMERCIALCHART_END_NAMESPACE
42 44
43 45 #endif // CHARTTHEME_H
@@ -1,190 +1,180
1 1 #include "qchart.h"
2 2 #include "qscatterseries.h"
3 3 #include "qscatterseries_p.h"
4 4 #include "qpieseries.h"
5 5 #include "qchartaxis.h"
6 6 #include "chartpresenter_p.h"
7 7 #include "chartdataset_p.h"
8 8
9 9 //series
10 10 #include "barchartseries.h"
11 11 #include "stackedbarchartseries.h"
12 12 #include "percentbarchartseries.h"
13 13 #include "qlinechartseries.h"
14 14
15 15 #include <QGraphicsScene>
16 16 #include <QGraphicsSceneResizeEvent>
17 17 #include <QDebug>
18 18
19 19 QTCOMMERCIALCHART_BEGIN_NAMESPACE
20 20
21 21 QChart::QChart(QGraphicsItem *parent, Qt::WindowFlags wFlags) : QGraphicsWidget(parent,wFlags),
22 22 m_backgroundItem(0),
23 23 m_titleItem(0),
24 24 m_dataset(new ChartDataSet(this)),
25 25 m_presenter(new ChartPresenter(this,m_dataset))
26 26 {
27 27 }
28 28
29 29 QChart::~QChart() {}
30 30
31 31 void QChart::addSeries(QChartSeries* series)
32 32 {
33 33 m_dataset->addSeries(series);
34 34 }
35 35
36 36 //TODO on review, is it really needed ??
37 37 QChartSeries* QChart::createSeries(QChartSeries::QChartSeriesType type)
38 38 {
39 39 // TODO: support also other types; not only scatter and pie
40 40
41 41 QChartSeries *series(0);
42 42
43 43 switch (type) {
44 44 case QChartSeries::SeriesTypeLine: {
45 45 series = QLineChartSeries::create();
46 46 break;
47 47 }
48 48 case QChartSeries::SeriesTypeBar: {
49 49 series = new BarChartSeries(this);
50 50 break;
51 51 }
52 52 case QChartSeries::SeriesTypeStackedBar: {
53 53 series = new StackedBarChartSeries(this);
54 54 break;
55 55 }
56 56 case QChartSeries::SeriesTypePercentBar: {
57 57 series = new PercentBarChartSeries(this);
58 58 break;
59 59 }
60 60 case QChartSeries::SeriesTypeScatter: {
61 61 series = new QScatterSeries(this);
62 62 break;
63 63 }
64 64 case QChartSeries::SeriesTypePie: {
65 65 series = new QPieSeries(this);
66 66 break;
67 67 }
68 68 default:
69 69 Q_ASSERT(false);
70 70 break;
71 71 }
72 72
73 73 addSeries(series);
74 74 return series;
75 75 }
76 76
77 77 void QChart::setChartBackgroundBrush(const QBrush& brush)
78 78 {
79 79
80 80 if(!m_backgroundItem) {
81 81 m_backgroundItem = new QGraphicsRectItem(this);
82 82 m_backgroundItem->setZValue(-1);
83 83 }
84 84
85 85 m_backgroundItem->setBrush(brush);
86 86 m_backgroundItem->update();
87 87 }
88 88
89 89 void QChart::setChartBackgroundPen(const QPen& pen)
90 90 {
91 91
92 92 if(!m_backgroundItem) {
93 93 m_backgroundItem = new QGraphicsRectItem(this);
94 94 m_backgroundItem->setZValue(-1);
95 95 }
96 96
97 97 m_backgroundItem->setPen(pen);
98 98 m_backgroundItem->update();
99 99 }
100 100
101 101 void QChart::setTitle(const QString& title,const QFont& font)
102 102 {
103 103 if(!m_titleItem) m_titleItem = new QGraphicsTextItem(this);
104 104 m_titleItem->setPlainText(title);
105 105 m_titleItem->setFont(font);
106 106 }
107 107
108 108 int QChart::margin() const
109 109 {
110 110 return m_presenter->margin();
111 111 }
112 112
113 113 void QChart::setMargin(int margin)
114 114 {
115 115 m_presenter->setMargin(margin);
116 116 }
117 117
118 void QChart::setTheme(QChart::ChartThemeId theme)
118 void QChart::setChartTheme(QChart::ChartTheme theme)
119 119 {
120 m_presenter->setTheme(theme);
120 m_presenter->setChartTheme(theme);
121 121 }
122 122
123 QChart::ChartThemeId QChart::theme()
123 QChart::ChartTheme QChart::chartTheme() const
124 124 {
125 return m_presenter->theme();
125 return m_presenter->chartTheme();
126 126 }
127 127
128 128 void QChart::zoomInToRect(const QRectF& rectangle)
129 129 {
130 130 m_presenter->zoomInToRect(rectangle);
131 131 }
132 132
133 133 void QChart::zoomIn()
134 134 {
135 135 m_presenter->zoomIn();
136 136 }
137 137
138 138 void QChart::zoomOut()
139 139 {
140 140 m_presenter->zoomOut();
141 141 }
142 142
143 143 void QChart::zoomReset()
144 144 {
145 145 m_presenter->zoomReset();
146 146 }
147 147
148 void QChart::setAxisX(const QChartAxis& axis)
148 void QChart::setAxisX(QChartAxis* axis)
149 149 {
150
150 m_presenter->setAxisX(axis);
151 151 }
152 void QChart::setAxisY(const QChartAxis& axis)
153 {
154
155 }
156
157 void QChart::setAxisY(const QList<QChartAxis>& axis)
152 void QChart::addAxisY(QChartAxis* axis)
158 153 {
159 //TODO not implemented
160 }
161
162 void QChart::setAxis(AxisItem *item, const QChartAxis& axis)
163 {
164
154 m_presenter->addAxisY(axis);
165 155 }
166 156
167 157 void QChart::resizeEvent(QGraphicsSceneResizeEvent *event)
168 158 {
169 159
170 160 m_rect = QRectF(QPoint(0,0),event->newSize());
171 161 QRectF rect = m_rect.adjusted(margin(),margin(), -margin(), -margin());
172 162
173 163 // recalculate title position
174 164 if (m_titleItem) {
175 165 QPointF center = m_rect.center() -m_titleItem->boundingRect().center();
176 166 m_titleItem->setPos(center.x(),m_rect.top()/2 + margin()/2);
177 167 }
178 168
179 169 //recalculate background gradient
180 170 if (m_backgroundItem) {
181 171 m_backgroundItem->setRect(rect);
182 172 }
183 173
184 174 QGraphicsWidget::resizeEvent(event);
185 175 update();
186 176 }
187 177
188 178 #include "moc_qchart.cpp"
189 179
190 180 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,92 +1,90
1 1 #ifndef CHART_H
2 2 #define CHART_H
3 3
4 4 #include <qchartglobal.h>
5 5 #include <qchartseries.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 QChartSeries;
16 16 class PlotDomain;
17 17 class BarGroup;
18 18 class QChartAxis;
19 19 class ChartTheme;
20 20 class ChartItem;
21 21 class ChartDataSet;
22 22 class ChartPresenter;
23 23
24 24 // TODO: We don't need to have QChart tied to QGraphicsItem:
25 25 //class QTCOMMERCIALCHART_EXPORT QChart
26 26 //class QTCOMMERCIALCHART_EXPORT QChartGraphicsItem : public QGraphicsItem {
27 27 // public: QChartGraphicsItem(QChart &chart);
28 28
29 29 /*!
30 30 * TODO: define the responsibilities
31 31 */
32 32 class QTCOMMERCIALCHART_EXPORT QChart : public QGraphicsWidget
33 33 {
34 34 Q_OBJECT
35 35 public:
36 enum ChartThemeId {
37 ChartThemeInvalid = -1,
36 enum ChartTheme {
38 37 /*! The default theme follows the GUI style of the Operating System */
39 38 ChartThemeDefault,
40 39 ChartThemeVanilla,
41 40 ChartThemeIcy,
42 41 ChartThemeGrayscale,
43 42 ChartThemeScientific,
44 43 //ChartThemeUnnamed1
45 44 };
46 45
47 46 public:
48 47 QChart(QGraphicsItem *parent = 0, Qt::WindowFlags wFlags = 0);
49 48 ~QChart();
50 49
51 50 void addSeries(QChartSeries* series);
52 51
53 52 //TODO: QChartSeries* createSeries(QSeriesData *data, QChartSeries::QChartSeriesType type);
54 53 // TODO: who owns the series now? maybe owned by chart and returned a reference instead...
55 54 QChartSeries* createSeries(QChartSeries::QChartSeriesType type);
56 55
57 56 void setMargin(int margin);
58 57 int margin() const;
59 void setTheme(QChart::ChartThemeId theme);
60 QChart::ChartThemeId theme();
58 void setChartTheme(QChart::ChartTheme theme);
59 QChart::ChartTheme chartTheme() const;
61 60
62 61 void setTitle(const QString& title,const QFont& font = QFont());
63 62 void setChartBackgroundBrush(const QBrush& brush);
64 63 void setChartBackgroundPen(const QPen& pen);
65 64
66 65 void zoomInToRect(const QRectF& rectangle);
67 66 void zoomIn();
68 67 void zoomOut();
69 68 void zoomReset();
70 69
71 void setAxisX(const QChartAxis& axis);
72 void setAxisY(const QChartAxis& axis);
73 void setAxisY(const QList<QChartAxis>& axis);
70 void setAxisX(QChartAxis* axis);
71 void addAxisY(QChartAxis* axis);
74 72
75 73 protected:
76 74 void resizeEvent(QGraphicsSceneResizeEvent *event);
77 75
78 76 private:
79 77 void setAxis(AxisItem *item, const QChartAxis& axis);
80 78
81 79 private:
82 80 Q_DISABLE_COPY(QChart)
83 81 QGraphicsRectItem* m_backgroundItem;
84 82 QGraphicsTextItem* m_titleItem;
85 83 QRectF m_rect;
86 84 ChartDataSet *m_dataset;
87 85 ChartPresenter *m_presenter;
88 86 };
89 87
90 88 QTCOMMERCIALCHART_END_NAMESPACE
91 89
92 90 #endif
@@ -1,198 +1,202
1 1 #include "qchartview.h"
2 2 #include "qchart.h"
3 3 #include <QGraphicsView>
4 4 #include <QGraphicsScene>
5 5 #include <QRubberBand>
6 6 #include <QResizeEvent>
7 7 #include <QDebug>
8 8
9 9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10 10
11 11 QChartView::QChartView(QWidget *parent) :
12 12 QGraphicsView(parent),
13 13 m_scene(new QGraphicsScene()),
14 14 m_chart(new QChart()),
15 15 m_rubberBand(0),
16 16 m_verticalRubberBand(false),
17 17 m_horizonalRubberBand(false)
18 18 {
19 19 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
20 20 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
21 21 setScene(m_scene);
22 22 m_chart->setMargin(50);
23 23 m_scene->addItem(m_chart);
24 24 setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
25 25 }
26 26
27 27 QChartView::~QChartView()
28 28 {
29 29 }
30 30
31 31 void QChartView::resizeEvent(QResizeEvent *event)
32 32 {
33 33 m_scene->setSceneRect(0,0,size().width(),size().height());
34 34 m_chart->resize(size());
35 35 QWidget::resizeEvent(event);
36 36 }
37 37
38 38
39 39 void QChartView::addSeries(QChartSeries* series)
40 40 {
41 41 m_chart->addSeries(series);
42 42 }
43 43
44 44 QChartSeries* QChartView::createSeries(QChartSeries::QChartSeriesType type)
45 45 {
46 46
47 47 return m_chart->createSeries(type);
48 48 }
49 49
50 50 void QChartView::zoomInToRect(const QRect& rectangle)
51 51 {
52 52 m_chart->zoomInToRect(rectangle);
53 53 }
54 54
55 55 void QChartView::zoomIn()
56 56 {
57 57 m_chart->zoomIn();
58 58 }
59 59
60 60 void QChartView::zoomOut()
61 61 {
62 62 m_chart->zoomOut();
63 63 }
64 64
65 65 int QChartView::margin() const
66 66 {
67 67 return m_chart->margin();
68 68 }
69 69
70 70 void QChartView::setTitle(const QString& title)
71 71 {
72 72 m_chart->setTitle(title);
73 73 }
74 74
75 75 void QChartView::setChartBackgroundBrush(const QBrush& brush)
76 76 {
77 77 m_chart->setChartBackgroundBrush(brush);
78 78 }
79 79 void QChartView::setChartBackgroundPen(const QPen& pen)
80 80 {
81 81 m_chart->setChartBackgroundPen(pen);
82 82 }
83 83
84 84
85 85 void QChartView::setRubberBandPolicy(const RubberBandPolicy policy)
86 86 {
87 87 switch(policy){
88 88 case VerticalRubberBand:
89 89 m_verticalRubberBand = true;
90 90 m_horizonalRubberBand = false;
91 91 break;
92 92 case HorizonalRubberBand:
93 93 m_verticalRubberBand = false;
94 94 m_horizonalRubberBand = true;
95 95 break;
96 96 case RectangleRubberBand:
97 97 m_verticalRubberBand = true;
98 98 m_horizonalRubberBand = true;
99 99 break;
100 100 case NoRubberBand:
101 101 default:
102 102 delete m_rubberBand;
103 103 m_rubberBand=0;
104 104 m_horizonalRubberBand = false;
105 105 m_verticalRubberBand = false;
106 106 return;
107 107 }
108 108 if(!m_rubberBand){
109 109 m_rubberBand = new QRubberBand(QRubberBand::Rectangle, this);
110 110 m_rubberBand->setEnabled(true);
111 111 }
112 112 }
113 113
114 114 QChartView::RubberBandPolicy QChartView::rubberBandPolicy() const
115 115 {
116 116 if(m_horizonalRubberBand && m_verticalRubberBand) return RectangleRubberBand;
117 117 if(m_horizonalRubberBand) return HorizonalRubberBand;
118 118 if(m_verticalRubberBand) return VerticalRubberBand;
119 119 return NoRubberBand;
120 120 }
121 121
122 122
123 123 void QChartView::mousePressEvent(QMouseEvent *event)
124 124 {
125 125 if(m_rubberBand && m_rubberBand->isEnabled() && event->button() == Qt::LeftButton) {
126 126
127 127 int margin = m_chart->margin();
128 128 QRect rect(margin, margin, width() - 2 * margin, height() - 2 * margin);
129 129
130 130 if (rect.contains(event->pos())) {
131 131 m_rubberBandOrigin = event->pos();
132 132 m_rubberBand->setGeometry(QRect(m_rubberBandOrigin, QSize()));
133 133 m_rubberBand->show();
134 134 event->accept();
135 135 }
136 136 }
137 137 }
138 138
139 139 void QChartView::mouseMoveEvent(QMouseEvent *event)
140 140 {
141 141 if(m_rubberBand && m_rubberBand->isVisible()){
142 142 int margin = m_chart->margin();
143 143 QRect rect(margin, margin, width() - 2 * margin, height() - 2 * margin);
144 144 int width = event->pos().x() - m_rubberBandOrigin.x();
145 145 int height = event->pos().y() - m_rubberBandOrigin.y();
146 146 if(!m_verticalRubberBand) {
147 147 m_rubberBandOrigin.setY(rect.top());
148 148 height = rect.height();
149 149 }
150 150 if(!m_horizonalRubberBand) {
151 151 m_rubberBandOrigin.setX(rect.left());
152 152 width= rect.width();
153 153 }
154 154 m_rubberBand->setGeometry(QRect(m_rubberBandOrigin.x(),m_rubberBandOrigin.y(), width,height).normalized());
155 155 } else {
156 156 QGraphicsView::mouseMoveEvent(event);
157 157 }
158 158 }
159 159
160 160 void QChartView::mouseReleaseEvent(QMouseEvent *event)
161 161 {
162 162 if(m_rubberBand){
163 163 if (event->button() == Qt::LeftButton && m_rubberBand->isVisible()) {
164 164 m_rubberBand->hide();
165 165 QRect rect = m_rubberBand->geometry();
166 166 m_chart->zoomInToRect(rect);
167 167 event->accept();
168 168 }
169 169
170 170 if(event->button()==Qt::RightButton)
171 171 m_chart->zoomReset();
172 172 }else{
173 173 QGraphicsView::mouseReleaseEvent(event);
174 174 }
175 175 }
176 176
177 177 void QChartView::keyPressEvent(QKeyEvent *event)
178 178 {
179 179 switch (event->key()) {
180 180 case Qt::Key_Plus:
181 181 zoomIn();
182 182 break;
183 183 case Qt::Key_Minus:
184 184 zoomOut();
185 185 break;
186 186 default:
187 187 QGraphicsView::keyPressEvent(event);
188 188 break;
189 189 }
190 190 }
191 191
192 void QChartView::setTheme(QChart::ChartThemeId theme)
192 void QChartView::setChartTheme(QChart::ChartTheme theme)
193 193 {
194 m_chart->setTheme(theme);
194 m_chart->setChartTheme(theme);
195 195 }
196 196
197 QChart::ChartTheme QChartView::chartTheme() const
198 {
199 return m_chart->chartTheme();
200 }
197 201
198 202 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,68 +1,69
1 1 #ifndef QCHARTWIDGET_H
2 2 #define QCHARTWIDGET_H
3 3
4 4 #include "qchartglobal.h"
5 5 #include "qchartseries.h"
6 6 #include "qchart.h"
7 7 #include <QGraphicsView>
8 8
9 9 class QGraphicsScene;
10 10 class QRubberBand;
11 11
12 12 QTCOMMERCIALCHART_BEGIN_NAMESPACE
13 13
14 14 class QChart;
15 15
16 16 class QTCOMMERCIALCHART_EXPORT QChartView : public QGraphicsView
17 17 {
18 18 public:
19 19 enum RubberBandPolicy { NoRubberBand, VerticalRubberBand, HorizonalRubberBand, RectangleRubberBand };
20 20
21 21 explicit QChartView(QWidget *parent = 0);
22 22 ~QChartView();
23 23
24 24 //implement from QWidget
25 25 void resizeEvent(QResizeEvent *event);
26 26
27 27 void addSeries(QChartSeries* series);
28 28
29 29 // Convenience function
30 30 QChartSeries* createSeries(QChartSeries::QChartSeriesType type);
31 31
32 32 int margin() const;
33 33
34 34 void setTitle(const QString& title);
35 35 void setChartBackgroundBrush(const QBrush& brush);
36 36 void setChartBackgroundPen(const QPen& pen);
37 37
38 38 void zoomInToRect(const QRect& rectangle);
39 39 void zoomIn();
40 40 void zoomOut();
41 41
42 42 void setRubberBandPolicy(const RubberBandPolicy );
43 43 RubberBandPolicy rubberBandPolicy() const;
44 44
45 void setTheme(QChart::ChartThemeId theme);
45 void setChartTheme(QChart::ChartTheme theme);
46 QChart::ChartTheme chartTheme() const;
46 47
47 48 protected:
48 49 void mousePressEvent(QMouseEvent *event);
49 50 void mouseMoveEvent(QMouseEvent *event);
50 51 void mouseReleaseEvent(QMouseEvent *event);
51 52 void keyPressEvent(QKeyEvent *event);
52 53
53 54
54 55 private:
55 56 QGraphicsScene *m_scene;
56 57 QChart* m_chart;
57 58 QPoint m_rubberBandOrigin;
58 59 QRubberBand* m_rubberBand;
59 60 bool m_verticalRubberBand;
60 61 bool m_horizonalRubberBand;
61 62 Q_DISABLE_COPY(QChartView)
62 63
63 64
64 65 };
65 66
66 67 QTCOMMERCIALCHART_END_NAMESPACE
67 68
68 69 #endif // QCHARTWIDGET_H
@@ -1,20 +1,20
1 1 #include "charttheme_p.h"
2 2
3 3 QTCOMMERCIALCHART_BEGIN_NAMESPACE
4 4
5 5 class ChartThemeGrayscale: public ChartTheme
6 6 {
7 7 public:
8 ChartThemeGrayscale()
8 ChartThemeGrayscale():ChartTheme(QChart::ChartThemeGrayscale)
9 9 {
10 10 m_seriesColor.append(QRgb(0xFF869299));
11 11 m_seriesColor.append(QRgb(0xFFA5BDCC));
12 12 m_seriesColor.append(QRgb(0xFFE8FFFC));
13 13 m_seriesColor.append(QRgb(0xFFCCC2C2));
14 14
15 15 m_gradientStartColor = QColor(QRgb(0xffffffff));
16 16 m_gradientEndColor = QColor(QRgb(0xffafafaf));
17 17 }
18 18 };
19 19
20 20 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,20 +1,20
1 1 #include "charttheme_p.h"
2 2
3 3 QTCOMMERCIALCHART_BEGIN_NAMESPACE
4 4
5 5 class ChartThemeIcy: public ChartTheme
6 6 {
7 7 public:
8 ChartThemeIcy()
8 ChartThemeIcy():ChartTheme(QChart::ChartThemeIcy)
9 9 {
10 10 m_seriesColor.append(QRgb(0xFF0D2673));
11 11 m_seriesColor.append(QRgb(0xFF2685BF));
12 12 m_seriesColor.append(QRgb(0xFF3DADD9));
13 13 m_seriesColor.append(QRgb(0xFF62C3D9));
14 14
15 15 m_gradientStartColor = QColor(QRgb(0xffBDE3F2));
16 16 m_gradientEndColor = QColor(QRgb(0xffafafaf));
17 17 }
18 18 };
19 19
20 20 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,20 +1,20
1 1 #include "charttheme_p.h"
2 2
3 3 QTCOMMERCIALCHART_BEGIN_NAMESPACE
4 4
5 5 class ChartThemeScientific: public ChartTheme
6 6 {
7 7 public:
8 ChartThemeScientific()
8 ChartThemeScientific():ChartTheme(QChart::ChartThemeScientific)
9 9 {
10 10 m_seriesColor.append(QRgb(0xFF000000));
11 11 m_seriesColor.append(QRgb(0xFFFFAD00));
12 12 m_seriesColor.append(QRgb(0xFF596A75));
13 13 m_seriesColor.append(QRgb(0xFF474747));
14 14
15 15 m_gradientStartColor = QColor(QRgb(0xffafafaf));
16 16 m_gradientEndColor = QColor(QRgb(0xffafafaf));
17 17 }
18 18 };
19 19
20 20 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,21 +1,21
1 1 #include "charttheme_p.h"
2 2
3 3 QTCOMMERCIALCHART_BEGIN_NAMESPACE
4 4
5 5 class ChartThemeVanilla: public ChartTheme
6 6 {
7 7 public:
8 ChartThemeVanilla()
8 ChartThemeVanilla():ChartTheme(QChart::ChartThemeVanilla)
9 9 {
10 10 m_seriesColor.append(QColor(217, 197, 116));
11 11 m_seriesColor.append(QColor(214, 168, 150));
12 12 m_seriesColor.append(QColor(160, 160, 113));
13 13 m_seriesColor.append(QColor(210, 210, 52));
14 14 m_seriesColor.append(QColor(136, 114, 58));
15 15
16 16 m_gradientStartColor = QColor(QRgb(0xff9d844d));
17 17 m_gradientEndColor = QColor(QRgb(0xffafafaf));
18 18 }
19 19 };
20 20
21 21 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now