Code

Add on-canvas editing of LPE PointParam.
[inkscape.git] / src / live_effects / parameter / point.cpp
1 #define INKSCAPE_LIVEPATHEFFECT_PARAMETER_POINT_CPP
3 /*
4  * Copyright (C) Johan Engelen 2007 <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/point.h"
10 #include "live_effects/parameter/pointparam-knotholder.h"
11 #include "live_effects/effect.h"
12 #include "svg/svg.h"
13 #include "svg/stringstream.h"
14 #include <gtkmm.h>
15 #include "ui/widget/point.h"
16 #include "widgets/icon.h"
17 #include "ui/widget/registered-widget.h"
18 #include "inkscape.h"
19 #include "verbs.h"
21 // needed for on-canvas editting:
22 #include "tools-switch.h"
23 #include "node-context.h"
24 #include "shape-editor.h"
25 #include "desktop.h"
26 #include "selection.h"
28 #define LPEPOINTPARAM_DEBUG // undefine to disable all on-canvas editing code for PointParam
30 namespace Inkscape {
32 namespace LivePathEffect {
34 PointParam::PointParam( const Glib::ustring& label, const Glib::ustring& tip,
35                         const Glib::ustring& key, Inkscape::UI::Widget::Registry* wr,
36                         Effect* effect, Geom::Point default_value )
37     : Geom::Point(default_value), Parameter(label, tip, key, wr, effect), defvalue(default_value)
38 {
39 #ifdef LPEPOINTPARAM_DEBUG
40     oncanvas_editable = true;
41 #endif
42 }
44 PointParam::~PointParam()
45 {
46 }
48 void
49 PointParam::param_set_default()
50 {
51     param_setValue(defvalue);
52 }
54 bool
55 PointParam::param_readSVGValue(const gchar * strvalue)
56 {
57     gchar ** strarray = g_strsplit(strvalue, ",", 2);
58     double newx, newy;
59     unsigned int success = sp_svg_number_read_d(strarray[0], &newx);
60     success += sp_svg_number_read_d(strarray[1], &newy);
61     g_strfreev (strarray);
62     if (success == 2) {
63         param_setValue( Geom::Point(newx, newy) );
64         return true;
65     }
66     return false;
67 }
69 gchar *
70 PointParam::param_writeSVGValue() const
71 {
72     Inkscape::SVGOStringStream os;
73     os << (*this)[0] << "," << (*this)[1];
74     gchar * str = g_strdup(os.str().c_str());
75     return str;
76 }
78 Gtk::Widget *
79 PointParam::param_newWidget(Gtk::Tooltips * tooltips)
80 {
81     Inkscape::UI::Widget::RegisteredPoint * pointwdg = Gtk::manage(
82         new Inkscape::UI::Widget::RegisteredPoint( param_label,
83                                                    param_tooltip,
84                                                    param_key,
85                                                    *param_wr,
86                                                    param_effect->getRepr(),
87                                                    param_effect->getSPDoc() ) );
88     pointwdg->setValue( (*this)[0], (*this)[1] );
89     pointwdg->clearProgrammatically();
90     pointwdg->set_undo_parameters(SP_VERB_DIALOG_LIVE_PATH_EFFECT, _("Change point parameter"));
92     Gtk::Widget*  pIcon = Gtk::manage( sp_icon_get_icon( "draw_node", Inkscape::ICON_SIZE_BUTTON) );
93     Gtk::Button * pButton = Gtk::manage(new Gtk::Button());
94     pButton->set_relief(Gtk::RELIEF_NONE);
95     pIcon->show();
96     pButton->add(*pIcon);
97     pButton->show();
98     pButton->signal_clicked().connect(sigc::mem_fun(*this, &PointParam::on_button_click));
99 #ifndef LPEPOINTPARAM_DEBUG
100     pButton->set_sensitive(false);
101 #endif
103     Gtk::HBox * hbox = Gtk::manage( new Gtk::HBox() );
104     static_cast<Gtk::HBox*>(hbox)->pack_start(*pButton, true, true);
105     static_cast<Gtk::HBox*>(hbox)->pack_start(*pointwdg, true, true);
106     static_cast<Gtk::HBox*>(hbox)->show_all_children();
108     tooltips->set_tip(*pButton, _("Edit on-canvas"));
110     return dynamic_cast<Gtk::Widget *> (hbox);
113 void
114 PointParam::param_setValue(Geom::Point newpoint)
116     *dynamic_cast<Geom::Point *>( this ) = newpoint;
119 void
120 PointParam::param_set_and_write_new_value (Geom::Point newpoint)
122     Inkscape::SVGOStringStream os;
123     os << newpoint[0] << "," << newpoint[1];
124     gchar * str = g_strdup(os.str().c_str());
125     param_write_to_repr(str);
126     g_free(str);
129 void
130 PointParam::param_editOncanvas(SPItem * item, SPDesktop * dt)
132     // If not already in nodecontext, goto it!
133     if (!tools_isactive(dt, TOOLS_NODES)) {
134         tools_switch_current(TOOLS_NODES);
135     }
137     PointParamKnotHolder * kh =  pointparam_knot_holder_new( dt, SP_OBJECT(param_effect->getLPEObj()), param_key.c_str(), item);
138     if (kh) {
139         pointparam_knot_holder_add_full(kh, * dynamic_cast<Geom::Point *>( this ), NULL, knot_shape, knot_mode, knot_color, param_getTooltip()->c_str() );
141         ShapeEditor * shape_editor = SP_NODE_CONTEXT( dt->event_context )->shape_editor;
142         shape_editor->set_knotholder(kh);
143     }
148 void
149 PointParam::param_transform_multiply(Geom::Matrix const& postmul, bool /*set*/)
151     param_set_and_write_new_value( (*this) * postmul );
155 void
156 PointParam::set_oncanvas_looks(SPKnotShapeType shape, SPKnotModeType mode, guint32 color)
158     knot_shape = shape;
159     knot_mode  = mode;
160     knot_color = color;
164 // CALLBACKS:
166 void
167 PointParam::on_button_click()
169     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
170     SPItem * item = sp_desktop_selection(desktop)->singleItem();
171     if (item != NULL) {
172         param_editOncanvas(item, desktop);
173     }
176 } /* namespace LivePathEffect */
178 } /* namespace Inkscape */
180 /*
181   Local Variables:
182   mode:c++
183   c-file-style:"stroustrup"
184   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
185   indent-tabs-mode:nil
186   fill-column:99
187   End:
188 */
189 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :