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