diff --git a/src/actions/actions-object.cpp b/src/actions/actions-object.cpp index 5f2a4b4fe55e86786d19c6e3e970392f77a2a985..efa45366c940c72ee8777e068cb4b254b05d9c69 100644 --- a/src/actions/actions-object.cpp +++ b/src/actions/actions-object.cpp @@ -101,6 +101,44 @@ void object_trace(Glib::VariantBase const &value, InkscapeApplication *app) mainloop->run(); } + +void +object_get_attribute(const Glib::VariantBase& value, InkscapeApplication *app) +{ + SPDocument* document = nullptr; + Inkscape::Selection* selection = nullptr; + if (!get_document_and_selection(app, &document, &selection)) { + return; + } + auto const attribute = Glib::VariantBase::cast_dynamic>(value).get(); + + for (auto obj : selection->objects()) { + Inkscape::XML::Node *repr = obj->getRepr(); + auto value = repr->attribute(attribute.c_str()); + show_output(value ? Glib::strescape(value) : "", false); + } +} + + +void +object_get_property(const Glib::VariantBase& value, InkscapeApplication *app) +{ + SPDocument* document = nullptr; + Inkscape::Selection* selection = nullptr; + if (!get_document_and_selection(app, &document, &selection)) { + return; + } + auto const attribute = Glib::VariantBase::cast_dynamic>(value).get(); + + for (auto obj : selection->objects()) { + Inkscape::XML::Node *repr = obj->getRepr(); + SPCSSAttr *css = sp_repr_css_attr(repr, "style"); + auto value = sp_repr_css_property(css, attribute.c_str(), ""); + show_output(value ? Glib::strescape(value) : "", false); + sp_repr_css_attr_unref(css); + } +} + // No sanity checking is done... should probably add. void object_set_attribute(const Glib::VariantBase& value, InkscapeApplication *app) @@ -378,6 +416,8 @@ std::vector> raw_data_object = // clang-format off {"app.object-set-attribute", N_("Set Attribute"), SECTION, N_("Set or update an attribute of selected objects; usage: object-set-attribute:attribute name, attribute value;")}, {"app.object-set-property", N_("Set Property"), SECTION, N_("Set or update a property on selected objects; usage: object-set-property:property name, property value;")}, + {"app.object-get-attribute", N_("Get Attribute"), SECTION, N_("Get the value of an attribute of selected objects; usage: object-get-attribute:attribute name;")}, + {"app.object-get-property", N_("Get Property"), SECTION, N_("Get the value of a property on selected objects; usage: object-get-property:property name;")}, {"app.object-unlink-clones", N_("Unlink Clones"), SECTION, N_("Unlink clones and symbols")}, {"app.object-to-path", N_("Object To Path"), SECTION, N_("Convert shapes to paths")}, @@ -422,6 +462,8 @@ add_actions_object(InkscapeApplication* app) // clang-format off gapp->add_action_with_parameter( "object-set-attribute", String, sigc::bind(sigc::ptr_fun(&object_set_attribute), app)); gapp->add_action_with_parameter( "object-set-property", String, sigc::bind(sigc::ptr_fun(&object_set_property), app)); + gapp->add_action_with_parameter( "object-get-attribute", String, sigc::bind(sigc::ptr_fun(&object_get_attribute), app)); + gapp->add_action_with_parameter( "object-get-property", String, sigc::bind(sigc::ptr_fun(&object_get_property), app)); gapp->add_action_with_parameter( "object-trace", String, sigc::bind(sigc::ptr_fun(&object_trace), app)); gapp->add_action( "object-unlink-clones", sigc::bind(sigc::ptr_fun(&object_unlink_clones), app)); diff --git a/testfiles/cli_tests/CMakeLists.txt b/testfiles/cli_tests/CMakeLists.txt index b0b9cf5e6e54e7c9c91fd4cf4fe9e7cc765d537f..180c4401508f23d442e5b41c6722e20dc040cac2 100644 --- a/testfiles/cli_tests/CMakeLists.txt +++ b/testfiles/cli_tests/CMakeLists.txt @@ -813,12 +813,30 @@ add_cli_test(actions-object-set-attribute INPUT_FILENAME rects.svg OUTPUT_FILENAME actions-object-set-attribute.png REFERENCE_FILENAME actions-object-set-attribute_expected.png) +# object-get-attribute +add_cli_test(actions-object-get-attribute INPUT_FILENAME rects.svg + PARAMETERS --actions=select-by-id:rect1$object-get-attribute:x + PASS_FOR_OUTPUT "10\n$") + +add_cli_test(actions-object-get-attribute_nonexistant INPUT_FILENAME rects.svg + PARAMETERS --actions=select-by-id:rect1$object-get-attribute:z + PASS_FOR_OUTPUT "\n$") + # object-set-property add_cli_test(actions-object-set-property INPUT_FILENAME areas.svg PARAMETERS --actions=select-by-id:MyRect$object-set-property:fill,gold OUTPUT_FILENAME actions-object-set-property.svg TEST_SCRIPT match_regex.sh actions-object-set-property.svg "style=\"fill:gold\"") +# object-get-property +add_cli_test(actions-object-get-property INPUT_FILENAME areas.svg + PARAMETERS --actions=select-by-id:MyStar$object-get-attribute:fill + PASS_FOR_OUTPUT "red\n$") + +add_cli_test(actions-object-get-property_nonexistant INPUT_FILENAME areas.svg + PARAMETERS --actions=select-by-id:MyStar$object-get-attribute:full + PASS_FOR_OUTPUT "\n$") + # path-simplify add_cli_test(actions-path-simplify INPUT_FILENAME path.svg PARAMETERS --actions=select-by-id:gates$path-simplify$export-id:gates$export-id-only