MandelbrotExample.C 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * Copyright (C) 2008 Emweb bvba, Kessel-Lo, Belgium.
  3. *
  4. * See the LICENSE file for terms of use.
  5. */
  6. #include <Wt/WApplication>
  7. #include <Wt/WBreak>
  8. #include <Wt/WPushButton>
  9. #include <Wt/WTable>
  10. #include <Wt/WTableCell>
  11. #include <Wt/WText>
  12. #include <boost/lexical_cast.hpp>
  13. #include "MandelbrotExample.h"
  14. #include "MandelbrotImage.h"
  15. MandelbrotExample::MandelbrotExample(WContainerWidget *parent)
  16. : WContainerWidget(parent)
  17. {
  18. new WText("<div style=\"height:1px; width: 1px;\"/>"
  19. "<h2>Wt Mandelbrot example</h2>"
  20. "<p>The image below is a WVirtualImage that renders the "
  21. "classic Mandelbrot fractal.</p>"
  22. "<p>It is drawn as a grid of many smaller images, "
  23. "computed on the fly, as you scroll around "
  24. "through the virtual image. You can scroll the image using the "
  25. "buttons, or by dragging the mouse.</p>", this);
  26. WTable *layout = new WTable(this);
  27. mandelbrot_ = new MandelbrotImage(400, 400,
  28. 3000, 3000,
  29. -2,
  30. -1.5,
  31. 1,
  32. 1.5, layout->elementAt(0, 0));
  33. WContainerWidget *buttons = new WContainerWidget(layout->elementAt(0, 0));
  34. buttons->resize(400, WLength::Auto);
  35. buttons->setContentAlignment(AlignCenter);
  36. (new WPushButton("Left", buttons))
  37. ->clicked().connect(this, &MandelbrotExample::moveLeft);
  38. (new WPushButton("Right", buttons))
  39. ->clicked().connect(this, &MandelbrotExample::moveRight);
  40. (new WPushButton("Up", buttons))
  41. ->clicked().connect(this, &MandelbrotExample::moveUp);
  42. (new WPushButton("Down", buttons))
  43. ->clicked().connect(this, &MandelbrotExample::moveDown);
  44. new WBreak(buttons);
  45. (new WPushButton("Zoom in", buttons))
  46. ->clicked().connect(this, &MandelbrotExample::zoomIn);
  47. (new WPushButton("Zoom out", buttons))
  48. ->clicked().connect(this, &MandelbrotExample::zoomOut);
  49. viewPortText_ = new WText(layout->elementAt(0, 1));
  50. layout->elementAt(0, 1)->setPadding(10);
  51. updateViewPortText();
  52. mandelbrot_->viewPortChanged()
  53. .connect(this, &MandelbrotExample::updateViewPortText);
  54. }
  55. void MandelbrotExample::moveLeft()
  56. {
  57. mandelbrot_->scroll(-50, 0);
  58. }
  59. void MandelbrotExample::moveRight()
  60. {
  61. mandelbrot_->scroll(50, 0);
  62. }
  63. void MandelbrotExample::moveUp()
  64. {
  65. mandelbrot_->scroll(0, -50);
  66. }
  67. void MandelbrotExample::moveDown()
  68. {
  69. mandelbrot_->scroll(0, 50);
  70. }
  71. void MandelbrotExample::zoomIn()
  72. {
  73. mandelbrot_->zoomIn();
  74. }
  75. void MandelbrotExample::zoomOut()
  76. {
  77. mandelbrot_->zoomOut();
  78. }
  79. void MandelbrotExample::updateViewPortText()
  80. {
  81. viewPortText_->setText
  82. ("Current viewport: ("
  83. + boost::lexical_cast<std::string>(mandelbrot_->currentX1()) + ","
  84. + boost::lexical_cast<std::string>(mandelbrot_->currentY1()) + ") to ("
  85. + boost::lexical_cast<std::string>(mandelbrot_->currentX2()) + ","
  86. + boost::lexical_cast<std::string>(mandelbrot_->currentY2()) + ")");
  87. }
  88. WApplication *createApplication(const WEnvironment& env)
  89. {
  90. WApplication *app = new WApplication(env);
  91. app->setTitle("Wt Mandelbrot example");
  92. MandelbrotExample *mandelbrot = new MandelbrotExample();
  93. mandelbrot->setPadding(8);
  94. app->root()->addWidget(mandelbrot);
  95. app->styleSheet().addRule("html, body",
  96. "border: 0px; margin: 0px; height: 100%;");
  97. return app;
  98. }
  99. int main(int argc, char **argv)
  100. {
  101. return WRun(argc, argv, &createApplication);
  102. }