##// END OF EJS Templates
Fixed layout for barcharts
sauimone -
r473:8959911f7807
parent child
Show More
@@ -1,97 +1,95
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, QGraphicsItem *parent) :
10 BarPresenter::BarPresenter(QBarSeries *series, QGraphicsItem *parent) :
11 BarPresenterBase(series, parent)
11 BarPresenterBase(series, parent)
12 {
12 {
13 mBarDefaultWidth = 5;
13 mBarWidth = 5;
14 }
14 }
15
15
16 void BarPresenter::layoutChanged()
16 void BarPresenter::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 return;
22 return;
23 }
23 }
24
24
25 if (childItems().count() == 0) {
25 if (childItems().count() == 0) {
26 qDebug() << "WARNING: BarPresenter::layoutChanged called before graphics items are created!";
26 qDebug() << "WARNING: BarPresenter::layoutChanged called before graphics items are created!";
27 return;
27 return;
28 }
28 }
29
29
30 // TODO: better way to auto-layout?
30 // Use temporary qreals for accurancy (we might get some compiler warnings... :)
31 // Use reals for accurancy (we might get some compiler warnings... :)
32 int categoryCount = mSeries->categoryCount();
31 int categoryCount = mSeries->categoryCount();
33 int setCount = mSeries->barsetCount();
32 int setCount = mSeries->barsetCount();
34
33
35 qreal tW = mWidth;
34 qreal tW = mWidth;
36 qreal tH = mHeight;
35 qreal tH = mHeight;
37 qreal tM = mSeries->max();
36 qreal tM = mSeries->max();
38 qreal scale = (tH/tM);
37 qreal scale = (tH/tM);
39 qreal tC = categoryCount+1;
38 qreal tC = categoryCount + 1;
40 qreal xStepPerSet = (tW/tC);
39 mBarWidth = tW / ((categoryCount * setCount) + tC);
40 qreal xStepPerCategory = (tW/tC) + mBarWidth;
41
41
42 // Scaling.
43 int itemIndex(0);
42 int itemIndex(0);
44 int labelIndex(0);
43 int labelIndex(0);
45
44
46 for (int category=0; category < categoryCount; category++) {
45 for (int category=0; category < categoryCount; category++) {
47 qreal xPos = xStepPerSet * category + ((tW + mBarDefaultWidth*setCount)/(categoryCount*2));
46 qreal xPos = xStepPerCategory * category;
48 qreal yPos = mHeight;
47 qreal yPos = mHeight;
49 for (int set = 0; set < setCount; set++) {
48 for (int set = 0; set < setCount; set++) {
50 qreal barHeight = mSeries->valueAt(set,category) * scale;
49 qreal barHeight = mSeries->valueAt(set,category) * scale;
51 Bar* bar = mBars.at(itemIndex);
50 Bar* bar = mBars.at(itemIndex);
52
51
53 // TODO: width settable per bar?
52 // TODO: width settable per bar?
54 bar->resize(mBarDefaultWidth, barHeight);
53 bar->resize(mBarWidth, barHeight);
55 bar->setBrush(mSeries->barsetAt(set)->brush());
54 bar->setBrush(mSeries->barsetAt(set)->brush());
56 bar->setPos(xPos, yPos-barHeight);
55 bar->setPos(xPos, yPos-barHeight);
57 itemIndex++;
56 itemIndex++;
58 xPos += mBarDefaultWidth;
57 xPos += mBarWidth;
59 }
58 }
60
59
61 // TODO: Layout for labels, remove magic number
60 // TODO: Layout for labels, remove magic number
62 xPos = xStepPerSet * category + ((tW + mBarDefaultWidth*setCount)/(categoryCount*2));
61 xPos = xStepPerCategory * category + mBarWidth/2;
63 BarLabel* label = mLabels.at(labelIndex);
62 BarLabel* label = mLabels.at(labelIndex);
64 label->setPos(xPos, mHeight + 20);
63 label->setPos(xPos, mHeight - 20);
65 labelIndex++;
64 labelIndex++;
66 }
65 }
67
66
68 // Position floating values
67 // Position floating values
69 itemIndex = 0;
68 itemIndex = 0;
70 for (int category=0; category < mSeries->categoryCount(); category++) {
69 for (int category=0; category < mSeries->categoryCount(); category++) {
71 qreal xPos = xStepPerSet * category + ((tW + mBarDefaultWidth*setCount)/(categoryCount*2));
70 qreal xPos = xStepPerCategory * category + mBarWidth/2;
72 qreal yPos = mHeight;
71 qreal yPos = mHeight;
73 for (int set=0; set < mSeries->barsetCount(); set++) {
72 for (int set=0; set < mSeries->barsetCount(); set++) {
74 qreal barHeight = mSeries->valueAt(set,category) * scale;
73 qreal barHeight = mSeries->valueAt(set,category) * scale;
75 BarValue* value = mFloatingValues.at(itemIndex);
74 BarValue* value = mFloatingValues.at(itemIndex);
76
75
77 // TODO: remove hard coding, apply layout
76 // TODO: remove hard coding, apply layout
78 value->resize(100,50);
77 value->resize(100,50);
79 value->setPos(xPos, yPos-barHeight/2);
78 value->setPos(xPos, yPos-barHeight/2);
80 value->setPen(QPen(QColor(255,255,255,255)));
79 value->setPen(QPen(QColor(255,255,255,255)));
81
80
82 if (mSeries->valueAt(set,category) != 0) {
81 if (mSeries->valueAt(set,category) != 0) {
83 value->setValueString(QString::number(mSeries->valueAt(set,category)));
82 value->setValueString(QString::number(mSeries->valueAt(set,category)));
84 } else {
83 } else {
85 value->setValueString(QString(""));
84 value->setValueString(QString(""));
86 }
85 }
87
86
88 itemIndex++;
87 itemIndex++;
89 xPos += mBarDefaultWidth;
88 xPos += mBarWidth;
90 }
89 }
91 }
90 }
92 mLayoutDirty = true;
93 }
91 }
94
92
95 #include "moc_barpresenter_p.cpp"
93 #include "moc_barpresenter_p.cpp"
96
94
97 QTCOMMERCIALCHART_END_NAMESPACE
95 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,159 +1,150
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 <QDebug>
8 #include <QDebug>
9 #include <QToolTip>
9 #include <QToolTip>
10
10
11 QTCOMMERCIALCHART_BEGIN_NAMESPACE
11 QTCOMMERCIALCHART_BEGIN_NAMESPACE
12
12
13 BarPresenterBase::BarPresenterBase(QBarSeries *series, QGraphicsItem *parent)
13 BarPresenterBase::BarPresenterBase(QBarSeries *series, QGraphicsItem *parent)
14 : ChartItem(parent)
14 : ChartItem(parent)
15 ,mBarDefaultWidth(20) // TODO: remove hard coding, when we have layout code ready
15 ,mBarWidth(20) // TODO: remove hard coding, when we have layout code ready
16 ,mLayoutSet(false)
16 ,mLayoutSet(false)
17 ,mLayoutDirty(true)
18 ,mSeparatorsEnabled(false)
17 ,mSeparatorsEnabled(false)
19 ,mSeries(series)
18 ,mSeries(series)
20 {
19 {
21 connect(series,SIGNAL(showToolTip(QPoint,QString)),this,SLOT(showToolTip(QPoint,QString)));
20 connect(series,SIGNAL(showToolTip(QPoint,QString)),this,SLOT(showToolTip(QPoint,QString)));
22 // connect(series,SIGNAL(separatorsEnabled(bool)),this,SLOT(enableSeparators(bool)));
23 dataChanged();
21 dataChanged();
24 }
22 }
25
23
26 BarPresenterBase::~BarPresenterBase()
24 BarPresenterBase::~BarPresenterBase()
27 {
25 {
28 disconnect(this,SLOT(showToolTip(QPoint,QString)));
26 disconnect(this,SLOT(showToolTip(QPoint,QString)));
29 }
27 }
30
28
31 void BarPresenterBase::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
29 void BarPresenterBase::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
32 {
30 {
33 if (!mLayoutSet) {
31 if (!mLayoutSet) {
34 qDebug() << "BarPresenterBase::paint called without layout set. Aborting.";
32 qDebug() << "BarPresenterBase::paint called without layout set. Aborting.";
35 return;
33 return;
36 }
34 }
37 // if (mLayoutDirty) {
35 foreach(QGraphicsItem* i, childItems()) {
38 // Layout or data has changed. Need to redraw.
36 i->paint(painter,option,widget);
39 foreach(QGraphicsItem* i, childItems()) {
37 }
40 i->paint(painter,option,widget);
41 }
42 // }
43 }
38 }
44
39
45 QRectF BarPresenterBase::boundingRect() const
40 QRectF BarPresenterBase::boundingRect() const
46 {
41 {
47 return QRectF(0,0,mWidth,mHeight);
42 return QRectF(0,0,mWidth,mHeight);
48 }
43 }
49
44
50 void BarPresenterBase::setBarWidth( int w )
45 void BarPresenterBase::setBarWidth( int w )
51 {
46 {
52 mBarDefaultWidth = w;
47 mBarWidth = w;
53 }
48 }
54
49
55 void BarPresenterBase::dataChanged()
50 void BarPresenterBase::dataChanged()
56 {
51 {
57 // TODO: performance optimizations. Do we really need to delete and create items every time data is changed or can we reuse them?
52 // TODO: performance optimizations. Do we really need to delete and create items every time data is changed or can we reuse them?
58 // qDebug() << "datachanged";
59 // Delete old bars
53 // Delete old bars
60 foreach (QGraphicsItem* item, childItems()) {
54 foreach (QGraphicsItem* item, childItems()) {
61 delete item;
55 delete item;
62 }
56 }
63
57
64 mBars.clear();
58 mBars.clear();
65 mLabels.clear();
59 mLabels.clear();
66 mSeparators.clear();
60 mSeparators.clear();
67 mFloatingValues.clear();
61 mFloatingValues.clear();
68
62
69 // Create new graphic items for bars
63 // Create new graphic items for bars
70 for (int c=0; c<mSeries->categoryCount(); c++) {
64 for (int c=0; c<mSeries->categoryCount(); c++) {
71 QString category = mSeries->categoryName(c);
65 QString category = mSeries->categoryName(c);
72 for (int s=0; s<mSeries->barsetCount(); s++) {
66 for (int s=0; s<mSeries->barsetCount(); s++) {
73 QBarSet *set = mSeries->barsetAt(s);
67 QBarSet *set = mSeries->barsetAt(s);
74 Bar *bar = new Bar(category,this);
68 Bar *bar = new Bar(category,this);
75 childItems().append(bar);
69 childItems().append(bar);
76 mBars.append(bar);
70 mBars.append(bar);
77 connect(bar,SIGNAL(clicked(QString)),set,SIGNAL(clicked(QString)));
71 connect(bar,SIGNAL(clicked(QString)),set,SIGNAL(clicked(QString)));
78 connect(bar,SIGNAL(rightClicked(QString)),set,SIGNAL(rightClicked(QString)));
72 connect(bar,SIGNAL(rightClicked(QString)),set,SIGNAL(rightClicked(QString)));
79 connect(bar,SIGNAL(hoverEntered(QPoint)),set,SLOT(barHoverEnterEvent(QPoint)));
73 connect(bar,SIGNAL(hoverEntered(QPoint)),set,SLOT(barHoverEnterEvent(QPoint)));
80 connect(bar,SIGNAL(hoverLeaved()),set,SLOT(barHoverLeaveEvent()));
74 connect(bar,SIGNAL(hoverLeaved()),set,SLOT(barHoverLeaveEvent()));
81 }
75 }
82 }
76 }
83
77
84 // Create labels
78 // Create labels
85 int count = mSeries->categoryCount();
79 int count = mSeries->categoryCount();
86 for (int i=0; i<count; i++) {
80 for (int i=0; i<count; i++) {
87 BarLabel* label = new BarLabel(this);
81 BarLabel* label = new BarLabel(this);
88 label->set(mSeries->categoryName(i));
82 label->set(mSeries->categoryName(i));
89 childItems().append(label);
83 childItems().append(label);
90 mLabels.append(label);
84 mLabels.append(label);
91 }
85 }
92
86
93 // Create separators
87 // Create separators
94 count = mSeries->categoryCount() - 1; // There is one less separator than columns
88 count = mSeries->categoryCount() - 1; // There is one less separator than columns
95 for (int i=0; i<count; i++) {
89 for (int i=0; i<count; i++) {
96 Separator* sep = new Separator(this);
90 Separator* sep = new Separator(this);
97 sep->setColor(QColor(255,0,0,255)); // TODO: color for separations from theme
91 sep->setColor(QColor(255,0,0,255)); // TODO: color for separations from theme
98 sep->setVisible(mSeparatorsEnabled);
92 sep->setVisible(mSeparatorsEnabled);
99 childItems().append(sep);
93 childItems().append(sep);
100 mSeparators.append(sep);
94 mSeparators.append(sep);
101 }
95 }
102
96
103 // Create floating values
97 // Create floating values
104 for (int category=0; category<mSeries->categoryCount(); category++) {
98 for (int category=0; category<mSeries->categoryCount(); category++) {
105 for (int s=0; s<mSeries->barsetCount(); s++) {
99 for (int s=0; s<mSeries->barsetCount(); s++) {
106 QBarSet *set = mSeries->barsetAt(s);
100 QBarSet *set = mSeries->barsetAt(s);
107 BarValue *value = new BarValue(*set, this);
101 BarValue *value = new BarValue(*set, this);
108 childItems().append(value);
102 childItems().append(value);
109 mFloatingValues.append(value);
103 mFloatingValues.append(value);
110 connect(set,SIGNAL(toggleFloatingValues()),value,SLOT(toggleVisible()));
104 connect(set,SIGNAL(toggleFloatingValues()),value,SLOT(toggleVisible()));
111 }
105 }
112 }
106 }
113
114 // TODO: if (autolayout) { layoutChanged() } or something
115 mLayoutDirty = true;
116 }
107 }
117
108
118 //handlers
109 //handlers
119
110
120 void BarPresenterBase::handleModelChanged(int index)
111 void BarPresenterBase::handleModelChanged(int index)
121 {
112 {
122 // qDebug() << "BarPresenterBase::handleModelChanged" << index;
113 // qDebug() << "BarPresenterBase::handleModelChanged" << index;
123 dataChanged();
114 dataChanged();
124 }
115 }
125
116
126 void BarPresenterBase::handleDomainChanged(const Domain& domain)
117 void BarPresenterBase::handleDomainChanged(const Domain& domain)
127 {
118 {
128 // qDebug() << "BarPresenterBase::handleDomainChanged";
119 // qDebug() << "BarPresenterBase::handleDomainChanged";
129 // TODO: Figure out the use case for this.
120 // TODO: Figure out the use case for this.
130 // Affects the size of visible item, so layout is changed.
121 // Affects the size of visible item, so layout is changed.
131 // layoutChanged();
122 // layoutChanged();
132 }
123 }
133
124
134 void BarPresenterBase::handleGeometryChanged(const QRectF& rect)
125 void BarPresenterBase::handleGeometryChanged(const QRectF& rect)
135 {
126 {
136 mWidth = rect.width();
127 mWidth = rect.width();
137 mHeight = rect.height();
128 mHeight = rect.height();
138 layoutChanged();
129 layoutChanged();
139 mLayoutSet = true;
130 mLayoutSet = true;
140 setPos(rect.topLeft());
131 setPos(rect.topLeft());
141 }
132 }
142
133
143 void BarPresenterBase::showToolTip(QPoint pos, QString tip)
134 void BarPresenterBase::showToolTip(QPoint pos, QString tip)
144 {
135 {
145 // TODO: cool tooltip instead of default
136 // TODO: cool tooltip instead of default
146 QToolTip::showText(pos,tip);
137 QToolTip::showText(pos,tip);
147 }
138 }
148
139
149 void BarPresenterBase::enableSeparators(bool enabled)
140 void BarPresenterBase::enableSeparators(bool enabled)
150 {
141 {
151 for (int i=0; i<mSeparators.count(); i++) {
142 for (int i=0; i<mSeparators.count(); i++) {
152 mSeparators.at(i)->setVisible(enabled);
143 mSeparators.at(i)->setVisible(enabled);
153 }
144 }
154 mSeparatorsEnabled = enabled;
145 mSeparatorsEnabled = enabled;
155 }
146 }
156
147
157 #include "moc_barpresenterbase_p.cpp"
148 #include "moc_barpresenterbase_p.cpp"
158
149
159 QTCOMMERCIALCHART_END_NAMESPACE
150 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,72 +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;
13 class BarLabel;
14 class Separator;
14 class Separator;
15 class BarValue;
15 class BarValue;
16
16
17 // Common implemantation of different presenters. Not to be instantiated.
17 // Common implemantation of different presenters. Not to be instantiated.
18 // TODO: combine this with BarPresenter and derive other presenters from it?
18 // TODO: combine this with BarPresenter and derive other presenters from it?
19 class BarPresenterBase : public QObject, public ChartItem
19 class BarPresenterBase : public QObject, public ChartItem
20 {
20 {
21 Q_OBJECT
21 Q_OBJECT
22 public:
22 public:
23 BarPresenterBase(QBarSeries *series, QGraphicsItem *parent = 0);
23 BarPresenterBase(QBarSeries *series, QGraphicsItem *parent = 0);
24 virtual ~BarPresenterBase();
24 virtual ~BarPresenterBase();
25
25
26 public:
26 public:
27 // From QGraphicsItem
27 // From QGraphicsItem
28 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
28 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
29 QRectF boundingRect() const;
29 QRectF boundingRect() const;
30
30
31 // TODO: these may change with layout awarness.
31 // TODO: these may change with layout awarness.
32 void setBarWidth( int w );
32 void setBarWidth( int w );
33
33
34 // 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 // TODO: Consider the domain for layoutChanged. May be use case, may not be. If it is, then the derived classes need to implement it
35 virtual void dataChanged(); // data of series has changed -> need to recalculate bar sizes
35 virtual void dataChanged(); // data of series has changed -> need to recalculate bar sizes
36 virtual void layoutChanged() = 0; // layout has changed -> need to recalculate bar sizes
36 virtual void layoutChanged() = 0; // layout has changed -> need to recalculate bar sizes
37
37
38 public slots:
38 public slots:
39 void handleModelChanged(int index);
39 void handleModelChanged(int index);
40 void handleDomainChanged(const Domain& domain);
40 void handleDomainChanged(const Domain& domain);
41 void handleGeometryChanged(const QRectF& size);
41 void handleGeometryChanged(const QRectF& size);
42
42
43 // Internal slots
43 // Internal slots
44 void showToolTip(QPoint pos, QString tip); // shows tooltip (if enabled)
44 void showToolTip(QPoint pos, QString tip); // shows tooltip (if enabled)
45 void enableSeparators(bool enabled);
45 void enableSeparators(bool enabled);
46
46
47 protected:
47 protected:
48
48
49 // TODO: consider these.
49 // TODO: consider these.
50 int mHeight; // Layout spesific
50 int mHeight; // Layout spesific
51 int mWidth;
51 int mWidth;
52 int mBarDefaultWidth;
52 qreal mBarWidth;
53
53
54 bool mLayoutSet; // True, if component has been laid out.
54 bool mLayoutSet; // True, if component has been laid out.
55 bool mLayoutDirty;
56
57 bool mSeparatorsEnabled;
55 bool mSeparatorsEnabled;
58
56
59 // Owned
60 QBarSeries* mSeries;
61
62 // Not owned.
57 // Not owned.
58 QBarSeries* mSeries;
63 QList<Bar*> mBars;
59 QList<Bar*> mBars;
64 QList<BarLabel*> mLabels;
60 QList<BarLabel*> mLabels;
65 QList<Separator*> mSeparators;
61 QList<Separator*> mSeparators;
66 QList<BarValue*> mFloatingValues;
62 QList<BarValue*> mFloatingValues;
67
63
68 };
64 };
69
65
70 QTCOMMERCIALCHART_END_NAMESPACE
66 QTCOMMERCIALCHART_END_NAMESPACE
71
67
72 #endif // BARPRESENTERBASE_H
68 #endif // BARPRESENTERBASE_H
@@ -1,66 +1,66
1 #include "barvalue_p.h"
1 #include "barvalue_p.h"
2 #include <QPainter>
2 #include <QPainter>
3 #include <QPen>
3 #include <QPen>
4
4
5 QTCOMMERCIALCHART_BEGIN_NAMESPACE
5 QTCOMMERCIALCHART_BEGIN_NAMESPACE
6
6
7 BarValue::BarValue(QBarSet &set, QGraphicsItem *parent)
7 BarValue::BarValue(QBarSet &set, QGraphicsItem *parent)
8 : QGraphicsObject(parent)
8 : QGraphicsObject(parent)
9 ,mBarSet(set)
9 ,mBarSet(set)
10 {
10 {
11 setVisible(false);
11 setVisible(false);
12 }
12 }
13
13
14 void BarValue::setValueString(QString str)
14 void BarValue::setValueString(QString str)
15 {
15 {
16 mValueString = str;
16 mValueString = str;
17 }
17 }
18
18
19 QString BarValue::valueString()
19 QString BarValue::valueString()
20 {
20 {
21 return mValueString;
21 return mValueString;
22 }
22 }
23
23
24 void BarValue::setPen(const QPen& pen)
24 void BarValue::setPen(const QPen pen)
25 {
25 {
26 mPen = pen;
26 mPen = pen;
27 }
27 }
28
28
29 const QPen& BarValue::pen()
29 QPen BarValue::pen() const
30 {
30 {
31 return mPen;
31 return mPen;
32 }
32 }
33
33
34 void BarValue::resize(qreal w, qreal h)
34 void BarValue::resize(qreal w, qreal h)
35 {
35 {
36 mWidth = w;
36 mWidth = w;
37 mHeight = h;
37 mHeight = h;
38 }
38 }
39
39
40 void BarValue::setPos(qreal x, qreal y)
40 void BarValue::setPos(qreal x, qreal y)
41 {
41 {
42 mXpos = x;
42 mXpos = x;
43 mYpos = y;
43 mYpos = y;
44 }
44 }
45
45
46 void BarValue::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
46 void BarValue::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
47 {
47 {
48 if (isVisible()) {
48 if (isVisible()) {
49 painter->setPen(mPen);
49 painter->setPen(mPen);
50 painter->drawText(boundingRect(),mValueString);
50 painter->drawText(boundingRect(),mValueString);
51 }
51 }
52 }
52 }
53
53
54 QRectF BarValue::boundingRect() const
54 QRectF BarValue::boundingRect() const
55 {
55 {
56 QRectF r(mXpos, mYpos, mWidth, mHeight);
56 QRectF r(mXpos, mYpos, mWidth, mHeight);
57 return r;
57 return r;
58 }
58 }
59
59
60 void BarValue::toggleVisible()
60 void BarValue::toggleVisible()
61 {
61 {
62 setVisible(!isVisible());
62 setVisible(!isVisible());
63 }
63 }
64
64
65 #include "moc_barvalue_p.cpp"
65 #include "moc_barvalue_p.cpp"
66 QTCOMMERCIALCHART_END_NAMESPACE
66 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,50 +1,50
1 #ifndef BARVALUE_P_H
1 #ifndef BARVALUE_P_H
2 #define BARVALUE_P_H
2 #define BARVALUE_P_H
3
3
4 #include "qchartglobal.h"
4 #include "qchartglobal.h"
5 #include <QGraphicsObject>
5 #include <QGraphicsObject>
6 #include <QPen>
6 #include <QPen>
7
7
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9
9
10 class QBarSet;
10 class QBarSet;
11
11
12 // Visual class for floating bar values
12 // Visual class for floating bar values
13 // By default these are not visible.
13 // By default these are not visible.
14 class BarValue : public QGraphicsObject
14 class BarValue : public QGraphicsObject
15 {
15 {
16 Q_OBJECT
16 Q_OBJECT
17 public:
17 public:
18 BarValue(QBarSet &set, QGraphicsItem *parent = 0);
18 BarValue(QBarSet &set, QGraphicsItem *parent = 0);
19
19
20 void setValueString(QString str);
20 void setValueString(QString str);
21 QString valueString();
21 QString valueString();
22
22
23 void setPen(const QPen& pen);
23 void setPen(const QPen pen);
24 const QPen& pen();
24 QPen pen() const;
25
25
26 void resize(qreal w, qreal h);
26 void resize(qreal w, qreal h);
27 void setPos(qreal x, qreal y);
27 void setPos(qreal x, qreal y);
28
28
29 // From QGraphicsItem
29 // From QGraphicsItem
30 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
30 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
31 QRectF boundingRect() const;
31 QRectF boundingRect() const;
32
32
33 public Q_SLOTS:
33 public Q_SLOTS:
34 void toggleVisible();
34 void toggleVisible();
35
35
36 private:
36 private:
37
37
38 QBarSet& mBarSet;
38 QBarSet& mBarSet;
39 QPen mPen;
39 QPen mPen;
40 QString mValueString;
40 QString mValueString;
41
41
42 qreal mXpos;
42 qreal mXpos;
43 qreal mYpos;
43 qreal mYpos;
44 qreal mWidth;
44 qreal mWidth;
45 qreal mHeight;
45 qreal mHeight;
46 };
46 };
47
47
48 QTCOMMERCIALCHART_END_NAMESPACE
48 QTCOMMERCIALCHART_END_NAMESPACE
49
49
50 #endif // BARVALUE_P_H
50 #endif // BARVALUE_P_H
@@ -1,112 +1,110
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, QGraphicsItem *parent) :
12 PercentBarPresenter::PercentBarPresenter(QBarSeries *series, QGraphicsItem *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 // TODO: better way to auto-layout
32 // Use temporary qreals for accurancy (we might get some compiler warnings... :)
33 // Use reals for accurancy (we might get some compiler warnings... :)
34 int count = mSeries->categoryCount();
35 int itemIndex(0);
36 int labelIndex(0);
37 qreal tW = mWidth;
33 qreal tW = mWidth;
38 qreal tC = count+1;
34 qreal tC = mSeries->categoryCount() + 1;
35 qreal cC = mSeries->categoryCount() * 2 + 1;
36 mBarWidth = tW / cC;
39 qreal xStep = (tW/tC);
37 qreal xStep = (tW/tC);
40 qreal xPos = ((tW/tC) - mBarDefaultWidth / 2);
38 qreal xPos = ((tW/tC) - mBarWidth / 2);
41 qreal h = mHeight;
39 qreal h = mHeight;
42
40
41 int itemIndex(0);
42 int labelIndex(0);
43 for (int category = 0; category < mSeries->categoryCount(); category++) {
43 for (int category = 0; category < mSeries->categoryCount(); category++) {
44 qreal colSum = mSeries->categorySum(category);
44 qreal colSum = mSeries->categorySum(category);
45 qreal scale = (h / colSum);
45 qreal scale = (h / colSum);
46 qreal yPos = h;
46 qreal yPos = h;
47 for (int set=0; set < mSeries->barsetCount(); set++) {
47 for (int set=0; set < mSeries->barsetCount(); set++) {
48 qreal barHeight = mSeries->valueAt(set, category) * scale;
48 qreal barHeight = mSeries->valueAt(set, category) * scale;
49 Bar* bar = mBars.at(itemIndex);
49 Bar* bar = mBars.at(itemIndex);
50
50
51 // TODO: width settable per bar?
51 // TODO: width settable per bar?
52 bar->resize(mBarDefaultWidth, barHeight);
52 bar->resize(mBarWidth, barHeight);
53 bar->setBrush(mSeries->barsetAt(set)->brush());
53 bar->setBrush(mSeries->barsetAt(set)->brush());
54 bar->setPos(xPos, yPos-barHeight);
54 bar->setPos(xPos, yPos-barHeight);
55 itemIndex++;
55 itemIndex++;
56 yPos -= barHeight;
56 yPos -= barHeight;
57 }
57 }
58
58
59 // TODO: Layout for labels, remove magic number
59 // TODO: Layout for labels, remove magic number
60 BarLabel* label = mLabels.at(labelIndex);
60 BarLabel* label = mLabels.at(labelIndex);
61 label->setPos(xPos, mHeight + 20);
61 label->setPos(xPos, mHeight + 20);
62 labelIndex++;
62 labelIndex++;
63 xPos += xStep;
63 xPos += xStep;
64 }
64 }
65
65
66 // Position separators
66 // Position separators
67 xPos = xStep + xStep/2;
67 xPos = xStep + xStep/2;
68 for (int s=0; s < mSeries->categoryCount() - 1; s++) {
68 for (int s=0; s < mSeries->categoryCount() - 1; s++) {
69 Separator* sep = mSeparators.at(s);
69 Separator* sep = mSeparators.at(s);
70 sep->setPos(xPos,0);
70 sep->setPos(xPos,0);
71 sep->setSize(QSizeF(1,mHeight));
71 sep->setSize(QSizeF(1,mHeight));
72 xPos += xStep;
72 xPos += xStep;
73 }
73 }
74
74
75 // Position floating values
75 // Position floating values
76 itemIndex = 0;
76 itemIndex = 0;
77 xPos = ((tW/tC) - mBarDefaultWidth / 2);
77 xPos = ((tW/tC) - mBarWidth / 2);
78 for (int category=0; category < mSeries->categoryCount(); category++) {
78 for (int category=0; category < mSeries->categoryCount(); category++) {
79 qreal yPos = h;
79 qreal yPos = h;
80 qreal colSum = mSeries->categorySum(category);
80 qreal colSum = mSeries->categorySum(category);
81 qreal scale = (h / colSum);
81 qreal scale = (h / colSum);
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 // TODO: remove hard coding, apply layout
86 // TODO: remove hard coding, apply layout
87 value->resize(100,50);
87 value->resize(100,50);
88 value->setPos(xPos, yPos-barHeight/2);
88 value->setPos(xPos, yPos-barHeight/2);
89 value->setPen(QPen(QColor(255,255,255,255)));
89 value->setPen(QPen(QColor(255,255,255,255)));
90
90
91 if (mSeries->valueAt(set,category) != 0) {
91 if (mSeries->valueAt(set,category) != 0) {
92 int p = mSeries->percentageAt(set,category) * 100;
92 int p = mSeries->percentageAt(set,category) * 100;
93 QString vString(QString::number(p));
93 QString vString(QString::number(p));
94 vString.truncate(3);
94 vString.truncate(3);
95 vString.append("%");
95 vString.append("%");
96 value->setValueString(vString);
96 value->setValueString(vString);
97 } else {
97 } else {
98 value->setValueString(QString(""));
98 value->setValueString(QString(""));
99 }
99 }
100
100
101 itemIndex++;
101 itemIndex++;
102 yPos -= barHeight;
102 yPos -= barHeight;
103 }
103 }
104 xPos += xStep;
104 xPos += xStep;
105 }
105 }
106
107 mLayoutDirty = true;
108 }
106 }
109
107
110 #include "moc_percentbarpresenter_p.cpp"
108 #include "moc_percentbarpresenter_p.cpp"
111
109
112 QTCOMMERCIALCHART_END_NAMESPACE
110 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,173 +1,173
1 #include "qbarset.h"
1 #include "qbarset.h"
2 #include <QDebug>
2 #include <QDebug>
3 #include <QToolTip>
3 #include <QToolTip>
4
4
5 QTCOMMERCIALCHART_BEGIN_NAMESPACE
5 QTCOMMERCIALCHART_BEGIN_NAMESPACE
6
6
7 /*!
7 /*!
8 \class QBarSet
8 \class QBarSet
9 \brief part of QtCommercial chart API.
9 \brief part of QtCommercial chart API.
10
10
11 QBarSet represents one set of bars. Set of bars contains one data value for each category.
11 QBarSet represents one set of bars. Set of bars contains one data value for each category.
12 First value of set is assumed to belong to first category, second to second category and so on.
12 First value of set is assumed to belong to first category, second to second category and so on.
13 If set has fewer values than there are categories, then the missing values are assumed to be
13 If set has fewer values than there are categories, then the missing values are assumed to be
14 at the end of set. For missing values in middle of a set, numerical value of zero is used.
14 at the end of set. For missing values in middle of a set, numerical value of zero is used.
15
15
16 \mainclass
16 \mainclass
17
17
18 \sa QBarSeries, QStackedBarSeries, QPercentBarSeries
18 \sa QBarSeries, QStackedBarSeries, QPercentBarSeries
19 */
19 */
20
20
21 /*!
21 /*!
22 \fn void QBarSet::clicked(QString category)
22 \fn void QBarSet::clicked(QString category)
23 \brief signals that set has been clicked
23 \brief signals that set has been clicked
24 Parameter \a category describes on which category was clicked
24 Parameter \a category describes on which category was clicked
25 */
25 */
26
26
27 /*!
27 /*!
28 \fn void QBarSet::rightClicked(QString category)
28 \fn void QBarSet::rightClicked(QString category)
29 \brief signals that set has been clicked with right mouse button
29 \brief signals that set has been clicked with right mouse button
30 Parameter \a category describes on which category was clicked
30 Parameter \a category describes on which category was clicked
31 */
31 */
32
32
33 /*!
33 /*!
34 \fn void QBarSet::hoverEnter(QPoint pos)
34 \fn void QBarSet::hoverEnter(QPoint pos)
35 \brief signals that mouse has entered over the set at position \a pos.
35 \brief signals that mouse has entered over the set at position \a pos.
36 */
36 */
37
37
38 /*!
38 /*!
39 \fn void QBarSet::hoverLeave()
39 \fn void QBarSet::hoverLeave()
40 \brief signals that mouse has left from the set.
40 \brief signals that mouse has left from the set.
41 */
41 */
42
42
43 /*!
43 /*!
44 \fn void QBarSet::toggleFloatingValues()
44 \fn void QBarSet::toggleFloatingValues()
45 \brief \internal
45 \brief \internal
46 */
46 */
47
47
48 /*!
48 /*!
49 \fn void QBarSet::showToolTip(QPoint pos, QString tip)
49 \fn void QBarSet::showToolTip(QPoint pos, QString tip)
50 \brief \internal \a pos \a tip
50 \brief \internal \a pos \a tip
51 */
51 */
52
52
53
53
54 /*!
54 /*!
55 Constructs QBarSet with a name of \a name and with parent of \a parent
55 Constructs QBarSet with a name of \a name and with parent of \a parent
56 */
56 */
57 QBarSet::QBarSet(QString name, QObject *parent)
57 QBarSet::QBarSet(QString name, QObject *parent)
58 : QObject(parent)
58 : QObject(parent)
59 ,mName(name)
59 ,mName(name)
60 {
60 {
61 }
61 }
62
62
63 /*!
63 /*!
64 Sets new \a name for set.
64 Sets new \a name for set.
65 */
65 */
66 void QBarSet::setName(QString name)
66 void QBarSet::setName(QString name)
67 {
67 {
68 mName = name;
68 mName = name;
69 }
69 }
70
70
71 /*!
71 /*!
72 Returns name of the set.
72 Returns name of the set.
73 */
73 */
74 QString QBarSet::name()
74 QString QBarSet::name()
75 {
75 {
76 return mName;
76 return mName;
77 }
77 }
78
78
79 /*!
79 /*!
80 Appends new value \a value to the end of set.
80 Appends new value \a value to the end of set.
81 */
81 */
82 QBarSet& QBarSet::operator << (const qreal &value)
82 QBarSet& QBarSet::operator << (const qreal &value)
83 {
83 {
84 mValues.append(value);
84 mValues.append(value);
85 return *this;
85 return *this;
86 }
86 }
87
87
88 /*!
88 /*!
89 Returns count of values in set.
89 Returns count of values in set.
90 */
90 */
91 int QBarSet::count()
91 int QBarSet::count()
92 {
92 {
93 return mValues.count();
93 return mValues.count();
94 }
94 }
95
95
96 /*!
96 /*!
97 Returns value of set indexed by \a index
97 Returns value of set indexed by \a index
98 */
98 */
99 qreal QBarSet::valueAt(int index)
99 qreal QBarSet::valueAt(int index)
100 {
100 {
101 return mValues.at(index);
101 return mValues.at(index);
102 }
102 }
103
103
104 /*!
104 /*!
105 Sets a new value \a value to set, indexed by \a index
105 Sets a new value \a value to set, indexed by \a index
106 */
106 */
107 void QBarSet::setValue(int index, qreal value)
107 void QBarSet::setValue(int index, qreal value)
108 {
108 {
109 mValues.replace(index,value);
109 mValues.replace(index,value);
110 }
110 }
111
111
112 qreal QBarSet::total()
112 qreal QBarSet::total()
113 {
113 {
114 qreal total(0);
114 qreal total(0);
115 for (int i=0; i<mValues.count(); i++) {
115 for (int i=0; i<mValues.count(); i++) {
116 total += mValues.at(i);
116 total += mValues.at(i);
117 }
117 }
118 return total;
118 return total;
119 }
119 }
120
120
121
121
122 /*!
122 /*!
123 Sets pen for set. Bars of this set are drawn using \a pen
123 Sets pen for set. Bars of this set are drawn using \a pen
124 */
124 */
125 void QBarSet::setPen(QPen pen)
125 void QBarSet::setPen(const QPen pen)
126 {
126 {
127 mPen = pen;
127 mPen = pen;
128 }
128 }
129
129
130 /*!
130 /*!
131 Returns pen of the set.
131 Returns pen of the set.
132 */
132 */
133 QPen QBarSet::pen()
133 QPen QBarSet::pen() const
134 {
134 {
135 return mPen;
135 return mPen;
136 }
136 }
137
137
138 /*!
138 /*!
139 Sets brush for the set. Bars of this set are drawn using \a brush
139 Sets brush for the set. Bars of this set are drawn using \a brush
140 */
140 */
141 void QBarSet::setBrush(QBrush brush)
141 void QBarSet::setBrush(const QBrush brush)
142 {
142 {
143 mBrush = brush;
143 mBrush = brush;
144 }
144 }
145
145
146 /*!
146 /*!
147 Returns brush of the set.
147 Returns brush of the set.
148 */
148 */
149 QBrush QBarSet::brush()
149 QBrush QBarSet::brush() const
150 {
150 {
151 return mBrush;
151 return mBrush;
152 }
152 }
153
153
154 /*!
154 /*!
155 \internal \a pos
155 \internal \a pos
156 */
156 */
157 void QBarSet::barHoverEnterEvent(QPoint pos)
157 void QBarSet::barHoverEnterEvent(QPoint pos)
158 {
158 {
159 emit showToolTip(pos, mName);
159 emit showToolTip(pos, mName);
160 emit hoverEnter(pos);
160 emit hoverEnter(pos);
161 }
161 }
162
162
163 /*!
163 /*!
164 \internal
164 \internal
165 */
165 */
166 void QBarSet::barHoverLeaveEvent()
166 void QBarSet::barHoverLeaveEvent()
167 {
167 {
168 // Emit signal to user of charts
168 // Emit signal to user of charts
169 emit hoverLeave();
169 emit hoverLeave();
170 }
170 }
171
171
172 #include "moc_qbarset.cpp"
172 #include "moc_qbarset.cpp"
173 QTCOMMERCIALCHART_END_NAMESPACE
173 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,67 +1,67
1 #ifndef QBARSET_H
1 #ifndef QBARSET_H
2 #define QBARSET_H
2 #define QBARSET_H
3
3
4 #include <qchartglobal.h>
4 #include <qchartglobal.h>
5 #include <QPen>
5 #include <QPen>
6 #include <QBrush>
6 #include <QBrush>
7
7
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9
9
10 class QTCOMMERCIALCHART_EXPORT QBarSet : public QObject
10 class QTCOMMERCIALCHART_EXPORT QBarSet : public QObject
11 {
11 {
12 Q_OBJECT
12 Q_OBJECT
13 public:
13 public:
14 QBarSet(QString name, QObject *parent = 0);
14 QBarSet(QString name, QObject *parent = 0);
15
15
16 void setName(QString name);
16 void setName(QString name);
17 QString name();
17 QString name();
18 QBarSet& operator << (const qreal &value); // appends new value to set
18 QBarSet& operator << (const qreal &value); // appends new value to set
19
19
20 // TODO: remove indices eventually. Use as internal?
20 // TODO: remove indices eventually. Use as internal?
21 int count(); // count of values in set
21 int count(); // count of values in set
22 qreal valueAt(int index); // for modifying individual values
22 qreal valueAt(int index); // for modifying individual values
23 void setValue(int index, qreal value); // setter for individual value
23 void setValue(int index, qreal value); // setter for individual value
24 qreal total(); // total values in the set
24 qreal total(); // total values in the set
25
25
26 // TODO:
26 // TODO:
27 //qreal value(QString category);
27 //qreal value(QString category);
28 //void setValue(QString category, qreal value);
28 //void setValue(QString category, qreal value);
29
29
30 void setPen(QPen pen);
30 void setPen(const QPen pen);
31 QPen pen();
31 QPen pen() const;
32
32
33 void setBrush(QBrush brush);
33 void setBrush(const QBrush brush);
34 QBrush brush();
34 QBrush brush() const;
35
35
36 Q_SIGNALS:
36 Q_SIGNALS:
37 void clicked(QString category); // Clicked and hover signals exposed to user
37 void clicked(QString category); // Clicked and hover signals exposed to user
38 void rightClicked(QString category);
38 void rightClicked(QString category);
39 void toggleFloatingValues();
39 void toggleFloatingValues();
40
40
41 // TODO: Expose this to user or not?
41 // TODO: Expose this to user or not?
42 // TODO: TO PIMPL --->
42 // TODO: TO PIMPL --->
43 void hoverEnter(QPoint pos);
43 void hoverEnter(QPoint pos);
44 void hoverLeave();
44 void hoverLeave();
45 void showToolTip(QPoint pos, QString tip); // Private signal
45 void showToolTip(QPoint pos, QString tip); // Private signal
46 // <--- TO PIMPL
46 // <--- TO PIMPL
47
47
48 public Q_SLOTS:
48 public Q_SLOTS:
49 // These are for internal communication
49 // These are for internal communication
50 // TODO: TO PIMPL --->
50 // TODO: TO PIMPL --->
51 void barHoverEnterEvent(QPoint pos);
51 void barHoverEnterEvent(QPoint pos);
52 void barHoverLeaveEvent();
52 void barHoverLeaveEvent();
53 // <--- TO PIMPL
53 // <--- TO PIMPL
54
54
55 private:
55 private:
56
56
57 QString mName;
57 QString mName;
58 QList<qreal> mValues; // TODO: replace with map (category, value)
58 QList<qreal> mValues; // TODO: replace with map (category, value)
59 QMap<QString,qreal> mMappedValues;
59 QMap<QString,qreal> mMappedValues;
60 QPen mPen;
60 QPen mPen;
61 QBrush mBrush;
61 QBrush mBrush;
62
62
63 };
63 };
64
64
65 QTCOMMERCIALCHART_END_NAMESPACE
65 QTCOMMERCIALCHART_END_NAMESPACE
66
66
67 #endif // QBARSET_H
67 #endif // QBARSET_H
@@ -1,116 +1,113
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, QGraphicsItem *parent) :
11 StackedBarPresenter::StackedBarPresenter(QBarSeries *series, QGraphicsItem *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 // TODO: better way to auto-layout
42 // Use temporary qreals for accurancy (we might get some compiler warnings... :)
43 // Use reals for accurancy (we might get some compiler warnings... :)
44 // TODO: use temp variable for category count...
45 qreal maxSum = mSeries->maxCategorySum();
43 qreal maxSum = mSeries->maxCategorySum();
46 qreal h = mHeight;
44 qreal h = mHeight;
47 qreal scale = (h / maxSum);
45 qreal scale = (h / maxSum);
48
49 int itemIndex(0);
50 int labelIndex(0);
51 qreal tW = mWidth;
46 qreal tW = mWidth;
52 qreal tC = mSeries->categoryCount() + 1;
47 qreal tC = mSeries->categoryCount() + 1;
48 qreal cC = mSeries->categoryCount() * 2 + 1;
49 mBarWidth = tW / cC;
53 qreal xStep = (tW/tC);
50 qreal xStep = (tW/tC);
54 qreal xPos = ((tW/tC) - mBarDefaultWidth / 2);
51 qreal xPos = ((tW/tC) - mBarWidth / 2);
55
52
53 int itemIndex(0);
54 int labelIndex(0);
56 for (int category = 0; category < mSeries->categoryCount(); category++) {
55 for (int category = 0; category < mSeries->categoryCount(); category++) {
57 qreal yPos = h;
56 qreal yPos = h;
58 for (int set=0; set < mSeries->barsetCount(); set++) {
57 for (int set=0; set < mSeries->barsetCount(); set++) {
59 qreal barHeight = mSeries->valueAt(set, category) * scale;
58 qreal barHeight = mSeries->valueAt(set, category) * scale;
60 Bar* bar = mBars.at(itemIndex);
59 Bar* bar = mBars.at(itemIndex);
61
60
62 bar->resize(mBarDefaultWidth, barHeight);
61 bar->resize(mBarWidth, barHeight);
63 bar->setBrush(mSeries->barsetAt(set)->brush());
62 bar->setBrush(mSeries->barsetAt(set)->brush());
64 bar->setPos(xPos, yPos-barHeight);
63 bar->setPos(xPos, yPos-barHeight);
65 itemIndex++;
64 itemIndex++;
66 yPos -= barHeight;
65 yPos -= barHeight;
67 }
66 }
68
67
69 // TODO: Layout for labels, remove magic number
68 // TODO: Layout for labels, remove magic number
70 BarLabel* label = mLabels.at(labelIndex);
69 BarLabel* label = mLabels.at(labelIndex);
71 label->setPos(xPos, mHeight + 20);
70 label->setPos(xPos, mHeight + 20);
72 labelIndex++;
71 labelIndex++;
73 xPos += xStep;
72 xPos += xStep;
74 }
73 }
75
74
76 // Position separators
75 // Position separators
77 xPos = xStep + xStep/2;
76 xPos = xStep + xStep/2;
78 for (int s=0; s < mSeries->categoryCount() - 1; s++) {
77 for (int s=0; s < mSeries->categoryCount() - 1; s++) {
79 Separator* sep = mSeparators.at(s);
78 Separator* sep = mSeparators.at(s);
80 sep->setPos(xPos,0);
79 sep->setPos(xPos,0);
81 sep->setSize(QSizeF(1,mHeight));
80 sep->setSize(QSizeF(1,mHeight));
82 xPos += xStep;
81 xPos += xStep;
83 }
82 }
84
83
85 // Position floating values
84 // Position floating values
86 itemIndex = 0;
85 itemIndex = 0;
87 xPos = ((tW/tC) - mBarDefaultWidth / 2);
86 xPos = ((tW/tC) - mBarWidth / 2);
88 for (int category=0; category < mSeries->categoryCount(); category++) {
87 for (int category=0; category < mSeries->categoryCount(); category++) {
89 qreal yPos = h;
88 qreal yPos = h;
90 for (int set=0; set < mSeries->barsetCount(); set++) {
89 for (int set=0; set < mSeries->barsetCount(); set++) {
91 qreal barHeight = mSeries->valueAt(set,category) * scale;
90 qreal barHeight = mSeries->valueAt(set,category) * scale;
92 BarValue* value = mFloatingValues.at(itemIndex);
91 BarValue* value = mFloatingValues.at(itemIndex);
93
92
94 // TODO: remove hard coding, apply layout
93 // TODO: remove hard coding, apply layout
95 value->resize(100,50);
94 value->resize(100,50);
96 value->setPos(xPos, yPos-barHeight/2);
95 value->setPos(xPos, yPos-barHeight/2);
97 value->setPen(QPen(QColor(255,255,255,255)));
96 value->setPen(QPen(QColor(255,255,255,255)));
98
97
99 if (mSeries->valueAt(set,category) != 0) {
98 if (mSeries->valueAt(set,category) != 0) {
100 value->setValueString(QString::number(mSeries->valueAt(set,category)));
99 value->setValueString(QString::number(mSeries->valueAt(set,category)));
101 } else {
100 } else {
102 value->setValueString(QString(""));
101 value->setValueString(QString(""));
103 }
102 }
104
103
105 itemIndex++;
104 itemIndex++;
106 yPos -= barHeight;
105 yPos -= barHeight;
107 }
106 }
108 xPos += xStep;
107 xPos += xStep;
109 }
108 }
110
111 mLayoutDirty = true;
112 }
109 }
113
110
114 #include "moc_stackedbarpresenter_p.cpp"
111 #include "moc_stackedbarpresenter_p.cpp"
115
112
116 QTCOMMERCIALCHART_END_NAMESPACE
113 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now