X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;f=src%2Flpe-tool-context.cpp;h=5c6575ccaed70ce19a6e3f59707509d8002fad5b;hb=6c3e745a94ef6b25a4ef9f018d350a7535aa45af;hp=fb252729dc1643245c5b350343e32c2e5b345282;hpb=72c33248c0b7e5b1d9a63b205947d98e379d1775;p=inkscape.git diff --git a/src/lpe-tool-context.cpp b/src/lpe-tool-context.cpp index fb252729d..5c6575cca 100644 --- a/src/lpe-tool-context.cpp +++ b/src/lpe-tool-context.cpp @@ -17,13 +17,17 @@ #include "config.h" #endif +#include <2geom/sbasis-geometric.h> +#include + +#include "macros.h" #include "forward.h" #include "pixmaps/cursor-node.xpm" #include "pixmaps/cursor-crosshairs.xpm" #include #include "desktop.h" #include "message-context.h" -#include "prefs-utils.h" +#include "preferences.h" #include "shape-editor.h" #include "selection.h" #include "desktop-handles.h" @@ -31,6 +35,8 @@ #include "display/curve.h" #include "display/canvas-bpath.h" #include "message-stack.h" +#include "sp-path.h" +#include "helper/units.h" #include "lpe-tool-context.h" @@ -39,12 +45,13 @@ static void sp_lpetool_context_init(SPLPEToolContext *erc); static void sp_lpetool_context_dispose(GObject *object); static void sp_lpetool_context_setup(SPEventContext *ec); -static void sp_lpetool_context_set(SPEventContext *ec, gchar const *key, gchar const *val); +static void sp_lpetool_context_set(SPEventContext *ec, Inkscape::Preferences::Entry *); +static gint sp_lpetool_context_item_handler(SPEventContext *ec, SPItem *item, GdkEvent *event); static gint sp_lpetool_context_root_handler(SPEventContext *ec, GdkEvent *event); void sp_lpetool_context_selection_changed(Inkscape::Selection *selection, gpointer data); -const int num_subtools = 7; +const int num_subtools = 8; Inkscape::LivePathEffect::EffectType lpesubtools[] = { Inkscape::LivePathEffect::INVALID_LPE, // this must be here to account for the "all inactive" action @@ -54,6 +61,7 @@ Inkscape::LivePathEffect::EffectType lpesubtools[] = { Inkscape::LivePathEffect::PARALLEL, Inkscape::LivePathEffect::PERP_BISECTOR, Inkscape::LivePathEffect::ANGLE_BISECTOR, + Inkscape::LivePathEffect::MIRROR_SYMMETRY, }; static SPPenContextClass *lpetool_parent_class = 0; @@ -92,6 +100,7 @@ sp_lpetool_context_class_init(SPLPEToolContextClass *klass) event_context_class->setup = sp_lpetool_context_setup; event_context_class->set = sp_lpetool_context_set; event_context_class->root_handler = sp_lpetool_context_root_handler; + event_context_class->item_handler = sp_lpetool_context_item_handler; } static void @@ -102,6 +111,7 @@ sp_lpetool_context_init(SPLPEToolContext *lc) lc->hot_y = 7; lc->canvas_bbox = NULL; + lc->measuring_items = new std::map; new (&lc->sel_changed_connection) sigc::connection(); } @@ -117,9 +127,17 @@ sp_lpetool_context_dispose(GObject *object) lc->canvas_bbox = NULL; } + lpetool_delete_measuring_items(lc); + delete lc->measuring_items; + lc->measuring_items = NULL; + lc->sel_changed_connection.disconnect(); lc->sel_changed_connection.~connection(); + if (lc->_lpetool_message_context) { + delete lc->_lpetool_message_context; + } + G_OBJECT_CLASS(lpetool_parent_class)->dispose(object); } @@ -142,20 +160,24 @@ sp_lpetool_context_setup(SPEventContext *ec) lpetool_context_switch_mode(lc, Inkscape::LivePathEffect::INVALID_LPE); lpetool_context_reset_limiting_bbox(lc); + lpetool_create_measuring_items(lc); // TODO temp force: ec->enableSelectionCue(); + + Inkscape::Preferences *prefs = Inkscape::Preferences::get(); if (item) { lc->shape_editor->set_item(item, SH_NODEPATH); lc->shape_editor->set_item(item, SH_KNOTHOLDER); } - if (prefs_get_int_attribute("tools.lpetool", "selcue", 0) != 0) { + if (prefs->getBool("/tools/lpetool/selcue")) { ec->enableSelectionCue(); } - lc->_message_context = new Inkscape::MessageContext((ec->desktop)->messageStack()); + lc->_lpetool_message_context = new Inkscape::MessageContext((ec->desktop)->messageStack()); + lc->shape_editor->update_statusbar(); } @@ -179,24 +201,55 @@ sp_lpetool_context_selection_changed(Inkscape::Selection *selection, gpointer da } static void -sp_lpetool_context_set(SPEventContext *ec, gchar const *key, gchar const *val) +sp_lpetool_context_set(SPEventContext *ec, Inkscape::Preferences::Entry *val) { - // FIXME: how to set this correcly? the value from preferences-skeleton.h doesn't seem to get - // read (it wants to set drag = 1) - lpetool_parent_class->set(ec, key, "drag"); + if (val->getEntryName() == "mode") { + Inkscape::Preferences::get()->setString("/tools/geometric/mode", "drag"); + SP_PEN_CONTEXT(ec)->mode = SP_PEN_CONTEXT_MODE_DRAG; + } - /** + /* //pass on up to parent class to handle common attributes. if ( lpetool_parent_class->set ) { lpetool_parent_class->set(ec, key, val); } - **/ + */ +} + +static gint +sp_lpetool_context_item_handler(SPEventContext *ec, SPItem *item, GdkEvent *event) +{ + gint ret = FALSE; + + switch (event->type) { + case GDK_BUTTON_PRESS: + { + // select the clicked item but do nothing else + Inkscape::Selection * const selection = sp_desktop_selection(ec->desktop); + selection->clear(); + selection->add(item); + ret = TRUE; + break; + } + case GDK_BUTTON_RELEASE: + // TODO: do we need to catch this or can we pass it on to the parent handler? + ret = TRUE; + break; + default: + break; + } + + if (!ret) { + if (((SPEventContextClass *) lpetool_parent_class)->item_handler) + ret = ((SPEventContextClass *) lpetool_parent_class)->item_handler(ec, item, event); + } + + return ret; } gint sp_lpetool_context_root_handler(SPEventContext *event_context, GdkEvent *event) { - //g_print ("sp_lpetool_context_root_handler()\n"); SPLPEToolContext *lc = SP_LPETOOL_CONTEXT(event_context); SPDesktop *desktop = event_context->desktop; Inkscape::Selection *selection = sp_desktop_selection (desktop); @@ -205,24 +258,22 @@ sp_lpetool_context_root_handler(SPEventContext *event_context, GdkEvent *event) if (sp_pen_context_has_waiting_LPE(lc)) { // quit when we are waiting for a LPE to be applied - g_print ("LPETool has waiting LPE. We call the pen tool parent context and return\n"); ret = ((SPEventContextClass *) lpetool_parent_class)->root_handler(event_context, event); return ret; } switch (event->type) { case GDK_BUTTON_PRESS: - g_print ("GDK_BUTTON_PRESS\n"); - if (lc->mode == Inkscape::LivePathEffect::INVALID_LPE) { - // don't do anything for now if we are inactive - desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Choose a construction tool from the toolbar.")); - g_print ("Flash statusbar\n"); - ret = true; - break; - } - if (event->button.button == 1 && !event_context->space_panning) { - g_print (" ... (passed if construct)\n"); + if (lc->mode == Inkscape::LivePathEffect::INVALID_LPE) { + // don't do anything for now if we are inactive (except clearing the selection + // since this was a click into empty space) + selection->clear(); + desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Choose a construction tool from the toolbar.")); + ret = true; + break; + } + // save drag origin event_context->xp = (gint) event->button.x; event_context->yp = (gint) event->button.y; @@ -231,13 +282,11 @@ sp_lpetool_context_root_handler(SPEventContext *event_context, GdkEvent *event) using namespace Inkscape::LivePathEffect; - int mode = prefs_get_int_attribute("tools.lpetool", "mode", 0); + Inkscape::Preferences *prefs = Inkscape::Preferences::get(); + int mode = prefs->getInt("/tools/lpetool/mode"); EffectType type = lpesubtools[mode]; - g_print ("Activating mode %d\n", mode); - // save drag origin - bool over_stroke = lc->shape_editor->is_over_stroke(NR::Point(event->button.x, event->button.y), true); - g_print ("over_stroke: %s\n", over_stroke ? "true" : "false"); + //bool over_stroke = lc->shape_editor->is_over_stroke(NR::Point(event->button.x, event->button.y), true); sp_pen_context_wait_for_LPE_mouse_clicks(lc, type, Inkscape::LivePathEffect::Effect::acceptsNumClicks(type)); @@ -322,6 +371,23 @@ lpetool_mode_to_index(Inkscape::LivePathEffect::EffectType const type) { return -1; } +/* + * Checks whether an item has a construction applied as LPE and if so returns the index in + * lpesubtools of this construction + */ +int lpetool_item_has_construction(SPLPEToolContext *lc, SPItem *item) +{ + if (!SP_IS_LPE_ITEM(item)) { + return -1; + } + + Inkscape::LivePathEffect::Effect* lpe = sp_lpe_item_get_current_lpe(SP_LPE_ITEM(item)); + if (!lpe) { + return -1; + } + return lpetool_mode_to_index(lpe->effectType()); +} + /* * Attempts to perform the construction of the given type (i.e., to apply the corresponding LPE) to * a single selected item. Returns whether we succeeded. @@ -353,6 +419,21 @@ lpetool_context_switch_mode(SPLPEToolContext *lc, Inkscape::LivePathEffect::Effe } } +void +lpetool_get_limiting_bbox_corners(SPDocument *document, Geom::Point &A, Geom::Point &B) { + Geom::Coord w = sp_document_width(document); + Geom::Coord h = sp_document_height(document); + Inkscape::Preferences *prefs = Inkscape::Preferences::get(); + + double ulx = prefs->getDouble("/tools/lpetool/bbox_upperleftx", 0); + double uly = prefs->getDouble("/tools/lpetool/bbox_upperlefty", 0); + double lrx = prefs->getDouble("/tools/lpetool/bbox_lowerrightx", w); + double lry = prefs->getDouble("/tools/lpetool/bbox_lowerrighty", h); + + A = Geom::Point(ulx, uly); + B = Geom::Point(lrx, lry); +} + /* * Reads the limiting bounding box from preferences and draws it on the screen */ @@ -365,15 +446,17 @@ lpetool_context_reset_limiting_bbox(SPLPEToolContext *lc) lc->canvas_bbox = NULL; } - if (prefs_get_int_attribute("tools.lpetool", "show_bbox", 1) == 0) + Inkscape::Preferences *prefs = Inkscape::Preferences::get(); + if (!prefs->getBool("/tools/lpetool/show_bbox", true)) return; SPDocument *document = sp_desktop_document(lc->desktop); - Geom::Coord w = sp_document_width(document); - Geom::Coord h = sp_document_height(document); - Geom::Point A(0,0); - Geom::Point B(w,h); + Geom::Point A, B; + lpetool_get_limiting_bbox_corners(document, A, B); + NR::Matrix doc2dt(lc->desktop->doc2dt()); + A *= doc2dt; + B *= doc2dt; Geom::Rect rect(A, B); SPCurve *curve = SPCurve::new_from_rect(rect); @@ -382,6 +465,111 @@ lpetool_context_reset_limiting_bbox(SPLPEToolContext *lc) sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(lc->canvas_bbox), 0x0000ffff, 0.8, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT, 5, 5); } +static void +set_pos_and_anchor(SPCanvasText *canvas_text, const Geom::Piecewise > &pwd2, + const double t, const double length, bool use_curvature = false) +{ + using namespace Geom; + + Piecewise > pwd2_reparam = arc_length_parametrization(pwd2, 2 , 0.1); + double t_reparam = pwd2_reparam.cuts.back() * t; + Point pos = pwd2_reparam.valueAt(t_reparam); + Point dir = unit_vector(derivative(pwd2_reparam).valueAt(t_reparam)); + Point n = -rot90(dir); + double angle = Geom::angle_between(dir, Point(1,0)); + + sp_canvastext_set_coords(canvas_text, pos + n * length); + sp_canvastext_set_anchor(canvas_text, std::sin(angle), -std::cos(angle)); +} + +void +lpetool_create_measuring_items(SPLPEToolContext *lc, Inkscape::Selection *selection) +{ + if (!selection) { + selection = sp_desktop_selection(lc->desktop); + } + Inkscape::Preferences *prefs = Inkscape::Preferences::get(); + bool show = prefs->getBool("/tools/lpetool/show_measuring_info", true); + + SPPath *path; + SPCurve *curve; + SPCanvasText *canvas_text; + SPCanvasGroup *tmpgrp = sp_desktop_tempgroup(lc->desktop); + gchar *arc_length; + double lengthval; + + for (GSList const *i = selection->itemList(); i != NULL; i = i->next) { + if (SP_IS_PATH(i->data)) { + path = SP_PATH(i->data); + curve = sp_shape_get_curve(SP_SHAPE(path)); + Geom::Piecewise > pwd2 = paths_to_pw(curve->get_pathvector()); + canvas_text = (SPCanvasText *) sp_canvastext_new(tmpgrp, lc->desktop, Geom::Point(0,0), ""); + if (!show) + sp_canvas_item_hide(SP_CANVAS_ITEM(canvas_text)); + + SPUnitId unitid = static_cast(prefs->getInt("/tools/lpetool/unitid", SP_UNIT_PX)); + SPUnit unit = sp_unit_get_by_id(unitid); + + lengthval = Geom::length(pwd2); + gboolean success; + success = sp_convert_distance(&lengthval, &sp_unit_get_by_id(SP_UNIT_PX), &unit); + arc_length = g_strdup_printf("%.2f %s", lengthval, success ? sp_unit_get_abbreviation(&unit) : "px"); + sp_canvastext_set_text (canvas_text, arc_length); + set_pos_and_anchor(canvas_text, pwd2, 0.5, 10); + // TODO: must we free arc_length? + (*lc->measuring_items)[path] = SP_CANVAS_ITEM(canvas_text); + } + } +} + +void +lpetool_delete_measuring_items(SPLPEToolContext *lc) +{ + std::map::iterator i; + for (i = lc->measuring_items->begin(); i != lc->measuring_items->end(); ++i) { + gtk_object_destroy(GTK_OBJECT(i->second)); + } + lc->measuring_items->clear(); +} + +void +lpetool_update_measuring_items(SPLPEToolContext *lc) +{ + Inkscape::Preferences *prefs = Inkscape::Preferences::get(); + SPPath *path; + SPCurve *curve; + double lengthval; + gchar *arc_length; + std::map::iterator i; + for (i = lc->measuring_items->begin(); i != lc->measuring_items->end(); ++i) { + path = i->first; + curve = sp_shape_get_curve(SP_SHAPE(path)); + Geom::Piecewise > pwd2 = Geom::paths_to_pw(curve->get_pathvector()); + SPUnitId unitid = static_cast(prefs->getInt("/tools/lpetool/unitid", SP_UNIT_PX)); + SPUnit unit = sp_unit_get_by_id(unitid); + lengthval = Geom::length(pwd2); + gboolean success; + success = sp_convert_distance(&lengthval, &sp_unit_get_by_id(SP_UNIT_PX), &unit); + arc_length = g_strdup_printf("%.2f %s", lengthval, success ? sp_unit_get_abbreviation(&unit) : "px"); + sp_canvastext_set_text (SP_CANVASTEXT(i->second), arc_length); + set_pos_and_anchor(SP_CANVASTEXT(i->second), pwd2, 0.5, 10); + // TODO: must we free arc_length? + } +} + +void +lpetool_show_measuring_info(SPLPEToolContext *lc, bool show) +{ + std::map::iterator i; + for (i = lc->measuring_items->begin(); i != lc->measuring_items->end(); ++i) { + if (show) { + sp_canvas_item_show(i->second); + } else { + sp_canvas_item_hide(i->second); + } + } +} + /* Local Variables: mode:c++