##// END OF EJS Templates
Fixed bug in bargroup. Still some incorrect positions when drawing.
sauimone -
r79:9c922b32f63a
parent child
Show More
@@ -1,138 +1,138
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 // TODO: refactor this
26 // TODO: refactor this
27 qDebug() << "BarGroup::setSize";
27 qDebug() << "BarGroup::setSize";
28 resize(size.width(),size.height());
28 resize(size.width(),size.height());
29 }
29 }
30
30
31 void BarGroup::setPlotDomain(const PlotDomain& data)
31 void BarGroup::setPlotDomain(const PlotDomain& data)
32 {
32 {
33 qDebug() << "BarGroup::setPlotDomain";
33 qDebug() << "BarGroup::setPlotDomain";
34 }
34 }
35
35
36
36
37 void BarGroup::resize( int w, int h )
37 void BarGroup::resize( int w, int h )
38 {
38 {
39 qDebug() << "QBarChart::resize";
39 qDebug() << "QBarChart::resize";
40 mWidth = w;
40 mWidth = w;
41 mHeight = h;
41 mHeight = h;
42 layoutChanged();
42 layoutChanged();
43 mLayoutSet = true;
43 mLayoutSet = true;
44 }
44 }
45
45
46 void BarGroup::setBarWidth( int w )
46 void BarGroup::setBarWidth( int w )
47 {
47 {
48 mBarDefaultWidth = w;
48 mBarDefaultWidth = w;
49 }
49 }
50
50
51 void BarGroup::setColor( QColor color )
51 void BarGroup::setColor( QColor color )
52 {
52 {
53 mColor = color;
53 mColor = color;
54 }
54 }
55
55
56 void BarGroup::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
56 void BarGroup::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
57 {
57 {
58 if (!mLayoutSet) {
58 if (!mLayoutSet) {
59 qDebug() << "QBarChart::paint called without layout set. Aborting.";
59 qDebug() << "QBarChart::paint called without layout set. Aborting.";
60 return;
60 return;
61 }
61 }
62 if (mLayoutDirty) {
62 if (mLayoutDirty) {
63 // Layout or data has changed. Need to redraw.
63 // Layout or data has changed. Need to redraw.
64 foreach(QGraphicsItem* i, childItems()) {
64 foreach(QGraphicsItem* i, childItems()) {
65 i->paint(painter,option,widget);
65 i->paint(painter,option,widget);
66 }
66 }
67 }
67 }
68 }
68 }
69
69
70 QRectF BarGroup::boundingRect() const
70 QRectF BarGroup::boundingRect() const
71 {
71 {
72 // TODO: correct this (currently ignores position)
72 // TODO: correct this (currently ignores position)
73 return QRectF(0,0,mWidth,mHeight);
73 return QRectF(0,0,mWidth,mHeight);
74 }
74 }
75
75
76
76
77 void BarGroup::dataChanged()
77 void BarGroup::dataChanged()
78 {
78 {
79 qDebug() << "QBarChart::dataChanged mSeries";
79 qDebug() << "QBarChart::dataChanged mSeries";
80
80
81 // Find out maximum and minimum of all series
81 // Find out maximum and minimum of all series
82 mMax = mSeries.max();
82 mMax = mSeries.max();
83 mMin = mSeries.min();
83 mMin = mSeries.min();
84
84
85 // Delete old bars
85 // Delete old bars
86 // Is this correct way to delete childItems?
86 // Is this correct way to delete childItems?
87 foreach (QGraphicsItem* item, childItems()) {
87 foreach (QGraphicsItem* item, childItems()) {
88 delete item;
88 delete item;
89 }
89 }
90
90
91 // Create new graphic items for bars
91 // Create new graphic items for bars
92 int totalItems = mSeries.countTotalItems();
92 int totalItems = mSeries.countTotalItems();
93 for (int i=0; i<totalItems; i++) {
93 for (int i=0; i<totalItems; i++) {
94 Bar *bar = new Bar(this);
94 Bar *bar = new Bar(this);
95 childItems().append(bar);
95 childItems().append(bar);
96 }
96 }
97
97
98 mLayoutDirty = true;
98 mLayoutDirty = true;
99 }
99 }
100
100
101 void BarGroup::layoutChanged()
101 void BarGroup::layoutChanged()
102 {
102 {
103 // Scale bars to new layout
103 // Scale bars to new layout
104 // Layout for bars:
104 // Layout for bars:
105 if (mSeries.countSeries() <= 0) {
105 if (mSeries.countSeries() <= 0) {
106 // Nothing to do.
106 // Nothing to do.
107 return;
107 return;
108 }
108 }
109
109
110 // Align center
110 // Align center
111 int count = mSeries.countItemsInSeries();
111 int count = mSeries.countItemsInSeries();
112 int posStep = (mWidth / (count));
112 int posStep = (mWidth / (count));
113 int startPos = (mWidth / count+1);
113 int startPos = (mWidth / count+1);
114 qDebug() << "startpos" << startPos;
114 qDebug() << "startpos" << startPos;
115
115
116 // Scaling. TODO: better one.
116 // Scaling. TODO: better one.
117 int itemIndex(0);
117 int itemIndex(0);
118 for (int series = 0; series < mSeries.countSeries(); series++) {
118 for (int series = 0; series < mSeries.countSeries(); series++) {
119 for (int item=0; item < mSeries.countItemsInSeries(); item++) {
119 for (int item=0; item < mSeries.countItemsInSeries(); item++) {
120 qDebug() << itemIndex;
120 qDebug() << itemIndex;
121 int barHeight = mSeries.valueAt(series, item) * mHeight / mMax;
121 int barHeight = mSeries.valueAt(series, item) * mHeight / mMax;
122 Bar* bar = reinterpret_cast<Bar*> (childItems().at(itemIndex));
122 Bar* bar = reinterpret_cast<Bar*> (childItems().at(itemIndex));
123
123
124 bar->resize(mBarDefaultWidth, barHeight); // TODO: width settable per bar
124 bar->resize(mBarDefaultWidth, barHeight); // TODO: width settable per bar
125 //TODO: test hack
125 //TODO: test hack
126 if (0 == series) {
126 if (0 == series) {
127 bar->setColor(QColor(255,0,0,128));
127 bar->setColor(QColor(255,0,0,128));
128 } else {
128 } else {
129 bar->setColor(QColor(255,255,0,128));
129 bar->setColor(QColor(255,255,0,128));
130 }
130 }
131 bar->setPos(itemIndex*posStep+startPos + series * mBarDefaultWidth, 0);
131 bar->setPos(item*posStep+startPos + series * mBarDefaultWidth, 0);
132 itemIndex++;
132 itemIndex++;
133 }
133 }
134 }
134 }
135 mLayoutDirty = true;
135 mLayoutDirty = true;
136 }
136 }
137
137
138 QTCOMMERCIALCHART_END_NAMESPACE
138 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now