Older/WebApplication/Dialog.cpp

121 lines
4.6 KiB
C++
Raw Permalink Normal View History

2024-10-31 23:30:34 +08:00
#include "Dialog.h"
2024-11-01 19:05:20 +08:00
#include <Wt/WLineEdit.h>
2024-10-31 23:30:34 +08:00
#include <Wt/WMessageBox.h>
#include <Wt/WPushButton.h>
Dialog::Dialog() {
Wt::WContainerWidget *textdiv = addWidget(std::make_unique<Wt::WContainerWidget>());
textdiv->setStyleClass("text");
Wt::WContainerWidget *buttons = addWidget(std::make_unique<Wt::WContainerWidget>());
buttons->setStyleClass("buttons");
Wt::WPushButton *button = buttons->addWidget(std::make_unique<Wt::WPushButton>("One liner"));
button->clicked().connect(this, &Dialog::messageBox1);
button = buttons->addWidget(std::make_unique<Wt::WPushButton>("Comfortable?"));
button->clicked().connect(this, &Dialog::messageBox2);
button = buttons->addWidget(std::make_unique<Wt::WPushButton>("Havoc!"));
button->clicked().connect(this, &Dialog::messageBox3);
button = buttons->addWidget(std::make_unique<Wt::WPushButton>("Discard"));
button->clicked().connect(this, &Dialog::messageBox4);
2024-11-01 19:05:20 +08:00
button = buttons->addWidget(std::make_unique<Wt::WPushButton>("Familiar"));
button->clicked().connect(this, &Dialog::custom);
2024-10-31 23:30:34 +08:00
textdiv = addWidget(std::make_unique<Wt::WContainerWidget>());
textdiv->setStyleClass("text");
m_status = textdiv->addWidget(std::make_unique<Wt::WText>("Go ahead..."));
}
void Dialog::setStatus(const Wt::WString &result) {
m_status->setText(result);
}
void Dialog::messageBox1() {
2024-11-21 00:23:59 +08:00
Wt::WMessageBox::show("信息", "这是一个对话框示例。", Wt::StandardButton::Ok);
2024-10-31 23:30:34 +08:00
setStatus("Ok'ed");
}
void Dialog::messageBox2() {
2024-11-21 00:09:54 +08:00
m_messageBox = std::make_unique<Wt::WMessageBox>("Question", "Are you getting comfortable?", Wt::Icon::Question,
2024-10-31 23:30:34 +08:00
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<Wt::WMessageBox>("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));
}
2024-11-01 19:05:20 +08:00
void Dialog::custom() {
Wt::WDialog dialog("个人信息");
dialog.setClosable(true);
dialog.setResizable(true);
dialog.rejectWhenEscapePressed(true);
dialog.contents()->addWidget(std::make_unique<Wt::WText>("请输入你的名字: "));
Wt::WLineEdit *edit = dialog.contents()->addWidget(std::make_unique<Wt::WLineEdit>());
Wt::WPushButton *ok = dialog.footer()->addWidget(std::make_unique<Wt::WPushButton>("确认"));
ok->setDefault(true);
edit->setFocus();
ok->clicked().connect(&dialog, &Wt::WDialog::accept);
if (dialog.exec() == Wt::DialogCode::Accepted) {
setStatus("欢迎, " + edit->text());
} else {
setStatus("Oh nevermind!");
}
}
2024-10-31 23:30:34 +08:00
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();
}