connector-context.cpp revision 6d8430ca47c35cff45dee59e1f9c025567d0af49
/*
* Connector creation tool
*
* Authors:
* Michael Wybrow <mjwybrow@users.sourceforge.net>
* Abhishek Sharma
* Jon A. Cruz <jon@joncruz.org>
* Martin Owens <doctormo@ubuntu.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 tranform, 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 "connector-context.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;
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_clear_active_knots(SPKnotList k);
/*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" ) {
}
}
static void
{
}
}
// Restore the default event generating behaviour.
}
//-----------------------------------------------------------------------------
static void
{
return;
}
if (cc->active_shape_repr) {
}
}
static void
{
// Hide the connection points if they exist.
if (k.size()) {
}
}
}
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
{
{
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->selected_handle)
{
if (cc_item_is_shape(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);
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();
}
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.
}
}
}
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;
}
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;
}
}
return ret;
}
static gint
{
switch (keyval) {
case GDK_KEY_Return:
case GDK_KEY_KP_Enter:
}
break;
case GDK_KEY_Escape:
_("Connector endpoint drag cancelled."));
}
// if drawing, cancel, otherwise pass it up for deselecting
}
break;
default:
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.
// 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;
}
{
{
// 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) {
}
// 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.
}
}
}
else
{
// Ensure the item's connection_points map
// has been updated
}
}
static void
{
{
{
// 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)) {
// 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.
}
}
}
/*
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 :