QT_地圖導航

 

代碼已經更新:http://www.cnblogs.com/douzujun/p/6272667.htmlhtml

 

 1 //地圖顯示功能
 2 #ifndef MAPWIDGET_H
 3 #define MAPWIDGET_H
 4 
 5 #include <QGraphicsView>
 6 #include <QLabel>
 7 #include <QLineEdit>
 8 #include <QPushButton>
 9 #include <QMouseEvent>
10 
11 class MapWidget : public QGraphicsView
12 {
13     Q_OBJECT
14 public:
15 
16     MapWidget();
17     void readMap();                //讀取地圖信息
18     QPointF mapToMap(QPointF);     //用於實現場景座標系與地圖座標間的映射.以得到某點的經緯度
19 
20 public slots:
21     void slotZoom(int);            //縮放信號
22 protected:
23     //完成地圖的顯示功能
24     void drawBackground (QPainter *painter, const QRectF &rect);
25 
26     void mouseMoveEvent (QMouseEvent *event);
27 private:
28     QPixmap map;
29     qreal zoom;
30     QLabel *viewCoord;  //視圖目前座標(以窗口左上爲原點)
31     QLabel *sceneCoord; //場景目前座標(以場景中心爲原點)
32     QLabel *mapCoord;   //地圖目前座標
33 
34     double x1, y1;
35     double x2, y2;
36 
37 };
38 
39 #endif // MAPWIDGET_H
//mapwidget.cpp
#include "mapwidget.h"
#include <QSlider>         //滑動條
#include <QGridLayout>
#include <QFile>
#include <QTextStream>
#include <QGraphicsScene>
#include <math.h>

MapWidget::MapWidget()
{
    //讀取地圖信息--用於讀取描述地圖信息的文件(包括地圖名及經緯度等信息)
    readMap ();
    zoom = 50;
    int width = map.width ();
    int height = map.height ();
    //新建一個QGraphicsScene對象爲主窗口鏈接一個場景
    QGraphicsScene *scene = new QGraphicsScene(this);
    //限定場景的顯示區域爲地圖大小
    scene->setSceneRect (-width/2, -height/2, width, height);
    setScene (scene);
    /***
     * The background is cached(隱藏,緩存). This affects both custom backgrounds, and
     * backgrounds based on the backgroundBrush property. When this flag is enabled,
     * QGraphicsView will allocate one pixmap with the full size of the viewport
     */
    setCacheMode (CacheBackground);
    /***
     * 用於地圖縮放的滑動條
     * 新建一個QSlider對象做爲地圖的縮放控制
     */
    QSlider *slider = new QSlider;
    //設置滾動條方向-垂直
    slider->setOrientation (Qt::Vertical);
    slider->setRange (1, 100);        //設置地圖縮放比例值範圍爲0~100
    slider->setTickInterval (10);     //顯示刻度間隔爲10
    slider->setValue (0);            //設置當前初始值爲50
    // 將縮放控制條的valueChanged()信號與地圖縮放slotZoom()槽函數相關聯
    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 *zoomLayout = new QVBoxLayout;
    zoomLayout->addWidget (zoominLabel);
    zoomLayout->addWidget (slider);
    zoomLayout->addWidget (zoomoutLabel);

    //座標顯示區佈局
    QVBoxLayout *coordLayout = new QVBoxLayout;
    coordLayout->addWidget (coordFrame);
    coordLayout->addStretch ();
    //主佈局
    QHBoxLayout *mainLayout = new QHBoxLayout;
    mainLayout->addLayout (zoomLayout);
    mainLayout->addLayout (coordLayout);
    mainLayout->addStretch ();          //平均分割
    mainLayout->setMargin (30);         //設置對話框(或窗體)的邊距爲30
    mainLayout->setSpacing (10);        //設定各個控件之間的間距爲10
    setLayout (mainLayout);             //在場景中設置爲佈局
    setWindowTitle ("Map Widget");
    setMinimumSize (600, 400);
}

//讀取地圖信息
void MapWidget::readMap ()
{
    QString mapName;
    //新建一個QFile對象,「map.txt"是描述地圖信息的文本文件
    QFile mapFile("maps.txt");
    //以"只讀"的方式打開此文件
    int ok = mapFile.open (QIODevice::ReadOnly);
    if (ok)                   //分別以讀取地圖的名稱和四個經緯度信息
    {
        QTextStream ts(&mapFile);
        if (!ts.atEnd ()) {              //沒有到末尾返回false
            ts >> mapName;               //儲存字符串
            ts >> x1 >> y1 >> x2 >> y2;  //儲存地圖左上角和右下角的經緯度
        }
    }
    map.load (mapName);      //將地圖讀取至私有標量map中
}

//根據縮放滑動條的當前值,肯定縮放的比例,調用scale()函數實現地圖縮放--完成地圖縮放功能slotZoom
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;            //當前放大值
}

//drawBackground()--以地圖圖片重繪場景的背景來實現地圖顯示
void MapWidget::drawBackground (QPainter *painter, const QRectF &rect)
{
    /***
     * The scene rectangle defines the extent of the scene, and in the view's case,
     * this means the area of the scene that you can navigate using the scroll bars.
     */
    painter->drawPixmap (int(sceneRect ().left ()) + 10, int(sceneRect ().top ()) - 10, map);

//    //Demo 在圖片上繪線
//    QPen pen;
//    pen.setWidth (4);
//    pen.setColor (Qt::red);
//    painter->setPen (pen);
//    painter->setBrush (Qt::red);
//    painter->drawLine (100, 10, 100, 100);

}

//完成某點在各層座標中的映射及顯示
void MapWidget::mouseMoveEvent (QMouseEvent *event)
{
    //QGraphicesView 座標
    QPoint viewPoint = event->pos ();   //鼠標事件位置
    viewCoord->setText (QString::number (viewPoint.x ()) + "," + QString::number (viewPoint.y ()));

    //QGraphiccsScene 座標 -- 將視圖座標轉換成場景座標
    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 ()));


}

//完成從場景至地圖座標的轉換mapToMap()
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;
}
 1 //mainwidget
 2 #ifndef MAINWINDOW_H
 3 #define MAINWINDOW_H
 4 
 5 #include <QMainWindow>
 6 #include "mapwidget.h"
 7 #include <QToolButton>
 8 #include <QLabel>
 9 #include <QComboBox>
10 #include <QSpinBox>
11 #include <QTextEdit>
12 
13 class MainWindow : public QMainWindow
14 {
15     Q_OBJECT
16 
17 public:
18     MainWindow(QWidget *parent = 0);
19     ~MainWindow();
20     void createToolBar();
21     void paintEvent (QPaintEvent *);
22 public slots:
23     void setStartStation();
24     void setEndStation();
25     void FindPath();
26     void Clear();
27 private:
28     MapWidget *mapWidget;
29     QLabel *startLabel;
30     QLabel *endLabel;
31     QComboBox *startComboBox;
32     QComboBox *endComboBox;
33     QToolButton *findPathBtn;
34     QToolButton *clearBtn;
35 };
36 
37 #endif // MAINWINDOW_H
 1 //佈局實現-mainwindow.cpp
 2 
 3 #include "mainwindow.h"
 4 #include <QToolBar>
 5 
 6 MainWindow::MainWindow(QWidget *parent)
 7     : QMainWindow(parent)
 8 {
 9     mapWidget = new MapWidget;
10     createToolBar ();  //實現一個工具欄
11     setCentralWidget (mapWidget);
12     setMinimumSize (600, 400);  //設置最小尺寸
13 }
14 
15 MainWindow::~MainWindow()
16 {
17 }
18 
19 void MainWindow::createToolBar ()
20 {
21     QToolBar *toolBar = addToolBar ("Tool");
22     startLabel = new QLabel(tr("起點: "));
23     startComboBox = new QComboBox;
24     startComboBox->addItem (tr("公寓6號樓")); startComboBox->addItem (tr("公寓10號樓"));
25     startComboBox->addItem (tr("公寓5號樓")); startComboBox->addItem (tr("公寓9號樓"));
26     startComboBox->addItem (tr("公寓4號樓")); startComboBox->addItem (tr("公寓8號樓"));
27     startComboBox->addItem (tr("公寓3號樓")); startComboBox->addItem (tr("公寓7號樓"));
28     startComboBox->addItem (tr("公寓2號樓"));
29     startComboBox->addItem (tr("公寓1號樓")); startComboBox->addItem (tr("圖書館"));
30     startComboBox->addItem (tr("一食堂")); startComboBox->addItem (tr("西操場"));
31     startComboBox->addItem (tr("公寓23號樓")); startComboBox->addItem (tr("公寓13號樓"));
32     startComboBox->addItem (tr("公寓22號樓")); startComboBox->addItem (tr("公寓12號樓"));
33 
34     endLabel = new QLabel(tr("\t終點: "));
35 
36     endComboBox = new QComboBox;
37     endComboBox->addItem (tr("公寓6號樓")); endComboBox->addItem (tr("公寓10號樓"));
38     endComboBox->addItem (tr("公寓5號樓")); endComboBox->addItem (tr("公寓9號樓"));
39     endComboBox->addItem (tr("公寓4號樓")); endComboBox->addItem (tr("公寓8號樓"));
40     endComboBox->addItem (tr("公寓3號樓")); endComboBox->addItem (tr("公寓7號樓"));
41     endComboBox->addItem (tr("公寓2號樓"));
42     endComboBox->addItem (tr("公寓1號樓")); endComboBox->addItem (tr("圖書館"));
43     endComboBox->addItem (tr("一食堂"));    endComboBox->addItem (tr("西操場"));
44     endComboBox->addItem (tr("公寓23號樓"));endComboBox->addItem (tr("公寓13號樓"));
45     endComboBox->addItem (tr("公寓22號樓"));endComboBox->addItem (tr("公寓12號樓"));
46 
47     connect (startComboBox, SIGNAL(activated(int)), this, SLOT(setStartStation()));
48     connect (endComboBox, SIGNAL(activated(int)), this, SLOT(setEndStation()));
49 
50     findPathBtn = new QToolButton;
51     findPathBtn->setText (tr("\t\t繪製最短路徑"));
52 
53     connect (findPathBtn, SIGNAL(clicked(bool)), this, SLOT(FindPath()));
54 
55     clearBtn = new QToolButton;
56     clearBtn->setText (tr("\t\t清除"));
57 
58     connect (clearBtn, SIGNAL(clicked(bool)), this, SLOT(Clear()));
59 
60     toolBar->addWidget (startLabel);
61     toolBar->addWidget (startComboBox);
62     toolBar->addWidget (endLabel);
63     toolBar->addWidget (endComboBox);
64     toolBar->addWidget (findPathBtn);
65     toolBar->addWidget (clearBtn);
66 }
67 
68 void MainWindow::setStartStation ()
69 {
70 
71 }
72 
73 void MainWindow::setEndStation ()
74 {
75 
76 }
77 
78 void MainWindow::FindPath ()
79 {
80 
81 }
82 
83 void MainWindow::Clear ()
84 {
85 
86 }
87 
88 void MainWindow::paintEvent (QPaintEvent *)
89 {
90 
91 }
相關文章
相關標籤/搜索