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