/** mainwidget.h
* 孤舟钓客
* 2011-07-25
*/
#ifndef MAPWIDGET_H
#define MAPWIDGET_H
#include <QGraphicsView>
class QPixmap;
class QLabel;
class QPointF;
class MapWidget : public QGraphicsView
{
Q_OBJECT
public:
MapWidget();
void readMap();
QPointF mapToMap(QPointF);
public slots:
void slotZoom(int);
protected:
void drawBackground(QPainter *painter, const QRectF &rect);
void mouseMoveEvent(QMouseEvent *event);
private:
QPixmap map;
qreal zoom;
QLabel *viewCoord;
QLabel *sceneCoord;
QLabel *mapCoord;
double x1, y1;
double x2, y2;
};
#endif // MAPWIDGET_H
/** mainwidget.cpp
* 孤舟钓客
* 2011-07-25
*/
#include "mapwidget.h"
#include <QtGui>
#include <math.h>
MapWidget::MapWidget()
{
readMap();
zoom = 50;
int width = map.width();
int height = map.height();
QGraphicsScene *scene = new QGraphicsScene(this);
scene->setSceneRect(-width/2, -height/2, width, height);
setScene(scene);
setCacheMode(CacheBackground);
QSlider *slider = new QSlider;
slider->setOrientation(Qt::Vertical);
slider->setRange(1,100);
slider->setTickInterval(10);
slider->setValue(50);
connect(slider, SIGNAL(valueChanged(int)), this, SLOT(slotZoom(int)));
QLabel *zoominLabel = new QLabel;
zoominLabel->setScaledContents(true);
zoominLabel->setPixmap(QPixmap(":/images/zoomin.png"));
QLabel *zoomoutLabel = new QLabel;
zoomoutLabel->setScaledContents(true);
zoomoutLabel->setPixmap(QPixmap(":/images/zoomout.png"));
// create coordiante area
QFrame *coordFrame = new QFrame;
QLabel *label1 = new QLabel(tr("GraphicsView :"));
viewCoord = new QLabel;
QLabel *label2 = new QLabel(tr("GraphicsScene :"));
sceneCoord = new QLabel;
QLabel *label3 = new QLabel(tr("map :"));
mapCoord = new QLabel;
QGridLayout *grid = new QGridLayout;
grid->addWidget(label1, 0, 0);
grid->addWidget(viewCoord, 0, 1);
grid->addWidget(label2, 1, 0);
grid->addWidget(sceneCoord, 1, 1);
grid->addWidget(label3, 2, 0);
grid->addWidget(mapCoord, 2, 1);
grid->setSizeConstraint(QLayout::SetFixedSize);
coordFrame->setLayout(grid);
// zoom layout
QVBoxLayout *zoomLayout = new QVBoxLayout;
zoomLayout->addWidget(zoominLabel);
zoomLayout->addWidget(slider);
zoomLayout->addWidget(zoomoutLabel);
// coordinate area layout
QVBoxLayout *coordLayout = new QVBoxLayout;
coordLayout->addWidget(coordFrame);
coordLayout->addStretch();
QHBoxLayout *layout = new QHBoxLayout;
layout->addLayout(zoomLayout);
layout->addLayout(coordLayout);
layout->addStretch();
layout->setMargin(30);
layout->setSpacing(10);
setLayout(layout);
setWindowTitle("Map Widget");
setMinimumSize(600,400);
}
// read map information
void MapWidget::readMap()
{
QFile mapFile("maps.txt");
QString mapName;
int ok = mapFile.open(QIODevice::ReadOnly);
if (ok)
{
QTextStream t(&mapFile);
if (!t.atEnd())
{
t >> mapName;
t >> x1 >> y1 >> x2 >> y2;
}
}
map.load(mapName);
if (map.isNull())
printf("map is null");
}
void MapWidget::slotZoom(int value)
{
qreal s;
if (value > zoom)
s = pow(1.01, (value-zoom));
else
s = pow((1/1.01), (zoom-value));
scale(s, s);
zoom = value;
}
void MapWidget::drawBackground(QPainter *painter, const QRectF &)
{
painter->drawPixmap(int(sceneRect().left()), int(sceneRect().top()), map);
}
void MapWidget::mouseMoveEvent(QMouseEvent *event)
{
// view coordinate
QPoint viewPoint = event->pos();
viewCoord->setText(QString::number(viewPoint.x()) + ", " + QString::number(viewPoint.y()));
// scene coordinate
QPointF scenePoint = mapToScene(viewPoint);
sceneCoord->setText(QString::number(scenePoint.x()) + ", " + QString::number(scenePoint.y()));
// map coordinate
QPointF latLon = mapToMap(scenePoint);
mapCoord->setText(QString::number(latLon.x()) + ", " + QString::number(latLon.y()));
}
// map scene coordinate to map
QPointF MapWidget::mapToMap(QPointF p)
{
QPointF latLon;
qreal w = sceneRect().width();
qreal h = sceneRect().height();
qreal lon = y1 - ((h/2 + p.y())*abs(y1-y2)/h);
qreal lat = x1 + ((w/2 + p.x())*abs(x1-x2)/w);
latLon.setX(lat);
latLon.setY(lon);
return latLon;
}
/** main.cpp
* 孤舟钓客
* 2011-07-25
*/
#include <QApplication>
#include "mapwidget.h"
int main(int argc, char * argv[])
{
QApplication app(argc, argv);
MapWidget map;
map.show();
return app.exec();
}
![](https://i-blog.csdnimg.cn/blog_migrate/94b06bf61a4f1d0408025185a5e10d43.jpeg)