File indexing completed on 2024-06-23 05:28:18

0001 /*
0002  * Button for selecting an image.
0003  *
0004  * Copyright (C) 2011  Martin Klapetek <martin.klapetek@gmail.com>
0005  * Copyright (C) 2011, 2012 David Edmundson <kde@davidedmundson.co.uk>
0006  *
0007  * This library is free software; you can redistribute it and/or
0008  * modify it under the terms of the GNU Lesser General Public
0009  * License as published by the Free Software Foundation; either
0010  * version 2.1 of the License, or (at your option) any later version.
0011  *
0012  * This library is distributed in the hope that it will be useful,
0013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0015  * Lesser General Public License for more details.
0016  *
0017  * You should have received a copy of the GNU Lesser General Public
0018  * License along with this library; if not, write to the Free Software
0019  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
0020  */
0021 
0022 #include "selectimagebutton.h"
0023 
0024 #include <QtGui/QWidgetAction>
0025 
0026 #include <KFileDialog>
0027 #include <KMenu>
0028 #include <KLocalizedString>
0029 #include <KMessageBox>
0030 
0031 SelectImageButton::SelectImageButton(QWidget *parent)
0032     : QToolButton(parent)
0033 {
0034     KMenu *menu = new KMenu(this);
0035 
0036     setPopupMode(QToolButton::InstantPopup);
0037 
0038     setIconSize(QSize(64,64));
0039 
0040     menu->addAction(KIcon(QLatin1String("document-open-folder")), i18n("Load from file..."), this, SLOT(onLoadImageFromFile()));
0041     menu->addAction(KIcon(QLatin1String("edit-clear")), i18n("Clear Image"), this, SLOT(onClearImage()));
0042     setMenu(menu);
0043 
0044     onClearImage();
0045 }
0046 
0047 SelectImageButton::~SelectImageButton()
0048 {
0049 
0050 }
0051 
0052 void SelectImageButton::setImagePath(const QString &imagePath) {
0053     m_imagePath = imagePath;
0054 
0055     QPixmap image(imagePath);
0056     if (! image.isNull()) {
0057         KIcon imageIcon;
0058         //scale oversized avatars to fit, but don't stretch smaller images
0059         imageIcon.addPixmap(image.scaled(iconSize().boundedTo(image.size()), Qt::KeepAspectRatio));
0060         setIcon(imageIcon);
0061     } else {
0062         setIcon(KIcon(QLatin1String("image-x-generic")));
0063     }
0064     Q_EMIT imagePathChanged(m_imagePath);
0065 }
0066 
0067 QString SelectImageButton::imagePath() const {
0068     return m_imagePath;
0069 }
0070 
0071 
0072 void SelectImageButton::onLoadImageFromFile()
0073 {
0074     KUrl fileUrl = KFileDialog::getImageOpenUrl(KUrl(), this,
0075                                                 i18n("Select image"));
0076 
0077     if (!fileUrl.isEmpty()) {
0078         setImagePath(fileUrl.path());
0079     } else {
0080         return;
0081     }
0082 }
0083 
0084 void SelectImageButton::onClearImage()
0085 {
0086     setImagePath(QString());
0087 }