File indexing completed on 2024-06-23 04:27:52

0001 /*
0002  *  SPDX-FileCopyrightText: 2022 Dmitry Kazakov <dimula73@gmail.com>
0003  *
0004  *  SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 #ifndef KISPREFIXEDOPTIONDATAWRAPPER_H
0007 #define KISPREFIXEDOPTIONDATAWRAPPER_H
0008 
0009 #include <QString>
0010 #include <kis_properties_configuration.h>
0011 
0012 /**
0013  * KisPrefixedOptionDataWrapper wraps an option data type
0014  * so that it would support prefixed loading/saving. That
0015  * is mostly used for masked brush features.
0016  */
0017 
0018 template <typename T>
0019 struct KisPrefixedOptionDataWrapper : public T
0020 {
0021     static constexpr bool supports_prefix = true;
0022 
0023     KisPrefixedOptionDataWrapper(const QString &_prefix)
0024         : prefix(_prefix)
0025     {
0026     }
0027 
0028     bool read(const KisPropertiesConfiguration *setting) {
0029         if (!setting) return false;
0030 
0031         if (prefix.isEmpty()) {
0032             return T::read(setting);
0033         } else {
0034             KisPropertiesConfiguration prefixedSetting;
0035             setting->getPrefixedProperties(prefix, &prefixedSetting);
0036             return T::read(&prefixedSetting);
0037         }
0038     }
0039 
0040     void write(KisPropertiesConfiguration *setting) const {
0041         if (prefix.isEmpty()) {
0042             T::write(setting);
0043         } else {
0044             KisPropertiesConfiguration prefixedSetting;
0045             T::write(&prefixedSetting);
0046             setting->setPrefixedProperties(prefix, &prefixedSetting);
0047         }
0048     }
0049 
0050     QString prefix;
0051 };
0052 
0053 #endif // KISPREFIXEDOPTIONDATAWRAPPER_H