ed243c1b2de30d5690c4f1ab3b5952fde1ff4e15
1 /*
2 * LPEToolContext: a context for a generic tool composed of subtools that are given by LPEs
3 *
4 * Authors:
5 * Maximilian Albert <maximilian.albert@gmail.com>
6 * Lauris Kaplinski <lauris@kaplinski.com>
7 *
8 * Copyright (C) 1998 The Free Software Foundation
9 * Copyright (C) 1999-2005 authors
10 * Copyright (C) 2001-2002 Ximian, Inc.
11 * Copyright (C) 2008 Maximilian Albert
12 *
13 * Released under GNU GPL, read the file 'COPYING' for more information
14 */
16 #ifdef HAVE_CONFIG_H
17 #include "config.h"
18 #endif
20 #include <2geom/sbasis-geometric.h>
21 #include <gdk/gdkkeysyms.h>
22 //#include <gtk/gtkobject.h>
24 #include "macros.h"
25 #include "forward.h"
26 #include "pixmaps/cursor-node.xpm"
27 #include "pixmaps/cursor-crosshairs.xpm"
28 #include <gtk/gtk.h>
29 #include "desktop.h"
30 #include "message-context.h"
31 #include "prefs-utils.h"
32 #include "shape-editor.h"
33 #include "selection.h"
34 #include "desktop-handles.h"
35 #include "document.h"
36 #include "display/curve.h"
37 #include "display/canvas-bpath.h"
38 #include "message-stack.h"
39 #include "sp-path.h"
40 #include "helper/units.h"
42 #include "lpe-tool-context.h"
44 static void sp_lpetool_context_class_init(SPLPEToolContextClass *klass);
45 static void sp_lpetool_context_init(SPLPEToolContext *erc);
46 static void sp_lpetool_context_dispose(GObject *object);
48 static void sp_lpetool_context_setup(SPEventContext *ec);
49 static void sp_lpetool_context_set(SPEventContext *ec, gchar const *key, gchar const *val);
50 static gint sp_lpetool_context_root_handler(SPEventContext *ec, GdkEvent *event);
52 void sp_lpetool_context_selection_changed(Inkscape::Selection *selection, gpointer data);
54 const int num_subtools = 8;
56 Inkscape::LivePathEffect::EffectType lpesubtools[] = {
57 Inkscape::LivePathEffect::INVALID_LPE, // this must be here to account for the "all inactive" action
58 Inkscape::LivePathEffect::LINE_SEGMENT,
59 Inkscape::LivePathEffect::CIRCLE_3PTS,
60 Inkscape::LivePathEffect::CIRCLE_WITH_RADIUS,
61 Inkscape::LivePathEffect::PARALLEL,
62 Inkscape::LivePathEffect::PERP_BISECTOR,
63 Inkscape::LivePathEffect::ANGLE_BISECTOR,
64 Inkscape::LivePathEffect::MIRROR_SYMMETRY,
65 };
67 static SPPenContextClass *lpetool_parent_class = 0;
69 GType sp_lpetool_context_get_type(void)
70 {
71 static GType type = 0;
72 if (!type) {
73 GTypeInfo info = {
74 sizeof(SPLPEToolContextClass),
75 0, // base_init
76 0, // base_finalize
77 (GClassInitFunc)sp_lpetool_context_class_init,
78 0, // class_finalize
79 0, // class_data
80 sizeof(SPLPEToolContext),
81 0, // n_preallocs
82 (GInstanceInitFunc)sp_lpetool_context_init,
83 0 // value_table
84 };
85 type = g_type_register_static(SP_TYPE_PEN_CONTEXT, "SPLPEToolContext", &info, static_cast<GTypeFlags>(0));
86 }
87 return type;
88 }
90 static void
91 sp_lpetool_context_class_init(SPLPEToolContextClass *klass)
92 {
93 GObjectClass *object_class = (GObjectClass *) klass;
94 SPEventContextClass *event_context_class = (SPEventContextClass *) klass;
96 lpetool_parent_class = (SPPenContextClass*)g_type_class_peek_parent(klass);
98 object_class->dispose = sp_lpetool_context_dispose;
100 event_context_class->setup = sp_lpetool_context_setup;
101 event_context_class->set = sp_lpetool_context_set;
102 event_context_class->root_handler = sp_lpetool_context_root_handler;
103 }
105 static void
106 sp_lpetool_context_init(SPLPEToolContext *lc)
107 {
108 lc->cursor_shape = cursor_crosshairs_xpm;
109 lc->hot_x = 7;
110 lc->hot_y = 7;
112 lc->canvas_bbox = NULL;
113 lc->measuring_items = new std::map<SPPath *, SPCanvasItem*>;
115 new (&lc->sel_changed_connection) sigc::connection();
116 }
118 static void
119 sp_lpetool_context_dispose(GObject *object)
120 {
121 SPLPEToolContext *lc = SP_LPETOOL_CONTEXT(object);
122 delete lc->shape_editor;
124 if (lc->canvas_bbox) {
125 gtk_object_destroy(GTK_OBJECT(lc->canvas_bbox));
126 lc->canvas_bbox = NULL;
127 }
129 lpetool_delete_measuring_items(lc);
130 delete lc->measuring_items;
131 lc->measuring_items = NULL;
133 lc->sel_changed_connection.disconnect();
134 lc->sel_changed_connection.~connection();
136 G_OBJECT_CLASS(lpetool_parent_class)->dispose(object);
137 }
139 static void
140 sp_lpetool_context_setup(SPEventContext *ec)
141 {
142 SPLPEToolContext *lc = SP_LPETOOL_CONTEXT(ec);
144 if (((SPEventContextClass *) lpetool_parent_class)->setup)
145 ((SPEventContextClass *) lpetool_parent_class)->setup(ec);
147 Inkscape::Selection *selection = sp_desktop_selection (ec->desktop);
148 SPItem *item = selection->singleItem();
150 lc->sel_changed_connection.disconnect();
151 lc->sel_changed_connection =
152 selection->connectChanged(sigc::bind(sigc::ptr_fun(&sp_lpetool_context_selection_changed), (gpointer)lc));
154 lc->shape_editor = new ShapeEditor(ec->desktop);
156 lpetool_context_switch_mode(lc, Inkscape::LivePathEffect::INVALID_LPE);
157 lpetool_context_reset_limiting_bbox(lc);
158 lpetool_create_measuring_items(lc);
160 // TODO temp force:
161 ec->enableSelectionCue();
163 if (item) {
164 lc->shape_editor->set_item(item, SH_NODEPATH);
165 lc->shape_editor->set_item(item, SH_KNOTHOLDER);
166 }
168 if (prefs_get_int_attribute("tools.lpetool", "selcue", 0) != 0) {
169 ec->enableSelectionCue();
170 }
172 lc->_message_context = new Inkscape::MessageContext((ec->desktop)->messageStack());
174 lc->shape_editor->update_statusbar();
175 }
177 /**
178 \brief Callback that processes the "changed" signal on the selection;
179 destroys old and creates new nodepath and reassigns listeners to the new selected item's repr
180 */
181 void
182 sp_lpetool_context_selection_changed(Inkscape::Selection *selection, gpointer data)
183 {
184 SPLPEToolContext *lc = SP_LPETOOL_CONTEXT(data);
186 // TODO: update ShapeEditorsCollective instead
187 lc->shape_editor->unset_item(SH_NODEPATH);
188 lc->shape_editor->unset_item(SH_KNOTHOLDER);
189 SPItem *item = selection->singleItem();
190 lc->shape_editor->set_item(item, SH_NODEPATH);
191 lc->shape_editor->set_item(item, SH_KNOTHOLDER);
192 lc->shape_editor->update_statusbar();
193 }
195 static void
196 sp_lpetool_context_set(SPEventContext *ec, gchar const *key, gchar const *val)
197 {
198 // FIXME: how to set this correcly? the value from preferences-skeleton.h doesn't seem to get
199 // read (it wants to set drag = 1)
200 lpetool_parent_class->set(ec, key, "drag");
202 /**
203 //pass on up to parent class to handle common attributes.
204 if ( lpetool_parent_class->set ) {
205 lpetool_parent_class->set(ec, key, val);
206 }
207 **/
208 }
210 gint
211 sp_lpetool_context_root_handler(SPEventContext *event_context, GdkEvent *event)
212 {
213 SPLPEToolContext *lc = SP_LPETOOL_CONTEXT(event_context);
214 SPDesktop *desktop = event_context->desktop;
215 Inkscape::Selection *selection = sp_desktop_selection (desktop);
217 bool ret = false;
219 if (sp_pen_context_has_waiting_LPE(lc)) {
220 // quit when we are waiting for a LPE to be applied
221 ret = ((SPEventContextClass *) lpetool_parent_class)->root_handler(event_context, event);
222 return ret;
223 }
225 switch (event->type) {
226 case GDK_BUTTON_PRESS:
227 if (lc->mode == Inkscape::LivePathEffect::INVALID_LPE) {
228 // don't do anything for now if we are inactive
229 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Choose a construction tool from the toolbar."));
230 ret = true;
231 break;
232 }
234 if (event->button.button == 1 && !event_context->space_panning) {
235 // save drag origin
236 event_context->xp = (gint) event->button.x;
237 event_context->yp = (gint) event->button.y;
238 event_context->within_tolerance = true;
239 lc->shape_editor->cancel_hit();
241 using namespace Inkscape::LivePathEffect;
243 int mode = prefs_get_int_attribute("tools.lpetool", "mode", 0);
244 EffectType type = lpesubtools[mode];
246 //bool over_stroke = lc->shape_editor->is_over_stroke(NR::Point(event->button.x, event->button.y), true);
248 sp_pen_context_wait_for_LPE_mouse_clicks(lc, type, Inkscape::LivePathEffect::Effect::acceptsNumClicks(type));
250 // we pass the mouse click on to pen tool as the first click which it should collect
251 ret = ((SPEventContextClass *) lpetool_parent_class)->root_handler(event_context, event);
252 }
253 break;
254 case GDK_MOTION_NOTIFY:
255 {
256 if (!lc->shape_editor->has_nodepath() || selection->singleItem() == NULL) {
257 break;
258 }
260 bool over_stroke = false;
261 over_stroke = lc->shape_editor->is_over_stroke(NR::Point(event->motion.x, event->motion.y), false);
263 if (over_stroke) {
264 event_context->cursor_shape = cursor_node_xpm;
265 event_context->hot_x = 1;
266 event_context->hot_y = 1;
267 sp_event_context_update_cursor(event_context);
268 } else {
269 lc->cursor_shape = cursor_crosshairs_xpm;
270 lc->hot_x = 7;
271 lc->hot_y = 7;
272 sp_event_context_update_cursor(event_context);
273 }
274 }
275 break;
278 case GDK_BUTTON_RELEASE:
279 {
280 /**
281 break;
282 **/
283 }
285 case GDK_KEY_PRESS:
286 /**
287 switch (get_group0_keyval (&event->key)) {
288 }
289 break;
290 **/
292 case GDK_KEY_RELEASE:
293 /**
294 switch (get_group0_keyval(&event->key)) {
295 case GDK_Control_L:
296 case GDK_Control_R:
297 dc->_message_context->clear();
298 break;
299 default:
300 break;
301 }
302 **/
304 default:
305 break;
306 }
308 if (!ret) {
309 if (((SPEventContextClass *) lpetool_parent_class)->root_handler) {
310 ret = ((SPEventContextClass *) lpetool_parent_class)->root_handler(event_context, event);
311 }
312 }
314 return ret;
315 }
317 /*
318 * Finds the index in the list of geometric subtools corresponding to the given LPE type.
319 * Returns -1 if no subtool is found.
320 */
321 int
322 lpetool_mode_to_index(Inkscape::LivePathEffect::EffectType const type) {
323 for (int i = 0; i < num_subtools; ++i) {
324 if (lpesubtools[i] == type) {
325 return i;
326 }
327 }
328 return -1;
329 }
331 /*
332 * Checks whether an item has a construction applied as LPE and if so returns the index in
333 * lpesubtools of this construction
334 */
335 int lpetool_item_has_construction(SPLPEToolContext *lc, SPItem *item)
336 {
337 if (!SP_IS_LPE_ITEM(item)) {
338 return -1;
339 }
341 Inkscape::LivePathEffect::Effect* lpe = sp_lpe_item_get_current_lpe(SP_LPE_ITEM(item));
342 if (!lpe) {
343 return -1;
344 }
345 return lpetool_mode_to_index(lpe->effectType());
346 }
348 /*
349 * Attempts to perform the construction of the given type (i.e., to apply the corresponding LPE) to
350 * a single selected item. Returns whether we succeeded.
351 */
352 bool
353 lpetool_try_construction(SPLPEToolContext *lc, Inkscape::LivePathEffect::EffectType const type)
354 {
355 Inkscape::Selection *selection = sp_desktop_selection(lc->desktop);
356 SPItem *item = selection->singleItem();
358 // TODO: should we check whether type represents a valid geometric construction?
359 if (item && SP_IS_LPE_ITEM(item) && Inkscape::LivePathEffect::Effect::acceptsNumClicks(type) == 0) {
360 Inkscape::LivePathEffect::Effect::createAndApply(type, sp_desktop_document(lc->desktop), item);
361 return true;
362 }
363 return false;
364 }
366 void
367 lpetool_context_switch_mode(SPLPEToolContext *lc, Inkscape::LivePathEffect::EffectType const type)
368 {
369 int index = lpetool_mode_to_index(type);
370 if (index != -1) {
371 lc->mode = type;
372 lc->desktop->setToolboxSelectOneValue ("lpetool_mode_action", index);
373 } else {
374 g_warning ("Invalid mode selected: %d", type);
375 return;
376 }
377 }
379 void
380 lpetool_get_limiting_bbox_corners(SPDocument *document, Geom::Point &A, Geom::Point &B) {
381 Geom::Coord w = sp_document_width(document);
382 Geom::Coord h = sp_document_height(document);
384 double ulx = prefs_get_double_attribute ("tools.lpetool", "bbox_upperleftx", 0);
385 double uly = prefs_get_double_attribute ("tools.lpetool", "bbox_upperlefty", 0);
386 double lrx = prefs_get_double_attribute ("tools.lpetool", "bbox_lowerrightx", w);
387 double lry = prefs_get_double_attribute ("tools.lpetool", "bbox_lowerrighty", h);
389 A = Geom::Point(ulx, uly);
390 B = Geom::Point(lrx, lry);
391 }
393 /*
394 * Reads the limiting bounding box from preferences and draws it on the screen
395 */
396 // TODO: Note that currently the bbox is not user-settable; we simply use the page borders
397 void
398 lpetool_context_reset_limiting_bbox(SPLPEToolContext *lc)
399 {
400 if (lc->canvas_bbox) {
401 gtk_object_destroy(GTK_OBJECT(lc->canvas_bbox));
402 lc->canvas_bbox = NULL;
403 }
405 if (prefs_get_int_attribute("tools.lpetool", "show_bbox", 1) == 0)
406 return;
408 SPDocument *document = sp_desktop_document(lc->desktop);
410 Geom::Point A, B;
411 lpetool_get_limiting_bbox_corners(document, A, B);
412 NR::Matrix doc2dt(lc->desktop->doc2dt());
413 A *= doc2dt;
414 B *= doc2dt;
416 Geom::Rect rect(A, B);
417 SPCurve *curve = SPCurve::new_from_rect(rect);
419 lc->canvas_bbox = sp_canvas_bpath_new (sp_desktop_controls(lc->desktop), curve);
420 sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(lc->canvas_bbox), 0x0000ffff, 0.8, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT, 5, 5);
421 }
423 static void
424 set_pos_and_anchor(SPCanvasText *canvas_text, const Geom::Piecewise<Geom::D2<Geom::SBasis> > &pwd2,
425 const double t, const double length, bool use_curvature = false)
426 {
427 using namespace Geom;
429 Piecewise<D2<SBasis> > pwd2_reparam = arc_length_parametrization(pwd2, 2 , 0.1);
430 double t_reparam = pwd2_reparam.cuts.back() * t;
431 Point pos = pwd2_reparam.valueAt(t_reparam);
432 Point dir = unit_vector(derivative(pwd2_reparam).valueAt(t_reparam));
433 Point n = -rot90(dir);
434 double angle = Geom::angle_between(dir, Point(1,0));
436 sp_canvastext_set_coords(canvas_text, pos + n * length);
437 sp_canvastext_set_anchor(canvas_text, std::sin(angle), -std::cos(angle));
438 }
440 void
441 lpetool_create_measuring_items(SPLPEToolContext *lc, Inkscape::Selection *selection)
442 {
443 bool show = prefs_get_int_attribute ("tools.lpetool", "show_measuring_info", 1) == 1 ? true : false;
444 if (!selection) {
445 selection = sp_desktop_selection(lc->desktop);
446 }
448 SPPath *path;
449 SPCurve *curve;
450 SPCanvasText *canvas_text;
451 SPCanvasGroup *tmpgrp = sp_desktop_tempgroup(lc->desktop);
452 gchar *arc_length;
453 double lengthval;
455 for (GSList const *i = selection->itemList(); i != NULL; i = i->next) {
456 if (SP_IS_PATH(i->data)) {
457 path = SP_PATH(i->data);
458 curve = sp_shape_get_curve(SP_SHAPE(path));
459 Geom::Piecewise<Geom::D2<Geom::SBasis> > pwd2 = paths_to_pw(curve->get_pathvector());
460 canvas_text = (SPCanvasText *) sp_canvastext_new(tmpgrp, lc->desktop, Geom::Point(0,0), "");
461 if (!show)
462 sp_canvas_item_hide(SP_CANVAS_ITEM(canvas_text));
464 SPUnitId unitid = static_cast<SPUnitId>(prefs_get_int_attribute("tools.lpetool", "unitid", SP_UNIT_PX));
465 SPUnit unit = sp_unit_get_by_id(unitid);
467 lengthval = Geom::length(pwd2);
468 gboolean success;
469 success = sp_convert_distance(&lengthval, &sp_unit_get_by_id(SP_UNIT_PX), &unit);
470 arc_length = g_strdup_printf("%.2f %s", lengthval, success ? sp_unit_get_abbreviation(&unit) : "px");
471 sp_canvastext_set_text (canvas_text, arc_length);
472 set_pos_and_anchor(canvas_text, pwd2, 0.5, 10);
473 // TODO: must we free arc_length?
474 (*lc->measuring_items)[path] = SP_CANVAS_ITEM(canvas_text);
475 }
476 }
477 }
479 void
480 lpetool_delete_measuring_items(SPLPEToolContext *lc)
481 {
482 std::map<SPPath *, SPCanvasItem*>::iterator i;
483 for (i = lc->measuring_items->begin(); i != lc->measuring_items->end(); ++i) {
484 gtk_object_destroy(GTK_OBJECT(i->second));
485 }
486 lc->measuring_items->clear();
487 }
489 void
490 lpetool_update_measuring_items(SPLPEToolContext *lc)
491 {
492 SPPath *path;
493 SPCurve *curve;
494 double lengthval;
495 gchar *arc_length;
496 std::map<SPPath *, SPCanvasItem*>::iterator i;
497 for (i = lc->measuring_items->begin(); i != lc->measuring_items->end(); ++i) {
498 path = i->first;
499 curve = sp_shape_get_curve(SP_SHAPE(path));
500 Geom::Piecewise<Geom::D2<Geom::SBasis> > pwd2 = Geom::paths_to_pw(curve->get_pathvector());
501 SPUnitId unitid = static_cast<SPUnitId>(prefs_get_int_attribute("tools.lpetool", "unitid", SP_UNIT_PX));
502 SPUnit unit = sp_unit_get_by_id(unitid);
503 lengthval = Geom::length(pwd2);
504 gboolean success;
505 success = sp_convert_distance(&lengthval, &sp_unit_get_by_id(SP_UNIT_PX), &unit);
506 arc_length = g_strdup_printf("%.2f %s", lengthval, success ? sp_unit_get_abbreviation(&unit) : "px");
507 sp_canvastext_set_text (SP_CANVASTEXT(i->second), arc_length);
508 set_pos_and_anchor(SP_CANVASTEXT(i->second), pwd2, 0.5, 10);
509 // TODO: must we free arc_length?
510 }
511 }
513 void
514 lpetool_show_measuring_info(SPLPEToolContext *lc, bool show)
515 {
516 std::map<SPPath *, SPCanvasItem*>::iterator i;
517 for (i = lc->measuring_items->begin(); i != lc->measuring_items->end(); ++i) {
518 if (show) {
519 sp_canvas_item_show(i->second);
520 } else {
521 sp_canvas_item_hide(i->second);
522 }
523 }
524 }
526 /*
527 Local Variables:
528 mode:c++
529 c-file-style:"stroustrup"
530 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
531 indent-tabs-mode:nil
532 fill-column:99
533 End:
534 */
535 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :