Project

General

Profile

zoom and pan; source code attached

Added by Mohammed Rashad almost 15 years ago

How to zoom and pan a WPainter::Image.

any one please help me.

I am stuck for a week. I tried mouseDrag Event and event.dragDelta.

first time panning works but when panned from a previously panned position the image goes back to initial location

Here is my code.

please please help.

class Editor : public WPaintedWidget

{

public:

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

void setColor(const WColor& c) {

color_ = c;

}

int dx,dy;

bool drag;

void dragged(const WMouseEvent& e);

protected:

virtual void paintEvent(WPaintDevice *paintDevice);

private:

WColor color_;

};

Editor::Editor(int width, int height, WContainerWidget *parent)

: WPaintedWidget(parent)

{

setSelectable(true);

resize(WLength(width), WLength(height));

dx=0;dy=0;

drag=false;

mouseDragged().connect(this, &Editor::dragged);

color_ = WColor(green);

}

void Editor::dragged(const WMouseEvent& e)

{

drag=true;

dx = e.dragDelta().x;

dy = e.dragDelta().y;

update();

}

void Editor::paintEvent(WPaintDevice *paintDevice)

{

WPainter painter(paintDevice);

painter.setRenderHint(WPainter::Antialiasing);

WPainter::Image image("loading.png",640,480);

if(drag==true) {

painter.drawImage(WPointF(dx,dy),image);

drag=false;

}

else {

painter.drawImage(WPointF(0,0),image);

}

}


Replies (1)

RE: zoom and pan; source code attached - Added by Wim Dumon almost 15 years ago

Mohammed,

Shouldn't you accumulate your drag deltas? I think it's easiest if you keep track of how much your upper left corner has moved away from its original position:

In your constructor:

// In the beginning, the topleft corner hasn't moved at all
topLeftX_ = 0;
topLeftY_ = 0;

Your drag slot:

void Editor::dragged(const WMouseEvent& e) {
  // Every drag modifies the position of the topleft corner
  topLeftX_ += e.dragDelta().x; // if it moves in the wrong direction use -=
  topLeftY_ += e.dragDelta().y; // if it moves in the wrong direction use -=
  update();
}

To paint:

{
  ...
  painter.drawImage(WPointF(topLeftX_, topLeftY_),image);
  ...
}

Or maybe better even, use translate() somewhere in the beginning of your painting function, so that you don't have to repeat the translation for every position that you want to draw.

    (1-1/1)