File indexing completed on 2024-04-28 16:55:13

0001 /***************************************************************************
0002  *   Copyright (C) 2010 by Dario Freddi <drf@kde.org>                      *
0003  *                                                                         *
0004  *   This program is free software; you can redistribute it and/or modify  *
0005  *   it under the terms of the GNU General Public License as published by  *
0006  *   the Free Software Foundation; either version 2 of the License, or     *
0007  *   (at your option) any later version.                                   *
0008  *                                                                         *
0009  *   This program is distributed in the hope that it will be useful,       *
0010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0012  *   GNU General Public License for more details.                          *
0013  *                                                                         *
0014  *   You should have received a copy of the GNU General Public License     *
0015  *   along with this program; if not, write to the                         *
0016  *   Free Software Foundation, Inc.,                                       *
0017  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA .        *
0018  ***************************************************************************/
0019 
0020 
0021 #ifndef POWERDEVIL_POWERDEVILACTIONCONFIG_H
0022 #define POWERDEVIL_POWERDEVILACTIONCONFIG_H
0023 
0024 #include <QWidget>
0025 
0026 #include <KConfigGroup>
0027 
0028 namespace PowerDevil {
0029 
0030 /**
0031  * @brief The base class for Action's config interfaces
0032  *
0033  * This class should be reimplemented when creating a new Action. It is used to generate
0034  * a configuration UI for your action and integrate it into KDE Power Management System's
0035  * config module seamlessly.
0036  *
0037  * @par Creating an ActionConfig
0038  *
0039  * If you already have been through creating an Action, you have seen that Action's desktop
0040  * file contains already all the needed information for loading its respective ActionConfig. For this
0041  * reason, despite ActionConfig being a plugin, you do not need to create a desktop file for it -
0042  * you just have to write the correct info into the Action's one.
0043  *
0044  * @par The key/value pair rationale
0045  *
0046  * The config UI works in a key/value fashion, where value is a generic QWidget. This is done
0047  * to keep the UI consistent without hurting the flexibility of the implementation.
0048  * Please remember to keep the configuration as easy and essential as possible: if you have gone
0049  * beyond 2 rows, you might be doing something wrong.
0050  *
0051  * @par Loading and saving configuration
0052  *
0053  * Of course, you should also be providing the logic for saving and loading your action's configuration.
0054  * This is done through KConfigGroup: your action has a KConfigGroup assigned by KDE Power Management System
0055  * which can be accessed through configGroup or overwritten through setConfigGroup. The very same group
0056  * you are loading/saving here will be the one passed to Action::loadAction.
0057  *
0058  * @see Action
0059  *
0060  * @since 4.6
0061  */
0062 class Q_DECL_EXPORT ActionConfig : public QObject
0063 {
0064     Q_OBJECT
0065     Q_DISABLE_COPY(ActionConfig)
0066 
0067 public:
0068     /**
0069      * Default constructor
0070      */
0071     explicit ActionConfig(QObject *parent);
0072     /**
0073      * Default destructor
0074      */
0075     ~ActionConfig() override;
0076 
0077     /**
0078      * @returns The KConfigGroup associated to this action, where data should be saved (and loaded)
0079      *
0080      * @note You should not track this KConfigGroup, as it might change from profile to profile.
0081      *       Always use this function to access it.
0082      */
0083     KConfigGroup configGroup() const;
0084     /**
0085      * Overwrites the config group associated with this Action with @c group.
0086      *
0087      * @param group A KConfigGroup carrying the settings for this Action.
0088      */
0089     void setConfigGroup(const KConfigGroup &group);
0090 
0091     /**
0092      * This function should return the key/value pairs containing your Action's configuration UI.
0093      *
0094      * @returns A list of QString, QWidget pairs representing your Action's configuration UI
0095      */
0096     virtual QList< QPair< QString, QWidget* > > buildUi() = 0;
0097 
0098     /**
0099      * This function gets called whenever the parent module requires to load a specific configuration. This
0100      * usually occurs when @c configGroup is updated, for example due to the fact that the user wants to modify
0101      * a different profile. In this function, you should update the info contained in the widgets generated
0102      * by @c buildUi accordingly.
0103      */
0104     virtual void load() = 0;
0105     /**
0106      * This function gets called whenever the parent module requires to save the configuration. Usually, you
0107      * want to read values from the widgets generated with @c buildUi and call @c setConfigGroup to update the
0108      * Action's configuration.
0109      */
0110     virtual void save() = 0;
0111 
0112 protected Q_SLOTS:
0113     /**
0114      * Call this slot whenever the user makes some changes to the widgets.
0115      */
0116     void setChanged();
0117 
0118 Q_SIGNALS:
0119     /**
0120      * This signal gets emitted every time the user changes the (unsaved) configuration. Do not emit this signal
0121      * directly: call setChanged instead.
0122      *
0123      * @see setChanged
0124      */
0125     void changed();
0126 
0127 private:
0128     class Private;
0129     Private * const d;
0130 };
0131 
0132 }
0133 
0134 #endif // POWERDEVIL_POWERDEVILACTIONCONFIG_H