File: MandelbrotExample.C

package info (click to toggle)
witty 3.1.2-3
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 45,512 kB
  • ctags: 35,832
  • sloc: cpp: 69,469; ansic: 66,945; xml: 4,383; sh: 594; perl: 108; makefile: 106
file content (125 lines) | stat: -rw-r--r-- 3,351 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
 * Copyright (C) 2008 Emweb bvba, Kessel-Lo, Belgium.
 *
 * See the LICENSE file for terms of use.
 */

#include <Wt/WApplication>
#include <Wt/WBreak>
#include <Wt/WPushButton>
#include <Wt/WTable>
#include <Wt/WTableCell>
#include <Wt/WText>
#include <boost/lexical_cast.hpp>

#include "MandelbrotExample.h"
#include "MandelbrotImage.h"

MandelbrotExample::MandelbrotExample(WContainerWidget *parent)
  : WContainerWidget(parent)
{
  new WText("<div style=\"height:1px; width: 1px;\"/>"
	    "<h2>Wt Mandelbrot example</h2>"
	    "<p>The image below is a WVirtualImage that renders the "
	    "classic Mandelbrot fractal.</p>"
	    "<p>It is drawn as a grid of many smaller images, "
	    "computed on the fly, as you scroll around "
	    "through the virtual image. You can scroll the image using the "
	    "buttons, or by dragging the mouse.</p>", this);

  WTable *layout = new WTable(this);

  mandelbrot_ = new MandelbrotImage(400, 400,
				    3000, 3000,
				    -2,
				    -1.5,
				    1,
				    1.5, layout->elementAt(0, 0));

  WContainerWidget *buttons = new WContainerWidget(layout->elementAt(0, 0));
  buttons->resize(400, WLength::Auto);
  buttons->setContentAlignment(AlignCenter);

  (new WPushButton("Left", buttons))
    ->clicked().connect(SLOT(this, MandelbrotExample::moveLeft));
  (new WPushButton("Right", buttons))
    ->clicked().connect(SLOT(this, MandelbrotExample::moveRight));
  (new WPushButton("Up", buttons))
    ->clicked().connect(SLOT(this, MandelbrotExample::moveUp));
  (new WPushButton("Down", buttons))
    ->clicked().connect(SLOT(this, MandelbrotExample::moveDown));

  new WBreak(buttons);

  (new WPushButton("Zoom in", buttons))
    ->clicked().connect(SLOT(this, MandelbrotExample::zoomIn));
  (new WPushButton("Zoom out", buttons))
    ->clicked().connect(SLOT(this, MandelbrotExample::zoomOut));

  viewPortText_ = new WText(layout->elementAt(0, 1));
  layout->elementAt(0, 1)->setPadding(10);

  updateViewPortText();

  mandelbrot_->viewPortChanged()
    .connect(SLOT(this, MandelbrotExample::updateViewPortText));
}

void MandelbrotExample::moveLeft()
{
  mandelbrot_->scroll(-50, 0);
}

void MandelbrotExample::moveRight()
{
  mandelbrot_->scroll(50, 0);
}

void MandelbrotExample::moveUp()
{
  mandelbrot_->scroll(0, -50);
}

void MandelbrotExample::moveDown()
{
  mandelbrot_->scroll(0, 50);
}

void MandelbrotExample::zoomIn()
{
  mandelbrot_->zoomIn();
}

void MandelbrotExample::zoomOut()
{
  mandelbrot_->zoomOut();
}

void MandelbrotExample::updateViewPortText()
{
  viewPortText_->setText
    ("Current viewport: ("
     + boost::lexical_cast<std::string>(mandelbrot_->currentX1()) + ","
     + boost::lexical_cast<std::string>(mandelbrot_->currentY1()) + ") to ("
     + boost::lexical_cast<std::string>(mandelbrot_->currentX2()) + ","
     + boost::lexical_cast<std::string>(mandelbrot_->currentY2()) + ")");
}

WApplication *createApplication(const WEnvironment& env)
{
  WApplication *app = new WApplication(env);
  app->setTitle("Wt Mandelbrot example");

  MandelbrotExample *mandelbrot = new MandelbrotExample();
  mandelbrot->setPadding(8);
  app->root()->addWidget(mandelbrot);

  app->styleSheet().addRule("html, body",
			    "border: 0px; margin: 0px; height: 100%;");
  return app;
}

int main(int argc, char **argv)
{
   return WRun(argc, argv, &createApplication);
}