##// END OF EJS Templates
commented out separatos. feature, we dont need?
sauimone -
r592:56a808b04750
parent child
Show More
@@ -1,96 +1,96
1 #include "barpresenter_p.h"
1 #include "barpresenter_p.h"
2 #include "bar_p.h"
2 #include "bar_p.h"
3 #include "barvalue_p.h"
3 #include "barvalue_p.h"
4 #include "separator_p.h"
4 #include "separator_p.h"
5 #include "qbarset.h"
5 #include "qbarset.h"
6 #include <QDebug>
6 #include <QDebug>
7
7
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9
9
10 BarPresenter::BarPresenter(QBarSeries *series, QChart *parent) :
10 BarPresenter::BarPresenter(QBarSeries *series, QChart *parent) :
11 BarPresenterBase(series, parent)
11 BarPresenterBase(series, parent)
12 {
12 {
13 }
13 }
14
14
15 void BarPresenter::layoutChanged()
15 void BarPresenter::layoutChanged()
16 {
16 {
17 // Scale bars to new layout
17 // Scale bars to new layout
18 // Layout for bars:
18 // Layout for bars:
19 if (mSeries->barsetCount() <= 0) {
19 if (mSeries->barsetCount() <= 0) {
20 qDebug() << "No sets in model!";
20 qDebug() << "No sets in model!";
21 return;
21 return;
22 }
22 }
23
23
24 if (childItems().count() == 0) {
24 if (childItems().count() == 0) {
25 qDebug() << "WARNING: BarPresenter::layoutChanged called before graphics items are created!";
25 qDebug() << "WARNING: BarPresenter::layoutChanged called before graphics items are created!";
26 return;
26 return;
27 }
27 }
28
28
29 // Use temporary qreals for accurancy (we might get some compiler warnings... :)
29 // Use temporary qreals for accurancy (we might get some compiler warnings... :)
30 int categoryCount = mSeries->categoryCount();
30 int categoryCount = mSeries->categoryCount();
31 int setCount = mSeries->barsetCount();
31 int setCount = mSeries->barsetCount();
32
32
33 qreal tW = mWidth;
33 qreal tW = mWidth;
34 qreal tH = mHeight;
34 qreal tH = mHeight;
35 qreal tM = mSeries->max();
35 qreal tM = mSeries->max();
36 qreal scale = (tH/tM);
36 qreal scale = (tH/tM);
37 qreal tC = categoryCount + 1;
37 qreal tC = categoryCount + 1;
38 qreal categoryWidth = tW/tC;
38 qreal categoryWidth = tW/tC;
39 mBarWidth = categoryWidth / (setCount+1);
39 mBarWidth = categoryWidth / (setCount+1);
40
40
41 int itemIndex(0);
41 int itemIndex(0);
42 for (int category=0; category < categoryCount; category++) {
42 for (int category=0; category < categoryCount; category++) {
43 qreal xPos = categoryWidth * category + categoryWidth /2 + mBarWidth/2;
43 qreal xPos = categoryWidth * category + categoryWidth /2 + mBarWidth/2;
44 qreal yPos = mHeight;
44 qreal yPos = mHeight;
45 for (int set = 0; set < setCount; set++) {
45 for (int set = 0; set < setCount; set++) {
46 qreal barHeight = mSeries->valueAt(set,category) * scale;
46 qreal barHeight = mSeries->valueAt(set,category) * scale;
47 Bar* bar = mBars.at(itemIndex);
47 Bar* bar = mBars.at(itemIndex);
48
48
49 // TODO: width settable per bar?
49 // TODO: width settable per bar?
50 bar->resize(mBarWidth, barHeight);
50 bar->resize(mBarWidth, barHeight);
51 bar->setBrush(mSeries->barsetAt(set)->brush());
51 bar->setBrush(mSeries->barsetAt(set)->brush());
52 bar->setPos(xPos, yPos-barHeight);
52 bar->setPos(xPos, yPos-barHeight);
53 itemIndex++;
53 itemIndex++;
54 xPos += mBarWidth;
54 xPos += mBarWidth;
55 }
55 }
56 }
56 }
57
57 /*
58 // Position separators
58 // Position separators
59 qreal xPos = categoryWidth + categoryWidth/2;
59 qreal xPos = categoryWidth + categoryWidth/2;
60 for (int s=0; s < mSeparators.count(); s++) {
60 for (int s=0; s < mSeparators.count(); s++) {
61 Separator* sep = mSeparators.at(s);
61 Separator* sep = mSeparators.at(s);
62 sep->setPos(xPos,0);
62 sep->setPos(xPos,0);
63 sep->setSize(QSizeF(1,mHeight));
63 sep->setSize(QSizeF(1,mHeight));
64 sep->setColor(QColor(255,0,0,255)); // TODO: color for separations from theme
64 sep->setColor(QColor(255,0,0,255)); // TODO: color for separations from theme
65 xPos += categoryWidth;
65 xPos += categoryWidth;
66 }
66 }
67
67 */
68 // Position floating values
68 // Position floating values
69 itemIndex = 0;
69 itemIndex = 0;
70 for (int category=0; category < mSeries->categoryCount(); category++) {
70 for (int category=0; category < mSeries->categoryCount(); category++) {
71 qreal xPos = categoryWidth * category + categoryWidth/2 + mBarWidth;
71 qreal xPos = categoryWidth * category + categoryWidth/2 + mBarWidth;
72 qreal yPos = mHeight;
72 qreal yPos = mHeight;
73 for (int set=0; set < mSeries->barsetCount(); set++) {
73 for (int set=0; set < mSeries->barsetCount(); set++) {
74 qreal barHeight = mSeries->valueAt(set,category) * scale;
74 qreal barHeight = mSeries->valueAt(set,category) * scale;
75 BarValue* value = mFloatingValues.at(itemIndex);
75 BarValue* value = mFloatingValues.at(itemIndex);
76
76
77 QBarSet* barSet = mSeries->barsetAt(set);
77 QBarSet* barSet = mSeries->barsetAt(set);
78 value->resize(100,50); // TODO: proper layout for this.
78 value->resize(100,50); // TODO: proper layout for this.
79 value->setPos(xPos, yPos-barHeight/2);
79 value->setPos(xPos, yPos-barHeight/2);
80 value->setPen(barSet->floatingValuePen());
80 value->setPen(barSet->floatingValuePen());
81
81
82 if (mSeries->valueAt(set,category) != 0) {
82 if (mSeries->valueAt(set,category) != 0) {
83 value->setValueString(QString::number(mSeries->valueAt(set,category)));
83 value->setValueString(QString::number(mSeries->valueAt(set,category)));
84 } else {
84 } else {
85 value->setValueString(QString(""));
85 value->setValueString(QString(""));
86 }
86 }
87
87
88 itemIndex++;
88 itemIndex++;
89 xPos += mBarWidth;
89 xPos += mBarWidth;
90 }
90 }
91 }
91 }
92 }
92 }
93
93
94 #include "moc_barpresenter_p.cpp"
94 #include "moc_barpresenter_p.cpp"
95
95
96 QTCOMMERCIALCHART_END_NAMESPACE
96 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,180 +1,182
1 #include "barpresenterbase_p.h"
1 #include "barpresenterbase_p.h"
2 #include "bar_p.h"
2 #include "bar_p.h"
3 #include "barvalue_p.h"
3 #include "barvalue_p.h"
4 #include "separator_p.h"
4 #include "separator_p.h"
5 #include "qbarset.h"
5 #include "qbarset.h"
6 #include "qbarseries.h"
6 #include "qbarseries.h"
7 #include "qchart.h"
7 #include "qchart.h"
8 #include "qchartaxis.h"
8 #include "qchartaxis.h"
9 #include "qchartaxiscategories.h"
9 #include "qchartaxiscategories.h"
10 #include <QDebug>
10 #include <QDebug>
11 #include <QToolTip>
11 #include <QToolTip>
12
12
13 QTCOMMERCIALCHART_BEGIN_NAMESPACE
13 QTCOMMERCIALCHART_BEGIN_NAMESPACE
14
14
15 BarPresenterBase::BarPresenterBase(QBarSeries *series, QChart *parent) :
15 BarPresenterBase::BarPresenterBase(QBarSeries *series, QChart *parent) :
16 ChartItem(parent),
16 ChartItem(parent),
17 mLayoutSet(false),
17 mLayoutSet(false),
18 mSeries(series),
18 mSeries(series),
19 mChart(parent),
19 mChart(parent),
20 mWidth(0),
20 mWidth(0),
21 mHeight(0)
21 mHeight(0)
22 {
22 {
23 connect(series,SIGNAL(showToolTip(QPoint,QString)),this,SLOT(showToolTip(QPoint,QString)));
23 connect(series,SIGNAL(showToolTip(QPoint,QString)),this,SLOT(showToolTip(QPoint,QString)));
24 connect(series,SIGNAL(enableSeparators(bool)),this,SLOT(enableSeparators(bool)));
24 // connect(series,SIGNAL(enableSeparators(bool)),this,SLOT(enableSeparators(bool)));
25 enableSeparators(series->separatorsVisible());
25 // enableSeparators(series->separatorsVisible());
26 initAxisLabels();
26 initAxisLabels();
27 dataChanged();
27 dataChanged();
28 }
28 }
29
29
30 BarPresenterBase::~BarPresenterBase()
30 BarPresenterBase::~BarPresenterBase()
31 {
31 {
32 disconnect(this,SLOT(showToolTip(QPoint,QString)));
32 disconnect(this,SLOT(showToolTip(QPoint,QString)));
33 disconnect(this,SLOT(enableSeparators(bool)));
33 disconnect(this,SLOT(enableSeparators(bool)));
34 }
34 }
35
35
36 void BarPresenterBase::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
36 void BarPresenterBase::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
37 {
37 {
38 if (!mLayoutSet) {
38 if (!mLayoutSet) {
39 qDebug() << "BarPresenterBase::paint called without layout set. Aborting.";
39 qDebug() << "BarPresenterBase::paint called without layout set. Aborting.";
40 return;
40 return;
41 }
41 }
42 foreach(QGraphicsItem* i, childItems()) {
42 foreach(QGraphicsItem* i, childItems()) {
43 i->paint(painter,option,widget);
43 i->paint(painter,option,widget);
44 }
44 }
45 }
45 }
46
46
47 QRectF BarPresenterBase::boundingRect() const
47 QRectF BarPresenterBase::boundingRect() const
48 {
48 {
49 return QRectF(0, 0, mWidth, mHeight);
49 return QRectF(0, 0, mWidth, mHeight);
50 }
50 }
51
51
52 void BarPresenterBase::dataChanged()
52 void BarPresenterBase::dataChanged()
53 {
53 {
54 // TODO: performance optimizations. Do we really need to delete and create items every time data is changed or can we reuse them?
54 // TODO: performance optimizations. Do we really need to delete and create items every time data is changed or can we reuse them?
55 // Delete old bars
55 // Delete old bars
56 foreach (QGraphicsItem* item, childItems()) {
56 foreach (QGraphicsItem* item, childItems()) {
57 delete item;
57 delete item;
58 }
58 }
59
59
60 mBars.clear();
60 mBars.clear();
61 mSeparators.clear();
61 // mSeparators.clear();
62 mFloatingValues.clear();
62 mFloatingValues.clear();
63
63
64 // Create new graphic items for bars
64 // Create new graphic items for bars
65 for (int c=0; c<mSeries->categoryCount(); c++) {
65 for (int c=0; c<mSeries->categoryCount(); c++) {
66 QString category = mSeries->categoryName(c);
66 QString category = mSeries->categoryName(c);
67 for (int s=0; s<mSeries->barsetCount(); s++) {
67 for (int s=0; s<mSeries->barsetCount(); s++) {
68 QBarSet *set = mSeries->barsetAt(s);
68 QBarSet *set = mSeries->barsetAt(s);
69 Bar *bar = new Bar(category,this);
69 Bar *bar = new Bar(category,this);
70 childItems().append(bar);
70 childItems().append(bar);
71 mBars.append(bar);
71 mBars.append(bar);
72 connect(bar,SIGNAL(clicked(QString)),set,SIGNAL(clicked(QString)));
72 connect(bar,SIGNAL(clicked(QString)),set,SIGNAL(clicked(QString)));
73 connect(bar,SIGNAL(rightClicked(QString)),set,SIGNAL(rightClicked(QString)));
73 connect(bar,SIGNAL(rightClicked(QString)),set,SIGNAL(rightClicked(QString)));
74 connect(bar,SIGNAL(hoverEntered(QPoint)),set,SLOT(barHoverEnterEvent(QPoint)));
74 connect(bar,SIGNAL(hoverEntered(QPoint)),set,SLOT(barHoverEnterEvent(QPoint)));
75 connect(bar,SIGNAL(hoverLeaved()),set,SLOT(barHoverLeaveEvent()));
75 connect(bar,SIGNAL(hoverLeaved()),set,SLOT(barHoverLeaveEvent()));
76 }
76 }
77 }
77 }
78
78 /*
79 // Create separators
79 // Create separators
80 int count = mSeries->categoryCount() - 1; // There is one less separator than columns
80 int count = mSeries->categoryCount() - 1; // There is one less separator than columns
81 for (int i=0; i<count; i++) {
81 for (int i=0; i<count; i++) {
82 Separator* sep = new Separator(this);
82 Separator* sep = new Separator(this);
83 sep->setVisible(mSeries->separatorsVisible());
83 sep->setVisible(mSeries->separatorsVisible());
84 childItems().append(sep);
84 childItems().append(sep);
85 mSeparators.append(sep);
85 mSeparators.append(sep);
86 }
86 }
87
87 */
88 // Create floating values
88 // Create floating values
89 for (int category=0; category<mSeries->categoryCount(); category++) {
89 for (int category=0; category<mSeries->categoryCount(); category++) {
90 for (int s=0; s<mSeries->barsetCount(); s++) {
90 for (int s=0; s<mSeries->barsetCount(); s++) {
91 QBarSet *set = mSeries->barsetAt(s);
91 QBarSet *set = mSeries->barsetAt(s);
92 BarValue *value = new BarValue(*set, this);
92 BarValue *value = new BarValue(*set, this);
93 childItems().append(value);
93 childItems().append(value);
94 mFloatingValues.append(value);
94 mFloatingValues.append(value);
95 connect(set,SIGNAL(toggleFloatingValues()),value,SLOT(toggleVisible()));
95 connect(set,SIGNAL(toggleFloatingValues()),value,SLOT(toggleVisible()));
96 }
96 }
97 }
97 }
98 }
98 }
99
99
100 void BarPresenterBase::initAxisLabels()
100 void BarPresenterBase::initAxisLabels()
101 {
101 {
102 int count = mSeries->categoryCount();
102 int count = mSeries->categoryCount();
103 if (0 == count) {
103 if (0 == count) {
104 return;
104 return;
105 }
105 }
106
106
107 mChart->axisX()->setTicksCount(count+2);
107 mChart->axisX()->setTicksCount(count+2);
108
108
109 qreal min = 0;
109 qreal min = 0;
110 qreal max = count+1;
110 qreal max = count+1;
111
111
112 mChart->axisX()->setMin(min);
112 mChart->axisX()->setMin(min);
113 mChart->axisX()->setMax(max);
113 mChart->axisX()->setMax(max);
114
114
115 QChartAxisCategories* categories = mChart->axisX()->categories();
115 QChartAxisCategories* categories = mChart->axisX()->categories();
116 categories->clear();
116 categories->clear();
117 for (int i=0; i<count; i++) {
117 for (int i=0; i<count; i++) {
118 categories->insert(i+1,mSeries->categoryName(i));
118 categories->insert(i+1,mSeries->categoryName(i));
119 }
119 }
120
120
121
121
122
122
123 mChart->axisX()->setLabelsVisible(true);
123 mChart->axisX()->setLabelsVisible(true);
124 }
124 }
125
125
126 //handlers
126 //handlers
127
127
128 void BarPresenterBase::handleModelChanged(int index)
128 void BarPresenterBase::handleModelChanged(int index)
129 {
129 {
130 // qDebug() << "BarPresenterBase::handleModelChanged" << index;
130 // qDebug() << "BarPresenterBase::handleModelChanged" << index;
131 dataChanged();
131 dataChanged();
132 }
132 }
133
133
134 void BarPresenterBase::handleDomainChanged(const Domain& domain)
134 void BarPresenterBase::handleDomainChanged(const Domain& domain)
135 {
135 {
136 qDebug() << "BarPresenterBase::handleDomainChanged";
136 qDebug() << "BarPresenterBase::handleDomainChanged";
137 /*
137 /*
138 int count = mSeries->categoryCount();
138 int count = mSeries->categoryCount();
139 if (0 == count) {
139 if (0 == count) {
140 return;
140 return;
141 }
141 }
142
142
143 // Position labels to domain
143 // Position labels to domain
144 qreal min = domain.minX();
144 qreal min = domain.minX();
145 qreal max = domain.maxX();
145 qreal max = domain.maxX();
146 qreal step = (max-min)/count;
146 qreal step = (max-min)/count;
147 QChartAxisCategories& categories = mChart->axisX()->categories();
147 QChartAxisCategories& categories = mChart->axisX()->categories();
148 categories.clear();
148 categories.clear();
149 for (int i=0; i<count; i++) {
149 for (int i=0; i<count; i++) {
150 categories.insert(min,mSeries->categoryName(i));
150 categories.insert(min,mSeries->categoryName(i));
151 min += step;
151 min += step;
152 }
152 }
153 */
153 */
154 }
154 }
155
155
156 void BarPresenterBase::handleGeometryChanged(const QRectF& rect)
156 void BarPresenterBase::handleGeometryChanged(const QRectF& rect)
157 {
157 {
158 mWidth = rect.width();
158 mWidth = rect.width();
159 mHeight = rect.height();
159 mHeight = rect.height();
160 layoutChanged();
160 layoutChanged();
161 mLayoutSet = true;
161 mLayoutSet = true;
162 setPos(rect.topLeft());
162 setPos(rect.topLeft());
163 }
163 }
164
164
165 void BarPresenterBase::showToolTip(QPoint pos, QString tip)
165 void BarPresenterBase::showToolTip(QPoint pos, QString tip)
166 {
166 {
167 // TODO: cool tooltip instead of default
167 // TODO: cool tooltip instead of default
168 QToolTip::showText(pos,tip);
168 QToolTip::showText(pos,tip);
169 }
169 }
170
170
171 /*
171 void BarPresenterBase::enableSeparators(bool enabled)
172 void BarPresenterBase::enableSeparators(bool enabled)
172 {
173 {
173 for (int i=0; i<mSeparators.count(); i++) {
174 for (int i=0; i<mSeparators.count(); i++) {
174 mSeparators.at(i)->setVisible(enabled);
175 mSeparators.at(i)->setVisible(enabled);
175 }
176 }
176 }
177 }
178 */
177
179
178 #include "moc_barpresenterbase_p.cpp"
180 #include "moc_barpresenterbase_p.cpp"
179
181
180 QTCOMMERCIALCHART_END_NAMESPACE
182 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,67 +1,67
1 #ifndef BARPRESENTERBASE_H
1 #ifndef BARPRESENTERBASE_H
2 #define BARPRESENTERBASE_H
2 #define BARPRESENTERBASE_H
3
3
4 #include "chartitem_p.h"
4 #include "chartitem_p.h"
5 #include "qbarseries.h"
5 #include "qbarseries.h"
6 #include <QPen>
6 #include <QPen>
7 #include <QBrush>
7 #include <QBrush>
8 #include <QGraphicsItem>
8 #include <QGraphicsItem>
9
9
10 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10 QTCOMMERCIALCHART_BEGIN_NAMESPACE
11
11
12 class Bar;
12 class Bar;
13 class Separator;
13 class Separator;
14 class BarValue;
14 class BarValue;
15 class QChartAxisCategories;
15 class QChartAxisCategories;
16 class QChart;
16 class QChart;
17
17
18 // Common implemantation of different presenters. Not to be instantiated.
18 // Common implemantation of different presenters. Not to be instantiated.
19 // TODO: combine this with BarPresenter and derive other presenters from it?
19 // TODO: combine this with BarPresenter and derive other presenters from it?
20 class BarPresenterBase : public QObject, public ChartItem
20 class BarPresenterBase : public QObject, public ChartItem
21 {
21 {
22 Q_OBJECT
22 Q_OBJECT
23 public:
23 public:
24 BarPresenterBase(QBarSeries *series, QChart *parent = 0);
24 BarPresenterBase(QBarSeries *series, QChart *parent = 0);
25 virtual ~BarPresenterBase();
25 virtual ~BarPresenterBase();
26
26
27 public:
27 public:
28 // From QGraphicsItem
28 // From QGraphicsItem
29 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
29 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
30 QRectF boundingRect() const;
30 QRectF boundingRect() const;
31
31
32 // TODO: Consider the domain for layoutChanged. May be use case, may not be. If it is, then the derived classes need to implement it
32 // TODO: Consider the domain for layoutChanged. May be use case, may not be. If it is, then the derived classes need to implement it
33 virtual void dataChanged(); // data of series has changed -> need to recalculate bar sizes
33 virtual void dataChanged(); // data of series has changed -> need to recalculate bar sizes
34 virtual void layoutChanged() = 0; // layout has changed -> need to recalculate bar sizes
34 virtual void layoutChanged() = 0; // layout has changed -> need to recalculate bar sizes
35
35
36 protected:
36 protected:
37 void initAxisLabels();
37 void initAxisLabels();
38
38
39 public slots:
39 public slots:
40 void handleModelChanged(int index);
40 void handleModelChanged(int index);
41 void handleDomainChanged(const Domain& domain);
41 void handleDomainChanged(const Domain& domain);
42 void handleGeometryChanged(const QRectF& size);
42 void handleGeometryChanged(const QRectF& size);
43
43
44 // Internal slots
44 // Internal slots
45 void showToolTip(QPoint pos, QString tip); // shows tooltip (if enabled)
45 void showToolTip(QPoint pos, QString tip); // shows tooltip (if enabled)
46 void enableSeparators(bool enabled);
46 // void enableSeparators(bool enabled);
47
47
48 protected:
48 protected:
49
49
50 // TODO: consider these.
50 // TODO: consider these.
51 int mHeight; // Layout spesific
51 int mHeight; // Layout spesific
52 int mWidth;
52 int mWidth;
53 qreal mBarWidth;
53 qreal mBarWidth;
54
54
55 bool mLayoutSet; // True, if component has been laid out.
55 bool mLayoutSet; // True, if component has been laid out.
56
56
57 // Not owned.
57 // Not owned.
58 QBarSeries* mSeries;
58 QBarSeries* mSeries;
59 QList<Bar*> mBars;
59 QList<Bar*> mBars;
60 QList<Separator*> mSeparators;
60 // QList<Separator*> mSeparators;
61 QList<BarValue*> mFloatingValues;
61 QList<BarValue*> mFloatingValues;
62 QChart* mChart;
62 QChart* mChart;
63 };
63 };
64
64
65 QTCOMMERCIALCHART_END_NAMESPACE
65 QTCOMMERCIALCHART_END_NAMESPACE
66
66
67 #endif // BARPRESENTERBASE_H
67 #endif // BARPRESENTERBASE_H
@@ -1,103 +1,103
1 #include "percentbarpresenter_p.h"
1 #include "percentbarpresenter_p.h"
2 #include "bar_p.h"
2 #include "bar_p.h"
3 #include "barvalue_p.h"
3 #include "barvalue_p.h"
4 #include "separator_p.h"
4 #include "separator_p.h"
5 #include "qbarset.h"
5 #include "qbarset.h"
6 #include <QDebug>
6 #include <QDebug>
7
7
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9
9
10
10
11 PercentBarPresenter::PercentBarPresenter(QBarSeries *series, QChart *parent) :
11 PercentBarPresenter::PercentBarPresenter(QBarSeries *series, QChart *parent) :
12 BarPresenterBase(series, parent)
12 BarPresenterBase(series, parent)
13 {
13 {
14 }
14 }
15
15
16 void PercentBarPresenter::layoutChanged()
16 void PercentBarPresenter::layoutChanged()
17 {
17 {
18 // Scale bars to new layout
18 // Scale bars to new layout
19 // Layout for bars:
19 // Layout for bars:
20 if (mSeries->barsetCount() <= 0) {
20 if (mSeries->barsetCount() <= 0) {
21 qDebug() << "No sets in model!";
21 qDebug() << "No sets in model!";
22 // Nothing to do.
22 // Nothing to do.
23 return;
23 return;
24 }
24 }
25
25
26 if (childItems().count() == 0) {
26 if (childItems().count() == 0) {
27 qDebug() << "WARNING: PercentBarPresenter::layoutChanged called before graphics items are created!";
27 qDebug() << "WARNING: PercentBarPresenter::layoutChanged called before graphics items are created!";
28 return;
28 return;
29 }
29 }
30
30
31 // Use temporary qreals for accurancy (we might get some compiler warnings... :)
31 // Use temporary qreals for accurancy (we might get some compiler warnings... :)
32 qreal tW = mWidth;
32 qreal tW = mWidth;
33 qreal tC = mSeries->categoryCount() + 1;
33 qreal tC = mSeries->categoryCount() + 1;
34 qreal cC = mSeries->categoryCount() * 2 + 1;
34 qreal cC = mSeries->categoryCount() * 2 + 1;
35 mBarWidth = tW / cC;
35 mBarWidth = tW / cC;
36 qreal xStep = (tW/tC);
36 qreal xStep = (tW/tC);
37 qreal xPos = ((tW/tC) - mBarWidth / 2);
37 qreal xPos = ((tW/tC) - mBarWidth / 2);
38 qreal h = mHeight;
38 qreal h = mHeight;
39
39
40 int itemIndex(0);
40 int itemIndex(0);
41 for (int category = 0; category < mSeries->categoryCount(); category++) {
41 for (int category = 0; category < mSeries->categoryCount(); category++) {
42 qreal colSum = mSeries->categorySum(category);
42 qreal colSum = mSeries->categorySum(category);
43 qreal scale = (h / colSum);
43 qreal scale = (h / colSum);
44 qreal yPos = h;
44 qreal yPos = h;
45 for (int set=0; set < mSeries->barsetCount(); set++) {
45 for (int set=0; set < mSeries->barsetCount(); set++) {
46 qreal barHeight = mSeries->valueAt(set, category) * scale;
46 qreal barHeight = mSeries->valueAt(set, category) * scale;
47 Bar* bar = mBars.at(itemIndex);
47 Bar* bar = mBars.at(itemIndex);
48
48
49 // TODO: width settable per bar?
49 // TODO: width settable per bar?
50 bar->resize(mBarWidth, barHeight);
50 bar->resize(mBarWidth, barHeight);
51 bar->setBrush(mSeries->barsetAt(set)->brush());
51 bar->setBrush(mSeries->barsetAt(set)->brush());
52 bar->setPos(xPos, yPos-barHeight);
52 bar->setPos(xPos, yPos-barHeight);
53 itemIndex++;
53 itemIndex++;
54 yPos -= barHeight;
54 yPos -= barHeight;
55 }
55 }
56 xPos += xStep;
56 xPos += xStep;
57 }
57 }
58
58 /*
59 // Position separators
59 // Position separators
60 xPos = xStep + xStep/2;
60 xPos = xStep + xStep/2;
61 for (int s=0; s < mSeries->categoryCount() - 1; s++) {
61 for (int s=0; s < mSeries->categoryCount() - 1; s++) {
62 Separator* sep = mSeparators.at(s);
62 Separator* sep = mSeparators.at(s);
63 sep->setPos(xPos,0);
63 sep->setPos(xPos,0);
64 sep->setSize(QSizeF(1,mHeight));
64 sep->setSize(QSizeF(1,mHeight));
65 xPos += xStep;
65 xPos += xStep;
66 }
66 }
67
67 */
68 // Position floating values
68 // Position floating values
69 itemIndex = 0;
69 itemIndex = 0;
70 xPos = (tW/tC);
70 xPos = (tW/tC);
71 for (int category=0; category < mSeries->categoryCount(); category++) {
71 for (int category=0; category < mSeries->categoryCount(); category++) {
72 qreal yPos = h;
72 qreal yPos = h;
73 qreal colSum = mSeries->categorySum(category);
73 qreal colSum = mSeries->categorySum(category);
74 qreal scale = (h / colSum);
74 qreal scale = (h / colSum);
75 for (int set=0; set < mSeries->barsetCount(); set++) {
75 for (int set=0; set < mSeries->barsetCount(); set++) {
76 qreal barHeight = mSeries->valueAt(set,category) * scale;
76 qreal barHeight = mSeries->valueAt(set,category) * scale;
77 BarValue* value = mFloatingValues.at(itemIndex);
77 BarValue* value = mFloatingValues.at(itemIndex);
78
78
79 QBarSet* barSet = mSeries->barsetAt(set);
79 QBarSet* barSet = mSeries->barsetAt(set);
80 value->resize(100,50); // TODO: proper layout for this.
80 value->resize(100,50); // TODO: proper layout for this.
81 value->setPos(xPos, yPos-barHeight/2);
81 value->setPos(xPos, yPos-barHeight/2);
82 value->setPen(barSet->floatingValuePen());
82 value->setPen(barSet->floatingValuePen());
83
83
84 if (mSeries->valueAt(set,category) != 0) {
84 if (mSeries->valueAt(set,category) != 0) {
85 int p = mSeries->percentageAt(set,category) * 100;
85 int p = mSeries->percentageAt(set,category) * 100;
86 QString vString(QString::number(p));
86 QString vString(QString::number(p));
87 vString.truncate(3);
87 vString.truncate(3);
88 vString.append("%");
88 vString.append("%");
89 value->setValueString(vString);
89 value->setValueString(vString);
90 } else {
90 } else {
91 value->setValueString(QString(""));
91 value->setValueString(QString(""));
92 }
92 }
93
93
94 itemIndex++;
94 itemIndex++;
95 yPos -= barHeight;
95 yPos -= barHeight;
96 }
96 }
97 xPos += xStep;
97 xPos += xStep;
98 }
98 }
99 }
99 }
100
100
101 #include "moc_percentbarpresenter_p.cpp"
101 #include "moc_percentbarpresenter_p.cpp"
102
102
103 QTCOMMERCIALCHART_END_NAMESPACE
103 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,106 +1,106
1 #include "stackedbarpresenter_p.h"
1 #include "stackedbarpresenter_p.h"
2 #include "bar_p.h"
2 #include "bar_p.h"
3 #include "barvalue_p.h"
3 #include "barvalue_p.h"
4 #include "separator_p.h"
4 #include "separator_p.h"
5 #include "qbarset.h"
5 #include "qbarset.h"
6 #include <QDebug>
6 #include <QDebug>
7
7
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9
9
10 StackedBarPresenter::StackedBarPresenter(QBarSeries *series, QChart *parent) :
10 StackedBarPresenter::StackedBarPresenter(QBarSeries *series, QChart *parent) :
11 BarPresenterBase(series,parent)
11 BarPresenterBase(series,parent)
12 {
12 {
13 }
13 }
14
14
15 StackedBarPresenter::~StackedBarPresenter()
15 StackedBarPresenter::~StackedBarPresenter()
16 {
16 {
17 }
17 }
18
18
19
19
20 void StackedBarPresenter::layoutChanged()
20 void StackedBarPresenter::layoutChanged()
21 {
21 {
22 // Scale bars to new layout
22 // Scale bars to new layout
23 // Layout for bars:
23 // Layout for bars:
24 if (mSeries->barsetCount() <= 0) {
24 if (mSeries->barsetCount() <= 0) {
25 qDebug() << "No sets in model!";
25 qDebug() << "No sets in model!";
26 // Nothing to do.
26 // Nothing to do.
27 return;
27 return;
28 }
28 }
29
29
30 if (mSeries->categoryCount() == 0) {
30 if (mSeries->categoryCount() == 0) {
31 qDebug() << "No categories in model!";
31 qDebug() << "No categories in model!";
32 // Nothing to do
32 // Nothing to do
33 return;
33 return;
34 }
34 }
35
35
36 if (childItems().count() == 0) {
36 if (childItems().count() == 0) {
37 qDebug() << "WARNING: StackedBarPresenter::layoutChanged called before graphics items are created!";
37 qDebug() << "WARNING: StackedBarPresenter::layoutChanged called before graphics items are created!";
38 return;
38 return;
39 }
39 }
40
40
41 // Use temporary qreals for accurancy (we might get some compiler warnings... :)
41 // Use temporary qreals for accurancy (we might get some compiler warnings... :)
42 qreal maxSum = mSeries->maxCategorySum();
42 qreal maxSum = mSeries->maxCategorySum();
43 qreal h = mHeight;
43 qreal h = mHeight;
44 qreal scale = (h / maxSum);
44 qreal scale = (h / maxSum);
45 qreal tW = mWidth;
45 qreal tW = mWidth;
46 qreal tC = mSeries->categoryCount() + 1;
46 qreal tC = mSeries->categoryCount() + 1;
47 qreal cC = mSeries->categoryCount() * 2 + 1;
47 qreal cC = mSeries->categoryCount() * 2 + 1;
48 mBarWidth = tW / cC;
48 mBarWidth = tW / cC;
49 qreal xStep = (tW/tC);
49 qreal xStep = (tW/tC);
50 qreal xPos = ((tW/tC) - mBarWidth / 2);
50 qreal xPos = ((tW/tC) - mBarWidth / 2);
51
51
52 int itemIndex(0);
52 int itemIndex(0);
53 for (int category = 0; category < mSeries->categoryCount(); category++) {
53 for (int category = 0; category < mSeries->categoryCount(); category++) {
54 qreal yPos = h;
54 qreal yPos = h;
55 for (int set=0; set < mSeries->barsetCount(); set++) {
55 for (int set=0; set < mSeries->barsetCount(); set++) {
56 qreal barHeight = mSeries->valueAt(set, category) * scale;
56 qreal barHeight = mSeries->valueAt(set, category) * scale;
57 Bar* bar = mBars.at(itemIndex);
57 Bar* bar = mBars.at(itemIndex);
58
58
59 bar->resize(mBarWidth, barHeight);
59 bar->resize(mBarWidth, barHeight);
60 bar->setBrush(mSeries->barsetAt(set)->brush());
60 bar->setBrush(mSeries->barsetAt(set)->brush());
61 bar->setPos(xPos, yPos-barHeight);
61 bar->setPos(xPos, yPos-barHeight);
62 itemIndex++;
62 itemIndex++;
63 yPos -= barHeight;
63 yPos -= barHeight;
64 }
64 }
65 xPos += xStep;
65 xPos += xStep;
66 }
66 }
67
67 /*
68 // Position separators
68 // Position separators
69 xPos = xStep + xStep/2;
69 xPos = xStep + xStep/2;
70 for (int s=0; s < mSeries->categoryCount() - 1; s++) {
70 for (int s=0; s < mSeries->categoryCount() - 1; s++) {
71 Separator* sep = mSeparators.at(s);
71 Separator* sep = mSeparators.at(s);
72 sep->setPos(xPos,0);
72 sep->setPos(xPos,0);
73 sep->setSize(QSizeF(1,mHeight));
73 sep->setSize(QSizeF(1,mHeight));
74 xPos += xStep;
74 xPos += xStep;
75 }
75 }
76
76 */
77 // Position floating values
77 // Position floating values
78 itemIndex = 0;
78 itemIndex = 0;
79 xPos = (tW/tC);
79 xPos = (tW/tC);
80 for (int category=0; category < mSeries->categoryCount(); category++) {
80 for (int category=0; category < mSeries->categoryCount(); category++) {
81 qreal yPos = h;
81 qreal yPos = h;
82 for (int set=0; set < mSeries->barsetCount(); set++) {
82 for (int set=0; set < mSeries->barsetCount(); set++) {
83 qreal barHeight = mSeries->valueAt(set,category) * scale;
83 qreal barHeight = mSeries->valueAt(set,category) * scale;
84 BarValue* value = mFloatingValues.at(itemIndex);
84 BarValue* value = mFloatingValues.at(itemIndex);
85
85
86 QBarSet* barSet = mSeries->barsetAt(set);
86 QBarSet* barSet = mSeries->barsetAt(set);
87 value->resize(100,50); // TODO: proper layout for this.
87 value->resize(100,50); // TODO: proper layout for this.
88 value->setPos(xPos, yPos-barHeight/2);
88 value->setPos(xPos, yPos-barHeight/2);
89 value->setPen(barSet->floatingValuePen());
89 value->setPen(barSet->floatingValuePen());
90
90
91 if (mSeries->valueAt(set,category) != 0) {
91 if (mSeries->valueAt(set,category) != 0) {
92 value->setValueString(QString::number(mSeries->valueAt(set,category)));
92 value->setValueString(QString::number(mSeries->valueAt(set,category)));
93 } else {
93 } else {
94 value->setValueString(QString(""));
94 value->setValueString(QString(""));
95 }
95 }
96
96
97 itemIndex++;
97 itemIndex++;
98 yPos -= barHeight;
98 yPos -= barHeight;
99 }
99 }
100 xPos += xStep;
100 xPos += xStep;
101 }
101 }
102 }
102 }
103
103
104 #include "moc_stackedbarpresenter_p.cpp"
104 #include "moc_stackedbarpresenter_p.cpp"
105
105
106 QTCOMMERCIALCHART_END_NAMESPACE
106 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now