##// END OF EJS Templates
fixed clipping in barcharts
sauimone -
r839:27902acc0ab1
parent child
Show More
@@ -29,8 +29,8
29 #include "chartpresenter_p.h"
29 #include "chartpresenter_p.h"
30 #include "chartanimator_p.h"
30 #include "chartanimator_p.h"
31 #include "chartdataset_p.h"
31 #include "chartdataset_p.h"
32 #include <QDebug>
33 #include <QToolTip>
32 #include <QToolTip>
33 #include <QPainter>
34
34
35 QTCOMMERCIALCHART_BEGIN_NAMESPACE
35 QTCOMMERCIALCHART_BEGIN_NAMESPACE
36
36
@@ -49,6 +49,7 BarChartItem::BarChartItem(QBarSeries *series, ChartPresenter *presenter) :
49 BarChartItem::~BarChartItem()
49 BarChartItem::~BarChartItem()
50 {
50 {
51 disconnect(this,SLOT(showToolTip(QPoint,QString)));
51 disconnect(this,SLOT(showToolTip(QPoint,QString)));
52 deleteItems();
52 }
53 }
53
54
54 void BarChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
55 void BarChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
@@ -58,21 +59,25 void BarChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *opti
58 return;
59 return;
59 }
60 }
60
61
61 foreach(QGraphicsItem* i, childItems())
62 painter->save();
62 i->paint(painter,option,widget);
63 painter->setClipRect(m_clipRect);
64 foreach (Bar *bar, m_bars)
65 bar->paint(painter,option,widget);
66 foreach (BarLabel* label, m_labels)
67 label->paint(painter,option,widget);
68 painter->restore();
63 }
69 }
64
70
65 QRectF BarChartItem::boundingRect() const
71 QRectF BarChartItem::boundingRect() const
66 {
72 {
67 return m_rect;
73 return m_rect.translated(-m_rect.topLeft());
74 // return m_rect;
68 }
75 }
69
76
70 void BarChartItem::dataChanged()
77 void BarChartItem::dataChanged()
71 {
78 {
72 // TODO: performance optimizations. Do we really need to delete and create items every time data is changed or can we reuse them?
79 // TODO: performance optimizations. Do we really need to delete and create items every time data is changed or can we reuse them?
73 // Delete old bars
80 deleteItems();
74 foreach (QGraphicsItem *item, childItems())
75 delete item;
76
81
77 m_bars.clear();
82 m_bars.clear();
78 m_labels.clear();
83 m_labels.clear();
@@ -83,8 +88,7 void BarChartItem::dataChanged()
83 QString category = m_series->categoryName(c);
88 QString category = m_series->categoryName(c);
84 for (int s = 0; s < m_series->barsetCount(); s++) {
89 for (int s = 0; s < m_series->barsetCount(); s++) {
85 QBarSet *set = m_series->barsetAt(s);
90 QBarSet *set = m_series->barsetAt(s);
86 Bar *bar = new Bar(category,this);
91 Bar *bar = new Bar(category,0); // Null parent is best :)
87 childItems().append(bar);
88 m_bars.append(bar);
92 m_bars.append(bar);
89 connect(bar, SIGNAL(clicked(QString,Qt::MouseButtons)), set, SIGNAL(clicked(QString,Qt::MouseButtons)));
93 connect(bar, SIGNAL(clicked(QString,Qt::MouseButtons)), set, SIGNAL(clicked(QString,Qt::MouseButtons)));
90 connect(bar, SIGNAL(hoverEntered(QPoint)), set, SLOT(barHoverEnterEvent(QPoint)));
94 connect(bar, SIGNAL(hoverEntered(QPoint)), set, SLOT(barHoverEnterEvent(QPoint)));
@@ -97,8 +101,7 void BarChartItem::dataChanged()
97 for (int category = 0; category < m_series->categoryCount(); category++) {
101 for (int category = 0; category < m_series->categoryCount(); category++) {
98 for (int s = 0; s < m_series->barsetCount(); s++) {
102 for (int s = 0; s < m_series->barsetCount(); s++) {
99 QBarSet *set = m_series->barsetAt(s);
103 QBarSet *set = m_series->barsetAt(s);
100 BarLabel *value = new BarLabel(*set, this);
104 BarLabel *value = new BarLabel(*set, 0);
101 childItems().append(value);
102 m_labels.append(value);
105 m_labels.append(value);
103 connect(set,SIGNAL(labelsVisibleChanged(bool)),value,SLOT(labelsVisibleChanged(bool)));
106 connect(set,SIGNAL(labelsVisibleChanged(bool)),value,SLOT(labelsVisibleChanged(bool)));
104 }
107 }
@@ -152,7 +155,7 QVector<QRectF> BarChartItem::calculateLayout()
152
155
153 label->setPos(xPos + (rect.width()/2 - label->boundingRect().width()/2)
156 label->setPos(xPos + (rect.width()/2 - label->boundingRect().width()/2)
154 ,yPos - barHeight/2 - label->boundingRect().height()/2);
157 ,yPos - barHeight/2 - label->boundingRect().height()/2);
155 // value->setFont(barSet->valueFont());
158 label->setFont(barSet->labelFont());
156
159
157 itemIndex++;
160 itemIndex++;
158 xPos += barWidth;
161 xPos += barWidth;
@@ -179,6 +182,14 void BarChartItem::setLayout(const QVector<QRectF> &layout)
179 update();
182 update();
180 }
183 }
181
184
185 void BarChartItem::deleteItems()
186 {
187 foreach (Bar *bar, m_bars)
188 delete bar;
189 foreach (BarLabel* label, m_labels)
190 delete label;
191 }
192
182 //handlers
193 //handlers
183
194
184 void BarChartItem::handleModelChanged(int index)
195 void BarChartItem::handleModelChanged(int index)
@@ -198,6 +209,7 void BarChartItem::handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal
198
209
199 void BarChartItem::handleGeometryChanged(const QRectF &rect)
210 void BarChartItem::handleGeometryChanged(const QRectF &rect)
200 {
211 {
212 m_clipRect = rect.translated(-rect.topLeft());
201 m_rect = rect;
213 m_rect = rect;
202 handleLayoutChanged();
214 handleLayoutChanged();
203 m_layoutSet = true;
215 m_layoutSet = true;
@@ -57,6 +57,9 public:
57
57
58 QRectF geometry() const { return m_rect;}
58 QRectF geometry() const { return m_rect;}
59
59
60 private:
61 void deleteItems();
62
60 public Q_SLOTS:
63 public Q_SLOTS:
61 void handleModelChanged(int index);
64 void handleModelChanged(int index);
62 void handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY);
65 void handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY);
@@ -75,6 +78,7 protected:
75 qreal m_domainMaxY;
78 qreal m_domainMaxY;
76
79
77 QRectF m_rect;
80 QRectF m_rect;
81 QRectF m_clipRect;
78 bool m_layoutSet; // True, if component has been laid out.
82 bool m_layoutSet; // True, if component has been laid out.
79 QVector<QRectF> m_layout;
83 QVector<QRectF> m_layout;
80
84
@@ -22,7 +22,6
22 #include "bar_p.h"
22 #include "bar_p.h"
23 #include "barlabel_p.h"
23 #include "barlabel_p.h"
24 #include "qbarset.h"
24 #include "qbarset.h"
25 #include <QDebug>
26
25
27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
26 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28
27
@@ -35,7 +34,7 QVector<QRectF> PercentBarChartItem::calculateLayout()
35 {
34 {
36 QVector<QRectF> layout;
35 QVector<QRectF> layout;
37
36
38 // Use temporary qreals for accurancy (we might get some compiler warnings... :)
37 // Use temporary qreals for accurancy
39 qreal width = geometry().width();
38 qreal width = geometry().width();
40 qreal height = geometry().height();
39 qreal height = geometry().height();
41
40
@@ -72,7 +71,7 QVector<QRectF> PercentBarChartItem::calculateLayout()
72
71
73 label->setPos(xPos + (rect.width()/2 - label->boundingRect().width()/2)
72 label->setPos(xPos + (rect.width()/2 - label->boundingRect().width()/2)
74 ,yPos - barHeight/2 - label->boundingRect().height()/2);
73 ,yPos - barHeight/2 - label->boundingRect().height()/2);
75 // value->setFont(barSet->valueFont());
74 label->setFont(barSet->labelFont());
76 itemIndex++;
75 itemIndex++;
77 yPos -= barHeight;
76 yPos -= barHeight;
78 }
77 }
@@ -391,6 +391,9 QBarCategories QBarSeries::categories() const
391 return categories;
391 return categories;
392 }
392 }
393
393
394 /*!
395 Sets the visibility of labels in series to \a visible
396 */
394 void QBarSeries::setLabelsVisible(bool visible)
397 void QBarSeries::setLabelsVisible(bool visible)
395 {
398 {
396 foreach (QBarSet* s, barSets()) {
399 foreach (QBarSet* s, barSets()) {
@@ -184,7 +184,7 QBrush QBarSet::brush() const
184 }
184 }
185
185
186 /*!
186 /*!
187 Sets pen of the values that are drawn on top of this barset
187 Sets \a pen of the values that are drawn on top of this barset
188 */
188 */
189 void QBarSet::setLabelPen(const QPen &pen)
189 void QBarSet::setLabelPen(const QPen &pen)
190 {
190 {
@@ -201,7 +201,7 QPen QBarSet::labelPen() const
201 }
201 }
202
202
203 /*!
203 /*!
204 Sets brush of the values that are drawn on top of this barset
204 Sets \a brush of the values that are drawn on top of this barset
205 */
205 */
206 void QBarSet::setLabelBrush(const QBrush &brush)
206 void QBarSet::setLabelBrush(const QBrush &brush)
207 {
207 {
@@ -218,7 +218,7 QBrush QBarSet::labelBrush() const
218 }
218 }
219
219
220 /*!
220 /*!
221 Sets the pen for values that are drawn on top of this barset
221 Sets the \a font for values that are drawn on top of this barset
222 */
222 */
223 void QBarSet::setLabelFont(const QFont &font)
223 void QBarSet::setLabelFont(const QFont &font)
224 {
224 {
@@ -22,7 +22,6
22 #include "bar_p.h"
22 #include "bar_p.h"
23 #include "barlabel_p.h"
23 #include "barlabel_p.h"
24 #include "qbarset.h"
24 #include "qbarset.h"
25 #include <QDebug>
26
25
27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
26 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28
27
@@ -31,24 +30,16 StackedBarChartItem::StackedBarChartItem(QBarSeries *series, ChartPresenter *pre
31 {
30 {
32 }
31 }
33
32
34 StackedBarChartItem::~StackedBarChartItem()
35 {
36 }
37
38 QVector<QRectF> StackedBarChartItem::calculateLayout()
33 QVector<QRectF> StackedBarChartItem::calculateLayout()
39 {
34 {
40 QVector<QRectF> layout;
35 QVector<QRectF> layout;
41 // Use temporary qreals for accurancy (we might get some compiler warnings... :)
36 // Use temporary qreals for accurancy
42
37
43 qreal maxSum = m_series->maxCategorySum();
44 // Domain:
38 // Domain:
45 if (m_domainMaxY > maxSum) {
39 qreal range = m_domainMaxY - m_domainMinY;
46 maxSum = m_domainMaxY;
47 }
48
49 qreal height = geometry().height();
40 qreal height = geometry().height();
50 qreal width = geometry().width();
41 qreal width = geometry().width();
51 qreal scale = (height / m_series->maxCategorySum());
42 qreal scale = (height / range);
52 qreal categotyCount = m_series->categoryCount();
43 qreal categotyCount = m_series->categoryCount();
53 qreal barWidth = width / (categotyCount * 2);
44 qreal barWidth = width / (categotyCount * 2);
54 qreal xStep = width / categotyCount;
45 qreal xStep = width / categotyCount;
@@ -56,7 +47,7 QVector<QRectF> StackedBarChartItem::calculateLayout()
56
47
57 int itemIndex(0);
48 int itemIndex(0);
58 for (int category = 0; category < categotyCount; category++) {
49 for (int category = 0; category < categotyCount; category++) {
59 qreal yPos = height;
50 qreal yPos = height + scale * m_domainMinY;
60 for (int set=0; set < m_series->barsetCount(); set++) {
51 for (int set=0; set < m_series->barsetCount(); set++) {
61 QBarSet* barSet = m_series->barsetAt(set);
52 QBarSet* barSet = m_series->barsetAt(set);
62
53
@@ -77,7 +68,7 QVector<QRectF> StackedBarChartItem::calculateLayout()
77
68
78 label->setPos(xPos + (rect.width()/2 - label->boundingRect().width()/2)
69 label->setPos(xPos + (rect.width()/2 - label->boundingRect().width()/2)
79 ,yPos - barHeight/2 - label->boundingRect().height()/2);
70 ,yPos - barHeight/2 - label->boundingRect().height()/2);
80 // value->setFont(barSet->valueFont());
71 label->setFont(barSet->labelFont());
81 itemIndex++;
72 itemIndex++;
82 yPos -= barHeight;
73 yPos -= barHeight;
83 }
74 }
@@ -32,7 +32,6 class StackedBarChartItem : public BarChartItem
32 Q_OBJECT
32 Q_OBJECT
33 public:
33 public:
34 StackedBarChartItem(QBarSeries *series, ChartPresenter *presenter);
34 StackedBarChartItem(QBarSeries *series, ChartPresenter *presenter);
35 ~StackedBarChartItem();
36
35
37 private:
36 private:
38 // From BarChartItem
37 // From BarChartItem
General Comments 0
You need to be logged in to leave comments. Login now