diff --git a/src/charts/legend/legendmarkeritem.cpp b/src/charts/legend/legendmarkeritem.cpp index ae5eb5e..125199e 100644 --- a/src/charts/legend/legendmarkeritem.cpp +++ b/src/charts/legend/legendmarkeritem.cpp @@ -124,9 +124,14 @@ void LegendMarkerItem::setGeometry(const QRectF &rect) qreal width = rect.width(); qreal x = m_margin + m_markerRect.width() + m_space + m_margin; QRectF truncatedRect; + const QString html = ChartPresenter::truncatedText(m_textItem->font(), m_label, qreal(0.0), + width - x, rect.height(), truncatedRect); + m_textItem->setHtml(html); + if (m_marker->m_legend->showToolTips() && html != m_label) + m_textItem->setToolTip(m_label); + else + m_textItem->setToolTip(QString()); - m_textItem->setHtml(ChartPresenter::truncatedText(m_textItem->font(), m_label, qreal(0.0), - width - x, rect.height(), truncatedRect)); m_textItem->setTextWidth(truncatedRect.width()); qreal y = qMax(m_markerRect.height() + 2 * m_margin, truncatedRect.height() + 2 * m_margin); @@ -195,6 +200,15 @@ void LegendMarkerItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event) emit m_marker->q_ptr->hovered(false); } +QString LegendMarkerItem::displayedLabel() const +{ + return m_textItem->toHtml(); +} + +void LegendMarkerItem::setToolTip(const QString &tip) +{ + m_textItem->setToolTip(tip); +} #include "moc_legendmarkeritem_p.cpp" diff --git a/src/charts/legend/legendmarkeritem_p.h b/src/charts/legend/legendmarkeritem_p.h index 4b33ae3..870cfb2 100644 --- a/src/charts/legend/legendmarkeritem_p.h +++ b/src/charts/legend/legendmarkeritem_p.h @@ -83,6 +83,8 @@ public: void hoverEnterEvent(QGraphicsSceneHoverEvent *event); void hoverLeaveEvent(QGraphicsSceneHoverEvent *event); + QString displayedLabel() const; + void setToolTip(const QString &tooltip); protected: QLegendMarkerPrivate *m_marker; // Knows QRectF m_markerRect; diff --git a/src/charts/legend/qlegend.cpp b/src/charts/legend/qlegend.cpp index 975de95..4f75747 100644 --- a/src/charts/legend/qlegend.cpp +++ b/src/charts/legend/qlegend.cpp @@ -164,6 +164,19 @@ QT_CHARTS_BEGIN_NAMESPACE */ /*! + \property QLegend::showToolTips + Whether tooltips are shown when the text is truncated. This is false by default. + + This will not have any effect when used in QML. +*/ + +/*! + \qmlproperty bool Legend::showToolTips + Whether tooltips are shown when the text is truncated. This is false by default. + This currently has no effect as there is no support for tooltips in QML. +*/ + +/*! \fn void QLegend::backgroundVisibleChanged(bool) The visibility of the legend background changed to \a visible. */ @@ -193,6 +206,11 @@ QT_CHARTS_BEGIN_NAMESPACE The use of reverse order for the markers in legend is changed to \a reverseMarkers. */ +/*! + \fn void QLegend::showToolTipsChanged(bool showToolTips) + This signal is emitted when the visibility of tooltips is changed to \a showToolTips. +*/ + QLegend::QLegend(QChart *chart): QGraphicsWidget(chart), d_ptr(new QLegendPrivate(chart->d_ptr->m_presenter, chart, this)) { @@ -455,6 +473,31 @@ void QLegend::setReverseMarkers(bool reverseMarkers) } /*! + Returns whether the tooltips are shown or not for the legend labels + when they are elided. +*/ + +bool QLegend::showToolTips() const +{ + return d_ptr->m_showToolTips; +} + +/*! + When \a show is true, the legend labels will show a tooltip when + the mouse hovers over them if the label itself is shown elided. + This is false by default. +*/ + +void QLegend::setShowToolTips(bool show) +{ + if (d_ptr->m_showToolTips != show) { + d_ptr->m_showToolTips = show; + d_ptr->updateToolTips(); + emit showToolTipsChanged(show); + } +} + +/*! \internal \a event see QGraphicsWidget for details */ void QLegend::hideEvent(QHideEvent *event) @@ -489,7 +532,8 @@ QLegendPrivate::QLegendPrivate(ChartPresenter *presenter, QChart *chart, QLegend m_diameter(5), m_attachedToChart(true), m_backgroundVisible(false), - m_reverseMarkers(false) + m_reverseMarkers(false), + m_showToolTips(false) { m_items->setHandlesChildEvents(false); } @@ -651,7 +695,15 @@ void QLegendPrivate::decorateMarkers(QList markers) } } - +void QLegendPrivate::updateToolTips() +{ + foreach (QLegendMarker *m, m_markers) { + if (m->d_ptr->m_item->displayedLabel() != m->label()) + m->d_ptr->m_item->setToolTip(m->label()); + else + m->d_ptr->m_item->setToolTip(QString()); + } +} #include "moc_qlegend.cpp" #include "moc_qlegend_p.cpp" diff --git a/src/charts/legend/qlegend.h b/src/charts/legend/qlegend.h index 7de215d..8825af4 100644 --- a/src/charts/legend/qlegend.h +++ b/src/charts/legend/qlegend.h @@ -52,6 +52,7 @@ class QT_CHARTS_EXPORT QLegend : public QGraphicsWidget Q_PROPERTY(QFont font READ font WRITE setFont NOTIFY fontChanged) Q_PROPERTY(QColor labelColor READ labelColor WRITE setLabelColor NOTIFY labelColorChanged) Q_PROPERTY(bool reverseMarkers READ reverseMarkers WRITE setReverseMarkers NOTIFY reverseMarkersChanged) + Q_PROPERTY(bool showToolTips READ showToolTips WRITE setShowToolTips NOTIFY showToolTipsChanged) private: explicit QLegend(QChart *chart); @@ -94,6 +95,8 @@ public: bool reverseMarkers(); void setReverseMarkers(bool reverseMarkers = true); + bool showToolTips() const; + void setShowToolTips(bool show); protected: void hideEvent(QHideEvent *event); void showEvent(QShowEvent *event); @@ -105,6 +108,7 @@ Q_SIGNALS: void fontChanged(QFont font); void labelColorChanged(QColor color); void reverseMarkersChanged(bool reverseMarkers); + void showToolTipsChanged(bool showToolTips); private: QScopedPointer d_ptr; diff --git a/src/charts/legend/qlegend_p.h b/src/charts/legend/qlegend_p.h index f32116f..7806015 100644 --- a/src/charts/legend/qlegend_p.h +++ b/src/charts/legend/qlegend_p.h @@ -75,6 +75,7 @@ private: void addMarkers(QList markers); void removeMarkers(QList markers); void decorateMarkers(QList markers); + void updateToolTips(); private: QLegend *q_ptr; @@ -92,6 +93,7 @@ private: bool m_attachedToChart; bool m_backgroundVisible; bool m_reverseMarkers; + bool m_showToolTips; QList m_markers; QList m_series;