KWidgetsAddons

kguiitem.cpp
1 /*
2  This file is part of the KDE libraries
3  SPDX-FileCopyrightText: 2001 Holger Freyther <[email protected]>
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 
40  : d(new KGuiItemPrivate)
41 {
42 }
43 
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 
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 
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 
133 bool KGuiItem::hasIcon() const
134 {
135  return d->m_hasIcon;
136 }
137 
139 {
140  d->m_text = text;
141 }
142 
144 {
145  d->m_icon = icon;
146  d->m_iconName.clear();
147  d->m_hasIcon = !icon.isNull();
148 }
149 
151 {
152  d->m_iconName = iconName;
153  d->m_icon = QIcon();
154  d->m_hasIcon = !iconName.isEmpty();
155 }
156 
158 {
159  d->m_toolTip = toolTip;
160 }
161 
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 }
QString whatsThis() const
Returns the WhatThis text used for this GUI item.
Definition: kguiitem.cpp:123
void truncate(int position)
bool hasIcon() const
Returns true if this GUI item has an icon set for it and false otherwise.
Definition: kguiitem.cpp:133
QString toolTip() const
Returns the tooltip used for this GUI item.
Definition: kguiitem.cpp:118
KGuiItem()
Constructs an empty KGuiItem.
Definition: kguiitem.cpp:39
void setIcon(const QIcon &icon)
void setText(const QString &text)
Sets the text to use for this GUI item.
Definition: kguiitem.cpp:138
~KGuiItem()
Destructor.
void resize(int size)
void setToolTip(const QString &tooltip)
Sets the tooltip text.
Definition: kguiitem.cpp:157
An abstract class for setting the text, icon, tooltip and WhatsThis data on a GUI item (e...
Definition: kguiitem.h:33
bool isEmpty() const const
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
QString plainText() const
Returns the text used by this GUI item after stripping all existing &#39;&&#39; characters which denote keybo...
Definition: kguiitem.cpp:73
void setIcon(const QIcon &iconset)
Sets the icon to be shown next to the text of this GUI item.
Definition: kguiitem.cpp:143
void setEnabled(bool enable)
Toggles the enabled property of this GUI item.
Definition: kguiitem.cpp:167
KGuiItem & operator=(const KGuiItem &other)
Assigns other to this KGuiItem object and returns a reference to this object.
void setWhatsThis(const QString &)
QString iconName() const
Returns the name of the icon used by this GUI item.
Definition: kguiitem.cpp:113
bool isNull() const const
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 setWhatsThis(const QString &whatsThis)
Sets the WhatThis text.
Definition: kguiitem.cpp:162
void setText(const QString &text)
QIcon icon() const
Returns the icon used by this GUI item.
Definition: kguiitem.cpp:101
QIcon fromTheme(const QString &name)
void setToolTip(const QString &)
QString text() const
Returns the text used by this GUI item; this may contain &#39;&&#39; characters which denote a keyboard accel...
Definition: kguiitem.cpp:68
bool isEnabled() const
Returns true if this GUI item is enabled and false otherwise.
Definition: kguiitem.cpp:128
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Tue May 11 2021 22:44:00 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.