File indexing completed on 2024-05-12 16:34:29

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 "MorphologyEffectConfigWidget.h"
0021 #include "MorphologyEffect.h"
0022 #include "KoFilterEffect.h"
0023 
0024 #include <QSpinBox>
0025 #include <klocalizedstring.h>
0026 
0027 #include <QGridLayout>
0028 #include <QLabel>
0029 #include <QRadioButton>
0030 #include <QButtonGroup>
0031 
0032 MorphologyEffectConfigWidget::MorphologyEffectConfigWidget(QWidget *parent)
0033         : KoFilterEffectConfigWidgetBase(parent), m_effect(0)
0034 {
0035     QGridLayout * g = new QGridLayout(this);
0036 
0037     m_operator = new QButtonGroup(this);
0038     QRadioButton * erode = new QRadioButton(i18n("Erode"), this);
0039     QRadioButton * dilate = new QRadioButton(i18n("Dilate"), this);
0040     m_operator->addButton(erode, MorphologyEffect::Erode);
0041     m_operator->addButton(dilate, MorphologyEffect::Dilate);
0042     g->addWidget(new QLabel(i18n("Operator:"), this), 0, 0);
0043     g->addWidget(erode, 0, 1);
0044     g->addWidget(dilate, 0, 2);
0045 
0046     g->addWidget(new QLabel(i18n("Radius x:"), this), 1, 0);
0047     m_radiusX = new QDoubleSpinBox(this);
0048     m_radiusX->setRange(0.0, 100);
0049     m_radiusX->setSingleStep(0.5);
0050     g->addWidget(m_radiusX, 1, 1, 1, 2);
0051 
0052     g->addWidget(new QLabel(i18n("Radius y:"), this), 2, 0);
0053     m_radiusY = new QDoubleSpinBox(this);
0054     m_radiusY->setRange(0.0, 100);
0055     m_radiusY->setSingleStep(0.5);
0056     g->addWidget(m_radiusY, 2, 1, 1, 2);
0057 
0058     setLayout(g);
0059 
0060     connect(m_operator, SIGNAL(buttonClicked(int)), this, SLOT(operatorChanged(int)));
0061     connect(m_radiusX, SIGNAL(valueChanged(double)), this, SLOT(radiusXChanged(double)));
0062     connect(m_radiusY, SIGNAL(valueChanged(double)), this, SLOT(radiusYChanged(double)));
0063 }
0064 
0065 bool MorphologyEffectConfigWidget::editFilterEffect(KoFilterEffect * filterEffect)
0066 {
0067     m_effect = dynamic_cast<MorphologyEffect*>(filterEffect);
0068     if (!m_effect)
0069         return false;
0070 
0071     m_operator->blockSignals(true);
0072     m_operator->button(m_effect->morphologyOperator())->setChecked(true);
0073     m_operator->blockSignals(false);
0074     m_radiusX->blockSignals(true);
0075     m_radiusX->setValue(m_effect->morphologyRadius().x()*100);
0076     m_radiusX->blockSignals(false);
0077     m_radiusY->blockSignals(true);
0078     m_radiusY->setValue(m_effect->morphologyRadius().y()*100);
0079     m_radiusY->blockSignals(false);
0080 
0081     return true;
0082 }
0083 
0084 void MorphologyEffectConfigWidget::operatorChanged(int id)
0085 {
0086     if (!m_effect)
0087         return;
0088 
0089     switch(id) {
0090         case MorphologyEffect::Erode:
0091             m_effect->setMorphologyOperator(MorphologyEffect::Erode);
0092             break;
0093         case MorphologyEffect::Dilate:
0094             m_effect->setMorphologyOperator(MorphologyEffect::Dilate);
0095             break;
0096     }
0097     emit filterChanged();
0098 }
0099 
0100 void MorphologyEffectConfigWidget::radiusXChanged(double x)
0101 {
0102     if (!m_effect)
0103         return;
0104 
0105     QPointF radius = m_effect->morphologyRadius();
0106     if (radius.x() != x)
0107         m_effect->setMorphologyRadius(QPointF(x*0.01, radius.y()));
0108 
0109     emit filterChanged();
0110 }
0111 
0112 void MorphologyEffectConfigWidget::radiusYChanged(double y)
0113 {
0114     if (!m_effect)
0115         return;
0116 
0117     QPointF radius = m_effect->morphologyRadius();
0118     if (radius.y() != y)
0119         m_effect->setMorphologyRadius(QPointF(radius.x(), y*0.01));
0120 
0121     emit filterChanged();
0122 }