Code

small cleanup in src/trace (warnings and trailing spaces)
[inkscape.git] / src / box3d-context.cpp
index a1e033955cc98761b2c8b853123d039bc39b2660..2d20318e36cd5bae58bfb56612adbd5c21b953d7 100644 (file)
@@ -38,9 +38,8 @@
 #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 "inkscape.h"
 #include "desktop-style.h"
 #include "transf_mat_3x4.h"
 #include "perspective-line.h"
@@ -48,6 +47,7 @@
 #include "box3d-side.h"
 #include "document-private.h"
 #include "line-geometry.h"
+#include "shape-editor.h"
 
 static void sp_box3d_context_class_init(Box3DContextClass *klass);
 static void sp_box3d_context_init(Box3DContext *box3d_context);
@@ -109,9 +109,6 @@ static void sp_box3d_context_init(Box3DContext *box3d_context)
     event_context->within_tolerance = false;
     event_context->item_to_select = NULL;
 
-    event_context->shape_repr = NULL;
-    event_context->shape_knot_holder = NULL;
-
     box3d_context->item = NULL;
 
     box3d_context->ctrl_dragged = false;
@@ -135,22 +132,14 @@ static void sp_box3d_context_dispose(GObject *object)
     bc->sel_changed_connection.disconnect();
     bc->sel_changed_connection.~connection();
 
+    delete ec->shape_editor;
+    ec->shape_editor = NULL;
+
     /* fixme: This is necessary because we do not grab */
     if (bc->item) {
         sp_box3d_finish(bc);
     }
 
-    if (ec->shape_knot_holder) {
-        sp_knot_holder_destroy(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 (bc->_message_context) {
         delete bc->_message_context;
     }
@@ -158,14 +147,6 @@ static void sp_box3d_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
@@ -175,35 +156,13 @@ static void sp_box3d_context_selection_changed(Inkscape::Selection *selection, g
     Box3DContext *bc = SP_BOX3D_CONTEXT(data);
     SPEventContext *ec = SP_EVENT_CONTEXT(bc);
 
-    if (ec->shape_knot_holder) { // destroy knotholder
-        sp_knot_holder_destroy(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;
-    }
-
-    SPDocument *doc = sp_desktop_document(bc->desktop);
-    doc->persps_sel.clear();
-    doc->persps_sel = persp3d_currently_selected_persps(ec);
-
-    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->unset_item(SH_KNOTHOLDER);
+    SPItem *item = selection->singleItem(); 
+    ec->shape_editor->set_item(item, SH_KNOTHOLDER);
 
-    if (doc->persps_sel.size() == 1) {
+    if (selection->perspList().size() == 1) {
         // selecting a single box changes the current perspective
-        doc->current_persp3d = *(doc->persps_sel.begin());
+        ec->desktop->doc()->current_persp3d = selection->perspList().front();
     }
 }
 
@@ -235,15 +194,11 @@ static void sp_box3d_context_setup(SPEventContext *ec)
 
     sp_box3d_context_check_for_persp_in_defs(sp_desktop_document (ec->desktop));
 
+    ec->shape_editor = new ShapeEditor(ec->desktop);
+
     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);
     }
 
     bc->sel_changed_connection.disconnect();
@@ -252,12 +207,13 @@ static void sp_box3d_context_setup(SPEventContext *ec)
     );
 
     bc->_vpdrag = new Box3D::VPDrag(sp_desktop_document (ec->desktop));
+    Inkscape::Preferences *prefs = Inkscape::Preferences::get();
 
-    if (prefs_get_int_attribute("tools.shapes", "selcue", 0) != 0) {
+    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();
     }
 
@@ -295,24 +251,25 @@ static gint sp_box3d_context_root_handler(SPEventContext *event_context, GdkEven
 
     SPDesktop *desktop = event_context->desktop;
     Inkscape::Selection *selection = sp_desktop_selection (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);
 
     Box3DContext *bc = SP_BOX3D_CONTEXT(event_context);
     g_assert (SP_ACTIVE_DOCUMENT->current_persp3d);
     Persp3D *cur_persp = SP_ACTIVE_DOCUMENT->current_persp3d;
 
-    event_context->tolerance = prefs_get_int_attribute_limited("options.dragtolerance", "value", 0, 0, 100);
+    event_context->tolerance = prefs->getIntLimited("/options/dragtolerance/value", 0, 0, 100);
 
     gint ret = FALSE;
     switch (event->type) {
     case GDK_BUTTON_PRESS:
         if ( event->button.button == 1  && !event_context->space_panning) {
-            NR::Point const button_w(event->button.x,
-                                     event->button.y);
+            Geom::Point const button_w(event->button.x,
+                                       event->button.y);
 
             // save drag origin
-            event_context->xp = (gint) button_w[NR::X];
-            event_context->yp = (gint) button_w[NR::Y];
+            event_context->xp = (gint) button_w[Geom::X];
+            event_context->yp = (gint) button_w[Geom::Y];
             event_context->within_tolerance = true;
             
             // remember clicked item, *not* disregarding groups (since a 3D box is a group), honoring Alt
@@ -321,27 +278,28 @@ static gint sp_box3d_context_root_handler(SPEventContext *event_context, GdkEven
             dragging = true;
             
             /*  */
-            NR::Point const button_dt(desktop->w2d(button_w));
-            bc->drag_origin = button_dt;
-            bc->drag_ptB = button_dt;
-            bc->drag_ptC = button_dt;
+            Geom::Point button_dt(desktop->w2d(button_w));
+            bc->drag_origin = from_2geom(button_dt);
+            bc->drag_ptB = from_2geom(button_dt);
+            bc->drag_ptC = from_2geom(button_dt);
 
             /* Projective preimages of clicked point under current perspective */
-            bc->drag_origin_proj = cur_persp->tmat.preimage (button_dt, 0, Proj::Z);
+            bc->drag_origin_proj = cur_persp->tmat.preimage (from_2geom(button_dt), 0, Proj::Z);
             bc->drag_ptB_proj = bc->drag_origin_proj;
             bc->drag_ptC_proj = bc->drag_origin_proj;
             bc->drag_ptC_proj.normalize();
             bc->drag_ptC_proj[Proj::Z] = 0.25;
 
             /* Snap center */
-            SnapManager const &m = desktop->namedview->snap_manager;
-            bc->center = m.freeSnap(Inkscape::Snapper::SNAPPOINT_NODE,
-                                    button_dt, bc->item).getPoint();
+            SnapManager &m = desktop->namedview->snap_manager;
+            m.setup(desktop, true, bc->item);
+            m.freeSnapReturnByRef(Inkscape::SnapPreferences::SNAPPOINT_NODE, button_dt);
+            bc->center = from_2geom(button_dt);
 
             sp_canvas_item_grab(SP_CANVAS_ITEM(desktop->acetate),
                                 ( GDK_KEY_PRESS_MASK |
                                   GDK_BUTTON_RELEASE_MASK       |
-                                  GDK_POINTER_MOTION_MASK       |
+                                  GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK       |
                                   GDK_BUTTON_PRESS_MASK ),
                                 NULL, event->button.time);
             ret = TRUE;
@@ -361,12 +319,13 @@ static gint sp_box3d_context_root_handler(SPEventContext *event_context, GdkEven
             // 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);
-            NR::Point motion_dt(desktop->w2d(motion_w));
+            Geom::Point const motion_w(event->motion.x,
+                                       event->motion.y);
+            Geom::Point motion_dt(desktop->w2d(motion_w));
 
-            SnapManager const &m = desktop->namedview->snap_manager;
-            motion_dt = m.freeSnap(Inkscape::Snapper::SNAPPOINT_NODE, motion_dt, bc->item).getPoint();
+            SnapManager &m = desktop->namedview->snap_manager;
+            m.setup(desktop, true, bc->item);
+            m.freeSnapReturnByRef(Inkscape::SnapPreferences::SNAPPOINT_NODE, motion_dt);
 
             bc->ctrl_dragged  = event->motion.state & GDK_CONTROL_MASK;
 
@@ -376,10 +335,10 @@ static gint sp_box3d_context_root_handler(SPEventContext *event_context, GdkEven
             }
 
             if (!bc->extruded) {
-               bc->drag_ptB = motion_dt;
-               bc->drag_ptC = motion_dt;
+               bc->drag_ptB = from_2geom(motion_dt);
+               bc->drag_ptC = from_2geom(motion_dt);
 
-                bc->drag_ptB_proj = cur_persp->tmat.preimage (motion_dt, 0, Proj::Z);
+                bc->drag_ptB_proj = cur_persp->tmat.preimage (from_2geom(motion_dt), 0, Proj::Z);
                 bc->drag_ptC_proj = bc->drag_ptB_proj;
                 bc->drag_ptC_proj.normalize();
                 bc->drag_ptC_proj[Proj::Z] = 0.25;
@@ -389,17 +348,19 @@ static gint sp_box3d_context_root_handler(SPEventContext *event_context, GdkEven
                 if (!bc->ctrl_dragged) {
                     /* snapping */
                     Box3D::PerspectiveLine pline (bc->drag_ptB, Proj::Z, SP_ACTIVE_DOCUMENT->current_persp3d);
-                    bc->drag_ptC = pline.closest_to (motion_dt);
+                    bc->drag_ptC = pline.closest_to (from_2geom(motion_dt));
 
                     bc->drag_ptB_proj.normalize();
                     bc->drag_ptC_proj = cur_persp->tmat.preimage (bc->drag_ptC, bc->drag_ptB_proj[Proj::X], Proj::X);
                 } else {
-                    bc->drag_ptC = motion_dt;
+                    bc->drag_ptC = from_2geom(motion_dt);
 
                     bc->drag_ptB_proj.normalize();
-                    bc->drag_ptC_proj = cur_persp->tmat.preimage (motion_dt, bc->drag_ptB_proj[Proj::X], Proj::X);
+                    bc->drag_ptC_proj = cur_persp->tmat.preimage (from_2geom(motion_dt), bc->drag_ptB_proj[Proj::X], Proj::X);
                 }
-                bc->drag_ptC = m.freeSnap(Inkscape::Snapper::SNAPPOINT_NODE, bc->drag_ptC, bc->item).getPoint();
+                Geom::Point pt2g = to_2geom(bc->drag_ptC);
+                m.freeSnapReturnByRef(Inkscape::SnapPreferences::SNAPPOINT_NODE, pt2g);
+                bc->drag_ptC = from_2geom(pt2g);
             }
 
             sp_box3d_drag(*bc, event->motion.state);
@@ -497,7 +458,7 @@ static gint sp_box3d_context_root_handler(SPEventContext *event_context, GdkEven
         case GDK_g:
         case GDK_G:
             if (MOD__SHIFT_ONLY) {
-                sp_selection_to_guides();
+                sp_selection_to_guides(desktop);
                 ret = true;
             }
             break;
@@ -509,7 +470,7 @@ static gint sp_box3d_context_root_handler(SPEventContext *event_context, GdkEven
                 ret = TRUE;
             }
             if (MOD__SHIFT_ONLY) {
-                persp3d_toggle_VPs(persp3d_currently_selected_persps(event_context), Proj::X);
+                persp3d_toggle_VPs(selection->perspList(), Proj::X);
                 bc->_vpdrag->updateLines(); // FIXME: Shouldn't this be done automatically?
                 ret = true;
             }
@@ -518,7 +479,7 @@ static gint sp_box3d_context_root_handler(SPEventContext *event_context, GdkEven
         case GDK_y:
         case GDK_Y:
             if (MOD__SHIFT_ONLY) {
-                persp3d_toggle_VPs(persp3d_currently_selected_persps(event_context), Proj::Y);
+                persp3d_toggle_VPs(selection->perspList(), Proj::Y);
                 bc->_vpdrag->updateLines(); // FIXME: Shouldn't this be done automatically?
                 ret = true;
             }
@@ -527,7 +488,7 @@ static gint sp_box3d_context_root_handler(SPEventContext *event_context, GdkEven
         case GDK_z:
         case GDK_Z:
             if (MOD__SHIFT_ONLY) {
-                persp3d_toggle_VPs(persp3d_currently_selected_persps(event_context), Proj::Z);
+                persp3d_toggle_VPs(selection->perspList(), Proj::Z);
                 bc->_vpdrag->updateLines(); // FIXME: Shouldn't this be done automatically?
                 ret = true;
             }
@@ -584,7 +545,7 @@ static void sp_box3d_drag(Box3DContext &bc, guint /*state*/)
         repr->setAttribute("sodipodi:type", "inkscape:box3d");
 
         /* Set style */
-        sp_desktop_apply_style_tool (desktop, repr, "tools.shapes.3dbox", false);
+        sp_desktop_apply_style_tool (desktop, repr, "/tools/shapes/3dbox", false);
 
         bc.item = (SPItem *) desktop->currentLayer()->appendChildRepr(repr);
         Inkscape::GC::release(repr);
@@ -670,6 +631,14 @@ static void sp_box3d_finish(Box3DContext *bc)
     bc->extruded = false;
 }
 
+void sp_box3d_context_update_lines(SPEventContext *ec) {
+    /*  update perspective lines if we are in the 3D box tool (so that infinite ones are shown correctly) */
+    if (SP_IS_BOX3D_CONTEXT (ec)) {
+        Box3DContext *bc = SP_BOX3D_CONTEXT (ec);
+        bc->_vpdrag->updateLines();
+    }
+}
+
 /*
   Local Variables:
   mode:c++