@@ -1,128 +1,133 | |||||
1 | #include "bargroup.h" |
|
1 | #include "bargroup.h" | |
2 | #include "bar.h" |
|
2 | #include "bar.h" | |
3 | #include <QDebug> |
|
3 | #include <QDebug> | |
4 |
|
4 | |||
5 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
5 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
6 |
|
6 | |||
7 | // TODO: singleton? |
|
7 | // TODO: singleton? | |
8 | //BarGroup* BarGroup::mBarGroupInstance = NULL; |
|
8 | //BarGroup* BarGroup::mBarGroupInstance = NULL; | |
9 |
|
9 | |||
10 | //BarGroup::BarGroup(QGraphicsItem *parent) : |
|
10 | //BarGroup::BarGroup(QGraphicsItem *parent) : | |
11 | // QGraphicsItem(parent) |
|
11 | // QGraphicsItem(parent) | |
12 | // ,mSeries(series) |
|
12 | // ,mSeries(series) | |
13 | BarGroup::BarGroup(BarChartSeries& series, QGraphicsItem *parent) : |
|
13 | BarGroup::BarGroup(BarChartSeries& series, QGraphicsItem *parent) : | |
14 | ChartItem(parent) |
|
14 | ChartItem(parent) | |
15 | ,mSeries(series) |
|
15 | ,mSeries(series) | |
16 | ,mLayoutSet(false) |
|
16 | ,mLayoutSet(false) | |
17 | ,mLayoutDirty(true) |
|
17 | ,mLayoutDirty(true) | |
18 | ,mBarDefaultWidth(10) |
|
18 | ,mBarDefaultWidth(10) | |
19 | { |
|
19 | { | |
20 | dataChanged(); |
|
20 | dataChanged(); | |
21 | } |
|
21 | } | |
22 |
|
22 | |||
23 |
|
23 | |||
24 | void BarGroup::setSize(const QSize& size) |
|
24 | void BarGroup::setSize(const QSize& size) | |
25 | { |
|
25 | { | |
26 | qDebug() << "BarGroup::setSize"; |
|
26 | qDebug() << "BarGroup::setSize"; | |
27 | mWidth = size.width(); |
|
27 | mWidth = size.width(); | |
28 | mHeight = size.height(); |
|
28 | mHeight = size.height(); | |
29 | layoutChanged(); |
|
29 | layoutChanged(); | |
30 | mLayoutSet = true; |
|
30 | mLayoutSet = true; | |
31 | } |
|
31 | } | |
32 |
|
32 | |||
33 | void BarGroup::setPlotDomain(const PlotDomain& data) |
|
33 | void BarGroup::setPlotDomain(const PlotDomain& data) | |
34 | { |
|
34 | { | |
35 | qDebug() << "BarGroup::setPlotDomain"; |
|
35 | qDebug() << "BarGroup::setPlotDomain"; | |
36 | // TODO: |
|
36 | // TODO: | |
37 | } |
|
37 | } | |
38 |
|
38 | |||
39 | void BarGroup::setBarWidth( int w ) |
|
39 | void BarGroup::setBarWidth( int w ) | |
40 | { |
|
40 | { | |
41 | mBarDefaultWidth = w; |
|
41 | mBarDefaultWidth = w; | |
42 | } |
|
42 | } | |
43 |
|
43 | |||
44 | int BarGroup::addColor( QColor color ) |
|
44 | int BarGroup::addColor( QColor color ) | |
45 | { |
|
45 | { | |
46 | int colorIndex = mColors.count(); |
|
46 | int colorIndex = mColors.count(); | |
47 | mColors.append(color); |
|
47 | mColors.append(color); | |
48 | return colorIndex; |
|
48 | return colorIndex; | |
49 | } |
|
49 | } | |
50 |
|
50 | |||
|
51 | void BarGroup::resetColors() | |||
|
52 | { | |||
|
53 | mColors.clear(); | |||
|
54 | } | |||
|
55 | ||||
51 | void BarGroup::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) |
|
56 | void BarGroup::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) | |
52 | { |
|
57 | { | |
53 | if (!mLayoutSet) { |
|
58 | if (!mLayoutSet) { | |
54 | qDebug() << "QBarChart::paint called without layout set. Aborting."; |
|
59 | qDebug() << "QBarChart::paint called without layout set. Aborting."; | |
55 | return; |
|
60 | return; | |
56 | } |
|
61 | } | |
57 | if (mLayoutDirty) { |
|
62 | if (mLayoutDirty) { | |
58 | // Layout or data has changed. Need to redraw. |
|
63 | // Layout or data has changed. Need to redraw. | |
59 | foreach(QGraphicsItem* i, childItems()) { |
|
64 | foreach(QGraphicsItem* i, childItems()) { | |
60 | i->paint(painter,option,widget); |
|
65 | i->paint(painter,option,widget); | |
61 | } |
|
66 | } | |
62 | } |
|
67 | } | |
63 | } |
|
68 | } | |
64 |
|
69 | |||
65 | QRectF BarGroup::boundingRect() const |
|
70 | QRectF BarGroup::boundingRect() const | |
66 | { |
|
71 | { | |
67 | return QRectF(0,0,mWidth,mHeight); |
|
72 | return QRectF(0,0,mWidth,mHeight); | |
68 | } |
|
73 | } | |
69 |
|
74 | |||
70 |
|
75 | |||
71 | void BarGroup::dataChanged() |
|
76 | void BarGroup::dataChanged() | |
72 | { |
|
77 | { | |
73 | qDebug() << "QBarChart::dataChanged mSeries"; |
|
78 | qDebug() << "QBarChart::dataChanged mSeries"; | |
74 |
|
79 | |||
75 | // Find out maximum and minimum of all series |
|
80 | // Find out maximum and minimum of all series | |
76 | mMax = mSeries.max(); |
|
81 | mMax = mSeries.max(); | |
77 | mMin = mSeries.min(); |
|
82 | mMin = mSeries.min(); | |
78 |
|
83 | |||
79 | // Delete old bars |
|
84 | // Delete old bars | |
80 | // Is this correct way to delete childItems? |
|
85 | // Is this correct way to delete childItems? | |
81 | foreach (QGraphicsItem* item, childItems()) { |
|
86 | foreach (QGraphicsItem* item, childItems()) { | |
82 | delete item; |
|
87 | delete item; | |
83 | } |
|
88 | } | |
84 |
|
89 | |||
85 | // Create new graphic items for bars |
|
90 | // Create new graphic items for bars | |
86 | int totalItems = mSeries.countTotalItems(); |
|
91 | int totalItems = mSeries.countTotalItems(); | |
87 | for (int i=0; i<totalItems; i++) { |
|
92 | for (int i=0; i<totalItems; i++) { | |
88 | Bar *bar = new Bar(this); |
|
93 | Bar *bar = new Bar(this); | |
89 | childItems().append(bar); |
|
94 | childItems().append(bar); | |
90 | } |
|
95 | } | |
91 |
|
96 | |||
92 | mLayoutDirty = true; |
|
97 | mLayoutDirty = true; | |
93 | } |
|
98 | } | |
94 |
|
99 | |||
95 | void BarGroup::layoutChanged() |
|
100 | void BarGroup::layoutChanged() | |
96 | { |
|
101 | { | |
97 | // Scale bars to new layout |
|
102 | // Scale bars to new layout | |
98 | // Layout for bars: |
|
103 | // Layout for bars: | |
99 | if (mSeries.countSeries() <= 0) { |
|
104 | if (mSeries.countSeries() <= 0) { | |
100 | // Nothing to do. |
|
105 | // Nothing to do. | |
101 | return; |
|
106 | return; | |
102 | } |
|
107 | } | |
103 |
|
108 | |||
104 | // TODO: better way to auto-layout |
|
109 | // TODO: better way to auto-layout | |
105 | int count = mSeries.countItemsInSeries(); |
|
110 | int count = mSeries.countItemsInSeries(); | |
106 | int posStep = (mWidth / (count+1)); |
|
111 | int posStep = (mWidth / (count+1)); | |
107 | int startPos = (mWidth / (count+1)) - mSeries.countSeries() * mBarDefaultWidth /2; |
|
112 | int startPos = (mWidth / (count+1)) - mSeries.countSeries() * mBarDefaultWidth /2; | |
108 | qDebug() << "startpos" << startPos; |
|
113 | qDebug() << "startpos" << startPos; | |
109 |
|
114 | |||
110 | // Scaling. |
|
115 | // Scaling. | |
111 | int itemIndex(0); |
|
116 | int itemIndex(0); | |
112 | for (int series = 0; series < mSeries.countSeries(); series++) { |
|
117 | for (int series = 0; series < mSeries.countSeries(); series++) { | |
113 | for (int item=0; item < mSeries.countItemsInSeries(); item++) { |
|
118 | for (int item=0; item < mSeries.countItemsInSeries(); item++) { | |
114 | qDebug() << itemIndex; |
|
119 | qDebug() << itemIndex; | |
115 | int barHeight = mSeries.valueAt(series, item) * mHeight / mMax; |
|
120 | int barHeight = mSeries.valueAt(series, item) * mHeight / mMax; | |
116 | Bar* bar = reinterpret_cast<Bar*> (childItems().at(itemIndex)); |
|
121 | Bar* bar = reinterpret_cast<Bar*> (childItems().at(itemIndex)); | |
117 |
|
122 | |||
118 | // TODO: width settable per bar? |
|
123 | // TODO: width settable per bar? | |
119 | bar->resize(mBarDefaultWidth, barHeight); |
|
124 | bar->resize(mBarDefaultWidth, barHeight); | |
120 | bar->setColor(mColors.at(series)); |
|
125 | bar->setColor(mColors.at(series)); | |
121 | bar->setPos(item*posStep+startPos + series * mBarDefaultWidth, mHeight); |
|
126 | bar->setPos(item*posStep+startPos + series * mBarDefaultWidth, mHeight); | |
122 | itemIndex++; |
|
127 | itemIndex++; | |
123 | } |
|
128 | } | |
124 | } |
|
129 | } | |
125 | mLayoutDirty = true; |
|
130 | mLayoutDirty = true; | |
126 | } |
|
131 | } | |
127 |
|
132 | |||
128 | QTCOMMERCIALCHART_END_NAMESPACE |
|
133 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,71 +1,71 | |||||
1 | #ifndef QBARCHART_H |
|
1 | #ifndef QBARCHART_H | |
2 | #define QBARCHART_H |
|
2 | #define QBARCHART_H | |
3 |
|
3 | |||
4 | #include "chartitem_p.h" |
|
4 | #include "chartitem_p.h" | |
5 | #include "bar.h" |
|
5 | #include "bar.h" | |
6 | #include "barchartseries.h" |
|
6 | #include "barchartseries.h" | |
7 |
|
7 | |||
8 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
8 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
9 |
|
9 | |||
10 | // TODO: Better name for this? The function of this class is to know where each bar in series is laid out. |
|
10 | // TODO: Better name for this? The function of this class is to know where each bar in series is laid out. | |
11 | //class BarGroup : public QGraphicsItemGroup |
|
11 | //class BarGroup : public QGraphicsItemGroup | |
12 |
|
12 | |||
13 | class BarGroup : public ChartItem |
|
13 | class BarGroup : public ChartItem | |
14 | { |
|
14 | { | |
15 | /* // TODO: implement as singleton? |
|
15 | /* // TODO: implement as singleton? | |
16 | private: |
|
16 | private: | |
17 | static BarGroup* mBarGroupInstance; |
|
17 | static BarGroup* mBarGroupInstance; | |
18 |
|
18 | |||
19 | public: |
|
19 | public: | |
20 | static BarGroup* instance() |
|
20 | static BarGroup* instance() | |
21 | { |
|
21 | { | |
22 | if (mBarGroupInstance == NULL) { |
|
22 | if (mBarGroupInstance == NULL) { | |
23 | mBarGroupInstance = new BarGroup(); |
|
23 | mBarGroupInstance = new BarGroup(); | |
24 | } |
|
24 | } | |
25 | return mBarGroupInstance; |
|
25 | return mBarGroupInstance; | |
26 | } |
|
26 | } | |
27 | private: |
|
27 | private: | |
28 | */ |
|
28 | */ | |
29 | public: |
|
29 | public: | |
30 | explicit BarGroup(BarChartSeries& series, QGraphicsItem *parent = 0); |
|
30 | explicit BarGroup(BarChartSeries& series, QGraphicsItem *parent = 0); | |
31 |
|
31 | |||
32 | // From ChartItem |
|
32 | // From ChartItem | |
33 | virtual void setSize(const QSize& size); |
|
33 | virtual void setSize(const QSize& size); | |
34 | virtual void setPlotDomain(const PlotDomain& data); |
|
34 | virtual void setPlotDomain(const PlotDomain& data); | |
35 |
|
35 | |||
36 | // Layout "api" |
|
36 | // Layout "api" | |
37 | void setPos(qreal x, qreal y); |
|
37 | void setPos(qreal x, qreal y); | |
38 | void setBarWidth( int w ); // Default width for each bar |
|
38 | void setBarWidth( int w ); // Default width for each bar | |
39 |
|
39 | |||
40 | // TODO: set color theme instead? or use some external color theme call this |
|
|||
41 | int addColor( QColor color ); |
|
40 | int addColor( QColor color ); | |
|
41 | void resetColors(); | |||
42 |
|
42 | |||
43 | // From QGraphicsItem |
|
43 | // From QGraphicsItem | |
44 | void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); |
|
44 | void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); | |
45 | QRectF boundingRect() const; |
|
45 | QRectF boundingRect() const; | |
46 |
|
46 | |||
47 | private: |
|
47 | private: | |
48 |
|
48 | |||
49 | void dataChanged(); // data of series has changed -> need to recalculate bar sizes |
|
49 | void dataChanged(); // data of series has changed -> need to recalculate bar sizes | |
50 | void layoutChanged(); // layout has changed -> need to recalculate bar sizes |
|
50 | void layoutChanged(); // layout has changed -> need to recalculate bar sizes | |
51 |
|
51 | |||
52 | private: |
|
52 | private: | |
53 |
|
53 | |||
54 | // Data |
|
54 | // Data | |
55 | BarChartSeries& mSeries; |
|
55 | BarChartSeries& mSeries; | |
56 | int mMin; // Min and max values of data. (updated when data is changed, used when drawing) |
|
56 | int mMin; // Min and max values of data. (updated when data is changed, used when drawing) | |
57 | int mMax; |
|
57 | int mMax; | |
58 |
|
58 | |||
59 | int mHeight; // Layout spesific |
|
59 | int mHeight; // Layout spesific | |
60 | int mWidth; |
|
60 | int mWidth; | |
61 | int mBarDefaultWidth; |
|
61 | int mBarDefaultWidth; | |
62 |
|
62 | |||
63 | bool mLayoutSet; // True, if component has been laid out. |
|
63 | bool mLayoutSet; // True, if component has been laid out. | |
64 | bool mLayoutDirty; |
|
64 | bool mLayoutDirty; | |
65 |
|
65 | |||
66 | QList<QColor> mColors; // List of colors for series for now |
|
66 | QList<QColor> mColors; // List of colors for series for now | |
67 | }; |
|
67 | }; | |
68 |
|
68 | |||
69 | QTCOMMERCIALCHART_END_NAMESPACE |
|
69 | QTCOMMERCIALCHART_END_NAMESPACE | |
70 |
|
70 | |||
71 | #endif // QBARCHART_H |
|
71 | #endif // QBARCHART_H |
General Comments 0
You need to be logged in to leave comments.
Login now