Skip to content
Permalink
Browse files

Creted history and log dialog

git-svn-id: http://sextante.googlecode.com/svn/trunk/soft/bindings/qgis-plugin@22 881b9c09-3ef8-f3c2-ec3d-21d735c97f4d
  • Loading branch information
volayaf@gmail.com
volayaf@gmail.com committed Feb 4, 2012
1 parent 2513431 commit c3dd363bb6e3f8fcd3e08573f67aa9dbd696de70
@@ -8,6 +8,7 @@
from sextante.core.Sextante import Sextante
from sextante.gui.SextanteToolbox import SextanteToolbox
from sextante.core.QGisLayers import QGisLayers
from sextante.gui.HistoryDialog import HistoryDialog

cmd_folder = os.path.split(inspect.getfile( inspect.currentframe() ))[0]
if cmd_folder not in sys.path:
@@ -39,6 +40,12 @@ def initGui(self):
QObject.connect(self.modelerAction, SIGNAL("triggered()"), self.openModeler)
self.menu.addAction(self.modelerAction)

icon = QIcon(os.path.dirname(__file__) + "/history.gif")
self.historyAction = QAction(icon, \
"&SEXTANTE History and log", self.iface.mainWindow())
QObject.connect(self.historyAction, SIGNAL("triggered()"), self.openHistory)
self.menu.addAction(self.historyAction)

menuBar = self.iface.mainWindow().menuBar()
menuBar.insertMenu(menuBar.actions()[-1], self.menu)

@@ -51,3 +58,8 @@ def openToolbox(self):

def openModeler(self):
pass

def openHistory(self):
dlg = HistoryDialog()
dlg.exec_()

@@ -7,10 +7,12 @@
from sextante.core.QGisLayers import QGisLayers
from sextante.gui.AlgorithmExecutor import AlgorithmExecutor, SilentProgress
from sextante.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
from sextante.core.SextanteUtils import SextanteUtils
from sextante.mmqgis.MMQGISAlgorithmProvider import MMQGISAlgorithmProvider

class Sextante:

providers = [SagaAlgorithmProvider(), ScriptAlgorithmProvider()]
providers = [SagaAlgorithmProvider(), ScriptAlgorithmProvider(),MMQGISAlgorithmProvider()]
algs = {}
actions = {}
contextMenuActions = []
@@ -25,6 +27,13 @@ def initialize():
Sextante.loadActions()
Sextante.loadContextMenuActions()


@staticmethod
def updateProviders():
for provider in Sextante.providers:
provider.loadAlgorithms()


@staticmethod
def loadAlgorithms():
for provider in Sextante.providers:
@@ -80,6 +89,7 @@ def alghelp(name):
else:
print "Algorithm not found"


@staticmethod
def runalg(name, *args):
alg = Sextante.getAlgorithm(name)
@@ -105,6 +115,8 @@ def runalg(name, *args):
return
i = i +1

SextanteUtils.addToLog(SextanteUtils.LOG_ALGORITHM, alg.getAsCommand())

try:
QApplication.setOverrideCursor(QCursor(Qt.WaitCursor))
AlgorithmExecutor.runalg(alg, SilentProgress())
@@ -123,3 +135,41 @@ def getObject(string):
QGisLayers.getObjectFromUri(string)


@staticmethod
def runandload(name, *args):
#a quick fix to call algorithms from the history dialog
alg = Sextante.getAlgorithm(name)
if alg == None:
#in theory, this could not happen. Maybe we should show a message box?
QMessageBox.critical(None,"Error", "Error: Algorithm not found\n")
return
if len(args) != len(alg.parameters) + len(alg.outputs):
QMessageBox.critical(None,"Error", "Error: Wrong number of parameters")
Sextante.alghelp(name)
return

alg = copy.deepcopy(alg)
i = 0
for param in alg.parameters:
if not param.setValue(args[i]):
QMessageBox.critical(None, "Error", "Error: Wrong parameter value: " + args[i])
return
i = i +1

for output in alg.outputs:
if not output.setChannel(args[i]):
QMessageBox.critical(None, "Error", "Error: Wrong output channel: " + args[i])
return
i = i +1

try:
QApplication.setOverrideCursor(QCursor(Qt.WaitCursor))
AlgorithmExecutor.runalg(alg, SilentProgress())
QApplication.restoreOverrideCursor()
QGisLayers.loadFromAlg(alg)
except GeoAlgorithmExecutionException, e:
QMessageBox.critical(None, "Error", e.msg)




@@ -4,6 +4,7 @@
from sextante.outputs.OutputVector import OutputVector
from sextante.outputs.OutputRaster import OutputRaster
import datetime

class SextanteUtils:

NUM_EXPORTED = 1
@@ -87,28 +88,28 @@ def getLogEntries():
tokens = line.split("|")
text=""
for i in range(2, len(tokens)):
text+=tokens[i] + "\n"
text+=tokens[i] + "|"
if line.startswith(SextanteUtils.LOG_ERROR):
errors.append(LogElement(tokens[1], text))
errors.append(LogEntry(tokens[1], text))
elif line.startswith(SextanteUtils.LOG_ALGORITHM):
algorithms.append(LogElement(tokens[1], text))
algorithms.append(LogEntry(tokens[1], tokens[2]))
elif line.startswith(SextanteUtils.LOG_WARNING):
warnings.append(LogElement(tokens[1], text))
warnings.append(LogEntry(tokens[1], text))
elif line.startswith(SextanteUtils.LOG_INFO):
info.append(LogElement(tokens[1], text))
info.append(LogEntry(tokens[1], text))
line = lines.readline()
lines.close()
entries[SextanteUtils.LOG_ERROR] = errors
entries[SextanteUtils.LOG_ALGORITHM] = algorithms
entries[SextanteUtils.LOG_INFO] = info
entries[SextanteUtils.LOG_WARNING] = warnings
return entries

class LogElement():

class LogEntry():
def __init__(self, date, text):
self.date = date
self.text = text



def mkdir(newdir):
if os.path.isdir(newdir):
pass
@@ -7,7 +7,6 @@ class AlgorithmExecutor:

@staticmethod
def runalg(alg, progress):
SextanteUtils.addToLog(SextanteUtils.LOG_ALGORITHM, alg.getAsCommand())
alg.execute(progress)
#=======================================================================
# th = RunAlgorithmThread(alg, progress)
@@ -0,0 +1,59 @@
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4 import QtCore, QtGui
from sextante.core.SextanteUtils import SextanteUtils

class HistoryDialog(QtGui.QDialog):
def __init__(self):
QtGui.QDialog.__init__(self)
self.setModal(True)
self.setupUi()

def setupUi(self):
self.setObjectName("HistoryDialog")
self.resize(650, 505)
self.tree = QtGui.QTreeWidget(self)
self.tree.setGeometry(QtCore.QRect(5, 5, 640, 245))
self.tree.setHeaderHidden(True)
self.tree.doubleClicked.connect(self.executeAlgorithm)
QObject.connect(self.tree, QtCore.SIGNAL("clicked()"), self.changeText)
self.fillTree()
self.text = QtGui.QTextEdit(self)
self.text.setGeometry(QtCore.QRect(5, 260, 640, 245))
self.text.setObjectName("text")
self.text.setReadOnly(True)
self.setWindowTitle("History")
QtCore.QMetaObject.connectSlotsByName(self)

def fillTree(self):
elements = SextanteUtils.getLogEntries()
for category in elements.keys():
groupItem = QtGui.QTreeWidgetItem()
groupItem.setText(0,category)
for entry in elements[category]:
item = TreeLogEntryItem(entry, category==SextanteUtils.LOG_ALGORITHM)
groupItem.addChild(item)
self.tree.addTopLevelItem(groupItem)


def executeAlgorithm(self):
item = self.tree.currentItem()
if isinstance(item, TreeLogEntryItem):
if item.isAlg:
script = "from sextante.core.Sextante import Sextante\n"
script+=item.entry.text.replace("runalg(","runandload(")
exec(script)


def changeText(self):
item = self.tree.currentItem()
if isinstance(item, TreeLogEntryItem):
self.text.setText(item.entry.msg)


class TreeLogEntryItem(QtGui.QTreeWidgetItem):
def __init__(self, entry, isAlg):
QTreeWidgetItem.__init__(self)
self.entry = entry
self.isAlg = isAlg
self.setText(0, "[" + entry.date + "]" + entry.text)#.split("\n")[0])
@@ -7,6 +7,7 @@
from sextante.core.QGisLayers import QGisLayers
from sextante.gui.AlgorithmExecutor import AlgorithmExecutor, SilentProgress
from sextante.gui.ProgressDialog import ProgressDialog
from sextante.core.SextanteUtils import SextanteUtils

try:
_fromUtf8 = QtCore.QString.fromUtf8
@@ -25,6 +26,7 @@ def __init__(self, iface):
class Ui_SextanteToolbox(object):

def updateTree(self):
Sextante.updateProviders()
Sextante.loadAlgorithms()
self.fillTree()

@@ -92,6 +94,7 @@ def executeAlgorithm(self):
dlg.exec_()
if dlg.alg != None:
QApplication.setOverrideCursor(QCursor(Qt.WaitCursor))
SextanteUtils.addToLog(SextanteUtils.LOG_ALGORITHM, alg.getAsCommand())
AlgorithmExecutor.runalg(alg, SilentProgress())
QGisLayers.loadFromAlg(alg)
QApplication.restoreOverrideCursor()
BIN +146 Bytes src/sextante/history.gif
Binary file not shown.
@@ -0,0 +1,14 @@
import os
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from sextante.script.ScriptAlgorithmProvider import ScriptAlgorithmProvider
class MMQGISAlgorithmProvider(ScriptAlgorithmProvider):

def getIcon(self):
return QIcon(os.path.dirname(__file__) + "/script.png")

def scriptsFolder(self):
return os.path.dirname(__file__) + "/scripts"

def getName(self):
return "mmqgis (vector analysis)"
Empty file.
@@ -53,6 +53,7 @@ def executeSaga(alg, progress):

proc = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE,stderr=subprocess.STDOUT).stdout
loglines=[]
loglines.append("SAGA Execution log message")
while 1:
line = proc.readline()
if not line:
@@ -4,7 +4,7 @@
class DeleteScriptAction(ContextAction):

def __init__(self):
self.name="Edit script"
self.name="Delete script"

def isEnabled(self):
return isinstance(self.alg, ScriptAlgorithm)
@@ -2,50 +2,51 @@
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from sextante.script.ScriptUtils import ScriptUtils
import os

class EditScriptDialog(QtGui.QDialog):
def __init__(self, alg):
self.alg = alg
QtGui.QDialog.__init__(self)
self.setModal(True)
self.ui = Ui_EditScriptDialog()
self.ui.setupUi(self)
self.setupUi()
self.update = False

class Ui_EditScriptDialog(object):
def setupUi(self, dialog):
self.dialog = dialog
dialog.setObjectName("Dialog")
dialog.resize(600, 350)
dialog.setWindowTitle("Edit script")
self.text = QtGui.QTextEdit(dialog)
self.text.setGeometry(QtCore.QRect(10, 10, 470, 300))
def setupUi(self):
self.setObjectName("Dialog")
self.resize(655, 360)
self.setWindowTitle("Edit script")
self.text = QtGui.QTextEdit(self)
self.text.setGeometry(QtCore.QRect(5, 5, 550, 350))
self.text.setObjectName("text")
self.text.setEnabled(True)
if dialog.alg != None:
self.text.setText(dialog.alg.script)
self.saveButton = QtGui.QPushButton(dialog)
self.saveButton.setGeometry(QtCore.QRect(490, 250, 81, 23))
if self.alg != None:
self.text.setText(self.alg.script)
self.saveButton = QtGui.QPushButton(self)
self.saveButton.setGeometry(QtCore.QRect(570, 300, 80, 23))
self.saveButton.setObjectName("saveButton")
self.saveButton.setText("Save")
self.cancelButton = QtGui.QPushButton(dialog)
self.cancelButton.setGeometry(QtCore.QRect(490, 290, 81, 23))
self.cancelButton = QtGui.QPushButton(self)
self.cancelButton.setGeometry(QtCore.QRect(570, 327, 80, 23))
self.cancelButton.setObjectName("cancelButton")
self.cancelButton.setText("Cancel")
QObject.connect(self.saveButton, QtCore.SIGNAL("clicked()"), self.saveAlgorithm)
QObject.connect(self.cancelButton, QtCore.SIGNAL("clicked()"), self.cancel)
QtCore.QMetaObject.connectSlotsByName(dialog)
QtCore.QMetaObject.connectSlotsByName(self)

def saveAlgorithm(self):
filename = QtGui.QFileDialog.getSaveFileName(self.dialog, "Save Script", ScriptUtils.scriptsFolder(), "Python scripts (*.py)")
if self.alg!=None:
filename = os.path.join(ScriptUtils.scriptsFolder(), self.alg.descriptionFile)
else:
filename = QtGui.QFileDialog.getSaveFileName(self, "Save Script", ScriptUtils.scriptsFolder(), "Python scripts (*.py)")
if filename:
text = self.text.toPlainText()
fout = open(filename, "w")
fout.write(text)
fout.close()
self.dialog.update = True
self.dialog.close()
self.update = True
self.close()

def cancel(self):
self.dialog.update = False
self.update = False
self.dialog.close()
@@ -32,10 +32,10 @@ def defineCharacteristicsFromFile(self):
lines = open(fullpath)
line = lines.readline()
while line != "":
if line.startswith("#"):
if line.startswith("##"):
self.processParameterLine(line.strip("\n"))
else:
self.script += line

self.script += line
line = lines.readline()
lines.close()

0 comments on commit c3dd363

Please sign in to comment.
You can’t perform that action at this time.