Code

Split SPCanvasItem and SPCanvasGroup to individual .h files. Removed forward header.
[inkscape.git] / src / eraser-context.cpp
index 86d5e66dc831aa60d1c11ea76580c9f9ece65a02..8ac765b9e6a87c9f87d344e687effc0a64f9fb13 100644 (file)
@@ -7,6 +7,7 @@
  *   bulia byak <buliabyak@users.sf.net>
  *   MenTaLguY <mental@rydia.net>
  *   Jon A. Cruz <jon@joncruz.org>
+ *   Abhishek Sharma
  *
  * The original dynadraw code:
  *   Paul Haeberli <paul@sgi.com>
@@ -33,8 +34,9 @@
 #include <numeric>
 
 #include "svg/svg.h"
+#include "display/sp-canvas.h"
 #include "display/canvas-bpath.h"
-#include "display/bezier-utils.h"
+#include <2geom/bezier-utils.h>
 
 #include <glib/gmem.h>
 #include "macros.h"
 #include "desktop.h"
 #include "desktop-events.h"
 #include "desktop-handles.h"
-#include "desktop-affine.h"
 #include "desktop-style.h"
 #include "message-context.h"
-#include "prefs-utils.h"
+#include "preferences.h"
 #include "pixmaps/cursor-eraser.xpm"
 #include "xml/repr.h"
 #include "context-fns.h"
@@ -66,6 +67,8 @@
 
 #include "eraser-context.h"
 
+using Inkscape::DocumentUndo;
+
 #define ERC_RED_RGBA 0xff0000ff
 
 #define TOLERANCE_ERASER 0.1
@@ -84,7 +87,7 @@ static void sp_eraser_context_init(SPEraserContext *erc);
 static void sp_eraser_context_dispose(GObject *object);
 
 static void sp_eraser_context_setup(SPEventContext *ec);
-static void sp_eraser_context_set(SPEventContext *ec, gchar const *key, gchar const *val);
+static void sp_eraser_context_set(SPEventContext *ec, Inkscape::Preferences::Entry *val);
 static gint sp_eraser_context_root_handler(SPEventContext *ec, GdkEvent *event);
 
 static void clear_current(SPEraserContext *dc);
@@ -204,7 +207,8 @@ static ProfileFloatElement f_profile[PROFILE_FLOAT_SIZE] = {
 
     erc->_message_context = new Inkscape::MessageContext(desktop->messageStack());
 
-    if (prefs_get_int_attribute("tools.eraser", "selcue", 0) != 0) {
+    Inkscape::Preferences *prefs = Inkscape::Preferences::get();
+    if (prefs->getBool("/tools/eraser/selcue", 0) != 0) {
         ec->enableSelectionCue();
     }
 // TODO temp force:
@@ -213,11 +217,11 @@ static ProfileFloatElement f_profile[PROFILE_FLOAT_SIZE] = {
 }
 
 static void
-sp_eraser_context_set(SPEventContext *ec, gchar const *key, gchar const *val)
+sp_eraser_context_set(SPEventContext *ec, Inkscape::Preferences::Entry *val)
 {
     //pass on up to parent class to handle common attributes.
     if ( eraser_parent_class->set ) {
-        eraser_parent_class->set(ec, key, val);
+        eraser_parent_class->set(ec, val);
     }
 }
 
@@ -231,18 +235,18 @@ flerp(double f0, double f1, double p)
 static Geom::Point
 sp_eraser_get_npoint(SPEraserContext const *dc, Geom::Point v)
 {
-    NR::Rect drect = SP_EVENT_CONTEXT(dc)->desktop->get_display_area();
-    double const max = MAX ( drect.dimensions()[NR::X], drect.dimensions()[NR::Y] );
-    return Geom::Point(( v[NR::X] - drect.min()[NR::X] ) / max,  ( v[NR::Y] - drect.min()[NR::Y] ) / max);
+    Geom::Rect drect = SP_EVENT_CONTEXT(dc)->desktop->get_display_area();
+    double const max = MAX ( drect.dimensions()[Geom::X], drect.dimensions()[Geom::Y] );
+    return Geom::Point(( v[Geom::X] - drect.min()[Geom::X] ) / max,  ( v[Geom::Y] - drect.min()[Geom::Y] ) / max);
 }
 
 /* Get view point */
 static Geom::Point
 sp_eraser_get_vpoint(SPEraserContext const *dc, Geom::Point n)
 {
-    NR::Rect drect = SP_EVENT_CONTEXT(dc)->desktop->get_display_area();
-    double const max = MAX ( drect.dimensions()[NR::X], drect.dimensions()[NR::Y] );
-    return Geom::Point(n[NR::X] * max + drect.min()[NR::X], n[NR::Y] * max + drect.min()[NR::Y]);
+    Geom::Rect drect = SP_EVENT_CONTEXT(dc)->desktop->get_display_area();
+    double const max = MAX ( drect.dimensions()[Geom::X], drect.dimensions()[Geom::Y] );
+    return Geom::Point(n[Geom::X] * max + drect.min()[Geom::X], n[Geom::Y] * max + drect.min()[Geom::Y]);
 }
 
 static void
@@ -295,7 +299,7 @@ sp_eraser_apply(SPEraserContext *dc, Geom::Point p)
     // This prevents flips, blobs, and jerks caused by microscopic tremor of the tablet pen,
     // especially bothersome at the start of the stroke where we don't yet have the inertia to
     // smooth them out.
-    if ( NR::L2(force) < ERASER_EPSILON || (dc->vel_max < ERASER_VEL_START && NR::L2(force) < ERASER_EPSILON_START)) {
+    if ( Geom::L2(force) < ERASER_EPSILON || (dc->vel_max < ERASER_VEL_START && Geom::L2(force) < ERASER_EPSILON_START)) {
         return FALSE;
     }
 
@@ -304,8 +308,8 @@ sp_eraser_apply(SPEraserContext *dc, Geom::Point p)
     /* Calculate new velocity */
     dc->vel += dc->acc;
 
-    if (NR::L2(dc->vel) > dc->vel_max)
-        dc->vel_max = NR::L2(dc->vel);
+    if (Geom::L2(dc->vel) > dc->vel_max)
+        dc->vel_max = Geom::L2(dc->vel);
 
     /* Calculate angle of drawing tool */
 
@@ -329,11 +333,11 @@ sp_eraser_apply(SPEraserContext *dc, Geom::Point p)
     }
 
     // 2. perpendicular to dc->vel (absolutely non-flat nib):
-    gdouble const mag_vel = NR::L2(dc->vel);
+    gdouble const mag_vel = Geom::L2(dc->vel);
     if ( mag_vel < ERASER_EPSILON ) {
         return FALSE;
     }
-    Geom::Point ang2 = NR::rot90(dc->vel) / mag_vel;
+    Geom::Point ang2 = Geom::rot90(dc->vel) / mag_vel;
 
     // 3. Average them using flatness parameter:
     // calculate angles
@@ -355,15 +359,15 @@ sp_eraser_apply(SPEraserContext *dc, Geom::Point p)
 
     // Try to detect a sudden flip when the new angle differs too much from the previous for the
     // current velocity; in that case discard this move
-    double angle_delta = NR::L2(Geom::Point (cos (new_ang), sin (new_ang)) - dc->ang);
-    if ( angle_delta / NR::L2(dc->vel) > 4000 ) {
+    double angle_delta = Geom::L2(Geom::Point (cos (new_ang), sin (new_ang)) - dc->ang);
+    if ( angle_delta / Geom::L2(dc->vel) > 4000 ) {
         return FALSE;
     }
 
     // convert to point
     dc->ang = Geom::Point (cos (new_ang), sin (new_ang));
 
-//    g_print ("force %g  acc %g  vel_max %g  vel %g  a1 %g  a2 %g  new_ang %g\n", NR::L2(force), NR::L2(dc->acc), dc->vel_max, NR::L2(dc->vel), a1, a2, new_ang);
+//    g_print ("force %g  acc %g  vel_max %g  vel %g  a1 %g  a2 %g  new_ang %g\n", Geom::L2(force), Geom::L2(dc->acc), dc->vel_max, Geom::L2(dc->vel), a1, a2, new_ang);
 
     /* Apply drag */
     dc->vel *= 1.0 - drag;
@@ -393,7 +397,7 @@ sp_eraser_brush(SPEraserContext *dc)
 
     double trace_thick = 1;
 
-    double width = (pressure_thick * trace_thick - vel_thin * NR::L2(dc->vel)) * dc->width;
+    double width = (pressure_thick * trace_thick - vel_thin * Geom::L2(dc->vel)) * dc->width;
 
     double tremble_left = 0, tremble_right = 0;
     if (dc->tremor > 0) {
@@ -413,8 +417,8 @@ sp_eraser_brush(SPEraserContext *dc)
         // (2) deflection depends on width, but is upped for small widths for better visual uniformity across widths;
         // (3) deflection somewhat depends on speed, to prevent fast strokes looking
         // comparatively smooth and slow ones excessively jittery
-        tremble_left  = (y1)*dc->tremor * (0.15 + 0.8*width) * (0.35 + 14*NR::L2(dc->vel));
-        tremble_right = (y2)*dc->tremor * (0.15 + 0.8*width) * (0.35 + 14*NR::L2(dc->vel));
+        tremble_left  = (y1)*dc->tremor * (0.15 + 0.8*width) * (0.35 + 14*Geom::L2(dc->vel));
+        tremble_right = (y2)*dc->tremor * (0.15 + 0.8*width) * (0.35 + 14*Geom::L2(dc->vel));
     }
 
     if ( width < 0.02 * dc->width ) {
@@ -555,8 +559,6 @@ sp_eraser_context_root_handler(SPEventContext *event_context,
         if (dc->dragging && event->button.button == 1 && !event_context->space_panning) {
             dc->dragging = FALSE;
 
-            boost::optional<NR::Rect> const b = Inkscape::Rubberband::get(desktop)->getRectangle();
-
             sp_eraser_apply(dc, motion_dt);
 
             /* Remove all temporary line segments */
@@ -578,10 +580,13 @@ sp_eraser_context_root_handler(SPEventContext *event_context,
                 dc->repr = NULL;
             }
 
-            Inkscape::Rubberband::get(desktop)->stop();
             dc->_message_context->clear();
             ret = TRUE;
         }
+        if (Inkscape::Rubberband::get(desktop)->is_started()) {
+            Inkscape::Rubberband::get(desktop)->stop();
+        }
+            
         break;
     }
 
@@ -715,20 +720,20 @@ set_to_accumulated(SPEraserContext *dc)
     if (!dc->accumulated->is_empty()) {
         if (!dc->repr) {
             /* Create object */
-            Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
+            Inkscape::XML::Document *xml_doc = desktop->doc()->getReprDoc();
             Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
 
             /* Set style */
-            sp_desktop_apply_style_tool (desktop, repr, "tools.eraser", false);
+            sp_desktop_apply_style_tool (desktop, repr, "/tools/eraser", false);
 
             dc->repr = repr;
 
             SPItem *item=SP_ITEM(desktop->currentLayer()->appendChildRepr(dc->repr));
             Inkscape::GC::release(dc->repr);
-            item->transform = SP_ITEM(desktop->currentRoot())->getRelativeTransform(desktop->currentLayer());
+            item->transform = SP_ITEM(desktop->currentLayer())->i2doc_affine().inverse();
             item->updateRepr();
         }
-        Geom::PathVector pathv = dc->accumulated->get_pathvector() * sp_desktop_dt2root_affine(desktop);
+        Geom::PathVector pathv = dc->accumulated->get_pathvector() * desktop->dt2doc();
         gchar *str = sp_svg_write_path(pathv);
         g_assert( str != NULL );
         dc->repr->setAttribute("d", str);
@@ -737,20 +742,22 @@ set_to_accumulated(SPEraserContext *dc)
         if ( dc->repr ) {
             bool wasSelection = false;
             Inkscape::Selection *selection = sp_desktop_selection(desktop);
-            gint eraserMode = (prefs_get_int_attribute("tools.eraser", "mode", 0) != 0) ? 1 : 0;
-            Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
+            Inkscape::Preferences *prefs = Inkscape::Preferences::get();
+            
+            gint eraserMode = prefs->getBool("/tools/eraser/mode") ? 1 : 0;
+            Inkscape::XML::Document *xml_doc = desktop->doc()->getReprDoc();
 
             SPItem* acid = SP_ITEM(desktop->doc()->getObjectByRepr(dc->repr));
-            boost::optional<NR::Rect> eraserBbox = acid->getBounds(NR::identity());
-            NR::Rect bounds = (*eraserBbox) * desktop->doc2dt();
+            Geom::OptRect eraserBbox = acid->getBounds(Geom::identity());
+            Geom::Rect bounds = (*eraserBbox) * desktop->doc2dt();
             std::vector<SPItem*> remainingItems;
             GSList* toWorkOn = 0;
             if (selection->isEmpty()) {
                 if ( eraserMode ) {
-                    toWorkOn = sp_document_partial_items_in_box(sp_desktop_document(desktop), desktop->dkey, bounds);
+                    toWorkOn = sp_desktop_document(desktop)->getItemsPartiallyInBox(desktop->dkey, bounds);
                 } else {
-                    Inkscape::Rubberband::Rubberband *r = Inkscape::Rubberband::get(desktop);
-                    toWorkOn = sp_document_items_at_points(sp_desktop_document(desktop), desktop->dkey, r->getPoints());
+                    Inkscape::Rubberband *r = Inkscape::Rubberband::get(desktop);
+                    toWorkOn = sp_desktop_document(desktop)->getItemsAtPoints(desktop->dkey, r->getPoints());
                 }
                 toWorkOn = g_slist_remove( toWorkOn, acid );
             } else {
@@ -763,7 +770,7 @@ set_to_accumulated(SPEraserContext *dc)
                     for (GSList *i = toWorkOn ; i ; i = i->next ) {
                         SPItem *item = SP_ITEM(i->data);
                         if ( eraserMode ) {
-                            boost::optional<NR::Rect> bbox = item->getBounds(NR::identity());
+                            Geom::OptRect bbox = item->getBounds(Geom::identity());
                             if (bbox && bbox->intersects(*eraserBbox)) {
                                 Inkscape::XML::Node* dup = dc->repr->duplicate(xml_doc);
                                 dc->repr->parent()->appendChild(dup);
@@ -826,10 +833,10 @@ set_to_accumulated(SPEraserContext *dc)
 
 
     if ( workDone ) {
-        sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_ERASER, 
-                         _("Draw eraser stroke"));
+        DocumentUndo::done(sp_desktop_document(desktop), SP_VERB_CONTEXT_ERASER, 
+                           _("Draw eraser stroke"));
     } else {
-        sp_document_cancel(sp_desktop_document(desktop));
+        DocumentUndo::cancel(sp_desktop_document(desktop));
     }
 }
 
@@ -839,11 +846,11 @@ add_cap(SPCurve *curve,
         Geom::Point const &to, Geom::Point const &post,
         double rounding)
 {
-    Geom::Point vel = rounding * NR::rot90( to - from ) / sqrt(2.0);
-    double mag = NR::L2(vel);
+    Geom::Point vel = rounding * Geom::rot90( to - from ) / sqrt(2.0);
+    double mag = Geom::L2(vel);
 
     Geom::Point v_in = from - pre;
-    double mag_in = NR::L2(v_in);
+    double mag_in = Geom::L2(v_in);
     if ( mag_in > ERASER_EPSILON ) {
         v_in = mag * v_in / mag_in;
     } else {
@@ -851,14 +858,14 @@ add_cap(SPCurve *curve,
     }
 
     Geom::Point v_out = to - post;
-    double mag_out = NR::L2(v_out);
+    double mag_out = Geom::L2(v_out);
     if ( mag_out > ERASER_EPSILON ) {
         v_out = mag * v_out / mag_out;
     } else {
         v_out = Geom::Point(0, 0);
     }
 
-    if ( NR::L2(v_in) > ERASER_EPSILON || NR::L2(v_out) > ERASER_EPSILON ) {
+    if ( Geom::L2(v_in) > ERASER_EPSILON || Geom::L2(v_out) > ERASER_EPSILON ) {
         curve->curveto(from + v_in, to + v_out, to);
     }
 }
@@ -911,7 +918,7 @@ fit_and_split(SPEraserContext *dc, gboolean release)
 {
     SPDesktop *desktop = SP_EVENT_CONTEXT(dc)->desktop;
 
-    double const tolerance_sq = square( NR::expansion(desktop->w2d()) * TOLERANCE_ERASER );
+    double const tolerance_sq = square( desktop->w2d().descrim() * TOLERANCE_ERASER );
 
 #ifdef ERASER_VERBOSE
     g_print("[F&S:R=%c]", release?'T':'F');
@@ -942,12 +949,12 @@ fit_and_split(SPEraserContext *dc, gboolean release)
         }
 
         Geom::Point b1[BEZIER_MAX_LENGTH];
-        gint const nb1 = sp_bezier_fit_cubic_r(b1, dc->point1, dc->npoints,
+        gint const nb1 = Geom::bezier_fit_cubic_r(b1, dc->point1, dc->npoints,
                                                tolerance_sq, BEZIER_MAX_BEZIERS);
         g_assert( nb1 * BEZIER_SIZE <= gint(G_N_ELEMENTS(b1)) );
 
         Geom::Point b2[BEZIER_MAX_LENGTH];
-        gint const nb2 = sp_bezier_fit_cubic_r(b2, dc->point2, dc->npoints,
+        gint const nb2 = Geom::bezier_fit_cubic_r(b2, dc->point2, dc->npoints,
                                                tolerance_sq, BEZIER_MAX_BEZIERS);
         g_assert( nb2 * BEZIER_SIZE <= gint(G_N_ELEMENTS(b2)) );
 
@@ -1003,7 +1010,8 @@ fit_and_split(SPEraserContext *dc, gboolean release)
         g_print("[%d]Yup\n", dc->npoints);
 #endif
         if (!release) {
-            gint eraserMode = (prefs_get_int_attribute("tools.eraser", "mode", 0) != 0) ? 1 : 0;
+            Inkscape::Preferences *prefs = Inkscape::Preferences::get();
+            gint eraserMode = prefs->getBool("/tools/eraser/mode") ? 1 : 0;
             g_assert(!dc->currentcurve->is_empty());
 
             SPCanvasItem *cbp = sp_canvas_item_new(sp_desktop_sketch(desktop),
@@ -1013,11 +1021,11 @@ fit_and_split(SPEraserContext *dc, gboolean release)
             sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH (cbp), curve);
             curve->unref();
 
-            guint32 fillColor = sp_desktop_get_color_tool (desktop, "tools.eraser", true);
-            //guint32 strokeColor = sp_desktop_get_color_tool (desktop, "tools.eraser", false);
-            double opacity = sp_desktop_get_master_opacity_tool (desktop, "tools.eraser");
-            double fillOpacity = sp_desktop_get_opacity_tool (desktop, "tools.eraser", true);
-            //double strokeOpacity = sp_desktop_get_opacity_tool (desktop, "tools.eraser", false);
+            guint32 fillColor = sp_desktop_get_color_tool (desktop, "/tools/eraser", true);
+            //guint32 strokeColor = sp_desktop_get_color_tool (desktop, "/tools/eraser", false);
+            double opacity = sp_desktop_get_master_opacity_tool (desktop, "/tools/eraser");
+            double fillOpacity = sp_desktop_get_opacity_tool (desktop, "/tools/eraser", true);
+            //double strokeOpacity = sp_desktop_get_opacity_tool (desktop, "/tools/eraser", false);
             sp_canvas_bpath_set_fill(SP_CANVAS_BPATH(cbp), ((fillColor & 0xffffff00) | SP_COLOR_F_TO_U(opacity*fillOpacity)), SP_WIND_RULE_EVENODD);
             //on second thougtht don't do stroke yet because we don't have stoke-width yet and because stoke appears between segments while drawing
             //sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(cbp), ((strokeColor & 0xffffff00) | SP_COLOR_F_TO_U(opacity*strokeOpacity)), 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
@@ -1070,4 +1078,4 @@ draw_temporary_box(SPEraserContext *dc)
   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 :