File indexing completed on 2024-05-05 17:43:13

0001 /*
0002  *   SPDX-FileCopyrightText: 2017 Ivan Cukic <ivan.cukic (at) kde.org>
0003  *
0004  *   SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0005  */
0006 
0007 #ifndef PLASMAVAULT_KDED_UI_DIALOG_DSL_H
0008 #define PLASMAVAULT_KDED_UI_DIALOG_DSL_H
0009 
0010 #include <QHash>
0011 #include <QString>
0012 #include <QVariant>
0013 #include <QWidget>
0014 
0015 #include <functional>
0016 
0017 #include <vault.h>
0018 
0019 namespace DialogDsl
0020 {
0021 // We want to have a normal ID for the QMap or QHash,
0022 // but we also want it to have a translated name
0023 class Key : public QByteArray
0024 {
0025 public:
0026     Key(const QByteArray &id, const QString &translation = QString());
0027 
0028     QString translation() const;
0029 
0030 private:
0031     QString m_translation;
0032 };
0033 
0034 namespace operators
0035 {
0036 // A nicer way to define a Key and its translation
0037 inline Key operator/(const char *id, const QString &name)
0038 {
0039     return Key(id, name);
0040 }
0041 }
0042 
0043 // A dialog module can return a set of configured key-value pairs
0044 class DialogModule : public QWidget
0045 {
0046     Q_OBJECT
0047 
0048     Q_PROPERTY(bool isValid READ isValid WRITE setIsValid NOTIFY isValidChanged)
0049 
0050 public:
0051     DialogModule(bool isValid);
0052 
0053     virtual PlasmaVault::Vault::Payload fields() const = 0;
0054     virtual void init(const PlasmaVault::Vault::Payload &payload);
0055 
0056     virtual void aboutToBeShown();
0057     virtual bool shouldBeShown() const;
0058     virtual void aboutToBeHidden();
0059 
0060     bool isValid() const;
0061     void setIsValid(bool valid);
0062 
0063 Q_SIGNALS:
0064     void isValidChanged(bool valid);
0065     void requestCancellation();
0066 
0067 private:
0068     bool m_isValid;
0069 };
0070 
0071 typedef std::function<DialogModule *()> ModuleFactory;
0072 
0073 class step : public QVector<ModuleFactory>
0074 {
0075 public:
0076     step() = default;
0077 
0078     step(const std::initializer_list<ModuleFactory> &modules)
0079         : QVector<ModuleFactory>(modules)
0080     {
0081     }
0082 
0083     friend step operator/(const QString &title, step &&from)
0084     {
0085         step result(std::move(from));
0086         result.m_title = title;
0087         return result;
0088     }
0089 
0090     QString title() const
0091     {
0092         return m_title;
0093     }
0094 
0095 private:
0096     QString m_title;
0097 };
0098 
0099 typedef QVector<step> steps;
0100 typedef QMap<Key, steps> Logic;
0101 
0102 // If we want to have a single page containing multiple modules
0103 class CompoundDialogModule : public DialogModule
0104 {
0105 public:
0106     CompoundDialogModule(const step &children);
0107 
0108     PlasmaVault::Vault::Payload fields() const override;
0109     void init(const PlasmaVault::Vault::Payload &payload) override;
0110 
0111 private:
0112     QVector<DialogModule *> m_children;
0113     QSet<DialogModule *> m_invalidChildren;
0114 };
0115 
0116 } // namespace DialogDsl
0117 
0118 #endif // include guard