Skip to content

Commit 7b61b83

Browse files
committedDec 7, 2012
added alternative organization in sextante toolbox
1 parent cb7d1ec commit 7b61b83

File tree

10 files changed

+217
-118
lines changed

10 files changed

+217
-118
lines changed
 
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# -*- coding: utf-8 -*-
2+
3+
"""
4+
***************************************************************************
5+
SextanteToolbox.py
6+
---------------------
7+
Date : August 2012
8+
Copyright : (C) 2012 by Victor Olaya
9+
Email : volayaf at gmail dot com
10+
***************************************************************************
11+
* *
12+
* This program is free software; you can redistribute it and/or modify *
13+
* it under the terms of the GNU General Public License as published by *
14+
* the Free Software Foundation; either version 2 of the License, or *
15+
* (at your option) any later version. *
16+
* *
17+
***************************************************************************
18+
"""
19+
from sextante.core.SextanteUtils import SextanteUtils
20+
21+
__author__ = 'Victor Olaya'
22+
__date__ = 'November 2012'
23+
__copyright__ = '(C) 2012, Victor Olaya'
24+
# This will get replaced with a git SHA1 when you do a git archive
25+
__revision__ = '$Format:%H$'
26+
27+
import os
28+
29+
class AlgorithmDecorator():
30+
31+
classification = {};
32+
33+
@staticmethod
34+
def loadClassification():
35+
if not os.path.isfile(AlgorithmDecorator.classificationFile()):
36+
return
37+
lines = open(AlgorithmDecorator.classificationFile())
38+
line = lines.readline().strip("\n")
39+
while line != "":
40+
tokens = line.split("\t")
41+
AlgorithmDecorator.classification[tokens[0]] = (tokens[1], tokens[2]);
42+
line = lines.readline().strip("\n")
43+
lines.close()
44+
45+
@staticmethod
46+
def classificationFile():
47+
return os.path.join(SextanteUtils.userFolder(), "sextante_qgis_algclass.txt")
48+
49+
@staticmethod
50+
def getGroupAndName(alg):
51+
if alg.commandLineName() in AlgorithmDecorator.classification:
52+
return AlgorithmDecorator.classification[alg.commandLineName]
53+
else:
54+
return (alg.group, alg.name)
55+
56+
57+
58+
59+

‎python/plugins/sextante/core/GeoAlgorithm.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,23 +25,20 @@ def __init__(self):
2525
self.parameters = list()
2626
#outputs generated by the algorithm
2727
self.outputs = list()
28+
#name and group for normal toolbox display
2829
self.name = ""
29-
self.group = ""
30+
self.group = ""
3031
#the crs taken from input layers (if possible), and used when loading output layers
3132
self.crs = None
3233
#change any of the following if your algorithm should not appear in the toolbox or modeler
3334
self.showInToolbox = True
3435
self.showInModeler = True
35-
#true if the algorithm has been canceled while it was being executed
36-
#default value is false, so it should be changed in processAlgorithm only if the algorithm
37-
#gets canceled
38-
#self.canceled = False
39-
4036
#to be set by the provider when it loads the algorithm
4137
self.provider = None
4238

4339
self.defineCharacteristics()
44-
40+
41+
4542
def getCopy(self):
4643
newone = copy.copy(self)
4744
newone.parameters = copy.deepcopy(self.parameters)
@@ -52,6 +49,10 @@ def getCopy(self):
5249
#=========================================================
5350
def getIcon(self):
5451
return QtGui.QIcon(os.path.dirname(__file__) + "/../images/alg.png")
52+
53+
@staticmethod
54+
def getDefaultIcon():
55+
return QtGui.QIcon(os.path.dirname(__file__) + "/../images/alg.png")
5556

5657
def helpFile(self):
5758
'''Returns the path to the help file with the description of this algorithm.
@@ -60,9 +61,7 @@ def helpFile(self):
6061

6162
def processAlgorithm(self):
6263
'''Here goes the algorithm itself
63-
There is no return value from this method. If the algorithm gets canceled
64-
while running this method, the self.canceled value should be set to false
65-
instead to indicate it.
64+
There is no return value from this method.
6665
A GeoAlgorithmExecutionException should be raised in case something goes wrong.
6766
'''
6867
pass

‎python/plugins/sextante/core/Sextante.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
* *
1717
***************************************************************************
1818
"""
19+
1920
__author__ = 'Victor Olaya'
2021
__date__ = 'August 2012'
2122
__copyright__ = '(C) 2012, Victor Olaya'
@@ -29,7 +30,7 @@
2930
from sextante.core.SextanteConfig import SextanteConfig
3031
from sextante.core.GeoAlgorithm import GeoAlgorithm
3132
from sextante.core.SextanteLog import SextanteLog
32-
33+
from sextante.core.AlgorithmClassification import AlgorithmDecorator
3334
from sextante.gui.AlgorithmExecutor import AlgorithmExecutor
3435
from sextante.gui.RenderingStyles import RenderingStyles
3536
from sextante.gui.SextantePostprocessing import SextantePostprocessing
@@ -132,9 +133,10 @@ def initialize():
132133
Sextante.modeler.initializeSettings();
133134
#and initialize
134135
SextanteLog.startLogging()
135-
SextanteConfig.initialize()
136+
SextanteConfig.initialize()
136137
SextanteConfig.loadSettings()
137138
RenderingStyles.loadStyles()
139+
AlgorithmDecorator.loadClassification()
138140
Sextante.loadFromProviders()
139141

140142
@staticmethod

‎python/plugins/sextante/core/SextanteConfig.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
class SextanteConfig():
3131

32+
USE_CATEGORIES = "USE_CATEGORIES"
3233
TABLE_LIKE_PARAM_PANEL = "TABLE_LIKE_PARAM_PANEL"
3334
OUTPUT_FOLDER = "OUTPUT_FOLDER"
3435
RASTER_STYLE = "RASTER_STYLE"
@@ -57,6 +58,7 @@ def initialize():
5758
SextanteConfig.addSetting(Setting("General", SextanteConfig.TABLE_LIKE_PARAM_PANEL, "Show table-like parameter panels", False))
5859
SextanteConfig.addSetting(Setting("General", SextanteConfig.USE_FILENAME_AS_LAYER_NAME, "Use filename as layer name", True))
5960
SextanteConfig.addSetting(Setting("General", SextanteConfig.SHOW_RECENT_ALGORITHMS, "Show recently executed algorithms", True))
61+
SextanteConfig.addSetting(Setting("General", SextanteConfig.USE_CATEGORIES, "Use categories to classify algorithms, instead of providers", False))
6062
SextanteConfig.addSetting(Setting("General", SextanteConfig.OUTPUT_FOLDER,
6163
"Output folder", SextanteUtils.tempFolder()))
6264
SextanteConfig.addSetting(Setting("General", SextanteConfig.RASTER_STYLE,"Style for raster layers",""))

‎python/plugins/sextante/grass/GrassAlgorithm.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -213,14 +213,14 @@ def processAlgorithm(self, progress):
213213
if layer in self.exportedLayers.keys():
214214
continue
215215
else:
216-
self.setSessionProjection(value, commands)
216+
self.setSessionProjection(layer, commands)
217217
commands.append(self.exportRasterLayer(layer))
218218
elif param.datatype == ParameterMultipleInput.TYPE_VECTOR_ANY:
219219
for layer in layers:
220220
if layer in self.exportedLayers.keys():
221221
continue
222222
else:
223-
self.setSessionProjection(value, commands)
223+
self.setSessionProjection(layer, commands)
224224
commands.append(self.exportVectorLayer(layer))
225225

226226
region = str(self.getParameterValue(self.GRASS_REGION_EXTENT_PARAMETER))
@@ -272,7 +272,7 @@ def processAlgorithm(self, progress):
272272
#an output name to make sure it is unique if the session uses this algorithm several times
273273
uniqueOutputName = out.name + uniqueSufix
274274
command += (" " + out.name + "=" + uniqueOutputName)
275-
# add output file to exported layers, to indicate that they are present in GRASS
275+
# add output file to exported layers, to indicate that they are present in GRASS
276276
self.exportedLayers[out.value]= uniqueOutputName
277277

278278

@@ -285,7 +285,7 @@ def processAlgorithm(self, progress):
285285
filename = out.value
286286
#Raster layer output: adjust region to layer before exporting
287287
commands.append("g.region rast=" + out.name + uniqueSufix)
288-
outputCommands.append("g.region rast=" + out.name)
288+
outputCommands.append("g.region rast=" + out.name + uniqueSufix)
289289
command = "r.out.gdal -c createopt=\"TFW=YES,COMPRESS=LZW\""
290290
command += " input="
291291
command += out.name + uniqueSufix
@@ -301,7 +301,7 @@ def processAlgorithm(self, progress):
301301
command += " olayer=" + os.path.basename(out.value)[:-4]
302302
command += " type=auto"
303303
commands.append(command)
304-
outputCommands.append("g.region rast=" + out.name)
304+
outputCommands.append(command)
305305

306306
#4 Run GRASS
307307
loglines = []

‎python/plugins/sextante/grass/GrassUtils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ def prepareGrassExecution(commands):
255255
return command
256256

257257
@staticmethod
258-
def executeGrass(commands, progress, outputCommands ):
258+
def executeGrass(commands, progress, outputCommands = None):
259259
loglines = []
260260
loglines.append("GRASS execution console output")
261261
grassOutDone = False

‎python/plugins/sextante/gui/ResultsDialog.py

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,7 @@ class ResultsDialog(QDialog, Ui_DlgResults):
3434
def __init__(self):
3535
QDialog.__init__(self)
3636
self.setupUi(self)
37-
38-
# seems not used and can be removed
39-
#self.groupIcon = QIcon()
40-
#self.groupIcon.addPixmap(self.style().standardPixmap(QStyle.SP_DirClosedIcon),
41-
# QIcon.Normal, QIcon.Off)
42-
#self.groupIcon.addPixmap(self.style().standardPixmap(QStyle.SP_DirOpenIcon),
43-
# QIcon.Normal, QIcon.On)
44-
37+
4538
self.keyIcon = QIcon()
4639
self.keyIcon.addPixmap(self.style().standardPixmap(QStyle.SP_FileIcon))
4740

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

Lines changed: 134 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
* *
1717
***************************************************************************
1818
"""
19-
import webbrowser
20-
2119
__author__ = 'Victor Olaya'
2220
__date__ = 'August 2012'
2321
__copyright__ = '(C) 2012, Victor Olaya'
@@ -31,6 +29,10 @@
3129
from PyQt4.QtCore import *
3230
from PyQt4.QtGui import *
3331

32+
import webbrowser
33+
from sextante.core.GeoAlgorithm import GeoAlgorithm
34+
from sextante.modeler.Providers import Providers
35+
from sextante.core.AlgorithmClassification import AlgorithmDecorator
3436
from sextante.core.Sextante import Sextante
3537
from sextante.core.SextanteLog import SextanteLog
3638
from sextante.core.SextanteConfig import SextanteConfig
@@ -154,6 +156,128 @@ def executeAlgorithm(self):
154156
action.execute()
155157

156158
def fillTree(self):
159+
useCategories = SextanteConfig.getSetting(SextanteConfig.USE_CATEGORIES)
160+
if useCategories:
161+
self.fillTreeUsingCategories()
162+
else:
163+
self.fillTreeUsingProviders()
164+
165+
self.algorithmTree.sortItems(0, Qt.AscendingOrder)
166+
167+
showRecent = SextanteConfig.getSetting(SextanteConfig.SHOW_RECENT_ALGORITHMS)
168+
if showRecent:
169+
recent = SextanteLog.getRecentAlgorithms()
170+
if len(recent) != 0:
171+
found = False
172+
recentItem = QTreeWidgetItem()
173+
recentItem.setText(0, self.tr("Recently used algorithms"))
174+
for algname in recent:
175+
alg = Sextante.getAlgorithm(algname)
176+
if alg is not None:
177+
algItem = TreeAlgorithmItem(alg)
178+
recentItem.addChild(algItem)
179+
found = True
180+
if found:
181+
self.algorithmTree.insertTopLevelItem(0, recentItem)
182+
recentItem.setExpanded(True)
183+
184+
self.algorithmTree.setWordWrap(True)
185+
186+
187+
def fillTreeUsingCategories(self):
188+
providersToExclude = ["model", "script"]
189+
self.algorithmTree.clear()
190+
text = unicode(self.searchBox.text())
191+
groups = {}
192+
for providerName in Sextante.algs.keys():
193+
provider = Sextante.algs[providerName]
194+
name = "ACTIVATE_" + providerName.upper().replace(" ", "_")
195+
if not SextanteConfig.getSetting(name):
196+
continue
197+
if providerName in providersToExclude or len(Providers.providers[providerName].actions) != 0:
198+
continue
199+
algs = provider.values()
200+
#add algorithms
201+
for alg in algs:
202+
if not alg.showInToolbox:
203+
continue
204+
altgroup, altname = AlgorithmDecorator.getGroupAndName(alg)
205+
if text =="" or text.lower() in altname.lower():
206+
if altgroup in groups:
207+
groupItem = groups[altgroup]
208+
else:
209+
groupItem = QTreeWidgetItem()
210+
groupItem.setText(0, altgroup)
211+
groupItem.setToolTip(0, altgroup)
212+
groupItem.setIcon(0, GeoAlgorithm.getDefaultIcon())
213+
groups[altgroup] = groupItem
214+
algItem = TreeAlgorithmItem(alg)
215+
groupItem.addChild(algItem)
216+
217+
if len(groups) > 0:
218+
mainItem = QTreeWidgetItem()
219+
mainItem.setText(0, "Geoalgorithms")
220+
mainItem.setIcon(0, GeoAlgorithm.getDefaultIcon())
221+
mainItem.setToolTip(0, mainItem.text(0))
222+
for groupItem in groups.values():
223+
mainItem.addChild(groupItem)
224+
self.algorithmTree.addTopLevelItem(mainItem)
225+
mainItem.setExpanded(text!="")
226+
for groupItem in groups.values():
227+
if text != "":
228+
groupItem.setExpanded(True)
229+
230+
231+
for providerName in Sextante.algs.keys():
232+
groups = {}
233+
provider = Sextante.algs[providerName]
234+
name = "ACTIVATE_" + providerName.upper().replace(" ", "_")
235+
if not SextanteConfig.getSetting(name):
236+
continue
237+
if providerName not in providersToExclude:
238+
continue
239+
algs = provider.values()
240+
#add algorithms
241+
for alg in algs:
242+
if not alg.showInToolbox:
243+
continue
244+
if text =="" or text.lower() in alg.name.lower():
245+
if alg.group in groups:
246+
groupItem = groups[alg.group]
247+
else:
248+
groupItem = QTreeWidgetItem()
249+
groupItem.setText(0, alg.group)
250+
groupItem.setToolTip(0, alg.group)
251+
groups[alg.group] = groupItem
252+
algItem = TreeAlgorithmItem(alg)
253+
groupItem.addChild(algItem)
254+
255+
actions = Sextante.actions[providerName]
256+
for action in actions:
257+
if text =="" or text.lower() in action.name.lower():
258+
if action.group in groups:
259+
groupItem = groups[action.group]
260+
else:
261+
groupItem = QTreeWidgetItem()
262+
groupItem.setText(0,action.group)
263+
groups[action.group] = groupItem
264+
algItem = TreeActionItem(action)
265+
groupItem.addChild(algItem)
266+
267+
if len(groups) > 0:
268+
providerItem = QTreeWidgetItem()
269+
providerItem.setText(0, Sextante.getProviderFromName(providerName).getDescription())
270+
providerItem.setIcon(0, Sextante.getProviderFromName(providerName).getIcon())
271+
providerItem.setToolTip(0, providerItem.text(0))
272+
for groupItem in groups.values():
273+
providerItem.addChild(groupItem)
274+
self.algorithmTree.addTopLevelItem(providerItem)
275+
providerItem.setExpanded(text!="")
276+
for groupItem in groups.values():
277+
if text != "":
278+
groupItem.setExpanded(True)
279+
280+
def fillTreeUsingProviders(self):
157281
self.algorithmTree.clear()
158282
text = unicode(self.searchBox.text())
159283
for providerName in Sextante.algs.keys():
@@ -204,35 +328,22 @@ def fillTree(self):
204328
if text != "":
205329
groupItem.setExpanded(True)
206330

207-
self.algorithmTree.sortItems(0, Qt.AscendingOrder)
208-
209-
showRecent = SextanteConfig.getSetting(SextanteConfig.SHOW_RECENT_ALGORITHMS)
210-
if showRecent:
211-
recent = SextanteLog.getRecentAlgorithms()
212-
if len(recent) != 0:
213-
found = False
214-
recentItem = QTreeWidgetItem()
215-
recentItem.setText(0, self.tr("Recently used algorithms"))
216-
for algname in recent:
217-
alg = Sextante.getAlgorithm(algname)
218-
if alg is not None:
219-
algItem = TreeAlgorithmItem(alg)
220-
recentItem.addChild(algItem)
221-
found = True
222-
if found:
223-
self.algorithmTree.insertTopLevelItem(0, recentItem)
224-
recentItem.setExpanded(True)
225331

226-
self.algorithmTree.setWordWrap(True)
227332

228333
class TreeAlgorithmItem(QTreeWidgetItem):
229334

230335
def __init__(self, alg):
336+
useCategories = SextanteConfig.getSetting(SextanteConfig.USE_CATEGORIES)
231337
QTreeWidgetItem.__init__(self)
232338
self.alg = alg
233339
self.setText(0, alg.name)
234-
self.setIcon(0, alg.getIcon())
235-
self.setToolTip(0, alg.name)
340+
icon = alg.getIcon()
341+
name = alg.name
342+
if useCategories:
343+
icon = GeoAlgorithm.getDefaultIcon()
344+
group, name = AlgorithmDecorator.getGroupAndName(alg)
345+
self.setIcon(0, icon)
346+
self.setToolTip(0, name)
236347

237348
class TreeActionItem(QTreeWidgetItem):
238349

‎python/plugins/sextante/tests/data/model

Lines changed: 0 additions & 68 deletions
This file was deleted.

‎python/plugins/sextante/tests/test.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
(at your option) any later version.
1111
1212
"""
13+
import sextante
1314

1415
__author__ = 'tim@linfiniti.com'
1516
__version__ = '0.3.0'
@@ -137,7 +138,7 @@ def setUp(self):
137138
print ' => ', self.args, bcolors.WARNING,
138139

139140
def runalg_none(self):
140-
result = Sextante.runalg(self.alg, *self.args)
141+
result = sextante.runalg(self.alg, *self.args)
141142
print bcolors.ENDC
142143
self.assertIsNotNone(result, self.msg)
143144
if not result:

0 commit comments

Comments
 (0)
Please sign in to comment.