Code

Merge and cleanup of GSoC C++-ification project.
[inkscape.git] / src / spiral-context.cpp
index b42195c6069d873aaa84328d5dee89808b0c5808..75488519273826d5040bac4f35714e9e042aed0b 100644 (file)
@@ -1,5 +1,3 @@
-#define __SP_SPIRAL_CONTEXT_C__
-
 /*
  * Spiral drawing context
  *
@@ -7,6 +5,8 @@
  *   Mitsuru Oka
  *   Lauris Kaplinski <lauris@kaplinski.com>
  *   bulia byak <buliabyak@users.sf.net>
+ *   Jon A. Cruz <jon@joncruz.org>
+ *   Abhishek Sharma
  *
  * Copyright (C) 1999-2001 Lauris Kaplinski
  * Copyright (C) 2001-2002 Mitsuru Oka
@@ -27,7 +27,6 @@
 #include "sp-namedview.h"
 #include "selection.h"
 #include "desktop-handles.h"
-#include "desktop-affine.h"
 #include "snap.h"
 #include "desktop.h"
 #include "desktop-style.h"
 #include "object-edit.h"
 #include "xml/repr.h"
 #include "xml/node-event-vector.h"
-#include "prefs-utils.h"
+#include "preferences.h"
 #include "context-fns.h"
+#include "shape-editor.h"
+
+using Inkscape::DocumentUndo;
 
 static void sp_spiral_context_class_init(SPSpiralContextClass * klass);
 static void sp_spiral_context_init(SPSpiralContext *spiral_context);
 static void sp_spiral_context_dispose(GObject *object);
 static void sp_spiral_context_setup(SPEventContext *ec);
-static void sp_spiral_context_set(SPEventContext *ec, gchar const *key, gchar const *val);
+static void sp_spiral_context_finish(SPEventContext *ec);
+static void sp_spiral_context_set(SPEventContext *ec, Inkscape::Preferences::Entry *val);
 
 static gint sp_spiral_context_root_handler(SPEventContext *event_context, GdkEvent *event);
 
-static void sp_spiral_drag(SPSpiralContext *sc, NR::Point p, guint state);
+static void sp_spiral_drag(SPSpiralContext *sc, Geom::Point p, guint state);
 static void sp_spiral_finish(SPSpiralContext *sc);
+static void sp_spiral_cancel(SPSpiralContext *sc);
 
 static SPEventContextClass *parent_class;
 
@@ -86,6 +90,7 @@ sp_spiral_context_class_init(SPSpiralContextClass *klass)
     object_class->dispose = sp_spiral_context_dispose;
 
     event_context_class->setup = sp_spiral_context_setup;
+    event_context_class->finish = sp_spiral_context_finish;
     event_context_class->set = sp_spiral_context_set;
     event_context_class->root_handler = sp_spiral_context_root_handler;
 }
@@ -104,9 +109,6 @@ sp_spiral_context_init(SPSpiralContext *spiral_context)
     event_context->within_tolerance = false;
     event_context->item_to_select = NULL;
 
-    event_context->shape_repr = NULL;
-    event_context->shape_knot_holder = NULL;
-
     spiral_context->item = NULL;
 
     spiral_context->revo = 3.0;
@@ -116,6 +118,20 @@ sp_spiral_context_init(SPSpiralContext *spiral_context)
     new (&spiral_context->sel_changed_connection) sigc::connection();
 }
 
+static void sp_spiral_context_finish(SPEventContext *ec)
+{
+    SPSpiralContext *sc = SP_SPIRAL_CONTEXT(ec);
+       SPDesktop *desktop = ec->desktop;
+
+       sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate), GDK_CURRENT_TIME);
+       sp_spiral_finish(sc);
+    sc->sel_changed_connection.disconnect();
+
+    if (((SPEventContextClass *) parent_class)->finish) {
+               ((SPEventContextClass *) parent_class)->finish(ec);
+       }
+}
+
 static void
 sp_spiral_context_dispose(GObject *object)
 {
@@ -127,20 +143,12 @@ sp_spiral_context_dispose(GObject *object)
     sc->sel_changed_connection.disconnect();
     sc->sel_changed_connection.~connection();
 
+    delete ec->shape_editor;
+    ec->shape_editor = NULL;
+
     /* fixme: This is necessary because we do not grab */
     if (sc->item) sp_spiral_finish(sc);
 
-    if (ec->shape_knot_holder) {
-        delete ec->shape_knot_holder;
-        ec->shape_knot_holder = NULL;
-    }
-
-    if (ec->shape_repr) { // remove old listener
-        sp_repr_remove_listener_by_data(ec->shape_repr, ec);
-        Inkscape::GC::release(ec->shape_repr);
-        ec->shape_repr = 0;
-    }
-
     if (sc->_message_context) {
         delete sc->_message_context;
     }
@@ -148,14 +156,6 @@ sp_spiral_context_dispose(GObject *object)
     G_OBJECT_CLASS(parent_class)->dispose(object);
 }
 
-static Inkscape::XML::NodeEventVector ec_shape_repr_events = {
-    NULL, /* child_added */
-    NULL, /* child_removed */
-    ec_shape_event_attr_changed,
-    NULL, /* content_changed */
-    NULL  /* order_changed */
-};
-
 /**
 \brief  Callback that processes the "changed" signal on the selection;
 destroys old and creates new knotholder
@@ -166,27 +166,9 @@ sp_spiral_context_selection_changed(Inkscape::Selection *selection, gpointer dat
     SPSpiralContext *sc = SP_SPIRAL_CONTEXT(data);
     SPEventContext *ec = SP_EVENT_CONTEXT(sc);
 
-    if (ec->shape_knot_holder) { // desktroy knotholder
-        delete ec->shape_knot_holder;
-        ec->shape_knot_holder = NULL;
-    }
-
-    if (ec->shape_repr) { // remove old listener
-        sp_repr_remove_listener_by_data(ec->shape_repr, ec);
-        Inkscape::GC::release(ec->shape_repr);
-        ec->shape_repr = 0;
-    }
-
+    ec->shape_editor->unset_item(SH_KNOTHOLDER);
     SPItem *item = selection->singleItem();
-    if (item) {
-        ec->shape_knot_holder = sp_item_knot_holder(item, ec->desktop);
-        Inkscape::XML::Node *shape_repr = SP_OBJECT_REPR(item);
-        if (shape_repr) {
-            ec->shape_repr = shape_repr;
-            Inkscape::GC::anchor(shape_repr);
-            sp_repr_add_listener(shape_repr, &ec_shape_repr_events, ec);
-        }
-    }
+    ec->shape_editor->set_item(item, SH_KNOTHOLDER);
 }
 
 static void
@@ -201,27 +183,22 @@ sp_spiral_context_setup(SPEventContext *ec)
     sp_event_context_read(ec, "revolution");
     sp_event_context_read(ec, "t0");
 
-    Inkscape::Selection *selection = sp_desktop_selection(ec->desktop);
+    ec->shape_editor = new ShapeEditor(ec->desktop);
 
-    SPItem *item = selection->singleItem();
+    SPItem *item = sp_desktop_selection(ec->desktop)->singleItem();
     if (item) {
-        ec->shape_knot_holder = sp_item_knot_holder(item, ec->desktop);
-        Inkscape::XML::Node *shape_repr = SP_OBJECT_REPR(item);
-        if (shape_repr) {
-            ec->shape_repr = shape_repr;
-            Inkscape::GC::anchor(shape_repr);
-            sp_repr_add_listener(shape_repr, &ec_shape_repr_events, ec);
-        }
+        ec->shape_editor->set_item(item, SH_KNOTHOLDER);
     }
 
+    Inkscape::Selection *selection = sp_desktop_selection(ec->desktop);
     sc->sel_changed_connection.disconnect();
     sc->sel_changed_connection = selection->connectChanged(sigc::bind(sigc::ptr_fun(&sp_spiral_context_selection_changed), (gpointer)sc));
 
-    if (prefs_get_int_attribute("tools.shapes", "selcue", 0) != 0) {
+    Inkscape::Preferences *prefs = Inkscape::Preferences::get();
+    if (prefs->getBool("/tools/shapes/selcue")) {
         ec->enableSelectionCue();
     }
-
-    if (prefs_get_int_attribute("tools.shapes", "gradientdrag", 0) != 0) {
+    if (prefs->getBool("/tools/shapes/gradientdrag")) {
         ec->enableGrDrag();
     }
 
@@ -229,19 +206,17 @@ sp_spiral_context_setup(SPEventContext *ec)
 }
 
 static void
-sp_spiral_context_set(SPEventContext *ec, gchar const *key, gchar const *val)
+sp_spiral_context_set(SPEventContext *ec, Inkscape::Preferences::Entry *val)
 {
     SPSpiralContext *sc = SP_SPIRAL_CONTEXT(ec);
-
-    if (!strcmp(key, "expansion")) {
-        sc->exp = (val) ? g_ascii_strtod(val, NULL) : 1.0;
-        sc->exp = CLAMP(sc->exp, 0.0, 1000.0);
-    } else if (!strcmp(key, "revolution")) {
-        sc->revo = (val) ? g_ascii_strtod(val, NULL) : 3.0;
-        sc->revo = CLAMP(sc->revo, 0.05, 40.0);
-    } else if (!strcmp(key, "t0")) {
-        sc->t0 = (val) ? g_ascii_strtod(val, NULL) : 0.0;
-        sc->t0 = CLAMP(sc->t0, 0.0, 0.999);
+    Glib::ustring name = val->getEntryName();
+
+    if (name == "expansion") {
+        sc->exp = CLAMP(val->getDouble(), 0.0, 1000.0);
+    } else if (name == "revolution") {
+        sc->revo = CLAMP(val->getDouble(3.0), 0.05, 40.0);
+    } else if (name == "t0") {
+        sc->t0 = CLAMP(val->getDouble(), 0.0, 0.999);
     }
 }
 
@@ -254,7 +229,8 @@ sp_spiral_context_root_handler(SPEventContext *event_context, GdkEvent *event)
     Inkscape::Selection *selection = sp_desktop_selection (desktop);
     SPSpiralContext *sc = SP_SPIRAL_CONTEXT(event_context);
 
-    event_context->tolerance = prefs_get_int_attribute_limited("options.dragtolerance", "value", 0, 0, 100);
+    Inkscape::Preferences *prefs = Inkscape::Preferences::get();
+    event_context->tolerance = prefs->getIntLimited("/options/dragtolerance/value", 0, 0, 100);
 
     gint ret = FALSE;
 
@@ -266,11 +242,9 @@ sp_spiral_context_root_handler(SPEventContext *event_context, GdkEvent *event)
                 sc->center = Inkscape::setup_for_drag_start(desktop, event_context, event);
 
                 SnapManager &m = desktop->namedview->snap_manager;
-                m.setup(desktop, NULL); //null, because we don't have an item yet
-                Geom::Point pt2g = to_2geom(sc->center);
-                m.freeSnapReturnByRef(Inkscape::Snapper::SNAPPOINT_NODE, pt2g);
-                sc->center = from_2geom(pt2g);
-
+                m.setup(desktop);
+                m.freeSnapReturnByRef(sc->center, Inkscape::SNAPSOURCE_NODE_HANDLE);
+                m.unSetup();
                 sp_canvas_item_grab(SP_CANVAS_ITEM(desktop->acetate),
                                     ( GDK_KEY_PRESS_MASK |
                                       GDK_BUTTON_RELEASE_MASK |
@@ -294,23 +268,32 @@ sp_spiral_context_root_handler(SPEventContext *event_context, GdkEvent *event)
                 // motion notify coordinates as given (no snapping back to origin)
                 event_context->within_tolerance = false;
 
-                NR::Point const motion_w(event->motion.x, event->motion.y);
-                Geom::Point motion_dt(to_2geom(event_context->desktop->w2d(motion_w)));
-                
+                Geom::Point const motion_w(event->motion.x, event->motion.y);
+                Geom::Point motion_dt(event_context->desktop->w2d(motion_w));
+
                 SnapManager &m = desktop->namedview->snap_manager;
-                m.setup(desktop, sc->item);
-                m.freeSnapReturnByRef(Inkscape::Snapper::SNAPPOINT_NODE, motion_dt);
+                m.setup(desktop, true, sc->item);
+                m.freeSnapReturnByRef(motion_dt, Inkscape::SNAPSOURCE_NODE_HANDLE);
+                m.unSetup();
                 sp_spiral_drag(sc, from_2geom(motion_dt), event->motion.state);
 
                 gobble_motion_events(GDK_BUTTON1_MASK);
 
                 ret = TRUE;
+            } else if (!sp_event_context_knot_mouseover(sc)) {
+                SnapManager &m = desktop->namedview->snap_manager;
+                m.setup(desktop);
+                Geom::Point const motion_w(event->motion.x, event->motion.y);
+                Geom::Point motion_dt(desktop->w2d(motion_w));
+                m.preSnap(Inkscape::SnapCandidatePoint(motion_dt, Inkscape::SNAPSOURCE_NODE_HANDLE));
+                m.unSetup();
             }
             break;
         case GDK_BUTTON_RELEASE:
             event_context->xp = event_context->yp = 0;
             if (event->button.button == 1 && !event_context->space_panning) {
                 dragging = FALSE;
+                sp_event_context_discard_delayed_snap_event(event_context);
                 if (!event_context->within_tolerance) {
                     // we've been dragging, finish the spiral
                     sp_spiral_finish(sc);
@@ -361,17 +344,23 @@ sp_spiral_context_root_handler(SPEventContext *event_context, GdkEvent *event)
                     }
                     break;
                 case GDK_Escape:
-                    sp_desktop_selection(desktop)->clear();
-                    //TODO: make dragging escapable by Esc
-                    break;
+                       if (dragging) {
+                               dragging = false;
+                               sp_event_context_discard_delayed_snap_event(event_context);
+                               // if drawing, cancel, otherwise pass it up for deselecting
+                               sp_spiral_cancel(sc);
+                               ret = TRUE;
+                       }
+                       break;
 
                 case GDK_space:
                     if (dragging) {
                         sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate),
                                               event->button.time);
                         dragging = false;
+                        sp_event_context_discard_delayed_snap_event(event_context);
                         if (!event_context->within_tolerance) {
-                            // we've been dragging, finish the rect
+                            // we've been dragging, finish the spiral
                             sp_spiral_finish(sc);
                         }
                         // do not return true, so that space would work switching to selector
@@ -410,12 +399,12 @@ sp_spiral_context_root_handler(SPEventContext *event_context, GdkEvent *event)
     return ret;
 }
 
-static void
-sp_spiral_drag(SPSpiralContext *sc, NR::Point p, guint state)
+static void sp_spiral_drag(SPSpiralContext *sc, Geom::Point p, guint state)
 {
     SPDesktop *desktop = SP_EVENT_CONTEXT(sc)->desktop;
 
-    int const snaps = prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12);
+    Inkscape::Preferences *prefs = Inkscape::Preferences::get();
+    int const snaps = prefs->getInt("/options/rotationsnapsperpi/value", 12);
 
     if (!sc->item) {
 
@@ -423,34 +412,36 @@ sp_spiral_drag(SPSpiralContext *sc, NR::Point p, guint state)
             return;
         }
 
-        /* Create object */
-        Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_EVENT_CONTEXT_DOCUMENT(sc));
+        // Create object
+        Inkscape::XML::Document *xml_doc = SP_EVENT_CONTEXT_DOCUMENT(sc)->getReprDoc();
         Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
         repr->setAttribute("sodipodi:type", "spiral");
 
-        /* Set style */
-        sp_desktop_apply_style_tool(desktop, repr, "tools.shapes.spiral", false);
+        // Set style
+        sp_desktop_apply_style_tool(desktop, repr, "/tools/shapes/spiral", false);
 
         sc->item = (SPItem *) desktop->currentLayer()->appendChildRepr(repr);
         Inkscape::GC::release(repr);
-        sc->item->transform = SP_ITEM(desktop->currentRoot())->getRelativeTransform(desktop->currentLayer());
+        sc->item->transform = SP_ITEM(desktop->currentLayer())->i2doc_affine().inverse();
         sc->item->updateRepr();
 
         sp_canvas_force_full_redraw_after_interruptions(desktop->canvas, 5);
     }
 
-    Geom::Point const p0 = to_2geom(sp_desktop_dt2root_xy_point(desktop, sc->center));
-    Geom::Point p1 = to_2geom(sp_desktop_dt2root_xy_point(desktop, p));
     SnapManager &m = desktop->namedview->snap_manager;
-    m.setup(desktop, sc->item);
-    m.freeSnapReturnByRef(Inkscape::Snapper::SNAPPOINT_NODE, p1);
+    m.setup(desktop, true, sc->item);
+    Geom::Point pt2g = to_2geom(p);
+    m.freeSnapReturnByRef(pt2g, Inkscape::SNAPSOURCE_NODE_HANDLE);
+    m.unSetup();
+    Geom::Point const p0 = desktop->dt2doc(sc->center);
+    Geom::Point const p1 = desktop->dt2doc(pt2g);
 
     SPSpiral *spiral = SP_SPIRAL(sc->item);
 
     Geom::Point const delta = p1 - p0;
     gdouble const rad = Geom::L2(delta);
 
-    gdouble arg = NR::atan2(delta) - 2.0*M_PI*spiral->revo;
+    gdouble arg = Geom::atan2(delta) - 2.0*M_PI*spiral->revo;
 
     if (state & GDK_CONTROL_MASK) {
         arg = sp_round(arg, M_PI/snaps);
@@ -477,22 +468,48 @@ sp_spiral_finish(SPSpiralContext *sc)
     sc->_message_context->clear();
 
     if (sc->item != NULL) {
-        SPDesktop *desktop = SP_EVENT_CONTEXT(sc)->desktop;
-        SPSpiral  *spiral = SP_SPIRAL(sc->item);
+       SPSpiral *spiral = SP_SPIRAL(sc->item);
+       if (spiral->rad == 0) {
+               sp_spiral_cancel(sc); // Don't allow the creating of zero sized spiral, for example when the start and and point snap to the snap grid point
+               return;
+       }
+
+       SPDesktop *desktop = SP_EVENT_CONTEXT(sc)->desktop;
 
-        sp_shape_set_shape(SP_SHAPE(spiral));
+        SP_SHAPE(spiral)->setShape();
         SP_OBJECT(spiral)->updateRepr(SP_OBJECT_WRITE_EXT);
 
         sp_canvas_end_forced_full_redraws(desktop->canvas);
 
         sp_desktop_selection(desktop)->set(sc->item);
-        sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_SPIRAL, 
-                         _("Create spiral"));
+        DocumentUndo::done(sp_desktop_document(desktop), SP_VERB_CONTEXT_SPIRAL,
+                           _("Create spiral"));
 
         sc->item = NULL;
     }
 }
 
+static void sp_spiral_cancel(SPSpiralContext *sc)
+{
+       SPDesktop *desktop = SP_EVENT_CONTEXT(sc)->desktop;
+
+       sp_desktop_selection(desktop)->clear();
+       sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate), 0);
+
+    if (sc->item != NULL) {
+       SP_OBJECT(sc->item)->deleteObject();
+       sc->item = NULL;
+    }
+
+    sc->within_tolerance = false;
+    sc->xp = 0;
+    sc->yp = 0;
+    sc->item_to_select = NULL;
+
+    sp_canvas_end_forced_full_redraws(desktop->canvas);
+
+    DocumentUndo::cancel(sp_desktop_document(desktop));
+}
 
 /*
   Local Variables:
@@ -503,4 +520,4 @@ sp_spiral_finish(SPSpiralContext *sc)
   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 :