From 3630721e6312ab4cb3d7ce76a09edfb75c60878c Mon Sep 17 00:00:00 2001 From: amass <168062547@qq.com> Date: Thu, 31 Oct 2024 23:30:34 +0800 Subject: [PATCH] add dialog example. --- WebApplication/CMakeLists.txt | 1 + WebApplication/Dialog.cpp | 95 +++++++++++++++++++++++++++++++++++ WebApplication/Dialog.h | 22 ++++++++ WebApplication/Hello.cpp | 3 ++ 4 files changed, 121 insertions(+) create mode 100644 WebApplication/Dialog.cpp create mode 100644 WebApplication/Dialog.h diff --git a/WebApplication/CMakeLists.txt b/WebApplication/CMakeLists.txt index cf6aa21..14c4d09 100644 --- a/WebApplication/CMakeLists.txt +++ b/WebApplication/CMakeLists.txt @@ -1,6 +1,7 @@ add_library(WebApplication WebApplication.h WebApplication.cpp Hello.h Hello.cpp + Dialog.h Dialog.cpp Session.h Session.cpp User.h User.cpp ) diff --git a/WebApplication/Dialog.cpp b/WebApplication/Dialog.cpp new file mode 100644 index 0000000..10f4122 --- /dev/null +++ b/WebApplication/Dialog.cpp @@ -0,0 +1,95 @@ +#include "Dialog.h" +#include +#include + +Dialog::Dialog() { + + Wt::WContainerWidget *textdiv = addWidget(std::make_unique()); + textdiv->setStyleClass("text"); + + Wt::WContainerWidget *buttons = addWidget(std::make_unique()); + buttons->setStyleClass("buttons"); + + Wt::WPushButton *button = buttons->addWidget(std::make_unique("One liner")); + button->clicked().connect(this, &Dialog::messageBox1); + + button = buttons->addWidget(std::make_unique("Comfortable?")); + button->clicked().connect(this, &Dialog::messageBox2); + + button = buttons->addWidget(std::make_unique("Havoc!")); + button->clicked().connect(this, &Dialog::messageBox3); + + button = buttons->addWidget(std::make_unique("Discard")); + button->clicked().connect(this, &Dialog::messageBox4); + + textdiv = addWidget(std::make_unique()); + textdiv->setStyleClass("text"); + m_status = textdiv->addWidget(std::make_unique("Go ahead...")); +} + +void Dialog::setStatus(const Wt::WString &result) { + m_status->setText(result); +} + +void Dialog::messageBox1() { + Wt::WMessageBox::show("信息", "这是一个对话框示例。", Wt::StandardButton::Ok); + setStatus("Ok'ed"); +} + +void Dialog::messageBox2() { + m_messageBox = std::make_unique("Question", "Are you getting comfortable ?", Wt::Icon::Question, + Wt::StandardButton::Yes | Wt::StandardButton::No | + Wt::StandardButton::Cancel); + + m_messageBox->buttonClicked().connect(this, &Dialog::messageBoxDone); + + m_messageBox->animateShow( + Wt::WAnimation(Wt::AnimationEffect::Pop | Wt::AnimationEffect::Fade, Wt::TimingFunction::Linear, 250)); +} + +void Dialog::messageBox3() { + Wt::StandardButton result = Wt::WMessageBox::show("Confirm", "About to wreak havoc... Continue ?", + Wt::StandardButton::Ok | Wt::StandardButton::Cancel, + Wt::WAnimation(Wt::AnimationEffect::SlideInFromTop)); + if (result == Wt::StandardButton::Ok) + setStatus("Wreaking havoc."); + else + setStatus("Cancelled!"); +} + +void Dialog::messageBox4() { + m_messageBox = std::make_unique("Warning!", + "Are you sure you want to continue?\n" + "You have unsaved changes.", + Wt::Icon::None, Wt::StandardButton::None); + + m_messageBox->addButton("Discard Modifications", Wt::StandardButton::Ok); + Wt::WPushButton *continueButton = m_messageBox->addButton("Cancel", Wt::StandardButton::Cancel); + m_messageBox->setDefaultButton(continueButton); + + m_messageBox->buttonClicked().connect(this, &Dialog::messageBoxDone); + + m_messageBox->setOffsets(0, Wt::Side::Bottom); + m_messageBox->animateShow(Wt::WAnimation(Wt::AnimationEffect::SlideInFromBottom | Wt::AnimationEffect::Fade, + Wt::TimingFunction::Linear, 250)); +} + +void Dialog::messageBoxDone(Wt::StandardButton result) { + switch (result) { + case Wt::StandardButton::Ok: + setStatus("Ok'ed"); + break; + case Wt::StandardButton::Cancel: + setStatus("Cancelled!"); + break; + case Wt::StandardButton::Yes: + setStatus("Me too!"); + break; + case Wt::StandardButton::No: + setStatus("Me neither!"); + break; + default: + setStatus("Unknown result?"); + } + m_messageBox.reset(); +} diff --git a/WebApplication/Dialog.h b/WebApplication/Dialog.h new file mode 100644 index 0000000..a4bba8b --- /dev/null +++ b/WebApplication/Dialog.h @@ -0,0 +1,22 @@ +#ifndef __DIALOG_H__ +#define __DIALOG_H__ + +#include + +class Dialog : public Wt::WContainerWidget { +public: + Dialog(); + +protected: + void setStatus(const Wt::WString &result); + void messageBox1(); + void messageBox2(); + void messageBox3(); + void messageBox4(); + void messageBoxDone(Wt::StandardButton result); + +private: + Wt::WText *m_status = nullptr; + std::unique_ptr m_messageBox; +}; +#endif // __DIALOG_H__ \ No newline at end of file diff --git a/WebApplication/Hello.cpp b/WebApplication/Hello.cpp index 86caca6..c9fb649 100644 --- a/WebApplication/Hello.cpp +++ b/WebApplication/Hello.cpp @@ -1,5 +1,6 @@ #include "Hello.h" #include "BoostLog.h" +#include "Dialog.h" #include "Session.h" #include #include @@ -61,6 +62,8 @@ Hello::Hello(const Wt::WEnvironment &env, bool embedded) : Wt::WApplication(env) authWidget->setRegistrationEnabled(true); authWidget->processEnvironment(); top->addWidget(std::move(authWidget)); + + top->addWidget(std::make_unique()); } Hello::~Hello() {