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