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