KIconThemes

kiconbutton.cpp
1 /* vi: ts=8 sts=4 sw=4
2  *
3  * This file is part of the KDE project, module kfile.
4  * Copyright (C) 2000 Geert Jansen <[email protected]>
5  * (C) 2000 Kurt Granroth <[email protected]>
6  * (C) 1997 Christoph Neerfeld <[email protected]>
7  * (C) 2002 Carsten Pfeiffer <[email protected]>
8  *
9  * This is free software; it comes under the GNU Library General
10  * Public License, version 2. See the file "COPYING.LIB" for the
11  * exact licensing terms.
12  */
13 
14 #include "kiconbutton.h"
15 
16 #include <QFileInfo>
17 
18 #include "kicondialog.h"
19 
20 class Q_DECL_HIDDEN KIconButton::KIconButtonPrivate
21 {
22 public:
23  KIconButtonPrivate(KIconButton *qq, KIconLoader *loader);
24  ~KIconButtonPrivate();
25 
26  // slots
27  void _k_slotChangeIcon();
28  void _k_newIconName(const QString &);
29 
30  KIconButton *q;
31 
32  int iconSize;
33  int buttonIconSize;
34  bool m_bStrictIconSize;
35 
36  bool mbUser;
37  KIconLoader::Group mGroup;
38  KIconLoader::Context mContext;
39 
40  QString mIcon;
41  KIconDialog *mpDialog;
42  KIconLoader *mpLoader;
43 };
44 
45 /*
46  * KIconButton: A "choose icon" pushbutton.
47  */
48 
50  : QPushButton(parent), d(new KIconButtonPrivate(this, KIconLoader::global()))
51 {
53 }
54 
56  : QPushButton(parent), d(new KIconButtonPrivate(this, loader))
57 {
59 }
60 
61 KIconButton::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  connect(q, &KIconButton::clicked, q, [this]() {_k_slotChangeIcon();});
75 }
76 
77 KIconButton::KIconButtonPrivate::~KIconButtonPrivate()
78 {
79  delete mpDialog;
80 }
81 
83 {
84  delete d;
85 }
86 
88 {
89  d->m_bStrictIconSize = b;
90 }
91 
92 bool KIconButton::strictIconSize() const
93 {
94  return d->m_bStrictIconSize;
95 }
96 
98 {
99  if (d->buttonIconSize == -1) {
100  QPushButton::setIconSize(QSize(size, size));
101  }
102 
103  d->iconSize = size;
104 }
105 
106 int KIconButton::iconSize() const
107 {
108  return d->iconSize;
109 }
110 
112 {
113  QPushButton::setIconSize(QSize(size, size));
114  d->buttonIconSize = size;
115 }
116 
118 {
119  return QPushButton::iconSize().height();
120 }
121 
123 {
124  d->mGroup = group;
125  d->mContext = context;
126  d->mbUser = user;
127 }
128 
130 {
131  d->mIcon = icon;
132  setIcon(QIcon::fromTheme(d->mIcon));
133 
134  if (!d->mpDialog) {
135  d->mpDialog = new KIconDialog(d->mpLoader, this);
136  connect(d->mpDialog, &KIconDialog::newIconName, this, [this](const QString &iconName) { d->_k_newIconName(iconName); });
137  }
138 
139  if (d->mbUser) {
140  d->mpDialog->setCustomLocation(QFileInfo(d->mpLoader->iconPath(d->mIcon, d->mGroup, true)).absolutePath());
141  }
142 }
143 
144 void KIconButton::setIcon(const QIcon &icon)
145 {
146  QPushButton::setIcon(icon);
147 }
148 
150 {
151  d->mIcon.clear();
152  setIcon(QIcon());
153 }
154 
155 const QString &KIconButton::icon() const
156 {
157  return d->mIcon;
158 }
159 
160 void KIconButton::KIconButtonPrivate::_k_slotChangeIcon()
161 {
162  if (!mpDialog) {
163  mpDialog = new KIconDialog(mpLoader, q);
164  connect(mpDialog, &KIconDialog::newIconName, q, [this](const QString &iconName) { _k_newIconName(iconName); });
165  }
166 
167  mpDialog->setup(mGroup, mContext, m_bStrictIconSize, iconSize, mbUser);
168  mpDialog->showDialog();
169 }
170 
171 void KIconButton::KIconButtonPrivate::_k_newIconName(const QString &name)
172 {
173  if (name.isEmpty()) {
174  return;
175  }
176 
177  q->setIcon(QIcon::fromTheme(name));
178  mIcon = name;
179 
180  if (mbUser) {
181  mpDialog->setCustomLocation(QFileInfo(mpLoader->iconPath(mIcon, mGroup, true)).absolutePath());
182  }
183 
184  emit q->iconChanged(name);
185 }
186 
187 #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:33
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:97
void setIcon(const QIcon &icon)
Desktop icons.
Definition: kiconloader.h:133
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:87
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.
Definition: kiconbutton.cpp:82
An icon that represents an application.
Definition: kiconloader.h:89
const QString & icon() const
Returns the name of the selected icon.
Context
Defines the context of the icon.
Definition: kiconloader.h:86
Group
The group of the icon.
Definition: kiconloader.h:129
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:49
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QObject * parent() const const
Iconloader for KDE.
Definition: kiconloader.h:78
void resetIcon()
Resets the icon (reverts to an empty button).
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Sun Jul 12 2020 22:41:33 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.