@@ -1,114 +1,115 | |||
|
1 | 1 | #include "barpresenterbase.h" |
|
2 | 2 | #include "bar_p.h" |
|
3 | 3 | #include "barlabel_p.h" |
|
4 | 4 | #include "separator_p.h" |
|
5 | #include "qbarset.h" | |
|
5 | 6 | #include <QDebug> |
|
6 | 7 | |
|
7 | 8 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
8 | 9 | |
|
9 | 10 | BarPresenterBase::BarPresenterBase(BarChartModel& model, QGraphicsItem *parent) |
|
10 | 11 | : ChartItem(parent) |
|
11 | 12 | ,mBarDefaultWidth(20) // TODO: remove hard coding, when we have layout code ready |
|
12 | 13 | ,mLayoutSet(false) |
|
13 | 14 | ,mLayoutDirty(true) |
|
14 | 15 | ,mSeparatorsVisible(true) |
|
15 | 16 | ,mModel(model) |
|
16 | 17 | { |
|
17 | 18 | dataChanged(); |
|
18 | 19 | } |
|
19 | 20 | |
|
20 | 21 | void BarPresenterBase::setSeparatorsVisible(bool visible) |
|
21 | 22 | { |
|
22 | 23 | mSeparatorsVisible = visible; |
|
23 | 24 | } |
|
24 | 25 | |
|
25 | 26 | void BarPresenterBase::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) |
|
26 | 27 | { |
|
27 | 28 | // qDebug() << "BarGroupBase::paint" << childItems().count(); |
|
28 | 29 | if (!mLayoutSet) { |
|
29 | 30 | qDebug() << "BarGroupBase::paint called without layout set. Aborting."; |
|
30 | 31 | return; |
|
31 | 32 | } |
|
32 | 33 | // if (mLayoutDirty) { |
|
33 | 34 | // Layout or data has changed. Need to redraw. |
|
34 | 35 | foreach(QGraphicsItem* i, childItems()) { |
|
35 | 36 | i->paint(painter,option,widget); |
|
36 | 37 | } |
|
37 | 38 | // } |
|
38 | 39 | } |
|
39 | 40 | |
|
40 | 41 | QRectF BarPresenterBase::boundingRect() const |
|
41 | 42 | { |
|
42 | 43 | return QRectF(0,0,mWidth,mHeight); |
|
43 | 44 | } |
|
44 | 45 | |
|
45 | 46 | void BarPresenterBase::setBarWidth( int w ) |
|
46 | 47 | { |
|
47 | 48 | mBarDefaultWidth = w; |
|
48 | 49 | } |
|
49 | 50 | |
|
50 | 51 | void BarPresenterBase::dataChanged() |
|
51 | 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 | 54 | |
|
54 | 55 | // Delete old bars |
|
55 | 56 | foreach (QGraphicsItem* item, childItems()) { |
|
56 | 57 | delete item; |
|
57 | 58 | } |
|
58 | 59 | |
|
59 | 60 | // Create new graphic items for bars |
|
60 | 61 | for (int s=0; s<mModel.countSets(); s++) { |
|
61 | 62 | QBarSet *set = mModel.nextSet(0==s); |
|
62 | 63 | for (int c=0; c<mModel.countCategories(); c++) { |
|
63 | 64 | Bar *bar = new Bar(this); |
|
64 | 65 | childItems().append(bar); |
|
65 | 66 | connect(bar,SIGNAL(clicked()),set,SLOT(barClicked())); |
|
66 | 67 | } |
|
67 | 68 | } |
|
68 | 69 | |
|
69 | 70 | int count = mModel.countCategories(); |
|
70 | 71 | for (int i=0; i<count; i++) { |
|
71 | 72 | BarLabel* label = new BarLabel(this); |
|
72 | 73 | label->set(mModel.label(i)); |
|
73 | 74 | childItems().append(label); |
|
74 | 75 | } |
|
75 | 76 | |
|
76 | 77 | count = mModel.countCategories() - 1; // There is one less separator than columns |
|
77 | 78 | for (int i=0; i<count; i++) { |
|
78 | 79 | Separator* sep = new Separator(this); |
|
79 | 80 | sep->setColor(QColor(255,0,0,255)); // TODO: color for separations from theme |
|
80 | 81 | childItems().append(sep); |
|
81 | 82 | } |
|
82 | 83 | |
|
83 | 84 | // TODO: if (autolayout) { layoutChanged() } or something |
|
84 | 85 | mLayoutDirty = true; |
|
85 | 86 | } |
|
86 | 87 | |
|
87 | 88 | //handlers |
|
88 | 89 | |
|
89 | 90 | void BarPresenterBase::handleModelChanged(int index) |
|
90 | 91 | { |
|
91 | 92 | // qDebug() << "BarGroupBase::handleModelChanged" << index; |
|
92 | 93 | dataChanged(); |
|
93 | 94 | } |
|
94 | 95 | |
|
95 | 96 | void BarPresenterBase::handleDomainChanged(const Domain& domain) |
|
96 | 97 | { |
|
97 | 98 | // qDebug() << "BarGroupBase::handleDomainChanged"; |
|
98 | 99 | // TODO: Figure out the use case for this. |
|
99 | 100 | // Affects the size of visible item, so layout is changed. |
|
100 | 101 | // layoutChanged(); |
|
101 | 102 | } |
|
102 | 103 | |
|
103 | 104 | void BarPresenterBase::handleGeometryChanged(const QRectF& rect) |
|
104 | 105 | { |
|
105 | 106 | mWidth = rect.width(); |
|
106 | 107 | mHeight = rect.height(); |
|
107 | 108 | layoutChanged(); |
|
108 | 109 | mLayoutSet = true; |
|
109 | 110 | setPos(rect.topLeft()); |
|
110 | 111 | } |
|
111 | 112 | |
|
112 | 113 | #include "moc_barpresenterbase.cpp" |
|
113 | 114 | |
|
114 | 115 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,50 +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 | 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 | 30 | // void clicked(); |
|
31 | 31 | /* |
|
32 | 32 | void hoverEnter(); |
|
33 | 33 | void hoverLeave(); |
|
34 | 34 | */ |
|
35 | 35 | |
|
36 | 36 | public Q_SLOTS: |
|
37 | 37 | void barClicked(); |
|
38 | 38 | |
|
39 | 39 | private: |
|
40 | 40 | |
|
41 | 41 | QString mName; |
|
42 | 42 | QList<qreal> mValues; |
|
43 | 43 | QPen mPen; |
|
44 | 44 | QBrush mBrush; |
|
45 | 45 | |
|
46 | 46 | }; |
|
47 | 47 | |
|
48 | 48 | QTCOMMERCIALCHART_END_NAMESPACE |
|
49 | 49 | |
|
50 | 50 | #endif // QBARSET_H |
General Comments 0
You need to be logged in to leave comments.
Login now