X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;f=src%2Fspiral-context.cpp;h=cd90ac1cf15362830cb1e07857100d1a7d213457;hb=59ce7a44179e0b794fd92050d79aab7450d8688a;hp=e878154ba47dc493328aa8f14c64e87081ae4aad;hpb=6926d8baf046c184bbcc8d0a62ee00128d6415a0;p=inkscape.git diff --git a/src/spiral-context.cpp b/src/spiral-context.cpp index e878154ba..cd90ac1cf 100644 --- a/src/spiral-context.cpp +++ b/src/spiral-context.cpp @@ -17,6 +17,8 @@ #include "config.h" #include +#include +#include #include "macros.h" #include "display/sp-canvas.h" @@ -25,31 +27,30 @@ #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 "message-context.h" #include "pixmaps/cursor-spiral.xpm" -#include "pixmaps/cursor-spiral.pixbuf" #include "spiral-context.h" #include "sp-metrics.h" #include #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" 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_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 SPEventContextClass *parent_class; @@ -95,11 +96,6 @@ sp_spiral_context_init(SPSpiralContext *spiral_context) SPEventContext *event_context = SP_EVENT_CONTEXT(spiral_context); event_context->cursor_shape = cursor_spiral_xpm; - event_context->cursor_pixbuf = gdk_pixbuf_new_from_inline( - -1, - cursor_spiral_pixbuf, - FALSE, - NULL); event_context->hot_x = 4; event_context->hot_y = 4; event_context->xp = 0; @@ -108,9 +104,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; @@ -131,20 +124,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) { - 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 (sc->_message_context) { delete sc->_message_context; } @@ -152,14 +137,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 @@ -170,27 +147,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 - 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; - } - + 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 @@ -205,27 +164,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(); } @@ -233,19 +187,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); } } @@ -258,31 +210,37 @@ 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; switch (event->type) { case GDK_BUTTON_PRESS: - if (event->button.button == 1) { + if (event->button.button == 1 && !event_context->space_panning) { dragging = TRUE; + sp_event_context_snap_window_open(event_context); sc->center = Inkscape::setup_for_drag_start(desktop, event_context, event); - SnapManager const &m = desktop->namedview->snap_manager; - sc->center = m.freeSnap(Inkscape::Snapper::SNAP_POINT, sc->center, sc->item).getPoint(); + SnapManager &m = desktop->namedview->snap_manager; + m.setup(desktop); + Geom::Point pt2g = to_2geom(sc->center); + m.freeSnapReturnByRef(Inkscape::SnapPreferences::SNAPPOINT_NODE, pt2g, Inkscape::SNAPSOURCE_HANDLE); + sc->center = from_2geom(pt2g); sp_canvas_item_grab(SP_CANVAS_ITEM(desktop->acetate), ( GDK_KEY_PRESS_MASK | GDK_BUTTON_RELEASE_MASK | GDK_POINTER_MOTION_MASK | + GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_PRESS_MASK ), NULL, event->button.time); ret = TRUE; } break; case GDK_MOTION_NOTIFY: - if (dragging && event->motion.state && GDK_BUTTON1_MASK) { + if (dragging && (event->motion.state & GDK_BUTTON1_MASK) && !event_context->space_panning) { if ( event_context->within_tolerance && ( abs( (gint) event->motion.x - event_context->xp ) < event_context->tolerance ) @@ -294,16 +252,24 @@ 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); - NR::Point const motion_dt(event_context->desktop->w2d(motion_w)); - sp_spiral_drag(sc, motion_dt, event->motion.state); + 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, true, sc->item); + m.freeSnapReturnByRef(Inkscape::SnapPreferences::SNAPPOINT_NODE, motion_dt, Inkscape::SNAPSOURCE_HANDLE); + sp_spiral_drag(sc, from_2geom(motion_dt), event->motion.state); + + gobble_motion_events(GDK_BUTTON1_MASK); + ret = TRUE; } break; case GDK_BUTTON_RELEASE: event_context->xp = event_context->yp = 0; - if (event->button.button == 1) { + if (event->button.button == 1 && !event_context->space_panning) { dragging = FALSE; + sp_event_context_snap_window_closed(event_context, false); //button release will also occur on a double-click; in that case suppress warnings if (!event_context->within_tolerance) { // we've been dragging, finish the spiral sp_spiral_finish(sc); @@ -356,6 +322,22 @@ sp_spiral_context_root_handler(SPEventContext *event_context, GdkEvent *event) case GDK_Escape: sp_desktop_selection(desktop)->clear(); //TODO: make dragging escapable by Esc + break; + + case GDK_space: + if (dragging) { + sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate), + event->button.time); + dragging = false; + sp_event_context_snap_window_closed(event_context); + if (!event_context->within_tolerance) { + // we've been dragging, finish the rect + sp_spiral_finish(sc); + } + // do not return true, so that space would work switching to selector + } + break; + default: break; } @@ -389,11 +371,12 @@ sp_spiral_context_root_handler(SPEventContext *event_context, GdkEvent *event) } static void -sp_spiral_drag(SPSpiralContext *sc, NR::Point p, guint state) +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) { @@ -402,38 +385,42 @@ sp_spiral_drag(SPSpiralContext *sc, NR::Point p, guint state) } /* Create object */ - Inkscape::XML::Node *repr = sp_repr_new("svg:path"); + Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_EVENT_CONTEXT_DOCUMENT(sc)); + 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); + 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_i2doc_affine(SP_ITEM(desktop->currentLayer())).inverse(); sc->item->updateRepr(); + + sp_canvas_force_full_redraw_after_interruptions(desktop->canvas, 5); } - NR::Point const p0 = sp_desktop_dt2root_xy_point(desktop, sc->center); - NR::Point p1 = sp_desktop_dt2root_xy_point(desktop, p); - SnapManager const &m = desktop->namedview->snap_manager; - p1 = m.freeSnap(Inkscape::Snapper::SNAP_POINT, p1, sc->item).getPoint(); + SnapManager &m = desktop->namedview->snap_manager; + m.setup(desktop, true, sc->item); + Geom::Point pt2g = to_2geom(p); + m.freeSnapReturnByRef(Inkscape::SnapPreferences::SNAPPOINT_NODE, pt2g, Inkscape::SNAPSOURCE_HANDLE); + + Geom::Point const p0 = desktop->dt2doc(sc->center); + Geom::Point const p1 = desktop->dt2doc(pt2g); SPSpiral *spiral = SP_SPIRAL(sc->item); - NR::Point const delta = p1 - p0; - gdouble const rad = NR::L2(delta); + 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); } - sp_canvas_force_full_redraws(desktop->canvas, 1); - /* Fixme: these parameters should be got from dialog box */ - sp_spiral_position_set(spiral, p0[NR::X], p0[NR::Y], + sp_spiral_position_set(spiral, p0[Geom::X], p0[Geom::Y], /*expansion*/ sc->exp, /*revolution*/ sc->revo, rad, arg, @@ -441,7 +428,7 @@ sp_spiral_drag(SPSpiralContext *sc, NR::Point p, guint state) /* status text */ GString *rads = SP_PX_TO_METRIC_STRING(rad, desktop->namedview->getDefaultMetric()); - sc->_message_context->setF(Inkscape::NORMAL_MESSAGE, + sc->_message_context->setF(Inkscape::IMMEDIATE_MESSAGE, _("Spiral: radius %s, angle %5g°; with Ctrl to snap angle"), rads->str, sp_round((arg + 2.0*M_PI*spiral->revo)*180/M_PI, 0.0001)); g_string_free(rads, FALSE); @@ -457,15 +444,15 @@ sp_spiral_finish(SPSpiralContext *sc) SPSpiral *spiral = SP_SPIRAL(sc->item); sp_shape_set_shape(SP_SHAPE(spiral)); - SP_OBJECT(spiral)->updateRepr(NULL, SP_OBJECT_WRITE_EXT); + 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, - /* TODO: annotate */ "spiral-context.cpp:462"); + sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_SPIRAL, + _("Create spiral")); sc->item = NULL; - - sp_canvas_clear_forced_full_redraws(desktop->canvas); } }