def contextMenuEvent(self, ev): index = self.indexAt(ev.pos()) if not index.isValid(): return if index != self.currentIndex(): self.itemChanged(index) item = self.currentItem() menu = QMenu(self) if isinstance(item, (Table, Schema)): menu.addAction(self.tr("Rename"), self.rename) menu.addAction(self.tr("Delete"), self.delete) if isinstance(item, Table) and item.canBeAddedToCanvas(): menu.addSeparator() menu.addAction(self.tr("Add to canvas"), self.addLayer) elif isinstance(item, DBPlugin): if item.database() is not None: menu.addAction(self.tr("Re-connect"), self.reconnect) menu.addAction(self.tr("Remove"), self.delete) elif not index.parent().isValid() and item.typeName() == "spatialite": menu.addAction(self.tr("New Connection..."), self.newConnection) if not menu.isEmpty(): menu.exec_(ev.globalPos()) menu.deleteLater()
def showPopupMenu(self, point): item = self.algorithmTree.itemAt(point) if isinstance(item, TreeAlgorithmItem): alg = item.alg popupmenu = QMenu() executeAction = QAction(self.tr('Execute'), self.algorithmTree) executeAction.triggered.connect(self.executeAlgorithm) popupmenu.addAction(executeAction) if alg.canRunInBatchMode and not alg.allowOnlyOpenedLayers: executeBatchAction = QAction( self.tr('Execute as batch process'), self.algorithmTree) executeBatchAction.triggered.connect( self.executeAlgorithmAsBatchProcess) popupmenu.addAction(executeBatchAction) popupmenu.addSeparator() editRenderingStylesAction = QAction( self.tr('Edit rendering styles for outputs'), self.algorithmTree) editRenderingStylesAction.triggered.connect( self.editRenderingStyles) popupmenu.addAction(editRenderingStylesAction) actions = Processing.contextMenuActions if len(actions) > 0: popupmenu.addSeparator() for action in actions: action.setData(alg, self) if action.isEnabled(): contextMenuAction = QAction(action.name, self.algorithmTree) contextMenuAction.triggered.connect(action.execute) popupmenu.addAction(contextMenuAction) popupmenu.exec_(self.algorithmTree.mapToGlobal(point))
def contextMenuEvent(self, e): menu = QMenu(self) iconRun = QgsApplication.getThemeIcon("console/iconRunConsole.png") iconClear = QgsApplication.getThemeIcon("console/iconClearConsole.png") iconHideTool = QgsApplication.getThemeIcon( "console/iconHideToolConsole.png") iconSettings = QgsApplication.getThemeIcon( "console/iconSettingsConsole.png") menu.addAction( iconHideTool, QCoreApplication.translate("PythonConsole", "Hide/Show Toolbar"), self.hideToolBar) menu.addSeparator() showEditorAction = menu.addAction( QCoreApplication.translate("PythonConsole", "Show Editor"), self.showEditor) menu.addSeparator() runAction = menu.addAction( iconRun, QCoreApplication.translate("PythonConsole", "Enter Selected"), self.enteredSelected, QKeySequence(Qt.CTRL + Qt.Key_E)) clearAction = menu.addAction( iconClear, QCoreApplication.translate("PythonConsole", "Clear Console"), self.clearConsole) menu.addSeparator() copyAction = menu.addAction( QCoreApplication.translate("PythonConsole", "Copy"), self.copy, QKeySequence.Copy) selectAllAction = menu.addAction( QCoreApplication.translate("PythonConsole", "Select All"), self.selectAll, QKeySequence.SelectAll) menu.addSeparator() menu.addAction( iconSettings, QCoreApplication.translate("PythonConsole", "Options..."), self.parent.openSettings) runAction.setEnabled(False) clearAction.setEnabled(False) copyAction.setEnabled(False) selectAllAction.setEnabled(False) showEditorAction.setEnabled(True) if self.hasSelectedText(): runAction.setEnabled(True) copyAction.setEnabled(True) if not self.text(3) == '': selectAllAction.setEnabled(True) clearAction.setEnabled(True) if self.parent.tabEditorWidget.isVisible(): showEditorAction.setEnabled(False) menu.exec_(self.mapToGlobal(e.pos()))
def contextMenuEvent(self, e): menu = QMenu(self) subMenu = QMenu(menu) titleHistoryMenu = QCoreApplication.translate("PythonConsole", "Command History") subMenu.setTitle(titleHistoryMenu) subMenu.addAction( QCoreApplication.translate("PythonConsole", "Show"), self.showHistory, 'Ctrl+Shift+SPACE') subMenu.addSeparator() subMenu.addAction( QCoreApplication.translate("PythonConsole", "Save"), self.writeHistoryFile) subMenu.addSeparator() subMenu.addAction( QCoreApplication.translate("PythonConsole", "Clear File"), self.clearHistory) subMenu.addAction( QCoreApplication.translate("PythonConsole", "Clear Session"), self.clearHistorySession) menu.addMenu(subMenu) menu.addSeparator() copyAction = menu.addAction( QCoreApplication.translate("PythonConsole", "Copy"), self.copy, QKeySequence.Copy) pasteAction = menu.addAction( QCoreApplication.translate("PythonConsole", "Paste"), self.paste, QKeySequence.Paste) copyAction.setEnabled(False) pasteAction.setEnabled(False) if self.hasSelectedText(): copyAction.setEnabled(True) if QApplication.clipboard().text(): pasteAction.setEnabled(True) menu.exec_(self.mapToGlobal(e.pos()))
def showPopupMenu(self, point): item = self.algorithmTree.itemAt(point) if isinstance(item, TreeAlgorithmItem): alg = item.alg popupmenu = QMenu() executeAction = QAction(self.tr('Execute'), self.algorithmTree) executeAction.triggered.connect(self.executeAlgorithm) popupmenu.addAction(executeAction) if alg.canRunInBatchMode and not alg.allowOnlyOpenedLayers: executeBatchAction = QAction( self.tr('Execute as batch process'), self.algorithmTree) executeBatchAction.triggered.connect( self.executeAlgorithmAsBatchProcess) popupmenu.addAction(executeBatchAction) popupmenu.addSeparator() editRenderingStylesAction = QAction( self.tr('Edit rendering styles for outputs'), self.algorithmTree) editRenderingStylesAction.triggered.connect( self.editRenderingStyles) popupmenu.addAction(editRenderingStylesAction) if isinstance(item, (TreeAlgorithmItem, TreeActionItem)): data = item.alg if isinstance(item, TreeAlgorithmItem) else item.action actions = Processing.contextMenuActions if len(actions) > 0: popupmenu.addSeparator() for action in actions: action.setData(data, self) if action.isEnabled(): contextMenuAction = QAction(action.name, self.algorithmTree) contextMenuAction.triggered.connect(action.execute) popupmenu.addAction(contextMenuAction) popupmenu.exec_(self.algorithmTree.mapToGlobal(point))
def contextMenuEvent(self, e): menu = QMenu(self) iconRun = QgsApplication.getThemeIcon("console/iconRunConsole.png") iconClear = QgsApplication.getThemeIcon("console/iconClearConsole.png") iconHideTool = QgsApplication.getThemeIcon("console/iconHideToolConsole.png") iconSettings = QgsApplication.getThemeIcon("console/iconSettingsConsole.png") menu.addAction(iconHideTool, QCoreApplication.translate("PythonConsole", "Hide/Show Toolbar"), self.hideToolBar) menu.addSeparator() showEditorAction = menu.addAction( QCoreApplication.translate("PythonConsole", "Show Editor"), self.showEditor) menu.addSeparator() runAction = menu.addAction(iconRun, QCoreApplication.translate("PythonConsole", "Enter Selected"), self.enteredSelected, QKeySequence(Qt.CTRL + Qt.Key_E)) clearAction = menu.addAction(iconClear, QCoreApplication.translate("PythonConsole", "Clear Console"), self.clearConsole) menu.addSeparator() copyAction = menu.addAction( QCoreApplication.translate("PythonConsole", "Copy"), self.copy, QKeySequence.Copy) selectAllAction = menu.addAction( QCoreApplication.translate("PythonConsole", "Select All"), self.selectAll, QKeySequence.SelectAll) menu.addSeparator() menu.addAction(iconSettings, QCoreApplication.translate("PythonConsole", "Options..."), self.parent.openSettings) runAction.setEnabled(False) clearAction.setEnabled(False) copyAction.setEnabled(False) selectAllAction.setEnabled(False) showEditorAction.setEnabled(True) if self.hasSelectedText(): runAction.setEnabled(True) copyAction.setEnabled(True) if not self.text(3) == '': selectAllAction.setEnabled(True) clearAction.setEnabled(True) if self.parent.tabEditorWidget.isVisible(): showEditorAction.setEnabled(False) menu.exec_(self.mapToGlobal(e.pos()))
class GdalTools: def __init__(self, iface): if not valid: return # Save reference to the QGIS interface self.iface = iface try: self.QgisVersion = unicode(QGis.QGIS_VERSION_INT) except: self.QgisVersion = unicode(QGis.qgisVersion)[0] if QGis.QGIS_VERSION[0:3] < "1.5": # For i18n support userPluginPath = qgis.utils.home_plugin_path + "/GdalTools" systemPluginPath = qgis.utils.sys_plugin_path + "/GdalTools" overrideLocale = QSettings().value("locale/overrideFlag", False, type=bool) if not overrideLocale: localeFullName = QLocale.system().name() else: localeFullName = QSettings().value("locale/userLocale", "", type=str) if QFileInfo(userPluginPath).exists(): translationPath = userPluginPath + "/i18n/GdalTools_" + localeFullName + ".qm" else: translationPath = systemPluginPath + "/i18n/GdalTools_" + localeFullName + ".qm" self.localePath = translationPath if QFileInfo(self.localePath).exists(): self.translator = QTranslator() self.translator.load(self.localePath) QCoreApplication.installTranslator(self.translator) # The list of actions added to menus, so we can remove them when unloading the plugin self._menuActions = [] def initGui(self): if not valid: return if int(self.QgisVersion) < 1: QMessageBox.warning( self.iface.getMainWindow(), "Gdal Tools", QCoreApplication.translate("GdalTools", "QGIS version detected: ") + unicode(self.QgisVersion) + ".xx\n" + QCoreApplication.translate("GdalTools", "This version of Gdal Tools requires at least QGIS version 1.0.0\nPlugin will not be enabled.")) return None from .tools.GdalTools_utils import GdalConfig, LayerRegistry self.GdalVersionNum = GdalConfig.versionNum() LayerRegistry.setIface(self.iface) # find the Raster menu rasterMenu = None menu_bar = self.iface.mainWindow().menuBar() actions = menu_bar.actions() rasterText = QCoreApplication.translate("QgisApp", "&Raster") for a in actions: if a.menu() is not None and a.menu().title() == rasterText: rasterMenu = a.menu() break if rasterMenu is None: # no Raster menu, create and insert it before the Help menu self.menu = QMenu(rasterText, self.iface.mainWindow()) lastAction = actions[len(actions) - 1] menu_bar.insertMenu(lastAction, self.menu) else: self.menu = rasterMenu self._menuActions.append(self.menu.addSeparator()) # projections menu (Warp (Reproject), Assign projection) self.projectionsMenu = QMenu(QCoreApplication.translate("GdalTools", "Projections"), self.iface.mainWindow()) self.projectionsMenu.setObjectName("projectionsMenu") self.warp = QAction(QIcon(":/icons/warp.png"), QCoreApplication.translate("GdalTools", "Warp (Reproject)..."), self.iface.mainWindow()) self.warp.setObjectName("warp") self.warp.setStatusTip(QCoreApplication.translate("GdalTools", "Warp an image into a new coordinate system")) self.warp.triggered.connect(self.doWarp) self.projection = QAction(QIcon(":icons/projection-add.png"), QCoreApplication.translate("GdalTools", "Assign Projection..."), self.iface.mainWindow()) self.projection.setObjectName("projection") self.projection.setStatusTip(QCoreApplication.translate("GdalTools", "Add projection info to the raster")) self.projection.triggered.connect(self.doProjection) self.extractProj = QAction(QIcon(":icons/projection-export.png"), QCoreApplication.translate("GdalTools", "Extract Projection..."), self.iface.mainWindow()) self.extractProj.setObjectName("extractProj") self.extractProj.setStatusTip(QCoreApplication.translate("GdalTools", "Extract projection information from raster(s)")) self.extractProj.triggered.connect(self.doExtractProj) self.projectionsMenu.addActions([self.warp, self.projection, self.extractProj]) # conversion menu (Rasterize (Vector to raster), Polygonize (Raster to vector), Translate, RGB to PCT, PCT to RGB) self.conversionMenu = QMenu(QCoreApplication.translate("GdalTools", "Conversion"), self.iface.mainWindow()) self.conversionMenu.setObjectName("conversionMenu") if self.GdalVersionNum >= 1300: self.rasterize = QAction(QIcon(":/icons/rasterize.png"), QCoreApplication.translate("GdalTools", "Rasterize (Vector to Raster)..."), self.iface.mainWindow()) self.rasterize.setObjectName("rasterize") self.rasterize.setStatusTip(QCoreApplication.translate("GdalTools", "Burns vector geometries into a raster")) self.rasterize.triggered.connect(self.doRasterize) self.conversionMenu.addAction(self.rasterize) if self.GdalVersionNum >= 1600: self.polygonize = QAction(QIcon(":/icons/polygonize.png"), QCoreApplication.translate("GdalTools", "Polygonize (Raster to Vector)..."), self.iface.mainWindow()) self.polygonize.setObjectName("polygonize") self.polygonize.setStatusTip(QCoreApplication.translate("GdalTools", "Produces a polygon feature layer from a raster")) self.polygonize.triggered.connect(self.doPolygonize) self.conversionMenu.addAction(self.polygonize) self.translate = QAction(QIcon(":/icons/translate.png"), QCoreApplication.translate("GdalTools", "Translate (Convert Format)..."), self.iface.mainWindow()) self.translate.setObjectName("translate") self.translate.setStatusTip(QCoreApplication.translate("GdalTools", "Converts raster data between different formats")) self.translate.triggered.connect(self.doTranslate) self.paletted = QAction(QIcon(":icons/24-to-8-bits.png"), QCoreApplication.translate("GdalTools", "RGB to PCT..."), self.iface.mainWindow()) self.paletted.setObjectName("paletted") self.paletted.setStatusTip(QCoreApplication.translate("GdalTools", "Convert a 24bit RGB image to 8bit paletted")) self.paletted.triggered.connect(self.doPaletted) self.rgb = QAction(QIcon(":icons/8-to-24-bits.png"), QCoreApplication.translate("GdalTools", "PCT to RGB..."), self.iface.mainWindow()) self.rgb.setObjectName("rgb") self.rgb.setStatusTip(QCoreApplication.translate("GdalTools", "Convert an 8bit paletted image to 24bit RGB")) self.rgb.triggered.connect(self.doRGB) self.conversionMenu.addActions([self.translate, self.paletted, self.rgb]) # extraction menu (Clipper, Contour) self.extractionMenu = QMenu(QCoreApplication.translate("GdalTools", "Extraction"), self.iface.mainWindow()) self.extractionMenu.setObjectName("extractionMenu") if self.GdalVersionNum >= 1600: self.contour = QAction(QIcon(":/icons/contour.png"), QCoreApplication.translate("GdalTools", "Contour..."), self.iface.mainWindow()) self.contour.setObjectName("contour") self.contour.setStatusTip(QCoreApplication.translate("GdalTools", "Builds vector contour lines from a DEM")) self.contour.triggered.connect(self.doContour) self.extractionMenu.addAction(self.contour) self.clipper = QAction(QIcon(":icons/raster-clip.png"), QCoreApplication.translate("GdalTools", "Clipper..."), self.iface.mainWindow()) self.clipper.setObjectName("clipper") #self.clipper.setStatusTip( QCoreApplication.translate( "GdalTools", "Converts raster data between different formats") ) self.clipper.triggered.connect(self.doClipper) self.extractionMenu.addActions([self.clipper]) # analysis menu (DEM (Terrain model), Grid (Interpolation), Near black, Proximity (Raster distance), Sieve) self.analysisMenu = QMenu(QCoreApplication.translate("GdalTools", "Analysis"), self.iface.mainWindow()) self.analysisMenu.setObjectName("analysisMenu") if self.GdalVersionNum >= 1600: self.sieve = QAction(QIcon(":/icons/sieve.png"), QCoreApplication.translate("GdalTools", "Sieve..."), self.iface.mainWindow()) self.sieve.setObjectName("sieve") self.sieve.setStatusTip(QCoreApplication.translate("GdalTools", "Removes small raster polygons")) self.sieve.triggered.connect(self.doSieve) self.analysisMenu.addAction(self.sieve) if self.GdalVersionNum >= 1500: self.nearBlack = QAction(QIcon(":/icons/nearblack.png"), QCoreApplication.translate("GdalTools", "Near Black..."), self.iface.mainWindow()) self.nearBlack.setObjectName("nearBlack") self.nearBlack.setStatusTip(QCoreApplication.translate("GdalTools", "Convert nearly black/white borders to exact value")) self.nearBlack.triggered.connect(self.doNearBlack) self.analysisMenu.addAction(self.nearBlack) if self.GdalVersionNum >= 1700: self.fillNodata = QAction(QIcon(":/icons/fillnodata.png"), QCoreApplication.translate("GdalTools", "Fill nodata..."), self.iface.mainWindow()) self.fillNodata.setObjectName("fillNodata") self.fillNodata.setStatusTip(QCoreApplication.translate("GdalTools", "Fill raster regions by interpolation from edges")) self.fillNodata.triggered.connect(self.doFillNodata) self.analysisMenu.addAction(self.fillNodata) if self.GdalVersionNum >= 1600: self.proximity = QAction(QIcon(":/icons/proximity.png"), QCoreApplication.translate("GdalTools", "Proximity (Raster Distance)..."), self.iface.mainWindow()) self.proximity.setObjectName("proximity") self.proximity.setStatusTip(QCoreApplication.translate("GdalTools", "Produces a raster proximity map")) self.proximity.triggered.connect(self.doProximity) self.analysisMenu.addAction(self.proximity) if self.GdalVersionNum >= 1500: self.grid = QAction(QIcon(":/icons/grid.png"), QCoreApplication.translate("GdalTools", "Grid (Interpolation)..."), self.iface.mainWindow()) self.grid.setObjectName("grid") self.grid.setStatusTip(QCoreApplication.translate("GdalTools", "Create raster from the scattered data")) self.grid.triggered.connect(self.doGrid) self.analysisMenu.addAction(self.grid) if self.GdalVersionNum >= 1700: self.dem = QAction(QIcon(":icons/dem.png"), QCoreApplication.translate("GdalTools", "DEM (Terrain Models)..."), self.iface.mainWindow()) self.dem.setObjectName("dem") self.dem.setStatusTip(QCoreApplication.translate("GdalTools", "Tool to analyze and visualize DEMs")) self.dem.triggered.connect(self.doDEM) self.analysisMenu.addAction(self.dem) #self.analysisMenu.addActions( [ ] ) # miscellaneous menu (Build overviews (Pyramids), Tile index, Information, Merge, Build Virtual Raster (Catalog)) self.miscellaneousMenu = QMenu(QCoreApplication.translate("GdalTools", "Miscellaneous"), self.iface.mainWindow()) self.miscellaneousMenu.setObjectName("miscellaneousMenu") if self.GdalVersionNum >= 1600: self.buildVRT = QAction(QIcon(":/icons/vrt.png"), QCoreApplication.translate("GdalTools", "Build Virtual Raster (Catalog)..."), self.iface.mainWindow()) self.buildVRT.setObjectName("buildVRT") self.buildVRT.setStatusTip(QCoreApplication.translate("GdalTools", "Builds a VRT from a list of datasets")) self.buildVRT.triggered.connect(self.doBuildVRT) self.miscellaneousMenu.addAction(self.buildVRT) self.merge = QAction(QIcon(":/icons/merge.png"), QCoreApplication.translate("GdalTools", "Merge..."), self.iface.mainWindow()) self.merge.setObjectName("merge") self.merge.setStatusTip(QCoreApplication.translate("GdalTools", "Build a quick mosaic from a set of images")) self.merge.triggered.connect(self.doMerge) self.info = QAction(QIcon(":/icons/raster-info.png"), QCoreApplication.translate("GdalTools", "Information..."), self.iface.mainWindow()) self.info.setObjectName("info") self.info.setStatusTip(QCoreApplication.translate("GdalTools", "Lists information about raster dataset")) self.info.triggered.connect(self.doInfo) self.overview = QAction(QIcon(":icons/raster-overview.png"), QCoreApplication.translate("GdalTools", "Build Overviews (Pyramids)..."), self.iface.mainWindow()) self.overview.setObjectName("overview") self.overview.setStatusTip(QCoreApplication.translate("GdalTools", "Builds or rebuilds overview images")) self.overview.triggered.connect(self.doOverview) self.tileindex = QAction(QIcon(":icons/tiles.png"), QCoreApplication.translate("GdalTools", "Tile Index..."), self.iface.mainWindow()) self.tileindex.setObjectName("tileindex") self.tileindex.setStatusTip(QCoreApplication.translate("GdalTools", "Build a shapefile as a raster tileindex")) self.tileindex.triggered.connect(self.doTileIndex) self.miscellaneousMenu.addActions([self.merge, self.info, self.overview, self.tileindex]) self._menuActions.append(self.menu.addMenu(self.projectionsMenu)) self._menuActions.append(self.menu.addMenu(self.conversionMenu)) self._menuActions.append(self.menu.addMenu(self.extractionMenu)) if not self.analysisMenu.isEmpty(): self._menuActions.append(self.menu.addMenu(self.analysisMenu)) self._menuActions.append(self.menu.addMenu(self.miscellaneousMenu)) self.settings = QAction(QCoreApplication.translate("GdalTools", "GdalTools Settings..."), self.iface.mainWindow()) self.settings.setObjectName("settings") self.settings.setStatusTip(QCoreApplication.translate("GdalTools", "Various settings for Gdal Tools")) self.settings.triggered.connect(self.doSettings) self.menu.addAction(self.settings) self._menuActions.append(self.settings) def unload(self): if not valid: return for a in self._menuActions: self.menu.removeAction(a) def doBuildVRT(self): from .tools.doBuildVRT import GdalToolsDialog as BuildVRT d = BuildVRT(self.iface) self.runToolDialog(d) def doContour(self): from .tools.doContour import GdalToolsDialog as Contour d = Contour(self.iface) self.runToolDialog(d) def doRasterize(self): from .tools.doRasterize import GdalToolsDialog as Rasterize d = Rasterize(self.iface) self.runToolDialog(d) def doPolygonize(self): from .tools.doPolygonize import GdalToolsDialog as Polygonize d = Polygonize(self.iface) self.runToolDialog(d) def doMerge(self): from .tools.doMerge import GdalToolsDialog as Merge d = Merge(self.iface) self.runToolDialog(d) def doSieve(self): from .tools.doSieve import GdalToolsDialog as Sieve d = Sieve(self.iface) self.runToolDialog(d) def doProximity(self): from .tools.doProximity import GdalToolsDialog as Proximity d = Proximity(self.iface) self.runToolDialog(d) def doNearBlack(self): from .tools.doNearBlack import GdalToolsDialog as NearBlack d = NearBlack(self.iface) self.runToolDialog(d) def doFillNodata(self): from .tools.doFillNodata import GdalToolsDialog as FillNodata d = FillNodata(self.iface) self.runToolDialog(d) def doWarp(self): from .tools.doWarp import GdalToolsDialog as Warp d = Warp(self.iface) self.runToolDialog(d) def doGrid(self): from .tools.doGrid import GdalToolsDialog as Grid d = Grid(self.iface) self.runToolDialog(d) def doTranslate(self): from .tools.doTranslate import GdalToolsDialog as Translate d = Translate(self.iface) self.runToolDialog(d) def doInfo(self): from .tools.doInfo import GdalToolsDialog as Info d = Info(self.iface) self.runToolDialog(d) def doProjection(self): from .tools.doProjection import GdalToolsDialog as Projection d = Projection(self.iface) self.runToolDialog(d) def doOverview(self): from .tools.doOverview import GdalToolsDialog as Overview d = Overview(self.iface) self.runToolDialog(d) def doClipper(self): from .tools.doClipper import GdalToolsDialog as Clipper d = Clipper(self.iface) self.runToolDialog(d) def doPaletted(self): from .tools.doRgbPct import GdalToolsDialog as RgbPct d = RgbPct(self.iface) self.runToolDialog(d) def doRGB(self): from .tools.doPctRgb import GdalToolsDialog as PctRgb d = PctRgb(self.iface) self.runToolDialog(d) def doTileIndex(self): from .tools.doTileIndex import GdalToolsDialog as TileIndex d = TileIndex(self.iface) self.runToolDialog(d) def doExtractProj(self): from .tools.doExtractProj import GdalToolsDialog as ExtractProj d = ExtractProj(self.iface) d.exec_() def doDEM(self): from .tools.doDEM import GdalToolsDialog as DEM d = DEM(self.iface) self.runToolDialog(d) def runToolDialog(self, dlg): dlg.show_() dlg.exec_() del dlg def doSettings(self): from .tools.doSettings import GdalToolsSettingsDialog as Settings d = Settings(self.iface) d.exec_()
class DBManager(QMainWindow): def __init__(self, iface, parent=None): QMainWindow.__init__(self, parent) self.setAttribute(Qt.WA_DeleteOnClose) self.setupUi() self.iface = iface # restore the window state settings = QSettings() self.restoreGeometry(settings.value("/DB_Manager/mainWindow/geometry", QByteArray(), type=QByteArray)) self.restoreState(settings.value("/DB_Manager/mainWindow/windowState", QByteArray(), type=QByteArray)) self.tabs.currentChanged.connect(self.tabChanged) self.tree.selectedItemChanged.connect(self.itemChanged) self.itemChanged(None) def closeEvent(self, e): self.unregisterAllActions() # clear preview, this will delete the layer in preview tab self.preview.loadPreview(None) # save the window state settings = QSettings() settings.setValue("/DB_Manager/mainWindow/windowState", self.saveState()) settings.setValue("/DB_Manager/mainWindow/geometry", self.saveGeometry()) QMainWindow.closeEvent(self, e) def refreshItem(self, item=None): QApplication.setOverrideCursor(Qt.WaitCursor) try: if item is None: item = self.tree.currentItem() self.tree.refreshItem(item) # refresh item children in the db tree except BaseError as e: DlgDbError.showError(e, self) return finally: QApplication.restoreOverrideCursor() def itemChanged(self, item): QApplication.setOverrideCursor(Qt.WaitCursor) try: self.reloadButtons() # clear preview, this will delete the layer in preview tab self.preview.loadPreview(None) self.refreshTabs() except BaseError as e: DlgDbError.showError(e, self) return finally: QApplication.restoreOverrideCursor() def reloadButtons(self): db = self.tree.currentDatabase() if not hasattr(self, '_lastDb'): self._lastDb = db elif db == self._lastDb: return # remove old actions if self._lastDb is not None: self.unregisterAllActions() # add actions of the selected database self._lastDb = db if self._lastDb is not None: self._lastDb.registerAllActions(self) def tabChanged(self, index): QApplication.setOverrideCursor(Qt.WaitCursor) try: self.refreshTabs() except BaseError as e: DlgDbError.showError(e, self) return finally: QApplication.restoreOverrideCursor() def refreshTabs(self): index = self.tabs.currentIndex() item = self.tree.currentItem() table = self.tree.currentTable() # enable/disable tabs self.tabs.setTabEnabled(self.tabs.indexOf(self.table), table is not None) self.tabs.setTabEnabled(self.tabs.indexOf(self.preview), table is not None and table.type in [table.VectorType, table.RasterType] and table.geomColumn is not None) # show the info tab if the current tab is disabled if not self.tabs.isTabEnabled(index): self.tabs.setCurrentWidget(self.info) current_tab = self.tabs.currentWidget() if current_tab == self.info: self.info.showInfo(item) elif current_tab == self.table: self.table.loadData(item) elif current_tab == self.preview: self.preview.loadPreview(item) def refreshActionSlot(self): self.info.setDirty() self.table.setDirty() self.preview.setDirty() self.refreshItem() def importActionSlot(self): db = self.tree.currentDatabase() if db is None: self.infoBar.pushMessage(self.tr("No database selected or you are not connected to it."), QgsMessageBar.INFO, self.iface.messageTimeout()) return outUri = db.uri() schema = self.tree.currentSchema() if schema: outUri.setDataSource(schema.name, "", "", "") from .dlg_import_vector import DlgImportVector dlg = DlgImportVector(None, db, outUri, self) dlg.exec_() def exportActionSlot(self): table = self.tree.currentTable() if table is None: self.infoBar.pushMessage(self.tr("Select the table you want export to file."), QgsMessageBar.INFO, self.iface.messageTimeout()) return inLayer = table.toMapLayer() from .dlg_export_vector import DlgExportVector dlg = DlgExportVector(inLayer, table.database(), self) dlg.exec_() inLayer.deleteLater() def runSqlWindow(self): db = self.tree.currentDatabase() if db is None: self.infoBar.pushMessage(self.tr("No database selected or you are not connected to it."), QgsMessageBar.INFO, self.iface.messageTimeout()) # force displaying of the message, it appears on the first tab (i.e. Info) self.tabs.setCurrentIndex(0) return from .dlg_sql_window import DlgSqlWindow query = DlgSqlWindow(self.iface, db, self) dbname = db.connection().connectionName() tabname = self.tr("Query") + u" (%s)" % dbname index = self.tabs.addTab(query, tabname) self.tabs.setTabIcon(index, db.connection().icon()) self.tabs.setCurrentIndex(index) query.nameChanged.connect(functools.partial(self.update_query_tab_name, index, dbname)) def update_query_tab_name(self, index, dbname, queryname): if not queryname: queryname = self.tr("Query") tabname = u"%s (%s)" % (queryname, dbname) self.tabs.setTabText(index, tabname) def showSystemTables(self): self.tree.showSystemTables(self.actionShowSystemTables.isChecked()) def registerAction(self, action, menuName, callback=None): """ register an action to the manager's main menu """ if not hasattr(self, '_registeredDbActions'): self._registeredDbActions = {} if callback is not None: def invoke_callback(x): return self.invokeCallback(callback) if menuName is None or menuName == "": self.addAction(action) if menuName not in self._registeredDbActions: self._registeredDbActions[menuName] = list() self._registeredDbActions[menuName].append(action) if callback is not None: action.triggered.connect(invoke_callback) return True # search for the menu actionMenu = None helpMenuAction = None for a in self.menuBar.actions(): if not a.menu() or a.menu().title() != menuName: continue if a.menu() != self.menuHelp: helpMenuAction = a actionMenu = a break # not found, add a new menu before the help menu if actionMenu is None: menu = QMenu(menuName, self) if helpMenuAction is not None: actionMenu = self.menuBar.insertMenu(helpMenuAction, menu) else: actionMenu = self.menuBar.addMenu(menu) menu = actionMenu.menu() menuActions = menu.actions() # get the placeholder's position to insert before it pos = 0 for pos in range(len(menuActions)): if menuActions[pos].isSeparator() and menuActions[pos].objectName().endswith("_placeholder"): menuActions[pos].setVisible(True) break if pos < len(menuActions): before = menuActions[pos] menu.insertAction(before, action) else: menu.addAction(action) actionMenu.setVisible(True) # show the menu if menuName not in self._registeredDbActions: self._registeredDbActions[menuName] = list() self._registeredDbActions[menuName].append(action) if callback is not None: action.triggered.connect(invoke_callback) return True def invokeCallback(self, callback, *params): """ Call a method passing the selected item in the database tree, the sender (usually a QAction), the plugin mainWindow and optionally additional parameters. This method takes care to override and restore the cursor, but also catches exceptions and displays the error dialog. """ QApplication.setOverrideCursor(Qt.WaitCursor) try: callback(self.tree.currentItem(), self.sender(), self, *params) except BaseError as e: # catch database errors and display the error dialog DlgDbError.showError(e, self) return finally: QApplication.restoreOverrideCursor() def unregisterAction(self, action, menuName): if not hasattr(self, '_registeredDbActions'): return if menuName is None or menuName == "": self.removeAction(action) if menuName in self._registeredDbActions: if self._registeredDbActions[menuName].count(action) > 0: self._registeredDbActions[menuName].remove(action) action.deleteLater() return True for a in self.menuBar.actions(): if not a.menu() or a.menu().title() != menuName: continue menu = a.menu() menuActions = menu.actions() menu.removeAction(action) if menu.isEmpty(): # hide the menu a.setVisible(False) if menuName in self._registeredDbActions: if self._registeredDbActions[menuName].count(action) > 0: self._registeredDbActions[menuName].remove(action) # hide the placeholder if there're no other registered actions if len(self._registeredDbActions[menuName]) <= 0: for i in range(len(menuActions)): if menuActions[i].isSeparator() and menuActions[i].objectName().endswith("_placeholder"): menuActions[i].setVisible(False) break action.deleteLater() return True return False def unregisterAllActions(self): if not hasattr(self, '_registeredDbActions'): return for menuName in self._registeredDbActions: for action in list(self._registeredDbActions[menuName]): self.unregisterAction(action, menuName) del self._registeredDbActions def close_tab(self, index): widget = self.tabs.widget(index) if widget not in [self.info, self.table, self.preview]: self.tabs.removeTab(index) widget.deleteLater() def setupUi(self): self.setWindowTitle(self.tr("DB Manager")) self.setWindowIcon(QIcon(":/db_manager/icon")) self.resize(QSize(700, 500).expandedTo(self.minimumSizeHint())) # create central tab widget and add the first 3 tabs: info, table and preview self.tabs = QTabWidget() self.info = InfoViewer(self) self.tabs.addTab(self.info, self.tr("Info")) self.table = TableViewer(self) self.tabs.addTab(self.table, self.tr("Table")) self.preview = LayerPreview(self) self.tabs.addTab(self.preview, self.tr("Preview")) self.setCentralWidget(self.tabs) # display close button for all tabs but the first 3 ones, i.e. # HACK: just hide the close button where not needed (GS) self.tabs.setTabsClosable(True) self.tabs.tabCloseRequested.connect(self.close_tab) tabbar = self.tabs.tabBar() for i in range(3): btn = tabbar.tabButton(i, QTabBar.RightSide) if tabbar.tabButton(i, QTabBar.RightSide) else tabbar.tabButton(i, QTabBar.LeftSide) btn.resize(0, 0) btn.hide() # Creates layout for message bar self.layout = QGridLayout(self.info) self.layout.setContentsMargins(0, 0, 0, 0) spacerItem = QSpacerItem(20, 40, QSizePolicy.Minimum, QSizePolicy.Expanding) self.layout.addItem(spacerItem, 1, 0, 1, 1) # init messageBar instance self.infoBar = QgsMessageBar(self.info) sizePolicy = QSizePolicy(QSizePolicy.Minimum, QSizePolicy.Fixed) self.infoBar.setSizePolicy(sizePolicy) self.layout.addWidget(self.infoBar, 0, 0, 1, 1) # create database tree self.dock = QDockWidget("Tree", self) self.dock.setObjectName("DB_Manager_DBView") self.dock.setFeatures(QDockWidget.DockWidgetMovable) self.tree = DBTree(self) self.dock.setWidget(self.tree) self.addDockWidget(Qt.LeftDockWidgetArea, self.dock) # create status bar self.statusBar = QStatusBar(self) self.setStatusBar(self.statusBar) # create menus self.menuBar = QMenuBar(self) self.menuDb = QMenu(self.tr("&Database"), self) self.menuBar.addMenu(self.menuDb) self.menuSchema = QMenu(self.tr("&Schema"), self) actionMenuSchema = self.menuBar.addMenu(self.menuSchema) self.menuTable = QMenu(self.tr("&Table"), self) actionMenuTable = self.menuBar.addMenu(self.menuTable) self.menuHelp = None # QMenu(self.tr("&Help"), self) # actionMenuHelp = self.menuBar.addMenu(self.menuHelp) self.setMenuBar(self.menuBar) # create toolbar self.toolBar = QToolBar("Default", self) self.toolBar.setObjectName("DB_Manager_ToolBar") self.addToolBar(self.toolBar) # create menus' actions # menu DATABASE sep = self.menuDb.addSeparator() sep.setObjectName("DB_Manager_DbMenu_placeholder") sep.setVisible(False) self.actionRefresh = self.menuDb.addAction(QIcon(":/db_manager/actions/refresh"), self.tr("&Refresh"), self.refreshActionSlot, QKeySequence("F5")) self.actionSqlWindow = self.menuDb.addAction(QIcon(":/db_manager/actions/sql_window"), self.tr("&SQL window"), self.runSqlWindow, QKeySequence("F2")) self.menuDb.addSeparator() self.actionClose = self.menuDb.addAction(QIcon(), self.tr("&Exit"), self.close, QKeySequence("CTRL+Q")) # menu SCHEMA sep = self.menuSchema.addSeparator() sep.setObjectName("DB_Manager_SchemaMenu_placeholder") sep.setVisible(False) actionMenuSchema.setVisible(False) # menu TABLE sep = self.menuTable.addSeparator() sep.setObjectName("DB_Manager_TableMenu_placeholder") sep.setVisible(False) self.actionImport = self.menuTable.addAction(QIcon(":/db_manager/actions/import"), self.tr("&Import layer/file"), self.importActionSlot) self.actionExport = self.menuTable.addAction(QIcon(":/db_manager/actions/export"), self.tr("&Export to file"), self.exportActionSlot) self.menuTable.addSeparator() #self.actionShowSystemTables = self.menuTable.addAction(self.tr("Show system tables/views"), self.showSystemTables) #self.actionShowSystemTables.setCheckable(True) #self.actionShowSystemTables.setChecked(True) actionMenuTable.setVisible(False) # add actions to the toolbar self.toolBar.addAction(self.actionRefresh) self.toolBar.addAction(self.actionSqlWindow) self.toolBar.addAction(self.actionImport) self.toolBar.addAction(self.actionExport)
class DBManager(QMainWindow): def __init__(self, iface, parent=None): QMainWindow.__init__(self, parent) self.setAttribute(Qt.WA_DeleteOnClose) self.setupUi() self.iface = iface # restore the window state settings = QSettings() self.restoreGeometry( settings.value("/DB_Manager/mainWindow/geometry", QByteArray(), type=QByteArray)) self.restoreState( settings.value("/DB_Manager/mainWindow/windowState", QByteArray(), type=QByteArray)) self.tabs.currentChanged.connect(self.tabChanged) self.tree.selectedItemChanged.connect(self.itemChanged) self.itemChanged(None) def closeEvent(self, e): self.unregisterAllActions() # clear preview, this will delete the layer in preview tab self.preview.loadPreview(None) # save the window state settings = QSettings() settings.setValue("/DB_Manager/mainWindow/windowState", self.saveState()) settings.setValue("/DB_Manager/mainWindow/geometry", self.saveGeometry()) QMainWindow.closeEvent(self, e) def refreshItem(self, item=None): QApplication.setOverrideCursor(Qt.WaitCursor) try: if item is None: item = self.tree.currentItem() self.tree.refreshItem(item) # refresh item children in the db tree except BaseError as e: DlgDbError.showError(e, self) return finally: QApplication.restoreOverrideCursor() def itemChanged(self, item): QApplication.setOverrideCursor(Qt.WaitCursor) try: self.reloadButtons() # clear preview, this will delete the layer in preview tab self.preview.loadPreview(None) self.refreshTabs() except BaseError as e: DlgDbError.showError(e, self) return finally: QApplication.restoreOverrideCursor() def reloadButtons(self): db = self.tree.currentDatabase() if not hasattr(self, '_lastDb'): self._lastDb = db elif db == self._lastDb: return # remove old actions if self._lastDb is not None: self.unregisterAllActions() # add actions of the selected database self._lastDb = db if self._lastDb is not None: self._lastDb.registerAllActions(self) def tabChanged(self, index): QApplication.setOverrideCursor(Qt.WaitCursor) try: self.refreshTabs() except BaseError as e: DlgDbError.showError(e, self) return finally: QApplication.restoreOverrideCursor() def refreshTabs(self): index = self.tabs.currentIndex() item = self.tree.currentItem() table = self.tree.currentTable() # enable/disable tabs self.tabs.setTabEnabled(self.tabs.indexOf(self.table), table is not None) self.tabs.setTabEnabled( self.tabs.indexOf(self.preview), table is not None and table.type in [table.VectorType, table.RasterType] and table.geomColumn is not None) # show the info tab if the current tab is disabled if not self.tabs.isTabEnabled(index): self.tabs.setCurrentWidget(self.info) current_tab = self.tabs.currentWidget() if current_tab == self.info: self.info.showInfo(item) elif current_tab == self.table: self.table.loadData(item) elif current_tab == self.preview: self.preview.loadPreview(item) def refreshActionSlot(self): self.info.setDirty() self.table.setDirty() self.preview.setDirty() self.refreshItem() def importActionSlot(self): db = self.tree.currentDatabase() if db is None: self.infoBar.pushMessage( self.tr( "No database selected or you are not connected to it."), QgsMessageBar.INFO, self.iface.messageTimeout()) return outUri = db.uri() schema = self.tree.currentSchema() if schema: outUri.setDataSource(schema.name, "", "", "") from .dlg_import_vector import DlgImportVector dlg = DlgImportVector(None, db, outUri, self) dlg.exec_() def exportActionSlot(self): table = self.tree.currentTable() if table is None: self.infoBar.pushMessage( self.tr("Select the table you want export to file."), QgsMessageBar.INFO, self.iface.messageTimeout()) return inLayer = table.toMapLayer() from .dlg_export_vector import DlgExportVector dlg = DlgExportVector(inLayer, table.database(), self) dlg.exec_() inLayer.deleteLater() def runSqlWindow(self): db = self.tree.currentDatabase() if db is None: self.infoBar.pushMessage( self.tr( "No database selected or you are not connected to it."), QgsMessageBar.INFO, self.iface.messageTimeout()) # force displaying of the message, it appears on the first tab (i.e. Info) self.tabs.setCurrentIndex(0) return from .dlg_sql_window import DlgSqlWindow query = DlgSqlWindow(self.iface, db, self) dbname = db.connection().connectionName() tabname = self.tr("Query") + u" (%s)" % dbname index = self.tabs.addTab(query, tabname) self.tabs.setTabIcon(index, db.connection().icon()) self.tabs.setCurrentIndex(index) query.nameChanged.connect( functools.partial(self.update_query_tab_name, index, dbname)) def update_query_tab_name(self, index, dbname, queryname): if not queryname: queryname = self.tr("Query") tabname = u"%s (%s)" % (queryname, dbname) self.tabs.setTabText(index, tabname) def showSystemTables(self): self.tree.showSystemTables(self.actionShowSystemTables.isChecked()) def registerAction(self, action, menuName, callback=None): """ register an action to the manager's main menu """ if not hasattr(self, '_registeredDbActions'): self._registeredDbActions = {} if callback is not None: def invoke_callback(x): return self.invokeCallback(callback) if menuName is None or menuName == "": self.addAction(action) if menuName not in self._registeredDbActions: self._registeredDbActions[menuName] = list() self._registeredDbActions[menuName].append(action) if callback is not None: action.triggered.connect(invoke_callback) return True # search for the menu actionMenu = None helpMenuAction = None for a in self.menuBar.actions(): if not a.menu() or a.menu().title() != menuName: continue if a.menu() != self.menuHelp: helpMenuAction = a actionMenu = a break # not found, add a new menu before the help menu if actionMenu is None: menu = QMenu(menuName, self) if helpMenuAction is not None: actionMenu = self.menuBar.insertMenu(helpMenuAction, menu) else: actionMenu = self.menuBar.addMenu(menu) menu = actionMenu.menu() menuActions = menu.actions() # get the placeholder's position to insert before it pos = 0 for pos in range(len(menuActions)): if menuActions[pos].isSeparator() and menuActions[pos].objectName( ).endswith("_placeholder"): menuActions[pos].setVisible(True) break if pos < len(menuActions): before = menuActions[pos] menu.insertAction(before, action) else: menu.addAction(action) actionMenu.setVisible(True) # show the menu if menuName not in self._registeredDbActions: self._registeredDbActions[menuName] = list() self._registeredDbActions[menuName].append(action) if callback is not None: action.triggered.connect(invoke_callback) return True def invokeCallback(self, callback, *params): """ Call a method passing the selected item in the database tree, the sender (usually a QAction), the plugin mainWindow and optionally additional parameters. This method takes care to override and restore the cursor, but also catches exceptions and displays the error dialog. """ QApplication.setOverrideCursor(Qt.WaitCursor) try: callback(self.tree.currentItem(), self.sender(), self, *params) except BaseError as e: # catch database errors and display the error dialog DlgDbError.showError(e, self) return finally: QApplication.restoreOverrideCursor() def unregisterAction(self, action, menuName): if not hasattr(self, '_registeredDbActions'): return if menuName is None or menuName == "": self.removeAction(action) if menuName in self._registeredDbActions: if self._registeredDbActions[menuName].count(action) > 0: self._registeredDbActions[menuName].remove(action) action.deleteLater() return True for a in self.menuBar.actions(): if not a.menu() or a.menu().title() != menuName: continue menu = a.menu() menuActions = menu.actions() menu.removeAction(action) if menu.isEmpty(): # hide the menu a.setVisible(False) if menuName in self._registeredDbActions: if self._registeredDbActions[menuName].count(action) > 0: self._registeredDbActions[menuName].remove(action) # hide the placeholder if there're no other registered actions if len(self._registeredDbActions[menuName]) <= 0: for i in range(len(menuActions)): if menuActions[i].isSeparator() and menuActions[ i].objectName().endswith("_placeholder"): menuActions[i].setVisible(False) break action.deleteLater() return True return False def unregisterAllActions(self): if not hasattr(self, '_registeredDbActions'): return for menuName in self._registeredDbActions: for action in list(self._registeredDbActions[menuName]): self.unregisterAction(action, menuName) del self._registeredDbActions def close_tab(self, index): widget = self.tabs.widget(index) if widget not in [self.info, self.table, self.preview]: self.tabs.removeTab(index) widget.deleteLater() def setupUi(self): self.setWindowTitle(self.tr("DB Manager")) self.setWindowIcon(QIcon(":/db_manager/icon")) self.resize(QSize(700, 500).expandedTo(self.minimumSizeHint())) # create central tab widget and add the first 3 tabs: info, table and preview self.tabs = QTabWidget() self.info = InfoViewer(self) self.tabs.addTab(self.info, self.tr("Info")) self.table = TableViewer(self) self.tabs.addTab(self.table, self.tr("Table")) self.preview = LayerPreview(self) self.tabs.addTab(self.preview, self.tr("Preview")) self.setCentralWidget(self.tabs) # display close button for all tabs but the first 3 ones, i.e. # HACK: just hide the close button where not needed (GS) self.tabs.setTabsClosable(True) self.tabs.tabCloseRequested.connect(self.close_tab) tabbar = self.tabs.tabBar() for i in range(3): btn = tabbar.tabButton(i, QTabBar.RightSide) if tabbar.tabButton( i, QTabBar.RightSide) else tabbar.tabButton( i, QTabBar.LeftSide) btn.resize(0, 0) btn.hide() # Creates layout for message bar self.layout = QGridLayout(self.info) self.layout.setContentsMargins(0, 0, 0, 0) spacerItem = QSpacerItem(20, 40, QSizePolicy.Minimum, QSizePolicy.Expanding) self.layout.addItem(spacerItem, 1, 0, 1, 1) # init messageBar instance self.infoBar = QgsMessageBar(self.info) sizePolicy = QSizePolicy(QSizePolicy.Minimum, QSizePolicy.Fixed) self.infoBar.setSizePolicy(sizePolicy) self.layout.addWidget(self.infoBar, 0, 0, 1, 1) # create database tree self.dock = QDockWidget("Tree", self) self.dock.setObjectName("DB_Manager_DBView") self.dock.setFeatures(QDockWidget.DockWidgetMovable) self.tree = DBTree(self) self.dock.setWidget(self.tree) self.addDockWidget(Qt.LeftDockWidgetArea, self.dock) # create status bar self.statusBar = QStatusBar(self) self.setStatusBar(self.statusBar) # create menus self.menuBar = QMenuBar(self) self.menuDb = QMenu(self.tr("&Database"), self) self.menuBar.addMenu(self.menuDb) self.menuSchema = QMenu(self.tr("&Schema"), self) actionMenuSchema = self.menuBar.addMenu(self.menuSchema) self.menuTable = QMenu(self.tr("&Table"), self) actionMenuTable = self.menuBar.addMenu(self.menuTable) self.menuHelp = None # QMenu(self.tr("&Help"), self) # actionMenuHelp = self.menuBar.addMenu(self.menuHelp) self.setMenuBar(self.menuBar) # create toolbar self.toolBar = QToolBar("Default", self) self.toolBar.setObjectName("DB_Manager_ToolBar") self.addToolBar(self.toolBar) # create menus' actions # menu DATABASE sep = self.menuDb.addSeparator() sep.setObjectName("DB_Manager_DbMenu_placeholder") sep.setVisible(False) self.actionRefresh = self.menuDb.addAction( QIcon(":/db_manager/actions/refresh"), self.tr("&Refresh"), self.refreshActionSlot, QKeySequence("F5")) self.actionSqlWindow = self.menuDb.addAction( QIcon(":/db_manager/actions/sql_window"), self.tr("&SQL window"), self.runSqlWindow, QKeySequence("F2")) self.menuDb.addSeparator() self.actionClose = self.menuDb.addAction(QIcon(), self.tr("&Exit"), self.close, QKeySequence("CTRL+Q")) # menu SCHEMA sep = self.menuSchema.addSeparator() sep.setObjectName("DB_Manager_SchemaMenu_placeholder") sep.setVisible(False) actionMenuSchema.setVisible(False) # menu TABLE sep = self.menuTable.addSeparator() sep.setObjectName("DB_Manager_TableMenu_placeholder") sep.setVisible(False) self.actionImport = self.menuTable.addAction( QIcon(":/db_manager/actions/import"), self.tr("&Import layer/file"), self.importActionSlot) self.actionExport = self.menuTable.addAction( QIcon(":/db_manager/actions/export"), self.tr("&Export to file"), self.exportActionSlot) self.menuTable.addSeparator() #self.actionShowSystemTables = self.menuTable.addAction(self.tr("Show system tables/views"), self.showSystemTables) #self.actionShowSystemTables.setCheckable(True) #self.actionShowSystemTables.setChecked(True) actionMenuTable.setVisible(False) # add actions to the toolbar self.toolBar.addAction(self.actionRefresh) self.toolBar.addAction(self.actionSqlWindow) self.toolBar.addAction(self.actionImport) self.toolBar.addAction(self.actionExport)
class GdalTools: def __init__(self, iface): if not valid: return # Save reference to the QGIS interface self.iface = iface try: self.QgisVersion = unicode(QGis.QGIS_VERSION_INT) except: self.QgisVersion = unicode(QGis.qgisVersion)[0] if QGis.QGIS_VERSION[0:3] < "1.5": # For i18n support userPluginPath = qgis.utils.home_plugin_path + "/GdalTools" systemPluginPath = qgis.utils.sys_plugin_path + "/GdalTools" overrideLocale = QSettings().value("locale/overrideFlag", False, type=bool) if not overrideLocale: localeFullName = QLocale.system().name() else: localeFullName = QSettings().value("locale/userLocale", "", type=str) if QFileInfo(userPluginPath).exists(): translationPath = userPluginPath + "/i18n/GdalTools_" + localeFullName + ".qm" else: translationPath = systemPluginPath + "/i18n/GdalTools_" + localeFullName + ".qm" self.localePath = translationPath if QFileInfo(self.localePath).exists(): self.translator = QTranslator() self.translator.load(self.localePath) QCoreApplication.installTranslator(self.translator) # The list of actions added to menus, so we can remove them when unloading the plugin self._menuActions = [] def initGui(self): if not valid: return if int(self.QgisVersion) < 1: QMessageBox.warning( self.iface.getMainWindow(), "Gdal Tools", QCoreApplication.translate("GdalTools", "QGIS version detected: ") + unicode(self.QgisVersion) + ".xx\n" + QCoreApplication.translate( "GdalTools", "This version of Gdal Tools requires at least QGIS version 1.0.0\nPlugin will not be enabled." )) return None from .tools.GdalTools_utils import GdalConfig, LayerRegistry self.GdalVersionNum = GdalConfig.versionNum() LayerRegistry.setIface(self.iface) # find the Raster menu rasterMenu = None menu_bar = self.iface.mainWindow().menuBar() actions = menu_bar.actions() rasterText = QCoreApplication.translate("QgisApp", "&Raster") for a in actions: if a.menu() is not None and a.menu().title() == rasterText: rasterMenu = a.menu() break if rasterMenu is None: # no Raster menu, create and insert it before the Help menu self.menu = QMenu(rasterText, self.iface.mainWindow()) lastAction = actions[len(actions) - 1] menu_bar.insertMenu(lastAction, self.menu) else: self.menu = rasterMenu self._menuActions.append(self.menu.addSeparator()) # projections menu (Warp (Reproject), Assign projection) self.projectionsMenu = QMenu( QCoreApplication.translate("GdalTools", "Projections"), self.iface.mainWindow()) self.projectionsMenu.setObjectName("projectionsMenu") self.warp = QAction( QIcon(":/icons/warp.png"), QCoreApplication.translate("GdalTools", "Warp (Reproject)..."), self.iface.mainWindow()) self.warp.setObjectName("warp") self.warp.setStatusTip( QCoreApplication.translate( "GdalTools", "Warp an image into a new coordinate system")) self.warp.triggered.connect(self.doWarp) self.projection = QAction( QIcon(":icons/projection-add.png"), QCoreApplication.translate("GdalTools", "Assign Projection..."), self.iface.mainWindow()) self.projection.setObjectName("projection") self.projection.setStatusTip( QCoreApplication.translate("GdalTools", "Add projection info to the raster")) self.projection.triggered.connect(self.doProjection) self.extractProj = QAction( QIcon(":icons/projection-export.png"), QCoreApplication.translate("GdalTools", "Extract Projection..."), self.iface.mainWindow()) self.extractProj.setObjectName("extractProj") self.extractProj.setStatusTip( QCoreApplication.translate( "GdalTools", "Extract projection information from raster(s)")) self.extractProj.triggered.connect(self.doExtractProj) self.projectionsMenu.addActions( [self.warp, self.projection, self.extractProj]) # conversion menu (Rasterize (Vector to raster), Polygonize (Raster to vector), Translate, RGB to PCT, PCT to RGB) self.conversionMenu = QMenu( QCoreApplication.translate("GdalTools", "Conversion"), self.iface.mainWindow()) self.conversionMenu.setObjectName("conversionMenu") if self.GdalVersionNum >= 1300: self.rasterize = QAction( QIcon(":/icons/rasterize.png"), QCoreApplication.translate("GdalTools", "Rasterize (Vector to Raster)..."), self.iface.mainWindow()) self.rasterize.setObjectName("rasterize") self.rasterize.setStatusTip( QCoreApplication.translate( "GdalTools", "Burns vector geometries into a raster")) self.rasterize.triggered.connect(self.doRasterize) self.conversionMenu.addAction(self.rasterize) if self.GdalVersionNum >= 1600: self.polygonize = QAction( QIcon(":/icons/polygonize.png"), QCoreApplication.translate("GdalTools", "Polygonize (Raster to Vector)..."), self.iface.mainWindow()) self.polygonize.setObjectName("polygonize") self.polygonize.setStatusTip( QCoreApplication.translate( "GdalTools", "Produces a polygon feature layer from a raster")) self.polygonize.triggered.connect(self.doPolygonize) self.conversionMenu.addAction(self.polygonize) self.translate = QAction( QIcon(":/icons/translate.png"), QCoreApplication.translate("GdalTools", "Translate (Convert Format)..."), self.iface.mainWindow()) self.translate.setObjectName("translate") self.translate.setStatusTip( QCoreApplication.translate( "GdalTools", "Converts raster data between different formats")) self.translate.triggered.connect(self.doTranslate) self.paletted = QAction( QIcon(":icons/24-to-8-bits.png"), QCoreApplication.translate("GdalTools", "RGB to PCT..."), self.iface.mainWindow()) self.paletted.setObjectName("paletted") self.paletted.setStatusTip( QCoreApplication.translate( "GdalTools", "Convert a 24bit RGB image to 8bit paletted")) self.paletted.triggered.connect(self.doPaletted) self.rgb = QAction( QIcon(":icons/8-to-24-bits.png"), QCoreApplication.translate("GdalTools", "PCT to RGB..."), self.iface.mainWindow()) self.rgb.setObjectName("rgb") self.rgb.setStatusTip( QCoreApplication.translate( "GdalTools", "Convert an 8bit paletted image to 24bit RGB")) self.rgb.triggered.connect(self.doRGB) self.conversionMenu.addActions( [self.translate, self.paletted, self.rgb]) # extraction menu (Clipper, Contour) self.extractionMenu = QMenu( QCoreApplication.translate("GdalTools", "Extraction"), self.iface.mainWindow()) self.extractionMenu.setObjectName("extractionMenu") if self.GdalVersionNum >= 1600: self.contour = QAction( QIcon(":/icons/contour.png"), QCoreApplication.translate("GdalTools", "Contour..."), self.iface.mainWindow()) self.contour.setObjectName("contour") self.contour.setStatusTip( QCoreApplication.translate( "GdalTools", "Builds vector contour lines from a DEM")) self.contour.triggered.connect(self.doContour) self.extractionMenu.addAction(self.contour) self.clipper = QAction( QIcon(":icons/raster-clip.png"), QCoreApplication.translate("GdalTools", "Clipper..."), self.iface.mainWindow()) self.clipper.setObjectName("clipper") #self.clipper.setStatusTip( QCoreApplication.translate( "GdalTools", "Converts raster data between different formats") ) self.clipper.triggered.connect(self.doClipper) self.extractionMenu.addActions([self.clipper]) # analysis menu (DEM (Terrain model), Grid (Interpolation), Near black, Proximity (Raster distance), Sieve) self.analysisMenu = QMenu( QCoreApplication.translate("GdalTools", "Analysis"), self.iface.mainWindow()) self.analysisMenu.setObjectName("analysisMenu") if self.GdalVersionNum >= 1600: self.sieve = QAction( QIcon(":/icons/sieve.png"), QCoreApplication.translate("GdalTools", "Sieve..."), self.iface.mainWindow()) self.sieve.setObjectName("sieve") self.sieve.setStatusTip( QCoreApplication.translate("GdalTools", "Removes small raster polygons")) self.sieve.triggered.connect(self.doSieve) self.analysisMenu.addAction(self.sieve) if self.GdalVersionNum >= 1500: self.nearBlack = QAction( QIcon(":/icons/nearblack.png"), QCoreApplication.translate("GdalTools", "Near Black..."), self.iface.mainWindow()) self.nearBlack.setObjectName("nearBlack") self.nearBlack.setStatusTip( QCoreApplication.translate( "GdalTools", "Convert nearly black/white borders to exact value")) self.nearBlack.triggered.connect(self.doNearBlack) self.analysisMenu.addAction(self.nearBlack) if self.GdalVersionNum >= 1700: self.fillNodata = QAction( QIcon(":/icons/fillnodata.png"), QCoreApplication.translate("GdalTools", "Fill nodata..."), self.iface.mainWindow()) self.fillNodata.setObjectName("fillNodata") self.fillNodata.setStatusTip( QCoreApplication.translate( "GdalTools", "Fill raster regions by interpolation from edges")) self.fillNodata.triggered.connect(self.doFillNodata) self.analysisMenu.addAction(self.fillNodata) if self.GdalVersionNum >= 1600: self.proximity = QAction( QIcon(":/icons/proximity.png"), QCoreApplication.translate("GdalTools", "Proximity (Raster Distance)..."), self.iface.mainWindow()) self.proximity.setObjectName("proximity") self.proximity.setStatusTip( QCoreApplication.translate("GdalTools", "Produces a raster proximity map")) self.proximity.triggered.connect(self.doProximity) self.analysisMenu.addAction(self.proximity) if self.GdalVersionNum >= 1500: self.grid = QAction( QIcon(":/icons/grid.png"), QCoreApplication.translate("GdalTools", "Grid (Interpolation)..."), self.iface.mainWindow()) self.grid.setObjectName("grid") self.grid.setStatusTip( QCoreApplication.translate( "GdalTools", "Create raster from the scattered data")) self.grid.triggered.connect(self.doGrid) self.analysisMenu.addAction(self.grid) if self.GdalVersionNum >= 1700: self.dem = QAction( QIcon(":icons/dem.png"), QCoreApplication.translate("GdalTools", "DEM (Terrain Models)..."), self.iface.mainWindow()) self.dem.setObjectName("dem") self.dem.setStatusTip( QCoreApplication.translate( "GdalTools", "Tool to analyze and visualize DEMs")) self.dem.triggered.connect(self.doDEM) self.analysisMenu.addAction(self.dem) #self.analysisMenu.addActions( [ ] ) # miscellaneous menu (Build overviews (Pyramids), Tile index, Information, Merge, Build Virtual Raster (Catalog)) self.miscellaneousMenu = QMenu( QCoreApplication.translate("GdalTools", "Miscellaneous"), self.iface.mainWindow()) self.miscellaneousMenu.setObjectName("miscellaneousMenu") if self.GdalVersionNum >= 1600: self.buildVRT = QAction( QIcon(":/icons/vrt.png"), QCoreApplication.translate( "GdalTools", "Build Virtual Raster (Catalog)..."), self.iface.mainWindow()) self.buildVRT.setObjectName("buildVRT") self.buildVRT.setStatusTip( QCoreApplication.translate( "GdalTools", "Builds a VRT from a list of datasets")) self.buildVRT.triggered.connect(self.doBuildVRT) self.miscellaneousMenu.addAction(self.buildVRT) self.merge = QAction( QIcon(":/icons/merge.png"), QCoreApplication.translate("GdalTools", "Merge..."), self.iface.mainWindow()) self.merge.setObjectName("merge") self.merge.setStatusTip( QCoreApplication.translate( "GdalTools", "Build a quick mosaic from a set of images")) self.merge.triggered.connect(self.doMerge) self.info = QAction( QIcon(":/icons/raster-info.png"), QCoreApplication.translate("GdalTools", "Information..."), self.iface.mainWindow()) self.info.setObjectName("info") self.info.setStatusTip( QCoreApplication.translate( "GdalTools", "Lists information about raster dataset")) self.info.triggered.connect(self.doInfo) self.overview = QAction( QIcon(":icons/raster-overview.png"), QCoreApplication.translate("GdalTools", "Build Overviews (Pyramids)..."), self.iface.mainWindow()) self.overview.setObjectName("overview") self.overview.setStatusTip( QCoreApplication.translate("GdalTools", "Builds or rebuilds overview images")) self.overview.triggered.connect(self.doOverview) self.tileindex = QAction( QIcon(":icons/tiles.png"), QCoreApplication.translate("GdalTools", "Tile Index..."), self.iface.mainWindow()) self.tileindex.setObjectName("tileindex") self.tileindex.setStatusTip( QCoreApplication.translate( "GdalTools", "Build a shapefile as a raster tileindex")) self.tileindex.triggered.connect(self.doTileIndex) self.miscellaneousMenu.addActions( [self.merge, self.info, self.overview, self.tileindex]) self._menuActions.append(self.menu.addMenu(self.projectionsMenu)) self._menuActions.append(self.menu.addMenu(self.conversionMenu)) self._menuActions.append(self.menu.addMenu(self.extractionMenu)) if not self.analysisMenu.isEmpty(): self._menuActions.append(self.menu.addMenu(self.analysisMenu)) self._menuActions.append(self.menu.addMenu(self.miscellaneousMenu)) self.settings = QAction( QCoreApplication.translate("GdalTools", "GdalTools Settings..."), self.iface.mainWindow()) self.settings.setObjectName("settings") self.settings.setStatusTip( QCoreApplication.translate("GdalTools", "Various settings for Gdal Tools")) self.settings.triggered.connect(self.doSettings) self.menu.addAction(self.settings) self._menuActions.append(self.settings) def unload(self): if not valid: return for a in self._menuActions: self.menu.removeAction(a) def doBuildVRT(self): from .tools.doBuildVRT import GdalToolsDialog as BuildVRT d = BuildVRT(self.iface) self.runToolDialog(d) def doContour(self): from .tools.doContour import GdalToolsDialog as Contour d = Contour(self.iface) self.runToolDialog(d) def doRasterize(self): from .tools.doRasterize import GdalToolsDialog as Rasterize d = Rasterize(self.iface) self.runToolDialog(d) def doPolygonize(self): from .tools.doPolygonize import GdalToolsDialog as Polygonize d = Polygonize(self.iface) self.runToolDialog(d) def doMerge(self): from .tools.doMerge import GdalToolsDialog as Merge d = Merge(self.iface) self.runToolDialog(d) def doSieve(self): from .tools.doSieve import GdalToolsDialog as Sieve d = Sieve(self.iface) self.runToolDialog(d) def doProximity(self): from .tools.doProximity import GdalToolsDialog as Proximity d = Proximity(self.iface) self.runToolDialog(d) def doNearBlack(self): from .tools.doNearBlack import GdalToolsDialog as NearBlack d = NearBlack(self.iface) self.runToolDialog(d) def doFillNodata(self): from .tools.doFillNodata import GdalToolsDialog as FillNodata d = FillNodata(self.iface) self.runToolDialog(d) def doWarp(self): from .tools.doWarp import GdalToolsDialog as Warp d = Warp(self.iface) self.runToolDialog(d) def doGrid(self): from .tools.doGrid import GdalToolsDialog as Grid d = Grid(self.iface) self.runToolDialog(d) def doTranslate(self): from .tools.doTranslate import GdalToolsDialog as Translate d = Translate(self.iface) self.runToolDialog(d) def doInfo(self): from .tools.doInfo import GdalToolsDialog as Info d = Info(self.iface) self.runToolDialog(d) def doProjection(self): from .tools.doProjection import GdalToolsDialog as Projection d = Projection(self.iface) self.runToolDialog(d) def doOverview(self): from .tools.doOverview import GdalToolsDialog as Overview d = Overview(self.iface) self.runToolDialog(d) def doClipper(self): from .tools.doClipper import GdalToolsDialog as Clipper d = Clipper(self.iface) self.runToolDialog(d) def doPaletted(self): from .tools.doRgbPct import GdalToolsDialog as RgbPct d = RgbPct(self.iface) self.runToolDialog(d) def doRGB(self): from .tools.doPctRgb import GdalToolsDialog as PctRgb d = PctRgb(self.iface) self.runToolDialog(d) def doTileIndex(self): from .tools.doTileIndex import GdalToolsDialog as TileIndex d = TileIndex(self.iface) self.runToolDialog(d) def doExtractProj(self): from .tools.doExtractProj import GdalToolsDialog as ExtractProj d = ExtractProj(self.iface) d.exec_() def doDEM(self): from .tools.doDEM import GdalToolsDialog as DEM d = DEM(self.iface) self.runToolDialog(d) def runToolDialog(self, dlg): dlg.show_() dlg.exec_() del dlg def doSettings(self): from .tools.doSettings import GdalToolsSettingsDialog as Settings d = Settings(self.iface) d.exec_()