| 1 | /* |
| 2 | SPDX-FileCopyrightText: 2010 Marco Martin <mart@kde.org> |
| 3 | SPDX-FileCopyrightText: 2014 David Edmundson <davidedmundson@kde.org> |
| 4 | |
| 5 | SPDX-License-Identifier: GPL-2.0-or-later |
| 6 | */ |
| 7 | #ifndef FRAMESVGITEM_P |
| 8 | #define FRAMESVGITEM_P |
| 9 | |
| 10 | #include <QQmlParserStatus> |
| 11 | #include <QQuickItem> |
| 12 | |
| 13 | #include <KSvg/FrameSvg> |
| 14 | |
| 15 | #include <qqmlregistration.h> |
| 16 | |
| 17 | namespace Kirigami |
| 18 | { |
| 19 | namespace Platform |
| 20 | { |
| 21 | class PlatformTheme; |
| 22 | } |
| 23 | }; |
| 24 | |
| 25 | namespace KSvg |
| 26 | { |
| 27 | class FrameSvg; |
| 28 | |
| 29 | class FrameSvgItemMargins : public QObject |
| 30 | { |
| 31 | Q_OBJECT |
| 32 | QML_ELEMENT |
| 33 | QML_UNCREATABLE("FrameSvgItemMargins are read-only properties of FrameSvgItem" ) |
| 34 | |
| 35 | Q_PROPERTY(qreal left READ left NOTIFY marginsChanged) |
| 36 | Q_PROPERTY(qreal top READ top NOTIFY marginsChanged) |
| 37 | Q_PROPERTY(qreal right READ right NOTIFY marginsChanged) |
| 38 | Q_PROPERTY(qreal bottom READ bottom NOTIFY marginsChanged) |
| 39 | Q_PROPERTY(qreal horizontal READ horizontal NOTIFY marginsChanged) |
| 40 | Q_PROPERTY(qreal vertical READ vertical NOTIFY marginsChanged) |
| 41 | |
| 42 | public: |
| 43 | FrameSvgItemMargins(KSvg::FrameSvg *frameSvg, QObject *parent = nullptr); |
| 44 | |
| 45 | qreal left() const; |
| 46 | qreal top() const; |
| 47 | qreal right() const; |
| 48 | qreal bottom() const; |
| 49 | qreal horizontal() const; |
| 50 | qreal vertical() const; |
| 51 | |
| 52 | /// returns a vector with left, top, right, bottom |
| 53 | QList<qreal> margins() const; |
| 54 | |
| 55 | void setFixed(bool fixed); |
| 56 | bool isFixed() const; |
| 57 | |
| 58 | void setInset(bool inset); |
| 59 | bool isInset() const; |
| 60 | |
| 61 | public Q_SLOTS: |
| 62 | void update(); |
| 63 | |
| 64 | Q_SIGNALS: |
| 65 | void marginsChanged(); |
| 66 | |
| 67 | private: |
| 68 | FrameSvg *m_frameSvg; |
| 69 | bool m_fixed; |
| 70 | bool m_inset; |
| 71 | }; |
| 72 | |
| 73 | /*! |
| 74 | * \qmltype FrameSvgItem |
| 75 | * \inqmlmodule org.kde.ksvg |
| 76 | * |
| 77 | * \brief An SVG Item with borders. |
| 78 | */ |
| 79 | class FrameSvgItem : public QQuickItem |
| 80 | { |
| 81 | Q_OBJECT |
| 82 | QML_ELEMENT |
| 83 | Q_INTERFACES(QQmlParserStatus) |
| 84 | |
| 85 | /*! |
| 86 | * \brief This property specifies the relative path of the SVG in the theme. |
| 87 | * |
| 88 | * Example: "widgets/background" |
| 89 | * |
| 90 | * \qmlproperty string FrameSvgItem::imagePath |
| 91 | */ |
| 92 | Q_PROPERTY(QString imagePath READ imagePath WRITE setImagePath NOTIFY imagePathChanged) |
| 93 | |
| 94 | /*! |
| 95 | * This property holds the prefix for the SVG. |
| 96 | * prefix for the 9-piece SVG, like "pushed" or "normal" for a button. |
| 97 | * see https://techbase.kde.org/Development/Tutorials/Plasma5/ThemeDetails |
| 98 | * for a list of paths and prefixes |
| 99 | * It can also be an array of strings, specifying a fallback chain in case |
| 100 | * the first element isn't found in the theme, eg ["toolbutton-normal", "normal"] |
| 101 | * so it's easy to keep backwards compatibility with old themes |
| 102 | * (Note: fallback chain is supported only \since 5.32) |
| 103 | * |
| 104 | * \qmlproperty variant FrameSvgItem::prefix |
| 105 | */ |
| 106 | Q_PROPERTY(QVariant prefix READ prefix WRITE setPrefix NOTIFY prefixChanged) |
| 107 | |
| 108 | /*! |
| 109 | * \brief This property holds the actual prefix that was used, if a fallback |
| 110 | * chain array was set as "prefix". |
| 111 | * |
| 112 | * \since 5.34 |
| 113 | * \qmlproperty string FrameSvgItem::usedPrefix |
| 114 | */ |
| 115 | Q_PROPERTY(QString usedPrefix READ usedPrefix NOTIFY usedPrefixChanged) |
| 116 | |
| 117 | /*! |
| 118 | * \qmlproperty qreal FrameSvgItem::margins.left |
| 119 | * \qmlproperty qreal FrameSvgItem::margins.top |
| 120 | * \qmlproperty qreal FrameSvgItem::margins.right |
| 121 | * \qmlproperty qreal FrameSvgItem::margins.bottom |
| 122 | * \qmlproperty qreal FrameSvgItem::margins.horizontal |
| 123 | * \qmlproperty qreal FrameSvgItem::margins.vertical |
| 124 | * |
| 125 | * \brief This property holds the frame's margins. |
| 126 | */ |
| 127 | Q_PROPERTY(KSvg::FrameSvgItemMargins *margins READ margins CONSTANT) |
| 128 | |
| 129 | /*! |
| 130 | * \qmlproperty qreal FrameSvgItem::fixedMargins.left |
| 131 | * \qmlproperty qreal FrameSvgItem::fixedMargins.top |
| 132 | * \qmlproperty qreal FrameSvgItem::fixedMargins.right |
| 133 | * \qmlproperty qreal FrameSvgItem::fixedMargins.bottom |
| 134 | * \qmlproperty qreal FrameSvgItem::fixedMargins.horizontal |
| 135 | * \qmlproperty qreal FrameSvgItem::fixedMargins.vertical |
| 136 | * |
| 137 | * \brief This property holds the fixed margins of the frame which are used |
| 138 | * regardless of any margins being enabled or not. |
| 139 | */ |
| 140 | Q_PROPERTY(KSvg::FrameSvgItemMargins *fixedMargins READ fixedMargins CONSTANT) |
| 141 | |
| 142 | /*! |
| 143 | * \qmlproperty qreal FrameSvgItem::inset.left |
| 144 | * \qmlproperty qreal FrameSvgItem::inset.top |
| 145 | * \qmlproperty qreal FrameSvgItem::inset.right |
| 146 | * \qmlproperty qreal FrameSvgItem::inset.bottom |
| 147 | * \qmlproperty qreal FrameSvgItem::inset.horizontal |
| 148 | * \qmlproperty qreal FrameSvgItem::inset.vertical |
| 149 | * |
| 150 | * \brief This property holds the frame's inset. |
| 151 | * |
| 152 | * \since 5.77 |
| 153 | */ |
| 154 | Q_PROPERTY(KSvg::FrameSvgItemMargins *inset READ inset CONSTANT) |
| 155 | |
| 156 | /*! |
| 157 | * \brief This property specifies which borders are shown. |
| 158 | * \sa KSvg::FrameSvg::EnabledBorder |
| 159 | * \qmlproperty flags<KSvg::FrameSvg::EnabledBorder> FrameSvgItem::enabledBorders |
| 160 | */ |
| 161 | Q_PROPERTY(KSvg::FrameSvg::EnabledBorders enabledBorders READ enabledBorders WRITE setEnabledBorders NOTIFY enabledBordersChanged) |
| 162 | |
| 163 | /*! |
| 164 | * \brief This property holds whether the current SVG is present in |
| 165 | * the currently-used theme and no fallback is involved. |
| 166 | * |
| 167 | * \qmlproperty bool FrameSvgItem::fromCurrentImageSet |
| 168 | */ |
| 169 | Q_PROPERTY(bool fromCurrentImageSet READ fromCurrentImageSet NOTIFY fromCurrentImageSetChanged) |
| 170 | |
| 171 | /*! |
| 172 | * \brief This property specifies the SVG's status. |
| 173 | * |
| 174 | * Depending on the specified status, the SVG will use different colors: |
| 175 | * * Normal: text's color is textColor, and background color is |
| 176 | * backgroundColor. |
| 177 | * * Selected: text color becomes highlightedText and background color is |
| 178 | * changed to highlightColor. |
| 179 | * |
| 180 | * \sa Kirigami::PlatformTheme |
| 181 | * \sa KSvg::Svg::status |
| 182 | * \since 5.23 |
| 183 | * \qmlproperty enum<KSvg::Svg::Status> FrameSvgItem::status |
| 184 | */ |
| 185 | Q_PROPERTY(KSvg::Svg::Status status READ status WRITE setStatus NOTIFY statusChanged) |
| 186 | |
| 187 | /*! |
| 188 | * \brief This property holds the mask that contains the SVG's painted areas. |
| 189 | * \since 5.58 |
| 190 | * \qmlproperty QRegion FrameSvgItem::mask |
| 191 | */ |
| 192 | Q_PROPERTY(QRegion mask READ mask NOTIFY maskChanged) |
| 193 | |
| 194 | /*! |
| 195 | * \brief This property holds the minimum height required to correctly draw |
| 196 | * this SVG. |
| 197 | * |
| 198 | * \since 5.101 |
| 199 | * \qmlproperty int FrameSvgItem::minimumDrawingHeight |
| 200 | */ |
| 201 | Q_PROPERTY(int minimumDrawingHeight READ minimumDrawingHeight NOTIFY repaintNeeded) |
| 202 | |
| 203 | /*! |
| 204 | * \brief This property holds the minimum width required to correctly draw |
| 205 | * this SVG. |
| 206 | * |
| 207 | * \since 5.101 |
| 208 | * \qmlproperty int FrameSvgItem::minimumDrawingWidth |
| 209 | */ |
| 210 | Q_PROPERTY(int minimumDrawingWidth READ minimumDrawingWidth NOTIFY repaintNeeded) |
| 211 | |
| 212 | public: |
| 213 | /*! |
| 214 | * \qmlmethod bool FrameSvgItem::hasElementPrefix(string prefix) |
| 215 | * |
| 216 | * Returns whether the svg has the necessary elements with the given prefix |
| 217 | * to draw a frame. |
| 218 | * |
| 219 | * \a prefix the given prefix we want to check if drawable |
| 220 | */ |
| 221 | Q_INVOKABLE bool hasElementPrefix(const QString &prefix) const; |
| 222 | |
| 223 | /*! |
| 224 | * \qmlmethod bool FrameSvgItem::hasElement(string elementName) |
| 225 | * |
| 226 | * Returns whether the SVG includes the given element. |
| 227 | * |
| 228 | * This is a convenience function that forwards to hasElement(). |
| 229 | * |
| 230 | * \sa KSvg::Svg::hasElement() |
| 231 | */ |
| 232 | Q_INVOKABLE bool hasElement(const QString &elementName) const; |
| 233 | |
| 234 | FrameSvgItem(QQuickItem *parent = nullptr); |
| 235 | ~FrameSvgItem() override; |
| 236 | |
| 237 | void setImagePath(const QString &path); |
| 238 | QString imagePath() const; |
| 239 | |
| 240 | void setPrefix(const QVariant &prefix); |
| 241 | QVariant prefix() const; |
| 242 | |
| 243 | QString usedPrefix() const; |
| 244 | |
| 245 | void setEnabledBorders(const KSvg::FrameSvg::EnabledBorders borders); |
| 246 | KSvg::FrameSvg::EnabledBorders enabledBorders() const; |
| 247 | |
| 248 | void setColorSet(KSvg::Svg::ColorSet colorSet); |
| 249 | KSvg::Svg::ColorSet colorSet() const; |
| 250 | |
| 251 | FrameSvgItemMargins *margins(); |
| 252 | FrameSvgItemMargins *fixedMargins(); |
| 253 | FrameSvgItemMargins *inset(); |
| 254 | |
| 255 | bool fromCurrentImageSet() const; |
| 256 | |
| 257 | void setStatus(KSvg::Svg::Status status); |
| 258 | KSvg::Svg::Status status() const; |
| 259 | int minimumDrawingHeight() const; |
| 260 | int minimumDrawingWidth() const; |
| 261 | |
| 262 | void geometryChange(const QRectF &newGeometry, const QRectF &oldGeometry) override; |
| 263 | |
| 264 | QRegion mask() const; |
| 265 | |
| 266 | /* |
| 267 | * Only to be used from inside this library, is not intended to be invokable |
| 268 | */ |
| 269 | KSvg::FrameSvg *frameSvg() const; |
| 270 | |
| 271 | QSGNode *updatePaintNode(QSGNode *, UpdatePaintNodeData *) override; |
| 272 | |
| 273 | void itemChange(QQuickItem::ItemChange change, const QQuickItem::ItemChangeData &data) override; |
| 274 | |
| 275 | protected: |
| 276 | void classBegin() override; |
| 277 | void componentComplete() override; |
| 278 | |
| 279 | Q_SIGNALS: |
| 280 | void imagePathChanged(); |
| 281 | void prefixChanged(); |
| 282 | void enabledBordersChanged(); |
| 283 | void fromCurrentImageSetChanged(); |
| 284 | void repaintNeeded(); |
| 285 | void statusChanged(); |
| 286 | void usedPrefixChanged(); |
| 287 | void maskChanged(); |
| 288 | |
| 289 | private Q_SLOTS: |
| 290 | void doUpdate(); |
| 291 | |
| 292 | private: |
| 293 | void updateDevicePixelRatio(); |
| 294 | void applyPrefixes(); |
| 295 | |
| 296 | KSvg::FrameSvg *m_frameSvg; |
| 297 | Kirigami::Platform::PlatformTheme *m_kirigamiTheme; |
| 298 | FrameSvgItemMargins *m_margins; |
| 299 | FrameSvgItemMargins *m_fixedMargins; |
| 300 | FrameSvgItemMargins *m_insetMargins; |
| 301 | // logged margins to check for changes |
| 302 | QList<qreal> m_oldMargins; |
| 303 | QList<qreal> m_oldFixedMargins; |
| 304 | QList<qreal> m_oldInsetMargins; |
| 305 | QStringList m_prefixes; |
| 306 | bool m_textureChanged; |
| 307 | bool m_sizeChanged; |
| 308 | bool m_fastPath; |
| 309 | }; |
| 310 | |
| 311 | } |
| 312 | |
| 313 | #endif |
| 314 | |