##// END OF EJS Templates
Add support for showing tooltips when the legend text is truncated...
Andy Shaw -
r2864:47032ec192b3
parent child
Show More
@@ -124,9 +124,14 void LegendMarkerItem::setGeometry(const QRectF &rect)
124 qreal width = rect.width();
124 qreal width = rect.width();
125 qreal x = m_margin + m_markerRect.width() + m_space + m_margin;
125 qreal x = m_margin + m_markerRect.width() + m_space + m_margin;
126 QRectF truncatedRect;
126 QRectF truncatedRect;
127 const QString html = ChartPresenter::truncatedText(m_textItem->font(), m_label, qreal(0.0),
128 width - x, rect.height(), truncatedRect);
129 m_textItem->setHtml(html);
130 if (m_marker->m_legend->showToolTips() && html != m_label)
131 m_textItem->setToolTip(m_label);
132 else
133 m_textItem->setToolTip(QString());
127
134
128 m_textItem->setHtml(ChartPresenter::truncatedText(m_textItem->font(), m_label, qreal(0.0),
129 width - x, rect.height(), truncatedRect));
130 m_textItem->setTextWidth(truncatedRect.width());
135 m_textItem->setTextWidth(truncatedRect.width());
131
136
132 qreal y = qMax(m_markerRect.height() + 2 * m_margin, truncatedRect.height() + 2 * m_margin);
137 qreal y = qMax(m_markerRect.height() + 2 * m_margin, truncatedRect.height() + 2 * m_margin);
@@ -195,6 +200,15 void LegendMarkerItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
195 emit m_marker->q_ptr->hovered(false);
200 emit m_marker->q_ptr->hovered(false);
196 }
201 }
197
202
203 QString LegendMarkerItem::displayedLabel() const
204 {
205 return m_textItem->toHtml();
206 }
207
208 void LegendMarkerItem::setToolTip(const QString &tip)
209 {
210 m_textItem->setToolTip(tip);
211 }
198
212
199 #include "moc_legendmarkeritem_p.cpp"
213 #include "moc_legendmarkeritem_p.cpp"
200
214
@@ -83,6 +83,8 public:
83 void hoverEnterEvent(QGraphicsSceneHoverEvent *event);
83 void hoverEnterEvent(QGraphicsSceneHoverEvent *event);
84 void hoverLeaveEvent(QGraphicsSceneHoverEvent *event);
84 void hoverLeaveEvent(QGraphicsSceneHoverEvent *event);
85
85
86 QString displayedLabel() const;
87 void setToolTip(const QString &tooltip);
86 protected:
88 protected:
87 QLegendMarkerPrivate *m_marker; // Knows
89 QLegendMarkerPrivate *m_marker; // Knows
88 QRectF m_markerRect;
90 QRectF m_markerRect;
@@ -164,6 +164,19 QT_CHARTS_BEGIN_NAMESPACE
164 */
164 */
165
165
166 /*!
166 /*!
167 \property QLegend::showToolTips
168 Whether tooltips are shown when the text is truncated. This is false by default.
169
170 This will not have any effect when used in QML.
171 */
172
173 /*!
174 \qmlproperty bool Legend::showToolTips
175 Whether tooltips are shown when the text is truncated. This is false by default.
176 This currently has no effect as there is no support for tooltips in QML.
177 */
178
179 /*!
167 \fn void QLegend::backgroundVisibleChanged(bool)
180 \fn void QLegend::backgroundVisibleChanged(bool)
168 The visibility of the legend background changed to \a visible.
181 The visibility of the legend background changed to \a visible.
169 */
182 */
@@ -193,6 +206,11 QT_CHARTS_BEGIN_NAMESPACE
193 The use of reverse order for the markers in legend is changed to \a reverseMarkers.
206 The use of reverse order for the markers in legend is changed to \a reverseMarkers.
194 */
207 */
195
208
209 /*!
210 \fn void QLegend::showToolTipsChanged(bool showToolTips)
211 This signal is emitted when the visibility of tooltips is changed to \a showToolTips.
212 */
213
196 QLegend::QLegend(QChart *chart): QGraphicsWidget(chart),
214 QLegend::QLegend(QChart *chart): QGraphicsWidget(chart),
197 d_ptr(new QLegendPrivate(chart->d_ptr->m_presenter, chart, this))
215 d_ptr(new QLegendPrivate(chart->d_ptr->m_presenter, chart, this))
198 {
216 {
@@ -455,6 +473,31 void QLegend::setReverseMarkers(bool reverseMarkers)
455 }
473 }
456
474
457 /*!
475 /*!
476 Returns whether the tooltips are shown or not for the legend labels
477 when they are elided.
478 */
479
480 bool QLegend::showToolTips() const
481 {
482 return d_ptr->m_showToolTips;
483 }
484
485 /*!
486 When \a show is true, the legend labels will show a tooltip when
487 the mouse hovers over them if the label itself is shown elided.
488 This is false by default.
489 */
490
491 void QLegend::setShowToolTips(bool show)
492 {
493 if (d_ptr->m_showToolTips != show) {
494 d_ptr->m_showToolTips = show;
495 d_ptr->updateToolTips();
496 emit showToolTipsChanged(show);
497 }
498 }
499
500 /*!
458 \internal \a event see QGraphicsWidget for details
501 \internal \a event see QGraphicsWidget for details
459 */
502 */
460 void QLegend::hideEvent(QHideEvent *event)
503 void QLegend::hideEvent(QHideEvent *event)
@@ -489,7 +532,8 QLegendPrivate::QLegendPrivate(ChartPresenter *presenter, QChart *chart, QLegend
489 m_diameter(5),
532 m_diameter(5),
490 m_attachedToChart(true),
533 m_attachedToChart(true),
491 m_backgroundVisible(false),
534 m_backgroundVisible(false),
492 m_reverseMarkers(false)
535 m_reverseMarkers(false),
536 m_showToolTips(false)
493 {
537 {
494 m_items->setHandlesChildEvents(false);
538 m_items->setHandlesChildEvents(false);
495 }
539 }
@@ -651,7 +695,15 void QLegendPrivate::decorateMarkers(QList<QLegendMarker *> markers)
651 }
695 }
652 }
696 }
653
697
654
698 void QLegendPrivate::updateToolTips()
699 {
700 foreach (QLegendMarker *m, m_markers) {
701 if (m->d_ptr->m_item->displayedLabel() != m->label())
702 m->d_ptr->m_item->setToolTip(m->label());
703 else
704 m->d_ptr->m_item->setToolTip(QString());
705 }
706 }
655 #include "moc_qlegend.cpp"
707 #include "moc_qlegend.cpp"
656 #include "moc_qlegend_p.cpp"
708 #include "moc_qlegend_p.cpp"
657
709
@@ -52,6 +52,7 class QT_CHARTS_EXPORT QLegend : public QGraphicsWidget
52 Q_PROPERTY(QFont font READ font WRITE setFont NOTIFY fontChanged)
52 Q_PROPERTY(QFont font READ font WRITE setFont NOTIFY fontChanged)
53 Q_PROPERTY(QColor labelColor READ labelColor WRITE setLabelColor NOTIFY labelColorChanged)
53 Q_PROPERTY(QColor labelColor READ labelColor WRITE setLabelColor NOTIFY labelColorChanged)
54 Q_PROPERTY(bool reverseMarkers READ reverseMarkers WRITE setReverseMarkers NOTIFY reverseMarkersChanged)
54 Q_PROPERTY(bool reverseMarkers READ reverseMarkers WRITE setReverseMarkers NOTIFY reverseMarkersChanged)
55 Q_PROPERTY(bool showToolTips READ showToolTips WRITE setShowToolTips NOTIFY showToolTipsChanged)
55
56
56 private:
57 private:
57 explicit QLegend(QChart *chart);
58 explicit QLegend(QChart *chart);
@@ -94,6 +95,8 public:
94 bool reverseMarkers();
95 bool reverseMarkers();
95 void setReverseMarkers(bool reverseMarkers = true);
96 void setReverseMarkers(bool reverseMarkers = true);
96
97
98 bool showToolTips() const;
99 void setShowToolTips(bool show);
97 protected:
100 protected:
98 void hideEvent(QHideEvent *event);
101 void hideEvent(QHideEvent *event);
99 void showEvent(QShowEvent *event);
102 void showEvent(QShowEvent *event);
@@ -105,6 +108,7 Q_SIGNALS:
105 void fontChanged(QFont font);
108 void fontChanged(QFont font);
106 void labelColorChanged(QColor color);
109 void labelColorChanged(QColor color);
107 void reverseMarkersChanged(bool reverseMarkers);
110 void reverseMarkersChanged(bool reverseMarkers);
111 void showToolTipsChanged(bool showToolTips);
108
112
109 private:
113 private:
110 QScopedPointer<QLegendPrivate> d_ptr;
114 QScopedPointer<QLegendPrivate> d_ptr;
@@ -75,6 +75,7 private:
75 void addMarkers(QList<QLegendMarker *> markers);
75 void addMarkers(QList<QLegendMarker *> markers);
76 void removeMarkers(QList<QLegendMarker *> markers);
76 void removeMarkers(QList<QLegendMarker *> markers);
77 void decorateMarkers(QList<QLegendMarker *> markers);
77 void decorateMarkers(QList<QLegendMarker *> markers);
78 void updateToolTips();
78
79
79 private:
80 private:
80 QLegend *q_ptr;
81 QLegend *q_ptr;
@@ -92,6 +93,7 private:
92 bool m_attachedToChart;
93 bool m_attachedToChart;
93 bool m_backgroundVisible;
94 bool m_backgroundVisible;
94 bool m_reverseMarkers;
95 bool m_reverseMarkers;
96 bool m_showToolTips;
95
97
96 QList<QLegendMarker *> m_markers;
98 QList<QLegendMarker *> m_markers;
97 QList<QAbstractSeries *> m_series;
99 QList<QAbstractSeries *> m_series;
General Comments 0
You need to be logged in to leave comments. Login now