Project

General

Profile

Bug #8051 » hello.cpp

Harald Elmer, 02/10/2021 04:15 PM

 
1
/*
2
* Copyright (C) 2008 Emweb bvba, Heverlee, Belgium.
3
*
4
* See the LICENSE file for terms of use.
5
*/
6

    
7
#include <Wt/WApplication.h>
8
#include <Wt/WBreak.h>
9
#include <Wt/WContainerWidget.h>
10
#include <Wt/WLineEdit.h>
11
#include <Wt/WPushButton.h>
12
#include <Wt/WText.h>
13
#include <Wt/WStackedWidget.h>
14
#include <Wt/WHBoxLayout.h>
15
#include <Wt/WLength.h>
16
#include <Wt/WStandardItemModel.h>
17
#include <Wt/Chart/WCartesianChart.h>
18
#include <Wt/WFlags.h>
19
#include <Wt/WResource.h>
20
#include <Wt/WPainter.h>
21
#include <Wt/WPdfImage.h>
22
#include <Wt/WResource.h>
23
#include <Wt/Http/Request.h>
24
#include <Wt/Http/Response.h>
25
#include <Wt/Render/WPdfRenderer.h>
26
#include <hpdf.h>
27
#include <Wt/WVBoxLayout.h>
28
#include <Wt/WDatePicker.h>
29

    
30
using namespace Wt;
31
using namespace Wt::Chart;
32

    
33
namespace {
34
	void HPDF_STDCALL error_handler(HPDF_STATUS error_no, HPDF_STATUS detail_no, void *user_data)
35
	{
36
		fprintf(stderr, "libharu error: error_no=%04X, detail_no=%d\n",
37
			(unsigned int)error_no, (int)detail_no);
38
	}
39
}
40

    
41
class ReportResource : public Wt::WResource
42
{
43
public:
44
	ReportResource();
45
	virtual ~ReportResource();
46

    
47
	virtual void handleRequest(const Wt::Http::Request& request,
48
		Wt::Http::Response& response);
49

    
50
};
51

    
52
//GLobal pointer for test
53
WCartesianChart *chart = NULL;
54

    
55
class HelloApplication : public Wt::WApplication
56
{
57
public:
58
	HelloApplication(const Wt::WEnvironment& env);
59

    
60
private:
61

    
62

    
63

    
64
};
65

    
66

    
67
HelloApplication::HelloApplication(const Wt::WEnvironment& env)
68
	: WApplication(env)
69
{
70

    
71

    
72
	setTitle("Hello world");                 
73

    
74
	//Main Container
75
	Wt::WContainerWidget *pCont1 = root()->addWidget(Wt::cpp14::make_unique<Wt::WContainerWidget>()); 
76
	pCont1->setWidth(Wt::WLength(100, Wt::WLength::Unit::Percentage));
77
	pCont1->setHeight(Wt::WLength(100, Wt::WLength::Unit::Percentage));
78
	pCont1->decorationStyle().setBackgroundColor(Wt::WColor("rgb(192,192,192)"));
79

    
80
	Wt::WVBoxLayout *pLayout = pCont1->setLayout(Wt::cpp14::make_unique<WVBoxLayout>());
81

    
82
	//Create Test-Model
83
	std::shared_ptr<WStandardItemModel> model
84
		= std::make_shared<WStandardItemModel>();
85

    
86
	//headers
87
	model->insertColumns(model->columnCount(), 2);
88
	model->setHeaderData(0, WString("Day"));
89
	model->setHeaderData(1, WString("Sales"));
90

    
91
	//data
92
	model->insertRows(model->rowCount(), 6);
93
	int row = 0;
94
	model->setData(row, 0, 1);
95
	model->setData(row, 1, 120);
96
	row++;
97
	model->setData(row, 0, 2);
98
	model->setData(row, 1, 30);
99
	row++;
100
	model->setData(row, 0, 3);
101
	model->setData(row, 1, 260);
102
	row++;
103
	model->setData(row, 0, 4);
104
	model->setData(row, 1, 160);
105
	row++;
106
	model->setData(row, 0, 5);
107
	model->setData(row, 1, 40);
108
	row++;
109
	model->setData(row, 0, 6);
110
	model->setData(row, 1, 120);
111
	row++;
112

    
113
	//Create Category Chart
114
	chart = pLayout->addWidget(cpp14::make_unique<WCartesianChart>());
115
	chart->setModel(model);
116
	chart->setXSeriesColumn(0);
117
	chart->setZoomEnabled(true);
118
	chart->setBackground(WColor(200, 200, 200));
119

    
120
	Wt::Chart::WheelActions actions;
121
	WFlags<KeyboardModifier> fl(KeyboardModifier::None);
122
	actions.insert(std::pair< WFlags<KeyboardModifier>, Wt::Chart::InteractiveAction>(fl, (Wt::Chart::InteractiveAction::ZoomX)));
123
	chart->setWheelActions(actions);
124
	
125
	for (int i = 1; i < model->columnCount(); ++i) 
126
	{
127
		std::unique_ptr<WDataSeries> s
128
			= cpp14::make_unique<WDataSeries>(i, SeriesType::Bar);
129
		s->setShadow(WShadow(3, 3, WColor(0, 0, 0, 127), 3));
130
		chart->addSeries(std::move(s));
131
	}
132
	chart->resize(800, 400);
133

    
134
	WPushButton *btn_pdf = pLayout->addWidget(cpp14::make_unique<WPushButton>("Print Pdf"));
135
	auto r = std::make_shared<ReportResource>();
136
	btn_pdf->setLink(WLink(r));
137
	btn_pdf->resize("120px", "30px");
138

    
139
	Wt::WDatePicker *picker = pLayout->addWidget(cpp14::make_unique<WDatePicker>());
140

    
141
}
142

    
143
//Report Resource
144
ReportResource::ReportResource() : WResource()
145
{
146
	suggestFileName("report.pdf");
147
}
148

    
149
ReportResource::~ReportResource()
150
{
151
	beingDeleted();
152
}
153

    
154
void ReportResource::handleRequest(const Wt::Http::Request& request, Wt::Http::Response& response)
155
{
156
	response.setMimeType("application/pdf");
157
	HPDF_Doc pdf = HPDF_New(error_handler, 0);
158

    
159
	// Note: UTF-8 encoding (for TrueType fonts) is only available since libharu 2.3.0 !
160
	HPDF_UseUTFEncodings(pdf);
161

    
162
	HPDF_Page page = HPDF_AddPage(pdf);
163
	HPDF_Page_SetSize(page, HPDF_PAGE_SIZE_A4, HPDF_PAGE_PORTRAIT);
164

    
165
	Wt::Render::WPdfRenderer renderer(pdf, page);
166
	renderer.setMargin(2.54);
167
	renderer.setDpi(96);
168
	renderer.render(Wt::WString("<html><b>Hallo pdf</b></html>"));
169

    
170
	if (chart)
171
	{
172
		WPdfImage pdfImage(pdf, page, 0, 0, 600, 400);
173
		{
174
			WPainter p(&pdfImage);
175
			chart->paint(p);
176
		}
177
	}
178

    
179
	HPDF_SaveToStream(pdf);
180
	unsigned int size = HPDF_GetStreamSize(pdf);
181
	HPDF_BYTE *buf = new HPDF_BYTE[size];
182
	HPDF_ReadFromStream(pdf, buf, &size);
183
	HPDF_Free(pdf);
184
	response.out().write((char*)buf, size);
185
	delete[] buf;
186
}
187

    
188

    
189

    
190
int main(int argc, char **argv)
191
{
192
	return Wt::WRun(argc, argv, [](const Wt::WEnvironment &env) {
193
		return Wt::cpp14::make_unique<HelloApplication>(env);
194
	});
195
}
(5-5/5)