1 /* This file is part of the KDE project
2  * Copyright (C) Boudewijn Rempt <boud@valdyas.org>, (C) 2008
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public License
15  * along with this library; see the file COPYING.LIB.  If not, write to
16  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19 #include "kis_pressure_opacity_option.h"
20 #include <klocalizedstring.h>
21 #include <kis_painter.h>
22 #include <KoColor.h>
23 
24 
KisPressureOpacityOption()25 KisPressureOpacityOption::KisPressureOpacityOption()
26     : KisCurveOption("Opacity", KisPaintOpOption::GENERAL, true)
27 {
28     m_checkable = false;
29 }
30 
31 
writeOptionSetting(KisPropertiesConfigurationSP setting) const32 void KisPressureOpacityOption::writeOptionSetting(KisPropertiesConfigurationSP setting) const
33 {
34     KisCurveOption::writeOptionSetting(setting);
35     setting->setProperty("OpacityVersion", "2");
36 }
37 
readOptionSetting(const KisPropertiesConfigurationSP setting)38 void KisPressureOpacityOption::readOptionSetting(const KisPropertiesConfigurationSP setting)
39 {
40     KisCurveOption::readOptionSetting(setting);
41     if (setting->getString("OpacityVersion", "1") == "1") {
42         KisDynamicSensorSP pressureSensor = sensor(PRESSURE, true);
43         if (pressureSensor) {
44             QList<QPointF> points = pressureSensor->curve().points();
45             QList<QPointF> points_new;
46             Q_FOREACH (const QPointF & p, points) {
47                 points_new.push_back(QPointF(p.x() * 0.5, p.y()));
48             }
49             pressureSensor->setCurve(KisCubicCurve(points_new));
50         }
51     }
52 }
53 
apply(KisPainter * painter,const KisPaintInformation & info) const54 quint8 KisPressureOpacityOption::apply(KisPainter* painter, const KisPaintInformation& info) const
55 {
56 
57     if (!isChecked()) {
58         return painter->opacity();
59     }
60     quint8 origOpacity = painter->opacity();
61 
62     qreal opacity = (qreal)(origOpacity * computeSizeLikeValue(info));
63     quint8 opacity2 = (quint8)qRound(qBound<qreal>(OPACITY_TRANSPARENT_U8, opacity, OPACITY_OPAQUE_U8));
64 
65     painter->setOpacityUpdateAverage(opacity2);
66     return origOpacity;
67 }
68 
getOpacityf(const KisPaintInformation & info)69 qreal KisPressureOpacityOption::getOpacityf(const KisPaintInformation& info)
70 {
71     if (!isChecked()) return 1.0;
72     return computeSizeLikeValue(info);
73 }
74 
75