diff options
| author | Joel Klinghed <the_jk@yahoo.com> | 2017-07-26 21:50:41 +0200 |
|---|---|---|
| committer | Joel Klinghed <the_jk@yahoo.com> | 2017-07-26 22:06:04 +0200 |
| commit | ba31faa55abedea506443df821e32aff93378c15 (patch) | |
| tree | ef192cd7b1c1fcc87ed8238353ea40c5f3e1dc32 | |
| parent | 629bd9dbd099af60812e80f851afb7902fc28c37 (diff) | |
Add GuiForm::Listener::changed which is called whenever a value is changed
| -rw-r--r-- | src/gui_form.hh | 1 | ||||
| -rw-r--r-- | src/gui_gtk.cc | 51 | ||||
| -rw-r--r-- | src/gui_qt.cc | 16 | ||||
| -rw-r--r-- | src/monitor-gui.cc | 9 |
4 files changed, 63 insertions, 14 deletions
diff --git a/src/gui_form.hh b/src/gui_form.hh index 972c781..c91ebce 100644 --- a/src/gui_form.hh +++ b/src/gui_form.hh @@ -14,6 +14,7 @@ public: public: virtual ~Listener() {} + virtual void changed(GuiForm* form, std::string const& id) = 0; virtual bool about_to_close(GuiForm* form) = 0; protected: diff --git a/src/gui_gtk.cc b/src/gui_gtk.cc index 7510f09..c7031a8 100644 --- a/src/gui_gtk.cc +++ b/src/gui_gtk.cc @@ -914,12 +914,12 @@ public: void add_string(std::string const& id, std::string const& label, std::string const& value, std::string const& description) override { - values_.emplace_back(new StringValue(id, label, description, value)); + values_.emplace_back(new StringValue(this, id, label, description, value)); } void add_number(std::string const& id, std::string const& label, uint64_t value, std::string const& description) override { - values_.emplace_back(new NumberValue(id, label, description, value)); + values_.emplace_back(new NumberValue(this, id, label, description, value)); } void add_file(std::string const& id, std::string const& label, @@ -927,7 +927,7 @@ public: std::string const& description, uint8_t flags, std::vector<Filter> const& filter) override { - values_.emplace_back(new FileValue(id, label, description, value, + values_.emplace_back(new FileValue(this, id, label, description, value, flags, filter)); } @@ -1027,6 +1027,8 @@ public: case STRING: gtk_entry_set_text(GTK_ENTRY(value->entry_), static_cast<StringValue*>(value.get())->value_.c_str()); + g_signal_connect(G_OBJECT(value->entry_), "changed", + G_CALLBACK(changed), value.get()); break; case NUMBER: { char tmp[20]; @@ -1035,6 +1037,8 @@ public: gtk_entry_set_text(GTK_ENTRY(value->entry_), tmp); gtk_entry_set_input_purpose(GTK_ENTRY(value->entry_), GTK_INPUT_PURPOSE_DIGITS); + g_signal_connect(G_OBJECT(value->entry_), "changed", + G_CALLBACK(changed), value.get()); break; } case FILE: { @@ -1045,6 +1049,8 @@ public: v->label_.c_str(), GTK_FILE_CHOOSER_ACTION_OPEN); v->chooser_ = GTK_FILE_CHOOSER(button); value->entry_ = button; + g_signal_connect(G_OBJECT(v->chooser_), "file-set", + G_CALLBACK(file_set), value.get()); } else { auto chooser = gtk_file_chooser_native_new( v->label_.c_str(), @@ -1057,6 +1063,8 @@ public: v->chooser_ = GTK_FILE_CHOOSER(chooser); gtk_editable_set_editable(GTK_EDITABLE(value->entry_), false); gtk_file_chooser_set_do_overwrite_confirmation(v->chooser_, true); + g_signal_connect(G_OBJECT(value->entry_), "changed", + G_CALLBACK(changed), value.get()); } if (!v->value_.empty()) { gtk_file_chooser_set_filename(v->chooser_, v->value_.c_str()); @@ -1148,15 +1156,16 @@ protected: }; struct Value { + GtkGuiForm* me_; Type const type_; std::string const id_; std::string const label_; std::string const description_; GtkWidget* entry_; - Value(Type type, std::string const& id, std::string const& label, - std::string const& description) - : type_(type), id_(id), label_(label), description_(description), + Value(GtkGuiForm* me, Type type, std::string const& id, + std::string const& label, std::string const& description) + : me_(me), type_(type), id_(id), label_(label), description_(description), entry_(nullptr) { } }; @@ -1164,18 +1173,18 @@ protected: struct StringValue : public Value { std::string value_; - StringValue(std::string const& id, std::string const& label, + StringValue(GtkGuiForm* me, std::string const& id, std::string const& label, std::string const& description, std::string const& value) - : Value(STRING, id, label, description), value_(value) { + : Value(me, STRING, id, label, description), value_(value) { } }; struct NumberValue : public Value { uint64_t value_; - NumberValue(std::string const& id, std::string const& label, + NumberValue(GtkGuiForm* me, std::string const& id, std::string const& label, std::string const& description, uint64_t value) - : Value(NUMBER, id, label, description), value_(value) { + : Value(me, NUMBER, id, label, description), value_(value) { } }; @@ -1185,10 +1194,10 @@ protected: std::vector<Filter> filter_; GtkFileChooser* chooser_; - FileValue(std::string const& id, std::string const& label, + FileValue(GtkGuiForm* me, std::string const& id, std::string const& label, std::string const& description, std::string const& value, uint8_t flags, std::vector<Filter> const& filter) - : Value(FILE, id, label, description), value_(value), + : Value(me, FILE, id, label, description), value_(value), flags_(flags), filter_(filter) { } }; @@ -1201,6 +1210,13 @@ protected: return true; } + void notify_changed(std::string const& id) { + auto it = observers_.notify(); + while (it.has_next()) { + it.next()->changed(this, id); + } + } + static bool get_number(GtkWidget* entry, uint64_t* number) { assert(entry && number); auto text = gtk_entry_get_text(GTK_ENTRY(entry)); @@ -1237,6 +1253,17 @@ protected: } } + static void changed(GtkEditable* UNUSED(editable), gpointer user_data) { + auto value = reinterpret_cast<Value*>(user_data); + value->me_->notify_changed(value->id_); + } + + static void file_set(GtkFileChooserButton* UNUSED(widget), + gpointer user_data) { + auto value = reinterpret_cast<Value*>(user_data); + value->me_->notify_changed(value->id_); + } + virtual gint add_extra_widgets(GtkGrid* UNUSED(grid), gint row, gint UNUSED(colspan)) { return row; diff --git a/src/gui_qt.cc b/src/gui_qt.cc index 3e7c696..cad859e 100644 --- a/src/gui_qt.cc +++ b/src/gui_qt.cc @@ -1021,13 +1021,18 @@ protected: static_cast<FileValue*>(value.get())->value_)); break; } + auto vp = value.get(); + edit->connect(edit, &QLineEdit::textChanged, + [=](QString const& UNUSED(text)) { + notify_changed(vp->id_); + }); layout->addWidget(label, row, 0); if (value->type_ == FILE) { auto button = new QPushButton("..."); edit->setReadOnly(true); dialog_->connect(button, &QPushButton::clicked, - [this,&value](bool UNUSED(checked)) { - showFileDialog(static_cast<FileValue*>(value.get())); + [=](bool UNUSED(checked)) { + showFileDialog(static_cast<FileValue*>(vp)); }); layout->addWidget(edit, row, 1); layout->addWidget(button, row, 2); @@ -1088,6 +1093,13 @@ protected: return true; } + void notify_changed(std::string const& id) { + auto it = observers_.notify(); + while (it.has_next()) { + it.next()->changed(this, id); + } + } + virtual int add_extra_widgets(QGridLayout*, int row, int UNUSED(columns)) { return row; } diff --git a/src/monitor-gui.cc b/src/monitor-gui.cc index 7b4f5f2..d293935 100644 --- a/src/monitor-gui.cc +++ b/src/monitor-gui.cc @@ -331,6 +331,9 @@ class MonitorGui : GuiMenu::Listener, GuiMain::Listener, Monitor::Delegate, private: class ConnectFormListener : public GuiFormApply::Listener { public: + void changed(GuiForm* UNUSED(form), std::string const& UNUSED(id)) override { + } + bool about_to_close(GuiForm* form) override { auto address = form->get_string("address"); if (address.empty()) { @@ -371,6 +374,9 @@ private: class SetupFormListener : public GuiFormApply::Listener { public: + void changed(GuiForm* UNUSED(form), std::string const& UNUSED(id)) override { + } + bool about_to_close(GuiForm* form) override { auto const& port = form->get_string("port"); if (port.empty()) { @@ -447,6 +453,9 @@ private: class GenerateFormListener : public GuiFormApply::Listener { public: + void changed(GuiForm* UNUSED(form), std::string const& UNUSED(id)) override { + } + bool about_to_close(GuiForm* form) override { auto const& output = form->get_file("output"); if (output.empty()) { |
