From 3fa3b4ba98d86dbdc252bf64faa82c426a1f2951 Mon Sep 17 00:00:00 2001 From: cilix42 Date: Thu, 31 Jul 2008 15:48:02 +0000 Subject: [PATCH] New unit parameter for LPEs which lpe-ruler now uses --- po/POTFILES.in | 3 + src/live_effects/lpe-ruler.cpp | 13 ++- src/live_effects/lpe-ruler.h | 2 + src/live_effects/parameter/Makefile_insert | 4 +- src/live_effects/parameter/unit.cpp | 94 ++++++++++++++++++++++ src/live_effects/parameter/unit.h | 51 ++++++++++++ 6 files changed, 164 insertions(+), 3 deletions(-) create mode 100644 src/live_effects/parameter/unit.cpp create mode 100644 src/live_effects/parameter/unit.h diff --git a/po/POTFILES.in b/po/POTFILES.in index 8c6c20c18..3bfdfc57d 100644 --- a/po/POTFILES.in +++ b/po/POTFILES.in @@ -208,6 +208,9 @@ src/live_effects/parameter/path.cpp src/live_effects/parameter/point.cpp src/live_effects/parameter/pointparam-knotholder.cpp src/live_effects/parameter/random.cpp +src/live_effects/parameter/random.cpp +src/live_effects/parameter/text.cpp +src/live_effects/parameter/unit.cpp src/main-cmdlineact.cpp src/main.cpp src/menus-skeleton.h diff --git a/src/live_effects/lpe-ruler.cpp b/src/live_effects/lpe-ruler.cpp index f41e752cb..8b457da58 100644 --- a/src/live_effects/lpe-ruler.cpp +++ b/src/live_effects/lpe-ruler.cpp @@ -27,12 +27,14 @@ LPERuler::LPERuler(LivePathEffectObject *lpeobject) : mark_distance(_("Mark distance"), _("Distance between ruler marks"), "mark_distance", &wr, this, 20), mark_length(_("Mark length"), _("Length of ruler marks"), "mark_length", &wr, this, 10), scale(_("Scale factor"), _("Scale factor for ruler distance (only affects on-canvas display of ruler length)"), "scale", &wr, this, 1.0), - info_text(_("Info text"), _("Parameter for text creation"), "info_text", &wr, this, "") + info_text(_("Info text"), _("Parameter for text creation"), "info_text", &wr, this, ""), + unit(_("Unit"), _("Unit"), "unit", &wr, this) { registerParameter(dynamic_cast(&mark_distance)); registerParameter(dynamic_cast(&mark_length)); registerParameter(dynamic_cast(&scale)); registerParameter(dynamic_cast(&info_text)); + registerParameter(dynamic_cast(&unit)); mark_distance.param_make_integer(); mark_length.param_make_integer(); @@ -88,14 +90,21 @@ LPERuler::doEffect_pwd2 (Geom::Piecewise > const & pwd2_i Point n(-rot90(dir) * mark_length); double length = L2(B - A); - gchar *dist = g_strdup_printf("%8.2f", length * scale); + /* convert the measured length to the correct unit ... */ + double lengthval = length * scale; + gboolean success = sp_convert_distance(&lengthval, &sp_unit_get_by_id(SP_UNIT_PX), unit); + + /* ... set it as the canvas text ... */ + gchar *dist = g_strdup_printf("%8.2f %s", lengthval, success ? unit.get_abbreviation() : "px"); info_text.param_setValue(dist); g_free(dist); + /* ... and adjust the text's position on canvas */ double angle = Geom::angle_between(dir, Geom::Point(1,0)); info_text.setPos((A + B) / 2 + 2.0 * n); info_text.setAnchor(std::sin(angle), -std::cos(angle)); + /* draw the actual ruler */ Point C, D; C = A - n; D = A + n; diff --git a/src/live_effects/lpe-ruler.h b/src/live_effects/lpe-ruler.h index 7ebc62fdb..48ca5d46f 100644 --- a/src/live_effects/lpe-ruler.h +++ b/src/live_effects/lpe-ruler.h @@ -17,6 +17,7 @@ #include "live_effects/effect.h" #include "live_effects/parameter/text.h" +#include "live_effects/parameter/unit.h" namespace Inkscape { namespace LivePathEffect { @@ -33,6 +34,7 @@ private: ScalarParam mark_length; ScalarParam scale; TextParamInternal info_text; + UnitParam unit; LPERuler(const LPERuler&); LPERuler& operator=(const LPERuler&); }; diff --git a/src/live_effects/parameter/Makefile_insert b/src/live_effects/parameter/Makefile_insert index 14bbc3285..a5bbe3e66 100644 --- a/src/live_effects/parameter/Makefile_insert +++ b/src/live_effects/parameter/Makefile_insert @@ -22,4 +22,6 @@ live_effects_parameter_liblpeparam_a_SOURCES = \ live_effects/parameter/path.cpp \ live_effects/parameter/path.h \ live_effects/parameter/text.cpp \ - live_effects/parameter/text.h + live_effects/parameter/text.h \ + live_effects/parameter/unit.cpp \ + live_effects/parameter/unit.h diff --git a/src/live_effects/parameter/unit.cpp b/src/live_effects/parameter/unit.cpp new file mode 100644 index 000000000..1ec15e337 --- /dev/null +++ b/src/live_effects/parameter/unit.cpp @@ -0,0 +1,94 @@ +#define INKSCAPE_LIVEPATHEFFECT_PARAMETER_UNIT_CPP + +/* + * Copyright (C) Maximilian Albert 2008 + * + * Released under GNU GPL, read the file 'COPYING' for more information + */ + +#include "live_effects/parameter/unit.h" +#include "live_effects/effect.h" +#include "ui/widget/registered-widget.h" + +namespace Inkscape { + +namespace LivePathEffect { + + +UnitParam::UnitParam( const Glib::ustring& label, const Glib::ustring& tip, + const Glib::ustring& key, Inkscape::UI::Widget::Registry* wr, + Effect* effect, SPUnitId default_value) + : Parameter(label, tip, key, wr, effect) +{ + defunit = &sp_unit_get_by_id(default_value);; + unit = defunit; +} + +UnitParam::~UnitParam() +{ +} + +bool +UnitParam::param_readSVGValue(const gchar * strvalue) +{ + SPUnit const *newval = sp_unit_get_by_abbreviation(strvalue); + if (newval) { + param_set_value(newval); + return true; + } + return false; +} + +gchar * +UnitParam::param_getSVGValue() const +{ + return g_strdup(sp_unit_get_abbreviation(unit)); +} + +void +UnitParam::param_set_default() +{ + param_set_value(defunit); +} + +void +UnitParam::param_set_value(SPUnit const *val) +{ + unit = val; +} + +const gchar * +UnitParam::get_abbreviation() +{ + return sp_unit_get_abbreviation(unit); +} + +Gtk::Widget * +UnitParam::param_newWidget(Gtk::Tooltips * /*tooltips*/) +{ + Inkscape::UI::Widget::RegisteredUnitMenu* unit_menu = Gtk::manage( + new Inkscape::UI::Widget::RegisteredUnitMenu(param_label, + param_key, + *param_wr, + param_effect->getRepr(), + param_effect->getSPDoc())); + + unit_menu->setUnit(unit); + unit_menu->set_undo_parameters(SP_VERB_DIALOG_LIVE_PATH_EFFECT, _("Change unit parameter")); + + return dynamic_cast (unit_menu); +} + +} /* namespace LivePathEffect */ +} /* namespace Inkscape */ + +/* + 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 : diff --git a/src/live_effects/parameter/unit.h b/src/live_effects/parameter/unit.h new file mode 100644 index 000000000..15192b074 --- /dev/null +++ b/src/live_effects/parameter/unit.h @@ -0,0 +1,51 @@ +#ifndef INKSCAPE_LIVEPATHEFFECT_PARAMETER_UNIT_H +#define INKSCAPE_LIVEPATHEFFECT_PARAMETER_UNIT_H + +/* + * Inkscape::LivePathEffectParameters + * +* Copyright (C) Maximilian Albert 2008 + * + * Released under GNU GPL, read the file 'COPYING' for more information + */ + +#include "live_effects/parameter/parameter.h" +#include + +namespace Inkscape { + +namespace LivePathEffect { + +class UnitParam : public Parameter { +public: + UnitParam(const Glib::ustring& label, + const Glib::ustring& tip, + const Glib::ustring& key, + Inkscape::UI::Widget::Registry* wr, + Effect* effect, + SPUnitId default_value = SP_UNIT_PX); + virtual ~UnitParam(); + + virtual bool param_readSVGValue(const gchar * strvalue); + virtual gchar * param_getSVGValue() const; + virtual void param_set_default(); + void param_set_value(SPUnit const *val); + const gchar *get_abbreviation(); + + virtual Gtk::Widget * param_newWidget(Gtk::Tooltips * tooltips); + + operator SPUnit const *() { return unit; } + +private: + SPUnit const *unit; + SPUnit const *defunit; + + UnitParam(const UnitParam&); + UnitParam& operator=(const UnitParam&); +}; + +} //namespace LivePathEffect + +} //namespace Inkscape + +#endif -- 2.30.2