~moduli16/inkscape/recolor

« back to all changes in this revision

Viewing changes to src/selection-chemistry.cpp

  • Committer: tavmjong-free
  • Date: 2012-10-11 17:54:14 UTC
  • Revision ID: tavmjong@free.fr-20121011175414-j1i7huls4h70n91n
Add symbols dialog. See: http://wiki.inkscape.org/wiki/index.php/SymbolsDialog

Show diffs side-by-side

added added

removed removed

Lines of Context:
11
11
 *   Martin Sucha <martin.sucha-inkscape@jts-sro.sk>
12
12
 *   Abhishek Sharma
13
13
 *   Kris De Gussem <Kris.DeGussem@gmail.com>
 
14
 *   Tavmjong Bah <tavmjong@free.fr> (Symbol additions)
14
15
 *
15
16
 * Copyright (C) 1999-2010,2012 authors
16
17
 * Copyright (C) 2001-2002 Ximian, Inc.
71
72
#include "sp-gradient-reference.h"
72
73
#include "sp-linear-gradient-fns.h"
73
74
#include "sp-pattern.h"
 
75
#include "sp-symbol.h"
74
76
#include "sp-radial-gradient-fns.h"
75
77
#include "gradient-context.h"
76
78
#include "sp-namedview.h"
2887
2889
    DocumentUndo::done(doc, SP_VERB_EDIT_SELECTION_2_GUIDES, _("Objects to guides"));
2888
2890
}
2889
2891
 
 
2892
/*
 
2893
 * Convert <g> to <symbol>, leaving all <use> elements referencing group unchanged.
 
2894
 */
 
2895
void sp_selection_symbol(SPDesktop *desktop, bool apply )
 
2896
{
 
2897
 
 
2898
    if (desktop == NULL) {
 
2899
        return;
 
2900
    }
 
2901
 
 
2902
    SPDocument *doc = sp_desktop_document(desktop);
 
2903
    Inkscape::XML::Document *xml_doc = doc->getReprDoc();
 
2904
 
 
2905
    Inkscape::Selection *selection = sp_desktop_selection(desktop);
 
2906
 
 
2907
    // Check if something is selected.
 
2908
    if (selection->isEmpty()) {
 
2909
        desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select one <b>group</b> to convert to symbol."));
 
2910
        return;
 
2911
    }
 
2912
 
 
2913
    SPObject* group = selection->single();
 
2914
 
 
2915
    // Make sure we have only one object in selection.
 
2916
    if( group == NULL ) {
 
2917
        desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select only one <b>group</b> to convert to symbol."));
 
2918
        return;
 
2919
    }
 
2920
 
 
2921
    // Make sure we convert the original.
 
2922
    if( SP_IS_USE( group ) ) {
 
2923
        desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select original (<b>Shift+D</b>) to convert to symbol."));
 
2924
        return;
 
2925
    }
 
2926
 
 
2927
    // Require that we really have a group.
 
2928
    if( !SP_IS_GROUP( group ) ) {
 
2929
        desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Group selection first to convert to symbol."));
 
2930
        return;
 
2931
    }
 
2932
 
 
2933
    doc->ensureUpToDate();
 
2934
 
 
2935
    Inkscape::XML::Node *symbol = xml_doc->createElement("svg:symbol");
 
2936
    symbol->setAttribute("style",     group->getAttribute("style"));
 
2937
    symbol->setAttribute("title",     group->getAttribute("title"));
 
2938
    symbol->setAttribute("transform", group->getAttribute("transform"));
 
2939
 
 
2940
    Glib::ustring id = group->getAttribute("id");
 
2941
 
 
2942
    // Now we need to copy all children of group
 
2943
    GSList* children = group->childList(false);
 
2944
    children = g_slist_reverse(children);
 
2945
    for (GSList* i = children; i != NULL; i = i->next ) {
 
2946
        SPObject* child = SP_OBJECT(i->data);
 
2947
        Inkscape::XML::Node *dup = child->getRepr()->duplicate(xml_doc);
 
2948
        symbol->appendChild(dup);
 
2949
        child->deleteObject(true);
 
2950
    }
 
2951
 
 
2952
    // Need to delete <g>; all <use> elements that referenced <g> should
 
2953
    // auto-magically reference <symbol>.
 
2954
    doc->getDefs()->getRepr()->appendChild(symbol);
 
2955
    symbol->setAttribute("id",id.c_str()); // After we delete group with same id.
 
2956
    // Mysterious, must set symbol ID before deleting group or all <use>
 
2957
    // refering to symbol get turned into groups. (Linked to unlinking clones?)
 
2958
    group->deleteObject(true);
 
2959
 
 
2960
    Inkscape::GC::release(symbol);
 
2961
    selection->clear();
 
2962
    // Group just disappears, nothing to select.
 
2963
 
 
2964
    // Need to signal Symbol dialog to update
 
2965
 
 
2966
    g_slist_free(children);
 
2967
 
 
2968
    DocumentUndo::done(doc, SP_VERB_EDIT_SYMBOL, _("Group to symbol"));
 
2969
}
 
2970
 
 
2971
/*
 
2972
 * Convert <symbol> to <g>. All <use> elements referencing symbol remain unchanged.
 
2973
 */
 
2974
void sp_selection_unsymbol(SPDesktop *desktop)
 
2975
{
 
2976
 
 
2977
    if (desktop == NULL) {
 
2978
        return;
 
2979
    }
 
2980
 
 
2981
    SPDocument *doc = sp_desktop_document(desktop);
 
2982
    Inkscape::XML::Document *xml_doc = doc->getReprDoc();
 
2983
 
 
2984
    Inkscape::Selection *selection = sp_desktop_selection(desktop);
 
2985
 
 
2986
    // Check if something is selected.
 
2987
    if (selection->isEmpty()) {
 
2988
        desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select a <b>symbol</b> to extract objects from."));
 
2989
        return;
 
2990
    }
 
2991
 
 
2992
    SPObject* use = selection->single();
 
2993
 
 
2994
    // Make sure we have only one object in selection.
 
2995
   if( use == NULL ) {
 
2996
        desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select only one <b>symbol</b> to convert to group."));
 
2997
        return;
 
2998
    }
 
2999
 
 
3000
    // Require that we really have a <use> that references a <symbol>.
 
3001
    if( !SP_IS_USE( use ) && !SP_IS_SYMBOL( use->firstChild() ) ) {
 
3002
        desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select only one <b>symbol</b> to convert to group."));
 
3003
        return;
 
3004
    }
 
3005
 
 
3006
    doc->ensureUpToDate();
 
3007
 
 
3008
    SPObject* symbol = use->firstChild();
 
3009
 
 
3010
    Inkscape::XML::Node *group = xml_doc->createElement("svg:g");
 
3011
    group->setAttribute("style",     symbol->getAttribute("style"));
 
3012
    group->setAttribute("title",     symbol->getAttribute("title"));
 
3013
    group->setAttribute("transform", symbol->getAttribute("transform"));
 
3014
 
 
3015
    Glib::ustring id = symbol->getAttribute("id");
 
3016
 
 
3017
    // Now we need to copy all children of symbol
 
3018
    GSList* children = symbol->childList(false);
 
3019
    children = g_slist_reverse(children);
 
3020
    for (GSList* i = children; i != NULL; i = i->next ) {
 
3021
        SPObject* child = SP_OBJECT(i->data);
 
3022
        Inkscape::XML::Node *dup = child->getRepr()->duplicate(xml_doc);
 
3023
        group->appendChild(dup);
 
3024
        child->deleteObject(true);
 
3025
    }
 
3026
 
 
3027
    SPObject* parent = use->parent; // So we insert <g> next to <use> (easier to find)
 
3028
 
 
3029
    // Need to delete <symbol>; all other <use> elements that referenced <symbol> should
 
3030
    // auto-magically reference <g>.
 
3031
    symbol->deleteObject(true);
 
3032
    group->setAttribute("id",id.c_str()); // After we delete symbol with same id.
 
3033
    parent->getRepr()->appendChild(group);
 
3034
    //use->deleteObject(true);
 
3035
 
 
3036
    SPItem *group_item = static_cast<SPItem *>(sp_desktop_document(desktop)->getObjectByRepr(group));
 
3037
    Inkscape::GC::release(group);
 
3038
    selection->clear();
 
3039
    selection->set(group_item);
 
3040
 
 
3041
    // Need to signal Symbol dialog to update
 
3042
 
 
3043
    g_slist_free(children);
 
3044
 
 
3045
    DocumentUndo::done(doc, SP_VERB_EDIT_UNSYMBOL, _("Group from symbol"));
 
3046
}
 
3047
 
2890
3048
void
2891
3049
sp_selection_tile(SPDesktop *desktop, bool apply)
2892
3050
{