【PyQt4实例10】地图浏览器

#-*- coding:utf-8 -*-

from PyQt4.QtCore import *
from PyQt4.QtGui import *
import math

QTextCodec.setCodecForTr(QTextCodec.codecForName("utf-8"))

class MapWidget(QGraphicsView):
    def __init__(self):
        super(MapWidget,self).__init__()

        self.zoom = 50
        self.map = QPixmap()
        self.x1 = QString()
        self.x2 = QString()
        self.y1 = QString()
        self.y2 = QString()
        self.readMap()
        self.width = self.map.width()
        self.height = self.map.height()
        
        self.scene = QGraphicsScene(self)
        self.scene.setSceneRect(-self.width/2,-self.height/2,self.width,self.height)
        self.setScene(self.scene)
        self.setCacheMode(self.CacheBackground)
        
        self.slider = QSlider()
        self.slider.setOrientation(Qt.Vertical)
        self.slider.setRange(1,100)
        self.slider.setTickInterval(10)
        self.slider.setValue(50)
        self.connect(self.slider,SIGNAL("valueChanged(int)"),self.slotZoom)
        
        self.zoominLabel = QLabel()
        self.zoominLabel.setScaledContents(True)
        self.zoominLabel.setPixmap(QPixmap("image/zoomin.png"))
        
        self.zoomoutLabel = QLabel()
        self.zoomoutLabel.setScaledContents(True)
        self.zoomoutLabel.setPixmap(QPixmap("image/deflate.png"))
        
        self.coordFrame = QFrame()
        self.label1 = QLabel(self.tr("GraphicsView:"))
        
        self.viewCoord = QLabel()
        self.label2 = QLabel(self.tr("GraphicsScene:"))
        
        self.sceneCoord = QLabel()
        self.label3 = QLabel(self.tr("map:"))
        self.mapCoord = QLabel()
        
        self.grid = QGridLayout()
        self.grid.addWidget(self.label1,0,0)
        self.grid.addWidget(self.viewCoord,0,1)
        self.grid.addWidget(self.label2,1,0)
        self.grid.addWidget(self.sceneCoord,1,1)
        self.grid.addWidget(self.label3,2,0)
        self.grid.addWidget(self.mapCoord,2,1)
        self.grid.setSizeConstraint(QLayout.SetFixedSize)
        self.coordFrame.setLayout(self.grid)
        
        self.zoomLayout = QVBoxLayout()
        self.zoomLayout.addWidget(self.zoominLabel)
        self.zoomLayout.addWidget(self.slider)
        self.zoomLayout.addWidget(self.zoomoutLabel)
        
        self.coordLayout = QVBoxLayout()
        self.coordLayout.addWidget(self.coordFrame)
        self.coordLayout.addStretch()
        
        self.layout = QHBoxLayout()
        self.layout.addLayout(self.zoomLayout)
        self.layout.addLayout(self.coordLayout) 
        self.layout.addStretch()
        self.layout.setMargin(30)
        self.layout.setSpacing(10)
        self.setLayout(self.layout)
        
        self.setWindowTitle(self.tr("地图浏览器"))
        self.setMinimumSize(600,400)
        
        

    def drawBackground(self,painter,rect):
        painter.drawPixmap(int(self.sceneRect().left()),int(self.sceneRect().top()),self.map)
        
    def readMap(self):
        mapFile = QFile("maps.txt")
        mapName = QString()
        
        ok = mapFile.open(QIODevice.ReadOnly)
        if(ok is True):
            t = QTextStream(mapFile)
            if(t.atEnd() is False):
                t >> mapName
                t >> self.x1 >> self.y1 >> self.x2 >> self.y2

        self.map.load(mapName)
    
    def slotZoom(self,v):
        if(v > self.zoom):
            s = math.pow(1.01,(v - self.zoom))
        else:
            s = math.pow((1/1.01),(self.zoom - v))
        self.scale(s,s)
        self.zoom = v
    
    def mouseMoveEvent(self,event):
        viewPoint = event.pos()
        self.viewCoord.setText(QString.number(viewPoint.x()) + "," + QString.number(viewPoint.y()))
        
        scenePoint = self.mapToScene(viewPoint)
        self.sceneCoord.setText(QString.number(scenePoint.x()) + "," + QString.number(scenePoint.y()))
        
        latLon = self.mapToMap(scenePoint)
        self.mapCoord.setText(QString.number(latLon.x()) + "," + QString.number(latLon.y()))
    
    def mapToMap(self,p):
        latLon = QPointF()
        w = self.sceneRect().width()
        h = self.sceneRect().height()
        self.x1= float(self.x1)
        self.x2= float(self.x2)
        self.y1= float(self.y1)
        self.y2= float(self.y2)
        
        lon = self.y1 - ((h/2 + p.y()) * abs(self.y1 - self.y2)/h)
        lat = self.x1 + ((w/2 + p.x()) * abs(self.x1 - self.x2)/w)
        latLon.setX(lat)
        latLon.setY(lon)
        return latLon
        

if __name__ == '__main__':
    import sys
    app = QApplication(sys.argv)
    mainwindow = MapWidget()
    mainwindow.show()
    sys.exit(app.exec_())

上述代码中的maps.txt文件如下:

aa.png 114.4665527 35.96022297 119.9597168 31.3911575


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值