@@ -1,95 +1,97 | |||
|
1 | 1 | #include "barpresenter.h" |
|
2 | 2 | #include "bar_p.h" |
|
3 | 3 | #include "barlabel_p.h" |
|
4 | 4 | #include "barvalue_p.h" |
|
5 | 5 | #include "qbarset.h" |
|
6 | 6 | #include <QDebug> |
|
7 | 7 | |
|
8 | 8 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
9 | 9 | |
|
10 |
BarPresenter::BarPresenter(BarChart |
|
|
11 |
BarPresenterBase( |
|
|
10 | BarPresenter::BarPresenter(QBarChartSeries *series, QGraphicsItem *parent) : | |
|
11 | BarPresenterBase(series, parent) | |
|
12 | 12 | { |
|
13 | 13 | mBarDefaultWidth = 15; |
|
14 | 14 | } |
|
15 | 15 | |
|
16 | 16 | void BarPresenter::layoutChanged() |
|
17 | 17 | { |
|
18 | 18 | // Scale bars to new layout |
|
19 | 19 | // Layout for bars: |
|
20 |
if (m |
|
|
20 | if (mSeries->countSets() <= 0) { | |
|
21 | 21 | qDebug() << "No sets in model!"; |
|
22 | 22 | return; |
|
23 | 23 | } |
|
24 | 24 | |
|
25 | 25 | if (childItems().count() == 0) { |
|
26 | 26 | qDebug() << "WARNING: BarPresenter::layoutChanged called before graphics items are created!"; |
|
27 | 27 | return; |
|
28 | 28 | } |
|
29 | 29 | |
|
30 | 30 | // TODO: better way to auto-layout? |
|
31 | 31 | // Use reals for accurancy (we might get some compiler warnings... :) |
|
32 |
int categoryCount = m |
|
|
33 |
int setCount = m |
|
|
32 | int categoryCount = mSeries->countCategories(); | |
|
33 | int setCount = mSeries->countSets(); | |
|
34 | 34 | |
|
35 | 35 | qreal tW = mWidth; |
|
36 | 36 | qreal tH = mHeight; |
|
37 |
qreal tM = m |
|
|
37 | qreal tM = mSeries->max(); | |
|
38 | 38 | qreal scale = (tH/tM); |
|
39 | 39 | qreal tC = categoryCount+1; |
|
40 | 40 | qreal xStepPerSet = (tW/tC); |
|
41 | 41 | |
|
42 | 42 | // Scaling. |
|
43 | 43 | int itemIndex(0); |
|
44 | 44 | int labelIndex(0); |
|
45 | 45 | |
|
46 | 46 | for (int category=0; category < categoryCount; category++) { |
|
47 | 47 | qreal xPos = xStepPerSet * category + ((tW + mBarDefaultWidth*setCount)/(categoryCount*2)); |
|
48 | 48 | qreal yPos = mHeight; |
|
49 | 49 | for (int set = 0; set < setCount; set++) { |
|
50 |
qreal barHeight = m |
|
|
50 | qreal barHeight = mSeries->valueAt(set,category) * scale; | |
|
51 | 51 | Bar* bar = mBars.at(itemIndex); |
|
52 | 52 | |
|
53 | 53 | // TODO: width settable per bar? |
|
54 | 54 | bar->resize(mBarDefaultWidth, barHeight); |
|
55 |
bar->setBrush(m |
|
|
56 |
bar->setPos(xPos, yPos-barHeight); |
|
|
55 | bar->setBrush(mSeries->setAt(set)->brush()); | |
|
56 | bar->setPos(xPos, yPos-barHeight); | |
|
57 | 57 | itemIndex++; |
|
58 | 58 | xPos += mBarDefaultWidth; |
|
59 | 59 | } |
|
60 | 60 | |
|
61 | 61 | // TODO: Layout for labels, remove magic number |
|
62 | 62 | xPos = xStepPerSet * category + ((tW + mBarDefaultWidth*setCount)/(categoryCount*2)); |
|
63 | 63 | BarLabel* label = mLabels.at(labelIndex); |
|
64 | 64 | label->setPos(xPos, mHeight + 20); |
|
65 | 65 | labelIndex++; |
|
66 | 66 | } |
|
67 | 67 | |
|
68 | 68 | // Position floating values |
|
69 | 69 | itemIndex = 0; |
|
70 |
for (int category=0; category < m |
|
|
70 | for (int category=0; category < mSeries->countCategories(); category++) { | |
|
71 | 71 | qreal xPos = xStepPerSet * category + ((tW + mBarDefaultWidth*setCount)/(categoryCount*2)); |
|
72 | 72 | qreal yPos = mHeight; |
|
73 |
for (int set=0; set < m |
|
|
74 |
qreal barHeight = m |
|
|
73 | for (int set=0; set < mSeries->countSets(); set++) { | |
|
74 | qreal barHeight = mSeries->valueAt(set,category) * scale; | |
|
75 | 75 | BarValue* value = mFloatingValues.at(itemIndex); |
|
76 | 76 | |
|
77 | 77 | // TODO: remove hard coding, apply layout |
|
78 | 78 | value->resize(100,50); |
|
79 | 79 | value->setPos(xPos, yPos-barHeight/2); |
|
80 | 80 | value->setPen(QPen(QColor(255,255,255,255))); |
|
81 | 81 | |
|
82 |
if (m |
|
|
83 |
value->setValueString(QString::number(m |
|
|
82 | if (mSeries->valueAt(set,category) != 0) { | |
|
83 | value->setValueString(QString::number(mSeries->valueAt(set,category))); | |
|
84 | 84 | } else { |
|
85 | 85 | value->setValueString(QString("")); |
|
86 | 86 | } |
|
87 | 87 | |
|
88 | 88 | itemIndex++; |
|
89 | 89 | xPos += mBarDefaultWidth; |
|
90 | 90 | } |
|
91 | 91 | } |
|
92 | 92 | mLayoutDirty = true; |
|
93 | 93 | } |
|
94 | 94 | |
|
95 | #include "moc_barpresenter.cpp" | |
|
96 | ||
|
95 | 97 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,27 +1,30 | |||
|
1 | 1 | #ifndef BARPRESENTER_H |
|
2 | 2 | #define BARPRESENTER_H |
|
3 | 3 | |
|
4 | #include "qchartglobal.h" | |
|
4 | 5 | #include "barpresenterbase.h" |
|
5 | #include "qbarchartseries.h" | |
|
6 | 6 | #include <QGraphicsItem> |
|
7 | 7 | |
|
8 | 8 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
9 | 9 | |
|
10 | class QBarChartSeries; | |
|
11 | ||
|
10 | 12 | // Presenter for parallel bars. Grouping of bars is done on category basis. |
|
11 | 13 | class BarPresenter : public BarPresenterBase |
|
12 | 14 | { |
|
15 | Q_OBJECT | |
|
13 | 16 | public: |
|
14 |
explicit BarPresenter(BarChart |
|
|
17 | explicit BarPresenter(QBarChartSeries *series, QGraphicsItem *parent = 0); | |
|
15 | 18 | |
|
16 | 19 | private: |
|
17 | 20 | |
|
18 | 21 | // From BarPresenterBase |
|
19 | 22 | void layoutChanged(); // layout has changed -> need to recalculate bar sizes |
|
20 | 23 | |
|
21 | 24 | private: |
|
22 | 25 | // Data |
|
23 | 26 | }; |
|
24 | 27 | |
|
25 | 28 | QTCOMMERCIALCHART_END_NAMESPACE |
|
26 | 29 | |
|
27 | 30 | #endif // BARPRESENTER_H |
@@ -1,150 +1,171 | |||
|
1 | 1 | #include "barpresenterbase.h" |
|
2 | 2 | #include "bar_p.h" |
|
3 | 3 | #include "barvalue_p.h" |
|
4 | 4 | #include "barlabel_p.h" |
|
5 | 5 | #include "separator_p.h" |
|
6 | 6 | #include "qbarset.h" |
|
7 | #include "qbarchartseries.h" | |
|
7 | 8 | #include <QDebug> |
|
9 | #include <QToolTip> | |
|
8 | 10 | |
|
9 | 11 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
10 | 12 | |
|
11 |
BarPresenterBase::BarPresenterBase(BarChart |
|
|
13 | BarPresenterBase::BarPresenterBase(QBarChartSeries *series, QGraphicsItem *parent) | |
|
12 | 14 | : ChartItem(parent) |
|
13 | 15 | ,mBarDefaultWidth(20) // TODO: remove hard coding, when we have layout code ready |
|
14 | 16 | ,mLayoutSet(false) |
|
15 | 17 | ,mLayoutDirty(true) |
|
16 | ,mSeparatorsVisible(false) | |
|
17 | ,mModel(model) | |
|
18 | ,mSeries(series) | |
|
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 | 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 | 36 | void BarPresenterBase::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) |
|
28 | 37 | { |
|
29 | 38 | if (!mLayoutSet) { |
|
30 | 39 | qDebug() << "BarPresenterBase::paint called without layout set. Aborting."; |
|
31 | 40 | return; |
|
32 | 41 | } |
|
33 | 42 | // if (mLayoutDirty) { |
|
34 | 43 | // Layout or data has changed. Need to redraw. |
|
35 | 44 | foreach(QGraphicsItem* i, childItems()) { |
|
36 | 45 | i->paint(painter,option,widget); |
|
37 | 46 | } |
|
38 | 47 | // } |
|
39 | 48 | } |
|
40 | 49 | |
|
41 | 50 | QRectF BarPresenterBase::boundingRect() const |
|
42 | 51 | { |
|
43 | 52 | return QRectF(0,0,mWidth,mHeight); |
|
44 | 53 | } |
|
45 | 54 | |
|
46 | 55 | void BarPresenterBase::setBarWidth( int w ) |
|
47 | 56 | { |
|
48 | 57 | mBarDefaultWidth = w; |
|
49 | 58 | } |
|
50 | 59 | |
|
51 | 60 | void BarPresenterBase::dataChanged() |
|
52 | 61 | { |
|
53 | 62 | // TODO: performance optimizations. Do we really need to delete and create items every time data is changed or can we reuse them? |
|
54 | 63 | qDebug() << "datachanged"; |
|
55 | 64 | // Delete old bars |
|
56 | 65 | foreach (QGraphicsItem* item, childItems()) { |
|
57 | 66 | delete item; |
|
58 | 67 | } |
|
59 | 68 | |
|
60 | 69 | mBars.clear(); |
|
61 | 70 | mLabels.clear(); |
|
62 | 71 | mSeparators.clear(); |
|
63 | 72 | mFloatingValues.clear(); |
|
64 | 73 | |
|
65 | 74 | // Create new graphic items for bars |
|
66 |
for (int c=0; c<m |
|
|
67 |
for (int s=0; s<m |
|
|
68 |
QBarSet *set = m |
|
|
75 | for (int c=0; c<mSeries->countCategories(); c++) { | |
|
76 | for (int s=0; s<mSeries->countSets(); s++) { | |
|
77 | QBarSet *set = mSeries->setAt(s); | |
|
69 | 78 | Bar *bar = new Bar(this); |
|
70 | 79 | childItems().append(bar); |
|
71 | 80 | mBars.append(bar); |
|
72 | 81 | connect(bar,SIGNAL(clicked()),set,SLOT(barClicked())); |
|
73 | 82 | // TODO: should the event be passed to set or not? |
|
74 | 83 | connect(bar,SIGNAL(hoverEntered(QPoint)),set,SLOT(barHoverEntered(QPoint))); |
|
75 | 84 | connect(bar,SIGNAL(hoverLeaved()),set,SLOT(barHoverLeaved())); |
|
76 | 85 | } |
|
77 | 86 | } |
|
78 | 87 | |
|
79 | 88 | // Create labels |
|
80 |
int count = m |
|
|
89 | int count = mSeries->countCategories(); | |
|
81 | 90 | for (int i=0; i<count; i++) { |
|
82 | 91 | BarLabel* label = new BarLabel(this); |
|
83 |
label->set(m |
|
|
92 | label->set(mSeries->label(i)); | |
|
84 | 93 | childItems().append(label); |
|
85 | 94 | mLabels.append(label); |
|
86 | 95 | } |
|
87 | 96 | |
|
88 | 97 | // Create separators |
|
89 |
count = m |
|
|
98 | count = mSeries->countCategories() - 1; // There is one less separator than columns | |
|
90 | 99 | for (int i=0; i<count; i++) { |
|
91 | 100 | Separator* sep = new Separator(this); |
|
92 | 101 | sep->setColor(QColor(255,0,0,255)); // TODO: color for separations from theme |
|
93 | 102 | childItems().append(sep); |
|
94 | 103 | mSeparators.append(sep); |
|
95 | 104 | } |
|
96 | 105 | |
|
97 | 106 | // Create floating values |
|
98 |
for (int category=0; category<m |
|
|
99 |
for (int s=0; s<m |
|
|
100 |
QBarSet *set = m |
|
|
107 | for (int category=0; category<mSeries->countCategories(); category++) { | |
|
108 | for (int s=0; s<mSeries->countSets(); s++) { | |
|
109 | QBarSet *set = mSeries->setAt(s); | |
|
101 | 110 | BarValue *value = new BarValue(*set, this); |
|
102 | 111 | childItems().append(value); |
|
103 | 112 | mFloatingValues.append(value); |
|
104 | 113 | connect(set,SIGNAL(toggleFloatingValues()),value,SLOT(toggleVisible())); |
|
105 | 114 | } |
|
106 | 115 | } |
|
107 | 116 | |
|
108 | 117 | // TODO: if (autolayout) { layoutChanged() } or something |
|
109 | 118 | mLayoutDirty = true; |
|
110 | 119 | } |
|
111 | 120 | |
|
112 | 121 | //handlers |
|
113 | 122 | |
|
114 | 123 | void BarPresenterBase::handleModelChanged(int index) |
|
115 | 124 | { |
|
116 | 125 | // qDebug() << "BarPresenterBase::handleModelChanged" << index; |
|
117 | 126 | dataChanged(); |
|
118 | 127 | } |
|
119 | 128 | |
|
120 | 129 | void BarPresenterBase::handleDomainChanged(const Domain& domain) |
|
121 | 130 | { |
|
122 | 131 | // qDebug() << "BarPresenterBase::handleDomainChanged"; |
|
123 | 132 | // TODO: Figure out the use case for this. |
|
124 | 133 | // Affects the size of visible item, so layout is changed. |
|
125 | 134 | // layoutChanged(); |
|
126 | 135 | } |
|
127 | 136 | |
|
128 | 137 | void BarPresenterBase::handleGeometryChanged(const QRectF& rect) |
|
129 | 138 | { |
|
130 | 139 | mWidth = rect.width(); |
|
131 | 140 | mHeight = rect.height(); |
|
132 | 141 | layoutChanged(); |
|
133 | 142 | mLayoutSet = true; |
|
134 | 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 | 169 | #include "moc_barpresenterbase.cpp" |
|
149 | 170 | |
|
150 | 171 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,69 +1,76 | |||
|
1 | 1 | #ifndef BARPRESENTERBASE_H |
|
2 | 2 | #define BARPRESENTERBASE_H |
|
3 | 3 | |
|
4 | 4 | #include "chartitem_p.h" |
|
5 |
#include "barchart |
|
|
5 | #include "qbarchartseries.h" | |
|
6 | 6 | #include <QPen> |
|
7 | 7 | #include <QBrush> |
|
8 | 8 | #include <QGraphicsItem> |
|
9 | 9 | |
|
10 | 10 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
11 | 11 | |
|
12 | 12 | class Bar; |
|
13 | 13 | class BarLabel; |
|
14 | 14 | class Separator; |
|
15 | 15 | class BarValue; |
|
16 | 16 | |
|
17 | 17 | // Common implemantation of different presenters. Not to be instantiated. |
|
18 | 18 | // TODO: combine this with BarPresenter and derive other presenters from it? |
|
19 | 19 | class BarPresenterBase : public QObject, public ChartItem |
|
20 | 20 | { |
|
21 | 21 | Q_OBJECT |
|
22 | 22 | public: |
|
23 |
BarPresenterBase(BarChart |
|
|
24 | void setSeparatorsVisible(bool visible = true); | |
|
23 | BarPresenterBase(QBarChartSeries *series, QGraphicsItem *parent = 0); | |
|
24 | ~BarPresenterBase(); | |
|
25 | 25 | |
|
26 | 26 | public: |
|
27 | ||
|
28 | 27 | // From QGraphicsItem |
|
29 | 28 | void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); |
|
30 | 29 | QRectF boundingRect() const; |
|
31 | 30 | |
|
32 | 31 | // TODO: these may change with layout awarness. |
|
33 | 32 | void setBarWidth( int w ); |
|
34 | 33 | |
|
35 | 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 |
|
36 | 35 | virtual void dataChanged(); // data of series has changed -> need to recalculate bar sizes |
|
37 | 36 | virtual void layoutChanged() = 0; // layout has changed -> need to recalculate bar sizes |
|
38 | 37 | |
|
39 | 38 | protected slots: |
|
40 | 39 | void handleModelChanged(int index); |
|
41 | 40 | void handleDomainChanged(const Domain& domain); |
|
42 | 41 | void handleGeometryChanged(const QRectF& size); |
|
43 | 42 | |
|
44 | void barHoverEntered(QGraphicsSceneHoverEvent *event); // Internal. | |
|
45 | void barHoverLeaved(QGraphicsSceneHoverEvent *event); | |
|
43 | // Internal slots | |
|
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 | 49 | protected: |
|
48 | 50 | |
|
49 | 51 | // TODO: consider these. |
|
50 | 52 | int mHeight; // Layout spesific |
|
51 | 53 | int mWidth; |
|
52 | 54 | int mBarDefaultWidth; |
|
53 | 55 | |
|
54 | 56 | bool mLayoutSet; // True, if component has been laid out. |
|
55 | 57 | bool mLayoutDirty; |
|
56 | 58 | |
|
57 | bool mSeparatorsVisible; | |
|
58 | BarChartModel& mModel; | |
|
59 | bool mFloatingValuesEnabled; | |
|
60 | bool mToolTipEnabled; | |
|
61 | bool mSeparatorsEnabled; | |
|
62 | ||
|
63 | // Owned | |
|
64 | QBarChartSeries* mSeries; | |
|
59 | 65 | |
|
60 | 66 | // Not owned. |
|
61 | 67 | QList<Bar*> mBars; |
|
62 | 68 | QList<BarLabel*> mLabels; |
|
63 | 69 | QList<Separator*> mSeparators; |
|
64 | 70 | QList<BarValue*> mFloatingValues; |
|
71 | ||
|
65 | 72 | }; |
|
66 | 73 | |
|
67 | 74 | QTCOMMERCIALCHART_END_NAMESPACE |
|
68 | 75 | |
|
69 | 76 | #endif // BARPRESENTERBASE_H |
@@ -1,110 +1,112 | |||
|
1 | 1 | #include "percentbarpresenter.h" |
|
2 | 2 | #include "bar_p.h" |
|
3 | 3 | #include "barlabel_p.h" |
|
4 | 4 | #include "barvalue_p.h" |
|
5 | 5 | #include "separator_p.h" |
|
6 | 6 | #include "qbarset.h" |
|
7 | 7 | #include <QDebug> |
|
8 | 8 | |
|
9 | 9 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
10 | 10 | |
|
11 | 11 | |
|
12 |
PercentBarPresenter::PercentBarPresenter(BarChart |
|
|
13 |
BarPresenterBase( |
|
|
12 | PercentBarPresenter::PercentBarPresenter(QBarChartSeries *series, QGraphicsItem *parent) : | |
|
13 | BarPresenterBase(series, parent) | |
|
14 | 14 | { |
|
15 | 15 | } |
|
16 | 16 | |
|
17 | 17 | void PercentBarPresenter::layoutChanged() |
|
18 | 18 | { |
|
19 | 19 | // Scale bars to new layout |
|
20 | 20 | // Layout for bars: |
|
21 |
if (m |
|
|
21 | if (mSeries->countSets() <= 0) { | |
|
22 | 22 | qDebug() << "No sets in model!"; |
|
23 | 23 | // Nothing to do. |
|
24 | 24 | return; |
|
25 | 25 | } |
|
26 | 26 | |
|
27 | 27 | if (childItems().count() == 0) { |
|
28 | 28 | qDebug() << "WARNING: PercentBarPresenter::layoutChanged called before graphics items are created!"; |
|
29 | 29 | return; |
|
30 | 30 | } |
|
31 | 31 | |
|
32 | 32 | // TODO: better way to auto-layout |
|
33 | 33 | // Use reals for accurancy (we might get some compiler warnings... :) |
|
34 |
int count = m |
|
|
34 | int count = mSeries->countCategories(); | |
|
35 | 35 | int itemIndex(0); |
|
36 | 36 | int labelIndex(0); |
|
37 | 37 | qreal tW = mWidth; |
|
38 | 38 | qreal tC = count+1; |
|
39 | 39 | qreal xStep = (tW/tC); |
|
40 | 40 | qreal xPos = ((tW/tC) - mBarDefaultWidth / 2); |
|
41 | 41 | qreal h = mHeight; |
|
42 | 42 | |
|
43 |
for (int category = 0; category < m |
|
|
44 |
qreal colSum = m |
|
|
43 | for (int category = 0; category < mSeries->countCategories(); category++) { | |
|
44 | qreal colSum = mSeries->categorySum(category); | |
|
45 | 45 | qreal scale = (h / colSum); |
|
46 | 46 | qreal yPos = h; |
|
47 |
for (int set=0; set < m |
|
|
48 |
qreal barHeight = m |
|
|
47 | for (int set=0; set < mSeries->countSets(); set++) { | |
|
48 | qreal barHeight = mSeries->valueAt(set, category) * scale; | |
|
49 | 49 | Bar* bar = mBars.at(itemIndex); |
|
50 | 50 | |
|
51 | 51 | // TODO: width settable per bar? |
|
52 | 52 | bar->resize(mBarDefaultWidth, barHeight); |
|
53 |
bar->setBrush(m |
|
|
53 | bar->setBrush(mSeries->setAt(set)->brush()); | |
|
54 | 54 | bar->setPos(xPos, yPos-barHeight); |
|
55 | 55 | itemIndex++; |
|
56 | 56 | yPos -= barHeight; |
|
57 | 57 | } |
|
58 | 58 | |
|
59 | 59 | // TODO: Layout for labels, remove magic number |
|
60 | 60 | BarLabel* label = mLabels.at(labelIndex); |
|
61 | 61 | label->setPos(xPos, mHeight + 20); |
|
62 | 62 | labelIndex++; |
|
63 | 63 | xPos += xStep; |
|
64 | 64 | } |
|
65 | 65 | |
|
66 | 66 | // Position separators |
|
67 | 67 | xPos = xStep + xStep/2; |
|
68 |
for (int s=0; s < m |
|
|
68 | for (int s=0; s < mSeries->countCategories() - 1; s++) { | |
|
69 | 69 | Separator* sep = mSeparators.at(s); |
|
70 | 70 | sep->setPos(xPos,0); |
|
71 | 71 | sep->setSize(QSizeF(1,mHeight)); |
|
72 | 72 | xPos += xStep; |
|
73 | 73 | } |
|
74 | 74 | |
|
75 | 75 | // Position floating values |
|
76 | 76 | itemIndex = 0; |
|
77 | 77 | xPos = ((tW/tC) - mBarDefaultWidth / 2); |
|
78 |
for (int category=0; category < m |
|
|
78 | for (int category=0; category < mSeries->countCategories(); category++) { | |
|
79 | 79 | qreal yPos = h; |
|
80 |
qreal colSum = m |
|
|
80 | qreal colSum = mSeries->categorySum(category); | |
|
81 | 81 | qreal scale = (h / colSum); |
|
82 |
for (int set=0; set < m |
|
|
83 |
qreal barHeight = m |
|
|
82 | for (int set=0; set < mSeries->countSets(); set++) { | |
|
83 | qreal barHeight = mSeries->valueAt(set,category) * scale; | |
|
84 | 84 | BarValue* value = mFloatingValues.at(itemIndex); |
|
85 | 85 | |
|
86 | 86 | // TODO: remove hard coding, apply layout |
|
87 | 87 | value->resize(100,50); |
|
88 | 88 | value->setPos(xPos, yPos-barHeight/2); |
|
89 | 89 | value->setPen(QPen(QColor(255,255,255,255))); |
|
90 | 90 | |
|
91 |
if (m |
|
|
92 |
int p = m |
|
|
91 | if (mSeries->valueAt(set,category) != 0) { | |
|
92 | int p = mSeries->percentageAt(set,category) * 100; | |
|
93 | 93 | QString vString(QString::number(p)); |
|
94 | 94 | vString.truncate(3); |
|
95 | 95 | vString.append("%"); |
|
96 | 96 | value->setValueString(vString); |
|
97 | 97 | } else { |
|
98 | 98 | value->setValueString(QString("")); |
|
99 | 99 | } |
|
100 | 100 | |
|
101 | 101 | itemIndex++; |
|
102 | 102 | yPos -= barHeight; |
|
103 | 103 | } |
|
104 | 104 | xPos += xStep; |
|
105 | 105 | } |
|
106 | 106 | |
|
107 | 107 | mLayoutDirty = true; |
|
108 | 108 | } |
|
109 | 109 | |
|
110 | #include "moc_percentbarpresenter.cpp" | |
|
111 | ||
|
110 | 112 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,28 +1,29 | |||
|
1 | 1 | #ifndef PERCENTBARPRESENTER_H |
|
2 | 2 | #define PERCENTBARPRESENTER_H |
|
3 | 3 | |
|
4 | 4 | #include "chartitem_p.h" |
|
5 | 5 | #include "bar_p.h" |
|
6 | 6 | #include "qpercentbarchartseries.h" |
|
7 | 7 | #include "barpresenterbase.h" |
|
8 | 8 | #include <QGraphicsItem> |
|
9 | 9 | |
|
10 | 10 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
11 | 11 | |
|
12 | 12 | class PercentBarPresenter : public BarPresenterBase |
|
13 | 13 | { |
|
14 | Q_OBJECT | |
|
14 | 15 | public: |
|
15 |
PercentBarPresenter(BarChart |
|
|
16 | PercentBarPresenter(QBarChartSeries *series, QGraphicsItem *parent = 0); | |
|
16 | 17 | |
|
17 | 18 | private: |
|
18 | 19 | |
|
19 | 20 | void layoutChanged(); // layout has changed -> need to recalculate bar sizes |
|
20 | 21 | |
|
21 | 22 | private: |
|
22 | 23 | |
|
23 | 24 | // Data |
|
24 | 25 | }; |
|
25 | 26 | |
|
26 | 27 | QTCOMMERCIALCHART_END_NAMESPACE |
|
27 | 28 | |
|
28 | 29 | #endif // PERCENTBARPRESENTER_H |
@@ -1,88 +1,116 | |||
|
1 | 1 | #include <QDebug> |
|
2 | 2 | #include "qbarchartseries.h" |
|
3 | 3 | #include "qbarcategory.h" |
|
4 | 4 | #include "qbarset.h" |
|
5 | 5 | #include "barchartmodel_p.h" |
|
6 | 6 | |
|
7 | 7 | |
|
8 | 8 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
9 | 9 | |
|
10 | 10 | QBarChartSeries::QBarChartSeries(QBarCategory *category, QObject *parent) |
|
11 | 11 | : QChartSeries(parent) |
|
12 | 12 | ,mModel(new BarChartModel(category, this)) |
|
13 | 13 | { |
|
14 | 14 | } |
|
15 | 15 | |
|
16 | 16 | void QBarChartSeries::addBarSet(QBarSet *set) |
|
17 | 17 | { |
|
18 |
|
|
|
19 |
|
|
|
18 | connect(this,SIGNAL(floatingValuesEnabled(bool)),set,SLOT(enableFloatingValues(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 | 22 | mModel->addBarSet(set); |
|
21 | 23 | } |
|
22 | 24 | |
|
23 | 25 | void QBarChartSeries::removeBarSet(QBarSet *set) |
|
24 | 26 | { |
|
25 |
|
|
|
26 |
|
|
|
27 | disconnect(set,SLOT(enableFloatingValues(bool))); | |
|
28 | disconnect(set,SLOT(enableSeparators(bool))); | |
|
29 | disconnect(set,SLOT(enableToolTip(bool))); | |
|
27 | 30 | mModel->removeBarSet(set); |
|
28 | 31 | } |
|
29 | 32 | |
|
30 | 33 | int QBarChartSeries::countSets() |
|
31 | 34 | { |
|
32 | 35 | return mModel->countSets(); |
|
33 | 36 | } |
|
34 | 37 | |
|
35 | 38 | QBarSet* QBarChartSeries::nextSet(bool getFirst) |
|
36 | 39 | { |
|
37 | 40 | return mModel->nextSet(getFirst); |
|
38 | 41 | } |
|
39 | 42 | |
|
43 | QBarSet* QBarChartSeries::setAt(int index) | |
|
44 | { | |
|
45 | return mModel->setAt(index); | |
|
46 | } | |
|
47 | ||
|
40 | 48 | QList<QString> QBarChartSeries::legend() |
|
41 | 49 | { |
|
42 | 50 | return mModel->legend(); |
|
43 | 51 | } |
|
44 | 52 | |
|
53 | QString QBarChartSeries::label(int category) | |
|
54 | { | |
|
55 | return mModel->label(category); | |
|
56 | } | |
|
57 | ||
|
45 | 58 | void QBarChartSeries::enableFloatingValues(bool enabled) |
|
46 | 59 | { |
|
47 | 60 | emit floatingValuesEnabled(enabled); |
|
48 | 61 | } |
|
49 | 62 | |
|
50 |
void QBarChartSeries::enable |
|
|
63 | void QBarChartSeries::enableToolTip(bool enabled) | |
|
51 | 64 | { |
|
52 |
emit |
|
|
65 | emit toolTipEnabled(enabled); | |
|
66 | } | |
|
67 | ||
|
68 | void QBarChartSeries::enableSeparators(bool enabled) | |
|
69 | { | |
|
70 | emit separatorsEnabled(enabled); | |
|
53 | 71 | } |
|
54 | 72 | |
|
55 | 73 | int QBarChartSeries::countCategories() |
|
56 | 74 | { |
|
57 | 75 | return mModel->countCategories(); |
|
58 | 76 | } |
|
59 | 77 | |
|
60 | 78 | qreal QBarChartSeries::min() |
|
61 | 79 | { |
|
62 | 80 | return mModel->min(); |
|
63 | 81 | } |
|
64 | 82 | |
|
65 | 83 | qreal QBarChartSeries::max() |
|
66 | 84 | { |
|
67 | 85 | return mModel->max(); |
|
68 | 86 | } |
|
69 | 87 | |
|
70 | 88 | qreal QBarChartSeries::valueAt(int set, int category) |
|
71 | 89 | { |
|
72 | 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 | 103 | qreal QBarChartSeries::maxCategorySum() |
|
76 | 104 | { |
|
77 | 105 | return mModel->maxCategorySum(); |
|
78 | 106 | } |
|
79 | 107 | |
|
80 | 108 | BarChartModel& QBarChartSeries::model() |
|
81 | 109 | { |
|
82 | 110 | return *mModel; |
|
83 | 111 | } |
|
84 | 112 | |
|
85 | 113 | |
|
86 | 114 | #include "moc_qbarchartseries.cpp" |
|
87 | 115 | |
|
88 | 116 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,59 +1,66 | |||
|
1 | 1 | #ifndef BARCHARTSERIES_H |
|
2 | 2 | #define BARCHARTSERIES_H |
|
3 | 3 | |
|
4 | 4 | #include "qchartseries.h" |
|
5 | 5 | |
|
6 | 6 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
7 | 7 | |
|
8 | 8 | class QBarCategory; |
|
9 | 9 | class QBarSet; |
|
10 | 10 | class BarChartModel; |
|
11 | 11 | |
|
12 | 12 | // Container for series |
|
13 | 13 | class QTCOMMERCIALCHART_EXPORT QBarChartSeries : public QChartSeries |
|
14 | 14 | { |
|
15 | 15 | Q_OBJECT |
|
16 | 16 | public: |
|
17 | 17 | QBarChartSeries(QBarCategory *category, QObject* parent=0); |
|
18 | 18 | |
|
19 | 19 | virtual QChartSeriesType type() const { return QChartSeries::SeriesTypeBar; } |
|
20 | 20 | |
|
21 | 21 | void addBarSet(QBarSet *set); // Takes ownership of set |
|
22 | 22 | void removeBarSet(QBarSet *set); // Releases ownership, doesn't delete set |
|
23 | 23 | int countSets(); |
|
24 | 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 | 31 | // Disabled by default. Call these to change behavior. |
|
29 | void enableFloatingValues(bool enabled=true); | |
|
30 |
void enable |
|
|
32 | void enableFloatingValues(bool enabled=true); // enables floating values on top of bars | |
|
33 | void enableToolTip(bool enabled=true); // enables tooltips | |
|
34 | void enableSeparators(bool enabled=true); // enables separators between categories | |
|
31 | 35 | |
|
36 | public: | |
|
32 | 37 | // TODO: Functions below this are not part of api and will be moved |
|
33 | 38 | // to private implementation, when we start using it (not part of api) |
|
34 | 39 | int countCategories(); |
|
35 | 40 | qreal min(); |
|
36 | 41 | qreal max(); |
|
37 | 42 | qreal valueAt(int set, int category); |
|
43 | qreal percentageAt(int set, int category); | |
|
44 | ||
|
45 | qreal categorySum(int category); | |
|
38 | 46 | qreal maxCategorySum(); |
|
39 | 47 | |
|
40 | 48 | BarChartModel& model(); |
|
41 | 49 | |
|
42 | 50 | signals: |
|
43 | 51 | void changed(int index); |
|
44 | 52 | |
|
45 | // TODO: these to private implementation. | |
|
53 | // TODO: internal signals, these to private implementation. | |
|
46 | 54 | void floatingValuesEnabled(bool enabled); |
|
47 |
void |
|
|
48 | ||
|
49 | ||
|
50 | //public Q_SLOTS: | |
|
55 | void toolTipEnabled(bool enabled); | |
|
56 | void separatorsEnabled(bool enabled); | |
|
57 | void showToolTip(QPoint pos, QString tip); | |
|
51 | 58 | |
|
52 | 59 | protected: |
|
53 | 60 | BarChartModel* mModel; |
|
54 | 61 | |
|
55 | 62 | }; |
|
56 | 63 | |
|
57 | 64 | QTCOMMERCIALCHART_END_NAMESPACE |
|
58 | 65 | |
|
59 | 66 | #endif // BARCHARTSERIES_H |
@@ -1,103 +1,111 | |||
|
1 | 1 | #include "qbarset.h" |
|
2 | 2 | #include <QDebug> |
|
3 | 3 | #include <QToolTip> |
|
4 | 4 | |
|
5 | 5 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
6 | 6 | |
|
7 | 7 | QBarSet::QBarSet(QString name, QObject *parent) |
|
8 | 8 | : QObject(parent) |
|
9 | 9 | ,mName(name) |
|
10 |
,m |
|
|
10 | ,mToolTipEnabled(true) // TODO: these 2 as false by default, when implementation is ready | |
|
11 | 11 | ,mFloatingValuesEnabled(true) |
|
12 | 12 | { |
|
13 | 13 | } |
|
14 | 14 | |
|
15 | 15 | void QBarSet::setName(QString name) |
|
16 | 16 | { |
|
17 | 17 | mName = name; |
|
18 | 18 | } |
|
19 | 19 | QString QBarSet::name() |
|
20 | 20 | { |
|
21 | 21 | return mName; |
|
22 | 22 | } |
|
23 | 23 | |
|
24 | 24 | QBarSet& QBarSet::operator << (const qreal &value) |
|
25 | 25 | { |
|
26 | 26 | mValues.append(value); |
|
27 | 27 | return *this; |
|
28 | 28 | } |
|
29 | 29 | |
|
30 | 30 | int QBarSet::count() |
|
31 | 31 | { |
|
32 | 32 | return mValues.count(); |
|
33 | 33 | } |
|
34 | 34 | |
|
35 | 35 | qreal QBarSet::valueAt(int index) |
|
36 | 36 | { |
|
37 | 37 | return mValues.at(index); |
|
38 | 38 | } |
|
39 | 39 | |
|
40 | 40 | void QBarSet::setValue(int index, qreal value) |
|
41 | 41 | { |
|
42 | 42 | mValues.replace(index,value); |
|
43 | 43 | } |
|
44 | 44 | |
|
45 | 45 | void QBarSet::setPen(const QPen& pen) |
|
46 | 46 | { |
|
47 | 47 | mPen = pen; |
|
48 | 48 | } |
|
49 | 49 | |
|
50 | 50 | const QPen& QBarSet::pen() const |
|
51 | 51 | { |
|
52 | 52 | return mPen; |
|
53 | 53 | } |
|
54 | 54 | |
|
55 | 55 | void QBarSet::setBrush(const QBrush& brush) |
|
56 | 56 | { |
|
57 | 57 | mBrush = brush; |
|
58 | 58 | } |
|
59 | 59 | |
|
60 | 60 | const QBrush& QBarSet::brush() const |
|
61 | 61 | { |
|
62 | 62 | return mBrush; |
|
63 | 63 | } |
|
64 | 64 | |
|
65 | 65 | void QBarSet::enableFloatingValues(bool enabled) |
|
66 | 66 | { |
|
67 |
qDebug() << "QBarSet::enableFloatingValues" |
|
|
67 | qDebug() << "QBarSet::enableFloatingValues"; | |
|
68 | 68 | mFloatingValuesEnabled = enabled; |
|
69 | 69 | } |
|
70 | 70 | |
|
71 | 71 | void QBarSet::enableToolTip(bool enabled) |
|
72 | 72 | { |
|
73 |
qDebug() << "QBarSet::enable |
|
|
74 |
m |
|
|
73 | qDebug() << "QBarSet::enableToolTip"; | |
|
74 | mToolTipEnabled = enabled; | |
|
75 | } | |
|
76 | ||
|
77 | void QBarSet::enableSeparators(bool enabled) | |
|
78 | { | |
|
79 | qDebug() << "QBarSet::enableSeparators"; | |
|
80 | mSeparatorsEnabled = enabled; | |
|
75 | 81 | } |
|
76 | 82 | |
|
77 | 83 | void QBarSet::barClicked() |
|
78 | 84 | { |
|
79 | qDebug() << "QBarset::barClicked" << this; | |
|
85 | // qDebug() << "QBarset::barClicked" << this; | |
|
80 | 86 | // Some bar of this set has been clicked |
|
81 | 87 | // TODO: What happens then? |
|
82 | 88 | emit clicked(); // Notify that set has been clicked |
|
83 | 89 | } |
|
84 | 90 | |
|
85 | 91 | void QBarSet::barHoverEntered(QPoint pos) |
|
86 | 92 | { |
|
87 | qDebug() << "QBarset::barHoverEntered" << this << pos; | |
|
88 | if (mHoverNamesEnabled) { | |
|
89 | QToolTip::showText(pos, mName); | |
|
90 | // emit hoverEnter(); | |
|
93 | if (mToolTipEnabled) { | |
|
94 | emit showToolTip(pos, mName); | |
|
91 | 95 | } |
|
96 | // Emit signal to user of charts | |
|
97 | emit hoverEnter(pos); | |
|
92 | 98 | } |
|
93 | 99 | |
|
94 | 100 | void QBarSet::barHoverLeaved() |
|
95 | 101 | { |
|
96 | qDebug() << "QBarset::barHoverLeaved" << this; | |
|
97 |
if (m |
|
|
98 | // emit hoverLeave(); | |
|
99 | } | |
|
102 | // qDebug() << "QBarset::barHoverLeaved" << this; | |
|
103 | // if (mToolTipEnabled) { | |
|
104 | // TODO: do what? | |
|
105 | // } | |
|
106 | // Emit signal to user of charts | |
|
107 | emit hoverLeave(); | |
|
100 | 108 | } |
|
101 | 109 | |
|
102 | 110 | #include "moc_qbarset.cpp" |
|
103 | 111 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,61 +1,63 | |||
|
1 | 1 | #ifndef QBARSET_H |
|
2 | 2 | #define QBARSET_H |
|
3 | 3 | |
|
4 | 4 | #include <qchartglobal.h> |
|
5 | 5 | #include <QPen> |
|
6 | 6 | #include <QBrush> |
|
7 | 7 | |
|
8 | 8 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
9 | 9 | |
|
10 | 10 | class QTCOMMERCIALCHART_EXPORT QBarSet : public QObject |
|
11 | 11 | { |
|
12 | 12 | Q_OBJECT |
|
13 | 13 | public: |
|
14 | 14 | QBarSet(QString name, QObject *parent = 0); |
|
15 | 15 | |
|
16 | 16 | void setName(QString name); |
|
17 | 17 | QString name(); |
|
18 | 18 | QBarSet& operator << (const qreal &value); // appends new value to set |
|
19 | 19 | |
|
20 | 20 | int count(); // count of values in set |
|
21 | 21 | qreal valueAt(int index); // for modifying individual values |
|
22 | 22 | void setValue(int index, qreal value); // setter for individual value |
|
23 | 23 | |
|
24 | 24 | void setPen(const QPen& pen); |
|
25 | 25 | const QPen& pen() const; |
|
26 | 26 | |
|
27 | 27 | void setBrush(const QBrush& brush); |
|
28 | 28 | const QBrush& brush() const; |
|
29 | 29 | |
|
30 | 30 | Q_SIGNALS: |
|
31 | 31 | void clicked(); // Clicked and hover signals exposed to user |
|
32 | void hoverEnter(); | |
|
32 | void hoverEnter(QPoint pos); | |
|
33 | 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 | 37 | public Q_SLOTS: |
|
37 | // TODO: should these be in series instead? | |
|
38 | 38 | void enableFloatingValues(bool enabled); // enables floating values on top of bars |
|
39 | 39 | void enableToolTip(bool enabled); // enables tooltips |
|
40 | void enableSeparators(bool enabled); // enables separators between categories | |
|
40 | 41 | |
|
41 | 42 | // TODO: these slots belong to private implementation. |
|
42 | 43 | // These are for single bars to notify set about internal events |
|
43 | 44 | void barClicked(); |
|
44 | 45 | void barHoverEntered(QPoint pos); |
|
45 | 46 | void barHoverLeaved(); |
|
46 | 47 | |
|
47 | 48 | private: |
|
48 | 49 | |
|
49 | 50 | QString mName; |
|
50 | 51 | QList<qreal> mValues; |
|
51 | 52 | QPen mPen; |
|
52 | 53 | QBrush mBrush; |
|
53 | 54 | |
|
54 | 55 | // TODO: to pimpl |
|
55 | 56 | bool mFloatingValuesEnabled; |
|
56 |
bool m |
|
|
57 | bool mToolTipEnabled; | |
|
58 | bool mSeparatorsEnabled; | |
|
57 | 59 | }; |
|
58 | 60 | |
|
59 | 61 | QTCOMMERCIALCHART_END_NAMESPACE |
|
60 | 62 | |
|
61 | 63 | #endif // QBARSET_H |
@@ -1,109 +1,111 | |||
|
1 | 1 | #include "stackedbarpresenter.h" |
|
2 | 2 | #include "bar_p.h" |
|
3 | 3 | #include "barlabel_p.h" |
|
4 | 4 | #include "barvalue_p.h" |
|
5 | 5 | #include "separator_p.h" |
|
6 | 6 | #include "qbarset.h" |
|
7 | 7 | #include <QDebug> |
|
8 | 8 | |
|
9 | 9 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
10 | 10 | |
|
11 |
StackedBarPresenter::StackedBarPresenter(BarChart |
|
|
12 |
BarPresenterBase( |
|
|
11 | StackedBarPresenter::StackedBarPresenter(QBarChartSeries *series, QGraphicsItem *parent) : | |
|
12 | BarPresenterBase(series,parent) | |
|
13 | 13 | { |
|
14 | 14 | } |
|
15 | 15 | |
|
16 | 16 | void StackedBarPresenter::layoutChanged() |
|
17 | 17 | { |
|
18 | 18 | // Scale bars to new layout |
|
19 | 19 | // Layout for bars: |
|
20 |
if (m |
|
|
20 | if (mSeries->countSets() <= 0) { | |
|
21 | 21 | qDebug() << "No sets in model!"; |
|
22 | 22 | // Nothing to do. |
|
23 | 23 | return; |
|
24 | 24 | } |
|
25 | 25 | |
|
26 |
if (m |
|
|
26 | if (mSeries->countCategories() == 0) { | |
|
27 | 27 | qDebug() << "No categories in model!"; |
|
28 | 28 | // Nothing to do |
|
29 | 29 | return; |
|
30 | 30 | } |
|
31 | 31 | |
|
32 | 32 | if (childItems().count() == 0) { |
|
33 | 33 | qDebug() << "WARNING: StackedBarPresenter::layoutChanged called before graphics items are created!"; |
|
34 | 34 | return; |
|
35 | 35 | } |
|
36 | 36 | |
|
37 | 37 | // TODO: better way to auto-layout |
|
38 | 38 | // Use reals for accurancy (we might get some compiler warnings... :) |
|
39 | 39 | // TODO: use temp variable for category count... |
|
40 |
qreal maxSum = m |
|
|
40 | qreal maxSum = mSeries->maxCategorySum(); | |
|
41 | 41 | qreal h = mHeight; |
|
42 | 42 | qreal scale = (h / maxSum); |
|
43 | 43 | |
|
44 | 44 | int itemIndex(0); |
|
45 | 45 | int labelIndex(0); |
|
46 | 46 | qreal tW = mWidth; |
|
47 |
qreal tC = m |
|
|
47 | qreal tC = mSeries->countCategories() + 1; | |
|
48 | 48 | qreal xStep = (tW/tC); |
|
49 | 49 | qreal xPos = ((tW/tC) - mBarDefaultWidth / 2); |
|
50 | 50 | |
|
51 |
for (int category = 0; category < m |
|
|
51 | for (int category = 0; category < mSeries->countCategories(); category++) { | |
|
52 | 52 | qreal yPos = h; |
|
53 |
for (int set=0; set < m |
|
|
54 |
qreal barHeight = m |
|
|
53 | for (int set=0; set < mSeries->countSets(); set++) { | |
|
54 | qreal barHeight = mSeries->valueAt(set, category) * scale; | |
|
55 | 55 | Bar* bar = mBars.at(itemIndex); |
|
56 | 56 | |
|
57 | 57 | bar->resize(mBarDefaultWidth, barHeight); |
|
58 |
bar->setBrush(m |
|
|
58 | bar->setBrush(mSeries->setAt(set)->brush()); | |
|
59 | 59 | bar->setPos(xPos, yPos-barHeight); |
|
60 | 60 | itemIndex++; |
|
61 | 61 | yPos -= barHeight; |
|
62 | 62 | } |
|
63 | 63 | |
|
64 | 64 | // TODO: Layout for labels, remove magic number |
|
65 | 65 | BarLabel* label = mLabels.at(labelIndex); |
|
66 | 66 | label->setPos(xPos, mHeight + 20); |
|
67 | 67 | labelIndex++; |
|
68 | 68 | xPos += xStep; |
|
69 | 69 | } |
|
70 | 70 | |
|
71 | 71 | // Position separators |
|
72 | 72 | xPos = xStep + xStep/2; |
|
73 |
for (int s=0; s < m |
|
|
73 | for (int s=0; s < mSeries->countCategories() - 1; s++) { | |
|
74 | 74 | Separator* sep = mSeparators.at(s); |
|
75 | 75 | sep->setPos(xPos,0); |
|
76 | 76 | sep->setSize(QSizeF(1,mHeight)); |
|
77 | 77 | xPos += xStep; |
|
78 | 78 | } |
|
79 | 79 | |
|
80 | 80 | // Position floating values |
|
81 | 81 | itemIndex = 0; |
|
82 | 82 | xPos = ((tW/tC) - mBarDefaultWidth / 2); |
|
83 |
for (int category=0; category < m |
|
|
83 | for (int category=0; category < mSeries->countCategories(); category++) { | |
|
84 | 84 | qreal yPos = h; |
|
85 |
for (int set=0; set < m |
|
|
86 |
qreal barHeight = m |
|
|
85 | for (int set=0; set < mSeries->countSets(); set++) { | |
|
86 | qreal barHeight = mSeries->valueAt(set,category) * scale; | |
|
87 | 87 | BarValue* value = mFloatingValues.at(itemIndex); |
|
88 | 88 | |
|
89 | 89 | // TODO: remove hard coding, apply layout |
|
90 | 90 | value->resize(100,50); |
|
91 | 91 | value->setPos(xPos, yPos-barHeight/2); |
|
92 | 92 | value->setPen(QPen(QColor(255,255,255,255))); |
|
93 | 93 | |
|
94 |
if (m |
|
|
95 |
value->setValueString(QString::number(m |
|
|
94 | if (mSeries->valueAt(set,category) != 0) { | |
|
95 | value->setValueString(QString::number(mSeries->valueAt(set,category))); | |
|
96 | 96 | } else { |
|
97 | 97 | value->setValueString(QString("")); |
|
98 | 98 | } |
|
99 | 99 | |
|
100 | 100 | itemIndex++; |
|
101 | 101 | yPos -= barHeight; |
|
102 | 102 | } |
|
103 | 103 | xPos += xStep; |
|
104 | 104 | } |
|
105 | 105 | |
|
106 | 106 | mLayoutDirty = true; |
|
107 | 107 | } |
|
108 | 108 | |
|
109 | #include "moc_stackedbarpresenter.cpp" | |
|
110 | ||
|
109 | 111 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,26 +1,27 | |||
|
1 | 1 | #ifndef STACKEDBARPRESENTER_H |
|
2 | 2 | #define STACKEDBARPRESENTER_H |
|
3 | 3 | |
|
4 | 4 | #include "barpresenterbase.h" |
|
5 | 5 | #include "qstackedbarchartseries.h" |
|
6 | 6 | #include <QGraphicsItem> |
|
7 | 7 | |
|
8 | 8 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
9 | 9 | |
|
10 | 10 | class StackedBarPresenter : public BarPresenterBase |
|
11 | 11 | { |
|
12 | Q_OBJECT | |
|
12 | 13 | public: |
|
13 |
StackedBarPresenter(BarChart |
|
|
14 | StackedBarPresenter(QBarChartSeries *series, QGraphicsItem *parent = 0); | |
|
14 | 15 | |
|
15 | 16 | private: |
|
16 | 17 | // From BarPresenterBase |
|
17 | 18 | void layoutChanged(); // layout has changed -> need to recalculate bar sizes |
|
18 | 19 | |
|
19 | 20 | private: |
|
20 | 21 | |
|
21 | 22 | // Data |
|
22 | 23 | }; |
|
23 | 24 | |
|
24 | 25 | QTCOMMERCIALCHART_END_NAMESPACE |
|
25 | 26 | |
|
26 | 27 | #endif // STACKEDBARPRESENTER_H |
@@ -1,226 +1,226 | |||
|
1 | 1 | #include "qchart.h" |
|
2 | 2 | #include "qchartaxis.h" |
|
3 | 3 | #include "chartpresenter_p.h" |
|
4 | 4 | #include "chartdataset_p.h" |
|
5 | 5 | #include "charttheme_p.h" |
|
6 | 6 | //series |
|
7 | 7 | #include "qbarchartseries.h" |
|
8 | 8 | #include "qstackedbarchartseries.h" |
|
9 | 9 | #include "qpercentbarchartseries.h" |
|
10 | 10 | #include "qlinechartseries.h" |
|
11 | 11 | #include "qpieseries.h" |
|
12 | 12 | #include "qscatterseries.h" |
|
13 | 13 | //items |
|
14 | 14 | #include "axisitem_p.h" |
|
15 | 15 | #include "barpresenter.h" |
|
16 | 16 | #include "stackedbarpresenter.h" |
|
17 | 17 | #include "linechartitem_p.h" |
|
18 | 18 | #include "percentbarpresenter.h" |
|
19 | 19 | #include "linechartanimationitem_p.h" |
|
20 | 20 | #include "piepresenter.h" |
|
21 | 21 | #include "scatterpresenter_p.h" |
|
22 | 22 | |
|
23 | 23 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
24 | 24 | |
|
25 | 25 | ChartPresenter::ChartPresenter(QChart* chart,ChartDataSet* dataset):QObject(chart), |
|
26 | 26 | m_chart(chart), |
|
27 | 27 | m_dataset(dataset), |
|
28 | 28 | m_chartTheme(0), |
|
29 | 29 | m_marginSize(0), |
|
30 | 30 | m_rect(QRectF(QPoint(0,0),m_chart->size())) |
|
31 | 31 | { |
|
32 | 32 | createConnections(); |
|
33 | 33 | setChartTheme(QChart::ChartThemeDefault); |
|
34 | 34 | |
|
35 | 35 | } |
|
36 | 36 | |
|
37 | 37 | ChartPresenter::~ChartPresenter() |
|
38 | 38 | { |
|
39 | 39 | } |
|
40 | 40 | |
|
41 | 41 | void ChartPresenter::createConnections() |
|
42 | 42 | { |
|
43 | 43 | QObject::connect(m_chart,SIGNAL(geometryChanged()),this,SLOT(handleGeometryChanged())); |
|
44 | 44 | QObject::connect(m_dataset,SIGNAL(seriesAdded(QChartSeries*)),this,SLOT(handleSeriesAdded(QChartSeries*))); |
|
45 | 45 | QObject::connect(m_dataset,SIGNAL(seriesRemoved(QChartSeries*)),this,SLOT(handleSeriesRemoved(QChartSeries*))); |
|
46 | 46 | QObject::connect(m_dataset,SIGNAL(axisAdded(QChartAxis*)),this,SLOT(handleAxisAdded(QChartAxis*))); |
|
47 | 47 | QObject::connect(m_dataset,SIGNAL(axisRemoved(QChartAxis*)),this,SLOT(handleAxisRemoved(QChartAxis*))); |
|
48 | 48 | QObject::connect(m_dataset,SIGNAL(seriesDomainChanged(QChartSeries*,const Domain&)),this,SLOT(handleSeriesDomainChanged(QChartSeries*,const Domain&))); |
|
49 | 49 | QObject::connect(m_dataset,SIGNAL(axisLabelsChanged(QChartAxis*,const QStringList&)),this,SLOT(handleAxisLabelsChanged(QChartAxis*,const QStringList&))); |
|
50 | 50 | } |
|
51 | 51 | |
|
52 | 52 | |
|
53 | 53 | QRectF ChartPresenter::geometry() const |
|
54 | 54 | { |
|
55 | 55 | return m_rect; |
|
56 | 56 | } |
|
57 | 57 | |
|
58 | 58 | void ChartPresenter::handleGeometryChanged() |
|
59 | 59 | { |
|
60 | 60 | m_rect = QRectF(QPoint(0,0),m_chart->size()); |
|
61 | 61 | m_rect.adjust(m_marginSize,m_marginSize, -m_marginSize, -m_marginSize); |
|
62 | 62 | Q_ASSERT(m_rect.isValid()); |
|
63 | 63 | emit geometryChanged(m_rect); |
|
64 | 64 | } |
|
65 | 65 | |
|
66 | 66 | int ChartPresenter::margin() const |
|
67 | 67 | { |
|
68 | 68 | return m_marginSize; |
|
69 | 69 | } |
|
70 | 70 | |
|
71 | 71 | void ChartPresenter::setMargin(int margin) |
|
72 | 72 | { |
|
73 | 73 | m_marginSize = margin; |
|
74 | 74 | } |
|
75 | 75 | |
|
76 | 76 | void ChartPresenter::handleAxisAdded(QChartAxis* axis) |
|
77 | 77 | { |
|
78 | 78 | AxisItem* item ; |
|
79 | 79 | |
|
80 | 80 | if(axis==m_dataset->axisX()){ |
|
81 | 81 | item = new AxisItem(AxisItem::X_AXIS,m_chart); |
|
82 | 82 | }else{ |
|
83 | 83 | item = new AxisItem(AxisItem::Y_AXIS,m_chart); |
|
84 | 84 | } |
|
85 | 85 | QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&))); |
|
86 | 86 | QObject::connect(axis,SIGNAL(update(QChartAxis*)),item,SLOT(handleAxisUpdate(QChartAxis*))); |
|
87 | 87 | |
|
88 | 88 | item->handleAxisUpdate(axis); |
|
89 | 89 | item->handleGeometryChanged(m_rect); |
|
90 | 90 | m_chartTheme->decorate(axis,item); |
|
91 | 91 | m_axisItems.insert(axis,item); |
|
92 | 92 | } |
|
93 | 93 | |
|
94 | 94 | void ChartPresenter::handleAxisRemoved(QChartAxis* axis) |
|
95 | 95 | { |
|
96 | 96 | AxisItem* item = m_axisItems.take(axis); |
|
97 | 97 | Q_ASSERT(item); |
|
98 | 98 | delete item; |
|
99 | 99 | } |
|
100 | 100 | |
|
101 | 101 | |
|
102 | 102 | void ChartPresenter::handleSeriesAdded(QChartSeries* series) |
|
103 | 103 | { |
|
104 | 104 | switch(series->type()) |
|
105 | 105 | { |
|
106 | 106 | case QChartSeries::SeriesTypeLine: { |
|
107 | 107 | QLineChartSeries* lineSeries = static_cast<QLineChartSeries*>(series); |
|
108 | 108 | LineChartItem* item = new LineChartAnimationItem(this,lineSeries,m_chart); |
|
109 | 109 | m_chartTheme->decorate(item,lineSeries,m_chartItems.count()); |
|
110 | 110 | QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&))); |
|
111 | 111 | QObject::connect(lineSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int))); |
|
112 | 112 | m_chartItems.insert(series,item); |
|
113 | 113 | break; |
|
114 | 114 | } |
|
115 | 115 | |
|
116 | 116 | case QChartSeries::SeriesTypeBar: { |
|
117 | 117 | QBarChartSeries* barSeries = static_cast<QBarChartSeries*>(series); |
|
118 |
BarPresenter* item = new BarPresenter(barSeries |
|
|
118 | BarPresenter* item = new BarPresenter(barSeries,m_chart); | |
|
119 | 119 | m_chartTheme->decorate(item,barSeries,m_chartItems.count()); |
|
120 | 120 | QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&))); |
|
121 | 121 | QObject::connect(barSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int))); |
|
122 | 122 | m_chartItems.insert(series,item); |
|
123 | 123 | // m_axisXItem->setVisible(false); |
|
124 | 124 | break; |
|
125 | 125 | } |
|
126 | 126 | |
|
127 | 127 | case QChartSeries::SeriesTypeStackedBar: { |
|
128 | 128 | |
|
129 | 129 | QStackedBarChartSeries* stackedBarSeries = static_cast<QStackedBarChartSeries*>(series); |
|
130 |
StackedBarPresenter* item = new StackedBarPresenter(stackedBarSeries |
|
|
130 | StackedBarPresenter* item = new StackedBarPresenter(stackedBarSeries,m_chart); | |
|
131 | 131 | m_chartTheme->decorate(item,stackedBarSeries,m_chartItems.count()); |
|
132 | 132 | QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&))); |
|
133 | 133 | QObject::connect(stackedBarSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int))); |
|
134 | 134 | m_chartItems.insert(series,item); |
|
135 | 135 | break; |
|
136 | 136 | } |
|
137 | 137 | |
|
138 | 138 | case QChartSeries::SeriesTypePercentBar: { |
|
139 | 139 | |
|
140 | 140 | QPercentBarChartSeries* percentBarSeries = static_cast<QPercentBarChartSeries*>(series); |
|
141 |
PercentBarPresenter* item = new PercentBarPresenter(percentBarSeries |
|
|
141 | PercentBarPresenter* item = new PercentBarPresenter(percentBarSeries,m_chart); | |
|
142 | 142 | m_chartTheme->decorate(item,percentBarSeries ,m_chartItems.count()); |
|
143 | 143 | QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&))); |
|
144 | 144 | QObject::connect(percentBarSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int))); |
|
145 | 145 | m_chartItems.insert(series,item); |
|
146 | 146 | break; |
|
147 | 147 | } |
|
148 | 148 | case QChartSeries::SeriesTypeScatter: { |
|
149 | 149 | QScatterSeries *scatterSeries = qobject_cast<QScatterSeries *>(series); |
|
150 | 150 | ScatterPresenter *scatterPresenter = new ScatterPresenter(scatterSeries, m_chart); |
|
151 | 151 | QObject::connect(this, SIGNAL(geometryChanged(const QRectF&)), |
|
152 | 152 | scatterPresenter, SLOT(handleGeometryChanged(const QRectF&))); |
|
153 | 153 | m_chartTheme->decorate(scatterPresenter, scatterSeries, m_chartItems.count()); |
|
154 | 154 | m_chartItems.insert(scatterSeries, scatterPresenter); |
|
155 | 155 | break; |
|
156 | 156 | } |
|
157 | 157 | case QChartSeries::SeriesTypePie: { |
|
158 | 158 | QPieSeries *s = qobject_cast<QPieSeries *>(series); |
|
159 | 159 | PiePresenter* pie = new PiePresenter(m_chart, s); |
|
160 | 160 | m_chartTheme->decorate(pie, s, m_chartItems.count()); |
|
161 | 161 | QObject::connect(this, SIGNAL(geometryChanged(const QRectF&)), pie, SLOT(handleGeometryChanged(const QRectF&))); |
|
162 | 162 | m_chartItems.insert(series, pie); |
|
163 | 163 | break; |
|
164 | 164 | } |
|
165 | 165 | default: { |
|
166 | 166 | qDebug()<< "Series type" << series->type() << "not implemented."; |
|
167 | 167 | break; |
|
168 | 168 | } |
|
169 | 169 | } |
|
170 | 170 | |
|
171 | 171 | if(m_rect.isValid()) emit geometryChanged(m_rect); |
|
172 | 172 | } |
|
173 | 173 | |
|
174 | 174 | void ChartPresenter::handleSeriesRemoved(QChartSeries* series) |
|
175 | 175 | { |
|
176 | 176 | ChartItem* item = m_chartItems.take(series); |
|
177 | 177 | delete item; |
|
178 | 178 | } |
|
179 | 179 | |
|
180 | 180 | void ChartPresenter::handleSeriesChanged(QChartSeries* series) |
|
181 | 181 | { |
|
182 | 182 | //TODO: |
|
183 | 183 | } |
|
184 | 184 | |
|
185 | 185 | void ChartPresenter::handleSeriesDomainChanged(QChartSeries* series, const Domain& domain) |
|
186 | 186 | { |
|
187 | 187 | m_chartItems.value(series)->handleDomainChanged(domain); |
|
188 | 188 | } |
|
189 | 189 | |
|
190 | 190 | void ChartPresenter::handleAxisLabelsChanged(QChartAxis* axis,const QStringList& labels) |
|
191 | 191 | { |
|
192 | 192 | m_axisItems.value(axis)->handleLabelsChanged(axis,labels); |
|
193 | 193 | } |
|
194 | 194 | |
|
195 | 195 | void ChartPresenter::setChartTheme(QChart::ChartTheme theme) |
|
196 | 196 | { |
|
197 | 197 | delete m_chartTheme; |
|
198 | 198 | |
|
199 | 199 | m_chartTheme = ChartTheme::createTheme(theme); |
|
200 | 200 | |
|
201 | 201 | m_chartTheme->decorate(m_chart); |
|
202 | 202 | QMapIterator<QChartSeries*,ChartItem*> i(m_chartItems); |
|
203 | 203 | |
|
204 | 204 | int index=0; |
|
205 | 205 | while (i.hasNext()) { |
|
206 | 206 | i.next(); |
|
207 | 207 | index++; |
|
208 | 208 | m_chartTheme->decorate(i.value(),i.key(),index); |
|
209 | 209 | } |
|
210 | 210 | |
|
211 | 211 | QMapIterator<QChartAxis*,AxisItem*> j(m_axisItems); |
|
212 | 212 | while (j.hasNext()) { |
|
213 | 213 | j.next(); |
|
214 | 214 | m_chartTheme->decorate(j.key(),j.value()); |
|
215 | 215 | } |
|
216 | 216 | } |
|
217 | 217 | |
|
218 | 218 | QChart::ChartTheme ChartPresenter::chartTheme() |
|
219 | 219 | { |
|
220 | 220 | return m_chartTheme->id(); |
|
221 | 221 | } |
|
222 | 222 | |
|
223 | 223 | |
|
224 | 224 | #include "moc_chartpresenter_p.cpp" |
|
225 | 225 | |
|
226 | 226 | QTCOMMERCIALCHART_END_NAMESPACE |
General Comments 0
You need to be logged in to leave comments.
Login now