summaryrefslogtreecommitdiffstats
path: root/src/ui/widget/attr-widget.h
blob: 014a540ceeb5b5fdea02cae15d1481e66e29d46d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * Authors:
 *   Nicholas Bishop <nicholasbishop@gmail.com>
 *   Rodrigo Kumpera <kumpera@gmail.com>
 *
 * Copyright (C) 2007 Authors
 *
 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
 */

#ifndef INKSCAPE_UI_WIDGET_ATTR_WIDGET_H
#define INKSCAPE_UI_WIDGET_ATTR_WIDGET_H

#include "attributes.h"
#include "object/sp-object.h"
#include "xml/node.h"

namespace Inkscape {
namespace UI {
namespace Widget {

enum DefaultValueType
{
    T_NONE,
    T_DOUBLE,
    T_VECT_DOUBLE,
    T_BOOL,
    T_UINT,
    T_CHARPTR
};

/**
 * Very basic interface for classes that control attributes.
 */
class DefaultValueHolder
{
    DefaultValueType type;
    union {
        double d_val;
        std::vector<double>* vt_val;
        bool b_val;
        unsigned int uint_val;
        char* cptr_val;
    } value;

    //FIXME remove copy ctor and assignment operator as private to avoid double free of the vector
public:
    DefaultValueHolder () {
        type = T_NONE;
    }

    DefaultValueHolder (double d) {
        type = T_DOUBLE;
        value.d_val = d;
    }

    DefaultValueHolder (std::vector<double>* d) {
        type = T_VECT_DOUBLE;
        value.vt_val = d;
    }

    DefaultValueHolder (char* c) {
        type = T_CHARPTR;
        value.cptr_val = c;
    }

    DefaultValueHolder (bool d) {
        type = T_BOOL;
        value.b_val = d;
    }

    DefaultValueHolder (unsigned int ui) {
        type = T_UINT;
        value.uint_val = ui;
    }

    ~DefaultValueHolder() {
        if (type == T_VECT_DOUBLE)
            delete value.vt_val;
    }

    unsigned int as_uint() {
        g_assert (type == T_UINT);
        return value.uint_val;
    }

    bool as_bool() {
        g_assert (type == T_BOOL);
        return value.b_val;
    }

    double as_double() {
        g_assert (type == T_DOUBLE);
        return value.d_val;
    }

    std::vector<double>* as_vector() {
        g_assert (type == T_VECT_DOUBLE);
        return value.vt_val;
    }

    char* as_charptr() {
        g_assert (type == T_CHARPTR);
        return value.cptr_val;
    }
};

class AttrWidget
{
public:
    AttrWidget(const SPAttributeEnum a, unsigned int value)
        : _attr(a),
          _default(value)
    {}

    AttrWidget(const SPAttributeEnum a, double value)
        : _attr(a),
          _default(value)
    {}

    AttrWidget(const SPAttributeEnum a, bool value)
        : _attr(a),
          _default(value)
    {}
    
    AttrWidget(const SPAttributeEnum a, char* value)
        : _attr(a),
          _default(value)
    {}
    
    AttrWidget(const SPAttributeEnum a)
        : _attr(a),
          _default()
    {}

    virtual ~AttrWidget()
    = default;

    virtual Glib::ustring get_as_attribute() const = 0;
    virtual void set_from_attribute(SPObject*) = 0;

    SPAttributeEnum get_attribute() const
    {
        return _attr;
    }

    sigc::signal<void>& signal_attr_changed()
    {
        return _signal;
    }
protected:
    DefaultValueHolder* get_default() { return &_default; }
    const gchar* attribute_value(SPObject* o) const
    {
        const gchar* name = (const gchar*)sp_attribute_name(_attr);
        if(name && o) {
            const gchar* val = o->getRepr()->attribute(name);
            return val;
        }
        return nullptr;
    }

private:
    const SPAttributeEnum _attr;
    DefaultValueHolder _default;
    sigc::signal<void> _signal;
};

}
}
}

#endif

/*
  Local Variables:
  mode:c++
  c-file-style:"stroustrup"
  c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
  indent-tabs-mode:nil
  fill-column:99
  End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :