##// END OF EJS Templates
removed old resize method from barchart
sauimone -
r90:a349c387ac62
parent child
Show More
@@ -1,136 +1,128
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
27 qDebug() << "BarGroup::setSize";
26 qDebug() << "BarGroup::setSize";
28 resize(size.width(),size.height());
27 mWidth = size.width();
28 mHeight = size.height();
29 layoutChanged();
30 mLayoutSet = true;
29 }
31 }
30
32
31 void BarGroup::setPlotDomain(const PlotDomain& data)
33 void BarGroup::setPlotDomain(const PlotDomain& data)
32 {
34 {
33 qDebug() << "BarGroup::setPlotDomain";
35 qDebug() << "BarGroup::setPlotDomain";
34 // TODO:
36 // TODO:
35 }
37 }
36
38
37
38 void BarGroup::resize( int w, int h )
39 {
40 qDebug() << "QBarChart::resize";
41 mWidth = w;
42 mHeight = h;
43 layoutChanged();
44 mLayoutSet = true;
45 }
46
47 void BarGroup::setBarWidth( int w )
39 void BarGroup::setBarWidth( int w )
48 {
40 {
49 mBarDefaultWidth = w;
41 mBarDefaultWidth = w;
50 }
42 }
51
43
52 int BarGroup::addColor( QColor color )
44 int BarGroup::addColor( QColor color )
53 {
45 {
54 int colorIndex = mColors.count();
46 int colorIndex = mColors.count();
55 mColors.append(color);
47 mColors.append(color);
56 return colorIndex;
48 return colorIndex;
57 }
49 }
58
50
59 void BarGroup::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
51 void BarGroup::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
60 {
52 {
61 if (!mLayoutSet) {
53 if (!mLayoutSet) {
62 qDebug() << "QBarChart::paint called without layout set. Aborting.";
54 qDebug() << "QBarChart::paint called without layout set. Aborting.";
63 return;
55 return;
64 }
56 }
65 if (mLayoutDirty) {
57 if (mLayoutDirty) {
66 // Layout or data has changed. Need to redraw.
58 // Layout or data has changed. Need to redraw.
67 foreach(QGraphicsItem* i, childItems()) {
59 foreach(QGraphicsItem* i, childItems()) {
68 i->paint(painter,option,widget);
60 i->paint(painter,option,widget);
69 }
61 }
70 }
62 }
71 }
63 }
72
64
73 QRectF BarGroup::boundingRect() const
65 QRectF BarGroup::boundingRect() const
74 {
66 {
75 return QRectF(0,0,mWidth,mHeight);
67 return QRectF(0,0,mWidth,mHeight);
76 }
68 }
77
69
78
70
79 void BarGroup::dataChanged()
71 void BarGroup::dataChanged()
80 {
72 {
81 qDebug() << "QBarChart::dataChanged mSeries";
73 qDebug() << "QBarChart::dataChanged mSeries";
82
74
83 // Find out maximum and minimum of all series
75 // Find out maximum and minimum of all series
84 mMax = mSeries.max();
76 mMax = mSeries.max();
85 mMin = mSeries.min();
77 mMin = mSeries.min();
86
78
87 // Delete old bars
79 // Delete old bars
88 // Is this correct way to delete childItems?
80 // Is this correct way to delete childItems?
89 foreach (QGraphicsItem* item, childItems()) {
81 foreach (QGraphicsItem* item, childItems()) {
90 delete item;
82 delete item;
91 }
83 }
92
84
93 // Create new graphic items for bars
85 // Create new graphic items for bars
94 int totalItems = mSeries.countTotalItems();
86 int totalItems = mSeries.countTotalItems();
95 for (int i=0; i<totalItems; i++) {
87 for (int i=0; i<totalItems; i++) {
96 Bar *bar = new Bar(this);
88 Bar *bar = new Bar(this);
97 childItems().append(bar);
89 childItems().append(bar);
98 }
90 }
99
91
100 mLayoutDirty = true;
92 mLayoutDirty = true;
101 }
93 }
102
94
103 void BarGroup::layoutChanged()
95 void BarGroup::layoutChanged()
104 {
96 {
105 // Scale bars to new layout
97 // Scale bars to new layout
106 // Layout for bars:
98 // Layout for bars:
107 if (mSeries.countSeries() <= 0) {
99 if (mSeries.countSeries() <= 0) {
108 // Nothing to do.
100 // Nothing to do.
109 return;
101 return;
110 }
102 }
111
103
112 // TODO: better way to auto-layout
104 // TODO: better way to auto-layout
113 int count = mSeries.countItemsInSeries();
105 int count = mSeries.countItemsInSeries();
114 int posStep = (mWidth / (count+1));
106 int posStep = (mWidth / (count+1));
115 int startPos = (mWidth / (count+1)) - mSeries.countSeries() * mBarDefaultWidth /2;
107 int startPos = (mWidth / (count+1)) - mSeries.countSeries() * mBarDefaultWidth /2;
116 qDebug() << "startpos" << startPos;
108 qDebug() << "startpos" << startPos;
117
109
118 // Scaling.
110 // Scaling.
119 int itemIndex(0);
111 int itemIndex(0);
120 for (int series = 0; series < mSeries.countSeries(); series++) {
112 for (int series = 0; series < mSeries.countSeries(); series++) {
121 for (int item=0; item < mSeries.countItemsInSeries(); item++) {
113 for (int item=0; item < mSeries.countItemsInSeries(); item++) {
122 qDebug() << itemIndex;
114 qDebug() << itemIndex;
123 int barHeight = mSeries.valueAt(series, item) * mHeight / mMax;
115 int barHeight = mSeries.valueAt(series, item) * mHeight / mMax;
124 Bar* bar = reinterpret_cast<Bar*> (childItems().at(itemIndex));
116 Bar* bar = reinterpret_cast<Bar*> (childItems().at(itemIndex));
125
117
126 // TODO: width settable per bar?
118 // TODO: width settable per bar?
127 bar->resize(mBarDefaultWidth, barHeight);
119 bar->resize(mBarDefaultWidth, barHeight);
128 bar->setColor(mColors.at(series));
120 bar->setColor(mColors.at(series));
129 bar->setPos(item*posStep+startPos + series * mBarDefaultWidth, mHeight);
121 bar->setPos(item*posStep+startPos + series * mBarDefaultWidth, mHeight);
130 itemIndex++;
122 itemIndex++;
131 }
123 }
132 }
124 }
133 mLayoutDirty = true;
125 mLayoutDirty = true;
134 }
126 }
135
127
136 QTCOMMERCIALCHART_END_NAMESPACE
128 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,72 +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 resize( int w, int h ); // Size for whole series. Single bars are drawn inside this area
38 void setPos(qreal x, qreal y);
37 void setPos(qreal x, qreal y);
39 void setBarWidth( int w ); // Default width for each bar
38 void setBarWidth( int w ); // Default width for each bar
40
39
41 // TODO: set color theme instead? or use some external color theme call this
40 // TODO: set color theme instead? or use some external color theme call this
42 int addColor( QColor color );
41 int addColor( QColor color );
43
42
44 // From QGraphicsItem
43 // From QGraphicsItem
45 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
44 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
46 QRectF boundingRect() const;
45 QRectF boundingRect() const;
47
46
48 private:
47 private:
49
48
50 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
51 void layoutChanged(); // layout has changed -> need to recalculate bar sizes
50 void layoutChanged(); // layout has changed -> need to recalculate bar sizes
52
51
53 private:
52 private:
54
53
55 // Data
54 // Data
56 BarChartSeries& mSeries;
55 BarChartSeries& mSeries;
57 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)
58 int mMax;
57 int mMax;
59
58
60 int mHeight; // Layout spesific
59 int mHeight; // Layout spesific
61 int mWidth;
60 int mWidth;
62 int mBarDefaultWidth;
61 int mBarDefaultWidth;
63
62
64 bool mLayoutSet; // True, if component has been laid out.
63 bool mLayoutSet; // True, if component has been laid out.
65 bool mLayoutDirty;
64 bool mLayoutDirty;
66
65
67 QList<QColor> mColors; // List of colors for series for now
66 QList<QColor> mColors; // List of colors for series for now
68 };
67 };
69
68
70 QTCOMMERCIALCHART_END_NAMESPACE
69 QTCOMMERCIALCHART_END_NAMESPACE
71
70
72 #endif // QBARCHART_H
71 #endif // QBARCHART_H
General Comments 0
You need to be logged in to leave comments. Login now