QGIS/python/plugins/sextante/gui/SextanteToolbox.py

258 lines
11 KiB
Python
Raw Normal View History

2012-10-05 23:28:47 +02:00
# -*- coding: utf-8 -*-
"""
***************************************************************************
SextanteToolbox.py
---------------------
Date : August 2012
Copyright : (C) 2012 by Victor Olaya
Email : volayaf at gmail dot com
***************************************************************************
* *
* 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. *
* *
***************************************************************************
"""
import webbrowser
2012-10-05 23:28:47 +02:00
__author__ = 'Victor Olaya'
__date__ = 'August 2012'
__copyright__ = '(C) 2012, Victor Olaya'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'
2012-09-18 21:53:25 +03:00
import os
import sys
import subprocess
2012-09-18 21:53:25 +03:00
2012-09-15 18:25:25 +03:00
from PyQt4.QtCore import *
from PyQt4.QtGui import *
2012-09-18 21:53:25 +03:00
2012-09-15 18:25:25 +03:00
from sextante.core.Sextante import Sextante
from sextante.core.SextanteLog import SextanteLog
from sextante.core.SextanteConfig import SextanteConfig
from sextante.core.QGisLayers import QGisLayers
2012-09-18 21:53:25 +03:00
from sextante.gui.ParametersDialog import ParametersDialog
from sextante.gui.BatchProcessingDialog import BatchProcessingDialog
from sextante.gui.EditRenderingStylesDialog import EditRenderingStylesDialog
2012-09-15 18:25:25 +03:00
try:
2012-09-18 21:53:25 +03:00
_fromUtf8 = QString.fromUtf8
2012-09-15 18:25:25 +03:00
except AttributeError:
_fromUtf8 = lambda s: s
2012-09-18 21:53:25 +03:00
class SextanteToolbox(QDockWidget):
2012-09-15 18:25:25 +03:00
def __init__(self, iface):
2012-09-18 21:53:25 +03:00
QDialog.__init__(self)
2012-09-15 18:25:25 +03:00
self.iface=iface
self.setupUi()
def algsListHasChanged(self):
self.fillTree()
def updateTree(self):
Sextante.updateAlgsList()
def setupUi(self):
self.setObjectName("SEXTANTE_Toolbox")
self.setFloating(False)
self.resize(400, 500)
2012-09-18 21:53:25 +03:00
self.setWindowTitle(self.tr("SEXTANTE Toolbox"))
self.contents = QWidget()
self.verticalLayout = QVBoxLayout(self.contents)
2012-09-15 18:25:25 +03:00
self.verticalLayout.setSpacing(2)
self.verticalLayout.setMargin(0)
2012-09-18 21:53:25 +03:00
self.externalAppsButton = QPushButton()
self.externalAppsButton.setText(self.tr("Click here to configure\nadditional algorithm providers"))
QObject.connect(self.externalAppsButton, SIGNAL("clicked()"), self.configureProviders)
2012-09-15 18:25:25 +03:00
self.verticalLayout.addWidget(self.externalAppsButton)
2012-09-18 21:53:25 +03:00
self.searchBox = QLineEdit(self.contents)
2012-09-15 18:25:25 +03:00
self.searchBox.textChanged.connect(self.fillTree)
self.verticalLayout.addWidget(self.searchBox)
2012-09-18 21:53:25 +03:00
self.algorithmTree = QTreeWidget(self.contents)
2012-09-15 18:25:25 +03:00
self.algorithmTree.setHeaderHidden(True)
self.algorithmTree.setContextMenuPolicy(Qt.CustomContextMenu)
self.fillTree()
2012-09-18 21:53:25 +03:00
self.connect(self.algorithmTree, SIGNAL('customContextMenuRequested(QPoint)'),
2012-09-15 18:25:25 +03:00
self.showPopupMenu)
self.verticalLayout.addWidget(self.algorithmTree)
self.algorithmTree.doubleClicked.connect(self.executeAlgorithm)
self.setWidget(self.contents)
self.iface.addDockWidget(Qt.RightDockWidgetArea, self)
2012-09-18 21:53:25 +03:00
QMetaObject.connectSlotsByName(self)
2012-09-15 18:25:25 +03:00
def configureProviders(self):
webbrowser.open("http://docs.qgis.org/html/en/user_manual/sextante/3rdParty.html")
#=======================================================================
# #QDesktopServices.openUrl(QUrl(os.path.join(os.path.dirname(__file__), os.path.pardir) + "/help/3rdParty.html"))
# filename = os.path.join(os.path.dirname(__file__), "..", "help", "3rdParty.html")
# if os.name == "nt":
# os.startfile(filename)
# elif sys.platform == "darwin":
# subprocess.Popen(('open', filename))
# else:
# subprocess.call(('xdg-open', filename))
#=======================================================================
2012-09-15 18:25:25 +03:00
def showPopupMenu(self,point):
item = self.algorithmTree.itemAt(point)
if isinstance(item, TreeAlgorithmItem):
alg = item.alg
popupmenu = QMenu()
2012-09-18 21:53:25 +03:00
executeAction = QAction(self.tr("Execute"), self.algorithmTree)
2012-09-15 18:25:25 +03:00
executeAction.triggered.connect(self.executeAlgorithm)
popupmenu.addAction(executeAction)
2012-09-18 21:53:25 +03:00
executeBatchAction = QAction(self.tr("Execute as batch process"), self.algorithmTree)
2012-09-15 18:25:25 +03:00
executeBatchAction.triggered.connect(self.executeAlgorithmAsBatchProcess)
popupmenu.addAction(executeBatchAction)
2012-09-18 21:53:25 +03:00
editRenderingStylesAction = QAction(self.tr("Edit rendering styles for outputs"), self.algorithmTree)
2012-09-15 18:25:25 +03:00
editRenderingStylesAction.triggered.connect(self.editRenderingStyles)
popupmenu.addAction(editRenderingStylesAction)
actions = Sextante.contextMenuActions
for action in actions:
action.setData(alg,self)
if action.isEnabled():
2012-09-18 21:53:25 +03:00
contextMenuAction = QAction(action.name, self.algorithmTree)
2012-09-15 18:25:25 +03:00
contextMenuAction.triggered.connect(action.execute)
popupmenu.addAction(contextMenuAction)
popupmenu.exec_(self.algorithmTree.mapToGlobal(point))
def editRenderingStyles(self):
item = self.algorithmTree.currentItem()
if isinstance(item, TreeAlgorithmItem):
alg = Sextante.getAlgorithm(item.alg.commandLineName())
dlg = EditRenderingStylesDialog(alg)
dlg.exec_()
def executeAlgorithmAsBatchProcess(self):
item = self.algorithmTree.currentItem()
if isinstance(item, TreeAlgorithmItem):
alg = Sextante.getAlgorithm(item.alg.commandLineName())
dlg = BatchProcessingDialog(alg)
dlg.exec_()
def executeAlgorithm(self):
item = self.algorithmTree.currentItem()
if isinstance(item, TreeAlgorithmItem):
alg = Sextante.getAlgorithm(item.alg.commandLineName())
message = alg.checkBeforeOpeningParametersDialog()
if message:
2012-09-18 21:53:25 +03:00
QMessageBox.warning(self, self.tr("Warning"), message)
2012-09-15 18:25:25 +03:00
return
alg = alg.getCopy()#copy.deepcopy(alg)
dlg = alg.getCustomParametersDialog()
if not dlg:
dlg = ParametersDialog(alg)
canvas = QGisLayers.iface.mapCanvas()
prevMapTool = canvas.mapTool()
dlg.show()
dlg.exec_()
if canvas.mapTool()!=prevMapTool:
try:
canvas.mapTool().reset()
except:
pass
canvas.setMapTool(prevMapTool)
if dlg.executed:
showRecent = SextanteConfig.getSetting(SextanteConfig.SHOW_RECENT_ALGORITHMS)
if showRecent:
self.fillTree()
if isinstance(item, TreeActionItem):
action = item.action
action.setData(self)
action.execute()
def fillTree(self):
self.algorithmTree.clear()
text = unicode(self.searchBox.text())
for providerName in Sextante.algs.keys():
groups = {}
provider = Sextante.algs[providerName]
name = "ACTIVATE_" + providerName.upper().replace(" ", "_")
if not SextanteConfig.getSetting(name):
continue
algs = provider.values()
#add algorithms
for alg in algs:
if not alg.showInToolbox:
continue
if text =="" or text.lower() in alg.name.lower():
if alg.group in groups:
groupItem = groups[alg.group]
else:
2012-09-18 21:53:25 +03:00
groupItem = QTreeWidgetItem()
2012-09-15 18:25:25 +03:00
groupItem.setText(0,alg.group)
groups[alg.group] = groupItem
algItem = TreeAlgorithmItem(alg)
groupItem.addChild(algItem)
actions = Sextante.actions[providerName]
for action in actions:
if text =="" or text.lower() in action.name.lower():
if action.group in groups:
groupItem = groups[action.group]
else:
2012-09-18 21:53:25 +03:00
groupItem = QTreeWidgetItem()
2012-09-15 18:25:25 +03:00
groupItem.setText(0,action.group)
groups[action.group] = groupItem
algItem = TreeActionItem(action)
groupItem.addChild(algItem)
2012-09-18 21:53:25 +03:00
if len(groups) > 0:
providerItem = QTreeWidgetItem()
2012-09-15 18:25:25 +03:00
providerItem.setText(0, Sextante.getProviderFromName(providerName).getDescription()
+ " [" + str(len(provider)) + " geoalgorithms]")
providerItem.setIcon(0, Sextante.getProviderFromName(providerName).getIcon())
for groupItem in groups.values():
providerItem.addChild(groupItem)
self.algorithmTree.addTopLevelItem(providerItem)
providerItem.setExpanded(text!="")
for groupItem in groups.values():
if text != "":
groupItem.setExpanded(True)
self.algorithmTree.sortItems(0, Qt.AscendingOrder)
showRecent = SextanteConfig.getSetting(SextanteConfig.SHOW_RECENT_ALGORITHMS)
if showRecent:
recent = SextanteLog.getRecentAlgorithms()
if len(recent) != 0:
found = False
2012-09-18 21:53:25 +03:00
recentItem = QTreeWidgetItem()
recentItem.setText(0, self.tr("Recently used algorithms"))
2012-09-15 18:25:25 +03:00
for algname in recent:
alg = Sextante.getAlgorithm(algname)
if alg is not None:
algItem = TreeAlgorithmItem(alg)
recentItem.addChild(algItem)
found = True
if found:
self.algorithmTree.insertTopLevelItem(0, recentItem)
recentItem.setExpanded(True)
self.algorithmTree.setWordWrap(True)
2012-09-18 21:53:25 +03:00
class TreeAlgorithmItem(QTreeWidgetItem):
2012-09-15 18:25:25 +03:00
def __init__(self, alg):
QTreeWidgetItem.__init__(self)
self.alg = alg
self.setText(0, alg.name)
self.setIcon(0, alg.getIcon())
self.setToolTip(0, alg.name)
2012-09-18 21:53:25 +03:00
class TreeActionItem(QTreeWidgetItem):
2012-09-15 18:25:25 +03:00
def __init__(self, action):
QTreeWidgetItem.__init__(self)
self.action = action
self.setText(0, action.name)
self.setIcon(0, action.getIcon())