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