Inkscape
Vector Graphics Editor
parameter.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-2.0-or-later
5/* Author:
6 * Ted Gould <ted@gould.cx>
7 * Johan Engelen <johan@shouraizou.nl>
8 * Jon A. Cruz <jon@joncruz.org>
9 *
10 * Copyright (C) 2005-2007 Authors
11 *
12 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
13 */
14
15#include <cstring>
16#include <list>
17
18#include <glibmm/i18n.h>
19#include <sigc++/sigc++.h>
20
21#include "parameter.h"
22#include "parameter-bool.h"
23#include "parameter-color.h"
24#include "parameter-float.h"
25#include "parameter-int.h"
26#include "parameter-notebook.h"
28#include "parameter-path.h"
29#include "parameter-string.h"
30#include "widget.h"
31#include "widget-label.h"
32
33#include "extension/extension.h"
34
35#include "object/sp-defs.h"
36
38
39#include "xml/node.h"
40
41
42namespace Inkscape {
43namespace Extension {
44
45
46// Re-implement ParamDescription for backwards-compatibility, deriving from both, WidgetLabel and InxParameter.
47// TODO: Should go away eventually...
48class ParamDescription : public virtual WidgetLabel, public virtual InxParameter {
49public:
50 ParamDescription(Inkscape::XML::Node *xml, Inkscape::Extension::Extension *ext)
51 : WidgetLabel(xml, ext)
52 , InxParameter(xml, ext)
53 {}
54
55 Gtk::Widget *get_widget(sigc::signal<void ()> *changeSignal) override
56 {
57 return this->WidgetLabel::get_widget(changeSignal);
58 }
59
60 // Well, no, I don't have a value! That's why I should not be an InxParameter!
61 std::string value_to_string() const override { return ""; }
62};
63
64
65
67{
68 InxParameter *param = nullptr;
69
70 try {
71 const char *type = in_repr->attribute("type");
72 if (!type) {
73 // we can't create a parameter without type
74 g_warning("Parameter without type in extension '%s'.", in_ext->get_id());
75 } else if(!strcmp(type, "bool") || !strcmp(type, "boolean")) { // support "boolean" for backwards-compatibility
76 param = new ParamBool(in_repr, in_ext);
77 } else if (!strcmp(type, "int")) {
78 param = new ParamInt(in_repr, in_ext);
79 } else if (!strcmp(type, "float")) {
80 param = new ParamFloat(in_repr, in_ext);
81 } else if (!strcmp(type, "string")) {
82 param = new ParamString(in_repr, in_ext);
83 } else if (!strcmp(type, "path")) {
84 param = new ParamPath(in_repr, in_ext);
85 } else if (!strcmp(type, "description")) {
86 // support deprecated "description" for backwards-compatibility
87 in_repr->setAttribute("gui-text", "description"); // TODO: hack to allow descriptions to be parameters
88 param = new ParamDescription(in_repr, in_ext);
89 } else if (!strcmp(type, "notebook")) {
90 in_repr->setAttribute("gui-text", "notebook"); // notebooks have no 'gui-text' (but Parameters need one)
91 param = new ParamNotebook(in_repr, in_ext);
92 } else if (!strcmp(type, "optiongroup")) {
93 param = new ParamOptionGroup(in_repr, in_ext);
94 } else if (!strcmp(type, "enum")) { // support deprecated "enum" for backwards-compatibility
95 in_repr->setAttribute("appearance", "combo");
96 param = new ParamOptionGroup(in_repr, in_ext);
97 } else if (!strcmp(type, "color")) {
98 param = new ParamColor(in_repr, in_ext);
99 } else {
100 g_warning("Unknown parameter type ('%s') in extension '%s'", type, in_ext->get_id());
101 }
102 } catch (const param_no_name&) {
103 } catch (const param_no_text&) {
104 }
105
106 // Note: param could equal nullptr
107 return param;
108}
109
111{
112 ParamBool const *boolpntr = dynamic_cast<ParamBool const *>(this);
113 if (!boolpntr) {
114 throw param_not_bool_param();
115 }
116 return boolpntr->get();
117}
118
120{
121 ParamInt const *intpntr = dynamic_cast<ParamInt const *>(this);
122 if (!intpntr) {
123 // This allows option groups to contain integers. Consider just using this.
125 return prefs->getInt(this->pref_name());
126 }
127 return intpntr->get();
128}
129
131{
132 ParamFloat const *floatpntr = dynamic_cast<ParamFloat const *>(this);
133 if (!floatpntr) {
134 throw param_not_float_param();
135 }
136 return floatpntr->get();
137}
138
139const char *InxParameter::get_string() const
140{
141 ParamString const *stringpntr = dynamic_cast<ParamString const *>(this);
142 if (!stringpntr) {
144 }
145 return stringpntr->get().c_str();
146}
147
149{
150 ParamOptionGroup const *param = dynamic_cast<ParamOptionGroup const *>(this);
151 if (!param) {
153 }
154 return param->get().c_str();
155}
156
157bool InxParameter::get_optiongroup_contains(const char *value) const
158{
159 ParamOptionGroup const *param = dynamic_cast<ParamOptionGroup const *>(this);
160 if (!param) {
162 }
163 return param->contains(value);
164}
165
166unsigned int InxParameter::get_color() const
167{
168 ParamColor const *param = dynamic_cast<ParamColor const *>(this);
169 if (!param) {
170 throw param_not_color_param();
171 }
172 return param->get();
173}
174
176{
177 ParamBool * boolpntr = dynamic_cast<ParamBool *>(this);
178 if (boolpntr == nullptr)
179 throw param_not_bool_param();
180 return boolpntr->set(in);
181}
182
184{
185 ParamInt *intpntr = dynamic_cast<ParamInt *>(this);
186 if (intpntr == nullptr)
187 throw param_not_int_param();
188 return intpntr->set(in);
189}
190
191double InxParameter::set_float(double in)
192{
193 ParamFloat * floatpntr;
194 floatpntr = dynamic_cast<ParamFloat *>(this);
195 if (floatpntr == nullptr)
196 throw param_not_float_param();
197 return floatpntr->set(in);
198}
199
200const char *InxParameter::set_string(const char *in)
201{
202 ParamString * stringpntr = dynamic_cast<ParamString *>(this);
203 if (stringpntr == nullptr)
205 return stringpntr->set(in).c_str();
206}
207
208const char *InxParameter::set_optiongroup(const char *in)
209{
210 ParamOptionGroup *param = dynamic_cast<ParamOptionGroup *>(this);
211 if (!param) {
213 }
214 return param->set(in).c_str();
215}
216
217unsigned int InxParameter::set_color(unsigned int in)
218{
219 ParamColor*param = dynamic_cast<ParamColor *>(this);
220 if (param == nullptr)
221 throw param_not_color_param();
222 return param->set(in);
223}
224
225
227 : InxWidget(in_repr, ext)
228{
229 // name (mandatory for all parameters)
230 const char *name = in_repr->attribute("name");
231 if (name) {
232 _name = g_strstrip(g_strdup(name));
233 }
234 if (!_name || !strcmp(_name, "")) {
235 g_warning("Parameter without name in extension '%s'.", _extension->get_id());
236 throw param_no_name();
237 }
238
239 // gui-text
240 const char *gui_text = in_repr->attribute("gui-text");
241 if (!gui_text) {
242 gui_text = in_repr->attribute("_gui-text"); // backwards-compatibility with underscored variants
243 }
244 if (gui_text) {
245 if (_translatable != NO) { // translate unless explicitly marked untranslatable
246 gui_text = get_translation(gui_text);
247 }
248 _text = g_strdup(gui_text);
249 }
250 if (!_text && !_hidden) {
251 g_warning("Parameter '%s' in extension '%s' is visible but does not have a 'gui-text'.",
253 throw param_no_text();
254 }
255
256 // gui-description (optional)
257 const char *gui_description = in_repr->attribute("gui-description");
258 if (!gui_description) {
259 gui_description = in_repr->attribute("_gui-description"); // backwards-compatibility with underscored variants
260 }
261 if (gui_description) {
262 if (_translatable != NO) { // translate unless explicitly marked untranslatable
263 gui_description = get_translation(gui_description);
264 }
265 _description = g_strdup(gui_description);
266 }
267}
268
270{
271 g_free(_name);
272 _name = nullptr;
273
274 g_free(_text);
275 _text = nullptr;
276
277 g_free(_description);
278 _description = nullptr;
279}
280
281Glib::ustring InxParameter::pref_name() const
282{
283 return Glib::ustring::compose("/extensions/%1.%2", _extension->get_id(), _name);
284}
285
287{
288 // if we end up here we're missing a definition of ::string() in one of the subclasses
289 g_critical("InxParameter::value_to_string called from parameter '%s' in extension '%s'", _name, _extension->get_id());
290 g_assert_not_reached();
291 return "";
292}
293
294void InxParameter::string_to_value(const std::string &in)
295{
296 g_critical("InxParameter::string_to_value called from parameter '%s' in extension '%s'", _name,
297 _extension->get_id());
298 g_assert_not_reached();
299}
300
301const std::string &InxParameter::set(const std::string &in)
302{
303 // Default and generic setter where in and out are consistant
304 string_to_value(in);
307 return in;
308}
309
310} // namespace Extension
311} // namespace Inkscape
312
313/*
314 Local Variables:
315 mode:c++
316 c-file-style:"stroustrup"
317 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
318 indent-tabs-mode:nil
319 fill-column:99
320 End:
321*/
322// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :
The object that is the basis for the Extension system.
Definition: extension.h:130
char const * get_id() const
Get the ID of this extension - not a copy don't delete!
Definition: extension.cpp:316
An error class for when a parameter is called on a type it is not.
Definition: parameter.h:122
A class to represent the parameter of an extension.
Definition: parameter.h:34
bool get_bool() const
Wrapper to cast to the object and use its function.
Definition: parameter.cpp:110
double set_float(double in)
Wrapper to cast to the object and use it's function.
Definition: parameter.cpp:191
const char * get_optiongroup() const
Wrapper to cast to the object and use it's function.
Definition: parameter.cpp:148
unsigned int set_color(unsigned int in)
Wrapper to cast to the object and use it's function.
Definition: parameter.cpp:217
bool get_optiongroup_contains(const char *value) const
Definition: parameter.cpp:157
char * _name
The name of this parameter.
Definition: parameter.h:133
unsigned int get_color() const
Wrapper to cast to the object and use it's function.
Definition: parameter.cpp:166
virtual const std::string & set(const std::string &in)
Calls string_to_value and then saves the result in the prefs.
Definition: parameter.cpp:301
static InxParameter * make(Inkscape::XML::Node *in_repr, Inkscape::Extension::Extension *in_ext)
Creates a new extension parameter for usage in a prefdialog.
Definition: parameter.cpp:66
const char * set_optiongroup(const char *in)
Wrapper to cast to the object and use it's function.
Definition: parameter.cpp:208
virtual std::string value_to_string() const
Gets the current value of the parameter in a string form.
Definition: parameter.cpp:286
int set_int(int in)
Wrapper to cast to the object and use it's function.
Definition: parameter.cpp:183
const char * get_string() const
Wrapper to cast to the object and use it's function.
Definition: parameter.cpp:139
double get_float() const
Wrapper to cast to the object and use it's function.
Definition: parameter.cpp:130
char const * name() const
Definition: parameter.h:78
char * _description
Extended description of the parameter (currently shown as tooltip on hover).
Definition: parameter.h:139
const char * set_string(const char *in)
Wrapper to cast to the object and use it's function.
Definition: parameter.cpp:200
int get_int() const
Wrapper to cast to the object and use it's function.
Definition: parameter.cpp:119
Glib::ustring pref_name() const
Build preference name for the current parameter.
Definition: parameter.cpp:281
InxParameter(Inkscape::XML::Node *in_repr, Inkscape::Extension::Extension *ext)
Definition: parameter.cpp:226
char * _text
Parameter text to show as the GUI label.
Definition: parameter.h:136
bool set_bool(bool in)
Wrapper to cast to the object and use it's function.
Definition: parameter.cpp:175
virtual void string_to_value(const std::string &in)
Sets the current value of the parameter from a string.
Definition: parameter.cpp:294
Base class to represent all widgets of an extension (including parameters)
Definition: widget.h:36
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
const char * get_translation(const char *msgid)
gets the gettext translation for msgid
Definition: widget.cpp:155
Translatable _translatable
Is widget translatable?
Definition: widget.h:118
bool set(bool in)
A function to set the state/value.
bool get() const
Returns the current state/value.
unsigned int get() const
Returns _value, with a \i const to protect it.
unsigned int set(unsigned int in)
double get() const
Returns _value.
double set(double in)
A function to set the _value.
int get() const
Returns _value.
Definition: parameter-int.h:36
int set(int in)
A function to set the _value.
A class to represent a notebook parameter of an extension.
bool contains(const Glib::ustring text) const
const Glib::ustring & set(const Glib::ustring &in)
A function to set the _value.
const Glib::ustring & get() const
Returns _value, with a \i const to protect it.
const Glib::ustring & set(Glib::ustring in)
A function to set the _value.
WidgetLabel(Inkscape::XML::Node *xml, Inkscape::Extension::Extension *ext)
Gtk::Widget * get_widget(sigc::signal< void()> *changeSignal) override
Create a label for the description.
Preference storage class.
Definition: preferences.h:63
static Preferences * get()
Access the singleton Preferences object.
Definition: preferences.h:599
int getInt(Glib::ustring const &pref_path, int def=0)
Retrieve an integer.
Definition: preferences.h:386
void setString(Glib::ustring const &pref_path, Glib::ustring const &value)
Set an UTF-8 string value.
Interface for refcounted XML nodes.
Definition: node.h:80
void setAttribute(Util::const_char_ptr key, Util::const_char_ptr value)
Change an attribute of this node.
Definition: node.cpp:25
virtual char const * attribute(char const *key) const =0
Get the string representation of a node's attribute.
A notebook with RGB, CMYK, CMS, HSL, and Wheel pages.
Inkscape::Extension::Extension: Frontend to certain, possibly pluggable, actions.
D2< T > compose(D2< T > const &a, T const &b)
Definition: d2.h:405
W & get_widget(const Glib::RefPtr< Gtk::Builder > &builder, const char *id)
Definition: builder-utils.h:33
CMYK to sRGB conversion routines.
Notebook parameter for extensions.
extension parameter for options with multiple predefined value choices
Path parameter for extensions.
Description widget for extensions.
Base class for extension widgets.
Interface for XML nodes.