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_REDUCE_COLORS 0
0030 
0031 
0032 #include "kpEffectReduceColorsWidget.h"
0033 
0034 #include "imagelib/effects/kpEffectReduceColors.h"
0035 #include "commands/imagelib/effects/kpEffectReduceColorsCommand.h"
0036 #include "pixmapfx/kpPixmapFX.h"
0037 
0038 #include "kpLogCategories.h"
0039 #include <KLocalizedString>
0040 
0041 #include <QButtonGroup>
0042 #include <QCheckBox>
0043 #include <QImage>
0044 #include <QRadioButton>
0045 #include <QVBoxLayout>
0046 
0047 kpEffectReduceColorsWidget::kpEffectReduceColorsWidget (bool actOnSelection,
0048                                                         QWidget *parent)
0049     : kpEffectWidgetBase (actOnSelection, parent)
0050 {
0051     auto *lay = new QVBoxLayout (this);
0052     lay->setContentsMargins(0, 0, 0, 0);
0053 
0054     m_blackAndWhiteRadioButton =
0055         new QRadioButton (i18n ("&Monochrome"), this);
0056 
0057     m_blackAndWhiteDitheredRadioButton =
0058         new QRadioButton (i18n ("Mo&nochrome (dithered)"), this);
0059 
0060     m_8BitRadioButton = new QRadioButton (i18n ("256 co&lor"), this);
0061 
0062     m_8BitDitheredRadioButton = new QRadioButton (i18n ("256 colo&r (dithered)"), this);
0063 
0064     m_24BitRadioButton = new QRadioButton (i18n ("24-&bit color"), this);
0065 
0066 
0067     // LOCOMPAT: don't think this is needed
0068     auto *buttonGroup = new QButtonGroup (this);
0069     buttonGroup->addButton (m_blackAndWhiteRadioButton);
0070     buttonGroup->addButton (m_blackAndWhiteDitheredRadioButton);
0071     buttonGroup->addButton (m_8BitRadioButton);
0072     buttonGroup->addButton (m_8BitDitheredRadioButton);
0073     buttonGroup->addButton (m_24BitRadioButton);
0074 
0075     m_defaultRadioButton = m_24BitRadioButton;
0076     m_defaultRadioButton->setChecked (true);
0077 
0078     lay->addWidget (m_blackAndWhiteRadioButton);
0079     lay->addWidget (m_blackAndWhiteDitheredRadioButton);
0080     lay->addWidget (m_8BitRadioButton);
0081     lay->addWidget (m_8BitDitheredRadioButton);
0082     lay->addWidget (m_24BitRadioButton);
0083 
0084     connect (m_blackAndWhiteRadioButton, &QRadioButton::toggled,
0085              this, &kpEffectReduceColorsWidget::settingsChanged);
0086 
0087     connect (m_blackAndWhiteDitheredRadioButton, &QRadioButton::toggled,
0088              this, &kpEffectReduceColorsWidget::settingsChanged);
0089 
0090     connect (m_8BitRadioButton, &QRadioButton::toggled,
0091              this, &kpEffectReduceColorsWidget::settingsChanged);
0092 
0093     connect (m_8BitDitheredRadioButton, &QRadioButton::toggled,
0094              this, &kpEffectReduceColorsWidget::settingsChanged);
0095 
0096     connect (m_24BitRadioButton, &QRadioButton::toggled,
0097              this, &kpEffectReduceColorsWidget::settingsChanged);
0098 }
0099 
0100 //---------------------------------------------------------------------
0101 
0102 // public
0103 int kpEffectReduceColorsWidget::depth () const
0104 {
0105     // These values (1, 8, 32) are QImage's supported depths.
0106     // TODO: Qt-4.7.1: 1, 8, 16, 24 and 32
0107     if (m_blackAndWhiteRadioButton->isChecked () ||
0108         m_blackAndWhiteDitheredRadioButton->isChecked ())
0109     {
0110         return 1;
0111     }
0112 
0113     if (m_8BitRadioButton->isChecked () ||
0114              m_8BitDitheredRadioButton->isChecked ())
0115     {
0116         return 8;
0117     }
0118 
0119     if (m_24BitRadioButton->isChecked ())
0120     {
0121         return 32;
0122     }
0123 
0124     return 0;
0125 }
0126 
0127 //---------------------------------------------------------------------
0128 
0129 // public
0130 bool kpEffectReduceColorsWidget::dither () const
0131 {
0132     return (m_blackAndWhiteDitheredRadioButton->isChecked () ||
0133             m_8BitDitheredRadioButton->isChecked ());
0134 }
0135 
0136 //---------------------------------------------------------------------
0137 //
0138 // kpEffectReduceColorsWidget implements kpEffectWidgetBase interface
0139 //
0140 
0141 // public virtual [base kpEffectWidgetBase]
0142 QString kpEffectReduceColorsWidget::caption () const
0143 {
0144     return i18n ("Reduce To");
0145 }
0146 
0147 //---------------------------------------------------------------------
0148 
0149 // public virtual [base kpEffectWidgetBase]
0150 bool kpEffectReduceColorsWidget::isNoOp () const
0151 {
0152     return (!m_defaultRadioButton || m_defaultRadioButton->isChecked ());
0153 }
0154 
0155 //---------------------------------------------------------------------
0156 
0157 // public virtual [base kpEffectWidgetBase]
0158 kpImage kpEffectReduceColorsWidget::applyEffect (const kpImage &image)
0159 {
0160     return kpEffectReduceColors::applyEffect (image, depth (), dither ());
0161 }
0162 
0163 //---------------------------------------------------------------------
0164 
0165 // public virtual [base kpEffectWidgetBase]
0166 kpEffectCommandBase *kpEffectReduceColorsWidget::createCommand (
0167         kpCommandEnvironment *cmdEnviron) const
0168 {
0169     return new kpEffectReduceColorsCommand (depth (), dither (),
0170                                             m_actOnSelection,
0171                                             cmdEnviron);
0172 }
0173 
0174 //---------------------------------------------------------------------
0175 
0176 #include "moc_kpEffectReduceColorsWidget.cpp"