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 <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 | |
20 | class KIconButtonPrivate |
21 | { |
22 | public: |
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 | |
51 | KIconButton::KIconButton(QWidget *parent) |
52 | : QPushButton(parent) |
53 | , d(new KIconButtonPrivate(this, KIconLoader::global())) |
54 | { |
55 | QPushButton::setIconSize(QSize(48, 48)); |
56 | } |
57 | |
58 | KIconButtonPrivate::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(sender: q, signal: &KIconButton::clicked, context: q, slot: [this]() { |
72 | _k_slotChangeIcon(); |
73 | }); |
74 | |
75 | q->setToolTip(i18nc("@info:tooltip" , "Select Icon…" )); |
76 | } |
77 | |
78 | KIconButtonPrivate::~KIconButtonPrivate() |
79 | { |
80 | delete mpDialog; |
81 | } |
82 | |
83 | KIconDialog *KIconButtonPrivate::dialog() |
84 | { |
85 | if (!mpDialog) { |
86 | mpDialog = new KIconDialog(q); |
87 | QObject::connect(sender: mpDialog, signal: &KIconDialog::newIconName, context: q, slot: [this](const QString &iconName) { |
88 | _k_newIconName(iconName); |
89 | }); |
90 | } |
91 | |
92 | return mpDialog; |
93 | } |
94 | |
95 | KIconButton::~KIconButton() = default; |
96 | |
97 | void KIconButton::setStrictIconSize(bool b) |
98 | { |
99 | d->m_bStrictIconSize = b; |
100 | } |
101 | |
102 | bool KIconButton::strictIconSize() const |
103 | { |
104 | return d->m_bStrictIconSize; |
105 | } |
106 | |
107 | void KIconButton::setIconSize(int size) |
108 | { |
109 | if (d->buttonIconSize == -1) { |
110 | QPushButton::setIconSize(QSize(size, size)); |
111 | } |
112 | |
113 | d->iconSize = size; |
114 | } |
115 | |
116 | int KIconButton::iconSize() const |
117 | { |
118 | return d->iconSize; |
119 | } |
120 | |
121 | void KIconButton::setButtonIconSize(int size) |
122 | { |
123 | QPushButton::setIconSize(QSize(size, size)); |
124 | d->buttonIconSize = size; |
125 | } |
126 | |
127 | int KIconButton::buttonIconSize() const |
128 | { |
129 | return QPushButton::iconSize().height(); |
130 | } |
131 | |
132 | void KIconButton::setIconType(KIconLoader::Group group, KIconLoader::Context context, bool user) |
133 | { |
134 | d->mGroup = group; |
135 | d->mContext = context; |
136 | d->mbUser = user; |
137 | } |
138 | |
139 | void KIconButton::setIcon(const QString &icon) |
140 | { |
141 | d->mIcon = icon; |
142 | setIcon(QIcon::fromTheme(name: d->mIcon)); |
143 | |
144 | if (d->mbUser) { |
145 | d->dialog()->setCustomLocation(QFileInfo(d->mpLoader->iconPath(name: d->mIcon, group_or_size: d->mGroup, canReturnNull: true)).absolutePath()); |
146 | } |
147 | } |
148 | |
149 | void KIconButton::setIcon(const QIcon &icon) |
150 | { |
151 | QPushButton::setIcon(icon); |
152 | } |
153 | |
154 | void KIconButton::resetIcon() |
155 | { |
156 | d->mIcon.clear(); |
157 | setIcon(QIcon()); |
158 | } |
159 | |
160 | const QString &KIconButton::icon() const |
161 | { |
162 | return d->mIcon; |
163 | } |
164 | |
165 | void KIconButtonPrivate::_k_slotChangeIcon() |
166 | { |
167 | dialog()->setup(group: mGroup, context: mContext, strictIconSize: m_bStrictIconSize, iconSize, user: mbUser); |
168 | dialog()->setSelectedIcon(mIcon); |
169 | dialog()->showDialog(); |
170 | } |
171 | |
172 | void 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(name: mIcon, group_or_size: mGroup, canReturnNull: true)).absolutePath()); |
183 | } |
184 | |
185 | Q_EMIT q->iconChanged(icon: name); |
186 | } |
187 | |
188 | #include "moc_kiconbutton.cpp" |
189 | |