File indexing completed on 2024-12-22 04:14:00

0001 /* This file is part of the KDE project
0002  * SPDX-FileCopyrightText: 2013 Martin Gräßlin <mgraesslin@kde.org>
0003  *
0004  * SPDX-License-Identifier: LGPL-2.0-or-later
0005  */
0006 #ifndef KCOLORSCHEMEMANAGER_H
0007 #define KCOLORSCHEMEMANAGER_H
0008 
0009 #include <kritawidgetutils_export.h>
0010 
0011 #include <QObject>
0012 
0013 class QAbstractItemModel;
0014 
0015 class KActionMenu;
0016 class KColorSchemeManagerPrivate;
0017 
0018 /**
0019  * A small helper to get access to all available color schemes and activating a scheme in the
0020  * QApplication. This is useful for applications which want to provide a selection of custom color
0021  * schemes to their user. For example it is very common for photo and painting applications to use
0022  * a dark color scheme even if the default is a light scheme.
0023  *
0024  * The KColorSchemeManager provides access to a QAbstractItemModel which holds all the available
0025  * schemes. A possible usage looks like the following:
0026  *
0027  * @code
0028  * KColorSchemeManager *schemes = new KColorSchemeManager(this);
0029  * QListView *view = new QListView(this);
0030  * view->setModel(schemes->model());
0031  * connect(view, &QListView::activated, schemes, &KColorSchemeManager::activateScheme);
0032  * @endcode
0033  *
0034  * In addition the KColorSchemeManager also provides the possibility to create a KActionMenu populated
0035  * with all the available color schemes in an action group. If one of the actions is selected the
0036  * scheme is applied instantly:
0037  *
0038  * @code
0039  * QToolButton *button = new QToolButton();
0040  * KColorSchemeManager *schemes = new KColorSchemeManager(this);
0041  * KActionMenu *menu = schemes->createSchemeSelectionMenu(QStringLiteral("Oxygen"), button);
0042  * button->setMenu(menu->menu());
0043  * @endcode
0044  *
0045  * @since 5.0
0046  */
0047 class KRITAWIDGETUTILS_EXPORT KColorSchemeManager : public QObject
0048 {
0049     Q_OBJECT
0050 public:
0051     explicit KColorSchemeManager(QObject *parent = 0);
0052     ~KColorSchemeManager() override;
0053 
0054     /**
0055      * A QAbstractItemModel of all available color schemes.
0056      *
0057      * The model provides the name of the scheme in Qt::DisplayRole, a preview
0058      * in Qt::DelegateRole and the full path to the scheme file in Qt::UserRole.
0059      *
0060      * @return Model of all available color schemes.
0061      */
0062     QAbstractItemModel *model() const;
0063     /**
0064      * Returns the model index for the scheme with the given @p name. If no such
0065      * scheme exists an invalid index is returned.
0066      * @see model
0067      */
0068     QModelIndex indexForScheme(const QString &name) const;
0069 
0070     /**
0071      * Creates a KActionMenu populated with all the available color schemes.
0072      * All actions are in an action group and when one of the actions is triggered the scheme
0073      * referenced by this action is activated.
0074      *
0075      * The color scheme with the same name as @p selectedSchemeName will be checked. If none
0076      * of the available color schemes has the same name, no action will be checked.
0077      *
0078      * The KActionMenu will not be updated in case the installed color schemes change. It's the
0079      * task of the user of the KActionMenu to monitor for changes if required.
0080      *
0081      * @param icon The icon to use for the KActionMenu
0082      * @param text The text to use for the KActionMenu
0083      * @param selectedSchemeName The name of the color scheme to select
0084      * @param parent The parent of the KActionMenu
0085      * @return KActionMenu populated with all available color schemes.
0086      * @see activateScheme
0087      */
0088     KActionMenu *createSchemeSelectionMenu(const QIcon &icon, const QString &text, const QString &selectedSchemeName, QObject *parent);
0089     KActionMenu *createSchemeSelectionMenu(const QString &text, const QString &selectedSchemeName, QObject *parent);
0090     KActionMenu *createSchemeSelectionMenu(const QString &selectedSchemeName, QObject *parent);
0091 
0092 public Q_SLOTS:
0093     /**
0094      * @brief Activates the KColorScheme identified by the provided @p index.
0095      *
0096      * Installs the KColorScheme as the QApplication's QPalette.
0097      *
0098      * @param index The index for the KColorScheme to activate.
0099      * The index must reference the QAbstractItemModel provided by @c model
0100      * @see model
0101      */
0102     void activateScheme(const QModelIndex &index);
0103 
0104 private:
0105     QScopedPointer<KColorSchemeManagerPrivate> d;
0106 };
0107 
0108 #endif