Code

break apart faster: prepend to list instead of append, add objects to selection whole...
[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  *
10  * Copyright (C) 1999-2004 Authors
11  * Copyright (C) 2001-2002 Ximian, Inc.
12  *
13  * Released under GNU GPL, read the file 'COPYING' for more information
14  */
16 #ifdef HAVE_CONFIG_H
17 # include <config.h>
18 #endif
19 #include "xml/repr.h"
20 #include "svg/svg.h"
21 #include "display/curve.h"
22 #include <glibmm/i18n.h>
23 #include "sp-path.h"
24 #include "sp-text.h"
25 #include "sp-flowtext.h"
26 #include "libnr/nr-path.h"
27 #include "text-editing.h"
28 #include "style.h"
29 #include "inkscape.h"
30 #include "document.h"
31 #include "message-stack.h"
32 #include "selection.h"
33 #include "desktop-handles.h"
35 /* Helper functions for sp_selected_path_to_curves */
36 static void sp_selected_path_to_curves0(gboolean do_document_done, guint32 text_grouping_policy);
37 static Inkscape::XML::Node *sp_selected_item_to_curved_repr(SPItem *item, guint32 text_grouping_policy);
38 enum {
39     /* Not used yet. This is the placeholder of Lauris's idea. */
40     SP_TOCURVE_INTERACTIVE       = 1 << 0,
41     SP_TOCURVE_GROUPING_BY_WORD  = 1 << 1,
42     SP_TOCURVE_GROUPING_BY_LINE  = 1 << 2,
43     SP_TOCURVE_GROUPING_BY_WHOLE = 1 << 3
44 };
46 void
47 sp_selected_path_combine(void)
48 {
49     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
51     Inkscape::Selection *selection = sp_desktop_selection(desktop);
52     GSList *items = (GSList *) selection->itemList();
54     if (g_slist_length(items) < 2) {
55         sp_desktop_message_stack(desktop)->flash(Inkscape::WARNING_MESSAGE, _("Select <b>at least two objects</b> to combine."));
56         return;
57     }
59     for (GSList *i = items; i != NULL; i = i->next) {
60         SPItem *item = (SPItem *) i->data;
61         if (!SP_IS_SHAPE(item) && !SP_IS_TEXT(item)) {
62             sp_desktop_message_stack(desktop)->flash(Inkscape::WARNING_MESSAGE, _("At least one of the objects is <b>not a path</b>, cannot combine."));
63             return;
64         }
65     }
67     Inkscape::XML::Node *parent = SP_OBJECT_REPR((SPItem *) items->data)->parent();
68     for (GSList *i = items; i != NULL; i = i->next) {
69         if ( SP_OBJECT_REPR((SPItem *) i->data)->parent() != parent ) {
70             sp_desktop_message_stack(desktop)->flash(Inkscape::ERROR_MESSAGE, _("You cannot combine objects from <b>different groups</b> or <b>layers</b>."));
71             return;
72         }
73     }
75     sp_selected_path_to_curves0(FALSE, 0);
77     items = (GSList *) selection->itemList();
79     items = g_slist_copy(items);
81     items = g_slist_sort(items, (GCompareFunc) sp_item_repr_compare_position);
83     // remember the position of the topmost object
84     gint topmost = (SP_OBJECT_REPR((SPItem *) g_slist_last(items)->data))->position();
86     // remember the id of the bottomost object
87     char const *id = SP_OBJECT_REPR((SPItem *) items->data)->attribute("id");
89     // FIXME: merge styles of combined objects instead of using the first one's style
90     gchar *style = g_strdup(SP_OBJECT_REPR((SPItem *) items->data)->attribute("style"));
92     GString *dstring = g_string_new("");
93     for (GSList *i = items; i != NULL; i = i->next) {
95         SPPath *path = (SPPath *) i->data;
96         SPCurve *c = sp_shape_get_curve(SP_SHAPE(path));
98         NArtBpath *abp = nr_artpath_affine(SP_CURVE_BPATH(c), SP_ITEM(path)->transform);
99         sp_curve_unref(c);
100         gchar *str = sp_svg_write_path(abp);
101         nr_free(abp);
103         dstring = g_string_append(dstring, str);
104         g_free(str);
106         // if this is the bottommost object,
107         if (!strcmp(SP_OBJECT_REPR(path)->attribute("id"), id)) {
108             // delete it so that its clones don't get alerted; this object will be restored shortly, with the same id
109             SP_OBJECT(path)->deleteObject(false);
110         } else {
111             // delete the object for real, so that its clones can take appropriate action
112             SP_OBJECT(path)->deleteObject();
113         }
115         topmost--;
116     }
118     g_slist_free(items);
120     Inkscape::XML::Node *repr = sp_repr_new("svg:path");
122     // restore id
123     repr->setAttribute("id", id);
125     repr->setAttribute("style", style);
126     g_free(style);
128     repr->setAttribute("d", dstring->str);
129     g_string_free(dstring, TRUE);
131     // add the new group to the group members' common parent
132     parent->appendChild(repr);
134     // move to the position of the topmost, reduced by the number of deleted items
135     repr->setPosition(topmost > 0 ? topmost + 1 : 0);
137     sp_document_done(sp_desktop_document(desktop));
139     selection->set(repr);
141     Inkscape::GC::release(repr);
144 void
145 sp_selected_path_break_apart(void)
147     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
149     Inkscape::Selection *selection = sp_desktop_selection(desktop);
151     if (selection->isEmpty()) {
152         sp_desktop_message_stack(desktop)->flash(Inkscape::WARNING_MESSAGE, _("Select <b>path(s)</b> to break apart."));
153         return;
154     }
156     bool did = false;
158     for (GSList *items = g_slist_copy((GSList *) selection->itemList());
159          items != NULL;
160          items = items->next) {
162         SPItem *item = (SPItem *) items->data;
164         if (!SP_IS_PATH(item))
165             continue;
167         SPPath *path = SP_PATH(item);
169         SPCurve *curve = sp_shape_get_curve(SP_SHAPE(path));
170         if (curve == NULL)
171             continue;
173         did = true;
175         Inkscape::XML::Node *parent = SP_OBJECT_REPR(item)->parent();
176         gint pos = SP_OBJECT_REPR(item)->position();
177         char const *id = SP_OBJECT_REPR(item)->attribute("id");
179         gchar *style = g_strdup(SP_OBJECT(item)->repr->attribute("style"));
181         NArtBpath *abp = nr_artpath_affine(SP_CURVE_BPATH(curve), (SP_ITEM(path))->transform);
183         sp_curve_unref(curve);
185         // it's going to resurrect as one of the pieces, so we delete without advertisement
186         SP_OBJECT(item)->deleteObject(false);
188         curve = sp_curve_new_from_bpath(abp);
189         g_assert(curve != NULL);
191         GSList *list = sp_curve_split(curve);
193         sp_curve_unref(curve);
195         GSList *reprs = NULL;
196         for (GSList *l = list; l != NULL; l = l->next) {
197             curve = (SPCurve *) l->data;
199             Inkscape::XML::Node *repr = sp_repr_new("svg:path");
200             repr->setAttribute("style", style);
202             gchar *str = sp_svg_write_path(SP_CURVE_BPATH(curve));
203             repr->setAttribute("d", str);
204             g_free(str);
206             // add the new repr to the parent
207             parent->appendChild(repr);
209             // move to the saved position
210             repr->setPosition(pos > 0 ? pos : 0);
212             // if it's the first one, restore id
213             if (l == list)
214                 repr->setAttribute("id", id);
216             reprs = g_slist_prepend (reprs, repr);
218             Inkscape::GC::release(repr);
219         }
221         selection->setReprList(reprs);
223         g_slist_free(reprs);
224         g_slist_free(list);
225         g_free(style);
227     }
229     if (did) {
230         sp_document_done(sp_desktop_document(desktop));
231     } else {
232         sp_desktop_message_stack(desktop)->flash(Inkscape::ERROR_MESSAGE, _("<b>No path(s)</b> to break apart in the selection."));
233         return;
234     }
237 /* This function is an entry point from GUI */
238 void
239 sp_selected_path_to_curves(void)
241     sp_selected_path_to_curves0(TRUE, SP_TOCURVE_INTERACTIVE);
244 static void
245 sp_selected_path_to_curves0(gboolean interactive, guint32 text_grouping_policy)
247     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
249     Inkscape::Selection *selection = sp_desktop_selection(desktop);
251     if (selection->isEmpty()) {
252         if (interactive)
253             sp_desktop_message_stack(desktop)->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to convert to path."));
254         return;
255     }
257     bool did = false;
259     for (GSList *items = g_slist_copy((GSList *) selection->itemList());
260          items != NULL;
261          items = items->next) {
263         SPItem *item = SP_ITEM(items->data);
265         Inkscape::XML::Node *repr = sp_selected_item_to_curved_repr(item, 0);
266         if (!repr)
267             continue;
269         did = true;
271         // remember the position of the item
272         gint pos = SP_OBJECT_REPR(item)->position();
273         // remember parent
274         Inkscape::XML::Node *parent = SP_OBJECT_REPR(item)->parent();
275         // remember id
276         char const *id = SP_OBJECT_REPR(item)->attribute("id");
278         selection->remove(item);
280         // it's going to resurrect, so we delete without advertisement
281         SP_OBJECT(item)->deleteObject(false);
283         // restore id
284         repr->setAttribute("id", id);
285         // add the new repr to the parent
286         parent->appendChild(repr);
287         // move to the saved position
288         repr->setPosition(pos > 0 ? pos : 0);
290         selection->add(repr);
291         Inkscape::GC::release(repr);
292     }
294     if (interactive) {
295         if (did) {
296             sp_document_done(sp_desktop_document(desktop));
297         } else {
298             sp_desktop_message_stack(desktop)->flash(Inkscape::ERROR_MESSAGE, _("<b>No objects</b> to convert to path in the selection."));
299             return;
300         }
301     }
304 static Inkscape::XML::Node *
305 sp_selected_item_to_curved_repr(SPItem *item, guint32 text_grouping_policy)
307     if (!item)
308         return NULL;
310     SPCurve *curve = NULL;
311     if (SP_IS_SHAPE(item)) {
312         curve = sp_shape_get_curve(SP_SHAPE(item));
313     } else if (SP_IS_TEXT(item) || SP_IS_FLOWTEXT(item)) {
314         curve = te_get_layout(item)->convertToCurves();
315     }
317     if (!curve)
318         return NULL;
320     Inkscape::XML::Node *repr = sp_repr_new("svg:path");
321     /* Transformation */
322     repr->setAttribute("transform", SP_OBJECT_REPR(item)->attribute("transform"));
323     /* Style */
324     gchar *style_str = sp_style_write_difference(SP_OBJECT_STYLE(item),
325                                                  SP_OBJECT_STYLE(SP_OBJECT_PARENT(item)));
326     repr->setAttribute("style", style_str);
327     g_free(style_str);
328     /* Rotation center */
329     sp_repr_set_attr(repr, "inkscape:transform-center-x", SP_OBJECT_REPR(item)->attribute("inkscape:transform-center-x"));
330     sp_repr_set_attr(repr, "inkscape:transform-center-y", SP_OBJECT_REPR(item)->attribute("inkscape:transform-center-y"));
332     /* Definition */
333     gchar *def_str = sp_svg_write_path(SP_CURVE_BPATH(curve));
334     repr->setAttribute("d", def_str);
335     g_free(def_str);
336     sp_curve_unref(curve);
337     return repr;
340 void
341 sp_selected_path_reverse()
343     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
345     Inkscape::Selection *selection = sp_desktop_selection(desktop);
346     GSList *items = (GSList *) selection->itemList();
348     if (!items) {
349         sp_desktop_message_stack(desktop)->flash(Inkscape::WARNING_MESSAGE, _("Select <b>path(s)</b> to reverse."));
350         return;
351     }
354     bool did = false;
355     for (GSList *i = items; i != NULL; i = i->next) {
357         if (!SP_IS_SHAPE(i->data))
358             continue;
360         did = true;
361         SPShape *shape = SP_SHAPE(i->data);
363         SPCurve *rcurve = sp_curve_reverse(shape->curve);
365         gchar *str = sp_svg_write_path(SP_CURVE_BPATH(rcurve));
366         SP_OBJECT_REPR(shape)->setAttribute("d", str);
367         g_free(str);
369         sp_curve_unref(rcurve);
370     }
372     if (did) {
373         sp_document_done(sp_desktop_document(desktop));
374     } else {
375         sp_desktop_message_stack(desktop)->flash(Inkscape::ERROR_MESSAGE, _("<b>No paths</b> to reverse in the selection."));
376     }
379 /*
380   Local Variables:
381   mode:c++
382   c-file-style:"stroustrup"
383   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
384   indent-tabs-mode:nil
385   fill-column:99
386   End:
387 */
388 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :