##// END OF EJS Templates
Fixed layout for barcharts
sauimone -
r473:8959911f7807
parent child
Show More
@@ -10,7 +10,7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
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()
@@ -27,8 +27,7 void BarPresenter::layoutChanged()
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
@@ -37,38 +36,38 void BarPresenter::layoutChanged()
37 36 qreal tM = mSeries->max();
38 37 qreal scale = (tH/tM);
39 38 qreal tC = categoryCount+1;
40 qreal xStepPerSet = (tW/tC);
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;
@@ -86,10 +85,9 void BarPresenter::layoutChanged()
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"
@@ -12,14 +12,12 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
@@ -34,12 +32,9 void BarPresenterBase::paint(QPainter *painter, const QStyleOptionGraphicsItem *
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 35 foreach(QGraphicsItem* i, childItems()) {
40 36 i->paint(painter,option,widget);
41 37 }
42 // }
43 38 }
44 39
45 40 QRectF BarPresenterBase::boundingRect() const
@@ -49,13 +44,12 QRectF BarPresenterBase::boundingRect() const
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;
@@ -110,9 +104,6 void BarPresenterBase::dataChanged()
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
@@ -49,17 +49,13 protected:
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;
@@ -21,12 +21,12 QString BarValue::valueString()
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 }
@@ -20,8 +20,8 public:
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);
@@ -29,17 +29,17 void PercentBarPresenter::layoutChanged()
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);
@@ -49,7 +49,7 void PercentBarPresenter::layoutChanged()
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++;
@@ -74,7 +74,7 void PercentBarPresenter::layoutChanged()
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);
@@ -103,8 +103,6 void PercentBarPresenter::layoutChanged()
103 103 }
104 104 xPos += xStep;
105 105 }
106
107 mLayoutDirty = true;
108 106 }
109 107
110 108 #include "moc_percentbarpresenter_p.cpp"
@@ -122,7 +122,7 qreal QBarSet::total()
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 }
@@ -130,7 +130,7 void QBarSet::setPen(QPen pen)
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 }
@@ -138,7 +138,7 QPen QBarSet::pen()
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 }
@@ -146,7 +146,7 void QBarSet::setBrush(QBrush brush)
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 }
@@ -27,11 +27,11 public:
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
@@ -39,27 +39,26 void StackedBarPresenter::layoutChanged()
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++;
@@ -84,7 +83,7 void StackedBarPresenter::layoutChanged()
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++) {
@@ -107,8 +106,6 void StackedBarPresenter::layoutChanged()
107 106 }
108 107 xPos += xStep;
109 108 }
110
111 mLayoutDirty = true;
112 109 }
113 110
114 111 #include "moc_stackedbarpresenter_p.cpp"
General Comments 0
You need to be logged in to leave comments. Login now