Project

General

Profile

RE: How to scale() by using WTransform matrix ยป standalone.cpp

Mohammed Rashad, 10/21/2010 04:00 PM

 
/*
* Copyright (C) 2008 Emweb bvba, Heverlee, Belgium.
*
* See the LICENSE file for terms of use.
*/

#include <Wt/WApplication>
#include <Wt/WBreak>
#include <Wt/WContainerWidget>
#include <Wt/WLineEdit>
#include <Wt/WPushButton>
#include <Wt/WText>

//#include "temp.cpp"

#include <boost/lexical_cast.hpp>
#include <iostream>
#include <string>

//#include "PaintBrush.h"

#include <Wt/WCssDecorationStyle>
#include <Wt/WPainter>
#include <Wt/WPainterPath>
#include <Wt/WPointF>
#include <Wt/WRectF>

#include <Wt/WPaintedWidget>
#include <Wt/WEvent>
#include <vector>


#include <Wt/WTable>
#include <Wt/WTableCell>
#include <Wt/WText>
#include <Wt/WVirtualImage>
#include <boost/lexical_cast.hpp>


#include <fstream>

#include <Wt/WResource>
#include <Wt/WImage>

#include <Wt/WRasterImage>
#include <Wt/Http/Response>




using namespace Wt;
using namespace std;


bool ZOOMIN,ZOOMOUT;

bool TRANSLATE;

bool OVERLAY;


WMouseEvent::Coordinates d(0,0);


class Editor : public WPaintedWidget
{
public:


Editor(int width, int height, WContainerWidget *parent = 0);

void setColor(const WColor& c) {
color_ = c;
}




double dx,dy;
double x,y;
bool drag;
bool newpath;
bool redraw;
double tx,ty;
double lastX, lastY;
int w,h;
WRectF window;
list<WMouseEvent::Coordinates> coords;
WPainterPath path_;
list<WPainterPath> plist;


void mouseClicked(const WMouseEvent& e);
void mouseButtonUp(const WMouseEvent& e);
void mouseMove(const WMouseEvent& e);
void mouseButtonDown(const WMouseEvent& e);

void redrawPainter();



protected:
virtual void paintEvent(WPaintDevice *paintDevice);

private:


WColor color_;





};

string ss;
Editor::Editor(int width, int height, WContainerWidget *parent)
: WPaintedWidget(parent)
{
setSelectable(true);
resize(WLength(width), WLength(height));

dx=0;dy=0;
drag= false;
newpath = false;
x=0;y=0;
w=640;h=480;
tx=0;ty=0;

ss="loading.png";


clicked().connect(this, &Editor::mouseClicked);
mouseWentUp().connect(this, &Editor::mouseButtonUp);
mouseWentDown().connect(this, &Editor::mouseButtonDown);
mouseMoved().connect(this, &Editor::mouseMove);



color_ = WColor(green);






}




void Editor::mouseButtonDown(const WMouseEvent& e){
if((e.button() == WMouseEvent::LeftButton) && (decorationStyle().cursor() == OpenHandCursor)) {
drag=true;


}

}
void Editor::mouseMove(const WMouseEvent& e)
{
if((drag==true) && (decorationStyle().cursor() == OpenHandCursor) ) {
dx = e.dragDelta().x;
dy = e.dragDelta().y;
update();
}

}


void Editor:: mouseButtonUp(const WMouseEvent& e)
{

if((e.button() == WMouseEvent::LeftButton) && (decorationStyle().cursor() == OpenHandCursor)) {
drag=false;
TRANSLATE=true;

x+=dx;y+=dy;


update();
}

}

void Editor::paintEvent(WPaintDevice *paintDevice)
{
WPainter painter(paintDevice);


WRectF viewport = painter.viewPort();

painter.setRenderHint(WPainter::Antialiasing);
WPainter::Image image(ss,640,480);

//clear();
if(TRANSLATE == true) {
TRANSLATE=false;
painter.drawImage(WPointF(x,y),image);

}
else{
painter.drawImage(WPointF(x+dx,y+dy),image);
}

viewport.setX(viewport.x()+x);
viewport.setY(viewport.y()+y);
tx = viewport.x();
ty = viewport.y();
viewport.setWidth(viewport.width()+x);
viewport.setHeight(viewport.height()+y);
painter.setViewPort(viewport);



//painter.drawImage(WPointF(0,0),image);
WPen pen;
pen.setWidth(3);
pen.setColor(color_);
painter.setPen(pen);

list<WPainterPath>::iterator pi;
for(pi=plist.begin();pi!=plist.end();++pi)
painter.drawPath((*pi));

//}

}





void Editor::mouseClicked(const WMouseEvent& e)
{


if(decorationStyle().cursor() == CrossCursor) {
TRANSLATE =true;
if(newpath==false) {
WMouseEvent::Coordinates c = e.widget();
c.x=c.x-tx;
c.y=c.y-ty;
coords.push_back(c);
path_ = WPainterPath(WPointF(c.x, c.y));
newpath=true;
}
else
{

WMouseEvent::Coordinates c = e.widget();
c.x=c.x-tx;
c.y=c.y-ty;
coords.push_back(c);
path_.lineTo(c.x, c.y);
plist.push_back(path_);
update();

}

}
}







class CollabMapping : public WApplication
{
public:
CollabMapping(const WEnvironment& env);

Editor *editor;

void ToggleEditing();
void ZoomIn();
void ZoomOut();
void EndLine();




};
void CollabMapping::ToggleEditing()
{
Cursor c= editor->decorationStyle().cursor();
if(c == CrossCursor) {
c=OpenHandCursor;
}
else
c=CrossCursor;

editor->decorationStyle().setCursor(c);
TRANSLATE=true;
editor->update();


}


void CollabMapping::ZoomIn() {
ZOOMIN=true;
editor->update();
}
void CollabMapping::ZoomOut()
{
ZOOMOUT=true;
editor->update();
}

void CollabMapping::EndLine() {
editor->newpath=false;
editor->coords.push_back(d);
}


CollabMapping::CollabMapping(const WEnvironment& env)
: WApplication(env)
{
setTitle("Map Display");






editor = new Editor(640,480,root());
editor->decorationStyle().setCursor(CrossCursor);
editor->decorationStyle().setBorder(WBorder(WBorder::Solid));

//editor->setPositionScheme(Fixed);


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

new WBreak(buttons);
new WBreak(buttons);
new WBreak(buttons);

(new WPushButton("Toggle", buttons))
->clicked().connect(this, &CollabMapping::ToggleEditing);
(new WPushButton("Zoom In", buttons))
->clicked().connect(this, &CollabMapping::ZoomIn);
(new WPushButton("Zoom out", buttons))
->clicked().connect(this, &CollabMapping::ZoomOut);
(new WPushButton("EndLine", buttons))
->clicked().connect(this, &CollabMapping::EndLine);





new WBreak(buttons);

}



WApplication *createApplication(const WEnvironment& env)
{

return new CollabMapping(env);
}

int main(int argc, char **argv)
{

return WRun(argc, argv, &createApplication);
}










    (1-1/1)