Skip to content

Commit

Permalink
added support for custom output rendering styles
Browse files Browse the repository at this point in the history
git-svn-id: http://sextante.googlecode.com/svn/trunk/soft/bindings/qgis-plugin@50 881b9c09-3ef8-f3c2-ec3d-21d735c97f4d
  • Loading branch information
volayaf committed Mar 21, 2012
1 parent 772e8e7 commit 3e95fb5
Show file tree
Hide file tree
Showing 8 changed files with 169 additions and 15 deletions.
23 changes: 12 additions & 11 deletions src/sextante/core/QGisLayers.py
Expand Up @@ -16,7 +16,7 @@ def getRasterLayers():
raster = list()

for layer in layers:
if layer.type() == layer.RasterLayer and not layer.usesProvider():
if layer.type() == layer.RasterLayer:
raster.append(layer)
return raster

Expand All @@ -25,7 +25,7 @@ def getVectorLayers(shapetype=-1):
layers = QGisLayers.iface.legendInterface().layers()
vector = list()
for layer in layers:
if layer.type() == layer.VectorLayer and not layer.usesProvider():
if layer.type() == layer.VectorLayer:
if shapetype == QGisLayers.ALL_TYPES or layer.geometryType() == shapetype:
vector.append(layer)
return vector
Expand All @@ -52,7 +52,7 @@ def loadList(layers):
QGisLayers.load(layer)

@staticmethod
def load(layer, name = None, crs = None):
def load(layer, name = None, crs = None, style = None):
if layer == None:
return
prjSetting = None
Expand All @@ -67,21 +67,22 @@ def load(layer, name = None, crs = None):
if qgslayer.isValid():
if crs != None:
qgslayer.setCrs(crs,False)
if qgslayer.geometryType == 0:
style = SextanteConfig.getSetting(SextanteConfig.VECTOR_POINT_STYLE)
elif qgslayer.geometryType == 1:
style = SextanteConfig.getSetting(SextanteConfig.VECTOR_LINE_STYLE)
else:
style = SextanteConfig.getSetting(SextanteConfig.VECTOR_POLYGON_STYLE)
if style == None:
if qgslayer.geometryType == 0:
style = SextanteConfig.getSetting(SextanteConfig.VECTOR_POINT_STYLE)
elif qgslayer.geometryType == 1:
style = SextanteConfig.getSetting(SextanteConfig.VECTOR_LINE_STYLE)
else:
style = SextanteConfig.getSetting(SextanteConfig.VECTOR_POLYGON_STYLE)
qgslayer.loadNamedStyle(style)
QgsMapLayerRegistry.instance().addMapLayer(qgslayer)
else:
qgslayer = QgsRasterLayer(layer, name)
if qgslayer.isValid():
if crs != None:
qgslayer.setCrs(crs,False)

style = SextanteConfig.getSetting(SextanteConfig.RASTER_STYLE)
if style == None:
style = SextanteConfig.getSetting(SextanteConfig.RASTER_STYLE)
qgslayer.loadNamedStyle(style)
QgsMapLayerRegistry.instance().addMapLayer(qgslayer)
QGisLayers.iface.legendInterface().refreshLayerSymbology(qgslayer)
Expand Down
2 changes: 2 additions & 0 deletions src/sextante/core/Sextante.py
Expand Up @@ -16,6 +16,7 @@
from sextante.r.RAlgorithmProvider import RAlgorithmProvider
from sextante.parameters.ParameterSelection import ParameterSelection
from sextante.grass.GrassAlgorithmProvider import GrassAlgorithmProvider
from sextante.gui.RenderingStyles import RenderingStyles

class Sextante:

Expand Down Expand Up @@ -48,6 +49,7 @@ def initialize():
SextanteLog.startLogging()
SextanteConfig.initialize()
SextanteConfig.loadSettings()
RenderingStyles.loadStyles()
Sextante.loadAlgorithms()
Sextante.loadActions()
Sextante.loadContextMenuActions()
Expand Down
11 changes: 8 additions & 3 deletions src/sextante/grass/GrassAlgorithm.py
Expand Up @@ -25,16 +25,21 @@ class GrassAlgorithm(GeoAlgorithm):

def __init__(self, descriptionfile):
GeoAlgorithm.__init__(self)
self._descriptionFile = descriptionfile
self.descriptionFile = descriptionfile
self.defineCharacteristicsFromFile()
self.numExportedLayers = 0
self.needsregion = False

def __deepcopy__(self,memo):
newone = GrassAlgorithm(self.descriptionFile)
newone.provider = self.provider
return newone

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

def defineCharacteristicsFromFile(self):
lines = open(self._descriptionFile)
lines = open(self.descriptionFile)
line = lines.readline().strip("\n").strip()
self.name = line
line = lines.readline().strip("\n").strip()
Expand All @@ -50,7 +55,7 @@ def defineCharacteristicsFromFile(self):
self.addOutput(OutputFactory.getFromString(line))
line = lines.readline().strip("\n").strip()
except Exception,e:
SextanteLog.addToLog(SextanteLog.LOG_ERROR, "Could not open GRASS algorithm: " + self._descriptionFile + "\n" + line)
SextanteLog.addToLog(SextanteLog.LOG_ERROR, "Could not open GRASS algorithm: " + self.descriptionFile + "\n" + line)
raise e
lines.close()

Expand Down
81 changes: 81 additions & 0 deletions src/sextante/gui/EditRenderingStylesDialog.py
@@ -0,0 +1,81 @@
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4 import QtCore, QtGui
from sextante.outputs.OutputRaster import OutputRaster
from sextante.outputs.OutputVector import OutputVector
from sextante.gui.RenderingStyles import RenderingStyles

class EditRenderingStylesDialog(QtGui.QDialog):
def __init__(self, alg):
QtGui.QDialog.__init__(self)
self.setModal(True)
self.alg = alg
self.setupUi()

def setupUi(self):
self.valueItems = {}
self.dependentItems = {}
self.resize(650, 450)
self.buttonBox = QtGui.QDialogButtonBox()
self.buttonBox.setOrientation(QtCore.Qt.Horizontal)
self.buttonBox.setStandardButtons(QtGui.QDialogButtonBox.Cancel|QtGui.QDialogButtonBox.Ok)
self.buttonBox.setObjectName("buttonBox")
self.tableWidget = QtGui.QTableWidget()
self.tableWidget.setSelectionMode(QtGui.QAbstractItemView.NoSelection)
self.tableWidget.setColumnCount(2)
self.tableWidget.setColumnWidth(0,300)
self.tableWidget.setColumnWidth(1,300)
self.tableWidget.setHorizontalHeaderItem(0, QtGui.QTableWidgetItem("Output"))
self.tableWidget.setHorizontalHeaderItem(1, QtGui.QTableWidgetItem("Style"))
self.tableWidget.setObjectName("tableWidget")
self.tableWidget.verticalHeader().setVisible(False)
self.tableWidget.horizontalHeader().setResizeMode(QtGui.QHeaderView.Stretch)
self.setTableContent()
self.setWindowTitle(self.alg.name)
self.verticalLayout = QtGui.QVBoxLayout()
self.verticalLayout.setSpacing(2)
self.verticalLayout.setMargin(0)
self.verticalLayout.setObjectName("vLayout")
self.verticalLayout.addWidget(self.tableWidget)
self.verticalLayout.addWidget(self.buttonBox)
self.setLayout(self.verticalLayout)
QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL("accepted()"), self.okPressed)
QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL("rejected()"), self.cancelPressed)
QtCore.QMetaObject.connectSlotsByName(self)

def setTableContent(self):
numOutputs = 0
for output in self.alg.outputs:
if isinstance(output, (OutputVector, OutputRaster)):
if isinstance(output, OutputVector):
if output.hidden:
continue
numOutputs += 1
self.tableWidget.setRowCount(numOutputs)

i = 0
for output in self.alg.outputs:
if isinstance(output, OutputVector):
if output.hidden:
continue
item = QtGui.QTableWidgetItem(output.description + "<" + output.__module__.split(".")[-1] + ">")
item.setFlags(QtCore.Qt.ItemIsEnabled)
self.tableWidget.setItem(i,0, item)
item = QtGui.QLineEdit()
style = RenderingStyles.getStyle(self.alg.commandLineName(), output.name)
if style:
item.setText(str(style))
self.valueItems[output.name] = item
self.tableWidget.setCellWidget(i,1, item)
self.tableWidget.setRowHeight(i,22)
i+=1

def okPressed(self):
styles = {}
for key in self.valueItems.keys():
styles[key] = str(self.valueItems[key].text())
RenderingStyles.addAlgStylesAndSave(self.alg.commandLineName(), styles)
self.close()

def cancelPressed(self):
self.close()
49 changes: 49 additions & 0 deletions src/sextante/gui/RenderingStyles.py
@@ -0,0 +1,49 @@
from sextante.core.SextanteUtils import SextanteUtils
import os.path

class RenderingStyles():

styles = {}

@staticmethod
def addAlgStylesAndSave(algname, styles):
RenderingStyles.styles[algname] = styles
RenderingStyles.saveSettings()


@staticmethod
def configFile():
return os.path.join(SextanteUtils.userFolder(), "sextante_qgis_styles.conf")

@staticmethod
def loadStyles():
if not os.path.isfile(RenderingStyles.configFile()):
return
lines = open(RenderingStyles.configFile())
line = lines.readline().strip("\n")
while line != "":
tokens = line.split("|")
if tokens[0] in RenderingStyles.styles.keys():
RenderingStyles.styles[tokens[0]][tokens[1]] = tokens[2]
else:
alg = {}
alg[tokens[1]]=tokens[2]
RenderingStyles.styles[tokens[0]] = alg
line = lines.readline().strip("\n")
lines.close()

@staticmethod
def saveSettings():
fout = open(RenderingStyles.configFile(), "w")
for alg in RenderingStyles.styles.keys():
for out in RenderingStyles.styles[alg].keys():
fout.write(alg + "|" + out + "|" + RenderingStyles.styles[alg][out] + "\n")
fout.close()

@staticmethod
def getStyle(algname, outputname):
if algname in RenderingStyles.styles:
if outputname in RenderingStyles.styles[algname]:
return RenderingStyles.styles[algname][outputname]
return None

3 changes: 2 additions & 1 deletion src/sextante/gui/SextantePostprocessing.py
Expand Up @@ -4,6 +4,7 @@
from sextante.outputs.OutputTable import OutputTable
from sextante.core.SextanteResults import SextanteResults
from sextante.gui.ResultsDialog import ResultsDialog
from sextante.gui.RenderingStyles import RenderingStyles
class SextantePostprocessing:

@staticmethod
Expand All @@ -14,7 +15,7 @@ def handleAlgorithmResults(alg):
if isinstance(out, OutputVector):
if out.hidden:
continue
QGisLayers.load(out.value, out.description, alg.crs)
QGisLayers.load(out.value, out.description, alg.crs, RenderingStyles[alg.commandLineName()][out.name])
elif isinstance(out, OutputTable):
pass #TODO*****
else:
Expand Down
11 changes: 11 additions & 0 deletions src/sextante/gui/SextanteToolbox.py
Expand Up @@ -5,6 +5,7 @@
from sextante.gui.ParametersDialog import ParametersDialog
import copy
from sextante.gui.BatchProcessingDialog import BatchProcessingDialog
from sextante.gui.EditRenderingStylesDialog import EditRenderingStylesDialog

try:
_fromUtf8 = QtCore.QString.fromUtf8
Expand Down Expand Up @@ -62,6 +63,9 @@ def showPopupMenu(self,point):
executeBatchAction = QtGui.QAction("Execute as batch process", self.algorithmTree)
executeBatchAction.triggered.connect(self.executeAlgorithmAsBatchProcess)
popupmenu.addAction(executeBatchAction)
editRenderingStylesAction = QtGui.QAction("Edit rendering styles for outputs", self.algorithmTree)
editRenderingStylesAction.triggered.connect(self.editRenderingStyles)
popupmenu.addAction(editRenderingStylesAction)
actions = Sextante.contextMenuActions
for action in actions:
action.setData(alg,self)
Expand All @@ -72,6 +76,13 @@ def showPopupMenu(self,point):

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()
Expand Down
4 changes: 4 additions & 0 deletions src/sextante/script/ScriptAlgorithm.py
Expand Up @@ -23,6 +23,10 @@ def __init__(self, descriptionfile):
self.descriptionFile = descriptionfile
self.defineCharacteristicsFromFile()

def __deepcopy__(self,memo):
newone = ScriptAlgorithm(self.descriptionFile)
newone.provider = self.provider
return newone

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

0 comments on commit 3e95fb5

Please sign in to comment.