Project

General

Profile

Bug #3432 ยป hello.C

Koen Deforche, 10/17/2014 11:42 AM

 
/*
* Copyright (C) 2008 Emweb bvba, Heverlee, Belgium.
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/

#include <iostream>
#include <boost/thread.hpp>

#include <Wt/WBreak>
#include <Wt/WContainerWidget>
#include <Wt/WLineEdit>
#include <Wt/WPushButton>
#include <Wt/WText>
#include <Wt/WProgressBar>

#include <QApplication>

#include "HelloApplication.h"
#include "QtObject.h"

class BigWorkWidget : public Wt::WContainerWidget
{
public:
BigWorkWidget(Wt::WContainerWidget *parent)
: WContainerWidget(parent)
{
startButton_ = new Wt::WPushButton("Start", this);
startButton_->clicked().connect(startButton_, &Wt::WPushButton::disable);
startButton_->clicked().connect(this, &BigWorkWidget::startBigWork);
startButton_->setMargin(2);

progress_ = new Wt::WProgressBar(this);
progress_->setInline(false);
progress_->setMinimum(0);
progress_->setMaximum(20);
progress_->setMargin(2);
}

virtual ~BigWorkWidget() {
workThread_.join();
}

private:
Wt::WPushButton *startButton_;
Wt::WProgressBar *progress_;

boost::thread workThread_;

void startBigWork() {
Wt::WApplication *app = Wt::WApplication::instance();

// Enable server push
app->enableUpdates(true);

workThread_
= boost::thread(boost::bind(&BigWorkWidget::doBigWork, this, app));

progress_->setValue(0);
startButton_->setText("Busy...");
}

/*
* This function runs from another thread.
*
* From within this thread, we cannot use WApplication::instance(),
* since that use thread-local storage. We can only access
* WApplication::instance() after we have grabbed its update-lock.
*/
void doBigWork(Wt::WApplication *app)
{
for (unsigned i = 0; i < 20; ++i) {
// Do 50 ms of hard work.
boost::this_thread::sleep(boost::posix_time::milliseconds(50));

// Get the application update lock to update the user-interface
// with a progress indication.
Wt::WApplication::UpdateLock uiLock(app);
if (uiLock) {
progress_->setValue(i + 1);
app->triggerUpdate();
}
}

Wt::WApplication::UpdateLock uiLock(app);

if (uiLock) {
startButton_->enable();
startButton_->setText("Again!");

app->triggerUpdate();

// Disable server push
app->enableUpdates(false);
}
}
};

// Needed when using WQApplication with Qt eventloop = true
//#include <QApplication>

using namespace Wt;

Dictionary::Dictionary(const WEnvironment& env)
: WQApplication(env, true)
{
/*
* Note: do not create any Qt objects from here. Initialize your
* application from within the virtual create() method.
*/
}

void Dictionary::create()
{
setTitle("Hello world");

root()->addWidget(new WText("Your name, please ? "));
nameEdit_ = new WLineEdit(root());
nameEdit_->setFocus();

WPushButton *b = new WPushButton("Greet me.", root());
b->setMargin(5, Left);

root()->addWidget(new WBreak());

greeting_ = new WText(root());

b->clicked().connect(this, &Dictionary::propagateGreet);
nameEdit_->enterPressed().connect(this, &Dictionary::propagateGreet);

qtSender_ = new QtObject(this);
qtReceiver_ = new QtObject(this);

QObject::connect(qtSender_, SIGNAL(greet(const QString&)),
qtReceiver_, SLOT(doGreet(const QString&)));

new BigWorkWidget(root());
}

void Dictionary::destroy()
{
/*
* Note: Delete any Qt object from here.
*/
delete qtSender_;
delete qtReceiver_;
}

void Dictionary::propagateGreet()
{
qtSender_->passGreet(toQString(nameEdit_->text()));
}

void Dictionary::doGreet(const QString& qname)
{
greeting_->setText("Hello there, " + toWString(qname));
}

WApplication *createApplication(const WEnvironment& env)
{
return new Dictionary(env);
}

int main(int argc, char **argv)
{
// Needed for Qt's eventloop threads to work
QApplication app(argc, argv);

return WRun(argc, argv, &createApplication);
}

    (1-1/1)