File indexing completed on 2024-05-12 13:00:15

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 
0016 #include <klocalizedstring.h>
0017 
0018 /**
0019  * A KoID is a combination of a user-visible string and a string that uniquely
0020  * identifies a given resource across languages.
0021  */
0022 class KoID
0023 {
0024 public:
0025     KoID() : m_id(), m_name() {}
0026 
0027     /**
0028      * Construct a KoID with the given id, and name, id is the untranslated
0029      * official name of the id, name should be translatable as it will be used
0030      * in the UI.
0031      * 
0032      * @code
0033      * KoID("id", i18n("name"))
0034      * @endcode
0035      */
0036     explicit KoID(const QString & id, const QString & name = QString())
0037             : m_id(id),
0038             m_name(name) {}
0039 
0040     /**
0041      * Use this constructor for static KoID. as KoID("id", ki18n("name"));
0042      * the name will be translated the first time it is needed. This is
0043      * important because static objects are constructed before translations
0044      * are initialized.
0045      */
0046     explicit KoID(const QString & id, const KLocalizedString& name )
0047             : m_id(id),
0048             m_localizedString(name) {}
0049 
0050 
0051     KoID(const KoID &rhs)
0052     {
0053         operator=(rhs);
0054     }
0055 
0056     KoID &operator=(const KoID &rhs)
0057     {
0058         m_id = rhs.m_id;
0059         m_name = rhs.name();
0060         return *this;
0061     }
0062 
0063     QString id() const {
0064         return m_id;
0065     }
0066 
0067     QString name() const {
0068         if (m_name.isEmpty() && !m_localizedString.isEmpty()) {
0069             m_name = m_localizedString.toString();
0070         }
0071         return m_name;
0072     }
0073 
0074     friend inline bool operator==(const KoID &, const KoID &);
0075     friend inline bool operator!=(const KoID &, const KoID &);
0076     friend inline bool operator<(const KoID &, const KoID &);
0077     friend inline bool operator>(const KoID &, const KoID &);
0078 
0079 private:
0080 
0081     QString m_id;
0082     mutable QString m_name;
0083     KLocalizedString m_localizedString;
0084 
0085 };
0086 
0087 Q_DECLARE_METATYPE(KoID)
0088 
0089 inline bool operator==(const KoID &v1, const KoID &v2)
0090 {
0091     return v1.m_id == v2.m_id;
0092 }
0093 
0094 inline bool operator!=(const KoID &v1, const KoID &v2)
0095 {
0096     return v1.m_id != v2.m_id;
0097 }
0098 
0099 
0100 inline bool operator<(const KoID &v1, const KoID &v2)
0101 {
0102     return v1.m_id < v2.m_id;
0103 }
0104 
0105 
0106 inline bool operator>(const KoID &v1, const KoID &v2)
0107 {
0108     return v1.m_id > v2.m_id;
0109 }
0110 
0111 inline QDebug operator<<(QDebug dbg, const KoID &id)
0112 {
0113     dbg.nospace() << id.name() << " (" << id.id() << " )";
0114 
0115     return dbg.space();
0116 }
0117 
0118 
0119 #endif