File indexing completed on 2024-05-19 04:25:11

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  * SPDX-FileCopyrightText: 2022 L. E. Segovia <amy@amyspark.me>
0006  *
0007  * SPDX-License-Identifier: LGPL-2.0-or-later
0008  */
0009 
0010 #ifndef _KO_ID_H_
0011 #define _KO_ID_H_
0012 
0013 #include <QDebug>
0014 #include <QMetaType>
0015 #include <QString>
0016 
0017 #include <boost/optional.hpp>
0018 #include <utility>
0019 
0020 #include <klocalizedstring.h>
0021 #include <KisLazyStorage.h>
0022 
0023 #include "kritaglobal_export.h"
0024 
0025 /**
0026  * A KoID is a combination of a user-visible string and a string that uniquely
0027  * identifies a given resource across languages.
0028  */
0029 class KRITAGLOBAL_EXPORT KoID
0030 {
0031 private:
0032     struct TranslatedString : public QString
0033     {
0034         TranslatedString(const boost::optional<KLocalizedString> &source);
0035 
0036         TranslatedString(const QString &value);
0037     };
0038 
0039     using StorageType =
0040         KisLazyStorage<TranslatedString,
0041         boost::optional<KLocalizedString>>;
0042 
0043     struct KoIDPrivate {
0044         KoIDPrivate(QString _id, const KLocalizedString &_name);
0045 
0046         KoIDPrivate(QString _id, const QString &_name);
0047 
0048         QString id;
0049         StorageType name;
0050     };
0051 
0052 public:
0053     KoID();
0054 
0055     /**
0056      * Construct a KoID with the given id, and name, id is the untranslated
0057      * official name of the id, name should be translatable as it will be used
0058      * in the UI.
0059      *
0060      * @code
0061      * KoID("id", i18n("name"))
0062      * @endcode
0063      */
0064     explicit KoID(const QString &id, const QString &name = QString());
0065 
0066     /**
0067      * Use this constructor for static KoID. as KoID("id", ki18n("name"));
0068      * the name will be translated the first time it is needed. This is
0069      * important because static objects are constructed before translations
0070      * are initialized.
0071      */
0072     explicit KoID(const QString &id, const KLocalizedString &name);
0073 
0074     KoID(const KoID &rhs);
0075 
0076     KoID &operator=(const KoID &rhs);
0077 
0078     QString id() const;
0079 
0080     QString name() const;
0081 
0082     friend inline bool operator==(const KoID &, const KoID &);
0083     friend inline bool operator!=(const KoID &, const KoID &);
0084     friend inline bool operator<(const KoID &, const KoID &);
0085     friend inline bool operator>(const KoID &, const KoID &);
0086 
0087     static bool compareNames(const KoID &id1, const KoID &id2)
0088     {
0089         return id1.name() < id2.name();
0090     }
0091 
0092 private:
0093     QSharedPointer<KoIDPrivate> m_d;
0094 };
0095 
0096 Q_DECLARE_METATYPE(KoID)
0097 
0098 inline bool operator==(const KoID &v1, const KoID &v2)
0099 {
0100     return v1.m_d == v2.m_d || v1.m_d->id == v2.m_d->id;
0101 }
0102 
0103 inline bool operator!=(const KoID &v1, const KoID &v2)
0104 {
0105     return !(v1 == v2);
0106 }
0107 
0108 inline bool operator<(const KoID &v1, const KoID &v2)
0109 {
0110     return v1.m_d->id < v2.m_d->id;
0111 }
0112 
0113 inline bool operator>(const KoID &v1, const KoID &v2)
0114 {
0115     return v1.m_d->id > v2.m_d->id;;
0116 }
0117 
0118 inline QDebug operator<<(QDebug dbg, const KoID &id)
0119 {
0120     dbg.nospace() << id.name() << " (" << id.id() << " )";
0121 
0122     return dbg.space();
0123 }
0124 
0125 #endif