kpEffectBalanceWidget.cpp 9.1 KB
Newer Older
1
/*
2
   Copyright (c) 2003-2007 Clarence Dang <dang@kde.org>
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
   All rights reserved.

   Redistribution and use in source and binary forms, with or without
   modification, are permitted provided that the following conditions
   are met:

   1. Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
   2. Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in the
      documentation and/or other materials provided with the distribution.

   THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
   IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
   IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
   INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
   NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
   THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/


#define DEBUG_KP_EFFECT_BALANCE 0


31 32 33 34 35 36 37 38
#include "kpEffectBalanceWidget.h"

#include "imagelib/effects/kpEffectBalance.h"
#include "commands/imagelib/effects/kpEffectBalanceCommand.h"
#include "pixmapfx/kpPixmapFX.h"

#include <kdebug.h>
#include <klocale.h>
Martin Koller's avatar
Martin Koller committed
39
#include <knuminput.h>
40

41
#include <cmath>
Adriaan de Groot's avatar
Adriaan de Groot committed
42
#include <math.h>
43

Christoph Feck's avatar
Christoph Feck committed
44
#include <qcombobox.h>
45 46 47 48 49 50 51 52 53 54 55 56
#include <qgridlayout.h>
#include <qlabel.h>
#include <qlayout.h>
#include <qpushbutton.h>

#if DEBUG_KP_EFFECT_BALANCE
    #include <qdatetime.h>
#endif


kpEffectBalanceWidget::kpEffectBalanceWidget (bool actOnSelection,
                                              QWidget *parent)
57
    : kpEffectWidgetBase (actOnSelection, parent)
58 59
{
    QGridLayout *lay = new QGridLayout (this);
60
    lay->setMargin (0);
61 62 63


    QLabel *brightnessLabel = new QLabel (i18n ("&Brightness:"), this);
Martin Koller's avatar
Martin Koller committed
64
    m_brightnessInput = new KIntNumInput (0/*value*/, this);
65
    m_brightnessInput->setRange (-50, 50);
Martin Koller's avatar
Martin Koller committed
66
    m_brightnessInput->setSliderEnabled();
67 68 69
    QPushButton *brightnessResetPushButton = new QPushButton (i18n ("Re&set"), this);

    QLabel *contrastLabel = new QLabel (i18n ("Co&ntrast:"), this);
Martin Koller's avatar
Martin Koller committed
70
    m_contrastInput = new KIntNumInput (0/*value*/, this);
71
    m_contrastInput->setRange (-50, 50);
Martin Koller's avatar
Martin Koller committed
72
    m_contrastInput->setSliderEnabled();
73 74 75
    QPushButton *contrastResetPushButton = new QPushButton (i18n ("&Reset"), this);

    QLabel *gammaLabel = new QLabel (i18n ("&Gamma:"), this);
Martin Koller's avatar
Martin Koller committed
76
    m_gammaInput = new KIntNumInput (0/*value*/, this);
77
    m_gammaInput->setRange (-50, 50);
Martin Koller's avatar
Martin Koller committed
78
    m_gammaInput->setSliderEnabled();
79 80
    // TODO: This is what should be shown in the m_gammaInput spinbox
    m_gammaLabel = new QLabel (this);
81 82
    // TODO: This doesn't seem to be wide enough with some fonts so the
    //       whole layout moves when we drag the gamma slider.
83 84 85 86 87 88
    m_gammaLabel->setMinimumWidth (m_gammaLabel->fontMetrics ().width (QLatin1String (" 10.00 ")));
    m_gammaLabel->setAlignment (m_gammaLabel->alignment () | Qt::AlignRight);
    QPushButton *gammaResetPushButton = new QPushButton (i18n ("Rese&t"), this);


    QWidget *spaceWidget = new QLabel (this);
89
    spaceWidget->setFixedSize (1, fontMetrics ().height () / 4);
90 91 92


    QLabel *channelLabel = new QLabel (i18n ("C&hannels:"), this);
Christoph Feck's avatar
Christoph Feck committed
93
    m_channelsComboBox = new QComboBox (this);
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
    m_channelsComboBox->addItem (i18n ("All"));
    m_channelsComboBox->addItem (i18n ("Red"));
    m_channelsComboBox->addItem (i18n ("Green"));
    m_channelsComboBox->addItem (i18n ("Blue"));


    QPushButton *resetPushButton = new QPushButton (i18n ("Reset &All Values"), this);


    brightnessLabel->setBuddy (m_brightnessInput);
    contrastLabel->setBuddy (m_contrastInput);
    gammaLabel->setBuddy (m_gammaInput);

    channelLabel->setBuddy (m_channelsComboBox);


    lay->addWidget (brightnessLabel, 0, 0);
    lay->addWidget (m_brightnessInput, 0, 1, 1, 2);
    lay->addWidget (brightnessResetPushButton, 0, 4);

    lay->addWidget (contrastLabel, 1, 0);
    lay->addWidget (m_contrastInput, 1, 1, 1, 2);
    lay->addWidget (contrastResetPushButton, 1, 4);

    lay->addWidget (gammaLabel, 2, 0);
    lay->addWidget (m_gammaInput, 2, 1, 1, 2);
    lay->addWidget (m_gammaLabel, 2, 3);
    lay->addWidget (gammaResetPushButton, 2, 4);

    lay->addWidget (spaceWidget, 3, 0, 1, 5);
    lay->addWidget (resetPushButton, 4, 2, 1, 3, Qt::AlignRight);

    lay->addWidget (channelLabel, 4, 0);
    lay->addWidget (m_channelsComboBox, 4, 1, Qt::AlignLeft);
    //lay->addWidget (resetPushButton, 4, 2, Qt::AlignRight);

    lay->setColumnStretch (1, 1);


    // (no need for settingsChangedDelayed() since BCG effect is so fast :))
    connect (m_brightnessInput, SIGNAL (valueChanged (int)),
             this, SIGNAL (settingsChangedNoWaitCursor ()));
    connect (m_contrastInput, SIGNAL (valueChanged (int)),
             this, SIGNAL (settingsChangedNoWaitCursor ()));

    connect (m_gammaInput, SIGNAL (valueChanged (int)),
             this, SLOT (recalculateGammaLabel ()));
    connect (m_gammaInput, SIGNAL (valueChanged (int)),
             this, SIGNAL (settingsChangedNoWaitCursor ()));

    connect (m_channelsComboBox, SIGNAL (activated (int)),
             this, SIGNAL (settingsChanged ()));

    connect (brightnessResetPushButton, SIGNAL (clicked ()),
             this, SLOT (resetBrightness ()));
    connect (contrastResetPushButton, SIGNAL (clicked ()),
             this, SLOT (resetContrast ()));
    connect (gammaResetPushButton, SIGNAL (clicked ()),
             this, SLOT (resetGamma ()));

    connect (resetPushButton, SIGNAL (clicked ()),
             this, SLOT (resetAll ()));


    recalculateGammaLabel ();
}

kpEffectBalanceWidget::~kpEffectBalanceWidget ()
{
}


166
// public virtual [base kpEffectWidgetBase]
167 168 169 170 171 172
QString kpEffectBalanceWidget::caption () const
{
    return i18n ("Settings");
}


173
// public virtual [base kpEffectWidgetBase]
174 175 176 177 178
bool kpEffectBalanceWidget::isNoOp () const
{
    return (brightness () == 0 && contrast () == 0 && gamma () == 0);
}

179 180
// public virtual [base kpEffectWidgetBase]
kpImage kpEffectBalanceWidget::applyEffect (const kpImage &image)
181
{
182
    return kpEffectBalance::applyEffect (image,
183
        channels (), brightness (), contrast (), gamma ());
184 185
}

186
// public virtual [base kpEffectWidgetBase]
187 188
kpEffectCommandBase *kpEffectBalanceWidget::createCommand (
        kpCommandEnvironment *cmdEnviron) const
189 190 191 192
{
    return new kpEffectBalanceCommand (channels (),
                                       brightness (), contrast (), gamma (),
                                       m_actOnSelection,
193
                                       cmdEnviron);
194 195 196 197 198 199 200 201 202 203
}


// protected
int kpEffectBalanceWidget::channels () const
{
    switch (m_channelsComboBox->currentIndex ())
    {
    default:
    case 0:
204
        return kpEffectBalance::RGB;
205 206

    case 1:
207
        return kpEffectBalance::Red;
208 209

    case 2:
210
        return kpEffectBalance::Green;
211 212

    case 3:
213
        return kpEffectBalance::Blue;
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
    }
}


// protected
int kpEffectBalanceWidget::brightness () const
{
    return m_brightnessInput->value ();
}

// protected
int kpEffectBalanceWidget::contrast () const
{
    return m_contrastInput->value ();
}

// protected
int kpEffectBalanceWidget::gamma () const
{
    return m_gammaInput->value ();
}


// protected slot
void kpEffectBalanceWidget::recalculateGammaLabel ()
{
    m_gammaLabel->setText (
        QLatin1String (" ") +
        QString::number (pow (10, gamma () / 50.0),
                         'f'/*[-]9.9*/,
                         2/*precision*/) +
        QLatin1String (" "));
    m_gammaLabel->repaint ();
}


// protected slot
void kpEffectBalanceWidget::resetBrightness ()
{
    if (brightness () == 0)
        return;

    bool sb = signalsBlocked ();

    if (!sb) blockSignals (true);
    m_brightnessInput->setValue (0);
    if (!sb) blockSignals (false);

    // Immediate update (if signals aren't blocked)
    emit settingsChanged ();
}

// protected slot
void kpEffectBalanceWidget::resetContrast ()
{
    if (contrast () == 0)
        return;

    bool sb = signalsBlocked ();

    if (!sb) blockSignals (true);
    m_contrastInput->setValue (0);
    if (!sb) blockSignals (false);

    // Immediate update (if signals aren't blocked)
    emit settingsChanged ();
}

// protected slot
void kpEffectBalanceWidget::resetGamma ()
{
    if (gamma () == 0)
        return;

    bool sb = signalsBlocked ();

    if (!sb) blockSignals (true);
    m_gammaInput->setValue (0);
    recalculateGammaLabel ();
    if (!sb) blockSignals (false);

    // Immediate update (if signals aren't blocked)
    emit settingsChanged ();
}


// protected slot
void kpEffectBalanceWidget::resetAll ()
{
    if (isNoOp ())
        return;

    // Prevent multiple settingsChanged() which would normally result in
    // redundant, expensive preview repaints
    blockSignals (true);

    resetBrightness ();
    resetContrast ();
    resetGamma ();

    recalculateGammaLabel ();

    blockSignals (false);

    emit settingsChanged ();
}