From 844b3f1ccd684cf1979db02b46a89c3c00fef3ec Mon Sep 17 00:00:00 2001 From: johanengelen Date: Tue, 15 Jan 2008 16:33:29 +0000 Subject: [PATCH] RegisteredWidget has been renamed to RegisteredWdg. This is a deprecated class, all that use it should be recoded and RegisteredWdg should be deleted as soon as possible. The *new* RegisteredWidget is a template class and should be subclassed (eventually) from Gtk::Widget, for example RegisteredWidget will be subclassed from Scalar, which is subclassed from Labelled, => Gtk::HBox => Gtk::Widget. This way, a RegisteredWidget can be directly used as Gtk::Widget, and gtk::manage works, etc! This all should solve the problem of having multiple widgets for the same parameters. --- src/live_effects/parameter/random.cpp | 43 +++++------- src/live_effects/parameter/random.h | 3 - src/ui/widget/registered-enums.h | 2 +- src/ui/widget/registered-widget.cpp | 78 ++++++++++++---------- src/ui/widget/registered-widget.h | 96 ++++++++++++++++++++------- 5 files changed, 132 insertions(+), 90 deletions(-) diff --git a/src/live_effects/parameter/random.cpp b/src/live_effects/parameter/random.cpp index 9ce4e41a9..850bb1a75 100644 --- a/src/live_effects/parameter/random.cpp +++ b/src/live_effects/parameter/random.cpp @@ -35,7 +35,6 @@ RandomParam::RandomParam( const Glib::ustring& label, const Glib::ustring& tip, min = -NR_HUGE; max = NR_HUGE; integer = false; - regrandom = NULL; defseed = default_seed; startseed = defseed; @@ -44,8 +43,6 @@ RandomParam::RandomParam( const Glib::ustring& label, const Glib::ustring& tip, RandomParam::~RandomParam() { - if (regrandom) - delete regrandom; } bool @@ -96,9 +93,6 @@ RandomParam::param_set_value(gdouble val, long newseed) startseed = setup_seed(newseed); seed = startseed; - - if (regrandom) - regrandom->setValue(value, startseed); } void @@ -106,20 +100,12 @@ RandomParam::param_set_range(gdouble min, gdouble max) { this->min = min; this->max = max; - if (regrandom) - regrandom->getR()->setRange(min, max); - - param_set_value(value, startseed); // reset value, to check whether it is in range } void RandomParam::param_make_integer(bool yes) { integer = yes; - if (regrandom) { - regrandom->getR()->setDigits(0); - regrandom->getR()->setIncrements(1, 10); - } } void @@ -132,19 +118,24 @@ RandomParam::resetRandomizer() Gtk::Widget * RandomParam::param_newWidget(Gtk::Tooltips * tooltips) { - // WIDGET TODO: This implementation is incorrect, it should create a *new* widget for the caller, not just return an already created widget - g_warning("RandomParam::param_newWidget still needs recoding to work with multiple document views"); - // TODO: add a button to set a different startseed - if (!regrandom) { - regrandom = new Inkscape::UI::Widget::RegisteredRandom(); - regrandom->init(param_label, param_tooltip, param_key, *param_wr, param_effect->getRepr(), param_effect->getSPDoc()); - regrandom->setValue(value, startseed); - if (integer) - param_make_integer(); - - regrandom->set_undo_parameters(SP_VERB_DIALOG_LIVE_PATH_EFFECT, _("Change random parameter")); + Inkscape::UI::Widget::RegisteredRandom* regrandom = Gtk::manage( + new Inkscape::UI::Widget::RegisteredRandom( param_label, + param_tooltip, + param_key, + *param_wr, + param_effect->getRepr(), + param_effect->getSPDoc() ) ); + + regrandom->setValue(value, startseed); + if (integer) { + regrandom->setDigits(0); + regrandom->setIncrements(1, 10); } - return dynamic_cast (regrandom->getR()); + regrandom->setRange(min, max); + + regrandom->set_undo_parameters(SP_VERB_DIALOG_LIVE_PATH_EFFECT, _("Change random parameter")); + + return dynamic_cast (regrandom); } RandomParam::operator gdouble() diff --git a/src/live_effects/parameter/random.h b/src/live_effects/parameter/random.h index 1b7d4cf16..6583ca556 100644 --- a/src/live_effects/parameter/random.h +++ b/src/live_effects/parameter/random.h @@ -15,7 +15,6 @@ #include <2geom/path.h> #include "ui/widget/registry.h" -#include "ui/widget/registered-widget.h" namespace Inkscape { @@ -59,8 +58,6 @@ protected: bool integer; gdouble defvalue; - Inkscape::UI::Widget::RegisteredRandom * regrandom; - private: long setup_seed(long); gdouble rand(); diff --git a/src/ui/widget/registered-enums.h b/src/ui/widget/registered-enums.h index 0a17aeca3..e3c72e821 100644 --- a/src/ui/widget/registered-enums.h +++ b/src/ui/widget/registered-enums.h @@ -19,7 +19,7 @@ namespace Inkscape { namespace UI { namespace Widget { -template class RegisteredEnum : public RegisteredWidget +template class RegisteredEnum : public RegisteredWdg { public: RegisteredEnum() { diff --git a/src/ui/widget/registered-widget.cpp b/src/ui/widget/registered-widget.cpp index 1364e5a2a..a54c89292 100644 --- a/src/ui/widget/registered-widget.cpp +++ b/src/ui/widget/registered-widget.cpp @@ -18,6 +18,7 @@ # include #endif +#include "registered-widget.h" #include "ui/widget/color-picker.h" #include "ui/widget/registry.h" @@ -36,7 +37,6 @@ #include "desktop-handles.h" #include "sp-namedview.h" -#include "registered-widget.h" #include "verbs.h" // for interruptability bug: @@ -51,8 +51,35 @@ namespace Widget { //--------------------------------------------------- + +template +void RegisteredWidget::write_to_xml(const char * svgstr) +{ + // Use local repr here. When repr is specified, use that one, but + // if repr==NULL, get the repr of namedview of active desktop. + Inkscape::XML::Node *local_repr = repr; + SPDocument *local_doc = doc; + if (!local_repr) { + // no repr specified, use active desktop's namedview's repr + SPDesktop* dt = SP_ACTIVE_DESKTOP; + local_repr = SP_OBJECT_REPR (sp_desktop_namedview(dt)); + local_doc = sp_desktop_document(dt); + } + + bool saved = sp_document_get_undo_sensitive (local_doc); + sp_document_set_undo_sensitive (local_doc, false); + if (!write_undo) local_repr->setAttribute(_key.c_str(), svgstr); + sp_document_set_undo_sensitive (local_doc, saved); + + local_doc->setModifiedSinceSave(); + if (write_undo) { + local_repr->setAttribute(_key.c_str(), svgstr); + sp_document_done (local_doc, event_type, event_description); + } +} + void -RegisteredWidget::write_to_xml(const char * svgstr) +RegisteredWdg::write_to_xml(const char * svgstr) { // Use local repr here. When repr is specified, use that one, but // if repr==NULL, get the repr of namedview of active desktop. @@ -586,56 +613,38 @@ RegisteredPoint::on_value_changed() * Registered RANDOM */ -RegisteredRandom::RegisteredRandom() -{ - _widget = NULL; -} - RegisteredRandom::~RegisteredRandom() { - if (_widget) - delete _widget; - _value_changed_connection.disconnect(); _reseeded_connection.disconnect(); } -void -RegisteredRandom::init ( const Glib::ustring& label, const Glib::ustring& tip, +RegisteredRandom::RegisteredRandom ( const Glib::ustring& label, const Glib::ustring& tip, const Glib::ustring& key, Registry& wr, Inkscape::XML::Node* repr_in, SPDocument * doc_in ) + : RegisteredWidget (label, tip) { init_parent(key, wr, repr_in, doc_in); - _widget = new Random (label, tip); - _widget->setRange (-1e6, 1e6); - _widget->setDigits (2); - _widget->setIncrements(0.1, 1.0); - _value_changed_connection = _widget->signal_value_changed().connect (sigc::mem_fun (*this, &RegisteredRandom::on_value_changed)); - _reseeded_connection = _widget->signal_reseeded.connect(sigc::mem_fun(*this, &RegisteredRandom::on_value_changed)); -} - -Random* -RegisteredRandom::getR() -{ - return _widget; + setRange (-1e6, 1e6); + setDigits (2); + setIncrements(0.1, 1.0); + _value_changed_connection = signal_value_changed().connect (sigc::mem_fun (*this, &RegisteredRandom::on_value_changed)); + _reseeded_connection = signal_reseeded.connect(sigc::mem_fun(*this, &RegisteredRandom::on_value_changed)); } void RegisteredRandom::setValue (double val, long startseed) { - if (!_widget) - return; - - _widget->setValue (val); - _widget->setStartSeed(startseed); + static_cast(this)->setValue (val); + setStartSeed(startseed); } void RegisteredRandom::on_value_changed() { - if (_widget->setProgrammatically) { - _widget->setProgrammatically = false; + if (setProgrammatically) { + setProgrammatically = false; return; } @@ -645,16 +654,15 @@ RegisteredRandom::on_value_changed() _wr->setUpdating (true); Inkscape::SVGOStringStream os; - os << _widget->getValue() << ';' << _widget->getStartSeed(); + os << getValue() << ';' << getStartSeed(); - _widget->set_sensitive(false); + set_sensitive(false); write_to_xml(os.str().c_str()); - _widget->set_sensitive(true); + set_sensitive(true); _wr->setUpdating (false); } - } // namespace Dialog } // namespace UI } // namespace Inkscape diff --git a/src/ui/widget/registered-widget.h b/src/ui/widget/registered-widget.h index 90fb44983..1a5501427 100644 --- a/src/ui/widget/registered-widget.h +++ b/src/ui/widget/registered-widget.h @@ -19,6 +19,8 @@ #include "xml/node.h" #include "registry.h" +#include "ui/widget/random.h" + class SPUnit; class SPDocument; @@ -39,9 +41,60 @@ class Scalar; class ScalarUnit; class UnitMenu; class Point; -class Random; -class RegisteredWidget { +template +class RegisteredWidget : public W { +public: + void set_undo_parameters(const unsigned int _event_type, Glib::ustring _event_description) + { + event_type = _event_type; + event_description = _event_description; + write_undo = true; + } + + bool is_updating() {if (_wr) return _wr->isUpdating(); else return false;} + +protected: + RegisteredWidget() : W() { construct(); } + template< typename A > + explicit RegisteredWidget( A& a ): W( a ) { construct(); } + template< typename A, typename B > + RegisteredWidget( A& a, B& b ): W( a, b ) { construct(); } + template< typename A, typename B, typename C > + RegisteredWidget( A& a, B& b, C& c ): W( a, b, c ) { construct(); } + + virtual ~RegisteredWidget() {}; + + void init_parent(const Glib::ustring& key, Registry& wr, Inkscape::XML::Node* repr_in, SPDocument *doc_in) + { + _wr = ≀ + _key = key; + repr = repr_in; + doc = doc_in; + if (repr && !doc) // doc cannot be NULL when repr is not NULL + g_warning("Initialization of registered widget using defined repr but with doc==NULL"); + } + + void write_to_xml(const char * svgstr); + + Registry * _wr; + Glib::ustring _key; + Inkscape::XML::Node * repr; + SPDocument * doc; + unsigned int event_type; + Glib::ustring event_description; + bool write_undo; + +private: + void construct() { + _wr = NULL; + repr = NULL; + doc = NULL; + write_undo = false; + } +}; + +class RegisteredWdg { public: void set_undo_parameters(const unsigned int _event_type, Glib::ustring _event_description) { @@ -53,7 +106,7 @@ public: bool is_updating() {if (_wr) return _wr->isUpdating(); else return false;} protected: - RegisteredWidget() + RegisteredWdg() { _wr = NULL; repr = NULL; @@ -84,7 +137,7 @@ protected: //####################################################### -class RegisteredCheckButton : public RegisteredWidget { +class RegisteredCheckButton : public RegisteredWdg { public: RegisteredCheckButton(); ~RegisteredCheckButton(); @@ -111,7 +164,7 @@ protected: void on_toggled(); }; -class RegisteredUnitMenu : public RegisteredWidget { +class RegisteredUnitMenu : public RegisteredWdg { public: RegisteredUnitMenu(); ~RegisteredUnitMenu(); @@ -134,7 +187,7 @@ protected: void on_changed(); }; -class RegisteredScalarUnit : public RegisteredWidget { +class RegisteredScalarUnit : public RegisteredWdg { public: RegisteredScalarUnit(); ~RegisteredScalarUnit(); @@ -162,7 +215,7 @@ protected: void on_value_changed(); }; -class RegisteredScalar : public RegisteredWidget { +class RegisteredScalar : public RegisteredWdg { public: RegisteredScalar(); ~RegisteredScalar(); @@ -187,7 +240,7 @@ protected: void on_value_changed(); }; -class RegisteredColorPicker : public RegisteredWidget { +class RegisteredColorPicker : public RegisteredWdg { public: RegisteredColorPicker(); ~RegisteredColorPicker(); @@ -219,7 +272,7 @@ protected: sigc::connection _changed_connection; }; -class RegisteredSuffixedInteger : public RegisteredWidget { +class RegisteredSuffixedInteger : public RegisteredWdg { public: RegisteredSuffixedInteger(); ~RegisteredSuffixedInteger(); @@ -249,7 +302,7 @@ protected: void on_value_changed(); }; -class RegisteredRadioButtonPair : public RegisteredWidget { +class RegisteredRadioButtonPair : public RegisteredWdg { public: RegisteredRadioButtonPair(); ~RegisteredRadioButtonPair(); @@ -283,7 +336,7 @@ protected: void on_value_changed(); }; -class RegisteredPoint : public RegisteredWidget { +class RegisteredPoint : public RegisteredWdg { public: RegisteredPoint(); ~RegisteredPoint(); @@ -309,27 +362,20 @@ protected: void on_value_changed(); }; -class RegisteredRandom : public RegisteredWidget { + +class RegisteredRandom : public RegisteredWidget { public: - RegisteredRandom(); - ~RegisteredRandom(); - void init (const Glib::ustring& label, - const Glib::ustring& tip, - const Glib::ustring& key, - Registry& wr, - Inkscape::XML::Node* repr_in, - SPDocument *doc_in); - inline void init ( const Glib::ustring& label, + virtual ~RegisteredRandom(); + RegisteredRandom ( const Glib::ustring& label, const Glib::ustring& tip, const Glib::ustring& key, - Registry& wr) - { init(label, tip, key, wr, NULL, NULL); }; + Registry& wr, + Inkscape::XML::Node* repr_in = NULL, + SPDocument *doc_in = NULL); - Random* getR(); void setValue (double val, long startseed); protected: - Random *_widget; sigc::connection _value_changed_connection; sigc::connection _reseeded_connection; void on_value_changed(); -- 2.30.2