lxqtsettings.h 6.65 KB
Newer Older
1 2 3 4
/* BEGIN_COMMON_COPYRIGHT_HEADER
 * (c)LGPL2+
 *
 * LXQt - a lightweight, Qt based, desktop toolset
5
 * https://lxqt.org
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
 *
 * Copyright: 2010-2011 Razor team
 * Authors:
 *   Alexander Sokoloff <sokoloff.a@gmail.com>
 *
 * This program or library is free software; you can redistribute it
 * and/or modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.

 * You should have received a copy of the GNU Lesser General
 * Public License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301 USA
 *
 * END_COMMON_COPYRIGHT_HEADER */


#ifndef LXQTSETTINGS_H
#define LXQTSETTINGS_H

#include <QObject>
#include <QSettings>
#include <QSharedDataPointer>
#include "lxqtglobals.h"

class QEvent;
38

39
namespace LXQt
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
{

class SettingsPrivate;
class GlobalSettings;

/*! \brief User settings handling */
class LXQT_API Settings : public QSettings
{
    Q_OBJECT
public:

    /*! \brief Constructs a Settings object for accessing settings of the module called module, and with parent parent.
    Settings can be accessed using the standard interface provided by QSettings, but it also provides some convenience functions and signals.

    \param module a base name of the config file. It's a name without
            the extension. For example: if you want to open settings for
            panel create it as Settings("panel").
            The function will create all parent directories necessary to create
            the file.
    \param parent It's no need to delete this class manually if it's set.
    */
    explicit Settings(const QString& module, QObject* parent = 0);
    //explicit Settings(QObject* parent=0);
    explicit Settings(const QSettings* parentSettings, const QString& subGroup, QObject* parent=0);
    explicit Settings(const QSettings& parentSettings, const QString& subGroup, QObject* parent=0);
    Settings(const QString &fileName, QSettings::Format format, QObject *parent = 0);
    ~Settings();

    static const GlobalSettings *globalSettings();


    /*! Returns the localized value for key. In the desktop file keys may be postfixed by [LOCALE]. If the
        localized value doesn't exist, returns non lokalized value. If non localized value doesn't exist, returns defaultValue.
        LOCALE must be of the form lang_COUNTRY.ENCODING@MODIFIER, where _COUNTRY, .ENCODING, and @MODIFIER may be omitted.

        If no default value is specified, a default QVariant is returned. */
    QVariant localizedValue(const QString& key, const QVariant& defaultValue = QVariant()) const;

    /*! Sets the value of setting key to value. If a localized version of the key already exists, the previous value is
        overwritten. Otherwise, it overwrites the the un-localized version. */
    void setLocalizedValue(const QString &key, const QVariant &value);

signals:
83 84
    /*! /brief signal for backward compatibility (emitted whenever settingsChangedFromExternal() or settingsChangedByApp() is emitted)
     */
85
    void settingsChanged();
86 87 88 89 90 91
    /*! /brief signal emitted when the settings file is changed by external application
     */
    void settingsChangedFromExternal();
    /*! /brief signal emitted when any setting is changed by this object
     */
    void settingsChangedByApp();
92 93 94 95 96 97 98 99 100 101 102

protected:
    bool event(QEvent *event);

protected slots:
    /*! Called when the config file is changed */
    virtual void fileChanged();

private slots:
    void _fileChanged(QString path);

103 104 105
private:
    void addWatchedFile(QString const & path);

106 107 108 109 110 111 112 113
private:
    Q_DISABLE_COPY(Settings)

    SettingsPrivate* const d_ptr;
    Q_DECLARE_PRIVATE(Settings)
};


114
class LXQtThemeData;
115 116

/*! \brief QSS theme handling */
117
class LXQT_API LXQtTheme
118 119 120
{
public:
    /// Constructs a null theme.
121
    LXQtTheme();
122 123 124 125

    /*! Constructs an theme from the dir with the given path. If path not absolute
        (i.e. the theme name only) the relevant dir must be found relative to the
        $XDG_DATA_HOME + $XDG_DATA_DIRS directories. */
126
    LXQtTheme(const QString &path);
127 128

    /// Constructs a copy of other. This is very fast.
129
    LXQtTheme(const LXQtTheme &other);
130

131 132
    LXQtTheme& operator=(const LXQtTheme &other);
    ~LXQtTheme();
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158

    /// Returns the name of the theme.
    QString name() const;

    QString path() const;

    QString previewImage() const;

    /// Returns true if this theme is valid; otherwise returns false.
    bool isValid() const;

    /*! \brief Returns StyleSheet text (not file name, but real text) of the module called module.
        Paths in url() C/QSS functions are parsed to be real values for the theme,
        relative to full path
    */
    QString qss(const QString& module) const;

    /*! \brief A full path to image used as a wallpaper
     \param screen is an ID of the screen like in Qt. -1 means default (any) screen.
                   Any other value greater than -1 is the exact screen (in dualhead).
                  In themes the index starts from 1 (ix 1 means 1st screen).
     \retval QString a file name (including path).
    */
    QString desktopBackground(int screen=-1) const;

    /// Returns the current lxqt theme.
159
    static const LXQtTheme &currentTheme();
160 161

    /// Returns the all themes found in the system.
162
    static QList<LXQtTheme> allThemes();
163 164

private:
165 166
    static LXQtTheme* mInstance;
    QSharedDataPointer<LXQtThemeData> d;
167 168 169
};

/*!
170 171
A global pointer referring to the unique LXQtTheme object.
It is equivalent to the pointer returned by the LXQtTheme::instance() function.
172 173
Only one theme object can be created. !*/

174
#define lxqtTheme LXQtTheme::currentTheme()
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215


class LXQT_API SettingsCache
{
public:
    explicit SettingsCache(QSettings &settings);
    explicit SettingsCache(QSettings *settings);
    virtual ~SettingsCache() {}

    void loadFromSettings();
    void loadToSettings();

private:
    QSettings &mSettings;
    QHash<QString, QVariant> mCache;
};

class GlobalSettingsPrivate;

class GlobalSettings: public Settings
{
    Q_OBJECT
public:
    GlobalSettings();
    ~GlobalSettings();

signals:
    /// Signal emitted when the icon theme has changed.
    void iconThemeChanged();

    /// Signal emitted when the lxqt theme has changed.
    void lxqtThemeChanged();

protected slots:
    void fileChanged();

private:
    GlobalSettingsPrivate* const d_ptr;
    Q_DECLARE_PRIVATE(GlobalSettings)
};

216
} // namespace LXQt
217
#endif // LXQTSETTINGS_H