##// END OF EJS Templates
refactored barcharts. layout to derived classess other funtionality to base class
sauimone -
r126:2dc5911e790e
parent child
Show More
@@ -0,0 +1,109
1 #include <limits.h>
2 #include <QDebug>
3 #include "barchartseriesbase.h"
4 #include "bargroup.h"
5
6 QTCOMMERCIALCHART_BEGIN_NAMESPACE
7
8 BarChartSeriesBase::BarChartSeriesBase(QObject *parent)
9 : QChartSeries(parent)
10 {
11 }
12
13 bool BarChartSeriesBase::setData(QAbstractItemModel* model)
14 {
15 mModel = model;
16 return true;
17 }
18
19 int BarChartSeriesBase::min()
20 {
21 Q_ASSERT(mModel->rowCount() > 0);
22 Q_ASSERT(mModel->columnCount() > 0);
23
24 // TODO: make min and max members and update them when data changes.
25 // This is slower since they are checked every time, even if data is same since previous call.
26 int min = INT_MAX;
27
28 for (int i=0; i <mModel->rowCount(); i++) {
29 for(int j=0; j<mModel->columnCount(); j++) {
30 int temp = mModel->data(mModel->index(i,j)).toInt();
31 if (temp < min) {
32 min = temp;
33 }
34 }
35 }
36 return min;
37 }
38
39 int BarChartSeriesBase::max()
40 {
41 Q_ASSERT(mModel->rowCount() > 0);
42 Q_ASSERT(mModel->columnCount() > 0);
43
44 // TODO: make min and max members and update them when data changes.
45 // This is slower since they are checked every time, even if data is same since previous call.
46 int max = INT_MIN;
47
48 for (int i=0; i <mModel->rowCount(); i++) {
49 for(int j=0; j<mModel->columnCount(); j++) {
50 int temp = mModel->data(mModel->index(i,j)).toInt();
51 if (temp > max) {
52 max = temp;
53 }
54 }
55 }
56 return max;
57 }
58
59 int BarChartSeriesBase::maxColumnSum()
60 {
61 Q_ASSERT(mModel->rowCount() > 0);
62 Q_ASSERT(mModel->columnCount() > 0);
63
64 int max = INT_MIN;
65
66 for (int col=0; col <mModel->columnCount(); col++) {
67 int sum = columnSum(col);
68 if (sum > max) {
69 max = sum;
70 }
71 }
72 return max;
73 }
74
75 int BarChartSeriesBase::countRows()
76 {
77 return mModel->rowCount();
78 }
79
80 int BarChartSeriesBase::countColumns()
81 {
82 return mModel->columnCount();
83 }
84
85 int BarChartSeriesBase::countTotalItems()
86 {
87 return mModel->rowCount() * mModel->columnCount();
88 }
89
90 int BarChartSeriesBase::valueAt(int row, int column)
91 {
92 QModelIndex index = mModel->index(row,column);
93 return mModel->data(index).toInt();
94 }
95
96 int BarChartSeriesBase::columnSum(int column)
97 {
98 int sum(0);
99 int count = mModel->rowCount();
100
101 for (int row = 0; row < count; row++) {
102 sum += mModel->data(mModel->index(row,column)).toInt();
103 }
104 return sum;
105 }
106
107 #include "moc_barchartseriesbase.cpp"
108
109 QTCOMMERCIALCHART_END_NAMESPACE
@@ -0,0 +1,49
1 #ifndef BARCHARTSERIESBASE_H
2 #define BARCHARTSERIESBASE_H
3
4 #include <QList>
5 #include <QAbstractItemModel>
6 #include "qchartseries.h"
7 #include "qchartglobal.h"
8
9 class BarGroupBase;
10
11 QTCOMMERCIALCHART_BEGIN_NAMESPACE
12
13 // Container for series
14 class QTCOMMERCIALCHART_EXPORT BarChartSeriesBase : public QChartSeries
15 {
16 Q_OBJECT
17 protected:
18 BarChartSeriesBase(QObject* parent=0);
19
20 public:
21 // from QChartSeries
22 virtual QChartSeriesType type() const { return QChartSeries::SeriesTypeInvalid; }
23
24 // TODO: Better data model?
25 virtual bool setData(QAbstractItemModel* model);
26
27 // Methods to find out minimum and maximum values of data
28 int min();
29 int max();
30 int maxColumnSum(); // returns maximum sum of items in all columns.
31
32 int countRows();
33 int countColumns(); // Count items in one series.
34 int countTotalItems();
35 int valueAt(int row, int column);
36
37 int columnSum(int column);
38
39 public Q_SLOTS:
40
41 private:
42
43 QAbstractItemModel* mModel;
44 BarGroupBase* mBarGroup;
45 };
46
47 QTCOMMERCIALCHART_END_NAMESPACE
48
49 #endif // BARCHARTSERIESBASE_H
@@ -0,0 +1,122
1 #include "bargroupbase.h"
2 #include "bar_p.h"
3 #include "barlabel_p.h"
4 #include "separator_p.h"
5 #include "barchartseriesbase.h"
6 #include <QDebug>
7
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9
10 BarGroupBase::BarGroupBase(BarChartSeriesBase& series, QGraphicsItem *parent)
11 : ChartItem(parent)
12 ,mSeries(series)
13 ,mBarDefaultWidth(20) // TODO: remove hard coding, when we have layout code ready
14 ,mLayoutSet(false)
15 ,mLayoutDirty(true)
16 ,mTheme(0)
17 ,mSeparatorsVisible(true)
18 {
19 dataChanged();
20 }
21
22 void BarGroupBase::setSeparatorsVisible(bool visible)
23 {
24 mSeparatorsVisible = visible;
25 }
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)
42 {
43 if (!mLayoutSet) {
44 qDebug() << "BarGroupBase::paint called without layout set. Aborting.";
45 return;
46 }
47 if (mLayoutDirty) {
48 // Layout or data has changed. Need to redraw.
49 foreach(QGraphicsItem* i, childItems()) {
50 i->paint(painter,option,widget);
51 }
52 }
53 }
54
55 QRectF BarGroupBase::boundingRect() const
56 {
57 return QRectF(0,0,mWidth,mHeight);
58 }
59
60 void BarGroupBase::themeChanged(ChartTheme *theme)
61 {
62 mTheme = theme;
63 }
64
65 void BarGroupBase::setBarWidth( int w )
66 {
67 mBarDefaultWidth = w;
68 }
69
70 int BarGroupBase::addColor( QColor color )
71 {
72 int colorIndex = mColors.count();
73 mColors.append(color);
74 return colorIndex;
75 }
76
77 void BarGroupBase::resetColors()
78 {
79 mColors.clear();
80 }
81
82 void BarGroupBase::dataChanged()
83 {
84 qDebug() << "BarGroupBase::dataChanged";
85
86 // Find out maximum and minimum of all series
87 mMax = mSeries.max();
88 mMin = mSeries.min();
89
90 // Delete old bars
91 foreach (QGraphicsItem* item, childItems()) {
92 delete item;
93 }
94
95 // Create new graphic items for bars
96 int totalItems = mSeries.countTotalItems();
97 for (int i=0; i<totalItems; i++) {
98 Bar *bar = new Bar(this);
99 childItems().append(bar);
100 }
101
102 // TODO: labels from series. This creates just some example labels
103 int count = mSeries.countColumns();
104 for (int i=0; i<count; i++) {
105 BarLabel* label = new BarLabel(this);
106 QString text("Label " + QString::number(i));
107 label->set(text);
108 childItems().append(label);
109 }
110
111 count = mSeries.countColumns() - 1; // There is one less separator than columns
112 for (int i=0; i<count; i++) {
113 Separator* sep = new Separator(this);
114 sep->setColor(QColor(255,0,0,255)); // TODO: color for separations from theme
115 childItems().append(sep);
116 }
117
118 // TODO: if (autolayout) { layoutChanged() } or something
119 mLayoutDirty = true;
120 }
121
122 QTCOMMERCIALCHART_END_NAMESPACE
@@ -0,0 +1,64
1 #ifndef BARGROUPBASE_H
2 #define BARGROUPBASE_H
3
4 #include "charttheme_p.h"
5 #include "chartitem_p.h"
6 //#include "barlabel_p.h"
7 //#include "bar_p.h"
8 #include "barchartseriesbase.h"
9 #include <QGraphicsItem>
10
11 QTCOMMERCIALCHART_BEGIN_NAMESPACE
12
13 // Base Class for bar groups. Common implemantation of different groups. Not to be instantiated.
14
15 class BarGroupBase : public ChartItem, public ChartThemeObserver
16 {
17
18 public:
19 BarGroupBase(BarChartSeriesBase& series, QGraphicsItem *parent = 0);
20 void setSeparatorsVisible(bool visible = true);
21
22 public: // From ChartItem
23 void setSize(const QSizeF &size);
24 void setPlotDomain(const PlotDomain& data);
25
26 // From QGraphicsItem
27 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
28 QRectF boundingRect() const;
29
30 // From ChartThemeObserver
31 void themeChanged(ChartTheme *theme);
32
33 // TODO: these may change with layout awarness.
34 void setBarWidth( int w );
35 int addColor( QColor color );
36 void resetColors();
37
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
40
41 protected:
42
43 BarChartSeriesBase& mSeries;
44
45 int mMin; // Min and max values of data. (updated when data is changed, used when drawing)
46 int mMax;
47
48 int mHeight; // Layout spesific
49 int mWidth;
50 int mBarDefaultWidth;
51
52 bool mLayoutSet; // True, if component has been laid out.
53 bool mLayoutDirty;
54
55 QList<QColor> mColors; // List of colors for series for now
56
57 ChartTheme* mTheme;
58 bool mSeparatorsVisible;
59
60 };
61
62 QTCOMMERCIALCHART_END_NAMESPACE
63
64 #endif // BARGROUPBASE_H
@@ -0,0 +1,49
1 #include "separator_p.h"
2 #include <QDebug>
3 #include <QPainter>
4
5 QTCOMMERCIALCHART_BEGIN_NAMESPACE
6
7 Separator::Separator(QGraphicsItem *parent)
8 : ChartItem(parent)
9 {
10 }
11
12 void Separator::setPos(qreal x, qreal y)
13 {
14 mXpos = x;
15 mYpos = y;
16 }
17
18 void Separator::setColor(QColor color)
19 {
20 mColor = color;
21 }
22
23 void Separator::setSize(const QSizeF &size)
24 {
25 mWidth = size.width();
26 mHeight = size.height();
27 }
28
29 void Separator::setPlotDomain(const PlotDomain& data)
30 {
31
32 }
33
34 void Separator::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
35 {
36 qDebug() << "separator::paint" << boundingRect();
37 QPen pen(mColor);
38 painter->setPen(pen);
39 painter->drawLine(mXpos,mYpos,mXpos,mHeight);
40 }
41
42 QRectF Separator::boundingRect() const
43 {
44 QRectF r(mXpos,mYpos,mWidth,mHeight);
45 return r;
46 }
47
48
49 QTCOMMERCIALCHART_END_NAMESPACE
@@ -0,0 +1,36
1 #ifndef SEPARATOR_H
2 #define SEPARATOR_H
3
4 #include "chartitem_p.h"
5
6 QTCOMMERCIALCHART_BEGIN_NAMESPACE
7
8 class Separator : public ChartItem
9 {
10 public:
11 Separator(QGraphicsItem *parent = 0);
12
13 void setPos(qreal x, qreal y);
14 void setColor(QColor color);
15
16 // From ChartItem
17 void setSize(const QSizeF &size);
18 void setPlotDomain(const PlotDomain& data);
19
20 // From QGraphicsItem
21 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
22 QRectF boundingRect() const;
23
24 private:
25
26 QColor mColor;
27 qreal mXpos;
28 qreal mYpos;
29 qreal mHeight;
30 qreal mWidth;
31
32 };
33
34 QTCOMMERCIALCHART_END_NAMESPACE
35
36 #endif // SEPARATOR_H
@@ -1,78 +1,11
1 #include <QDebug>
1 #include <QDebug>
2 #include "barchartseries.h"
2 #include "barchartseries.h"
3 #include "bargroup.h"
3
4 QTCOMMERCIALCHART_BEGIN_NAMESPACE
4 QTCOMMERCIALCHART_BEGIN_NAMESPACE
5
5
6 BarChartSeries::BarChartSeries(QObject *parent)
6 BarChartSeries::BarChartSeries(QObject *parent)
7 : QChartSeries(parent)
7 : BarChartSeriesBase(parent)
8 {
9 }
10
11 bool BarChartSeries::setData(QAbstractItemModel* model)
12 {
13 mModel = model;
14 return true;
15 }
16
17 int BarChartSeries::min()
18 {
19 Q_ASSERT(mModel->rowCount() > 0);
20 Q_ASSERT(mModel->columnCount() > 0);
21
22 // TODO: make min and max members and update them when data changes.
23 // This is slower since they are checked every time, even if data is same since previous call.
24 int min = INT_MAX;
25
26 for (int i=0; i <mModel->rowCount(); i++) {
27 for(int j=0; j<mModel->columnCount(); j++) {
28 int temp = mModel->data(mModel->index(i,j)).toInt();
29 if (temp < min) {
30 min = temp;
31 }
32 }
33 }
34 return min;
35 }
36
37 int BarChartSeries::max()
38 {
39 Q_ASSERT(mModel->rowCount() > 0);
40 Q_ASSERT(mModel->columnCount() > 0);
41
42 // TODO: make min and max members and update them when data changes.
43 // This is slower since they are checked every time, even if data is same since previous call.
44 int max = INT_MIN;
45
46 for (int i=0; i <mModel->rowCount(); i++) {
47 for(int j=0; j<mModel->columnCount(); j++) {
48 int temp = mModel->data(mModel->index(i,j)).toInt();
49 if (temp > max) {
50 max = temp;
51 }
52 }
53 }
54 return max;
55 }
56
57
58 int BarChartSeries::countRows()
59 {
60 return mModel->rowCount();
61 }
62
63 int BarChartSeries::countColumns()
64 {
65 return mModel->columnCount();
66 }
67
68 int BarChartSeries::countTotalItems()
69 {
70 return mModel->rowCount() * mModel->columnCount();
71 }
72
73 int BarChartSeries::valueAt(int row, int column)
74 {
8 {
75 return mModel->data(mModel->index(row,column)).toInt();
76 }
9 }
77
10
78 #include "moc_barchartseries.cpp"
11 #include "moc_barchartseries.cpp"
@@ -3,37 +3,22
3
3
4 #include <QList>
4 #include <QList>
5 #include <QAbstractItemModel>
5 #include <QAbstractItemModel>
6 #include "qchartseries.h"
6 #include "barchartseriesbase.h"
7 #include "qchartglobal.h"
8
7
9 // TODO: Can this class be combined with series?
10 class BarGroup;
8 class BarGroup;
11
9
12 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10 QTCOMMERCIALCHART_BEGIN_NAMESPACE
13
11
14 // Container for series
12 // Container for series
15 class QTCOMMERCIALCHART_EXPORT BarChartSeries : public QChartSeries
13 class QTCOMMERCIALCHART_EXPORT BarChartSeries : public BarChartSeriesBase
16 {
14 {
17 Q_OBJECT
15 Q_OBJECT
18 public:
16 public:
19 BarChartSeries(QObject* parent=0);
17 BarChartSeries(QObject* parent=0);
20
18
21 // from QChartSeries
19 // from BarChartSeriesBase
22 virtual QChartSeriesType type() const { return QChartSeries::SeriesTypeBar; }
20 virtual QChartSeriesType type() const { return QChartSeries::SeriesTypeBar; }
23
21
24 // TODO: Better data model?
25 virtual bool setData(QAbstractItemModel* model);
26
27 // Methods to find out minimum and maximum values of data
28 int min();
29 int max();
30 int countRows();
31 int countColumns(); // Count items in one series.
32 int countTotalItems();
33 int valueAt(int row, int column);
34
35 public Q_SLOTS:
36
37 private:
22 private:
38
23
39 QAbstractItemModel* mModel;
24 QAbstractItemModel* mModel;
@@ -6,101 +6,9
6 QTCOMMERCIALCHART_BEGIN_NAMESPACE
6 QTCOMMERCIALCHART_BEGIN_NAMESPACE
7
7
8 BarGroup::BarGroup(BarChartSeries& series, QGraphicsItem *parent) :
8 BarGroup::BarGroup(BarChartSeries& series, QGraphicsItem *parent) :
9 ChartItem(parent)
9 BarGroupBase(series,parent)
10 ,mSeries(series)
11 ,mBarDefaultWidth(10)
12 ,mLayoutSet(false)
13 ,mLayoutDirty(true)
14 {
10 {
15 dataChanged();
11 mBarDefaultWidth = 10;
16 }
17
18 void BarGroup::setSize(const QSizeF& size)
19 {
20 qDebug() << "BarGroup::setSize";
21 mWidth = size.width();
22 mHeight = size.height();
23 layoutChanged();
24 mLayoutSet = true;
25 }
26
27 void BarGroup::setPlotDomain(const PlotDomain& data)
28 {
29 qDebug() << "BarGroup::setPlotDomain";
30 // TODO:
31 mPlotDomain = data;
32 }
33
34 void BarGroup::setBarWidth( int w )
35 {
36 mBarDefaultWidth = w;
37 }
38
39 int BarGroup::addColor( QColor color )
40 {
41 int colorIndex = mColors.count();
42 mColors.append(color);
43 return colorIndex;
44 }
45
46 void BarGroup::resetColors()
47 {
48 mColors.clear();
49 }
50
51 void BarGroup::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
52 {
53 if (!mLayoutSet) {
54 qDebug() << "QBarChart::paint called without layout set. Aborting.";
55 return;
56 }
57 if (mLayoutDirty) {
58 // Layout or data has changed. Need to redraw.
59 foreach(QGraphicsItem* i, childItems()) {
60 i->paint(painter,option,widget);
61 mLayoutDirty = false;
62 }
63 }
64 }
65
66 QRectF BarGroup::boundingRect() const
67 {
68 return QRectF(0,0,mWidth,mHeight);
69 }
70
71
72 void BarGroup::dataChanged()
73 {
74 qDebug() << "QBarChart::dataChanged mSeries";
75
76 // Find out maximum and minimum of all series
77 mMax = mSeries.max();
78 mMin = mSeries.min();
79
80 // Delete old bars
81 // Is this correct way to delete childItems?
82 foreach (QGraphicsItem* item, childItems()) {
83 delete item;
84 }
85
86 // Create new graphic items for bars
87 int totalItems = mSeries.countTotalItems();
88 for (int i=0; i<totalItems; i++) {
89 Bar *bar = new Bar(this);
90 childItems().append(bar);
91 }
92
93 // TODO: labels from series. This creates just some example labels
94 int count = mSeries.countColumns();
95 for (int i=0; i<count; i++) {
96 BarLabel* label = new BarLabel(this);
97 QString text("Label " + QString::number(i));
98 label->set(text);
99 childItems().append(label);
100 }
101
102 // TODO: if (autolayout) { layoutChanged() } or something
103 mLayoutDirty = true;
104 }
12 }
105
13
106 void BarGroup::layoutChanged()
14 void BarGroup::layoutChanged()
@@ -1,55 +1,26
1 #ifndef QBARGROUP_H
1 #ifndef QBARGROUP_H
2 #define QBARGROUP_H
2 #define QBARGROUP_H
3
3
4 #include "chartitem_p.h"
4 #include "bargroupbase.h"
5 #include "bar_p.h"
6 #include "barchartseries.h"
5 #include "barchartseries.h"
7 #include <QGraphicsItem>
6 #include <QGraphicsItem>
8
7
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10
9
11 class BarGroup : public ChartItem
10 // Base class for bar groups
11
12 class BarGroup : public BarGroupBase
12 {
13 {
13 public:
14 public:
14 explicit BarGroup(BarChartSeries& series, QGraphicsItem *parent = 0);
15 explicit BarGroup(BarChartSeries& series, QGraphicsItem *parent = 0);
15
16
16 public: // from ChartItem
17 void setSize(const QSizeF &size);
18 void setPlotDomain(const PlotDomain& data);
19
20 // Layout "api"
21 void setPos(qreal x, qreal y);
22 void setBarWidth( int w ); // Default width for each bar
23
24 int addColor( QColor color );
25 void resetColors();
26
27 // From QGraphicsItem
28 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
29 QRectF boundingRect() const;
30
31 private:
17 private:
32
18
33 void dataChanged(); // data of series has changed -> need to recalculate bar sizes
19 // From BarGroupBase
34 void layoutChanged(); // layout has changed -> need to recalculate bar sizes
20 void layoutChanged(); // layout has changed -> need to recalculate bar sizes
35
21
36 private:
22 private:
37
38 // Data
23 // Data
39 BarChartSeries& mSeries;
40 int mMin; // Min and max values of data. (updated when data is changed, used when drawing)
41 int mMax;
42
43 int mHeight; // Layout spesific
44 int mWidth;
45 int mBarDefaultWidth;
46
47 bool mLayoutSet; // True, if component has been laid out.
48 bool mLayoutDirty;
49
50 QList<QColor> mColors; // List of colors for series for now
51
52 PlotDomain mPlotDomain;
53 };
24 };
54
25
55 QTCOMMERCIALCHART_END_NAMESPACE
26 QTCOMMERCIALCHART_END_NAMESPACE
@@ -7,104 +7,10
7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8
8
9 PercentBarChartSeries::PercentBarChartSeries(QObject *parent) :
9 PercentBarChartSeries::PercentBarChartSeries(QObject *parent) :
10 QChartSeries(parent)
10 BarChartSeriesBase(parent)
11 {
11 {
12 }
12 }
13
13
14 bool PercentBarChartSeries::setData(QAbstractItemModel* model)
15 {
16 mModel = model;
17 return true;
18 }
19
20 int PercentBarChartSeries::min()
21 {
22 Q_ASSERT(mModel->rowCount() > 0);
23 Q_ASSERT(mModel->columnCount() > 0);
24
25 // TODO: make min and max members and update them when data changes.
26 // This is slower since they are checked every time, even if data is same since previous call.
27 int min = INT_MAX;
28
29 for (int i=0; i <mModel->rowCount(); i++) {
30 for(int j=0; j<mModel->columnCount(); j++) {
31 int temp = mModel->data(mModel->index(i,j)).toInt();
32 if (temp < min) {
33 min = temp;
34 }
35 }
36 }
37 return min;
38 }
39
40 int PercentBarChartSeries::max()
41 {
42 Q_ASSERT(mModel->rowCount() > 0);
43 Q_ASSERT(mModel->columnCount() > 0);
44
45 // TODO: make min and max members and update them when data changes.
46 // This is slower since they are checked every time, even if data is same since previous call.
47 int max = INT_MIN;
48
49 for (int i=0; i <mModel->rowCount(); i++) {
50 for(int j=0; j<mModel->columnCount(); j++) {
51 int temp = mModel->data(mModel->index(i,j)).toInt();
52 if (temp > max) {
53 max = temp;
54 }
55 }
56 }
57 return max;
58 }
59
60 int PercentBarChartSeries::maxColumnSum()
61 {
62 Q_ASSERT(mModel->rowCount() > 0);
63 Q_ASSERT(mModel->columnCount() > 0);
64
65 int max = INT_MIN;
66
67 for (int col=0; col <mModel->columnCount(); col++) {
68 int sum = columnSum(col);
69 if (sum > max) {
70 max = sum;
71 }
72 }
73 return max;
74 }
75
76 int PercentBarChartSeries::countRows()
77 {
78 return mModel->rowCount();
79 }
80
81 int PercentBarChartSeries::countColumns()
82 {
83 return mModel->columnCount();
84 }
85
86 int PercentBarChartSeries::countTotalItems()
87 {
88 return mModel->rowCount() * mModel->columnCount();
89 }
90
91 int PercentBarChartSeries::valueAt(int row, int column)
92 {
93 QModelIndex index = mModel->index(row,column);
94 return mModel->data(index).toInt();
95 }
96
97 int PercentBarChartSeries::columnSum(int column)
98 {
99 int sum(0);
100 int count = mModel->rowCount();
101
102 for (int row = 0; row < count; row++) {
103 sum += mModel->data(mModel->index(row,column)).toInt();
104 }
105 return sum;
106 }
107
108 #include "moc_percentbarchartseries.cpp"
14 #include "moc_percentbarchartseries.cpp"
109
15
110 QTCOMMERCIALCHART_END_NAMESPACE
16 QTCOMMERCIALCHART_END_NAMESPACE
@@ -3,36 +3,21
3
3
4 #include <QList>
4 #include <QList>
5 #include <QAbstractItemModel>
5 #include <QAbstractItemModel>
6 #include "qchartseries.h"
6 #include "barchartseriesbase.h"
7
7
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9
9
10 class PercentBarGroup;
10 class PercentBarGroup;
11
11
12 class QTCOMMERCIALCHART_EXPORT PercentBarChartSeries : public QChartSeries
12 class QTCOMMERCIALCHART_EXPORT PercentBarChartSeries : public BarChartSeriesBase
13 {
13 {
14 Q_OBJECT
14 Q_OBJECT
15 public:
15 public:
16 PercentBarChartSeries(QObject* parent=0);
16 PercentBarChartSeries(QObject* parent=0);
17
17
18 // from QChartSeries
18 // from BarChartSeriesBase
19 virtual QChartSeriesType type() const { return QChartSeries::SeriesTypePercentBar; }
19 virtual QChartSeriesType type() const { return QChartSeries::SeriesTypePercentBar; }
20
20
21 // TODO: Better data model?
22 virtual bool setData(QAbstractItemModel* model);
23
24 // Methods to find out minimum and maximum values of data
25 int min();
26 int max();
27 int maxColumnSum(); // returns maximum sum of items in all columns.
28
29 int countRows();
30 int countColumns();
31 int countTotalItems();
32 int valueAt(int row, int column);
33
34 int columnSum(int column);
35
36 public Q_SLOTS:
21 public Q_SLOTS:
37
22
38 private:
23 private:
@@ -1,106 +1,15
1 #include "percentbargroup.h"
1 #include "percentbargroup.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 <QDebug>
5 #include <QDebug>
5
6
6 QTCOMMERCIALCHART_BEGIN_NAMESPACE
7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
7
8
8 PercentBarGroup::PercentBarGroup(PercentBarChartSeries& series, QGraphicsItem *parent) :
9 ChartItem(parent)
10 ,mSeries(series)
11 ,mBarDefaultWidth(20) // TODO: remove hard coding, when we have layout code ready
12 ,mLayoutSet(false)
13 ,mLayoutDirty(true)
14 {
15 dataChanged();
16 }
17
18
19 void PercentBarGroup::setSize(const QSizeF& size)
20 {
21 // qDebug() << "PercentBarGroup::setSize";
22 mWidth = size.width();
23 mHeight = size.height();
24 layoutChanged();
25 mLayoutSet = true;
26 }
27
28 void PercentBarGroup::setPlotDomain(const PlotDomain& data)
29 {
30 qDebug() << "PercentBarGroup::setPlotDomain";
31 // TODO:
32 }
33
9
34 void PercentBarGroup::setBarWidth( int w )
10 PercentBarGroup::PercentBarGroup(PercentBarChartSeries& series, QGraphicsItem *parent) :
35 {
11 BarGroupBase(series, parent)
36 mBarDefaultWidth = w;
37 }
38
39 int PercentBarGroup::addColor( QColor color )
40 {
41 int colorIndex = mColors.count();
42 mColors.append(color);
43 return colorIndex;
44 }
45
46 void PercentBarGroup::resetColors()
47 {
48 mColors.clear();
49 }
50
51 void PercentBarGroup::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
52 {
53 if (!mLayoutSet) {
54 qDebug() << "QBarChart::paint called without layout set. Aborting.";
55 return;
56 }
57 if (mLayoutDirty) {
58 // Layout or data has changed. Need to redraw.
59 foreach(QGraphicsItem* i, childItems()) {
60 i->paint(painter,option,widget);
61 }
62 mLayoutDirty = false;
63 }
64 }
65
66 QRectF PercentBarGroup::boundingRect() const
67 {
68 return QRectF(0,0,mWidth,mHeight);
69 }
70
71
72 void PercentBarGroup::dataChanged()
73 {
12 {
74 qDebug() << "QBarChart::dataChanged mSeries";
75
76 // Find out maximum and minimum of all series
77 mMax = mSeries.max();
78 mMin = mSeries.min();
79
80 // Delete old bars
81 // Is this correct way to delete childItems?
82 foreach (QGraphicsItem* item, childItems()) {
83 delete item;
84 }
85
86 // Create new graphic items for bars
87 int totalItems = mSeries.countTotalItems();
88 for (int i=0; i<totalItems; i++) {
89 Bar *bar = new Bar(this);
90 childItems().append(bar);
91 }
92
93 // TODO: labels from series. This creates just some example labels
94 int count = mSeries.countColumns();
95 for (int i=0; i<count; i++) {
96 BarLabel* label = new BarLabel(this);
97 QString text("Label " + QString::number(i));
98 label->set(text);
99 childItems().append(label);
100 }
101
102 // TODO: if (autolayout) { layoutChanged() } or something
103 mLayoutDirty = true;
104 }
13 }
105
14
106 void PercentBarGroup::layoutChanged()
15 void PercentBarGroup::layoutChanged()
@@ -146,6 +55,18 void PercentBarGroup::layoutChanged()
146 labelIndex++;
55 labelIndex++;
147 xPos += xStep;
56 xPos += xStep;
148 }
57 }
58
59 // Position separators
60 int separatorIndex = labelIndex; // Separators are after labels in childItems(). TODO: better way to store these?
61 xPos = xStep + xStep/2; // Initial position is between first and second group. ie one and half steps from left.
62 for (int s=0; s < mSeries.countColumns() - 1; s++) {
63 Separator* sep = reinterpret_cast<Separator*> (childItems().at(separatorIndex));
64 sep->setPos(xPos,0);
65 sep->setSize(QSizeF(1,mHeight));
66 xPos += xStep;
67 separatorIndex++;
68 }
69
149 mLayoutDirty = true;
70 mLayoutDirty = true;
150 }
71 }
151
72
@@ -4,52 +4,23
4 #include "chartitem_p.h"
4 #include "chartitem_p.h"
5 #include "bar_p.h"
5 #include "bar_p.h"
6 #include "percentbarchartseries.h"
6 #include "percentbarchartseries.h"
7 #include "bargroupbase.h"
7 #include <QGraphicsItem>
8 #include <QGraphicsItem>
8
9
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10
11
11 class PercentBarGroup : public ChartItem
12 class PercentBarGroup : public BarGroupBase
12 {
13 {
13 public:
14 public:
14 PercentBarGroup(PercentBarChartSeries& series, QGraphicsItem *parent = 0);
15 PercentBarGroup(PercentBarChartSeries& series, QGraphicsItem *parent = 0);
15
16
16 public: // From ChartItem
17 void setSize(const QSizeF &size);
18 void setPlotDomain(const PlotDomain& data);
19
20 public:
21 // Layout "api"
22 void setPos(qreal x, qreal y);
23 void setBarWidth( int w ); // Default width for each bar
24
25 int addColor( QColor color );
26 void resetColors();
27
28 // From QGraphicsItem
29 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
30 QRectF boundingRect() const;
31
32 private:
17 private:
33
18
34 void dataChanged(); // data of series has changed -> need to recalculate bar sizes
35 void layoutChanged(); // layout has changed -> need to recalculate bar sizes
19 void layoutChanged(); // layout has changed -> need to recalculate bar sizes
36
20
37 private:
21 private:
38
22
39 // Data
23 // Data
40 PercentBarChartSeries& mSeries;
41 int mMin; // Min and max values of data. (updated when data is changed, used when drawing)
42 int mMax;
43
44 int mHeight; // Layout spesific
45 int mWidth;
46 int mBarDefaultWidth;
47
48 bool mLayoutSet; // True, if component has been laid out.
49 bool mLayoutDirty;
50
51 QList<QColor> mColors; // List of colors for series for now
52
53 };
24 };
54
25
55 QTCOMMERCIALCHART_END_NAMESPACE
26 QTCOMMERCIALCHART_END_NAMESPACE
@@ -5,104 +5,10
5 QTCOMMERCIALCHART_BEGIN_NAMESPACE
5 QTCOMMERCIALCHART_BEGIN_NAMESPACE
6
6
7 StackedBarChartSeries::StackedBarChartSeries(QObject *parent) :
7 StackedBarChartSeries::StackedBarChartSeries(QObject *parent) :
8 QChartSeries(parent)
8 BarChartSeriesBase(parent)
9 {
9 {
10 }
10 }
11
11
12 bool StackedBarChartSeries::setData(QAbstractItemModel* model)
13 {
14 mModel = model;
15 return true;
16 }
17
18 int StackedBarChartSeries::min()
19 {
20 Q_ASSERT(mModel->rowCount() > 0);
21 Q_ASSERT(mModel->columnCount() > 0);
22
23 // TODO: make min and max members and update them when data changes.
24 // This is slower since they are checked every time, even if data is same since previous call.
25 int min = INT_MAX;
26
27 for (int i=0; i <mModel->rowCount(); i++) {
28 for(int j=0; j<mModel->columnCount(); j++) {
29 int temp = mModel->data(mModel->index(i,j)).toInt();
30 if (temp < min) {
31 min = temp;
32 }
33 }
34 }
35 return min;
36 }
37
38 int StackedBarChartSeries::max()
39 {
40 Q_ASSERT(mModel->rowCount() > 0);
41 Q_ASSERT(mModel->columnCount() > 0);
42
43 // TODO: make min and max members and update them when data changes.
44 // This is slower since they are checked every time, even if data is same since previous call.
45 int max = INT_MIN;
46
47 for (int i=0; i <mModel->rowCount(); i++) {
48 for(int j=0; j<mModel->columnCount(); j++) {
49 int temp = mModel->data(mModel->index(i,j)).toInt();
50 if (temp > max) {
51 max = temp;
52 }
53 }
54 }
55 return max;
56 }
57
58 int StackedBarChartSeries::maxColumnSum()
59 {
60 Q_ASSERT(mModel->rowCount() > 0);
61 Q_ASSERT(mModel->columnCount() > 0);
62
63 int max = INT_MIN;
64
65 for (int col=0; col <mModel->columnCount(); col++) {
66 int sum = columnSum(col);
67 if (sum > max) {
68 max = sum;
69 }
70 }
71 return max;
72 }
73
74 int StackedBarChartSeries::countRows()
75 {
76 return mModel->rowCount();
77 }
78
79 int StackedBarChartSeries::countColumns()
80 {
81 return mModel->columnCount();
82 }
83
84 int StackedBarChartSeries::countTotalItems()
85 {
86 return mModel->rowCount() * mModel->columnCount();
87 }
88
89 int StackedBarChartSeries::valueAt(int row, int column)
90 {
91 QModelIndex index = mModel->index(row,column);
92 return mModel->data(index).toInt();
93 }
94
95 int StackedBarChartSeries::columnSum(int column)
96 {
97 int sum(0);
98 int count = mModel->rowCount();
99
100 for (int row = 0; row < count; row++) {
101 sum += mModel->data(mModel->index(row,column)).toInt();
102 }
103 return sum;
104 }
105
106 #include "moc_stackedbarchartseries.cpp"
12 #include "moc_stackedbarchartseries.cpp"
107
13
108 QTCOMMERCIALCHART_END_NAMESPACE
14 QTCOMMERCIALCHART_END_NAMESPACE
@@ -3,13 +3,13
3
3
4 #include <QList>
4 #include <QList>
5 #include <QAbstractItemModel>
5 #include <QAbstractItemModel>
6 #include "qchartseries.h"
6 #include "barchartseriesbase.h"
7
7
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9
9
10 class StackedBarGroup;
10 class StackedBarGroup;
11
11
12 class QTCOMMERCIALCHART_EXPORT StackedBarChartSeries : public QChartSeries
12 class QTCOMMERCIALCHART_EXPORT StackedBarChartSeries : public BarChartSeriesBase
13 {
13 {
14 Q_OBJECT
14 Q_OBJECT
15 public:
15 public:
@@ -18,22 +18,6 public:
18 // from QChartSeries
18 // from QChartSeries
19 virtual QChartSeriesType type() const { return QChartSeries::SeriesTypeStackedBar; }
19 virtual QChartSeriesType type() const { return QChartSeries::SeriesTypeStackedBar; }
20
20
21 // TODO: Better data model?
22 virtual bool setData(QAbstractItemModel* model);
23
24 // Methods to find out minimum and maximum values of data
25 int min();
26 int max();
27 int maxColumnSum(); // returns maximum sum of items in all columns.
28
29 int countRows();
30 int countColumns();
31 int countTotalItems();
32 int valueAt(int row, int column);
33
34 private:
35 int columnSum(int column);
36
37 public Q_SLOTS:
21 public Q_SLOTS:
38
22
39 private:
23 private:
@@ -1,130 +1,15
1 #include "stackedbargroup.h"
1 #include "stackedbargroup.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 <QDebug>
5 #include <QDebug>
5 #include <QPainter>
6 #include <QPainter>
6
7
7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8
9
9 StackedBarGroup::StackedBarGroup(StackedBarChartSeries& series, QGraphicsItem *parent) :
10 StackedBarGroup::StackedBarGroup(StackedBarChartSeries& series, QGraphicsItem *parent) :
10 ChartItem(parent)
11 BarGroupBase(series,parent)
11 ,mSeries(series)
12 ,mBarDefaultWidth(20) // TODO: remove hard coding, when we have layout code ready
13 ,mLayoutSet(false)
14 ,mLayoutDirty(true)
15 ,mTheme(0)
16 ,mSeparatorsVisible(true)
17 {
12 {
18 dataChanged();
19 }
20
21 void StackedBarGroup::setSeparatorsVisible(bool visible)
22 {
23 mSeparatorsVisible = visible;
24 }
25
26 void StackedBarGroup::setSize(const QSizeF& size)
27 {
28 mWidth = size.width();
29 mHeight = size.height();
30 layoutChanged();
31 mLayoutSet = true;
32 }
33
34 void StackedBarGroup::setPlotDomain(const PlotDomain& data)
35 {
36 qDebug() << "StackedBarGroup::setPlotDomain";
37 // TODO:
38 }
39
40 void StackedBarGroup::themeChanged(ChartTheme *theme)
41 {
42 mTheme = theme;
43 }
44
45 void StackedBarGroup::setBarWidth( int w )
46 {
47 mBarDefaultWidth = w;
48 }
49
50 int StackedBarGroup::addColor( QColor color )
51 {
52 int colorIndex = mColors.count();
53 mColors.append(color);
54 return colorIndex;
55 }
56
57 void StackedBarGroup::resetColors()
58 {
59 mColors.clear();
60 }
61
62 void StackedBarGroup::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
63 {
64 if (!mLayoutSet) {
65 qDebug() << "QBarChart::paint called without layout set. Aborting.";
66 return;
67 }
68 if (mLayoutDirty) {
69 // Layout or data has changed. Need to redraw.
70 foreach(QGraphicsItem* i, childItems()) {
71 i->paint(painter,option,widget);
72 }
73 if (mSeparatorsVisible) {
74 //TODO: own class for separators (graphicsitem), because they may have style etc later.
75 // this is just to see that the positions are calculated correctly.
76 QPen pen(QColor(0,0,255,255));
77 painter->setPen(pen);
78 for (int i=0; i<mSeparatorPositions.count(); i++ ) {
79 qreal xp = mSeparatorPositions.at(i);
80 painter->drawLine(xp,0,xp,mHeight);
81 }
82 }
83 // mLayoutDirty = false;
84 }
85
86 }
87
88 QRectF StackedBarGroup::boundingRect() const
89 {
90 return QRectF(0,0,mWidth,mHeight);
91 }
92
93
94 void StackedBarGroup::dataChanged()
95 {
96 qDebug() << "QBarChart::dataChanged";
97
98 // Find out maximum and minimum of all series
99 mMax = mSeries.max();
100 mMin = mSeries.min();
101
102 // Delete old bars
103 // Is this correct way to delete childItems?
104 foreach (QGraphicsItem* item, childItems()) {
105 delete item;
106 }
107
108 // Create new graphic items for bars
109 int totalItems = mSeries.countTotalItems();
110 for (int i=0; i<totalItems; i++) {
111 Bar *bar = new Bar(this);
112 childItems().append(bar);
113 }
114
115 // TODO: labels from series. This creates just some example labels
116 int count = mSeries.countColumns();
117 for (int i=0; i<count; i++) {
118 BarLabel* label = new BarLabel(this);
119 QString text("Label " + QString::number(i));
120 label->set(text);
121 childItems().append(label);
122 }
123
124 mSeparatorPositions.clear();
125
126 // TODO: if (autolayout) { layoutChanged() } or something
127 mLayoutDirty = true;
128 }
13 }
129
14
130 void StackedBarGroup::layoutChanged()
15 void StackedBarGroup::layoutChanged()
@@ -178,12 +63,16 void StackedBarGroup::layoutChanged()
178 xPos += xStep;
63 xPos += xStep;
179 }
64 }
180
65
66
181 // Position separators
67 // Position separators
182 mSeparatorPositions.clear();
68 int separatorIndex = labelIndex; // Separators are after labels in childItems(). TODO: better way to store these?
183 xPos = xStep + xStep/2; // Initial position is between first and second group. ie one and half steps from left.
69 xPos = xStep + xStep/2; // Initial position is between first and second group. ie one and half steps from left.
184 for (int s=0; s < mSeries.countColumns() - 1; s++) {
70 for (int s=0; s < mSeries.countColumns() - 1; s++) {
185 mSeparatorPositions.append(xPos);
71 Separator* sep = reinterpret_cast<Separator*> (childItems().at(separatorIndex));
72 sep->setPos(xPos,0);
73 sep->setSize(QSizeF(1,mHeight));
186 xPos += xStep;
74 xPos += xStep;
75 separatorIndex++;
187 }
76 }
188
77
189 mLayoutDirty = true;
78 mLayoutDirty = true;
@@ -1,67 +1,24
1 #ifndef STACKEDBARGROUP_H
1 #ifndef STACKEDBARGROUP_H
2 #define STACKEDBARGROUP_H
2 #define STACKEDBARGROUP_H
3
3
4 #include "charttheme_p.h"
4 #include "bargroupbase.h"
5 #include "chartitem_p.h"
6 #include "barlabel_p.h"
7 #include "bar_p.h"
8 #include "stackedbarchartseries.h"
5 #include "stackedbarchartseries.h"
9 #include <QGraphicsItem>
6 #include <QGraphicsItem>
10
7
11 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
12
9
13 // TODO: derive this from ChartObjectInterface, when setSize is back in ChartItem
10 class StackedBarGroup : public BarGroupBase
14 class StackedBarGroup : public ChartItem, public ChartThemeObserver
15 {
11 {
16 public:
12 public:
17 StackedBarGroup(StackedBarChartSeries& series, QGraphicsItem *parent = 0);
13 StackedBarGroup(StackedBarChartSeries& series, QGraphicsItem *parent = 0);
18
14
19 // Bar chart spesific
20 void setSeparatorsVisible(bool visible = true);
21
22 public: // From ChartItem
23 void setSize(const QSizeF &size);
24 void setPlotDomain(const PlotDomain& data);
25
26 // From ChartThemeObserver
27 void themeChanged(ChartTheme *theme);
28
29 public: // Layout "api"
30 void setPos(qreal x, qreal y);
31 void setBarWidth( int w ); // Default width for each bar
32
33 int addColor( QColor color );
34 void resetColors();
35
36 // From QGraphicsItem
37 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
38 QRectF boundingRect() const;
39
40 private:
15 private:
41
16 // From BarGroupBase
42 void dataChanged(); // data of series has changed -> need to recalculate bar sizes
43 void layoutChanged(); // layout has changed -> need to recalculate bar sizes
17 void layoutChanged(); // layout has changed -> need to recalculate bar sizes
44
18
45 private:
19 private:
46
20
47 // Data
21 // Data
48 StackedBarChartSeries& mSeries;
49 int mMin; // Min and max values of data. (updated when data is changed, used when drawing)
50 int mMax;
51
52 int mHeight; // Layout spesific
53 int mWidth;
54 int mBarDefaultWidth;
55
56 bool mLayoutSet; // True, if component has been laid out.
57 bool mLayoutDirty;
58
59 QList<QColor> mColors; // List of colors for series for now
60
61 ChartTheme* mTheme;
62 bool mSeparatorsVisible;
63 QList<qreal> mSeparatorPositions;
64
65 };
22 };
66
23
67 QTCOMMERCIALCHART_END_NAMESPACE
24 QTCOMMERCIALCHART_END_NAMESPACE
@@ -32,12 +32,16 SOURCES += \
32 qchartview.cpp \
32 qchartview.cpp \
33 qchartseries.cpp \
33 qchartseries.cpp \
34 qchartaxis.cpp \
34 qchartaxis.cpp \
35 charttheme.cpp
35 charttheme.cpp \
36 barchart/separator.cpp \
37 barchart/bargroupbase.cpp \
38 barchart/barchartseriesbase.cpp
36
39
37 PRIVATE_HEADERS += \
40 PRIVATE_HEADERS += \
38 xylinechart/xylinechartitem_p.h \
41 xylinechart/xylinechartitem_p.h \
39 barchart/barlabel_p.h \
42 barchart/barlabel_p.h \
40 barchart/bar_p.h \
43 barchart/bar_p.h \
44 barchart/separator_p.h \
41 plotdomain_p.h \
45 plotdomain_p.h \
42 qscatterseries_p.h \
46 qscatterseries_p.h \
43 qpieseries_p.h \
47 qpieseries_p.h \
@@ -60,6 +64,8 PUBLIC_HEADERS += \
60 barchart/stackedbargroup.h \
64 barchart/stackedbargroup.h \
61 barchart/percentbarchartseries.h \
65 barchart/percentbarchartseries.h \
62 barchart/percentbargroup.h \
66 barchart/percentbargroup.h \
67 barchart/barchartseriesbase.h \
68 barchart/bargroupbase.h \
63 qchartview.h \
69 qchartview.h \
64 qchartaxis.h
70 qchartaxis.h
65
71
General Comments 0
You need to be logged in to leave comments. Login now