Code

add widget controls for LPE VectorParam. Hide these controls for LPE Hatches.
[inkscape.git] / src / live_effects / parameter / vector.cpp
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 #include "ui/widget/registered-widget.h"
17 #include "live_effects/effect.h"
18 #include "desktop.h"
20 namespace Inkscape {
22 namespace LivePathEffect {
24 VectorParam::VectorParam( const Glib::ustring& label, const Glib::ustring& tip,
25                         const Glib::ustring& key, Inkscape::UI::Widget::Registry* wr,
26                         Effect* effect, Geom::Point default_vector)
27     : Parameter(label, tip, key, wr, effect),
28       defvalue(default_vector),
29       origin(0.,0.),
30       vector(default_vector)
31 {
32     vec_knot_shape = SP_KNOT_SHAPE_DIAMOND;
33     vec_knot_mode  = SP_KNOT_MODE_XOR;
34     vec_knot_color = 0xffffb500;
35     ori_knot_shape = SP_KNOT_SHAPE_CIRCLE;
36     ori_knot_mode  = SP_KNOT_MODE_XOR;
37     ori_knot_color = 0xffffb500;
38 }
40 VectorParam::~VectorParam()
41 {
43 }
45 void
46 VectorParam::param_set_default()
47 {
48     setOrigin(Geom::Point(0.,0.));
49     setVector(defvalue);
50 }
52 bool
53 VectorParam::param_readSVGValue(const gchar * strvalue)
54 {
55     gchar ** strarray = g_strsplit(strvalue, ",", 4);
56     double val[4];
57     unsigned int i = 0;
58     while (strarray[i] && i < 4) {
59         if (sp_svg_number_read_d(strarray[i], &val[i]) != 0) {
60             i++;
61         } else {
62             break;
63         }
64     }
65     g_strfreev (strarray);
66     if (i == 4) {
67         setOrigin( Geom::Point(val[0], val[1]) );
68         setVector( Geom::Point(val[2], val[3]) );
69         return true;
70     }
71     return false;
72 }
74 gchar *
75 VectorParam::param_getSVGValue() const
76 {
77     Inkscape::SVGOStringStream os;
78     os << origin << " , " << vector;
79     gchar * str = g_strdup(os.str().c_str());
80     return str;
81 }
83 Gtk::Widget *
84 VectorParam::param_newWidget(Gtk::Tooltips * /*tooltips*/)
85 {
86     Inkscape::UI::Widget::RegisteredVector * pointwdg = Gtk::manage(
87         new Inkscape::UI::Widget::RegisteredVector( param_label,
88                                                     param_tooltip,
89                                                     param_key,
90                                                     *param_wr,
91                                                     param_effect->getRepr(),
92                                                     param_effect->getSPDoc() ) );
93     pointwdg->setPolarCoords();
94     pointwdg->setValue( vector, origin );
95     pointwdg->clearProgrammatically();
96     pointwdg->set_undo_parameters(SP_VERB_DIALOG_LIVE_PATH_EFFECT, _("Change vector parameter"));
98     Gtk::HBox * hbox = Gtk::manage( new Gtk::HBox() );
99     static_cast<Gtk::HBox*>(hbox)->pack_start(*pointwdg, true, true);
100     static_cast<Gtk::HBox*>(hbox)->show_all_children();
102     return dynamic_cast<Gtk::Widget *> (hbox);
105 void
106 VectorParam::set_and_write_new_values(Geom::Point const &new_origin, Geom::Point const &new_vector)
108     setValues(new_origin, new_vector);
109     gchar * str = param_getSVGValue();
110     param_write_to_repr(str);
111     g_free(str);
114 void
115 VectorParam::param_transform_multiply(Geom::Matrix const& postmul, bool /*set*/)
117         set_and_write_new_values( origin * postmul, vector * postmul.without_translation() );
121 void
122 VectorParam::set_vector_oncanvas_looks(SPKnotShapeType shape, SPKnotModeType mode, guint32 color)
124     vec_knot_shape = shape;
125     vec_knot_mode  = mode;
126     vec_knot_color = color;
129 void
130 VectorParam::set_origin_oncanvas_looks(SPKnotShapeType shape, SPKnotModeType mode, guint32 color)
132     ori_knot_shape = shape;
133     ori_knot_mode  = mode;
134     ori_knot_color = color;
137 class VectorParamKnotHolderEntity_Origin : public LPEKnotHolderEntity {
138 public:
139     VectorParamKnotHolderEntity_Origin(VectorParam *p) : param(p) { }
140     virtual ~VectorParamKnotHolderEntity_Origin() {}
142     virtual void knot_set(Geom::Point const &p, Geom::Point const &/*origin*/, guint /*state*/) {
143         Geom::Point const s = snap_knot_position(p);
144         param->setOrigin(s);
145         sp_lpe_item_update_patheffect(SP_LPE_ITEM(item), false, false);
146     };
147     virtual Geom::Point knot_get(){
148         return param->origin;
149     };
150     virtual void knot_click(guint /*state*/){
151         g_print ("This is the origin handle associated to parameter '%s'\n", param->param_key.c_str());
152     };
154 private:
155     VectorParam *param;
156 };
158 class VectorParamKnotHolderEntity_Vector : public LPEKnotHolderEntity {
159 public:
160     VectorParamKnotHolderEntity_Vector(VectorParam *p) : param(p) { }
161     virtual ~VectorParamKnotHolderEntity_Vector() {}
163     virtual void knot_set(Geom::Point const &p, Geom::Point const &/*origin*/, guint /*state*/) {
164         Geom::Point const s = p - param->origin;
165         /// @todo implement angle snapping when holding CTRL
166         param->setVector(s);
167         sp_lpe_item_update_patheffect(SP_LPE_ITEM(item), false, false);
168     };
169     virtual Geom::Point knot_get(){
170         return param->origin + param->vector;
171     };
172     virtual void knot_click(guint /*state*/){
173         g_print ("This is the vector handle associated to parameter '%s'\n", param->param_key.c_str());
174     };
176 private:
177     VectorParam *param;
178 };
180 void
181 VectorParam::addKnotHolderEntities(KnotHolder *knotholder, SPDesktop *desktop, SPItem *item)
183     VectorParamKnotHolderEntity_Origin *origin_e = new VectorParamKnotHolderEntity_Origin(this);
184     origin_e->create(desktop, item, knotholder, handleTip(), ori_knot_shape, ori_knot_mode, ori_knot_color);
185     knotholder->add(origin_e);
187     VectorParamKnotHolderEntity_Vector *vector_e = new VectorParamKnotHolderEntity_Vector(this);
188     vector_e->create(desktop, item, knotholder, handleTip(), vec_knot_shape, vec_knot_mode, vec_knot_color);
189     knotholder->add(vector_e);
192 } /* namespace LivePathEffect */
194 } /* namespace Inkscape */
196 /*
197   Local Variables:
198   mode:c++
199   c-file-style:"stroustrup"
200   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
201   indent-tabs-mode:nil
202   fill-column:99
203   End:
204 */
205 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :