connector-tool.cpp revision 407677d9e3b0de59f918786d87a49974f703cd7c
/*
* Connector creation tool
*
* Authors:
* Michael Wybrow <mjwybrow@users.sourceforge.net>
* Abhishek Sharma
* Jon A. Cruz <jon@joncruz.org>
* Martin Owens <doctormo@gmail.com>
*
* Copyright (C) 2005-2008 Michael Wybrow
* Copyright (C) 2009 Monash University
* Copyright (C) 2012 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 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.
*
* ----------------------------------------------------------------------------
*
* Notes:
*
* Much of the way connectors work for user-defined points has been
* changed so that it no longer defines special attributes to record
* the points. Instead it uses single node paths to define points
* who are then seperate objects that can be fixed on the canvas,
* grouped into objects and take full advantage of all transform, snap
* and align functionality of all other objects.
*
* 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.
*
* inactive when a shape is not selected in the connector context.
*
*/
#include <gdk/gdkkeysyms.h>
#include <string>
#include <cstring>
#include "ui/tools/connector-tool.h"
#include "pixmaps/cursor-connector.xpm"
#include "xml/node-event-vector.h"
#include "desktop.h"
#include "desktop-style.h"
#include "desktop-handles.h"
#include "document.h"
#include "document-undo.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"
#include "verbs.h"
using Inkscape::DocumentUndo;
#include "tool-factory.h"
namespace Inkscape {
namespace UI {
namespace Tools {
static void cc_clear_active_knots(SPKnotList k);
/*static Geom::Point connector_drag_origin_w(0, 0);
static bool connector_within_tolerance = false;*/
NULL, /* child_added */
NULL, /* child_added */
NULL, /* content_changed */
NULL /* order_changed */
};
NULL, /* child_added */
NULL, /* child_added */
NULL, /* content_changed */
NULL /* order_changed */
};
namespace {
return new ConnectorTool();
}
bool connectorContextRegistered = ToolFactory::instance().registerObject("/tools/connector", createConnectorContext);
}
return ConnectorTool::prefsPath;
}
, npoints(0)
, red_color(0xff00007f)
, green_curve(NULL)
, newConnRef(NULL)
, curvature(0.0)
, isOrthogonal(false)
, active_shape(NULL)
, active_conn(NULL)
, clickeditem(NULL)
{
for (int i = 0; i < 2; ++i) {
this->endpt_handle[i] = NULL;
this->endpt_handler_id[i] = 0;
}
}
ConnectorTool::~ConnectorTool() {
this->sel_changed_connection.disconnect();
for (int i = 0; i < 2; ++i) {
if (this->endpt_handle[1]) {
g_object_unref(this->endpt_handle[i]);
this->endpt_handle[i] = NULL;
}
}
if (this->shref) {
}
if (this->ehref) {
}
}
void ConnectorTool::setup() {
this->sel_changed_connection.disconnect();
);
/* Create red bpath */
/* Create red curve */
/* Create green curve */
this->green_curve = new SPCurve();
// Notice the initial selection.
//cc_selection_changed(this->selection, (gpointer) this);
this->_selectionChanged(this->selection);
this->within_tolerance = false;
sp_event_context_read(this, "curvature");
sp_event_context_read(this, "orthogonal");
this->enableSelectionCue();
}
// Make sure we see all enter events for canvas items,
// even if a mouse button is depressed.
}
/* fixme: Proper error handling for non-numeric data. Use a locale-independent function like
if (name == "curvature") {
} else if (name == "orthogonal") {
}
}
void ConnectorTool::finish() {
this->_finish();
this->state = SP_CONNECTOR_CONTEXT_IDLE;
if (this->selection) {
}
this->cc_clear_active_shape();
this->cc_clear_active_conn();
// Restore the default event generating behaviour.
}
//-----------------------------------------------------------------------------
void ConnectorTool::cc_clear_active_shape() {
if (this->active_shape == NULL) {
return;
}
g_assert( this->active_shape_repr );
g_assert( this->active_shape_layer_repr );
this->active_shape = NULL;
if (this->active_shape_repr) {
sp_repr_remove_listener_by_data(this->active_shape_repr, this);
this->active_shape_repr = NULL;
sp_repr_remove_listener_by_data(this->active_shape_layer_repr, this);
this->active_shape_layer_repr = NULL;
}
cc_clear_active_knots(this->knots);
}
static void
{
// Hide the connection points if they exist.
if (k.size()) {
}
}
}
void ConnectorTool::cc_clear_active_conn() {
if (this->active_conn == NULL) {
return;
}
g_assert( this->active_conn_repr );
this->active_conn = NULL;
if (this->active_conn_repr) {
sp_repr_remove_listener_by_data(this->active_conn_repr, this);
this->active_conn_repr = NULL;
}
// Hide the endpoint handles.
for (int i = 0; i < 2; ++i) {
if (this->endpt_handle[i]) {
sp_knot_hide(this->endpt_handle[i]);
}
}
}
{
p = this->active_handle->pos;
return true;
}
return false;
}
static void
{
}
static void
{
}
bool ret = false;
case GDK_BUTTON_RELEASE:
this->_resetColors();
this->state = SP_CONNECTOR_CONTEXT_IDLE;
}
if (this->state != SP_CONNECTOR_CONTEXT_IDLE) {
// 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?)
*/
this->_setActiveShape(item);
}
}
ret = true;
}
break;
case GDK_ENTER_NOTIFY:
if (!this->selected_handle) {
if (cc_item_is_shape(item)) {
this->_setActiveShape(item);
}
ret = true;
}
break;
default:
break;
}
return ret;
}
bool ret = false;
case GDK_BUTTON_PRESS:
break;
case GDK_MOTION_NOTIFY:
break;
case GDK_BUTTON_RELEASE:
break;
case GDK_KEY_PRESS:
break;
default:
break;
}
if (!ret) {
}
return ret;
}
/* Find desktop coordinates */
bool ret = false;
return true;
}
this->within_tolerance = true;
switch (this->state) {
/* This is allowed, if we just canceled curve */
{
if ( this->npoints == 0 ) {
this->cc_clear_active_conn();
/* 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();
}
this->_setInitialPoint(p);
}
this->state = SP_CONNECTOR_CONTEXT_DRAGGING;
ret = true;
break;
}
{
// This is the second click of a connector creation.
m.unSetup();
this->_setSubsequentPoint(p);
this->_finishSegment(p);
this->_ptHandleTest(p, &this->ehref);
if (this->npoints != 0) {
this->_finish();
}
this->cc_set_active_conn(this->newconn);
this->state = SP_CONNECTOR_CONTEXT_IDLE;
ret = true;
break;
}
{
g_warning("Button down in CLOSE state");
break;
}
default:
break;
}
if (this->state == SP_CONNECTOR_CONTEXT_REROUTING) {
// A context menu is going to be triggered here,
// so end the rerouting operation.
this->_reroutingFinish(&p);
this->state = SP_CONNECTOR_CONTEXT_IDLE;
// Don't set ret to TRUE, so we drop through to the
// parent handler which will open the context menu.
}
else if (this->npoints != 0) {
this->_finish();
this->state = SP_CONNECTOR_CONTEXT_IDLE;
ret = true;
}
}
return ret;
}
bool ret = false;
// allow middle-button scrolling
return false;
}
if (this->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)
this->within_tolerance = false;
/* Find desktop coordinates */
switch (this->state) {
{
// This is movement during a connector creation.
if ( this->npoints > 0 ) {
m.unSetup();
this->_setSubsequentPoint(p);
ret = true;
}
break;
}
{
m.unSetup();
// Update the hidden path
if (this->clickedhandle == this->endpt_handle[0]) {
}
else {
}
// Copy this to the temporary visible path
ret = true;
break;
}
/* This is perfectly valid */
break;
default:
if (!this->sp_event_context_knot_mouseover()) {
m.unSetup();
}
break;
}
return ret;
}
bool ret = false;
/* Find desktop coordinates */
switch (this->state) {
//case SP_CONNECTOR_CONTEXT_POINT:
{
m.unSetup();
if (this->within_tolerance)
{
this->_finishSegment(p);
return true;
}
// Connector has been created via a drag, end it now.
this->_setSubsequentPoint(p);
this->_finishSegment(p);
// Test whether we clicked on a connection point
this->_ptHandleTest(p, &this->ehref);
if (this->npoints != 0) {
this->_finish();
}
this->cc_set_active_conn(this->newconn);
this->state = SP_CONNECTOR_CONTEXT_IDLE;
break;
}
{
m.unSetup();
this->_reroutingFinish(&p);
doc->ensureUpToDate();
this->state = SP_CONNECTOR_CONTEXT_IDLE;
return true;
break;
}
/* This is allowed, if we just cancelled curve */
break;
default:
break;
}
ret = true;
}
return ret;
}
bool ret = false;
switch (keyval) {
case GDK_KEY_Return:
case GDK_KEY_KP_Enter:
if (this->npoints != 0) {
this->_finish();
this->state = SP_CONNECTOR_CONTEXT_IDLE;
ret = true;
}
break;
case GDK_KEY_Escape:
if (this->state == SP_CONNECTOR_CONTEXT_REROUTING) {
this->_reroutingFinish(NULL);
this->state = SP_CONNECTOR_CONTEXT_IDLE;
_("Connector endpoint drag cancelled."));
ret = true;
}
else if (this->npoints != 0) {
// if drawing, cancel, otherwise pass it up for deselecting
this->state = SP_CONNECTOR_CONTEXT_STOP;
this->_resetColors();
ret = true;
}
break;
default:
break;
}
return ret;
}
// Clear the temporary path:
if (p != NULL)
{
// Test whether we clicked on a connection point
if (found) {
if (this->clickedhandle == this->endpt_handle[0]) {
}
else {
}
}
}
this->clickeditem->setHidden(false);
this->clickeditem->updateRepr();
_("Reroute connector"));
this->cc_set_active_conn(this->clickeditem);
}
void ConnectorTool::_resetColors() {
/* Red */
this->green_curve->reset();
this->npoints = 0;
}
this->p[0] = p;
this->p[1] = p;
this->npoints = 2;
}
if (!this->newConnRef) {
if (this->isOrthogonal)
else
}
// Set new endpoint.
// Immediately generate new routes for connector.
this->newConnRef->makePathInvalid();
// 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.
*/
void ConnectorTool::_concatColorsAndFlush() {
SPCurve *c = this->green_curve;
this->green_curve = new SPCurve();
if (c->is_empty()) {
c->unref();
return;
}
this->_flushWhite(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
*
*/
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;
if (this->shref)
{
connection = true;
}
if (this->ehref)
{
connection = true;
}
// Process pending updates.
this->newconn->updateRepr();
doc->ensureUpToDate();
if (connection) {
// Adjust endpoints to shape edge.
this->newconn->updateRepr();
}
// 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();
}
this->p[0] = this->p[3];
this->p[1] = this->p[4];
this->npoints = 2;
}
}
void ConnectorTool::_finish() {
this->_concatColorsAndFlush();
this->npoints = 0;
if (this->newConnRef) {
this->newConnRef->removeFromGraph();
delete this->newConnRef;
this->newConnRef = NULL;
}
}
static gboolean
{
case GDK_ENTER_NOTIFY:
if (knot_tip)
{
}
break;
case GDK_LEAVE_NOTIFY:
/* FIXME: the following test is a workaround for LP Bug #1273510.
* It seems that a signal is not correctly disconnected, maybe
* something missing in cc_clear_active_conn()? */
if (cc) {
}
if (knot_tip) {
}
break;
default:
break;
}
return consumed;
}
static gboolean
{
//g_assert( SP_IS_CONNECTOR_CONTEXT(cc) );
case GDK_BUTTON_PRESS:
// Disconnect from attached shape
}
else {
}
// Show the red path for dragging.
// The rest of the interaction rerouting the connector is
// handled by the context root handler.
}
break;
default:
break;
}
return consumed;
}
// We don't want to use the standard knot handler.
knot->_event_handler_id = 0;
}
if (this->active_shape != item)
{
// The active shape has changed
// Rebuild everything
this->active_shape = item;
// Remove existing active shape listeners
if (this->active_shape_repr) {
sp_repr_remove_listener_by_data(this->active_shape_repr, this);
sp_repr_remove_listener_by_data(this->active_shape_layer_repr, this);
}
// Listen in case the active shape changes
if (this->active_shape_repr) {
}
cc_clear_active_knots(this->knots);
// The idea here is to try and add a group's children to solidify
// connection handling. We react to path objects with only one node.
}
}
this->_activeShapeAddKnot(item);
}
else
{
// Ensure the item's connection_points map
// has been updated
}
}
if (this->active_conn == item)
{
{
// Connector is invisible because it is clipped to the boundary of
// two overlpapping shapes.
sp_knot_hide(this->endpt_handle[0]);
}
else
{
// Just adjust handle positions.
}
return;
}
this->active_conn = item;
// Remove existing active conn listeners
if (this->active_conn_repr) {
sp_repr_remove_listener_by_data(this->active_conn_repr, this);
this->active_conn_repr = NULL;
}
// Listen in case the active conn changes
if (this->active_conn_repr) {
}
for (int i = 0; i < 2; ++i) {
// Create the handle if it doesn't exist
if ( this->endpt_handle[i] == NULL ) {
_("<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;
this->endpt_handle[i] = knot;
}
// Remove any existing handlers
if (this->endpt_handler_id[i]) {
this->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.
this->endpt_handler_id[i] = g_signal_connect_after(
G_CALLBACK(endpt_handler), this);
}
{
// Connector is invisible because it is clipped to the boundary
// of two overlpapping shapes. So, it doesn't need endpoints.
return;
}
sp_knot_show(this->endpt_handle[0]);
}
{
{
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)) {
// 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");
}
if (this->active_conn == item) {
// Nothing to change.
return;
}
this->cc_clear_active_conn();
return;
}
if (cc_item_is_connector(item)) {
this->cc_set_active_conn(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.
}
}
}
}
}
}
/*
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 :