Warning, file /graphics/krita/interfaces/KoID.h was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /* This file is part of the KDE project
0002  * SPDX-FileCopyrightText: 2006 Thomas Zander <zander@kde.org>
0003  * SPDX-FileCopyrightText: 2004 Cyrille Berger <cberger@cberger.net>
0004  * SPDX-FileCopyrightText: 2006 Boudewijn Rempt <boud@valdyas.org>
0005  *
0006  * SPDX-License-Identifier: LGPL-2.0-or-later
0007  */
0008 
0009 #ifndef _KO_ID_H_
0010 #define _KO_ID_H_
0011 
0012 #include <QString>
0013 #include <QMetaType>
0014 #include <QDebug>
0015 #include <boost/optional.hpp>
0016 
0017 #include <klocalizedstring.h>
0018 #include <KisLazyStorage.h>
0019 
0020 /**
0021  * A KoID is a combination of a user-visible string and a string that uniquely
0022  * identifies a given resource across languages.
0023  */
0024 class KoID
0025 {
0026 private:
0027     struct TranslatedString : public QString
0028     {
0029         TranslatedString(const boost::optional<KLocalizedString> &source)
0030             : QString(!source->isEmpty() ? source->toString() : QString())
0031         {
0032         }
0033 
0034         TranslatedString(const QString &value)
0035             : QString(value)
0036         {
0037         }
0038     };
0039 
0040     using StorageType =
0041         KisLazyStorage<TranslatedString,
0042         boost::optional<KLocalizedString>>;
0043 
0044     struct KoIDPrivate {
0045         KoIDPrivate(const QString &_id, const KLocalizedString &_name)
0046             : id(_id),
0047               name(_name)
0048         {}
0049 
0050         KoIDPrivate(const QString &_id, const QString &_name)
0051             : id(_id),
0052               name(StorageType::init_value_tag(), _name)
0053         {}
0054 
0055         QString id;
0056         StorageType name;
0057     };
0058 
0059 public:
0060     KoID()
0061         : m_d(new KoIDPrivate(QString(), QString()))
0062     {}
0063 
0064     /**
0065      * Construct a KoID with the given id, and name, id is the untranslated
0066      * official name of the id, name should be translatable as it will be used
0067      * in the UI.
0068      *
0069      * @code
0070      * KoID("id", i18n("name"))
0071      * @endcode
0072      */
0073     explicit KoID(const QString &id, const QString &name = QString())
0074         : m_d(new KoIDPrivate(id, name))
0075     {}
0076 
0077     /**
0078      * Use this constructore for static KoID. as KoID("id", ki18n("name"));
0079      * the name will be translated the first time it is needed. This is
0080      * important because static objects are constructed before translations
0081      * are initialized.
0082      */
0083     explicit KoID(const QString &id, const KLocalizedString &name)
0084         : m_d(new KoIDPrivate(id, name))
0085     {}
0086 
0087     KoID(const KoID &rhs)
0088         : m_d(rhs.m_d)
0089     {
0090     }
0091 
0092     KoID &operator=(const KoID &rhs)
0093     {
0094         if (this != &rhs) {
0095             m_d = rhs.m_d;
0096         }
0097         return *this;
0098     }
0099 
0100     QString id() const
0101     {
0102         return m_d->id;
0103     }
0104 
0105     QString name() const
0106     {
0107         return *m_d->name;
0108     }
0109 
0110     friend inline bool operator==(const KoID &, const KoID &);
0111     friend inline bool operator!=(const KoID &, const KoID &);
0112     friend inline bool operator<(const KoID &, const KoID &);
0113     friend inline bool operator>(const KoID &, const KoID &);
0114 
0115     static bool compareNames(const KoID &id1, const KoID &id2)
0116     {
0117         return id1.name() < id2.name();
0118     }
0119 
0120 private:
0121     QSharedPointer<KoIDPrivate> m_d;
0122 };
0123 
0124 Q_DECLARE_METATYPE(KoID)
0125 
0126 inline bool operator==(const KoID &v1, const KoID &v2)
0127 {
0128     return v1.m_d == v2.m_d || v1.m_d->id == v2.m_d->id;
0129 }
0130 
0131 inline bool operator!=(const KoID &v1, const KoID &v2)
0132 {
0133     return !(v1 == v2);
0134 }
0135 
0136 inline bool operator<(const KoID &v1, const KoID &v2)
0137 {
0138     return v1.m_d->id < v2.m_d->id;
0139 }
0140 
0141 inline bool operator>(const KoID &v1, const KoID &v2)
0142 {
0143     return v1.m_d->id > v2.m_d->id;;
0144 }
0145 
0146 inline QDebug operator<<(QDebug dbg, const KoID &id)
0147 {
0148     dbg.nospace() << id.name() << " (" << id.id() << " )";
0149 
0150     return dbg.space();
0151 }
0152 
0153 #endif