From d7de7fe9427df19497b336dd4a5eca61055bd437 Mon Sep 17 00:00:00 2001 From: Joel Klinghed Date: Mon, 24 Jul 2017 20:58:16 +0200 Subject: Add description to GuiForm fields And improve GTK implementation of GuiForm to use GtkGrid instead of GtkBox --- src/gui_form.hh | 6 ++++-- src/gui_gtk.cc | 61 +++++++++++++++++++++++++++++++++++------------------- src/gui_qt.cc | 30 ++++++++++++++++++++------- src/monitor-gui.cc | 15 ++++++++------ 4 files changed, 75 insertions(+), 37 deletions(-) (limited to 'src') diff --git a/src/gui_form.hh b/src/gui_form.hh index 95ec221..4ba5f15 100644 --- a/src/gui_form.hh +++ b/src/gui_form.hh @@ -22,9 +22,11 @@ public: static GuiForm* create(std::string const& title, std::string const& text); virtual void add_string(std::string const& id, std::string const& label, - std::string const& value) = 0; + std::string const& value, + std::string const& description) = 0; virtual void add_number(std::string const& id, std::string const& label, - uint64_t value) = 0; + uint64_t value, + std::string const& description) = 0; virtual void add_listener(Listener* listener) = 0; virtual void remove_listener(Listener* listener) = 0; diff --git a/src/gui_gtk.cc b/src/gui_gtk.cc index 6afec5d..14c1f63 100644 --- a/src/gui_gtk.cc +++ b/src/gui_gtk.cc @@ -909,13 +909,14 @@ public: } void add_string(std::string const& id, std::string const& label, - std::string const& value) override { - values_.emplace_back(new StringValue(id, label, value)); + std::string const& value, + std::string const& description) override { + values_.emplace_back(new StringValue(id, label, description, value)); } void add_number(std::string const& id, std::string const& label, - uint64_t value) override { - values_.emplace_back(new NumberValue(id, label, value)); + uint64_t value, std::string const& description) override { + values_.emplace_back(new NumberValue(id, label, description, value)); } std::string get_string(std::string const& id) const override { @@ -975,10 +976,15 @@ public: GTK_RESPONSE_REJECT, nullptr); auto content_area = gtk_dialog_get_content_area(GTK_DIALOG(dialog_)); - auto spacing = 5; + auto grid = gtk_grid_new(); + gtk_grid_set_column_spacing(GTK_GRID(grid), 5); + gtk_grid_set_row_spacing(GTK_GRID(grid), 5); + gint row = 0; if (!text_.empty()) { auto label = gtk_label_new(text_.c_str()); - gtk_box_pack_start(GTK_BOX(content_area), label, true, true, spacing); + gtk_label_set_width_chars(GTK_LABEL(label), 35); + gtk_label_set_line_wrap(GTK_LABEL(label), true); + gtk_grid_attach(GTK_GRID(grid), label, 0, row++, 2, 1); } for (auto& value : values_) { auto label = gtk_label_new(value->label_.c_str()); @@ -999,16 +1005,24 @@ public: break; } } - auto box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, spacing); - gtk_box_pack_start(GTK_BOX(box), label, false, false, 0); - gtk_box_pack_start(GTK_BOX(box), value->entry_, true, true, 0); - gtk_box_pack_start(GTK_BOX(content_area), box, false, false, spacing); + gtk_grid_attach(GTK_GRID(grid), label, 0, row, 1, 1); + gtk_grid_attach(GTK_GRID(grid), value->entry_, 1, row++, 1, 1); + if (!value->description_.empty()) { + auto desc = gtk_label_new(NULL); + gtk_label_set_markup(GTK_LABEL(desc), + ("" + value->description_ + "").c_str()); + gtk_widget_set_halign(GTK_WIDGET(desc), GTK_ALIGN_END); + gtk_label_set_line_wrap(GTK_LABEL(desc), true); + gtk_label_set_max_width_chars(GTK_LABEL(desc), 35); + gtk_grid_attach(GTK_GRID(grid), desc, 0, row++, 2, 1); + } } error_ = gtk_label_new(""); gtk_label_set_xalign(GTK_LABEL(error_), 0.0); - gtk_box_pack_start(GTK_BOX(content_area), error_, true, true, spacing); + gtk_grid_attach(GTK_GRID(grid), error_, 0, row++, 2, 1); + gtk_container_add(GTK_CONTAINER(content_area), grid); gtk_widget_show_all(dialog_); - add_extra_widgets(GTK_BOX(content_area), spacing); + row = add_extra_widgets(GTK_GRID(grid), row); gtk_widget_hide(error_); gtk_dialog_set_default_response(GTK_DIALOG(dialog_), GTK_RESPONSE_ACCEPT); gint result; @@ -1047,10 +1061,13 @@ protected: 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) - : type_(type), id_(id), label_(label), entry_(nullptr) { + Value(Type type, std::string const& id, std::string const& label, + std::string const& description) + : type_(type), id_(id), label_(label), description_(description), + entry_(nullptr) { } }; @@ -1058,8 +1075,8 @@ protected: std::string value_; StringValue(std::string const& id, std::string const& label, - std::string const& value) - : Value(STRING, id, label), value_(value) { + std::string const& description, std::string const& value) + : Value(STRING, id, label, description), value_(value) { } }; @@ -1067,8 +1084,8 @@ protected: uint64_t value_; NumberValue(std::string const& id, std::string const& label, - uint64_t value) - : Value(NUMBER, id, label), value_(value) { + std::string const& description, uint64_t value) + : Value(NUMBER, id, label, description), value_(value) { } }; @@ -1093,7 +1110,8 @@ protected: return false; } - virtual void add_extra_widgets(GtkBox*, gint) { + virtual gint add_extra_widgets(GtkGrid* UNUSED(grid), gint row) { + return row; } virtual void reset_extra_widgets() { @@ -1139,10 +1157,11 @@ public: } private: - void add_extra_widgets(GtkBox* content_area, gint spacing) override { + gint add_extra_widgets(GtkGrid* layout, gint row) override { assert(!spinner_); spinner_ = gtk_spinner_new(); - gtk_box_pack_start(content_area, spinner_, true, true, spacing); + gtk_grid_attach(layout, spinner_, 0, row++, 2, 1); + return row; } void reset_extra_widgets() override { diff --git a/src/gui_qt.cc b/src/gui_qt.cc index cc2ad0d..596ddda 100644 --- a/src/gui_qt.cc +++ b/src/gui_qt.cc @@ -839,13 +839,15 @@ public: } void add_string(std::string const& id, std::string const& label, - std::string const& value) override { - values_.emplace_back(new StringValue(id, label, value)); + std::string const& value, + std::string const& description) override { + values_.emplace_back(new StringValue(id, label, description, value)); } void add_number(std::string const& id, std::string const& label, - uint64_t value) override { - values_.emplace_back(new NumberValue(id, label, value)); + uint64_t value, + std::string const& description) override { + values_.emplace_back(new NumberValue(id, label, description, value)); } std::string get_string(std::string const& id) const override { @@ -904,10 +906,13 @@ protected: Type const type_; std::string const id_; std::string const label_; + std::string const description_; QLineEdit* edit_; - Value(Type type, std::string const& id, std::string const& label) - : type_(type), id_(id), label_(label), edit_(nullptr) { + Value(Type type, std::string const& id, std::string const& label, + std::string const& description) + : type_(type), id_(id), label_(label), description_(description), + edit_(nullptr) { } }; @@ -915,8 +920,9 @@ protected: std::string value_; StringValue(std::string const& id, std::string const& label, + std::string const& description, std::string const& value) - : Value(STRING, id, label), value_(value) { + : Value(STRING, id, label, description), value_(value) { } }; @@ -924,8 +930,9 @@ protected: uint64_t value_; NumberValue(std::string const& id, std::string const& label, + std::string const& description, uint64_t value) - : Value(NUMBER, id, label), value_(value) { + : Value(NUMBER, id, label, description), value_(value) { } }; @@ -972,6 +979,13 @@ protected: layout->addWidget(label, row, 0); layout->addWidget(edit, row, 1); ++row; + if (!value->description_.empty()) { + auto desc = new QLabel( + QString::fromStdString("" + value->description_ + "")); + desc->setAlignment(Qt::AlignTrailing); + desc->setWordWrap(true); + layout->addWidget(desc, row++, 0, 1, 2); + } } error_ = new QLabel(); layout->addWidget(error_, row++, 0, 1, 2); diff --git a/src/monitor-gui.cc b/src/monitor-gui.cc index fa43169..806f8da 100644 --- a/src/monitor-gui.cc +++ b/src/monitor-gui.cc @@ -503,14 +503,16 @@ public: new SetupFormListener()); connect_.reset( GuiFormApply::create("Setup...", - "Setup a proxy to start monitoring traffic." - " Leave bind empty to listen on all interfaces.", + "Setup a proxy to start monitoring traffic.", "Setup", dlg.get())); connect_->add_string("bind", "Address", - main_->config()->get("bind", "")); - connect_->add_string("port", "port", - main_->config()->get("port", "8080")); + main_->config()->get("bind", ""), + "Address to listen for proxy connections on." + " Leave empty to listen on all interfaces."); + connect_->add_string("port", "Port", + main_->config()->get("port", "8080"), + "Port to listen for proxy connections on."); connect_->add_listener(lst.get()); if (connect_->show(main_.get())) { monitor_->attach(); @@ -532,7 +534,8 @@ public: "Connect", dlg.get())); connect_->add_string("address", "Address", - main_->config()->get("connect", "")); + main_->config()->get("connect", ""), + "Host and optional port (defaults to 9000)"); connect_->add_listener(lst.get()); if (connect_->show(main_.get())) { monitor_->attach(); -- cgit v1.2.3-70-g09d2