30248edd0bc148703fa91badda8f82ea6e65f8d0
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>
23 #include "macros.h"
24 #include "forward.h"
25 #include "pixmaps/cursor-node.xpm"
26 #include "pixmaps/cursor-crosshairs.xpm"
27 #include <gtk/gtk.h>
28 #include "desktop.h"
29 #include "message-context.h"
30 #include "preferences.h"
31 #include "shape-editor.h"
32 #include "selection.h"
33 #include "desktop-handles.h"
34 #include "document.h"
35 #include "display/curve.h"
36 #include "display/canvas-bpath.h"
37 #include "message-stack.h"
38 #include "sp-path.h"
39 #include "helper/units.h"
41 #include "lpe-tool-context.h"
43 static void sp_lpetool_context_class_init(SPLPEToolContextClass *klass);
44 static void sp_lpetool_context_init(SPLPEToolContext *erc);
45 static void sp_lpetool_context_dispose(GObject *object);
47 static void sp_lpetool_context_setup(SPEventContext *ec);
48 static void sp_lpetool_context_set(SPEventContext *ec, Inkscape::Preferences::Entry *);
49 static gint sp_lpetool_context_item_handler(SPEventContext *ec, SPItem *item, GdkEvent *event);
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 event_context_class->item_handler = sp_lpetool_context_item_handler;
104 }
106 static void
107 sp_lpetool_context_init(SPLPEToolContext *lc)
108 {
109 lc->cursor_shape = cursor_crosshairs_xpm;
110 lc->hot_x = 7;
111 lc->hot_y = 7;
113 lc->canvas_bbox = NULL;
114 lc->measuring_items = new std::map<SPPath *, SPCanvasItem*>;
116 new (&lc->sel_changed_connection) sigc::connection();
117 }
119 static void
120 sp_lpetool_context_dispose(GObject *object)
121 {
122 SPLPEToolContext *lc = SP_LPETOOL_CONTEXT(object);
123 delete lc->shape_editor;
125 if (lc->canvas_bbox) {
126 gtk_object_destroy(GTK_OBJECT(lc->canvas_bbox));
127 lc->canvas_bbox = NULL;
128 }
130 lpetool_delete_measuring_items(lc);
131 delete lc->measuring_items;
132 lc->measuring_items = NULL;
134 lc->sel_changed_connection.disconnect();
135 lc->sel_changed_connection.~connection();
137 if (lc->_lpetool_message_context) {
138 delete lc->_lpetool_message_context;
139 }
141 G_OBJECT_CLASS(lpetool_parent_class)->dispose(object);
142 }
144 static void
145 sp_lpetool_context_setup(SPEventContext *ec)
146 {
147 SPLPEToolContext *lc = SP_LPETOOL_CONTEXT(ec);
149 if (((SPEventContextClass *) lpetool_parent_class)->setup)
150 ((SPEventContextClass *) lpetool_parent_class)->setup(ec);
152 Inkscape::Selection *selection = sp_desktop_selection (ec->desktop);
153 SPItem *item = selection->singleItem();
155 lc->sel_changed_connection.disconnect();
156 lc->sel_changed_connection =
157 selection->connectChanged(sigc::bind(sigc::ptr_fun(&sp_lpetool_context_selection_changed), (gpointer)lc));
159 lc->shape_editor = new ShapeEditor(ec->desktop);
161 lpetool_context_switch_mode(lc, Inkscape::LivePathEffect::INVALID_LPE);
162 lpetool_context_reset_limiting_bbox(lc);
163 lpetool_create_measuring_items(lc);
165 // TODO temp force:
166 ec->enableSelectionCue();
168 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
170 if (item) {
171 lc->shape_editor->set_item(item, SH_NODEPATH);
172 lc->shape_editor->set_item(item, SH_KNOTHOLDER);
173 }
175 if (prefs->getBool("/tools/lpetool/selcue")) {
176 ec->enableSelectionCue();
177 }
179 lc->_lpetool_message_context = new Inkscape::MessageContext((ec->desktop)->messageStack());
182 lc->shape_editor->update_statusbar();
183 }
185 /**
186 \brief Callback that processes the "changed" signal on the selection;
187 destroys old and creates new nodepath and reassigns listeners to the new selected item's repr
188 */
189 void
190 sp_lpetool_context_selection_changed(Inkscape::Selection *selection, gpointer data)
191 {
192 SPLPEToolContext *lc = SP_LPETOOL_CONTEXT(data);
194 // TODO: update ShapeEditorsCollective instead
195 lc->shape_editor->unset_item(SH_NODEPATH);
196 lc->shape_editor->unset_item(SH_KNOTHOLDER);
197 SPItem *item = selection->singleItem();
198 lc->shape_editor->set_item(item, SH_NODEPATH);
199 lc->shape_editor->set_item(item, SH_KNOTHOLDER);
200 lc->shape_editor->update_statusbar();
201 }
203 static void
204 sp_lpetool_context_set(SPEventContext *ec, Inkscape::Preferences::Entry *val)
205 {
206 if (val->getEntryName() == "mode") {
207 Inkscape::Preferences::get()->setString("/tools/geometric/mode", "drag");
208 SP_PEN_CONTEXT(ec)->mode = SP_PEN_CONTEXT_MODE_DRAG;
209 }
211 /*
212 //pass on up to parent class to handle common attributes.
213 if ( lpetool_parent_class->set ) {
214 lpetool_parent_class->set(ec, key, val);
215 }
216 */
217 }
219 static gint
220 sp_lpetool_context_item_handler(SPEventContext *ec, SPItem *item, GdkEvent *event)
221 {
222 gint ret = FALSE;
224 switch (event->type) {
225 case GDK_BUTTON_PRESS:
226 {
227 // select the clicked item but do nothing else
228 Inkscape::Selection * const selection = sp_desktop_selection(ec->desktop);
229 selection->clear();
230 selection->add(item);
231 ret = TRUE;
232 break;
233 }
234 case GDK_BUTTON_RELEASE:
235 // TODO: do we need to catch this or can we pass it on to the parent handler?
236 ret = TRUE;
237 break;
238 default:
239 break;
240 }
242 if (!ret) {
243 if (((SPEventContextClass *) lpetool_parent_class)->item_handler)
244 ret = ((SPEventContextClass *) lpetool_parent_class)->item_handler(ec, item, event);
245 }
247 return ret;
248 }
250 gint
251 sp_lpetool_context_root_handler(SPEventContext *event_context, GdkEvent *event)
252 {
253 SPLPEToolContext *lc = SP_LPETOOL_CONTEXT(event_context);
254 SPDesktop *desktop = event_context->desktop;
255 Inkscape::Selection *selection = sp_desktop_selection (desktop);
257 bool ret = false;
259 if (sp_pen_context_has_waiting_LPE(lc)) {
260 // quit when we are waiting for a LPE to be applied
261 ret = ((SPEventContextClass *) lpetool_parent_class)->root_handler(event_context, event);
262 return ret;
263 }
265 switch (event->type) {
266 case GDK_BUTTON_PRESS:
267 if (event->button.button == 1 && !event_context->space_panning) {
268 if (lc->mode == Inkscape::LivePathEffect::INVALID_LPE) {
269 // don't do anything for now if we are inactive (except clearing the selection
270 // since this was a click into empty space)
271 selection->clear();
272 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Choose a construction tool from the toolbar."));
273 ret = true;
274 break;
275 }
277 // save drag origin
278 event_context->xp = (gint) event->button.x;
279 event_context->yp = (gint) event->button.y;
280 event_context->within_tolerance = true;
281 lc->shape_editor->cancel_hit();
283 using namespace Inkscape::LivePathEffect;
285 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
286 int mode = prefs->getInt("/tools/lpetool/mode");
287 EffectType type = lpesubtools[mode];
289 //bool over_stroke = lc->shape_editor->is_over_stroke(Geom::Point(event->button.x, event->button.y), true);
291 sp_pen_context_wait_for_LPE_mouse_clicks(lc, type, Inkscape::LivePathEffect::Effect::acceptsNumClicks(type));
293 // we pass the mouse click on to pen tool as the first click which it should collect
294 ret = ((SPEventContextClass *) lpetool_parent_class)->root_handler(event_context, event);
295 }
296 break;
297 case GDK_MOTION_NOTIFY:
298 {
299 if (!lc->shape_editor->has_nodepath() || selection->singleItem() == NULL) {
300 break;
301 }
303 bool over_stroke = false;
304 over_stroke = lc->shape_editor->is_over_stroke(Geom::Point(event->motion.x, event->motion.y), false);
306 if (over_stroke) {
307 event_context->cursor_shape = cursor_node_xpm;
308 event_context->hot_x = 1;
309 event_context->hot_y = 1;
310 sp_event_context_update_cursor(event_context);
311 } else {
312 lc->cursor_shape = cursor_crosshairs_xpm;
313 lc->hot_x = 7;
314 lc->hot_y = 7;
315 sp_event_context_update_cursor(event_context);
316 }
317 }
318 break;
321 case GDK_BUTTON_RELEASE:
322 {
323 /**
324 break;
325 **/
326 }
328 case GDK_KEY_PRESS:
329 /**
330 switch (get_group0_keyval (&event->key)) {
331 }
332 break;
333 **/
335 case GDK_KEY_RELEASE:
336 /**
337 switch (get_group0_keyval(&event->key)) {
338 case GDK_Control_L:
339 case GDK_Control_R:
340 dc->_message_context->clear();
341 break;
342 default:
343 break;
344 }
345 **/
347 default:
348 break;
349 }
351 if (!ret) {
352 if (((SPEventContextClass *) lpetool_parent_class)->root_handler) {
353 ret = ((SPEventContextClass *) lpetool_parent_class)->root_handler(event_context, event);
354 }
355 }
357 return ret;
358 }
360 /*
361 * Finds the index in the list of geometric subtools corresponding to the given LPE type.
362 * Returns -1 if no subtool is found.
363 */
364 int
365 lpetool_mode_to_index(Inkscape::LivePathEffect::EffectType const type) {
366 for (int i = 0; i < num_subtools; ++i) {
367 if (lpesubtools[i] == type) {
368 return i;
369 }
370 }
371 return -1;
372 }
374 /*
375 * Checks whether an item has a construction applied as LPE and if so returns the index in
376 * lpesubtools of this construction
377 */
378 int lpetool_item_has_construction(SPLPEToolContext */*lc*/, SPItem *item)
379 {
380 if (!SP_IS_LPE_ITEM(item)) {
381 return -1;
382 }
384 Inkscape::LivePathEffect::Effect* lpe = sp_lpe_item_get_current_lpe(SP_LPE_ITEM(item));
385 if (!lpe) {
386 return -1;
387 }
388 return lpetool_mode_to_index(lpe->effectType());
389 }
391 /*
392 * Attempts to perform the construction of the given type (i.e., to apply the corresponding LPE) to
393 * a single selected item. Returns whether we succeeded.
394 */
395 bool
396 lpetool_try_construction(SPLPEToolContext *lc, Inkscape::LivePathEffect::EffectType const type)
397 {
398 Inkscape::Selection *selection = sp_desktop_selection(lc->desktop);
399 SPItem *item = selection->singleItem();
401 // TODO: should we check whether type represents a valid geometric construction?
402 if (item && SP_IS_LPE_ITEM(item) && Inkscape::LivePathEffect::Effect::acceptsNumClicks(type) == 0) {
403 Inkscape::LivePathEffect::Effect::createAndApply(type, sp_desktop_document(lc->desktop), item);
404 return true;
405 }
406 return false;
407 }
409 void
410 lpetool_context_switch_mode(SPLPEToolContext *lc, Inkscape::LivePathEffect::EffectType const type)
411 {
412 int index = lpetool_mode_to_index(type);
413 if (index != -1) {
414 lc->mode = type;
415 lc->desktop->setToolboxSelectOneValue ("lpetool_mode_action", index);
416 } else {
417 g_warning ("Invalid mode selected: %d", type);
418 return;
419 }
420 }
422 void
423 lpetool_get_limiting_bbox_corners(SPDocument *document, Geom::Point &A, Geom::Point &B) {
424 Geom::Coord w = sp_document_width(document);
425 Geom::Coord h = sp_document_height(document);
426 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
428 double ulx = prefs->getDouble("/tools/lpetool/bbox_upperleftx", 0);
429 double uly = prefs->getDouble("/tools/lpetool/bbox_upperlefty", 0);
430 double lrx = prefs->getDouble("/tools/lpetool/bbox_lowerrightx", w);
431 double lry = prefs->getDouble("/tools/lpetool/bbox_lowerrighty", h);
433 A = Geom::Point(ulx, uly);
434 B = Geom::Point(lrx, lry);
435 }
437 /*
438 * Reads the limiting bounding box from preferences and draws it on the screen
439 */
440 // TODO: Note that currently the bbox is not user-settable; we simply use the page borders
441 void
442 lpetool_context_reset_limiting_bbox(SPLPEToolContext *lc)
443 {
444 if (lc->canvas_bbox) {
445 gtk_object_destroy(GTK_OBJECT(lc->canvas_bbox));
446 lc->canvas_bbox = NULL;
447 }
449 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
450 if (!prefs->getBool("/tools/lpetool/show_bbox", true))
451 return;
453 SPDocument *document = sp_desktop_document(lc->desktop);
455 Geom::Point A, B;
456 lpetool_get_limiting_bbox_corners(document, A, B);
457 Geom::Matrix doc2dt(lc->desktop->doc2dt());
458 A *= doc2dt;
459 B *= doc2dt;
461 Geom::Rect rect(A, B);
462 SPCurve *curve = SPCurve::new_from_rect(rect);
464 lc->canvas_bbox = sp_canvas_bpath_new (sp_desktop_controls(lc->desktop), curve);
465 sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(lc->canvas_bbox), 0x0000ffff, 0.8, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT, 5, 5);
466 }
468 static void
469 set_pos_and_anchor(SPCanvasText *canvas_text, const Geom::Piecewise<Geom::D2<Geom::SBasis> > &pwd2,
470 const double t, const double length, bool /*use_curvature*/ = false)
471 {
472 using namespace Geom;
474 Piecewise<D2<SBasis> > pwd2_reparam = arc_length_parametrization(pwd2, 2 , 0.1);
475 double t_reparam = pwd2_reparam.cuts.back() * t;
476 Point pos = pwd2_reparam.valueAt(t_reparam);
477 Point dir = unit_vector(derivative(pwd2_reparam).valueAt(t_reparam));
478 Point n = -rot90(dir);
479 double angle = Geom::angle_between(dir, Point(1,0));
481 sp_canvastext_set_coords(canvas_text, pos + n * length);
482 sp_canvastext_set_anchor(canvas_text, std::sin(angle), -std::cos(angle));
483 }
485 void
486 lpetool_create_measuring_items(SPLPEToolContext *lc, Inkscape::Selection *selection)
487 {
488 if (!selection) {
489 selection = sp_desktop_selection(lc->desktop);
490 }
491 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
492 bool show = prefs->getBool("/tools/lpetool/show_measuring_info", true);
494 SPPath *path;
495 SPCurve *curve;
496 SPCanvasText *canvas_text;
497 SPCanvasGroup *tmpgrp = sp_desktop_tempgroup(lc->desktop);
498 gchar *arc_length;
499 double lengthval;
501 for (GSList const *i = selection->itemList(); i != NULL; i = i->next) {
502 if (SP_IS_PATH(i->data)) {
503 path = SP_PATH(i->data);
504 curve = sp_shape_get_curve(SP_SHAPE(path));
505 Geom::Piecewise<Geom::D2<Geom::SBasis> > pwd2 = paths_to_pw(curve->get_pathvector());
506 canvas_text = (SPCanvasText *) sp_canvastext_new(tmpgrp, lc->desktop, Geom::Point(0,0), "");
507 if (!show)
508 sp_canvas_item_hide(SP_CANVAS_ITEM(canvas_text));
510 SPUnitId unitid = static_cast<SPUnitId>(prefs->getInt("/tools/lpetool/unitid", SP_UNIT_PX));
511 SPUnit unit = sp_unit_get_by_id(unitid);
513 lengthval = Geom::length(pwd2);
514 gboolean success;
515 success = sp_convert_distance(&lengthval, &sp_unit_get_by_id(SP_UNIT_PX), &unit);
516 arc_length = g_strdup_printf("%.2f %s", lengthval, success ? sp_unit_get_abbreviation(&unit) : "px");
517 sp_canvastext_set_text (canvas_text, arc_length);
518 set_pos_and_anchor(canvas_text, pwd2, 0.5, 10);
519 // TODO: must we free arc_length?
520 (*lc->measuring_items)[path] = SP_CANVAS_ITEM(canvas_text);
521 }
522 }
523 }
525 void
526 lpetool_delete_measuring_items(SPLPEToolContext *lc)
527 {
528 std::map<SPPath *, SPCanvasItem*>::iterator i;
529 for (i = lc->measuring_items->begin(); i != lc->measuring_items->end(); ++i) {
530 gtk_object_destroy(GTK_OBJECT(i->second));
531 }
532 lc->measuring_items->clear();
533 }
535 void
536 lpetool_update_measuring_items(SPLPEToolContext *lc)
537 {
538 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
539 SPPath *path;
540 SPCurve *curve;
541 double lengthval;
542 gchar *arc_length;
543 std::map<SPPath *, SPCanvasItem*>::iterator i;
544 for (i = lc->measuring_items->begin(); i != lc->measuring_items->end(); ++i) {
545 path = i->first;
546 curve = sp_shape_get_curve(SP_SHAPE(path));
547 Geom::Piecewise<Geom::D2<Geom::SBasis> > pwd2 = Geom::paths_to_pw(curve->get_pathvector());
548 SPUnitId unitid = static_cast<SPUnitId>(prefs->getInt("/tools/lpetool/unitid", SP_UNIT_PX));
549 SPUnit unit = sp_unit_get_by_id(unitid);
550 lengthval = Geom::length(pwd2);
551 gboolean success;
552 success = sp_convert_distance(&lengthval, &sp_unit_get_by_id(SP_UNIT_PX), &unit);
553 arc_length = g_strdup_printf("%.2f %s", lengthval, success ? sp_unit_get_abbreviation(&unit) : "px");
554 sp_canvastext_set_text (SP_CANVASTEXT(i->second), arc_length);
555 set_pos_and_anchor(SP_CANVASTEXT(i->second), pwd2, 0.5, 10);
556 // TODO: must we free arc_length?
557 }
558 }
560 void
561 lpetool_show_measuring_info(SPLPEToolContext *lc, bool show)
562 {
563 std::map<SPPath *, SPCanvasItem*>::iterator i;
564 for (i = lc->measuring_items->begin(); i != lc->measuring_items->end(); ++i) {
565 if (show) {
566 sp_canvas_item_show(i->second);
567 } else {
568 sp_canvas_item_hide(i->second);
569 }
570 }
571 }
573 /*
574 Local Variables:
575 mode:c++
576 c-file-style:"stroustrup"
577 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
578 indent-tabs-mode:nil
579 fill-column:99
580 End:
581 */
582 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :