Krita Source Code Documentation
Loading...
Searching...
No Matches
KisCurveOptionModel.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2022 Dmitry Kazakov <dimula73@gmail.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 */
6
8
9#include <lager/lenses/tuple.hpp>
10#include <KisZug.h>
11#include <KisLager.h>
12
13auto activeCurveLens = lager::lenses::getset(
14 [](const std::tuple<KisCurveOptionDataCommon, QString> &data) -> QString {
15 QString activeCurve;
16 const bool useSameCurve = std::get<0>(data).useSameCurve;
17
18 if (useSameCurve) {
19 activeCurve = std::get<0>(data).commonCurve;
20 } else {
21 const QString activeSensorId = std::get<1>(data);
22 KIS_SAFE_ASSERT_RECOVER_RETURN_VALUE(!activeSensorId.isEmpty(), activeCurve);
23 std::vector<const KisSensorData*> srcSensors = std::get<0>(data).sensors();
24 auto it =
25 std::find_if(srcSensors.begin(), srcSensors.end(),
26 [activeSensorId] (const KisSensorData *sensor) {
27 return sensor->id.id() == activeSensorId;
28 });
29
30 KIS_SAFE_ASSERT_RECOVER_NOOP(it != srcSensors.end());
31
32 if (it != srcSensors.end()) {
33 activeCurve = (*it)->curve;
34 }
35 }
36
37 return activeCurve;
38 },
39 [](std::tuple<KisCurveOptionDataCommon, QString> data, QString curve) {
40 const bool useSameCurve = std::get<0>(data).useSameCurve;
41
42 if (useSameCurve) {
43 std::get<0>(data).commonCurve = curve;
44 } else {
45 const QString activeSensorId = std::get<1>(data);
46 KIS_SAFE_ASSERT_RECOVER_RETURN_VALUE(!activeSensorId.isEmpty(), data);
47 std::vector<KisSensorData*> srcSensors = std::get<0>(data).sensors();
48 auto it =
49 std::find_if(srcSensors.begin(), srcSensors.end(),
50 [activeSensorId] (const KisSensorData *sensor) {
51 return sensor->id.id() == activeSensorId;
52 });
53
54 KIS_SAFE_ASSERT_RECOVER_NOOP(it != srcSensors.end());
55
56 if (it != srcSensors.end()) {
57 (*it)->curve = curve;
58 }
59 }
60
61 return data;
62 });
63
64auto activeCurveRangeLens = lager::lenses::getset(
65 [](const std::tuple<KisCurveOptionDataCommon, QString> &data) -> QRectF {
66 QRectF activeCurveRange(0.0, 0.0, 1.0, 1.0);
67
68 const QString activeSensorId = std::get<1>(data);
69 KIS_SAFE_ASSERT_RECOVER_RETURN_VALUE(!activeSensorId.isEmpty(), activeCurveRange);
70 std::vector<const KisSensorData*> srcSensors = std::get<0>(data).sensors();
71 auto it =
72 std::find_if(srcSensors.begin(), srcSensors.end(),
73 [activeSensorId] (const KisSensorData *sensor) {
74 return sensor->id.id() == activeSensorId;
75 });
76
77 KIS_SAFE_ASSERT_RECOVER_NOOP(it != srcSensors.end());
78
79 if (it != srcSensors.end()) {
80 activeCurveRange = (*it)->baseCurveRange();
81 }
82
83 return activeCurveRange;
84 },
85 [](std::tuple<KisCurveOptionDataCommon, QString> data, const QRectF curveRange) {
86 const QString activeSensorId = std::get<1>(data);
87 KIS_SAFE_ASSERT_RECOVER_RETURN_VALUE(!activeSensorId.isEmpty(), data);
88 std::vector<KisSensorData*> srcSensors = std::get<0>(data).sensors();
89 auto it =
90 std::find_if(srcSensors.begin(), srcSensors.end(),
91 [activeSensorId] (const KisSensorData *sensor) {
92 return sensor->id.id() == activeSensorId;
93 });
94
95 KIS_SAFE_ASSERT_RECOVER_NOOP(it != srcSensors.end());
96
97 if (it != srcSensors.end()) {
98 (*it)->setBaseCurveRange(curveRange);
99 }
100
101 return data;
102 });
103
104int calcActiveSensorLength(const KisCurveOptionDataCommon &data, const QString &activeSensorId) {
105 return data.sensorData->calcActiveSensorLength(activeSensorId);
106}
107
108KisCurveOptionModel::KisCurveOptionModel(lager::cursor<KisCurveOptionDataCommon> _optionData,
109 lager::reader<bool> externallyEnabled,
110 std::optional<lager::reader<RangeState>> strengthRangeOverride,
111 qreal strengthDisplayMultiplier,
112 KisCurveRangeModelFactory rangeModelFactory)
113 : optionData(_optionData)
114 , strengthRangeNorm(strengthRangeOverride ? *strengthRangeOverride :
115 lager::with(optionData[&KisCurveOptionDataCommon::strengthMinValue],
116 optionData[&KisCurveOptionDataCommon::strengthMaxValue]))
117 , activeSensorIdData(optionData->sensors().front()->id.id())
120 , LAGER_QT(effectiveIsChecked) {lager::with(LAGER_QT(isChecked), externallyEnabled).map(std::logical_and{})}
121 , LAGER_QT(effectiveStrengthValueNorm) {
122 lager::with(strengthRangeNorm.zoom(lager::lenses::first),
124 strengthRangeNorm.zoom(lager::lenses::second))
125 .map(&qBound<qreal>)}
126 , LAGER_QT(strengthValueDenorm) {
128 .zoom(kislager::lenses::scale<qreal>(strengthDisplayMultiplier))}
129 , LAGER_QT(effectiveStrengthStateDenorm) {
130 lager::with(LAGER_QT(effectiveStrengthValueNorm),
131 strengthRangeNorm.zoom(lager::lenses::first),
132 strengthRangeNorm.zoom(lager::lenses::second))
133 .xform(kiszug::foreach_arg(kiszug::map_multiply<qreal>(strengthDisplayMultiplier)))}
137 , LAGER_QT(activeSensorId) {activeSensorIdData}
138 , LAGER_QT(activeSensorLength) {lager::with(optionData, activeSensorIdData).map(&calcActiveSensorLength)}
139 , LAGER_QT(labelsState) {lager::with(LAGER_QT(activeSensorId), LAGER_QT(activeSensorLength))}
140 , LAGER_QT(activeCurve) {lager::with(optionData,
141 LAGER_QT(activeSensorId))
142 .zoom(activeCurveLens)}
143 , rangeModel(rangeModelFactory(LAGER_QT(activeCurve),
144 lager::with(optionData,
145 LAGER_QT(activeSensorId))
147 LAGER_QT(activeSensorId),
148 LAGER_QT(activeSensorLength)))
149 , LAGER_QT(displayedCurve) {rangeModel->curve()}
150 , LAGER_QT(curveXMinLabel) {rangeModel->xMinLabel()}
151 , LAGER_QT(curveXMaxLabel) {rangeModel->xMaxLabel()}
152 , LAGER_QT(curveYMinLabel) {rangeModel->yMinLabel()}
153 , LAGER_QT(curveYMaxLabel) {rangeModel->yMaxLabel()}
154{
155}
156
158{
159}
160
162{
164 data.isChecked = effectiveIsChecked();
165 std::tie(data.strengthMinValue, data.strengthMaxValue) =
166 strengthRangeNorm.get();
167 data.strengthValue = effectiveStrengthValueNorm();
168 return data;
169}
auto activeCurveRangeLens
LAGER_QT(isChecked)
rangeModel(rangeModelFactory(LAGER_QT(activeCurve), lager::with(optionData, LAGER_QT(activeSensorId)) .zoom(activeCurveRangeLens), LAGER_QT(activeSensorId), LAGER_QT(activeSensorLength)))
int calcActiveSensorLength(const KisCurveOptionDataCommon &data, const QString &activeSensorId)
auto activeCurveLens
std::function< KisCurveRangeModelInterface *(lager::cursor< QString >, lager::cursor< QRectF >, lager::reader< QString >, lager::reader< int >)> KisCurveRangeModelFactory
KisCurveOptionDataCommon bakedOptionData() const
lager::reader< RangeState > strengthRangeNorm
KisCurveOptionModel(lager::cursor< KisCurveOptionDataCommon > optionData, lager::reader< bool > externallyEnabled, std::optional< lager::reader< RangeState > > strengthRangeOverride, qreal strengthDisplayMultiplier, KisCurveRangeModelFactory rangeModelFactory)
lager::cursor< KisCurveOptionDataCommon > optionData
#define KIS_SAFE_ASSERT_RECOVER_RETURN_VALUE(cond, val)
Definition kis_assert.h:129
#define KIS_SAFE_ASSERT_RECOVER_NOOP(cond)
Definition kis_assert.h:130
constexpr auto foreach_arg
Definition KisZug.h:84
QSharedDataPointer< KisSensorPackInterface > sensorData