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

0001 /* This file is part of the KDE project
0002  * Copyright (c) 2009-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 "ColorMatrixEffectConfigWidget.h"
0021 #include "ColorMatrixEffect.h"
0022 #include "KoFilterEffect.h"
0023 #include "MatrixDataModel.h"
0024 
0025 #include <QSpinBox>
0026 #include <kcombobox.h>
0027 #include <klocalizedstring.h>
0028 
0029 #include <QGridLayout>
0030 #include <QLabel>
0031 #include <QStackedWidget>
0032 #include <QTableView>
0033 #include <QHeaderView>
0034 
0035 ColorMatrixEffectConfigWidget::ColorMatrixEffectConfigWidget(QWidget *parent)
0036         : KoFilterEffectConfigWidgetBase(parent), m_effect(0)
0037 {
0038     QGridLayout * g = new QGridLayout(this);
0039 
0040     m_type = new KComboBox(this);
0041     m_type->addItem(i18n("Apply color matrix"));
0042     m_type->addItem(i18n("Saturate colors"));
0043     m_type->addItem(i18n("Rotate hue"));
0044     m_type->addItem(i18n("Luminance to alpha"));
0045     g->addWidget(m_type, 0, 0);
0046 
0047     m_stack = new QStackedWidget(this);
0048     m_stack->setContentsMargins(0, 0, 0, 0);
0049     g->addWidget(m_stack, 1, 0);
0050 
0051     m_matrixModel = new MatrixDataModel(this);
0052 
0053     QTableView * matrixWidget = new QTableView(m_stack);
0054     matrixWidget->setModel(m_matrixModel);
0055     matrixWidget->horizontalHeader()->hide();
0056     matrixWidget->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
0057     matrixWidget->verticalHeader()->hide();
0058     matrixWidget->verticalHeader()->setResizeMode(QHeaderView::ResizeToContents);
0059     m_stack->addWidget(matrixWidget);
0060 
0061     QWidget * saturateWidget = new QWidget(m_stack);
0062     QGridLayout * saturateLayout = new QGridLayout(saturateWidget);
0063     saturateLayout->addWidget(new QLabel(i18n("Saturate value"), saturateWidget), 0, 0);
0064     m_saturate = new QDoubleSpinBox(saturateWidget);
0065     m_saturate->setRange(0.0, 1.0);
0066     m_saturate->setSingleStep(0.05);
0067     saturateLayout->addWidget(m_saturate, 0, 1);
0068     saturateLayout->addItem(new QSpacerItem(0, 1, QSizePolicy::Minimum, QSizePolicy::MinimumExpanding), 1, 0);
0069     saturateWidget->setLayout(saturateLayout);
0070     m_stack->addWidget(saturateWidget);
0071 
0072     QWidget * hueRotateWidget = new QWidget(m_stack);
0073     QGridLayout * hueRotateLayout = new QGridLayout(hueRotateWidget);
0074     hueRotateLayout->addWidget(new QLabel(i18n("Angle"), hueRotateWidget), 0, 0);
0075     m_hueRotate = new QDoubleSpinBox(hueRotateWidget);
0076     m_hueRotate->setRange(0.0, 360.0);
0077     m_hueRotate->setSingleStep(1.0);
0078     hueRotateLayout->addWidget(m_hueRotate, 0, 1);
0079     hueRotateLayout->addItem(new QSpacerItem(0, 1, QSizePolicy::Minimum, QSizePolicy::MinimumExpanding), 1, 0);
0080     hueRotateWidget->setLayout(hueRotateLayout);
0081     m_stack->addWidget(hueRotateWidget);
0082 
0083     QWidget * luminanceWidget = new QWidget(m_stack);
0084     m_stack->addWidget(luminanceWidget);
0085 
0086     setLayout(g);
0087 
0088     connect(m_type, SIGNAL(currentIndexChanged(int)), m_stack, SLOT(setCurrentIndex(int)));
0089     connect(m_type, SIGNAL(currentIndexChanged(int)), this, SLOT(typeChanged(int)));
0090     connect(m_saturate, SIGNAL(valueChanged(double)), this, SLOT(saturateChanged(double)));
0091     connect(m_hueRotate, SIGNAL(valueChanged(double)), this, SLOT(hueRotateChanged(double)));
0092     connect(m_matrixModel, SIGNAL(dataChanged(QModelIndex,QModelIndex)), this, SLOT(matrixChanged()));
0093 }
0094 
0095 bool ColorMatrixEffectConfigWidget::editFilterEffect(KoFilterEffect * filterEffect)
0096 {
0097     m_effect = dynamic_cast<ColorMatrixEffect*>(filterEffect);
0098     if (!m_effect)
0099         return false;
0100 
0101     m_type->blockSignals(true);
0102 
0103     switch (m_effect->type()) {
0104     case ColorMatrixEffect::Matrix:
0105         m_type->setCurrentIndex(0);
0106         m_matrixModel->setMatrix(m_effect->colorMatrix(), m_effect->colorMatrixRowCount(), m_effect->colorMatrixColumnCount());
0107         break;
0108     case ColorMatrixEffect::Saturate:
0109         m_type->setCurrentIndex(1);
0110         m_saturate->blockSignals(true);
0111         m_saturate->setValue(m_effect->saturate());
0112         m_saturate->blockSignals(false);
0113         break;
0114     case ColorMatrixEffect::HueRotate:
0115         m_type->setCurrentIndex(2);
0116         m_hueRotate->blockSignals(true);
0117         m_hueRotate->setValue(m_effect->hueRotate());
0118         m_hueRotate->blockSignals(false);
0119         break;
0120     case ColorMatrixEffect::LuminanceAlpha:
0121         m_type->setCurrentIndex(3);
0122         break;
0123     }
0124 
0125     m_type->blockSignals(false);
0126     m_stack->setCurrentIndex(m_type->currentIndex());
0127 
0128     return true;
0129 }
0130 
0131 void ColorMatrixEffectConfigWidget::matrixChanged()
0132 {
0133     if (!m_effect)
0134         return;
0135 
0136     m_effect->setColorMatrix(m_matrixModel->matrix());
0137     emit filterChanged();
0138 }
0139 
0140 void ColorMatrixEffectConfigWidget::saturateChanged(double saturate)
0141 {
0142     if (!m_effect)
0143         return;
0144 
0145     m_effect->setSaturate(saturate);
0146     emit filterChanged();
0147 }
0148 
0149 void ColorMatrixEffectConfigWidget::hueRotateChanged(double angle)
0150 {
0151     if (!m_effect)
0152         return;
0153 
0154     m_effect->setHueRotate(angle);
0155     emit filterChanged();
0156 }
0157 
0158 void ColorMatrixEffectConfigWidget::typeChanged(int index)
0159 {
0160     if (!m_effect)
0161         return;
0162 
0163     if (index == ColorMatrixEffect::Matrix) {
0164         m_effect->setColorMatrix(m_matrixModel->matrix());
0165     } else if (index == ColorMatrixEffect::Saturate) {
0166         m_effect->setSaturate(m_saturate->value());
0167     } else if (index == ColorMatrixEffect::HueRotate) {
0168         m_effect->setHueRotate(m_hueRotate->value());
0169     } else {
0170         m_effect->setLuminanceAlpha();
0171     }
0172     emit filterChanged();
0173 }