File indexing completed on 2024-05-05 17:04:28

0001 /* This file is part of the KDE project
0002  * Copyright (C) 2006 Thomas Zander <zander@kde.org>
0003  * Copyright (c) 2004 Cyrille Berger <cberger@cberger.net>
0004  * Copyright (c) 2006 Boudewijn Rempt <boud@valdyas.org>
0005  *
0006  * This library is free software; you can redistribute it and/or
0007  * modify it under the terms of the GNU Library General Public
0008  * License as published by the Free Software Foundation; either
0009  * version 2 of the License, or (at your option) any later version.
0010  *
0011  * This library is distributed in the hope that it will be useful,
0012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0014  * Library General Public License for more details.
0015  *
0016  * You should have received a copy of the GNU Library General Public License
0017  * along with this library; see the file COPYING.LIB.  If not, write to
0018  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0019  * Boston, MA 02110-1301, USA.
0020  */
0021 
0022 #ifndef _KO_ID_H_
0023 #define _KO_ID_H_
0024 
0025 #include <QString>
0026 #include <QMetaType>
0027 #include <QDebug>
0028 
0029 #include <klocalizedstring.h>
0030 
0031 /**
0032  * A KoID is a combination of a user-visible string and a string that uniquely
0033  * identifies a given resource across languages.
0034  */
0035 class KoID
0036 {
0037 public:
0038     KoID() : m_id(), m_name() {}
0039 
0040     /**
0041      * Construct a KoID with the given id, and name, id is the untranslated
0042      * official name of the id, name should be translatable as it will be used
0043      * in the UI.
0044      * 
0045      * @code
0046      * KoID("id", i18n("name"))
0047      * @endcode
0048      */
0049     explicit KoID(const QString & id, const QString & name = QString())
0050             : m_id(id),
0051             m_name(name) {}
0052 
0053     /**
0054      * Use this constructor for static KoID. as KoID("id", ki18n("name"));
0055      * the name will be translated the first time it is needed. This is
0056      * important because static objects are constructed before translations
0057      * are initialized.
0058      */
0059     explicit KoID(const QString & id, const KLocalizedString& name )
0060             : m_id(id),
0061             m_localizedString(name) {}
0062 
0063 
0064     KoID(const KoID &rhs)
0065     {
0066         m_id = rhs.m_id;
0067         m_name = rhs.name();
0068     }
0069 
0070     QString id() const {
0071         return m_id;
0072     }
0073 
0074     QString name() const {
0075         if (m_name.isEmpty() && !m_localizedString.isEmpty()) {
0076             m_name = m_localizedString.toString();
0077         }
0078         return m_name;
0079     }
0080 
0081     friend inline bool operator==(const KoID &, const KoID &);
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 
0086 private:
0087 
0088     QString m_id;
0089     mutable QString m_name;
0090     KLocalizedString m_localizedString;
0091 
0092 };
0093 
0094 Q_DECLARE_METATYPE(KoID)
0095 
0096 inline bool operator==(const KoID &v1, const KoID &v2)
0097 {
0098     return v1.m_id == v2.m_id;
0099 }
0100 
0101 inline bool operator!=(const KoID &v1, const KoID &v2)
0102 {
0103     return v1.m_id != v2.m_id;
0104 }
0105 
0106 
0107 inline bool operator<(const KoID &v1, const KoID &v2)
0108 {
0109     return v1.m_id < v2.m_id;
0110 }
0111 
0112 
0113 inline bool operator>(const KoID &v1, const KoID &v2)
0114 {
0115     return v1.m_id > v2.m_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 
0126 #endif