##// END OF EJS Templates
Adds refactored axis to presenter
Michal Klocek -
r140:683f8b9c197b
parent child
Show More
@@ -1,164 +1,184
1 1 #include "axisitem_p.h"
2 #include "qchartaxis.h"
2 3 #include <QPainter>
3 4 #include <QDebug>
4 5
5 6 #define LABEL_PADDING 5
6 7
7 8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 9
9 AxisItem::AxisItem(AxisType type,QGraphicsItem* parent) :
10 AxisItem::AxisItem(QChartAxis* axis,AxisType type,QGraphicsItem* parent) :
10 11 ChartItem(parent),
12 m_axis(axis),
11 13 m_ticks(4),
12 14 m_type(type)
13 15 {
14 16 }
15 17
16 18 AxisItem::~AxisItem()
17 19 {
18 20 }
19 21
20 22 void AxisItem::setLength(int length)
21 23 {
22 24 QPainterPath path;
23 25 path.moveTo(QPointF(0,0));
24 26 path.lineTo(length,0);
25 27 // path.lineTo(length-4,0);
26 28 // path.lineTo(length,3);
27 29 // path.lineTo(length-4,6);
28 30 // path.lineTo(length-4,4);
29 31 // path.lineTo(0,4);
30 32 // path.lineTo(0,2);
31 33 m_path=path;
32 34 update();
33 35 }
34 36
35 37 QRectF AxisItem::boundingRect() const
36 38 {
37 39 return m_rect;
38 40 }
39 41
40 void AxisItem::setPlotDomain(const PlotDomain& plotDomain)
41 {
42 m_plotDomain = plotDomain;
43 createItems();
44 }
45
46 void AxisItem::setSize(const QSizeF &size)
47 {
48 m_rect = QRectF(QPoint(0,0),size);
49 createItems();
50 }
51 42
52 43 /*
53 44 void AxisItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,QWidget *widget)
54 45 {
55 46 if (!m_rect.isValid())
56 47 return;
57 48
58 49 if(m_type==X_AXIS) {
59 50
60 51 const qreal deltaX = m_rect.width() / m_ticks;
61 52
62 53 for (int i = 0; i <= m_ticks; ++i) {
63 54
64 55 int x = i * deltaX + m_rect.left();
65 56
66 57 if(i==0) x--;
67 58 if(i==m_ticks) x++;
68 59
69 60 qreal label = m_plotDomain.m_minX + (i * m_plotDomain.spanX()
70 61 / m_ticks);
71 62 painter->drawLine(x, m_rect.top()-1, x, m_rect.bottom()+1);
72 63 // painter->drawLine(x, m_rect.bottom()-1, x, m_rect.bottom()-1 + 5);
73 64
74 65 painter->drawText(x - 50, m_rect.bottom() + 5, 100, 20,Qt::AlignHCenter | Qt::AlignTop, QString::number(label));
75 66 }
76 67 }
77 68
78 69 if(m_type==Y_AXIS) {
79 70
80 71 const qreal deltaY = (m_rect.height()) / m_ticks;
81 72
82 73 for (int j = 0; j <= m_ticks; ++j) {
83 74
84 75 int y = j * -deltaY + m_rect.bottom();
85 76
86 77 if(j==0) y++;
87 78 if(j==m_ticks) y--;
88 79
89 80 qreal label = m_plotDomain.m_minY + (j * m_plotDomain.spanY()
90 81 / m_ticks);
91 82
92 83 painter->drawLine(m_rect.left()-1, y, m_rect.right()+1, y);
93 84 //painter->drawLine(m_rect.left() - 5, y, m_rect.left(), y);
94 85 //TODO : margin = 50 ;
95 86 painter->drawText(m_rect.left() - 50, y - 10, 50 - 5, 20,
96 87 Qt::AlignRight | Qt::AlignVCenter,
97 88 QString::number(label));
98 89 }
99 90 }
100 91
101 92 //painter->drawRect(m_rect.adjusted(0, 0, -1, -1));
102 93 }
103 94 */
104 95 void AxisItem::createItems()
105 96 {
106 97
107 //TODO: this is very inefficient handling
108
109 qDeleteAll(m_shades);
110 m_shades.clear();
111 qDeleteAll(m_grid);
112 m_grid.clear();
113 qDeleteAll(m_labels);
114 m_labels.clear();
115
98 if(!m_rect.isValid()) return;
116 99
117 100 if(m_type==X_AXIS) {
118 101
119 102 const qreal deltaX = m_rect.width() / m_ticks;
120 103
121 104 for (int i = 0; i <= m_ticks; ++i) {
122 105
123 106 int x = i * deltaX + m_rect.left();
124 107
125 qreal label = m_plotDomain.m_minX + (i * m_plotDomain.spanX()/ m_ticks);
108 qreal label = m_domain.m_minX + (i * m_domain.spanX()/ m_ticks);
126 109
127 110 m_grid<<new QGraphicsLineItem(x, m_rect.top(), x, m_rect.bottom(),this);
128 111
129 112 QGraphicsSimpleTextItem* text = new QGraphicsSimpleTextItem(QString::number(label),this);
130 113 QPointF center = text->boundingRect().center();
131 114 text->setPos(x - center.x(), m_rect.bottom() + LABEL_PADDING);
132 115 //text->rotate(-45);
133 116 m_labels<<text;
134 117 }
135 118 }
136 119
137 120 if(m_type==Y_AXIS) {
138 121
139 122 const qreal deltaY = m_rect.height()/ m_ticks;
140 123
141 124 for (int j = 0; j <= m_ticks; ++j) {
142 125
143 126 int y = j * -deltaY + m_rect.bottom();
144 127
145 qreal label = m_plotDomain.m_minY + (j * m_plotDomain.spanY()
128 qreal label = m_domain.m_minY + (j * m_domain.spanY()
146 129 / m_ticks);
147 130
148 131 m_grid<<new QGraphicsLineItem(m_rect.left() , y, m_rect.right(), y,this);
149 132 QGraphicsSimpleTextItem* text = new QGraphicsSimpleTextItem(QString::number(label),this);
150 133 QPointF center = text->boundingRect().center();
151 134
152 135 text->setPos(m_rect.left() - text->boundingRect().width() - LABEL_PADDING , y-center.y());
153 136 //text->rotate(-45);
154 137 m_labels<<text;
155 138
156 139 }
157 140 }
158 141
159 142 //painter->drawRect(m_rect.adjusted(0, 0, -1, -1));
160 143 }
161 144
145 void AxisItem::clear()
146 {
147 qDeleteAll(m_shades);
148 m_shades.clear();
149 qDeleteAll(m_grid);
150 m_grid.clear();
151 qDeleteAll(m_labels);
152 m_labels.clear();
153 }
154
155 void AxisItem::updateDomain()
156 {
157 clear();
158 createItems();
159 }
160
161 void AxisItem::handleAxisChanged()
162 {
163 //m_axis->
164 }
165
166 void AxisItem::handleDomainChanged(const Domain& domain)
167 {
168 m_domain = domain;
169 clear();
170 createItems();
171 }
172
173 void AxisItem::handleGeometryChanged(const QRectF& rect)
174 {
175 Q_ASSERT(rect.isValid());
176 m_rect = rect;
177 clear();
178 createItems();
179 }
180
162 181 //TODO "nice numbers algorithm"
182 #include "moc_axisitem_p.cpp"
163 183
164 184 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,47 +1,73
1 1 #ifndef AXISITEM_H_
2 2 #define AXISITEM_H_
3 3
4 #include "plotdomain_p.h"
4 #include "domain_p.h"
5 5 #include "chartitem_p.h"
6 6 #include <QGraphicsItem>
7 7
8 8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 9
10 class AxisItem : public ChartItem
10 class QChartAxis;
11
12 class AxisItem : public QObject, public ChartItem
11 13 {
14 Q_OBJECT
12 15 public:
13 16 enum AxisType{X_AXIS,Y_AXIS};
14 17
15 AxisItem(AxisType type = X_AXIS,QGraphicsItem* parent = 0);
18 AxisItem(QChartAxis* axis,AxisType type = X_AXIS,QGraphicsItem* parent = 0);
16 19 ~AxisItem();
17 20
18 21 //from QGraphicsItem
19 22 QRectF boundingRect() const;
20 23 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){};
21 24
25 protected slots:
26 void handleAxisChanged();
27 void handleDomainChanged(const Domain& domain);
28 void handleGeometryChanged(const QRectF& size);
29
30 protected:
31 void updateDomain();
32
33 private:
34 void clear();
35
36
22 37 public: // from ChartItem
23 void setSize(const QSizeF &size);
24 void setPlotDomain(const PlotDomain& data);
38 void setSize(const QSizeF &size){};
39 void setPlotDomain(const PlotDomain& data){};
25 40
26 41 public:
27 42 void setLength(int length);
28 43 void setWidth(int width);
29 44 AxisType axisType() const {return m_type;};
30 45
46 protected:
47
48
31 49 private:
32 50 void createItems();
33 51 private:
34 QRectF m_rect;
52
53 QChartAxis* m_axis;
54 AxisType m_type;
35 55 int m_ticks;
36 PlotDomain m_plotDomain;
56 Domain m_domain;
57
58
59
60 QRectF m_rect;
61
37 62 QPainterPath m_path;
38 63
64
39 65 QList<QGraphicsLineItem*> m_grid;
40 66 QList<QGraphicsRectItem*> m_shades;
41 67 QList<QGraphicsSimpleTextItem*> m_labels;
42 AxisType m_type;
68
43 69 };
44 70
45 71 QTCOMMERCIALCHART_END_NAMESPACE
46 72
47 73 #endif /* AXISITEM_H_ */
@@ -1,226 +1,243
1 1 #include "qchart.h"
2 #include "qchartaxis.h"
2 3 #include "chartpresenter_p.h"
3 4 #include "chartdataset_p.h"
4 5 //series
5 6 #include "barchartseries.h"
6 7 #include "stackedbarchartseries.h"
7 8 #include "percentbarchartseries.h"
8 9 #include "qxychartseries.h"
9 10 //items
11 #include "axisitem_p.h"
10 12 #include "bargroup.h"
11 13 #include "stackedbargroup.h"
12 14 #include "xylinechartitem_p.h"
13 15 #include "percentbargroup.h"
14 16 #include "linechartanimationitem_p.h"
15 17
16 18 #include <QAbstractAnimation>
17 19 #include <QPropertyAnimation>
18 20
19 21 QTCOMMERCIALCHART_BEGIN_NAMESPACE
20 22
21 23 ChartPresenter::ChartPresenter(QChart* chart,ChartDataSet* dataset):QObject(chart),
22 24 m_chart(chart),
23 25 m_dataset(dataset),
24 26 m_domainIndex(0),
25 27 m_marginSize(0),
26 28 m_rect(QRectF(QPoint(0,0),m_chart->size()))
27 29 {
28 creteConnections();
30 createConnections();
31 createDeafultAxis();
29 32 }
30 33
31 34 ChartPresenter::~ChartPresenter()
32 35 {
33 36 }
34 37
35 void ChartPresenter::creteConnections()
38 void ChartPresenter::createDeafultAxis()
39 {
40 //default axis
41 QChartAxis* axisX = new QChartAxis(this);
42 QChartAxis* axisY = new QChartAxis(this);
43
44 m_axis << new AxisItem(axisX,AxisItem::X_AXIS,m_chart);
45 m_axis << new AxisItem(axisY,AxisItem::Y_AXIS,m_chart);
46
47 foreach(AxisItem* item, m_axis) {
48 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
49 QObject::connect(m_dataset,SIGNAL(domainChanged(const Domain&)),item,SLOT(handleDomainChanged(const Domain&)));
50 }
51 }
52
53 void ChartPresenter::createConnections()
36 54 {
37 55 QObject::connect(m_chart,SIGNAL(geometryChanged()),this,SLOT(handleGeometryChanged()));
38 56 QObject::connect(m_dataset,SIGNAL(seriesAdded(QChartSeries*)),this,SLOT(handleSeriesAdded(QChartSeries*)));
39 57 }
40 58
41 59 void ChartPresenter::handleGeometryChanged()
42 60 {
43 61 m_rect = QRectF(QPoint(0,0),m_chart->size());
44 62 m_rect.adjust(m_marginSize,m_marginSize, -m_marginSize, -m_marginSize);
45 63 emit geometryChanged(m_rect);
46 64 }
47 65
48 66 int ChartPresenter::margin() const
49 67 {
50 68 return m_marginSize;
51 69 }
52 70
53 71 void ChartPresenter::setMargin(int margin)
54 72 {
55 73 m_marginSize = margin;
56 74 }
57 75
58 76 void ChartPresenter::handleSeriesAdded(QChartSeries* series)
59 77 {
60 78 switch(series->type())
61 79 {
62 80 case QChartSeries::SeriesTypeLine: {
63 81 QXYChartSeries* lineSeries = static_cast<QXYChartSeries*>(series);
64 82 XYLineChartItem* item = new LineChartAnimationItem(this,lineSeries,m_chart);
65 83 item->setPen(lineSeries->pen());
66 84 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
67 85 QObject::connect(m_dataset,SIGNAL(domainChanged(const Domain&)),item,SLOT(handleDomainChanged(const Domain&)));
68 86 QObject::connect(lineSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int)));
69 87 m_chartItems.insert(series,item);
70 88 break;
71 89 }
72 90
73 91 case QChartSeries::SeriesTypeBar: {
74 92 BarChartSeries* barSeries = static_cast<BarChartSeries*>(series);
75 93 BarGroup* item = new BarGroup(*barSeries,m_chart);
76 94
77 95 // Add some fugly colors for 5 fist series...
78 96 item->addColor(QColor(255,0,0,128));
79 97 item->addColor(QColor(255,255,0,128));
80 98 item->addColor(QColor(0,255,0,128));
81 99 item->addColor(QColor(0,0,255,128));
82 100 item->addColor(QColor(255,128,0,128));
83 101
84 102 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
85 103 QObject::connect(m_dataset,SIGNAL(domainChanged(const Domain&)),item,SLOT(handleDomainChanged(const Domain&)));
86 104 QObject::connect(barSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int)));
87 105 m_chartItems.insert(series,item);
88 106 // m_axisXItem->setVisible(false);
89 107 break;
90 108 }
91 109
92 110 case QChartSeries::SeriesTypeStackedBar: {
93 111
94 112 StackedBarChartSeries* stackedBarSeries = static_cast<StackedBarChartSeries*>(series);
95 113 StackedBarGroup* item = new StackedBarGroup(*stackedBarSeries,m_chart);
96 114
97 115 // Add some fugly colors for 5 fist series...
98 116 item->addColor(QColor(255,0,0,128));
99 117 item->addColor(QColor(255,255,0,128));
100 118 item->addColor(QColor(0,255,0,128));
101 119 item->addColor(QColor(0,0,255,128));
102 120 item->addColor(QColor(255,128,0,128));
103 121 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
104 122 QObject::connect(m_dataset,SIGNAL(domainChanged(const Domain&)),item,SLOT(handleDomainChanged(const Domain&)));
105 123 QObject::connect(stackedBarSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int)));
106 124 m_chartItems.insert(series,item);
107 125 break;
108 126 }
109 127
110 128 case QChartSeries::SeriesTypePercentBar: {
111 129
112 130 PercentBarChartSeries* percentBarSeries = static_cast<PercentBarChartSeries*>(series);
113 131 PercentBarGroup* item = new PercentBarGroup(*percentBarSeries,m_chart);
114 132
115 133 // Add some fugly colors for 5 fist series...
116 134 item->addColor(QColor(255,0,0,128));
117 135 item->addColor(QColor(255,255,0,128));
118 136 item->addColor(QColor(0,255,0,128));
119 137 item->addColor(QColor(0,0,255,128));
120 138 item->addColor(QColor(255,128,0,128));
121 139 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
122 140 QObject::connect(m_dataset,SIGNAL(domainChanged(const Domain&)),item,SLOT(handleDomainChanged(const Domain&)));
123 141 QObject::connect(percentBarSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int)));
124 142 m_chartItems.insert(series,item);
125 143 break;
126 }
144 }
127 145 /*
128 146 case QChartSeries::SeriesTypeScatter: {
129 QScatterSeries *scatterSeries = qobject_cast<QScatterSeries *>(series);
130 scatterSeries->d->m_theme = m_chartTheme->themeForSeries();
131 scatterSeries->d->setParentItem(this);
132 scatterSeries->d->m_boundingRect = m_rect.adjusted(margin(),margin(), -margin(), -margin());
133 m_chartItems << scatterSeries->d;
134 m_chartTheme->addObserver(scatterSeries->d);
147 QScatterSeries *scatterSeries = qobject_cast<QScatterSeries *>(series);
148 scatterSeries->d->m_theme = m_chartTheme->themeForSeries();
149 scatterSeries->d->setParentItem(this);
150 scatterSeries->d->m_boundingRect = m_rect.adjusted(margin(),margin(), -margin(), -margin());
151 m_chartItems << scatterSeries->d;
152 m_chartTheme->addObserver(scatterSeries->d);
135 153
136 154 foreach (qreal x, scatterSeries->d->m_x) {
137 155 domain.m_minX = qMin(domain.m_minX, x);
138 156 domain.m_maxX = qMax(domain.m_maxX, x);
139 157 }
140 158 foreach (qreal y, scatterSeries->d->m_y) {
141 159 domain.m_minY = qMin(domain.m_minY, y);
142 160 domain.m_maxY = qMax(domain.m_maxY, y);
143 161 }
144 162
145 163 break;
146 164 }
147 165 case QChartSeries::SeriesTypePie: {
148 166 QPieSeries *pieSeries = qobject_cast<QPieSeries *>(series);
149 167 pieSeries->d->setParentItem(this);
150 168 m_chartItems << pieSeries->d;
151 169 pieSeries->d->m_chartTheme = m_chartTheme;
152 170 m_chartTheme->addObserver(pieSeries->d);
153 171 break;
154 172 }
155 173 default:
156 174 break;
157 175 }
158 176 */
159 177
160 178 default: {
161 179 qDebug()<< "Series type" << series->type() << "not implemented.";
162 180 break;
163 181 }
164 182 }
165 183 }
166 184
167 185 void ChartPresenter::handleSeriesChanged(QChartSeries* series)
168 186 {
169 //TODO:
187 //TODO:
170 188 }
171 189
172 190 void ChartPresenter::zoomInToRect(const QRectF& rect)
173 191 {
174 192 if(!rect.isValid()) return;
175 193 QRectF r = rect.normalized();
176 194 r.translate(-m_marginSize, -m_marginSize);
177 195 Domain domain (m_dataset->domain().subDomain(rect,m_rect.width(),m_rect.height()));
178 196 m_dataset->addDomain(domain);
179 197 }
180 198
181
182 199 void ChartPresenter::zoomIn()
183 200 {
184 201 if (!m_dataset->nextDomain()) {
185 202 QRectF rect = m_rect;
186 203 rect.setWidth(rect.width()/2);
187 204 rect.setHeight(rect.height()/2);
188 205 rect.moveCenter(m_rect.center());
189 206 Domain domain (m_dataset->domain().subDomain(rect,m_rect.width(),m_rect.height()));
190 207 m_dataset->addDomain(domain);
191 208 }
192 209 }
193 210
194 211 void ChartPresenter::zoomOut()
195 212 {
196 213 m_dataset->previousDomain();
197 214 }
198 215
199 216 void ChartPresenter::zoomReset()
200 217 {
201 218 m_dataset->clearDomains();
202 219 }
203 220
204 221 /*
205 void ChartPresenter::setAxisX(const QChartAxis& axis)
206 {
207 setAxis(m_axisXItem,axis);
208 }
209 void ChartPresenter::setAxisY(const QChartAxis& axis)
210 {
211 setAxis(m_axisYItem.at(0),axis);
212 }
213
214 void ChartPresenter::setAxisY(const QList<QChartAxis>& axis)
215 {
216 //TODO not implemented
217 }
218
219 void ChartPresenter::setAxis(AxisItem *item, const QChartAxis& axis)
220 {
221 item->setVisible(axis.isAxisVisible());
222 }
223 */
222 void ChartPresenter::setAxisX(const QChartAxis& axis)
223 {
224 setAxis(m_axisXItem,axis);
225 }
226 void ChartPresenter::setAxisY(const QChartAxis& axis)
227 {
228 setAxis(m_axisYItem.at(0),axis);
229 }
230
231 void ChartPresenter::setAxisY(const QList<QChartAxis>& axis)
232 {
233 //TODO not implemented
234 }
235
236 void ChartPresenter::setAxis(AxisItem *item, const QChartAxis& axis)
237 {
238 item->setVisible(axis.isAxisVisible());
239 }
240 */
224 241 #include "moc_chartpresenter_p.cpp"
225 242
226 243 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,64 +1,65
1 1 #ifndef CHARTPRESENTER_H_
2 2 #define CHARTPRESENTER_H_
3 3
4 4 #include "qchartglobal.h"
5 5 #include <QRectF>
6 6
7 7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 8
9 9 class ChartItem;
10 10 class QChartSeries;
11 11 class ChartDataSet;
12 12 class QChart;
13 13 class Domain;
14 class QXYChartSeries;
15 class XYLineChartItem;
14 class AxisItem;
16 15
17 16 class ChartPresenter: public QObject
18 17 {
19 18 Q_OBJECT
20 19 public:
21 20 ChartPresenter(QChart* chart,ChartDataSet *dataset);
22 21 virtual ~ChartPresenter();
23 22 /*
24 23 void setAxisX(const QChartAxis& axis);
25 24 void setAxisY(const QChartAxis& axis);
26 25 void setAxisY(const QList<QChartAxis>& axis);
27 26
28 27
29 28 */
30 29
31 30 void setMargin(int margin);
32 31 int margin() const;
33 32
34 33 void zoomInToRect(const QRectF& rectangle);
35 34 void zoomIn();
36 35 void zoomOut();
37 36 void zoomReset();
38 37
39 38 private:
40 void creteConnections();
39 void createConnections();
40 void createDeafultAxis();
41 41
42 42 public slots:
43 43 void handleSeriesAdded(QChartSeries* series);
44 44 void handleSeriesRemoved(QChartSeries* series){};
45 45 void handleSeriesChanged(QChartSeries* series);
46 46 //void handleDomainChanged(Domain oldDomain,Domain newDomain);
47 47 void handleGeometryChanged();
48 48
49 49 signals:
50 50 void geometryChanged(const QRectF& rect);
51 51
52 52 private:
53 53 QMap<QChartSeries*,ChartItem*> m_chartItems;
54 54 QChart* m_chart;
55 55 ChartDataSet* m_dataset;
56 56 QVector<Domain> m_domains;
57 QList<AxisItem*> m_axis;
57 58 int m_domainIndex;
58 59 int m_marginSize;
59 60 QRectF m_rect;
60 61 };
61 62
62 63 QTCOMMERCIALCHART_END_NAMESPACE
63 64
64 65 #endif /* CHARTPRESENTER_H_ */
@@ -1,232 +1,231
1 1 #include "qchart.h"
2 2 #include "qchartseries.h"
3 3 #include "qscatterseries.h"
4 4 #include "qscatterseries_p.h"
5 5 #include "qpieseries.h"
6 6 #include "qpieseries_p.h"
7 7 #include "qchartaxis.h"
8 8 #include "charttheme_p.h"
9 9 #include "chartitem_p.h"
10 10 #include "plotdomain_p.h"
11 11 #include "axisitem_p.h"
12 12 #include "chartpresenter_p.h"
13 13 #include "chartdataset_p.h"
14 14
15 15 //series
16 16 #include "barchartseries.h"
17 17 #include "stackedbarchartseries.h"
18 18 #include "percentbarchartseries.h"
19 19 #include "qxychartseries.h"
20 20
21 21
22 22 #include <QGraphicsScene>
23 23 #include <QGraphicsSceneResizeEvent>
24 24 #include <QDebug>
25 25
26 26 QTCOMMERCIALCHART_BEGIN_NAMESPACE
27 27
28 28 QChart::QChart(QGraphicsItem *parent, Qt::WindowFlags wFlags) : QGraphicsWidget(parent,wFlags),
29 29 m_backgroundItem(0),
30 30 m_titleItem(0),
31 m_axisXItem(new AxisItem(AxisItem::X_AXIS, this)),
31 m_axisXItem(0),
32 32 m_plotDataIndex(0),
33 33 m_chartTheme(new ChartTheme(this)),
34 34 //m_dataset(0),
35 35 m_dataset(new ChartDataSet(this)),
36 36 //m_presenter(0)
37 37 m_presenter(new ChartPresenter(this,m_dataset))
38 38 {
39 39 // TODO: the default theme?
40 40 setTheme(QChart::ChartThemeDefault);
41 41
42 42 PlotDomain domain;
43 43 m_plotDomainList << domain;
44 44
45 m_axisYItem << new AxisItem(AxisItem::Y_AXIS,this);
46 m_chartItems << m_axisXItem;
47 m_chartItems << m_axisYItem.at(0);
45 //m_chartItems << m_axisXItem;
46 //m_chartItems << m_axisYItem.at(0);
48 47 }
49 48
50 49 QChart::~QChart(){}
51 50
52 51 void QChart::addSeries(QChartSeries* series)
53 52 {
54 53 m_dataset->addSeries(series);
55 54 }
56 55
57 56 QChartSeries* QChart::createSeries(QChartSeries::QChartSeriesType type)
58 57 {
59 58 // TODO: support also other types; not only scatter and pie
60 59
61 60 QChartSeries *series(0);
62 61
63 62 switch (type) {
64 63 case QChartSeries::SeriesTypeLine: {
65 64 series = QXYChartSeries::create();
66 65 break;
67 66 }
68 67 case QChartSeries::SeriesTypeBar: {
69 68 series = new BarChartSeries(this);
70 69 break;
71 70 }
72 71 case QChartSeries::SeriesTypeStackedBar: {
73 72 series = new StackedBarChartSeries(this);
74 73 break;
75 74 }
76 75 case QChartSeries::SeriesTypePercentBar: {
77 76 series = new PercentBarChartSeries(this);
78 77 break;
79 78 }
80 79 case QChartSeries::SeriesTypeScatter: {
81 80 series = new QScatterSeries(this);
82 81 break;
83 82 }
84 83 case QChartSeries::SeriesTypePie: {
85 84 series = new QPieSeries(this);
86 85 break;
87 86 }
88 87 default:
89 88 Q_ASSERT(false);
90 89 break;
91 90 }
92 91
93 92 addSeries(series);
94 93 return series;
95 94 }
96 95
97 96 void QChart::setChartBackgroundBrush(const QBrush& brush)
98 97 {
99 98
100 99 if(!m_backgroundItem){
101 100 m_backgroundItem = new QGraphicsRectItem(this);
102 101 m_backgroundItem->setZValue(-1);
103 102 }
104 103
105 104 m_backgroundItem->setBrush(brush);
106 105 m_backgroundItem->update();
107 106 }
108 107
109 108 void QChart::setChartBackgroundPen(const QPen& pen)
110 109 {
111 110
112 111 if(!m_backgroundItem){
113 112 m_backgroundItem = new QGraphicsRectItem(this);
114 113 m_backgroundItem->setZValue(-1);
115 114 }
116 115
117 116 m_backgroundItem->setPen(pen);
118 117 m_backgroundItem->update();
119 118 }
120 119
121 120 void QChart::setTitle(const QString& title,const QFont& font)
122 121 {
123 122 if(!m_titleItem) m_titleItem = new QGraphicsTextItem(this);
124 123 m_titleItem->setPlainText(title);
125 124 m_titleItem->setFont(font);
126 125 }
127 126
128 127 int QChart::margin() const
129 128 {
130 129 m_presenter->margin();
131 130 }
132 131
133 132 void QChart::setMargin(int margin)
134 133 {
135 134 m_presenter->setMargin(margin);
136 135 }
137 136
138 137 void QChart::setTheme(QChart::ChartThemeId theme)
139 138 {
140 139 m_chartTheme->setTheme(theme);
141 140
142 141 QLinearGradient backgroundGradient;
143 142 backgroundGradient.setColorAt(0.0, m_chartTheme->d->m_gradientStartColor);
144 143 backgroundGradient.setColorAt(1.0, m_chartTheme->d->m_gradientEndColor);
145 144 backgroundGradient.setCoordinateMode(QGradient::ObjectBoundingMode);
146 145 setChartBackgroundBrush(backgroundGradient);
147 146
148 147 // TODO: Move the controlling of the series presentations into private implementation of the
149 148 // series instead of QChart controlling themes for each
150 149 // In other words, the following should be used when creating xy series:
151 150 // m_chartTheme->addObserver(xyseries)
152 151 foreach (QChartSeries *series, m_chartSeries) {
153 152 if (series->type() == QChartSeries::SeriesTypeLine) {
154 153 QXYChartSeries *xyseries = static_cast<QXYChartSeries *>(series);
155 154 SeriesTheme seriesTheme = m_chartTheme->themeForSeries();
156 155 xyseries->setPen(seriesTheme.linePen);
157 156 }
158 157 }
159 158
160 159 update();
161 160 }
162 161
163 162 QChart::ChartThemeId QChart::theme()
164 163 {
165 164 return (QChart::ChartThemeId) m_chartTheme->d->m_currentTheme;
166 165 }
167 166
168 167 void QChart::zoomInToRect(const QRectF& rectangle)
169 168 {
170 169 m_presenter->zoomInToRect(rectangle);
171 170 }
172 171
173 172 void QChart::zoomIn()
174 173 {
175 174 m_presenter->zoomIn();
176 175 }
177 176
178 177 void QChart::zoomOut()
179 178 {
180 179 m_presenter->zoomOut();
181 180 }
182 181
183 182 void QChart::zoomReset()
184 183 {
185 184 m_presenter->zoomReset();
186 185 }
187 186
188 187 void QChart::setAxisX(const QChartAxis& axis)
189 188 {
190 189 setAxis(m_axisXItem,axis);
191 190 }
192 191 void QChart::setAxisY(const QChartAxis& axis)
193 192 {
194 193 setAxis(m_axisYItem.at(0),axis);
195 194 }
196 195
197 196 void QChart::setAxisY(const QList<QChartAxis>& axis)
198 197 {
199 198 //TODO not implemented
200 199 }
201 200
202 201 void QChart::setAxis(AxisItem *item, const QChartAxis& axis)
203 202 {
204 203 item->setVisible(axis.isAxisVisible());
205 204 }
206 205
207 206 void QChart::resizeEvent(QGraphicsSceneResizeEvent *event)
208 207 {
209 208
210 209 m_rect = QRectF(QPoint(0,0),event->newSize());
211 210 QRectF rect = m_rect.adjusted(margin(),margin(), -margin(), -margin());
212 211
213 212 // recalculate title position
214 213 if (m_titleItem) {
215 214 QPointF center = m_rect.center() -m_titleItem->boundingRect().center();
216 215 m_titleItem->setPos(center.x(),m_rect.top()/2 + margin()/2);
217 216 }
218 217
219 218 //recalculate background gradient
220 219 if (m_backgroundItem) {
221 220 m_backgroundItem->setRect(rect);
222 221 }
223 222
224 223 QGraphicsWidget::resizeEvent(event);
225 224 update();
226 225 }
227 226
228 227
229 228
230 229 #include "moc_qchart.cpp"
231 230
232 231 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,43 +1,53
1 1
2 2 #include "qchartaxis.h"
3 3
4 4 QTCOMMERCIALCHART_BEGIN_NAMESPACE
5 5
6 QChartAxis::QChartAxis():
6 QChartAxis::QChartAxis(QObject* parent):QObject(parent),
7 7 m_axisVisible(true),
8 8 m_girdVisible(true),
9 9 m_labelsVisible(true),
10 10 m_rowShadesVisible(true)
11 11 {
12 12 // TODO Auto-generated constructor stub
13 13
14 14 }
15 15
16 16 QChartAxis::~QChartAxis()
17 17 {
18 18 // TODO Auto-generated destructor stub
19 19 }
20 20
21 void QChartAxis::setAxisPen(const QPen& pen)
22 {
23 m_axisPen = pen;
24 }
25
26 void QChartAxis::setAxisBrush(const QBrush& brush)
27 {
28 m_axisBrush = brush;
29 }
30
21 31 void QChartAxis::setAxisVisible(bool visible)
22 32 {
23 33 m_axisVisible=visible;
24 34 }
25 35
26 36 void QChartAxis::setGridVisible(bool visible)
27 37 {
28 38 m_girdVisible=visible;
29 39 }
30 40
31 41 void QChartAxis::setLabelsVisible(bool visible)
32 42 {
33 43 m_labelsVisible=visible;
34 44 }
35 45
36 46 void QChartAxis::setRowShadesVisible(bool visible)
37 47 {
38 48 m_rowShadesVisible=visible;
39 49 }
40 50
41
51 #include "moc_qchartaxis.cpp"
42 52
43 53 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,31 +1,75
1 1 #ifndef QCHARTAXIS_H_
2 2 #define QCHARTAXIS_H_
3 3
4 4 #include <qchartglobal.h>
5 #include <QPen>
6
5 7
6 8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
7 9
8 class QChartAxis
10 class QChartAxis : public QObject
9 11 {
12 Q_OBJECT
13
14 Q_PROPERTY(bool axisVisible READ isAxisVisible WRITE setAxisVisible NOTIFY axisVisibilityChanged);
15 Q_PROPERTY(QPen axisPen READ axisPen WRITE setAxisPen NOTIFY axisPenChanged);
16 Q_PROPERTY(QBrush axisBrush READ axisBrush WRITE setAxisBrush NOTIFY axisBurshChanged);
17
18 // Q_PROPERTY(bool axisVisible READ isAxisVisible WRITE setAxisVisible NOTIFY axisVisibilityChanged);
10 19 public:
11 QChartAxis();
20 enum LabelOrientation{ HORIZONTAL, VERTICAL , SLIDE };
21
22 QChartAxis(QObject* parent = 0);
12 23 virtual ~QChartAxis();
13 24
25 //axis
14 26 bool isAxisVisible() const { return m_axisVisible;};
15 27 void setAxisVisible(bool visible);
28 void setAxisPen(const QPen& pen);
29 const QPen& axisPen() const { return m_axisPen;};
30 void setAxisBrush(const QBrush& brush);
31 const QBrush& axisBrush() const { return m_axisBrush;};
32
33 //grid
16 34 bool isGridVisible() const { return m_girdVisible;};
17 35 void setGridVisible(bool visible);
36
18 37 bool isLabelsVisible() const { return m_labelsVisible;};
19 38 void setLabelsVisible(bool visible);
39
20 40 bool isRowShadesVisible() const { return m_rowShadesVisible;};
21 41 void setRowShadesVisible(bool visible);
22 42
43 /*
44 void setLabelFont(const QFont& font);
45 const QFont& labelFont();
46
47 void setLabelPen(const QPen& pen);
48 const QPen& labelPen();
49
50 void setGridPen(const QPen& pen);
51 const QPen& gridPen();
52
53 void setGridBrush(const QBrush& brush);
54 const QBrush& gridBrush();
55 */
56
57 signals:
58 void axisVisibilityChanged();
59 void axisPenChanged();
60 void axisBurshChanged();
61
62
23 63 private:
64
24 65 bool m_axisVisible;
66 QPen m_axisPen;
67 QBrush m_axisBrush;
68
25 69 bool m_girdVisible;
26 70 bool m_labelsVisible;
27 71 bool m_rowShadesVisible;
28 72 };
29 73
30 74 QTCOMMERCIALCHART_END_NAMESPACE
31 75 #endif /* QCHARTAXIS_H_ */
General Comments 0
You need to be logged in to leave comments. Login now