KIconThemes

kiconbutton.cpp
1 /* vi: ts=8 sts=4 sw=4
2 
3  This file is part of the KDE project, module kfile.
4  SPDX-FileCopyrightText: 2000 Geert Jansen <[email protected]>
5  SPDX-FileCopyrightText: 2000 Kurt Granroth <[email protected]>
6  SPDX-FileCopyrightText: 1997 Christoph Neerfeld <[email protected]>
7  SPDX-FileCopyrightText: 2002 Carsten Pfeiffer <[email protected]>
8 
9  SPDX-License-Identifier: LGPL-2.0-only
10 */
11 
12 #include "kiconbutton.h"
13 
14 #include <QFileInfo>
15 
16 #include "kicondialog.h"
17 
18 class KIconButtonPrivate
19 {
20 public:
21  KIconButtonPrivate(KIconButton *qq, KIconLoader *loader);
22  ~KIconButtonPrivate();
23 
24  // slots
25  void _k_slotChangeIcon();
26  void _k_newIconName(const QString &);
27 
28  KIconButton *q;
29 
30  int iconSize;
31  int buttonIconSize;
32  bool m_bStrictIconSize;
33 
34  bool mbUser;
35  KIconLoader::Group mGroup;
36  KIconLoader::Context mContext;
37 
38  QString mIcon;
39  KIconDialog *mpDialog;
40  KIconLoader *mpLoader;
41 };
42 
43 /*
44  * KIconButton: A "choose icon" pushbutton.
45  */
46 
48  : QPushButton(parent)
49  , d(new KIconButtonPrivate(this, KIconLoader::global()))
50 {
52 }
53 
55  : QPushButton(parent)
56  , d(new KIconButtonPrivate(this, loader))
57 {
59 }
60 
61 KIconButtonPrivate::KIconButtonPrivate(KIconButton *qq, KIconLoader *loader)
62  : q(qq)
63 {
64  m_bStrictIconSize = false;
65  iconSize = 0; // let KIconLoader choose the default
66  buttonIconSize = -1; // When buttonIconSize is -1, iconSize will be used for the button
67 
68  mGroup = KIconLoader::Desktop;
69  mContext = KIconLoader::Application;
70  mbUser = false;
71 
72  mpLoader = loader;
73  mpDialog = nullptr;
74  QObject::connect(q, &KIconButton::clicked, q, [this]() {
75  _k_slotChangeIcon();
76  });
77 }
78 
79 KIconButtonPrivate::~KIconButtonPrivate()
80 {
81  delete mpDialog;
82 }
83 
84 KIconButton::~KIconButton() = default;
85 
87 {
88  d->m_bStrictIconSize = b;
89 }
90 
91 bool KIconButton::strictIconSize() const
92 {
93  return d->m_bStrictIconSize;
94 }
95 
97 {
98  if (d->buttonIconSize == -1) {
99  QPushButton::setIconSize(QSize(size, size));
100  }
101 
102  d->iconSize = size;
103 }
104 
105 int KIconButton::iconSize() const
106 {
107  return d->iconSize;
108 }
109 
111 {
112  QPushButton::setIconSize(QSize(size, size));
113  d->buttonIconSize = size;
114 }
115 
117 {
118  return QPushButton::iconSize().height();
119 }
120 
122 {
123  d->mGroup = group;
124  d->mContext = context;
125  d->mbUser = user;
126 }
127 
129 {
130  d->mIcon = icon;
131  setIcon(QIcon::fromTheme(d->mIcon));
132 
133  if (!d->mpDialog) {
134  d->mpDialog = new KIconDialog(d->mpLoader, this);
135  connect(d->mpDialog, &KIconDialog::newIconName, this, [this](const QString &iconName) {
136  d->_k_newIconName(iconName);
137  });
138  }
139 
140  if (d->mbUser) {
141  d->mpDialog->setCustomLocation(QFileInfo(d->mpLoader->iconPath(d->mIcon, d->mGroup, true)).absolutePath());
142  }
143 }
144 
145 void KIconButton::setIcon(const QIcon &icon)
146 {
147  QPushButton::setIcon(icon);
148 }
149 
151 {
152  d->mIcon.clear();
153  setIcon(QIcon());
154 }
155 
156 const QString &KIconButton::icon() const
157 {
158  return d->mIcon;
159 }
160 
161 void KIconButtonPrivate::_k_slotChangeIcon()
162 {
163  if (!mpDialog) {
164  mpDialog = new KIconDialog(mpLoader, q);
165  QObject::connect(mpDialog, &KIconDialog::newIconName, q, [this](const QString &iconName) {
166  _k_newIconName(iconName);
167  });
168  }
169 
170  mpDialog->setup(mGroup, mContext, m_bStrictIconSize, iconSize, mbUser);
171  mpDialog->showDialog();
172 }
173 
174 void KIconButtonPrivate::_k_newIconName(const QString &name)
175 {
176  if (name.isEmpty()) {
177  return;
178  }
179 
180  q->setIcon(QIcon::fromTheme(name));
181  mIcon = name;
182 
183  if (mbUser) {
184  mpDialog->setCustomLocation(QFileInfo(mpLoader->iconPath(mIcon, mGroup, true)).absolutePath());
185  }
186 
187  Q_EMIT q->iconChanged(name);
188 }
189 
190 #include "moc_kiconbutton.cpp"
QButtonGroup * group() const const
void setButtonIconSize(int size)
Sets the size of the icon to be shown on the button.
void setIcon(const QString &icon)
Sets the button&#39;s initial icon.
A pushbutton for choosing an icon.
Definition: kiconbutton.h:32
void setIconType(KIconLoader::Group group, KIconLoader::Context context, bool user=false)
Sets the icon group and context.
void setIconSize(int size)
Sets the size of the icon to be shown / selected.
Definition: kiconbutton.cpp:96
void setIcon(const QIcon &icon)
Desktop icons.
Definition: kiconloader.h:129
Dialog for interactive selection of icons.
Definition: kicondialog.h:32
void setStrictIconSize(bool b)
Sets a strict icon size policy for allowed icons.
Definition: kiconbutton.cpp:86
int iconSize() const
Returns the icon size set via setIconSize() or 0, if the default icon size will be used...
int buttonIconSize() const
Returns the button&#39;s icon size.
QSize size() const const
void setIconSize(const QSize &size)
bool isEmpty() const const
void clicked(bool checked)
~KIconButton()
Destructs the button.
An icon that represents an application.
Definition: kiconloader.h:84
const QString & icon() const
Returns the name of the selected icon.
Context
Defines the context of the icon.
Definition: kiconloader.h:81
Group
The group of the icon.
Definition: kiconloader.h:125
int height() const const
QIcon fromTheme(const QString &name)
bool strictIconSize() const
Returns true if a strict icon size policy is set.
KIconButton(QWidget *parent=nullptr)
Constructs a KIconButton using the global icon loader.
Definition: kiconbutton.cpp:47
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QObject * parent() const const
Q_EMITQ_EMIT
Iconloader for KDE.
Definition: kiconloader.h:73
void resetIcon()
Resets the icon (reverts to an empty button).
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Sat Oct 23 2021 22:42:42 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.