File indexing completed on 2024-04-14 14:31:57

0001 // Copyright (c) 2002-2004 Rob Kaper <cap@capsi.com>
0002 // Copyright (c) 2015 Pino Toscano <pino@kde.org>
0003 //
0004 // This program is free software; you can redistribute it and/or
0005 // modify it under the terms of the GNU General Public License
0006 // version 2 as published by the Free Software Foundation.
0007 //
0008 // This program is distributed in the hope that it will be useful,
0009 // but WITHOUT ANY WARRANTY; without even the implied warranty of
0010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0011 // General Public License for more details.
0012 //
0013 // You should have received a copy of the GNU General Public License
0014 // along with this program; see the file COPYING.  If not, write to
0015 // the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0016 // Boston, MA 02110-1301, USA.
0017 
0018 #include "customlocationiconbutton.h"
0019 
0020 #include <QFile>
0021 #include <QFileInfo>
0022 #include <QVariant>
0023 
0024 #include <kicondialog.h>
0025 #include <kiconloader.h>
0026 
0027 CustomLocationIconButton::CustomLocationIconButton(QWidget *parent)
0028     : QPushButton(parent)
0029 {
0030     setProperty("kcfg_property", QByteArray("image"));
0031 
0032     connect(this, SIGNAL(clicked()), this, SLOT(slotChooseImage()));
0033 }
0034 
0035 CustomLocationIconButton::~CustomLocationIconButton()
0036 {
0037 }
0038 
0039 void CustomLocationIconButton::setTokenTheme(const TokenTheme &theme)
0040 {
0041     m_tokenTheme = theme;
0042 }
0043 
0044 void CustomLocationIconButton::setImage(const QString &image)
0045 {
0046     if (!checkValid())
0047         return;
0048 
0049     const QPixmap p = m_tokenTheme.tokenPixmap(image);
0050     if (p.isNull())
0051         return;
0052 
0053     setIcon(QIcon(p));
0054     setIconSize(p.size());
0055     m_image = image;
0056 }
0057 
0058 QString CustomLocationIconButton::image() const
0059 {
0060     return m_image;
0061 }
0062 
0063 void CustomLocationIconButton::slotChooseImage()
0064 {
0065     if (!checkValid())
0066         return;
0067 
0068     KIconDialog iconDialog(this);
0069     iconDialog.setCustomLocation(m_tokenTheme.path());
0070     // begin with user icons, lock editing
0071     iconDialog.setup(KIconLoader::Desktop, KIconLoader::Application, false, 0, true, true, true);
0072 
0073     const QString image = iconDialog.openDialog();
0074     if (image.isEmpty())
0075         return;
0076 
0077     m_image = QFileInfo(image).fileName();
0078 
0079     setImage(m_image);
0080     Q_EMIT imageChanged(m_image);
0081 }
0082 
0083 bool CustomLocationIconButton::checkValid() const
0084 {
0085     return m_tokenTheme.isValid();
0086 }
0087 
0088 #include "moc_customlocationiconbutton.cpp"