##// END OF EJS Templates
moved tooltip to presenter
sauimone -
r288:5f68000bea7a
parent child
Show More
@@ -7,8 +7,8
7
7
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9
9
10 BarPresenter::BarPresenter(BarChartModel& model, QGraphicsItem *parent) :
10 BarPresenter::BarPresenter(QBarChartSeries *series, QGraphicsItem *parent) :
11 BarPresenterBase(model,parent)
11 BarPresenterBase(series, parent)
12 {
12 {
13 mBarDefaultWidth = 15;
13 mBarDefaultWidth = 15;
14 }
14 }
@@ -17,7 +17,7 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 (mModel.countSets() <= 0) {
20 if (mSeries->countSets() <= 0) {
21 qDebug() << "No sets in model!";
21 qDebug() << "No sets in model!";
22 return;
22 return;
23 }
23 }
@@ -29,12 +29,12 void BarPresenter::layoutChanged()
29
29
30 // TODO: better way to auto-layout?
30 // TODO: better way to auto-layout?
31 // Use reals for accurancy (we might get some compiler warnings... :)
31 // Use reals for accurancy (we might get some compiler warnings... :)
32 int categoryCount = mModel.countCategories();
32 int categoryCount = mSeries->countCategories();
33 int setCount = mModel.countSets();
33 int setCount = mSeries->countSets();
34
34
35 qreal tW = mWidth;
35 qreal tW = mWidth;
36 qreal tH = mHeight;
36 qreal tH = mHeight;
37 qreal tM = mModel.max();
37 qreal tM = mSeries->max();
38 qreal scale = (tH/tM);
38 qreal scale = (tH/tM);
39 qreal tC = categoryCount+1;
39 qreal tC = categoryCount+1;
40 qreal xStepPerSet = (tW/tC);
40 qreal xStepPerSet = (tW/tC);
@@ -47,13 +47,13 void BarPresenter::layoutChanged()
47 qreal xPos = xStepPerSet * category + ((tW + mBarDefaultWidth*setCount)/(categoryCount*2));
47 qreal xPos = xStepPerSet * category + ((tW + mBarDefaultWidth*setCount)/(categoryCount*2));
48 qreal yPos = mHeight;
48 qreal yPos = mHeight;
49 for (int set = 0; set < setCount; set++) {
49 for (int set = 0; set < setCount; set++) {
50 qreal barHeight = mModel.valueAt(set, category) * scale;
50 qreal barHeight = mSeries->valueAt(set,category) * scale;
51 Bar* bar = mBars.at(itemIndex);
51 Bar* bar = mBars.at(itemIndex);
52
52
53 // TODO: width settable per bar?
53 // TODO: width settable per bar?
54 bar->resize(mBarDefaultWidth, barHeight);
54 bar->resize(mBarDefaultWidth, barHeight);
55 bar->setBrush(mModel.setAt(set)->brush());
55 bar->setBrush(mSeries->setAt(set)->brush());
56 bar->setPos(xPos, yPos-barHeight); // item*posStep+startPos + set * mBarDefaultWidth, mHeight);
56 bar->setPos(xPos, yPos-barHeight);
57 itemIndex++;
57 itemIndex++;
58 xPos += mBarDefaultWidth;
58 xPos += mBarDefaultWidth;
59 }
59 }
@@ -67,11 +67,11 void BarPresenter::layoutChanged()
67
67
68 // Position floating values
68 // Position floating values
69 itemIndex = 0;
69 itemIndex = 0;
70 for (int category=0; category < mModel.countCategories(); category++) {
70 for (int category=0; category < mSeries->countCategories(); category++) {
71 qreal xPos = xStepPerSet * category + ((tW + mBarDefaultWidth*setCount)/(categoryCount*2));
71 qreal xPos = xStepPerSet * category + ((tW + mBarDefaultWidth*setCount)/(categoryCount*2));
72 qreal yPos = mHeight;
72 qreal yPos = mHeight;
73 for (int set=0; set < mModel.countSets(); set++) {
73 for (int set=0; set < mSeries->countSets(); set++) {
74 qreal barHeight = mModel.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 // TODO: remove hard coding, apply layout
77 // TODO: remove hard coding, apply layout
@@ -79,8 +79,8 void BarPresenter::layoutChanged()
79 value->setPos(xPos, yPos-barHeight/2);
79 value->setPos(xPos, yPos-barHeight/2);
80 value->setPen(QPen(QColor(255,255,255,255)));
80 value->setPen(QPen(QColor(255,255,255,255)));
81
81
82 if (mModel.valueAt(set,category) != 0) {
82 if (mSeries->valueAt(set,category) != 0) {
83 value->setValueString(QString::number(mModel.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 }
@@ -92,4 +92,6 void BarPresenter::layoutChanged()
92 mLayoutDirty = true;
92 mLayoutDirty = true;
93 }
93 }
94
94
95 #include "moc_barpresenter.cpp"
96
95 QTCOMMERCIALCHART_END_NAMESPACE
97 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,17 +1,20
1 #ifndef BARPRESENTER_H
1 #ifndef BARPRESENTER_H
2 #define BARPRESENTER_H
2 #define BARPRESENTER_H
3
3
4 #include "qchartglobal.h"
4 #include "barpresenterbase.h"
5 #include "barpresenterbase.h"
5 #include "qbarchartseries.h"
6 #include <QGraphicsItem>
6 #include <QGraphicsItem>
7
7
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9
9
10 class QBarChartSeries;
11
10 // Presenter for parallel bars. Grouping of bars is done on category basis.
12 // Presenter for parallel bars. Grouping of bars is done on category basis.
11 class BarPresenter : public BarPresenterBase
13 class BarPresenter : public BarPresenterBase
12 {
14 {
15 Q_OBJECT
13 public:
16 public:
14 explicit BarPresenter(BarChartModel& model, QGraphicsItem *parent = 0);
17 explicit BarPresenter(QBarChartSeries *series, QGraphicsItem *parent = 0);
15
18
16 private:
19 private:
17
20
@@ -4,24 +4,33
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 "qbarchartseries.h"
7 #include <QDebug>
8 #include <QDebug>
9 #include <QToolTip>
8
10
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
11 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10
12
11 BarPresenterBase::BarPresenterBase(BarChartModel& model, QGraphicsItem *parent)
13 BarPresenterBase::BarPresenterBase(QBarChartSeries *series, QGraphicsItem *parent)
12 : ChartItem(parent)
14 : ChartItem(parent)
13 ,mBarDefaultWidth(20) // TODO: remove hard coding, when we have layout code ready
15 ,mBarDefaultWidth(20) // TODO: remove hard coding, when we have layout code ready
14 ,mLayoutSet(false)
16 ,mLayoutSet(false)
15 ,mLayoutDirty(true)
17 ,mLayoutDirty(true)
16 ,mSeparatorsVisible(false)
18 ,mSeries(series)
17 ,mModel(model)
18 {
19 {
20 connect(series,SIGNAL(floatingValuesEnabled(bool)),this,SLOT(enableFloatingValues(bool)));
21 connect(series,SIGNAL(toolTipEnabled(bool)),this,SLOT(enableToolTip(bool)));
22 connect(series,SIGNAL(separatorsEnabled(bool)),this,SLOT(enableSeparators(bool)));
23 connect(series,SIGNAL(showToolTip(QPoint,QString)),this,SLOT(showToolTip(QPoint,QString)));
19 dataChanged();
24 dataChanged();
20 }
25 }
21
26
22 void BarPresenterBase::setSeparatorsVisible(bool visible)
27 BarPresenterBase::~BarPresenterBase()
23 {
28 {
24 mSeparatorsVisible = visible;
29 disconnect(this,SLOT(enableFloatingValues(bool)));
30 disconnect(this,SLOT(enableToolTip(bool)));
31 disconnect(this,SLOT(enableSeparators(bool)));
32 disconnect(this,SLOT(showToolTip(QPoint,QString)));
33 delete mSeries;
25 }
34 }
26
35
27 void BarPresenterBase::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
36 void BarPresenterBase::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
@@ -63,9 +72,9 void BarPresenterBase::dataChanged()
63 mFloatingValues.clear();
72 mFloatingValues.clear();
64
73
65 // Create new graphic items for bars
74 // Create new graphic items for bars
66 for (int c=0; c<mModel.countCategories(); c++) {
75 for (int c=0; c<mSeries->countCategories(); c++) {
67 for (int s=0; s<mModel.countSets(); s++) {
76 for (int s=0; s<mSeries->countSets(); s++) {
68 QBarSet *set = mModel.setAt(s);
77 QBarSet *set = mSeries->setAt(s);
69 Bar *bar = new Bar(this);
78 Bar *bar = new Bar(this);
70 childItems().append(bar);
79 childItems().append(bar);
71 mBars.append(bar);
80 mBars.append(bar);
@@ -77,16 +86,16 void BarPresenterBase::dataChanged()
77 }
86 }
78
87
79 // Create labels
88 // Create labels
80 int count = mModel.countCategories();
89 int count = mSeries->countCategories();
81 for (int i=0; i<count; i++) {
90 for (int i=0; i<count; i++) {
82 BarLabel* label = new BarLabel(this);
91 BarLabel* label = new BarLabel(this);
83 label->set(mModel.label(i));
92 label->set(mSeries->label(i));
84 childItems().append(label);
93 childItems().append(label);
85 mLabels.append(label);
94 mLabels.append(label);
86 }
95 }
87
96
88 // Create separators
97 // Create separators
89 count = mModel.countCategories() - 1; // There is one less separator than columns
98 count = mSeries->countCategories() - 1; // There is one less separator than columns
90 for (int i=0; i<count; i++) {
99 for (int i=0; i<count; i++) {
91 Separator* sep = new Separator(this);
100 Separator* sep = new Separator(this);
92 sep->setColor(QColor(255,0,0,255)); // TODO: color for separations from theme
101 sep->setColor(QColor(255,0,0,255)); // TODO: color for separations from theme
@@ -95,9 +104,9 void BarPresenterBase::dataChanged()
95 }
104 }
96
105
97 // Create floating values
106 // Create floating values
98 for (int category=0; category<mModel.countCategories(); category++) {
107 for (int category=0; category<mSeries->countCategories(); category++) {
99 for (int s=0; s<mModel.countSets(); s++) {
108 for (int s=0; s<mSeries->countSets(); s++) {
100 QBarSet *set = mModel.setAt(s);
109 QBarSet *set = mSeries->setAt(s);
101 BarValue *value = new BarValue(*set, this);
110 BarValue *value = new BarValue(*set, this);
102 childItems().append(value);
111 childItems().append(value);
103 mFloatingValues.append(value);
112 mFloatingValues.append(value);
@@ -134,15 +143,27 void BarPresenterBase::handleGeometryChanged(const QRectF& rect)
134 setPos(rect.topLeft());
143 setPos(rect.topLeft());
135 }
144 }
136
145
146 void BarPresenterBase::enableFloatingValues(bool enabled)
147 {
148 mFloatingValuesEnabled = enabled;
149 }
150
151 void BarPresenterBase::enableToolTip(bool enabled)
152 {
153 mToolTipEnabled = enabled;
154 }
137
155
138 void BarPresenterBase::barHoverEntered(QGraphicsSceneHoverEvent *event)
156 void BarPresenterBase::enableSeparators(bool enabled)
139 {
157 {
140 //TODO: show tooltip (name of series, where bar belongs...)
158 mSeparatorsEnabled = enabled;
141 }
159 }
142
160
143 void BarPresenterBase::barHoverLeaved(QGraphicsSceneHoverEvent *event)
161 void BarPresenterBase::showToolTip(QPoint pos, QString tip)
144 {
162 {
145 //TODO: hide tooltip (name of series, where bar belongs...)
163 if (mToolTipEnabled) {
164 // TODO: cool tooltip instead of default
165 QToolTip::showText(pos,tip);
166 }
146 }
167 }
147
168
148 #include "moc_barpresenterbase.cpp"
169 #include "moc_barpresenterbase.cpp"
@@ -2,7 +2,7
2 #define BARPRESENTERBASE_H
2 #define BARPRESENTERBASE_H
3
3
4 #include "chartitem_p.h"
4 #include "chartitem_p.h"
5 #include "barchartmodel_p.h"
5 #include "qbarchartseries.h"
6 #include <QPen>
6 #include <QPen>
7 #include <QBrush>
7 #include <QBrush>
8 #include <QGraphicsItem>
8 #include <QGraphicsItem>
@@ -20,11 +20,10 class BarPresenterBase : public QObject, public ChartItem
20 {
20 {
21 Q_OBJECT
21 Q_OBJECT
22 public:
22 public:
23 BarPresenterBase(BarChartModel& model, QGraphicsItem *parent = 0);
23 BarPresenterBase(QBarChartSeries *series, QGraphicsItem *parent = 0);
24 void setSeparatorsVisible(bool visible = true);
24 ~BarPresenterBase();
25
25
26 public:
26 public:
27
28 // From QGraphicsItem
27 // From QGraphicsItem
29 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
28 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
30 QRectF boundingRect() const;
29 QRectF boundingRect() const;
@@ -41,8 +40,11 protected slots:
41 void handleDomainChanged(const Domain& domain);
40 void handleDomainChanged(const Domain& domain);
42 void handleGeometryChanged(const QRectF& size);
41 void handleGeometryChanged(const QRectF& size);
43
42
44 void barHoverEntered(QGraphicsSceneHoverEvent *event); // Internal.
43 // Internal slots
45 void barHoverLeaved(QGraphicsSceneHoverEvent *event);
44 void enableFloatingValues(bool enabled=true); // enables floating values on top of bars
45 void enableToolTip(bool enabled=true); // enables tooltips
46 void enableSeparators(bool enabled=true); // enables separators between categories
47 void showToolTip(QPoint pos, QString tip); // shows tooltip (if enabled)
46
48
47 protected:
49 protected:
48
50
@@ -54,14 +56,19 protected:
54 bool mLayoutSet; // True, if component has been laid out.
56 bool mLayoutSet; // True, if component has been laid out.
55 bool mLayoutDirty;
57 bool mLayoutDirty;
56
58
57 bool mSeparatorsVisible;
59 bool mFloatingValuesEnabled;
58 BarChartModel& mModel;
60 bool mToolTipEnabled;
61 bool mSeparatorsEnabled;
62
63 // Owned
64 QBarChartSeries* mSeries;
59
65
60 // Not owned.
66 // Not owned.
61 QList<Bar*> mBars;
67 QList<Bar*> mBars;
62 QList<BarLabel*> mLabels;
68 QList<BarLabel*> mLabels;
63 QList<Separator*> mSeparators;
69 QList<Separator*> mSeparators;
64 QList<BarValue*> mFloatingValues;
70 QList<BarValue*> mFloatingValues;
71
65 };
72 };
66
73
67 QTCOMMERCIALCHART_END_NAMESPACE
74 QTCOMMERCIALCHART_END_NAMESPACE
@@ -9,8 +9,8
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10
10
11
11
12 PercentBarPresenter::PercentBarPresenter(BarChartModel& model, QGraphicsItem *parent) :
12 PercentBarPresenter::PercentBarPresenter(QBarChartSeries *series, QGraphicsItem *parent) :
13 BarPresenterBase(model, parent)
13 BarPresenterBase(series, parent)
14 {
14 {
15 }
15 }
16
16
@@ -18,7 +18,7 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 (mModel.countSets() <= 0) {
21 if (mSeries->countSets() <= 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;
@@ -31,7 +31,7 void PercentBarPresenter::layoutChanged()
31
31
32 // TODO: better way to auto-layout
32 // TODO: better way to auto-layout
33 // Use reals for accurancy (we might get some compiler warnings... :)
33 // Use reals for accurancy (we might get some compiler warnings... :)
34 int count = mModel.countCategories();
34 int count = mSeries->countCategories();
35 int itemIndex(0);
35 int itemIndex(0);
36 int labelIndex(0);
36 int labelIndex(0);
37 qreal tW = mWidth;
37 qreal tW = mWidth;
@@ -40,17 +40,17 void PercentBarPresenter::layoutChanged()
40 qreal xPos = ((tW/tC) - mBarDefaultWidth / 2);
40 qreal xPos = ((tW/tC) - mBarDefaultWidth / 2);
41 qreal h = mHeight;
41 qreal h = mHeight;
42
42
43 for (int category = 0; category < mModel.countCategories(); category++) {
43 for (int category = 0; category < mSeries->countCategories(); category++) {
44 qreal colSum = mModel.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 < mModel.countSets(); set++) {
47 for (int set=0; set < mSeries->countSets(); set++) {
48 qreal barHeight = mModel.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(mBarDefaultWidth, barHeight);
53 bar->setBrush(mModel.setAt(set)->brush());
53 bar->setBrush(mSeries->setAt(set)->brush());
54 bar->setPos(xPos, yPos-barHeight);
54 bar->setPos(xPos, yPos-barHeight);
55 itemIndex++;
55 itemIndex++;
56 yPos -= barHeight;
56 yPos -= barHeight;
@@ -65,7 +65,7 void PercentBarPresenter::layoutChanged()
65
65
66 // Position separators
66 // Position separators
67 xPos = xStep + xStep/2;
67 xPos = xStep + xStep/2;
68 for (int s=0; s < mModel.countCategories() - 1; s++) {
68 for (int s=0; s < mSeries->countCategories() - 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));
@@ -75,12 +75,12 void PercentBarPresenter::layoutChanged()
75 // Position floating values
75 // Position floating values
76 itemIndex = 0;
76 itemIndex = 0;
77 xPos = ((tW/tC) - mBarDefaultWidth / 2);
77 xPos = ((tW/tC) - mBarDefaultWidth / 2);
78 for (int category=0; category < mModel.countCategories(); category++) {
78 for (int category=0; category < mSeries->countCategories(); category++) {
79 qreal yPos = h;
79 qreal yPos = h;
80 qreal colSum = mModel.categorySum(category);
80 qreal colSum = mSeries->categorySum(category);
81 qreal scale = (h / colSum);
81 qreal scale = (h / colSum);
82 for (int set=0; set < mModel.countSets(); set++) {
82 for (int set=0; set < mSeries->countSets(); set++) {
83 qreal barHeight = mModel.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
@@ -88,8 +88,8 void PercentBarPresenter::layoutChanged()
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 (mModel.valueAt(set,category) != 0) {
91 if (mSeries->valueAt(set,category) != 0) {
92 int p = mModel.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("%");
@@ -107,4 +107,6 void PercentBarPresenter::layoutChanged()
107 mLayoutDirty = true;
107 mLayoutDirty = true;
108 }
108 }
109
109
110 #include "moc_percentbarpresenter.cpp"
111
110 QTCOMMERCIALCHART_END_NAMESPACE
112 QTCOMMERCIALCHART_END_NAMESPACE
@@ -11,8 +11,9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
11
11
12 class PercentBarPresenter : public BarPresenterBase
12 class PercentBarPresenter : public BarPresenterBase
13 {
13 {
14 Q_OBJECT
14 public:
15 public:
15 PercentBarPresenter(BarChartModel& model, QGraphicsItem *parent = 0);
16 PercentBarPresenter(QBarChartSeries *series, QGraphicsItem *parent = 0);
16
17
17 private:
18 private:
18
19
@@ -15,15 +15,18 QBarChartSeries::QBarChartSeries(QBarCategory *category, QObject *parent)
15
15
16 void QBarChartSeries::addBarSet(QBarSet *set)
16 void QBarChartSeries::addBarSet(QBarSet *set)
17 {
17 {
18 // connect(this,SIGNAL(floatingValuesEnabled(bool)),set,SLOT(enableFloatingValues(bool)));
18 connect(this,SIGNAL(floatingValuesEnabled(bool)),set,SLOT(enableFloatingValues(bool)));
19 // connect(this,SIGNAL(hoverNamesEnabled(bool)),set,SLOT(enableHoverNames(bool)));
19 connect(this,SIGNAL(separatorsEnabled(bool)),set,SLOT(enableSeparators(bool)));
20 connect(this,SIGNAL(toolTipEnabled(bool)),set,SLOT(enableToolTip(bool)));
21 connect(set,SIGNAL(showToolTip(QPoint,QString)),this,SIGNAL(showToolTip(QPoint,QString)));
20 mModel->addBarSet(set);
22 mModel->addBarSet(set);
21 }
23 }
22
24
23 void QBarChartSeries::removeBarSet(QBarSet *set)
25 void QBarChartSeries::removeBarSet(QBarSet *set)
24 {
26 {
25 // disconnect(set,SLOT(enableFloatingValues(bool)));
27 disconnect(set,SLOT(enableFloatingValues(bool)));
26 // disconnect(set,SLOT(enableHoverNames(bool)));
28 disconnect(set,SLOT(enableSeparators(bool)));
29 disconnect(set,SLOT(enableToolTip(bool)));
27 mModel->removeBarSet(set);
30 mModel->removeBarSet(set);
28 }
31 }
29
32
@@ -37,19 +40,34 QBarSet* QBarChartSeries::nextSet(bool getFirst)
37 return mModel->nextSet(getFirst);
40 return mModel->nextSet(getFirst);
38 }
41 }
39
42
43 QBarSet* QBarChartSeries::setAt(int index)
44 {
45 return mModel->setAt(index);
46 }
47
40 QList<QString> QBarChartSeries::legend()
48 QList<QString> QBarChartSeries::legend()
41 {
49 {
42 return mModel->legend();
50 return mModel->legend();
43 }
51 }
44
52
53 QString QBarChartSeries::label(int category)
54 {
55 return mModel->label(category);
56 }
57
45 void QBarChartSeries::enableFloatingValues(bool enabled)
58 void QBarChartSeries::enableFloatingValues(bool enabled)
46 {
59 {
47 emit floatingValuesEnabled(enabled);
60 emit floatingValuesEnabled(enabled);
48 }
61 }
49
62
50 void QBarChartSeries::enableHoverNames(bool enabled)
63 void QBarChartSeries::enableToolTip(bool enabled)
51 {
64 {
52 emit hoverNamesEnabled(enabled);
65 emit toolTipEnabled(enabled);
66 }
67
68 void QBarChartSeries::enableSeparators(bool enabled)
69 {
70 emit separatorsEnabled(enabled);
53 }
71 }
54
72
55 int QBarChartSeries::countCategories()
73 int QBarChartSeries::countCategories()
@@ -72,6 +90,16 qreal QBarChartSeries::valueAt(int set, int category)
72 return mModel->valueAt(set,category);
90 return mModel->valueAt(set,category);
73 }
91 }
74
92
93 qreal QBarChartSeries::percentageAt(int set, int category)
94 {
95 return mModel->percentageAt(set,category);
96 }
97
98 qreal QBarChartSeries::categorySum(int category)
99 {
100 return mModel->categorySum(category);
101 }
102
75 qreal QBarChartSeries::maxCategorySum()
103 qreal QBarChartSeries::maxCategorySum()
76 {
104 {
77 return mModel->maxCategorySum();
105 return mModel->maxCategorySum();
@@ -22,19 +22,27 public:
22 void removeBarSet(QBarSet *set); // Releases ownership, doesn't delete set
22 void removeBarSet(QBarSet *set); // Releases ownership, doesn't delete set
23 int countSets();
23 int countSets();
24 QBarSet* nextSet(bool getFirst=false); // Returns first set, if called with true
24 QBarSet* nextSet(bool getFirst=false); // Returns first set, if called with true
25 QBarSet *setAt(int index);
25
26
26 QList<QString> legend(); // Returns legend of series (ie. names of all sets in series)
27 QList<QString> legend(); // Returns legend of series (ie. names of all sets in series)
28 QString label(int category);
27
29
30 public Q_SLOTS:
28 // Disabled by default. Call these to change behavior.
31 // Disabled by default. Call these to change behavior.
29 void enableFloatingValues(bool enabled=true);
32 void enableFloatingValues(bool enabled=true); // enables floating values on top of bars
30 void enableHoverNames(bool enabled=true);
33 void enableToolTip(bool enabled=true); // enables tooltips
34 void enableSeparators(bool enabled=true); // enables separators between categories
31
35
36 public:
32 // TODO: Functions below this are not part of api and will be moved
37 // TODO: Functions below this are not part of api and will be moved
33 // to private implementation, when we start using it (not part of api)
38 // to private implementation, when we start using it (not part of api)
34 int countCategories();
39 int countCategories();
35 qreal min();
40 qreal min();
36 qreal max();
41 qreal max();
37 qreal valueAt(int set, int category);
42 qreal valueAt(int set, int category);
43 qreal percentageAt(int set, int category);
44
45 qreal categorySum(int category);
38 qreal maxCategorySum();
46 qreal maxCategorySum();
39
47
40 BarChartModel& model();
48 BarChartModel& model();
@@ -42,12 +50,11 public:
42 signals:
50 signals:
43 void changed(int index);
51 void changed(int index);
44
52
45 // TODO: these to private implementation.
53 // TODO: internal signals, these to private implementation.
46 void floatingValuesEnabled(bool enabled);
54 void floatingValuesEnabled(bool enabled);
47 void hoverNamesEnabled(bool enabled);
55 void toolTipEnabled(bool enabled);
48
56 void separatorsEnabled(bool enabled);
49
57 void showToolTip(QPoint pos, QString tip);
50 //public Q_SLOTS:
51
58
52 protected:
59 protected:
53 BarChartModel* mModel;
60 BarChartModel* mModel;
@@ -7,7 +7,7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
7 QBarSet::QBarSet(QString name, QObject *parent)
7 QBarSet::QBarSet(QString name, QObject *parent)
8 : QObject(parent)
8 : QObject(parent)
9 ,mName(name)
9 ,mName(name)
10 ,mHoverNamesEnabled(true) // TODO: these 2 as false by default, when implementation is ready
10 ,mToolTipEnabled(true) // TODO: these 2 as false by default, when implementation is ready
11 ,mFloatingValuesEnabled(true)
11 ,mFloatingValuesEnabled(true)
12 {
12 {
13 }
13 }
@@ -64,19 +64,25 const QBrush& QBarSet::brush() const
64
64
65 void QBarSet::enableFloatingValues(bool enabled)
65 void QBarSet::enableFloatingValues(bool enabled)
66 {
66 {
67 qDebug() << "QBarSet::enableFloatingValues" << enabled;
67 qDebug() << "QBarSet::enableFloatingValues";
68 mFloatingValuesEnabled = enabled;
68 mFloatingValuesEnabled = enabled;
69 }
69 }
70
70
71 void QBarSet::enableToolTip(bool enabled)
71 void QBarSet::enableToolTip(bool enabled)
72 {
72 {
73 qDebug() << "QBarSet::enableHoverNames" << enabled;
73 qDebug() << "QBarSet::enableToolTip";
74 mHoverNamesEnabled = enabled;
74 mToolTipEnabled = enabled;
75 }
76
77 void QBarSet::enableSeparators(bool enabled)
78 {
79 qDebug() << "QBarSet::enableSeparators";
80 mSeparatorsEnabled = enabled;
75 }
81 }
76
82
77 void QBarSet::barClicked()
83 void QBarSet::barClicked()
78 {
84 {
79 qDebug() << "QBarset::barClicked" << this;
85 // qDebug() << "QBarset::barClicked" << this;
80 // Some bar of this set has been clicked
86 // Some bar of this set has been clicked
81 // TODO: What happens then?
87 // TODO: What happens then?
82 emit clicked(); // Notify that set has been clicked
88 emit clicked(); // Notify that set has been clicked
@@ -84,19 +90,21 void QBarSet::barClicked()
84
90
85 void QBarSet::barHoverEntered(QPoint pos)
91 void QBarSet::barHoverEntered(QPoint pos)
86 {
92 {
87 qDebug() << "QBarset::barHoverEntered" << this << pos;
93 if (mToolTipEnabled) {
88 if (mHoverNamesEnabled) {
94 emit showToolTip(pos, mName);
89 QToolTip::showText(pos, mName);
90 // emit hoverEnter();
91 }
95 }
96 // Emit signal to user of charts
97 emit hoverEnter(pos);
92 }
98 }
93
99
94 void QBarSet::barHoverLeaved()
100 void QBarSet::barHoverLeaved()
95 {
101 {
96 qDebug() << "QBarset::barHoverLeaved" << this;
102 // qDebug() << "QBarset::barHoverLeaved" << this;
97 if (mHoverNamesEnabled) {
103 // if (mToolTipEnabled) {
98 // emit hoverLeave();
104 // TODO: do what?
99 }
105 // }
106 // Emit signal to user of charts
107 emit hoverLeave();
100 }
108 }
101
109
102 #include "moc_qbarset.cpp"
110 #include "moc_qbarset.cpp"
@@ -29,14 +29,15 public:
29
29
30 Q_SIGNALS:
30 Q_SIGNALS:
31 void clicked(); // Clicked and hover signals exposed to user
31 void clicked(); // Clicked and hover signals exposed to user
32 void hoverEnter();
32 void hoverEnter(QPoint pos);
33 void hoverLeave();
33 void hoverLeave();
34 void toggleFloatingValues(); // Private signal, TODO: move to private impl
34 void toggleFloatingValues(); // Private signal, TODO: move to private impl
35 void showToolTip(QPoint pos, QString tip); // Private signal, TODO: move to private impl
35
36
36 public Q_SLOTS:
37 public Q_SLOTS:
37 // TODO: should these be in series instead?
38 void enableFloatingValues(bool enabled); // enables floating values on top of bars
38 void enableFloatingValues(bool enabled); // enables floating values on top of bars
39 void enableToolTip(bool enabled); // enables tooltips
39 void enableToolTip(bool enabled); // enables tooltips
40 void enableSeparators(bool enabled); // enables separators between categories
40
41
41 // TODO: these slots belong to private implementation.
42 // TODO: these slots belong to private implementation.
42 // These are for single bars to notify set about internal events
43 // These are for single bars to notify set about internal events
@@ -53,7 +54,8 private:
53
54
54 // TODO: to pimpl
55 // TODO: to pimpl
55 bool mFloatingValuesEnabled;
56 bool mFloatingValuesEnabled;
56 bool mHoverNamesEnabled;
57 bool mToolTipEnabled;
58 bool mSeparatorsEnabled;
57 };
59 };
58
60
59 QTCOMMERCIALCHART_END_NAMESPACE
61 QTCOMMERCIALCHART_END_NAMESPACE
@@ -8,8 +8,8
8
8
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10
10
11 StackedBarPresenter::StackedBarPresenter(BarChartModel& model, QGraphicsItem *parent) :
11 StackedBarPresenter::StackedBarPresenter(QBarChartSeries *series, QGraphicsItem *parent) :
12 BarPresenterBase(model,parent)
12 BarPresenterBase(series,parent)
13 {
13 {
14 }
14 }
15
15
@@ -17,13 +17,13 void StackedBarPresenter::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 (mModel.countSets() <= 0) {
20 if (mSeries->countSets() <= 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 (mModel.countCategories() == 0) {
26 if (mSeries->countCategories() == 0) {
27 qDebug() << "No categories in model!";
27 qDebug() << "No categories in model!";
28 // Nothing to do
28 // Nothing to do
29 return;
29 return;
@@ -37,25 +37,25 void StackedBarPresenter::layoutChanged()
37 // TODO: better way to auto-layout
37 // TODO: better way to auto-layout
38 // Use reals for accurancy (we might get some compiler warnings... :)
38 // Use reals for accurancy (we might get some compiler warnings... :)
39 // TODO: use temp variable for category count...
39 // TODO: use temp variable for category count...
40 qreal maxSum = mModel.maxCategorySum();
40 qreal maxSum = mSeries->maxCategorySum();
41 qreal h = mHeight;
41 qreal h = mHeight;
42 qreal scale = (h / maxSum);
42 qreal scale = (h / maxSum);
43
43
44 int itemIndex(0);
44 int itemIndex(0);
45 int labelIndex(0);
45 int labelIndex(0);
46 qreal tW = mWidth;
46 qreal tW = mWidth;
47 qreal tC = mModel.countCategories() + 1;
47 qreal tC = mSeries->countCategories() + 1;
48 qreal xStep = (tW/tC);
48 qreal xStep = (tW/tC);
49 qreal xPos = ((tW/tC) - mBarDefaultWidth / 2);
49 qreal xPos = ((tW/tC) - mBarDefaultWidth / 2);
50
50
51 for (int category = 0; category < mModel.countCategories(); category++) {
51 for (int category = 0; category < mSeries->countCategories(); category++) {
52 qreal yPos = h;
52 qreal yPos = h;
53 for (int set=0; set < mModel.countSets(); set++) {
53 for (int set=0; set < mSeries->countSets(); set++) {
54 qreal barHeight = mModel.valueAt(set, category) * scale;
54 qreal barHeight = mSeries->valueAt(set, category) * scale;
55 Bar* bar = mBars.at(itemIndex);
55 Bar* bar = mBars.at(itemIndex);
56
56
57 bar->resize(mBarDefaultWidth, barHeight);
57 bar->resize(mBarDefaultWidth, barHeight);
58 bar->setBrush(mModel.setAt(set)->brush());
58 bar->setBrush(mSeries->setAt(set)->brush());
59 bar->setPos(xPos, yPos-barHeight);
59 bar->setPos(xPos, yPos-barHeight);
60 itemIndex++;
60 itemIndex++;
61 yPos -= barHeight;
61 yPos -= barHeight;
@@ -70,7 +70,7 void StackedBarPresenter::layoutChanged()
70
70
71 // Position separators
71 // Position separators
72 xPos = xStep + xStep/2;
72 xPos = xStep + xStep/2;
73 for (int s=0; s < mModel.countCategories() - 1; s++) {
73 for (int s=0; s < mSeries->countCategories() - 1; s++) {
74 Separator* sep = mSeparators.at(s);
74 Separator* sep = mSeparators.at(s);
75 sep->setPos(xPos,0);
75 sep->setPos(xPos,0);
76 sep->setSize(QSizeF(1,mHeight));
76 sep->setSize(QSizeF(1,mHeight));
@@ -80,10 +80,10 void StackedBarPresenter::layoutChanged()
80 // Position floating values
80 // Position floating values
81 itemIndex = 0;
81 itemIndex = 0;
82 xPos = ((tW/tC) - mBarDefaultWidth / 2);
82 xPos = ((tW/tC) - mBarDefaultWidth / 2);
83 for (int category=0; category < mModel.countCategories(); category++) {
83 for (int category=0; category < mSeries->countCategories(); category++) {
84 qreal yPos = h;
84 qreal yPos = h;
85 for (int set=0; set < mModel.countSets(); set++) {
85 for (int set=0; set < mSeries->countSets(); set++) {
86 qreal barHeight = mModel.valueAt(set,category) * scale;
86 qreal barHeight = mSeries->valueAt(set,category) * scale;
87 BarValue* value = mFloatingValues.at(itemIndex);
87 BarValue* value = mFloatingValues.at(itemIndex);
88
88
89 // TODO: remove hard coding, apply layout
89 // TODO: remove hard coding, apply layout
@@ -91,8 +91,8 void StackedBarPresenter::layoutChanged()
91 value->setPos(xPos, yPos-barHeight/2);
91 value->setPos(xPos, yPos-barHeight/2);
92 value->setPen(QPen(QColor(255,255,255,255)));
92 value->setPen(QPen(QColor(255,255,255,255)));
93
93
94 if (mModel.valueAt(set,category) != 0) {
94 if (mSeries->valueAt(set,category) != 0) {
95 value->setValueString(QString::number(mModel.valueAt(set,category)));
95 value->setValueString(QString::number(mSeries->valueAt(set,category)));
96 } else {
96 } else {
97 value->setValueString(QString(""));
97 value->setValueString(QString(""));
98 }
98 }
@@ -106,4 +106,6 void StackedBarPresenter::layoutChanged()
106 mLayoutDirty = true;
106 mLayoutDirty = true;
107 }
107 }
108
108
109 #include "moc_stackedbarpresenter.cpp"
110
109 QTCOMMERCIALCHART_END_NAMESPACE
111 QTCOMMERCIALCHART_END_NAMESPACE
@@ -9,8 +9,9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9
9
10 class StackedBarPresenter : public BarPresenterBase
10 class StackedBarPresenter : public BarPresenterBase
11 {
11 {
12 Q_OBJECT
12 public:
13 public:
13 StackedBarPresenter(BarChartModel& model, QGraphicsItem *parent = 0);
14 StackedBarPresenter(QBarChartSeries *series, QGraphicsItem *parent = 0);
14
15
15 private:
16 private:
16 // From BarPresenterBase
17 // From BarPresenterBase
@@ -115,7 +115,7 void ChartPresenter::handleSeriesAdded(QChartSeries* series)
115
115
116 case QChartSeries::SeriesTypeBar: {
116 case QChartSeries::SeriesTypeBar: {
117 QBarChartSeries* barSeries = static_cast<QBarChartSeries*>(series);
117 QBarChartSeries* barSeries = static_cast<QBarChartSeries*>(series);
118 BarPresenter* item = new BarPresenter(barSeries->model(),m_chart);
118 BarPresenter* item = new BarPresenter(barSeries,m_chart);
119 m_chartTheme->decorate(item,barSeries,m_chartItems.count());
119 m_chartTheme->decorate(item,barSeries,m_chartItems.count());
120 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
120 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
121 QObject::connect(barSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int)));
121 QObject::connect(barSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int)));
@@ -127,7 +127,7 void ChartPresenter::handleSeriesAdded(QChartSeries* series)
127 case QChartSeries::SeriesTypeStackedBar: {
127 case QChartSeries::SeriesTypeStackedBar: {
128
128
129 QStackedBarChartSeries* stackedBarSeries = static_cast<QStackedBarChartSeries*>(series);
129 QStackedBarChartSeries* stackedBarSeries = static_cast<QStackedBarChartSeries*>(series);
130 StackedBarPresenter* item = new StackedBarPresenter(stackedBarSeries->model(),m_chart);
130 StackedBarPresenter* item = new StackedBarPresenter(stackedBarSeries,m_chart);
131 m_chartTheme->decorate(item,stackedBarSeries,m_chartItems.count());
131 m_chartTheme->decorate(item,stackedBarSeries,m_chartItems.count());
132 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
132 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
133 QObject::connect(stackedBarSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int)));
133 QObject::connect(stackedBarSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int)));
@@ -138,7 +138,7 void ChartPresenter::handleSeriesAdded(QChartSeries* series)
138 case QChartSeries::SeriesTypePercentBar: {
138 case QChartSeries::SeriesTypePercentBar: {
139
139
140 QPercentBarChartSeries* percentBarSeries = static_cast<QPercentBarChartSeries*>(series);
140 QPercentBarChartSeries* percentBarSeries = static_cast<QPercentBarChartSeries*>(series);
141 PercentBarPresenter* item = new PercentBarPresenter(percentBarSeries->model(),m_chart);
141 PercentBarPresenter* item = new PercentBarPresenter(percentBarSeries,m_chart);
142 m_chartTheme->decorate(item,percentBarSeries ,m_chartItems.count());
142 m_chartTheme->decorate(item,percentBarSeries ,m_chartItems.count());
143 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
143 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
144 QObject::connect(percentBarSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int)));
144 QObject::connect(percentBarSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int)));
General Comments 0
You need to be logged in to leave comments. Login now