From 64ce203ee93252ef6d22351885198949173d868e Mon Sep 17 00:00:00 2001 From: Jabiertxof Date: Fri, 28 Oct 2022 19:10:45 +0200 Subject: [PATCH] This MR along with another in extensions allow inkscape extension creator update the selection of items and nodes --- src/attributes.cpp | 1 + src/attributes.h | 1 + src/extension/execution-env.cpp | 5 +++- src/extension/execution-env.h | 3 +++ src/selection.cpp | 43 +++++++++++++++++++++++++++++++ src/selection.h | 4 +++ testfiles/src/attributes-test.cpp | 1 + 7 files changed, 57 insertions(+), 1 deletion(-) diff --git a/src/attributes.cpp b/src/attributes.cpp index 5dd9dcfc47..3187b1950d 100644 --- a/src/attributes.cpp +++ b/src/attributes.cpp @@ -42,6 +42,7 @@ static SPStyleProp const props[] = { {SPAttr::PRESERVEASPECTRATIO, "preserveAspectRatio"}, {SPAttr::ONLOAD, "onload"}, {SPAttr::SODIPODI_DOCNAME, "sodipodi:docname"}, + {SPAttr::INKSCAPE_SELECTIONDATA, "inkscape:selectiondata"}, /* SPItem */ {SPAttr::TRANSFORM, "transform"}, {SPAttr::SODIPODI_TYPE, "sodipodi:type"}, diff --git a/src/attributes.h b/src/attributes.h index d3c946799d..a81d9dfcd1 100644 --- a/src/attributes.h +++ b/src/attributes.h @@ -40,6 +40,7 @@ enum class SPAttr { PRESERVEASPECTRATIO, ONLOAD, SODIPODI_DOCNAME, + INKSCAPE_SELECTIONDATA, /* SPItem */ TRANSFORM, SODIPODI_TYPE, diff --git a/src/extension/execution-env.cpp b/src/extension/execution-env.cpp index 0875d132fa..08144b8f84 100644 --- a/src/extension/execution-env.cpp +++ b/src/extension/execution-env.cpp @@ -24,6 +24,7 @@ #include "document-undo.h" #include "desktop.h" #include "object/sp-namedview.h" +#include "object/sp-item.h" #include "ui/widget/canvas.h" // To get window (perverse!) @@ -207,7 +208,9 @@ ExecutionEnv::run () { _effect->get_imp()->effect(_effect, _doc, _docCache); desktop->clearWaitingCursor(); _state = ExecutionEnv::COMPLETE; - selection->restoreBackup(); + const gchar * selectiondata = desktop->getDocument()->getNamedView()->getAttribute("inkscape:selectiondata"); + selection->restoreBackup(selectiondata); + desktop->getDocument()->getNamedView()->setAttribute("inkscape:selectiondata", nullptr); // _runComplete.signal(); return; } diff --git a/src/extension/execution-env.h b/src/extension/execution-env.h index fb54ceb71d..70bc9a8256 100644 --- a/src/extension/execution-env.h +++ b/src/extension/execution-env.h @@ -13,6 +13,7 @@ #include #include +#include "object/sp-object.h" #include @@ -98,6 +99,8 @@ public: private: void runComplete (); + void removeClass(SPObject *obj, const Glib::ustring &className, bool all); + void createWorkingDialog (); void workingCanceled (const int resp); void genDocCache (); diff --git a/src/selection.cpp b/src/selection.cpp index 2641dfd17d..3ab273cfc0 100644 --- a/src/selection.cpp +++ b/src/selection.cpp @@ -33,6 +33,7 @@ #include "ui/tool/multi-path-manipulator.h" #include "ui/tool/path-manipulator.h" #include "ui/tool/control-point-selection.h" +#include "svg/svg.h" #include "layer-manager.h" #include "page-manager.h" #include "object/sp-path.h" @@ -331,6 +332,48 @@ Selection::setBackup () }//end add selected nodes } +void +Selection::restoreBackup(const gchar * selectiondata) +{ + if (!selectiondata) { + restoreBackup(); + return; + } + _selected_ids.clear(); + _seldata.clear(); + gchar ** strarray = g_strsplit(selectiondata, "@", 0); + gchar ** iter = strarray; + Glib::ustring previter = ""; + while (*iter != nullptr) { + gchar ** strsubarray = g_strsplit(*iter, "," , 0); + gchar ** subiter = strsubarray; + size_t pos = 0; + while (*subiter != nullptr) { + if (!pos) { + _selected_ids.emplace_back(*subiter); + } else { + if (pos % 2 == 0) { + float spf; + sp_svg_number_read_f(previter.c_str(), &spf); + int sp = (unsigned int)spf; + float nlf; + sp_svg_number_read_f(g_strstrip(*subiter), &nlf); + int nl = (unsigned int)nlf; + _seldata.emplace_back(std::string(_selected_ids.back()),std::make_pair(sp,nl)); + } else { + previter = Glib::ustring(*subiter); + } + } + pos++; + subiter++; + } + g_strfreev (strsubarray); + iter++; + } + g_strfreev (strarray); + restoreBackup(); +} + void Selection::restoreBackup() { diff --git a/src/selection.h b/src/selection.h index 9dd6eda603..07eb019762 100644 --- a/src/selection.h +++ b/src/selection.h @@ -198,6 +198,10 @@ public: * Restore a selection from a existing backup */ void restoreBackup(); + /** + * Restore a selection from a string property in svg usualy set by extensions + */ + void restoreBackup(const gchar * selectiondata); /** * Here store a paramlist when set backup */ diff --git a/testfiles/src/attributes-test.cpp b/testfiles/src/attributes-test.cpp index 3b161f721e..adf72a2765 100644 --- a/testfiles/src/attributes-test.cpp +++ b/testfiles/src/attributes-test.cpp @@ -477,6 +477,7 @@ std::vector getKnownAttrs() AttributeInfo("inkscape:zoom", true), AttributeInfo("inkscape:svg-dpi", true), AttributeInfo("inkscape:swatch", true), + AttributeInfo("inkscape:selectiondata", true), AttributeInfo("sodipodi:arc-type", true), AttributeInfo("sodipodi:arg1", true), AttributeInfo("sodipodi:arg2", true), -- GitLab