diff --git a/src/barchart/barchart.pri b/src/barchart/barchart.pri index 3cf27e7..96c41d7 100644 --- a/src/barchart/barchart.pri +++ b/src/barchart/barchart.pri @@ -12,7 +12,6 @@ SOURCES += \ $$PWD/qstackedbarseries.cpp \ $$PWD/qgroupedbarseries.cpp \ $$PWD/stackedbarchartitem.cpp \ - $$PWD/barlabel.cpp \ $$PWD/qbarmodelmapper.cpp PRIVATE_HEADERS += \ @@ -21,7 +20,6 @@ PRIVATE_HEADERS += \ $$PWD/percentbarchartitem_p.h \ $$PWD/stackedbarchartitem_p.h \ $$PWD/groupedbarchartitem_p.h \ - $$PWD/barlabel_p.h \ $$PWD/qbarset_p.h \ $$PWD/qbarseries_p.h \ $$PWD/qstackedbarseries_p.h\ diff --git a/src/barchart/barchartitem.cpp b/src/barchart/barchartitem.cpp index c75555b..a340bf4 100644 --- a/src/barchart/barchartitem.cpp +++ b/src/barchart/barchartitem.cpp @@ -20,7 +20,6 @@ #include "barchartitem_p.h" #include "bar_p.h" -#include "barlabel_p.h" #include "qbarset.h" #include "qbarset_p.h" #include "qbarseries.h" @@ -41,6 +40,7 @@ BarChartItem::BarChartItem(QBarSeries *series, ChartPresenter *presenter) : setFlag(ItemClipsChildrenToShape); connect(series->d_func(), SIGNAL(updatedBars()), this, SLOT(handleLayoutChanged())); connect(series->d_func(), SIGNAL(restructuredBars()), this, SLOT(handleModelChanged())); + connect(series->d_func(), SIGNAL(labelsVisibleChanged(bool)), this, SLOT(labelsVisibleChanged(bool))); setZValue(ChartPresenter::BarSeriesZValue); dataChanged(); } @@ -71,11 +71,15 @@ void BarChartItem::dataChanged() m_labels.clear(); m_layout.clear(); + bool labelsVisible = m_series->isLabelsVisible(); + // Create new graphic items for bars for (int c = 0; c < m_series->categoryCount(); c++) { QString category = m_series->d_func()->categoryName(c); for (int s = 0; s < m_series->barsetCount(); s++) { QBarSet *set = m_series->d_func()->barsetAt(s); + + // Bars Bar *bar = new Bar(set,category,this); m_bars.append(bar); connect(bar, SIGNAL(clicked(QString)), set, SIGNAL(clicked(QString))); @@ -83,18 +87,13 @@ void BarChartItem::dataChanged() connect(bar, SIGNAL(hovered(bool)), set, SIGNAL(hovered(bool))); connect(bar, SIGNAL(hovered(QBarSet*,bool)), m_series, SIGNAL(hovered(QBarSet*,bool))); m_layout.append(QRectF(0, 0, 0, 0)); - } - } - // Create labels - for (int category = 0; category < m_series->categoryCount(); category++) { - for (int s = 0; s < m_series->barsetCount(); s++) { - QBarSet *set = m_series->d_func()->barsetAt(s); - BarLabel *value = new BarLabel(*set, this); - m_labels.append(value); - connect(set->d_ptr.data(),SIGNAL(labelsVisibleChanged(bool)),value,SLOT(labelsVisibleChanged(bool))); + // Labels + QGraphicsSimpleTextItem *label = new QGraphicsSimpleTextItem(this); + label->setVisible(labelsVisible); + m_labels.append(label); } - } + } // TODO: Is this the right place to call it? // presenter()->chartTheme()->decorate(m_series, presenter()->dataSet()->seriesIndex(m_series)); @@ -133,7 +132,7 @@ QVector BarChartItem::calculateLayout() bar->setPen(barSet->pen()); bar->setBrush(barSet->brush()); - BarLabel* label = m_labels.at(itemIndex); + QGraphicsSimpleTextItem* label = m_labels.at(itemIndex); if (!qFuzzyIsNull(barSet->at(category).y())) { label->setText(QString::number(barSet->at(category).y())); @@ -205,6 +204,14 @@ void BarChartItem::handleLayoutChanged() update(); } +void BarChartItem::labelsVisibleChanged(bool visible) +{ + foreach (QGraphicsSimpleTextItem* label, m_labels) { + label->setVisible(visible); + } + update(); +} + #include "moc_barchartitem_p.cpp" QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/barchart/barchartitem_p.h b/src/barchart/barchartitem_p.h index ed12a33..e0bc4bd 100644 --- a/src/barchart/barchartitem_p.h +++ b/src/barchart/barchartitem_p.h @@ -29,7 +29,6 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE class Bar; -class BarLabel; class QAxisCategories; class QChart; @@ -61,6 +60,7 @@ public Q_SLOTS: void handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY); void handleGeometryChanged(const QRectF &size); void handleLayoutChanged(); + void labelsVisibleChanged(bool visible); protected: @@ -76,7 +76,7 @@ protected: // Not owned. QBarSeries *m_series; QList m_bars; - QList m_labels; + QList m_labels; }; QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/barchart/barlabel.cpp b/src/barchart/barlabel.cpp deleted file mode 100644 index 9757f52..0000000 --- a/src/barchart/barlabel.cpp +++ /dev/null @@ -1,104 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2012 Digia Plc -** All rights reserved. -** For any questions to Digia, please use contact form at http://qt.digia.com -** -** This file is part of the Qt Commercial Charts Add-on. -** -** $QT_BEGIN_LICENSE$ -** Licensees holding valid Qt Commercial licenses may use this file in -** accordance with the Qt Commercial License Agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and Digia. -** -** If you have questions regarding the use of this file, please use -** contact form at http://qt.digia.com -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#include "barlabel_p.h" -#include "chartpresenter_p.h" -#include -#include -#include -#include - -QTCOMMERCIALCHART_BEGIN_NAMESPACE - -BarLabel::BarLabel(QBarSet &barSet, QGraphicsItem *parent) : QGraphicsObject(parent), - m_barSet(barSet), - m_textItem(new QGraphicsSimpleTextItem(this)) -{ -// connect(&set,SIGNAL(labelsVisibleChanged(bool)),value,SLOT(labelsVisibleChanged(bool))); - setZValue(ChartPresenter::BarSeriesZValue + 1); - setVisible(barSet.labelsVisible()); - setPen(Qt::NoPen); - setBrush(Qt::SolidPattern); -} - -void BarLabel::setText(QString str) -{ - m_textItem->setText(str); -} - -QString BarLabel::text() const -{ - return m_textItem->text(); -} - -void BarLabel::setPen(const QPen &pen) -{ - m_textItem->setPen(pen); -} - -QPen BarLabel::pen() const -{ - return m_textItem->pen(); -} - -void BarLabel::setBrush(const QBrush &brush) -{ - m_textItem->setBrush(brush); -} - -QBrush BarLabel::brush() const -{ - return m_textItem->brush(); -} - -void BarLabel::setFont(const QFont &font) -{ - m_textItem->setFont(font); -} - -QFont BarLabel::font() const -{ - return m_textItem->font(); -} - -void BarLabel::setPos(qreal x, qreal y) -{ - m_textItem->setPos(x,y); -} - -void BarLabel::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) -{ - Q_UNUSED(painter) - Q_UNUSED(option) - Q_UNUSED(widget) -} - -QRectF BarLabel::boundingRect() const -{ - return m_textItem->boundingRect(); -} - -void BarLabel::labelsVisibleChanged(bool visible) -{ - setVisible(visible); -} - -#include "moc_barlabel_p.cpp" -QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/barchart/barlabel_p.h b/src/barchart/barlabel_p.h deleted file mode 100644 index ee8f4b7..0000000 --- a/src/barchart/barlabel_p.h +++ /dev/null @@ -1,70 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2012 Digia Plc -** All rights reserved. -** For any questions to Digia, please use contact form at http://qt.digia.com -** -** This file is part of the Qt Commercial Charts Add-on. -** -** $QT_BEGIN_LICENSE$ -** Licensees holding valid Qt Commercial licenses may use this file in -** accordance with the Qt Commercial License Agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and Digia. -** -** If you have questions regarding the use of this file, please use -** contact form at http://qt.digia.com -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#ifndef BARLABEL_P_H -#define BARLABEL_P_H - -#include "qchartglobal.h" -#include -#include -class QGraphicsSimpleTextItem; - -QTCOMMERCIALCHART_BEGIN_NAMESPACE - -class QBarSet; - -// Visual class for bar values -// By default these are not visible. -class BarLabel : public QGraphicsObject -{ - Q_OBJECT -public: - explicit BarLabel(QBarSet &barSet, QGraphicsItem *parent = 0); - - void setText(QString str); - QString text() const; - - void setPen(const QPen &pen); - QPen pen() const; - - void setBrush(const QBrush &brush); - QBrush brush() const; - - void setFont(const QFont &font); - QFont font() const; - - void setPos(qreal x, qreal y); - - // From QGraphicsItem - void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); - QRectF boundingRect() const; - -public Q_SLOTS: - void labelsVisibleChanged(bool visible); - -private: - - QBarSet &m_barSet; - QGraphicsSimpleTextItem *m_textItem; -}; - -QTCOMMERCIALCHART_END_NAMESPACE - -#endif // BARLABEL_P_H diff --git a/src/barchart/groupedbarchartitem.cpp b/src/barchart/groupedbarchartitem.cpp index 23a7332..471c82c 100644 --- a/src/barchart/groupedbarchartitem.cpp +++ b/src/barchart/groupedbarchartitem.cpp @@ -20,7 +20,6 @@ #include "groupedbarchartitem_p.h" #include "bar_p.h" -#include "barlabel_p.h" #include "qbarset_p.h" #include "qbarseries_p.h" #include "qbarset.h" @@ -67,7 +66,7 @@ QVector GroupedBarChartItem::calculateLayout() bar->setPen(barSet->pen()); bar->setBrush(barSet->brush()); - BarLabel* label = m_labels.at(itemIndex); + QGraphicsSimpleTextItem* label = m_labels.at(itemIndex); if (!qFuzzyIsNull(barSet->at(category).y())) { label->setText(QString::number(barSet->at(category).y())); diff --git a/src/barchart/percentbarchartitem.cpp b/src/barchart/percentbarchartitem.cpp index ad45202..7cf1154 100644 --- a/src/barchart/percentbarchartitem.cpp +++ b/src/barchart/percentbarchartitem.cpp @@ -20,7 +20,6 @@ #include "percentbarchartitem_p.h" #include "bar_p.h" -#include "barlabel_p.h" #include "qbarseries_p.h" #include "qbarset.h" #include @@ -67,7 +66,7 @@ QVector PercentBarChartItem::calculateLayout() QRectF rect(xPos, yPos-barHeight, barWidth, barHeight); layout.append(rect); - BarLabel* label = m_labels.at(itemIndex); + QGraphicsSimpleTextItem* label = m_labels.at(itemIndex); if (!qFuzzyIsNull(m_series->d_func()->valueAt(set,category))) { int p = m_series->d_func()->percentageAt(set,category) * 100; diff --git a/src/barchart/qbarseries.cpp b/src/barchart/qbarseries.cpp index cd1c0af..cbe574b 100644 --- a/src/barchart/qbarseries.cpp +++ b/src/barchart/qbarseries.cpp @@ -230,16 +230,25 @@ QBarCategories QBarSeries::categories() const */ void QBarSeries::setLabelsVisible(bool visible) { - foreach (QBarSet* s, barSets()) { - s->setLabelsVisible(visible); + Q_D(QBarSeries); + if (d->m_labelsVisible != visible) { + d->m_labelsVisible = visible; + emit d->updatedBars(); } } +bool QBarSeries::isLabelsVisible() const +{ + Q_D(const QBarSeries); + return d->m_labelsVisible; +} + /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// QBarSeriesPrivate::QBarSeriesPrivate(QBarSeries *q) : QAbstractSeriesPrivate(q), - m_barMargin(0.05) // Default value is 5% of category width + m_barMargin(0.05), // Default value is 5% of category width + m_labelsVisible(false) { } diff --git a/src/barchart/qbarseries.h b/src/barchart/qbarseries.h index 6bb406e..d39dc62 100644 --- a/src/barchart/qbarseries.h +++ b/src/barchart/qbarseries.h @@ -53,6 +53,7 @@ public: QBarCategories categories() const; void setLabelsVisible(bool visible = true); + bool isLabelsVisible() const; protected: explicit QBarSeries(QBarSeriesPrivate &d,QObject *parent = 0); diff --git a/src/barchart/qbarseries_p.h b/src/barchart/qbarseries_p.h index 102d817..cb01f61 100644 --- a/src/barchart/qbarseries_p.h +++ b/src/barchart/qbarseries_p.h @@ -44,6 +44,7 @@ Q_SIGNALS: void updatedBars(); void restructuredBars(); void categoriesUpdated(); + void labelsVisibleChanged(bool visible); private Q_SLOTS: void barsetChanged(); @@ -52,6 +53,8 @@ protected: QList m_barSets; QBarCategories m_categories; qreal m_barMargin; + bool m_labelsVisible; + private: Q_DECLARE_PUBLIC(QBarSeries) }; diff --git a/src/barchart/qbarset.cpp b/src/barchart/qbarset.cpp index 1871ab3..e799691 100644 --- a/src/barchart/qbarset.cpp +++ b/src/barchart/qbarset.cpp @@ -324,32 +324,11 @@ QFont QBarSet::labelFont() const return d_ptr->m_labelFont; } -/*! - Sets visibility of bar labels. If \a visible is true, labels are drawn on top of barsets. -*/ - -void QBarSet::setLabelsVisible(bool visible) -{ - if(d_ptr->m_labelsVisible!=visible) { - d_ptr->m_labelsVisible = visible; - emit d_ptr->labelsVisibleChanged(visible); - } -} - -/*! - Returns the visibility of values -*/ -bool QBarSet::labelsVisible() const -{ - return d_ptr->m_labelsVisible; -} - //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// QBarSetPrivate::QBarSetPrivate(const QString name, QBarSet *parent) : QObject(parent), q_ptr(parent), - m_name(name), - m_labelsVisible(false) + m_name(name) { } diff --git a/src/barchart/qbarset.h b/src/barchart/qbarset.h index 3235c08..c070917 100644 --- a/src/barchart/qbarset.h +++ b/src/barchart/qbarset.h @@ -41,16 +41,16 @@ public: void setName(const QString name); QString name() const; - void append(const QPointF value); // Appends bar with x-value - void append(const QList values); // Same with list - void append(const qreal value); // TODO: change so, that count becomes x-value - void append(const QList values); // Append list of values. Using index as x-value + void append(const QPointF value); + void append(const QList values); + void append(const qreal value); + void append(const QList values); - QBarSet& operator << (const qreal &value); // TODO: change implementations so, that count becomes x-value - QBarSet& operator << (const QPointF &value); // Appends bar with x-value + QBarSet& operator << (const qreal &value); + QBarSet& operator << (const QPointF &value); - void insert(const int index, const qreal value); // TODO: internal reindexing (what happens, if there are points with real x values?) - void remove(const int index); // TODO: internal reindexing (what happens, if there are points with real x values?) + void insert(const int index, const qreal value); + void remove(const int index); void replace(const int index, const qreal value); QPointF at(const int index) const; QPointF operator [] (const int index) const; @@ -72,9 +72,6 @@ public: void setLabelFont(const QFont &font); QFont labelFont() const; - void setLabelsVisible(bool visible = true); - bool labelsVisible() const; - Q_SIGNALS: void clicked(QString category); void hovered(bool status); diff --git a/src/barchart/qbarset_p.h b/src/barchart/qbarset_p.h index a992436..900159f 100644 --- a/src/barchart/qbarset_p.h +++ b/src/barchart/qbarset_p.h @@ -3,6 +3,9 @@ #include "qbarset.h" #include +#include +#include +#include QTCOMMERCIALCHART_BEGIN_NAMESPACE @@ -18,7 +21,6 @@ Q_SIGNALS: void clicked(QString category); void restructuredBars(); void updatedBars(); - void labelsVisibleChanged(bool visible); public: QBarSet * const q_ptr; @@ -29,7 +31,6 @@ public: QPen m_labelPen; QBrush m_labelBrush; QFont m_labelFont; - bool m_labelsVisible; friend class QBarSet; }; diff --git a/src/barchart/stackedbarchartitem.cpp b/src/barchart/stackedbarchartitem.cpp index e87d9b7..604e0e8 100644 --- a/src/barchart/stackedbarchartitem.cpp +++ b/src/barchart/stackedbarchartitem.cpp @@ -20,7 +20,6 @@ #include "stackedbarchartitem_p.h" #include "bar_p.h" -#include "barlabel_p.h" #include "qbarset_p.h" #include "qbarseries_p.h" #include "qbarset.h" @@ -64,7 +63,7 @@ QVector StackedBarChartItem::calculateLayout() QRectF rect(xPos, yPos-barHeight, barWidth, barHeight); layout.append(rect); - BarLabel* label = m_labels.at(itemIndex); + QGraphicsSimpleTextItem* label = m_labels.at(itemIndex); if (!qFuzzyIsNull(barSet->at(category).y())) { label->setText(QString::number(barSet->at(category).y())); diff --git a/tests/auto/qbarseries/tst_qbarseries.cpp b/tests/auto/qbarseries/tst_qbarseries.cpp index bb2bc3a..0474fcd 100644 --- a/tests/auto/qbarseries/tst_qbarseries.cpp +++ b/tests/auto/qbarseries/tst_qbarseries.cpp @@ -403,27 +403,23 @@ void tst_QBarSeries::setLabelsVisible_data() void tst_QBarSeries::setLabelsVisible() { // labels should be invisible by default - foreach (QBarSet* s, m_testSets) { - QVERIFY(s->labelsVisible() == false); - } + QVERIFY(m_barseries->isLabelsVisible() == false); + QVERIFY(m_barseries_with_sets->isLabelsVisible() == false); // turn labels to visible m_barseries_with_sets->setLabelsVisible(true); - foreach (QBarSet* s, m_testSets) { - QVERIFY(s->labelsVisible() == true); - } + // TODO: test the signal + QVERIFY(m_barseries_with_sets->isLabelsVisible() == true); // turn labels to invisible m_barseries_with_sets->setLabelsVisible(false); - foreach (QBarSet* s, m_testSets) { - QVERIFY(s->labelsVisible() == false); - } + // TODO: test the signal + QVERIFY(m_barseries_with_sets->isLabelsVisible() == false); // without parameter, should turn labels to visible m_barseries_with_sets->setLabelsVisible(); - foreach (QBarSet* s, m_testSets) { - QVERIFY(s->labelsVisible() == true); - } + // TODO: test the signal + QVERIFY(m_barseries_with_sets->isLabelsVisible() == true); } void tst_QBarSeries::mouseclicked_data()