1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (C) Johan Engelen 2007 <j.b.c.engelen@utwente.nl>
4  *
5  * Released under GNU GPL v2+, read the file 'COPYING' for more information.
6  */
7 
8 
9 #include "live_effects/parameter/parameter.h"
10 #include "live_effects/effect.h"
11 #include "svg/svg.h"
12 #include "xml/repr.h"
13 
14 #include "svg/stringstream.h"
15 
16 #include "verbs.h"
17 
18 #include <glibmm/i18n.h>
19 
20 #include <utility>
21 
22 #define noLPEREALPARAM_DEBUG
23 
24 namespace Inkscape {
25 
26 namespace LivePathEffect {
27 
28 
Parameter(Glib::ustring label,Glib::ustring tip,Glib::ustring key,Inkscape::UI::Widget::Registry * wr,Effect * effect)29 Parameter::Parameter(Glib::ustring label, Glib::ustring tip, Glib::ustring key, Inkscape::UI::Widget::Registry *wr,
30                      Effect *effect)
31     : param_key(std::move(key))
32     , param_wr(wr)
33     , param_label(std::move(label))
34     , oncanvas_editable(false)
35     , widget_is_visible(true)
36     , widget_is_enabled(true)
37     , param_tooltip(std::move(tip))
38     , param_effect(effect)
39 {
40 }
41 
param_write_to_repr(const char * svgd)42 void Parameter::param_write_to_repr(const char *svgd)
43 {
44     param_effect->getRepr()->setAttribute(param_key, svgd);
45 }
46 
write_to_SVG()47 void Parameter::write_to_SVG()
48 {
49     param_write_to_repr(param_getSVGValue().c_str());
50 }
51 
52 /*###########################################
53  *   REAL PARAM
54  */
ScalarParam(const Glib::ustring & label,const Glib::ustring & tip,const Glib::ustring & key,Inkscape::UI::Widget::Registry * wr,Effect * effect,gdouble default_value)55 ScalarParam::ScalarParam(const Glib::ustring &label, const Glib::ustring &tip, const Glib::ustring &key,
56                          Inkscape::UI::Widget::Registry *wr, Effect *effect, gdouble default_value)
57     : Parameter(label, tip, key, wr, effect)
58     , value(default_value)
59     , min(-SCALARPARAM_G_MAXDOUBLE)
60     , max(SCALARPARAM_G_MAXDOUBLE)
61     , integer(false)
62     , defvalue(default_value)
63     , digits(2)
64     , inc_step(0.1)
65     , inc_page(1)
66     , add_slider(false)
67     , _set_undo(true)
68 {
69 }
70 
71 ScalarParam::~ScalarParam() = default;
72 
param_readSVGValue(const gchar * strvalue)73 bool ScalarParam::param_readSVGValue(const gchar *strvalue)
74 {
75     double newval;
76     unsigned int success = sp_svg_number_read_d(strvalue, &newval);
77     if (success == 1) {
78         param_set_value(newval);
79         return true;
80     }
81     return false;
82 }
83 
param_getSVGValue() const84 Glib::ustring ScalarParam::param_getSVGValue() const
85 {
86     Inkscape::SVGOStringStream os;
87     os << value;
88     return os.str();
89 }
90 
param_getDefaultSVGValue() const91 Glib::ustring ScalarParam::param_getDefaultSVGValue() const
92 {
93     Inkscape::SVGOStringStream os;
94     os << defvalue;
95     return os.str();
96 }
97 
param_set_default()98 void ScalarParam::param_set_default() { param_set_value(defvalue); }
99 
param_update_default(gdouble default_value)100 void ScalarParam::param_update_default(gdouble default_value) { defvalue = default_value; }
101 
param_update_default(const gchar * default_value)102 void ScalarParam::param_update_default(const gchar *default_value)
103 {
104     double newval;
105     unsigned int success = sp_svg_number_read_d(default_value, &newval);
106     if (success == 1) {
107         param_update_default(newval);
108     }
109 }
110 
param_transform_multiply(Geom::Affine const & postmul,bool set)111 void ScalarParam::param_transform_multiply(Geom::Affine const &postmul, bool set)
112 {
113     // Check if proportional stroke-width scaling is on
114     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
115     bool transform_stroke = prefs ? prefs->getBool("/options/transform/stroke", true) : true;
116     if (transform_stroke || set) {
117         param_set_value(value * postmul.descrim());
118         write_to_SVG();
119     }
120 }
121 
param_set_value(gdouble val)122 void ScalarParam::param_set_value(gdouble val)
123 {
124     value = val;
125     if (integer)
126         value = round(value);
127     if (value > max)
128         value = max;
129     if (value < min)
130         value = min;
131 }
132 
param_set_range(gdouble min,gdouble max)133 void ScalarParam::param_set_range(gdouble min, gdouble max)
134 {
135     // if you look at client code, you'll see that many effects
136     // has a tendency to set an upper range of Geom::infinity().
137     // Once again, in gtk2, this is not a problem. But in gtk3,
138     // widgets get allocated the amount of size they ask for,
139     // leading to excessively long widgets.
140     if (min >= -SCALARPARAM_G_MAXDOUBLE) {
141         this->min = min;
142     } else {
143         this->min = -SCALARPARAM_G_MAXDOUBLE;
144     }
145     if (max <= SCALARPARAM_G_MAXDOUBLE) {
146         this->max = max;
147     } else {
148         this->max = SCALARPARAM_G_MAXDOUBLE;
149     }
150     param_set_value(value); // reset value to see whether it is in ranges
151 }
152 
param_make_integer(bool yes)153 void ScalarParam::param_make_integer(bool yes)
154 {
155     integer = yes;
156     digits = 0;
157     inc_step = 1;
158     inc_page = 10;
159 }
160 
param_set_undo(bool set_undo)161 void ScalarParam::param_set_undo(bool set_undo) { _set_undo = set_undo; }
162 
param_newWidget()163 Gtk::Widget *ScalarParam::param_newWidget()
164 {
165     if (widget_is_visible) {
166         Inkscape::UI::Widget::RegisteredScalar *rsu = Gtk::manage(new Inkscape::UI::Widget::RegisteredScalar(
167             param_label, param_tooltip, param_key, *param_wr, param_effect->getRepr(), param_effect->getSPDoc()));
168 
169         rsu->setValue(value);
170         rsu->setDigits(digits);
171         rsu->setIncrements(inc_step, inc_page);
172         rsu->setRange(min, max);
173         rsu->setProgrammatically = false;
174         if (add_slider) {
175             rsu->addSlider();
176         }
177         if (_set_undo) {
178             rsu->set_undo_parameters(SP_VERB_DIALOG_LIVE_PATH_EFFECT, _("Change scalar parameter"));
179         }
180         return dynamic_cast<Gtk::Widget *>(rsu);
181     } else {
182         return nullptr;
183     }
184 }
185 
param_set_digits(unsigned digits)186 void ScalarParam::param_set_digits(unsigned digits) { this->digits = digits; }
187 
param_set_increments(double step,double page)188 void ScalarParam::param_set_increments(double step, double page)
189 {
190     inc_step = step;
191     inc_page = page;
192 }
193 
194 
195 
196 } /* namespace LivePathEffect */
197 } /* namespace Inkscape */
198 
199 /*
200   Local Variables:
201   mode:c++
202   c-file-style:"stroustrup"
203   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
204   indent-tabs-mode:nil
205   fill-column:99
206   End:
207 */
208 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :
209