File indexing completed on 2022-12-06 15:16:36

0001 /*
0002     SPDX-FileCopyrightText: 2012 Marco Martin <mart@kde.org>
0003     SPDX-FileCopyrightText: 2014 David Edmundson <davidedmudnson@kde.org>
0004     SPDX-FileCopyrightText: 2016 Smith AR <audoban@openmailbox.org>
0005     SPDX-FileCopyrightText: 2016 Michail Vourlakos <mvourlakos@gmail.com>
0006 
0007     This file is part of Latte-Dock and is a Fork of PlasmaCore::IconItem
0008 
0009     SPDX-License-Identifier: GPL-2.0-or-later
0010 */
0011 
0012 #ifndef ICONITEM_H
0013 #define ICONITEM_H
0014 
0015 // C++
0016 #include <memory>
0017 
0018 // Qt
0019 #include <QQuickItem>
0020 #include <QIcon>
0021 #include <QImage>
0022 #include <QPixmap>
0023 
0024 // Plasma
0025 #include <Plasma/Svg>
0026 
0027 // this file is based on PlasmaCore::IconItem class, thanks to KDE
0028 namespace Latte {
0029 class IconItem : public QQuickItem
0030 {
0031     Q_OBJECT
0032 
0033     /**
0034      * Sets the icon to be displayed. Source can be one of:
0035      *  - iconName (as a string)
0036      *  - URL
0037      *  - QImage
0038      *  - QPixmap
0039      *  - QIcon
0040      *
0041      * When passing an icon name (or a QIcon with an icon name set) it will:
0042      *  - load the plasma variant if usesPlasmaTheme is set and exists
0043      *  - otherwise try to load the icon as an SVG so colorscopes apply
0044      *  - load the icon as normal
0045      */
0046     Q_PROPERTY(QVariant source READ source WRITE setSource NOTIFY sourceChanged)
0047 
0048     /**
0049      * Specifies the color group to use for this icon
0050      * This only applies to icons loaded from the plasma theme
0051      */
0052     Q_PROPERTY(Plasma::Theme::ColorGroup colorGroup READ colorGroup WRITE setColorGroup NOTIFY colorGroupChanged)
0053 
0054     /**
0055       * Specifies the overlay(s) for this icon
0056       */
0057     Q_PROPERTY(QStringList overlays READ overlays WRITE setOverlays NOTIFY overlaysChanged)
0058 
0059     /**
0060      * See QQuickItem::smooth
0061      */
0062     Q_PROPERTY(bool smooth READ smooth WRITE setSmooth NOTIFY smoothChanged)
0063 
0064     /**
0065      * Apply a visual indication that this icon is active.
0066      * Typically used to indicate that it is hovered
0067      */
0068     Q_PROPERTY(bool active READ isActive WRITE setActive NOTIFY activeChanged)
0069 
0070     /**
0071      * True if a valid icon is set. False otherwise.
0072      */
0073     Q_PROPERTY(bool valid READ isValid NOTIFY validChanged)
0074 
0075     /**
0076      * The width of the icon that is actually painted
0077      */
0078     Q_PROPERTY(int paintedWidth READ paintedWidth NOTIFY paintedSizeChanged)
0079 
0080     /**
0081      * The height of the icon actually being drawn.
0082      */
0083     Q_PROPERTY(int paintedHeight READ paintedHeight NOTIFY paintedSizeChanged)
0084 
0085     /**
0086      * If set, icon will try and use icons from the Plasma theme if possible
0087      */
0088     Q_PROPERTY(bool usesPlasmaTheme READ usesPlasmaTheme WRITE setUsesPlasmaTheme NOTIFY usesPlasmaThemeChanged)
0089 
0090     /**
0091      * If set, icon will provide a background and glow color
0092      */
0093     Q_PROPERTY(bool providesColors READ providesColors WRITE setProvidesColors NOTIFY providesColorsChanged)
0094 
0095     /**
0096      * Contains the last valid icon name
0097      */
0098     Q_PROPERTY(QString lastValidSourceName READ lastValidSourceName NOTIFY lastValidSourceNameChanged)
0099 
0100     Q_PROPERTY(QColor backgroundColor READ backgroundColor NOTIFY backgroundColorChanged)
0101     Q_PROPERTY(QColor glowColor READ glowColor NOTIFY glowColorChanged)
0102 public:
0103     IconItem(QQuickItem *parent = nullptr);
0104     virtual ~IconItem();
0105 
0106     void setSource(const QVariant &source);
0107     QVariant source() const;
0108 
0109     void setColorGroup(Plasma::Theme::ColorGroup group);
0110     Plasma::Theme::ColorGroup colorGroup() const;
0111 
0112     void setOverlays(const QStringList &overlays);
0113     QStringList overlays() const;
0114 
0115     bool isActive() const;
0116     void setActive(bool active);
0117 
0118     void setSmooth(const bool smooth);
0119     bool smooth() const;
0120 
0121     bool isValid() const;
0122 
0123     bool providesColors() const;
0124     void setProvidesColors(const bool provides);
0125 
0126     bool usesPlasmaTheme() const;
0127     void setUsesPlasmaTheme(bool usesPlasmaTheme);
0128 
0129     int paintedWidth() const;
0130     int paintedHeight() const;
0131 
0132     QString lastValidSourceName();
0133 
0134     QColor backgroundColor() const;
0135 
0136     QColor glowColor() const;
0137 
0138     void updatePolish() Q_DECL_OVERRIDE;
0139     QSGNode *updatePaintNode(QSGNode *oldNode, UpdatePaintNodeData *updatePaintNodeData) override;
0140 
0141     void itemChange(ItemChange change, const ItemChangeData &value) override;
0142     void geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry) override;
0143 
0144     void componentComplete() Q_DECL_OVERRIDE;
0145 
0146 signals:
0147     void activeChanged();
0148     void backgroundColorChanged();
0149     void colorGroupChanged();
0150     void glowColorChanged();
0151     void lastValidSourceNameChanged();
0152     void overlaysChanged();
0153     void paintedSizeChanged();
0154     void providesColorsChanged();
0155     void smoothChanged();
0156     void sourceChanged();
0157     void usesPlasmaThemeChanged();
0158     void validChanged();
0159 
0160 private slots:
0161     void schedulePixmapUpdate();
0162     void enabledChanged();
0163 
0164 private:
0165     void loadPixmap();
0166     void updateColors();
0167     void setLastLoadedSourceId(QString id);
0168     void setLastValidSourceName(QString name);
0169     void setBackgroundColor(QColor background);
0170     void setGlowColor(QColor glow);
0171 
0172 private:
0173     bool m_active;
0174     bool m_providesColors{false};
0175     bool m_smooth;
0176 
0177 
0178     bool m_textureChanged;
0179     bool m_sizeChanged;
0180     bool m_usesPlasmaTheme;
0181 
0182     QColor m_backgroundColor;
0183     QColor m_glowColor;
0184 
0185     QIcon m_icon;
0186     QPixmap m_iconPixmap;
0187     QImage m_imageIcon;
0188     std::unique_ptr<Plasma::Svg> m_svgIcon;
0189     QString m_svgIconName;
0190 
0191     //! can be used to track changes during source "changes" independent
0192     //! of the source type
0193     int m_iconCounter{0};
0194 
0195     //! last source name that was valid
0196     QString m_lastValidSourceName;
0197 
0198     //! the last icon that was loaded independent of its type svg/pixmap/image
0199     //! this is set and used only internally. One of its uses is to check
0200     //! when the colors must be updated
0201     QString m_lastLoadedSourceId;
0202 
0203     //! last source name that was used in order to produce colors
0204     QString m_lastColorsSourceId;
0205 
0206     QStringList m_overlays;
0207 
0208     Plasma::Theme::ColorGroup m_colorGroup;
0209 
0210     //this contains the raw variant it was passed
0211     QVariant m_source;
0212 
0213     QSizeF m_implicitSize;
0214 };
0215 
0216 }
0217 #endif // ICONITEM_H