Skip to content

Commit 7276ac6

Browse files
author
rblazek
committed
view output button
git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@5020 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent 6cbc8df commit 7276ac6

File tree

3 files changed

+206
-44
lines changed

3 files changed

+206
-44
lines changed

src/plugins/grass/qgsgrassmodule.cpp

+139-29
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
#include "qgsmapcanvas.h"
7474
#include "qgsmaplayer.h"
7575
#include "qgsvectorlayer.h"
76+
#include <qgsrasterlayer.h>
7677
#include "qgsdataprovider.h"
7778
#include "qgsfield.h"
7879
#include "qgsfeature.h"
@@ -89,6 +90,7 @@ extern "C" {
8990
#include "qgsgrassmodule.h"
9091
#include "qgsgrassmapcalc.h"
9192
#include "qgsgrasstools.h"
93+
#include "qgsgrassselect.h"
9294

9395
bool QgsGrassModule::mExecPathInited = 0;
9496
QStringList QgsGrassModule::mExecPath;
@@ -124,9 +126,7 @@ bool QgsGrassModule::inExecPath ( QString file )
124126

125127
QgsGrassModule::QgsGrassModule ( QgsGrassTools *tools, QgisApp *qgisApp, QgisIface *iface,
126128
QString path, QWidget * parent, const char * name, Qt::WFlags f )
127-
//:QgsGrassModuleBase ( parent, name, f )
128-
//tim removed params during qt4 ui port - FIXME
129-
:QgsGrassModuleBase ( )
129+
:QgsGrassModuleBase ( ), mSuccess(false)
130130
{
131131
#ifdef QGISDEBUG
132132
std::cerr << "QgsGrassModule()" << std::endl;
@@ -214,6 +214,14 @@ QgsGrassModule::QgsGrassModule ( QgsGrassTools *tools, QgisApp *qgisApp, QgisIfa
214214
mOptions = new QgsGrassModuleStandardOptions ( mTools, this,
215215
mQgisApp, mIface, mXName, qDocElem, mTabWidget->page(0) );
216216
}
217+
218+
// Hide display if there is no output
219+
if ( mOptions->output(QgsGrassModuleOption::Vector).size() == 0
220+
&& mOptions->output(QgsGrassModuleOption::Raster).size() == 0 )
221+
{
222+
mViewButton->hide();
223+
}
224+
mViewButton->setEnabled(false);
217225

218226
// Create manual if available
219227
QString gisBase = getenv("GISBASE");
@@ -477,6 +485,36 @@ QStringList QgsGrassModuleStandardOptions::checkOutput()
477485
return list;
478486
}
479487

488+
QStringList QgsGrassModuleStandardOptions::output(int type )
489+
{
490+
#ifdef QGISDEBUG
491+
std::cerr << "QgsGrassModuleStandardOptions::output()" << std::endl;
492+
#endif
493+
QStringList list;
494+
495+
for ( int i = 0; i < mItems.size(); i++ )
496+
{
497+
if ( typeid(*(mItems[i])) != typeid (QgsGrassModuleOption) ) {
498+
continue;
499+
}
500+
QgsGrassModuleOption *opt =
501+
dynamic_cast<QgsGrassModuleOption *> ( mItems[i] );
502+
503+
std::cerr << "opt->key() = " << opt->key().ascii() << std::endl;
504+
505+
if ( opt->isOutput() )
506+
{
507+
if ( opt->outputType() == type )
508+
{
509+
QString out = opt->value();
510+
list.append ( out );
511+
}
512+
}
513+
}
514+
515+
return list;
516+
}
517+
480518
QStringList QgsGrassModuleStandardOptions::ready()
481519
{
482520
#ifdef QGISDEBUG
@@ -700,6 +738,14 @@ void QgsGrassModule::run()
700738
command.append ( " --o" );
701739
}
702740
}
741+
742+
// Remember output maps
743+
mOutputVector = mOptions->output(QgsGrassModuleOption::Vector);
744+
std::cerr << "mOutputVector.size() = " << mOutputVector.size() << std::endl;
745+
mOutputRaster = mOptions->output(QgsGrassModuleOption::Raster);
746+
std::cerr << "mOutputRaster.size() = " << mOutputRaster.size() << std::endl;
747+
mSuccess = false;
748+
mViewButton->setEnabled(false);
703749

704750
QStringList list = mOptions->arguments();
705751

@@ -742,6 +788,8 @@ void QgsGrassModule::finished()
742788
if ( mProcess.exitStatus () == 0 ) {
743789
mOutputTextBrowser->append( "<B>Successfully finished</B>" );
744790
mProgressBar->setProgress ( 100, 100 );
791+
mSuccess = true;
792+
mViewButton->setEnabled(true);
745793
} else {
746794
mOutputTextBrowser->append( "<B>Finished with error</B>" );
747795
}
@@ -814,6 +862,50 @@ void QgsGrassModule::close()
814862
delete this;
815863
}
816864

865+
void QgsGrassModule::viewOutput()
866+
{
867+
#ifdef QGISDEBUG
868+
std::cerr << "QgsGrassModule::viewOutput()" << std::endl;
869+
#endif
870+
871+
if ( !mSuccess ) return;
872+
873+
for (int i = 0; i < mOutputVector.size(); i++ )
874+
{
875+
QString map = mOutputVector.at(i);
876+
877+
QStringList layers = QgsGrassSelect::vectorLayers (
878+
QgsGrass::getDefaultGisdbase(),
879+
QgsGrass::getDefaultLocation(),
880+
QgsGrass::getDefaultMapset(), map );
881+
882+
// TODO common method for add all layers
883+
for ( int j = 0; j < layers.count(); j++ )
884+
{
885+
QString uri = QgsGrass::getDefaultGisdbase() + "/"
886+
+ QgsGrass::getDefaultLocation() + "/"
887+
+ QgsGrass::getDefaultMapset() + "/"
888+
+ map + "/" + layers[i];
889+
890+
// TODO vector layer name
891+
mIface->addVectorLayer( uri, layers[i], "grass");
892+
}
893+
}
894+
895+
for (int i = 0; i < mOutputRaster.size(); i++ )
896+
{
897+
QString map = mOutputRaster.at(i);
898+
899+
QString uri = QgsGrass::getDefaultGisdbase() + "/"
900+
+ QgsGrass::getDefaultLocation() + "/"
901+
+ QgsGrass::getDefaultMapset()
902+
+ "/cellhd/" + map;
903+
904+
QgsRasterLayer *layer = new QgsRasterLayer( uri, map );
905+
mIface->addRasterLayer(layer);
906+
}
907+
}
908+
817909
QgisIface *QgsGrassModule::qgisIface() { return mIface; }
818910
QgisApp *QgsGrassModule::qgisApp() { return mQgisApp; }
819911

@@ -854,7 +946,7 @@ QgsGrassModuleOption::QgsGrassModuleOption ( QgsGrassModule *module, QString key
854946
QWidget * parent)
855947
: QGroupBox ( parent ),
856948
QgsGrassModuleItem ( module, key, qdesc, gdesc, gnode ),
857-
mIsOutput(false), mValueType(String), mHaveLimits(false)
949+
mIsOutput(false), mValueType(String), mHaveLimits(false), mOutputType(None)
858950
{
859951
#ifdef QGISDEBUG
860952
std::cerr << "QgsGrassModuleOption::QgsGrassModuleOption" << std::endl;
@@ -880,10 +972,20 @@ QgsGrassModuleOption::QgsGrassModuleOption ( QgsGrassModule *module, QString key
880972
QDomElement promptElem = promptNode.toElement();
881973
QString element = promptElem.attribute("element");
882974
QString age = promptElem.attribute("age");
975+
883976
if ( age == "new" )
884977
{
885978
mOutputElement = element;
886979
mIsOutput = true;
980+
981+
if ( element == "vector" )
982+
{
983+
mOutputType = Vector;
984+
}
985+
else if ( element == "cell" )
986+
{
987+
mOutputType = Raster;
988+
}
887989
}
888990
}
889991

@@ -1086,38 +1188,46 @@ QString QgsGrassModuleOption::outputExists()
10861188
return QString();
10871189
}
10881190

1191+
QString QgsGrassModuleOption::value()
1192+
{
1193+
QString value;
1194+
1195+
if ( mControlType == LineEdit ) {
1196+
for ( int i = 0; i < mLineEdits.size(); i++ )
1197+
{
1198+
QLineEdit *lineEdit = mLineEdits.at(i);
1199+
if( lineEdit->text().trimmed().length() > 0 )
1200+
{
1201+
if ( value.length() > 0 ) value.append (",");
1202+
value.append ( lineEdit->text().trimmed() );
1203+
}
1204+
}
1205+
}
1206+
else if ( mControlType == ComboBox )
1207+
{
1208+
value = mValues[mComboBox->currentItem()];
1209+
}
1210+
else if ( mControlType == CheckBoxes )
1211+
{
1212+
int cnt = 0;
1213+
for ( int i = 0; i < mCheckBoxes.size(); i++ ) {
1214+
if ( mCheckBoxes[i]->isChecked() ) {
1215+
if ( cnt > 0 ) value.append ( "," );
1216+
value.append ( mValues[i] );
1217+
}
1218+
}
1219+
}
1220+
return value;
1221+
}
1222+
10891223
QStringList QgsGrassModuleOption::options()
10901224
{
10911225
QStringList list;
10921226

10931227
if ( mHidden ) {
10941228
list.push_back( mKey + "=" + mAnswer );
10951229
} else {
1096-
if ( mControlType == LineEdit ) {
1097-
QString vals;
1098-
for ( int i = 0; i < mLineEdits.size(); i++ )
1099-
{
1100-
QLineEdit *lineEdit = mLineEdits.at(i);
1101-
if( lineEdit->text().trimmed().length() > 0 )
1102-
{
1103-
if ( vals.length() > 0 ) vals.append (",");
1104-
vals.append ( lineEdit->text().trimmed() );
1105-
}
1106-
}
1107-
list.push_back( mKey + "=" + vals );
1108-
} else if ( mControlType == ComboBox ) {
1109-
list.push_back( mKey + "=" + mValues[mComboBox->currentItem()] );
1110-
} else if ( mControlType == CheckBoxes ) {
1111-
QString opt = mKey + "=";
1112-
int cnt = 0;
1113-
for ( int i = 0; i < mCheckBoxes.size(); i++ ) {
1114-
if ( mCheckBoxes[i]->isChecked() ) {
1115-
if ( cnt > 0 ) opt.append ( "," );
1116-
opt.append ( mValues[i] );
1117-
}
1118-
}
1119-
list.push_back( opt );
1120-
}
1230+
list.push_back( mKey + "=" + value() );
11211231
}
11221232
return list;
11231233
}

src/plugins/grass/qgsgrassmodule.h

+30-1
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,10 @@ public slots:
105105
void on_mCloseButton_clicked() { close(); }
106106
void close ();
107107

108+
//! Show output in map view
109+
void on_mViewButton_clicked() { viewOutput(); }
110+
void viewOutput ();
111+
108112
//! Running process finished
109113
void finished ();
110114

@@ -147,6 +151,15 @@ public slots:
147151

148152
//! Pointer to options widget
149153
QgsGrassModuleOptions *mOptions;
154+
155+
//! Last raster output
156+
QStringList mOutputRaster;
157+
158+
//! Last vector output
159+
QStringList mOutputVector;
160+
161+
//! True if the module successfully finished
162+
bool mSuccess;
150163
};
151164

152165
/*! \class QgsGrassModuleOptions
@@ -176,6 +189,9 @@ class QgsGrassModuleOptions
176189
// Returns empty string or error message
177190
virtual QStringList ready() { return QStringList() ; }
178191

192+
//! Get list of current output maps
193+
virtual QStringList output(int type) { return QStringList() ; }
194+
179195
protected:
180196
//! QGIS application
181197
QgisApp *mQgisApp;
@@ -222,9 +238,10 @@ class QgsGrassModuleStandardOptions: public QgsGrassModuleOptions, QWidget
222238
// ! Get item by ID
223239
QgsGrassModuleItem *item(QString id);
224240

241+
// Reimplemented methods from QgsGrassModuleOptions
225242
QStringList checkOutput();
226-
227243
QStringList ready() ;
244+
QStringList output(int type);
228245

229246
private:
230247
//! Name of module executable
@@ -320,19 +337,28 @@ class QgsGrassModuleOption: public QGroupBox, public QgsGrassModuleItem
320337

321338
//! Control option
322339
enum ValueType { Double, Integer, String };
340+
341+
//! Output type
342+
enum OutputType { None, Vector, Raster };
323343

324344
//! Retruns list of options which will be passed to module
325345
virtual QStringList options();
326346

327347
//! True if this option is output
328348
bool isOutput() { return mIsOutput; }
349+
350+
//! Returns output type
351+
int outputType() { return mOutputType; }
329352

330353
//! If output, check if current output exists
331354
// Returns emppty string or name of existing output
332355
QString outputExists();
333356

334357
QString ready() ;
335358

359+
//! Current value
360+
QString value();
361+
336362
public slots:
337363
// Add new line edit for multiple options
338364
void addLineEdit();
@@ -347,6 +373,9 @@ public slots:
347373
//! Value type
348374
ValueType mValueType;
349375

376+
//! Output type
377+
OutputType mOutputType;
378+
350379
//! If have defined value limits
351380
bool mHaveLimits;
352381
double mMin, mMax;

0 commit comments

Comments
 (0)