##// END OF EJS Templates
enablers for tooltip and floating values, bug fixing, updated examples. tidying up the code
sauimone -
r296:8254aab7233d
parent child
Show More
@@ -4,9 +4,7
4 4
5 5 TARGET = barchart
6 6 SOURCES += main.cpp \
7 chartwidget.cpp \
8 custombarset.cpp
7 chartwidget.cpp
9 8 HEADERS += \
10 chartwidget.h \
11 custombarset.h
9 chartwidget.h
12 10
@@ -5,8 +5,6
5 5 #include <qbarset.h>
6 6 #include <qbarcategory.h>
7 7
8 #include "custombarset.h"
9
10 8 QTCOMMERCIALCHART_USE_NAMESPACE
11 9
12 10 int main(int argc, char *argv[])
@@ -20,11 +18,11 int main(int argc, char *argv[])
20 18 QBarChartSeries* series= new QBarChartSeries(category);
21 19
22 20 // We use custom set, which connects some signals. Could use QBarSet here if we don't need signals
23 CustomBarSet *set0 = new CustomBarSet("Bub");
24 CustomBarSet *set1 = new CustomBarSet("Bob");
25 CustomBarSet *set2 = new CustomBarSet("Guybrush");
26 CustomBarSet *set3 = new CustomBarSet("Larry");
27 CustomBarSet *set4 = new CustomBarSet("Zak");
21 QBarSet *set0 = new QBarSet("Bub");
22 QBarSet *set1 = new QBarSet("Bob");
23 QBarSet *set2 = new QBarSet("Guybrush");
24 QBarSet *set3 = new QBarSet("Larry");
25 QBarSet *set4 = new QBarSet("Zak");
28 26
29 27 // Create some test data to chart
30 28 *set0 << 1 << 2 << 3 << 4 << 5 << 6 << 7 << 8 << 9 << 10 << 11 << 12;
@@ -39,6 +37,9 int main(int argc, char *argv[])
39 37 series->addBarSet(set3);
40 38 series->addBarSet(set4);
41 39
40 series->enableToolTip(true);
41 series->enableFloatingValues(true);
42
42 43 QChartView* chartView = new QChartView(&window);
43 44 chartView->addSeries(series);
44 45 chartView->setChartTitle("simple stacked barchart");
@@ -4,8 +4,7
4 4 #include <qpercentbarchartseries.h>
5 5 #include <qbarcategory.h>
6 6 #include <qchartview.h>
7
8 #include "custombarset.h"
7 #include <qbarset.h>
9 8
10 9 QTCOMMERCIALCHART_USE_NAMESPACE
11 10
@@ -19,12 +18,11 int main(int argc, char *argv[])
19 18
20 19 QPercentBarChartSeries* series = new QPercentBarChartSeries(category);
21 20
22 // We use custom set, which connects some signals. Could use QBarSet here if we don't need signals
23 CustomBarSet *set0 = new CustomBarSet("Bub");
24 CustomBarSet *set1 = new CustomBarSet("Bob");
25 CustomBarSet *set2 = new CustomBarSet("Guybrush");
26 CustomBarSet *set3 = new CustomBarSet("Larry");
27 CustomBarSet *set4 = new CustomBarSet("Zak");
21 QBarSet *set0 = new QBarSet("Bub");
22 QBarSet *set1 = new QBarSet("Bob");
23 QBarSet *set2 = new QBarSet("Guybrush");
24 QBarSet *set3 = new QBarSet("Larry");
25 QBarSet *set4 = new QBarSet("Zak");
28 26
29 27 // Create some test data to chart
30 28 *set0 << 1 << 2 << 3 << 4 << 5 << 6 << 7 << 8 << 9 << 10 << 11 << 12;
@@ -39,6 +37,9 int main(int argc, char *argv[])
39 37 series->addBarSet(set3);
40 38 series->addBarSet(set4);
41 39
40 series->enableToolTip(true);
41 series->enableFloatingValues(true);
42
42 43 QChartView* chartView = new QChartView(&window);
43 44 chartView->addSeries(series);
44 45 chartView->setChartTitle("simple percent barchart");
@@ -4,9 +4,7
4 4
5 5 TARGET = percentbarchart
6 6 SOURCES += main.cpp \
7 chartwidget.cpp \
8 custombarset.cpp
7 chartwidget.cpp
9 8 HEADERS += \
10 chartwidget.h \
11 custombarset.h
9 chartwidget.h
12 10
@@ -5,8 +5,6
5 5 #include <qbarset.h>
6 6 #include <qbarcategory.h>
7 7
8 #include "custombarset.h"
9
10 8 QTCOMMERCIALCHART_USE_NAMESPACE
11 9
12 10 int main(int argc, char *argv[])
@@ -19,12 +17,11 int main(int argc, char *argv[])
19 17
20 18 QStackedBarChartSeries* series = new QStackedBarChartSeries(category);
21 19
22 // We use custom set, which connects some signals. Could use QBarSet here if we don't need signals
23 CustomBarSet *set0 = new CustomBarSet("Bub");
24 CustomBarSet *set1 = new CustomBarSet("Bob");
25 CustomBarSet *set2 = new CustomBarSet("Guybrush");
26 CustomBarSet *set3 = new CustomBarSet("Larry");
27 CustomBarSet *set4 = new CustomBarSet("Zak");
20 QBarSet *set0 = new QBarSet("Bub");
21 QBarSet *set1 = new QBarSet("Bob");
22 QBarSet *set2 = new QBarSet("Guybrush");
23 QBarSet *set3 = new QBarSet("Larry");
24 QBarSet *set4 = new QBarSet("Zak");
28 25
29 26 // Create some test data to chart
30 27 *set0 << 1 << 2 << 3 << 4 << 5 << 6 << 7 << 8 << 9 << 10 << 11 << 12;
@@ -39,6 +36,9 int main(int argc, char *argv[])
39 36 series->addBarSet(set3);
40 37 series->addBarSet(set4);
41 38
39 series->enableToolTip(true);
40 series->enableFloatingValues(true);
41
42 42 QChartView* chartView = new QChartView(&window);
43 43 chartView->addSeries(series);
44 44 chartView->setChartTitle("simple stacked barchart");
@@ -3,9 +3,7
3 3 }
4 4 TARGET = stackedbarchart
5 5 SOURCES += main.cpp \
6 chartwidget.cpp \
7 custombarset.cpp
6 chartwidget.cpp
8 7 HEADERS += \
9 chartwidget.h \
10 custombarset.h
8 chartwidget.h
11 9
@@ -15,20 +15,16 BarPresenterBase::BarPresenterBase(QBarChartSeries *series, QGraphicsItem *paren
15 15 ,mBarDefaultWidth(20) // TODO: remove hard coding, when we have layout code ready
16 16 ,mLayoutSet(false)
17 17 ,mLayoutDirty(true)
18 ,mSeparatorsEnabled(false)
18 19 ,mSeries(series)
19 20 {
20 connect(series,SIGNAL(floatingValuesEnabled(bool)),this,SLOT(enableFloatingValues(bool)));
21 connect(series,SIGNAL(toolTipEnabled(bool)),this,SLOT(enableToolTip(bool)));
22 connect(series,SIGNAL(separatorsEnabled(bool)),this,SLOT(enableSeparators(bool)));
23 21 connect(series,SIGNAL(showToolTip(QPoint,QString)),this,SLOT(showToolTip(QPoint,QString)));
22 connect(series,SIGNAL(separatorsEnabled(bool)),this,SLOT(enableSeparators(bool)));
24 23 dataChanged();
25 24 }
26 25
27 26 BarPresenterBase::~BarPresenterBase()
28 27 {
29 disconnect(this,SLOT(enableFloatingValues(bool)));
30 disconnect(this,SLOT(enableToolTip(bool)));
31 disconnect(this,SLOT(enableSeparators(bool)));
32 28 disconnect(this,SLOT(showToolTip(QPoint,QString)));
33 29 delete mSeries;
34 30 }
@@ -79,7 +75,6 void BarPresenterBase::dataChanged()
79 75 childItems().append(bar);
80 76 mBars.append(bar);
81 77 connect(bar,SIGNAL(clicked()),set,SLOT(barClicked()));
82 // TODO: should the event be passed to set or not?
83 78 connect(bar,SIGNAL(hoverEntered(QPoint)),set,SLOT(barHoverEntered(QPoint)));
84 79 connect(bar,SIGNAL(hoverLeaved()),set,SLOT(barHoverLeaved()));
85 80 }
@@ -99,6 +94,7 void BarPresenterBase::dataChanged()
99 94 for (int i=0; i<count; i++) {
100 95 Separator* sep = new Separator(this);
101 96 sep->setColor(QColor(255,0,0,255)); // TODO: color for separations from theme
97 sep->setVisible(mSeparatorsEnabled);
102 98 childItems().append(sep);
103 99 mSeparators.append(sep);
104 100 }
@@ -143,27 +139,18 void BarPresenterBase::handleGeometryChanged(const QRectF& rect)
143 139 setPos(rect.topLeft());
144 140 }
145 141
146 void BarPresenterBase::enableFloatingValues(bool enabled)
147 {
148 mFloatingValuesEnabled = enabled;
149 }
150
151 void BarPresenterBase::enableToolTip(bool enabled)
142 void BarPresenterBase::showToolTip(QPoint pos, QString tip)
152 143 {
153 mToolTipEnabled = enabled;
144 // TODO: cool tooltip instead of default
145 QToolTip::showText(pos,tip);
154 146 }
155 147
156 148 void BarPresenterBase::enableSeparators(bool enabled)
157 149 {
158 mSeparatorsEnabled = enabled;
159 }
160
161 void BarPresenterBase::showToolTip(QPoint pos, QString tip)
162 {
163 if (mToolTipEnabled) {
164 // TODO: cool tooltip instead of default
165 QToolTip::showText(pos,tip);
150 for (int i=0; i<mSeparators.count(); i++) {
151 mSeparators.at(i)->setVisible(enabled);
166 152 }
153 mSeparatorsEnabled = enabled;
167 154 }
168 155
169 156 #include "moc_barpresenterbase.cpp"
@@ -41,10 +41,8 protected slots:
41 41 void handleGeometryChanged(const QRectF& size);
42 42
43 43 // Internal slots
44 void enableFloatingValues(bool enabled=true); // enables floating values on top of bars
45 void enableToolTip(bool enabled=true); // enables tooltips
46 void enableSeparators(bool enabled=true); // enables separators between categories
47 44 void showToolTip(QPoint pos, QString tip); // shows tooltip (if enabled)
45 void enableSeparators(bool enabled);
48 46
49 47 protected:
50 48
@@ -56,8 +54,6 protected:
56 54 bool mLayoutSet; // True, if component has been laid out.
57 55 bool mLayoutDirty;
58 56
59 bool mFloatingValuesEnabled;
60 bool mToolTipEnabled;
61 57 bool mSeparatorsEnabled;
62 58
63 59 // Owned
@@ -15,18 +15,11 QBarChartSeries::QBarChartSeries(QBarCategory *category, QObject *parent)
15 15
16 16 void QBarChartSeries::addBarSet(QBarSet *set)
17 17 {
18 connect(this,SIGNAL(floatingValuesEnabled(bool)),set,SLOT(enableFloatingValues(bool)));
19 connect(this,SIGNAL(separatorsEnabled(bool)),set,SLOT(enableSeparators(bool)));
20 connect(this,SIGNAL(toolTipEnabled(bool)),set,SLOT(enableToolTip(bool)));
21 connect(set,SIGNAL(showToolTip(QPoint,QString)),this,SIGNAL(showToolTip(QPoint,QString)));
22 18 mModel->addBarSet(set);
23 19 }
24 20
25 21 void QBarChartSeries::removeBarSet(QBarSet *set)
26 22 {
27 disconnect(set,SLOT(enableFloatingValues(bool)));
28 disconnect(set,SLOT(enableSeparators(bool)));
29 disconnect(set,SLOT(enableToolTip(bool)));
30 23 mModel->removeBarSet(set);
31 24 }
32 25
@@ -57,12 +50,32 QString QBarChartSeries::label(int category)
57 50
58 51 void QBarChartSeries::enableFloatingValues(bool enabled)
59 52 {
60 emit floatingValuesEnabled(enabled);
53 if (enabled) {
54 for (int i=0; i<mModel->countSets(); i++) {
55 QBarSet *set = mModel->setAt(i);
56 connect(set,SIGNAL(clicked()),set,SIGNAL(toggleFloatingValues()));
57 }
58 } else {
59 for (int i=0; i<mModel->countSets(); i++) {
60 QBarSet *set = mModel->setAt(i);
61 disconnect(set,SIGNAL(clicked()),set,SIGNAL(toggleFloatingValues()));
62 }
63 }
61 64 }
62 65
63 66 void QBarChartSeries::enableToolTip(bool enabled)
64 67 {
65 emit toolTipEnabled(enabled);
68 if (enabled) {
69 for (int i=0; i<mModel->countSets(); i++) {
70 QBarSet *set = mModel->setAt(i);
71 connect(set,SIGNAL(showToolTip(QPoint,QString)),this,SIGNAL(showToolTip(QPoint,QString)));
72 }
73 } else {
74 for (int i=0; i<mModel->countSets(); i++) {
75 QBarSet *set = mModel->setAt(i);
76 disconnect(set,SIGNAL(showToolTip(QPoint,QString)),this,SIGNAL(showToolTip(QPoint,QString)));
77 }
78 }
66 79 }
67 80
68 81 void QBarChartSeries::enableSeparators(bool enabled)
@@ -110,7 +123,6 BarChartModel& QBarChartSeries::model()
110 123 return *mModel;
111 124 }
112 125
113
114 126 #include "moc_qbarchartseries.cpp"
115 127
116 128 QTCOMMERCIALCHART_END_NAMESPACE
@@ -21,44 +21,44 public:
21 21 void addBarSet(QBarSet *set); // Takes ownership of set
22 22 void removeBarSet(QBarSet *set); // Releases ownership, doesn't delete set
23 23 int countSets();
24 QBarSet* nextSet(bool getFirst=false); // Returns first set, if called with true
24 QBarSet* nextSet(bool getFirst=false); // Returns first set, if called with true
25 25 QBarSet *setAt(int index);
26 26
27 27 QList<QString> legend(); // Returns legend of series (ie. names of all sets in series)
28 28 QString label(int category);
29 29
30 public Q_SLOTS:
31 // Disabled by default. Call these to change behavior.
32 void enableFloatingValues(bool enabled=true); // enables floating values on top of bars
33 void enableToolTip(bool enabled=true); // enables tooltips
34 void enableSeparators(bool enabled=true); // enables separators between categories
35
36 30 public:
37 31 // TODO: Functions below this are not part of api and will be moved
38 // to private implementation, when we start using it (not part of api)
32 // to private implementation, when we start using it
33 // TODO: TO PIMPL --->
39 34 int countCategories();
40 35 qreal min();
41 36 qreal max();
42 37 qreal valueAt(int set, int category);
43 38 qreal percentageAt(int set, int category);
44
45 39 qreal categorySum(int category);
46 40 qreal maxCategorySum();
47
48 41 BarChartModel& model();
42 // <--- TO PIMPL
49 43
50 44 signals:
51 45 void changed(int index);
52 46
53 47 // TODO: internal signals, these to private implementation.
48 // TODO: TO PIMPL --->
54 49 void floatingValuesEnabled(bool enabled);
55 50 void toolTipEnabled(bool enabled);
56 51 void separatorsEnabled(bool enabled);
57 52 void showToolTip(QPoint pos, QString tip);
53 // <--- TO PIMPL
54
55 public Q_SLOTS:
56 void enableFloatingValues(bool enabled=true); // enables floating values on top of bars
57 void enableToolTip(bool enabled=true); // enables tooltips
58 void enableSeparators(bool enabled=true); // enables separators between categories
58 59
59 60 protected:
60 61 BarChartModel* mModel;
61
62 62 };
63 63
64 64 QTCOMMERCIALCHART_END_NAMESPACE
@@ -7,8 +7,6 QTCOMMERCIALCHART_BEGIN_NAMESPACE
7 7 QBarSet::QBarSet(QString name, QObject *parent)
8 8 : QObject(parent)
9 9 ,mName(name)
10 ,mToolTipEnabled(true) // TODO: these 2 as false by default, when implementation is ready
11 ,mFloatingValuesEnabled(true)
12 10 {
13 11 }
14 12
@@ -62,24 +60,6 const QBrush& QBarSet::brush() const
62 60 return mBrush;
63 61 }
64 62
65 void QBarSet::enableFloatingValues(bool enabled)
66 {
67 qDebug() << "QBarSet::enableFloatingValues";
68 mFloatingValuesEnabled = enabled;
69 }
70
71 void QBarSet::enableToolTip(bool enabled)
72 {
73 qDebug() << "QBarSet::enableToolTip";
74 mToolTipEnabled = enabled;
75 }
76
77 void QBarSet::enableSeparators(bool enabled)
78 {
79 qDebug() << "QBarSet::enableSeparators";
80 mSeparatorsEnabled = enabled;
81 }
82
83 63 void QBarSet::barClicked()
84 64 {
85 65 // qDebug() << "QBarset::barClicked" << this;
@@ -90,10 +70,7 void QBarSet::barClicked()
90 70
91 71 void QBarSet::barHoverEntered(QPoint pos)
92 72 {
93 if (mToolTipEnabled) {
94 emit showToolTip(pos, mName);
95 }
96 // Emit signal to user of charts
73 emit showToolTip(pos, mName);
97 74 emit hoverEnter(pos);
98 75 }
99 76
@@ -28,22 +28,23 public:
28 28 const QBrush& brush() const;
29 29
30 30 Q_SIGNALS:
31 void clicked(); // Clicked and hover signals exposed to user
31 void clicked(); // Clicked and hover signals exposed to user
32 32 void hoverEnter(QPoint pos);
33 33 void hoverLeave();
34 void toggleFloatingValues(); // Private signal, TODO: move to private impl
35 void showToolTip(QPoint pos, QString tip); // Private signal, TODO: move to private impl
36 34
37 public Q_SLOTS:
38 void enableFloatingValues(bool enabled); // enables floating values on top of bars
39 void enableToolTip(bool enabled); // enables tooltips
40 void enableSeparators(bool enabled); // enables separators between categories
35 // TODO: Expose this to user or not?
36 // TODO: TO PIMPL --->
37 void toggleFloatingValues();
38 void showToolTip(QPoint pos, QString tip); // Private signal
39 // <--- TO PIMPL
41 40
42 // TODO: these slots belong to private implementation.
43 // These are for single bars to notify set about internal events
41 public Q_SLOTS:
42 // These are for internal communication
43 // TODO: TO PIMPL --->
44 44 void barClicked();
45 45 void barHoverEntered(QPoint pos);
46 46 void barHoverLeaved();
47 // <--- TO PIMPL
47 48
48 49 private:
49 50
@@ -52,10 +53,6 private:
52 53 QPen mPen;
53 54 QBrush mBrush;
54 55
55 // TODO: to pimpl
56 bool mFloatingValuesEnabled;
57 bool mToolTipEnabled;
58 bool mSeparatorsEnabled;
59 56 };
60 57
61 58 QTCOMMERCIALCHART_END_NAMESPACE
@@ -28,10 +28,11 void Separator::setSize(const QSizeF &size)
28 28
29 29 void Separator::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
30 30 {
31 // TODO: real pen
32 QPen pen(mColor);
33 painter->setPen(pen);
34 painter->drawLine(mXpos,mYpos,mXpos,mHeight);
31 if (isVisible()) {
32 QPen pen(mColor);
33 painter->setPen(pen);
34 painter->drawLine(mXpos,mYpos,mXpos,mHeight);
35 }
35 36 }
36 37
37 38 QRectF Separator::boundingRect() const
@@ -271,6 +271,9 void MainWidget::addSeries(QString seriesName, int columnCount, int rowCount, QS
271 271 }
272 272 series->addBarSet(set);
273 273 }
274 series->enableFloatingValues();
275 series->enableToolTip();
276 series->enableSeparators(false);
274 277 m_chartWidget->addSeries(series);
275 278 setCurrentSeries(series);
276 279 }
1 NO CONTENT: file was removed
1 NO CONTENT: file was removed
1 NO CONTENT: file was removed
1 NO CONTENT: file was removed
1 NO CONTENT: file was removed
1 NO CONTENT: file was removed
General Comments 0
You need to be logged in to leave comments. Login now