File indexing completed on 2024-09-08 12:18:03
0001 /* vi: ts=8 sts=4 sw=4 0002 0003 This file is part of the KDE project, module kfile. 0004 SPDX-FileCopyrightText: 2000 Geert Jansen <jansen@kde.org> 0005 SPDX-FileCopyrightText: 2000 Kurt Granroth <granroth@kde.org> 0006 SPDX-FileCopyrightText: 1997 Christoph Neerfeld <chris@kde.org> 0007 SPDX-FileCopyrightText: 2002 Carsten Pfeiffer <pfeiffer@kde.org> 0008 0009 SPDX-License-Identifier: LGPL-2.0-only 0010 */ 0011 0012 #include "kiconbutton.h" 0013 0014 #include <QFileInfo> 0015 0016 #include <KLocalizedString> 0017 0018 #include "kicondialog.h" 0019 0020 class KIconButtonPrivate 0021 { 0022 public: 0023 KIconButtonPrivate(KIconButton *qq, KIconLoader *loader); 0024 ~KIconButtonPrivate(); 0025 0026 KIconDialog *dialog(); 0027 0028 // slots 0029 void _k_slotChangeIcon(); 0030 void _k_newIconName(const QString &); 0031 0032 KIconButton *q; 0033 0034 int iconSize; 0035 int buttonIconSize; 0036 bool m_bStrictIconSize; 0037 0038 bool mbUser; 0039 KIconLoader::Group mGroup; 0040 KIconLoader::Context mContext; 0041 0042 QString mIcon; 0043 KIconDialog *mpDialog; 0044 KIconLoader *mpLoader; 0045 }; 0046 0047 /* 0048 * KIconButton: A "choose icon" pushbutton. 0049 */ 0050 0051 KIconButton::KIconButton(QWidget *parent) 0052 : QPushButton(parent) 0053 , d(new KIconButtonPrivate(this, KIconLoader::global())) 0054 { 0055 QPushButton::setIconSize(QSize(48, 48)); 0056 } 0057 0058 #if KICONTHEMES_BUILD_DEPRECATED_SINCE(5, 104) 0059 KIconButton::KIconButton(KIconLoader *loader, QWidget *parent) 0060 : QPushButton(parent) 0061 , d(new KIconButtonPrivate(this, loader)) 0062 { 0063 QPushButton::setIconSize(QSize(48, 48)); 0064 } 0065 #endif 0066 0067 KIconButtonPrivate::KIconButtonPrivate(KIconButton *qq, KIconLoader *loader) 0068 : q(qq) 0069 { 0070 m_bStrictIconSize = false; 0071 iconSize = 0; // let KIconLoader choose the default 0072 buttonIconSize = -1; // When buttonIconSize is -1, iconSize will be used for the button 0073 0074 mGroup = KIconLoader::Desktop; 0075 mContext = KIconLoader::Application; 0076 mbUser = false; 0077 0078 mpLoader = loader; 0079 mpDialog = nullptr; 0080 QObject::connect(q, &KIconButton::clicked, q, [this]() { 0081 _k_slotChangeIcon(); 0082 }); 0083 0084 q->setToolTip(i18nc("@info:tooltip", "Select Icon…")); 0085 } 0086 0087 KIconButtonPrivate::~KIconButtonPrivate() 0088 { 0089 delete mpDialog; 0090 } 0091 0092 KIconDialog *KIconButtonPrivate::dialog() 0093 { 0094 if (!mpDialog) { 0095 #if KICONTHEMES_BUILD_DEPRECATED_SINCE(5, 104) 0096 mpDialog = new KIconDialog(mpLoader, q); 0097 #else 0098 mpDialog = new KIconDialog(q); 0099 #endif 0100 QObject::connect(mpDialog, &KIconDialog::newIconName, q, [this](const QString &iconName) { 0101 _k_newIconName(iconName); 0102 }); 0103 } 0104 0105 return mpDialog; 0106 } 0107 0108 KIconButton::~KIconButton() = default; 0109 0110 void KIconButton::setStrictIconSize(bool b) 0111 { 0112 d->m_bStrictIconSize = b; 0113 } 0114 0115 bool KIconButton::strictIconSize() const 0116 { 0117 return d->m_bStrictIconSize; 0118 } 0119 0120 void KIconButton::setIconSize(int size) 0121 { 0122 if (d->buttonIconSize == -1) { 0123 QPushButton::setIconSize(QSize(size, size)); 0124 } 0125 0126 d->iconSize = size; 0127 } 0128 0129 int KIconButton::iconSize() const 0130 { 0131 return d->iconSize; 0132 } 0133 0134 void KIconButton::setButtonIconSize(int size) 0135 { 0136 QPushButton::setIconSize(QSize(size, size)); 0137 d->buttonIconSize = size; 0138 } 0139 0140 int KIconButton::buttonIconSize() const 0141 { 0142 return QPushButton::iconSize().height(); 0143 } 0144 0145 void KIconButton::setIconType(KIconLoader::Group group, KIconLoader::Context context, bool user) 0146 { 0147 d->mGroup = group; 0148 d->mContext = context; 0149 d->mbUser = user; 0150 } 0151 0152 void KIconButton::setIcon(const QString &icon) 0153 { 0154 d->mIcon = icon; 0155 setIcon(QIcon::fromTheme(d->mIcon)); 0156 0157 if (d->mbUser) { 0158 d->dialog()->setCustomLocation(QFileInfo(d->mpLoader->iconPath(d->mIcon, d->mGroup, true)).absolutePath()); 0159 } 0160 } 0161 0162 void KIconButton::setIcon(const QIcon &icon) 0163 { 0164 QPushButton::setIcon(icon); 0165 } 0166 0167 void KIconButton::resetIcon() 0168 { 0169 d->mIcon.clear(); 0170 setIcon(QIcon()); 0171 } 0172 0173 const QString &KIconButton::icon() const 0174 { 0175 return d->mIcon; 0176 } 0177 0178 void KIconButtonPrivate::_k_slotChangeIcon() 0179 { 0180 dialog()->setup(mGroup, mContext, m_bStrictIconSize, iconSize, mbUser); 0181 dialog()->setSelectedIcon(mIcon); 0182 dialog()->showDialog(); 0183 } 0184 0185 void KIconButtonPrivate::_k_newIconName(const QString &name) 0186 { 0187 if (name.isEmpty()) { 0188 return; 0189 } 0190 0191 q->setIcon(QIcon::fromTheme(name)); 0192 mIcon = name; 0193 0194 if (mbUser) { 0195 mpDialog->setCustomLocation(QFileInfo(mpLoader->iconPath(mIcon, mGroup, true)).absolutePath()); 0196 } 0197 0198 Q_EMIT q->iconChanged(name); 0199 } 0200 0201 #include "moc_kiconbutton.cpp"