Skip to content

Commit

Permalink
allow to use loaded layers as input files (instead of file selector) …
Browse files Browse the repository at this point in the history
…for GDALTools Build Virtual Raster
  • Loading branch information
etiennesky committed Mar 9, 2012
1 parent a873124 commit 3c1eb0a
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 14 deletions.
25 changes: 22 additions & 3 deletions python/plugins/GdalTools/tools/doBuildVRT.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from PyQt4.QtGui import *
from qgis.core import *
from qgis.gui import *
from qgis.utils import *

This comment has been minimized.

Copy link
@brushtyler

brushtyler Mar 9, 2012

Contributor

this line is not required: iface is available yet through self.iface (line 15)


from ui_widgetBuildVRT import Ui_GdalToolsWidget as Ui_Widget
from widgetPluginBase import GdalToolsBasePluginWidget as BasePluginWidget
Expand Down Expand Up @@ -38,6 +39,7 @@ def __init__(self, iface):
self.connect(self.inSelector, SIGNAL("selectClicked()"), self.fillInputFilesEdit)
self.connect(self.outSelector, SIGNAL("selectClicked()"), self.fillOutputFileEdit)
self.connect( self.inputDirCheck, SIGNAL( "stateChanged( int )" ), self.switchToolMode )
self.connect( self.useSelectedLayersCheck, SIGNAL( "stateChanged( int )" ), self.switchLayerMode )

def switchToolMode(self):
self.recurseCheck.setVisible( self.inputDirCheck.isChecked() )
Expand All @@ -55,6 +57,12 @@ def switchToolMode(self):
QObject.connect(self.inSelector, SIGNAL("selectClicked()"), self.fillInputFilesEdit)
QObject.disconnect(self.inSelector, SIGNAL("selectClicked()"), self.fillInputDir)

def switchLayerMode(self):
enableInputFiles = not self.useSelectedLayersCheck.isChecked()
self.inputDirCheck.setEnabled( enableInputFiles )
self.inSelector.setEnabled( enableInputFiles )
self.recurseCheck.setEnabled( enableInputFiles )

def fillInputFilesEdit(self):
lastUsedFilter = Utils.FileFilter.lastUsedRasterFilter()
files = Utils.FileDialog.getOpenFileNames(self, self.tr( "Select the files for VRT" ), Utils.FileFilter.allRastersFilter(), lastUsedFilter)
Expand Down Expand Up @@ -88,10 +96,13 @@ def getArguments(self):
if self.allowProjDiffCheck.isChecked():
arguments << "-allow_projection_difference"
arguments << self.getOutputFileName()
if self.inputDirCheck.isChecked():
arguments << Utils.getRasterFiles( self.getInputFileName(), self.recurseCheck.isChecked() )
if self.useSelectedLayersCheck.isChecked():
arguments << self.getInputFileNamesFromSelectedLayers()
else:
arguments << self.getInputFileName()
if self.inputDirCheck.isChecked():
arguments << Utils.getRasterFiles( self.getInputFileName(), self.recurseCheck.isChecked() )
else:
arguments << self.getInputFileName()
return arguments

def getOutputFileName(self):
Expand All @@ -102,6 +113,14 @@ def getInputFileName(self):
return self.inSelector.filename()
return self.inSelector.filename().split(",")

def getInputFileNamesFromSelectedLayers(self):

This comment has been minimized.

Copy link
@brushtyler

brushtyler Mar 9, 2012

Contributor

all the the code in this function could be replaced by using the LayerRegistry class within the GdalTools_utils module (look at line 9 for its import) which takes care to retrieve the list of supported layers:
return [ layer.source() for layer in Utils.LayerRegistry.instance().getRasterLayers() ]

This comment has been minimized.

Copy link
@brushtyler

brushtyler Mar 9, 2012

Contributor

BTW if selected layers changes on the legend the set of selected layers is not refreshed untile the user change something in the tool UI (e.g. check a box).
This sometimes could be a problem (i.e. layers referenced in python but deleted in QGis), so would preferable to override the onLayersChanged() method (defined in BasePluginWidget within the widgetPluginBase module) which was created just for this purpose.

This comment has been minimized.

Copy link
@etiennesky

etiennesky Mar 9, 2012

Author Contributor

I thought that when calling layers = iface.mapCanvas().layers() you get the current set of selected layers . I guess you mean that the text in the dialog should be updated dynamically when layers change?

layers = iface.mapCanvas().layers()
files = list()
for layer in layers:
if layer.type() == QgsMapLayer.RasterLayer and layer.providerType() == "gdal":
files.append( str(layer.source()) )
return files

def addLayerIntoCanvas(self, fileInfo):
self.iface.addRasterLayer(fileInfo.filePath())

29 changes: 18 additions & 11 deletions python/plugins/GdalTools/tools/widgetBuildVRT.ui
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,20 @@
<enum>QLayout::SetNoConstraint</enum>
</property>
<item row="0" column="0" colspan="2">
<widget class="QCheckBox" name="useSelectedLayersCheck">
<property name="text">
<string>Use selected layers for input</string>
</property>
</widget>
</item>
<item row="1" column="0" colspan="2">
<widget class="QCheckBox" name="inputDirCheck">
<property name="text">
<string>Choose input directory instead of files</string>
</property>
</widget>
</item>
<item row="1" column="0">
<item row="2" column="0">
<widget class="QLabel" name="label">
<property name="text">
<string>&amp;Input files</string>
Expand All @@ -42,14 +49,14 @@
</property>
</widget>
</item>
<item row="2" column="1">
<item row="3" column="1">
<widget class="QCheckBox" name="recurseCheck">
<property name="text">
<string>Recurse subdirectories</string>
</property>
</widget>
</item>
<item row="3" column="0">
<item row="4" column="0">
<widget class="QLabel" name="label_2">
<property name="text">
<string>&amp;Output file</string>
Expand All @@ -59,14 +66,14 @@
</property>
</widget>
</item>
<item row="4" column="0">
<item row="5" column="0">
<widget class="QCheckBox" name="resolutionCheck">
<property name="text">
<string>&amp;Resolution</string>
</property>
</widget>
</item>
<item row="4" column="1">
<item row="5" column="1">
<widget class="QComboBox" name="resolutionComboBox">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Fixed">
Expand Down Expand Up @@ -94,14 +101,14 @@
</item>
</widget>
</item>
<item row="5" column="0">
<item row="6" column="0">
<widget class="QCheckBox" name="srcNoDataCheck">
<property name="text">
<string>&amp;Source No Data</string>
</property>
</widget>
</item>
<item row="5" column="1">
<item row="6" column="1">
<widget class="QSpinBox" name="srcNoDataSpin">
<property name="minimum">
<number>-100000</number>
Expand All @@ -111,20 +118,20 @@
</property>
</widget>
</item>
<item row="6" column="0">
<item row="7" column="0">
<widget class="QCheckBox" name="separateCheck">
<property name="text">
<string>Se&amp;parate</string>
</property>
</widget>
</item>
<item row="1" column="1">
<item row="2" column="1">
<widget class="GdalToolsInOutSelector" name="inSelector" native="true"/>
</item>
<item row="3" column="1">
<item row="4" column="1">
<widget class="GdalToolsInOutSelector" name="outSelector" native="true"/>
</item>
<item row="7" column="0" colspan="2">
<item row="8" column="0" colspan="2">
<widget class="QCheckBox" name="allowProjDiffCheck">
<property name="text">
<string>Allow projection difference</string>
Expand Down

2 comments on commit 3c1eb0a

@brushtyler
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ethienne, thanks for your patch! I'm going to apply it making the changes above.

@etiennesky
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! will look into the changes you make.

Please sign in to comment.