gui2/edit_text: Add an option to disallow whitespace-only input

This commit is contained in:
Iris Morelle 2019-11-16 05:08:14 -03:00
parent b56bf27604
commit 78df5c305e
2 changed files with 39 additions and 3 deletions

View file

@ -16,7 +16,12 @@
#include "gui/dialogs/edit_text.hpp"
#include "gui/auxiliary/field.hpp"
#include "gui/widgets/button.hpp"
#include "gui/widgets/settings.hpp"
#include "gui/widgets/text_box.hpp"
#include <boost/algorithm/string/trim.hpp>
namespace gui2
{
@ -41,14 +46,36 @@ namespace dialogs
REGISTER_DIALOG(edit_text)
//TODO: add a way to disallow certain characters (like spaces or ")
edit_text::edit_text(const std::string& title,
const std::string& label,
std::string& text)
std::string& text,
bool disallow_empty)
: disallow_empty_(disallow_empty)
{
register_label("title", true, title, true);
register_label("label", true, label, true);
register_text("text", true, text, true);
}
void edit_text::pre_show(window& window)
{
if(disallow_empty_) {
text_box& text = find_widget<text_box>(&window, "text", false);
connect_signal_notify_modified(text, std::bind(&edit_text::on_text_change, this));
on_text_change();
}
}
void edit_text::on_text_change()
{
text_box& text = find_widget<text_box>(get_window(), "text", false);
button& ok_button = find_widget<button>(get_window(), "ok", false);
auto clean_value = text.get_value();
boost::trim(clean_value);
ok_button.set_active(!boost::trim_copy(text.get_value()).empty());
}
} // namespace dialogs
} // namespace gui2

View file

@ -34,10 +34,13 @@ public:
* - Output: The new unit name the user entered
* if the dialog returns @ref retval::OK,
* undefined otherwise.
* @param disallow_empty Whether to prevent the user from entering a string that is
* empty or consists only of whitespace.
*/
edit_text(const std::string& title,
const std::string& label,
std::string& text);
std::string& text,
bool disallow_empty = false);
/**
* Executes the dialog.
@ -54,6 +57,12 @@ public:
private:
/** Inherited from modal_dialog, implemented by REGISTER_DIALOG. */
virtual const std::string& window_id() const override;
virtual void pre_show(window& window) override;
void on_text_change();
bool disallow_empty_;
};
} // namespace dialogs
} // namespace gui2