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

0001 /* This file is part of the KDE project
0002  * Copyright (c) 2009 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 "CompositeEffectConfigWidget.h"
0021 #include "CompositeEffect.h"
0022 #include "KoFilterEffect.h"
0023 
0024 #include <QSpinBox>
0025 #include <kcombobox.h>
0026 #include <klocalizedstring.h>
0027 
0028 #include <QGridLayout>
0029 #include <QLabel>
0030 
0031 CompositeEffectConfigWidget::CompositeEffectConfigWidget(QWidget *parent)
0032         : KoFilterEffectConfigWidgetBase(parent), m_effect(0)
0033 {
0034     QGridLayout * g = new QGridLayout(this);
0035 
0036     g->addWidget(new QLabel(i18n("Operation"), this), 0, 0);
0037 
0038     m_operation = new KComboBox(this);
0039     m_operation->addItem("Over");
0040     m_operation->addItem("In");
0041     m_operation->addItem("Out");
0042     m_operation->addItem("Atop");
0043     m_operation->addItem("Xor");
0044     m_operation->addItem("Arithmetic");
0045     g->addWidget(m_operation, 0, 1);
0046 
0047     m_arithmeticWidget = new QWidget(this);
0048     QGridLayout * arithmeticLayout = new QGridLayout(m_arithmeticWidget);
0049     for (int i = 0; i < 4; ++i) {
0050         m_k[i] = new QDoubleSpinBox(m_arithmeticWidget);
0051         arithmeticLayout->addWidget(new QLabel(QString("k%1").arg(i + 1)), i / 2, (2*i) % 4);
0052         arithmeticLayout->addWidget(m_k[i], i / 2, (2*i + 1) % 4);
0053         connect(m_k[i], SIGNAL(valueChanged(double)), this, SLOT(valueChanged()));
0054     }
0055     m_arithmeticWidget->setContentsMargins(0, 0, 0, 0);
0056     g->addWidget(m_arithmeticWidget, 1, 0, 1, 2);
0057     g->addItem(new QSpacerItem(0, 1, QSizePolicy::Minimum, QSizePolicy::MinimumExpanding), 2, 0);
0058 
0059     connect(m_operation, SIGNAL(currentIndexChanged(int)), this, SLOT(operationChanged(int)));
0060 }
0061 
0062 bool CompositeEffectConfigWidget::editFilterEffect(KoFilterEffect * filterEffect)
0063 {
0064     m_effect = dynamic_cast<CompositeEffect*>(filterEffect);
0065     if (!m_effect)
0066         return false;
0067 
0068     m_operation->blockSignals(true);
0069     m_operation->setCurrentIndex(m_effect->operation());
0070     m_operation->blockSignals(false);
0071 
0072     const qreal * k = m_effect->arithmeticValues();
0073     for (int i = 0; i < 4; ++i) {
0074         m_k[i]->blockSignals(true);
0075         m_k[i]->setValue(k[i]);
0076         m_k[i]->blockSignals(false);
0077     }
0078     m_arithmeticWidget->setVisible(m_effect->operation() == CompositeEffect::Arithmetic);
0079 
0080     return true;
0081 }
0082 
0083 void CompositeEffectConfigWidget::operationChanged(int index)
0084 {
0085     m_arithmeticWidget->setVisible(index == 6);
0086     if (m_effect) {
0087         m_effect->setOperation(static_cast<CompositeEffect::Operation>(index));
0088         emit filterChanged();
0089     }
0090 }
0091 
0092 void CompositeEffectConfigWidget::valueChanged()
0093 {
0094     if (!m_effect)
0095         return;
0096 
0097     qreal k[4] = {0};
0098     for (int i = 0; i < 4; ++i) {
0099         k[i] = m_k[i]->value();
0100     }
0101 
0102     m_effect->setArithmeticValues(k);
0103     emit filterChanged();
0104 }