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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
|
/*
* Central facade for accessing and managing on-canvas controls.
*
* Author:
* Jon A. Cruz <jon@joncruz.org>
*
* Copyright 2012 Authors
*
* Released under GNU GPL, read the file 'COPYING' for more information
*/
#include "control-manager.h"
#include <algorithm>
#include <glib.h>
#include <glib-object.h>
#include "display/sodipodi-ctrl.h" // for SP_TYPE_CTRL
#include "display/sp-canvas-item.h"
#include "display/sp-ctrlline.h"
#include "display/sp-ctrlpoint.h"
#include "preferences.h"
namespace {
std::map<Inkscape::ControlType, std::vector<int> > sizeTable;
} // namespace
#define FILL_COLOR_NORMAL 0xffffff7f
#define FILL_COLOR_MOUSEOVER 0xff0000ff
// Default color for line:
#define LINE_COLOR_PRIMARY 0x0000ff7f
#define LINE_COLOR_SECONDARY 0xff00007f
#define LINE_COLOR_TERTIARY 0xffff007f
namespace Inkscape {
class ControlManagerImpl
{
public:
ControlManagerImpl();
~ControlManagerImpl() {}
SPCanvasItem *createControl(SPCanvasGroup *parent, ControlType type);
void setControlSize(int size, bool force = false);
void track(SPCanvasItem *anchor);
sigc::connection connectCtrlSizeChanged(const sigc::slot<void> &slot);
void updateItem(SPCanvasItem *item);
private:
static void thingFinalized(gpointer data, GObject *wasObj);
void thingFinalized(GObject *wasObj);
class PrefListener : public Inkscape::Preferences::Observer
{
public:
PrefListener(ControlManagerImpl &manager) : Inkscape::Preferences::Observer("/options/grabsize/value"), _mgr(manager) {}
virtual ~PrefListener() {}
virtual void notify(Inkscape::Preferences::Entry const &val) {
int size = val.getIntLimited(3, 1, 7);
_mgr.setControlSize(size);
}
ControlManagerImpl &_mgr;
};
sigc::signal<void> _sizeChangedSignal;
PrefListener _prefHook;
int _size;
std::vector<SPCanvasItem *> _itemList;
std::map<Inkscape::ControlType, std::vector<int> > _sizeTable;
};
ControlManagerImpl::ControlManagerImpl() :
_sizeChangedSignal(),
_prefHook(*this),
_size(3),
_itemList(),
_sizeTable()
{
Inkscape::Preferences *prefs = Inkscape::Preferences::get();
prefs->addObserver(_prefHook);
_size = prefs->getIntLimited("/options/grabsize/value", 3, 1, 7);
{
int sizes[] = {8, 8, 8, 8, 8, 8, 8};
_sizeTable[CTRL_TYPE_UNKNOWN] = std::vector<int>(sizes, sizes + (sizeof(sizes) / sizeof(sizes[0])));
}
{
int sizes[] = {2, 4, 6, 8, 10, 12, 14};
_sizeTable[CTRL_TYPE_ANCHOR] = std::vector<int>(sizes, sizes + (sizeof(sizes) / sizeof(sizes[0])));
}
{
int sizes[] = {2, 4, 7, 8, 9, 10, 12};
_sizeTable[CTRL_TYPE_ADJ_HANDLE] = std::vector<int>(sizes, sizes + (sizeof(sizes) / sizeof(sizes[0])));
}
{
int sizes[] = {4, 6, 8, 10, 12, 14, 16};
_sizeTable[CTRL_TYPE_POINT] = std::vector<int>(sizes, sizes + (sizeof(sizes) / sizeof(sizes[0])));
_sizeTable[CTRL_TYPE_ROTATE] = std::vector<int>(sizes, sizes + (sizeof(sizes) / sizeof(sizes[0])));
_sizeTable[CTRL_TYPE_SIZER] = std::vector<int>(sizes, sizes + (sizeof(sizes) / sizeof(sizes[0])));
_sizeTable[CTRL_TYPE_SHAPER] = std::vector<int>(sizes, sizes + (sizeof(sizes) / sizeof(sizes[0])));
}
{
int sizes[] = {2, 3, 4, 7, 8, 9, 10};
_sizeTable[CTRL_TYPE_ORIGIN] = std::vector<int>(sizes, sizes + (sizeof(sizes) / sizeof(sizes[0])));
}
{
int sizes[] = {1, 1, 1, 1, 1, 1, 1};
_sizeTable[CTRL_TYPE_INVISIPOINT] = std::vector<int>(sizes, sizes + (sizeof(sizes) / sizeof(sizes[0])));
}
}
void ControlManagerImpl::setControlSize(int size, bool force)
{
if ((size < 1) || (size > 7)) {
g_warning("Illegal logical size set: %d", size);
} else if (force || (size != _size)) {
_size = size;
for (std::vector<SPCanvasItem *>::iterator it = _itemList.begin(); it != _itemList.end(); ++it)
{
if (*it) {
updateItem(*it);
}
}
_sizeChangedSignal.emit();
}
}
SPCanvasItem *ControlManagerImpl::createControl(SPCanvasGroup *parent, ControlType type)
{
SPCanvasItem *item = 0;
double targetSize = _sizeTable[type][_size - 1];
switch (type)
{
case CTRL_TYPE_ADJ_HANDLE:
item = sp_canvas_item_new(parent, SP_TYPE_CTRL,
"shape", SP_CTRL_SHAPE_CIRCLE,
"size", targetSize,
"filled", 0,
"fill_color", 0xff00007f,
"stroked", 1,
"stroke_color", 0x0000ff7f,
NULL);
break;
case CTRL_TYPE_ANCHOR:
item = sp_canvas_item_new(parent, SP_TYPE_CTRL,
"size", targetSize,
"filled", 1,
"fill_color", FILL_COLOR_NORMAL,
"stroked", 1,
"stroke_color", 0x000000ff,
NULL);
break;
case CTRL_TYPE_ORIGIN:
item = sp_canvas_item_new(parent, SP_TYPE_CTRLPOINT,
NULL);
break;
case CTRL_TYPE_INVISIPOINT:
item = sp_canvas_item_new(parent, SP_TYPE_CTRL,
"shape", SP_CTRL_SHAPE_SQUARE,
"size", targetSize,
NULL);
break;
case CTRL_TYPE_UNKNOWN:
default:
item = sp_canvas_item_new(parent, SP_TYPE_CTRL, NULL);
}
if (item) {
item->ctrlType = type;
}
return item;
}
void ControlManagerImpl::track(SPCanvasItem *item)
{
g_object_weak_ref( G_OBJECT(item), ControlManagerImpl::thingFinalized, this );
_itemList.push_back(item);
setControlSize(_size, true);
}
sigc::connection ControlManagerImpl::connectCtrlSizeChanged(const sigc::slot<void> &slot)
{
return _sizeChangedSignal.connect(slot);
}
void ControlManagerImpl::updateItem(SPCanvasItem *item)
{
if (item) {
double target = _sizeTable[item->ctrlType][_size - 1];
if ((item->ctrlType == CTRL_TYPE_ORIGIN) && SP_IS_CTRLPOINT(item)) {
sp_ctrlpoint_set_radius(SP_CTRLPOINT(item), target / 2.0);
} else {
sp_canvas_item_set(item, "size", target, NULL);
}
sp_canvas_item_request_update(item);
}
}
void ControlManagerImpl::thingFinalized(gpointer data, GObject *wasObj)
{
if (data) {
reinterpret_cast<ControlManagerImpl *>(data)->thingFinalized(wasObj);
}
}
void ControlManagerImpl::thingFinalized(GObject *wasObj)
{
SPCanvasItem *wasItem = reinterpret_cast<SPCanvasItem *>(wasObj);
if (wasItem)
{
std::vector<SPCanvasItem *>::iterator it = std::find(_itemList.begin(), _itemList.end(), wasItem);
if (it != _itemList.end())
{
_itemList.erase(it);
}
}
}
// ----------------------------------------------------
ControlManager::ControlManager() :
_impl(new ControlManagerImpl())
{
}
ControlManager::~ControlManager()
{
}
ControlManager &ControlManager::getManager()
{
static ControlManager instance;
return instance;
}
SPCanvasItem *ControlManager::createControl(SPCanvasGroup *parent, ControlType type)
{
return _impl->createControl(parent, type);
}
SPCtrlLine *ControlManager::createControlLine(SPCanvasGroup *parent, CtrlLineType type)
{
SPCtrlLine *line = SP_CTRLLINE(sp_canvas_item_new(parent, SP_TYPE_CTRLLINE, NULL));
if (line) {
line->ctrlType = CTRL_TYPE_LINE;
line->setRgba32((type == CTLINE_PRIMARY) ? LINE_COLOR_PRIMARY :
(type == CTLINE_SECONDARY) ? LINE_COLOR_SECONDARY : LINE_COLOR_TERTIARY);
}
return line;
}
SPCtrlLine *ControlManager::createControlLine(SPCanvasGroup *parent, Geom::Point const &p1, Geom::Point const &p2, CtrlLineType type)
{
SPCtrlLine *line = createControlLine(parent, type);
if (line) {
line->setCoords(p1, p2);
}
return line;
}
void ControlManager::track(SPCanvasItem *item)
{
_impl->track(item);
}
sigc::connection ControlManager::connectCtrlSizeChanged(const sigc::slot<void> &slot)
{
return _impl->connectCtrlSizeChanged(slot);
}
void ControlManager::updateItem(SPCanvasItem *item)
{
return _impl->updateItem(item);
}
} // namespace Inkscape
/*
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 :
|