diff options
| author | Felipe Corr??a da Silva Sanches <juca@members.fsf.org> | 2011-06-06 23:21:21 +0000 |
|---|---|---|
| committer | Felipe C. da S. Sanches <juca@members.fsf.org> | 2011-06-06 23:21:21 +0000 |
| commit | da2a6c9d0f013483a708cad53bb83c0d0766bd69 (patch) | |
| tree | 372b87e9fc196804e27e0ff7b826917e70e1d2e2 /src | |
| parent | Extensions. Adding i18n support to voronoi2svg.py and interp_att_g.py. New IN... (diff) | |
| download | inkscape-da2a6c9d0f013483a708cad53bb83c0d0766bd69.tar.gz inkscape-da2a6c9d0f013483a708cad53bb83c0d0766bd69.zip | |
Introducing our new nice measurement tool! :-D
(bzr r10259)
Diffstat (limited to 'src')
| -rw-r--r-- | src/Makefile_insert | 3 | ||||
| -rw-r--r-- | src/measure-context.cpp | 237 | ||||
| -rw-r--r-- | src/measure-context.h | 35 | ||||
| -rw-r--r-- | src/tools-switch.cpp | 8 | ||||
| -rw-r--r-- | src/tools-switch.h | 1 | ||||
| -rw-r--r-- | src/ui/dialog/inkscape-preferences.h | 1 | ||||
| -rw-r--r-- | src/ui/icon-names.h | 2 | ||||
| -rw-r--r-- | src/verbs.cpp | 11 | ||||
| -rw-r--r-- | src/verbs.h | 2 | ||||
| -rw-r--r-- | src/widgets/toolbox.cpp | 15 |
10 files changed, 313 insertions, 2 deletions
diff --git a/src/Makefile_insert b/src/Makefile_insert index e7bf9715a..aadf9d404 100644 --- a/src/Makefile_insert +++ b/src/Makefile_insert @@ -251,7 +251,8 @@ ink_common_sources += \ vanishing-point.cpp vanishing-point.h \ verbs.cpp verbs.h \ version.cpp version.h \ - zoom-context.cpp zoom-context.h + zoom-context.cpp zoom-context.h \ + measure-context.cpp measure-context.h # Additional dependencies diff --git a/src/measure-context.cpp b/src/measure-context.cpp new file mode 100644 index 000000000..1e0339424 --- /dev/null +++ b/src/measure-context.cpp @@ -0,0 +1,237 @@ +/* + * Our nice measuring tool + * + * Authors: + * Felipe Correa da Silva Sanches <juca@members.fsf.org> + * + * Copyright (C) 2011 Authors + * + * Released under GNU GPL, read the file 'COPYING' for more information + */ + + +#include <gdk/gdkkeysyms.h> + +#include "macros.h" +#include "display/sp-ctrlline.h" +#include "display/sp-canvas-item.h" +#include "display/sp-canvas-util.h" +#include "desktop.h" +#include "pixmaps/cursor-measure.xpm" +#include "preferences.h" +#include "inkscape.h" +#include "desktop-handles.h" +#include "measure-context.h" +#include "display/canvas-text.h" + +static void sp_measure_context_class_init(SPMeasureContextClass *klass); +static void sp_measure_context_init(SPMeasureContext *measure_context); +static void sp_measure_context_setup(SPEventContext *ec); +static void sp_measure_context_finish (SPEventContext *ec); + +static gint sp_measure_context_root_handler(SPEventContext *event_context, GdkEvent *event); +static gint sp_measure_context_item_handler(SPEventContext *event_context, SPItem *item, GdkEvent *event); + +static SPEventContextClass *parent_class; + +static gint xp = 0, yp = 0; // where drag started +static gint tolerance = 0; +static bool within_tolerance = false; +static SPCanvasItem * line = NULL; +Geom::Point start_point; +SPCanvasItem *measure_text = NULL; + +GType sp_measure_context_get_type(void) +{ + static GType type = 0; + + if (!type) { + GTypeInfo info = { + sizeof(SPMeasureContextClass), + NULL, NULL, + (GClassInitFunc) sp_measure_context_class_init, + NULL, NULL, + sizeof(SPMeasureContext), + 4, + (GInstanceInitFunc) sp_measure_context_init, + NULL, /* value_table */ + }; + type = g_type_register_static(SP_TYPE_EVENT_CONTEXT, "SPMeasureContext", &info, (GTypeFlags) 0); + } + + return type; +} + +static void sp_measure_context_class_init(SPMeasureContextClass *klass) +{ + SPEventContextClass *event_context_class = (SPEventContextClass *) klass; + + parent_class = (SPEventContextClass*) g_type_class_peek_parent(klass); + + event_context_class->setup = sp_measure_context_setup; + event_context_class->finish = sp_measure_context_finish; + + event_context_class->root_handler = sp_measure_context_root_handler; + event_context_class->item_handler = sp_measure_context_item_handler; +} + +static void sp_measure_context_init (SPMeasureContext *measure_context) +{ + SPEventContext *event_context = SP_EVENT_CONTEXT(measure_context); + + event_context->cursor_shape = cursor_measure_xpm; + event_context->hot_x = 3; + event_context->hot_y = 5; +} + +static void +sp_measure_context_finish (SPEventContext *ec) +{ + SPMeasureContext *mc = SP_MEASURE_CONTEXT(ec); + + ec->enableGrDrag(false); + + if (mc->grabbed) { + sp_canvas_item_ungrab(mc->grabbed, GDK_CURRENT_TIME); + mc->grabbed = NULL; + } +} + +static void sp_measure_context_setup(SPEventContext *ec) +{ + if (((SPEventContextClass *) parent_class)->setup) { + ((SPEventContextClass *) parent_class)->setup(ec); + } +} + +static gint sp_measure_context_item_handler(SPEventContext *event_context, SPItem *item, GdkEvent *event) +{ + gint ret = FALSE; + + if (((SPEventContextClass *) parent_class)->item_handler) { + ret = ((SPEventContextClass *) parent_class)->item_handler (event_context, item, event); + } + + return ret; +} + +static gint sp_measure_context_root_handler(SPEventContext *event_context, GdkEvent *event) +{ + SPDesktop *desktop = event_context->desktop; + Inkscape::Preferences *prefs = Inkscape::Preferences::get(); + tolerance = prefs->getIntLimited("/options/dragtolerance/value", 0, 0, 100); + + SPMeasureContext *mc = SP_MEASURE_CONTEXT(event_context); + gint ret = FALSE; + + switch (event->type) { + case GDK_BUTTON_PRESS: + { + Geom::Point const button_w(event->button.x, event->button.y); + start_point = desktop->w2d(button_w); + if (event->button.button == 1 && !event_context->space_panning) { + // save drag origin + xp = (gint) event->button.x; + yp = (gint) event->button.y; + within_tolerance = true; + + ret = TRUE; + } + + if (!line){ + SPDesktop *desktop = inkscape_active_desktop(); + line = sp_canvas_item_new(sp_desktop_controls(desktop), SP_TYPE_CTRLLINE, NULL); + } + + if (!measure_text){ + measure_text = sp_canvastext_new(sp_desktop_tempgroup(desktop), desktop, start_point, ""); + SP_CANVASTEXT(measure_text)->rgba = 0x7f7f7fff; + sp_canvastext_set_anchor(SP_CANVASTEXT(measure_text), -1, 1);//why? + } + + sp_ctrlline_set_coords (SP_CTRLLINE(line), start_point, start_point); + sp_canvastext_set_text (SP_CANVASTEXT(measure_text), ""); + sp_canvas_item_show (line); + sp_canvas_item_show (measure_text); + + sp_canvas_item_grab(SP_CANVAS_ITEM(desktop->acetate), + GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK | GDK_BUTTON_RELEASE_MASK | GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_PRESS_MASK, + NULL, event->button.time); + mc->grabbed = SP_CANVAS_ITEM(desktop->acetate); + break; + } + + case GDK_MOTION_NOTIFY: + { + if (event->motion.state & GDK_BUTTON1_MASK && !event_context->space_panning) { + ret = TRUE; + + if ( within_tolerance + && ( abs( (gint) event->motion.x - xp ) < tolerance ) + && ( abs( (gint) event->motion.y - yp ) < tolerance ) ) { + break; // do not drag if we're within tolerance from origin + } + // Once the user has moved farther than tolerance from the original location + // (indicating they intend to move the object, not click), then always process the + // motion notify coordinates as given (no snapping back to origin) + within_tolerance = false; + + Geom::Point const motion_w(event->motion.x, event->motion.y); + Geom::Point const motion_dt(desktop->w2d(motion_w)); + + sp_ctrlline_set_coords (SP_CTRLLINE(line), start_point[Geom::X], start_point[Geom::Y], motion_dt[Geom::X], motion_dt[Geom::Y]); + + Geom::Point measure_text_pos = (start_point + motion_dt)/2; + double length = (start_point - motion_dt).length(); + char* measure_str = (char*) malloc(sizeof(char)*20); + sprintf(measure_str, "%2f", length); + + sp_canvastext_set_coords (SP_CANVASTEXT(measure_text), desktop->dt2doc(measure_text_pos)); + sp_canvastext_set_text (SP_CANVASTEXT(measure_text), measure_str); + free(measure_str); + + gobble_motion_events(GDK_BUTTON1_MASK); + } + break; + } + + case GDK_BUTTON_RELEASE: + { + if (line){ + sp_canvas_item_hide(line); + } + + if (measure_text){ + sp_canvas_item_hide(measure_text); + } + + if (mc->grabbed) { + sp_canvas_item_ungrab(mc->grabbed, event->button.time); + mc->grabbed = NULL; + } + xp = yp = 0; + break; + } + default: + break; + } + + if (!ret) { + if (((SPEventContextClass *) parent_class)->root_handler) { + ret = ((SPEventContextClass *) parent_class)->root_handler(event_context, event); + } + } + + return ret; +} + +/* + Local Variables: + mode:c++ + c-file-style:"stroustrup" + c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +)) + indent-tabs-mode:nil + fill-column:99 + End: +*/ +// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 : diff --git a/src/measure-context.h b/src/measure-context.h new file mode 100644 index 000000000..f6065b3e6 --- /dev/null +++ b/src/measure-context.h @@ -0,0 +1,35 @@ +#ifndef __SP_MEASURING_CONTEXT_H__ +#define __SP_MEASURING_CONTEXT_H__ + +/* + * Our fine measuring tool + * + * Authors: + * Felipe Correa da Silva Sanches <juca@members.fsf.org> + * + * Copyright (C) 2011 Authors + * + * Released under GNU GPL, read the file 'COPYING' for more information + */ + +#include "event-context.h" + +#define SP_TYPE_MEASURE_CONTEXT (sp_measure_context_get_type ()) +#define SP_MEASURE_CONTEXT(obj) (GTK_CHECK_CAST ((obj), SP_TYPE_MEASURE_CONTEXT, SPMeasureContext)) +#define SP_IS_MEASURE_CONTEXT(obj) (GTK_CHECK_TYPE ((obj), SP_TYPE_MEASURE_CONTEXT)) + +class SPMeasureContext; +class SPMeasureContextClass; + +struct SPMeasureContext { + SPEventContext event_context; + SPCanvasItem *grabbed; +}; + +struct SPMeasureContextClass { + SPEventContextClass parent_class; +}; + +GType sp_measure_context_get_type (void); + +#endif diff --git a/src/tools-switch.cpp b/src/tools-switch.cpp index 1f624cc35..42eaf4474 100644 --- a/src/tools-switch.cpp +++ b/src/tools-switch.cpp @@ -50,6 +50,7 @@ #include "sp-flowtext.h" #include "gradient-context.h" #include "zoom-context.h" +#include "measure-context.h" #include "dropper-context.h" #include "connector-context.h" #include "flood-context.h" @@ -75,6 +76,7 @@ static char const *const tool_names[] = { "/tools/text", "/tools/gradient", "/tools/zoom", + "/tools/measure", "/tools/dropper", "/tools/connector", "/tools/paintbucket", @@ -208,6 +210,12 @@ tools_switch(SPDesktop *dt, int num) inkscape_eventcontext_set(sp_desktop_event_context(dt)); dt->tipsMessageContext()->set(Inkscape::NORMAL_MESSAGE, _("<b>Click</b> or <b>drag around an area</b> to zoom in, <b>Shift+click</b> to zoom out.")); break; + case TOOLS_MEASURE: + dt->set_event_context(SP_TYPE_MEASURE_CONTEXT, tool_names[num]); + dt->activate_guides(false); + inkscape_eventcontext_set(sp_desktop_event_context(dt)); + dt->tipsMessageContext()->set(Inkscape::NORMAL_MESSAGE, _("<b>Drag</b> to measure the dimensions of objects.")); + break; case TOOLS_DROPPER: dt->set_event_context(SP_TYPE_DROPPER_CONTEXT, tool_names[num]); dt->activate_guides(false); diff --git a/src/tools-switch.h b/src/tools-switch.h index 4cc9aa93d..75c728179 100644 --- a/src/tools-switch.h +++ b/src/tools-switch.h @@ -31,6 +31,7 @@ enum { TOOLS_TEXT, TOOLS_GRADIENT, TOOLS_ZOOM, + TOOLS_MEASURE, TOOLS_DROPPER, TOOLS_CONNECTOR, TOOLS_PAINTBUCKET, diff --git a/src/ui/dialog/inkscape-preferences.h b/src/ui/dialog/inkscape-preferences.h index eede9eafe..34bf1e87a 100644 --- a/src/ui/dialog/inkscape-preferences.h +++ b/src/ui/dialog/inkscape-preferences.h @@ -46,6 +46,7 @@ enum { PREFS_PAGE_TOOLS_TWEAK, PREFS_PAGE_TOOLS_SPRAY, PREFS_PAGE_TOOLS_ZOOM, + PREFS_PAGE_TOOLS_MEASURE, PREFS_PAGE_TOOLS_SHAPES, PREFS_PAGE_TOOLS_SHAPES_RECT, PREFS_PAGE_TOOLS_SHAPES_3DBOX, diff --git a/src/ui/icon-names.h b/src/ui/icon-names.h index 2ec03c5cc..f7c16b0ed 100644 --- a/src/ui/icon-names.h +++ b/src/ui/icon-names.h @@ -556,6 +556,8 @@ "xml-text-new" #define INKSCAPE_ICON_ZOOM \ "zoom" +#define INKSCAPE_ICON_MEASURE \ + "measure" #define INKSCAPE_ICON_ZOOM_DOUBLE_SIZE \ "zoom-double-size" #define INKSCAPE_ICON_ZOOM_FIT_DRAWING \ diff --git a/src/verbs.cpp b/src/verbs.cpp index de935f700..c332cc93d 100644 --- a/src/verbs.cpp +++ b/src/verbs.cpp @@ -1466,6 +1466,9 @@ ContextVerb::perform(SPAction *action, void *data, void */*pdata*/) case SP_VERB_CONTEXT_ZOOM: tools_switch(dt, TOOLS_ZOOM); break; + case SP_VERB_CONTEXT_MEASURE: + tools_switch(dt, TOOLS_MEASURE); + break; case SP_VERB_CONTEXT_DROPPER: tools_switch(dt, TOOLS_DROPPER); break; @@ -1542,6 +1545,10 @@ ContextVerb::perform(SPAction *action, void *data, void */*pdata*/) prefs->setInt("/dialogs/preferences/page", PREFS_PAGE_TOOLS_ZOOM); dt->_dlg_mgr->showDialog("InkscapePreferences"); break; + case SP_VERB_CONTEXT_MEASURE_PREFS: + prefs->setInt("/dialogs/preferences/page", PREFS_PAGE_TOOLS_MEASURE); + dt->_dlg_mgr->showDialog("InkscapePreferences"); + break; case SP_VERB_CONTEXT_DROPPER_PREFS: prefs->setInt("/dialogs/preferences/page", PREFS_PAGE_TOOLS_DROPPER); dt->_dlg_mgr->showDialog("InkscapePreferences"); @@ -2522,6 +2529,8 @@ Verb *Verb::_base_verbs[] = { N_("Create and edit gradients"), INKSCAPE_ICON_COLOR_GRADIENT), new ContextVerb(SP_VERB_CONTEXT_ZOOM, "ToolZoom", N_("Zoom"), N_("Zoom in or out"), INKSCAPE_ICON_ZOOM), + new ContextVerb(SP_VERB_CONTEXT_MEASURE, "ToolMeasure", N_("Measure"), + N_("Measurement tool"), INKSCAPE_ICON_MEASURE), new ContextVerb(SP_VERB_CONTEXT_DROPPER, "ToolDropper", N_("Dropper"), N_("Pick colors from image"), INKSCAPE_ICON_COLOR_PICKER), new ContextVerb(SP_VERB_CONTEXT_CONNECTOR, "ToolConnector", N_("Connector"), @@ -2565,6 +2574,8 @@ Verb *Verb::_base_verbs[] = { N_("Open Preferences for the Gradient tool"), NULL), new ContextVerb(SP_VERB_CONTEXT_ZOOM_PREFS, "ZoomPrefs", N_("Zoom Preferences"), N_("Open Preferences for the Zoom tool"), NULL), + new ContextVerb(SP_VERB_CONTEXT_MEASURE_PREFS, "MeasurePrefs", N_("Measure Preferences"), + N_("Open Preferences for the Measure tool"), NULL), new ContextVerb(SP_VERB_CONTEXT_DROPPER_PREFS, "DropperPrefs", N_("Dropper Preferences"), N_("Open Preferences for the Dropper tool"), NULL), new ContextVerb(SP_VERB_CONTEXT_CONNECTOR_PREFS, "ConnectorPrefs", N_("Connector Preferences"), diff --git a/src/verbs.h b/src/verbs.h index 0c781f0b6..de7a96797 100644 --- a/src/verbs.h +++ b/src/verbs.h @@ -165,6 +165,7 @@ enum { SP_VERB_CONTEXT_TEXT, SP_VERB_CONTEXT_GRADIENT, SP_VERB_CONTEXT_ZOOM, + SP_VERB_CONTEXT_MEASURE, SP_VERB_CONTEXT_DROPPER, SP_VERB_CONTEXT_CONNECTOR, SP_VERB_CONTEXT_PAINTBUCKET, @@ -187,6 +188,7 @@ enum { SP_VERB_CONTEXT_TEXT_PREFS, SP_VERB_CONTEXT_GRADIENT_PREFS, SP_VERB_CONTEXT_ZOOM_PREFS, + SP_VERB_CONTEXT_MEASURE_PREFS, SP_VERB_CONTEXT_DROPPER_PREFS, SP_VERB_CONTEXT_CONNECTOR_PREFS, SP_VERB_CONTEXT_PAINTBUCKET_PREFS, diff --git a/src/widgets/toolbox.cpp b/src/widgets/toolbox.cpp index 0dcecfcb1..9c3a8346e 100644 --- a/src/widgets/toolbox.cpp +++ b/src/widgets/toolbox.cpp @@ -133,6 +133,7 @@ static void sp_node_toolbox_prep(SPDesktop *desktop, GtkActionGroup* mainA static void sp_tweak_toolbox_prep(SPDesktop *desktop, GtkActionGroup* mainActions, GObject* holder); static void sp_spray_toolbox_prep(SPDesktop *desktop, GtkActionGroup* mainActions, GObject* holder); static void sp_zoom_toolbox_prep(SPDesktop *desktop, GtkActionGroup* mainActions, GObject* holder); +static void sp_measure_toolbox_prep(SPDesktop *desktop, GtkActionGroup* mainActions, GObject* holder); static void sp_star_toolbox_prep(SPDesktop *desktop, GtkActionGroup* mainActions, GObject* holder); static void sp_arc_toolbox_prep(SPDesktop *desktop, GtkActionGroup* mainActions, GObject* holder); static void sp_rect_toolbox_prep(SPDesktop *desktop, GtkActionGroup* mainActions, GObject* holder); @@ -174,6 +175,7 @@ static struct { { "SPTweakContext", "tweak_tool", SP_VERB_CONTEXT_TWEAK, SP_VERB_CONTEXT_TWEAK_PREFS }, { "SPSprayContext", "spray_tool", SP_VERB_CONTEXT_SPRAY, SP_VERB_CONTEXT_SPRAY_PREFS }, { "SPZoomContext", "zoom_tool", SP_VERB_CONTEXT_ZOOM, SP_VERB_CONTEXT_ZOOM_PREFS }, + { "SPMeasureContext", "measure_tool", SP_VERB_CONTEXT_MEASURE, SP_VERB_CONTEXT_MEASURE_PREFS }, { "SPRectContext", "rect_tool", SP_VERB_CONTEXT_RECT, SP_VERB_CONTEXT_RECT_PREFS }, { "Box3DContext", "3dbox_tool", SP_VERB_CONTEXT_3DBOX, SP_VERB_CONTEXT_3DBOX_PREFS }, { "SPArcContext", "arc_tool", SP_VERB_CONTEXT_ARC, SP_VERB_CONTEXT_ARC_PREFS }, @@ -212,6 +214,8 @@ static struct { SP_VERB_INVALID, 0, 0}, { "SPZoomContext", "zoom_toolbox", 0, sp_zoom_toolbox_prep, "ZoomToolbar", SP_VERB_INVALID, 0, 0}, + { "SPMeasureContext", "measure_toolbox", 0, sp_measure_toolbox_prep, "MeasureToolbar", + SP_VERB_INVALID, 0, 0}, { "SPStarContext", "star_toolbox", 0, sp_star_toolbox_prep, "StarToolbar", SP_VERB_CONTEXT_STAR_PREFS, "/tools/shapes/star", N_("Style of new stars")}, { "SPRectContext", "rect_toolbox", 0, sp_rect_toolbox_prep, "RectToolbar", @@ -361,6 +365,9 @@ static gchar const * ui_descr = " <toolitem action='ZoomNext' />" " </toolbar>" + " <toolbar name='MeasureToolbar'>" + " </toolbar>" + " <toolbar name='StarToolbar'>" " <separator />" " <toolitem action='StarStateAction' />" @@ -932,7 +939,7 @@ static Glib::RefPtr<Gtk::ActionGroup> create_or_fetch_actions( SPDesktop* deskto SP_VERB_ZOOM_PAGE, SP_VERB_ZOOM_PAGE_WIDTH, SP_VERB_ZOOM_PREV, - SP_VERB_ZOOM_SELECTION, + SP_VERB_ZOOM_SELECTION }; Inkscape::IconSize toolboxSize = ToolboxFactory::prefToSize("/toolbox/small"); @@ -1627,6 +1634,11 @@ static void sp_zoom_toolbox_prep(SPDesktop * /*desktop*/, GtkActionGroup* /*main // no custom GtkAction setup needed } // end of sp_zoom_toolbox_prep() +static void sp_measure_toolbox_prep(SPDesktop * /*desktop*/, GtkActionGroup* /*mainActions*/, GObject* /*holder*/) +{ + // no custom GtkAction setup needed +} // end of sp_measure_toolbox_prep() + void ToolboxFactory::setToolboxDesktop(GtkWidget *toolbox, SPDesktop *desktop) { sigc::connection *conn = static_cast<sigc::connection*>(g_object_get_data(G_OBJECT(toolbox), @@ -1838,6 +1850,7 @@ void setup_tool_toolbox(GtkWidget *toolbox, SPDesktop *desktop) " <toolitem action='ToolNode' />" " <toolitem action='ToolTweak' />" " <toolitem action='ToolZoom' />" + " <toolitem action='ToolMeasure' />" " <!-- Shapes -->" " <toolitem action='ToolRect' />" |
