From 8254aab7233d9a36a84a802c05087ec714917e6d 2012-02-23 14:33:10 From: sauimone Date: 2012-02-23 14:33:10 Subject: [PATCH] enablers for tooltip and floating values, bug fixing, updated examples. tidying up the code --- diff --git a/example/barchart/barchart.pro b/example/barchart/barchart.pro index 9b25d42..009c8c7 100644 --- a/example/barchart/barchart.pro +++ b/example/barchart/barchart.pro @@ -4,9 +4,7 @@ TARGET = barchart SOURCES += main.cpp \ - chartwidget.cpp \ - custombarset.cpp + chartwidget.cpp HEADERS += \ - chartwidget.h \ - custombarset.h + chartwidget.h diff --git a/example/barchart/custombarset.cpp b/example/barchart/custombarset.cpp deleted file mode 100644 index ef978d4..0000000 --- a/example/barchart/custombarset.cpp +++ /dev/null @@ -1,8 +0,0 @@ -#include "custombarset.h" - -CustomBarSet::CustomBarSet(QString name, QObject *parent) : - QBarSet(name, parent) -{ - // Using signal to signal connection here. - connect(this,SIGNAL(clicked()),this,SIGNAL(toggleFloatingValues())); -} diff --git a/example/barchart/custombarset.h b/example/barchart/custombarset.h deleted file mode 100644 index ea8e897..0000000 --- a/example/barchart/custombarset.h +++ /dev/null @@ -1,20 +0,0 @@ -#ifndef CUSTOMBARSET_H -#define CUSTOMBARSET_H - -#include - -QTCOMMERCIALCHART_USE_NAMESPACE - -class CustomBarSet : public QBarSet -{ - Q_OBJECT -public: - explicit CustomBarSet(QString name, QObject *parent = 0); - -signals: - -public slots: - -}; - -#endif // CUSTOMBARSET_H diff --git a/example/barchart/main.cpp b/example/barchart/main.cpp index 8a8b861..9cbf30d 100644 --- a/example/barchart/main.cpp +++ b/example/barchart/main.cpp @@ -5,8 +5,6 @@ #include #include -#include "custombarset.h" - QTCOMMERCIALCHART_USE_NAMESPACE int main(int argc, char *argv[]) @@ -20,11 +18,11 @@ int main(int argc, char *argv[]) QBarChartSeries* series= new QBarChartSeries(category); // We use custom set, which connects some signals. Could use QBarSet here if we don't need signals - CustomBarSet *set0 = new CustomBarSet("Bub"); - CustomBarSet *set1 = new CustomBarSet("Bob"); - CustomBarSet *set2 = new CustomBarSet("Guybrush"); - CustomBarSet *set3 = new CustomBarSet("Larry"); - CustomBarSet *set4 = new CustomBarSet("Zak"); + QBarSet *set0 = new QBarSet("Bub"); + QBarSet *set1 = new QBarSet("Bob"); + QBarSet *set2 = new QBarSet("Guybrush"); + QBarSet *set3 = new QBarSet("Larry"); + QBarSet *set4 = new QBarSet("Zak"); // Create some test data to chart *set0 << 1 << 2 << 3 << 4 << 5 << 6 << 7 << 8 << 9 << 10 << 11 << 12; @@ -39,6 +37,9 @@ int main(int argc, char *argv[]) series->addBarSet(set3); series->addBarSet(set4); + series->enableToolTip(true); + series->enableFloatingValues(true); + QChartView* chartView = new QChartView(&window); chartView->addSeries(series); chartView->setChartTitle("simple stacked barchart"); diff --git a/example/percentbarchart/custombarset.cpp b/example/percentbarchart/custombarset.cpp deleted file mode 100644 index ef978d4..0000000 --- a/example/percentbarchart/custombarset.cpp +++ /dev/null @@ -1,8 +0,0 @@ -#include "custombarset.h" - -CustomBarSet::CustomBarSet(QString name, QObject *parent) : - QBarSet(name, parent) -{ - // Using signal to signal connection here. - connect(this,SIGNAL(clicked()),this,SIGNAL(toggleFloatingValues())); -} diff --git a/example/percentbarchart/custombarset.h b/example/percentbarchart/custombarset.h deleted file mode 100644 index ea8e897..0000000 --- a/example/percentbarchart/custombarset.h +++ /dev/null @@ -1,20 +0,0 @@ -#ifndef CUSTOMBARSET_H -#define CUSTOMBARSET_H - -#include - -QTCOMMERCIALCHART_USE_NAMESPACE - -class CustomBarSet : public QBarSet -{ - Q_OBJECT -public: - explicit CustomBarSet(QString name, QObject *parent = 0); - -signals: - -public slots: - -}; - -#endif // CUSTOMBARSET_H diff --git a/example/percentbarchart/main.cpp b/example/percentbarchart/main.cpp index 8399201..7680b89 100644 --- a/example/percentbarchart/main.cpp +++ b/example/percentbarchart/main.cpp @@ -4,8 +4,7 @@ #include #include #include - -#include "custombarset.h" +#include QTCOMMERCIALCHART_USE_NAMESPACE @@ -19,12 +18,11 @@ int main(int argc, char *argv[]) QPercentBarChartSeries* series = new QPercentBarChartSeries(category); - // We use custom set, which connects some signals. Could use QBarSet here if we don't need signals - CustomBarSet *set0 = new CustomBarSet("Bub"); - CustomBarSet *set1 = new CustomBarSet("Bob"); - CustomBarSet *set2 = new CustomBarSet("Guybrush"); - CustomBarSet *set3 = new CustomBarSet("Larry"); - CustomBarSet *set4 = new CustomBarSet("Zak"); + QBarSet *set0 = new QBarSet("Bub"); + QBarSet *set1 = new QBarSet("Bob"); + QBarSet *set2 = new QBarSet("Guybrush"); + QBarSet *set3 = new QBarSet("Larry"); + QBarSet *set4 = new QBarSet("Zak"); // Create some test data to chart *set0 << 1 << 2 << 3 << 4 << 5 << 6 << 7 << 8 << 9 << 10 << 11 << 12; @@ -39,6 +37,9 @@ int main(int argc, char *argv[]) series->addBarSet(set3); series->addBarSet(set4); + series->enableToolTip(true); + series->enableFloatingValues(true); + QChartView* chartView = new QChartView(&window); chartView->addSeries(series); chartView->setChartTitle("simple percent barchart"); diff --git a/example/percentbarchart/percentbarchart.pro b/example/percentbarchart/percentbarchart.pro index 9ce8259..4f19441 100644 --- a/example/percentbarchart/percentbarchart.pro +++ b/example/percentbarchart/percentbarchart.pro @@ -4,9 +4,7 @@ TARGET = percentbarchart SOURCES += main.cpp \ - chartwidget.cpp \ - custombarset.cpp + chartwidget.cpp HEADERS += \ - chartwidget.h \ - custombarset.h + chartwidget.h diff --git a/example/stackedbarchart/custombarset.cpp b/example/stackedbarchart/custombarset.cpp deleted file mode 100644 index ef978d4..0000000 --- a/example/stackedbarchart/custombarset.cpp +++ /dev/null @@ -1,8 +0,0 @@ -#include "custombarset.h" - -CustomBarSet::CustomBarSet(QString name, QObject *parent) : - QBarSet(name, parent) -{ - // Using signal to signal connection here. - connect(this,SIGNAL(clicked()),this,SIGNAL(toggleFloatingValues())); -} diff --git a/example/stackedbarchart/custombarset.h b/example/stackedbarchart/custombarset.h deleted file mode 100644 index ea8e897..0000000 --- a/example/stackedbarchart/custombarset.h +++ /dev/null @@ -1,20 +0,0 @@ -#ifndef CUSTOMBARSET_H -#define CUSTOMBARSET_H - -#include - -QTCOMMERCIALCHART_USE_NAMESPACE - -class CustomBarSet : public QBarSet -{ - Q_OBJECT -public: - explicit CustomBarSet(QString name, QObject *parent = 0); - -signals: - -public slots: - -}; - -#endif // CUSTOMBARSET_H diff --git a/example/stackedbarchart/main.cpp b/example/stackedbarchart/main.cpp index 677d789..39ceaef 100644 --- a/example/stackedbarchart/main.cpp +++ b/example/stackedbarchart/main.cpp @@ -5,8 +5,6 @@ #include #include -#include "custombarset.h" - QTCOMMERCIALCHART_USE_NAMESPACE int main(int argc, char *argv[]) @@ -19,12 +17,11 @@ int main(int argc, char *argv[]) QStackedBarChartSeries* series = new QStackedBarChartSeries(category); - // We use custom set, which connects some signals. Could use QBarSet here if we don't need signals - CustomBarSet *set0 = new CustomBarSet("Bub"); - CustomBarSet *set1 = new CustomBarSet("Bob"); - CustomBarSet *set2 = new CustomBarSet("Guybrush"); - CustomBarSet *set3 = new CustomBarSet("Larry"); - CustomBarSet *set4 = new CustomBarSet("Zak"); + QBarSet *set0 = new QBarSet("Bub"); + QBarSet *set1 = new QBarSet("Bob"); + QBarSet *set2 = new QBarSet("Guybrush"); + QBarSet *set3 = new QBarSet("Larry"); + QBarSet *set4 = new QBarSet("Zak"); // Create some test data to chart *set0 << 1 << 2 << 3 << 4 << 5 << 6 << 7 << 8 << 9 << 10 << 11 << 12; @@ -39,6 +36,9 @@ int main(int argc, char *argv[]) series->addBarSet(set3); series->addBarSet(set4); + series->enableToolTip(true); + series->enableFloatingValues(true); + QChartView* chartView = new QChartView(&window); chartView->addSeries(series); chartView->setChartTitle("simple stacked barchart"); diff --git a/example/stackedbarchart/stackedbarchart.pro b/example/stackedbarchart/stackedbarchart.pro index 405a6ab..33296fa 100644 --- a/example/stackedbarchart/stackedbarchart.pro +++ b/example/stackedbarchart/stackedbarchart.pro @@ -3,9 +3,7 @@ } TARGET = stackedbarchart SOURCES += main.cpp \ - chartwidget.cpp \ - custombarset.cpp + chartwidget.cpp HEADERS += \ - chartwidget.h \ - custombarset.h + chartwidget.h diff --git a/src/barchart/barpresenterbase.cpp b/src/barchart/barpresenterbase.cpp index 207d9c0..dd21a43 100644 --- a/src/barchart/barpresenterbase.cpp +++ b/src/barchart/barpresenterbase.cpp @@ -15,20 +15,16 @@ BarPresenterBase::BarPresenterBase(QBarChartSeries *series, QGraphicsItem *paren ,mBarDefaultWidth(20) // TODO: remove hard coding, when we have layout code ready ,mLayoutSet(false) ,mLayoutDirty(true) + ,mSeparatorsEnabled(false) ,mSeries(series) { - connect(series,SIGNAL(floatingValuesEnabled(bool)),this,SLOT(enableFloatingValues(bool))); - connect(series,SIGNAL(toolTipEnabled(bool)),this,SLOT(enableToolTip(bool))); - connect(series,SIGNAL(separatorsEnabled(bool)),this,SLOT(enableSeparators(bool))); connect(series,SIGNAL(showToolTip(QPoint,QString)),this,SLOT(showToolTip(QPoint,QString))); + connect(series,SIGNAL(separatorsEnabled(bool)),this,SLOT(enableSeparators(bool))); dataChanged(); } BarPresenterBase::~BarPresenterBase() { - disconnect(this,SLOT(enableFloatingValues(bool))); - disconnect(this,SLOT(enableToolTip(bool))); - disconnect(this,SLOT(enableSeparators(bool))); disconnect(this,SLOT(showToolTip(QPoint,QString))); delete mSeries; } @@ -79,7 +75,6 @@ void BarPresenterBase::dataChanged() childItems().append(bar); mBars.append(bar); connect(bar,SIGNAL(clicked()),set,SLOT(barClicked())); - // TODO: should the event be passed to set or not? connect(bar,SIGNAL(hoverEntered(QPoint)),set,SLOT(barHoverEntered(QPoint))); connect(bar,SIGNAL(hoverLeaved()),set,SLOT(barHoverLeaved())); } @@ -99,6 +94,7 @@ void BarPresenterBase::dataChanged() for (int i=0; isetColor(QColor(255,0,0,255)); // TODO: color for separations from theme + sep->setVisible(mSeparatorsEnabled); childItems().append(sep); mSeparators.append(sep); } @@ -143,27 +139,18 @@ void BarPresenterBase::handleGeometryChanged(const QRectF& rect) setPos(rect.topLeft()); } -void BarPresenterBase::enableFloatingValues(bool enabled) -{ - mFloatingValuesEnabled = enabled; -} - -void BarPresenterBase::enableToolTip(bool enabled) +void BarPresenterBase::showToolTip(QPoint pos, QString tip) { - mToolTipEnabled = enabled; + // TODO: cool tooltip instead of default + QToolTip::showText(pos,tip); } void BarPresenterBase::enableSeparators(bool enabled) { - mSeparatorsEnabled = enabled; -} - -void BarPresenterBase::showToolTip(QPoint pos, QString tip) -{ - if (mToolTipEnabled) { - // TODO: cool tooltip instead of default - QToolTip::showText(pos,tip); + for (int i=0; isetVisible(enabled); } + mSeparatorsEnabled = enabled; } #include "moc_barpresenterbase.cpp" diff --git a/src/barchart/barpresenterbase.h b/src/barchart/barpresenterbase.h index 7787409..720ab44 100644 --- a/src/barchart/barpresenterbase.h +++ b/src/barchart/barpresenterbase.h @@ -41,10 +41,8 @@ protected slots: void handleGeometryChanged(const QRectF& size); // Internal slots - void enableFloatingValues(bool enabled=true); // enables floating values on top of bars - void enableToolTip(bool enabled=true); // enables tooltips - void enableSeparators(bool enabled=true); // enables separators between categories void showToolTip(QPoint pos, QString tip); // shows tooltip (if enabled) + void enableSeparators(bool enabled); protected: @@ -56,8 +54,6 @@ protected: bool mLayoutSet; // True, if component has been laid out. bool mLayoutDirty; - bool mFloatingValuesEnabled; - bool mToolTipEnabled; bool mSeparatorsEnabled; // Owned diff --git a/src/barchart/qbarchartseries.cpp b/src/barchart/qbarchartseries.cpp index 4a0e31a..2a93a76 100644 --- a/src/barchart/qbarchartseries.cpp +++ b/src/barchart/qbarchartseries.cpp @@ -15,18 +15,11 @@ QBarChartSeries::QBarChartSeries(QBarCategory *category, QObject *parent) void QBarChartSeries::addBarSet(QBarSet *set) { - connect(this,SIGNAL(floatingValuesEnabled(bool)),set,SLOT(enableFloatingValues(bool))); - connect(this,SIGNAL(separatorsEnabled(bool)),set,SLOT(enableSeparators(bool))); - connect(this,SIGNAL(toolTipEnabled(bool)),set,SLOT(enableToolTip(bool))); - connect(set,SIGNAL(showToolTip(QPoint,QString)),this,SIGNAL(showToolTip(QPoint,QString))); mModel->addBarSet(set); } void QBarChartSeries::removeBarSet(QBarSet *set) { - disconnect(set,SLOT(enableFloatingValues(bool))); - disconnect(set,SLOT(enableSeparators(bool))); - disconnect(set,SLOT(enableToolTip(bool))); mModel->removeBarSet(set); } @@ -57,12 +50,32 @@ QString QBarChartSeries::label(int category) void QBarChartSeries::enableFloatingValues(bool enabled) { - emit floatingValuesEnabled(enabled); + if (enabled) { + for (int i=0; icountSets(); i++) { + QBarSet *set = mModel->setAt(i); + connect(set,SIGNAL(clicked()),set,SIGNAL(toggleFloatingValues())); + } + } else { + for (int i=0; icountSets(); i++) { + QBarSet *set = mModel->setAt(i); + disconnect(set,SIGNAL(clicked()),set,SIGNAL(toggleFloatingValues())); + } + } } void QBarChartSeries::enableToolTip(bool enabled) { - emit toolTipEnabled(enabled); + if (enabled) { + for (int i=0; icountSets(); i++) { + QBarSet *set = mModel->setAt(i); + connect(set,SIGNAL(showToolTip(QPoint,QString)),this,SIGNAL(showToolTip(QPoint,QString))); + } + } else { + for (int i=0; icountSets(); i++) { + QBarSet *set = mModel->setAt(i); + disconnect(set,SIGNAL(showToolTip(QPoint,QString)),this,SIGNAL(showToolTip(QPoint,QString))); + } + } } void QBarChartSeries::enableSeparators(bool enabled) @@ -110,7 +123,6 @@ BarChartModel& QBarChartSeries::model() return *mModel; } - #include "moc_qbarchartseries.cpp" QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/barchart/qbarchartseries.h b/src/barchart/qbarchartseries.h index bfe345a..bc0ea76 100644 --- a/src/barchart/qbarchartseries.h +++ b/src/barchart/qbarchartseries.h @@ -21,44 +21,44 @@ public: void addBarSet(QBarSet *set); // Takes ownership of set void removeBarSet(QBarSet *set); // Releases ownership, doesn't delete set int countSets(); - QBarSet* nextSet(bool getFirst=false); // Returns first set, if called with true + QBarSet* nextSet(bool getFirst=false); // Returns first set, if called with true QBarSet *setAt(int index); QList legend(); // Returns legend of series (ie. names of all sets in series) QString label(int category); -public Q_SLOTS: - // Disabled by default. Call these to change behavior. - void enableFloatingValues(bool enabled=true); // enables floating values on top of bars - void enableToolTip(bool enabled=true); // enables tooltips - void enableSeparators(bool enabled=true); // enables separators between categories - public: // TODO: Functions below this are not part of api and will be moved - // to private implementation, when we start using it (not part of api) + // to private implementation, when we start using it + // TODO: TO PIMPL ---> int countCategories(); qreal min(); qreal max(); qreal valueAt(int set, int category); qreal percentageAt(int set, int category); - qreal categorySum(int category); qreal maxCategorySum(); - BarChartModel& model(); + // <--- TO PIMPL signals: void changed(int index); // TODO: internal signals, these to private implementation. + // TODO: TO PIMPL ---> void floatingValuesEnabled(bool enabled); void toolTipEnabled(bool enabled); void separatorsEnabled(bool enabled); void showToolTip(QPoint pos, QString tip); + // <--- TO PIMPL + +public Q_SLOTS: + void enableFloatingValues(bool enabled=true); // enables floating values on top of bars + void enableToolTip(bool enabled=true); // enables tooltips + void enableSeparators(bool enabled=true); // enables separators between categories protected: BarChartModel* mModel; - }; QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/barchart/qbarset.cpp b/src/barchart/qbarset.cpp index 7fe2ef2..f27523b 100644 --- a/src/barchart/qbarset.cpp +++ b/src/barchart/qbarset.cpp @@ -7,8 +7,6 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE QBarSet::QBarSet(QString name, QObject *parent) : QObject(parent) ,mName(name) - ,mToolTipEnabled(true) // TODO: these 2 as false by default, when implementation is ready - ,mFloatingValuesEnabled(true) { } @@ -62,24 +60,6 @@ const QBrush& QBarSet::brush() const return mBrush; } -void QBarSet::enableFloatingValues(bool enabled) -{ - qDebug() << "QBarSet::enableFloatingValues"; - mFloatingValuesEnabled = enabled; -} - -void QBarSet::enableToolTip(bool enabled) -{ - qDebug() << "QBarSet::enableToolTip"; - mToolTipEnabled = enabled; -} - -void QBarSet::enableSeparators(bool enabled) -{ - qDebug() << "QBarSet::enableSeparators"; - mSeparatorsEnabled = enabled; -} - void QBarSet::barClicked() { // qDebug() << "QBarset::barClicked" << this; @@ -90,10 +70,7 @@ void QBarSet::barClicked() void QBarSet::barHoverEntered(QPoint pos) { - if (mToolTipEnabled) { - emit showToolTip(pos, mName); - } - // Emit signal to user of charts + emit showToolTip(pos, mName); emit hoverEnter(pos); } diff --git a/src/barchart/qbarset.h b/src/barchart/qbarset.h index 964144e..3c35e5c 100644 --- a/src/barchart/qbarset.h +++ b/src/barchart/qbarset.h @@ -28,22 +28,23 @@ public: const QBrush& brush() const; Q_SIGNALS: - void clicked(); // Clicked and hover signals exposed to user + void clicked(); // Clicked and hover signals exposed to user void hoverEnter(QPoint pos); void hoverLeave(); - void toggleFloatingValues(); // Private signal, TODO: move to private impl - void showToolTip(QPoint pos, QString tip); // Private signal, TODO: move to private impl -public Q_SLOTS: - void enableFloatingValues(bool enabled); // enables floating values on top of bars - void enableToolTip(bool enabled); // enables tooltips - void enableSeparators(bool enabled); // enables separators between categories + // TODO: Expose this to user or not? + // TODO: TO PIMPL ---> + void toggleFloatingValues(); + void showToolTip(QPoint pos, QString tip); // Private signal + // <--- TO PIMPL - // TODO: these slots belong to private implementation. - // These are for single bars to notify set about internal events +public Q_SLOTS: + // These are for internal communication + // TODO: TO PIMPL ---> void barClicked(); void barHoverEntered(QPoint pos); void barHoverLeaved(); + // <--- TO PIMPL private: @@ -52,10 +53,6 @@ private: QPen mPen; QBrush mBrush; - // TODO: to pimpl - bool mFloatingValuesEnabled; - bool mToolTipEnabled; - bool mSeparatorsEnabled; }; QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/barchart/separator.cpp b/src/barchart/separator.cpp index 56bacf1..4fe4155 100644 --- a/src/barchart/separator.cpp +++ b/src/barchart/separator.cpp @@ -28,10 +28,11 @@ void Separator::setSize(const QSizeF &size) void Separator::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) { - // TODO: real pen - QPen pen(mColor); - painter->setPen(pen); - painter->drawLine(mXpos,mYpos,mXpos,mHeight); + if (isVisible()) { + QPen pen(mColor); + painter->setPen(pen); + painter->drawLine(mXpos,mYpos,mXpos,mHeight); + } } QRectF Separator::boundingRect() const diff --git a/test/chartwidgettest/mainwidget.cpp b/test/chartwidgettest/mainwidget.cpp index e567452..735ecce 100644 --- a/test/chartwidgettest/mainwidget.cpp +++ b/test/chartwidgettest/mainwidget.cpp @@ -271,6 +271,9 @@ void MainWidget::addSeries(QString seriesName, int columnCount, int rowCount, QS } series->addBarSet(set); } + series->enableFloatingValues(); + series->enableToolTip(); + series->enableSeparators(false); m_chartWidget->addSeries(series); setCurrentSeries(series); }