Skip to content

Commit 3e95fb5

Browse files
author
volayaf
committed
added support for custom output rendering styles
git-svn-id: http://sextante.googlecode.com/svn/trunk/soft/bindings/qgis-plugin@50 881b9c09-3ef8-f3c2-ec3d-21d735c97f4d
1 parent 772e8e7 commit 3e95fb5

8 files changed

+169
-15
lines changed

src/sextante/core/QGisLayers.py

+12-11
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def getRasterLayers():
1616
raster = list()
1717

1818
for layer in layers:
19-
if layer.type() == layer.RasterLayer and not layer.usesProvider():
19+
if layer.type() == layer.RasterLayer:
2020
raster.append(layer)
2121
return raster
2222

@@ -25,7 +25,7 @@ def getVectorLayers(shapetype=-1):
2525
layers = QGisLayers.iface.legendInterface().layers()
2626
vector = list()
2727
for layer in layers:
28-
if layer.type() == layer.VectorLayer and not layer.usesProvider():
28+
if layer.type() == layer.VectorLayer:
2929
if shapetype == QGisLayers.ALL_TYPES or layer.geometryType() == shapetype:
3030
vector.append(layer)
3131
return vector
@@ -52,7 +52,7 @@ def loadList(layers):
5252
QGisLayers.load(layer)
5353

5454
@staticmethod
55-
def load(layer, name = None, crs = None):
55+
def load(layer, name = None, crs = None, style = None):
5656
if layer == None:
5757
return
5858
prjSetting = None
@@ -67,21 +67,22 @@ def load(layer, name = None, crs = None):
6767
if qgslayer.isValid():
6868
if crs != None:
6969
qgslayer.setCrs(crs,False)
70-
if qgslayer.geometryType == 0:
71-
style = SextanteConfig.getSetting(SextanteConfig.VECTOR_POINT_STYLE)
72-
elif qgslayer.geometryType == 1:
73-
style = SextanteConfig.getSetting(SextanteConfig.VECTOR_LINE_STYLE)
74-
else:
75-
style = SextanteConfig.getSetting(SextanteConfig.VECTOR_POLYGON_STYLE)
70+
if style == None:
71+
if qgslayer.geometryType == 0:
72+
style = SextanteConfig.getSetting(SextanteConfig.VECTOR_POINT_STYLE)
73+
elif qgslayer.geometryType == 1:
74+
style = SextanteConfig.getSetting(SextanteConfig.VECTOR_LINE_STYLE)
75+
else:
76+
style = SextanteConfig.getSetting(SextanteConfig.VECTOR_POLYGON_STYLE)
7677
qgslayer.loadNamedStyle(style)
7778
QgsMapLayerRegistry.instance().addMapLayer(qgslayer)
7879
else:
7980
qgslayer = QgsRasterLayer(layer, name)
8081
if qgslayer.isValid():
8182
if crs != None:
8283
qgslayer.setCrs(crs,False)
83-
84-
style = SextanteConfig.getSetting(SextanteConfig.RASTER_STYLE)
84+
if style == None:
85+
style = SextanteConfig.getSetting(SextanteConfig.RASTER_STYLE)
8586
qgslayer.loadNamedStyle(style)
8687
QgsMapLayerRegistry.instance().addMapLayer(qgslayer)
8788
QGisLayers.iface.legendInterface().refreshLayerSymbology(qgslayer)

src/sextante/core/Sextante.py

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from sextante.r.RAlgorithmProvider import RAlgorithmProvider
1717
from sextante.parameters.ParameterSelection import ParameterSelection
1818
from sextante.grass.GrassAlgorithmProvider import GrassAlgorithmProvider
19+
from sextante.gui.RenderingStyles import RenderingStyles
1920

2021
class Sextante:
2122

@@ -48,6 +49,7 @@ def initialize():
4849
SextanteLog.startLogging()
4950
SextanteConfig.initialize()
5051
SextanteConfig.loadSettings()
52+
RenderingStyles.loadStyles()
5153
Sextante.loadAlgorithms()
5254
Sextante.loadActions()
5355
Sextante.loadContextMenuActions()

src/sextante/grass/GrassAlgorithm.py

+8-3
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,21 @@ class GrassAlgorithm(GeoAlgorithm):
2525

2626
def __init__(self, descriptionfile):
2727
GeoAlgorithm.__init__(self)
28-
self._descriptionFile = descriptionfile
28+
self.descriptionFile = descriptionfile
2929
self.defineCharacteristicsFromFile()
3030
self.numExportedLayers = 0
3131
self.needsregion = False
3232

33+
def __deepcopy__(self,memo):
34+
newone = GrassAlgorithm(self.descriptionFile)
35+
newone.provider = self.provider
36+
return newone
37+
3338
def getIcon(self):
3439
return QIcon(os.path.dirname(__file__) + "/../images/grass.png")
3540

3641
def defineCharacteristicsFromFile(self):
37-
lines = open(self._descriptionFile)
42+
lines = open(self.descriptionFile)
3843
line = lines.readline().strip("\n").strip()
3944
self.name = line
4045
line = lines.readline().strip("\n").strip()
@@ -50,7 +55,7 @@ def defineCharacteristicsFromFile(self):
5055
self.addOutput(OutputFactory.getFromString(line))
5156
line = lines.readline().strip("\n").strip()
5257
except Exception,e:
53-
SextanteLog.addToLog(SextanteLog.LOG_ERROR, "Could not open GRASS algorithm: " + self._descriptionFile + "\n" + line)
58+
SextanteLog.addToLog(SextanteLog.LOG_ERROR, "Could not open GRASS algorithm: " + self.descriptionFile + "\n" + line)
5459
raise e
5560
lines.close()
5661

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
from PyQt4.QtCore import *
2+
from PyQt4.QtGui import *
3+
from PyQt4 import QtCore, QtGui
4+
from sextante.outputs.OutputRaster import OutputRaster
5+
from sextante.outputs.OutputVector import OutputVector
6+
from sextante.gui.RenderingStyles import RenderingStyles
7+
8+
class EditRenderingStylesDialog(QtGui.QDialog):
9+
def __init__(self, alg):
10+
QtGui.QDialog.__init__(self)
11+
self.setModal(True)
12+
self.alg = alg
13+
self.setupUi()
14+
15+
def setupUi(self):
16+
self.valueItems = {}
17+
self.dependentItems = {}
18+
self.resize(650, 450)
19+
self.buttonBox = QtGui.QDialogButtonBox()
20+
self.buttonBox.setOrientation(QtCore.Qt.Horizontal)
21+
self.buttonBox.setStandardButtons(QtGui.QDialogButtonBox.Cancel|QtGui.QDialogButtonBox.Ok)
22+
self.buttonBox.setObjectName("buttonBox")
23+
self.tableWidget = QtGui.QTableWidget()
24+
self.tableWidget.setSelectionMode(QtGui.QAbstractItemView.NoSelection)
25+
self.tableWidget.setColumnCount(2)
26+
self.tableWidget.setColumnWidth(0,300)
27+
self.tableWidget.setColumnWidth(1,300)
28+
self.tableWidget.setHorizontalHeaderItem(0, QtGui.QTableWidgetItem("Output"))
29+
self.tableWidget.setHorizontalHeaderItem(1, QtGui.QTableWidgetItem("Style"))
30+
self.tableWidget.setObjectName("tableWidget")
31+
self.tableWidget.verticalHeader().setVisible(False)
32+
self.tableWidget.horizontalHeader().setResizeMode(QtGui.QHeaderView.Stretch)
33+
self.setTableContent()
34+
self.setWindowTitle(self.alg.name)
35+
self.verticalLayout = QtGui.QVBoxLayout()
36+
self.verticalLayout.setSpacing(2)
37+
self.verticalLayout.setMargin(0)
38+
self.verticalLayout.setObjectName("vLayout")
39+
self.verticalLayout.addWidget(self.tableWidget)
40+
self.verticalLayout.addWidget(self.buttonBox)
41+
self.setLayout(self.verticalLayout)
42+
QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL("accepted()"), self.okPressed)
43+
QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL("rejected()"), self.cancelPressed)
44+
QtCore.QMetaObject.connectSlotsByName(self)
45+
46+
def setTableContent(self):
47+
numOutputs = 0
48+
for output in self.alg.outputs:
49+
if isinstance(output, (OutputVector, OutputRaster)):
50+
if isinstance(output, OutputVector):
51+
if output.hidden:
52+
continue
53+
numOutputs += 1
54+
self.tableWidget.setRowCount(numOutputs)
55+
56+
i = 0
57+
for output in self.alg.outputs:
58+
if isinstance(output, OutputVector):
59+
if output.hidden:
60+
continue
61+
item = QtGui.QTableWidgetItem(output.description + "<" + output.__module__.split(".")[-1] + ">")
62+
item.setFlags(QtCore.Qt.ItemIsEnabled)
63+
self.tableWidget.setItem(i,0, item)
64+
item = QtGui.QLineEdit()
65+
style = RenderingStyles.getStyle(self.alg.commandLineName(), output.name)
66+
if style:
67+
item.setText(str(style))
68+
self.valueItems[output.name] = item
69+
self.tableWidget.setCellWidget(i,1, item)
70+
self.tableWidget.setRowHeight(i,22)
71+
i+=1
72+
73+
def okPressed(self):
74+
styles = {}
75+
for key in self.valueItems.keys():
76+
styles[key] = str(self.valueItems[key].text())
77+
RenderingStyles.addAlgStylesAndSave(self.alg.commandLineName(), styles)
78+
self.close()
79+
80+
def cancelPressed(self):
81+
self.close()

src/sextante/gui/RenderingStyles.py

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
from sextante.core.SextanteUtils import SextanteUtils
2+
import os.path
3+
4+
class RenderingStyles():
5+
6+
styles = {}
7+
8+
@staticmethod
9+
def addAlgStylesAndSave(algname, styles):
10+
RenderingStyles.styles[algname] = styles
11+
RenderingStyles.saveSettings()
12+
13+
14+
@staticmethod
15+
def configFile():
16+
return os.path.join(SextanteUtils.userFolder(), "sextante_qgis_styles.conf")
17+
18+
@staticmethod
19+
def loadStyles():
20+
if not os.path.isfile(RenderingStyles.configFile()):
21+
return
22+
lines = open(RenderingStyles.configFile())
23+
line = lines.readline().strip("\n")
24+
while line != "":
25+
tokens = line.split("|")
26+
if tokens[0] in RenderingStyles.styles.keys():
27+
RenderingStyles.styles[tokens[0]][tokens[1]] = tokens[2]
28+
else:
29+
alg = {}
30+
alg[tokens[1]]=tokens[2]
31+
RenderingStyles.styles[tokens[0]] = alg
32+
line = lines.readline().strip("\n")
33+
lines.close()
34+
35+
@staticmethod
36+
def saveSettings():
37+
fout = open(RenderingStyles.configFile(), "w")
38+
for alg in RenderingStyles.styles.keys():
39+
for out in RenderingStyles.styles[alg].keys():
40+
fout.write(alg + "|" + out + "|" + RenderingStyles.styles[alg][out] + "\n")
41+
fout.close()
42+
43+
@staticmethod
44+
def getStyle(algname, outputname):
45+
if algname in RenderingStyles.styles:
46+
if outputname in RenderingStyles.styles[algname]:
47+
return RenderingStyles.styles[algname][outputname]
48+
return None
49+

src/sextante/gui/SextantePostprocessing.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from sextante.outputs.OutputTable import OutputTable
55
from sextante.core.SextanteResults import SextanteResults
66
from sextante.gui.ResultsDialog import ResultsDialog
7+
from sextante.gui.RenderingStyles import RenderingStyles
78
class SextantePostprocessing:
89

910
@staticmethod
@@ -14,7 +15,7 @@ def handleAlgorithmResults(alg):
1415
if isinstance(out, OutputVector):
1516
if out.hidden:
1617
continue
17-
QGisLayers.load(out.value, out.description, alg.crs)
18+
QGisLayers.load(out.value, out.description, alg.crs, RenderingStyles[alg.commandLineName()][out.name])
1819
elif isinstance(out, OutputTable):
1920
pass #TODO*****
2021
else:

src/sextante/gui/SextanteToolbox.py

+11
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from sextante.gui.ParametersDialog import ParametersDialog
66
import copy
77
from sextante.gui.BatchProcessingDialog import BatchProcessingDialog
8+
from sextante.gui.EditRenderingStylesDialog import EditRenderingStylesDialog
89

910
try:
1011
_fromUtf8 = QtCore.QString.fromUtf8
@@ -62,6 +63,9 @@ def showPopupMenu(self,point):
6263
executeBatchAction = QtGui.QAction("Execute as batch process", self.algorithmTree)
6364
executeBatchAction.triggered.connect(self.executeAlgorithmAsBatchProcess)
6465
popupmenu.addAction(executeBatchAction)
66+
editRenderingStylesAction = QtGui.QAction("Edit rendering styles for outputs", self.algorithmTree)
67+
editRenderingStylesAction.triggered.connect(self.editRenderingStyles)
68+
popupmenu.addAction(editRenderingStylesAction)
6569
actions = Sextante.contextMenuActions
6670
for action in actions:
6771
action.setData(alg,self)
@@ -72,6 +76,13 @@ def showPopupMenu(self,point):
7276

7377
popupmenu.exec_(self.algorithmTree.mapToGlobal(point))
7478

79+
def editRenderingStyles(self):
80+
item = self.algorithmTree.currentItem()
81+
if isinstance(item, TreeAlgorithmItem):
82+
alg = Sextante.getAlgorithm(item.alg.commandLineName())
83+
dlg = EditRenderingStylesDialog(alg)
84+
dlg.exec_()
85+
7586

7687
def executeAlgorithmAsBatchProcess(self):
7788
item = self.algorithmTree.currentItem()

src/sextante/script/ScriptAlgorithm.py

+4
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ def __init__(self, descriptionfile):
2323
self.descriptionFile = descriptionfile
2424
self.defineCharacteristicsFromFile()
2525

26+
def __deepcopy__(self,memo):
27+
newone = ScriptAlgorithm(self.descriptionFile)
28+
newone.provider = self.provider
29+
return newone
2630

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

0 commit comments

Comments
 (0)