Qt 地图浏览器:使用QGraphicsView

/** 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();
}


  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值