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