1 /*
2  *  This file is part of RawTherapee.
3  *
4  *  Copyright (c) 2004-2010 Gabor Horvath <hgabor@rawtherapee.com>
5  *
6  *  RawTherapee is free software: you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation, either version 3 of the License, or
9  *  (at your option) any later version.
10  *
11  *  RawTherapee is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with RawTherapee.  If not, see <https://www.gnu.org/licenses/>.
18  */
19 #include "thresholdadjuster.h"
20 #include <sigc++/slot.h>
21 #include <cmath>
22 #include "multilangmgr.h"
23 #include "../rtengine/rtengine.h"
24 #include "options.h"
25 #include "guiutils.h"
26 #include "rtimage.h"
27 
28 #define MIN_RESET_BUTTON_HEIGHT 17
29 
ThresholdAdjuster(Glib::ustring label,double minValueBottom,double maxValueBottom,double defBottom,Glib::ustring labelBottom,unsigned int precisionBottom,double minValueTop,double maxValueTop,double defTop,Glib::ustring labelTop,unsigned int precisionTop,ThresholdCurveProvider * curveProvider,bool editedCheckBox)30 ThresholdAdjuster::ThresholdAdjuster (Glib::ustring label,
31                                       double minValueBottom, double maxValueBottom, double defBottom, Glib::ustring labelBottom, unsigned int precisionBottom,
32                                       double minValueTop,    double maxValueTop,    double defTop,    Glib::ustring labelTop,    unsigned int precisionTop,
33                                       ThresholdCurveProvider* curveProvider, bool editedCheckBox)
34     : tSelector(minValueBottom, maxValueBottom, defBottom, labelBottom, precisionBottom, minValueTop, maxValueTop, defTop, labelTop, precisionTop, curveProvider)
35 
36 {
37     initialDefaultVal[ThresholdSelector::TS_BOTTOMLEFT] = defBottom;
38     initialDefaultVal[ThresholdSelector::TS_TOPLEFT] = defTop;
39     initialDefaultVal[ThresholdSelector::TS_BOTTOMRIGHT] = 0.; // unused
40     initialDefaultVal[ThresholdSelector::TS_TOPRIGHT] = 0.;    // unused
41 
42     initObject (label, editedCheckBox);
43 }
44 
ThresholdAdjuster(Glib::ustring label,double minValue,double maxValue,double defBottom,double defTop,unsigned int precision,bool startAtOne,bool editedCheckBox)45 ThresholdAdjuster::ThresholdAdjuster (Glib::ustring label, double minValue, double maxValue, double defBottom,
46                                       double defTop, unsigned int precision, bool startAtOne, bool editedCheckBox)
47     : tSelector(minValue, maxValue, defBottom, defTop, precision, startAtOne)
48 {
49     initialDefaultVal[ThresholdSelector::TS_BOTTOMLEFT] = defBottom;
50     initialDefaultVal[ThresholdSelector::TS_TOPLEFT] = defTop;
51     initialDefaultVal[ThresholdSelector::TS_BOTTOMRIGHT] = maxValue;
52     initialDefaultVal[ThresholdSelector::TS_TOPRIGHT] = maxValue;
53 
54     initObject (label, editedCheckBox);
55 }
56 
ThresholdAdjuster(Glib::ustring label,double minValue,double maxValue,double defBottomLeft,double defTopLeft,double defBottomRight,double defTopRight,unsigned int precision,bool startAtOne,bool editedCheckBox)57 ThresholdAdjuster::ThresholdAdjuster (Glib::ustring label, double minValue, double maxValue,
58                                       double defBottomLeft, double defTopLeft, double defBottomRight, double defTopRight,
59                                       unsigned int precision, bool startAtOne, bool editedCheckBox)
60     : tSelector(minValue, maxValue, defBottomLeft, defTopLeft,
61                 defBottomRight, defTopRight, precision, startAtOne)
62 {
63     initialDefaultVal[ThresholdSelector::TS_BOTTOMLEFT] = defBottomLeft;
64     initialDefaultVal[ThresholdSelector::TS_TOPLEFT] = defTopLeft;
65     initialDefaultVal[ThresholdSelector::TS_BOTTOMRIGHT] = defBottomRight;
66     initialDefaultVal[ThresholdSelector::TS_TOPRIGHT] = defTopRight;
67 
68     initObject (label, editedCheckBox);
69 }
70 
initObject(Glib::ustring label,bool editedcb)71 void ThresholdAdjuster::initObject (Glib::ustring label, bool editedcb)
72 {
73 
74     adjusterListener = nullptr;
75     afterReset = false;
76     blocked = false;
77 
78     addMode = false;
79 
80     delay = options.adjusterMinDelay;
81 
82     set_name("ThresholdAdjuster");
83 
84     hbox = Gtk::manage (new Gtk::HBox ());
85 
86     this->label = Gtk::manage (new Gtk::Label (label, Gtk::ALIGN_START));
87 
88     if (editedcb) {
89         editedCheckBox = Gtk::manage (new Gtk::CheckButton ());
90         editedChange = editedCheckBox->signal_toggled().connect( sigc::mem_fun(*this, &ThresholdAdjuster::editedToggled) );
91         hbox->pack_start (*editedCheckBox);
92     } else {
93         editedCheckBox = nullptr;
94     }
95 
96     hbox->pack_start (*this->label);
97 
98     reset = Gtk::manage (new Gtk::Button ());
99     reset->add (*Gtk::manage (new RTImage ("undo-small.png", "redo-small.png")));
100     reset->set_relief (Gtk::RELIEF_NONE);
101     reset->set_tooltip_markup (M("ADJUSTER_RESET_TO_DEFAULT"));
102 
103     hbox->pack_end (*reset, Gtk::PACK_SHRINK, 0);
104 
105     reset->set_size_request (-1, this->label->get_height() > MIN_RESET_BUTTON_HEIGHT ? this->label->get_height() : MIN_RESET_BUTTON_HEIGHT);
106 
107     pack_start (*hbox, false, false);
108     pack_start (tSelector, false, false);
109 
110     editedState = defEditedState = Irrelevant;
111 
112     selectorChange = tSelector.signal_value_changed().connect( sigc::mem_fun(*this, &ThresholdAdjuster::selectorChanged) );
113     reset->signal_button_release_event().connect_notify( sigc::mem_fun(*this, &ThresholdAdjuster::resetPressed) );
114 
115     show_all ();
116 }
117 
~ThresholdAdjuster()118 ThresholdAdjuster::~ThresholdAdjuster ()
119 {
120 
121     selectorChange.disconnect();
122     delayConnection.block(true);
123     adjusterListener = nullptr;
124 }
125 
setDefault(double bottom,double top)126 void ThresholdAdjuster::setDefault (double bottom, double top)
127 {
128 
129     selectorChange.block (true);
130     tSelector.setPositions(shapeValue(bottom), shapeValue(top));
131     selectorChange.block (false);
132 }
133 
setDefault(double bottomLeft,double topLeft,double bottomRight,double topRight)134 void ThresholdAdjuster::setDefault (double bottomLeft, double topLeft, double bottomRight, double topRight)
135 {
136 
137     selectorChange.block (true);
138     tSelector.setPositions(shapeValue(bottomLeft), shapeValue(topLeft), shapeValue(bottomRight), shapeValue(topRight));
139     selectorChange.block (false);
140 }
141 
setDefaultEditedState(EditedState eState)142 void ThresholdAdjuster::setDefaultEditedState (EditedState eState)
143 {
144 
145     defEditedState = eState;
146 }
147 
resetPressed(GdkEventButton * event)148 void ThresholdAdjuster::resetPressed (GdkEventButton* event)
149 {
150 
151     if (editedState != Irrelevant) {
152         editedState = defEditedState;
153 
154         if (editedCheckBox) {
155             editedChange.block (true);
156             editedCheckBox->set_active (defEditedState == Edited);
157             editedChange.block (false);
158         }
159 
160         refreshLabelStyle ();
161     }
162 
163     afterReset = true;
164 
165     if ((event != nullptr) && (event->state & GDK_CONTROL_MASK) && (event->button == 1))
166         // CTRL pressed : resetting to current default value
167     {
168         tSelector.reset();
169     } else
170         // no modifier key or addMode=true : resetting to initial default value
171         tSelector.setPositions(initialDefaultVal[ThresholdSelector::TS_BOTTOMLEFT],
172                                initialDefaultVal[ThresholdSelector::TS_TOPLEFT],
173                                initialDefaultVal[ThresholdSelector::TS_BOTTOMRIGHT],
174                                initialDefaultVal[ThresholdSelector::TS_TOPRIGHT]);
175 }
176 
shapeValue(double a)177 double ThresholdAdjuster::shapeValue (double a)
178 {
179 
180     unsigned int digit = tSelector.getPrecision();
181     return round(a * pow(double(10), digit)) / pow(double(10), digit);
182 }
183 
selectorChanged()184 void ThresholdAdjuster::selectorChanged ()
185 {
186 
187     if (delayConnection.connected()) {
188         delayConnection.disconnect ();
189     }
190 
191     if (delay == 0) {
192         if (adjusterListener && !blocked) {
193             sendToListener ();
194         }
195     } else {
196         delayConnection = Glib::signal_timeout().connect (sigc::mem_fun(*this, &ThresholdAdjuster::notifyListener), delay);
197     }
198 
199     if (!afterReset && editedState == UnEdited) {
200         editedState = Edited;
201 
202         if (editedCheckBox) {
203             editedChange.block (true);
204             editedCheckBox->set_active (true);
205             editedChange.block (false);
206         }
207 
208         refreshLabelStyle ();
209     }
210 
211     afterReset = false;
212 }
213 
setValue(double bottom,double top)214 void ThresholdAdjuster::setValue (double bottom, double top)
215 {
216 
217     selectorChange.block (true);
218     tSelector.setPositions(bottom, top);
219     selectorChange.block (false);
220     afterReset = false;
221 }
222 
setValue(double bottomLeft,double topLeft,double bottomRight,double topRight)223 void ThresholdAdjuster::setValue (double bottomLeft, double topLeft, double bottomRight, double topRight)
224 {
225 
226     selectorChange.block (true);
227     tSelector.setPositions(bottomLeft, topLeft, bottomRight, topRight);
228     selectorChange.block (false);
229     afterReset = false;
230 }
231 
getValue(double & bottom,double & top)232 void ThresholdAdjuster::getValue (double& bottom, double& top)
233 {
234     tSelector.getPositions<double> (bottom, top);
235 }
getValue(double & bottomLeft,double & topLeft,double & bottomRight,double & topRight)236 void ThresholdAdjuster::getValue (double& bottomLeft, double& topLeft, double& bottomRight, double& topRight)
237 {
238     tSelector.getPositions<double> (bottomLeft, topLeft, bottomRight, topRight);
239 }
getValue(int & bottom,int & top)240 void ThresholdAdjuster::getValue (int& bottom, int& top)
241 {
242     tSelector.getPositions<int> (bottom, top);
243 }
getValue(int & bottomLeft,int & topLeft,int & bottomRight,int & topRight)244 void ThresholdAdjuster::getValue (int& bottomLeft, int& topLeft, int& bottomRight, int& topRight)
245 {
246     tSelector.getPositions<int> (bottomLeft, topLeft, bottomRight, topRight);
247 }
248 
getValue(Glib::ustring & bottom,Glib::ustring & top)249 void ThresholdAdjuster::getValue (Glib::ustring& bottom, Glib::ustring& top)
250 {
251     tSelector.getPositions (bottom, top);
252 }
253 
getValue(Glib::ustring & bottomLeft,Glib::ustring & topLeft,Glib::ustring & bottomRight,Glib::ustring & topRight)254 void ThresholdAdjuster::getValue (Glib::ustring& bottomLeft, Glib::ustring& topLeft, Glib::ustring& bottomRight, Glib::ustring& topRight)
255 {
256     tSelector.getPositions (bottomLeft, topLeft, bottomRight, topRight);
257 }
258 
notifyListener()259 bool ThresholdAdjuster::notifyListener ()
260 {
261 
262     if (adjusterListener != nullptr && !blocked) {
263         GThreadLock lock;
264         sendToListener();
265     }
266 
267     return false;
268 }
269 
setBgCurveProvider(ThresholdCurveProvider * provider)270 void ThresholdAdjuster::setBgCurveProvider (ThresholdCurveProvider* provider)
271 {
272     tSelector.setBgCurveProvider(provider);
273 }
274 
275 
setEnabled(bool enabled)276 void ThresholdAdjuster::setEnabled (bool enabled)
277 {
278 
279     tSelector.set_sensitive (enabled);
280 }
281 
setEditedState(EditedState eState)282 void ThresholdAdjuster::setEditedState (EditedState eState)
283 {
284 
285     if (editedState != eState) {
286         if (editedCheckBox) {
287             editedChange.block (true);
288             editedCheckBox->set_active (eState == Edited);
289             editedChange.block (false);
290         }
291 
292         editedState = eState;
293         refreshLabelStyle ();
294     }
295 }
296 
getEditedState()297 EditedState ThresholdAdjuster::getEditedState ()
298 {
299 
300     if (editedState != Irrelevant && editedCheckBox) {
301         editedState = editedCheckBox->get_active () ? Edited : UnEdited;
302     }
303 
304     return editedState;
305 }
306 
showEditedCB()307 void ThresholdAdjuster::showEditedCB ()
308 {
309 
310     if (!editedCheckBox) {
311         editedCheckBox =  Gtk::manage(new Gtk::CheckButton ());
312         hbox->pack_start (*editedCheckBox, Gtk::PACK_SHRINK, 2);
313         hbox->reorder_child (*editedCheckBox, 0);
314         editedChange = editedCheckBox->signal_toggled().connect( sigc::mem_fun(*this, &ThresholdAdjuster::editedToggled) );
315     }
316 }
317 
refreshLabelStyle()318 void ThresholdAdjuster::refreshLabelStyle ()
319 {
320 
321     /*  Glib::RefPtr<Gtk::StyleContext> style = label->get_style_context ();
322         Pango::FontDescription fd = style->get_font ();
323         fd.set_weight (editedState==Edited ? Pango::WEIGHT_BOLD : Pango::WEIGHT_NORMAL);
324         style->set_font (fd);
325         label->set_style (style);
326         label->queue_draw ();*/
327 }
328 
editedToggled()329 void ThresholdAdjuster::editedToggled ()
330 {
331 
332     if (adjusterListener && !blocked) {
333         sendToListener ();
334     }
335 }
336 
sendToListener()337 void ThresholdAdjuster::sendToListener ()
338 {
339     if (tSelector.getPrecision() > 0) {
340         // if precision is >0, then we assume that the listener is waiting for doubles
341         rtengine::procparams::Threshold<double> t = tSelector.getPositions<double>();
342 
343         if (tSelector.isDouble()) {
344             adjusterListener->adjusterChanged (this, t.getBottomLeft(), t.getTopLeft(), t.getBottomRight(), t.getTopRight());
345             adjusterListener->adjusterChanged2 (this, t.getBottomLeft(), t.getTopLeft(), t.getBottomRight(), t.getTopRight());
346         } else {
347             adjusterListener->adjusterChanged (this, t.getBottomLeft(), t.getTopLeft());
348         }
349     } else {
350         // if precision is equal to 0, then we assume that the listener is waiting for integers
351         rtengine::procparams::Threshold<int> t = tSelector.getPositions<int>();
352 
353         if (tSelector.isDouble()) {
354             adjusterListener->adjusterChanged (this, t.getBottomLeft(), t.getTopLeft(), t.getBottomRight(), t.getTopRight());
355             adjusterListener->adjusterChanged2 (this, t.getBottomLeft(), t.getTopLeft(), t.getBottomRight(), t.getTopRight());
356         } else {
357             adjusterListener->adjusterChanged (this, t.getBottomLeft(), t.getTopLeft());
358         }
359     }
360 }
361 
set_tooltip_markup(const Glib::ustring & markup)362 void ThresholdAdjuster::set_tooltip_markup(const Glib::ustring& markup)
363 {
364     tSelector.set_tooltip_markup(markup);
365 }
366 
set_tooltip_text(const Glib::ustring & text)367 void ThresholdAdjuster::set_tooltip_text(const Glib::ustring& text)
368 {
369     tSelector.set_tooltip_text(text);
370 }
371 
372 /* For better readability, this method create the history string of the parameter column,
373  * so that the parameters list can be read in a more logical way (i.e. corresponding
374  * to the startAtOne field)
375  *
376  * If separatedMode==true, the top slider is assumed to be the primary slider, then the bottom slider as the second one
377  */
getHistoryString()378 Glib::ustring ThresholdAdjuster::getHistoryString ()
379 {
380     if (tSelector.isDouble()) {
381         Glib::ustring bl, tl, br, tr;
382         tSelector.getPositions(bl, tl, br, tr);
383         return Glib::ustring::compose(tSelector.isStartAtOne() ? "%2, %1, %3, %4" : "%1, %2, %4, %3", bl, tl, br, tr);
384     } else {
385         Glib::ustring b, t;
386         tSelector.getPositions(b, t);
387         return Glib::ustring::compose(tSelector.isStartAtOne() || separatedMode ? "%2, %1" : "%1, %2", b, t);
388     }
389 }
390