| 1 | // Copyright (C) 2016 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 | |
| 4 | #include <QtCharts/QLegendMarker> |
| 5 | #include <private/qlegendmarker_p.h> |
| 6 | #include <private/legendmarkeritem_p.h> |
| 7 | #include <QtCharts/QLegend> |
| 8 | #include <private/qlegend_p.h> |
| 9 | #include <private/legendlayout_p.h> |
| 10 | #include <QtGui/QFontMetrics> |
| 11 | #include <QtWidgets/QGraphicsSceneEvent> |
| 12 | #include <QtCharts/QAbstractSeries> |
| 13 | |
| 14 | QT_BEGIN_NAMESPACE |
| 15 | |
| 16 | /*! |
| 17 | \class QLegendMarker |
| 18 | \inmodule QtCharts |
| 19 | \brief The QLegendMarker class is an abstract object that can be used to access |
| 20 | markers within a legend. |
| 21 | |
| 22 | A legend marker consists of an icon and a label. The icon color corresponds to the color |
| 23 | used to draw a series and the label displays the name of the series (or the label of the |
| 24 | slice for a pie series or bar set for a bar series). A legend marker is always related to |
| 25 | one series, slice, or bar set. |
| 26 | |
| 27 | \image examples_percentbarchart_legend.png |
| 28 | |
| 29 | \sa QLegend |
| 30 | */ |
| 31 | /*! |
| 32 | \enum QLegendMarker::LegendMarkerType |
| 33 | \since 5.8 |
| 34 | |
| 35 | The type of the legend marker object. |
| 36 | |
| 37 | \value LegendMarkerTypeArea |
| 38 | A legend marker for an area series. |
| 39 | \value LegendMarkerTypeBar |
| 40 | A legend marker for a bar set. |
| 41 | \value LegendMarkerTypePie |
| 42 | A legend marker for a pie slice. |
| 43 | \value LegendMarkerTypeXY |
| 44 | A legend marker for a line, spline, or scatter series. |
| 45 | \value LegendMarkerTypeBoxPlot |
| 46 | A legend marker for a box plot series. |
| 47 | \value LegendMarkerTypeCandlestick |
| 48 | A legend marker for a candlestick series. |
| 49 | */ |
| 50 | |
| 51 | /*! |
| 52 | \fn virtual LegendMarkerType QLegendMarker::type() = 0; |
| 53 | Returns the type of the legend marker for the related series, pie slice, or bar set. |
| 54 | |
| 55 | \sa LegendMarkerType |
| 56 | */ |
| 57 | |
| 58 | /*! |
| 59 | \fn virtual QAbstractSeries* QLegendMarker::series() = 0; |
| 60 | Returns a pointer to the series that is related to this legend marker. A legend marker |
| 61 | is always related to a series. |
| 62 | */ |
| 63 | |
| 64 | /*! |
| 65 | \fn void QLegendMarker::clicked(); |
| 66 | This signal is emitted when the legend marker is clicked. |
| 67 | */ |
| 68 | |
| 69 | /*! |
| 70 | \fn void QLegendMarker::hovered(bool status); |
| 71 | This signal is emitted when a mouse is hovered over the legend marker. |
| 72 | When the mouse moves over the marker, \a status turns \c true, and when |
| 73 | the mouse moves away again, it turns \c false. |
| 74 | */ |
| 75 | |
| 76 | /*! |
| 77 | \fn void QLegendMarker::labelChanged() |
| 78 | This signal is emitted when the label of the legend marker has changed. |
| 79 | */ |
| 80 | |
| 81 | /*! |
| 82 | \fn void QLegendMarker::labelBrushChanged() |
| 83 | This signal is emitted when the label brush of the legend marker has changed. |
| 84 | */ |
| 85 | |
| 86 | /*! |
| 87 | \fn void QLegendMarker::fontChanged() |
| 88 | This signal is emitted when the (label) font of the legend marker has changed. |
| 89 | */ |
| 90 | |
| 91 | /*! |
| 92 | \fn void QLegendMarker::penChanged() |
| 93 | This signal is emitted when the pen of the legend marker has changed. |
| 94 | */ |
| 95 | |
| 96 | /*! |
| 97 | \fn void QLegendMarker::brushChanged() |
| 98 | This signal is emitted when the brush of the legend marker has changed. |
| 99 | */ |
| 100 | |
| 101 | /*! |
| 102 | \fn void QLegendMarker::visibleChanged() |
| 103 | This signal is emitted when the visibility of the legend marker has changed. |
| 104 | */ |
| 105 | |
| 106 | /*! |
| 107 | \property QLegendMarker::label |
| 108 | \brief The text shown in the legend for a legend marker. |
| 109 | */ |
| 110 | |
| 111 | /*! |
| 112 | \property QLegendMarker::labelBrush |
| 113 | \brief The brush of the label. |
| 114 | */ |
| 115 | |
| 116 | /*! |
| 117 | \property QLegendMarker::font |
| 118 | \brief The font of the label. |
| 119 | */ |
| 120 | |
| 121 | /*! |
| 122 | \property QLegendMarker::pen |
| 123 | \brief The pen used to draw the outline of the icon. |
| 124 | */ |
| 125 | |
| 126 | /*! |
| 127 | \property QLegendMarker::brush |
| 128 | \brief The brush used to fill the icon. |
| 129 | */ |
| 130 | |
| 131 | /*! |
| 132 | \property QLegendMarker::visible |
| 133 | \brief The visibility of the legend marker. |
| 134 | |
| 135 | The visibility affects both the legend marker label and the icon. |
| 136 | */ |
| 137 | |
| 138 | /*! |
| 139 | \property QLegendMarker::shape |
| 140 | |
| 141 | The shape of the legend marker. Defaults to QLegend::MarkerShapeDefault, which indicates |
| 142 | the shape is determined by QLegend::markerShape property. |
| 143 | */ |
| 144 | |
| 145 | /*! |
| 146 | \internal |
| 147 | */ |
| 148 | QLegendMarker::QLegendMarker(QLegendMarkerPrivate &d, QObject *parent) : |
| 149 | QObject(parent), |
| 150 | d_ptr(&d) |
| 151 | { |
| 152 | d_ptr->m_item->setVisible(d_ptr->series()->isVisible()); |
| 153 | } |
| 154 | |
| 155 | /*! |
| 156 | Removes the legend marker. |
| 157 | */ |
| 158 | QLegendMarker::~QLegendMarker() |
| 159 | { |
| 160 | } |
| 161 | |
| 162 | /*! |
| 163 | Returns the label of the marker. |
| 164 | */ |
| 165 | QString QLegendMarker::label() const |
| 166 | { |
| 167 | return d_ptr->m_item->label(); |
| 168 | } |
| 169 | |
| 170 | /*! |
| 171 | Sets the label of the marker to \a label. |
| 172 | |
| 173 | \note Changing the name of a series also changes the label of its marker. |
| 174 | */ |
| 175 | void QLegendMarker::setLabel(const QString &label) |
| 176 | { |
| 177 | if (label.isEmpty()) { |
| 178 | d_ptr->m_customLabel = false; |
| 179 | } else { |
| 180 | d_ptr->m_customLabel = true; |
| 181 | d_ptr->m_item->setLabel(label); |
| 182 | } |
| 183 | } |
| 184 | /*! |
| 185 | Returns the brush that is used to draw the label. |
| 186 | */ |
| 187 | QBrush QLegendMarker::labelBrush() const |
| 188 | { |
| 189 | return d_ptr->m_item->labelBrush(); |
| 190 | } |
| 191 | |
| 192 | /*! |
| 193 | Sets the the brush used to draw to label to \a brush. |
| 194 | */ |
| 195 | void QLegendMarker::setLabelBrush(const QBrush &brush) |
| 196 | { |
| 197 | d_ptr->m_item->setLabelBrush(brush); |
| 198 | } |
| 199 | |
| 200 | /*! |
| 201 | Retuns the font of the label. |
| 202 | */ |
| 203 | QFont QLegendMarker::font() const |
| 204 | { |
| 205 | return d_ptr->m_item->font(); |
| 206 | } |
| 207 | |
| 208 | /*! |
| 209 | Sets the font of the label to \a font. |
| 210 | */ |
| 211 | void QLegendMarker::setFont(const QFont &font) |
| 212 | { |
| 213 | d_ptr->m_item->setFont(font); |
| 214 | } |
| 215 | |
| 216 | /*! |
| 217 | Returns the pen used to draw the outline of the icon. |
| 218 | */ |
| 219 | QPen QLegendMarker::pen() const |
| 220 | { |
| 221 | return d_ptr->m_item->pen(); |
| 222 | } |
| 223 | |
| 224 | /*! |
| 225 | Sets the \a pen used to draw the outline of the icon to \a pen. |
| 226 | */ |
| 227 | void QLegendMarker::setPen(const QPen &pen) |
| 228 | { |
| 229 | if (pen == QPen(Qt::NoPen)) { |
| 230 | d_ptr->m_customPen = false; |
| 231 | } else { |
| 232 | d_ptr->m_customPen = true; |
| 233 | d_ptr->m_item->setPen(pen); |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | /*! |
| 238 | Returns the brush used to fill the icon. |
| 239 | */ |
| 240 | QBrush QLegendMarker::brush() const |
| 241 | { |
| 242 | return d_ptr->m_item->brush(); |
| 243 | } |
| 244 | |
| 245 | /*! |
| 246 | Sets the brush used to fill the icon to \a brush. |
| 247 | |
| 248 | \note Changing the color of the series also changes the color of the icon. |
| 249 | */ |
| 250 | void QLegendMarker::setBrush(const QBrush &brush) |
| 251 | { |
| 252 | if (brush == QBrush(Qt::NoBrush)) { |
| 253 | d_ptr->m_customBrush = false; |
| 254 | } else { |
| 255 | d_ptr->m_customBrush = true; |
| 256 | d_ptr->m_item->setBrush(brush); |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | /*! |
| 261 | Returns the visibility of the marker. |
| 262 | */ |
| 263 | bool QLegendMarker::isVisible() const |
| 264 | { |
| 265 | return d_ptr->m_item->isVisible(); |
| 266 | } |
| 267 | |
| 268 | /*! |
| 269 | Sets the marker's visibility to \a visible. |
| 270 | */ |
| 271 | void QLegendMarker::setVisible(bool visible) |
| 272 | { |
| 273 | d_ptr->m_item->setVisible(visible); |
| 274 | } |
| 275 | |
| 276 | QLegend::MarkerShape QLegendMarker::shape() const |
| 277 | { |
| 278 | return d_ptr->m_item->markerShape(); |
| 279 | } |
| 280 | |
| 281 | void QLegendMarker::setShape(QLegend::MarkerShape shape) |
| 282 | { |
| 283 | if (shape != d_ptr->m_item->markerShape()) { |
| 284 | d_ptr->m_item->setMarkerShape(shape); |
| 285 | d_ptr->handleShapeChange(); |
| 286 | emit shapeChanged(); |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 291 | QLegendMarkerPrivate::QLegendMarkerPrivate(QLegendMarker *q, QLegend *legend) : |
| 292 | m_legend(legend), |
| 293 | m_customLabel(false), |
| 294 | m_customBrush(false), |
| 295 | m_customPen(false), |
| 296 | q_ptr(q) |
| 297 | { |
| 298 | m_item = new LegendMarkerItem(this); |
| 299 | |
| 300 | connect(sender: legend, signal: &QLegend::markerShapeChanged, context: this, |
| 301 | slot: &QLegendMarkerPrivate::handleShapeChange); |
| 302 | } |
| 303 | |
| 304 | QLegendMarkerPrivate::~QLegendMarkerPrivate() |
| 305 | { |
| 306 | delete m_item; |
| 307 | } |
| 308 | |
| 309 | void QLegendMarkerPrivate::invalidateLegend() |
| 310 | { |
| 311 | m_item->updateGeometry(); |
| 312 | m_legend->d_ptr->m_layout->invalidate(); |
| 313 | } |
| 314 | |
| 315 | void QLegendMarkerPrivate::invalidateAllItems() |
| 316 | { |
| 317 | QList<QLegendMarker *> markers = m_legend->markers(); |
| 318 | for (int i = 0; i < markers.size(); i++) |
| 319 | markers.at(i)->d_ptr->m_item->updateGeometry(); |
| 320 | m_legend->d_ptr->m_layout->invalidate(); |
| 321 | } |
| 322 | |
| 323 | void QLegendMarkerPrivate::handleShapeChange() |
| 324 | { |
| 325 | m_item->updateMarkerShapeAndSize(); |
| 326 | m_legend->d_ptr->m_layout->invalidate(); |
| 327 | } |
| 328 | |
| 329 | QT_END_NAMESPACE |
| 330 | |
| 331 | #include "moc_qlegendmarker.cpp" |
| 332 | #include "moc_qlegendmarker_p.cpp" |
| 333 | |