c87e860d5da882c6b3dea4eef091b9f6ca6a44d0
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 };
32 class DefaultValueHolder
33 {
34 DefaultValueType type;
35 union {
36 double d_val;
37 std::vector<double>* vt_val;
38 bool b_val;
39 } value;
41 //FIXME remove copy ctor and assignment operator as private to avoid double free of the vector
42 public:
43 DefaultValueHolder () {
44 type = T_NONE;
45 }
47 DefaultValueHolder (double d) {
48 type = T_DOUBLE;
49 value.d_val = d;
50 }
52 DefaultValueHolder (std::vector<double>* d) {
53 type = T_VECT_DOUBLE;
54 value.vt_val = d;
55 }
57 DefaultValueHolder (bool d) {
58 type = T_BOOL;
59 value.b_val = d;
60 }
62 ~DefaultValueHolder() {
63 if (type == T_VECT_DOUBLE)
64 delete value.vt_val;
65 }
67 bool as_bool() {
68 g_assert (type == T_BOOL);
69 return value.b_val;
70 }
72 double as_double() {
73 g_assert (type == T_DOUBLE);
74 return value.d_val;
75 }
77 std::vector<double>* as_vector() {
78 g_assert (type == T_VECT_DOUBLE);
79 return value.vt_val;
80 }
81 };
83 class AttrWidget
84 {
85 public:
86 AttrWidget(const SPAttributeEnum a, double value)
87 : _attr(a),
88 _default(value)
89 {}
91 AttrWidget(const SPAttributeEnum a, bool value)
92 : _attr(a),
93 _default(value)
94 {}
96 AttrWidget(const SPAttributeEnum a)
97 : _attr(a),
98 _default()
99 {}
101 virtual ~AttrWidget()
102 {}
104 virtual Glib::ustring get_as_attribute() const = 0;
105 virtual void set_from_attribute(SPObject*) = 0;
107 SPAttributeEnum get_attribute() const
108 {
109 return _attr;
110 }
112 sigc::signal<void>& signal_attr_changed()
113 {
114 return _signal;
115 }
116 protected:
117 DefaultValueHolder* get_default() { return &_default; }
118 const gchar* attribute_value(SPObject* o) const
119 {
120 const gchar* name = (const gchar*)sp_attribute_name(_attr);
121 if(name && o) {
122 const gchar* val = SP_OBJECT_REPR(o)->attribute(name);
123 return val;
124 }
125 return 0;
126 }
128 private:
129 const SPAttributeEnum _attr;
130 DefaultValueHolder _default;
131 sigc::signal<void> _signal;
132 };
134 }
135 }
136 }
138 #endif
140 /*
141 Local Variables:
142 mode:c++
143 c-file-style:"stroustrup"
144 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
145 indent-tabs-mode:nil
146 fill-column:99
147 End:
148 */
149 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :