|
#include <Wt/WApplication.h>
|
|
#include <Wt/WBrush.h>
|
|
#include <Wt/WColor.h>
|
|
#include <Wt/WContainerWidget.h>
|
|
#include <Wt/WEnvironment.h>
|
|
#include <Wt/WPaintedWidget.h>
|
|
#include <Wt/WPainter.h>
|
|
#include <Wt/WPdfImage.h>
|
|
#include <Wt/WPen.h>
|
|
#include <Wt/WPushButton.h>
|
|
#include <Wt/WRectF.h>
|
|
#include <Wt/WText.h>
|
|
|
|
using namespace Wt;
|
|
|
|
class EllipsePainter : public WPainter
|
|
{
|
|
public:
|
|
EllipsePainter(WPaintDevice *paintDevice, int width, int height) : WPainter(paintDevice)
|
|
{
|
|
auto y = 20.0;
|
|
|
|
static std::array<std::tuple<PenStyle, int, std::string>, 3> const pens {
|
|
std::make_tuple(PenStyle::None, 0, "None"),
|
|
std::make_tuple(PenStyle::SolidLine, 0, "cosmetic"),
|
|
std::make_tuple(PenStyle::SolidLine, 20, "20 px")
|
|
};
|
|
|
|
for (auto pen: pens) {
|
|
save();
|
|
WRectF textRect(0, y, 800, 30);
|
|
y += 30;
|
|
drawText(textRect, AlignmentFlag::Left, std::string("======= pen: ") + std::get<2>(pen) + " =======");
|
|
WPen ellipsePen;
|
|
ellipsePen.setStyle(std::get<0>(pen));
|
|
ellipsePen.setWidth(std::get<1>(pen));
|
|
|
|
// FIXME: Shouldn't be needed? Defaults to Square which is incorrectly rendered on Canvas
|
|
ellipsePen.setCapStyle(PenCapStyle::Flat);
|
|
auto rect = WRectF(20, y, width, height);
|
|
|
|
fillRect(rect, WBrush(StandardColor::Yellow));
|
|
|
|
setPen(ellipsePen);
|
|
setBrush(WBrush(StandardColor::Green));
|
|
drawEllipse(rect);
|
|
restore();
|
|
y += 130;
|
|
}
|
|
}
|
|
};
|
|
|
|
class PaintedWidget : public WPaintedWidget
|
|
{
|
|
public:
|
|
PaintedWidget(int width, int height) :
|
|
Wt::WPaintedWidget(), width_(width), height_(height) { }
|
|
~PaintedWidget() { }
|
|
|
|
protected:
|
|
void paintEvent(WPaintDevice *paintDevice) {
|
|
EllipsePainter painter(paintDevice, width_, height_);
|
|
}
|
|
|
|
private:
|
|
int width_;
|
|
int height_;
|
|
};
|
|
|
|
class EllipsePdfResource : public Wt::WPdfImage
|
|
{
|
|
public:
|
|
EllipsePdfResource() : WPdfImage(612, 792)
|
|
{
|
|
suggestFileName("ellipse_test.pdf");
|
|
paint();
|
|
}
|
|
|
|
private:
|
|
void paint() {
|
|
EllipsePainter(this, 200, 100);
|
|
}
|
|
};
|
|
|
|
class TestApp : public WApplication {
|
|
public:
|
|
TestApp(const WEnvironment& env);
|
|
};
|
|
|
|
TestApp::TestApp(const WEnvironment& env) : WApplication(env)
|
|
{
|
|
setTitle("Test drawEllipse with pens");
|
|
|
|
auto pdf = std::make_shared<EllipsePdfResource>();
|
|
auto button = root()->addNew<WPushButton>("View test PDF");
|
|
button->setLink(WLink(pdf));
|
|
button->setInline(false);
|
|
button->setMargin(20);
|
|
|
|
static std::array<std::pair<RenderMethod, std::string>, 3> const methods {
|
|
std::make_pair(RenderMethod::HtmlCanvas, "canvas"),
|
|
std::make_pair(RenderMethod::PngImage, "png" ),
|
|
std::make_pair(RenderMethod::InlineSvgVml, "svg" )
|
|
};
|
|
|
|
for (auto method: methods) {
|
|
root()->addNew<WText>(std::string("method: ") + method.second);
|
|
auto pw = root()->addNew<PaintedWidget>(200, 100);
|
|
pw->resize(800, 500);
|
|
pw->setPreferredMethod(method.first);
|
|
}
|
|
}
|
|
|
|
int main(int argc, char **argv)
|
|
{
|
|
return WRun(argc, argv, [](const WEnvironment& env) {return std::make_unique<TestApp>(env);});
|
|
}
|