From 00bb1dc48e8b4f0e607adc1d60b456cac4fbd1e1 Mon Sep 17 00:00:00 2001 From: Joel Klinghed Date: Fri, 28 Jul 2017 23:31:36 +0200 Subject: Add GuiMessage, simple message dialog --- src/gui_gtk.cc | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/gui_message.hh | 38 +++++++++++++++++++++++++++++ src/gui_qt.cc | 63 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 172 insertions(+) create mode 100644 src/gui_message.hh diff --git a/src/gui_gtk.cc b/src/gui_gtk.cc index 0c251a9..062ac4c 100644 --- a/src/gui_gtk.cc +++ b/src/gui_gtk.cc @@ -16,6 +16,7 @@ #include "gui_listmodel.hh" #include "gui_main.hh" #include "gui_menu.hh" +#include "gui_message.hh" #include "gui_statusbar.hh" #include "gui_textwnd.hh" #include "looper.hh" @@ -1501,6 +1502,69 @@ private: bool applied_; }; +class GtkGuiMessage : public virtual GuiMessage, public GtkGuiWindow { +public: + GtkGuiMessage(Type type, std::string const& title, std::string const& text) + : type_(type), title_(title), text_(text), dialog_(nullptr) { + } + + ~GtkGuiMessage() override { + assert(!dialog_); + } + + void set_title(std::string const& title) override { + title_ = title; + GtkGuiWindow::set_title(title); + } + std::string const& title() const override { + return title_; + } + + void* impl() const override { + return dialog_; + } + + void add_listener(GuiMessage::Listener* listener) override { + observers_.insert(listener); + } + + void remove_listener(GuiMessage::Listener* listener) override { + observers_.erase(listener); + } + + void show(GuiWindow* parent) override { + if (dialog_) { + assert(false); + return; + } + GtkMessageType type = GTK_MESSAGE_OTHER; + switch (type_) { + case ERROR: + type = GTK_MESSAGE_ERROR; + break; + case OTHER: + break; + } + dialog_ = gtk_message_dialog_new( + reinterpret_cast(parent->impl()), + GTK_DIALOG_MODAL, + type, + GTK_BUTTONS_CLOSE, + "%s", + text_.c_str()); + gtk_dialog_run(GTK_DIALOG(dialog_)); + gtk_widget_destroy(dialog_); + dialog_ = nullptr; + } + +protected: + Type type_; + std::string title_; + std::string text_; + Observers observers_; + GtkWidget* dialog_; +}; + class GtkGuiTextWindow : public virtual GuiTextWindow, public GtkGuiWindow { public: GtkGuiTextWindow(std::string const& title, uint32_t width, uint32_t height, @@ -2139,6 +2203,13 @@ GuiFormApply* GuiFormApply::create(std::string const& title, return new GtkGuiFormApply(title, text, apply_button, delegate); } +// static +GuiMessage* GuiMessage::create(Type type, + std::string const& title, + std::string const& text) { + return new GtkGuiMessage(type, title, text); +} + // static Looper* GuiMain::createLooper() { return new GtkLooper(); diff --git a/src/gui_message.hh b/src/gui_message.hh new file mode 100644 index 0000000..c44cce1 --- /dev/null +++ b/src/gui_message.hh @@ -0,0 +1,38 @@ +// -*- mode: c++; c-basic-offset: 2; -*- + +#ifndef GUI_MESSAGE_HH +#define GUI_MESSAGE_HH + +#include +#include + +#include "gui_window.hh" + +class GuiMessage : public GuiWindow { +public: + class Listener : public GuiWindow::Listener { + public: + virtual ~Listener() {} + + protected: + Listener() {} + }; + + enum Type { + ERROR, + OTHER, + }; + + static GuiMessage* create(Type type, std::string const& title, + std::string const& text); + + virtual void add_listener(Listener* listener) = 0; + virtual void remove_listener(Listener* listener) = 0; + + virtual void show(GuiWindow* parent) = 0; + +protected: + GuiMessage() {} +}; + +#endif // GUI_MESSAGE_HH diff --git a/src/gui_qt.cc b/src/gui_qt.cc index 17eda8f..9ffdfc9 100644 --- a/src/gui_qt.cc +++ b/src/gui_qt.cc @@ -46,6 +46,7 @@ #include "gui_listmodel.hh" #include "gui_main.hh" #include "gui_menu.hh" +#include "gui_message.hh" #include "gui_statusbar.hh" #include "gui_textwnd.hh" #include "looper.hh" @@ -1334,6 +1335,61 @@ private: bool applied_; }; +class QtGuiMessage : public virtual GuiMessage, public QtGuiWindow { +public: + QtGuiMessage(Type type, std::string const& title, std::string const& text) + : type_(type), title_(title), text_(text) { + } + + void add_listener(GuiMessage::Listener* listener) override { + observers_.insert(listener); + } + + void remove_listener(GuiMessage::Listener* listener) override { + observers_.erase(listener); + } + + void set_title(std::string const& title) override { + title_ = title; + } + std::string const& title() const override { + return title_; + } + + void* impl() const override { + return QtGuiWindow::impl(); + } + + bool showWidget() override { + show(QApplication::activeWindow()); + return true; + } + + void show(GuiWindow* parent) override { + auto wnd = static_cast(parent->impl()); + show(wnd->widget()); + } + +protected: + void show(QWidget* parent) { + switch (type_) { + case ERROR: + QMessageBox::critical(parent, QString::fromStdString(title_), + QString::fromStdString(text_)); + break; + case OTHER: + QMessageBox::information(parent, QString::fromStdString(title_), + QString::fromStdString(text_)); + break; + } + } + + Type type_; + std::string title_; + std::string text_; + Observers observers_; +}; + class OptionalCloseWidget : public QWidget { public: class Delegate { @@ -1732,6 +1788,13 @@ GuiFormApply* GuiFormApply::create(std::string const& title, return new QtGuiFormApply(title, text, apply_button, delegate); } +// static +GuiMessage* GuiMessage::create(Type type, + std::string const& title, + std::string const& text) { + return new QtGuiMessage(type, title, text); +} + // static Looper* GuiMain::createLooper() { return new QtLooper(); -- cgit v1.2.3-70-g09d2