index c909f0a0962ad0da4f0c04ca37f342959acf5baa..f259492962d927d3d5421be5e529f26887428894 100644 (file)
--- a/src/lpe-tool-context.cpp
+++ b/src/lpe-tool-context.cpp
* Authors:
* Maximilian Albert <maximilian.albert@gmail.com>
* Lauris Kaplinski <lauris@kaplinski.com>
+ * Abhishek Sharma
*
* Copyright (C) 1998 The Free Software Foundation
* Copyright (C) 1999-2005 authors
* Released under GNU GPL, read the file 'COPYING' for more information
*/
+#ifdef HAVE_CONFIG_H
#include "config.h"
+#endif
+#include <2geom/sbasis-geometric.h>
+#include <gdk/gdkkeysyms.h>
+
+#include "macros.h"
#include "forward.h"
-#include "pixmaps/cursor-pencil.xpm"
+#include "pixmaps/cursor-crosshairs.xpm"
#include <gtk/gtk.h>
#include "desktop.h"
#include "message-context.h"
-#include "prefs-utils.h"
-
-/**
-
-#include <gdk/gdkkeysyms.h>
-#include <glibmm/i18n.h>
-#include <string>
-#include <cstring>
-#include <numeric>
-
-#include "svg/svg.h"
-#include "display/canvas-bpath.h"
-#include "display/bezier-utils.h"
-
-#include <glib/gmem.h>
-#include "macros.h"
-#include "document.h"
+#include "preferences.h"
+#include "shape-editor.h"
#include "selection.h"
-#include "desktop-events.h"
#include "desktop-handles.h"
-#include "desktop-affine.h"
-#include "desktop-style.h"
-#include "xml/repr.h"
-#include "context-fns.h"
-#include "sp-item.h"
-#include "inkscape.h"
-#include "color.h"
-#include "rubberband.h"
-#include "splivarot.h"
-#include "sp-item-group.h"
-#include "sp-shape.h"
-#include "sp-path.h"
-#include "sp-text.h"
+#include "document.h"
+#include "display/curve.h"
#include "display/canvas-bpath.h"
-#include "display/canvas-arena.h"
-#include "livarot/Shape.h"
-#include <2geom/isnan.h>
-#include <2geom/pathvector.h>
-**/
+#include "display/canvas-text.h"
+#include "message-stack.h"
+#include "sp-path.h"
+#include "helper/units.h"
#include "lpe-tool-context.h"
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);
-const int num_subtools = 4;
+void sp_lpetool_context_selection_changed(Inkscape::Selection *selection, gpointer data);
-Inkscape::LivePathEffect::EffectType lpesubtools[] = {
- Inkscape::LivePathEffect::LINE_SEGMENT,
- Inkscape::LivePathEffect::ANGLE_BISECTOR,
- Inkscape::LivePathEffect::CIRCLE_3PTS,
- Inkscape::LivePathEffect::PERP_BISECTOR,
+const int num_subtools = 8;
+
+SubtoolEntry lpesubtools[] = {
+ // this must be here to account for the "all inactive" action
+ {Inkscape::LivePathEffect::INVALID_LPE, "draw-geometry-inactive"},
+ {Inkscape::LivePathEffect::LINE_SEGMENT, "draw-geometry-line-segment"},
+ {Inkscape::LivePathEffect::CIRCLE_3PTS, "draw-geometry-circle-from-three-points"},
+ {Inkscape::LivePathEffect::CIRCLE_WITH_RADIUS, "draw-geometry-circle-from-radius"},
+ {Inkscape::LivePathEffect::PARALLEL, "draw-geometry-line-parallel"},
+ {Inkscape::LivePathEffect::PERP_BISECTOR, "draw-geometry-line-perpendicular"},
+ {Inkscape::LivePathEffect::ANGLE_BISECTOR, "draw-geometry-angle-bisector"},
+ {Inkscape::LivePathEffect::MIRROR_SYMMETRY, "draw-geometry-mirror"}
};
static SPPenContextClass *lpetool_parent_class = 0;
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
sp_lpetool_context_init(SPLPEToolContext *lc)
{
- /**
- lc->NodeContextCpp::cursor_shape = cursor_pencil_xpm;
- lc->NodeContextCpp::hot_x = 4;
- lc->NodeContextCpp::hot_y = 4;
- **/
- lc->cursor_shape = cursor_pencil_xpm;
- lc->hot_x = 4;
- lc->hot_y = 4;
-
- //lc->tool_state = LPETOOL_STATE_NODE;
+ lc->cursor_shape = cursor_crosshairs_xpm;
+ lc->hot_x = 7;
+ lc->hot_y = 7;
+
+ lc->canvas_bbox = NULL;
+ lc->measuring_items = new std::map<SPPath *, SPCanvasItem*>;
+
+ new (&lc->sel_changed_connection) sigc::connection();
}
static void
sp_lpetool_context_dispose(GObject *object)
{
- //SPLPEToolContext *erc = SP_LPETOOL_CONTEXT(object);
+ SPLPEToolContext *lc = SP_LPETOOL_CONTEXT(object);
+ delete lc->shape_editor;
+
+ if (lc->canvas_bbox) {
+ gtk_object_destroy(GTK_OBJECT(lc->canvas_bbox));
+ 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);
}
if (((SPEventContextClass *) lpetool_parent_class)->setup)
((SPEventContextClass *) lpetool_parent_class)->setup(ec);
- lc->_message_context = new Inkscape::MessageContext((ec->desktop)->messageStack());
+ Inkscape::Selection *selection = sp_desktop_selection (ec->desktop);
+ SPItem *item = selection->singleItem();
- //lc->my_nc = new NodeContextCpp(lc->desktop, lc->prefs_repr, lc->key);
+ lc->sel_changed_connection.disconnect();
+ lc->sel_changed_connection =
+ selection->connectChanged(sigc::bind(sigc::ptr_fun(&sp_lpetool_context_selection_changed), (gpointer)lc));
+
+ lc->shape_editor = new ShapeEditor(ec->desktop);
+
+ lpetool_context_switch_mode(lc, Inkscape::LivePathEffect::INVALID_LPE);
+ lpetool_context_reset_limiting_bbox(lc);
+ lpetool_create_measuring_items(lc);
- if (prefs_get_int_attribute("tools.lpetool", "selcue", 0) != 0) {
- ec->enableSelectionCue();
- }
// 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->getBool("/tools/lpetool/selcue")) {
+ ec->enableSelectionCue();
+ }
+
+ lc->_lpetool_message_context = new Inkscape::MessageContext((ec->desktop)->messageStack());
+}
+
+/**
+\brief Callback that processes the "changed" signal on the selection;
+destroys old and creates new nodepath and reassigns listeners to the new selected item's repr
+*/
+void
+sp_lpetool_context_selection_changed(Inkscape::Selection *selection, gpointer data)
+{
+ SPLPEToolContext *lc = SP_LPETOOL_CONTEXT(data);
+
+ lc->shape_editor->unset_item(SH_KNOTHOLDER);
+ SPItem *item = selection->singleItem();
+ lc->shape_editor->set_item(item, SH_KNOTHOLDER);
}
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);
}
- **/
+ */
}
-/**
-void
-sp_erc_update_toolbox (SPDesktop *desktop, const gchar *id, double value)
+static gint
+sp_lpetool_context_item_handler(SPEventContext *ec, SPItem *item, GdkEvent *event)
{
- desktop->setToolboxAdjustmentValue (id, value);
+ 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)
{
SPLPEToolContext *lc = SP_LPETOOL_CONTEXT(event_context);
- //SPDesktop *desktop = event_context->desktop;
+ SPDesktop *desktop = event_context->desktop;
+ Inkscape::Selection *selection = sp_desktop_selection (desktop);
- //gint ret = FALSE;
bool ret = false;
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");
-
- if (((SPEventContextClass *) lpetool_parent_class)->root_handler) {
- ret = ((SPEventContextClass *) lpetool_parent_class)->root_handler(event_context, event);
- }
-
+ ret = ((SPEventContextClass *) lpetool_parent_class)->root_handler(event_context, event);
return ret;
}
switch (event->type) {
case GDK_BUTTON_PRESS:
- {
- using namespace Inkscape::LivePathEffect;
-
- int mode = prefs_get_int_attribute("tools.lpetool", "mode", 0);
- EffectType type = lpesubtools[mode];
- g_print ("Activating mode %d\n", mode);
-
- sp_pen_context_wait_for_LPE_mouse_clicks(lc, type, Effect::acceptsNumClicks(type));
-
- // we pass the mouse click on to pen tool as the first click which it should collect
- if (((SPEventContextClass *) lpetool_parent_class)->root_handler) {
- ret = ((SPEventContextClass *) lpetool_parent_class)->root_handler(event_context, event);
- }
-
- ret = true;
- break;
- }
- /**
if (event->button.button == 1 && !event_context->space_panning) {
- SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(dc);
-
- NR::Point const button_w(event->button.x,
- event->button.y);
- NR::Point const button_dt(desktop->w2d(button_w));
-
-
- if (Inkscape::have_viable_layer(desktop, dc->_message_context) == false) {
- return TRUE;
+ 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;
}
- sp_lpetool_reset(dc, button_dt);
- sp_lpetool_extinput(dc, event);
- sp_lpetool_apply(dc, button_dt);
- dc->accumulated->reset();
- if (dc->repr) {
- dc->repr = NULL;
- }
+ // save drag origin
+ event_context->xp = (gint) event->button.x;
+ event_context->yp = (gint) event->button.y;
+ event_context->within_tolerance = true;
- Inkscape::Rubberband::get()->start(desktop, button_dt);
- Inkscape::Rubberband::get()->setMode(RUBBERBAND_MODE_TOUCHPATH);
+ using namespace Inkscape::LivePathEffect;
- // initialize first point
- dc->npoints = 0;
+ Inkscape::Preferences *prefs = Inkscape::Preferences::get();
+ int mode = prefs->getInt("/tools/lpetool/mode");
+ EffectType type = lpesubtools[mode].type;
- sp_canvas_item_grab(SP_CANVAS_ITEM(desktop->acetate),
- ( GDK_KEY_PRESS_MASK |
- GDK_BUTTON_RELEASE_MASK |
- GDK_POINTER_MOTION_MASK |
- GDK_BUTTON_PRESS_MASK ),
- NULL,
- event->button.time);
+ //bool over_stroke = lc->shape_editor->is_over_stroke(Geom::Point(event->button.x, event->button.y), true);
- ret = TRUE;
+ sp_pen_context_wait_for_LPE_mouse_clicks(lc, type, Inkscape::LivePathEffect::Effect::acceptsNumClicks(type));
- sp_canvas_force_full_redraw_after_interruptions(desktop->canvas, 3);
- dc->is_drawing = true;
+ // we pass the mouse click on to pen tool as the first click which it should collect
+ ret = ((SPEventContextClass *) lpetool_parent_class)->root_handler(event_context, event);
}
- **/
break;
- case GDK_MOTION_NOTIFY:
- /**
- {
- NR::Point const motion_w(event->motion.x,
- event->motion.y);
- NR::Point motion_dt(desktop->w2d(motion_w));
- sp_lpetool_extinput(dc, event);
-
- dc->_message_context->clear();
-
- if ( dc->is_drawing && (event->motion.state & GDK_BUTTON1_MASK) && !event_context->space_panning) {
- dc->dragging = TRUE;
-
- dc->_message_context->set(Inkscape::NORMAL_MESSAGE, _("<b>Drawing</b> an lpetool stroke"));
-
- if (!sp_lpetool_apply(dc, motion_dt)) {
- ret = TRUE;
- break;
- }
-
- if ( dc->cur != dc->last ) {
- sp_lpetool_brush(dc);
- g_assert( dc->npoints > 0 );
- fit_and_split(dc, FALSE);
- }
- ret = TRUE;
- }
- Inkscape::Rubberband::get()->move(motion_dt);
- }
- **/
- break;
case GDK_BUTTON_RELEASE:
- /**
{
- NR::Point const motion_w(event->button.x, event->button.y);
- NR::Point const motion_dt(desktop->w2d(motion_w));
-
- sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate), event->button.time);
- sp_canvas_end_forced_full_redraws(desktop->canvas);
- dc->is_drawing = false;
-
- if (dc->dragging && event->button.button == 1 && !event_context->space_panning) {
- dc->dragging = FALSE;
-
- NR::Maybe<NR::Rect> const b = Inkscape::Rubberband::get()->getRectangle();
-
- sp_lpetool_apply(dc, motion_dt);
-
- // Remove all temporary line segments
- while (dc->segments) {
- gtk_object_destroy(GTK_OBJECT(dc->segments->data));
- dc->segments = g_slist_remove(dc->segments, dc->segments->data);
- }
-
- // Create object
- fit_and_split(dc, TRUE);
- accumulate_lpetool(dc);
- set_to_accumulated(dc); // performs document_done
-
- // reset accumulated curve
- dc->accumulated->reset();
-
- clear_current(dc);
- if (dc->repr) {
- dc->repr = NULL;
- }
-
- Inkscape::Rubberband::get()->stop();
- dc->_message_context->clear();
- ret = TRUE;
- }
+ /**
break;
+ **/
}
- **/
case GDK_KEY_PRESS:
/**
switch (get_group0_keyval (&event->key)) {
- case GDK_Up:
- case GDK_KP_Up:
- if (!MOD__CTRL_ONLY) {
- dc->angle += 5.0;
- if (dc->angle > 90.0)
- dc->angle = 90.0;
- sp_erc_update_toolbox (desktop, "lpetool-angle", dc->angle);
- ret = TRUE;
- }
- break;
- case GDK_Down:
- case GDK_KP_Down:
- if (!MOD__CTRL_ONLY) {
- dc->angle -= 5.0;
- if (dc->angle < -90.0)
- dc->angle = -90.0;
- sp_erc_update_toolbox (desktop, "lpetool-angle", dc->angle);
- ret = TRUE;
- }
- break;
- case GDK_Right:
- case GDK_KP_Right:
- if (!MOD__CTRL_ONLY) {
- dc->width += 0.01;
- if (dc->width > 1.0)
- dc->width = 1.0;
- sp_erc_update_toolbox (desktop, "altx-lpetool", dc->width * 100); // the same spinbutton is for alt+x
- ret = TRUE;
- }
- break;
- case GDK_Left:
- case GDK_KP_Left:
- if (!MOD__CTRL_ONLY) {
- dc->width -= 0.01;
- if (dc->width < 0.01)
- dc->width = 0.01;
- sp_erc_update_toolbox (desktop, "altx-lpetool", dc->width * 100);
- ret = TRUE;
- }
- break;
- case GDK_Home:
- case GDK_KP_Home:
- dc->width = 0.01;
- sp_erc_update_toolbox (desktop, "altx-lpetool", dc->width * 100);
- ret = TRUE;
- break;
- case GDK_End:
- case GDK_KP_End:
- dc->width = 1.0;
- sp_erc_update_toolbox (desktop, "altx-lpetool", dc->width * 100);
- ret = TRUE;
- break;
- case GDK_x:
- case GDK_X:
- if (MOD__ALT_ONLY) {
- desktop->setToolboxFocusTo ("altx-lpetool");
- ret = TRUE;
- }
- break;
- case GDK_Escape:
- Inkscape::Rubberband::get()->stop();
- if (dc->is_drawing) {
- // if drawing, cancel, otherwise pass it up for deselecting
- lpetool_cancel (dc);
- ret = TRUE;
- }
- break;
- case GDK_z:
- case GDK_Z:
- if (MOD__CTRL_ONLY && dc->is_drawing) {
- // if drawing, cancel, otherwise pass it up for undo
- lpetool_cancel (dc);
- ret = TRUE;
- }
- break;
- default:
- break;
}
- **/
break;
+ **/
case GDK_KEY_RELEASE:
/**
@@ -449,6 +329,219 @@ sp_lpetool_context_root_handler(SPEventContext *event_context, GdkEvent *event)
return ret;
}
+/*
+ * Finds the index in the list of geometric subtools corresponding to the given LPE type.
+ * Returns -1 if no subtool is found.
+ */
+int
+lpetool_mode_to_index(Inkscape::LivePathEffect::EffectType const type) {
+ for (int i = 0; i < num_subtools; ++i) {
+ if (lpesubtools[i].type == type) {
+ return i;
+ }
+ }
+ 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.
+ */
+bool
+lpetool_try_construction(SPLPEToolContext *lc, Inkscape::LivePathEffect::EffectType const type)
+{
+ Inkscape::Selection *selection = sp_desktop_selection(lc->desktop);
+ SPItem *item = selection->singleItem();
+
+ // TODO: should we check whether type represents a valid geometric construction?
+ if (item && SP_IS_LPE_ITEM(item) && Inkscape::LivePathEffect::Effect::acceptsNumClicks(type) == 0) {
+ Inkscape::LivePathEffect::Effect::createAndApply(type, sp_desktop_document(lc->desktop), item);
+ return true;
+ }
+ return false;
+}
+
+void
+lpetool_context_switch_mode(SPLPEToolContext *lc, Inkscape::LivePathEffect::EffectType const type)
+{
+ int index = lpetool_mode_to_index(type);
+ if (index != -1) {
+ lc->mode = type;
+ lc->desktop->setToolboxSelectOneValue ("lpetool_mode_action", index);
+ } else {
+ g_warning ("Invalid mode selected: %d", type);
+ return;
+ }
+}
+
+void
+lpetool_get_limiting_bbox_corners(SPDocument *document, Geom::Point &A, Geom::Point &B) {
+ Geom::Coord w = document->getWidth();
+ Geom::Coord h = document->getHeight();
+ 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
+ */
+// TODO: Note that currently the bbox is not user-settable; we simply use the page borders
+void
+lpetool_context_reset_limiting_bbox(SPLPEToolContext *lc)
+{
+ if (lc->canvas_bbox) {
+ gtk_object_destroy(GTK_OBJECT(lc->canvas_bbox));
+ lc->canvas_bbox = NULL;
+ }
+
+ Inkscape::Preferences *prefs = Inkscape::Preferences::get();
+ if (!prefs->getBool("/tools/lpetool/show_bbox", true))
+ return;
+
+ SPDocument *document = sp_desktop_document(lc->desktop);
+
+ Geom::Point A, B;
+ lpetool_get_limiting_bbox_corners(document, A, B);
+ Geom::Matrix doc2dt(lc->desktop->doc2dt());
+ A *= doc2dt;
+ B *= doc2dt;
+
+ Geom::Rect rect(A, B);
+ SPCurve *curve = SPCurve::new_from_rect(rect);
+
+ lc->canvas_bbox = sp_canvas_bpath_new (sp_desktop_controls(lc->desktop), curve);
+ 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<Geom::D2<Geom::SBasis> > &pwd2,
+ const double t, const double length, bool /*use_curvature*/ = false)
+{
+ using namespace Geom;
+
+ Piecewise<D2<SBasis> > 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(path)->getCurve();
+ Geom::Piecewise<Geom::D2<Geom::SBasis> > 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<SPUnitId>(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<SPPath *, SPCanvasItem*>::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<SPPath *, SPCanvasItem*>::iterator i;
+ for (i = lc->measuring_items->begin(); i != lc->measuring_items->end(); ++i) {
+ path = i->first;
+ curve = SP_SHAPE(path)->getCurve();
+ Geom::Piecewise<Geom::D2<Geom::SBasis> > pwd2 = Geom::paths_to_pw(curve->get_pathvector());
+ SPUnitId unitid = static_cast<SPUnitId>(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<SPPath *, SPCanvasItem*>::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++
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 :