/usr/lib/Wt/examples/planner/ShapesWidget.C is in witty-examples 3.1.10-1ubuntu2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 126 127 128 129 130 131 132 133 134 | /*
* Copyright (C) 2010 Emweb bvba, Kessel-Lo, Belgium.
*
* See the LICENSE file for terms of use.
*/
#include "ShapesWidget.h"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <Wt/WPainter>
#include <Wt/WGlobal>
using namespace Wt;
ShapesWidget::ShapesWidget(WContainerWidget* parent)
: WPaintedWidget(parent),
toSelect_(0)
{}
ShapesWidget::~ShapesWidget()
{
cleanupShapes();
}
WString ShapesWidget::selectedColor()
{
return toSelect_->color().colorName();
}
WString ShapesWidget::selectedShape()
{
return toSelect_->shapeName();
}
bool ShapesWidget::correctlyClicked(const WMouseEvent& me)
{
return toSelect_->contains(WPointF(me.widget().x, me.widget().y));
}
double ShapesWidget::randomDouble()
{
return rand() / (double(RAND_MAX) + 1);
}
int ShapesWidget::randomInt(const unsigned max)
{
return (rand() % max);
}
void ShapesWidget::cleanupShapes()
{
for (unsigned i = 0; i < shapes_.size(); i++) {
delete shapes_[i];
}
shapes_.clear();
toSelect_ = 0;
}
void ShapesWidget::initShapes()
{
cleanupShapes();
const unsigned numberOfShapes = 5;
unsigned i = 1;
toSelect_ = createRandomShape();
while (i < numberOfShapes) {
Shape* s = createRandomShape();
if (!sameShapeAndColor(s, toSelect_)) {
shapes_.push_back(s);
i++;
} else {
delete s;
}
}
shapes_.insert(shapes_.begin() + randomInt((int)shapes_.size()), toSelect_);
}
void ShapesWidget::paintEvent(WPaintDevice *paintDevice)
{
WPainter painter(paintDevice);
for (unsigned i = 0; i < shapes_.size(); i++) {
Shape* s = shapes_[i];
s->paint(painter);
}
}
bool ShapesWidget::sameShapeAndColor(const Shape* s1, const Shape* s2)
{
return s1->shapeName() == s2->shapeName()
&& s1->color() == s2->color();
}
ShapeColor ShapesWidget::createRandomColor()
{
static const unsigned amountOfColors = 4;
switch (randomInt(amountOfColors)) {
case 0:
return ShapeColor(red, WString::tr("captcha.red"));
case 1:
return ShapeColor(green, WString::tr("captcha.green"));
case 2:
return ShapeColor(blue, WString::tr("captcha.blue"));
case 3:
return ShapeColor(yellow, WString::tr("captcha.yellow"));
default:
return ShapeColor(black, WString("Invalid color"));
}
}
Shape* ShapesWidget::createRandomShape()
{
double size = 6 + randomDouble() * 6;
ShapeColor c = createRandomColor();
double x = size + randomDouble() * (width().value() - 2 * size);
double y = size + randomDouble() * (height().value() - 2 * size);
const unsigned amountOfShapes = 2;
unsigned shapeId = randomInt(amountOfShapes);
if (shapeId == 0)
return new Circle(WPointF(x, y), c, size);
else
return new Rectangle(WPointF(x, y), c, size);
}
|