From 6e5c780a8b57993834671ea0e89a40194760f5d6 Mon Sep 17 00:00:00 2001 From: rblazek Date: Wed, 15 Feb 2006 17:25:12 +0000 Subject: [PATCH] check if output exists git-svn-id: http://svn.osgeo.org/qgis/trunk@4865 c8812cc2-4d05-0410-92ff-de0c093fc19c --- src/plugins/grass/qgsgrassmodule.cpp | 90 +++++++++++++++++++++++++++- src/plugins/grass/qgsgrassmodule.h | 21 ++++++- 2 files changed, 109 insertions(+), 2 deletions(-) diff --git a/src/plugins/grass/qgsgrassmodule.cpp b/src/plugins/grass/qgsgrassmodule.cpp index 7632af161c66..10017cc50bca 100644 --- a/src/plugins/grass/qgsgrassmodule.cpp +++ b/src/plugins/grass/qgsgrassmodule.cpp @@ -611,9 +611,25 @@ void QgsGrassModule::run() if ( mProcess.isRunning() ) { } + mProcess.clearArguments(); mProcess.addArgument( mXName ); command = mXName; + + // Check if output exists + QStringList outputExists = mOptions->checkOutput(); + if ( outputExists.size() > 0 ) + { + int ret = QMessageBox::question ( 0, "Warning", + "Output " + outputExists.join(",") + + " exists! Overwrite?", + QMessageBox::Yes, QMessageBox::No ); + + if ( ret == QMessageBox::No ) return; + + mProcess.addArgument( "--o" ); + command.append ( " --o" ); + } QStringList list = mOptions->arguments(); @@ -764,7 +780,8 @@ QgsGrassModuleOption::QgsGrassModuleOption ( QgsGrassModule *module, QString key QDomElement &qdesc, QDomElement &gdesc, QDomNode &gnode, QWidget * parent) : Q3GroupBox ( 1, Qt::Vertical, parent ), - QgsGrassModuleItem ( module, key, qdesc, gdesc, gnode ) + QgsGrassModuleItem ( module, key, qdesc, gdesc, gnode ), + mIsOutput(false) { #ifdef QGISDEBUG std::cerr << "QgsGrassModuleOption::QgsGrassModuleOption" << std::endl; @@ -781,6 +798,19 @@ QgsGrassModuleOption::QgsGrassModuleOption ( QgsGrassModule *module, QString key } setTitle ( " " + tit + " " ); + + // Is it output? + QDomNode promptNode = gnode.namedItem ( "gisprompt" ); + if ( !promptNode.isNull() ) { + QDomElement promptElem = promptNode.toElement(); + QString element = promptElem.attribute("element"); + QString age = promptElem.attribute("age"); + if ( age == "new" ) + { + mOutputElement = element; + mIsOutput = true; + } + } // String without options if ( !mHidden ) @@ -859,6 +889,34 @@ QgsGrassModuleOption::QgsGrassModuleOption ( QgsGrassModule *module, QString key } } +QString QgsGrassModuleOption::outputExists() +{ + std::cerr << "QgsGrassModuleOption::outputExists()" << std::endl; + + if ( !mIsOutput ) return QString(); + + QString value = mLineEdit->text().trimmed(); + std::cerr << "mKey = " << mKey.ascii() << std::endl; + std::cerr << "value = " << value.ascii() << std::endl; + std::cerr << "mOutputElement = " << mOutputElement.ascii() << std::endl; + + if ( value.length() == 0 ) return QString(); + + QString path = QgsGrass::getDefaultGisdbase() + "/" + + QgsGrass::getDefaultLocation() + "/" + + QgsGrass::getDefaultMapset() + "/" + + mOutputElement + "/" + value; + + QFileInfo fi(path); + + if ( fi.exists() ) + { + return (mLineEdit->text()); + } + + return QString(); +} + QStringList QgsGrassModuleOption::options() { QStringList list; @@ -1088,6 +1146,36 @@ QgsGrassModuleInput::QgsGrassModuleInput ( QgsGrassModule *module, updateQgisLayers(); } +QStringList QgsGrassModuleStandardOptions::checkOutput() +{ + #ifdef QGISDEBUG + std::cerr << "QgsGrassModuleStandardOptions::checkOutput()" << std::endl; + #endif + QStringList list; + + for ( int i = 0; i < mItems.size(); i++ ) + { + if ( typeid(*(mItems[i])) != typeid (QgsGrassModuleOption) ) { + continue; + } + QgsGrassModuleOption *opt = + dynamic_cast ( mItems[i] ); + + std::cerr << "opt->key() = " << opt->key().ascii() << std::endl; + + if ( opt->isOutput() ) + { + QString out = opt->outputExists(); + if ( !out.isNull() ) + { + list.append ( opt->key()+ ":" + out ); + } + } + } + + return list; +} + void QgsGrassModuleInput::updateQgisLayers() { #ifdef QGISDEBUG diff --git a/src/plugins/grass/qgsgrassmodule.h b/src/plugins/grass/qgsgrassmodule.h index 3a2e04ff2991..041d4eb8f508 100644 --- a/src/plugins/grass/qgsgrassmodule.h +++ b/src/plugins/grass/qgsgrassmodule.h @@ -163,6 +163,11 @@ class QgsGrassModuleOptions //! Get module options as list of arguments for QProcess virtual QStringList arguments(); + //! Check if output exists + // return empty list + // return list of existing output maps + virtual QStringList checkOutput() { return QStringList() ; } + protected: //! QGIS application QgisApp *mQgisApp; @@ -209,6 +214,8 @@ class QgsGrassModuleStandardOptions: public QgsGrassModuleOptions, QWidget // ! Get item by ID QgsGrassModuleItem *item(QString id); + QStringList checkOutput(); + private: //! Name of module executable QString mXName; @@ -299,7 +306,13 @@ class QgsGrassModuleOption: public Q3GroupBox, public QgsGrassModuleItem //! Retruns list of options which will be passed to module virtual QStringList options(); - + + //! True if this option is output + bool isOutput() { return mIsOutput; } + + //! If output, check if current output exists + // Returns emppty string or name of existing output + QString outputExists(); private: //! Control type @@ -316,6 +329,12 @@ class QgsGrassModuleOption: public Q3GroupBox, public QgsGrassModuleItem //! Line QLineEdit *mLineEdit; + + //! True if this option is GRASS output + bool mIsOutput; + + //! Output element + QString mOutputElement; }; /********************** QgsGrassModuleFlag ************************/ /*! \class QgsGrassModuleFlag