node-tool.cpp revision 70d31ae8a7a27e57cfcdc921ea0d2f47c92442a4
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @brief New node tool - implementation
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * Krzysztof KosiĆski <tweenk@gmail.com>
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * Copyright (C) 2009 Authors
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * Released under GNU GPL, read the file 'COPYING' for more information
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp/** @struct InkNodeTool
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * Node tool event context.
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @par Architectural overview of the tool
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * Here's a breakdown of what each object does.
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * - Handle: shows a handle and keeps the node type constraint (smooth / symmetric) by updating
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * the other handle's position when dragged. Its move() method cannot violate the constraints.
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * - Node: keeps node type constraints for auto nodes and smooth nodes at ends of linear segments.
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * Its move() method cannot violate constraints. Handles linear grow and dispatches spatial grow
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * to MultiPathManipulator. Keeps a reference to its NodeList.
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * - NodeList: exposes an iterator-based interface to nodes. It is possible to obtain an iterator
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * to a node from the node. Keeps a reference to its SubpathList.
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * - SubpathList: list of NodeLists that represents an editable pathvector. Keeps a reference
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * to its PathManipulator.
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * - PathManipulator: performs most of the single-path actions like reverse subpaths,
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * delete segment, shift selection, etc. Keeps a reference to MultiPathManipulator.
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * - MultiPathManipulator: performs additional operations for actions that are not per-path,
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * for example node joins and segment joins. Tracks the control transforms for PMs that edit
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * clipping paths and masks. It is more or less equivalent to ShapeEditor and in the future
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * it might handle all shapes. Handles XML commit of actions that affect all paths or
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * the node selection and removes PathManipulators that have no nodes left after e.g. node
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * - ControlPointSelection: keeps track of node selection and a set of nodes that can potentially
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * be selected. There can be more than one selection. Performs actions that require no
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * knowledge about the path, only about the nodes, like dragging and transforms. It is not
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * specific to nodes and can accomodate any control point derived from SelectableControlPoint.
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * Transforms nodes in response to transform handle events.
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * - TransformHandleSet: displays nodeset transform handles and emits transform events. The aim
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * is to eventually use a common class for object and control point transforms.
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * - SelectableControlPoint: base for any type of selectable point. It can belong to only one
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * selection.
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * @par Plans for the future
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * - MultiPathManipulator should become a generic shape editor that manages all active manipulator,
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * more or less like the old ShapeEditor.
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * - Knotholder should be rewritten into one manipulator class per shape, using the control point
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * classes. Interesting features like dragging rectangle sides could be added along the way.
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * - Better handling of clip and mask editing, particularly in response to undo.
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * - High level refactoring of the event context hierarchy. All aspects of tools, like toolbox
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * controls, icons, event handling should be collected in one class, though each aspect
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * of a tool might be in an separate class for better modularity. The long term goal is to allow
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * tools to be defined in extensions or shared library plugins.
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTrippvoid ink_node_tool_class_init(InkNodeToolClass *klass);
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTrippgint ink_node_tool_root_handler(SPEventContext *event_context, GdkEvent *event);
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTrippgint ink_node_tool_item_handler(SPEventContext *event_context, SPItem *item, GdkEvent *event);
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTrippvoid ink_node_tool_set(SPEventContext *ec, Inkscape::Preferences::Entry *value);
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTrippvoid ink_node_tool_update_tip(InkNodeTool *nt, GdkEvent *event);
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTrippvoid ink_node_tool_selection_changed(InkNodeTool *nt, Inkscape::Selection *sel);
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTrippvoid ink_node_tool_select_area(InkNodeTool *nt, Geom::Rect const &, GdkEventButton *);
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTrippvoid ink_node_tool_select_point(InkNodeTool *nt, Geom::Point const &, GdkEventButton *);
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTrippvoid ink_node_tool_mouseover_changed(InkNodeTool *nt, Inkscape::UI::ControlPoint *p);
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp} // anonymous namespace
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp type = g_type_register_static(SP_TYPE_EVENT_CONTEXT, "InkNodeTool", &info, (GTypeFlags)0);
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp return reinterpret_cast<SPCanvasGroup*>(sp_canvas_item_new(
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp SPEventContextClass *event_context_class = (SPEventContextClass *) klass;
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp event_context_class->root_handler = ink_node_tool_root_handler;
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp event_context_class->item_handler = ink_node_tool_item_handler;
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp new (&nt->_selection_changed_connection) sigc::connection();
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp new (&nt->_selection_modified_connection) sigc::connection();
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp new (&nt->_mouseover_changed_connection) sigc::connection();
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp //new (&nt->_mgroup) Inkscape::UI::ManipulatorGroup(nt->desktop);
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp G_OBJECT_CLASS(g_type_class_peek(g_type_parent(INK_TYPE_NODE_TOOL)))->dispose(object);
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp SPEventContextClass *parent = (SPEventContextClass *) g_type_class_peek(g_type_parent(INK_TYPE_NODE_TOOL));
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp nt->_node_message_context = new Inkscape::MessageContext((ec->desktop)->messageStack());
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp nt->_path_data.reset(new Inkscape::UI::PathSharedData());
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp // selector has to be created here, so that its hidden control point is on the bottom
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp nt->_selector.reset(new Inkscape::UI::Selector(nt->desktop));
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp // Prepare canvas groups for controls. This guarantees correct z-order, so that
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp // for example a dragpoint won't obscure a node
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp data.outline_group = create_control_group(nt->desktop);
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp data.node_data.handle_line_group = create_control_group(nt->desktop);
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp data.dragpoint_group = create_control_group(nt->desktop);
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp nt->_transform_handle_group = create_control_group(nt->desktop);
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp data.node_data.node_group = create_control_group(nt->desktop);
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp data.node_data.handle_group = create_control_group(nt->desktop);
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp Inkscape::Selection *selection = sp_desktop_selection (ec->desktop);
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp /*nt->_selection_modified_connection.disconnect();
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp nt->_selection_modified_connection =
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp selection->connectModified(
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp sigc::hide(sigc::bind<0>(
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp sigc::ptr_fun(&ink_node_tool_selection_modified),
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp Inkscape::UI::ControlPoint::signal_mouseover_change.connect(
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp new Inkscape::UI::ControlPointSelection(nt->desktop, nt->_transform_handle_group));
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp nt->_multipath.reset(new Inkscape::UI::MultiPathManipulator(data,
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp sigc::mem_fun(*nt->desktop, &SPDesktop::emitToolSubselectionChanged),
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp (void*) 0));
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp // TODO remove this!
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp // read prefs before adding items to selection to prevent momentarily showing the outline
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp sp_event_context_read(nt, "single_node_transform_handles");
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp Inkscape::Preferences *prefs = Inkscape::Preferences::get();
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp nt->desktop->emitToolSubselectionChanged(NULL); // sets the coord entry fields to inactive
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTrippvoid ink_node_tool_set(SPEventContext *ec, Inkscape::Preferences::Entry *value)
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp nt->_multipath->showPathDirection(nt->show_path_direction);
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp nt->show_transform_handles, nt->single_node_transform_handles);
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp } else if (entry_name == "single_node_transform_handles") {
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp nt->show_transform_handles, nt->single_node_transform_handles);
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp ink_node_tool_selection_changed(nt, nt->desktop->selection);
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp ink_node_tool_selection_changed(nt, nt->desktop->selection);
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp (SPEventContextClass *) g_type_class_peek(g_type_parent(INK_TYPE_NODE_TOOL));
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp/** Recursively collect ShapeRecords */
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTrippvoid gather_items(InkNodeTool *nt, SPItem *base, SPObject *obj, Inkscape::UI::ShapeRole role,
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp if (!obj) return;
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp if (SP_IS_PATH(obj) && obj->repr->attribute("inkscape:original-d") != NULL) {
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp } else if (role != SHAPE_ROLE_NORMAL && (SP_IS_GROUP(obj) || SP_IS_OBJECTGROUP(obj))) {
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp // TODO add support for objectBoundingBox
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp r.edit_transform = base ? sp_item_i2doc_affine(base) : Geom::identity();
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp // this item was encountered the first time
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp gather_items(nt, item, item->clip_ref->getObject(), SHAPE_ROLE_CLIPPING_PATH, s);
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp gather_items(nt, item, item->mask_ref->getObject(), SHAPE_ROLE_MASK, s);
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp bool operator()(SPItem *i) const { return SP_IS_PATH(i); }
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTrippvoid ink_node_tool_selection_changed(InkNodeTool *nt, Inkscape::Selection *sel)
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp for (GSList *i = const_cast<GSList*>(ilist); i; i = i->next) {
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp gather_items(nt, NULL, static_cast<SPItem*>(obj), SHAPE_ROLE_NORMAL, shapes);
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp // ugly hack: set the first editable non-path item for knotholder
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp // maybe use multiple ShapeEditors for now, to allow editing many shapes at once?
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp bool something_set = false;
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp for (std::set<ShapeRecord>::iterator i = shapes.begin(); i != shapes.end(); ++i) {
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp ShapeRecord const &r = *i;
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTrippgint ink_node_tool_root_handler(SPEventContext *event_context, GdkEvent *event)
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp /* things to handle here:
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * 1. selection of items
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * 2. passing events to manipulators
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp * 3. some keybindings
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp using namespace Inkscape::UI; // pull in event helpers
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp InkNodeTool *nt = static_cast<InkNodeTool*>(event_context);
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp static Inkscape::Preferences *prefs = Inkscape::Preferences::get();
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp // create outline
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp if (prefs->getBool("/tools/nodes/pathflash_enabled")) {
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp SPItem *over_item = sp_event_context_find_item (desktop, event_point(event->button),
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp if (!prefs->getBool("/tools/nodes/pathflash_selected") && selection->includes(over_item)) break;
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp desktop->remove_temporary_canvasitem(nt->flash_tempitem);
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp if (!SP_IS_PATH(over_item)) break; // for now, handle only paths
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp SPCurve *c = sp_path_get_curve_for_edit(SP_PATH(over_item));
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp SPCanvasItem *flash = sp_canvas_bpath_new(sp_desktop_tempgroup(desktop), c);
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp prefs->getInt("/tools/nodes/highlight_color", 0xff0000ff), 1.0,
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp sp_canvas_bpath_set_fill(SP_CANVAS_BPATH(flash), 0, SP_WIND_RULE_NONZERO);
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp nt->flash_tempitem = desktop->add_temporary_canvasitem(flash,
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp prefs->getInt("/tools/nodes/pathflash_timeout", 500));
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp return true;
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp // select all nodes in subpaths that have something selected
09688ec5ffb8b9cf9883a770e2f9ebd60b28888dTripp // if nothing is selected, select everything
case GDK_KEY_RELEASE:
SPEventContextClass *parent_class = (SPEventContextClass *) g_type_class_peek(g_type_parent(INK_TYPE_NODE_TOOL));
return FALSE;
if (sz != 0) {
return FALSE;
if (!event) return;