mapwidget.h
#pragma once
#include <QGraphicsView>
#include <QLabel>
#include <QMouseEvent>
class MapWidget : public QGraphicsView
{
Q_OBJECT
public:
MapWidget();
~MapWidget();
void readMap();
QPointF mapToMap(QPointF);
public slots:
void slotZoom(int value);
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;
};
mapwidget.cpp
#include "mapwidget.h"
#include "qslider.h"
#include <QGridLayout>
#include <QTextStream>
#include "qdebug.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("zoomin.png"));
QLabel *zoomoutlabel = new QLabel;
zoomoutlabel->setScaledContents(true);
zoomoutlabel->setPixmap(QPixmap("zoomout.png"));
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 *gridLayout = new QGridLayout;
gridLayout->addWidget(label1, 0, 0);
gridLayout->addWidget(viewCoord, 0, 1);
gridLayout->addWidget(label2, 1, 0);
gridLayout->addWidget(sceneCoord, 1, 1);
gridLayout->addWidget(label3, 2, 0);
gridLayout->addWidget(mapCoord, 2, 1);
gridLayout->setSizeConstraint(QLayout::SetFixedSize);
QFrame *coordFrame = new QFrame;
coordFrame->setLayout(gridLayout);
QVBoxLayout *zoomLaout = new QVBoxLayout;
zoomLaout->addWidget(zoominlabel);
zoomLaout->addWidget(slider);
zoomLaout->addWidget(zoomoutlabel);
QVBoxLayout *coordLayout = new QVBoxLayout;
coordLayout->addWidget(coordFrame);
coordLayout->addStretch();
QHBoxLayout *mainLayout = new QHBoxLayout;
mainLayout->addLayout(zoomLaout);
mainLayout->addLayout(coordLayout);
mainLayout->addStretch();
mainLayout->setMargin(30);
mainLayout->setSpacing(10);
setLayout(mainLayout);
setMinimumSize(600, 400);
}
MapWidget::~MapWidget()
{
}
void MapWidget::readMap()
{
QString mapName;
QFile mapFile("maps.txt");
int ok = mapFile.open(QIODevice::ReadOnly);
if (ok)
{
QTextStream ts(&mapFile);
if (!ts.atEnd())
{
ts >> mapName;
ts >> x1 >> y1 >> x2 >> y2;
}
}
map.load(mapName);
if (map.isNull())
{
qDebug() << "map is null";
}
}
QPointF MapWidget::mapToMap(QPointF p)
{
QPointF latLon;
int w = sceneRect().width();
int 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;
}
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 & rect)
{
painter->drawPixmap(int(sceneRect().left()), int(sceneRect().top()), map);
}
void MapWidget::mouseMoveEvent(QMouseEvent * event)
{
QPoint viewPoint = event->pos();
viewCoord->setText(QString::number(viewPoint.x()) + "," + QString::number(viewPoint.y()));
QPointF scenePoint = mapToScene(viewPoint);
sceneCoord->setText(QString::number(scenePoint.x()) + "," + QString::number(scenePoint.y()));
QPointF latLon = mapToMap(scenePoint);
mapCoord->setText(QString::number(latLon.x()) + "," + QString::number(latLon.y()));
}
main.cpp
#include "mapwidget.h"
#include <QtWidgets/QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MapWidget w;
w.show();
return a.exec();
}