KIconThemes

kiconbutton.cpp
1/*
2
3 This file is part of the KDE project, module kfile.
4 SPDX-FileCopyrightText: 2000 Geert Jansen <jansen@kde.org>
5 SPDX-FileCopyrightText: 2000 Kurt Granroth <granroth@kde.org>
6 SPDX-FileCopyrightText: 1997 Christoph Neerfeld <chris@kde.org>
7 SPDX-FileCopyrightText: 2002 Carsten Pfeiffer <pfeiffer@kde.org>
8
9 SPDX-License-Identifier: LGPL-2.0-only
10*/
11
12#include "kiconbutton.h"
13
14#include <QFileInfo>
15
16#include <KLocalizedString>
17
18#include "kicondialog.h"
19
20class KIconButtonPrivate
21{
22public:
23 KIconButtonPrivate(KIconButton *qq, KIconLoader *loader);
24 ~KIconButtonPrivate();
25
26 KIconDialog *dialog();
27
28 // slots
29 void _k_slotChangeIcon();
30 void _k_newIconName(const QString &);
31
32 KIconButton *q;
33
34 int iconSize;
35 int buttonIconSize;
36 bool m_bStrictIconSize;
37
38 bool mbUser;
39 KIconLoader::Group mGroup;
40 KIconLoader::Context mContext;
41
42 QString mIcon;
43 KIconDialog *mpDialog;
44 KIconLoader *mpLoader;
45};
46
47/*
48 * KIconButton: A "choose icon" pushbutton.
49 */
50
52 : QPushButton(parent)
53 , d(new KIconButtonPrivate(this, KIconLoader::global()))
54{
56}
57
58KIconButtonPrivate::KIconButtonPrivate(KIconButton *qq, KIconLoader *loader)
59 : q(qq)
60{
61 m_bStrictIconSize = false;
62 iconSize = 0; // let KIconLoader choose the default
63 buttonIconSize = -1; // When buttonIconSize is -1, iconSize will be used for the button
64
65 mGroup = KIconLoader::Desktop;
66 mContext = KIconLoader::Application;
67 mbUser = false;
68
69 mpLoader = loader;
70 mpDialog = nullptr;
71 QObject::connect(q, &KIconButton::clicked, q, [this]() {
72 _k_slotChangeIcon();
73 });
74
75 q->setToolTip(i18nc("@info:tooltip", "Select Icon…"));
76}
77
78KIconButtonPrivate::~KIconButtonPrivate()
79{
80 delete mpDialog;
81}
82
83KIconDialog *KIconButtonPrivate::dialog()
84{
85 if (!mpDialog) {
86 mpDialog = new KIconDialog(q);
87 QObject::connect(mpDialog, &KIconDialog::newIconName, q, [this](const QString &iconName) {
88 _k_newIconName(iconName);
89 });
90 }
91
92 return mpDialog;
93}
94
96
98{
99 d->m_bStrictIconSize = b;
100}
101
102bool KIconButton::strictIconSize() const
103{
104 return d->m_bStrictIconSize;
105}
106
108{
109 if (d->buttonIconSize == -1) {
111 }
112
113 d->iconSize = size;
114}
115
116int KIconButton::iconSize() const
117{
118 return d->iconSize;
119}
120
122{
124 d->buttonIconSize = size;
125}
126
128{
129 return QPushButton::iconSize().height();
130}
131
133{
134 d->mGroup = group;
135 d->mContext = context;
136 d->mbUser = user;
137}
138
140{
141 d->mIcon = icon;
142 setIcon(QIcon::fromTheme(d->mIcon));
143
144 if (d->mbUser) {
145 d->dialog()->setCustomLocation(QFileInfo(d->mpLoader->iconPath(d->mIcon, d->mGroup, true)).absolutePath());
146 }
147}
148
149void KIconButton::setIcon(const QIcon &icon)
150{
152}
153
155{
156 d->mIcon.clear();
157 setIcon(QIcon());
158}
159
160const QString &KIconButton::icon() const
161{
162 return d->mIcon;
163}
164
165void KIconButtonPrivate::_k_slotChangeIcon()
166{
167 dialog()->setup(mGroup, mContext, m_bStrictIconSize, iconSize, mbUser);
168 dialog()->setSelectedIcon(mIcon);
169 dialog()->showDialog();
170}
171
172void KIconButtonPrivate::_k_newIconName(const QString &name)
173{
174 if (name.isEmpty()) {
175 return;
176 }
177
178 q->setIcon(QIcon::fromTheme(name));
179 mIcon = name;
180
181 if (mbUser) {
182 mpDialog->setCustomLocation(QFileInfo(mpLoader->iconPath(mIcon, mGroup, true)).absolutePath());
183 }
184
185 Q_EMIT q->iconChanged(name);
186}
187
188#include "moc_kiconbutton.cpp"
A pushbutton for choosing an icon.
Definition kiconbutton.h:33
void setIcon(const QString &icon)
Sets the button's initial icon.
void setStrictIconSize(bool b)
Sets a strict icon size policy for allowed icons.
void setIconType(KIconLoader::Group group, KIconLoader::Context context, bool user=false)
Sets the icon group and context.
int buttonIconSize() const
Returns the button's icon size.
KIconButton(QWidget *parent=nullptr)
Constructs a KIconButton using the global icon loader.
~KIconButton() override
Destructs the button.
void setButtonIconSize(int size)
Sets the size of the icon to be shown on the button.
void iconChanged(const QString &icon)
Emitted when the icon has changed.
void resetIcon()
Resets the icon (reverts to an empty button).
void setIconSize(int size)
Sets the size of the icon to be shown / selected.
Dialog for interactive selection of icons.
Definition kicondialog.h:33
void setup(KIconLoader::Group group, KIconLoader::Context context=KIconLoader::Application, bool strictIconSize=false, int iconSize=0, bool user=false, bool lockUser=false, bool lockCustomDir=false)
Allows you to set the same parameters as in the class method getIcon(), as well as two additional par...
void setSelectedIcon(const QString &iconName)
Sets the icon that is initially selected in the dialog.
void showDialog()
show()s this dialog and emits a newIconName(const QString&) signal when successful.
void setCustomLocation(const QString &location)
Sets the location of the custom icon directory.
Iconloader for KDE.
Definition kiconloader.h:73
Group
The group of the icon.
@ Desktop
Desktop icons.
Context
Defines the context of the icon.
Definition kiconloader.h:80
@ Application
An icon that represents an application.
Definition kiconloader.h:83
QString iconPath(const QString &name, int group_or_size, bool canReturnNull=false) const
Returns the path of an icon.
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QString name(StandardAction id)
void clicked(bool checked)
QButtonGroup * group() const const
void setIcon(const QIcon &icon)
void setIconSize(const QSize &size)
QString absolutePath() const const
QIcon fromTheme(const QString &name)
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
bool isEmpty() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:08:02 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.