1 | /* |
---|---|
2 | This file is part of the KDE libraries |
3 | SPDX-FileCopyrightText: 2001 Holger Freyther <freyher@yahoo.com> |
4 | |
5 | based on ideas from Martijn and Simon |
6 | many thanks to Simon |
7 | |
8 | SPDX-License-Identifier: LGPL-2.0-only |
9 | */ |
10 | |
11 | #include "kguiitem.h" |
12 | |
13 | #include <QPushButton> |
14 | #include <QSharedData> |
15 | |
16 | class KGuiItemPrivate : public QSharedData |
17 | { |
18 | public: |
19 | KGuiItemPrivate() |
20 | { |
21 | m_enabled = true; |
22 | m_hasIcon = false; |
23 | } |
24 | |
25 | KGuiItemPrivate(const KGuiItemPrivate &other) = default; |
26 | |
27 | KGuiItemPrivate &operator=(const KGuiItemPrivate &other) = default; |
28 | |
29 | QString m_text; |
30 | QString m_toolTip; |
31 | QString m_whatsThis; |
32 | QString m_statusText; |
33 | QString m_iconName; |
34 | QIcon m_icon; |
35 | bool m_hasIcon : 1; |
36 | bool m_enabled : 1; |
37 | }; |
38 | |
39 | KGuiItem::KGuiItem() |
40 | : d(new KGuiItemPrivate) |
41 | { |
42 | } |
43 | |
44 | KGuiItem::KGuiItem(const QString &text, const QString &iconName, const QString &toolTip, const QString &whatsThis) |
45 | : d(new KGuiItemPrivate) |
46 | { |
47 | d->m_text = text; |
48 | d->m_toolTip = toolTip; |
49 | d->m_whatsThis = whatsThis; |
50 | setIconName(iconName); |
51 | } |
52 | |
53 | KGuiItem::KGuiItem(const QString &text, const QIcon &icon, const QString &toolTip, const QString &whatsThis) |
54 | : d(new KGuiItemPrivate) |
55 | { |
56 | d->m_text = text; |
57 | d->m_toolTip = toolTip; |
58 | d->m_whatsThis = whatsThis; |
59 | setIcon(icon); |
60 | } |
61 | |
62 | KGuiItem::KGuiItem(const KGuiItem &rhs) = default; |
63 | |
64 | KGuiItem &KGuiItem::operator=(const KGuiItem &rhs) = default; |
65 | |
66 | KGuiItem::~KGuiItem() = default; |
67 | |
68 | QString KGuiItem::text() const |
69 | { |
70 | return d->m_text; |
71 | } |
72 | |
73 | QString KGuiItem::plainText() const |
74 | { |
75 | const int len = d->m_text.length(); |
76 | |
77 | if (len == 0) { |
78 | return d->m_text; |
79 | } |
80 | |
81 | // Can assume len >= 1 from now on. |
82 | QString stripped; |
83 | |
84 | int resultLength = 0; |
85 | stripped.resize(size: len); |
86 | |
87 | const QChar *data = d->m_text.unicode(); |
88 | for (int pos = 0; pos < len; ++pos) { |
89 | if (data[pos] != QLatin1Char('&')) { |
90 | stripped[resultLength++] = data[pos]; |
91 | } else if (pos + 1 < len && data[pos + 1] == QLatin1Char('&')) { |
92 | stripped[resultLength++] = data[pos++]; |
93 | } |
94 | } |
95 | |
96 | stripped.truncate(pos: resultLength); |
97 | |
98 | return stripped; |
99 | } |
100 | |
101 | QIcon KGuiItem::icon() const |
102 | { |
103 | if (d->m_hasIcon) { |
104 | if (!d->m_iconName.isEmpty()) { |
105 | return QIcon::fromTheme(name: d->m_iconName); |
106 | } else { |
107 | return d->m_icon; |
108 | } |
109 | } |
110 | return QIcon(); |
111 | } |
112 | |
113 | QString KGuiItem::iconName() const |
114 | { |
115 | return d->m_iconName; |
116 | } |
117 | |
118 | QString KGuiItem::toolTip() const |
119 | { |
120 | return d->m_toolTip; |
121 | } |
122 | |
123 | QString KGuiItem::whatsThis() const |
124 | { |
125 | return d->m_whatsThis; |
126 | } |
127 | |
128 | bool KGuiItem::isEnabled() const |
129 | { |
130 | return d->m_enabled; |
131 | } |
132 | |
133 | bool KGuiItem::hasIcon() const |
134 | { |
135 | return d->m_hasIcon; |
136 | } |
137 | |
138 | void KGuiItem::setText(const QString &text) |
139 | { |
140 | d->m_text = text; |
141 | } |
142 | |
143 | void KGuiItem::setIcon(const QIcon &icon) |
144 | { |
145 | d->m_icon = icon; |
146 | d->m_iconName.clear(); |
147 | d->m_hasIcon = !icon.isNull(); |
148 | } |
149 | |
150 | void KGuiItem::setIconName(const QString &iconName) |
151 | { |
152 | d->m_iconName = iconName; |
153 | d->m_icon = QIcon(); |
154 | d->m_hasIcon = !iconName.isEmpty(); |
155 | } |
156 | |
157 | void KGuiItem::setToolTip(const QString &toolTip) |
158 | { |
159 | d->m_toolTip = toolTip; |
160 | } |
161 | |
162 | void KGuiItem::setWhatsThis(const QString &whatsThis) |
163 | { |
164 | d->m_whatsThis = whatsThis; |
165 | } |
166 | |
167 | void KGuiItem::setEnabled(bool enabled) |
168 | { |
169 | d->m_enabled = enabled; |
170 | } |
171 | |
172 | void KGuiItem::assign(QPushButton *button, const KGuiItem &item) |
173 | { |
174 | button->setText(item.d->m_text); |
175 | button->setIcon(item.icon()); |
176 | button->setToolTip(item.d->m_toolTip); |
177 | button->setWhatsThis(item.d->m_whatsThis); |
178 | } |
179 |