From d062a7f38647978a6384a3d10e0541e32e57cbc9 2012-02-22 12:21:34 From: sauimone Date: 2012-02-22 12:21:34 Subject: [PATCH] updated barchart examples. minor fixes --- diff --git a/example/barchart/barchart.pro b/example/barchart/barchart.pro index 009c8c7..9b25d42 100644 --- a/example/barchart/barchart.pro +++ b/example/barchart/barchart.pro @@ -4,7 +4,9 @@ TARGET = barchart SOURCES += main.cpp \ - chartwidget.cpp + chartwidget.cpp \ + custombarset.cpp HEADERS += \ - chartwidget.h + chartwidget.h \ + custombarset.h diff --git a/example/barchart/custombarset.cpp b/example/barchart/custombarset.cpp new file mode 100644 index 0000000..b65b708 --- /dev/null +++ b/example/barchart/custombarset.cpp @@ -0,0 +1,8 @@ +#include "custombarset.h" + +CustomBarSet::CustomBarSet(QObject *parent) : + QBarSet(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 new file mode 100644 index 0000000..5b21ecf --- /dev/null +++ b/example/barchart/custombarset.h @@ -0,0 +1,20 @@ +#ifndef CUSTOMBARSET_H +#define CUSTOMBARSET_H + +#include + +QTCOMMERCIALCHART_USE_NAMESPACE + +class CustomBarSet : public QBarSet +{ + Q_OBJECT +public: + explicit CustomBarSet(QObject *parent = 0); + +signals: + +public slots: + +}; + +#endif // CUSTOMBARSET_H diff --git a/example/barchart/main.cpp b/example/barchart/main.cpp index a450fa4..0697619 100644 --- a/example/barchart/main.cpp +++ b/example/barchart/main.cpp @@ -1,10 +1,11 @@ #include #include -#include +#include #include -#include #include -#include "chartwidget.h" +#include + +#include "custombarset.h" QTCOMMERCIALCHART_USE_NAMESPACE @@ -16,13 +17,14 @@ int main(int argc, char *argv[]) QBarCategory *category = new QBarCategory; *category << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "June" << "Jul" << "Aug" << "Sep" << "Oct" << "Nov" << "Dec"; - QBarChartSeries* series0 = new QBarChartSeries(category); + QBarChartSeries* series= new QBarChartSeries(category); - QBarSet *set0 = new QBarSet; - QBarSet *set1 = new QBarSet; - QBarSet *set2 = new QBarSet; - QBarSet *set3 = new QBarSet; - QBarSet *set4 = new QBarSet; + // We use custom set, which connects some signals. Could use QBarSet here if we don't need signals + CustomBarSet *set0 = new CustomBarSet; + CustomBarSet *set1 = new CustomBarSet; + CustomBarSet *set2 = new CustomBarSet; + CustomBarSet *set3 = new CustomBarSet; + CustomBarSet *set4 = new CustomBarSet; // Create some test data to chart *set0 << 1 << 2 << 3 << 4 << 5 << 6 << 7 << 8 << 9 << 10 << 11 << 12; @@ -31,16 +33,18 @@ int main(int argc, char *argv[]) *set3 << 5 << 6 << 7 << 3 << 4 << 5 << 8 << 9 << 10 << 5 << 2 << 7; *set4 << 9 << 7 << 5 << 3 << 1 << 2 << 4 << 6 << 8 << 10 << 1 << 6; - series0->addBarSet(set0); - series0->addBarSet(set1); - series0->addBarSet(set2); - series0->addBarSet(set3); - series0->addBarSet(set4); + series->addBarSet(set0); + series->addBarSet(set1); + series->addBarSet(set2); + series->addBarSet(set3); + series->addBarSet(set4); - ChartWidget* chartWidget = new ChartWidget(&window); - chartWidget->addSeries(series0); + QChartView* chartView = new QChartView(&window); + chartView->addSeries(series); + chartView->setChartTitle("simple stacked barchart"); + chartView->setChartTheme(QChart::ChartThemeIcy); - window.setCentralWidget(chartWidget); + window.setCentralWidget(chartView); window.resize(400, 300); window.show(); diff --git a/example/percentbarchart/custombarset.cpp b/example/percentbarchart/custombarset.cpp new file mode 100644 index 0000000..b65b708 --- /dev/null +++ b/example/percentbarchart/custombarset.cpp @@ -0,0 +1,8 @@ +#include "custombarset.h" + +CustomBarSet::CustomBarSet(QObject *parent) : + QBarSet(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 new file mode 100644 index 0000000..5b21ecf --- /dev/null +++ b/example/percentbarchart/custombarset.h @@ -0,0 +1,20 @@ +#ifndef CUSTOMBARSET_H +#define CUSTOMBARSET_H + +#include + +QTCOMMERCIALCHART_USE_NAMESPACE + +class CustomBarSet : public QBarSet +{ + Q_OBJECT +public: + explicit CustomBarSet(QObject *parent = 0); + +signals: + +public slots: + +}; + +#endif // CUSTOMBARSET_H diff --git a/example/percentbarchart/main.cpp b/example/percentbarchart/main.cpp index d0d0394..f0e8da2 100644 --- a/example/percentbarchart/main.cpp +++ b/example/percentbarchart/main.cpp @@ -2,9 +2,11 @@ #include #include #include -#include "chartwidget.h" #include -#include +#include + +//#include +#include "custombarset.h" QTCOMMERCIALCHART_USE_NAMESPACE @@ -16,13 +18,14 @@ int main(int argc, char *argv[]) QBarCategory *category = new QBarCategory; *category << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "June" << "Jul" << "Aug" << "Sep" << "Oct" << "Nov" << "Dec"; - QPercentBarChartSeries* series0 = new QPercentBarChartSeries(category); + QPercentBarChartSeries* series = new QPercentBarChartSeries(category); - QBarSet *set0 = new QBarSet; - QBarSet *set1 = new QBarSet; - QBarSet *set2 = new QBarSet; - QBarSet *set3 = new QBarSet; - QBarSet *set4 = new QBarSet; + // We use custom set, which connects some signals. Could use QBarSet here if we don't need signals + CustomBarSet *set0 = new CustomBarSet; + CustomBarSet *set1 = new CustomBarSet; + CustomBarSet *set2 = new CustomBarSet; + CustomBarSet *set3 = new CustomBarSet; + CustomBarSet *set4 = new CustomBarSet; // Create some test data to chart *set0 << 1 << 2 << 3 << 4 << 5 << 6 << 7 << 8 << 9 << 10 << 11 << 12; @@ -31,16 +34,18 @@ int main(int argc, char *argv[]) *set3 << 5 << 6 << 7 << 3 << 4 << 5 << 8 << 9 << 10 << 5 << 2 << 7; *set4 << 9 << 7 << 5 << 3 << 1 << 2 << 4 << 6 << 8 << 10 << 1 << 6; - series0->addBarSet(set0); - series0->addBarSet(set1); - series0->addBarSet(set2); - series0->addBarSet(set3); - series0->addBarSet(set4); + series->addBarSet(set0); + series->addBarSet(set1); + series->addBarSet(set2); + series->addBarSet(set3); + series->addBarSet(set4); - ChartWidget* chartWidget = new ChartWidget(&window); - chartWidget->addSeries(series0); + QChartView* chartView = new QChartView(&window); + chartView->addSeries(series); + chartView->setChartTitle("simple percent barchart"); + chartView->setChartTheme(QChart::ChartThemeIcy); - window.setCentralWidget(chartWidget); + window.setCentralWidget(chartView); window.resize(400, 300); window.show(); diff --git a/example/percentbarchart/percentbarchart.pro b/example/percentbarchart/percentbarchart.pro index 4f19441..9ce8259 100644 --- a/example/percentbarchart/percentbarchart.pro +++ b/example/percentbarchart/percentbarchart.pro @@ -4,7 +4,9 @@ TARGET = percentbarchart SOURCES += main.cpp \ - chartwidget.cpp + chartwidget.cpp \ + custombarset.cpp HEADERS += \ - chartwidget.h + chartwidget.h \ + custombarset.h diff --git a/example/stackedbarchart/custombarset.cpp b/example/stackedbarchart/custombarset.cpp new file mode 100644 index 0000000..b65b708 --- /dev/null +++ b/example/stackedbarchart/custombarset.cpp @@ -0,0 +1,8 @@ +#include "custombarset.h" + +CustomBarSet::CustomBarSet(QObject *parent) : + QBarSet(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 new file mode 100644 index 0000000..5b21ecf --- /dev/null +++ b/example/stackedbarchart/custombarset.h @@ -0,0 +1,20 @@ +#ifndef CUSTOMBARSET_H +#define CUSTOMBARSET_H + +#include + +QTCOMMERCIALCHART_USE_NAMESPACE + +class CustomBarSet : public QBarSet +{ + Q_OBJECT +public: + explicit CustomBarSet(QObject *parent = 0); + +signals: + +public slots: + +}; + +#endif // CUSTOMBARSET_H diff --git a/example/stackedbarchart/main.cpp b/example/stackedbarchart/main.cpp index cf6294c..b5db2d9 100644 --- a/example/stackedbarchart/main.cpp +++ b/example/stackedbarchart/main.cpp @@ -1,11 +1,12 @@ #include #include -#include +#include #include -#include "chartwidget.h" #include #include +#include "custombarset.h" + QTCOMMERCIALCHART_USE_NAMESPACE int main(int argc, char *argv[]) @@ -16,31 +17,34 @@ int main(int argc, char *argv[]) QBarCategory *category = new QBarCategory; *category << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "June" << "Jul" << "Aug" << "Sep" << "Oct" << "Nov" << "Dec"; - QStackedBarChartSeries* series0 = new QStackedBarChartSeries(category); + QStackedBarChartSeries* series = new QStackedBarChartSeries(category); - QBarSet *set0 = new QBarSet; - QBarSet *set1 = new QBarSet; - QBarSet *set2 = new QBarSet; - QBarSet *set3 = new QBarSet; - QBarSet *set4 = new QBarSet; + // We use custom set, which connects some signals. Could use QBarSet here if we don't need signals + CustomBarSet *set0 = new CustomBarSet; + CustomBarSet *set1 = new CustomBarSet; + CustomBarSet *set2 = new CustomBarSet; + CustomBarSet *set3 = new CustomBarSet; + CustomBarSet *set4 = new CustomBarSet; // Create some test data to chart *set0 << 1 << 2 << 3 << 4 << 5 << 6 << 7 << 8 << 9 << 10 << 11 << 12; -// *set1 << 5 << 0 << 0 << 4 << 0 << 7 << 8 << 9 << 9 << 0 << 4 << 2; -// *set2 << 3 << 5 << 8 << 13 << 8 << 5 << 3 << 2 << 1 << 1 << 3 << 5; -// *set3 << 5 << 6 << 7 << 3 << 4 << 5 << 8 << 9 << 10 << 5 << 2 << 7; + *set1 << 5 << 0 << 0 << 4 << 0 << 7 << 8 << 9 << 9 << 0 << 4 << 2; + *set2 << 3 << 5 << 8 << 13 << 8 << 5 << 3 << 2 << 1 << 1 << 3 << 5; + *set3 << 5 << 6 << 7 << 3 << 4 << 5 << 8 << 9 << 10 << 5 << 2 << 7; *set4 << 9 << 7 << 5 << 3 << 1 << 2 << 4 << 6 << 8 << 10 << 1 << 6; - series0->addBarSet(set0); -// series0->addBarSet(set1); -// series0->addBarSet(set2); -// series0->addBarSet(set3); - series0->addBarSet(set4); + series->addBarSet(set0); + series->addBarSet(set1); + series->addBarSet(set2); + series->addBarSet(set3); + series->addBarSet(set4); - ChartWidget* chartWidget = new ChartWidget(&window); - chartWidget->addSeries(series0); + QChartView* chartView = new QChartView(&window); + chartView->addSeries(series); + chartView->setChartTitle("simple stacked barchart"); + chartView->setChartTheme(QChart::ChartThemeIcy); - window.setCentralWidget(chartWidget); + window.setCentralWidget(chartView); window.resize(400, 300); window.show(); diff --git a/example/stackedbarchart/stackedbarchart.pro b/example/stackedbarchart/stackedbarchart.pro index 33296fa..405a6ab 100644 --- a/example/stackedbarchart/stackedbarchart.pro +++ b/example/stackedbarchart/stackedbarchart.pro @@ -3,7 +3,9 @@ } TARGET = stackedbarchart SOURCES += main.cpp \ - chartwidget.cpp + chartwidget.cpp \ + custombarset.cpp HEADERS += \ - chartwidget.h + chartwidget.h \ + custombarset.h diff --git a/src/barchart/bar.cpp b/src/barchart/bar.cpp index 941c5be..aa9c629 100644 --- a/src/barchart/bar.cpp +++ b/src/barchart/bar.cpp @@ -72,6 +72,16 @@ void Bar::mousePressEvent(QGraphicsSceneMouseEvent* /*event*/) emit clicked(); } +void Bar::hoverEnterEvent(QGraphicsSceneHoverEvent* /*event*/) +{ + emit hoverEntered(); +} + +void Bar::hoverLeaveEvent(QGraphicsSceneHoverEvent* /*event*/) +{ + emit hoverLeaved(); +} + #include "moc_bar_p.cpp" QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/barchart/bar_p.h b/src/barchart/bar_p.h index 6b2c13a..842a5a7 100644 --- a/src/barchart/bar_p.h +++ b/src/barchart/bar_p.h @@ -27,13 +27,16 @@ public: // from ChartItem public: // From QGraphicsItem - void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0); QRectF boundingRect() const; void mousePressEvent(QGraphicsSceneMouseEvent *event); + void hoverEnterEvent(QGraphicsSceneHoverEvent *event); + void hoverLeaveEvent(QGraphicsSceneHoverEvent *event); Q_SIGNALS: void clicked(); + void hoverEntered(); + void hoverLeaved(); private: diff --git a/src/barchart/barpresenter.cpp b/src/barchart/barpresenter.cpp index 592f997..bf6eaeb 100644 --- a/src/barchart/barpresenter.cpp +++ b/src/barchart/barpresenter.cpp @@ -76,11 +76,14 @@ void BarPresenter::layoutChanged() // TODO: remove hard coding, apply layout value->resize(100,50); - value->setPos(xPos + mBarDefaultWidth/2, yPos-barHeight/2); + value->setPos(xPos, yPos-barHeight/2); value->setPen(QPen(QColor(255,255,255,255))); - QString vString(QString::number(mModel.valueAt(set,category))); - value->setValueString(vString); + if (mModel.valueAt(set,category) != 0) { + value->setValueString(QString::number(mModel.valueAt(set,category))); + } else { + value->setValueString(QString("")); + } itemIndex++; xPos += mBarDefaultWidth; diff --git a/src/barchart/barpresenterbase.cpp b/src/barchart/barpresenterbase.cpp index 75d5782..d719868 100644 --- a/src/barchart/barpresenterbase.cpp +++ b/src/barchart/barpresenterbase.cpp @@ -70,6 +70,8 @@ void BarPresenterBase::dataChanged() childItems().append(bar); mBars.append(bar); connect(bar,SIGNAL(clicked()),set,SLOT(barClicked())); + connect(bar,SIGNAL(hoverEntered()),set,SLOT(barHoverEntered())); + connect(bar,SIGNAL(hoverLeaved()),set,SLOT(barHoverLeaved())); } } @@ -98,7 +100,7 @@ void BarPresenterBase::dataChanged() BarValue *value = new BarValue(*set, this); childItems().append(value); mFloatingValues.append(value); - connect(set,SIGNAL(clicked()),value,SLOT(toggleVisible())); + connect(set,SIGNAL(toggleFloatingValues()),value,SLOT(toggleVisible())); } } diff --git a/src/barchart/percentbarpresenter.cpp b/src/barchart/percentbarpresenter.cpp index 7169a17..81afb6e 100644 --- a/src/barchart/percentbarpresenter.cpp +++ b/src/barchart/percentbarpresenter.cpp @@ -84,12 +84,16 @@ void PercentBarPresenter::layoutChanged() BarValue* value = mFloatingValues.at(itemIndex); // TODO: remove hard coding, apply layout - value->setPos(xPos + mBarDefaultWidth/2, yPos-barHeight/2); + value->setPos(xPos, yPos-barHeight/2); value->setPen(QPen(QColor(255,255,255,255))); - QString vString(QString::number(mModel.percentageAt(set,category) * 100)); - vString.append("%"); - value->setValueString(vString); + if (mModel.valueAt(set,category) != 0) { + QString vString(QString::number(mModel.percentageAt(set,category) * 100)); + vString.append("%"); + value->setValueString(vString); + } else { + value->setValueString(QString("")); + } itemIndex++; yPos -= barHeight; diff --git a/src/barchart/qbarset.cpp b/src/barchart/qbarset.cpp index 1548c2d..fa72ab1 100644 --- a/src/barchart/qbarset.cpp +++ b/src/barchart/qbarset.cpp @@ -3,7 +3,8 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE -QBarSet::QBarSet() +QBarSet::QBarSet(QObject *parent) + : QObject(parent) { mFloatingValuesVisible = false; } @@ -71,5 +72,17 @@ void QBarSet::barClicked() emit clicked(); // Notify that set has been clicked } +void QBarSet::barHoverEntered() +{ + qDebug() << "QBarset::barHoverEntered" << this; + emit hoverEnter(); +} + +void QBarSet::barHoverLeaved() +{ + qDebug() << "QBarset::barHoverLeaved" << this; + emit hoverLeave(); +} + #include "moc_qbarset.cpp" QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/barchart/qbarset.h b/src/barchart/qbarset.h index 889881a..e84c13a 100644 --- a/src/barchart/qbarset.h +++ b/src/barchart/qbarset.h @@ -1,7 +1,7 @@ #ifndef QBARSET_H #define QBARSET_H -#include "qchartglobal.h" +#include #include #include @@ -11,7 +11,7 @@ class QTCOMMERCIALCHART_EXPORT QBarSet : public QObject { Q_OBJECT public: - QBarSet(); + QBarSet(QObject *parent = 0); void setName(QString name); QString name(); @@ -31,13 +31,14 @@ public: Q_SIGNALS: void clicked(); -/* void hoverEnter(); void hoverLeave(); -*/ + void toggleFloatingValues(); public Q_SLOTS: void barClicked(); + void barHoverEntered(); + void barHoverLeaved(); private: diff --git a/src/barchart/qstackedbarchartseries.h b/src/barchart/qstackedbarchartseries.h index 167a38a..df7c905 100644 --- a/src/barchart/qstackedbarchartseries.h +++ b/src/barchart/qstackedbarchartseries.h @@ -20,12 +20,12 @@ public: // from QChartSeries virtual QChartSeriesType type() const { return QChartSeries::SeriesTypeStackedBar; } + // Set handling void addBarSet(QBarSet *set); // Takes ownership void removeBarSet(QBarSet *set); // Also deletes the set, if set is owned. int countSets(); QBarSet* nextSet(bool first=false); // Returns first set, if called with true - //TODO: //QList legend(); // Returns legend of series (ie. names of all sets in series) // TODO: Functions below this are not part of api and will be moved diff --git a/src/barchart/stackedbarpresenter.cpp b/src/barchart/stackedbarpresenter.cpp index e5dafa0..8816756 100644 --- a/src/barchart/stackedbarpresenter.cpp +++ b/src/barchart/stackedbarpresenter.cpp @@ -88,11 +88,14 @@ void StackedBarPresenter::layoutChanged() // TODO: remove hard coding, apply layout value->resize(100,50); - value->setPos(xPos + mBarDefaultWidth/2, yPos-barHeight/2); + value->setPos(xPos, yPos-barHeight/2); value->setPen(QPen(QColor(255,255,255,255))); - QString vString(QString::number(mModel.valueAt(set,category))); - value->setValueString(vString); + if (mModel.valueAt(set,category) != 0) { + value->setValueString(QString::number(mModel.valueAt(set,category))); + } else { + value->setValueString(QString("")); + } itemIndex++; yPos -= barHeight;