Code

Fix for Bug 238113 (preserve title/desc for various Inkscape operations)
[inkscape.git] / src / path-chemistry.cpp
1 #define __SP_PATH_CHEMISTRY_C__
3 /*
4  * Here are handlers for modifying selections, specific to paths
5  *
6  * Authors:
7  *   Lauris Kaplinski <lauris@kaplinski.com>
8  *   bulia byak <buliabyak@users.sf.net>
9  *   Jasper van de Gronde <th.v.d.gronde@hccnet.nl>
10  *
11  * Copyright (C) 1999-2008 Authors
12  * Copyright (C) 2001-2002 Ximian, Inc.
13  *
14  * Released under GNU GPL, read the file 'COPYING' for more information
15  */
17 #ifdef HAVE_CONFIG_H
18 # include <config.h>
19 #endif
20 #include <cstring>
21 #include <string>
22 #include "xml/repr.h"
23 #include "svg/svg.h"
24 #include "display/curve.h"
25 #include <glib/gmem.h>
26 #include <glibmm/i18n.h>
27 #include "sp-path.h"
28 #include "sp-text.h"
29 #include "sp-flowtext.h"
30 #include "libnr/nr-path.h"
31 #include "text-editing.h"
32 #include "style.h"
33 #include "desktop.h"
34 #include "document.h"
35 #include "message-stack.h"
36 #include "selection.h"
37 #include "desktop-handles.h"
38 #include "box3d.h"
39 #include <2geom/pathvector.h>
40 #include "path-chemistry.h"
42 /* Helper functions for sp_selected_path_to_curves */
43 static void sp_selected_path_to_curves0(SPDesktop *desktop, bool do_document_done, guint32 text_grouping_policy);
44 static bool sp_item_list_to_curves(const GSList *items, GSList **selected, GSList **to_select);
46 enum {
47     /* Not used yet. This is the placeholder of Lauris's idea. */
48     SP_TOCURVE_INTERACTIVE       = 1 << 0,
49     SP_TOCURVE_GROUPING_BY_WORD  = 1 << 1,
50     SP_TOCURVE_GROUPING_BY_LINE  = 1 << 2,
51     SP_TOCURVE_GROUPING_BY_WHOLE = 1 << 3
52 };
54 void
55 sp_selected_path_combine(SPDesktop *desktop)
56 {
57     Inkscape::Selection *selection = sp_desktop_selection(desktop);
58     SPDocument *doc = sp_desktop_document(desktop);
59     
60     if (g_slist_length((GSList *) selection->itemList()) < 2) {
61         sp_desktop_message_stack(desktop)->flash(Inkscape::WARNING_MESSAGE, _("Select <b>at least two objects</b> to combine."));
62         return;
63     }
65     desktop->messageStack()->flash(Inkscape::IMMEDIATE_MESSAGE, _("Combining paths..."));
66     // set "busy" cursor
67     desktop->setWaitingCursor();
69     GSList *items = g_slist_copy((GSList *) selection->itemList());
70     GSList *to_paths = NULL;
71     for (GSList *i = items; i != NULL; i = i->next) {
72         SPItem *item = (SPItem *) i->data;
73         if (!SP_IS_PATH(item))
74             to_paths = g_slist_prepend(to_paths, item);
75     }
76     GSList *converted = NULL;
77     bool did = sp_item_list_to_curves(to_paths, &items, &converted);
78     g_slist_free(to_paths);
79     for (GSList *i = converted; i != NULL; i = i->next)
80         items = g_slist_prepend(items, doc->getObjectByRepr((Inkscape::XML::Node*)(i->data)));
82     items = g_slist_sort(items, (GCompareFunc) sp_item_repr_compare_position);
83     items = g_slist_reverse(items);
85     // remember the position, id, transform and style of the topmost path, they will be assigned to the combined one
86     gint position = 0;
87     char const *id = NULL;
88     char const *transform = NULL;
89     gchar *style = NULL;
90     gchar *path_effect = NULL;
92     SPCurve* curve = 0;
93     SPItem *first = NULL;
94     Inkscape::XML::Node *parent = NULL; 
96     for (GSList *i = items; i != NULL; i = i->next) {  // going from top to bottom
98         SPItem *item = (SPItem *) i->data;
99         if (!SP_IS_PATH(item))
100             continue;
101         did = true;
103         SPCurve *c = sp_path_get_curve_for_edit(SP_PATH(item));
104         if (first == NULL) {  // this is the topmost path
105             first = item;
106             parent = SP_OBJECT_REPR(first)->parent();
107             position = SP_OBJECT_REPR(first)->position();
108             id = SP_OBJECT_REPR(first)->attribute("id");
109             transform = SP_OBJECT_REPR(first)->attribute("transform");
110             // FIXME: merge styles of combined objects instead of using the first one's style
111             style = g_strdup(SP_OBJECT_REPR(first)->attribute("style"));
112             path_effect = g_strdup(SP_OBJECT_REPR(first)->attribute("inkscape:path-effect"));
113             //c->transform(item->transform);
114             curve = c;
115         } else {
116             c->transform(item->getRelativeTransform(SP_OBJECT(first)));
117             curve->append(c, false);
118             c->unref();
119         }
121         // unless this is the topmost object,
122         if (item != first) {
123             // reduce position only if the same parent
124             if (SP_OBJECT_REPR(item)->parent() == parent)
125                 position--;
126             // delete the object for real, so that its clones can take appropriate action
127             SP_OBJECT(item)->deleteObject();
128         }
129     }
131     g_slist_free(items);
133     if (did) {
134         selection->clear();
136         // delete the topmost one so that its clones don't get alerted; this object will be
137         // restored shortly, with the same id
138         SP_OBJECT(first)->deleteObject(false);
140         Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
141         Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
143         // restore id, transform, path effect, and style
144         repr->setAttribute("id", id);
145         if (transform) repr->setAttribute("transform", transform);
146         repr->setAttribute("style", style);
147         g_free(style);
149         // set path data corresponding to new curve
150         gchar *dstring = sp_svg_write_path(curve->get_pathvector());
151         curve->unref();
152         repr->setAttribute("d", dstring);
153         if (path_effect)
154             repr->setAttribute("inkscape:original-d", dstring);
155         g_free(dstring);
157         repr->setAttribute("inkscape:path-effect", path_effect);
158         g_free(path_effect);
160         // add the new group to the parent of the topmost
161         parent->appendChild(repr);
163         // move to the position of the topmost, reduced by the number of deleted items
164         repr->setPosition(position > 0 ? position : 0);
166         sp_document_done(sp_desktop_document(desktop), SP_VERB_SELECTION_COMBINE, 
167                          _("Combine"));
169         selection->set(repr);
171         Inkscape::GC::release(repr);
173     } else {
174         sp_desktop_message_stack(desktop)->flash(Inkscape::ERROR_MESSAGE, _("<b>No path(s)</b> to combine in the selection."));
175     }
177     desktop->clearWaitingCursor();
180 void
181 sp_selected_path_break_apart(SPDesktop *desktop)
183     Inkscape::Selection *selection = sp_desktop_selection(desktop);
185     if (selection->isEmpty()) {
186         sp_desktop_message_stack(desktop)->flash(Inkscape::WARNING_MESSAGE, _("Select <b>path(s)</b> to break apart."));
187         return;
188     }
190     desktop->messageStack()->flash(Inkscape::IMMEDIATE_MESSAGE, _("Breaking apart paths..."));
191     // set "busy" cursor
192     desktop->setWaitingCursor();
194     bool did = false;
196     for (GSList *items = g_slist_copy((GSList *) selection->itemList());
197          items != NULL;
198          items = items->next) {
200         SPItem *item = (SPItem *) items->data;
202         if (!SP_IS_PATH(item))
203             continue;
205         SPPath *path = SP_PATH(item);
207         SPCurve *curve = sp_path_get_curve_for_edit(SP_PATH(path));
208         if (curve == NULL)
209             continue;
211         did = true;
213         Inkscape::XML::Node *parent = SP_OBJECT_REPR(item)->parent();
214         gint pos = SP_OBJECT_REPR(item)->position();
215         char const *id = SP_OBJECT_REPR(item)->attribute("id");
217         gchar *style = g_strdup(SP_OBJECT(item)->repr->attribute("style"));
218         gchar *path_effect = g_strdup(SP_OBJECT(item)->repr->attribute("inkscape:path-effect"));
220         Geom::PathVector apv = curve->get_pathvector() * SP_ITEM(path)->transform;
222         curve->unref();
224         // it's going to resurrect as one of the pieces, so we delete without advertisement
225         SP_OBJECT(item)->deleteObject(false);
227         curve = new SPCurve(apv);
228         g_assert(curve != NULL);
230         GSList *list = curve->split();
232         curve->unref();
234         GSList *reprs = NULL;
235         for (GSList *l = list; l != NULL; l = l->next) {
236             curve = (SPCurve *) l->data;
238             Inkscape::XML::Node *repr = parent->document()->createElement("svg:path");
239             repr->setAttribute("style", style);
241             gchar *str = sp_svg_write_path(curve->get_pathvector());
242             repr->setAttribute("d", str);
243             if (path_effect)
244                 repr->setAttribute("inkscape:original-d", str);
245             g_free(str);
247             repr->setAttribute("inkscape:path-effect", path_effect);
249             // add the new repr to the parent
250             parent->appendChild(repr);
252             // move to the saved position
253             repr->setPosition(pos > 0 ? pos : 0);
255             // if it's the first one, restore id
256             if (l == list)
257                 repr->setAttribute("id", id);
259             reprs = g_slist_prepend (reprs, repr);
261             Inkscape::GC::release(repr);
262         }
264         selection->setReprList(reprs);
266         g_slist_free(reprs);
267         g_slist_free(list);
268         g_free(style);
270     }
272     desktop->clearWaitingCursor();
274     if (did) {
275         sp_document_done(sp_desktop_document(desktop), SP_VERB_SELECTION_BREAK_APART, 
276                          _("Break apart"));
277     } else {
278         sp_desktop_message_stack(desktop)->flash(Inkscape::ERROR_MESSAGE, _("<b>No path(s)</b> to break apart in the selection."));
279     }
282 /* This function is an entry point from GUI */
283 void
284 sp_selected_path_to_curves(SPDesktop *desktop, bool interactive)
286     sp_selected_path_to_curves0(desktop, interactive, interactive ? SP_TOCURVE_INTERACTIVE : 0);
289 static void
290 sp_selected_path_to_curves0(SPDesktop *desktop, bool interactive, guint32 /*text_grouping_policy*/)
292     Inkscape::Selection *selection = sp_desktop_selection(desktop);
294     if (selection->isEmpty()) {
295         if (interactive)
296             sp_desktop_message_stack(desktop)->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to convert to path."));
297         return;
298     }
300     bool did = false;
301     if (interactive) {
302         desktop->messageStack()->flash(Inkscape::IMMEDIATE_MESSAGE, _("Converting objects to paths..."));
303         // set "busy" cursor
304         desktop->setWaitingCursor();
305     }
307     GSList *selected = g_slist_copy((GSList *) selection->itemList());
308     GSList *to_select = NULL;
309     selection->clear();
310     GSList *items = g_slist_copy(selected);
312     did = sp_item_list_to_curves(items, &selected, &to_select);
314     g_slist_free (items);
315     selection->setReprList(to_select);
316     selection->addList(selected);
317     g_slist_free (to_select);
318     g_slist_free (selected);
320     if (interactive) {
321         desktop->clearWaitingCursor();
322         if (did) {
323             sp_document_done(sp_desktop_document(desktop), SP_VERB_OBJECT_TO_CURVE, 
324                              _("Object to path"));
325         } else {
326             sp_desktop_message_stack(desktop)->flash(Inkscape::ERROR_MESSAGE, _("<b>No objects</b> to convert to path in the selection."));
327             return;
328         }
329     }
332 static bool
333 sp_item_list_to_curves(const GSList *items, GSList **selected, GSList **to_select)
335     bool did = false;
336     
337     for (;
338          items != NULL;
339          items = items->next) {
341         SPItem *item = SP_ITEM(items->data);
342         SPDocument *document = item->document;
344         if (SP_IS_PATH(item) && !SP_PATH(item)->original_curve) {
345             continue; // already a path, and no path effect
346         }
348         if (SP_IS_BOX3D(item)) {
349             // convert 3D box to ordinary group of paths; replace the old element in 'selected' with the new group
350             Inkscape::XML::Node *repr = SP_OBJECT_REPR(box3d_convert_to_group(SP_BOX3D(item)));
351             
352             if (repr) {
353                 *to_select = g_slist_prepend (*to_select, repr);
354                 did = true;
355                 *selected = g_slist_remove (*selected, item);
356             }
358             continue;
359         }
360         
361         if (SP_IS_GROUP(item)) {
362             sp_lpe_item_remove_all_path_effects(SP_LPE_ITEM(item), true);
363             GSList *item_list = sp_item_group_item_list(SP_GROUP(item));
364             
365             GSList *item_to_select = NULL;
366             GSList *item_selected = NULL;
367             
368             if (sp_item_list_to_curves(item_list, &item_selected, &item_to_select))
369                 did = true;
371             g_slist_free(item_list);
372             g_slist_free(item_to_select);
373             g_slist_free(item_selected);
375             continue;
376         }
378         Inkscape::XML::Node *repr = sp_selected_item_to_curved_repr(item, 0);
379         if (!repr)
380             continue;
382         did = true;
383         *selected = g_slist_remove (*selected, item);
385         // remember the position of the item
386         gint pos = SP_OBJECT_REPR(item)->position();
387         // remember parent
388         Inkscape::XML::Node *parent = SP_OBJECT_REPR(item)->parent();
389         // remember id
390         char const *id = SP_OBJECT_REPR(item)->attribute("id");
391         // remember title
392         gchar *title = item->title();
393         // remember description
394         gchar *desc = item->desc();
396         // It's going to resurrect, so we delete without notifying listeners.
397         SP_OBJECT(item)->deleteObject(false);
399         // restore id
400         repr->setAttribute("id", id);
401         // add the new repr to the parent
402         parent->appendChild(repr);
403         SPObject* newObj = document->getObjectByRepr(repr);
404         if (title && newObj) {
405                 newObj->setTitle(title);
406                 g_free(title);
407         }
408         if (desc && newObj) {
409                 newObj->setDesc(desc);
410                 g_free(desc);
411         }
413         // move to the saved position
414         repr->setPosition(pos > 0 ? pos : 0);
416         /* Buglet: We don't re-add the (new version of the) object to the selection of any other
417          * desktops where it was previously selected. */
418         *to_select = g_slist_prepend (*to_select, repr);
419         Inkscape::GC::release(repr);
420     }
421     
422     return did;
425 Inkscape::XML::Node *
426 sp_selected_item_to_curved_repr(SPItem *item, guint32 /*text_grouping_policy*/)
428     if (!item)
429         return NULL;
431     SPCurve *curve = NULL;
432     if (SP_IS_SHAPE(item)) {
433         curve = sp_shape_get_curve(SP_SHAPE(item));
434     } else if (SP_IS_TEXT(item) || SP_IS_FLOWTEXT(item)) {
435         curve = te_get_layout(item)->convertToCurves();
436     }
438     if (!curve)
439         return NULL;
441     // Prevent empty paths from being added to the document
442     // otherwise we end up with zomby markup in the SVG file
443     if(curve->is_empty())
444     {
445         curve->unref();
446         return NULL;
447     }
449     Inkscape::XML::Document *xml_doc = SP_OBJECT_REPR(item)->document();
450     Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
451     /* Transformation */
452     repr->setAttribute("transform", SP_OBJECT_REPR(item)->attribute("transform"));
453     /* Style */
454     gchar *style_str = sp_style_write_difference(SP_OBJECT_STYLE(item),
455                                                  SP_OBJECT_STYLE(SP_OBJECT_PARENT(item)));
456     repr->setAttribute("style", style_str);
457     g_free(style_str);
459     /* Mask */
460     gchar *mask_str = (gchar *) SP_OBJECT_REPR(item)->attribute("mask");
461     if ( mask_str )
462         repr->setAttribute("mask", mask_str);
464     /* Clip path */
465     gchar *clip_path_str = (gchar *) SP_OBJECT_REPR(item)->attribute("clip-path");
466     if ( clip_path_str )
467         repr->setAttribute("clip-path", clip_path_str);
469     /* Rotation center */
470     repr->setAttribute("inkscape:transform-center-x", SP_OBJECT_REPR(item)->attribute("inkscape:transform-center-x"), false);
471     repr->setAttribute("inkscape:transform-center-y", SP_OBJECT_REPR(item)->attribute("inkscape:transform-center-y"), false);
473     /* Definition */
474     gchar *def_str = sp_svg_write_path(curve->get_pathvector());
475     repr->setAttribute("d", def_str);
476     g_free(def_str);
477     curve->unref();
478     return repr;
482 // FIXME: THIS DOES NOT REVERSE THE NODETYPES ORDER!
483 void
484 sp_selected_path_reverse(SPDesktop *desktop)
486     Inkscape::Selection *selection = sp_desktop_selection(desktop);
487     GSList *items = (GSList *) selection->itemList();
489     if (!items) {
490         sp_desktop_message_stack(desktop)->flash(Inkscape::WARNING_MESSAGE, _("Select <b>path(s)</b> to reverse."));
491         return;
492     }
495     // set "busy" cursor
496     desktop->setWaitingCursor();
498     bool did = false;
499     desktop->messageStack()->flash(Inkscape::IMMEDIATE_MESSAGE, _("Reversing paths..."));
501     for (GSList *i = items; i != NULL; i = i->next) {
503         if (!SP_IS_PATH(i->data))
504             continue;
506         did = true;
507         SPPath *path = SP_PATH(i->data);
509         SPCurve *rcurve = sp_path_get_curve_reference(path)->create_reverse();
511         gchar *str = sp_svg_write_path(rcurve->get_pathvector());
512         if ( sp_lpe_item_has_path_effect_recursive(SP_LPE_ITEM(path)) ) {
513             SP_OBJECT_REPR(path)->setAttribute("inkscape:original-d", str);
514         } else {
515             SP_OBJECT_REPR(path)->setAttribute("d", str);
516         }
517         g_free(str);
519         rcurve->unref();
520     }
522     desktop->clearWaitingCursor();
524     if (did) {
525         sp_document_done(sp_desktop_document(desktop), SP_VERB_SELECTION_REVERSE,
526                          _("Reverse path"));
527     } else {
528         sp_desktop_message_stack(desktop)->flash(Inkscape::ERROR_MESSAGE, _("<b>No paths</b> to reverse in the selection."));
529     }
532 /*
533   Local Variables:
534   mode:c++
535   c-file-style:"stroustrup"
536   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
537   indent-tabs-mode:nil
538   fill-column:99
539   End:
540 */
541 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :