##// END OF EJS Templates
Refactored for MVP...
Michal Klocek -
r139:167329998526
parent child
Show More
@@ -1,122 +1,131
1 1 #include "bargroupbase.h"
2 2 #include "bar_p.h"
3 3 #include "barlabel_p.h"
4 4 #include "separator_p.h"
5 5 #include "barchartseriesbase.h"
6 6 #include <QDebug>
7 7
8 8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 9
10 10 BarGroupBase::BarGroupBase(BarChartSeriesBase& series, QGraphicsItem *parent)
11 11 : ChartItem(parent)
12 12 ,mSeries(series)
13 13 ,mBarDefaultWidth(20) // TODO: remove hard coding, when we have layout code ready
14 14 ,mLayoutSet(false)
15 15 ,mLayoutDirty(true)
16 16 ,mTheme(0)
17 17 ,mSeparatorsVisible(true)
18 18 {
19 19 dataChanged();
20 20 }
21 21
22 22 void BarGroupBase::setSeparatorsVisible(bool visible)
23 23 {
24 24 mSeparatorsVisible = visible;
25 25 }
26 26
27 void BarGroupBase::setSize(const QSizeF& size)
28 {
29 mWidth = size.width();
30 mHeight = size.height();
31 layoutChanged();
32 mLayoutSet = true;
33 }
34
35 void BarGroupBase::setPlotDomain(const PlotDomain& data)
36 {
37 qDebug() << "BarGroupBase::setPlotDomain";
38 // TODO:
39 }
40
41 27 void BarGroupBase::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
42 28 {
43 29 if (!mLayoutSet) {
44 30 qDebug() << "BarGroupBase::paint called without layout set. Aborting.";
45 31 return;
46 32 }
47 33 if (mLayoutDirty) {
48 34 // Layout or data has changed. Need to redraw.
49 35 foreach(QGraphicsItem* i, childItems()) {
50 36 i->paint(painter,option,widget);
51 37 }
52 38 }
53 39 }
54 40
55 41 QRectF BarGroupBase::boundingRect() const
56 42 {
57 43 return QRectF(0,0,mWidth,mHeight);
58 44 }
59 45
60 46 void BarGroupBase::themeChanged(ChartTheme *theme)
61 47 {
62 48 mTheme = theme;
63 49 }
64 50
65 51 void BarGroupBase::setBarWidth( int w )
66 52 {
67 53 mBarDefaultWidth = w;
68 54 }
69 55
70 56 int BarGroupBase::addColor( QColor color )
71 57 {
72 58 int colorIndex = mColors.count();
73 59 mColors.append(color);
74 60 return colorIndex;
75 61 }
76 62
77 63 void BarGroupBase::resetColors()
78 64 {
79 65 mColors.clear();
80 66 }
81 67
82 68 void BarGroupBase::dataChanged()
83 69 {
84 70 qDebug() << "BarGroupBase::dataChanged";
85 71
86 72 // Find out maximum and minimum of all series
87 73 mMax = mSeries.max();
88 74 mMin = mSeries.min();
89 75
90 76 // Delete old bars
91 77 foreach (QGraphicsItem* item, childItems()) {
92 78 delete item;
93 79 }
94 80
95 81 // Create new graphic items for bars
96 82 int totalItems = mSeries.countTotalItems();
97 83 for (int i=0; i<totalItems; i++) {
98 84 Bar *bar = new Bar(this);
99 85 childItems().append(bar);
100 86 }
101 87
102 88 // TODO: labels from series. This creates just some example labels
103 89 int count = mSeries.countColumns();
104 90 for (int i=0; i<count; i++) {
105 91 BarLabel* label = new BarLabel(this);
106 92 QString text("Label " + QString::number(i));
107 93 label->set(text);
108 94 childItems().append(label);
109 95 }
110 96
111 97 count = mSeries.countColumns() - 1; // There is one less separator than columns
112 98 for (int i=0; i<count; i++) {
113 99 Separator* sep = new Separator(this);
114 100 sep->setColor(QColor(255,0,0,255)); // TODO: color for separations from theme
115 101 childItems().append(sep);
116 102 }
117 103
118 104 // TODO: if (autolayout) { layoutChanged() } or something
119 105 mLayoutDirty = true;
120 106 }
121 107
108 //handlers
109
110 void BarGroupBase::handleModelChanged(int index)
111 {
112 qDebug() << "BarGroupBase::handleModelChanged";
113 }
114
115 void BarGroupBase::handleDomainChanged(const Domain& domain)
116 {
117 qDebug() << "BarGroupBase::handleModelChanged";
118 }
119
120 void BarGroupBase::handleGeometryChanged(const QRectF& rect)
121 {
122 mWidth = rect.width();
123 mHeight = rect.height();
124 layoutChanged();
125 mLayoutSet = true;
126 setPos(rect.topLeft());
127 }
128
129 #include "moc_bargroupbase.cpp"
130
122 131 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,64 +1,70
1 1 #ifndef BARGROUPBASE_H
2 2 #define BARGROUPBASE_H
3 3
4 4 #include "charttheme_p.h"
5 5 #include "chartitem_p.h"
6 6 //#include "barlabel_p.h"
7 7 //#include "bar_p.h"
8 8 #include "barchartseriesbase.h"
9 9 #include <QGraphicsItem>
10 10
11 11 QTCOMMERCIALCHART_BEGIN_NAMESPACE
12 12
13 13 // Base Class for bar groups. Common implemantation of different groups. Not to be instantiated.
14 14
15 class BarGroupBase : public ChartItem, public ChartThemeObserver
15 class BarGroupBase : public QObject, public ChartItem
16 16 {
17
17 Q_OBJECT
18 18 public:
19 19 BarGroupBase(BarChartSeriesBase& series, QGraphicsItem *parent = 0);
20 20 void setSeparatorsVisible(bool visible = true);
21 21
22 22 public: // From ChartItem
23 void setSize(const QSizeF &size);
24 void setPlotDomain(const PlotDomain& data);
23 void setSize(const QSizeF &size){};
24 void setPlotDomain(const PlotDomain& data){};
25 25
26 26 // From QGraphicsItem
27 27 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
28 28 QRectF boundingRect() const;
29 29
30 30 // From ChartThemeObserver
31 31 void themeChanged(ChartTheme *theme);
32 32
33 33 // TODO: these may change with layout awarness.
34 34 void setBarWidth( int w );
35 35 int addColor( QColor color );
36 36 void resetColors();
37 37
38 38 virtual void dataChanged(); // data of series has changed -> need to recalculate bar sizes
39 39 virtual void layoutChanged() = 0; // layout has changed -> need to recalculate bar sizes
40 40
41 protected slots:
42 void handleModelChanged(int index);
43 void handleDomainChanged(const Domain& domain);
44 void handleGeometryChanged(const QRectF& size);
45
46
41 47 protected:
42 48
43 49 BarChartSeriesBase& mSeries;
44 50
45 51 int mMin; // Min and max values of data. (updated when data is changed, used when drawing)
46 52 int mMax;
47 53
48 54 int mHeight; // Layout spesific
49 55 int mWidth;
50 56 int mBarDefaultWidth;
51 57
52 58 bool mLayoutSet; // True, if component has been laid out.
53 59 bool mLayoutDirty;
54 60
55 61 QList<QColor> mColors; // List of colors for series for now
56 62
57 63 ChartTheme* mTheme;
58 64 bool mSeparatorsVisible;
59 65
60 66 };
61 67
62 68 QTCOMMERCIALCHART_END_NAMESPACE
63 69
64 70 #endif // BARGROUPBASE_H
@@ -1,64 +1,140
1 1 #include "chartdataset_p.h"
2 //series
2 3 #include "qxychartseries.h"
4 #include "barchartseries.h"
5 #include "stackedbarchartseries.h"
6 #include "percentbarchartseries.h"
3 7
4 8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
5 9
6 10 ChartDataSet::ChartDataSet(QObject *parent):QObject(parent)
7 11 {
8 12 Domain domain;
9 13 m_domains<<domain;
10 14 }
11 15
12 16 ChartDataSet::~ChartDataSet()
13 17 {
14 18 // TODO Auto-generated destructor stub
15 19 }
16 20
17 21 const Domain& ChartDataSet::domain() const
18 22 {
19 23 return m_domains[m_domainIndex];
20 24 }
21 25
22 26 void ChartDataSet::addSeries(QChartSeries* series)
23 27 {
24 // TODO: we should check the series not already added
25 m_chartSeries << series;
26 m_domainIndex = 0;
27 m_domains.resize(1);
28
29 Domain& domain = m_domains[m_domainIndex];
30
31 switch(series->type())
32 {
33 case QChartSeries::SeriesTypeLine: {
34
35 QXYChartSeries* xyseries = static_cast<QXYChartSeries*>(series);
36
37 for (int i = 0; i < xyseries->count(); i++)
38 {
39 qreal x = xyseries->x(i);
40 qreal y = xyseries->y(i);
41 domain.m_minX = qMin(domain.m_minX,x);
42 domain.m_minY = qMin(domain.m_minY,y);
43 domain.m_maxX = qMax(domain.m_maxX,x);
44 domain.m_maxY = qMax(domain.m_maxY,y);
45 }
46
47 emit domainChanged();
48
49 break;
50 }
51 default: {
52 qDebug()<<__FUNCTION__<<"type" << series->type()<<"not supported";
53 return;
54 break;
55 }
56
57 }
58
59 emit seriesAdded(series);
28 // TODO: we should check the series not already added
29 m_chartSeries << series;
30 m_domainIndex = 0;
31 m_domains.resize(1);
32
33 Domain& domain = m_domains[m_domainIndex];
34
35 switch(series->type())
36 {
37 case QChartSeries::SeriesTypeLine: {
38
39 QXYChartSeries* xyseries = static_cast<QXYChartSeries*>(series);
40
41 for (int i = 0; i < xyseries->count(); i++)
42 {
43 qreal x = xyseries->x(i);
44 qreal y = xyseries->y(i);
45 domain.m_minX = qMin(domain.m_minX,x);
46 domain.m_minY = qMin(domain.m_minY,y);
47 domain.m_maxX = qMax(domain.m_maxX,x);
48 domain.m_maxY = qMax(domain.m_maxY,y);
49 }
50 break;
51 }
52 case QChartSeries::SeriesTypeBar: {
53
54 BarChartSeries* barSeries = static_cast<BarChartSeries*>(series);
55 qreal x = barSeries->countColumns();
56 qreal y = barSeries->max();
57 domain.m_minX = qMin(domain.m_minX,x);
58 domain.m_minY = qMin(domain.m_minY,y);
59 domain.m_maxX = qMax(domain.m_maxX,x);
60 domain.m_maxY = qMax(domain.m_maxY,y);
61 }
62 break;
63 case QChartSeries::SeriesTypeStackedBar: {
64
65 StackedBarChartSeries* stackedBarSeries = static_cast<StackedBarChartSeries*>(series);
66 qreal x = stackedBarSeries->countColumns();
67 qreal y = stackedBarSeries->maxColumnSum();
68 domain.m_minX = qMin(domain.m_minX,x);
69 domain.m_minY = qMin(domain.m_minY,y);
70 domain.m_maxX = qMax(domain.m_maxX,x);
71 domain.m_maxY = qMax(domain.m_maxY,y);
72 }
73 break;
74 case QChartSeries::SeriesTypePercentBar: {
75
76 PercentBarChartSeries* percentBarSeries = static_cast<PercentBarChartSeries*>(series);
77 qreal x = percentBarSeries->countColumns();
78 domain.m_minX = qMin(domain.m_minX,x);
79 domain.m_minY = 0;
80 domain.m_maxX = qMax(domain.m_maxX,x);
81 domain.m_maxY = 100;
82 }
83 break;
84
85 default: {
86 qDebug()<<__FUNCTION__<<"type" << series->type()<<"not supported";
87 return;
88 break;
89 }
90
91 }
92
93 emit seriesAdded(series);
94 emit domainChanged(domain);
95 }
96
97 bool ChartDataSet::nextDomain()
98 {
99 if (m_domainIndex < m_domains.count() - 1) {
100 m_domainIndex++;
101 emit domainChanged(m_domains[m_domainIndex]);
102 return true;
103 }
104 else {
105 return false;
106 }
107 }
108
109 bool ChartDataSet::previousDomain()
110 {
111 if (m_domainIndex > 0) {
112 m_domainIndex--;
113 emit domainChanged(m_domains[m_domainIndex]);
114 return true;
115 }
116 else {
117 return false;
118 }
119 }
120
121 void ChartDataSet::clearDomains()
122 {
123 if (m_domainIndex > 0) {
124 m_domainIndex = 0;
125 emit domainChanged(m_domains[m_domainIndex]);
126 }
127 }
128
129 void ChartDataSet::addDomain(const Domain& domain)
130 {
131 m_domains.resize(m_domainIndex + 1);
132 m_domains << domain;
133 m_domainIndex++;
134
135 emit domainChanged(domain);
60 136 }
61 137
62 138 #include "moc_chartdataset_p.cpp"
63 139
64 140 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,32 +1,36
1 1 #ifndef CHARTENGINE_P_H_
2 2 #define CHARTENGINE_P_H_
3 3
4 4 #include <qchartseries.h>
5 5 #include "domain_p.h"
6 6 #include <QVector>
7 7
8 8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 9
10 10 class ChartDataSet : public QObject
11 11 {
12 12 Q_OBJECT
13 13 public:
14 14 ChartDataSet(QObject* parent =0);
15 15 virtual ~ChartDataSet();
16 16
17 17 void addSeries(QChartSeries* series);
18 void addDomain(const Domain& domain);
19 bool nextDomain();
20 bool previousDomain();
21 void clearDomains();
18 22 const Domain& domain() const;
19 23
20 24 signals:
21 25 void seriesAdded(QChartSeries* series);
22 void domainChanged();
26 void domainChanged(const Domain& domain);
23 27
24 28 private:
25 29 QList<QChartSeries*> m_chartSeries;
26 30 QVector<Domain> m_domains;
27 31 int m_domainIndex;
28 32 };
29 33
30 34 QTCOMMERCIALCHART_END_NAMESPACE
31 35
32 36 #endif /* CHARTENGINE_P_H_ */
@@ -1,85 +1,226
1 #include "qchart.h"
1 2 #include "chartpresenter_p.h"
2 3 #include "chartdataset_p.h"
4 //series
5 #include "barchartseries.h"
6 #include "stackedbarchartseries.h"
7 #include "percentbarchartseries.h"
3 8 #include "qxychartseries.h"
9 //items
10 #include "bargroup.h"
11 #include "stackedbargroup.h"
4 12 #include "xylinechartitem_p.h"
13 #include "percentbargroup.h"
5 14 #include "linechartanimationitem_p.h"
6 #include "qchart.h"
7 15
8 16 #include <QAbstractAnimation>
9 17 #include <QPropertyAnimation>
10 18
11 19 QTCOMMERCIALCHART_BEGIN_NAMESPACE
12 20
13 21 ChartPresenter::ChartPresenter(QChart* chart,ChartDataSet* dataset):QObject(chart),
14 22 m_chart(chart),
15 23 m_dataset(dataset),
24 m_domainIndex(0),
25 m_marginSize(0),
16 26 m_rect(QRectF(QPoint(0,0),m_chart->size()))
17 27 {
18 28 creteConnections();
19 29 }
20 30
21 31 ChartPresenter::~ChartPresenter()
22 32 {
23 33 }
24 34
25 35 void ChartPresenter::creteConnections()
26 36 {
27 37 QObject::connect(m_chart,SIGNAL(geometryChanged()),this,SLOT(handleGeometryChanged()));
28 38 QObject::connect(m_dataset,SIGNAL(seriesAdded(QChartSeries*)),this,SLOT(handleSeriesAdded(QChartSeries*)));
29 QObject::connect(m_dataset,SIGNAL(domainChanged()),this,SLOT(handleDomainChanged()));
30 39 }
31 40
32 41 void ChartPresenter::handleGeometryChanged()
33 42 {
34 43 m_rect = QRectF(QPoint(0,0),m_chart->size());
44 m_rect.adjust(m_marginSize,m_marginSize, -m_marginSize, -m_marginSize);
45 emit geometryChanged(m_rect);
46 }
35 47
36 int margin = m_chart->margin();
37 m_rect.adjust(margin,margin, -margin, -margin);
48 int ChartPresenter::margin() const
49 {
50 return m_marginSize;
51 }
38 52
39 foreach (ChartItem *item, m_chartItems) {
40 item->setPos(m_rect.topLeft());
41 item->setSize(m_rect.size());
42 item->updateItem();
43 }
53 void ChartPresenter::setMargin(int margin)
54 {
55 m_marginSize = margin;
44 56 }
45 57
46 58 void ChartPresenter::handleSeriesAdded(QChartSeries* series)
47 59 {
48 60 switch(series->type())
49 61 {
50 62 case QChartSeries::SeriesTypeLine: {
63 QXYChartSeries* lineSeries = static_cast<QXYChartSeries*>(series);
64 XYLineChartItem* item = new LineChartAnimationItem(this,lineSeries,m_chart);
65 item->setPen(lineSeries->pen());
66 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
67 QObject::connect(m_dataset,SIGNAL(domainChanged(const Domain&)),item,SLOT(handleDomainChanged(const Domain&)));
68 QObject::connect(lineSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int)));
69 m_chartItems.insert(series,item);
70 break;
71 }
72
73 case QChartSeries::SeriesTypeBar: {
74 BarChartSeries* barSeries = static_cast<BarChartSeries*>(series);
75 BarGroup* item = new BarGroup(*barSeries,m_chart);
76
77 // Add some fugly colors for 5 fist series...
78 item->addColor(QColor(255,0,0,128));
79 item->addColor(QColor(255,255,0,128));
80 item->addColor(QColor(0,255,0,128));
81 item->addColor(QColor(0,0,255,128));
82 item->addColor(QColor(255,128,0,128));
51 83
52 QXYChartSeries* xyseries = static_cast<QXYChartSeries*>(series);
53 //TODO: series->createViewItem();
54 //XYLineChartItem* item = new XYLineChartItem(this,m_chart);
55 XYLineChartItem* item = new LineChartAnimationItem(this,xyseries,m_chart);
56 item->setDomain(m_dataset->domain());
57 item->updateItem();
84 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
85 QObject::connect(m_dataset,SIGNAL(domainChanged(const Domain&)),item,SLOT(handleDomainChanged(const Domain&)));
86 QObject::connect(barSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int)));
58 87 m_chartItems.insert(series,item);
88 // m_axisXItem->setVisible(false);
59 89 break;
60 90 }
61 91
92 case QChartSeries::SeriesTypeStackedBar: {
93
94 StackedBarChartSeries* stackedBarSeries = static_cast<StackedBarChartSeries*>(series);
95 StackedBarGroup* item = new StackedBarGroup(*stackedBarSeries,m_chart);
96
97 // Add some fugly colors for 5 fist series...
98 item->addColor(QColor(255,0,0,128));
99 item->addColor(QColor(255,255,0,128));
100 item->addColor(QColor(0,255,0,128));
101 item->addColor(QColor(0,0,255,128));
102 item->addColor(QColor(255,128,0,128));
103 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
104 QObject::connect(m_dataset,SIGNAL(domainChanged(const Domain&)),item,SLOT(handleDomainChanged(const Domain&)));
105 QObject::connect(stackedBarSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int)));
106 m_chartItems.insert(series,item);
107 break;
108 }
109
110 case QChartSeries::SeriesTypePercentBar: {
111
112 PercentBarChartSeries* percentBarSeries = static_cast<PercentBarChartSeries*>(series);
113 PercentBarGroup* item = new PercentBarGroup(*percentBarSeries,m_chart);
114
115 // Add some fugly colors for 5 fist series...
116 item->addColor(QColor(255,0,0,128));
117 item->addColor(QColor(255,255,0,128));
118 item->addColor(QColor(0,255,0,128));
119 item->addColor(QColor(0,0,255,128));
120 item->addColor(QColor(255,128,0,128));
121 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
122 QObject::connect(m_dataset,SIGNAL(domainChanged(const Domain&)),item,SLOT(handleDomainChanged(const Domain&)));
123 QObject::connect(percentBarSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int)));
124 m_chartItems.insert(series,item);
125 break;
126 }
127 /*
128 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);
135
136 foreach (qreal x, scatterSeries->d->m_x) {
137 domain.m_minX = qMin(domain.m_minX, x);
138 domain.m_maxX = qMax(domain.m_maxX, x);
139 }
140 foreach (qreal y, scatterSeries->d->m_y) {
141 domain.m_minY = qMin(domain.m_minY, y);
142 domain.m_maxY = qMax(domain.m_maxY, y);
143 }
144
145 break;
146 }
147 case QChartSeries::SeriesTypePie: {
148 QPieSeries *pieSeries = qobject_cast<QPieSeries *>(series);
149 pieSeries->d->setParentItem(this);
150 m_chartItems << pieSeries->d;
151 pieSeries->d->m_chartTheme = m_chartTheme;
152 m_chartTheme->addObserver(pieSeries->d);
153 break;
154 }
155 default:
156 break;
157 }
158 */
159
62 160 default: {
63 qDebug()<< "Series type" << series->type() << "not implemented.";
64 break;
161 qDebug()<< "Series type" << series->type() << "not implemented.";
162 break;
65 163 }
66 164 }
67 165 }
68 166
69 167 void ChartPresenter::handleSeriesChanged(QChartSeries* series)
70 168 {
71 switch(series->type())
72 {
73 case QChartSeries::SeriesTypeLine: {
169 //TODO:
170 }
171
172 void ChartPresenter::zoomInToRect(const QRectF& rect)
173 {
174 if(!rect.isValid()) return;
175 QRectF r = rect.normalized();
176 r.translate(-m_marginSize, -m_marginSize);
177 Domain domain (m_dataset->domain().subDomain(rect,m_rect.width(),m_rect.height()));
178 m_dataset->addDomain(domain);
179 }
74 180
75 break;
76 }
77 181
78 }
182 void ChartPresenter::zoomIn()
183 {
184 if (!m_dataset->nextDomain()) {
185 QRectF rect = m_rect;
186 rect.setWidth(rect.width()/2);
187 rect.setHeight(rect.height()/2);
188 rect.moveCenter(m_rect.center());
189 Domain domain (m_dataset->domain().subDomain(rect,m_rect.width(),m_rect.height()));
190 m_dataset->addDomain(domain);
191 }
192 }
79 193
80 //m_chartItems.value(series)->updateItem();
194 void ChartPresenter::zoomOut()
195 {
196 m_dataset->previousDomain();
81 197 }
82 198
199 void ChartPresenter::zoomReset()
200 {
201 m_dataset->clearDomains();
202 }
203
204 /*
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 */
83 224 #include "moc_chartpresenter_p.cpp"
84 225
85 226 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,45 +1,64
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 14 class QXYChartSeries;
15 15 class XYLineChartItem;
16 16
17 17 class ChartPresenter: public QObject
18 18 {
19 19 Q_OBJECT
20 20 public:
21 21 ChartPresenter(QChart* chart,ChartDataSet *dataset);
22 22 virtual ~ChartPresenter();
23 /*
24 void setAxisX(const QChartAxis& axis);
25 void setAxisY(const QChartAxis& axis);
26 void setAxisY(const QList<QChartAxis>& axis);
27
28
29 */
30
31 void setMargin(int margin);
32 int margin() const;
33
34 void zoomInToRect(const QRectF& rectangle);
35 void zoomIn();
36 void zoomOut();
37 void zoomReset();
23 38
24 39 private:
25 40 void creteConnections();
26 41
27 42 public slots:
28 43 void handleSeriesAdded(QChartSeries* series);
29 44 void handleSeriesRemoved(QChartSeries* series){};
30 45 void handleSeriesChanged(QChartSeries* series);
31 46 //void handleDomainChanged(Domain oldDomain,Domain newDomain);
32 47 void handleGeometryChanged();
33 48
34
49 signals:
50 void geometryChanged(const QRectF& rect);
35 51
36 52 private:
37 53 QMap<QChartSeries*,ChartItem*> m_chartItems;
38 54 QChart* m_chart;
39 55 ChartDataSet* m_dataset;
56 QVector<Domain> m_domains;
57 int m_domainIndex;
58 int m_marginSize;
40 59 QRectF m_rect;
41 60 };
42 61
43 62 QTCOMMERCIALCHART_END_NAMESPACE
44 63
45 64 #endif /* CHARTPRESENTER_H_ */
@@ -1,45 +1,45
1 1 #include "domain_p.h"
2 2
3 3 QTCOMMERCIALCHART_BEGIN_NAMESPACE
4 4
5 5 Domain::Domain():
6 6 m_minX(0),
7 7 m_maxX(0),
8 8 m_minY(0),
9 9 m_maxY(0)
10 10 {
11 11
12 12 }
13 13
14 14 Domain::~Domain()
15 15 {
16 16 // TODO Auto-generated destructor stub
17 17 }
18 18
19 19 qreal Domain::spanX() const
20 20 {
21 21 Q_ASSERT(m_maxX >= m_minX);
22 22 return m_maxX - m_minX;
23 23 }
24 24
25 25 qreal Domain::spanY() const
26 26 {
27 27 Q_ASSERT(m_maxY >= m_minY);
28 28 return m_maxY - m_minY;
29 29 }
30 30
31 Domain Domain::subDomain(const QRect& rect, qreal maxWidth,qreal maxHeight) const
31 Domain Domain::subDomain(const QRectF& rect, qreal maxWidth,qreal maxHeight) const
32 32 {
33 33 Domain domain;
34 34
35 35 qreal dx = spanX() / maxWidth;
36 36 qreal dy = spanY() / maxHeight;
37 37
38 38 domain.m_minX = m_minX + dx * rect.left();
39 39 domain.m_maxX = m_minX + dx * rect.right();
40 40 domain.m_minY = m_maxY - dy * rect.bottom();
41 41 domain.m_maxY = m_maxY - dy * rect.top();
42 42
43 43 return domain;
44 44 }
45 45 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,27 +1,27
1 1 #ifndef DOMAIN_H_
2 2 #define DOMAIN_H_
3 3 #include "qchartglobal.h"
4 4 #include <QRect>
5 5
6 6 QTCOMMERCIALCHART_BEGIN_NAMESPACE
7 7
8 8 class Domain {
9 9 public:
10 10 Domain();
11 11 virtual ~Domain();
12 12
13 13 qreal spanX() const;
14 14 qreal spanY() const;
15 15
16 Domain subDomain(const QRect& rect, qreal maxWidth, qreal maxHeight) const;
16 Domain subDomain(const QRectF& rect, qreal maxWidth, qreal maxHeight) const;
17 17
18 18 public:
19 19 qreal m_minX;
20 20 qreal m_maxX;
21 21 qreal m_minY;
22 22 qreal m_maxY;
23 23 };
24 24
25 25 QTCOMMERCIALCHART_END_NAMESPACE
26 26
27 27 #endif
@@ -1,425 +1,232
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 #include "qxychartseries.h"
8 7 #include "qchartaxis.h"
9 #include "barchartseries.h"
10 #include "bargroup.h"
11 #include "stackedbarchartseries.h"
12 #include "stackedbargroup.h"
13 #include "percentbarchartseries.h"
14 #include "percentbargroup.h"
15 8 #include "charttheme_p.h"
16 9 #include "chartitem_p.h"
17
18 #include "xylinechartitem_p.h"
19 10 #include "plotdomain_p.h"
20 11 #include "axisitem_p.h"
21 12 #include "chartpresenter_p.h"
22 13 #include "chartdataset_p.h"
14
15 //series
16 #include "barchartseries.h"
17 #include "stackedbarchartseries.h"
18 #include "percentbarchartseries.h"
19 #include "qxychartseries.h"
20
21
23 22 #include <QGraphicsScene>
24 23 #include <QGraphicsSceneResizeEvent>
25 24 #include <QDebug>
26 25
27 26 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28 27
29 28 QChart::QChart(QGraphicsItem *parent, Qt::WindowFlags wFlags) : QGraphicsWidget(parent,wFlags),
30 29 m_backgroundItem(0),
31 30 m_titleItem(0),
32 31 m_axisXItem(new AxisItem(AxisItem::X_AXIS, this)),
33 32 m_plotDataIndex(0),
34 m_marginSize(0),
35 33 m_chartTheme(new ChartTheme(this)),
36 m_dataset(0),
37 //m_dataset(new ChartDataSet(this)),
38 m_presenter(0)
39 //m_presenter(new ChartPresenter(this,m_dataset))
34 //m_dataset(0),
35 m_dataset(new ChartDataSet(this)),
36 //m_presenter(0)
37 m_presenter(new ChartPresenter(this,m_dataset))
40 38 {
41 39 // TODO: the default theme?
42 40 setTheme(QChart::ChartThemeDefault);
43 41
44 42 PlotDomain domain;
45 43 m_plotDomainList << domain;
44
46 45 m_axisYItem << new AxisItem(AxisItem::Y_AXIS,this);
47 46 m_chartItems << m_axisXItem;
48 47 m_chartItems << m_axisYItem.at(0);
49 48 }
50 49
51 50 QChart::~QChart(){}
52 51
53 52 void QChart::addSeries(QChartSeries* series)
54 53 {
55 if(m_dataset) {
56 m_dataset->addSeries(series);
57 return;
58 }
59
60 Q_ASSERT(series);
61 Q_ASSERT(series->type() != QChartSeries::SeriesTypeInvalid);
62
63 // TODO: we should check the series not already added
64
65
66 m_chartSeries << series;
67
68 m_plotDataIndex = 0 ;
69 m_plotDomainList.resize(1);
70 PlotDomain& domain = m_plotDomainList[m_plotDataIndex];
71
72 switch(series->type())
73 {
74 case QChartSeries::SeriesTypeLine: {
75
76 QXYChartSeries* xyseries = static_cast<QXYChartSeries*>(series);
77
78 for (int i = 0 ; i < xyseries->count() ; i++) {
79 qreal x = xyseries->x(i);
80 qreal y = xyseries->y(i);
81 domain.m_minX = qMin(domain.m_minX,x);
82 domain.m_minY = qMin(domain.m_minY,y);
83 domain.m_maxX = qMax(domain.m_maxX,x);
84 domain.m_maxY = qMax(domain.m_maxY,y);
85 }
86
87 //XYLineChartItem* item = new XYLineChartItem(xyseries,0,this);
88
89 //m_chartItems << item;
90 // TODO:
91 //m_chartTheme->addObserver(xyseries);
92
93 break;
94 }
95 case QChartSeries::SeriesTypeBar: {
96
97 qDebug() << "barSeries added";
98 BarChartSeries* barSeries = static_cast<BarChartSeries*>(series);
99 BarGroup* barGroup = new BarGroup(*barSeries,this);
100
101 // Add some fugly colors for 5 fist series...
102 barGroup->addColor(QColor(255,0,0,128));
103 barGroup->addColor(QColor(255,255,0,128));
104 barGroup->addColor(QColor(0,255,0,128));
105 barGroup->addColor(QColor(0,0,255,128));
106 barGroup->addColor(QColor(255,128,0,128));
107
108 m_chartItems << barGroup;
109 childItems().append(barGroup);
110
111 qreal x = barSeries->countColumns();
112 qreal y = barSeries->max();
113 domain.m_minX = qMin(domain.m_minX,x);
114 domain.m_minY = qMin(domain.m_minY,y);
115 domain.m_maxX = qMax(domain.m_maxX,x);
116 domain.m_maxY = qMax(domain.m_maxY,y);
117 m_axisXItem->setVisible(false);
118 break;
119 }
120 case QChartSeries::SeriesTypeStackedBar: {
121
122 qDebug() << "barSeries added";
123 StackedBarChartSeries* stackedBarSeries = static_cast<StackedBarChartSeries*>(series);
124 StackedBarGroup* stackedBarGroup = new StackedBarGroup(*stackedBarSeries,this);
125
126 // Add some fugly colors for 5 fist series...
127 stackedBarGroup->addColor(QColor(255,0,0,128));
128 stackedBarGroup->addColor(QColor(255,255,0,128));
129 stackedBarGroup->addColor(QColor(0,255,0,128));
130 stackedBarGroup->addColor(QColor(0,0,255,128));
131 stackedBarGroup->addColor(QColor(255,128,0,128));
132
133 m_chartItems << stackedBarGroup;
134 childItems().append(stackedBarGroup);
135
136 qreal x = stackedBarSeries->countColumns();
137 qreal y = stackedBarSeries->maxColumnSum();
138 domain.m_minX = qMin(domain.m_minX,x);
139 domain.m_minY = qMin(domain.m_minY,y);
140 domain.m_maxX = qMax(domain.m_maxX,x);
141 domain.m_maxY = qMax(domain.m_maxY,y);
142 m_axisXItem->setVisible(false);
143 break;
144 }
145 case QChartSeries::SeriesTypePercentBar: {
146
147 qDebug() << "barSeries added";
148 PercentBarChartSeries* percentBarSeries = static_cast<PercentBarChartSeries*>(series);
149 PercentBarGroup* percentBarGroup = new PercentBarGroup(*percentBarSeries,this);
150
151 // Add some fugly colors for 5 fist series...
152 percentBarGroup->addColor(QColor(255,0,0,128));
153 percentBarGroup->addColor(QColor(255,255,0,128));
154 percentBarGroup->addColor(QColor(0,255,0,128));
155 percentBarGroup->addColor(QColor(0,0,255,128));
156 percentBarGroup->addColor(QColor(255,128,0,128));
157
158 m_chartItems << percentBarGroup;
159 childItems().append(percentBarGroup);
160
161 qreal x = percentBarSeries->countColumns();
162 domain.m_minX = qMin(domain.m_minX,x);
163 domain.m_minY = 0;
164 domain.m_maxX = qMax(domain.m_maxX,x);
165 domain.m_maxY = 100;
166 m_axisXItem->setVisible(false);
167 break;
168 }
169 case QChartSeries::SeriesTypeScatter: {
170 QScatterSeries *scatterSeries = qobject_cast<QScatterSeries *>(series);
171 scatterSeries->d->m_theme = m_chartTheme->themeForSeries();
172 scatterSeries->d->setParentItem(this);
173 scatterSeries->d->m_boundingRect = m_rect.adjusted(margin(),margin(), -margin(), -margin());
174 m_chartItems << scatterSeries->d;
175 m_chartTheme->addObserver(scatterSeries->d);
176
177 foreach (qreal x, scatterSeries->d->m_x) {
178 domain.m_minX = qMin(domain.m_minX, x);
179 domain.m_maxX = qMax(domain.m_maxX, x);
180 }
181 foreach (qreal y, scatterSeries->d->m_y) {
182 domain.m_minY = qMin(domain.m_minY, y);
183 domain.m_maxY = qMax(domain.m_maxY, y);
184 }
185
186 break;
187 }
188 case QChartSeries::SeriesTypePie: {
189 QPieSeries *pieSeries = qobject_cast<QPieSeries *>(series);
190 pieSeries->d->setParentItem(this);
191 m_chartItems << pieSeries->d;
192 pieSeries->d->m_chartTheme = m_chartTheme;
193 m_chartTheme->addObserver(pieSeries->d);
194 break;
195 }
196 default:
197 break;
198 }
199
200 // Update all the items to match the new visible area of the chart
201 foreach(ChartItem* i, m_chartItems)
202 i->setPlotDomain(m_plotDomainList.at(m_plotDataIndex));
54 m_dataset->addSeries(series);
203 55 }
204 56
205 57 QChartSeries* QChart::createSeries(QChartSeries::QChartSeriesType type)
206 58 {
207 59 // TODO: support also other types; not only scatter and pie
208 60
209 61 QChartSeries *series(0);
210 62
211 63 switch (type) {
212 64 case QChartSeries::SeriesTypeLine: {
213 65 series = QXYChartSeries::create();
214 66 break;
215 67 }
216 68 case QChartSeries::SeriesTypeBar: {
217 69 series = new BarChartSeries(this);
218 70 break;
219 71 }
220 72 case QChartSeries::SeriesTypeStackedBar: {
221 73 series = new StackedBarChartSeries(this);
222 74 break;
223 75 }
224 76 case QChartSeries::SeriesTypePercentBar: {
225 77 series = new PercentBarChartSeries(this);
226 78 break;
227 79 }
228 80 case QChartSeries::SeriesTypeScatter: {
229 81 series = new QScatterSeries(this);
230 82 break;
231 83 }
232 84 case QChartSeries::SeriesTypePie: {
233 85 series = new QPieSeries(this);
234 86 break;
235 87 }
236 88 default:
237 89 Q_ASSERT(false);
238 90 break;
239 91 }
240 92
241 93 addSeries(series);
242 94 return series;
243 95 }
244 96
245 97 void QChart::setChartBackgroundBrush(const QBrush& brush)
246 98 {
247 99
248 100 if(!m_backgroundItem){
249 101 m_backgroundItem = new QGraphicsRectItem(this);
250 102 m_backgroundItem->setZValue(-1);
251 103 }
252 104
253 105 m_backgroundItem->setBrush(brush);
254 106 m_backgroundItem->update();
255 107 }
256 108
257 109 void QChart::setChartBackgroundPen(const QPen& pen)
258 110 {
259 111
260 112 if(!m_backgroundItem){
261 113 m_backgroundItem = new QGraphicsRectItem(this);
262 114 m_backgroundItem->setZValue(-1);
263 115 }
264 116
265 117 m_backgroundItem->setPen(pen);
266 118 m_backgroundItem->update();
267 119 }
268 120
269 121 void QChart::setTitle(const QString& title,const QFont& font)
270 122 {
271 123 if(!m_titleItem) m_titleItem = new QGraphicsTextItem(this);
272 124 m_titleItem->setPlainText(title);
273 125 m_titleItem->setFont(font);
274 126 }
275 127
276 128 int QChart::margin() const
277 129 {
278 return m_marginSize;
130 m_presenter->margin();
279 131 }
280 132
281 133 void QChart::setMargin(int margin)
282 134 {
283 m_marginSize = margin;
135 m_presenter->setMargin(margin);
284 136 }
285 137
286 138 void QChart::setTheme(QChart::ChartThemeId theme)
287 139 {
288 140 m_chartTheme->setTheme(theme);
289 141
290 142 QLinearGradient backgroundGradient;
291 143 backgroundGradient.setColorAt(0.0, m_chartTheme->d->m_gradientStartColor);
292 144 backgroundGradient.setColorAt(1.0, m_chartTheme->d->m_gradientEndColor);
293 145 backgroundGradient.setCoordinateMode(QGradient::ObjectBoundingMode);
294 146 setChartBackgroundBrush(backgroundGradient);
295 147
296 148 // TODO: Move the controlling of the series presentations into private implementation of the
297 149 // series instead of QChart controlling themes for each
298 150 // In other words, the following should be used when creating xy series:
299 151 // m_chartTheme->addObserver(xyseries)
300 152 foreach (QChartSeries *series, m_chartSeries) {
301 153 if (series->type() == QChartSeries::SeriesTypeLine) {
302 154 QXYChartSeries *xyseries = static_cast<QXYChartSeries *>(series);
303 155 SeriesTheme seriesTheme = m_chartTheme->themeForSeries();
304 156 xyseries->setPen(seriesTheme.linePen);
305 157 }
306 158 }
307 159
308 160 update();
309 161 }
310 162
311 163 QChart::ChartThemeId QChart::theme()
312 164 {
313 165 return (QChart::ChartThemeId) m_chartTheme->d->m_currentTheme;
314 166 }
315 167
316 168 void QChart::zoomInToRect(const QRectF& rectangle)
317 169 {
318
319 if(!rectangle.isValid()) return;
320
321 qreal margin = this->margin();
322
323 QRectF rect = rectangle.normalized();
324 rect.translate(-margin, -margin);
325
326 PlotDomain& oldDomain = m_plotDomainList[m_plotDataIndex];
327
328 PlotDomain domain = oldDomain.subDomain(rect,m_rect.width() - 2 * margin,m_rect.height() - 2 * margin);
329
330 m_plotDomainList.resize(m_plotDataIndex + 1);
331 m_plotDomainList<<domain;
332 m_plotDataIndex++;
333
334 foreach (ChartItem* item, m_chartItems)
335 item->setPlotDomain(m_plotDomainList[m_plotDataIndex]);
336 update();
170 m_presenter->zoomInToRect(rectangle);
337 171 }
338 172
339 173 void QChart::zoomIn()
340 174 {
341 if (m_plotDataIndex < m_plotDomainList.count() - 1) {
342 m_plotDataIndex++;
343 foreach (ChartItem* item, m_chartItems)
344 item->setPlotDomain(m_plotDomainList[m_plotDataIndex]);
345 update();
346 } else {
347 QRectF rect = m_rect.adjusted(margin(),margin(), -margin(), -margin());
348 rect.setWidth(rect.width()/2);
349 rect.setHeight(rect.height()/2);
350 rect.moveCenter(m_rect.center());
351 zoomInToRect(rect);
352 }
175 m_presenter->zoomIn();
353 176 }
354 177
355 178 void QChart::zoomOut()
356 179 {
357 if (m_plotDataIndex > 0) {
358 m_plotDataIndex--;
359 foreach (ChartItem* item, m_chartItems)
360 item->setPlotDomain(m_plotDomainList[m_plotDataIndex]);
361 update();
362 }
180 m_presenter->zoomOut();
363 181 }
364 182
365 183 void QChart::zoomReset()
366 184 {
367 if (m_plotDataIndex > 0) {
368 m_plotDataIndex = 0;
369 foreach (ChartItem* item, m_chartItems)
370 item->setPlotDomain(m_plotDomainList[m_plotDataIndex]);
371 update();
372 }
185 m_presenter->zoomReset();
373 186 }
374 187
375 188 void QChart::setAxisX(const QChartAxis& axis)
376 189 {
377 190 setAxis(m_axisXItem,axis);
378 191 }
379 192 void QChart::setAxisY(const QChartAxis& axis)
380 193 {
381 194 setAxis(m_axisYItem.at(0),axis);
382 195 }
383 196
384 197 void QChart::setAxisY(const QList<QChartAxis>& axis)
385 198 {
386 199 //TODO not implemented
387 200 }
388 201
389 202 void QChart::setAxis(AxisItem *item, const QChartAxis& axis)
390 203 {
391 204 item->setVisible(axis.isAxisVisible());
392 205 }
393 206
394 207 void QChart::resizeEvent(QGraphicsSceneResizeEvent *event)
395 208 {
396 209
397 210 m_rect = QRectF(QPoint(0,0),event->newSize());
398 211 QRectF rect = m_rect.adjusted(margin(),margin(), -margin(), -margin());
399 212
400 213 // recalculate title position
401 214 if (m_titleItem) {
402 215 QPointF center = m_rect.center() -m_titleItem->boundingRect().center();
403 216 m_titleItem->setPos(center.x(),m_rect.top()/2 + margin()/2);
404 217 }
405 218
406 219 //recalculate background gradient
407 220 if (m_backgroundItem) {
408 221 m_backgroundItem->setRect(rect);
409 222 }
410 223
411 // resize and reposition childs
412 foreach (ChartItem *item, m_chartItems) {
413 item->setPos(rect.topLeft());
414 item->setSize(rect.size());
415 }
416
417 224 QGraphicsWidget::resizeEvent(event);
418 225 update();
419 226 }
420 227
421 228
422 229
423 230 #include "moc_qchart.cpp"
424 231
425 232 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,107 +1,101
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 GradientOrientation {
37 HorizonatlGradientOrientation,
38 VerticalGradientOrientation
39 };
40 36 enum ChartThemeId {
41 37 ChartThemeInvalid = -1,
42 38 /*! The default theme follows the GUI style of the Operating System */
43 39 ChartThemeDefault,
44 40 ChartThemeVanilla,
45 41 ChartThemeIcy,
46 42 ChartThemeGrayscale,
47 43 ChartThemeScientific,
48 44 ChartThemeUnnamed1
49 45 };
50 46
51 47 public:
52 48 QChart(QGraphicsItem *parent = 0, Qt::WindowFlags wFlags = 0);
53 49 ~QChart();
54 50
55 51 void addSeries(QChartSeries* series);
56 52
57 53 //TODO: QChartSeries* createSeries(QSeriesData *data, QChartSeries::QChartSeriesType type);
58 54 // TODO: who owns the series now? maybe owned by chart and returned a reference instead...
59 55 QChartSeries* createSeries(QChartSeries::QChartSeriesType type);
60 56
61 57 void setMargin(int margin);
62 58 int margin() const;
63 59 void setTheme(QChart::ChartThemeId theme);
64 60 QChart::ChartThemeId theme();
65 61
66 62 void setTitle(const QString& title,const QFont& font = QFont());
67 63 void setChartBackgroundBrush(const QBrush& brush);
68 64 void setChartBackgroundPen(const QPen& pen);
69 65
70 66 void zoomInToRect(const QRectF& rectangle);
71 67 void zoomIn();
72 68 void zoomOut();
73 69 void zoomReset();
74 70
75 71 void setAxisX(const QChartAxis& axis);
76 72 void setAxisY(const QChartAxis& axis);
77 73 void setAxisY(const QList<QChartAxis>& axis);
78 74
79 75 protected:
80 76 void resizeEvent(QGraphicsSceneResizeEvent *event);
81 77
82 78 private:
83 79 void setAxis(AxisItem *item, const QChartAxis& axis);
84 80
85 81 private:
86 82 Q_DISABLE_COPY(QChart)
87 83 QGraphicsRectItem* m_backgroundItem;
88 GradientOrientation m_bacgroundOrinetation;
89 84 QGraphicsTextItem* m_titleItem;
90 85 AxisItem* m_axisXItem;
91 86 QList<AxisItem*> m_axisYItem;
92 87 QRectF m_rect;
93 88 QList<QChartSeries *> m_chartSeries;
94 89 QList<ChartItem *> m_chartItems;
95 90 QVector<PlotDomain> m_plotDomainList;
96 91 int m_plotDataIndex;
97 int m_marginSize;
98 92 ChartTheme *m_chartTheme;
99 93
100 94
101 95 ChartDataSet *m_dataset;
102 96 ChartPresenter *m_presenter;
103 97 };
104 98
105 99 QTCOMMERCIALCHART_END_NAMESPACE
106 100
107 101 #endif
@@ -1,213 +1,202
1 1 #include "xylinechartitem_p.h"
2 2 #include "qxychartseries.h"
3 3 #include "chartpresenter_p.h"
4 4 #include <QPainter>
5 5 #include <QStyleOptionGraphicsItem>
6 6
7 7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 8
9 9 XYLineChartItem::XYLineChartItem(ChartPresenter* presenter, QXYChartSeries* series,QGraphicsItem *parent):ChartItem(parent),
10 10 m_presenter(presenter),
11 11 m_series(series),
12 12 m_dirtyData(false),
13 13 m_dirtyGeometry(false),
14 14 m_dirtyDomain(false)
15 15 {
16 QObject::connect(series,SIGNAL(changed(int)),this,SLOT(handleSeriesChanged(int)));
17 16 }
18 17
19 18 QRectF XYLineChartItem::boundingRect() const
20 19 {
21 20 return m_rect;
22 21 }
23 22
24 23 QPainterPath XYLineChartItem::shape() const
25 24 {
26 25 return m_path;
27 26 }
28 27
29 void XYLineChartItem::setSize(const QSizeF& size)
30 {
31 m_size=size;
32 m_dirtyGeometry=true;
33 }
34
35 void XYLineChartItem::setDomain(const Domain& domain)
36 {
37 m_domain=domain;
38 m_dirtyDomain=true;
39 }
40
41 void XYLineChartItem::setSeries(QXYChartSeries* series)
42 {
43 m_series = series;
44 m_dirtyData=true;
45 }
46 28
47 29 void XYLineChartItem::addPoints(const QVector<QPointF>& points)
48 30 {
49 31 m_data = points;
50 32 for(int i=0; i<m_data.size();i++){
51 33 const QPointF& point =m_data[i];
52 34 QGraphicsRectItem* item = new QGraphicsRectItem(0,0,3,3,this);
53 35 item->setPos(point.x()-1,point.y()-1);
54 36 m_points << item;
55 37 }
56 38 }
57 39
58 40 void XYLineChartItem::addPoint(const QPointF& point)
59 41 {
60 42 m_data << point;
61 43 QGraphicsRectItem* item = new QGraphicsRectItem(0,0,3,3,this);
62 44 item->setPos(point.x()-1,point.y()-1);
63 45 m_points << item;
64 46 }
65 47
66 48 void XYLineChartItem::removePoint(const QPointF& point)
67 49 {
68 50 Q_ASSERT(m_data.count() == m_points.count());
69 51 int index = m_data.lastIndexOf(point,0);
70 52 m_data.remove(index);
71 53 delete(m_points.takeAt(index));
72 54 }
73 55
74 56 void XYLineChartItem::setPoint(const QPointF& oldPoint,const QPointF& newPoint)
75 57 {
76 58 Q_ASSERT(m_data.count() == m_points.count());
77 59 int index = m_data.lastIndexOf(oldPoint,0);
78 60
79 61 if(index > -1){
80 62 m_data.replace(index,newPoint);
81 63 QGraphicsItem* item = m_points.at(index);
82 64 item->setPos(newPoint.x()-1,newPoint.y()-1);
83 65 }
84 66 }
85 67
86 68 void XYLineChartItem::setPoint(int index,const QPointF& point)
87 69 {
88 70 Q_ASSERT(m_data.count() == m_points.count());
89 71 Q_ASSERT(index>=0);
90 72
91 73 m_data.replace(index,point);
92 74 QGraphicsItem* item = m_points.at(index);
93 75 item->setPos(point.x()-1,point.y()-1);
94 76 }
95 77
96 78 void XYLineChartItem::clear()
97 79 {
98 80 qDeleteAll(m_points);
99 81 m_points.clear();
100 82 m_hash.clear();
101 83 m_path = QPainterPath();
102 84 m_rect = QRect();
103 85 m_data.clear();
104 86 }
105 87
106 88 void XYLineChartItem::clearView()
107 89 {
108 90 qDeleteAll(m_points);
109 91 m_points.clear();
110 92 m_path = QPainterPath();
111 93 m_rect = QRect();
112 94 m_data.clear();
113 95 }
114 96
115 void XYLineChartItem::handleSeriesChanged(int index)
116 {
117 Q_ASSERT(index<m_series->count());
118 if(m_hash.contains(index)){
119 int i = m_hash.value(index);
120 QPointF point;
121 calculatePoint(point,index,m_series,m_size,m_domain);
122 setPoint(i,point);
123 }
124 }
125
126 97 void XYLineChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
127 98 {
128 99 Q_UNUSED(widget);
129 100 Q_UNUSED(option);
101 painter->setPen(m_pen);
130 102 painter->drawPath(m_path);
131 103 }
132 104
133 105 void XYLineChartItem::calculatePoint(QPointF& point, int index, const QXYChartSeries* series,const QSizeF& size, const Domain& domain) const
134 106 {
135 107 const qreal deltaX = size.width()/domain.spanX();
136 108 const qreal deltaY = size.height()/domain.spanY();
137 109 qreal x = (series->x(index) - domain.m_minX)* deltaX;
138 110 qreal y = (series->y(index) - domain.m_minY)*-deltaY + size.height();
139 111 point.setX(x);
140 112 point.setY(y);
141 113 }
142 114
143 115
144 116 void XYLineChartItem::calculatePoints(QVector<QPointF>& points, QHash<int,int>& hash,const QXYChartSeries* series,const QSizeF& size, const Domain& domain) const
145 117 {
146 118 const qreal deltaX = size.width()/domain.spanX();
147 119 const qreal deltaY = size.height()/domain.spanY();
148 120
149 121 for (int i = 0; i < series->count(); ++i) {
150 122 qreal x = (series->x(i) - domain.m_minX)* deltaX;
151 123 if(x<0 || x > size.width()) continue;
152 124 qreal y = (series->y(i) - domain.m_minY)*-deltaY + size.height();
153 125 if(y<0 || y > size.height()) continue;
154 126 hash[i] = points.size();
155 127 points << QPointF(x,y);
156 128 }
157 129 }
158 130
159 131 void XYLineChartItem::updateDomain()
160 132 {
161 133 clear();
162 134 calculatePoints(m_data,m_hash,m_series,m_size, m_domain);
163 135 addPoints(m_data);
164 m_dirtyGeometry = true;
165 136 }
166 137
167 138 void XYLineChartItem::updateData()
168 139 {
169 140 //for now the same
170 141 updateDomain();
171 142 }
172 143
173 144 void XYLineChartItem::updateGeometry()
174 145 {
175 146
176 147 if(m_data.size()==0) return;
177 148
178 149 prepareGeometryChange();
179 150 QPainterPath path;
180 151 const QPointF& point = m_data.at(0);
181 152 path.moveTo(point);
182 153
183 154 foreach( const QPointF& point , m_data) {
184 155 path.lineTo(point);
185 156 }
186 157
187 158 m_path = path;
188 159 m_rect = path.boundingRect();
189 160 }
190 161
191 void XYLineChartItem::updateItem()
162 void XYLineChartItem::setPen(const QPen& pen)
192 163 {
193 if(m_dirtyDomain) {
194 updateDomain();
195 m_dirtyData = false;
196 }
164 m_pen = pen;
165 }
197 166
198 if(m_dirtyData) {
199 updateData();
200 m_dirtyData = false;
201 }
167 //handlers
202 168
203 if(m_dirtyGeometry) {
204 updateGeometry();
205 m_dirtyGeometry = false;
169 void XYLineChartItem::handleModelChanged(int index)
170 {
171 Q_ASSERT(index<m_series->count());
172 if(m_hash.contains(index)){
173 int i = m_hash.value(index);
174 QPointF point;
175 calculatePoint(point,index,m_series,m_size,m_domain);
176 setPoint(i,point);
206 177 }
178 update();
179 }
207 180
181 void XYLineChartItem::handleDomainChanged(const Domain& domain)
182 {
183 m_domain = domain;
184 updateDomain();
208 185 update();
209 186 }
210 187
188 void XYLineChartItem::handleGeometryChanged(const QRectF& rect)
189 {
190 Q_ASSERT(rect.isValid());
191
192 m_size=rect.size();
193 updateDomain();
194 updateGeometry();
195 setPos(rect.topLeft());
196 update();
197 }
198
199
211 200 #include "moc_xylinechartitem_p.cpp"
212 201
213 202 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,74 +1,79
1 1 #ifndef XYLINECHARTITEM_H
2 2 #define XYLINECHARTITEM_H
3 3
4 4 #include "qchartglobal.h"
5 5 #include "chartitem_p.h"
6 #include <QPen>
6 7
7 8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 9
9 10 class ChartPresenter;
10 11 class QXYChartSeries;
11 12 class LineChartAnimationItem;
12 13
13 14 class XYLineChartItem : public QObject , public ChartItem
14 15 {
15 16 Q_OBJECT
16 17 public:
17 18 XYLineChartItem(ChartPresenter* presenter, QXYChartSeries* series,QGraphicsItem *parent = 0);
18 19 ~ XYLineChartItem(){};
19 20
20 21 //from QGraphicsItem
21 22 QRectF boundingRect() const;
22 23 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
23 24 QPainterPath shape() const;
25
26
27 void setPen(const QPen& pen);
28
24 29 //from ChartItem
25 void setSize(const QSizeF& size);
30 void setSize(const QSizeF& size){};
26 31 void setPlotDomain(const PlotDomain& data){};
27 void setDomain(const Domain& data);
28 void setSeries(QXYChartSeries* series);
29 const Domain& domain() const { return m_domain;}
30 //ChartAnimationManager* animationManager();
31 32
32 void updateItem();
33
34 const Domain& domain() const { return m_domain;}
33 35
34 36 virtual void addPoint(const QPointF& );
35 37 virtual void addPoints(const QVector<QPointF>& points);
36 38 virtual void removePoint(const QPointF& point);
37 39 virtual void setPoint(const QPointF& oldPoint, const QPointF& newPoint);
38 40 virtual void setPoint(int index,const QPointF& point);
39 41 void clear();
40 42 void clearView();
41 43 int count() const { return m_data.size();}
42 44
43 45 const QVector<QPointF>& points(){ return m_data;}
44 46
45 47 protected:
46 48 virtual void updateGeometry();
47 49 virtual void updateData();
48 50 virtual void updateDomain();
49 51 //refactor
50 52 void calculatePoint(QPointF& point, int index, const QXYChartSeries* series,const QSizeF& size, const Domain& domain) const;
51 53 void calculatePoints(QVector<QPointF>& points,QHash<int,int>& hash,const QXYChartSeries* series, const QSizeF& size, const Domain& domain) const;
52 54
53 private slots:
54 void handleSeriesChanged(int index);
55 protected slots:
56 void handleModelChanged(int index);
57 void handleDomainChanged(const Domain& domain);
58 void handleGeometryChanged(const QRectF& size);
55 59
56 60 private:
57 61 ChartPresenter* m_presenter;
58 62 QPainterPath m_path;
59 63 QSizeF m_size;
60 64 QRectF m_rect;
61 65 Domain m_domain;
62 66 QList<QGraphicsItem*> m_points;
63 67 QVector<QPointF> m_data;
64 68 QHash<int,int> m_hash;
65 69 QXYChartSeries* m_series;
70 QPen m_pen;
66 71 bool m_dirtyData;
67 72 bool m_dirtyGeometry;
68 73 bool m_dirtyDomain;
69 74
70 75 };
71 76
72 77 QTCOMMERCIALCHART_END_NAMESPACE
73 78
74 79 #endif
General Comments 0
You need to be logged in to leave comments. Login now