1 #define INKSCAPE_LIVEPATHEFFECT_PARAMETER_VECTOR_CPP
3 /*
4 * Copyright (C) Johan Engelen 2008 <j.b.c.engelen@utwente.nl>
5 *
6 * Released under GNU GPL, read the file 'COPYING' for more information
7 */
9 #include "live_effects/parameter/vector.h"
10 #include "sp-lpe-item.h"
11 #include "knotholder.h"
12 #include "svg/svg.h"
13 #include "svg/stringstream.h"
14 #include <gtkmm.h>
16 // needed for on-canvas editting:
17 class SPDesktop;
19 namespace Inkscape {
21 namespace LivePathEffect {
23 VectorParam::VectorParam( const Glib::ustring& label, const Glib::ustring& tip,
24 const Glib::ustring& key, Inkscape::UI::Widget::Registry* wr,
25 Effect* effect, Geom::Point default_vector)
26 : Parameter(label, tip, key, wr, effect),
27 defvalue(default_vector),
28 origin(0.,0.),
29 vector(default_vector)
30 {
31 vec_knot_shape = SP_KNOT_SHAPE_DIAMOND;
32 vec_knot_mode = SP_KNOT_MODE_XOR;
33 vec_knot_color = 0xffffb500;
34 ori_knot_shape = SP_KNOT_SHAPE_CIRCLE;
35 ori_knot_mode = SP_KNOT_MODE_XOR;
36 ori_knot_color = 0xffffb500;
37 }
39 VectorParam::~VectorParam()
40 {
42 }
44 void
45 VectorParam::param_set_default()
46 {
47 setOrigin(Geom::Point(0.,0.));
48 setVector(defvalue);
49 }
51 bool
52 VectorParam::param_readSVGValue(const gchar * strvalue)
53 {
54 gchar ** strarray = g_strsplit(strvalue, ",", 4);
55 double val[4];
56 unsigned int i = 0;
57 while (strarray[i] && i < 4) {
58 if (sp_svg_number_read_d(strarray[i], &val[i]) != 0) {
59 i++;
60 } else {
61 break;
62 }
63 }
64 g_strfreev (strarray);
65 if (i == 4) {
66 setOrigin( Geom::Point(val[0], val[1]) );
67 setVector( Geom::Point(val[2], val[3]) );
68 return true;
69 }
70 return false;
71 }
73 gchar *
74 VectorParam::param_getSVGValue() const
75 {
76 Inkscape::SVGOStringStream os;
77 os << origin << " , " << vector;
78 gchar * str = g_strdup(os.str().c_str());
79 return str;
80 }
82 Gtk::Widget *
83 VectorParam::param_newWidget(Gtk::Tooltips * /*tooltips*/)
84 {
85 /*
86 Inkscape::UI::Widget::RegisteredTransformedPoint * pointwdg = Gtk::manage(
87 new Inkscape::UI::Widget::RegisteredTransformedPoint( param_label,
88 param_tooltip,
89 param_key,
90 *param_wr,
91 param_effect->getRepr(),
92 param_effect->getSPDoc() ) );
93 // TODO: fix to get correct desktop (don't use SP_ACTIVE_DESKTOP)
94 SPDesktop *desktop = SP_ACTIVE_DESKTOP;
95 Geom::Matrix transf = desktop->doc2dt();
96 pointwdg->setTransform(transf);
97 pointwdg->setValue( *this );
98 pointwdg->clearProgrammatically();
99 pointwdg->set_undo_parameters(SP_VERB_DIALOG_LIVE_PATH_EFFECT, _("Change point parameter"));
101 Gtk::HBox * hbox = Gtk::manage( new Gtk::HBox() );
102 static_cast<Gtk::HBox*>(hbox)->pack_start(*pointwdg, true, true);
103 static_cast<Gtk::HBox*>(hbox)->show_all_children();
105 return dynamic_cast<Gtk::Widget *> (hbox);
106 */ return NULL;
107 }
109 void
110 VectorParam::set_and_write_new_values(Geom::Point const &new_origin, Geom::Point const &new_vector)
111 {
112 setValues(new_origin, new_vector);
113 gchar * str = param_getSVGValue();
114 param_write_to_repr(str);
115 g_free(str);
116 }
118 void
119 VectorParam::param_transform_multiply(Geom::Matrix const& postmul, bool /*set*/)
120 {
121 set_and_write_new_values( origin * postmul, vector * postmul.without_translation() );
122 }
125 void
126 VectorParam::set_vector_oncanvas_looks(SPKnotShapeType shape, SPKnotModeType mode, guint32 color)
127 {
128 vec_knot_shape = shape;
129 vec_knot_mode = mode;
130 vec_knot_color = color;
131 }
133 void
134 VectorParam::set_origin_oncanvas_looks(SPKnotShapeType shape, SPKnotModeType mode, guint32 color)
135 {
136 ori_knot_shape = shape;
137 ori_knot_mode = mode;
138 ori_knot_color = color;
139 }
141 class VectorParamKnotHolderEntity_Origin : public LPEKnotHolderEntity {
142 public:
143 VectorParamKnotHolderEntity_Origin(VectorParam *p) : param(p) { }
144 virtual ~VectorParamKnotHolderEntity_Origin() {}
146 virtual void knot_set(Geom::Point const &p, Geom::Point const &/*origin*/, guint /*state*/) {
147 Geom::Point const s = snap_knot_position(p);
148 param->setOrigin(s);
149 sp_lpe_item_update_patheffect(SP_LPE_ITEM(item), false, false);
150 };
151 virtual Geom::Point knot_get(){
152 return param->origin;
153 };
154 virtual void knot_click(guint /*state*/){
155 g_print ("This is the origin handle associated to parameter '%s'\n", param->param_key.c_str());
156 };
158 private:
159 VectorParam *param;
160 };
162 class VectorParamKnotHolderEntity_Vector : public LPEKnotHolderEntity {
163 public:
164 VectorParamKnotHolderEntity_Vector(VectorParam *p) : param(p) { }
165 virtual ~VectorParamKnotHolderEntity_Vector() {}
167 virtual void knot_set(Geom::Point const &p, Geom::Point const &/*origin*/, guint /*state*/) {
168 Geom::Point const s = p - param->origin;
169 /// @todo implement angle snapping when holding CTRL
170 param->setVector(s);
171 sp_lpe_item_update_patheffect(SP_LPE_ITEM(item), false, false);
172 };
173 virtual Geom::Point knot_get(){
174 return param->origin + param->vector;
175 };
176 virtual void knot_click(guint /*state*/){
177 g_print ("This is the vector handle associated to parameter '%s'\n", param->param_key.c_str());
178 };
180 private:
181 VectorParam *param;
182 };
184 void
185 VectorParam::addKnotHolderEntities(KnotHolder *knotholder, SPDesktop *desktop, SPItem *item)
186 {
187 VectorParamKnotHolderEntity_Origin *origin_e = new VectorParamKnotHolderEntity_Origin(this);
188 origin_e->create(desktop, item, knotholder, handleTip(), ori_knot_shape, ori_knot_mode, ori_knot_color);
189 knotholder->add(origin_e);
191 VectorParamKnotHolderEntity_Vector *vector_e = new VectorParamKnotHolderEntity_Vector(this);
192 vector_e->create(desktop, item, knotholder, handleTip(), vec_knot_shape, vec_knot_mode, vec_knot_color);
193 knotholder->add(vector_e);
194 }
196 } /* namespace LivePathEffect */
198 } /* namespace Inkscape */
200 /*
201 Local Variables:
202 mode:c++
203 c-file-style:"stroustrup"
204 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
205 indent-tabs-mode:nil
206 fill-column:99
207 End:
208 */
209 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :