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 "prefs-utils.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, gchar const *key, gchar const *val);
49 static gint sp_lpetool_context_root_handler(SPEventContext *ec, GdkEvent *event);
51 void sp_lpetool_context_selection_changed(Inkscape::Selection *selection, gpointer data);
53 const int num_subtools = 8;
55 Inkscape::LivePathEffect::EffectType lpesubtools[] = {
56 Inkscape::LivePathEffect::INVALID_LPE, // this must be here to account for the "all inactive" action
57 Inkscape::LivePathEffect::LINE_SEGMENT,
58 Inkscape::LivePathEffect::CIRCLE_3PTS,
59 Inkscape::LivePathEffect::CIRCLE_WITH_RADIUS,
60 Inkscape::LivePathEffect::PARALLEL,
61 Inkscape::LivePathEffect::PERP_BISECTOR,
62 Inkscape::LivePathEffect::ANGLE_BISECTOR,
63 Inkscape::LivePathEffect::MIRROR_SYMMETRY,
64 };
66 static SPPenContextClass *lpetool_parent_class = 0;
68 GType sp_lpetool_context_get_type(void)
69 {
70 static GType type = 0;
71 if (!type) {
72 GTypeInfo info = {
73 sizeof(SPLPEToolContextClass),
74 0, // base_init
75 0, // base_finalize
76 (GClassInitFunc)sp_lpetool_context_class_init,
77 0, // class_finalize
78 0, // class_data
79 sizeof(SPLPEToolContext),
80 0, // n_preallocs
81 (GInstanceInitFunc)sp_lpetool_context_init,
82 0 // value_table
83 };
84 type = g_type_register_static(SP_TYPE_PEN_CONTEXT, "SPLPEToolContext", &info, static_cast<GTypeFlags>(0));
85 }
86 return type;
87 }
89 static void
90 sp_lpetool_context_class_init(SPLPEToolContextClass *klass)
91 {
92 GObjectClass *object_class = (GObjectClass *) klass;
93 SPEventContextClass *event_context_class = (SPEventContextClass *) klass;
95 lpetool_parent_class = (SPPenContextClass*)g_type_class_peek_parent(klass);
97 object_class->dispose = sp_lpetool_context_dispose;
99 event_context_class->setup = sp_lpetool_context_setup;
100 event_context_class->set = sp_lpetool_context_set;
101 event_context_class->root_handler = sp_lpetool_context_root_handler;
102 }
104 static void
105 sp_lpetool_context_init(SPLPEToolContext *lc)
106 {
107 lc->cursor_shape = cursor_crosshairs_xpm;
108 lc->hot_x = 7;
109 lc->hot_y = 7;
111 lc->canvas_bbox = NULL;
112 lc->measuring_items = new std::map<SPPath *, SPCanvasItem*>;
114 new (&lc->sel_changed_connection) sigc::connection();
115 }
117 static void
118 sp_lpetool_context_dispose(GObject *object)
119 {
120 SPLPEToolContext *lc = SP_LPETOOL_CONTEXT(object);
121 delete lc->shape_editor;
123 if (lc->canvas_bbox) {
124 gtk_object_destroy(GTK_OBJECT(lc->canvas_bbox));
125 lc->canvas_bbox = NULL;
126 }
128 lpetool_delete_measuring_items(lc);
129 delete lc->measuring_items;
130 lc->measuring_items = NULL;
132 lc->sel_changed_connection.disconnect();
133 lc->sel_changed_connection.~connection();
135 G_OBJECT_CLASS(lpetool_parent_class)->dispose(object);
136 }
138 static void
139 sp_lpetool_context_setup(SPEventContext *ec)
140 {
141 SPLPEToolContext *lc = SP_LPETOOL_CONTEXT(ec);
143 if (((SPEventContextClass *) lpetool_parent_class)->setup)
144 ((SPEventContextClass *) lpetool_parent_class)->setup(ec);
146 Inkscape::Selection *selection = sp_desktop_selection (ec->desktop);
147 SPItem *item = selection->singleItem();
149 lc->sel_changed_connection.disconnect();
150 lc->sel_changed_connection =
151 selection->connectChanged(sigc::bind(sigc::ptr_fun(&sp_lpetool_context_selection_changed), (gpointer)lc));
153 lc->shape_editor = new ShapeEditor(ec->desktop);
155 lpetool_context_switch_mode(lc, Inkscape::LivePathEffect::INVALID_LPE);
156 lpetool_context_reset_limiting_bbox(lc);
157 lpetool_create_measuring_items(lc);
159 // TODO temp force:
160 ec->enableSelectionCue();
162 if (item) {
163 lc->shape_editor->set_item(item, SH_NODEPATH);
164 lc->shape_editor->set_item(item, SH_KNOTHOLDER);
165 }
167 if (prefs_get_int_attribute("tools.lpetool", "selcue", 0) != 0) {
168 ec->enableSelectionCue();
169 }
171 lc->_message_context = new Inkscape::MessageContext((ec->desktop)->messageStack());
173 lc->shape_editor->update_statusbar();
174 }
176 /**
177 \brief Callback that processes the "changed" signal on the selection;
178 destroys old and creates new nodepath and reassigns listeners to the new selected item's repr
179 */
180 void
181 sp_lpetool_context_selection_changed(Inkscape::Selection *selection, gpointer data)
182 {
183 SPLPEToolContext *lc = SP_LPETOOL_CONTEXT(data);
185 // TODO: update ShapeEditorsCollective instead
186 lc->shape_editor->unset_item(SH_NODEPATH);
187 lc->shape_editor->unset_item(SH_KNOTHOLDER);
188 SPItem *item = selection->singleItem();
189 lc->shape_editor->set_item(item, SH_NODEPATH);
190 lc->shape_editor->set_item(item, SH_KNOTHOLDER);
191 lc->shape_editor->update_statusbar();
192 }
194 static void
195 sp_lpetool_context_set(SPEventContext *ec, gchar const *key, gchar const *val)
196 {
197 // FIXME: how to set this correcly? the value from preferences-skeleton.h doesn't seem to get
198 // read (it wants to set drag = 1)
199 lpetool_parent_class->set(ec, key, "drag");
201 /**
202 //pass on up to parent class to handle common attributes.
203 if ( lpetool_parent_class->set ) {
204 lpetool_parent_class->set(ec, key, val);
205 }
206 **/
207 }
209 gint
210 sp_lpetool_context_root_handler(SPEventContext *event_context, GdkEvent *event)
211 {
212 SPLPEToolContext *lc = SP_LPETOOL_CONTEXT(event_context);
213 SPDesktop *desktop = event_context->desktop;
214 Inkscape::Selection *selection = sp_desktop_selection (desktop);
216 bool ret = false;
218 if (sp_pen_context_has_waiting_LPE(lc)) {
219 // quit when we are waiting for a LPE to be applied
220 ret = ((SPEventContextClass *) lpetool_parent_class)->root_handler(event_context, event);
221 return ret;
222 }
224 switch (event->type) {
225 case GDK_BUTTON_PRESS:
226 if (event->button.button == 1 && !event_context->space_panning) {
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 // save drag origin
235 event_context->xp = (gint) event->button.x;
236 event_context->yp = (gint) event->button.y;
237 event_context->within_tolerance = true;
238 lc->shape_editor->cancel_hit();
240 using namespace Inkscape::LivePathEffect;
242 int mode = prefs_get_int_attribute("tools.lpetool", "mode", 0);
243 EffectType type = lpesubtools[mode];
245 //bool over_stroke = lc->shape_editor->is_over_stroke(NR::Point(event->button.x, event->button.y), true);
247 sp_pen_context_wait_for_LPE_mouse_clicks(lc, type, Inkscape::LivePathEffect::Effect::acceptsNumClicks(type));
249 // we pass the mouse click on to pen tool as the first click which it should collect
250 ret = ((SPEventContextClass *) lpetool_parent_class)->root_handler(event_context, event);
251 }
252 break;
253 case GDK_MOTION_NOTIFY:
254 {
255 if (!lc->shape_editor->has_nodepath() || selection->singleItem() == NULL) {
256 break;
257 }
259 bool over_stroke = false;
260 over_stroke = lc->shape_editor->is_over_stroke(NR::Point(event->motion.x, event->motion.y), false);
262 if (over_stroke) {
263 event_context->cursor_shape = cursor_node_xpm;
264 event_context->hot_x = 1;
265 event_context->hot_y = 1;
266 sp_event_context_update_cursor(event_context);
267 } else {
268 lc->cursor_shape = cursor_crosshairs_xpm;
269 lc->hot_x = 7;
270 lc->hot_y = 7;
271 sp_event_context_update_cursor(event_context);
272 }
273 }
274 break;
277 case GDK_BUTTON_RELEASE:
278 {
279 /**
280 break;
281 **/
282 }
284 case GDK_KEY_PRESS:
285 /**
286 switch (get_group0_keyval (&event->key)) {
287 }
288 break;
289 **/
291 case GDK_KEY_RELEASE:
292 /**
293 switch (get_group0_keyval(&event->key)) {
294 case GDK_Control_L:
295 case GDK_Control_R:
296 dc->_message_context->clear();
297 break;
298 default:
299 break;
300 }
301 **/
303 default:
304 break;
305 }
307 if (!ret) {
308 if (((SPEventContextClass *) lpetool_parent_class)->root_handler) {
309 ret = ((SPEventContextClass *) lpetool_parent_class)->root_handler(event_context, event);
310 }
311 }
313 return ret;
314 }
316 /*
317 * Finds the index in the list of geometric subtools corresponding to the given LPE type.
318 * Returns -1 if no subtool is found.
319 */
320 int
321 lpetool_mode_to_index(Inkscape::LivePathEffect::EffectType const type) {
322 for (int i = 0; i < num_subtools; ++i) {
323 if (lpesubtools[i] == type) {
324 return i;
325 }
326 }
327 return -1;
328 }
330 /*
331 * Checks whether an item has a construction applied as LPE and if so returns the index in
332 * lpesubtools of this construction
333 */
334 int lpetool_item_has_construction(SPLPEToolContext *lc, SPItem *item)
335 {
336 if (!SP_IS_LPE_ITEM(item)) {
337 return -1;
338 }
340 Inkscape::LivePathEffect::Effect* lpe = sp_lpe_item_get_current_lpe(SP_LPE_ITEM(item));
341 if (!lpe) {
342 return -1;
343 }
344 return lpetool_mode_to_index(lpe->effectType());
345 }
347 /*
348 * Attempts to perform the construction of the given type (i.e., to apply the corresponding LPE) to
349 * a single selected item. Returns whether we succeeded.
350 */
351 bool
352 lpetool_try_construction(SPLPEToolContext *lc, Inkscape::LivePathEffect::EffectType const type)
353 {
354 Inkscape::Selection *selection = sp_desktop_selection(lc->desktop);
355 SPItem *item = selection->singleItem();
357 // TODO: should we check whether type represents a valid geometric construction?
358 if (item && SP_IS_LPE_ITEM(item) && Inkscape::LivePathEffect::Effect::acceptsNumClicks(type) == 0) {
359 Inkscape::LivePathEffect::Effect::createAndApply(type, sp_desktop_document(lc->desktop), item);
360 return true;
361 }
362 return false;
363 }
365 void
366 lpetool_context_switch_mode(SPLPEToolContext *lc, Inkscape::LivePathEffect::EffectType const type)
367 {
368 int index = lpetool_mode_to_index(type);
369 if (index != -1) {
370 lc->mode = type;
371 lc->desktop->setToolboxSelectOneValue ("lpetool_mode_action", index);
372 } else {
373 g_warning ("Invalid mode selected: %d", type);
374 return;
375 }
376 }
378 void
379 lpetool_get_limiting_bbox_corners(SPDocument *document, Geom::Point &A, Geom::Point &B) {
380 Geom::Coord w = sp_document_width(document);
381 Geom::Coord h = sp_document_height(document);
383 double ulx = prefs_get_double_attribute ("tools.lpetool", "bbox_upperleftx", 0);
384 double uly = prefs_get_double_attribute ("tools.lpetool", "bbox_upperlefty", 0);
385 double lrx = prefs_get_double_attribute ("tools.lpetool", "bbox_lowerrightx", w);
386 double lry = prefs_get_double_attribute ("tools.lpetool", "bbox_lowerrighty", h);
388 A = Geom::Point(ulx, uly);
389 B = Geom::Point(lrx, lry);
390 }
392 /*
393 * Reads the limiting bounding box from preferences and draws it on the screen
394 */
395 // TODO: Note that currently the bbox is not user-settable; we simply use the page borders
396 void
397 lpetool_context_reset_limiting_bbox(SPLPEToolContext *lc)
398 {
399 if (lc->canvas_bbox) {
400 gtk_object_destroy(GTK_OBJECT(lc->canvas_bbox));
401 lc->canvas_bbox = NULL;
402 }
404 if (prefs_get_int_attribute("tools.lpetool", "show_bbox", 1) == 0)
405 return;
407 SPDocument *document = sp_desktop_document(lc->desktop);
409 Geom::Point A, B;
410 lpetool_get_limiting_bbox_corners(document, A, B);
411 NR::Matrix doc2dt(lc->desktop->doc2dt());
412 A *= doc2dt;
413 B *= doc2dt;
415 Geom::Rect rect(A, B);
416 SPCurve *curve = SPCurve::new_from_rect(rect);
418 lc->canvas_bbox = sp_canvas_bpath_new (sp_desktop_controls(lc->desktop), curve);
419 sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(lc->canvas_bbox), 0x0000ffff, 0.8, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT, 5, 5);
420 }
422 static void
423 set_pos_and_anchor(SPCanvasText *canvas_text, const Geom::Piecewise<Geom::D2<Geom::SBasis> > &pwd2,
424 const double t, const double length, bool use_curvature = false)
425 {
426 using namespace Geom;
428 Piecewise<D2<SBasis> > pwd2_reparam = arc_length_parametrization(pwd2, 2 , 0.1);
429 double t_reparam = pwd2_reparam.cuts.back() * t;
430 Point pos = pwd2_reparam.valueAt(t_reparam);
431 Point dir = unit_vector(derivative(pwd2_reparam).valueAt(t_reparam));
432 Point n = -rot90(dir);
433 double angle = Geom::angle_between(dir, Point(1,0));
435 sp_canvastext_set_coords(canvas_text, pos + n * length);
436 sp_canvastext_set_anchor(canvas_text, std::sin(angle), -std::cos(angle));
437 }
439 void
440 lpetool_create_measuring_items(SPLPEToolContext *lc, Inkscape::Selection *selection)
441 {
442 bool show = prefs_get_int_attribute ("tools.lpetool", "show_measuring_info", 1) == 1 ? true : false;
443 if (!selection) {
444 selection = sp_desktop_selection(lc->desktop);
445 }
447 SPPath *path;
448 SPCurve *curve;
449 SPCanvasText *canvas_text;
450 SPCanvasGroup *tmpgrp = sp_desktop_tempgroup(lc->desktop);
451 gchar *arc_length;
452 double lengthval;
454 for (GSList const *i = selection->itemList(); i != NULL; i = i->next) {
455 if (SP_IS_PATH(i->data)) {
456 path = SP_PATH(i->data);
457 curve = sp_shape_get_curve(SP_SHAPE(path));
458 Geom::Piecewise<Geom::D2<Geom::SBasis> > pwd2 = paths_to_pw(curve->get_pathvector());
459 canvas_text = (SPCanvasText *) sp_canvastext_new(tmpgrp, lc->desktop, Geom::Point(0,0), "");
460 if (!show)
461 sp_canvas_item_hide(SP_CANVAS_ITEM(canvas_text));
463 SPUnitId unitid = static_cast<SPUnitId>(prefs_get_int_attribute("tools.lpetool", "unitid", SP_UNIT_PX));
464 SPUnit unit = sp_unit_get_by_id(unitid);
466 lengthval = Geom::length(pwd2);
467 gboolean success;
468 success = sp_convert_distance(&lengthval, &sp_unit_get_by_id(SP_UNIT_PX), &unit);
469 arc_length = g_strdup_printf("%.2f %s", lengthval, success ? sp_unit_get_abbreviation(&unit) : "px");
470 sp_canvastext_set_text (canvas_text, arc_length);
471 set_pos_and_anchor(canvas_text, pwd2, 0.5, 10);
472 // TODO: must we free arc_length?
473 (*lc->measuring_items)[path] = SP_CANVAS_ITEM(canvas_text);
474 }
475 }
476 }
478 void
479 lpetool_delete_measuring_items(SPLPEToolContext *lc)
480 {
481 std::map<SPPath *, SPCanvasItem*>::iterator i;
482 for (i = lc->measuring_items->begin(); i != lc->measuring_items->end(); ++i) {
483 gtk_object_destroy(GTK_OBJECT(i->second));
484 }
485 lc->measuring_items->clear();
486 }
488 void
489 lpetool_update_measuring_items(SPLPEToolContext *lc)
490 {
491 SPPath *path;
492 SPCurve *curve;
493 double lengthval;
494 gchar *arc_length;
495 std::map<SPPath *, SPCanvasItem*>::iterator i;
496 for (i = lc->measuring_items->begin(); i != lc->measuring_items->end(); ++i) {
497 path = i->first;
498 curve = sp_shape_get_curve(SP_SHAPE(path));
499 Geom::Piecewise<Geom::D2<Geom::SBasis> > pwd2 = Geom::paths_to_pw(curve->get_pathvector());
500 SPUnitId unitid = static_cast<SPUnitId>(prefs_get_int_attribute("tools.lpetool", "unitid", SP_UNIT_PX));
501 SPUnit unit = sp_unit_get_by_id(unitid);
502 lengthval = Geom::length(pwd2);
503 gboolean success;
504 success = sp_convert_distance(&lengthval, &sp_unit_get_by_id(SP_UNIT_PX), &unit);
505 arc_length = g_strdup_printf("%.2f %s", lengthval, success ? sp_unit_get_abbreviation(&unit) : "px");
506 sp_canvastext_set_text (SP_CANVASTEXT(i->second), arc_length);
507 set_pos_and_anchor(SP_CANVASTEXT(i->second), pwd2, 0.5, 10);
508 // TODO: must we free arc_length?
509 }
510 }
512 void
513 lpetool_show_measuring_info(SPLPEToolContext *lc, bool show)
514 {
515 std::map<SPPath *, SPCanvasItem*>::iterator i;
516 for (i = lc->measuring_items->begin(); i != lc->measuring_items->end(); ++i) {
517 if (show) {
518 sp_canvas_item_show(i->second);
519 } else {
520 sp_canvas_item_hide(i->second);
521 }
522 }
523 }
525 /*
526 Local Variables:
527 mode:c++
528 c-file-style:"stroustrup"
529 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
530 indent-tabs-mode:nil
531 fill-column:99
532 End:
533 */
534 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :