connector-context.cpp revision acd7e37b7d41c18cacb9d1fbac525924c889edcd
/**
* Connector creation tool
*
* Authors:
* Michael Wybrow <mjwybrow@users.sourceforge.net>
* Abhishek Sharma
* Jon A. Cruz <jon@joncruz.org>
*
* Copyright (C) 2005-2008 Michael Wybrow
* Copyright (C) 2009 Monash University
* Copyright (C) 2010 authors
*
* Released under GNU GPL, read the file 'COPYING' for more information
*
* TODO:
* o Show a visual indicator for objects with the 'avoid' property set.
* o Allow user to change a object between a path and connector through
* the interface.
* o Create an interface for setting markers (arrow heads).
* o Better distinguish between paths and connectors to prevent problems
* in the node tool and paths accidentally being turned into connectors
* in the connector tool. Perhaps have a way to convert between.
* o Only call libavoid's updateEndPoint as required. Currently we do it
* for both endpoints, even if only one is moving.
* o Allow user-placeable connection points.
* o Deal sanely with connectors with both endpoints attached to the
* same connection point, and drawing of connectors attaching
* overlapping shapes (currently tries to adjust connector to be
* outside both bounding boxes).
* o Fix many special cases related to connectors updating,
* e.g., copying a couple of shapes and a connector that are
* attached to each other.
* e.g., detach connector when it is moved or transformed in
* one of the other contexts.
* o Cope with shapes whose ids change when they have attached
* connectors.
* o During dragging motion, gobble up to and use the final motion event.
* Gobbling away all duplicates after the current can occasionally result
* in the path lagging behind the mouse cursor if it is no longer being
* dragged.
* o Fix up libavoid's representation after undo actions. It doesn't see
* any transform signals and hence doesn't know shapes have moved back to
* there earlier positions.
* or the connector tool should always start in drawing mode.
* o Correct the problem with switching to the select tool when pressing
* space bar (there are moments when it refuses to do so).
*
* ----------------------------------------------------------------------------
*
* mjwybrow's observations on acracan's Summer of Code connector work:
*
* - GUI comments:
*
* - Buttons for adding and removing user-specified connection
* points should probably have "+" and "-" symbols on them so they
* are consistent with the similar buttons for the node tool.
* - Controls on the connector tool be should be reordered logically,
* possibly as follows:
*
* *Connector*: [Polyline-radio-button] [Orthgonal-radio-button]
* [Curvature-control] | *Shape*: [Avoid-button] [Dont-avoid-button]
* [Spacing-control] | *Connection pts*: [Edit-mode] [Add-pt] [Rm-pt]
*
* I think that the network layout controls be moved to the
* Align and Distribute dialog (there is already the layout button
* there, but no options are exposed).
*
* I think that the style change between polyline and orthogonal
* would be much clearer with two buttons (radio behaviour -- just
* one is true).
*
* The other tools show a label change from "New:" to "Change:"
* depending on whether an object is selected. We could consider
* this but there may not be space.
*
* The Add-pt and Rm-pt buttons should be greyed out (inactive) if
* we are not in connection point editing mode. And probably also
* if there is no shape selected, i.e. at the times they have no
* effect when clicked.
*
* inactive when a shape is not selected in the connector context.
*
*
* - Strange things can happen if you have connectors selected, or
* try rerouting connectors by dragging their endpoints when in
* connection point editing mode.
*
* - Possibly the selected shape's connection points should always
* be shown (i.e., have knots) when in editing mode.
*
* - It is a little strange to be able to place connection points
* competely outside shapes. Especially when you later can't draw
* connectors to them since the knots are only visible when you
* are over the shape. I think that you should only be able to
* place connection points inside or on the boundary of the shape
* itself.
*
* - The intended ability to place a new point at the current cursor
* position by pressing RETURN does not seem to work.
*
* - The Status bar tooltip should change to reflect editing mode
* and tell the user about RETURN and how to use the tool.
*
* - Connection points general:
*
* - Connection points that were inside the shape can end up outside
* after a rotation is applied to the shape in the select tool.
* It doesn't seem like the correct transform is being applied to
* these, or it is being applied at the wrong time. I'd expect
* connection points to rotate with the shape, and stay at the
* same position "on the shape"
*
* - I was able to make the connectors attached to a shape fall off
* the shape after scaling it. Not sure the exact cause, but may
* require more investigation/debugging.
*
* - The user-defined connection points should be either absolute
* (as the current ones are) or defined as a percentage of the
* shape. These would be based on a toggle setting on the
* toolbar, and they would be placed in exactly the same way by
* the user. The only difference would be that they would be
* store as percentage positions in the SVG connection-points
* object was resized or scaled.
*
* - Thinking more, I think you always want to store and think about
* the positions of connection points to be pre-transform, but
* obviously the shape transform is applied to them. That way,
* they will rotate and scale automatically with the shape, when
* the shape transform is altered. The Percentage version would
* compute their position from the pre-transform dimensions and
* then have the transform applied to them, for example.
*
* - The connection points in the test_connection_points.svg file
* seem to follow the shape when it is moved, but connection
* points I add to new shapes, do not follow the shape, either
* when the shape is just moved or transformed. There is
* something wrong here. What exactly should the behaviour be
* currently?
*
* - I see that connection points are specified at absolute canvas
* positions. I really think that they should be specified in
* shape coordinated relative to the shapes. There may be
* transforms applied to layers and the canvas which would make
* specifying them quite difficult. I'd expect a position of 0, 0
* to be on the shape in question or very close to it, for example.
*
*/
#include <gdk/gdkkeysyms.h>
#include <string>
#include <cstring>
#include "connector-context.h"
#include "pixmaps/cursor-connector.xpm"
#include "pixmaps/cursor-node.xpm"
//#include "pixmaps/cursor-node-m.xpm"
//#include "pixmaps/cursor-node-d.xpm"
#include "xml/node-event-vector.h"
#include "desktop.h"
#include "desktop-style.h"
#include "desktop-handles.h"
#include "document.h"
#include "message-context.h"
#include "message-stack.h"
#include "selection.h"
#include "inkscape.h"
#include "preferences.h"
#include "sp-path.h"
#include "display/sp-canvas.h"
#include "display/canvas-bpath.h"
#include "display/sodipodi-ctrl.h"
#include <glibmm/stringutils.h>
#include "snap.h"
#include "knot.h"
#include "sp-conn-end.h"
#include "sp-conn-end-pair.h"
#include "conn-avoid-ref.h"
#include "libavoid/vertices.h"
#include "context-fns.h"
#include "sp-namedview.h"
#include "sp-text.h"
#include "sp-flowtext.h"
using Inkscape::DocumentUndo;
static gint sp_connector_context_item_handler(SPEventContext *event_context, SPItem *item, GdkEvent *event);
// Stuff borrowed from DrawContext
// Context event handlers
static gint connector_handle_button_press(SPConnectorContext *const cc, GdkEventButton const &bevent);
static gint connector_handle_motion_notify(SPConnectorContext *const cc, GdkEventMotion const &mevent);
static gint connector_handle_button_release(SPConnectorContext *const cc, GdkEventButton const &revent);
static void cc_active_shape_add_knot(SPDesktop* desktop, SPItem* item, ConnectionPointMap &cphandles, ConnectionPoint& cp);
static bool conn_pt_handle_test(SPConnectorContext *cc, Geom::Point& p, gchar **href, gchar **cpid);
static char* cc_knot_tips[] = { _("<b>Connection point</b>: click or drag to create a new connector"),
_("<b>Connection point</b>: click to select, drag to move") };
/*static Geom::Point connector_drag_origin_w(0, 0);
static bool connector_within_tolerance = false;*/
static SPEventContextClass *parent_class;
NULL, /* child_added */
NULL, /* child_added */
NULL, /* content_changed */
NULL /* order_changed */
};
NULL, /* child_added */
NULL, /* child_added */
NULL, /* content_changed */
NULL /* order_changed */
};
{
if (!type) {
sizeof(SPConnectorContextClass),
sizeof(SPConnectorContext),
4,
NULL, /* value_table */
};
}
return type;
}
static void
{
}
static void
{
for (int i = 0; i < 2; ++i) {
cc->endpt_handler_id[i] = 0;
}
}
static void
{
}
}
for (int i = 0; i < 2; ++i) {
}
}
}
}
}
}
}
static void
{
}
/* Create red bpath */
/* Create red curve */
/* Create green curve */
// Notice the initial selection.
cc->within_tolerance = false;
ec->enableSelectionCue();
}
// Make sure we see all enter events for canvas items,
// even if a mouse button is depressed.
}
static void
{
/* fixme: Proper error handling for non-numeric data. Use a locale-independent function like
if ( name == "curvature" ) {
}
else if ( name == "orthogonal" ) {
}
else if ( name == "mode")
{
sp_connector_context_switch_mode(ec, val->getBool() ? SP_CONNECTOR_CONTEXT_EDITING_MODE : SP_CONNECTOR_CONTEXT_DRAWING_MODE);
}
}
{
{
if (cc->selected_handle)
// Show all default connection points
}
{
/* if (cc->active_shape)
{
cc->selection->set( cc->active_shape );
}
else
{
SPItem* item = cc->selection->singleItem();
if ( item )
{
cc_set_active_shape(cc, item);
cc->selection->set( item );
}
}*/
}
}
static void
{
}
}
// Restore the default event generating behaviour.
}
//-----------------------------------------------------------------------------
static void
{
return;
}
if (cc->active_shape_repr) {
}
// Hide the connection points if they exist.
}
}
}
static void
{
return;
}
if (cc->active_conn_repr) {
}
// Hide the endpoint handles.
for (int i = 0; i < 2; ++i) {
if (cc->endpt_handle[i]) {
}
}
}
static bool
{
// TODO: this will need to change when there are more connection
// points available for each shape.
{
return true;
}
return false;
}
static void
{
}
static void
{
}
static gint
{
case GDK_BUTTON_RELEASE:
{
}
// Doing something else like rerouting.
break;
}
// find out clicked item, honoring Alt
} else {
{
}
/* When selecting a new item,
do not allow showing connection points
on connectors. (yet?)
*/
}
}
break;
case GDK_ENTER_NOTIFY:
{
if (cc->mode == SP_CONNECTOR_CONTEXT_DRAWING_MODE || (cc->mode == SP_CONNECTOR_CONTEXT_EDITING_MODE && !cc->selected_handle))
{
if (cc_item_is_shape(item)) {
// I don't really understand what the above does,
// so I commented it.
// This is a shape, so show connection point(s).
/* if (!(cc->active_shape)
// Don't show handle for another handle.
// || (cc->connpthandles.find((SPKnot*) item) != cc->connpthandles.end())
)
{
cc_set_active_shape(cc, item);
}*/
}
}
break;
}
default:
break;
}
return ret;
}
{
case GDK_BUTTON_PRESS:
break;
case GDK_MOTION_NOTIFY:
break;
case GDK_BUTTON_RELEASE:
break;
case GDK_KEY_PRESS:
break;
default:
break;
}
if (!ret) {
if (parent_root_handler) {
}
}
return ret;
}
static gint
{
/* Find desktop coordinates */
{
return TRUE;
}
bevent.y);
// connector_drag_origin_w = event_w;
cc->within_tolerance = true;
/* This is allowed, if we just canceled curve */
{
SP_EVENT_CONTEXT_DESKTOP(cc)->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Creating new connector"));
/* Set start anchor */
/* Create green anchor */
// Test whether we clicked on a connection point
if (!found) {
// This is the first point, so just snap it to the grid
// as there's no other points to go off.
m.unSetup();
}
}
break;
}
{
// This is the second click of a connector creation.
m.unSetup();
// Test whether we clicked on a connection point
}
break;
}
{
g_warning("Button down in CLOSE state");
break;
}
default:
break;
}
// A context menu is going to be triggered here,
// so end the rerouting operation.
// Don't set ret to TRUE, so we drop through to the
// parent handler which will open the context menu.
}
}
}
}
{
{
// Initialize variables in case of dragging
return TRUE;
}
cc->within_tolerance = true;
{
{
// We do not allow selecting and, thereby, moving default knots
{
{
if ( cc->selected_handle )
}
}
else
// Just ignore the default connection point
return FALSE;
}
else
if ( cc->selected_handle )
{
}
if ( cc->selected_handle )
{
}
break;
// Dragging valid because of the way we create
// new connection points.
// Do nothing.
break;
}
}
}
return ret;
}
static gint
{
if (event_context->space_panning || mevent.state & GDK_BUTTON2_MASK || mevent.state & GDK_BUTTON3_MASK) {
// allow middle-button scrolling
return FALSE;
}
if (cc->within_tolerance) {
return FALSE; // Do not drag if we're within tolerance from origin.
}
}
// Once the user has moved farther than tolerance from the original location
// (indicating they intend to move the object, not click), then always process
// the motion notify coordinates as given (no snapping back to origin)
cc->within_tolerance = false;
/* Find desktop coordinates */
{
{
// This is movement during a connector creation.
m.unSetup();
}
break;
}
{
m.unSetup();
// Update the hidden path
}
else {
}
// Copy this to the temporary visible path
break;
}
/* This is perfectly valid */
break;
default:
if (!sp_event_context_knot_mouseover(cc)) {
m.unSetup();
}
break;
}
}
{
{
break;
break;
}
}
return ret;
}
static gint
{
/* Find desktop coordinates */
{
//case SP_CONNECTOR_CONTEXT_POINT:
{
m.unSetup();
if (cc->within_tolerance)
{
return TRUE;
}
// Connector has been created via a drag, end it now.
// Test whether we clicked on a connection point
}
break;
}
{
m.unSetup();
doc->ensureUpToDate();
return TRUE;
break;
}
/* This is allowed, if we just cancelled curve */
break;
default:
break;
}
}
{
{
if (!cc->within_tolerance)
{
m.unSetup();
}
break;
m.unSetup();
doc->ensureUpToDate();
for (ConnectionPointMap::iterator it = cc->connpthandles.begin(); it != cc->connpthandles.end(); ++it)
{
break;
}
break;
}
}
}
return ret;
}
static gint
{
/* fixme: */
{
switch (keyval) {
case GDK_Return:
case GDK_KP_Enter:
}
break;
case GDK_Escape:
_("Connector endpoint drag cancelled."));
}
// if drawing, cancel, otherwise pass it up for deselecting
}
break;
default:
break;
}
}
{
{
if ( keyval == GDK_Escape )
{
// Cancel connection point dragging
// Obtain original position
_("Connection point drag cancelled."));
}
{
// Put connection point at current position
if (!cc->within_tolerance)
{
m.unSetup();
}
}
break;
if ( keyval == GDK_Escape )
{
// Just destroy the knot
}
{
m.unSetup();
doc->ensureUpToDate();
for (ConnectionPointMap::iterator it = cc->connpthandles.begin(); it != cc->connpthandles.end(); ++it)
{
break;
}
}
break;
{
}
break;
}
}
return ret;
}
static void
{
// Clear the temporary path:
if (p != NULL)
{
// Test whether we clicked on a connection point
if (found) {
}
else {
}
}
}
_("Reroute connector"));
}
static void
{
/* Red */
}
static void
{
cc->p[0] = p;
cc->p[1] = p;
}
static void
{
if (!cc->newConnRef) {
if (cc->isOrthogonal)
else
}
// Set new endpoint.
// Immediately generate new routes for connector.
// Recreate curve from libavoid route.
}
/**
* Concats red, blue and green.
* If any anchors are defined, process these, optionally removing curves from white list
* Invoke _flush_white to write result back to object.
*/
static void
{
if (c->is_empty()) {
c->unref();
return;
}
spcc_flush_white(cc, c);
c->unref();
}
/*
* Flushes white curve(s) and additional curve into object
*
* No cleaning of colored curves - this has to be done by caller
* No rereading of white data, so if you cannot rely on ::modified, do it in caller
*
*/
static void
{
SPCurve *c;
if (gc) {
c = gc;
c->ref();
} else {
return;
}
/* Now we have to go back to item coordinates at last */
if ( c && !c->is_empty() ) {
/* We actually have something to write */
/* Set style */
/* Attach repr */
bool connection = false;
{
}
connection = true;
}
{
}
connection = true;
}
// Process pending updates.
doc->ensureUpToDate();
if (connection) {
// Adjust endpoints to shape edge.
}
// Only set the selection after we are finished with creating the attributes of
// the connector. Otherwise, the selection change may alter the defaults for
// values like curvature in the connector context, preventing subsequent lookup
// of their original values.
}
c->unref();
}
static void
{
}
}
static void
{
if (cc->newConnRef) {
delete cc->newConnRef;
}
}
static gboolean
{
case GDK_ENTER_NOTIFY:
if (knot_tip)
{
}
break;
case GDK_LEAVE_NOTIFY:
if (knot_tip) {
}
break;
default:
break;
}
return consumed;
}
static gboolean
{
case GDK_BUTTON_PRESS:
// Disconnect from attached shape
}
else {
}
// Show the red path for dragging.
cc->red_curve = SP_PATH(cc->clickeditem)->original_curve ? SP_PATH(cc->clickeditem)->original_curve->copy() : SP_PATH(cc->clickeditem)->curve->copy();
// The rest of the interaction rerouting the connector is
// handled by the context root handler.
}
break;
default:
break;
}
return consumed;
}
static void cc_active_shape_add_knot(SPDesktop* desktop, SPItem* item, ConnectionPointMap &cphandles, ConnectionPoint& cp)
{
// We don't want to use the standard knot handler.
knot->_event_handler_id = 0;
sp_knot_set_position(knot, item->avoidRef->getConnectionPointPos(cp.type, cp.id) * desktop->doc2dt(), 0);
}
{
{
// The active shape has changed
// Rebuild everything
// Remove existing active shape listeners
if (cc->active_shape_repr) {
}
// Listen in case the active shape changes
if (cc->active_shape_repr) {
}
// Set the connection points.
// destroy the old list
{
}
// build the new one
// Also add default connection points
// For now, only centre default connection point will
// be available
}
else
{
// The active shape didn't change
// Update only the connection point knots
// Ensure the item's connection_points map
// has been updated
{
bool removed = false;
{
{
// Connection point position has changed
// Update knot position
}
else
{
// This connection point does no longer exist,
// remove the knot
++it;
removed = true;
}
}
else
{
// It's a default connection point
// Just make sure it's position is correct
}
if ( !removed )
++it;
}
// Add knots for new connection points.
// A new connection point has been added
// to the shape. Add a knot for it.
}
}
static void
{
SPCurve *curve = SP_PATH(item)->original_curve ? SP_PATH(item)->original_curve : SP_PATH(item)->curve;
{
{
// Connector is invisible because it is clipped to the boundary of
// two overlpapping shapes.
}
else
{
// Just adjust handle positions.
}
return;
}
// Remove existing active conn listeners
if (cc->active_conn_repr) {
}
// Listen in case the active conn changes
if (cc->active_conn_repr) {
}
for (int i = 0; i < 2; ++i) {
// Create the handle if it doesn't exist
_("<b>Connector endpoint</b>: drag to reroute or connect to new shapes"));
// We don't want to use the standard knot handler,
// since we don't want this knot to be draggable.
knot->_event_handler_id = 0;
}
// Remove any existing handlers
if (cc->endpt_handler_id[i]) {
cc->endpt_handler_id[i] = 0;
}
// Setup handlers for connector endpoints, this is
// is as 'after' so that cc_generic_knot_handler is
// triggered first for any endpoint.
}
{
// Connector is invisible because it is clipped to the boundary
// of two overlpapping shapes. So, it doesn't need endpoints.
return;
}
}
{
{
if (cc->selected_handle)
{
}
// We do not process events on this knot.
knot->_event_handler_id = 0;
cc_select_handle( knot );
}
}
{
{
}
}
{
if (SP_IS_PATH(item)) {
// Open paths are connectors.
return false;
}
}
// Don't count text as a shape we can connect connector to.
return false;
}
}
return true;
}
{
if (SP_IS_PATH(item)) {
bool closed = SP_PATH(item)->original_curve ? SP_PATH(item)->original_curve->is_closed() : SP_PATH(item)->curve->is_closed();
// To be considered a connector, an object must be a non-closed
// path that is marked with a "inkscape:connector-type" attribute.
return true;
}
}
return false;
}
void cc_selection_set_avoid(bool const set_avoid)
{
return;
}
int changes = 0;
while (l) {
if (cc_item_is_shape(item)) {
changes++;
}
l = l->next;
}
if (changes == 0) {
_("Select <b>at least one non-connector object</b>."));
return;
}
char *event_desc = (set_avoid) ?
_("Make connectors avoid selected objects") :
_("Make connectors ignore selected objects");
}
static void
{
//SPEventContext *ec = SP_EVENT_CONTEXT(cc);
{
// Nothing to change.
return;
}
{
return;
}
if (cc_item_is_connector(item)) {
}
}
static void
{
// The active shape has been deleted. Clear active shape.
}
}
static void
{
// Look for changes that result in onscreen movement.
{
// Active shape has moved. Clear active shape.
}
// The active conn has been moved.
// Set it again, which just sets new handle positions.
}
}
else
// The connection points of the active shape
// have changed. Update them.
}
/*
Local Variables:
mode:c++
c-file-style:"stroustrup"
c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
indent-tabs-mode:nil
fill-column:99
End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :