1 | /* This file is part of the KDE project |
2 | * Copyright (C) 2006 Thomas Zander <zander@kde.org> |
3 | * Copyright (c) 2004 Cyrille Berger <cberger@cberger.net> |
4 | * Copyright (c) 2006 Boudewijn Rempt <boud@valdyas.org> |
5 | * |
6 | * This library is free software; you can redistribute it and/or |
7 | * modify it under the terms of the GNU Library General Public |
8 | * License as published by the Free Software Foundation; either |
9 | * version 2 of the License, or (at your option) any later version. |
10 | * |
11 | * This library is distributed in the hope that it will be useful, |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
14 | * Library General Public License for more details. |
15 | * |
16 | * You should have received a copy of the GNU Library General Public License |
17 | * along with this library; see the file COPYING.LIB. If not, write to |
18 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
19 | * Boston, MA 02110-1301, USA. |
20 | */ |
21 | |
22 | #ifndef _KO_ID_H_ |
23 | #define _KO_ID_H_ |
24 | |
25 | #include <QString> |
26 | #include <QMetaType> |
27 | #include <QDebug> |
28 | |
29 | #include <klocalizedstring.h> |
30 | |
31 | /** |
32 | * A KoID is a combination of a user-visible string and a string that uniquely |
33 | * identifies a given resource across languages. |
34 | */ |
35 | class KoID |
36 | { |
37 | public: |
38 | KoID() : m_id(), m_name() {} |
39 | |
40 | /** |
41 | * Construct a KoID with the given id, and name, id is the untranslated |
42 | * official name of the id, name should be translatable as it will be used |
43 | * in the UI. |
44 | * |
45 | * @code |
46 | * KoID("id", i18n("name")) |
47 | * @endcode |
48 | */ |
49 | explicit KoID(const QString & id, const QString & name = QString()) |
50 | : m_id(id), |
51 | m_name(name) {} |
52 | |
53 | /** |
54 | * Use this constructore for static KoID. as KoID("id", ki18n("name")); |
55 | * the name will be translated the first time it is needed. This is |
56 | * important because static objects are constructed before translations |
57 | * are initialized. |
58 | */ |
59 | explicit KoID(const QString & id, const KLocalizedString& name ) |
60 | : m_id(id), |
61 | m_localizedString(name) {} |
62 | |
63 | |
64 | KoID(const KoID &rhs) |
65 | { |
66 | m_id = rhs.m_id; |
67 | m_name = rhs.name(); |
68 | } |
69 | |
70 | QString id() const { |
71 | return m_id; |
72 | } |
73 | |
74 | QString name() const { |
75 | if(m_name.isEmpty()) m_name = m_localizedString.toString(); |
76 | return m_name; |
77 | } |
78 | |
79 | friend inline bool operator==(const KoID &, const KoID &); |
80 | friend inline bool operator!=(const KoID &, const KoID &); |
81 | friend inline bool operator<(const KoID &, const KoID &); |
82 | friend inline bool operator>(const KoID &, const KoID &); |
83 | |
84 | private: |
85 | |
86 | QString m_id; |
87 | mutable QString m_name; |
88 | KLocalizedString m_localizedString; |
89 | |
90 | }; |
91 | |
92 | Q_DECLARE_METATYPE(KoID) |
93 | |
94 | inline bool operator==(const KoID &v1, const KoID &v2) |
95 | { |
96 | return v1.m_id == v2.m_id; |
97 | } |
98 | |
99 | inline bool operator!=(const KoID &v1, const KoID &v2) |
100 | { |
101 | return v1.m_id != v2.m_id; |
102 | } |
103 | |
104 | |
105 | inline bool operator<(const KoID &v1, const KoID &v2) |
106 | { |
107 | return v1.m_id < v2.m_id; |
108 | } |
109 | |
110 | |
111 | inline bool operator>(const KoID &v1, const KoID &v2) |
112 | { |
113 | return v1.m_id > v2.m_id; |
114 | } |
115 | |
116 | inline QDebug operator<<(QDebug dbg, const KoID &id) |
117 | { |
118 | dbg.nospace() << id.name() << " (" << id.id() << " )" ; |
119 | |
120 | return dbg.space(); |
121 | } |
122 | |
123 | |
124 | #endif |
125 | |