#--------------------------------------------------------------------- # # mirrormap - A QGIS plugin to display a mirror map in another panel # # Copyright (C) 2009 Aaron Racicot, Z-Pulley Inc. # # EMAIL: aaronr (at) z-pulley.com # WEB : www.z-pulley.com # www.reprojected.com # #--------------------------------------------------------------------- # # licensed under the terms of GNU GPL 2 # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License along # with this program; if not, write to the Free Software Foundation, Inc., # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. # #--------------------------------------------------------------------- from PyQt4.QtCore import * from PyQt4.QtGui import * from qgis.core import * from qgis.gui import * import resources from mirrormap_ui import Ui_MirrorMapWindow import os import tempfile import math class MirrorMapWindow(QDialog, Ui_MirrorMapWindow): def __init__(self, iface, fl, plugin): QDialog.__init__(self, iface.mainWindow(), fl) self.setupUi(self) self.iface = iface self.plugin = plugin class MirrorMapPlugin(QObject): def __init__(self, iface): QObject.__init__(self) # Save a reference to the QGIS iface self.iface = iface self.canvas = iface.mapCanvas() self.mirrormap_gui = None def initGui(self): self.helpaction = QAction(QIcon(":/help.png"), "About", self.iface.mainWindow()) self.helpaction.setWhatsThis("MirrorMap Help") QObject.connect(self.helpaction, SIGNAL("activated()"), self.helprun) self.mirrormapaction = QAction(QIcon(":/mirrormapicon.png"), "MirrorMap", self.iface.mainWindow()) self.mirrormapaction.setWhatsThis("MirrorMap Plugin") QObject.connect(self.mirrormapaction, SIGNAL("activated()"), self.mirrormaprun) # Add to the main toolbar self.iface.addPluginToMenu("Mirror Map", self.helpaction) self.iface.addPluginToMenu("Mirror Map", self.mirrormapaction) self.iface.addToolBarIcon(self.mirrormapaction) pluginMenu = self.iface.pluginMenu() for action in pluginMenu.actions(): if action.text() == QString("Mirror Map"): # update the icon action.setIcon(QIcon(":/mirrormapicon.png")) # Set up the GUIS needed self.addrasteraction = QAction(QIcon(":/mActionAddRasterLayer.png"), "Add Raster", self.iface.mainWindow()) self.addrasteraction.setWhatsThis("Add Raster Layer") QObject.connect(self.addrasteraction, SIGNAL("activated()"), self.addRasterLayer) self.addograction = QAction(QIcon(":/mActionAddOgrLayer.png"), "Add OGR Vector", self.iface.mainWindow()) self.addograction.setWhatsThis("Add OGR Vector Layer") QObject.connect(self.addograction, SIGNAL("activated()"), self.addVectorLayer) self.adddbaction = QAction(QIcon(":/mActionAddDbLayer.png"), "Add Database", self.iface.mainWindow()) self.adddbaction.setWhatsThis("Add Database Layer") QObject.connect(self.adddbaction, SIGNAL("activated()"), self.helprun) self.addwmsaction = QAction(QIcon(":/mActionAddWmsLayer.png"), "Add WMS", self.iface.mainWindow()) self.addwmsaction.setWhatsThis("Add WMS Layer") QObject.connect(self.addwmsaction, SIGNAL("activated()"), self.helprun) flags = Qt.WindowTitleHint | Qt.WindowSystemMenuHint | Qt.WindowMaximizeButtonHint self.mirrormap_gui = MirrorMapWindow(self.iface,flags,self) self.mirrormap_gui.toolbar = QToolBar(self.iface.mainWindow()) self.mirrormap_gui.toolbar.setContentsMargins(0,0,0,0) self.mirrormap_gui.toolbar.addAction(self.addrasteraction) self.mirrormap_gui.toolbar.addAction(self.addograction) self.adddbaction.setEnabled(False) self.mirrormap_gui.toolbar.addAction(self.adddbaction) self.addwmsaction.setEnabled(False) self.mirrormap_gui.toolbar.addAction(self.addwmsaction) self.mirrormap_gui.toolbar_container.layout().addWidget(self.mirrormap_gui.toolbar,0,Qt.AlignHCenter) self.connect(self.mirrormap_gui.btnClose, SIGNAL("clicked()"), self.mirrormap_gui.hide) self.connect(self.mirrormap_gui.btnRemove, SIGNAL("clicked()"), self.removeCanvas) self.newCanvas = None self.newWidget = None self.layers = [] self.canvasSplit = False def removeCanvas(self): for layer in self.layers: QgsMapLayerRegistry.instance().removeMapLayer(layer.layer().getLayerID(),False) self.layers = [] self.disconnect(self.iface.mapCanvas(), SIGNAL("extentsChanged()"), self.coordChange) resize = self.newWidget.size() self.iface.mainWindow().setCentralWidget(self.iface.mapCanvas()) self.iface.mapCanvas().resize(resize) self.iface.mapCanvas().setMinimumSize(resize) self.canvasSplit = False self.mirrormap_gui.hide() def createSplit(self): mainWindow = self.iface.mainWindow() mapCanvas = self.iface.mapCanvas() mapCanvasSize = mapCanvas.size() mapCanvas.setMinimumSize(QSize(0,0)) newWidget = QWidget(mainWindow) self.newWidget = newWidget newWidget.resize(mapCanvasSize) newWidget.setMinimumSize(mapCanvasSize) #newWidget.setSizePolicy(QSizePolicy(QSizePolicy.Maximum,QSizePolicy.Maximum)) newLayout = QHBoxLayout(newWidget) newCanvas = QgsMapCanvas(mainWindow) newCanvas.setCanvasColor(QColor(255,255,255)) newCanvas.enableAntiAliasing(True) newCanvas.useImageToRender(False) newCanvas.show() newCanvas.resize(QSize(mapCanvasSize.width()/2,mapCanvasSize.height())) #newCanvas.setSizePolicy(QSizePolicy(QSizePolicy.Maximum,QSizePolicy.Maximum)) #newCanvas.setMinimumSize(QSize(mapCanvasSize.width()/2,mapCanvasSize.height())) mapCanvas.resize(QSize(mapCanvasSize.width()/2,mapCanvasSize.height())) #mapCanvas.setSizePolicy(QSizePolicy(QSizePolicy.Maximum,QSizePolicy.Maximum)) oldMapCanvasMinSize = mapCanvas.minimumSize() #mapCanvas.setMinimumSize(QSize(mapCanvasSize.width()/2,mapCanvasSize.height())) newLayout.addWidget(mapCanvas) newLayout.addWidget(newCanvas) mainWindow.setCentralWidget(newWidget) #newCanvas.setMinimumSize(oldMapCanvasMinSize) #mapCanvas.setMinimumSize(oldMapCanvasMinSize) self.newCanvas = newCanvas self.toolPan = QgsMapToolPan(self.newCanvas) # Default to pan tool # self.newCanvas.setMapTool(self.toolPan) self.connect(self.iface.mapCanvas(), SIGNAL("extentsChanged()"), self.coordChange) def coordChange(self): # print "coordChange" self.newCanvas.setExtent(self.iface.mapCanvas().extent()) self.newCanvas.refresh() def mirrormaprun(self): if self.canvasSplit == False: self.createSplit() self.canvasSplit = True self.mirrormap_gui.show() # print "mirrormaprun pressed" def unload(self): # Remove the plugin self.iface.removePluginMenu("Mirror Map",self.mirrormapaction) self.iface.removePluginMenu("Mirror Map",self.helpaction) self.iface.removeToolBarIcon(self.mirrormapaction) def helprun(self): # print "Help pressed..." infoString = QString("Written by Aaron Racicot\naaronr@z-pulley.com\n") infoString = infoString.append("Company - http://www.z-pulley.com\n\n") infoString = infoString.append("Blog - http://www.reprojected.com\n\n") infoString = infoString.append("Source: http://svn.reprojected.com/") infoString = infoString.append("qgisplugins/trunk/mirrormap\n") infoString = infoString.append("TRAC: http://trac.reprojected.com/") infoString = infoString.append("qgisplugins\n") QMessageBox.information(self.iface.mainWindow(), "MirrorMap",infoString) # Signal handeler for add layer button def addVectorLayer(self): qd=QFileDialog() filter_str = QString("*.shp") f=qd.getOpenFileName(self.iface.mainWindow(),QString(),QString(),filter_str) # Check for cancel if len(f) == 0: return info = QFileInfo(QString(f)) # create layer layer = QgsVectorLayer(QString(f), info.completeBaseName(), "ogr") if not layer.isValid(): capture_string = QString("ERROR reading file") return self.newCanvas.setExtent(self.iface.mapCanvas().extent()) QgsMapLayerRegistry.instance().addMapLayer(layer,False) # set the map canvas layer set cl = QgsMapCanvasLayer(layer) self.layers.insert(0,cl) self.newCanvas.setLayerSet(self.layers) self.newCanvas.refresh() # Signal handeler for add layer button def addRasterLayer(self): qd=QFileDialog() filter_str = QString("*.tif") f=qd.getOpenFileName(self.iface.mainWindow(),QString(),QString(),filter_str) # Check for cancel if len(f) == 0: return capture_string = QString(f) info = QFileInfo(QString(f)) # create layer layer = QgsRasterLayer(info.filePath(), info.completeBaseName()) if not layer.isValid(): capture_string = QString("ERROR reading file") return self.newCanvas.setExtent(self.iface.mapCanvas().extent()) # add layer to the registry QgsMapLayerRegistry.instance().addMapLayer(layer,False) # set the map canvas layer set cl = QgsMapCanvasLayer(layer) self.layers.insert(0,cl) self.newCanvas.setLayerSet(self.layers) self.newCanvas.refresh()