##// END OF EJS Templates
making bar as qobject causes crash for some reason. rewinding back a bit...
sauimone -
r247:8f79fcf18e90
parent child
Show More
@@ -1,77 +1,76
1 #include "bar_p.h"
1 #include "bar_p.h"
2 #include <QDebug>
2 #include <QDebug>
3 #include <QPainter>
3 #include <QPainter>
4
4
5 QTCOMMERCIALCHART_BEGIN_NAMESPACE
5 QTCOMMERCIALCHART_BEGIN_NAMESPACE
6
6
7 Bar::Bar(QGraphicsItem *parent)
7 Bar::Bar(QGraphicsItem *parent)
8 : QGraphicsObject(parent)
8 : ChartItem(parent)
9 {
9 {
10 }
10 }
11
11
12 void Bar::setSize(const QSizeF& size)
13 {
14 mWidth = size.width();
15 mHeight = size.height();
16 }
17
18
12 void Bar::resize( qreal w, qreal h )
19 void Bar::resize( qreal w, qreal h )
13 {
20 {
14 // qDebug() << "bar::resize" << w << h;
21 // qDebug() << "bar::resize" << w << h;
15 mWidth = w;
22 mWidth = w;
16 mHeight = h;
23 mHeight = h;
17 }
24 }
18
25
26 void Bar::setColor( QColor col )
27 {
28 mColor = col;
29 }
30
19 void Bar::setPos(qreal x, qreal y)
31 void Bar::setPos(qreal x, qreal y)
20 {
32 {
21 // qDebug() << "Bar::setpos" << x << y;
33 // qDebug() << "Bar::setpos" << x << y;
22 mXpos = x;
34 mXpos = x;
23 mYpos = y;
35 mYpos = y;
24 }
36 }
25
37
26 void Bar::setPen(QPen pen)
38 void Bar::setPen(QPen pen)
27 {
39 {
28 mPen = pen;
40 mPen = pen;
29 }
41 }
30
42
31 void Bar::setBrush(QBrush brush)
43 void Bar::setBrush(QBrush brush)
32 {
44 {
33 mBrush = brush;
45 mBrush = brush;
34 }
46 }
35
47
36 void Bar::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
48 void Bar::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
37 {
49 {
38 if (0 == mHeight) {
50 if (0 == mHeight) {
39 return;
51 return;
40 }
52 }
41
53 // TODO: accept brush instead of color
42 painter->setBrush(mBrush);
54 painter->setBrush(mBrush);
55 // QBrush brush(mColor);
56 // painter->setBrush(brush);
43
57
44 // This compensates for rounding errors. drawRect takes ints and cumulative error of pos + size may be over 1.
58 // This compensates for rounding errors. drawRect takes ints and cumulative error of pos + size may be over 1.
45 int x0 = mXpos;
59 int x0 = mXpos;
46 int x1 = (mXpos + mWidth);
60 int x1 = (mXpos + mWidth);
47 int w = x1-x0;
61 int w = x1-x0;
48 int y0 = mYpos;
62 int y0 = mYpos;
49 int y1 = (mYpos + mHeight);
63 int y1 = (mYpos + mHeight);
50 int h = y1-y0;
64 int h = y1-y0;
51 painter->drawRect(x0, y0 ,w ,h);
65 painter->drawRect(x0, y0 ,w ,h);
52 }
66 }
53
67
54 QRectF Bar::boundingRect() const
68 QRectF Bar::boundingRect() const
55 {
69 {
56 QRectF r(mXpos, mYpos, mXpos + mWidth, mYpos + mHeight);
70 QRectF r(mXpos, mYpos, mXpos + mWidth, mYpos + mHeight);
57 return r;
71 return r;
58 }
72 }
59
73
60 void Bar::hoverEnterEvent(QGraphicsSceneHoverEvent* /*event*/)
74 //#include "moc_bar_p.cpp"
61 {
62 emit hoverEnter();
63 }
64
65 void Bar::hoverLeaveEvent(QGraphicsSceneHoverEvent* /*event*/)
66 {
67 emit hoverLeave();
68 }
69
70 void Bar::mousePressEvent(QGraphicsSceneMouseEvent* /*event*/)
71 {
72 emit clicked();
73 }
74
75 #include "moc_bar_p.cpp"
76
75
77 QTCOMMERCIALCHART_END_NAMESPACE
76 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,56 +1,49
1 #ifndef BAR_H
1 #ifndef BAR_H
2 #define BAR_H
2 #define BAR_H
3
3
4 //#include "chartitem_p.h"
4 #include "chartitem_p.h"
5 #include "qchartglobal.h"
5 #include "qchartglobal.h"
6 #include <QGraphicsObject>
6 #include <QGraphicsItem>
7 #include <QPen>
7 #include <QPen>
8 #include <QBrush>
8 #include <QBrush>
9
9
10 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10 QTCOMMERCIALCHART_BEGIN_NAMESPACE
11
11
12 // Single bar item of chart
12 // Single bar item of chart
13 class Bar : public QGraphicsObject
13 class Bar : public ChartItem
14 {
14 {
15 Q_OBJECT
16
17 public:
15 public:
18 Bar(QGraphicsItem *parent=0);
16 Bar(QGraphicsItem *parent=0);
19
17
20 public:
18 public: // from ChartItem
19 void setSize(const QSizeF &size);
21
20
22 // Layout Stuff
21 // Layout Stuff
23 void resize(qreal w, qreal h); // Size of bar.
22 void resize(qreal w, qreal h); // Size of bar.
24 void setPos(qreal x, qreal y);
23 void setPos(qreal x, qreal y);
25 void setPen(QPen pen);
24 void setPen(QPen pen);
26 void setBrush(QBrush brush);
25 void setBrush(QBrush brush);
26 void setColor( QColor col); // deprecated
27
27
28 public:
28 public:
29
30 // From QGraphicsItem
29 // From QGraphicsItem
30
31 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
31 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
32 QRectF boundingRect() const;
32 QRectF boundingRect() const;
33 void hoverEnterEvent(QGraphicsSceneHoverEvent *event);
34 void hoverLeaveEvent(QGraphicsSceneHoverEvent *event);
35 void mousePressEvent(QGraphicsSceneMouseEvent *event);
36
37 Q_SIGNALS:
38 void clicked();
39 void hoverEnter();
40 void hoverLeave();
41
33
42 private:
34 private:
43
35
44 qreal mHeight;
36 qreal mHeight;
45 qreal mWidth;
37 qreal mWidth;
46 qreal mXpos;
38 qreal mXpos;
47 qreal mYpos;
39 qreal mYpos;
40 QColor mColor;
48
41
49 QBrush mBrush;
42 QBrush mBrush;
50 QPen mPen;
43 QPen mPen;
51
44
52 };
45 };
53
46
54 QTCOMMERCIALCHART_END_NAMESPACE
47 QTCOMMERCIALCHART_END_NAMESPACE
55
48
56 #endif // BAR_H
49 #endif // BAR_H
@@ -1,115 +1,115
1 #include "barpresenterbase.h"
1 #include "barpresenterbase.h"
2 #include "bar_p.h"
2 #include "bar_p.h"
3 #include "barlabel_p.h"
3 #include "barlabel_p.h"
4 #include "separator_p.h"
4 #include "separator_p.h"
5 #include "qbarset.h"
5 #include "qbarset.h"
6 #include <QDebug>
6 #include <QDebug>
7
7
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9
9
10 BarPresenterBase::BarPresenterBase(BarChartModel& model, QGraphicsItem *parent)
10 BarPresenterBase::BarPresenterBase(BarChartModel& model, QGraphicsItem *parent)
11 : ChartItem(parent)
11 : ChartItem(parent)
12 ,mBarDefaultWidth(20) // TODO: remove hard coding, when we have layout code ready
12 ,mBarDefaultWidth(20) // TODO: remove hard coding, when we have layout code ready
13 ,mLayoutSet(false)
13 ,mLayoutSet(false)
14 ,mLayoutDirty(true)
14 ,mLayoutDirty(true)
15 ,mSeparatorsVisible(true)
15 ,mSeparatorsVisible(true)
16 ,mModel(model)
16 ,mModel(model)
17 {
17 {
18 dataChanged();
18 dataChanged();
19 }
19 }
20
20
21 void BarPresenterBase::setSeparatorsVisible(bool visible)
21 void BarPresenterBase::setSeparatorsVisible(bool visible)
22 {
22 {
23 mSeparatorsVisible = visible;
23 mSeparatorsVisible = visible;
24 }
24 }
25
25
26 void BarPresenterBase::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
26 void BarPresenterBase::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
27 {
27 {
28 // qDebug() << "BarGroupBase::paint" << childItems().count();
28 // qDebug() << "BarGroupBase::paint" << childItems().count();
29 if (!mLayoutSet) {
29 if (!mLayoutSet) {
30 qDebug() << "BarGroupBase::paint called without layout set. Aborting.";
30 qDebug() << "BarGroupBase::paint called without layout set. Aborting.";
31 return;
31 return;
32 }
32 }
33 // if (mLayoutDirty) {
33 // if (mLayoutDirty) {
34 // Layout or data has changed. Need to redraw.
34 // Layout or data has changed. Need to redraw.
35 foreach(QGraphicsItem* i, childItems()) {
35 foreach(QGraphicsItem* i, childItems()) {
36 i->paint(painter,option,widget);
36 i->paint(painter,option,widget);
37 }
37 }
38 // }
38 // }
39 }
39 }
40
40
41 QRectF BarPresenterBase::boundingRect() const
41 QRectF BarPresenterBase::boundingRect() const
42 {
42 {
43 return QRectF(0,0,mWidth,mHeight);
43 return QRectF(0,0,mWidth,mHeight);
44 }
44 }
45
45
46 void BarPresenterBase::setBarWidth( int w )
46 void BarPresenterBase::setBarWidth( int w )
47 {
47 {
48 mBarDefaultWidth = w;
48 mBarDefaultWidth = w;
49 }
49 }
50
50
51 void BarPresenterBase::dataChanged()
51 void BarPresenterBase::dataChanged()
52 {
52 {
53 // TODO: performance optimizations. Do we really need to delete and create items every time data is changed or can we reuse them?
53 // TODO: performance optimizations. Do we really need to delete and create items every time data is changed or can we reuse them?
54
54
55 // Delete old bars
55 // Delete old bars
56 foreach (QGraphicsItem* item, childItems()) {
56 foreach (QGraphicsItem* item, childItems()) {
57 delete item;
57 delete item;
58 }
58 }
59
59
60 // Create new graphic items for bars
60 // Create new graphic items for bars
61 for (int s=0; s<mModel.countSets(); s++) {
61 for (int s=0; s<mModel.countSets(); s++) {
62 QBarSet *set = mModel.nextSet(0==s);
62 QBarSet *set = mModel.nextSet(0==s);
63 for (int c=0; c<mModel.countCategories(); c++) {
63 for (int c=0; c<mModel.countCategories(); c++) {
64 Bar *bar = new Bar(this);
64 Bar *bar = new Bar(this);
65 childItems().append(bar);
65 childItems().append(bar);
66 connect(bar,SIGNAL(clicked()),set,SLOT(barClicked()));
66 //connect(bar,SIGNAL(clicked()),set,SLOT(barClicked()));
67 }
67 }
68 }
68 }
69
69
70 int count = mModel.countCategories();
70 int count = mModel.countCategories();
71 for (int i=0; i<count; i++) {
71 for (int i=0; i<count; i++) {
72 BarLabel* label = new BarLabel(this);
72 BarLabel* label = new BarLabel(this);
73 label->set(mModel.label(i));
73 label->set(mModel.label(i));
74 childItems().append(label);
74 childItems().append(label);
75 }
75 }
76
76
77 count = mModel.countCategories() - 1; // There is one less separator than columns
77 count = mModel.countCategories() - 1; // There is one less separator than columns
78 for (int i=0; i<count; i++) {
78 for (int i=0; i<count; i++) {
79 Separator* sep = new Separator(this);
79 Separator* sep = new Separator(this);
80 sep->setColor(QColor(255,0,0,255)); // TODO: color for separations from theme
80 sep->setColor(QColor(255,0,0,255)); // TODO: color for separations from theme
81 childItems().append(sep);
81 childItems().append(sep);
82 }
82 }
83
83
84 // TODO: if (autolayout) { layoutChanged() } or something
84 // TODO: if (autolayout) { layoutChanged() } or something
85 mLayoutDirty = true;
85 mLayoutDirty = true;
86 }
86 }
87
87
88 //handlers
88 //handlers
89
89
90 void BarPresenterBase::handleModelChanged(int index)
90 void BarPresenterBase::handleModelChanged(int index)
91 {
91 {
92 // qDebug() << "BarGroupBase::handleModelChanged" << index;
92 // qDebug() << "BarGroupBase::handleModelChanged" << index;
93 dataChanged();
93 dataChanged();
94 }
94 }
95
95
96 void BarPresenterBase::handleDomainChanged(const Domain& domain)
96 void BarPresenterBase::handleDomainChanged(const Domain& domain)
97 {
97 {
98 // qDebug() << "BarGroupBase::handleDomainChanged";
98 // qDebug() << "BarGroupBase::handleDomainChanged";
99 // TODO: Figure out the use case for this.
99 // TODO: Figure out the use case for this.
100 // Affects the size of visible item, so layout is changed.
100 // Affects the size of visible item, so layout is changed.
101 // layoutChanged();
101 // layoutChanged();
102 }
102 }
103
103
104 void BarPresenterBase::handleGeometryChanged(const QRectF& rect)
104 void BarPresenterBase::handleGeometryChanged(const QRectF& rect)
105 {
105 {
106 mWidth = rect.width();
106 mWidth = rect.width();
107 mHeight = rect.height();
107 mHeight = rect.height();
108 layoutChanged();
108 layoutChanged();
109 mLayoutSet = true;
109 mLayoutSet = true;
110 setPos(rect.topLeft());
110 setPos(rect.topLeft());
111 }
111 }
112
112
113 #include "moc_barpresenterbase.cpp"
113 #include "moc_barpresenterbase.cpp"
114
114
115 QTCOMMERCIALCHART_END_NAMESPACE
115 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now