Code

Unselet item in geometry tool when the user clicked into empty space
[inkscape.git] / src / lpe-tool-context.cpp
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_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;
106 static void
107 sp_lpetool_context_init(SPLPEToolContext *lc)
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();
119 static void
120 sp_lpetool_context_dispose(GObject *object)
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);
144 static void
145 sp_lpetool_context_setup(SPEventContext *ec)
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     if (item) {
169         lc->shape_editor->set_item(item, SH_NODEPATH);
170         lc->shape_editor->set_item(item, SH_KNOTHOLDER);
171     }
173     if (prefs_get_int_attribute("tools.lpetool", "selcue", 0) != 0) {
174         ec->enableSelectionCue();
175     }
177     lc->_lpetool_message_context = new Inkscape::MessageContext((ec->desktop)->messageStack());
180     lc->shape_editor->update_statusbar();
183 /**
184 \brief  Callback that processes the "changed" signal on the selection;
185 destroys old and creates new nodepath and reassigns listeners to the new selected item's repr
186 */
187 void
188 sp_lpetool_context_selection_changed(Inkscape::Selection *selection, gpointer data)
190     SPLPEToolContext *lc = SP_LPETOOL_CONTEXT(data);
192     // TODO: update ShapeEditorsCollective instead
193     lc->shape_editor->unset_item(SH_NODEPATH);
194     lc->shape_editor->unset_item(SH_KNOTHOLDER);
195     SPItem *item = selection->singleItem();
196     lc->shape_editor->set_item(item, SH_NODEPATH);
197     lc->shape_editor->set_item(item, SH_KNOTHOLDER);
198     lc->shape_editor->update_statusbar();
201 static void
202 sp_lpetool_context_set(SPEventContext *ec, gchar const *key, gchar const *val)
204     // FIXME: how to set this correcly? the value from preferences-skeleton.h doesn't seem to get
205     // read (it wants to set drag = 1)
206     lpetool_parent_class->set(ec, key, "drag");
208     /**
209     //pass on up to parent class to handle common attributes.
210     if ( lpetool_parent_class->set ) {
211         lpetool_parent_class->set(ec, key, val);
212     }
213     **/
216 static gint 
217 sp_lpetool_context_item_handler(SPEventContext *ec, SPItem *item, GdkEvent *event)
219     gint ret = FALSE;
221     switch (event->type) {
222         case GDK_BUTTON_PRESS:
223         {
224             // select the clicked item but do nothing else
225             Inkscape::Selection * const selection = sp_desktop_selection(ec->desktop);
226             selection->clear();
227             selection->add(item);
228             ret = TRUE;
229             break;
230         }
231         case GDK_BUTTON_RELEASE:
232             // TODO: do we need to catch this or can we pass it on to the parent handler?
233             ret = TRUE;
234             break;
235         default:
236             break;
237     }
239     if (!ret) {
240         if (((SPEventContextClass *) lpetool_parent_class)->item_handler)
241             ret = ((SPEventContextClass *) lpetool_parent_class)->item_handler(ec, item, event);
242     }
244     return ret;
247 gint
248 sp_lpetool_context_root_handler(SPEventContext *event_context, GdkEvent *event)
250     SPLPEToolContext *lc = SP_LPETOOL_CONTEXT(event_context);
251     SPDesktop *desktop = event_context->desktop;
252     Inkscape::Selection *selection = sp_desktop_selection (desktop);
254     bool ret = false;
256     if (sp_pen_context_has_waiting_LPE(lc)) {
257         // quit when we are waiting for a LPE to be applied
258         ret = ((SPEventContextClass *) lpetool_parent_class)->root_handler(event_context, event);
259         return ret;
260     }
262     switch (event->type) {
263         case GDK_BUTTON_PRESS:
264             if (event->button.button == 1 && !event_context->space_panning) {
265                 if (lc->mode == Inkscape::LivePathEffect::INVALID_LPE) {
266                     // don't do anything for now if we are inactive (except clearing the selection
267                     // since this was a click into empty space)
268                     selection->clear();
269                     desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Choose a construction tool from the toolbar."));
270                     ret = true;
271                     break;
272                 }
274                 // save drag origin
275                 event_context->xp = (gint) event->button.x;
276                 event_context->yp = (gint) event->button.y;
277                 event_context->within_tolerance = true;
278                 lc->shape_editor->cancel_hit();
280                 using namespace Inkscape::LivePathEffect;
282                 int mode = prefs_get_int_attribute("tools.lpetool", "mode", 0);
283                 EffectType type = lpesubtools[mode];
285                 //bool over_stroke = lc->shape_editor->is_over_stroke(NR::Point(event->button.x, event->button.y), true);
287                 sp_pen_context_wait_for_LPE_mouse_clicks(lc, type, Inkscape::LivePathEffect::Effect::acceptsNumClicks(type));
289                 // we pass the mouse click on to pen tool as the first click which it should collect
290                 ret = ((SPEventContextClass *) lpetool_parent_class)->root_handler(event_context, event);
291             }
292             break;
293         case GDK_MOTION_NOTIFY:
294         {
295             if (!lc->shape_editor->has_nodepath() || selection->singleItem() == NULL) {
296                 break;
297             }
299             bool over_stroke = false;
300             over_stroke = lc->shape_editor->is_over_stroke(NR::Point(event->motion.x, event->motion.y), false);
302             if (over_stroke) {
303                 event_context->cursor_shape = cursor_node_xpm;
304                 event_context->hot_x = 1;
305                 event_context->hot_y = 1;
306                 sp_event_context_update_cursor(event_context);
307             } else {
308                 lc->cursor_shape = cursor_crosshairs_xpm;
309                 lc->hot_x = 7;
310                 lc->hot_y = 7;
311                 sp_event_context_update_cursor(event_context);
312             }
313         }
314         break;
317     case GDK_BUTTON_RELEASE:
318     {
319         /**
320         break;
321         **/
322     }
324     case GDK_KEY_PRESS:
325         /**
326         switch (get_group0_keyval (&event->key)) {
327         }
328         break;
329         **/
331     case GDK_KEY_RELEASE:
332         /**
333         switch (get_group0_keyval(&event->key)) {
334             case GDK_Control_L:
335             case GDK_Control_R:
336                 dc->_message_context->clear();
337                 break;
338             default:
339                 break;
340         }
341         **/
343     default:
344         break;
345     }
347     if (!ret) {
348         if (((SPEventContextClass *) lpetool_parent_class)->root_handler) {
349             ret = ((SPEventContextClass *) lpetool_parent_class)->root_handler(event_context, event);
350         }
351     }
353     return ret;
356 /*
357  * Finds the index in the list of geometric subtools corresponding to the given LPE type.
358  * Returns -1 if no subtool is found.
359  */
360 int
361 lpetool_mode_to_index(Inkscape::LivePathEffect::EffectType const type) {
362     for (int i = 0; i < num_subtools; ++i) {
363         if (lpesubtools[i] == type) {
364             return i;
365         }
366     }
367     return -1;
370 /*
371  * Checks whether an item has a construction applied as LPE and if so returns the index in
372  * lpesubtools of this construction
373  */
374 int lpetool_item_has_construction(SPLPEToolContext *lc, SPItem *item)
376     if (!SP_IS_LPE_ITEM(item)) {
377         return -1;
378     }
380     Inkscape::LivePathEffect::Effect* lpe = sp_lpe_item_get_current_lpe(SP_LPE_ITEM(item));
381     if (!lpe) {
382         return -1;
383     }
384     return lpetool_mode_to_index(lpe->effectType());
387 /*
388  * Attempts to perform the construction of the given type (i.e., to apply the corresponding LPE) to
389  * a single selected item. Returns whether we succeeded.
390  */
391 bool
392 lpetool_try_construction(SPLPEToolContext *lc, Inkscape::LivePathEffect::EffectType const type)
394     Inkscape::Selection *selection = sp_desktop_selection(lc->desktop);
395     SPItem *item = selection->singleItem();
397     // TODO: should we check whether type represents a valid geometric construction?
398     if (item && SP_IS_LPE_ITEM(item) && Inkscape::LivePathEffect::Effect::acceptsNumClicks(type) == 0) {
399         Inkscape::LivePathEffect::Effect::createAndApply(type, sp_desktop_document(lc->desktop), item);
400         return true;
401     }
402     return false;
405 void
406 lpetool_context_switch_mode(SPLPEToolContext *lc, Inkscape::LivePathEffect::EffectType const type)
408     int index = lpetool_mode_to_index(type);
409     if (index != -1) {
410         lc->mode = type;
411         lc->desktop->setToolboxSelectOneValue ("lpetool_mode_action", index);
412     } else {
413         g_warning ("Invalid mode selected: %d", type);
414         return;
415     }
418 void
419 lpetool_get_limiting_bbox_corners(SPDocument *document, Geom::Point &A, Geom::Point &B) {
420     Geom::Coord w = sp_document_width(document);
421     Geom::Coord h = sp_document_height(document);
423     double ulx = prefs_get_double_attribute ("tools.lpetool", "bbox_upperleftx", 0);
424     double uly = prefs_get_double_attribute ("tools.lpetool", "bbox_upperlefty", 0);
425     double lrx = prefs_get_double_attribute ("tools.lpetool", "bbox_lowerrightx", w);
426     double lry = prefs_get_double_attribute ("tools.lpetool", "bbox_lowerrighty", h);
428     A = Geom::Point(ulx, uly);
429     B = Geom::Point(lrx, lry);
432 /*
433  * Reads the limiting bounding box from preferences and draws it on the screen
434  */
435 // TODO: Note that currently the bbox is not user-settable; we simply use the page borders
436 void
437 lpetool_context_reset_limiting_bbox(SPLPEToolContext *lc)
439     if (lc->canvas_bbox) {
440         gtk_object_destroy(GTK_OBJECT(lc->canvas_bbox));
441         lc->canvas_bbox = NULL;
442     }
444     if (prefs_get_int_attribute("tools.lpetool", "show_bbox", 1) == 0)
445         return;
447     SPDocument *document = sp_desktop_document(lc->desktop);
449     Geom::Point A, B;
450     lpetool_get_limiting_bbox_corners(document, A, B);
451     NR::Matrix doc2dt(lc->desktop->doc2dt());
452     A *= doc2dt;
453     B *= doc2dt;
455     Geom::Rect rect(A, B);
456     SPCurve *curve = SPCurve::new_from_rect(rect);
458     lc->canvas_bbox = sp_canvas_bpath_new (sp_desktop_controls(lc->desktop), curve);
459     sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(lc->canvas_bbox), 0x0000ffff, 0.8, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT, 5, 5);
462 static void
463 set_pos_and_anchor(SPCanvasText *canvas_text, const Geom::Piecewise<Geom::D2<Geom::SBasis> > &pwd2,
464                    const double t, const double length, bool use_curvature = false)
466     using namespace Geom;
468     Piecewise<D2<SBasis> > pwd2_reparam = arc_length_parametrization(pwd2, 2 , 0.1);
469     double t_reparam = pwd2_reparam.cuts.back() * t;
470     Point pos = pwd2_reparam.valueAt(t_reparam);
471     Point dir = unit_vector(derivative(pwd2_reparam).valueAt(t_reparam));
472     Point n = -rot90(dir);
473     double angle = Geom::angle_between(dir, Point(1,0));
475     sp_canvastext_set_coords(canvas_text, pos + n * length);
476     sp_canvastext_set_anchor(canvas_text, std::sin(angle), -std::cos(angle));
479 void
480 lpetool_create_measuring_items(SPLPEToolContext *lc, Inkscape::Selection *selection)
482     bool show = prefs_get_int_attribute ("tools.lpetool", "show_measuring_info",  1) == 1 ? true : false;
483     if (!selection) {
484         selection = sp_desktop_selection(lc->desktop);
485     }
487     SPPath *path;
488     SPCurve *curve;
489     SPCanvasText *canvas_text;
490     SPCanvasGroup *tmpgrp = sp_desktop_tempgroup(lc->desktop);
491     gchar *arc_length;
492     double lengthval;
494     for (GSList const *i = selection->itemList(); i != NULL; i = i->next) {
495         if (SP_IS_PATH(i->data)) {
496             path = SP_PATH(i->data);
497             curve = sp_shape_get_curve(SP_SHAPE(path));
498             Geom::Piecewise<Geom::D2<Geom::SBasis> > pwd2 = paths_to_pw(curve->get_pathvector());
499             canvas_text = (SPCanvasText *) sp_canvastext_new(tmpgrp, lc->desktop, Geom::Point(0,0), "");
500             if (!show)
501                 sp_canvas_item_hide(SP_CANVAS_ITEM(canvas_text));
503             SPUnitId unitid = static_cast<SPUnitId>(prefs_get_int_attribute("tools.lpetool", "unitid", SP_UNIT_PX));
504             SPUnit unit = sp_unit_get_by_id(unitid);
506             lengthval = Geom::length(pwd2);
507             gboolean success;
508             success = sp_convert_distance(&lengthval, &sp_unit_get_by_id(SP_UNIT_PX), &unit);
509             arc_length = g_strdup_printf("%.2f %s", lengthval, success ? sp_unit_get_abbreviation(&unit) : "px");
510             sp_canvastext_set_text (canvas_text, arc_length);
511             set_pos_and_anchor(canvas_text, pwd2, 0.5, 10);
512             // TODO: must we free arc_length?
513             (*lc->measuring_items)[path] = SP_CANVAS_ITEM(canvas_text);
514         }
515     }
518 void
519 lpetool_delete_measuring_items(SPLPEToolContext *lc)
521     std::map<SPPath *, SPCanvasItem*>::iterator i;
522     for (i = lc->measuring_items->begin(); i != lc->measuring_items->end(); ++i) {
523         gtk_object_destroy(GTK_OBJECT(i->second));
524     }
525     lc->measuring_items->clear();
528 void
529 lpetool_update_measuring_items(SPLPEToolContext *lc)
531     SPPath *path;
532     SPCurve *curve;
533     double lengthval;
534     gchar *arc_length;
535     std::map<SPPath *, SPCanvasItem*>::iterator i;
536     for (i = lc->measuring_items->begin(); i != lc->measuring_items->end(); ++i) {
537         path = i->first;
538         curve = sp_shape_get_curve(SP_SHAPE(path));
539         Geom::Piecewise<Geom::D2<Geom::SBasis> > pwd2 = Geom::paths_to_pw(curve->get_pathvector());
540         SPUnitId unitid = static_cast<SPUnitId>(prefs_get_int_attribute("tools.lpetool", "unitid", SP_UNIT_PX));
541         SPUnit unit = sp_unit_get_by_id(unitid);
542         lengthval = Geom::length(pwd2);
543         gboolean success;
544         success = sp_convert_distance(&lengthval, &sp_unit_get_by_id(SP_UNIT_PX), &unit);
545         arc_length = g_strdup_printf("%.2f %s", lengthval, success ? sp_unit_get_abbreviation(&unit) : "px");
546         sp_canvastext_set_text (SP_CANVASTEXT(i->second), arc_length);
547         set_pos_and_anchor(SP_CANVASTEXT(i->second), pwd2, 0.5, 10);
548         // TODO: must we free arc_length?
549     }
552 void
553 lpetool_show_measuring_info(SPLPEToolContext *lc, bool show)
555     std::map<SPPath *, SPCanvasItem*>::iterator i;
556     for (i = lc->measuring_items->begin(); i != lc->measuring_items->end(); ++i) {
557         if (show) {
558             sp_canvas_item_show(i->second);
559         } else {
560             sp_canvas_item_hide(i->second);
561         }
562     }
565 /*
566   Local Variables:
567   mode:c++
568   c-file-style:"stroustrup"
569   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
570   indent-tabs-mode:nil
571   fill-column:99
572   End:
573 */
574 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :