Project

General

Profile

Support #2066 ยป main.cpp

Vitaly Volochay, 07/29/2013 05:41 PM

 
1
/* Copyright (c) 2013 Forward Systems. All rights reserved */
2
#include <QDebug>
3
#include <QImage>
4
#include <QPainter>
5
#include <Wt/WApplication>
6
#include <Wt/WResource>
7
#include <Wt/WServer>
8
#include <Wt/WCssTheme>
9
#include <Wt/WBootstrapTheme>
10
#include <Wt/WEnvironment>
11
#include <Wt/WFileResource>
12
#include <Wt/WImage>
13
#include <Wt/WContainerWidget>
14
#include <Wt/WMenu>
15
#include <Wt/WNavigationBar>
16
#include <Wt/WPainter>
17
#include <Wt/WPaintedWidget>
18
#include <Wt/WVBoxLayout>
19
#include <Wt/WHBoxLayout>
20
#include <Wt/WPushButton>
21
#include <WQApplication>
22

    
23
using namespace Wt;
24

    
25
class SomePaintedWidget: public WPaintedWidget
26
{
27
public:
28
	SomePaintedWidget(WContainerWidget* parent = 0);
29

    
30
protected:
31
	void paintEvent(WPaintDevice *paintDevice);
32
};
33

    
34
SomePaintedWidget::SomePaintedWidget(WContainerWidget *parent):
35
	WPaintedWidget(parent)
36
{
37
	resize(200, 200);
38
}
39

    
40
void
41
SomePaintedWidget::paintEvent(WPaintDevice *paintDevice)
42
{
43
	Wt::WPainter painter(paintDevice);
44
	painter.setRenderHint(Wt::WPainter::Antialiasing);
45

    
46
	painter.drawRect(Wt::WRectF(10, 10, 100, 100));
47
}
48

    
49
class TestApplication: public WQApplication
50
{
51
public:
52
	TestApplication(const WEnvironment& env);
53
	void create();
54
	void destroy() {}
55

    
56
private:
57
	void paintWithQt();
58
	void paintWithWt();
59

    
60
	WVBoxLayout* mainLayout;
61
	WPushButton* qtButton;
62
	WPushButton* wtButton;
63
};
64

    
65
TestApplication::TestApplication(const WEnvironment& env):
66
	WQApplication(env)
67
{
68

    
69
}
70

    
71
void
72
TestApplication::create()
73
{
74
	qtButton = new WPushButton("Paint with QPainter");
75
	qtButton->clicked().connect(this, &TestApplication::paintWithQt);
76
	wtButton = new WPushButton("Paint with WPainter");
77
	wtButton->clicked().connect(this, &TestApplication::paintWithWt);
78
	WHBoxLayout* buttonsLayout = new WHBoxLayout;
79
	buttonsLayout->addWidget(qtButton);
80
	buttonsLayout->addWidget(wtButton);
81
	buttonsLayout->addStretch();
82

    
83
	mainLayout = new WVBoxLayout(root());
84
	mainLayout->addLayout(buttonsLayout, 0, Wt::AlignTop);
85
	setlocale(LC_NUMERIC,"C");
86
}
87

    
88
void
89
TestApplication::paintWithQt()
90
{
91
	//qtButton->setEnabled(false);
92
	QImage image = QImage(QSize(100,100), QImage::Format_MonoLSB);
93
	image.fill(Qt::color0);
94

    
95
	QString imageName = "qimage.png";
96
	QPainter painter;
97
	painter.begin(&image);
98
	painter.setPen(QPen(Qt::color1));
99
	painter.drawRect(QRect(0,0,50,50));
100
	painter.end();
101
#warning comment next line and everything works
102
	image.save(imageName);
103

    
104
	WFileResource *imageFile = new WFileResource("image/png",
105
												 imageName.toStdString());
106
	WImage *wImage = new WImage(imageFile, "PNG version");
107
	mainLayout->addWidget(wImage);
108
}
109

    
110
void
111
TestApplication::paintWithWt()
112
{
113
	//wtButton->setEnabled(false);
114
	mainLayout->addWidget(new SomePaintedWidget);
115
}
116

    
117
WApplication*
118
createApplication(const WEnvironment& env)
119
{
120
	WApplication *app = new TestApplication(env);
121
	const std::string *theme = env.getParameter("theme");
122
	if (theme)
123
	{
124
		app->setTheme(new Wt::WCssTheme(*theme));
125
	}
126
	else
127
	{
128
		app->setTheme(new Wt::WBootstrapTheme(app));
129
	}
130

    
131
	return app;
132
}
133

    
134
int
135
main(int argc, char **argv)
136
{
137
	try
138
	{
139
		WServer server(argv[0]);
140
		server.setServerConfiguration(argc, argv, WTHTTP_CONFIGURATION);
141
		server.addEntryPoint(Application, createApplication);
142
		if (server.start())
143
		{
144
			WServer::waitForShutdown();
145
			server.stop();
146
		}
147
	}
148
	catch (WServer::Exception& e)
149
	{
150
		qDebug() << e.what();
151
		return 1;
152
	}
153

    
154
	return 0;
155
}
    (1-1/1)