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