KWidgetsAddons

kguiitem.cpp
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
16class KGuiItemPrivate : public QSharedData
17{
18public:
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
40 : d(new KGuiItemPrivate)
41{
42}
43
44KGuiItem::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;
51}
52
53KGuiItem::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;
60}
61
62KGuiItem::KGuiItem(const KGuiItem &rhs) = default;
63
64KGuiItem &KGuiItem::operator=(const KGuiItem &rhs) = default;
65
66KGuiItem::~KGuiItem() = default;
67
69{
70 return d->m_text;
71}
72
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(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(resultLength);
97
98 return stripped;
99}
100
102{
103 if (d->m_hasIcon) {
104 if (!d->m_iconName.isEmpty()) {
105 return QIcon::fromTheme(d->m_iconName);
106 } else {
107 return d->m_icon;
108 }
109 }
110 return QIcon();
111}
112
114{
115 return d->m_iconName;
116}
117
119{
120 return d->m_toolTip;
121}
122
124{
125 return d->m_whatsThis;
126}
127
129{
130 return d->m_enabled;
131}
132
134{
135 return d->m_hasIcon;
136}
137
138void KGuiItem::setText(const QString &text)
139{
140 d->m_text = text;
141}
142
143void KGuiItem::setIcon(const QIcon &icon)
144{
145 d->m_icon = icon;
146 d->m_iconName.clear();
147 d->m_hasIcon = !icon.isNull();
148}
149
150void KGuiItem::setIconName(const QString &iconName)
151{
152 d->m_iconName = iconName;
153 d->m_icon = QIcon();
154 d->m_hasIcon = !iconName.isEmpty();
155}
156
157void KGuiItem::setToolTip(const QString &toolTip)
158{
159 d->m_toolTip = toolTip;
160}
161
162void KGuiItem::setWhatsThis(const QString &whatsThis)
163{
164 d->m_whatsThis = whatsThis;
165}
166
167void KGuiItem::setEnabled(bool enabled)
168{
169 d->m_enabled = enabled;
170}
171
172void 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}
An abstract class for setting the text, icon, tooltip and WhatsThis data on a GUI item (e....
Definition kguiitem.h:34
void setIcon(const QIcon &iconset)
Sets the icon to be shown next to the text of this GUI item.
Definition kguiitem.cpp:143
static void assign(QPushButton *button, const KGuiItem &item)
A static method that can be used to set the text, icon, tooltip and WhatThis properties from item on ...
Definition kguiitem.cpp:172
bool hasIcon() const
Returns true if this GUI item has an icon set for it and false otherwise.
Definition kguiitem.cpp:133
QIcon icon() const
Returns the icon used by this GUI item.
Definition kguiitem.cpp:101
KGuiItem & operator=(const KGuiItem &other)
Assigns other to this KGuiItem object and returns a reference to this object.
QString toolTip() const
Returns the tooltip used for this GUI item.
Definition kguiitem.cpp:118
QString whatsThis() const
Returns the WhatThis text used for this GUI item.
Definition kguiitem.cpp:123
QString text() const
Returns the text used by this GUI item.
Definition kguiitem.cpp:68
void setWhatsThis(const QString &whatsThis)
Sets the WhatThis text.
Definition kguiitem.cpp:162
bool isEnabled() const
Returns true if this GUI item is enabled and false otherwise.
Definition kguiitem.cpp:128
KGuiItem()
Constructs an empty KGuiItem.
Definition kguiitem.cpp:39
void setToolTip(const QString &tooltip)
Sets the tooltip text.
Definition kguiitem.cpp:157
QString iconName() const
Returns the name of the icon used by this GUI item.
Definition kguiitem.cpp:113
void setText(const QString &text)
Sets the text to use for this GUI item.
Definition kguiitem.cpp:138
void setEnabled(bool enable)
Toggles the enabled property of this GUI item.
Definition kguiitem.cpp:167
QString plainText() const
Returns the text used by this GUI item after stripping all existing '&' characters which denote keybo...
Definition kguiitem.cpp:73
~KGuiItem()
Destructor.
void setIconName(const QString &iconName)
Sets the name of the icon that will be shown next to the text of this GUI item.
Definition kguiitem.cpp:150
void setIcon(const QIcon &icon)
void setText(const QString &text)
QIcon fromTheme(const QString &name)
bool isNull() const const
bool isEmpty() const const
void resize(qsizetype newSize, QChar fillChar)
void truncate(qsizetype position)
void setToolTip(const QString &)
void setWhatsThis(const QString &)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:14:43 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.