File indexing completed on 2025-01-12 04:00:37

0001 
0002 /*
0003    Copyright (c) 2003-2007 Clarence Dang <dang@kde.org>
0004    All rights reserved.
0005 
0006    Redistribution and use in source and binary forms, with or without
0007    modification, are permitted provided that the following conditions
0008    are met:
0009 
0010    1. Redistributions of source code must retain the above copyright
0011       notice, this list of conditions and the following disclaimer.
0012    2. Redistributions in binary form must reproduce the above copyright
0013       notice, this list of conditions and the following disclaimer in the
0014       documentation and/or other materials provided with the distribution.
0015 
0016    THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
0017    IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
0018    OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
0019    IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
0020    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
0021    NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
0022    DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
0023    THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
0024    (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
0025    THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0026 */
0027 
0028 
0029 #define DEBUG_KP_EFFECT_INVERT 0
0030 
0031 
0032 #include "kpEffectInvertWidget.h"
0033 
0034 #include "imagelib/effects/kpEffectInvert.h"
0035 #include "commands/imagelib/effects/kpEffectInvertCommand.h"
0036 #include "pixmapfx/kpPixmapFX.h"
0037 
0038 #include "kpLogCategories.h"
0039 #include <KLocalizedString>
0040 
0041 #include <QCheckBox>
0042 #include <QVBoxLayout>
0043 
0044 kpEffectInvertWidget::kpEffectInvertWidget (bool actOnSelection,
0045                                             QWidget *parent)
0046     : kpEffectWidgetBase (actOnSelection, parent)
0047 {
0048     auto *topLevelLay = new QVBoxLayout (this);
0049     topLevelLay->setContentsMargins(0, 0, 0, 0);
0050 
0051     auto *centerWidget = new QWidget (this);
0052     topLevelLay->addWidget (centerWidget, 0/*stretch*/, Qt::AlignCenter);
0053 
0054     auto *centerWidgetLay = new QVBoxLayout (centerWidget );
0055     centerWidgetLay->setContentsMargins(0, 0, 0, 0);
0056 
0057     m_redCheckBox = new QCheckBox (i18n ("&Red"), centerWidget);
0058     m_greenCheckBox = new QCheckBox (i18n ("&Green"), centerWidget);
0059     m_blueCheckBox = new QCheckBox (i18n ("&Blue"), centerWidget);
0060 
0061     auto *spaceWidget = new QWidget (centerWidget);
0062     spaceWidget->setFixedSize (1, fontMetrics ().height () / 4);
0063 
0064     m_allCheckBox = new QCheckBox (i18n ("&All"), centerWidget);
0065 
0066 
0067     m_redCheckBox->setChecked (false);
0068     m_greenCheckBox->setChecked (false);
0069     m_blueCheckBox->setChecked (false);
0070 
0071     m_allCheckBox->setChecked (false);
0072 
0073 
0074     centerWidgetLay->addWidget (m_redCheckBox);
0075     centerWidgetLay->addWidget (m_greenCheckBox);
0076     centerWidgetLay->addWidget (m_blueCheckBox);
0077 
0078     centerWidgetLay->addWidget (spaceWidget);
0079 
0080     centerWidgetLay->addWidget (m_allCheckBox);
0081 
0082 
0083     m_inSignalHandler = false;
0084     connect (m_redCheckBox, &QCheckBox::toggled,
0085              this, &kpEffectInvertWidget::slotRGBCheckBoxToggled);
0086 
0087     connect (m_greenCheckBox, &QCheckBox::toggled,
0088              this, &kpEffectInvertWidget::slotRGBCheckBoxToggled);
0089 
0090     connect (m_blueCheckBox, &QCheckBox::toggled,
0091              this, &kpEffectInvertWidget::slotRGBCheckBoxToggled);
0092 
0093     connect (m_allCheckBox, &QCheckBox::toggled,
0094              this, &kpEffectInvertWidget::slotAllCheckBoxToggled);
0095 }
0096 
0097 kpEffectInvertWidget::~kpEffectInvertWidget () = default;
0098 
0099 
0100 // public
0101 int kpEffectInvertWidget::channels () const
0102 {
0103 #if DEBUG_KP_EFFECT_INVERT
0104     qCDebug(kpLogWidgets) << "kpEffectInvertWidget::channels()"
0105                << " isChecked: r=" << m_redCheckBox->isChecked ()
0106                << " g=" << m_greenCheckBox->isChecked ()
0107                << " b=" << m_blueCheckBox->isChecked ()
0108                << endl;
0109 #endif
0110 
0111     int channels = 0;
0112 
0113 
0114     if (m_redCheckBox->isChecked ()) {
0115         channels |= kpEffectInvert::Red;
0116     }
0117 
0118     if (m_greenCheckBox->isChecked ()) {
0119         channels |= kpEffectInvert::Green;
0120     }
0121 
0122     if (m_blueCheckBox->isChecked ()) {
0123         channels |= kpEffectInvert::Blue;
0124     }
0125 
0126 
0127 #if DEBUG_KP_EFFECT_INVERT
0128     qCDebug(kpLogWidgets) << "\treturning channels=" << (int *) channels;
0129 #endif
0130     return channels;
0131 }
0132 
0133 
0134 //
0135 // kpEffectInvertWidget implements kpEffectWidgetBase interface
0136 //
0137 
0138 // public virtual [base kpEffectWidgetBase]
0139 QString kpEffectInvertWidget::caption () const
0140 {
0141     return i18n ("Channels");
0142 }
0143 
0144 
0145 // public virtual [base kpEffectWidgetBase]
0146 bool kpEffectInvertWidget::isNoOp () const
0147 {
0148     return (channels () == kpEffectInvert::None);
0149 }
0150 
0151 // public virtual [base kpEffectWidgetBase]
0152 kpImage kpEffectInvertWidget::applyEffect (const kpImage &image)
0153 {
0154     return kpEffectInvert::applyEffect (image, channels ());
0155 }
0156 
0157 
0158 // public virtual [base kpEffectWidgetBase]
0159 kpEffectCommandBase *kpEffectInvertWidget::createCommand (
0160         kpCommandEnvironment *cmdEnviron) const
0161 {
0162     return new kpEffectInvertCommand (channels (),
0163                                       m_actOnSelection,
0164                                       cmdEnviron);
0165 }
0166 
0167 
0168 // protected slots
0169 void kpEffectInvertWidget::slotRGBCheckBoxToggled ()
0170 {
0171     if (m_inSignalHandler) {
0172         return;
0173     }
0174 
0175     m_inSignalHandler = true;
0176 
0177     //blockSignals (true);
0178     m_allCheckBox->setChecked (m_redCheckBox->isChecked () &&
0179                                m_blueCheckBox->isChecked () &&
0180                                m_greenCheckBox->isChecked ());
0181     //blockSignals (false);
0182 
0183     Q_EMIT settingsChanged ();
0184 
0185     m_inSignalHandler = false;
0186 }
0187 
0188 // protected slot
0189 void kpEffectInvertWidget::slotAllCheckBoxToggled ()
0190 {
0191     if (m_inSignalHandler) {
0192         return;
0193     }
0194 
0195     m_inSignalHandler = true;
0196 
0197     //blockSignals (true);
0198     m_redCheckBox->setChecked (m_allCheckBox->isChecked ());
0199     m_greenCheckBox->setChecked (m_allCheckBox->isChecked ());
0200     m_blueCheckBox->setChecked (m_allCheckBox->isChecked ());
0201     //blockSignals (false);
0202 
0203     Q_EMIT settingsChanged ();
0204 
0205     m_inSignalHandler = false;
0206 }
0207 
0208 #include "moc_kpEffectInvertWidget.cpp"