##// END OF EJS Templates
Cleanup; removed left over .h file and squashed warnings
Tero Ahola -
r121:4c25bd6a036a
parent child
Show More
@@ -1,79 +1,80
1 1 #include <QDebug>
2 2 #include "barchartseries.h"
3 3 #include "bargroup.h"
4 4 QTCOMMERCIALCHART_BEGIN_NAMESPACE
5 5
6 6 BarChartSeries::BarChartSeries(QObject *parent)
7 7 : QChartSeries(parent)
8 8 {
9 9 }
10 10
11 11 bool BarChartSeries::setData(QAbstractItemModel* model)
12 12 {
13 13 mModel = model;
14 return true;
14 15 }
15 16
16 17 int BarChartSeries::min()
17 18 {
18 19 Q_ASSERT(mModel->rowCount() > 0);
19 20 Q_ASSERT(mModel->columnCount() > 0);
20 21
21 22 // TODO: make min and max members and update them when data changes.
22 23 // This is slower since they are checked every time, even if data is same since previous call.
23 24 int min = INT_MAX;
24 25
25 26 for (int i=0; i <mModel->rowCount(); i++) {
26 27 for(int j=0; j<mModel->columnCount(); j++) {
27 28 int temp = mModel->data(mModel->index(i,j)).toInt();
28 29 if (temp < min) {
29 30 min = temp;
30 31 }
31 32 }
32 33 }
33 34 return min;
34 35 }
35 36
36 37 int BarChartSeries::max()
37 38 {
38 39 Q_ASSERT(mModel->rowCount() > 0);
39 40 Q_ASSERT(mModel->columnCount() > 0);
40 41
41 42 // TODO: make min and max members and update them when data changes.
42 43 // This is slower since they are checked every time, even if data is same since previous call.
43 44 int max = INT_MIN;
44 45
45 46 for (int i=0; i <mModel->rowCount(); i++) {
46 47 for(int j=0; j<mModel->columnCount(); j++) {
47 48 int temp = mModel->data(mModel->index(i,j)).toInt();
48 49 if (temp > max) {
49 50 max = temp;
50 51 }
51 52 }
52 53 }
53 54 return max;
54 55 }
55 56
56 57
57 58 int BarChartSeries::countRows()
58 59 {
59 60 return mModel->rowCount();
60 61 }
61 62
62 63 int BarChartSeries::countColumns()
63 64 {
64 65 return mModel->columnCount();
65 66 }
66 67
67 68 int BarChartSeries::countTotalItems()
68 69 {
69 70 return mModel->rowCount() * mModel->columnCount();
70 71 }
71 72
72 73 int BarChartSeries::valueAt(int row, int column)
73 74 {
74 75 return mModel->data(mModel->index(row,column)).toInt();
75 76 }
76 77
77 78 #include "moc_barchartseries.cpp"
78 79
79 80 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,157 +1,157
1 1 #include "bargroup.h"
2 2 #include "bar_p.h"
3 3 #include "barlabel_p.h"
4 4 #include <QDebug>
5 5
6 6 QTCOMMERCIALCHART_BEGIN_NAMESPACE
7 7
8 8 BarGroup::BarGroup(BarChartSeries& series, QGraphicsItem *parent) :
9 9 ChartItem(parent)
10 10 ,mSeries(series)
11 ,mBarDefaultWidth(10)
11 12 ,mLayoutSet(false)
12 13 ,mLayoutDirty(true)
13 ,mBarDefaultWidth(10)
14 14 {
15 15 dataChanged();
16 16 }
17 17
18 18 void BarGroup::setSize(const QSizeF& size)
19 19 {
20 20 qDebug() << "BarGroup::setSize";
21 21 mWidth = size.width();
22 22 mHeight = size.height();
23 23 layoutChanged();
24 24 mLayoutSet = true;
25 25 }
26 26
27 27 void BarGroup::setPlotDomain(const PlotDomain& data)
28 28 {
29 29 qDebug() << "BarGroup::setPlotDomain";
30 30 // TODO:
31 31 mPlotDomain = data;
32 32 }
33 33
34 34 void BarGroup::setBarWidth( int w )
35 35 {
36 36 mBarDefaultWidth = w;
37 37 }
38 38
39 39 int BarGroup::addColor( QColor color )
40 40 {
41 41 int colorIndex = mColors.count();
42 42 mColors.append(color);
43 43 return colorIndex;
44 44 }
45 45
46 46 void BarGroup::resetColors()
47 47 {
48 48 mColors.clear();
49 49 }
50 50
51 51 void BarGroup::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
52 52 {
53 53 if (!mLayoutSet) {
54 54 qDebug() << "QBarChart::paint called without layout set. Aborting.";
55 55 return;
56 56 }
57 57 if (mLayoutDirty) {
58 58 // Layout or data has changed. Need to redraw.
59 59 foreach(QGraphicsItem* i, childItems()) {
60 60 i->paint(painter,option,widget);
61 61 mLayoutDirty = false;
62 62 }
63 63 }
64 64 }
65 65
66 66 QRectF BarGroup::boundingRect() const
67 67 {
68 68 return QRectF(0,0,mWidth,mHeight);
69 69 }
70 70
71 71
72 72 void BarGroup::dataChanged()
73 73 {
74 74 qDebug() << "QBarChart::dataChanged mSeries";
75 75
76 76 // Find out maximum and minimum of all series
77 77 mMax = mSeries.max();
78 78 mMin = mSeries.min();
79 79
80 80 // Delete old bars
81 81 // Is this correct way to delete childItems?
82 82 foreach (QGraphicsItem* item, childItems()) {
83 83 delete item;
84 84 }
85 85
86 86 // Create new graphic items for bars
87 87 int totalItems = mSeries.countTotalItems();
88 88 for (int i=0; i<totalItems; i++) {
89 89 Bar *bar = new Bar(this);
90 90 childItems().append(bar);
91 91 }
92 92
93 93 // TODO: labels from series. This creates just some example labels
94 94 int count = mSeries.countColumns();
95 95 for (int i=0; i<count; i++) {
96 96 BarLabel* label = new BarLabel(this);
97 97 QString text("Label " + QString::number(i));
98 98 label->set(text);
99 99 childItems().append(label);
100 100 }
101 101
102 102 // TODO: if (autolayout) { layoutChanged() } or something
103 103 mLayoutDirty = true;
104 104 }
105 105
106 106 void BarGroup::layoutChanged()
107 107 {
108 108 // Scale bars to new layout
109 109 // Layout for bars:
110 110 if (mSeries.countRows() <= 0) {
111 111 // Nothing to do.
112 112 return;
113 113 }
114 114
115 115 // TODO: better way to auto-layout?
116 116 // Use reals for accurancy (we might get some compiler warnings... :)
117 117 int columnCount = mSeries.countColumns();
118 118 int rowCount = mSeries.countRows();
119 119
120 120 qreal tW = mWidth;
121 121 qreal tH = mHeight;
122 122 qreal tM = mMax;
123 123 qreal scale = (tH/tM);
124 124
125 125 qreal tC = columnCount+1;
126 126 qreal xStepPerSeries = (tW/tC);
127 127
128 128 // Scaling.
129 129 int itemIndex(0);
130 130 int labelIndex = mSeries.countColumns() * mSeries.countRows();
131 131
132 132 for (int column=0; column < columnCount; column++) {
133 133 qreal xPos = xStepPerSeries * column + ((tW + mBarDefaultWidth*rowCount)/(columnCount*2));
134 134 qreal yPos = mHeight;
135 135 for (int row = 0; row < rowCount; row++) {
136 136 qreal barHeight = mSeries.valueAt(row, column) * scale;
137 137 Bar* bar = reinterpret_cast<Bar*> (childItems().at(itemIndex));
138 138
139 139 // TODO: width settable per bar?
140 140 bar->resize(mBarDefaultWidth, barHeight);
141 141 bar->setColor(mColors.at(row));
142 142 bar->setPos(xPos, yPos-barHeight); // item*posStep+startPos + series * mBarDefaultWidth, mHeight);
143 143 itemIndex++;
144 144 xPos += mBarDefaultWidth;
145 145 }
146 146
147 147 // TODO: Layout for labels, remove magic number
148 148 xPos = xStepPerSeries * column + ((tW + mBarDefaultWidth*rowCount)/(columnCount*2));
149 149 BarLabel* label = reinterpret_cast<BarLabel*> (childItems().at(labelIndex));
150 150 label->setPos(xPos, mHeight + 20);
151 151 labelIndex++;
152 152 }
153 153
154 154 mLayoutDirty = true;
155 155 }
156 156
157 157 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,110 +1,111
1 1 #include "percentbarchartseries.h"
2 2
3 3 #include <limits.h>
4 4 #include <QDebug>
5 5 #include "percentbarchartseries.h"
6 6
7 7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 8
9 9 PercentBarChartSeries::PercentBarChartSeries(QObject *parent) :
10 10 QChartSeries(parent)
11 11 {
12 12 }
13 13
14 14 bool PercentBarChartSeries::setData(QAbstractItemModel* model)
15 15 {
16 16 mModel = model;
17 return true;
17 18 }
18 19
19 20 int PercentBarChartSeries::min()
20 21 {
21 22 Q_ASSERT(mModel->rowCount() > 0);
22 23 Q_ASSERT(mModel->columnCount() > 0);
23 24
24 25 // TODO: make min and max members and update them when data changes.
25 26 // This is slower since they are checked every time, even if data is same since previous call.
26 27 int min = INT_MAX;
27 28
28 29 for (int i=0; i <mModel->rowCount(); i++) {
29 30 for(int j=0; j<mModel->columnCount(); j++) {
30 31 int temp = mModel->data(mModel->index(i,j)).toInt();
31 32 if (temp < min) {
32 33 min = temp;
33 34 }
34 35 }
35 36 }
36 37 return min;
37 38 }
38 39
39 40 int PercentBarChartSeries::max()
40 41 {
41 42 Q_ASSERT(mModel->rowCount() > 0);
42 43 Q_ASSERT(mModel->columnCount() > 0);
43 44
44 45 // TODO: make min and max members and update them when data changes.
45 46 // This is slower since they are checked every time, even if data is same since previous call.
46 47 int max = INT_MIN;
47 48
48 49 for (int i=0; i <mModel->rowCount(); i++) {
49 50 for(int j=0; j<mModel->columnCount(); j++) {
50 51 int temp = mModel->data(mModel->index(i,j)).toInt();
51 52 if (temp > max) {
52 53 max = temp;
53 54 }
54 55 }
55 56 }
56 57 return max;
57 58 }
58 59
59 60 int PercentBarChartSeries::maxColumnSum()
60 61 {
61 62 Q_ASSERT(mModel->rowCount() > 0);
62 63 Q_ASSERT(mModel->columnCount() > 0);
63 64
64 65 int max = INT_MIN;
65 66
66 67 for (int col=0; col <mModel->columnCount(); col++) {
67 68 int sum = columnSum(col);
68 69 if (sum > max) {
69 70 max = sum;
70 71 }
71 72 }
72 73 return max;
73 74 }
74 75
75 76 int PercentBarChartSeries::countRows()
76 77 {
77 78 return mModel->rowCount();
78 79 }
79 80
80 81 int PercentBarChartSeries::countColumns()
81 82 {
82 83 return mModel->columnCount();
83 84 }
84 85
85 86 int PercentBarChartSeries::countTotalItems()
86 87 {
87 88 return mModel->rowCount() * mModel->columnCount();
88 89 }
89 90
90 91 int PercentBarChartSeries::valueAt(int row, int column)
91 92 {
92 93 QModelIndex index = mModel->index(row,column);
93 94 return mModel->data(index).toInt();
94 95 }
95 96
96 97 int PercentBarChartSeries::columnSum(int column)
97 98 {
98 99 int sum(0);
99 100 int count = mModel->rowCount();
100 101
101 102 for (int row = 0; row < count; row++) {
102 103 sum += mModel->data(mModel->index(row,column)).toInt();
103 104 }
104 105 return sum;
105 106 }
106 107
107 108 #include "moc_percentbarchartseries.cpp"
108 109
109 110 QTCOMMERCIALCHART_END_NAMESPACE
110 111
@@ -1,152 +1,152
1 1 #include "percentbargroup.h"
2 2 #include "bar_p.h"
3 3 #include "barlabel_p.h"
4 4 #include <QDebug>
5 5
6 6 QTCOMMERCIALCHART_BEGIN_NAMESPACE
7 7
8 8 PercentBarGroup::PercentBarGroup(PercentBarChartSeries& series, QGraphicsItem *parent) :
9 ChartItem(parent)
10 ,mSeries(series)
11 ,mLayoutSet(false)
12 ,mLayoutDirty(true)
13 ,mBarDefaultWidth(20) // TODO: remove hard coding, when we have layout code ready
9 ChartItem(parent)
10 ,mSeries(series)
11 ,mBarDefaultWidth(20) // TODO: remove hard coding, when we have layout code ready
12 ,mLayoutSet(false)
13 ,mLayoutDirty(true)
14 14 {
15 15 dataChanged();
16 16 }
17 17
18 18
19 19 void PercentBarGroup::setSize(const QSizeF& size)
20 20 {
21 21 // qDebug() << "PercentBarGroup::setSize";
22 22 mWidth = size.width();
23 23 mHeight = size.height();
24 24 layoutChanged();
25 25 mLayoutSet = true;
26 26 }
27 27
28 28 void PercentBarGroup::setPlotDomain(const PlotDomain& data)
29 29 {
30 30 qDebug() << "PercentBarGroup::setPlotDomain";
31 31 // TODO:
32 32 }
33 33
34 34 void PercentBarGroup::setBarWidth( int w )
35 35 {
36 36 mBarDefaultWidth = w;
37 37 }
38 38
39 39 int PercentBarGroup::addColor( QColor color )
40 40 {
41 41 int colorIndex = mColors.count();
42 42 mColors.append(color);
43 43 return colorIndex;
44 44 }
45 45
46 46 void PercentBarGroup::resetColors()
47 47 {
48 48 mColors.clear();
49 49 }
50 50
51 51 void PercentBarGroup::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
52 52 {
53 53 if (!mLayoutSet) {
54 54 qDebug() << "QBarChart::paint called without layout set. Aborting.";
55 55 return;
56 56 }
57 57 if (mLayoutDirty) {
58 58 // Layout or data has changed. Need to redraw.
59 59 foreach(QGraphicsItem* i, childItems()) {
60 60 i->paint(painter,option,widget);
61 61 }
62 62 mLayoutDirty = false;
63 63 }
64 64 }
65 65
66 66 QRectF PercentBarGroup::boundingRect() const
67 67 {
68 68 return QRectF(0,0,mWidth,mHeight);
69 69 }
70 70
71 71
72 72 void PercentBarGroup::dataChanged()
73 73 {
74 74 qDebug() << "QBarChart::dataChanged mSeries";
75 75
76 76 // Find out maximum and minimum of all series
77 77 mMax = mSeries.max();
78 78 mMin = mSeries.min();
79 79
80 80 // Delete old bars
81 81 // Is this correct way to delete childItems?
82 82 foreach (QGraphicsItem* item, childItems()) {
83 83 delete item;
84 84 }
85 85
86 86 // Create new graphic items for bars
87 87 int totalItems = mSeries.countTotalItems();
88 88 for (int i=0; i<totalItems; i++) {
89 89 Bar *bar = new Bar(this);
90 90 childItems().append(bar);
91 91 }
92 92
93 93 // TODO: labels from series. This creates just some example labels
94 94 int count = mSeries.countColumns();
95 95 for (int i=0; i<count; i++) {
96 96 BarLabel* label = new BarLabel(this);
97 97 QString text("Label " + QString::number(i));
98 98 label->set(text);
99 99 childItems().append(label);
100 100 }
101 101
102 102 // TODO: if (autolayout) { layoutChanged() } or something
103 103 mLayoutDirty = true;
104 104 }
105 105
106 106 void PercentBarGroup::layoutChanged()
107 107 {
108 108 // Scale bars to new layout
109 109 // Layout for bars:
110 110 if (mSeries.countRows() <= 0) {
111 111 // Nothing to do.
112 112 return;
113 113 }
114 114
115 115 // TODO: better way to auto-layout
116 116 // Use reals for accurancy (we might get some compiler warnings... :)
117 117 int count = mSeries.countColumns();
118 118 int itemIndex(0);
119 119 qreal tW = mWidth;
120 120 qreal tC = count+1;
121 121 qreal xStep = (tW/tC);
122 122 // qreal xPos = ((tW/tC) + mBarDefaultWidth / 2);
123 123 qreal xPos = ((tW/tC) - mBarDefaultWidth / 2);
124 124 int labelIndex = mSeries.countColumns() * mSeries.countRows();
125 125
126 126 for (int column = 0; column < mSeries.countColumns(); column++) {
127 127 qreal colSum = mSeries.columnSum(column);
128 128 qreal h = mHeight;
129 129 qreal scale = (h / colSum);
130 130 qreal yPos = h;
131 131 for (int row=0; row < mSeries.countRows(); row++) {
132 132 qreal barHeight = mSeries.valueAt(row, column) * scale;
133 133 Bar* bar = reinterpret_cast<Bar*> (childItems().at(itemIndex));
134 134
135 135 // TODO: width settable per bar?
136 136 bar->resize(mBarDefaultWidth, barHeight);
137 137 bar->setColor(mColors.at(row));
138 138 bar->setPos(xPos, yPos-barHeight);
139 139 itemIndex++;
140 140 yPos -= barHeight;
141 141 }
142 142
143 143 // TODO: Layout for labels, remove magic number
144 144 BarLabel* label = reinterpret_cast<BarLabel*> (childItems().at(labelIndex));
145 145 label->setPos(xPos, mHeight + 20);
146 146 labelIndex++;
147 147 xPos += xStep;
148 148 }
149 149 mLayoutDirty = true;
150 150 }
151 151
152 152 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,108 +1,109
1 1 #include <limits.h>
2 2 #include <QDebug>
3 3 #include "stackedbarchartseries.h"
4 4
5 5 QTCOMMERCIALCHART_BEGIN_NAMESPACE
6 6
7 7 StackedBarChartSeries::StackedBarChartSeries(QObject *parent) :
8 8 QChartSeries(parent)
9 9 {
10 10 }
11 11
12 12 bool StackedBarChartSeries::setData(QAbstractItemModel* model)
13 13 {
14 14 mModel = model;
15 return true;
15 16 }
16 17
17 18 int StackedBarChartSeries::min()
18 19 {
19 20 Q_ASSERT(mModel->rowCount() > 0);
20 21 Q_ASSERT(mModel->columnCount() > 0);
21 22
22 23 // TODO: make min and max members and update them when data changes.
23 24 // This is slower since they are checked every time, even if data is same since previous call.
24 25 int min = INT_MAX;
25 26
26 27 for (int i=0; i <mModel->rowCount(); i++) {
27 28 for(int j=0; j<mModel->columnCount(); j++) {
28 29 int temp = mModel->data(mModel->index(i,j)).toInt();
29 30 if (temp < min) {
30 31 min = temp;
31 32 }
32 33 }
33 34 }
34 35 return min;
35 36 }
36 37
37 38 int StackedBarChartSeries::max()
38 39 {
39 40 Q_ASSERT(mModel->rowCount() > 0);
40 41 Q_ASSERT(mModel->columnCount() > 0);
41 42
42 43 // TODO: make min and max members and update them when data changes.
43 44 // This is slower since they are checked every time, even if data is same since previous call.
44 45 int max = INT_MIN;
45 46
46 47 for (int i=0; i <mModel->rowCount(); i++) {
47 48 for(int j=0; j<mModel->columnCount(); j++) {
48 49 int temp = mModel->data(mModel->index(i,j)).toInt();
49 50 if (temp > max) {
50 51 max = temp;
51 52 }
52 53 }
53 54 }
54 55 return max;
55 56 }
56 57
57 58 int StackedBarChartSeries::maxColumnSum()
58 59 {
59 60 Q_ASSERT(mModel->rowCount() > 0);
60 61 Q_ASSERT(mModel->columnCount() > 0);
61 62
62 63 int max = INT_MIN;
63 64
64 65 for (int col=0; col <mModel->columnCount(); col++) {
65 66 int sum = columnSum(col);
66 67 if (sum > max) {
67 68 max = sum;
68 69 }
69 70 }
70 71 return max;
71 72 }
72 73
73 74 int StackedBarChartSeries::countRows()
74 75 {
75 76 return mModel->rowCount();
76 77 }
77 78
78 79 int StackedBarChartSeries::countColumns()
79 80 {
80 81 return mModel->columnCount();
81 82 }
82 83
83 84 int StackedBarChartSeries::countTotalItems()
84 85 {
85 86 return mModel->rowCount() * mModel->columnCount();
86 87 }
87 88
88 89 int StackedBarChartSeries::valueAt(int row, int column)
89 90 {
90 91 QModelIndex index = mModel->index(row,column);
91 92 return mModel->data(index).toInt();
92 93 }
93 94
94 95 int StackedBarChartSeries::columnSum(int column)
95 96 {
96 97 int sum(0);
97 98 int count = mModel->rowCount();
98 99
99 100 for (int row = 0; row < count; row++) {
100 101 sum += mModel->data(mModel->index(row,column)).toInt();
101 102 }
102 103 return sum;
103 104 }
104 105
105 106 #include "moc_stackedbarchartseries.cpp"
106 107
107 108 QTCOMMERCIALCHART_END_NAMESPACE
108 109
@@ -1,192 +1,192
1 1 #include "stackedbargroup.h"
2 2 #include "bar_p.h"
3 3 #include "barlabel_p.h"
4 4 #include <QDebug>
5 5 #include <QPainter>
6 6
7 7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 8
9 9 StackedBarGroup::StackedBarGroup(StackedBarChartSeries& series, QGraphicsItem *parent) :
10 ChartItem(parent)
11 ,mSeries(series)
12 ,mLayoutSet(false)
13 ,mLayoutDirty(true)
14 ,mBarDefaultWidth(20) // TODO: remove hard coding, when we have layout code ready
15 ,mTheme(0)
16 ,mSeparatorsVisible(true)
10 ChartItem(parent)
11 ,mSeries(series)
12 ,mBarDefaultWidth(20) // TODO: remove hard coding, when we have layout code ready
13 ,mLayoutSet(false)
14 ,mLayoutDirty(true)
15 ,mTheme(0)
16 ,mSeparatorsVisible(true)
17 17 {
18 18 dataChanged();
19 19 }
20 20
21 21 void StackedBarGroup::setSeparatorsVisible(bool visible)
22 22 {
23 23 mSeparatorsVisible = visible;
24 24 }
25 25
26 26 void StackedBarGroup::setSize(const QSizeF& size)
27 27 {
28 28 mWidth = size.width();
29 29 mHeight = size.height();
30 30 layoutChanged();
31 31 mLayoutSet = true;
32 32 }
33 33
34 34 void StackedBarGroup::setPlotDomain(const PlotDomain& data)
35 35 {
36 36 qDebug() << "StackedBarGroup::setPlotDomain";
37 37 // TODO:
38 38 }
39 39
40 40 void StackedBarGroup::themeChanged(ChartTheme *theme)
41 41 {
42 42 mTheme = theme;
43 43 }
44 44
45 45 void StackedBarGroup::setBarWidth( int w )
46 46 {
47 47 mBarDefaultWidth = w;
48 48 }
49 49
50 50 int StackedBarGroup::addColor( QColor color )
51 51 {
52 52 int colorIndex = mColors.count();
53 53 mColors.append(color);
54 54 return colorIndex;
55 55 }
56 56
57 57 void StackedBarGroup::resetColors()
58 58 {
59 59 mColors.clear();
60 60 }
61 61
62 62 void StackedBarGroup::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
63 63 {
64 64 if (!mLayoutSet) {
65 65 qDebug() << "QBarChart::paint called without layout set. Aborting.";
66 66 return;
67 67 }
68 68 if (mLayoutDirty) {
69 69 // Layout or data has changed. Need to redraw.
70 70 foreach(QGraphicsItem* i, childItems()) {
71 71 i->paint(painter,option,widget);
72 72 }
73 73 if (mSeparatorsVisible) {
74 74 //TODO: own class for separators (graphicsitem), because they may have style etc later.
75 75 // this is just to see that the positions are calculated correctly.
76 76 QPen pen(QColor(0,0,255,255));
77 77 painter->setPen(pen);
78 78 for (int i=0; i<mSeparatorPositions.count(); i++ ) {
79 79 qreal xp = mSeparatorPositions.at(i);
80 80 painter->drawLine(xp,0,xp,mHeight);
81 81 }
82 82 }
83 83 // mLayoutDirty = false;
84 84 }
85 85
86 86 }
87 87
88 88 QRectF StackedBarGroup::boundingRect() const
89 89 {
90 90 return QRectF(0,0,mWidth,mHeight);
91 91 }
92 92
93 93
94 94 void StackedBarGroup::dataChanged()
95 95 {
96 96 qDebug() << "QBarChart::dataChanged";
97 97
98 98 // Find out maximum and minimum of all series
99 99 mMax = mSeries.max();
100 100 mMin = mSeries.min();
101 101
102 102 // Delete old bars
103 103 // Is this correct way to delete childItems?
104 104 foreach (QGraphicsItem* item, childItems()) {
105 105 delete item;
106 106 }
107 107
108 108 // Create new graphic items for bars
109 109 int totalItems = mSeries.countTotalItems();
110 110 for (int i=0; i<totalItems; i++) {
111 111 Bar *bar = new Bar(this);
112 112 childItems().append(bar);
113 113 }
114 114
115 115 // TODO: labels from series. This creates just some example labels
116 116 int count = mSeries.countColumns();
117 117 for (int i=0; i<count; i++) {
118 118 BarLabel* label = new BarLabel(this);
119 119 QString text("Label " + QString::number(i));
120 120 label->set(text);
121 121 childItems().append(label);
122 122 }
123 123
124 124 mSeparatorPositions.clear();
125 125
126 126 // TODO: if (autolayout) { layoutChanged() } or something
127 127 mLayoutDirty = true;
128 128 }
129 129
130 130 void StackedBarGroup::layoutChanged()
131 131 {
132 132 // Scale bars to new layout
133 133 // Layout for bars:
134 134 if (mSeries.countRows() <= 0) {
135 135 // Nothing to do.
136 136 return;
137 137 }
138 138
139 139 if (mSeries.countColumns() == 0) {
140 140 // Nothing to do
141 141 return;
142 142 }
143 143
144 144 // TODO: better way to auto-layout
145 145 // Use reals for accurancy (we might get some compiler warnings... :)
146 146 // TODO: use temp variable for column count...
147 147 qreal maxSum = mSeries.maxColumnSum();
148 148 qreal h = mHeight;
149 149 qreal scale = (h / maxSum);
150 150
151 151 int itemIndex(0);
152 152 qreal tW = mWidth;
153 153 qreal tC = mSeries.countColumns() + 1;
154 154 qreal xStep = (tW/tC);
155 155 qreal xPos = ((tW/tC) - mBarDefaultWidth / 2);
156 156 int labelIndex = mSeries.countColumns() * mSeries.countRows();
157 157
158 158 for (int column = 0; column < mSeries.countColumns(); column++) {
159 159 qreal yPos = h;
160 160 for (int row=0; row < mSeries.countRows(); row++) {
161 161 qreal barHeight = mSeries.valueAt(row, column) * scale;
162 162 Bar* bar = reinterpret_cast<Bar*> (childItems().at(itemIndex));
163 163
164 164 // TODO: width settable per bar?
165 165 // TODO: how to get color for series(x) from theme?
166 166 // mTheme->themeForSeries();
167 167 bar->resize(mBarDefaultWidth, barHeight);
168 168 bar->setColor(mColors.at(row));
169 169 bar->setPos(xPos, yPos-barHeight);
170 170 itemIndex++;
171 171 yPos -= barHeight;
172 172 }
173 173
174 174 // TODO: Layout for labels, remove magic number
175 175 BarLabel* label = reinterpret_cast<BarLabel*> (childItems().at(labelIndex));
176 176 label->setPos(xPos, mHeight + 20);
177 177 labelIndex++;
178 178 xPos += xStep;
179 179 }
180 180
181 181 // Position separators
182 182 mSeparatorPositions.clear();
183 183 xPos = xStep + xStep/2; // Initial position is between first and second group. ie one and half steps from left.
184 184 for (int s=0; s < mSeries.countColumns() - 1; s++) {
185 185 mSeparatorPositions.append(xPos);
186 186 xPos += xStep;
187 187 }
188 188
189 189 mLayoutDirty = true;
190 190 }
191 191
192 192 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,140 +1,140
1 1 #include "qscatterseries.h"
2 2 #include "qscatterseries_p.h"
3 3 #include "qchart.h"
4 4 #include <QPainter>
5 5 #include <QGraphicsScene>
6 6 #include <QDebug>
7 7
8 8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 9
10 10 //#define QSeriesData QList<qreal>
11 11
12 12 QScatterSeriesPrivate::QScatterSeriesPrivate(QGraphicsItem *parent) :
13 13 ChartItem(parent),
14 m_boundingRect(),
14 15 m_markerColor(QColor()),
15 m_visibleChartArea(),
16 m_boundingRect()
16 m_visibleChartArea()
17 17 {
18 18 if (parent)
19 19 m_boundingRect = parent->boundingRect();
20 20 }
21 21
22 22 void QScatterSeriesPrivate::changeGeometry()
23 23 {
24 24 if (m_boundingRect.isValid()) {
25 25 prepareGeometryChange();
26 26 qreal scalex = m_boundingRect.width() / m_visibleChartArea.spanX();
27 27 qreal scaley = m_boundingRect.height() / m_visibleChartArea.spanY();
28 28 m_scenex.clear();
29 29 m_sceney.clear();
30 30
31 31 // Convert relative coordinates to absolute pixel coordinates that can be used for drawing
32 32 foreach(qreal x, m_x)
33 33 m_scenex.append(m_boundingRect.left() + x * scalex - m_visibleChartArea.m_minX * scalex);
34 34
35 35 foreach(qreal y, m_y)
36 36 m_sceney.append(m_boundingRect.bottom() - y * scaley + m_visibleChartArea.m_minY * scaley);
37 37 }
38 38 }
39 39
40 40 void QScatterSeriesPrivate::setSize(const QSizeF &size)
41 41 {
42 42 // m_boundingRect = QRectF(pos().x(), pos().y(), size.width(), size.height());
43 43 m_boundingRect = QRectF(0, 0, size.width(), size.height());
44 44 changeGeometry();
45 45 }
46 46
47 47 void QScatterSeriesPrivate::themeChanged(ChartTheme *theme)
48 48 {
49 49 m_theme = theme->themeForSeries();
50 50 }
51 51
52 52 void QScatterSeriesPrivate::setPlotDomain(const PlotDomain& plotDomain)
53 53 {
54 54 m_visibleChartArea = plotDomain;
55 55 changeGeometry();
56 56 }
57 57
58 58 QRectF QScatterSeriesPrivate::boundingRect() const
59 59 {
60 60 return m_boundingRect;
61 61 }
62 62
63 63 void QScatterSeriesPrivate::paint(QPainter *painter, const QStyleOptionGraphicsItem */*option*/, QWidget */*widget*/)
64 64 {
65 65 // TODO: The opacity should be user definable?
66 66 //brush.setColor(QColor(255, 82, 0, 100));
67 67 if (m_markerColor.isValid()) {
68 68 QPen pen = painter->pen();
69 69 QBrush brush = pen.brush();
70 70 brush.setColor(m_markerColor);
71 71 pen.setBrush(brush);
72 72 pen.setWidth(4);
73 73 painter->setPen(pen);
74 74 }
75 75 else
76 76 painter->setPen(m_theme.markerPen);
77 77 // brush.setColor(m_theme..lineColor);
78 78
79 79 // TODO: m_scenex and m_sceny are left empty during construction -> we would need a resize
80 80 // event right after construction or maybe given a size during initialization
81 81 for (int i(0); i < m_scenex.count() && i < m_sceney.count(); i++) {
82 82 if (scene()->width() > m_scenex.at(i) && scene()->height() > m_sceney.at(i))
83 83 //painter->drawArc(m_scenex.at(i), m_sceney.at(i), 2, 2, 0, 5760);
84 84 painter->drawPoint(m_scenex.at(i), m_sceney.at(i));
85 85 }
86 86 }
87 87
88 88 QScatterSeries::QScatterSeries(QObject *parent) :
89 89 QChartSeries(parent),
90 90 d(new QScatterSeriesPrivate(qobject_cast<QGraphicsItem *> (parent)))
91 91 {
92 92 }
93 93
94 94 bool QScatterSeries::setData(QList<qreal> xlist, QList<qreal> ylist)
95 95 {
96 96 // TODO: validate data
97 97 d->m_x = xlist;
98 98 d->m_y = ylist;
99 99
100 100 // TODO: the following updates the visible chart area setting of the series, we would instead
101 101 // need to update the _chart's_ visible area... this would require a callback or
102 102 // similar to the parenting QChart object...
103 103 foreach (qreal x, d->m_x) {
104 104 d->m_visibleChartArea.m_minX = qMin(d->m_visibleChartArea.m_minX, x);
105 105 d->m_visibleChartArea.m_maxX = qMax(d->m_visibleChartArea.m_maxX, x);
106 106 }
107 107 foreach (qreal y, d->m_y) {
108 108 d->m_visibleChartArea.m_minY = qMin(d->m_visibleChartArea.m_minY, y);
109 109 d->m_visibleChartArea.m_maxY = qMax(d->m_visibleChartArea.m_maxY, y);
110 110 }
111 111
112 112 d->changeGeometry();
113 113
114 114 return true;
115 115 }
116 116
117 117 void QScatterSeries::setMarkerColor(QColor color)
118 118 {
119 119 d->m_markerColor = color;
120 120 }
121 121
122 122 QColor QScatterSeries::markerColor()
123 123 {
124 124 return d->m_markerColor;
125 125 }
126 126
127 127 // TODO:
128 128 //void QScatterSeries::chartScaleChanged(qreal xscale, qreal yscale)
129 129 //{
130 130 // d->rescale(xscale, yscale);
131 131 //}
132 132
133 133 QScatterSeries::~QScatterSeries()
134 134 {
135 135 delete d;
136 136 }
137 137
138 138 #include "moc_qscatterseries.cpp"
139 139
140 140 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,16 +1,15
1 1 #include <QtGui/QApplication>
2 2 #include <QDeclarativeEngine>
3 3 #include "qmlapplicationviewer.h"
4 #include "chart.h"
5 4
6 5 Q_DECL_EXPORT int main(int argc, char *argv[])
7 6 {
8 7 QScopedPointer<QApplication> app(createApplication(argc, argv));
9 8 QScopedPointer<QmlApplicationViewer> viewer(QmlApplicationViewer::create());
10 9
11 10 viewer->setOrientation(QmlApplicationViewer::ScreenOrientationAuto);
12 11 viewer->setMainQmlFile(QLatin1String("qml/qmlchart/main.qml"));
13 12 viewer->showExpanded();
14 13
15 14 return app->exec();
16 15 }
1 NO CONTENT: file was removed
General Comments 0
You need to be logged in to leave comments. Login now