Inkscape
Vector Graphics Editor
Loading...
Searching...
No Matches
parameter-float.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Copyright (C) 2005-2007 Authors:
4 * Ted Gould <ted@gould.cx>
5 * Johan Engelen <johan@shouraizou.nl> *
6 * Jon A. Cruz <jon@joncruz.org>
7 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
8 */
9
10#include "parameter-float.h"
11
12#include <iomanip>
13
14#include <gtkmm/adjustment.h>
15#include <gtkmm/box.h>
16#include <gtkmm/label.h>
17
18#include "extension/extension.h"
19#include "preferences.h"
20#include "ui/pack.h"
24#include "xml/node.h"
25
26namespace Inkscape::Extension {
27
29 : InxParameter(xml, ext)
30{
31 // get value
32 if (xml->firstChild()) {
33 const char *value = xml->firstChild()->content();
34 if (value)
35 string_to_value(value);
36 }
37
39 _value = prefs->getDouble(pref_name(), _value);
40
41 // parse and apply limits
42 const char *min = xml->attribute("min");
43 if (min) {
44 _min = g_ascii_strtod(min, nullptr);
45 }
46
47 const char *max = xml->attribute("max");
48 if (max) {
49 _max = g_ascii_strtod(max, nullptr);
50 }
51
52 if (_value < _min) {
53 _value = _min;
54 }
55
56 if (_value > _max) {
57 _value = _max;
58 }
59
60 // parse precision
61 const char *precision = xml->attribute("precision");
62 if (precision != nullptr) {
63 _precision = strtol(precision, nullptr, 0);
64 }
65
66
67 // parse appearance
68 if (_appearance) {
69 if (!strcmp(_appearance, "full")) {
70 _mode = FULL;
71 } else {
72 g_warning("Invalid value ('%s') for appearance of parameter '%s' in extension '%s'",
74 }
75 }
76}
77
86double ParamFloat::set(double in)
87{
88 _value = in;
89 if (_value > _max) {
90 _value = _max;
91 }
92 if (_value < _min) {
93 _value = _min;
94 }
95
97 prefs->setDouble(pref_name(), _value);
98
99 return _value;
100}
101
103{
104 return Inkscape::ustring::format_classic(std::setprecision(_precision), std::fixed, _value);
105}
106
107void ParamFloat::string_to_value(const std::string &in)
108{
109 _value = g_ascii_strtod(in.c_str(), nullptr);
110}
111
113class ParamFloatAdjustment : public Gtk::Adjustment {
115 ParamFloat *_pref;
116 sigc::signal<void ()> *_changeSignal;
117protected:
120 ParamFloatAdjustment(ParamFloat *param, sigc::signal<void ()> *changeSignal)
121 : Gtk::Adjustment(0.0, param->min(), param->max(), 0.1, 1.0, 0)
122 , _pref(param)
123 , _changeSignal(changeSignal) {
124 this->set_value(_pref->get());
125 this->signal_value_changed().connect(sigc::mem_fun(*this, &ParamFloatAdjustment::val_changed));
126 return;
127 };
128
129 void val_changed ();
130
131public:
132 static Glib::RefPtr<ParamFloatAdjustment> create(ParamFloat* param, sigc::signal<void ()>* changeSignal) {
133 return Glib::make_refptr_for_instance(new ParamFloatAdjustment(param, changeSignal));
134 }
135
136}; /* class ParamFloatAdjustment */
137
144void ParamFloatAdjustment::val_changed()
145{
146 _pref->set(this->get_value());
147 if (_changeSignal != nullptr) {
148 _changeSignal->emit();
149 }
150 return;
151}
152
158Gtk::Widget *ParamFloat::get_widget(sigc::signal<void ()> *changeSignal)
159{
160 if (_hidden) {
161 return nullptr;
162 }
163
164 auto const hbox = Gtk::make_managed<Gtk::Box>(Gtk::Orientation::HORIZONTAL, GUI_PARAM_WIDGETS_SPACING);
165
166 auto fadjust = ParamFloatAdjustment::create(this, changeSignal);
167
168 if (_mode == FULL) {
169
170 Glib::ustring text;
171 if (_text != nullptr)
172 text = _text;
173 auto const scale = Gtk::make_managed<UI::Widget::SpinScale>(text, fadjust, _precision);
174 scale->set_size_request(400, -1);
175 scale->set_visible(true);
176 UI::pack_start(*hbox, *scale, true, true);
177
178 }
179 else if (_mode == DEFAULT) {
180
181 auto const label = Gtk::make_managed<Gtk::Label>(_text, Gtk::Align::START);
182 label->set_visible(true);
183 UI::pack_start(*hbox, *label, true, true);
184
185 auto const spin = Gtk::make_managed<Inkscape::UI::Widget::SpinButton>(fadjust, 0.1, _precision);
186 spin->set_visible(true);
187 UI::pack_start(*hbox, *spin, false, false);
188 }
189
190 hbox->set_visible(true);
191 return hbox;
192}
193
194} // namespace Inkscape::Extension
195
196/*
197 Local Variables:
198 mode:c++
199 c-file-style:"stroustrup"
200 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
201 indent-tabs-mode:nil
202 fill-column:99
203 End:
204*/
205// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :
double scale
Definition aa.cpp:228
The object that is the basis for the Extension system.
Definition extension.h:133
char const * get_id() const
Get the ID of this extension - not a copy don't delete!
A class to represent the parameter of an extension.
Definition parameter.h:38
char * _name
The name of this parameter.
Definition parameter.h:137
static constexpr int GUI_PARAM_WIDGETS_SPACING
Recommended spacing between the widgets making up a single Parameter (e.g.
Definition parameter.h:123
Glib::ustring pref_name() const
Build preference name for the current parameter.
char * _text
Parameter text to show as the GUI label.
Definition parameter.h:140
Inkscape::Extension::Extension * _extension
Which extension is this Widget attached to.
Definition widget.h:102
bool _hidden
Whether the widget is visible.
Definition widget.h:108
char * _appearance
Appearance of the widget (not used by all widgets).
Definition widget.h:115
AppearanceMode _mode
appearance mode
double get() const
Returns _value.
double set(double in)
A function to set the _value.
ParamFloat(Inkscape::XML::Node *xml, Inkscape::Extension::Extension *ext)
void string_to_value(const std::string &in) override
Sets the current value of the parameter from a string.
std::string value_to_string() const override
Gets the current value of the parameter in a string form.
Gtk::Widget * get_widget(sigc::signal< void()> *changeSignal) override
Creates a Float Adjustment for a float parameter.
int _precision
numeric precision (i.e.
Preference storage class.
Definition preferences.h:61
double getDouble(Glib::ustring const &pref_path, double def=0.0, Glib::ustring const &unit="")
Retrieve a floating point value.
static Preferences * get()
Access the singleton Preferences object.
void setDouble(Glib::ustring const &pref_path, double value)
Set a floating point value.
Interface for refcounted XML nodes.
Definition node.h:80
virtual Node * firstChild()=0
Get the first child of this node.
virtual char const * attribute(char const *key) const =0
Get the string representation of a node's attribute.
virtual char const * content() const =0
Get the content of a text or comment node.
Inkscape::Extension::Extension: Frontend to certain, possibly pluggable, actions.
Glib::ustring label
Definition desktop.h:50
Extension support.
void pack_start(Gtk::Box &box, Gtk::Widget &child, bool const expand, bool const fill, unsigned const padding)
Adds child to box, packed with reference to the start of box.
Definition pack.cpp:141
Glib::ustring format_classic(T const &... args)
Helpers for using Gtk::Boxes, encapsulating large changes between GTK3 & GTK4.
Singleton class to access the preferences file in a convenient way.
Derived from and replaces SpinSlider.
std::unique_ptr< Toolbar >(* create)()
Definition toolbars.cpp:56
Interface for XML nodes.