Skip to content

Commit

Permalink
file option
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.osgeo.org/qgis/trunk@5073 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
rblazek committed Mar 22, 2006
1 parent 7e28a54 commit 30c7158
Show file tree
Hide file tree
Showing 2 changed files with 230 additions and 14 deletions.
184 changes: 174 additions & 10 deletions src/plugins/grass/qgsgrassmodule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
#include <QDoubleValidator>
#include <QPushButton>
#include <QGroupBox>
#include <QFileDialog>

#include "qgis.h"
#include "qgsapplication.h"
Expand Down Expand Up @@ -332,7 +333,7 @@ QgsGrassModuleStandardOptions::QgsGrassModuleStandardOptions (
QDomElement e = n.toElement();
if( !e.isNull() ) {
QString optionType = e.tagName();
//std::cout << "optionType = " << optionType << std::endl;
std::cout << "optionType = " << optionType.toLocal8Bit().data() << std::endl;

QString key = e.attribute("key");
std::cout << "key = " << key.toLocal8Bit().data() << std::endl;
Expand Down Expand Up @@ -404,6 +405,13 @@ QgsGrassModuleStandardOptions::QgsGrassModuleStandardOptions (
layout->addWidget ( mi );
mItems.push_back(mi);
}
else if ( optionType == "file" )
{
QgsGrassModuleFile *mi = new QgsGrassModuleFile (
mModule, key, e, gDocElem, gnode, mParent );
layout->addWidget ( mi );
mItems.push_back(mi);
}
else if ( optionType == "flag" )
{
QgsGrassModuleFlag *flag = new QgsGrassModuleFlag (
Expand Down Expand Up @@ -946,7 +954,7 @@ QgsGrassModuleOption::QgsGrassModuleOption ( QgsGrassModule *module, QString key
QWidget * parent)
: QGroupBox ( parent ),
QgsGrassModuleItem ( module, key, qdesc, gdesc, gnode ),
mIsOutput(false), mValueType(String), mHaveLimits(false), mOutputType(None)
mIsOutput(false), mValueType(String), mHaveLimits(false), mOutputType(None), mControlType(NoControl)
{
#ifdef QGISDEBUG
std::cerr << "QgsGrassModuleOption::QgsGrassModuleOption" << std::endl;
Expand Down Expand Up @@ -1041,6 +1049,10 @@ QgsGrassModuleOption::QgsGrassModuleOption ( QgsGrassModule *module, QString key

if ( mControlType == ComboBox ) {
mComboBox->insertItem ( desc );
if ( mAnswer.length() > 0 && desc == mAnswer )
{
mComboBox->setCurrentIndex(mComboBox->count()-1);
}
} else {
QCheckBox *cb = new QCheckBox ( desc, this );
mCheckBoxes.push_back ( cb );
Expand All @@ -1060,12 +1072,6 @@ QgsGrassModuleOption::QgsGrassModuleOption ( QgsGrassModule *module, QString key
// Line edit
mControlType = LineEdit;

QDomNode n = gnode.namedItem ( "default" );
if ( !n.isNull() ) {
QDomElement e = n.toElement();
mDefault = e.text().stripWhiteSpace();
}

if ( gelem.attribute("type") == "integer" )
{
mValueType = Integer;
Expand Down Expand Up @@ -1129,7 +1135,7 @@ void QgsGrassModuleOption::addLineEdit()
// TODO make the widget growing with new lines. HOW???!!!
QLineEdit *lineEdit = new QLineEdit ( this );
mLineEdits.push_back (lineEdit );
lineEdit->setText ( mDefault );
lineEdit->setText ( mAnswer );

if ( mValueType == Integer )
{
Expand Down Expand Up @@ -1729,7 +1735,20 @@ QgsGrassModuleItem::QgsGrassModuleItem( QgsGrassModule *module, QString key,
mHidden(false),
mModule(module)
{
mAnswer = qdesc.attribute("answer", "");
//mAnswer = qdesc.attribute("answer", "");

if ( !qdesc.attribute("answer").isNull() )
{
mAnswer = qdesc.attribute("answer").trimmed();
}
else
{
QDomNode n = gnode.namedItem ( "default" );
if ( !n.isNull() ) {
QDomElement e = n.toElement();
mAnswer = e.text().trimmed();
}
}

if ( qdesc.attribute("hidden") == "yes" ) {
mHidden = true;
Expand Down Expand Up @@ -2083,3 +2102,148 @@ QStringList QgsGrassModuleSelection::options()
QgsGrassModuleSelection::~QgsGrassModuleSelection()
{
}

/***************** QgsGrassModuleFile *********************/

QgsGrassModuleFile::QgsGrassModuleFile (
QgsGrassModule *module,
QString key, QDomElement &qdesc,
QDomElement &gdesc, QDomNode &gnode, QWidget * parent)
: QGroupBox ( parent ),
QgsGrassModuleItem ( module, key, qdesc, gdesc, gnode ),
mType(Old)
{
QString tit;
if ( mDescription.isEmpty() )
{
tit = "File";
}
else
{
if ( mDescription.length() > 40 ) {
tit = mDescription.left(40) + " ...";
} else {
tit = mDescription;
}
}

setTitle ( " " + tit + " " );

QDomNode promptNode = gnode.namedItem ( "gisprompt" );
QDomElement promptElem = promptNode.toElement();
QString element = promptElem.attribute("element");

if ( qdesc.attribute("type").toLower() == "new" )
{
mType = New;
}

if ( !qdesc.attribute("filters").isNull() )
{
mFilters = qdesc.attribute("filters").split(";;");

if ( mFilters.size() > 0 )
{
QRegExp rx ( ".*\\( *..([^ )]*).*" );
QString ext;
if ( rx.search(mFilters.at(0)) == 0 )
{
mSuffix = rx.cap(1);
}
}
}

mFileOption = qdesc.attribute("fileoption");

QHBoxLayout *l = new QHBoxLayout (this);
mLineEdit = new QLineEdit ();
mBrowseButton = new QPushButton ( "..." );
l->addWidget ( mLineEdit );
l->addWidget ( mBrowseButton );

connect ( mBrowseButton, SIGNAL(clicked()),
this, SLOT(browse()) );
}

QStringList QgsGrassModuleFile::options()
{
QStringList list;
QString path = mLineEdit->text().trimmed();

if ( mFileOption.isNull() )
{
QString opt(mKey + "=" + path );
list.push_back( opt );
}
else
{
QFileInfo fi(path);

QString opt(mKey + "=" + fi.dirPath() );
list.push_back( opt );

opt = mFileOption + "=" + fi.baseName();
list.push_back( opt );
}

return list;
}

void QgsGrassModuleFile::browse()
{
// TODO: unfortunately QFileDialog does not support 'new' directory
QFileDialog *fd = new QFileDialog ( this, NULL, mLineEdit->text() );

fd->setDirectory ( QDir::current() );

fd->setMode ( QFileDialog::AnyFile );

if ( mType == New )
{
fd->setAcceptMode ( QFileDialog::AcceptSave );
}
else
{
fd->setAcceptMode ( QFileDialog::AcceptOpen );
}

if ( mFilters.size() > 0 )
{
fd->setFilters ( mFilters );
}
fd->setDefaultSuffix ( mSuffix );

if ( fd->exec() == QDialog::Accepted )
{
mLineEdit->setText( fd->selectedFile() );
}
}

QString QgsGrassModuleFile::ready()
{
#ifdef QGISDEBUG
std::cerr << "QgsGrassModuleFile::ready()" << std::endl;
#endif

QString error;
QString path = mLineEdit->text().trimmed();


if ( path.length() == 0 )
{
error.append ( title() + ":&nbsp;missing value" );
return error;
}

QFileInfo fi(path);
if ( !fi.dir().exists() )
{
error.append ( title() + ":&nbsp;directory does not exist" );
}

return error;
}

QgsGrassModuleFile::~QgsGrassModuleFile()
{
}
60 changes: 56 additions & 4 deletions src/plugins/grass/qgsgrassmodule.h
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,7 @@ class QgsGrassModuleItem

//! Predefined answer from config
QString mAnswer;

private:

};
Expand All @@ -333,7 +334,7 @@ class QgsGrassModuleOption: public QGroupBox, public QgsGrassModuleItem
~QgsGrassModuleOption();

//! Control option
enum ControlType { LineEdit, ComboBox, SpinBox, CheckBoxes };
enum ControlType { NoControl, LineEdit, ComboBox, SpinBox, CheckBoxes };

//! Control option
enum ValueType { Double, Integer, String };
Expand Down Expand Up @@ -380,9 +381,6 @@ public slots:
bool mHaveLimits;
double mMin, mMax;

//! Default value
QString mDefault;

//! Combobox
QComboBox *mComboBox;

Expand Down Expand Up @@ -667,4 +665,58 @@ public slots:
QLineEdit *mLineEdit;
};

/*********************** QgsGrassModuleFile **********************/

/*! \class QgsGrassModuleSelection
* \brief Input/output file.
*/
class QgsGrassModuleFile: public QGroupBox, public QgsGrassModuleItem
{
Q_OBJECT;

public:
/*! \brief Constructor
* \param qdesc option element in QGIS module description XML file
* \param gdesc GRASS module XML description file
*/
QgsGrassModuleFile ( QgsGrassModule *module,
QString key,
QDomElement &qdesc, QDomElement &gdesc,
QDomNode &gnode,
QWidget * parent = 0 );

//! Destructor
~QgsGrassModuleFile();

//! File type
enum Type { Old, New };

// Reimplemented methods from QgsGrassModuleOptions
QStringList options();
QString ready() ;

public slots:
// browse files
void browse();

private:
// ! File type (New, Old)
int mType;

// ! Optionaly split file to dir and file path
QString mFileOption;

// ! Default suffix
QString mSuffix;

//! Line
QLineEdit *mLineEdit;

//! Browse button
QPushButton *mBrowseButton;

//! File filters
QStringList mFilters;
};

#endif // QGSGRASSMODULE_H

0 comments on commit 30c7158

Please sign in to comment.