Skip to content

Commit 4106f2d

Browse files
author
rblazek
committed
multiple input values, check input values
git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@5014 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent 54c96a6 commit 4106f2d

File tree

2 files changed

+235
-63
lines changed

2 files changed

+235
-63
lines changed

src/plugins/grass/qgsgrassmodule.cpp

Lines changed: 199 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,12 @@
6161
#include <qimage.h>
6262
//Added by qt3to4:
6363
#include <QVBoxLayout>
64+
#include <QHBoxLayout>
6465
#include <QGridLayout>
6566
#include <QIntValidator>
6667
#include <QDoubleValidator>
68+
#include <QPushButton>
69+
#include <QGroupBox>
6770

6871
#include "qgis.h"
6972
#include "qgsapplication.h"
@@ -405,9 +408,7 @@ QgsGrassModuleStandardOptions::QgsGrassModuleStandardOptions (
405408
n = n.nextSibling();
406409
}
407410

408-
QSpacerItem *si = new QSpacerItem ( 10, 10, QSizePolicy::Minimum, QSizePolicy::Expanding );
409-
layout->addItem ( si );
410-
411+
layout->addStretch();
411412
}
412413

413414
QStringList QgsGrassModuleStandardOptions::arguments()
@@ -446,6 +447,55 @@ QgsGrassModuleItem *QgsGrassModuleStandardOptions::item ( QString id )
446447
return 0;
447448
}
448449

450+
QStringList QgsGrassModuleStandardOptions::checkOutput()
451+
{
452+
#ifdef QGISDEBUG
453+
std::cerr << "QgsGrassModuleStandardOptions::checkOutput()" << std::endl;
454+
#endif
455+
QStringList list;
456+
457+
for ( int i = 0; i < mItems.size(); i++ )
458+
{
459+
if ( typeid(*(mItems[i])) != typeid (QgsGrassModuleOption) ) {
460+
continue;
461+
}
462+
QgsGrassModuleOption *opt =
463+
dynamic_cast<QgsGrassModuleOption *> ( mItems[i] );
464+
465+
std::cerr << "opt->key() = " << opt->key().ascii() << std::endl;
466+
467+
if ( opt->isOutput() )
468+
{
469+
QString out = opt->outputExists();
470+
if ( !out.isNull() )
471+
{
472+
list.append ( out );
473+
}
474+
}
475+
}
476+
477+
return list;
478+
}
479+
480+
QStringList QgsGrassModuleStandardOptions::ready()
481+
{
482+
#ifdef QGISDEBUG
483+
std::cerr << "QgsGrassModuleStandardOptions::ready()" << std::endl;
484+
#endif
485+
QStringList list;
486+
487+
for ( int i = 0; i < mItems.size(); i++ )
488+
{
489+
QString err = mItems[i]->ready();
490+
if ( !err.isNull() )
491+
{
492+
list.append ( err );
493+
}
494+
}
495+
496+
return list;
497+
}
498+
449499
QgsGrassModuleStandardOptions::~QgsGrassModuleStandardOptions()
450500
{
451501
}
@@ -619,6 +669,19 @@ void QgsGrassModule::run()
619669
mProcess.addArgument( mXName );
620670
command = mXName;
621671

672+
// Check if options are ready
673+
QStringList readyErrors = mOptions->ready();
674+
if ( readyErrors.size() > 0 )
675+
{
676+
QString err;
677+
for ( int i = 0; i < readyErrors.size(); i++ )
678+
{
679+
err.append ( readyErrors.at(i) + "<br>" );
680+
}
681+
QMessageBox::warning ( 0, "Warning", err );
682+
return;
683+
}
684+
622685
// Check if output exists
623686
QStringList outputExists = mOptions->checkOutput();
624687
if ( outputExists.size() > 0 )
@@ -789,16 +852,18 @@ QDomNode QgsGrassModule::nodeByKey ( QDomElement elem, QString key )
789852
QgsGrassModuleOption::QgsGrassModuleOption ( QgsGrassModule *module, QString key,
790853
QDomElement &qdesc, QDomElement &gdesc, QDomNode &gnode,
791854
QWidget * parent)
792-
: Q3GroupBox ( 1, Qt::Vertical, parent ),
855+
: QGroupBox ( parent ),
793856
QgsGrassModuleItem ( module, key, qdesc, gdesc, gnode ),
794-
mIsOutput(false), mValueType(String)
857+
mIsOutput(false), mValueType(String), mHaveLimits(false)
795858
{
796859
#ifdef QGISDEBUG
797860
std::cerr << "QgsGrassModuleOption::QgsGrassModuleOption" << std::endl;
798861
#endif
799-
setSizePolicy ( QSizePolicy::Preferred, QSizePolicy::Minimum );
862+
setSizePolicy ( QSizePolicy::MinimumExpanding, QSizePolicy::Minimum );
800863

801864
if ( mHidden ) hide();
865+
866+
mLayout = new QVBoxLayout ();
802867

803868
QString tit;
804869
if ( mDescription.length() > 40 ) {
@@ -833,6 +898,7 @@ QgsGrassModuleOption::QgsGrassModuleOption ( QgsGrassModule *module, QString key
833898

834899
if ( !valuesNode.isNull() && valuesNode.childNodes().count() > 1 )
835900
{
901+
setLayout(mLayout);
836902
// predefined values -> ComboBox or CheckBox
837903

838904
// one or many?
@@ -841,6 +907,7 @@ QgsGrassModuleOption::QgsGrassModuleOption ( QgsGrassModule *module, QString key
841907
} else {
842908
mControlType = ComboBox;
843909
mComboBox = new QComboBox ( this );
910+
mLayout->addWidget ( mComboBox );
844911
}
845912

846913
// List of values to be excluded
@@ -875,6 +942,7 @@ QgsGrassModuleOption::QgsGrassModuleOption ( QgsGrassModule *module, QString key
875942
} else {
876943
QCheckBox *cb = new QCheckBox ( desc, this );
877944
mCheckBoxes.push_back ( cb );
945+
mLayout->addWidget ( cb );
878946
}
879947

880948
mValues.push_back ( val );
@@ -890,12 +958,17 @@ QgsGrassModuleOption::QgsGrassModuleOption ( QgsGrassModule *module, QString key
890958
// Line edit
891959
mControlType = LineEdit;
892960

893-
mLineEdit = new QLineEdit ( this );
894961
QDomNode n = gnode.namedItem ( "default" );
895962
if ( !n.isNull() ) {
896963
QDomElement e = n.toElement();
897-
QString def = e.text().stripWhiteSpace();
898-
mLineEdit->setText ( def );
964+
mDefault = e.text().stripWhiteSpace();
965+
}
966+
967+
if ( gelem.attribute("type") == "integer" )
968+
{
969+
mValueType = Integer;
970+
} else if ( gelem.attribute("type") == "float" ) {
971+
mValueType = Double;
899972
}
900973

901974
QStringList minMax;
@@ -908,39 +981,90 @@ QgsGrassModuleOption::QgsGrassModuleOption ( QgsGrassModule *module, QString key
908981
QDomElement e = n.toElement();
909982
QString val = e.text().stripWhiteSpace();
910983
minMax = val.split("-");
984+
if ( minMax.size() == 2 )
985+
{
986+
mHaveLimits = true;
987+
mMin = minMax.at(0).toDouble();
988+
mMax = minMax.at(1).toDouble();
989+
}
911990
}
912991
}
913-
if ( gelem.attribute("type") == "integer" )
992+
993+
addLineEdit();
994+
995+
// add/delete buttons for multiple options
996+
if ( gelem.attribute("multiple") == "yes" )
914997
{
915-
mValueType = Integer;
916-
if ( minMax.size() == 2 ) {
917-
mValidator = new QIntValidator( minMax.at(0).toInt(),
918-
minMax.at(1).toInt(), this );
919-
} else {
920-
mValidator = new QIntValidator( this );
921-
}
922-
mLineEdit->setValidator ( mValidator );
923-
} else if ( gelem.attribute("type") == "float" ) {
924-
mValueType = Double;
925-
if ( minMax.size() == 2 ) {
926-
mValidator = new QDoubleValidator( minMax.at(0).toDouble(),
927-
minMax.at(1).toDouble(), 10, this );
928-
} else {
929-
mValidator = new QDoubleValidator( this );
930-
}
931-
mLineEdit->setValidator ( mValidator );
932-
}
998+
QHBoxLayout *l = new QHBoxLayout (this);
999+
QVBoxLayout *vl = new QVBoxLayout ();
1000+
l->insertLayout( -1, mLayout );
1001+
l->insertLayout( -1, vl );
1002+
1003+
// TODO: how to keep both buttons on the top?
1004+
QPushButton *b = new QPushButton ( "+", this);
1005+
connect( b, SIGNAL(clicked()), this, SLOT(addLineEdit()) );
1006+
vl->addWidget ( b, 0, Qt::AlignTop );
1007+
1008+
b = new QPushButton ( "-", this);
1009+
connect( b, SIGNAL(clicked()), this, SLOT(removeLineEdit()) );
1010+
vl->addWidget ( b, 0, Qt::AlignTop );
1011+
1012+
// Dont enable this, it makes the group box expanding
1013+
// vl->addStretch();
1014+
}
1015+
else
1016+
{
1017+
setLayout ( mLayout );
1018+
}
9331019
}
9341020
}
9351021
}
9361022

1023+
void QgsGrassModuleOption::addLineEdit()
1024+
{
1025+
std::cerr << "QgsGrassModuleOption::addLineEdit()" << std::endl;
1026+
1027+
// TODO make the widget growing with new lines. HOW???!!!
1028+
QLineEdit *lineEdit = new QLineEdit ( this );
1029+
mLineEdits.push_back (lineEdit );
1030+
lineEdit->setText ( mDefault );
1031+
1032+
if ( mValueType == Integer )
1033+
{
1034+
if ( mHaveLimits ) {
1035+
mValidator = new QIntValidator( (int)mMin, (int)mMax, this );
1036+
} else {
1037+
mValidator = new QIntValidator( this );
1038+
}
1039+
lineEdit->setValidator ( mValidator );
1040+
} else if ( mValueType == Double ) {
1041+
if ( mHaveLimits ) {
1042+
mValidator = new QDoubleValidator( mMin, mMax, 10, this );
1043+
} else {
1044+
mValidator = new QDoubleValidator( this );
1045+
}
1046+
lineEdit->setValidator ( mValidator );
1047+
}
1048+
1049+
mLayout->addWidget ( lineEdit );
1050+
}
1051+
1052+
void QgsGrassModuleOption::removeLineEdit()
1053+
{
1054+
std::cerr << "QgsGrassModuleOption::removeLineEdit()" << std::endl;
1055+
if ( mLineEdits.size() < 2 ) return;
1056+
delete mLineEdits.at(mLineEdits.size()-1);
1057+
mLineEdits.pop_back();
1058+
}
1059+
9371060
QString QgsGrassModuleOption::outputExists()
9381061
{
9391062
std::cerr << "QgsGrassModuleOption::outputExists()" << std::endl;
9401063

9411064
if ( !mIsOutput ) return QString();
9421065

943-
QString value = mLineEdit->text().trimmed();
1066+
QLineEdit *lineEdit = mLineEdits.at(0);
1067+
QString value = lineEdit->text().trimmed();
9441068
std::cerr << "mKey = " << mKey.ascii() << std::endl;
9451069
std::cerr << "value = " << value.ascii() << std::endl;
9461070
std::cerr << "mOutputElement = " << mOutputElement.ascii() << std::endl;
@@ -956,7 +1080,7 @@ QString QgsGrassModuleOption::outputExists()
9561080

9571081
if ( fi.exists() )
9581082
{
959-
return (mLineEdit->text());
1083+
return (lineEdit->text());
9601084
}
9611085

9621086
return QString();
@@ -970,7 +1094,17 @@ QStringList QgsGrassModuleOption::options()
9701094
list.push_back( mKey + "=" + mAnswer );
9711095
} else {
9721096
if ( mControlType == LineEdit ) {
973-
list.push_back( mKey + "=" + mLineEdit->text() );
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 );
9741108
} else if ( mControlType == ComboBox ) {
9751109
list.push_back( mKey + "=" + mValues[mComboBox->currentItem()] );
9761110
} else if ( mControlType == CheckBoxes ) {
@@ -988,6 +1122,24 @@ QStringList QgsGrassModuleOption::options()
9881122
return list;
9891123
}
9901124

1125+
QString QgsGrassModuleOption::ready()
1126+
{
1127+
#ifdef QGISDEBUG
1128+
std::cerr << "QgsGrassModuleOption::ready()" << std::endl;
1129+
#endif
1130+
1131+
QString error;
1132+
1133+
if ( mControlType == LineEdit )
1134+
{
1135+
if ( mLineEdits.at(0)->text().trimmed().length() == 0 )
1136+
{
1137+
error.append ( title() + ":&nbsp;missing value" );
1138+
}
1139+
}
1140+
return error;
1141+
}
1142+
9911143
QgsGrassModuleOption::~QgsGrassModuleOption()
9921144
{
9931145
}
@@ -1190,36 +1342,6 @@ QgsGrassModuleInput::QgsGrassModuleInput ( QgsGrassModule *module,
11901342
updateQgisLayers();
11911343
}
11921344

1193-
QStringList QgsGrassModuleStandardOptions::checkOutput()
1194-
{
1195-
#ifdef QGISDEBUG
1196-
std::cerr << "QgsGrassModuleStandardOptions::checkOutput()" << std::endl;
1197-
#endif
1198-
QStringList list;
1199-
1200-
for ( int i = 0; i < mItems.size(); i++ )
1201-
{
1202-
if ( typeid(*(mItems[i])) != typeid (QgsGrassModuleOption) ) {
1203-
continue;
1204-
}
1205-
QgsGrassModuleOption *opt =
1206-
dynamic_cast<QgsGrassModuleOption *> ( mItems[i] );
1207-
1208-
std::cerr << "opt->key() = " << opt->key().ascii() << std::endl;
1209-
1210-
if ( opt->isOutput() )
1211-
{
1212-
QString out = opt->outputExists();
1213-
if ( !out.isNull() )
1214-
{
1215-
list.append ( out );
1216-
}
1217-
}
1218-
}
1219-
1220-
return list;
1221-
}
1222-
12231345
void QgsGrassModuleInput::updateQgisLayers()
12241346
{
12251347
#ifdef QGISDEBUG
@@ -1469,6 +1591,22 @@ void QgsGrassModuleInput::changed(int i)
14691591
emit valueChanged();
14701592
}
14711593

1594+
QString QgsGrassModuleInput::ready()
1595+
{
1596+
#ifdef QGISDEBUG
1597+
std::cerr << "QgsGrassModuleInput::ready()" << std::endl;
1598+
#endif
1599+
1600+
QString error;
1601+
1602+
std::cerr << "count = " << mLayerComboBox->count() << std::endl;
1603+
if ( mLayerComboBox->count() == 0 )
1604+
{
1605+
error.append ( title() + ":&nbsp;no input" );
1606+
}
1607+
return error;
1608+
}
1609+
14721610
QgsGrassModuleInput::~QgsGrassModuleInput()
14731611
{
14741612
}

0 commit comments

Comments
 (0)