kis_airbrush_option_widget.cpp 5.56 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 *  Copyright (c) 2008 Boudewijn Rempt <boud@valdyas.org>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */
18
#include "kis_airbrush_option_widget.h"
19
#include "kis_paintop_settings.h"
20
#include <klocalizedstring.h>
21 22 23 24 25 26

#include <QWidget>
#include <QRadioButton>

#include "ui_wdgairbrush.h"

27
const qreal MINIMUM_RATE = 1.0;
28 29
const qreal MAXIMUM_RATE = 1000.0;
const int RATE_NUM_DECIMALS = 2;
Allen Marshall's avatar
Allen Marshall committed
30
const qreal RATE_EXPONENT_RATIO = 2.0;
31 32
const qreal RATE_SINGLE_STEP = 1.0;
const qreal DEFAULT_RATE = 20.0;
33 34 35 36

class KisAirbrushWidget: public QWidget, public Ui::WdgAirbrush
{
public:
Allen Marshall's avatar
Allen Marshall committed
37
    KisAirbrushWidget(QWidget *parent = 0, bool canIgnoreSpacing = true)
38
        : QWidget(parent) {
39
        setupUi(this);
40

41 42 43 44
        sliderRate->setRange(MINIMUM_RATE, MAXIMUM_RATE, RATE_NUM_DECIMALS);
        sliderRate->setExponentRatio(RATE_EXPONENT_RATIO);
        sliderRate->setSingleStep(RATE_SINGLE_STEP);
        sliderRate->setValue(DEFAULT_RATE);
Allen Marshall's avatar
Allen Marshall committed
45 46 47

        checkBoxIgnoreSpacing->setVisible(canIgnoreSpacing);
        checkBoxIgnoreSpacing->setEnabled(canIgnoreSpacing);
48 49 50
    }
};

51
struct KisAirbrushOptionWidget::Private {
52 53 54 55 56
public:
    bool ignoreSpacing;
    // We store the airbrush interval (in milliseconds) instead of the rate because the interval is
    // likely to be accessed more often.
    qreal airbrushInterval;
57
    QScopedPointer<KisAirbrushWidget> configPage;
58
};
59

60
KisAirbrushOptionWidget::KisAirbrushOptionWidget(bool enabled, bool canIgnoreSpacing)
61
    : KisPaintOpOption(KisPaintOpOption::COLOR, enabled)
62
    , m_d(new Private())
63
{
64
    setObjectName("KisAirbrushOption");
65 66

    // Initialize GUI.
67
    m_checkable = true;
68
    m_d->configPage.reset(new KisAirbrushWidget(nullptr, canIgnoreSpacing));
69
    connect(m_d->configPage->sliderRate, SIGNAL(valueChanged(qreal)), SLOT(slotIntervalChanged()));
70
    connect(m_d->configPage->checkBoxIgnoreSpacing, SIGNAL(toggled(bool)),
71
            SLOT(slotIgnoreSpacingChanged()));
72
    setConfigurationPage(m_d->configPage.data());
73

74 75 76 77
    // Read initial configuration from the GUI.
    updateIgnoreSpacing();
    updateInterval();
}
78

79
KisAirbrushOptionWidget::~KisAirbrushOptionWidget()
80
{
81
    delete m_d;
82 83
}

84
void KisAirbrushOptionWidget::writeOptionSetting(KisPropertiesConfigurationSP setting) const
85
{
86
    KIS_SAFE_ASSERT_RECOVER (m_d->airbrushInterval > 0.0) {
87 88
        m_d->airbrushInterval = 1.0;
    }
89
    setting->setProperty(AIRBRUSH_ENABLED, isChecked());
90
    setting->setProperty(AIRBRUSH_RATE, 1000.0 / m_d->airbrushInterval);
91 92 93

    qDebug() << "writeOptionSetting. Interval:" << m_d->airbrushInterval << "Calculated rate" << 1000.0 / m_d->airbrushInterval << "Rate in widget" << m_d->configPage->sliderRate->value();

94
    setting->setProperty(AIRBRUSH_IGNORE_SPACING, m_d->ignoreSpacing);
95 96
}

97
void KisAirbrushOptionWidget::readOptionSetting(const KisPropertiesConfigurationSP setting)
98 99
{
    setChecked(setting->getBool(AIRBRUSH_ENABLED));
100 101 102
    // Update settings in the widget. The widget's signals should cause the changes to be propagated
    // to this->m_d as well.
    m_d->configPage->sliderRate->setValue(setting->getDouble(AIRBRUSH_RATE, DEFAULT_RATE));
103 104 105

    qDebug() << "readOptionSetting. Interval:" << m_d->airbrushInterval << "Rate from settings" << setting->getDouble(AIRBRUSH_RATE, DEFAULT_RATE) << "Rate in widget" << m_d->configPage->sliderRate->value();
    m_d->configPage->checkBoxIgnoreSpacing->setChecked(setting->getBool(AIRBRUSH_IGNORE_SPACING, false));
106 107
}

108
qreal KisAirbrushOptionWidget::airbrushInterval() const
109 110 111 112
{
    return m_d->airbrushInterval;
}

113
bool KisAirbrushOptionWidget::ignoreSpacing() const
114 115
{
    return m_d->ignoreSpacing;
116 117
}

118
void KisAirbrushOptionWidget::slotIntervalChanged()
119 120 121 122 123
{
    updateInterval();
    emitSettingChanged();
}

124
void KisAirbrushOptionWidget::slotIgnoreSpacingChanged()
125 126 127 128 129
{
    updateIgnoreSpacing();
    emitSettingChanged();
}

130
void KisAirbrushOptionWidget::updateInterval()
131 132 133
{
    // Get rate in dabs per second, then convert to interval in milliseconds.
    qreal rate = m_d->configPage->sliderRate->value();
134
    KIS_SAFE_ASSERT_RECOVER(rate > 0.0) {
135 136 137
        rate = 1.0;
    }
    m_d->airbrushInterval = 1000.0 / rate;
138
    qDebug() << "updateInterval();. Interval:" << m_d->airbrushInterval << "Rate in widget" << m_d->configPage->sliderRate->value();
139 140
}

141
void KisAirbrushOptionWidget::updateIgnoreSpacing()
142 143 144
{
    m_d->ignoreSpacing = m_d->configPage->checkBoxIgnoreSpacing->isChecked();
}
145 146 147 148 149 150 151 152


void KisAirbrushOptionProperties::readOptionSettingImpl(const KisPropertiesConfiguration *setting){
    enabled = setting->getBool(AIRBRUSH_ENABLED);
    airbrushInterval = 1000.0 / setting->getDouble(AIRBRUSH_RATE, DEFAULT_RATE);
    ignoreSpacing = setting->getBool(AIRBRUSH_IGNORE_SPACING, false);
}

153
void KisAirbrushOptionProperties::writeOptionSettingImpl(KisPropertiesConfiguration *setting) const {
154 155 156 157
    setting->setProperty(AIRBRUSH_ENABLED, enabled);
    setting->setProperty(AIRBRUSH_RATE, 1000.0 / airbrushInterval > 0 ? airbrushInterval : 1.0);
    setting->setProperty(AIRBRUSH_IGNORE_SPACING, ignoreSpacing);
}