#ifndef MAPBOX_H
#define MAPBOX_H
#include <QPixmap>
#include <QWidget>
Q_OBJECT
public:
protected:
private slots:
void updatePixmap
(const QImage &image
);
void fitInWindows();
private:
void zoom(double zoomFactor);
void scroll(int deltaX, int deltaY);
double sx1, sy1, sx2, sy2; ///< coordinates of corner pixels of source - pixmap - rectangle
double dx1, dy1, dx2, dy2; ///< coordinates of corner pixels of destination - widget - rectangle
};
#endif
#ifndef MAPBOX_H
#define MAPBOX_H
#include <QPixmap>
#include <QWidget>
class MapBox : public QWidget {
Q_OBJECT
public:
MapBox(QWidget *parent = 0);
protected:
void paintEvent(QPaintEvent *event);
void keyPressEvent(QKeyEvent *event);
void wheelEvent(QWheelEvent *event);
void mousePressEvent(QMouseEvent *event);
void mouseMoveEvent(QMouseEvent *event);
private slots:
void updatePixmap(const QImage &image);
void fitInWindows();
private:
void zoom(double zoomFactor);
void scroll(int deltaX, int deltaY);
QPixmap pixmap;
QPoint lastDragPos;
double sx1, sy1, sx2, sy2; ///< coordinates of corner pixels of source - pixmap - rectangle
double dx1, dy1, dx2, dy2; ///< coordinates of corner pixels of destination - widget - rectangle
};
#endif
To copy to clipboard, switch view to plain text mode
#include <QtGui>
#include <math.h>
#include "MapBox.h"
const double widgetX = 400;
const double widgetY = 400;
const double ZoomOutFactor = 0.8;
const double ZoomInFactor = 1 / ZoomOutFactor;
const double ScrollStep = 20;
//qRegisterMetaType<QImage>("QImage");
setWindowTitle(tr("MapBox"));
setCursor(Qt::CrossCursor);
resize(400, 400);
// setting source and destination init corners..
sx1 = 0;
sy1 = 0;
sx2 = widgetX;
sy2 = widgetY;
dx1 = 0;
dy1 = 0;
dx2 = widgetX;
dy2 = widgetY;
image.load("biggerO.png");
updatePixmap(image);
}
void
painter.fillRect(rect(), Qt::black);
if (pixmap.isNull()) return;
QRectF source
(sx1, sy1, sx2
-sx1, sy2
-sy1
);
// the second point is in coordinates origin of the firt point !!!! QRectF destination
(dx1, dy1, dx2
-dx1, dy2
-dy1
);
// the second point is in coordinates origin of the firt point !!!! /*
This is the main function of the widget... the good pointa are:
A) It takes into account the low level details of scaling, such interpolation
B) If the destination is outside the widgets bounds, it doesn't matter. It make its job
on the widget without any error (in this sence it isnot like an array luckily...)
*/
painter.drawPixmap(destination, pixmap, source);
}
void
switch (event->key()) {
case Qt::Key_Plus:
zoom(ZoomInFactor);
break;
case Qt::Key_Minus:
zoom(ZoomOutFactor);
break;
case Qt::Key_Left:
scroll(+ScrollStep, 0);
break;
case Qt::Key_Right:
scroll(-ScrollStep, 0);
break;
case Qt::Key_Down:
scroll(0, -ScrollStep);
break;
case Qt::Key_Up:
scroll(0, +ScrollStep);
break;
default:
}
}
void
int numDegrees = event->delta() / 8;
double numSteps = numDegrees / 15.0f;
zoom(pow(ZoomInFactor, numSteps));
}
void
if (event->button() == Qt::LeftButton)
lastDragPos = event->pos();
}
void
if (event->buttons() & Qt::LeftButton) {
scroll(event->pos().x()-lastDragPos.x(), event->pos().y()-lastDragPos.y());
lastDragPos = event->pos();
update();
}
}
void
MapBox
::updatePixmap(const QImage &image
){ pixmap
= QPixmap::fromImage(image
);
fitInWindows();
}
void MapBox::fitInWindows(){
double tempXScale = widgetX/pixmap.width();
double tempYScale = widgetY/pixmap.height();
sx1 = 0;
sy1 = 0;
sx2 = pixmap.width();
sy2 = pixmap.height();
dx1 = 0;
dy1 = 0;
if ( tempXScale >= tempYScale){
dx2 = pixmap.width()*tempYScale;
dy2 = widgetY;
} else {
dx2 = widgetX;
dy2 = pixmap.height()*tempXScale;
}
update();
}
void
MapBox::zoom(double zoomFactor){
double dx1new, dx2new, dy1new, dy2new;
dx1new = dx2- (dx2-dx1)* ( 1+ (zoomFactor-1)/2 );
dx2new = dx1+ (dx2-dx1)* ( 1+ (zoomFactor-1)/2 );
dy1new = dy2- (dy2-dy1)* ( 1+ (zoomFactor-1)/2 );
dy2new = dy1+ (dy2-dy1)* ( 1+ (zoomFactor-1)/2 );
dx1 = dx1new;
dy1 = dy1new;
dx2 = dx2new;
dy2 = dy2new;
update();
}
void
MapBox::scroll(int deltaX, int deltaY){
dx1 += ((double) deltaX);
dx2 += ((double) deltaX);
dy1 += ((double) deltaY);
dy2 += ((double) deltaY);
update();
}
#include <QtGui>
#include <math.h>
#include "MapBox.h"
const double widgetX = 400;
const double widgetY = 400;
const double ZoomOutFactor = 0.8;
const double ZoomInFactor = 1 / ZoomOutFactor;
const double ScrollStep = 20;
MapBox::MapBox(QWidget *parent) :QWidget(parent) {
//qRegisterMetaType<QImage>("QImage");
setWindowTitle(tr("MapBox"));
setCursor(Qt::CrossCursor);
resize(400, 400);
// setting source and destination init corners..
sx1 = 0;
sy1 = 0;
sx2 = widgetX;
sy2 = widgetY;
dx1 = 0;
dy1 = 0;
dx2 = widgetX;
dy2 = widgetY;
QImage image;
image.load("biggerO.png");
updatePixmap(image);
}
void
MapBox::paintEvent(QPaintEvent *){
QPainter painter(this);
painter.fillRect(rect(), Qt::black);
if (pixmap.isNull()) return;
QRectF source (sx1, sy1, sx2-sx1, sy2-sy1); // the second point is in coordinates origin of the firt point !!!!
QRectF destination(dx1, dy1, dx2-dx1, dy2-dy1); // the second point is in coordinates origin of the firt point !!!!
/*
This is the main function of the widget... the good pointa are:
A) It takes into account the low level details of scaling, such interpolation
B) If the destination is outside the widgets bounds, it doesn't matter. It make its job
on the widget without any error (in this sence it isnot like an array luckily...)
*/
painter.drawPixmap(destination, pixmap, source);
}
void
MapBox::keyPressEvent(QKeyEvent *event) {
switch (event->key()) {
case Qt::Key_Plus:
zoom(ZoomInFactor);
break;
case Qt::Key_Minus:
zoom(ZoomOutFactor);
break;
case Qt::Key_Left:
scroll(+ScrollStep, 0);
break;
case Qt::Key_Right:
scroll(-ScrollStep, 0);
break;
case Qt::Key_Down:
scroll(0, -ScrollStep);
break;
case Qt::Key_Up:
scroll(0, +ScrollStep);
break;
default:
QWidget::keyPressEvent(event);
}
}
void
MapBox::wheelEvent(QWheelEvent *event){
int numDegrees = event->delta() / 8;
double numSteps = numDegrees / 15.0f;
zoom(pow(ZoomInFactor, numSteps));
}
void
MapBox::mousePressEvent(QMouseEvent *event){
if (event->button() == Qt::LeftButton)
lastDragPos = event->pos();
}
void
MapBox::mouseMoveEvent(QMouseEvent *event) {
if (event->buttons() & Qt::LeftButton) {
scroll(event->pos().x()-lastDragPos.x(), event->pos().y()-lastDragPos.y());
lastDragPos = event->pos();
update();
}
}
void
MapBox::updatePixmap(const QImage &image){
pixmap = QPixmap::fromImage(image);
fitInWindows();
}
void MapBox::fitInWindows(){
double tempXScale = widgetX/pixmap.width();
double tempYScale = widgetY/pixmap.height();
sx1 = 0;
sy1 = 0;
sx2 = pixmap.width();
sy2 = pixmap.height();
dx1 = 0;
dy1 = 0;
if ( tempXScale >= tempYScale){
dx2 = pixmap.width()*tempYScale;
dy2 = widgetY;
} else {
dx2 = widgetX;
dy2 = pixmap.height()*tempXScale;
}
update();
}
void
MapBox::zoom(double zoomFactor){
double dx1new, dx2new, dy1new, dy2new;
dx1new = dx2- (dx2-dx1)* ( 1+ (zoomFactor-1)/2 );
dx2new = dx1+ (dx2-dx1)* ( 1+ (zoomFactor-1)/2 );
dy1new = dy2- (dy2-dy1)* ( 1+ (zoomFactor-1)/2 );
dy2new = dy1+ (dy2-dy1)* ( 1+ (zoomFactor-1)/2 );
dx1 = dx1new;
dy1 = dy1new;
dx2 = dx2new;
dy2 = dy2new;
update();
}
void
MapBox::scroll(int deltaX, int deltaY){
dx1 += ((double) deltaX);
dx2 += ((double) deltaX);
dy1 += ((double) deltaY);
dy2 += ((double) deltaY);
update();
}
To copy to clipboard, switch view to plain text mode
Bookmarks