Code

First step towards making helper paths for LPE items work better
[inkscape.git] / src / node-context.cpp
index 56b6dd760ff13ff44159feb5af7b115bca8dc3a0..88ea1667d24e40632208f8e9225dbb13073fc3c7 100644 (file)
@@ -13,6 +13,8 @@
 #ifdef HAVE_CONFIG_H
 # include "config.h"
 #endif
+#include <cstring>
+#include <string>
 #include <gdk/gdkkeysyms.h>
 #include "macros.h"
 #include <glibmm/i18n.h>
 #include "style.h"
 #include "splivarot.h"
 #include "shape-editor.h"
+#include "live_effects/effect.h"
+
+// needed for flash nodepath upon mouseover:
+#include "display/canvas-bpath.h"
+#include "display/curve.h"
 
 static void sp_node_context_class_init(SPNodeContextClass *klass);
 static void sp_node_context_init(SPNodeContext *node_context);
@@ -95,6 +102,10 @@ sp_node_context_init(SPNodeContext *node_context)
     node_context->rightctrl = FALSE;
     
     new (&node_context->sel_changed_connection) sigc::connection();
+
+    node_context->flash_tempitem = NULL;
+    node_context->flashed_item = NULL;
+    node_context->remove_flash_counter = 0;
 }
 
 static void
@@ -125,10 +136,14 @@ sp_node_context_setup(SPEventContext *ec)
     if (((SPEventContextClass *) parent_class)->setup)
         ((SPEventContextClass *) parent_class)->setup(ec);
 
+    Inkscape::Selection *selection = sp_desktop_selection (ec->desktop);
     nc->sel_changed_connection.disconnect();
-    nc->sel_changed_connection = sp_desktop_selection(ec->desktop)->connectChanged(sigc::bind(sigc::ptr_fun(&sp_node_context_selection_changed), (gpointer)nc));
+    nc->sel_modified_connection.disconnect();
+    nc->sel_changed_connection =
+        selection->connectChanged(sigc::bind(sigc::ptr_fun(&sp_node_context_selection_changed), (gpointer)nc));
+    nc->sel_modified_connection =
+        selection->connectModified(sigc::bind(sigc::ptr_fun(&sp_node_context_selection_modified), (gpointer)nc));
 
-    Inkscape::Selection *selection = sp_desktop_selection(ec->desktop);
     SPItem *item = selection->singleItem();
 
     nc->shape_editor = new ShapeEditor(ec->desktop);
@@ -153,11 +168,34 @@ sp_node_context_setup(SPEventContext *ec)
         ec->enableGrDrag();
     }
 
+    ec->desktop->emitToolSubselectionChanged(NULL); // sets the coord entry fields to inactive
+
     nc->_node_message_context = new Inkscape::MessageContext((ec->desktop)->messageStack());
 
     nc->shape_editor->update_statusbar();
 }
 
+static void
+sp_node_context_flash_path(SPEventContext *event_context, SPItem *item, guint timeout) {
+    SPNodeContext *nc = SP_NODE_CONTEXT(event_context);
+
+    nc->remove_flash_counter = 3; // for some reason root_handler is called twice after each item_handler...
+    if (nc->flashed_item != item) {
+        // we entered a new item
+        nc->flashed_item = item;
+        SPDesktop *desktop = event_context->desktop;
+        if (nc->flash_tempitem) {
+            desktop->remove_temporary_canvasitem(nc->flash_tempitem);
+            nc->flash_tempitem = NULL;
+        }
+
+        if (SP_IS_PATH(item)) {
+            SPCanvasItem *canvasitem = sp_nodepath_generate_helperpath(desktop, SP_PATH(item));
+            nc->flash_tempitem = desktop->add_temporary_canvasitem (canvasitem, timeout);
+        }
+    }
+}
+
 /**
 \brief  Callback that processes the "changed" signal on the selection;
 destroys old and creates new nodepath and reassigns listeners to the new selected item's repr
@@ -171,10 +209,30 @@ sp_node_context_selection_changed(Inkscape::Selection *selection, gpointer data)
     nc->shape_editor->unset_item();
     SPItem *item = selection->singleItem(); 
     nc->shape_editor->set_item(item);
-
     nc->shape_editor->update_statusbar();
 }
 
+/**
+\brief  Callback that processes the "modified" signal on the selection;
+updates temporary canvasitems associated to LPEItems in the selection
+*/
+void
+sp_node_context_selection_modified(Inkscape::Selection *selection, guint /*flags*/, gpointer /*data*/)
+{
+    // TODO: do this for *all* items in the selection
+    SPItem *item = selection->singleItem();
+
+    // TODO: This is *very* inefficient! Can we avoid destroying and recreating the temporary
+    // items? Also, we should only update those that actually need it!
+    if (item && SP_IS_LPE_ITEM(item)) {
+        SPLPEItem *lpeitem = SP_LPE_ITEM(item);
+        SPDesktop *desktop = selection->desktop();
+
+        sp_lpe_item_remove_temporary_canvasitems(lpeitem, desktop);
+        sp_lpe_item_add_temporary_canvasitems(lpeitem, desktop);
+    }
+}
+
 void
 sp_node_context_show_modifier_tip(SPEventContext *event_context, GdkEvent *event)
 {
@@ -185,12 +243,29 @@ sp_node_context_show_modifier_tip(SPEventContext *event_context, GdkEvent *event
          _("<b>Alt</b>: lock handle length; <b>Ctrl+Alt</b>: move along handles"));
 }
 
-
 static gint
 sp_node_context_item_handler(SPEventContext *event_context, SPItem *item, GdkEvent *event)
 {
     gint ret = FALSE;
 
+    if (prefs_get_int_attribute ("tools.nodes", "pathflash_enabled", 0) == 1) {
+        guint timeout = prefs_get_int_attribute("tools.nodes", "pathflash_timeout", 500);
+        if (SP_IS_LPE_ITEM(item)) {
+            Inkscape::LivePathEffect::Effect *lpe = sp_lpe_item_get_current_lpe(SP_LPE_ITEM(item));
+            if (lpe) {
+                if (lpe->pathFlashType() == Inkscape::LivePathEffect::SUPPRESS_FLASH) {
+                    // suppressed and permanent flashes for LPE items are handled in
+                    // sp_node_context_selection_changed()
+                    return ret;
+                }
+                if (lpe->pathFlashType() == Inkscape::LivePathEffect::PERMANENT_FLASH) {
+                    timeout = 0;
+                }
+            }
+        }
+        sp_node_context_flash_path(event_context, item, timeout);
+    }
+
     if (((SPEventContextClass *) parent_class)->item_handler)
         ret = ((SPEventContextClass *) parent_class)->item_handler(event_context, item, event);
 
@@ -209,6 +284,14 @@ sp_node_context_root_handler(SPEventContext *event_context, GdkEvent *event)
     int const snaps = prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12);
     double const offset = prefs_get_double_attribute_limited("options.defaultscale", "value", 2, 0, 1000);
 
+    if ( (nc->flash_tempitem) && (nc->remove_flash_counter <= 0) ) {
+        desktop->remove_temporary_canvasitem(nc->flash_tempitem);
+        nc->flash_tempitem = NULL;
+        nc->flashed_item = NULL; // also reset this one, so the next time the same object is hovered over it shows again the highlight
+    } else {
+        nc->remove_flash_counter--;
+    }
+
     gint ret = FALSE;
     switch (event->type) {
         case GDK_BUTTON_PRESS:
@@ -482,17 +565,26 @@ sp_node_context_root_handler(SPEventContext *event_context, GdkEvent *event)
                         ret = TRUE;
                     }
                     break;
+                case GDK_x:
+                case GDK_X:
+                    if (MOD__ALT_ONLY) {
+                        desktop->setToolboxFocusTo ("altx-nodes");
+                        ret = TRUE;
+                    }
+                    break;
                 case GDK_Left: // move selection left
                 case GDK_KP_Left:
                 case GDK_KP_4:
                     if (!MOD__CTRL) { // not ctrl
+                        gint mul = 1 + gobble_key_events(
+                            get_group0_keyval(&event->key), 0); // with any mask
                         if (MOD__ALT) { // alt
-                            if (MOD__SHIFT) nc->shape_editor->move_nodes_screen(-10, 0); // shift
-                            else nc->shape_editor->move_nodes_screen(-1, 0); // no shift
+                            if (MOD__SHIFT) nc->shape_editor->move_nodes_screen(mul*-10, 0); // shift
+                            else nc->shape_editor->move_nodes_screen(mul*-1, 0); // no shift
                         }
                         else { // no alt
-                            if (MOD__SHIFT) nc->shape_editor->move_nodes(-10*nudge, 0); // shift
-                            else nc->shape_editor->move_nodes(-nudge, 0); // no shift
+                            if (MOD__SHIFT) nc->shape_editor->move_nodes(mul*-10*nudge, 0); // shift
+                            else nc->shape_editor->move_nodes(mul*-nudge, 0); // no shift
                         }
                         ret = TRUE;
                     }
@@ -501,13 +593,15 @@ sp_node_context_root_handler(SPEventContext *event_context, GdkEvent *event)
                 case GDK_KP_Up:
                 case GDK_KP_8:
                     if (!MOD__CTRL) { // not ctrl
+                        gint mul = 1 + gobble_key_events(
+                            get_group0_keyval(&event->key), 0); // with any mask
                         if (MOD__ALT) { // alt
-                            if (MOD__SHIFT) nc->shape_editor->move_nodes_screen(0, 10); // shift
-                            else nc->shape_editor->move_nodes_screen(0, 1); // no shift
+                            if (MOD__SHIFT) nc->shape_editor->move_nodes_screen(0, mul*10); // shift
+                            else nc->shape_editor->move_nodes_screen(0, mul*1); // no shift
                         }
                         else { // no alt
-                            if (MOD__SHIFT) nc->shape_editor->move_nodes(0, 10*nudge); // shift
-                            else nc->shape_editor->move_nodes(0, nudge); // no shift
+                            if (MOD__SHIFT) nc->shape_editor->move_nodes(0, mul*10*nudge); // shift
+                            else nc->shape_editor->move_nodes(0, mul*nudge); // no shift
                         }
                         ret = TRUE;
                     }
@@ -516,13 +610,15 @@ sp_node_context_root_handler(SPEventContext *event_context, GdkEvent *event)
                 case GDK_KP_Right:
                 case GDK_KP_6:
                     if (!MOD__CTRL) { // not ctrl
+                        gint mul = 1 + gobble_key_events(
+                            get_group0_keyval(&event->key), 0); // with any mask
                         if (MOD__ALT) { // alt
-                            if (MOD__SHIFT) nc->shape_editor->move_nodes_screen(10, 0); // shift
-                            else nc->shape_editor->move_nodes_screen(1, 0); // no shift
+                            if (MOD__SHIFT) nc->shape_editor->move_nodes_screen(mul*10, 0); // shift
+                            else nc->shape_editor->move_nodes_screen(mul*1, 0); // no shift
                         }
                         else { // no alt
-                            if (MOD__SHIFT) nc->shape_editor->move_nodes(10*nudge, 0); // shift
-                            else nc->shape_editor->move_nodes(nudge, 0); // no shift
+                            if (MOD__SHIFT) nc->shape_editor->move_nodes(mul*10*nudge, 0); // shift
+                            else nc->shape_editor->move_nodes(mul*nudge, 0); // no shift
                         }
                         ret = TRUE;
                     }
@@ -531,13 +627,15 @@ sp_node_context_root_handler(SPEventContext *event_context, GdkEvent *event)
                 case GDK_KP_Down:
                 case GDK_KP_2:
                     if (!MOD__CTRL) { // not ctrl
+                        gint mul = 1 + gobble_key_events(
+                            get_group0_keyval(&event->key), 0); // with any mask
                         if (MOD__ALT) { // alt
-                            if (MOD__SHIFT) nc->shape_editor->move_nodes_screen(0, -10); // shift
-                            else nc->shape_editor->move_nodes_screen(0, -1); // no shift
+                            if (MOD__SHIFT) nc->shape_editor->move_nodes_screen(0, mul*-10); // shift
+                            else nc->shape_editor->move_nodes_screen(0, mul*-1); // no shift
                         }
                         else { // no alt
-                            if (MOD__SHIFT) nc->shape_editor->move_nodes(0, -10*nudge); // shift
-                            else nc->shape_editor->move_nodes(0, -nudge); // no shift
+                            if (MOD__SHIFT) nc->shape_editor->move_nodes(0, mul*-10*nudge); // shift
+                            else nc->shape_editor->move_nodes(0, mul*-nudge); // no shift
                         }
                         ret = TRUE;
                     }