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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
|
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Copyright (C) Theodore Janeczko 2012 <flutterguy317@gmail.com>
*
* Released under GNU GPL v2+, read the file 'COPYING' for more information.
*/
#include "live_effects/parameter/patharray.h"
#include <2geom/coord.h>
#include <2geom/point.h>
#include <glibmm/i18n.h>
#include <gtkmm/icontheme.h>
#include <gtkmm/imagemenuitem.h>
#include <gtkmm/scrolledwindow.h>
#include <gtkmm/separatormenuitem.h>
#include <gtkmm/widget.h>
#include "display/curve.h"
#include "document-undo.h"
#include "document.h"
#include "inkscape.h"
#include "live_effects/effect.h"
#include "live_effects/lpe-bspline.h"
#include "live_effects/lpe-spiro.h"
#include "live_effects/lpeobject-reference.h"
#include "live_effects/lpeobject.h"
#include "live_effects/parameter/patharray.h"
#include "object/sp-shape.h"
#include "object/sp-text.h"
#include "object/uri.h"
#include "originalpath.h"
#include "svg/stringstream.h"
#include "svg/svg.h"
#include "ui/clipboard.h"
#include "ui/icon-loader.h"
#include "ui/icon-names.h"
namespace Inkscape {
namespace LivePathEffect {
class PathArrayParam::ModelColumns : public Gtk::TreeModel::ColumnRecord
{
public:
ModelColumns()
{
add(_colObject);
add(_colLabel);
add(_colReverse);
add(_colVisible);
}
~ModelColumns() override = default;
Gtk::TreeModelColumn<PathAndDirectionAndVisible*> _colObject;
Gtk::TreeModelColumn<Glib::ustring> _colLabel;
Gtk::TreeModelColumn<bool> _colReverse;
Gtk::TreeModelColumn<bool> _colVisible;
};
PathArrayParam::PathArrayParam(const Glib::ustring &label, const Glib::ustring &tip, const Glib::ustring &key,
Inkscape::UI::Widget::Registry *wr, Effect *effect)
: Parameter(label, tip, key, wr, effect)
, _vector()
{
_tree = nullptr;
_scroller = nullptr;
_model = nullptr;
// refresh widgets on load to allow to remove the
// memory leak calling initui here
param_effect->refresh_widgets = true;
oncanvas_editable = true;
_from_original_d = false;
_allow_only_bspline_spiro = false;
}
PathArrayParam::~PathArrayParam()
{
while (!_vector.empty()) {
PathAndDirectionAndVisible *w = _vector.back();
unlink(w);
}
delete _model;
}
void PathArrayParam::initui()
{
SPDesktop * desktop = SP_ACTIVE_DESKTOP;
if (!desktop) {
return;
}
if (!_tree) {
_tree = manage(new Gtk::TreeView());
_model = new ModelColumns();
_store = Gtk::TreeStore::create(*_model);
_tree->set_model(_store);
_tree->set_reorderable(true);
_tree->enable_model_drag_dest (Gdk::ACTION_MOVE);
Gtk::CellRendererToggle *toggle_reverse = manage(new Gtk::CellRendererToggle());
int reverseColNum = _tree->append_column(_("Reverse"), *toggle_reverse) - 1;
Gtk::TreeViewColumn* col_reverse = _tree->get_column(reverseColNum);
toggle_reverse->set_activatable(true);
toggle_reverse->signal_toggled().connect(sigc::mem_fun(*this, &PathArrayParam::on_reverse_toggled));
col_reverse->add_attribute(toggle_reverse->property_active(), _model->_colReverse);
Gtk::CellRendererToggle *toggle_visible = manage(new Gtk::CellRendererToggle());
int visibleColNum = _tree->append_column(_("Visible"), *toggle_visible) - 1;
Gtk::TreeViewColumn* col_visible = _tree->get_column(visibleColNum);
toggle_visible->set_activatable(true);
toggle_visible->signal_toggled().connect(sigc::mem_fun(*this, &PathArrayParam::on_visible_toggled));
col_visible->add_attribute(toggle_visible->property_active(), _model->_colVisible);
Gtk::CellRendererText *text_renderer = manage(new Gtk::CellRendererText());
int nameColNum = _tree->append_column(_("Name"), *text_renderer) - 1;
Gtk::TreeView::Column *name_column = _tree->get_column(nameColNum);
name_column->add_attribute(text_renderer->property_text(), _model->_colLabel);
_tree->set_expander_column(*_tree->get_column(nameColNum) );
_tree->set_search_column(_model->_colLabel);
_scroller = Gtk::manage(new Gtk::ScrolledWindow());
//quick little hack -- newer versions of gtk gave the item zero space allotment
_scroller->set_size_request(-1, 120);
_scroller->add(*_tree);
_scroller->set_policy( Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC );
//_scroller.set_shadow_type(Gtk::SHADOW_IN);
}
param_readSVGValue(param_getSVGValue().c_str());
}
void PathArrayParam::on_reverse_toggled(const Glib::ustring &path)
{
Gtk::TreeModel::iterator iter = _store->get_iter(path);
Gtk::TreeModel::Row row = *iter;
PathAndDirectionAndVisible *w = row[_model->_colObject];
row[_model->_colReverse] = !row[_model->_colReverse];
w->reversed = row[_model->_colReverse];
param_write_to_repr(param_getSVGValue().c_str());
DocumentUndo::done(param_effect->getSPDoc(), _("Link path parameter to path"), INKSCAPE_ICON("dialog-path-effects"));
}
void PathArrayParam::on_visible_toggled(const Glib::ustring &path)
{
Gtk::TreeModel::iterator iter = _store->get_iter(path);
Gtk::TreeModel::Row row = *iter;
PathAndDirectionAndVisible *w = row[_model->_colObject];
row[_model->_colVisible] = !row[_model->_colVisible];
w->visibled = row[_model->_colVisible];
param_write_to_repr(param_getSVGValue().c_str());
DocumentUndo::done(param_effect->getSPDoc(), _("Toggle path parameter visibility"), INKSCAPE_ICON("dialog-path-effects"));
}
void PathArrayParam::param_set_default() {}
Gtk::Widget *PathArrayParam::param_newWidget()
{
Gtk::Box* vbox = Gtk::manage(new Gtk::Box(Gtk::ORIENTATION_VERTICAL));
Gtk::Box* hbox = Gtk::manage(new Gtk::Box(Gtk::ORIENTATION_HORIZONTAL));
_tree = nullptr;
_model = nullptr;
_scroller = nullptr;
initui();
vbox->pack_start(*_scroller, Gtk::PACK_EXPAND_WIDGET);
{ // Paste path to link button
Gtk::Image *pIcon = Gtk::manage(sp_get_icon_image("edit-clone", Gtk::ICON_SIZE_BUTTON));
Gtk::Button *pButton = Gtk::manage(new Gtk::Button());
pButton->set_relief(Gtk::RELIEF_NONE);
pIcon->show();
pButton->add(*pIcon);
pButton->show();
pButton->signal_clicked().connect(sigc::mem_fun(*this, &PathArrayParam::on_link_button_click));
hbox->pack_start(*pButton, Gtk::PACK_SHRINK);
pButton->set_tooltip_text(_("Link to path in clipboard"));
}
{ // Remove linked path
Gtk::Image *pIcon = Gtk::manage(sp_get_icon_image("list-remove", Gtk::ICON_SIZE_BUTTON));
Gtk::Button *pButton = Gtk::manage(new Gtk::Button());
pButton->set_relief(Gtk::RELIEF_NONE);
pIcon->show();
pButton->add(*pIcon);
pButton->show();
pButton->signal_clicked().connect(sigc::mem_fun(*this, &PathArrayParam::on_remove_button_click));
hbox->pack_start(*pButton, Gtk::PACK_SHRINK);
pButton->set_tooltip_text(_("Remove Path"));
}
{ // Move Down
Gtk::Image *pIcon = Gtk::manage(sp_get_icon_image("go-down", Gtk::ICON_SIZE_BUTTON));
Gtk::Button *pButton = Gtk::manage(new Gtk::Button());
pButton->set_relief(Gtk::RELIEF_NONE);
pIcon->show();
pButton->add(*pIcon);
pButton->show();
pButton->signal_clicked().connect(sigc::mem_fun(*this, &PathArrayParam::on_down_button_click));
hbox->pack_end(*pButton, Gtk::PACK_SHRINK);
pButton->set_tooltip_text(_("Move Down"));
}
{ // Move Down
Gtk::Image *pIcon = Gtk::manage(sp_get_icon_image("go-up", Gtk::ICON_SIZE_BUTTON));
Gtk::Button *pButton = Gtk::manage(new Gtk::Button());
pButton->set_relief(Gtk::RELIEF_NONE);
pIcon->show();
pButton->add(*pIcon);
pButton->show();
pButton->signal_clicked().connect(sigc::mem_fun(*this, &PathArrayParam::on_up_button_click));
hbox->pack_end(*pButton, Gtk::PACK_SHRINK);
pButton->set_tooltip_text(_("Move Up"));
}
vbox->pack_end(*hbox, Gtk::PACK_SHRINK);
vbox->show_all_children(true);
return vbox;
}
bool PathArrayParam::_selectIndex(const Gtk::TreeIter &iter, int *i)
{
if ((*i)-- <= 0) {
_tree->get_selection()->select(iter);
return true;
}
return false;
}
std::vector<SPObject *> PathArrayParam::param_get_satellites()
{
std::vector<SPObject *> objs;
for (auto &iter : _vector) {
if (iter && iter->ref.isAttached()) {
SPObject *obj = iter->ref.getObject();
if (obj) {
objs.push_back(obj);
}
}
}
return objs;
}
void PathArrayParam::on_up_button_click()
{
Gtk::TreeModel::iterator iter = _tree->get_selection()->get_selected();
if (iter) {
Gtk::TreeModel::Row row = *iter;
int i = -1;
std::vector<PathAndDirectionAndVisible*>::iterator piter = _vector.begin();
for (std::vector<PathAndDirectionAndVisible*>::iterator iter = _vector.begin(); iter != _vector.end(); piter = iter, i++, ++iter) {
if (*iter == row[_model->_colObject]) {
_vector.erase(iter);
_vector.insert(piter, row[_model->_colObject]);
break;
}
}
param_write_to_repr(param_getSVGValue().c_str());
DocumentUndo::done(param_effect->getSPDoc(), _("Move path up"), INKSCAPE_ICON("dialog-path-effects"));
_store->foreach_iter(sigc::bind<int *>(sigc::mem_fun(*this, &PathArrayParam::_selectIndex), &i));
}
}
void PathArrayParam::on_down_button_click()
{
Gtk::TreeModel::iterator iter = _tree->get_selection()->get_selected();
if (iter) {
Gtk::TreeModel::Row row = *iter;
int i = 0;
for (std::vector<PathAndDirectionAndVisible*>::iterator iter = _vector.begin(); iter != _vector.end(); i++, ++iter) {
if (*iter == row[_model->_colObject]) {
std::vector<PathAndDirectionAndVisible*>::iterator niter = _vector.erase(iter);
if (niter != _vector.end()) {
++niter;
i++;
}
_vector.insert(niter, row[_model->_colObject]);
break;
}
}
param_write_to_repr(param_getSVGValue().c_str());
DocumentUndo::done(param_effect->getSPDoc(), _("Move path down"), INKSCAPE_ICON("dialog-path-effects"));
_store->foreach_iter(sigc::bind<int *>(sigc::mem_fun(*this, &PathArrayParam::_selectIndex), &i));
}
}
void PathArrayParam::on_remove_button_click()
{
Gtk::TreeModel::iterator iter = _tree->get_selection()->get_selected();
if (iter) {
Gtk::TreeModel::Row row = *iter;
unlink(row[_model->_colObject]);
param_write_to_repr(param_getSVGValue().c_str());
DocumentUndo::done(param_effect->getSPDoc(), _("Remove path"), INKSCAPE_ICON("dialog-path-effects"));
}
}
void PathArrayParam::on_link_button_click()
{
Inkscape::UI::ClipboardManager *cm = Inkscape::UI::ClipboardManager::get();
std::vector<Glib::ustring> pathsid = cm->getElementsOfType(SP_ACTIVE_DESKTOP, "svg:path");
std::vector<Glib::ustring> textsid = cm->getElementsOfType(SP_ACTIVE_DESKTOP, "svg:text");
pathsid.insert(pathsid.end(), textsid.begin(), textsid.end());
if (pathsid.empty()) {
return;
}
bool foundOne = false;
Inkscape::SVGOStringStream os;
for (auto iter : _vector) {
if (foundOne) {
os << "|";
} else {
foundOne = true;
}
os << iter->href << "," << (iter->reversed ? "1" : "0") << "," << (iter->visibled ? "1" : "0");
}
for (auto pathid : pathsid) {
// add '#' at start to make it an uri.
pathid.insert(pathid.begin(), '#');
if (foundOne) {
os << "|";
} else {
foundOne = true;
}
os << pathid.c_str() << ",0,1";
}
param_write_to_repr(os.str().c_str());
DocumentUndo::done(param_effect->getSPDoc(), _("Link patharray parameter to path"), INKSCAPE_ICON("dialog-path-effects"));
}
void PathArrayParam::unlink(PathAndDirectionAndVisible *to)
{
to->linked_modified_connection.disconnect();
to->linked_delete_connection.disconnect();
to->ref.detach();
to->_pathvector = Geom::PathVector();
if (to->href) {
g_free(to->href);
to->href = nullptr;
}
for (std::vector<PathAndDirectionAndVisible*>::iterator iter = _vector.begin(); iter != _vector.end(); ++iter) {
if (*iter == to) {
PathAndDirectionAndVisible *w = *iter;
_vector.erase(iter);
delete w;
return;
}
}
}
void PathArrayParam::start_listening()
{
for (auto w : _vector) {
linked_changed(nullptr,w->ref.getObject(), w);
}
}
void PathArrayParam::linked_delete(SPObject * /*deleted*/, PathAndDirectionAndVisible * /*to*/)
{
// unlink(to);
param_write_to_repr(param_getSVGValue().c_str());
}
bool PathArrayParam::_updateLink(const Gtk::TreeIter &iter, PathAndDirectionAndVisible *pd)
{
Gtk::TreeModel::Row row = *iter;
if (row[_model->_colObject] == pd) {
SPObject *obj = pd->ref.getObject();
row[_model->_colLabel] = obj && obj->getId() ? ( obj->label() ? obj->label() : obj->getId() ) : pd->href;
return true;
}
return false;
}
void PathArrayParam::linked_changed(SPObject * /*old_obj*/, SPObject *new_obj, PathAndDirectionAndVisible *to)
{
to->linked_delete_connection.disconnect();
to->linked_modified_connection.disconnect();
if (new_obj && SP_IS_ITEM(new_obj)) {
to->linked_delete_connection = new_obj->connectDelete(
sigc::bind<PathAndDirectionAndVisible *>(sigc::mem_fun(*this, &PathArrayParam::linked_delete), to));
to->linked_modified_connection = new_obj->connectModified(
sigc::bind<PathAndDirectionAndVisible *>(sigc::mem_fun(*this, &PathArrayParam::linked_modified), to));
linked_modified(new_obj, SP_OBJECT_MODIFIED_FLAG, to);
} else {
to->_pathvector = Geom::PathVector();
param_effect->getLPEObj()->requestModified(SP_OBJECT_MODIFIED_FLAG);
if (_store.get()) {
_store->foreach_iter(
sigc::bind<PathAndDirectionAndVisible *>(sigc::mem_fun(*this, &PathArrayParam::_updateLink), to));
}
}
}
void PathArrayParam::setPathVector(SPObject *linked_obj, guint /*flags*/, PathAndDirectionAndVisible *to)
{
if (!to) {
return;
}
std::unique_ptr<SPCurve> curve;
SPText *text = dynamic_cast<SPText *>(linked_obj);
if (auto shape = dynamic_cast<SPShape const *>(linked_obj)) {
SPLPEItem * lpe_item = SP_LPE_ITEM(linked_obj);
if (_from_original_d) {
curve = SPCurve::copy(shape->curveForEdit());
} else if (_allow_only_bspline_spiro && lpe_item && lpe_item->hasPathEffect()){
curve = SPCurve::copy(shape->curveForEdit());
PathEffectList lpelist = lpe_item->getEffectList();
PathEffectList::iterator i;
for (i = lpelist.begin(); i != lpelist.end(); ++i) {
LivePathEffectObject *lpeobj = (*i)->lpeobject;
if (lpeobj) {
Inkscape::LivePathEffect::Effect *lpe = lpeobj->get_lpe();
if (dynamic_cast<Inkscape::LivePathEffect::LPEBSpline *>(lpe)) {
Geom::PathVector hp;
LivePathEffect::sp_bspline_do_effect(curve.get(), 0, hp);
} else if (dynamic_cast<Inkscape::LivePathEffect::LPESpiro *>(lpe)) {
LivePathEffect::sp_spiro_do_effect(curve.get());
}
}
}
} else {
curve = SPCurve::copy(shape->curve());
}
} else if (text) {
bool hidden = text->isHidden();
if (hidden) {
if (to->_pathvector.empty()) {
text->setHidden(false);
curve = text->getNormalizedBpath();
text->setHidden(true);
} else {
if (curve == nullptr) {
curve = std::make_unique<SPCurve>();
}
curve->set_pathvector(to->_pathvector);
}
} else {
curve = text->getNormalizedBpath();
}
}
if (curve == nullptr) {
// curve invalid, set empty pathvector
to->_pathvector = Geom::PathVector();
} else {
to->_pathvector = curve->get_pathvector();
}
}
void PathArrayParam::linked_modified(SPObject *linked_obj, guint flags, PathAndDirectionAndVisible *to)
{
if (!_updating && param_effect->getSPDoc()->isSensitive() && flags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG |
SP_OBJECT_CHILD_MODIFIED_FLAG | SP_OBJECT_VIEWPORT_MODIFIED_FLAG))
{
if (!to) {
return;
}
setPathVector(linked_obj, flags, to);
param_effect->getLPEObj()->requestModified(SP_OBJECT_MODIFIED_FLAG);
if (_store.get()) {
_store->foreach_iter(
sigc::bind<PathAndDirectionAndVisible *>(sigc::mem_fun(*this, &PathArrayParam::_updateLink), to));
}
}
}
bool PathArrayParam::param_readSVGValue(const gchar *strvalue)
{
if (strvalue) {
while (!_vector.empty()) {
PathAndDirectionAndVisible *w = _vector.back();
unlink(w);
}
if (_store.get()) {
_store->clear();
}
gchar ** strarray = g_strsplit(strvalue, "|", 0);
bool write = false;
for (gchar ** iter = strarray; *iter != nullptr; iter++) {
if ((*iter)[0] == '#') {
gchar ** substrarray = g_strsplit(*iter, ",", 0);
SPObject * old_ref = param_effect->getSPDoc()->getObjectByHref(*substrarray);
if (old_ref) {
SPObject * successor = old_ref->_successor;
Glib::ustring id = *substrarray;
if (successor) {
id = successor->getId();
id.insert(id.begin(), '#');
write = true;
}
*(substrarray) = g_strdup(id.c_str());
}
PathAndDirectionAndVisible* w = new PathAndDirectionAndVisible((SPObject *)param_effect->getLPEObj());
w->href = g_strdup(*substrarray);
w->reversed = *(substrarray+1) != nullptr && (*(substrarray+1))[0] == '1';
//Like this to make backwards compatible, new value added in 0.93
w->visibled = *(substrarray+2) == nullptr || (*(substrarray+2))[0] == '1';
w->linked_changed_connection = w->ref.changedSignal().connect(
sigc::bind<PathAndDirectionAndVisible *>(sigc::mem_fun(*this, &PathArrayParam::linked_changed), w));
w->ref.attach(URI(w->href));
_vector.push_back(w);
if (_store.get()) {
Gtk::TreeModel::iterator iter = _store->append();
Gtk::TreeModel::Row row = *iter;
SPObject *obj = w->ref.getObject();
row[_model->_colObject] = w;
row[_model->_colLabel] = obj ? ( obj->label() ? obj->label() : obj->getId() ) : w->href;
row[_model->_colReverse] = w->reversed;
row[_model->_colVisible] = w->visibled;
}
g_strfreev (substrarray);
}
}
g_strfreev (strarray);
if (write) {
auto full = param_getSVGValue();
param_write_to_repr(full.c_str());
}
return true;
}
return false;
}
Glib::ustring PathArrayParam::param_getSVGValue() const
{
Inkscape::SVGOStringStream os;
bool foundOne = false;
for (auto iter : _vector) {
if (foundOne) {
os << "|";
} else {
foundOne = true;
}
os << iter->href << "," << (iter->reversed ? "1" : "0") << "," << (iter->visibled ? "1" : "0");
}
return os.str();
}
Glib::ustring PathArrayParam::param_getDefaultSVGValue() const
{
return "";
}
void PathArrayParam::update()
{
for (auto & iter : _vector) {
SPObject *linked_obj = iter->ref.getObject();
linked_modified(linked_obj, SP_OBJECT_MODIFIED_FLAG, iter);
}
}
} /* namespace LivePathEffect */
} /* 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 :
|