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