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 
15 class Q_DECL_HIDDEN KGuiItem::KGuiItemPrivate
16 {
17 public:
18  KGuiItemPrivate()
19  {
20  m_enabled = true;
21  m_hasIcon = false;
22  }
23 
24  KGuiItemPrivate(const KGuiItemPrivate &rhs)
25  {
26  (*this) = rhs;
27  }
28 
29  KGuiItemPrivate &operator=(const KGuiItemPrivate &rhs)
30  {
31  m_text = rhs.m_text;
32  m_icon = rhs.m_icon;
33  m_iconName = rhs.m_iconName;
34  m_toolTip = rhs.m_toolTip;
35  m_whatsThis = rhs.m_whatsThis;
36  m_statusText = rhs.m_statusText;
37  m_enabled = rhs.m_enabled;
38  m_hasIcon = rhs.m_hasIcon;
39 
40  return *this;
41  }
42 
43  QString m_text;
44  QString m_toolTip;
45  QString m_whatsThis;
46  QString m_statusText;
47  QString m_iconName;
48  QIcon m_icon;
49  bool m_hasIcon : 1;
50  bool m_enabled : 1;
51 };
52 
53 KGuiItem::KGuiItem()
54 {
55  d = new KGuiItemPrivate;
56 }
57 
58 KGuiItem::KGuiItem(const QString &text, const QString &iconName,
59  const QString &toolTip, const QString &whatsThis)
60 {
61  d = new KGuiItemPrivate;
62  d->m_text = text;
63  d->m_toolTip = toolTip;
64  d->m_whatsThis = whatsThis;
65  setIconName(iconName);
66 }
67 
68 KGuiItem::KGuiItem(const QString &text, const QIcon &icon,
69  const QString &toolTip, const QString &whatsThis)
70 {
71  d = new KGuiItemPrivate;
72  d->m_text = text;
73  d->m_toolTip = toolTip;
74  d->m_whatsThis = whatsThis;
75  setIcon(icon);
76 }
77 
78 KGuiItem::KGuiItem(const KGuiItem &rhs)
79  : d(nullptr)
80 {
81  (*this) = rhs;
82 }
83 
84 KGuiItem &KGuiItem::operator=(const KGuiItem &rhs)
85 {
86  if (d == rhs.d) {
87  return *this;
88  }
89 
90  Q_ASSERT(rhs.d);
91 
92  delete d;
93  d = new KGuiItemPrivate(*rhs.d);
94 
95  return *this;
96 }
97 
98 KGuiItem::~KGuiItem()
99 {
100  delete d;
101 }
102 
103 QString KGuiItem::text() const
104 {
105  return d->m_text;
106 }
107 
108 QString KGuiItem::plainText() const
109 {
110  const int len = d->m_text.length();
111 
112  if (len == 0) {
113  return d->m_text;
114  }
115 
116  //Can assume len >= 1 from now on.
117  QString stripped;
118 
119  int resultLength = 0;
120  stripped.resize(len);
121 
122  const QChar *data = d->m_text.unicode();
123  for (int pos = 0; pos < len; ++pos) {
124  if (data[ pos ] != QLatin1Char('&')) {
125  stripped[ resultLength++ ] = data[ pos ];
126  } else if (pos + 1 < len && data[ pos + 1 ] == QLatin1Char('&')) {
127  stripped[ resultLength++ ] = data[ pos++ ];
128  }
129  }
130 
131  stripped.truncate(resultLength);
132 
133  return stripped;
134 }
135 
136 QIcon KGuiItem::icon() const
137 {
138  if (d->m_hasIcon) {
139  if (!d->m_iconName.isEmpty()) {
140  return QIcon::fromTheme(d->m_iconName);
141  } else {
142  return d->m_icon;
143  }
144  }
145  return QIcon();
146 }
147 
148 QString KGuiItem::iconName() const
149 {
150  return d->m_iconName;
151 }
152 
153 QString KGuiItem::toolTip() const
154 {
155  return d->m_toolTip;
156 }
157 
158 QString KGuiItem::whatsThis() const
159 {
160  return d->m_whatsThis;
161 }
162 
163 bool KGuiItem::isEnabled() const
164 {
165  return d->m_enabled;
166 }
167 
168 bool KGuiItem::hasIcon() const
169 {
170  return d->m_hasIcon;
171 }
172 
173 void KGuiItem::setText(const QString &text)
174 {
175  d->m_text = text;
176 }
177 
178 void KGuiItem::setIcon(const QIcon &icon)
179 {
180  d->m_icon = icon;
181  d->m_iconName.clear();
182  d->m_hasIcon = !icon.isNull();
183 }
184 
185 void KGuiItem::setIconName(const QString &iconName)
186 {
187  d->m_iconName = iconName;
188  d->m_icon = QIcon();
189  d->m_hasIcon = !iconName.isEmpty();
190 }
191 
192 void KGuiItem::setToolTip(const QString &toolTip)
193 {
194  d->m_toolTip = toolTip;
195 }
196 
197 void KGuiItem::setWhatsThis(const QString &whatsThis)
198 {
199  d->m_whatsThis = whatsThis;
200 }
201 
202 void KGuiItem::setEnabled(bool enabled)
203 {
204  d->m_enabled = enabled;
205 }
206 
207 void KGuiItem::assign(QPushButton *button, const KGuiItem &item)
208 {
209  button->setText(item.d->m_text);
210  button->setIcon(item.icon());
211  button->setToolTip(item.d->m_toolTip);
212  button->setWhatsThis(item.d->m_whatsThis);
213 }
214 
QString text() const const
void truncate(int position)
QVariant data() const const
QIcon icon() const const
void setIcon(const QIcon &icon)
QString toolTip() const const
void resize(int size)
An abstract class for GUI data such as ToolTip and Icon.
Definition: kguiitem.h:31
bool isEmpty() const const
QString whatsThis() const const
void setWhatsThis(const QString &)
bool isNull() const const
void setText(const QString &text)
QIcon fromTheme(const QString &name)
void setToolTip(const QString &)
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Wed Sep 30 2020 22:52:02 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.