File indexing completed on 2024-04-28 16:49:38

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