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