File indexing completed on 2024-04-28 04:05:05

0001 /*
0002     SPDX-FileCopyrightText: 2012 Stefan Majewsky <majewsky@gmx.net>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-only
0005 */
0006 
0007 #ifndef KGAMETHEMEPROVIDER_H
0008 #define KGAMETHEMEPROVIDER_H
0009 
0010 // own
0011 #include "kdegames_export.h"
0012 #include "kgametheme.h"
0013 // Qt
0014 #include <QObject>
0015 #include <QQmlEngine>
0016 // Std
0017 #include <memory>
0018 
0019 /**
0020  * @class KGameThemeProvider kgamethemeprovider.h <KGameThemeProvider>
0021  *
0022  * A theme provider manages KGameTheme instances, and maintains a selection of
0023  * the currentTheme(). It can automatically coordinate its selection with a
0024  * KGameRenderer instance.
0025  *
0026  * @note KGameThemeProvider instances store selections in the application config,
0027  *       in the group [KgTheme]. This is documented here because this
0028  *       information is relevant for kconfig_
0029  */
0030 class KDEGAMES_EXPORT KGameThemeProvider : public QObject
0031 {
0032     Q_OBJECT
0033     Q_PROPERTY(const KGameTheme *currentTheme READ currentTheme WRITE setCurrentTheme NOTIFY currentThemeChanged)
0034     Q_PROPERTY(QString name READ name NOTIFY nameChanged)
0035     Q_PROPERTY(QString currentThemeName READ currentThemeName NOTIFY currentThemeNameChanged)
0036     Q_DISABLE_COPY(KGameThemeProvider)
0037 
0038 public:
0039     /// Constructor. If you don't want KGameThemeProvider to store the current
0040     /// theme selection in the application config file automatically, set
0041     /// @a configKey to an empty QByteArray.
0042     ///
0043     /// If there are multiple KGameThemeProvider instances, make sure they use
0044     /// different config keys to avoid collisions.
0045     explicit KGameThemeProvider(const QByteArray &configKey = QByteArrayLiteral("Theme"), QObject *parent = nullptr);
0046     /// Destructor.
0047     ~KGameThemeProvider() override;
0048 
0049     /// @return the name of the KGameThemeProvider object. This name can be
0050     /// used as QML element ID to reference the object inside QML.
0051     /// @since 4.11
0052     QString name() const;
0053 
0054     /// @return the themes in this provider
0055     QList<const KGameTheme *> themes() const;
0056     /// @return the default theme, or 0 if the provider does not contain any
0057     /// themes
0058     const KGameTheme *defaultTheme() const;
0059     /// @see defaultTheme()
0060     ///
0061     /// Usually this will be set automatically by discoverThemes(). Call this
0062     /// before the first call to currentTheme(), it won't have any effect
0063     /// afterwards. @a theme must already have been added to this instance.
0064     void setDefaultTheme(const KGameTheme *theme);
0065     /// @return the currently selected theme, or 0 if the provider does not
0066     /// contain any themes
0067     ///
0068     /// After the KGameThemeProvider instance has been created, the current
0069     /// theme will not be determined until this method is called
0070     /// for the first time. This allows the application developer to set up
0071     /// the theme provider before it restores the theme selection from the
0072     /// configuration file.
0073     const KGameTheme *currentTheme() const;
0074 
0075     /// @return the name of the current theme
0076     /// @since 4.11
0077     QString currentThemeName() const;
0078 
0079     /// Adds a @a theme to this instance. The theme provider takes ownership
0080     /// of @a theme.
0081     void addTheme(KGameTheme *theme);
0082     /// This method reads theme description files from a standard location.
0083     /// The @p directory argument is passed to QStandardPaths like this:
0084     /// @code
0085     /// QStandardPaths::locateAll(QStandardPaths::AppDataLocation, directory, QStandardPaths::LocateDirectory)
0086     /// @endcode
0087     /// The typical usage is to install theme description files in
0088     /// @code
0089     ///${KDE_INSTALL_DATADIR}/<appname>/themes
0090     /// @endcode
0091     /// and then call:
0092     /// @code
0093     /// themeProvider.discoverThemes(QStringLiteral("themes"));
0094     /// @endcode
0095     /// If a @p themeClass's QMetaObject is given, the created themes will be
0096     /// instances of this KGameTheme subclass. The @p themeClass must export
0097     /// (with the Q_INVOKABLE marker) a constructor with the same signature
0098     /// as the KGameTheme constructor.
0099     /// @since 7.4
0100     void discoverThemes(const QString &directory, const QString &defaultThemeName = QStringLiteral("default"), const QMetaObject *themeClass = nullptr);
0101     /// After this provider has been set up with discoverThemes(), this
0102     /// method may be used to read additional themes which were added since
0103     /// the discoverThemes() call. This is esp. useful for KNewStuff
0104     /// integration.
0105     void rediscoverThemes();
0106 
0107     /// Generate a preview pixmap for the given theme. The application will
0108     /// typically want to reimplement this to load the given theme into a
0109     /// KGameRenderer and then arrange some sprites into a preview.
0110     ///
0111     /// @a size is the maximal allowed size.
0112     ///
0113     /// The default implementation tries to load a preview image from
0114     /// KGameTheme::previewPath(), and resizes the result to fit in @a size.
0115     virtual QPixmap generatePreview(const KGameTheme *theme, QSize size);
0116 
0117     /// Registers this KGameThemeProvider with @a engine's root context with ID
0118     /// @a name and constructs a KGameImageProvider corresponding
0119     /// to this KGameThemeProvider and adds it to the QML engine, also
0120     /// with @a name, which will receive sprite requests
0121     /// @since 4.11
0122     void setDeclarativeEngine(const QString &name, QQmlEngine *engine);
0123 
0124 Q_SIGNALS:
0125     /// Emitted when the current theme changes. @see setCurrentTheme
0126     void currentThemeChanged(const KGameTheme *theme);
0127     /// Emitted when the name of the provider changes.
0128     /// @since 4.11
0129     void nameChanged(const QString &name);
0130     /// Emitted when the name of the current theme changes.
0131     /// @since 4.11
0132     void currentThemeNameChanged(const QString &themeName);
0133 
0134 public Q_SLOTS:
0135     /// Select a new theme. The given theme must already have been added to
0136     /// this instance.
0137     void setCurrentTheme(const KGameTheme *theme);
0138 
0139 private:
0140     std::unique_ptr<class KGameThemeProviderPrivate> const d_ptr;
0141     Q_DECLARE_PRIVATE(KGameThemeProvider)
0142 };
0143 
0144 Q_DECLARE_METATYPE(KGameThemeProvider *)
0145 QML_DECLARE_TYPE(KGameThemeProvider *)
0146 
0147 #endif // KGAMETHEMEPROVIDER_H