Code

* use enums to deal with displacementmap channel selectors
[inkscape.git] / src / ui / widget / attr-widget.h
1 /**
2  * \brief Very basic interface for classes that control attributes
3  *
4  * Authors:
5  *   Nicholas Bishop <nicholasbishop@gmail.com>
6  *   Rodrigo Kumpera <kumpera@gmail.com>
7  *
8  * Copyright (C) 2007 Authors
9  *
10  * Released under GNU GPL.  Read the file 'COPYING' for more information.
11  */
13 #ifndef INKSCAPE_UI_WIDGET_ATTR_WIDGET_H
14 #define INKSCAPE_UI_WIDGET_ATTR_WIDGET_H
16 #include "attributes.h"
17 #include "sp-object.h"
18 #include "xml/node.h"
20 namespace Inkscape {
21 namespace UI {
22 namespace Widget {
24 enum DefaultValueType
25 {
26     T_NONE,
27     T_DOUBLE,
28     T_VECT_DOUBLE,
29     T_BOOL,
30     T_UINT
31 };
33 class DefaultValueHolder
34 {
35     DefaultValueType type;
36     union {
37         double d_val;
38         std::vector<double>* vt_val;
39         bool b_val;
40         unsigned int uint_val;
41     } value;
43     //FIXME remove copy ctor and assignment operator as private to avoid double free of the vector
44 public:
45     DefaultValueHolder () {
46         type = T_NONE;
47     }
49     DefaultValueHolder (double d) {
50         type = T_DOUBLE;
51         value.d_val = d;
52     }
54     DefaultValueHolder (std::vector<double>* d) {
55         type = T_VECT_DOUBLE;
56         value.vt_val = d;
57     }
59     DefaultValueHolder (bool d) {
60         type = T_BOOL;
61         value.b_val = d;
62     }
64     DefaultValueHolder (unsigned int ui) {
65         type = T_UINT;
66         value.uint_val = ui;
67     }
69     ~DefaultValueHolder() {
70         if (type == T_VECT_DOUBLE)
71             delete value.vt_val;
72     }
74     unsigned int as_uint() {
75         g_assert (type == T_UINT);
76         return value.uint_val;
77     }
79     bool as_bool() {
80         g_assert (type == T_BOOL);
81         return value.b_val;
82     }
84     double as_double() {
85         g_assert (type == T_DOUBLE);
86         return value.d_val;
87     }
89     std::vector<double>* as_vector() {
90         g_assert (type == T_VECT_DOUBLE);
91         return value.vt_val;
92     }
93 };
95 class AttrWidget
96 {
97 public:
98     AttrWidget(const SPAttributeEnum a, unsigned int value)
99         : _attr(a),
100           _default(value)
101     {}
103     AttrWidget(const SPAttributeEnum a, double value)
104         : _attr(a),
105           _default(value)
106     {}
108     AttrWidget(const SPAttributeEnum a, bool value)
109         : _attr(a),
110           _default(value)
111     {}
113     AttrWidget(const SPAttributeEnum a)
114         : _attr(a),
115           _default()
116     {}
118     virtual ~AttrWidget()
119     {}
121     virtual Glib::ustring get_as_attribute() const = 0;
122     virtual void set_from_attribute(SPObject*) = 0;
124     SPAttributeEnum get_attribute() const
125     {
126         return _attr;
127     }
129     sigc::signal<void>& signal_attr_changed()
130     {
131         return _signal;
132     }
133 protected:
134     DefaultValueHolder* get_default() { return &_default; }
135     const gchar* attribute_value(SPObject* o) const
136     {
137         const gchar* name = (const gchar*)sp_attribute_name(_attr);
138         if(name && o) {
139             const gchar* val = SP_OBJECT_REPR(o)->attribute(name);
140             return val;
141         }
142         return 0;
143     }
145 private:
146     const SPAttributeEnum _attr;
147     DefaultValueHolder _default;
148     sigc::signal<void> _signal;
149 };
155 #endif
157 /*
158   Local Variables:
159   mode:c++
160   c-file-style:"stroustrup"
161   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
162   indent-tabs-mode:nil
163   fill-column:99
164   End:
165 */
166 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :