2010-06-04 00:31:48 +00:00
# -*- coding: utf-8 -*-
2012-10-06 13:10:25 +02:00
"""
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
doRgbPct . py
- - - - - - - - - - - - - - - - - - - - -
Date : June 2010
Copyright : ( C ) 2010 by Giuseppe Sucameli
Email : brush dot tyler at gmail dot com
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* *
* This program is free software ; you can redistribute it and / or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation ; either version 2 of the License , or *
* ( at your option ) any later version . *
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
"""
__author__ = ' Giuseppe Sucameli '
__date__ = ' June 2010 '
__copyright__ = ' (C) 2010, Giuseppe Sucameli '
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = ' $Format: % H$ '
2010-06-04 00:31:48 +00:00
from PyQt4 . QtCore import *
from PyQt4 . QtGui import *
from qgis . core import *
from qgis . gui import *
from ui_widgetConvert import Ui_GdalToolsWidget as Ui_Widget
from widgetBatchBase import GdalToolsBaseBatchWidget as BaseBatchWidget
import GdalTools_utils as Utils
class GdalToolsDialog ( QWidget , Ui_Widget , BaseBatchWidget ) :
def __init__ ( self , iface ) :
QWidget . __init__ ( self )
self . iface = iface
self . setupUi ( self )
BaseBatchWidget . __init__ ( self , self . iface , " rgb2pct.py " )
2011-04-15 18:30:10 +00:00
self . outSelector . setType ( self . outSelector . FILE )
2010-06-04 00:31:48 +00:00
# set the default QSpinBoxes and QProgressBar value
self . colorsSpin . setValue ( 2 )
self . progressBar . setValue ( 0 )
self . progressBar . hide ( )
self . outputFormat = Utils . fillRasterOutputFormat ( )
self . setParamsStatus (
[
2011-04-15 18:30:10 +00:00
( self . inSelector , SIGNAL ( " filenameChanged() " ) ) ,
( self . outSelector , SIGNAL ( " filenameChanged() " ) ) ,
2010-06-04 00:31:48 +00:00
( self . colorsSpin , SIGNAL ( " valueChanged(int) " ) , self . colorsCheck ) ,
2011-04-15 18:30:10 +00:00
( self . bandSpin , SIGNAL ( " valueChanged(int) " ) , self . bandCheck , " -1 " ) # hide this option
2010-06-04 00:31:48 +00:00
]
)
2011-04-15 18:30:10 +00:00
self . connect ( self . inSelector , SIGNAL ( " selectClicked() " ) , self . fillInputFile )
self . connect ( self . outSelector , SIGNAL ( " selectClicked() " ) , self . fillOutputFileEdit )
2010-06-04 00:31:48 +00:00
self . connect ( self . batchCheck , SIGNAL ( " stateChanged( int ) " ) , self . switchToolMode )
# switch to batch or normal mode
def switchToolMode ( self ) :
self . setCommandViewerEnabled ( not self . batchCheck . isChecked ( ) )
2011-04-15 18:30:10 +00:00
self . progressBar . setVisible ( self . batchCheck . isChecked ( ) )
2010-06-04 00:31:48 +00:00
2011-04-15 18:30:10 +00:00
self . inSelector . setType ( self . inSelector . FILE if self . batchCheck . isChecked ( ) else self . inSelector . FILE_LAYER )
self . outSelector . clear ( )
2010-06-04 00:31:48 +00:00
if self . batchCheck . isChecked ( ) :
self . inFileLabel = self . label . text ( )
self . outFileLabel = self . label_2 . text ( )
2010-12-22 03:25:14 +00:00
self . label . setText ( QCoreApplication . translate ( " GdalTools " , " &Input directory " ) )
self . label_2 . setText ( QCoreApplication . translate ( " GdalTools " , " &Output directory " ) )
2010-06-04 00:31:48 +00:00
2011-04-15 18:30:10 +00:00
QObject . disconnect ( self . inSelector , SIGNAL ( " selectClicked() " ) , self . fillInputFile )
QObject . disconnect ( self . outSelector , SIGNAL ( " selectClicked() " ) , self . fillOutputFileEdit )
2010-06-04 00:31:48 +00:00
2011-04-15 18:30:10 +00:00
QObject . connect ( self . inSelector , SIGNAL ( " selectClicked() " ) , self . fillInputDir )
QObject . connect ( self . outSelector , SIGNAL ( " selectClicked() " ) , self . fillOutputDir )
2010-06-04 00:31:48 +00:00
else :
self . label . setText ( self . inFileLabel )
self . label_2 . setText ( self . outFileLabel )
2011-04-15 18:30:10 +00:00
QObject . disconnect ( self . inSelector , SIGNAL ( " selectClicked() " ) , self . fillInputDir )
QObject . disconnect ( self . outSelector , SIGNAL ( " selectClicked() " ) , self . fillOutputDir )
2010-06-04 00:31:48 +00:00
2011-04-15 18:30:10 +00:00
QObject . connect ( self . inSelector , SIGNAL ( " selectClicked() " ) , self . fillInputFile )
QObject . connect ( self . outSelector , SIGNAL ( " selectClicked() " ) , self . fillOutputFileEdit )
2010-06-04 00:31:48 +00:00
2011-03-14 16:01:10 +00:00
def onLayersChanged ( self ) :
2011-04-15 18:30:10 +00:00
self . inSelector . setLayers ( Utils . LayerRegistry . instance ( ) . getRasterLayers ( ) )
2010-06-04 00:31:48 +00:00
def fillInputFile ( self ) :
lastUsedFilter = Utils . FileFilter . lastUsedRasterFilter ( )
inputFile = Utils . FileDialog . getOpenFileName ( self , self . tr ( " Select the input file for convert " ) , Utils . FileFilter . allRastersFilter ( ) , lastUsedFilter )
2013-06-17 15:27:54 +02:00
if not inputFile :
2010-06-04 00:31:48 +00:00
return
Utils . FileFilter . setLastUsedRasterFilter ( lastUsedFilter )
2011-04-15 18:30:10 +00:00
self . inSelector . setFilename ( inputFile )
2010-06-04 00:31:48 +00:00
def fillOutputFileEdit ( self ) :
lastUsedFilter = Utils . FileFilter . lastUsedRasterFilter ( )
2014-10-29 23:54:44 +01:00
outputFile = Utils . FileDialog . getSaveFileName ( self , self . tr ( " Select the raster file to save the results to " ) , Utils . FileFilter . saveRastersFilter ( ) , lastUsedFilter )
2013-06-17 15:27:54 +02:00
if not outputFile :
2010-06-04 00:31:48 +00:00
return
Utils . FileFilter . setLastUsedRasterFilter ( lastUsedFilter )
self . outputFormat = Utils . fillRasterOutputFormat ( lastUsedFilter , outputFile )
2011-04-15 18:30:10 +00:00
self . outSelector . setFilename ( outputFile )
2010-06-04 00:31:48 +00:00
def fillInputDir ( self ) :
2011-04-15 18:30:10 +00:00
inputDir = Utils . FileDialog . getExistingDirectory ( self , self . tr ( " Select the input directory with files for convert " ) )
2013-06-17 15:27:54 +02:00
if not inputDir :
2010-06-04 00:31:48 +00:00
return
2011-04-15 18:30:10 +00:00
self . inSelector . setFilename ( inputDir )
2010-06-04 00:31:48 +00:00
def fillOutputDir ( self ) :
outputDir = Utils . FileDialog . getExistingDirectory ( self , self . tr ( " Select the output directory to save the results to " ) )
2013-06-17 15:27:54 +02:00
if not outputDir :
2010-06-04 00:31:48 +00:00
return
2011-04-15 18:30:10 +00:00
self . outSelector . setFilename ( outputDir )
2010-06-04 00:31:48 +00:00
def getArguments ( self ) :
2013-06-17 15:27:54 +02:00
arguments = [ ]
2010-06-04 00:31:48 +00:00
if self . colorsCheck . isChecked ( ) :
2013-06-17 15:27:54 +02:00
arguments . append ( " -n " )
arguments . append ( str ( self . colorsSpin . value ( ) ) )
2010-06-04 00:31:48 +00:00
if self . isBatchEnabled ( ) :
return arguments
2011-04-15 18:30:10 +00:00
outputFn = self . getOutputFileName ( )
2013-06-17 15:27:54 +02:00
if outputFn :
arguments . append ( " -of " )
arguments . append ( self . outputFormat )
arguments . append ( self . getInputFileName ( ) )
arguments . append ( outputFn )
2010-06-04 00:31:48 +00:00
return arguments
def getInputFileName ( self ) :
2011-04-15 18:30:10 +00:00
return self . inSelector . filename ( )
2010-06-04 00:31:48 +00:00
def getOutputFileName ( self ) :
2011-04-15 18:30:10 +00:00
return self . outSelector . filename ( )
2010-06-04 00:31:48 +00:00
def addLayerIntoCanvas ( self , fileInfo ) :
self . iface . addRasterLayer ( fileInfo . filePath ( ) )
def isBatchEnabled ( self ) :
return self . batchCheck . isChecked ( )
def setProgressRange ( self , maximum ) :
self . progressBar . setRange ( 0 , maximum )
def updateProgress ( self , index , total ) :
if index < total :
self . progressBar . setValue ( index + 1 )
else :
self . progressBar . setValue ( 0 )
2011-04-15 18:30:10 +00:00