Code

Merge and cleanup of GSoC C++-ification project.
[inkscape.git] / src / connector-context.cpp
index a419af44aea6b6b03437cafea56125548dac4eeb..b1061c1247a47452fb5b6d2aa5d30d4504ed5808 100644 (file)
@@ -3,6 +3,7 @@
  *
  * Authors:
  *   Michael Wybrow <mjwybrow@users.sourceforge.net>
+ *   Abhishek Sharma
  *
  * Copyright (C) 2005-2008  Michael Wybrow
  * Copyright (C) 2009  Monash University
 #include "sp-flowtext.h"
 #include "display/curve.h"
 
+using Inkscape::DocumentUndo;
+
 static void sp_connector_context_class_init(SPConnectorContextClass *klass);
 static void sp_connector_context_init(SPConnectorContext *conn_context);
 static void sp_connector_context_dispose(GObject *object);
@@ -768,7 +771,6 @@ connector_handle_button_press(SPConnectorContext *const cc, GdkEventButton const
             Geom::Point const event_dt = cc->desktop->w2d(event_w);
 
             SnapManager &m = cc->desktop->namedview->snap_manager;
-            m.setup(cc->desktop);
 
             switch (cc->state) {
                 case SP_CONNECTOR_CONTEXT_STOP:
@@ -790,7 +792,9 @@ connector_handle_button_press(SPConnectorContext *const cc, GdkEventButton const
                         if (!found) {
                             // This is the first point, so just snap it to the grid
                             // as there's no other points to go off.
+                            m.setup(cc->desktop);
                             m.freeSnapReturnByRef(p, Inkscape::SNAPSOURCE_OTHER_HANDLE);
+                            m.unSetup();
                         }
                         spcc_connector_set_initial_point(cc, p);
 
@@ -802,7 +806,9 @@ connector_handle_button_press(SPConnectorContext *const cc, GdkEventButton const
                 case SP_CONNECTOR_CONTEXT_DRAGGING:
                 {
                     // This is the second click of a connector creation.
+                    m.setup(cc->desktop);
                     m.freeSnapReturnByRef(p, Inkscape::SNAPSOURCE_OTHER_HANDLE);
+                    m.unSetup();
 
                     spcc_connector_set_subsequent_point(cc, p);
                     spcc_connector_finish_segment(cc, p);
@@ -942,7 +948,6 @@ connector_handle_motion_notify(SPConnectorContext *const cc, GdkEventMotion cons
     if ( cc->mode == SP_CONNECTOR_CONTEXT_DRAWING_MODE )
     {
         SnapManager &m = dt->namedview->snap_manager;
-        m.setup(dt);
 
         switch (cc->state) {
             case SP_CONNECTOR_CONTEXT_DRAGGING:
@@ -950,7 +955,9 @@ connector_handle_motion_notify(SPConnectorContext *const cc, GdkEventMotion cons
                 gobble_motion_events(mevent.state);
                 // This is movement during a connector creation.
                 if ( cc->npoints > 0 ) {
+                    m.setup(dt);
                     m.freeSnapReturnByRef(p, Inkscape::SNAPSOURCE_OTHER_HANDLE);
+                    m.unSetup();
                     cc->selection->clear();
                     spcc_connector_set_subsequent_point(cc, p);
                     ret = TRUE;
@@ -962,7 +969,9 @@ connector_handle_motion_notify(SPConnectorContext *const cc, GdkEventMotion cons
                 gobble_motion_events(GDK_BUTTON1_MASK);
                 g_assert( SP_IS_PATH(cc->clickeditem));
 
+                m.setup(dt);
                 m.freeSnapReturnByRef(p, Inkscape::SNAPSOURCE_OTHER_HANDLE);
+                m.unSetup();
 
                 // Update the hidden path
                 Geom::Matrix i2d = (cc->clickeditem)->i2d_affine();
@@ -993,7 +1002,9 @@ connector_handle_motion_notify(SPConnectorContext *const cc, GdkEventMotion cons
                 break;
             default:
                 if (!sp_event_context_knot_mouseover(cc)) {
+                    m.setup(dt);
                     m.preSnap(Inkscape::SnapCandidatePoint(p, Inkscape::SNAPSOURCE_OTHER_HANDLE));
+                    m.unSetup();
                 }
                 break;
         }
@@ -1028,7 +1039,6 @@ connector_handle_button_release(SPConnectorContext *const cc, GdkEventButton con
         SPDocument *doc = sp_desktop_document(desktop);
 
         SnapManager &m = desktop->namedview->snap_manager;
-        m.setup(desktop);
 
         Geom::Point const event_w(revent.x, revent.y);
 
@@ -1040,7 +1050,9 @@ connector_handle_button_release(SPConnectorContext *const cc, GdkEventButton con
                 //case SP_CONNECTOR_CONTEXT_POINT:
                 case SP_CONNECTOR_CONTEXT_DRAGGING:
                 {
+                    m.setup(desktop);
                     m.freeSnapReturnByRef(p, Inkscape::SNAPSOURCE_OTHER_HANDLE);
+                    m.unSetup();
 
                     if (cc->within_tolerance)
                     {
@@ -1061,10 +1073,12 @@ connector_handle_button_release(SPConnectorContext *const cc, GdkEventButton con
                 }
                 case SP_CONNECTOR_CONTEXT_REROUTING:
                 {
+                    m.setup(desktop);
                     m.freeSnapReturnByRef(p, Inkscape::SNAPSOURCE_OTHER_HANDLE);
+                    m.unSetup();
                     cc_connector_rerouting_finish(cc, &p);
 
-                    doc->ensure_up_to_date();
+                    doc->ensureUpToDate();
                     cc->state = SP_CONNECTOR_CONTEXT_IDLE;
                     return TRUE;
                     break;
@@ -1085,7 +1099,9 @@ connector_handle_button_release(SPConnectorContext *const cc, GdkEventButton con
 
                     if (!cc->within_tolerance)
                     {
+                        m.setup(desktop);
                         m.freeSnapReturnByRef(p, Inkscape::SNAPSOURCE_OTHER_HANDLE);
+                        m.unSetup();
                         sp_knot_set_position(cc->selected_handle, p, 0);
                         ConnectionPoint& cp = cc->connpthandles[cc->selected_handle];
                         cp.pos = p * (cc->active_shape)->dt2i_affine();
@@ -1098,7 +1114,9 @@ connector_handle_button_release(SPConnectorContext *const cc, GdkEventButton con
 
 
                 case SP_CONNECTOR_CONTEXT_NEWCONNPOINT:
+                    m.setup(desktop);
                     m.freeSnapReturnByRef(p, Inkscape::SNAPSOURCE_OTHER_HANDLE);
+                    m.unSetup();
 
                     sp_knot_set_position(cc->selected_handle, p, 0);
 
@@ -1108,7 +1126,7 @@ connector_handle_button_release(SPConnectorContext *const cc, GdkEventButton con
                     cp.dir = Avoid::ConnDirAll;
                     g_object_unref(cc->selected_handle);
                     cc->active_shape->avoidRef->addConnectionPoint(cp);
-                    doc->ensure_up_to_date();
+                    doc->ensureUpToDate();
                     for (ConnectionPointMap::iterator it = cc->connpthandles.begin(); it != cc->connpthandles.end(); ++it)
                         if (it->second.type == ConnPointUserDefined && it->second.id == cp.id)
                         {
@@ -1152,7 +1170,7 @@ connector_handle_key_press(SPConnectorContext *const cc, guint const keyval)
 
                     cc_connector_rerouting_finish(cc, NULL);
 
-                    SPDocumentUndo::undo(doc);
+                    DocumentUndo::undo(doc);
 
                     cc->state = SP_CONNECTOR_CONTEXT_IDLE;
                     desktop->messageStack()->flash( Inkscape::NORMAL_MESSAGE,
@@ -1193,15 +1211,15 @@ connector_handle_key_press(SPConnectorContext *const cc, guint const keyval)
                 {
                     // Put connection point at current position
 
-                    SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(cc);
-                    SnapManager &m = desktop->namedview->snap_manager;
-                    m.setup(desktop);
                     Geom::Point p = cc->selected_handle->pos;
-//                     SPEventContext* event_context = SP_EVENT_CONTEXT( cc );
 
                     if (!cc->within_tolerance)
                     {
+                        SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(cc);
+                        SnapManager &m = desktop->namedview->snap_manager;
+                        m.setup(desktop);
                         m.freeSnapReturnByRef(p, Inkscape::SNAPSOURCE_OTHER_HANDLE);
+                        m.unSetup();
                         sp_knot_set_position(cc->selected_handle, p, 0);
                         ConnectionPoint& cp = cc->connpthandles[cc->selected_handle];
                         cp.pos = p * (cc->active_shape)->dt2i_affine();
@@ -1228,9 +1246,8 @@ connector_handle_key_press(SPConnectorContext *const cc, guint const keyval)
                     SnapManager &m = desktop->namedview->snap_manager;
                     m.setup(desktop);
                     Geom::Point p = cc->selected_handle->pos;
-
                     m.freeSnapReturnByRef(p, Inkscape::SNAPSOURCE_OTHER_HANDLE);
-
+                    m.unSetup();
                     sp_knot_set_position(cc->selected_handle, p, 0);
 
                     ConnectionPoint cp;
@@ -1239,7 +1256,7 @@ connector_handle_key_press(SPConnectorContext *const cc, guint const keyval)
                     cp.dir = Avoid::ConnDirAll;
                     g_object_unref(cc->selected_handle);
                     cc->active_shape->avoidRef->addConnectionPoint(cp);
-                    doc->ensure_up_to_date();
+                    doc->ensureUpToDate();
                     for (ConnectionPointMap::iterator it = cc->connpthandles.begin(); it != cc->connpthandles.end(); ++it)
                         if (it->second.type == ConnPointUserDefined && it->second.id == cp.id)
                         {
@@ -1286,16 +1303,12 @@ cc_connector_rerouting_finish(SPConnectorContext *const cc, Geom::Point *const p
 
         if (found) {
             if (cc->clickedhandle == cc->endpt_handle[0]) {
-                (cc->clickeditem)->setAttribute(
-                        "inkscape:connection-start", shape_label, false);
-                (cc->clickeditem)->setAttribute(
-                        "inkscape:connection-start-point", cpid, false);
+                cc->clickeditem->setAttribute("inkscape:connection-start", shape_label, false);
+                cc->clickeditem->setAttribute("inkscape:connection-start-point", cpid, false);
             }
             else {
-                (cc->clickeditem)->setAttribute(
-                        "inkscape:connection-end", shape_label, false);
-                (cc->clickeditem)->setAttribute(
-                        "inkscape:connection-end-point", cpid, false);
+                cc->clickeditem->setAttribute("inkscape:connection-end", shape_label, false);
+                cc->clickeditem->setAttribute("inkscape:connection-end-point", cpid, false);
             }
             g_free(shape_label);
         }
@@ -1303,7 +1316,7 @@ cc_connector_rerouting_finish(SPConnectorContext *const cc, Geom::Point *const p
     cc->clickeditem->setHidden(false);
     sp_conn_reroute_path_immediate(SP_PATH(cc->clickeditem));
     cc->clickeditem->updateRepr();
-    SPDocumentUndo::done(doc, SP_VERB_CONTEXT_CONNECTOR,
+    DocumentUndo::done(doc, SP_VERB_CONTEXT_CONNECTOR,
                      _("Reroute connector"));
     cc_set_active_conn(cc, cc->clickeditem);
 }
@@ -1415,7 +1428,7 @@ spcc_flush_white(SPConnectorContext *cc, SPCurve *gc)
 
     SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(cc);
     SPDocument *doc = sp_desktop_document(desktop);
-    Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
+    Inkscape::XML::Document *xml_doc = doc->getReprDoc();
 
     if ( c && !c->is_empty() ) {
         /* We actually have something to write */
@@ -1434,32 +1447,30 @@ spcc_flush_white(SPConnectorContext *cc, SPCurve *gc)
         cc->newconn->transform = SP_ITEM(desktop->currentLayer())->i2doc_affine().inverse();
 
         bool connection = false;
-        (cc->newconn)->setAttribute( "inkscape:connector-type",
-                cc->isOrthogonal ? "orthogonal" : "polyline", false);
-        (cc->newconn)->setAttribute( "inkscape:connector-curvature",
-                Glib::Ascii::dtostr(cc->curvature).c_str(), false);
+        cc->newconn->setAttribute( "inkscape:connector-type",
+                                   cc->isOrthogonal ? "orthogonal" : "polyline", false );
+        cc->newconn->setAttribute( "inkscape:connector-curvature",
+                                   Glib::Ascii::dtostr(cc->curvature).c_str(), false );
         if (cc->shref)
         {
-            (cc->newconn)->setAttribute( "inkscape:connection-start",
-                    cc->shref, false);
-            if (cc->scpid)
-                (cc->newconn)->setAttribute( "inkscape:connection-start-point",
-                        cc->scpid, false);
+            cc->newconn->setAttribute( "inkscape:connection-start", cc->shref, false);
+            if (cc->scpid) {
+                cc->newconn->setAttribute( "inkscape:connection-start-point", cc->scpid, false);
+            }
             connection = true;
         }
 
         if (cc->ehref)
         {
-            (cc->newconn)->setAttribute( "inkscape:connection-end",
-                    cc->ehref, false);
-            if (cc->ecpid)
-                (cc->newconn)->setAttribute( "inkscape:connection-end-point",
-                        cc->ecpid, false);
+            cc->newconn->setAttribute( "inkscape:connection-end", cc->ehref, false);
+            if (cc->ecpid) {
+                cc->newconn->setAttribute( "inkscape:connection-end-point", cc->ecpid, false);
+            }
             connection = true;
         }
         // Process pending updates.
         cc->newconn->updateRepr();
-        doc->ensure_up_to_date();
+        doc->ensureUpToDate();
 
         if (connection) {
             // Adjust endpoints to shape edge.
@@ -1477,7 +1488,7 @@ spcc_flush_white(SPConnectorContext *cc, SPCurve *gc)
 
     c->unref();
 
-    SPDocumentUndo::done(doc, SP_VERB_CONTEXT_CONNECTOR, _("Create connector"));
+    DocumentUndo::done(doc, SP_VERB_CONTEXT_CONNECTOR, _("Create connector"));
 }
 
 
@@ -1693,7 +1704,7 @@ static void cc_set_active_shape(SPConnectorContext *cc, SPItem *item)
 
         // Ensure the item's connection_points map
         // has been updated
-        SP_OBJECT_DOCUMENT(item)->ensure_up_to_date();
+        item->document->ensureUpToDate();
 
         std::set<int> seen;
         for  ( ConnectionPointMap::iterator it = cc->connpthandles.begin(); it != cc->connpthandles.end() ;)
@@ -1756,12 +1767,22 @@ cc_set_active_conn(SPConnectorContext *cc, SPItem *item)
 
     if (cc->active_conn == item)
     {
-        // Just adjust handle positions.
-        Geom::Point startpt = *(curve->first_point()) * i2d;
-        sp_knot_set_position(cc->endpt_handle[0], startpt, 0);
+        if (curve->is_empty())
+        {
+            // Connector is invisible because it is clipped to the boundary of
+            // two overlpapping shapes.
+            sp_knot_hide(cc->endpt_handle[0]);
+            sp_knot_hide(cc->endpt_handle[1]);
+        }
+        else
+        {
+            // Just adjust handle positions.
+            Geom::Point startpt = *(curve->first_point()) * i2d;
+            sp_knot_set_position(cc->endpt_handle[0], startpt, 0);
 
-        Geom::Point endpt = *(curve->last_point()) * i2d;
-        sp_knot_set_position(cc->endpt_handle[1], endpt, 0);
+            Geom::Point endpt = *(curve->last_point()) * i2d;
+            sp_knot_set_position(cc->endpt_handle[1], endpt, 0);
+        }
 
         return;
     }
@@ -1824,6 +1845,13 @@ cc_set_active_conn(SPConnectorContext *cc, SPItem *item)
                 G_CALLBACK(endpt_handler), cc);
     }
 
+    if (curve->is_empty())
+    {
+        // Connector is invisible because it is clipped to the boundary 
+        // of two overlpapping shapes.  So, it doesn't need endpoints.
+        return;
+    }
+
     Geom::Point startpt = *(curve->first_point()) * i2d;
     sp_knot_set_position(cc->endpt_handle[0], startpt, 0);
 
@@ -1887,8 +1915,10 @@ static bool cc_item_is_shape(SPItem *item)
 bool cc_item_is_connector(SPItem *item)
 {
     if (SP_IS_PATH(item)) {
-        if (SP_PATH(item)->connEndPair.isAutoRoutingConn()) {
-            g_assert( SP_PATH(item)->original_curve ? !(SP_PATH(item)->original_curve->is_closed()) : !(SP_PATH(item)->curve->is_closed()) );
+        bool closed = SP_PATH(item)->original_curve ? SP_PATH(item)->original_curve->is_closed() : SP_PATH(item)->curve->is_closed();
+        if (SP_PATH(item)->connEndPair.isAutoRoutingConn() && !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;
         }
     }
@@ -1917,8 +1947,7 @@ void cc_selection_set_avoid(bool const set_avoid)
         char const *value = (set_avoid) ? "true" : NULL;
 
         if (cc_item_is_shape(item)) {
-            item->setAttribute("inkscape:connector-avoid",
-                    value, false);
+            item->setAttribute("inkscape:connector-avoid", value, false);
             item->avoidRef->handleSettingChange();
             changes++;
         }
@@ -1935,7 +1964,7 @@ void cc_selection_set_avoid(bool const set_avoid)
     char *event_desc = (set_avoid) ?
             _("Make connectors avoid selected objects") :
             _("Make connectors ignore selected objects");
-    SPDocumentUndo::done(document, SP_VERB_CONTEXT_CONNECTOR, event_desc);
+    DocumentUndo::done(document, SP_VERB_CONTEXT_CONNECTOR, event_desc);
 }
 
 
@@ -2019,4 +2048,4 @@ shape_event_attr_changed(Inkscape::XML::Node *repr, gchar const *name,
   fill-column:99
   End:
 */
-// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :
+// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :