Project

General

Profile

RE: WDialog behaviour in a mobile browser ยป testDialog.cpp

Mario Diethelm, 11/16/2024 02:06 PM

 
#include <Wt/WApplication.h>
#include <Wt/WBootstrap5Theme.h>
#include <Wt/WContainerWidget.h>
#include <Wt/WDialog.h>
#include <Wt/WPushButton.h>
#include <Wt/WResource.h>
#include <Wt/WTimer.h>

using namespace std;
using namespace Wt;

class WorkingClass : public Wt::WResource
{
public:
WorkingClass() :
workDialog(nullptr),
customMessage(nullptr),
workingTimer(nullptr)
{

if (!workDialog) {
workDialog = make_unique<WDialog>("Working dialog ...");
customMessage = workDialog->contents()->addWidget(make_unique<Wt::WText>());
customMessage->setMargin(30, Side::Top | Side::Left);
customMessage->setText("");
workDialog->setTitleBarEnabled(true);
workDialog->setModal(true);
workDialog->setResizable(false);
workDialog->setClosable(false);
workDialog->setHidden(true);
}
workingTimer = make_unique<WTimer>();
workingTimer->timeout().connect(this, &WorkingClass::workFinished);
workingTimer->setInterval(chrono::seconds(5));

}

Signal<> doneSignal;

void ShowDialog(std::string const& msg) {
if (workDialog) {
customMessage->setText(msg);
workDialog->setHidden(false);
}
}

void CloseDialog(void) {
if (workDialog)
workDialog->setHidden(true);
}

void DoingWork(void) {
workingTimer->start();
}

void handleRequest(Wt::Http::Request const &request, Wt::Http::Response &response)
{


}

private:
unique_ptr<WDialog> workDialog;
unique_ptr<WTimer> workingTimer;
WText* customMessage;

void workFinished(void)
{

doneSignal.emit();
}

};


class TestApplication : public WApplication {
public:
TestApplication(const WEnvironment& env)
: WApplication(env),
workingObject(nullptr),
someText(nullptr)
{
setTheme(std::make_shared<WBootstrap5Theme>());
root()->addNew<WBreak>();
root()->addNew<WBreak>();
someText = root()->addNew<WText>("Push the button to get the working class working...");
someText->setMargin(30, Side::Top | Side::Left);
root()->addNew<WBreak>();
root()->addNew<WBreak>();
auto workBtn = root()->addNew<WPushButton>("Push me for work.");
workBtn->setMargin(60, Side::Left);
workBtn->clicked().connect(this, &TestApplication::performWork);

}

private:
unique_ptr<WorkingClass> workingObject;
WText* someText;

void performWork()
{
if (!workingObject) {
workingObject = make_unique<WorkingClass>();
workingObject->doneSignal.connect(this, &TestApplication::doneWork);
}
workingObject->ShowDialog("my message for working time");
someText->setText("now performing the hard work...");
workingObject->DoingWork();

}

void doneWork()
{
someText->setText("hard work finished. Push the button again to get the working class working...");
if (workingObject)
workingObject->CloseDialog();
}

};


int main(int argc, char **argv)
{
return WRun(argc, argv, [](const WEnvironment& env) {
return make_unique<TestApplication>(env);
});
}
    (1-1/1)