File indexing completed on 2024-05-19 16:07:24

0001 /* This file is part of the KDE project
0002  * Copyright (c) 2010 Jan Hambrecht <jaham@gmx.net>
0003  *
0004  * This library is free software; you can redistribute it and/or
0005  * modify it under the terms of the GNU Lesser General Public
0006  * License as published by the Free Software Foundation; either
0007  * version 2.1 of the License, or (at your option) any later version.
0008  *
0009  * This library is distributed in the hope that it will be useful,
0010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012  * Library General Public License for more details.
0013  *
0014  * You should have received a copy of the GNU Lesser General Public License
0015  * along with this library; see the file COPYING.LIB.  If not, write to
0016  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017  * Boston, MA 02110-1301, USA.
0018  */
0019 
0020 #include "ImageEffectConfigWidget.h"
0021 #include "ImageEffect.h"
0022 
0023 #include "KoFilterEffect.h"
0024 #include <KoFileDialog.h>
0025 
0026 #include <klocalizedstring.h>
0027 
0028 #include <QGridLayout>
0029 #include <QLabel>
0030 #include <QPushButton>
0031 #include <QImageReader>
0032 #include <KConfigGroup>
0033 
0034 ImageEffectConfigWidget::ImageEffectConfigWidget(QWidget *parent)
0035         : KoFilterEffectConfigWidgetBase(parent), m_effect(0)
0036 {
0037     QGridLayout * g = new QGridLayout(this);
0038 
0039     m_image = new QLabel(this);
0040     QPushButton * button = new QPushButton(i18n("Select image..."), this);
0041 
0042     g->addWidget(m_image, 0, 0, Qt::AlignCenter);
0043     g->addWidget(button, 0, 1);
0044 
0045     setLayout(g);
0046 
0047     connect(button, SIGNAL(clicked()), this, SLOT(selectImage()));
0048 }
0049 
0050 bool ImageEffectConfigWidget::editFilterEffect(KoFilterEffect * filterEffect)
0051 {
0052     m_effect = dynamic_cast<ImageEffect*>(filterEffect);
0053     if (!m_effect)
0054         return false;
0055 
0056     m_image->setPixmap(QPixmap::fromImage(m_effect->image().scaledToWidth(80)));
0057 
0058     return true;
0059 }
0060 
0061 void ImageEffectConfigWidget::selectImage()
0062 {
0063     if (!m_effect)
0064         return;
0065 
0066     KoFileDialog dialog(0, KoFileDialog::OpenFile, "OpenDocument");
0067     dialog.setCaption(i18n("Select image"));
0068     dialog.setImageFilters();
0069 
0070     QString fname = dialog.filename();
0071 
0072     if (fname.isEmpty()) return;
0073 
0074     QImage newImage;
0075     if (!newImage.load(fname))
0076         return;
0077 
0078     m_effect->setImage(newImage);
0079     editFilterEffect(m_effect);
0080 
0081     emit filterChanged();
0082 }