Code

New LPE FreehandShape derived from PatternAlongPath (for the shapes in pen/pencil...
authorcilix42 <cilix42@users.sourceforge.net>
Tue, 15 Jul 2008 12:41:43 +0000 (12:41 +0000)
committercilix42 <cilix42@users.sourceforge.net>
Tue, 15 Jul 2008 12:41:43 +0000 (12:41 +0000)
src/draw-context.cpp
src/live_effects/effect.cpp
src/live_effects/effect.h
src/live_effects/lpe-patternalongpath.cpp
src/live_effects/lpe-patternalongpath.h
src/sp-lpe-item.cpp
src/sp-lpe-item.h

index 6831654eab6f1bc687913b9aecc5ab9cb350f5df..51938dc9eb800817e6ba3877f2ef42dccd48b120 100644 (file)
@@ -248,13 +248,13 @@ sp_draw_context_root_handler(SPEventContext *ec, GdkEvent *event)
 }
 
 static void
-spdc_paste_curve_as_param_path(const SPCurve *c, SPDrawContext *dc, SPItem *item)
+spdc_paste_curve_as_freehand_shape(const SPCurve *c, SPDrawContext *dc, SPItem *item)
 {
     using namespace Inkscape::LivePathEffect;
 
     // TODO: Don't paste path if nothing is on the clipboard
 
-    Effect::createAndApply(Inkscape::LivePathEffect::PATTERN_ALONG_PATH, dc->desktop->doc(), item);
+    Effect::createAndApply(Inkscape::LivePathEffect::FREEHAND_SHAPE, dc->desktop->doc(), item);
     Effect* lpe = sp_lpe_item_get_current_lpe(SP_LPE_ITEM(item));
     gchar *svgd = sp_svg_write_path(c->get_pathvector());
     static_cast<LPEPatternAlongPath*>(lpe)->pattern.paste_param_path(svgd);
@@ -269,7 +269,7 @@ spdc_check_for_and_apply_waiting_LPE(SPDrawContext *dc, SPItem *item)
 {
     using namespace Inkscape::LivePathEffect;
 
-    if (item) {
+    if (item && SP_IS_LPE_ITEM(item)) {
         if (prefs_get_int_attribute("tools.freehand", "spiro-spline-mode", 0)) {
             Effect::createAndApply(SPIRO, dc->desktop->doc(), item);
         }
@@ -286,7 +286,7 @@ spdc_check_for_and_apply_waiting_LPE(SPDrawContext *dc, SPItem *item)
             case 1:
             {
                 // take shape from clipboard; TODO: catch the case where clipboard is empty
-                Effect::createAndApply(PATTERN_ALONG_PATH, dc->desktop->doc(), item);
+                Effect::createAndApply(FREEHAND_SHAPE, dc->desktop->doc(), item);
                 Effect* lpe = sp_lpe_item_get_current_lpe(SP_LPE_ITEM(item));
                 static_cast<LPEPatternAlongPath*>(lpe)->pattern.on_paste_button_click();
 
@@ -303,7 +303,7 @@ spdc_check_for_and_apply_waiting_LPE(SPDrawContext *dc, SPItem *item)
                 c->lineto(200,10);
                 c->lineto(200,0);
                 c->closepath();
-                spdc_paste_curve_as_param_path(c, dc, item);
+                spdc_paste_curve_as_freehand_shape(c, dc, item);
                 c->unref();
 
                 shape_applied = true;
@@ -319,7 +319,7 @@ spdc_check_for_and_apply_waiting_LPE(SPDrawContext *dc, SPItem *item)
                 c->lineto(0,10);
                 c->lineto(200,5);
                 c->closepath();
-                spdc_paste_curve_as_param_path(c, dc, item);
+                spdc_paste_curve_as_freehand_shape(c, dc, item);
                 c->unref();
 
                 shape_applied = true;
@@ -355,10 +355,6 @@ spdc_check_for_and_apply_waiting_LPE(SPDrawContext *dc, SPItem *item)
 static void
 spdc_selection_changed(Inkscape::Selection *sel, SPDrawContext *dc)
 {
-    // note: in draw context, selection_changed() is only called with a valid item as argument when
-    // a new item was created; otherwise the following function call would yield wrong results
-    spdc_check_for_and_apply_waiting_LPE(dc, sel->singleItem());
-
     if (dc->attach) {
         spdc_attach_selection(dc, sel);
     }
@@ -652,6 +648,11 @@ spdc_flush_white(SPDrawContext *dc, SPCurve *gc)
             item->updateRepr();
         }
 
+
+        // we finished the path; now apply any waiting LPEs or freehand shapes
+        // FIXME: placing this here seems to cause issues with undo!
+        spdc_check_for_and_apply_waiting_LPE(dc, dc->selection->singleItem());
+
         sp_document_done(doc, SP_IS_PEN_CONTEXT(dc)? SP_VERB_CONTEXT_PEN : SP_VERB_CONTEXT_PENCIL, 
                          _("Draw path"));
 
index d5ac6dc7485812be8a98564405b48385a204bdcd..4c74d56a6f39d50acfb3b11613335e7809471d46 100644 (file)
@@ -72,6 +72,7 @@ const Util::EnumData<EffectType> LPETypeData[INVALID_LPE] = {
     // {constant defined in effect.h, N_("name of your effect"), "name of your effect in SVG"}
     {BEND_PATH,             N_("Bend"),                  "bend_path"},
     {PATTERN_ALONG_PATH,    N_("Pattern Along Path"),    "skeletal"},   // for historic reasons, this effect is called skeletal(strokes) in Inkscape:SVG
+    {FREEHAND_SHAPE,        N_("Freehand Shape"),        "freehand_shape"}, // this is actually a special type of PatternAlongPath, used to paste shapes in pen/pencil tool
     {SKETCH,                N_("Sketch"),                "sketch"},
     {VONKOCH,               N_("VonKoch"),               "vonkoch"},
     {KNOT,                  N_("Knot"),                  "knot"},
@@ -106,6 +107,9 @@ Effect::New(EffectType lpenr, LivePathEffectObject *lpeobj)
         case PATTERN_ALONG_PATH:
             neweffect = static_cast<Effect*> ( new LPEPatternAlongPath(lpeobj) );
             break;
+        case FREEHAND_SHAPE:
+            neweffect = static_cast<Effect*> ( new LPEFreehandShape(lpeobj) );
+            break;
         case BEND_PATH:
             neweffect = static_cast<Effect*> ( new LPEBendPath(lpeobj) );
             break;
index eced0a1941b22aaf915514379f7a41b5a23c2517..fbaf8f86719d5ff9d9b91af8b8a8ae8f95aa2413 100644 (file)
@@ -53,6 +53,7 @@ namespace LivePathEffect {
 enum EffectType {
     BEND_PATH = 0,
     PATTERN_ALONG_PATH,
+    FREEHAND_SHAPE,
     SKETCH,
     VONKOCH,
     KNOT,
index 2e10efe7a0b90bfb2de9ea7f2056dbfd8e063525..30ec589afc2162bd5625efd3f424028d46262b2d 100644 (file)
@@ -7,6 +7,7 @@
  */
 
 #include "live_effects/lpe-patternalongpath.h"
+#include "live_effects/lpeobject.h"
 #include "sp-shape.h"
 #include "display/curve.h"
 #include <libnr/n-art-bpath.h>
@@ -203,6 +204,9 @@ LPEPatternAlongPath::transform_multiply(Geom::Matrix const& postmul, bool set)
     Effect::transform_multiply(postmul, set);
 }
 
+LPEFreehandShape::LPEFreehandShape(LivePathEffectObject *lpeobject) : LPEPatternAlongPath(lpeobject)
+{
+}
 
 } // namespace LivePathEffect
 } /* namespace Inkscape */
index 341f9945426c06d11b48d63be45164ecea223a7d..10b9e8bc28f3eb7505e4026b77b00a65ccdff052 100644 (file)
@@ -51,7 +51,24 @@ private:
     LPEPatternAlongPath& operator=(const LPEPatternAlongPath&);
 };
 
+class LPEFreehandShape : public LPEPatternAlongPath {
+public:
+  LPEFreehandShape(LivePathEffectObject *lpeobject);
+  virtual ~LPEFreehandShape() {}
+};
+
 }; //namespace LivePathEffect
 }; //namespace Inkscape
 
 #endif
+
+/*
+  Local Variables:
+  mode:c++
+  c-file-style:"stroustrup"
+  c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
+  indent-tabs-mode:nil
+  fill-column:99
+  End:
+*/
+// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :
index 3cb022f5bd8a80fcdaf3cf2d0d910bf4a480aaf1..229ae326eddcff8c95bd3aa6514db2bdf081da5e 100644 (file)
@@ -574,6 +574,30 @@ bool sp_lpe_item_has_path_effect_recursive(SPLPEItem *lpeitem)
     }
 }
 
+Inkscape::LivePathEffect::Effect*
+sp_lpe_item_has_path_effect_of_type(SPLPEItem *lpeitem, int type)
+{
+    std::list<Inkscape::LivePathEffect::LPEObjectReference *>::iterator i;
+    for (i = lpeitem->path_effect_list->begin(); i != lpeitem->path_effect_list->end(); ++i) {
+        if ((*i)->lpeobject->lpe->effectType() == type) {
+            return (*i)->lpeobject->lpe;
+        }
+    }
+    return NULL;
+}
+
+/* Return false if the item is not a path or already has a shape applied */
+bool sp_lpe_item_can_accept_freehand_shape(SPLPEItem *lpeitem)
+{
+    if (!SP_IS_PATH(lpeitem))
+        return false;
+
+    if (sp_lpe_item_has_path_effect_of_type(lpeitem, Inkscape::LivePathEffect::FREEHAND_SHAPE))
+        return false;
+
+    return true;
+}
+
 void sp_lpe_item_edit_next_param_oncanvas(SPLPEItem *lpeitem, SPDesktop *dt)
 {
     Inkscape::LivePathEffect::LPEObjectReference *lperef = sp_lpe_item_get_current_lpereference(lpeitem);
index 54642f27b17a657c7f321bb2437d2b20c8f77bd8..c92616ccade3716657592892282e8c8f5f2df443 100644 (file)
@@ -68,6 +68,8 @@ void sp_lpe_item_down_current_path_effect(SPLPEItem *lpeitem);
 void sp_lpe_item_up_current_path_effect(SPLPEItem *lpeitem);
 bool sp_lpe_item_has_path_effect(SPLPEItem *lpeitem);
 bool sp_lpe_item_has_path_effect_recursive(SPLPEItem *lpeitem);
+Inkscape::LivePathEffect::Effect* sp_lpe_item_has_path_effect_of_type(SPLPEItem *lpeitem, int type);
+bool sp_lpe_item_can_accept_freehand_shape(SPLPEItem *lpeitem);
 void sp_lpe_item_edit_next_param_oncanvas(SPLPEItem *lpeitem, SPDesktop *dt);
 PathEffectList sp_lpe_item_get_effect_list(SPLPEItem *lpeitem);
 Inkscape::LivePathEffect::LPEObjectReference* sp_lpe_item_get_current_lpereference(SPLPEItem *lpeitem);