X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;f=src%2Fdraw-context.cpp;h=62ae67336cad48236adbbcb3c29132257aa716b6;hb=9dc68827cbd515262ecb8d5ae8547d9e82c72e00;hp=d21ee5ffec156f834542e223b7f408c18a02a95f;hpb=5a4218b349e84ead65be4de6401af178d526e64f;p=inkscape.git diff --git a/src/draw-context.cpp b/src/draw-context.cpp index d21ee5ffe..62ae67336 100644 --- a/src/draw-context.cpp +++ b/src/draw-context.cpp @@ -1,10 +1,9 @@ -#define __SP_DRAW_CONTEXT_C__ - /* * Generic drawing context * * Author: * Lauris Kaplinski + * Abhishek Sharma * * Copyright (C) 2000 Lauris Kaplinski * Copyright (C) 2000-2001 Ximian, Inc. @@ -26,7 +25,6 @@ #include #include "display/curve.h" #include "desktop.h" -#include "desktop-affine.h" #include "desktop-handles.h" #include "desktop-style.h" #include "document.h" @@ -35,7 +33,7 @@ #include "message-stack.h" #include "pen-context.h" #include "lpe-tool-context.h" -#include "prefs-utils.h" +#include "preferences.h" #include "selection.h" #include "selection-chemistry.h" #include "snap.h" @@ -44,12 +42,14 @@ #include "live_effects/lpe-patternalongpath.h" #include "style.h" +using Inkscape::DocumentUndo; + static void sp_draw_context_class_init(SPDrawContextClass *klass); static void sp_draw_context_init(SPDrawContext *dc); static void sp_draw_context_dispose(GObject *object); static void sp_draw_context_setup(SPEventContext *ec); -static void sp_draw_context_set(SPEventContext *ec, gchar const *key, gchar const *value); +static void sp_draw_context_set(SPEventContext *ec, Inkscape::Preferences::Entry *val); static void sp_draw_context_finish(SPEventContext *ec); static gint sp_draw_context_root_handler(SPEventContext *event_context, GdkEvent *event); @@ -225,7 +225,7 @@ sp_draw_context_finish(SPEventContext *ec) } static void -sp_draw_context_set(SPEventContext */*ec*/, const gchar */*key*/, const gchar */*value*/) +sp_draw_context_set(SPEventContext */*ec*/, Inkscape::Preferences::Entry */*val*/) { } @@ -263,12 +263,12 @@ sp_draw_context_root_handler(SPEventContext *ec, GdkEvent *event) return ret; } -static char const * +static Glib::ustring const tool_name(SPDrawContext *dc) { return ( SP_IS_PEN_CONTEXT(dc) - ? "tools.freehand.pen" - : "tools.freehand.pencil" ); + ? "/tools/freehand/pen" + : "/tools/freehand/pencil" ); } static void @@ -278,7 +278,7 @@ spdc_paste_curve_as_freehand_shape(const SPCurve *c, SPDrawContext *dc, SPItem * // TODO: Don't paste path if nothing is on the clipboard - Effect::createAndApply(Inkscape::LivePathEffect::FREEHAND_SHAPE, dc->desktop->doc(), item); + Effect::createAndApply(PATTERN_ALONG_PATH, dc->desktop->doc(), item); Effect* lpe = sp_lpe_item_get_current_lpe(SP_LPE_ITEM(item)); gchar *svgd = sp_svg_write_path(c->get_pathvector()); static_cast(lpe)->pattern.paste_param_path(svgd); @@ -292,18 +292,19 @@ void spdc_check_for_and_apply_waiting_LPE(SPDrawContext *dc, SPItem *item) { using namespace Inkscape::LivePathEffect; + Inkscape::Preferences *prefs = Inkscape::Preferences::get(); if (item && SP_IS_LPE_ITEM(item)) { - if (prefs_get_int_attribute(tool_name(dc), "freehand-mode", 0) == 1) { + if (prefs->getInt(tool_name(dc) + "/freehand-mode", 0) == 1) { Effect::createAndApply(SPIRO, dc->desktop->doc(), item); } - int shape = prefs_get_int_attribute(tool_name(dc), "shape", 0); + int shape = prefs->getInt(tool_name(dc) + "/shape", 0); bool shape_applied = false; SPCSSAttr *css_item = sp_css_attr_from_object (SP_OBJECT(item), SP_STYLE_FLAG_ALWAYS); const char *cstroke = sp_repr_css_property(css_item, "stroke", "none"); -#define SHAPE_LENGTH 100 +#define SHAPE_LENGTH 10 #define SHAPE_HEIGHT 10 switch (shape) { @@ -359,7 +360,7 @@ spdc_check_for_and_apply_waiting_LPE(SPDrawContext *dc, SPItem *item) case 4: { // take shape from clipboard; TODO: catch the case where clipboard is empty - Effect::createAndApply(FREEHAND_SHAPE, dc->desktop->doc(), item); + Effect::createAndApply(PATTERN_ALONG_PATH, dc->desktop->doc(), item); Effect* lpe = sp_lpe_item_get_current_lpe(SP_LPE_ITEM(item)); static_cast(lpe)->pattern.on_paste_button_click(); @@ -372,7 +373,12 @@ spdc_check_for_and_apply_waiting_LPE(SPDrawContext *dc, SPItem *item) if (shape_applied) { // apply original stroke color as fill and unset stroke; then return SPCSSAttr *css = sp_repr_css_attr_new(); - sp_repr_css_set_property (css, "fill", cstroke); + + if (!strcmp(cstroke, "none")){ + sp_repr_css_set_property (css, "fill", "black"); + } else { + sp_repr_css_set_property (css, "fill", cstroke); + } sp_repr_css_set_property (css, "stroke", "none"); sp_desktop_apply_css_recursive(SP_OBJECT(item), css, true); sp_repr_css_attr_unref(css); @@ -385,7 +391,7 @@ spdc_check_for_and_apply_waiting_LPE(SPDrawContext *dc, SPItem *item) if (SP_IS_LPETOOL_CONTEXT(dc)) { // since a geometric LPE was applied, we switch back to "inactive" mode - lpetool_context_switch_mode(SP_LPETOOL_CONTEXT(dc), Inkscape::LivePathEffect::INVALID_LPE); + lpetool_context_switch_mode(SP_LPETOOL_CONTEXT(dc), INVALID_LPE); } } if (SP_IS_PEN_CONTEXT(dc)) { @@ -433,7 +439,7 @@ spdc_attach_selection(SPDrawContext *dc, Inkscape::Selection */*sel*/) /* Curve list */ /* We keep it in desktop coordinates to eliminate calculation errors */ SPCurve *norm = sp_path_get_curve_for_edit (SP_PATH(item)); - norm->transform(sp_item_i2d_affine(dc->white_item)); + norm->transform((dc->white_item)->i2d_affine()); g_return_if_fail( norm != NULL ); dc->white_curves = g_slist_reverse(norm->split()); norm->unref(); @@ -463,63 +469,49 @@ spdc_attach_selection(SPDrawContext *dc, Inkscape::Selection */*sel*/) * \param dc draw context * \param p cursor point (to be changed by snapping) * \param o origin point - * \param state keyboard state to check if ctrl was pressed + * \param state keyboard state to check if ctrl or shift was pressed */ void spdc_endpoint_snap_rotation(SPEventContext const *const ec, Geom::Point &p, Geom::Point const &o, guint state) { - unsigned const snaps = abs(prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12)); - /* 0 means no snapping. */ - - /* mirrored by fabs, so this corresponds to 15 degrees */ - Geom::Point best; /* best solution */ - double bn = NR_HUGE; /* best normal */ - double bdot = 0; - Geom::Point v = Geom::Point(0, 1); - double const r00 = cos(M_PI / snaps), r01 = sin(M_PI / snaps); - double const r10 = -r01, r11 = r00; - - Geom::Point delta = p - o; - - for (unsigned i = 0; i < snaps; i++) { - double const ndot = fabs(dot(v,Geom::rot90(delta))); - Geom::Point t(r00*v[NR::X] + r01*v[NR::Y], - r10*v[NR::X] + r11*v[NR::Y]); - if (ndot < bn) { - /* I think it is better numerically to use the normal, rather than the dot product - * to assess solutions, but I haven't proven it. */ - bn = ndot; - best = v; - bdot = dot(v, delta); - } - v = t; + Inkscape::Preferences *prefs = Inkscape::Preferences::get(); + unsigned const snaps = abs(prefs->getInt("/options/rotationsnapsperpi/value", 12)); + SnapManager &m = SP_EVENT_CONTEXT_DESKTOP(ec)->namedview->snap_manager; + m.setup(SP_EVENT_CONTEXT_DESKTOP(ec)); + + bool snap_enabled = m.snapprefs.getSnapEnabledGlobally(); + if (state & GDK_SHIFT_MASK) { + // SHIFT disables all snapping, except the angular snapping. After all, the user explicitly asked for angular + // snapping by pressing CTRL, otherwise we wouldn't have arrived here. But although we temporarily disable + // the snapping here, we must still call for a constrained snap in order to apply the constraints (i.e. round + // to the nearest angle increment) + m.snapprefs.setSnapEnabledGlobally(false); } - if (fabs(bdot) > 0) { - p = o + bdot * best; + Inkscape::SnappedPoint dummy = m.constrainedAngularSnap(Inkscape::SnapCandidatePoint(p, Inkscape::SNAPSOURCE_NODE_HANDLE), boost::optional(), o, snaps); + p = dummy.getPoint(); - if (!(state & GDK_SHIFT_MASK)) { //SHIFT disables all snapping, except the angular snapping above - //After all, the user explicitely asked for angular snapping by - //pressing CTRL - /* Snap it along best vector */ - SnapManager &m = SP_EVENT_CONTEXT_DESKTOP(ec)->namedview->snap_manager; - m.setup(SP_EVENT_CONTEXT_DESKTOP(ec)); - Geom::Point pt2g = to_2geom(p); - m.constrainedSnapReturnByRef( Inkscape::Snapper::SNAPPOINT_NODE, pt2g, Inkscape::Snapper::ConstraintLine(best)); - p = from_2geom(pt2g); - } + if (state & GDK_SHIFT_MASK) { + m.snapprefs.setSnapEnabledGlobally(snap_enabled); // restore the original setting } + + m.unSetup(); } void spdc_endpoint_snap_free(SPEventContext const * const ec, Geom::Point& p, guint const /*state*/) { - SnapManager &m = SP_EVENT_CONTEXT_DESKTOP(ec)->namedview->snap_manager; - m.setup(SP_EVENT_CONTEXT_DESKTOP(ec)); - Geom::Point pt2g = to_2geom(p); - m.freeSnapReturnByRef(Inkscape::Snapper::SNAPPOINT_NODE, pt2g); - p = from_2geom(pt2g); + SPDesktop *dt = SP_EVENT_CONTEXT_DESKTOP(ec); + SnapManager &m = dt->namedview->snap_manager; + Inkscape::Selection *selection = sp_desktop_selection (dt); + + // selection->singleItem() is the item that is currently being drawn. This item will not be snapped to (to avoid self-snapping) + // TODO: Allow snapping to the stationary parts of the item, and only ignore the last segment + + m.setup(dt, true, selection->singleItem()); + m.freeSnapReturnByRef(p, Inkscape::SNAPSOURCE_NODE_HANDLE); + m.unSetup(); } static SPCurve * @@ -648,12 +640,12 @@ spdc_flush_white(SPDrawContext *dc, SPCurve *gc) /* Now we have to go back to item coordinates at last */ c->transform( dc->white_item - ? sp_item_dt2i_affine(dc->white_item) - : to_2geom(sp_desktop_dt2root_affine(SP_EVENT_CONTEXT_DESKTOP(dc))) ); + ? (dc->white_item)->dt2i_affine() + : SP_EVENT_CONTEXT_DESKTOP(dc)->dt2doc() ); SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(dc); 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 */ @@ -666,7 +658,7 @@ spdc_flush_white(SPDrawContext *dc, SPCurve *gc) } else { repr = xml_doc->createElement("svg:path"); /* Set style */ - sp_desktop_apply_style_tool(desktop, repr, tool_name(dc), false); + sp_desktop_apply_style_tool(desktop, repr, tool_name(dc).data(), false); } gchar *str = sp_svg_write_path( c->get_pathvector() ); @@ -686,11 +678,11 @@ spdc_flush_white(SPDrawContext *dc, SPCurve *gc) dc->selection->set(repr); Inkscape::GC::release(repr); - item->transform = i2i_affine(desktop->currentRoot(), desktop->currentLayer()); + item->transform = SP_ITEM(desktop->currentLayer())->i2doc_affine().inverse(); item->updateRepr(); } - sp_document_done(doc, SP_IS_PEN_CONTEXT(dc)? SP_VERB_CONTEXT_PEN : SP_VERB_CONTEXT_PENCIL, + DocumentUndo::done(doc, SP_IS_PEN_CONTEXT(dc)? SP_VERB_CONTEXT_PEN : SP_VERB_CONTEXT_PENCIL, _("Draw path")); // When quickly drawing several subpaths with Shift, the next subpath may be finished and @@ -704,7 +696,7 @@ spdc_flush_white(SPDrawContext *dc, SPCurve *gc) c->unref(); /* Flush pending updates */ - sp_document_ensure_up_to_date(doc); + doc->ensureUpToDate(); } /** @@ -794,10 +786,11 @@ spdc_free_colors(SPDrawContext *dc) /* Create a single dot represented by a circle */ void spdc_create_single_dot(SPEventContext *ec, Geom::Point const &pt, char const *tool, guint event_state) { - g_return_if_fail(!strcmp(tool, "tools.freehand.pen") || !strcmp(tool, "tools.freehand.pencil")); + g_return_if_fail(!strcmp(tool, "/tools/freehand/pen") || !strcmp(tool, "/tools/freehand/pencil")); + Glib::ustring tool_path = tool; SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(ec); - 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"); repr->setAttribute("sodipodi:type", "arc"); SPItem *item = SP_ITEM(desktop->currentLayer()->appendChildRepr(repr)); @@ -825,9 +818,11 @@ void spdc_create_single_dot(SPEventContext *ec, Geom::Point const &pt, char cons /* put the circle where the mouse click occurred and set the diameter to the current stroke width, multiplied by the amount specified in the preferences */ - Geom::Matrix const i2d (sp_item_i2d_affine (item)); - Geom::Point pp = pt * i2d; - double rad = 0.5 * prefs_get_double_attribute(tool, "dot-size", 3.0); + Inkscape::Preferences *prefs = Inkscape::Preferences::get(); + + Geom::Matrix const i2d (item->i2d_affine ()); + Geom::Point pp = pt; + double rad = 0.5 * prefs->getDouble(tool_path + "/dot-size", 3.0); if (event_state & GDK_MOD1_MASK) { /* TODO: We vary the dot size between 0.5*rad and 1.5*rad, where rad is the dot size as specified in prefs. Very simple, but it might be sufficient in practice. If not, @@ -840,16 +835,17 @@ void spdc_create_single_dot(SPEventContext *ec, Geom::Point const &pt, char cons rad *= 2; } - sp_repr_set_svg_double (repr, "sodipodi:cx", pp[NR::X]); - sp_repr_set_svg_double (repr, "sodipodi:cy", pp[NR::Y]); + sp_repr_set_svg_double (repr, "sodipodi:cx", pp[Geom::X]); + sp_repr_set_svg_double (repr, "sodipodi:cy", pp[Geom::Y]); sp_repr_set_svg_double (repr, "sodipodi:rx", rad * stroke_width); sp_repr_set_svg_double (repr, "sodipodi:ry", rad * stroke_width); item->updateRepr(); + item->set_item_transform(i2d.inverse()); sp_desktop_selection(desktop)->set(item); desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Creating single dot")); - sp_document_done(sp_desktop_document(desktop), SP_VERB_NONE, _("Create single dot")); + DocumentUndo::done(sp_desktop_document(desktop), SP_VERB_NONE, _("Create single dot")); } /* @@ -861,4 +857,4 @@ void spdc_create_single_dot(SPEventContext *ec, Geom::Point const &pt, char cons 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 :