File indexing completed on 2024-05-12 05:46:39

0001 /***************************************************************************
0002  *   Copyright 2012 Stefan Majewsky <majewsky@gmx.net>                     *
0003  *                                                                         *
0004  *   This program is free software; you can redistribute it and/or modify  *
0005  *   it under the terms of the GNU Library General Public License          *
0006  *   version 2 as published by the Free Software Foundation                *
0007  *                                                                         *
0008  *   This program is distributed in the hope that it will be useful,       *
0009  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0010  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0011  *   GNU Library General Public License for more details.                  *
0012  *                                                                         *
0013  *   You should have received a copy of the GNU Library General Public     *
0014  *   License along with this program; if not, write to the                 *
0015  *   Free Software Foundation, Inc.,                                       *
0016  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
0017  ***************************************************************************/
0018 
0019 #ifndef KGTHEME_H
0020 #define KGTHEME_H
0021 
0022 //The complete API needs to be there when compiling libkdegames.
0023 #ifdef MAKE_KDEGAMES_LIB
0024 #define KGTHEME_PROVIDE_COMPATIBILITY_API
0025 #endif //MAKE_KDEGAMES_LIB
0026 
0027 #include <QMetaType>
0028 #include <QObject>
0029 #include <QLoggingCategory>
0030 #include <QPixmap>
0031 
0032 #include <libkdegames_export.h>
0033 
0034 /**
0035  * @class KgTheme kgtheme.h <KgTheme>
0036  *
0037  * A theme describes the visual appearance of a game. Themes in kdegames usually
0038  * reference a SVGZ file which is loaded into a KGameRenderer to provide pixmaps
0039  * for use on the game canvas.
0040  *
0041  * Themes are usually managed (and discovered) by a KgThemeProvider.
0042  *
0043  * @section fileformat Default file format for theme descriptions
0044  *
0045  * Although KgTheme and KgThemeProvider do not need special theme description
0046  * files for most basic usage, there is a format for theme description files
0047  * based on the XDG Desktop File Specification. The following example shows the
0048  * recognized keys:
0049  *
0050  * @code
0051  * [KGameTheme]
0052  * VersionFormat=1
0053  * Name=Example
0054  * Description=This theme serves as an example.
0055  * FileName=example.svgz
0056  * Author=John Doe
0057  * AuthorEmail=john.doe@example.com
0058  * Preview=example.png
0059  * @endcode
0060  *
0061  * All keys are considered optional, except perhaps for the "FileName" key: If
0062  * that is not given, the theme cannot be used with KGameRenderer. The paths in
0063  * "FileName" and "Preview" are resolved relative to the directory that contains
0064  * the theme description file.
0065  *
0066  * If the [KGameTheme] group contains any further keys, their values can be
0067  * retrieved through the KgTheme::customData() method.
0068  */
0069 
0070 Q_DECLARE_LOGGING_CATEGORY(GAMES_LIB)
0071 
0072 class KDEGAMES_EXPORT KgTheme : public QObject
0073 {
0074     Q_OBJECT
0075     Q_PROPERTY(QByteArray identifier READ identifier NOTIFY readOnlyProperty)
0076     //it is not intended to allow these properties to change after the initial
0077     //setup (note how KgThemeProvider returns only const KgTheme*), hence
0078     //a dummy NOTIFY signal is enough
0079     Q_PROPERTY(QString name READ name WRITE setName NOTIFY readOnlyProperty)
0080     Q_PROPERTY(QString description READ description WRITE setDescription NOTIFY readOnlyProperty)
0081     Q_PROPERTY(QString author READ author WRITE setAuthor NOTIFY readOnlyProperty)
0082     Q_PROPERTY(QString authorEmail READ authorEmail WRITE setAuthorEmail NOTIFY readOnlyProperty)
0083     Q_PROPERTY(QString graphicsPath READ graphicsPath WRITE setGraphicsPath NOTIFY readOnlyProperty)
0084     Q_PROPERTY(QString previewPath READ previewPath WRITE setPreviewPath NOTIFY readOnlyProperty)
0085     Q_DISABLE_COPY(KgTheme)
0086     public:
0087         ///Constructor. The @a identifier must be application-unique.
0088         explicit KgTheme(const QByteArray& identifier, QObject* parent = nullptr);
0089         ///Destructor.
0090         virtual ~KgTheme();
0091 
0092         ///Initializes a KgTheme instance by reading a description file.
0093         ///@return whether @a path is a valid theme description file (if not,
0094         ///        the theme instance is not changed by this method call)
0095         ///@note A non-static member function has been chosen over the more
0096         ///      common pattern of using a static member function like
0097         ///      "KgTheme::fromDesktopFile" to accommodate applications which
0098         ///      want to subclass KgTheme.
0099         virtual bool readFromDesktopFile(const QString& path);
0100 
0101 #ifdef KGTHEME_PROVIDE_COMPATIBILITY_API
0102         ///Use a different group name in theme description files. For example,
0103         ///KMahjongg backgrounds and tilesets use "[KMahjonggBackground]" and
0104         ///"[KMahjonggTileset]" instead of "[KGameTheme]".
0105         static void addConfigGroupName(const QString& name);
0106 #endif
0107 
0108         ///@return the internal identifier for this theme (used e.g. for
0109         ///        finding a pixmap cache or storing a theme selection)
0110         QByteArray identifier() const;
0111 
0112         ///@return the name of this theme
0113         QString name() const;
0114         ///@see name()
0115         void setName(const QString& name);
0116         ///@return an additional description beyond the name()
0117         QString description() const;
0118         ///@see description()
0119         void setDescription(const QString& description);
0120         ///@return the name of the theme author
0121         QString author() const;
0122         ///@see author()
0123         void setAuthor(const QString& author);
0124         ///@return the email address of the theme author
0125         QString authorEmail() const;
0126         ///@see authorEmail()
0127         void setAuthorEmail(const QString& authorEmail);
0128 
0129         ///@return the path of the SVG file which holds the theme contents
0130         QString graphicsPath() const;
0131         ///@see graphicsPath()
0132         void setGraphicsPath(const QString& path);
0133         ///@return the path to an image file containing a preview, or an empty
0134         ///        string if no preview file is known
0135         QString previewPath() const;
0136         ///@see previewPath()
0137         void setPreviewPath(const QString& path);
0138 
0139         ///@return custom data
0140         ///
0141         ///This API is provided for theme description files which contain
0142         ///additional application-specific metadata.
0143         QMap<QString, QString> customData() const;
0144         ///@overload that returns a single value from the customData() map
0145         QString customData(const QString& key, const QString& defaultValue = QString()) const;
0146         ///@see customData()
0147         void setCustomData(const QMap<QString, QString>& customData);
0148     Q_SIGNALS:
0149         ///This signal is never emitted. It is provided because QML likes to
0150         ///complain about properties without NOTIFY signals, even readonly ones.
0151         void readOnlyProperty();
0152     private:
0153         class Private;
0154         Private* const d;
0155 };
0156 
0157 Q_DECLARE_METATYPE(const KgTheme*)
0158 
0159 #endif // KGTHEME_H