2010-06-04 00:31:48 +00:00
# -*- coding: utf-8 -*-
from PyQt4 . QtCore import *
from PyQt4 . QtGui import *
from qgis . core import *
from qgis . gui import *
# to know the os
import platform
import os
from ui_dialogBase import Ui_GdalToolsDialog as Ui_Dialog
import GdalTools_utils as Utils
2011-10-07 01:26:13 +02:00
from . . import resources_rc
2010-06-04 00:31:48 +00:00
import os , platform
class GdalToolsBaseDialog ( QDialog , Ui_Dialog ) :
def __init__ ( self , parent , iface , pluginBase , pluginName , pluginCommand ) :
QDialog . __init__ ( self , parent )
2011-03-14 16:01:10 +00:00
self . setAttribute ( Qt . WA_DeleteOnClose )
2010-06-04 00:31:48 +00:00
self . iface = iface
self . process = QProcess ( self )
2011-12-09 17:29:23 +01:00
Utils . setProcessEnvironment ( self . process )
2010-06-04 00:31:48 +00:00
self . connect ( self . process , SIGNAL ( " error(QProcess::ProcessError) " ) , self . processError )
self . connect ( self . process , SIGNAL ( " finished(int, QProcess::ExitStatus) " ) , self . processFinished )
self . setupUi ( self )
2011-04-15 17:58:00 +00:00
self . arguments = QStringList ( )
2010-06-04 00:31:48 +00:00
2011-10-07 01:26:13 +02:00
self . editCmdBtn . setIcon ( QIcon ( " :/icons/edit.png " ) )
self . connect ( self . editCmdBtn , SIGNAL ( " toggled(bool) " ) , self . editCommand )
self . resetCmdBtn . setIcon ( QIcon ( " :/icons/reset.png " ) )
self . connect ( self . resetCmdBtn , SIGNAL ( " clicked() " ) , self . resetCommand )
self . editCommand ( False )
2010-06-04 00:31:48 +00:00
self . connect ( self . buttonBox , SIGNAL ( " rejected() " ) , self . reject )
self . connect ( self . buttonBox , SIGNAL ( " accepted() " ) , self . accept )
self . connect ( self . buttonBox , SIGNAL ( " helpRequested() " ) , self . help )
self . buttonBox . button ( QDialogButtonBox . Ok ) . setDefault ( True )
self . plugin = pluginBase
self . connect ( self . plugin , SIGNAL ( " valuesChanged(const QStringList &) " ) , self . refreshArgs )
self . pluginLayout . addWidget ( self . plugin )
self . plugin . setFocus ( )
self . setWindowTitle ( pluginName )
2011-04-15 17:58:00 +00:00
self . setPluginCommand ( pluginCommand )
2010-06-04 00:31:48 +00:00
2011-04-15 17:58:00 +00:00
def setPluginCommand ( self , cmd ) :
2010-06-04 00:31:48 +00:00
# on Windows replace the .py with .bat extension
2011-04-15 17:58:00 +00:00
if platform . system ( ) == " Windows " and cmd [ - 3 : ] == " .py " :
self . command = cmd [ : - 3 ] + " .bat "
2010-06-04 00:31:48 +00:00
else :
2011-04-15 17:58:00 +00:00
self . command = cmd
2010-06-04 00:31:48 +00:00
2011-04-15 17:58:00 +00:00
if cmd [ - 3 : ] == " .py " :
self . helpFileName = cmd [ : - 3 ] + " .html "
2010-06-04 00:31:48 +00:00
else :
2011-04-15 17:58:00 +00:00
self . helpFileName = cmd + " .html "
2010-06-04 00:31:48 +00:00
2011-10-07 01:26:13 +02:00
def editCommand ( self , enabled ) :
if not self . commandIsEnabled ( ) :
return
self . editCmdBtn . setChecked ( enabled )
self . resetCmdBtn . setEnabled ( enabled )
self . textEditCommand . setReadOnly ( not enabled )
self . controlsWidget . setEnabled ( not enabled )
self . emit ( SIGNAL ( " refreshArgs() " ) )
def resetCommand ( self ) :
if not self . commandIsEditable ( ) :
return
self . emit ( SIGNAL ( " refreshArgs() " ) )
def commandIsEditable ( self ) :
return self . commandIsEnabled ( ) and self . editCmdBtn . isChecked ( )
def setCommandViewerEnabled ( self , enable ) :
if not enable :
self . editCommand ( False )
self . commandWidget . setEnabled ( enable )
def commandIsEnabled ( self ) :
return self . commandWidget . isEnabled ( )
2010-06-04 00:31:48 +00:00
def reject ( self ) :
2011-03-22 16:45:30 +00:00
if self . process . state ( ) != QProcess . NotRunning :
ret = QMessageBox . warning ( self , self . tr ( " Warning " ) , self . tr ( " The command is still running. \n Do you want terminate it anyway? " ) , QMessageBox . Yes | QMessageBox . No )
if ret == QMessageBox . No :
return
self . disconnect ( self . process , SIGNAL ( " error(QProcess::ProcessError) " ) , self . processError )
self . disconnect ( self . process , SIGNAL ( " finished(int, QProcess::ExitStatus) " ) , self . processFinished )
2010-06-04 00:31:48 +00:00
self . emit ( SIGNAL ( " closeClicked() " ) )
def accept ( self ) :
self . emit ( SIGNAL ( " okClicked() " ) )
def help ( self ) :
self . emit ( SIGNAL ( " helpClicked() " ) )
def processError ( self , error ) :
self . emit ( SIGNAL ( " processError(QProcess::ProcessError) " ) , error )
def processFinished ( self , exitCode , status ) :
self . emit ( SIGNAL ( " processFinished(int, QProcess::ExitStatus) " ) , exitCode , status )
# show the online tool documentation in the default browser
def onHelp ( self ) :
2010-11-11 15:33:29 +00:00
helpPath = Utils . getHelpPath ( )
if helpPath . isEmpty ( ) :
url = QUrl ( " http://www.gdal.org/ " + self . helpFileName )
else :
url = QUrl . fromLocalFile ( helpPath + ' / ' + self . helpFileName )
2010-06-04 00:31:48 +00:00
QDesktopServices . openUrl ( url )
# called when a value in the plugin widget interface changed
def refreshArgs ( self , args ) :
self . arguments = args
2011-10-07 01:26:13 +02:00
if not self . commandIsEnabled ( ) :
self . textEditCommand . setPlainText ( self . command )
2010-06-04 00:31:48 +00:00
else :
2011-10-07 01:26:13 +02:00
self . textEditCommand . setPlainText ( self . command + " " + Utils . escapeAndJoin ( self . arguments ) )
2010-06-04 00:31:48 +00:00
# enables the OK button
def enableRun ( self , enable = True ) :
self . buttonBox . button ( QDialogButtonBox . Ok ) . setEnabled ( enable )
# start the command execution
def onRun ( self ) :
self . enableRun ( False )
self . setCursor ( Qt . WaitCursor )
2011-10-07 01:26:13 +02:00
if not self . commandIsEditable ( ) :
self . process . start ( self . command , self . arguments , QIODevice . ReadOnly )
else :
self . process . start ( self . textEditCommand . toPlainText ( ) , QIODevice . ReadOnly )
2010-06-04 00:31:48 +00:00
# stop the command execution
def stop ( self ) :
self . enableRun ( True )
self . setCursor ( Qt . ArrowCursor )
self . process . kill ( )
# called on closing the dialog, stop the process if it's running
def onClosing ( self ) :
self . stop ( )
QDialog . reject ( self )
# called if an error occurs when the command has not already finished, shows the occurred error message
def onError ( self , error ) :
if error == QProcess . FailedToStart :
msg = QCoreApplication . translate ( " GdalTools " , " The process failed to start. Either the invoked program is missing, or you may have insufficient permissions to invoke the program. " )
elif error == QProcess . Crashed :
msg = QCoreApplication . translate ( " GdalTools " , " The process crashed some time after starting successfully. " )
else :
msg = QCoreApplication . translate ( " GdalTools " , " An unknown error occurred. " )
QErrorMessage ( self ) . showMessage ( msg )
QApplication . processEvents ( ) # give the user chance to see the message
self . stop ( )
# called when the command finished its execution, shows an error message if there's one
# and, if required, load the output file in canvas
def onFinished ( self , exitCode , status ) :
if status == QProcess . CrashExit :
self . stop ( )
return
if self . command . find ( " gdalinfo " ) != - 1 and exitCode == 0 :
print " *** DEBUG *** "
self . emit ( SIGNAL ( " finished(bool) " ) , self . loadCheckBox . isChecked ( ) )
self . stop ( )
return
# show the error message if there's one, otherwise show the process output message
msg = QString ( self . process . readAllStandardError ( ) )
if msg . isEmpty ( ) :
outMessages = QString ( self . process . readAllStandardOutput ( ) ) . split ( " \n " )
# make sure to not show the help
for m in outMessages :
m = m . trimmed ( )
if m . isEmpty ( ) :
continue
if m . contains ( QRegExp ( " ^(?:[Uu]sage: \\ s)? " + QRegExp . escape ( self . command ) + " \\ s " ) ) :
if msg . isEmpty ( ) :
msg = self . tr ( " Invalid parameters. " )
break
if m . contains ( QRegExp ( " 0(?: \\ .+[1-9]0 { 1,2})+ " ) ) :
continue
if not msg . isEmpty ( ) :
msg + = " \n "
msg + = m
QErrorMessage ( self ) . showMessage ( msg . replace ( " \n " , " <br> " ) )
if exitCode == 0 :
self . emit ( SIGNAL ( " finished(bool) " ) , self . loadCheckBox . isChecked ( ) )
self . stop ( )