File indexing completed on 2024-05-19 16:47:29

0001 // clang-format off
0002 /*
0003  * KDiff3 - Text Diff And Merge Tool
0004  *
0005  * SPDX-FileCopyrightText: 2002-2011 Joachim Eibl, joachim.eibl at gmx.de
0006  * SPDX-FileCopyrightText: 2018-2020 Michael Reeves reeves.87@gmail.com
0007  * SPDX-License-Identifier: GPL-2.0-or-later
0008  */
0009 // clang-format on
0010 
0011 #ifndef OPTIONITEMS_H
0012 #define OPTIONITEMS_H
0013 
0014 #include "common.h"
0015 #include "TypeUtils.h"
0016 
0017 #include <boost/signals2.hpp>
0018 
0019 #include <QComboBox>
0020 #include <QLabel>
0021 #include <QLineEdit>
0022 #include <QPushButton>
0023 #include <QString>
0024 #include <QTextCodec>
0025 
0026 #include <KLocalizedString>
0027 
0028 class OptionItemBase
0029 {
0030   public:
0031     explicit OptionItemBase(const QString& saveName);
0032     virtual ~OptionItemBase() = default;
0033 
0034     virtual void setToDefault() = 0;
0035     virtual void setToCurrent() = 0;
0036 
0037     virtual void apply() = 0;
0038 
0039     virtual void write(ValueMap*) const = 0;
0040     virtual void read(ValueMap*) = 0;
0041 
0042     void preserve()
0043     {
0044         if(!m_bPreserved)
0045         {
0046             m_bPreserved = true;
0047             preserveImp();
0048         }
0049     }
0050 
0051     void unpreserve()
0052     {
0053         if(m_bPreserved)
0054         {
0055             unpreserveImp();
0056         }
0057     }
0058 
0059     bool accept(const QString& key, const QString& val);
0060 
0061     [[nodiscard]] QString getSaveName() const { return m_saveName; }
0062   protected:
0063     virtual void preserveImp() = 0;
0064     virtual void unpreserveImp() = 0;
0065     bool m_bPreserved;
0066     QString m_saveName;
0067     std::list<boost::signals2::scoped_connection> connections;
0068     Q_DISABLE_COPY(OptionItemBase)
0069 };
0070 
0071 template <class T>
0072 class Option : public OptionItemBase
0073 {
0074   public:
0075     explicit Option(const QString& saveName)
0076         : OptionItemBase(saveName)
0077     {
0078     }
0079 
0080     explicit Option(T* pVar, const QString& saveName):OptionItemBase(saveName)
0081     {
0082         m_pVar = pVar;
0083     }
0084 
0085     explicit Option(const T& defaultVal, const QString& saveName, T* pVar)
0086         : Option<T>(pVar, defaultVal, saveName)
0087     {
0088     }
0089 
0090     explicit Option(T* pVar, const T& defaultValue, const QString& saveName)
0091         : OptionItemBase(saveName)
0092     {
0093         m_pVar = pVar;
0094         m_defaultVal = defaultValue;
0095     }
0096 
0097     void setToDefault() override {};
0098     void setToCurrent() override {};
0099     [[nodiscard]] const T& getDefault() const { return m_defaultVal; };
0100     [[nodiscard]] const T getCurrent() const { return *m_pVar; };
0101 
0102     virtual void setCurrent(const T& inValue) { *m_pVar = inValue; }
0103 
0104     void apply() override {};
0105     virtual void apply(const T& inValue) { *m_pVar = inValue; }
0106 
0107     void write(ValueMap* config) const override { config->writeEntry(m_saveName, *m_pVar); }
0108     void read(ValueMap* config) override { *m_pVar = config->readEntry(m_saveName, m_defaultVal); }
0109 
0110   protected:
0111     void preserveImp() override { m_preservedVal = *m_pVar; }
0112     void unpreserveImp() override { *m_pVar = m_preservedVal; }
0113     T* m_pVar = nullptr;
0114     T m_preservedVal;
0115     T m_defaultVal;
0116 
0117   private:
0118     Q_DISABLE_COPY(Option)
0119 };
0120 
0121 template <class T>
0122 class OptionNum : public Option<T>
0123 {
0124   public:
0125     using Option<T>::Option;
0126     explicit OptionNum(T* pVar, const QString& saveName)
0127         : Option<T>(pVar, saveName)
0128     {
0129     }
0130 
0131     explicit OptionNum(T* pVar, const T& defaultValue, const QString& saveName)
0132         : Option<T>(pVar, defaultValue, saveName)
0133     {
0134     }
0135 
0136     void setCurrent(const T& inValue) override
0137     {
0138         Option<T>::setCurrent(inValue);
0139     }
0140 
0141     static const QString toString(const T inValue)
0142     {
0143         //QString::setNum does not use locale formatting instead it always use QLocale::C.
0144         return QLocale().toString(inValue);
0145     }
0146     [[nodiscard]] const QString getString() const
0147     {
0148         //QString::setNum does not use locale formatting instead it always use QLocale::C.
0149         return QLocale().toString(Option<T>::getCurrent());
0150     }
0151 
0152   private:
0153     Q_DISABLE_COPY(OptionNum)
0154 };
0155 
0156 typedef Option<bool> OptionToggleAction;
0157 typedef OptionNum<qint32> OptionInt;
0158 typedef Option<QPoint> OptionPoint;
0159 typedef Option<QSize> OptionSize;
0160 typedef Option<QStringList> OptionStringList;
0161 
0162 typedef Option<bool> OptionBool;
0163 typedef Option<QFont> OptionFont;
0164 typedef Option<QColor> OptionColor;
0165 typedef Option<QString> OptionString;
0166 
0167 class OptionCodec : public OptionString
0168 {
0169   public:
0170     using OptionString::Option;
0171 
0172     void setCurrent(const QString& name) override { OptionString::setCurrent(name); };
0173     void setCurrent(const QByteArray& name) { OptionString::setCurrent(QString::fromLatin1(name)); }
0174     [[nodiscard]] const QString& defaultName() const { return mDefaultName; }
0175 
0176     void saveDefaultIndex(const SafeInt<qint32> i) { defaultIndex = i; };
0177     [[nodiscard]] qint32 getDefaultIndex() const { return defaultIndex; }
0178 
0179   private:
0180     const QString mDefaultName = QLatin1String(QTextCodec::codecForLocale()->name());
0181     qint32 defaultIndex = 0;
0182     Q_DISABLE_COPY(OptionCodec)
0183 };
0184 
0185 #endif // !OPTIONITEMS_H