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