Project

General

Profile

Support #4152 » main.cpp

Drus Kor, 06/30/2015 11:24 AM

 
1

    
2

    
3
#include <Wt/WApplication>
4
#include <Wt/WPainter>
5
#include <Wt/WPaintDevice>
6
#include <Wt/WPaintedWidget>
7
#include <Wt/WContainerWidget>
8
#include <Wt/WServer>
9
using namespace Wt;
10

    
11

    
12
class ShapesWidget : public Wt::WPaintedWidget
13
{
14
public:
15
    ShapesWidget(Wt::WContainerWidget *parent = 0)
16
        : Wt::WPaintedWidget(parent)
17
    {
18
        resize(310, 400);  // Provide a default size.
19
    }
20

    
21

    
22
protected:
23
    void paintEvent(Wt::WPaintDevice *paintDevice) {
24
        Wt::WPainter painter(paintDevice);
25
        painter.setPen(Wt::red);
26

    
27

    
28
	// DRAWING PRIMITIVE SHAPES
29
	// Draw a line from (0, 0) to (200, 0) and then 30 px downwards.
30
	painter.drawLine(0, 0, 200, 0);
31
	painter.drawLine(200, 0, 200, 30);
32

    
33

    
34
	// Draw and fill a rectangle from (0, 25) with width 80 and height 25
35
	// using the current pen (red) and brush (default: white).
36
	painter.drawRect(0, 25, 80, 50);
37
	// Set the current brush with the global color name 'green'.
38
	painter.setBrush(Wt::WBrush(Wt::green));
39
	// Draw the same rectangle from (100, 25) using the current pen and brush.
40
	painter.drawRect(100, 25, 80, 50);
41
	// Fill a rectangle from (100, 25) with width 80 and height 25.
42
	// Choose a color with red=0, green=255, blue=0, alpha=64.
43
	painter.fillRect(220, 25, 80, 50, Wt::WBrush(Wt::WColor(0, 255, 0, 64)));
44

    
45

    
46
	// Draw the outline of an ellipse.
47
	painter.drawEllipse(0, 100, 80, 50);
48
	// Draw the upper segment of the ellipse (angle = 180 x 1/16th of a degree)
49
	painter.drawChord(100, 100, 80, 50, 0, 180*16);
50

    
51

    
52
	// Draw an open arc and a closed arc.
53
	painter.drawArc(220, 100, 50, 50, 90*16, 90*16);
54
	painter.drawArc(240, 100, 50, 50, 0, 90*16);
55
	painter.drawLine(265, 100, 265, 125);
56
	painter.drawLine(265, 125, 290, 125);
57

    
58

    
59
	// Draw a 6-point polygon and fill it with the current brush.
60
	const Wt::WPointF points[]
61
	    = { Wt::WPointF(120, 170), Wt::WPointF(160, 170),
62
		Wt::WPointF(180, 204.6), Wt::WPointF(160, 239.2),
63
		Wt::WPointF(120, 239.2), Wt::WPointF(100, 204.6) };
64
	painter.drawPolygon(points, 6);
65
	// DRAWING SHAPES USING A PATH
66
	// Create an ellipse path and fill it.
67
	Wt::WPainterPath filledEllipsePath = Wt::WPainterPath();
68
	filledEllipsePath.addEllipse(0, 180, 80, 50);
69
	filledEllipsePath.closeSubPath();
70
	painter.drawPath(filledEllipsePath);
71

    
72

    
73
	// Create a new path for a triangle.
74
	Wt::WPainterPath filledTrianglePath = Wt::WPainterPath();
75
	filledTrianglePath.moveTo(0, 270);
76
	filledTrianglePath.lineTo(80,270);
77
	filledTrianglePath.lineTo(0, 350);
78
	filledTrianglePath.closeSubPath();
79
	// Draw the path and fill it.
80
	painter.drawPath(filledTrianglePath);
81

    
82

    
83
	// strokePath draws a path but doesn't fill it.
84
	Wt::WPainterPath strokedTrianglePath = Wt::WPainterPath();
85
	strokedTrianglePath.moveTo(100,270);
86
	strokedTrianglePath.lineTo(100,350);
87
	strokedTrianglePath.lineTo(20, 350);
88
	strokedTrianglePath.closeSubPath();
89
	Wt::WPen pen = Wt::WPen();
90
	pen.setWidth(3);
91
	painter.strokePath(strokedTrianglePath, pen);
92

    
93

    
94
	// Draw a balloon with quadratic bezier curves.
95
	Wt::WPainterPath quadraticCurvePath = Wt::WPainterPath();
96
	quadraticCurvePath.moveTo(250,150);
97
	quadraticCurvePath.quadTo(200,150, 200,187.5);
98
	quadraticCurvePath.quadTo(200,225, 225,225);
99
	quadraticCurvePath.quadTo(225,245, 205,250);
100
	quadraticCurvePath.quadTo(235,245, 240,225);
101
	quadraticCurvePath.quadTo(300,225, 300,187.5);
102
	quadraticCurvePath.quadTo(300,150, 250,150);
103
	painter.strokePath(quadraticCurvePath, pen);
104

    
105

    
106
	// Draw a heart with cubic bezier curves.
107
	Wt::WPainterPath bezierCurvePath = Wt::WPainterPath();
108
	bezierCurvePath.moveTo( 255,285);
109
	bezierCurvePath.cubicTo(255,282,  250,270,  230,270);
110
	bezierCurvePath.cubicTo(200,270,  200,307.5,200,307.5);
111
	bezierCurvePath.cubicTo(200,325,  220,357,  255,365);
112
	bezierCurvePath.cubicTo(290,347,  310,325,  310,307.5);
113
	bezierCurvePath.cubicTo(310,307.5,310,270,  290,270);
114
	bezierCurvePath.cubicTo(265,270,  255,282,  255,285);
115
	painter.setBrush(Wt::WBrush(Wt::red));
116
	painter.drawPath(bezierCurvePath);
117
    }
118
};
119

    
120

    
121

    
122

    
123
WApplication *createApplication( const WEnvironment& env )
124
{
125
  WApplication *app = new WApplication( env );
126
  Wt::WContainerWidget *container = new Wt::WContainerWidget( app->root() );
127
  container->addWidget( new ShapesWidget(container) );
128
  return app;
129
}
130

    
131

    
132

    
133

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

    
(1-1/8)