X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;ds=inline;f=src%2Fshape-editor.cpp;h=d7cafaed2ab12864d502e2e2f3a7c58d8f26593c;hb=c4b828ca1807524249c2793e18be249cf2a7660d;hp=508d2228d3cdec2f551f8d2c0ec23d0955a0c83c;hpb=42e99769805c14a5cc01c805faa3c3b03f9dd1c0;p=inkscape.git diff --git a/src/shape-editor.cpp b/src/shape-editor.cpp index 508d2228d..d7cafaed2 100644 --- a/src/shape-editor.cpp +++ b/src/shape-editor.cpp @@ -12,6 +12,7 @@ #include "config.h" #endif +#include #include #include "sp-object.h" @@ -21,17 +22,19 @@ #include "desktop.h" #include "desktop-handles.h" #include "knotholder.h" -#include "node-context.h" +#include "live_effects/parameter/point.h" +#include "nodepath.h" #include "xml/node-event-vector.h" -#include "prefs-utils.h" +#include "preferences.h" #include "object-edit.h" -#include "splivarot.h" #include "style.h" +#include "display/curve.h" +#include <2geom/pathvector.h> #include "shape-editor.h" -ShapeEditorsCollective::ShapeEditorsCollective(SPDesktop *dt) { +ShapeEditorsCollective::ShapeEditorsCollective(SPDesktop */*dt*/) { } ShapeEditorsCollective::~ShapeEditorsCollective() { @@ -50,38 +53,47 @@ ShapeEditor::ShapeEditor(SPDesktop *dt) { this->nodepath = NULL; this->knotholder = NULL; this->hit = false; + this->listener_attached_for = NULL; } ShapeEditor::~ShapeEditor() { - unset_item(); + unset_item(SH_KNOTHOLDER); + unset_item(SH_NODEPATH); } -void ShapeEditor::unset_item() { - +void ShapeEditor::unset_item(SubType type, bool keep_knotholder) { Inkscape::XML::Node *old_repr = NULL; - if (this->nodepath) { - old_repr = this->nodepath->repr; - } - - if (!old_repr && this->knotholder) { - old_repr = this->knotholder->repr; - } - - if (old_repr) { // remove old listener - sp_repr_remove_listener_by_data(old_repr, this); - Inkscape::GC::release(old_repr); - } - - if (this->nodepath) { - this->grab_node = -1; - sp_nodepath_destroy(this->nodepath); - this->nodepath = NULL; - } - - if (this->knotholder) { - sp_knot_holder_destroy(this->knotholder); - this->knotholder = NULL; + switch (type) { + case SH_NODEPATH: + if (this->nodepath) { + old_repr = this->nodepath->repr; + if (old_repr && old_repr == listener_attached_for) { + sp_repr_remove_listener_by_data(old_repr, this); + Inkscape::GC::release(old_repr); + listener_attached_for = NULL; + } + + this->grab_node = -1; + delete this->nodepath; + this->nodepath = NULL; + } + break; + case SH_KNOTHOLDER: + if (this->knotholder) { + old_repr = this->knotholder->repr; + if (old_repr && old_repr == listener_attached_for) { + sp_repr_remove_listener_by_data(old_repr, this); + Inkscape::GC::release(old_repr); + listener_attached_for = NULL; + } + + if (!keep_knotholder) { + delete this->knotholder; + this->knotholder = NULL; + } + } + break; } } @@ -93,30 +105,52 @@ bool ShapeEditor::has_knotholder () { return (this->knotholder != NULL); } -bool ShapeEditor::has_local_change () { - if (this->nodepath) - return (this->nodepath->local_change != 0); - else if (this->knotholder) - return (this->knotholder->local_change != 0); - else - return false; +void ShapeEditor::update_knotholder () { + if (this->knotholder) + this->knotholder->update_knots(); } -void ShapeEditor::decrement_local_change () { - if (this->nodepath) { - if (this->nodepath->local_change > 0) - this->nodepath->local_change--; - } else if (this->knotholder) { - this->knotholder->local_change = FALSE; +bool ShapeEditor::has_local_change (SubType type) { + switch (type) { + case SH_NODEPATH: + return (this->nodepath && this->nodepath->local_change); + case SH_KNOTHOLDER: + return (this->knotholder && this->knotholder->local_change != 0); + default: + g_assert_not_reached(); } } -SPItem *ShapeEditor::get_item () { - SPItem *item = NULL; - if (this->has_nodepath()) { - item = this->nodepath->item; - } else if (this->has_knotholder()) { - item = SP_ITEM(this->knotholder->item); +void ShapeEditor::decrement_local_change (SubType type) { + switch (type) { + case SH_NODEPATH: + if (this->nodepath && this->nodepath->local_change > 0) { + this->nodepath->local_change--; + } + break; + case SH_KNOTHOLDER: + if (this->knotholder) { + this->knotholder->local_change = FALSE; + } + break; + default: + g_assert_not_reached(); + } +} + +const SPItem *ShapeEditor::get_item (SubType type) { + const SPItem *item = NULL; + switch (type) { + case SH_NODEPATH: + if (this->has_nodepath()) { + item = this->nodepath->item; + } + break; + case SH_KNOTHOLDER: + if (this->has_knotholder()) { + item = this->knotholder->getItem(); + } + break; } return item; } @@ -140,28 +174,30 @@ bool ShapeEditor::nodepath_edits_repr_key(gchar const *name) { return false; } -static void shapeeditor_event_attr_changed(Inkscape::XML::Node *repr, gchar const *name, - gchar const *old_value, gchar const *new_value, - bool is_interactive, gpointer data) +static void shapeeditor_event_attr_changed(Inkscape::XML::Node */*repr*/, gchar const *name, + gchar const */*old_value*/, gchar const */*new_value*/, + bool /*is_interactive*/, gpointer data) { - gboolean changed = FALSE; + gboolean changed_np = FALSE; + gboolean changed_kh = FALSE; g_assert(data); ShapeEditor *sh = ((ShapeEditor *) data); - if ( sh->has_knotholder() || ( sh->has_nodepath() && sh->nodepath_edits_repr_key(name) ) ) + if (sh->has_nodepath() && sh->nodepath_edits_repr_key(name)) { - changed = !sh->has_local_change(); - sh->decrement_local_change(); + changed_np = !sh->has_local_change(SH_NODEPATH); + sh->decrement_local_change(SH_NODEPATH); + } - if (changed) { + if (changed_np) { GList *saved = NULL; if (sh->has_nodepath()) { saved = sh->save_nodepath_selection(); } - sh->reset_item (); + sh->reset_item(SH_NODEPATH); if (sh->has_nodepath() && saved) { sh->restore_nodepath_selection(saved); @@ -169,6 +205,18 @@ static void shapeeditor_event_attr_changed(Inkscape::XML::Node *repr, gchar cons } } + + if (sh->has_knotholder()) + { + changed_kh = !sh->has_local_change(SH_KNOTHOLDER); + sh->decrement_local_change(SH_KNOTHOLDER); + if (changed_kh) { + // this can happen if an LPEItem's knotholder handle was dragged, in which case we want + // to keep the knotholder; in all other cases (e.g., if the LPE itself changes) we delete it + sh->reset_item(SH_KNOTHOLDER, !strcmp(name, "d")); + } + } + sh->update_statusbar(); //TODO: sh->get_container()->update_statusbar(); } @@ -181,75 +229,121 @@ static Inkscape::XML::NodeEventVector shapeeditor_repr_events = { }; -void ShapeEditor::set_item(SPItem *item) { - - unset_item(); +void ShapeEditor::set_item(SPItem *item, SubType type, bool keep_knotholder) { + // this happens (and should only happen) when for an LPEItem having both knotholder and nodepath the knotholder + // is adapted; in this case we don't want to delete the knotholder since this freezes the handles + unset_item(type, keep_knotholder); this->grab_node = -1; if (item) { - this->nodepath = sp_nodepath_new(desktop, item, (prefs_get_int_attribute("tools.nodes", "show_handles", 1) != 0)); - if (this->nodepath) { - this->nodepath->shape_editor = this; - } - this->knotholder = sp_item_knot_holder(item, desktop); - - if (this->nodepath || this->knotholder) { - // setting new listener - Inkscape::XML::Node *repr; - if (this->knotholder) - repr = this->knotholder->repr; - else - repr = SP_OBJECT_REPR(item); - if (repr) { - Inkscape::GC::anchor(repr); - sp_repr_add_listener(repr, &shapeeditor_repr_events, this); - } + Inkscape::XML::Node *repr; + switch(type) { + case SH_NODEPATH: + if (SP_IS_LPE_ITEM(item)) { + Inkscape::Preferences *prefs = Inkscape::Preferences::get(); + this->nodepath = sp_nodepath_new(desktop, item, (prefs->getBool("/tools/nodes/show_handles", true))); + } + if (this->nodepath) { + this->nodepath->shape_editor = this; + + // setting new listener + repr = SP_OBJECT_REPR(item); + if (repr != listener_attached_for) { + Inkscape::GC::anchor(repr); + sp_repr_add_listener(repr, &shapeeditor_repr_events, this); + listener_attached_for = repr; + } + } + break; + + case SH_KNOTHOLDER: + if (!this->knotholder) { + // only recreate knotholder if none is present + this->knotholder = sp_item_knot_holder(item, desktop); + } + if (this->knotholder) { + this->knotholder->update_knots(); + // setting new listener + repr = this->knotholder->repr; + if (repr != listener_attached_for) { + Inkscape::GC::anchor(repr); + sp_repr_add_listener(repr, &shapeeditor_repr_events, this); + listener_attached_for = repr; + } + } + break; } } } /** Please note that this function only works for path parameters. * All other parameters probably will crash Inkscape! -* Fortunately, there are no other on-canvas edittable objects at this moment :) */ -void ShapeEditor::set_item_livepatheffect_parameter(SPItem *item, SPObject *lpeobject, const char * key) { - - unset_item(); +void ShapeEditor::set_item_lpe_path_parameter(SPItem *item, SPObject *lpeobject, const char * key) +{ + unset_item(SH_NODEPATH); this->grab_node = -1; if (lpeobject) { - this->knotholder = NULL; // it's a path, no special knotholder needed. - this->nodepath = sp_nodepath_new( desktop, lpeobject, - (prefs_get_int_attribute("tools.nodes", "show_handles", 1) != 0), + Inkscape::Preferences *prefs = Inkscape::Preferences::get(); + this->nodepath = sp_nodepath_new( desktop, lpeobject, + (prefs->getInt("/tools/nodes/show_handles", true)), key, item); if (this->nodepath) { - this->nodepath->shape_editor = this; + this->nodepath->shape_editor = this; // setting new listener Inkscape::XML::Node *repr = SP_OBJECT_REPR(lpeobject); - if (repr) { + if (repr && repr != listener_attached_for) { Inkscape::GC::anchor(repr); sp_repr_add_listener(repr, &shapeeditor_repr_events, this); + listener_attached_for = repr; } } } } +/** +* pass a new knotholder to ShapeEditor to manage (and delete) +*/ +void +ShapeEditor::set_knotholder(KnotHolder * knot_holder) +{ + unset_item(SH_KNOTHOLDER); + + this->grab_node = -1; + + if (knot_holder) { + this->knotholder = knot_holder; + } +} + + /** FIXME: think about this. Is this thing only called when the item needs to be updated? Why not make a reload function in NodePath and in KnotHolder? */ -void ShapeEditor::reset_item () +void ShapeEditor::reset_item (SubType type, bool keep_knotholder) { - if ( (this->nodepath) && (IS_LIVEPATHEFFECT(this->nodepath->object)) ) { - SPItem * item = this->nodepath->item; - SPObject *obj = this->nodepath->object; - char * key = g_strdup(this->nodepath->repr_key); - set_item_livepatheffect_parameter(item, obj, key); - g_free(key); - } else { - SPItem * item = get_item(); - set_item(item); + switch (type) { + case SH_NODEPATH: + if ( (this->nodepath) && (IS_LIVEPATHEFFECT(this->nodepath->object)) ) { + SPItem * item = this->nodepath->item; + SPObject *obj = this->nodepath->object; + char * key = g_strdup(this->nodepath->repr_key); + set_item_lpe_path_parameter(item, obj, key); // the above checks for nodepath, so it is indeed a path that we are editing + g_free(key); + } else { + SPItem * item = (SPItem *) get_item(SH_NODEPATH); + set_item(item, SH_NODEPATH); + } + break; + case SH_KNOTHOLDER: + if (this->knotholder) { + SPItem * item = (SPItem *) get_item(SH_KNOTHOLDER); + set_item(item, SH_KNOTHOLDER, keep_knotholder); + } + break; } } @@ -262,45 +356,55 @@ void ShapeEditor::update_statusbar () { sp_nodepath_update_statusbar(this->nodepath); } -bool ShapeEditor::is_over_stroke (NR::Point event_p, bool remember) { - - if (!this->nodepath) +bool ShapeEditor::is_over_stroke (Geom::Point event_p, bool remember) { + if (!this->nodepath) return false; // no stroke in knotholder - SPItem *item = get_item(); + const SPItem *item = get_item(SH_NODEPATH); //Translate click point into proper coord system this->curvepoint_doc = desktop->w2d(event_p); this->curvepoint_doc *= sp_item_dt2i_affine(item); - sp_nodepath_ensure_livarot_path(this->nodepath); - - NR::Maybe position = get_nearest_position_on_Path(this->nodepath->livarot_path, this->curvepoint_doc); - if (!position) { + SPCurve *curve = this->nodepath->curve; // not sure if np->curve is always up to date... + Geom::PathVector const &pathv = curve->get_pathvector(); + boost::optional pvpos = Geom::nearestPoint(pathv, this->curvepoint_doc); + if (!pvpos) { + g_print("Warning! Possible error?\n"); return false; } - NR::Point nearest = get_point_on_Path(this->nodepath->livarot_path, position->piece, position->t); - NR::Point delta = nearest - this->curvepoint_doc; + Geom::Point nearest = pathv[pvpos->path_nr].pointAt(pvpos->t); + Geom::Point delta = nearest - this->curvepoint_doc; delta = desktop->d2w(delta); + Inkscape::Preferences *prefs = Inkscape::Preferences::get(); double stroke_tolerance = - ( !SP_OBJECT_STYLE(item)->stroke.isNone() ? - desktop->current_zoom() * - SP_OBJECT_STYLE (item)->stroke_width.computed * - sp_item_i2d_affine (item).expansion() * 0.5 + (( !SP_OBJECT_STYLE(item)->stroke.isNone() ? + desktop->current_zoom() * + SP_OBJECT_STYLE (item)->stroke_width.computed * 0.5 * + to_2geom(sp_item_i2d_affine(item)).descrim() : 0.0) - + prefs_get_int_attribute_limited("options.dragtolerance", "value", 0, 0, 100); //(double) SP_EVENT_CONTEXT(nc)->tolerance; - - bool close = (NR::L2 (delta) < stroke_tolerance); + + prefs->getIntLimited("/options/dragtolerance/value", 0, 0, 100)) / to_2geom(sp_item_i2d_affine(item)).descrim(); + bool close = (Geom::L2 (delta) < stroke_tolerance); if (remember && close) { - this->curvepoint_event[NR::X] = (gint) event_p [NR::X]; - this->curvepoint_event[NR::Y] = (gint) event_p [NR::Y]; + // calculate index for nodepath's representation. + double int_part; + double t = std::modf(pvpos->t, &int_part); + unsigned int segment_index = (unsigned int)int_part + 1; + for (unsigned int i = 0; i < pvpos->path_nr; ++i) { + segment_index += pathv[i].size() + 1; + if (pathv[i].closed()) + segment_index += 1; + } + + this->curvepoint_event[Geom::X] = (gint) event_p [Geom::X]; + this->curvepoint_event[Geom::Y] = (gint) event_p [Geom::Y]; this->hit = true; - this->grab_t = position->t; - this->grab_node = position->piece; + this->grab_t = t; + this->grab_node = segment_index; } return close; @@ -317,7 +421,8 @@ void ShapeEditor::add_node_near_point() { void ShapeEditor::select_segment_near_point(bool toggle) { if (this->nodepath) { sp_nodepath_select_segment_near_point(this->nodepath, this->curvepoint_doc, toggle); - } else if (this->knotholder) { + } + if (this->knotholder) { // we do not select segments in knotholder... yet? } } @@ -348,19 +453,20 @@ void ShapeEditor::curve_drag(gdouble eventx, gdouble eventy) { // The coordinates hasn't changed since the last motion event, abort - if (this->curvepoint_event[NR::X] == x && - this->curvepoint_event[NR::Y] == y) + if (this->curvepoint_event[Geom::X] == x && + this->curvepoint_event[Geom::Y] == y) return; - NR::Point const delta_w(eventx - this->curvepoint_event[NR::X], - eventy - this->curvepoint_event[NR::Y]); - NR::Point const delta_dt(this->desktop->w2d(delta_w)); + Geom::Point const delta_w(eventx - this->curvepoint_event[Geom::X], + eventy - this->curvepoint_event[Geom::Y]); + Geom::Point const delta_dt(this->desktop->w2d(delta_w)); - sp_nodepath_curve_drag (this->grab_node, this->grab_t, delta_dt); //!!! FIXME: which nodepath?!!! also uses current!!! - this->curvepoint_event[NR::X] = x; - this->curvepoint_event[NR::Y] = y; + sp_nodepath_curve_drag (this->nodepath, this->grab_node, this->grab_t, delta_dt); + this->curvepoint_event[Geom::X] = x; + this->curvepoint_event[Geom::Y] = y; - } else if (this->knotholder) { + } + if (this->knotholder) { // we do not drag curve in knotholder } @@ -372,7 +478,7 @@ void ShapeEditor::finish_drag() { } } -void ShapeEditor::select_rect(NR::Rect const &rect, bool add) { +void ShapeEditor::select_rect(Geom::Rect const &rect, bool add) { if (this->nodepath) { sp_nodepath_select_rect(this->nodepath, rect, add); } @@ -431,8 +537,8 @@ void ShapeEditor::set_type_of_segments(NRPathcode code) { sp_node_selected_set_line_type(this->nodepath, code); } -void ShapeEditor::move_nodes_screen(gdouble dx, gdouble dy) { - sp_node_selected_move_screen(this->nodepath, dx, dy); +void ShapeEditor::move_nodes_screen(SPDesktop *desktop, gdouble dx, gdouble dy) { + sp_node_selected_move_screen(desktop, this->nodepath, dx, dy); } void ShapeEditor::move_nodes(gdouble dx, gdouble dy) { sp_node_selected_move(this->nodepath, dx, dy); @@ -451,18 +557,18 @@ void ShapeEditor::scale_nodes_screen(gdouble const grow, int const which) { } void ShapeEditor::select_all (bool invert) { - if (this->nodepath) + if (this->nodepath) sp_nodepath_select_all (this->nodepath, invert); } void ShapeEditor::select_all_from_subpath (bool invert) { - if (this->nodepath) + if (this->nodepath) sp_nodepath_select_all_from_subpath (this->nodepath, invert); } void ShapeEditor::select_next () { if (this->nodepath) { sp_nodepath_select_next (this->nodepath); if (this->nodepath->numSelected() >= 1) { - this->desktop->scroll_to_point(&(this->nodepath->singleSelectedCoords()), 1.0); + this->desktop->scroll_to_point(this->nodepath->singleSelectedCoords(), 1.0); } } } @@ -470,7 +576,7 @@ void ShapeEditor::select_prev () { if (this->nodepath) { sp_nodepath_select_prev (this->nodepath); if (this->nodepath->numSelected() >= 1) { - this->desktop->scroll_to_point(&(this->nodepath->singleSelectedCoords()), 1.0); + this->desktop->scroll_to_point(this->nodepath->singleSelectedCoords(), 1.0); } } } @@ -480,18 +586,22 @@ void ShapeEditor::show_handles (bool show) { sp_nodepath_show_handles (this->nodepath, show); } +void ShapeEditor::show_helperpath (bool show) { + if (this->nodepath) + sp_nodepath_show_helperpath (this->nodepath, show); +} -void ShapeEditor::flip (NR::Dim2 axis, NR::Maybe center) { - if (this->nodepath) +void ShapeEditor::flip (Geom::Dim2 axis, boost::optional center) { + if (this->nodepath) sp_nodepath_flip (this->nodepath, axis, center); } -void ShapeEditor::distribute (NR::Dim2 axis) { - if (this->nodepath) +void ShapeEditor::distribute (Geom::Dim2 axis) { + if (this->nodepath) sp_nodepath_selected_distribute (this->nodepath, axis); } -void ShapeEditor::align (NR::Dim2 axis) { - if (this->nodepath) +void ShapeEditor::align (Geom::Dim2 axis) { + if (this->nodepath) sp_nodepath_selected_align (this->nodepath, axis); }