File indexing completed on 2024-12-22 04:12:40

0001 /*
0002  *  SPDX-FileCopyrightText: 2014 Dmitry Kazakov <dimula73@gmail.com>
0003  *
0004  *  SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 
0007 #include "kis_gamma_exposure_action.h"
0008 
0009 #include <QApplication>
0010 #include <QIcon>
0011 
0012 #include <klocalizedstring.h>
0013 #include <kis_canvas2.h>
0014 #include "kis_cursor.h"
0015 #include "KisViewManager.h"
0016 #include "kis_input_manager.h"
0017 #include "krita_utils.h"
0018 #include "kis_exposure_gamma_correction_interface.h"
0019 
0020 
0021 class KisGammaExposureAction::Private
0022 {
0023 public:
0024     Private(KisGammaExposureAction *qq) : q(qq) {}
0025 
0026     // Coverity requires sane defaults for all variables (CID 248819)
0027     KisGammaExposureAction *q {nullptr};
0028 
0029     Shortcuts mode {ExposureShortcut};
0030 
0031     qreal baseExposure {0.0};
0032     qreal baseGamma {0.0};
0033 
0034     void addExposure(qreal diff);
0035     void addGamma(qreal diff);
0036 };
0037 
0038 
0039 void KisGammaExposureAction::Private::addExposure(qreal diff)
0040 {
0041     KisExposureGammaCorrectionInterface *interface =
0042         q->inputManager()->canvas()->exposureGammaCorrectionInterface();
0043 
0044     if (!interface->canChangeExposureAndGamma()) return;
0045 
0046     interface->setCurrentExposure(interface->currentExposure() + diff);
0047 }
0048 
0049 void KisGammaExposureAction::Private::addGamma(qreal diff)
0050 {
0051     KisExposureGammaCorrectionInterface *interface =
0052         q->inputManager()->canvas()->exposureGammaCorrectionInterface();
0053 
0054     if (!interface->canChangeExposureAndGamma()) return;
0055 
0056     interface->setCurrentGamma(interface->currentGamma() + diff);
0057 }
0058 
0059 KisGammaExposureAction::KisGammaExposureAction()
0060     : KisAbstractInputAction("Exposure or Gamma")
0061     , d(new Private(this))
0062 {
0063     setName(i18n("Exposure and Gamma"));
0064     setDescription(i18n("The <i>Exposure and Gamma</i> action changes the display mode of the canvas."));
0065 
0066     QHash< QString, int > shortcuts;
0067     shortcuts.insert(i18n("Exposure Mode"), ExposureShortcut);
0068     shortcuts.insert(i18n("Gamma Mode"), GammaShortcut);
0069 
0070     shortcuts.insert(i18n("Exposure +0.5"), AddExposure05Shortcut);
0071     shortcuts.insert(i18n("Exposure -0.5"), RemoveExposure05Shortcut);
0072     shortcuts.insert(i18n("Gamma +0.5"), AddGamma05Shortcut);
0073     shortcuts.insert(i18n("Gamma -0.5"), RemoveGamma05Shortcut);
0074 
0075     shortcuts.insert(i18n("Exposure +0.2"), AddExposure02Shortcut);
0076     shortcuts.insert(i18n("Exposure -0.2"), RemoveExposure02Shortcut);
0077     shortcuts.insert(i18n("Gamma +0.2"), AddGamma02Shortcut);
0078     shortcuts.insert(i18n("Gamma -0.2"), RemoveGamma02Shortcut);
0079 
0080     shortcuts.insert(i18n("Reset Exposure and Gamma"), ResetExposureAndGammaShortcut);
0081     setShortcutIndexes(shortcuts);
0082 }
0083 
0084 KisGammaExposureAction::~KisGammaExposureAction()
0085 {
0086     delete d;
0087 }
0088 
0089 int KisGammaExposureAction::priority() const
0090 {
0091     return 5;
0092 }
0093 
0094 void KisGammaExposureAction::activate(int shortcut)
0095 {
0096     if (shortcut == ExposureShortcut) {
0097         QApplication::setOverrideCursor(KisCursor::changeExposureCursor());
0098     } else /* if (shortcut == GammaShortcut) */ {
0099         QApplication::setOverrideCursor(KisCursor::changeGammaCursor());
0100     }
0101 }
0102 
0103 void KisGammaExposureAction::deactivate(int shortcut)
0104 {
0105     Q_UNUSED(shortcut);
0106     QApplication::restoreOverrideCursor();
0107 }
0108 
0109 void KisGammaExposureAction::begin(int shortcut, QEvent *event)
0110 {
0111     KisAbstractInputAction::begin(shortcut, event);
0112 
0113     KisExposureGammaCorrectionInterface *interface =
0114         inputManager()->canvas()->exposureGammaCorrectionInterface();
0115 
0116     switch(shortcut) {
0117     case ExposureShortcut:
0118         d->baseExposure = interface->currentExposure();
0119         d->mode = (Shortcuts)shortcut;
0120         break;
0121     case GammaShortcut:
0122         d->baseGamma = interface->currentGamma();
0123         d->mode = (Shortcuts)shortcut;
0124         break;
0125 
0126     case AddExposure05Shortcut:
0127         d->addExposure(0.5);
0128         break;
0129     case RemoveExposure05Shortcut:
0130         d->addExposure(-0.5);
0131         break;
0132     case AddGamma05Shortcut:
0133         d->addGamma(0.5);
0134         break;
0135     case RemoveGamma05Shortcut:
0136         d->addGamma(-0.5);
0137         break;
0138 
0139     case AddExposure02Shortcut:
0140         d->addExposure(0.2);
0141         break;
0142     case RemoveExposure02Shortcut:
0143         d->addExposure(-0.2);
0144         break;
0145     case AddGamma02Shortcut:
0146         d->addGamma(0.2);
0147         break;
0148     case RemoveGamma02Shortcut:
0149         d->addGamma(-0.2);
0150         break;
0151 
0152     case ResetExposureAndGammaShortcut: {
0153         KisExposureGammaCorrectionInterface *interface =
0154             inputManager()->canvas()->exposureGammaCorrectionInterface();
0155         if (!interface->canChangeExposureAndGamma()) break;
0156 
0157         interface->setCurrentGamma(1.0);
0158         interface->setCurrentExposure(0.0);
0159         break;
0160     }
0161     }
0162 }
0163 
0164 void KisGammaExposureAction::cursorMovedAbsolute(const QPointF &startPos, const QPointF &pos)
0165 {
0166     QPointF diff = -(pos - startPos);
0167 
0168     const int step = 200;
0169 
0170     KisExposureGammaCorrectionInterface *interface =
0171         inputManager()->canvas()->exposureGammaCorrectionInterface();
0172 
0173     if (!interface->canChangeExposureAndGamma()) return;
0174 
0175 
0176     if (d->mode == ExposureShortcut) {
0177         const qreal currentExposure = d->baseExposure + qreal(diff.y()) / step;
0178         interface->setCurrentExposure(currentExposure);
0179     } else if (d->mode == GammaShortcut) {
0180         const qreal currentGamma = d->baseExposure + qreal(diff.y()) / step;
0181         interface->setCurrentGamma(currentGamma);
0182     }
0183 }
0184 
0185 bool KisGammaExposureAction::isShortcutRequired(int shortcut) const
0186 {
0187     Q_UNUSED(shortcut);
0188     return false;
0189 }