Older/WebApplication/Dialog.cpp
amass 3e2cbf0179
All checks were successful
Deploy / Build (push) Successful in 3m21s
deploy res.
2024-11-21 00:23:59 +08:00

121 lines
4.6 KiB
C++

#include "Dialog.h"
#include <Wt/WLineEdit.h>
#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);
button = buttons->addWidget(std::make_unique<Wt::WPushButton>("Familiar"));
button->clicked().connect(this, &Dialog::custom);
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() {
Wt::WMessageBox::show("信息", "这是一个对话框示例。", Wt::StandardButton::Ok);
setStatus("Ok'ed");
}
void Dialog::messageBox2() {
m_messageBox = std::make_unique<Wt::WMessageBox>("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<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));
}
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!");
}
}
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();
}