📄 qgsgrassmodule.cpp
字号:
mProcess.setReadChannel ( QProcess::StandardError ); QString line; QRegExp rxpercent ( "GRASS_INFO_PERCENT: (\\d+)" ); QRegExp rxmessage ( "GRASS_INFO_MESSAGE\\(\\d+,\\d+\\): (.*)" ); QRegExp rxwarning ( "GRASS_INFO_WARNING\\(\\d+,\\d+\\): (.*)" ); QRegExp rxerror ( "GRASS_INFO_ERROR\\(\\d+,\\d+\\): (.*)" ); QRegExp rxend ( "GRASS_INFO_END\\(\\d+,\\d+\\)" ); while ( mProcess.canReadLine() ) { //line = QString::fromLocal8Bit( mProcess.readLineStderr().ascii() ); QByteArray ba = mProcess.readLine(); line = QString::fromLocal8Bit( QString(ba).ascii() ); //QgsDebugMsg(QString("line: '%1'").arg(line)); if ( rxpercent.search ( line ) != -1 ) { int progress = rxpercent.cap(1).toInt(); mProgressBar->setProgress ( progress, 100 ); } else if ( rxmessage.search ( line ) != -1 ) { mOutputTextBrowser->append ( rxmessage.cap(1) ); } else if ( rxwarning.search ( line ) != -1 ) { QString warn = rxwarning.cap(1); QString img = QgsApplication::pkgDataPath() + "/themes/default/grass/grass_module_warning.png"; mOutputTextBrowser->append ( "<img src=\"" + img + "\">" + warn ); } else if ( rxerror.search ( line ) != -1 ) { QString error = rxerror.cap(1); QString img = QgsApplication::pkgDataPath() + "/themes/default/grass/grass_module_error.png"; mOutputTextBrowser->append ( "<img src=\"" + img + "\">" + error ); } else if ( rxend.search ( line ) != -1 ) { // Do nothing } else { mOutputTextBrowser->append ( line + "\n" ); } }}void QgsGrassModule::close(){ QgsDebugMsg("called."); QTabWidget *tw = dynamic_cast<QTabWidget *>(mParent); tw->removePage (this ); delete this;}void QgsGrassModule::viewOutput(){ QgsDebugMsg("called."); if ( !mSuccess ) return; for (int i = 0; i < mOutputVector.size(); i++ ) { QString map = mOutputVector.at(i); QStringList layers = QgsGrassSelect::vectorLayers ( QgsGrass::getDefaultGisdbase(), QgsGrass::getDefaultLocation(), QgsGrass::getDefaultMapset(), map ); // check whether there are 1_* layers // if so, 0_* layers won't be added bool onlyLayer1 = false; for ( int j = 0; j < layers.count(); j++ ) { if (layers[j].left(1) == "1") { onlyLayer1 = true; break; } } // TODO common method for add all layers for ( int j = 0; j < layers.count(); j++ ) { QString uri = QgsGrass::getDefaultGisdbase() + "/" + QgsGrass::getDefaultLocation() + "/" + QgsGrass::getDefaultMapset() + "/" + map + "/" + layers[j]; // skip 0_* layers if (onlyLayer1 && layers[j].left(1) != "1") continue; // TODO vector layer name mIface->addVectorLayer( uri, layers[j], "grass"); } } for (int i = 0; i < mOutputRaster.size(); i++ ) { QString map = mOutputRaster.at(i); QString uri = QgsGrass::getDefaultGisdbase() + "/" + QgsGrass::getDefaultLocation() + "/" + QgsGrass::getDefaultMapset() + "/cellhd/" + map; mIface->addRasterLayer(uri, map); }}QgisInterface *QgsGrassModule::qgisIface() { return mIface; }QgsGrassModule::~QgsGrassModule(){ QgsDebugMsg("called."); if ( mProcess.state() == QProcess::Running ) { mProcess.kill(); }}QDomNode QgsGrassModule::nodeByKey ( QDomElement elem, QString key ){ QgsDebugMsg("called with key=" + key); QDomNode n = elem.firstChild(); while( !n.isNull() ) { QDomElement e = n.toElement(); if( !e.isNull() ) { if ( e.tagName() == "parameter" || e.tagName() == "flag" ) { if ( e.attribute("name") == key ) { return n; } } } n = n.nextSibling(); } return QDomNode();}/******************* QgsGrassModuleOption ****************************/QgsGrassModuleOption::QgsGrassModuleOption ( QgsGrassModule *module, QString key, QDomElement &qdesc, QDomElement &gdesc, QDomNode &gnode, QWidget * parent) : QGroupBox ( parent ), QgsGrassModuleItem ( module, key, qdesc, gdesc, gnode ), mControlType(NoControl), mValueType(String), mOutputType(None), mHaveLimits(false), mIsOutput(false){ QgsDebugMsg("called."); setSizePolicy ( QSizePolicy::MinimumExpanding, QSizePolicy::Minimum ); if ( mHidden ) hide(); mLayout = new QVBoxLayout (); QString tit; if ( mDescription.length() > 40 ) { tit = mDescription.left(40) + " ..."; } else { tit = mDescription; } 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; if ( element == "vector" ) { mOutputType = Vector; } else if ( element == "cell" ) { mOutputType = Raster; } } } // String without options if ( !mHidden ) { QDomElement gelem = gnode.toElement(); // Predefined values ? QDomNode valuesNode = gnode.namedItem ( "values" ); QDomElement valuesElem = valuesNode.toElement(); // null if valuesNode is null if ( !valuesNode.isNull() && valuesNode.childNodes().count() > 1 ) { setLayout(mLayout); // predefined values -> ComboBox or CheckBox // one or many? if ( gelem.attribute("multiple") == "yes" ) { mControlType = CheckBoxes; } else { mControlType = ComboBox; mComboBox = new QComboBox ( this ); mLayout->addWidget ( mComboBox ); } // List of values to be excluded QStringList exclude = QStringList::split ( ',', qdesc.attribute("exclude") ); QDomNode valueNode = valuesElem.firstChild(); while( !valueNode.isNull() ) { QDomElement valueElem = valueNode.toElement(); if( !valueElem.isNull() && valueElem.tagName() == "value" ) { QDomNode n = valueNode.namedItem ( "name" ); if ( !n.isNull() ) { QDomElement e = n.toElement(); QString val = e.text().stripWhiteSpace(); if ( exclude.contains(val) == 0 ) { n = valueNode.namedItem ( "description" ); QString desc; if ( !n.isNull() ) { e = n.toElement(); desc = e.text().stripWhiteSpace(); } else { desc = val; } desc.replace( 0, 1, desc.left(1).upper() ); 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 ); mLayout->addWidget ( cb ); } mValues.push_back ( val ); } } } valueNode = valueNode.nextSibling(); } } else // No values { // Line edit mControlType = LineEdit; if ( gelem.attribute("type") == "integer" ) { mValueType = Integer; } else if ( gelem.attribute("type") == "float" ) { mValueType = Double; } QStringList minMax; if ( valuesNode.childNodes().count() == 1 ) { QDomNode valueNode = valuesElem.firstChild(); QDomNode n = valueNode.namedItem ( "name" ); if ( !n.isNull() ) { QDomElement e = n.toElement(); QString val = e.text().stripWhiteSpace(); minMax = val.split("-"); if ( minMax.size() == 2 ) { mHaveLimits = true; mMin = minMax.at(0).toDouble(); mMax = minMax.at(1).toDouble(); } } } QDomNode keydescNode = gnode.namedItem ( "keydesc" ); if (!keydescNode.isNull()) { // fixed number of line edits // Example: // <keydesc> // <item order="1">rows</item> // <item order="2">columns</item> // </keydesc> QDomNodeList keydescs = keydescNode.childNodes(); for (int k = 0; k < keydescs.count(); k++) { QDomNode nodeItem = keydescs.at(k); QString itemDesc = nodeItem.toElement().text().stripWhiteSpace(); //QString itemDesc = nodeItem.firstChild().toText().data(); QgsDebugMsg("keydesc item = " + itemDesc); addLineEdit(); } setLayout(mLayout); } else if ( gelem.attribute("multiple") == "yes" ) { // variable number of line edits // add/delete buttons for multiple options QHBoxLayout *l = new QHBoxLayout (this); QVBoxLayout *vl = new QVBoxLayout (); l->insertLayout( -1, mLayout ); l->insertLayout( -1, vl ); // TODO: how to keep both buttons on the top? QPushButton *b = new QPushButton ( "+", this); connect( b, SIGNAL(clicked()), this, SLOT(addLineEdit()) ); vl->addWidget ( b, 0, Qt::AlignTop ); b = new QPushButton ( "-", this); connect( b, SIGNAL(clicked()), this, SLOT(removeLineEdit()) ); vl->addWidget ( b, 0, Qt::AlignTop ); // Dont enable this, it makes the group box expanding // vl->addStretch(); } else { // only one line edit addLineEdit(); setLayout ( mLayout ); } } } mUsesRegion = false; QString region = qdesc.attribute("region"); if ( region.length() > 0 ) { if ( region == "yes" ) mUsesRegion = true; } else { QgsDebugMsg("\n\n\n\n**************************"); QgsDebugMsg("isOutput = " + isOutput() ); QgsDebugMsg("mOutputType = " + mOutputType); if ( isOutput() && mOutputType == Raster ) mUsesRegion = true; } QgsDebugMsg("mUsesRegion = " + mUsesRegion);}void QgsGrassModuleOption::addLineEdit(){ QgsDebugMsg("called."); // TODO make the widget growing with new lines. HOW???!!! QLineEdit *lineEdit = new QLineEdit ( this ); mLineEdits.push_back (lineEdit ); lineEdit->setText ( mAnswer ); if ( mValueType == Integer ) { if ( mHaveLimits ) { mValidator = new QIntValidator( (int)mMin, (int)mMax, this ); } else { mValidator = new QIntValidator( this ); } lineEdit->setValidator ( mValidator ); } else if ( mValueType == Double ) { if ( mHaveLimits ) { mValidator = new QDoubleValidator( mMin, mMax, 10, this ); } else { mValidator = new QDoubleValidator( this ); } lineEdit->setValidator ( mValidator ); } else if ( mIsOutput ) { QRegExp rx; if ( mOutputType == Vector ) { rx.setPattern("[A-Za-z_][A-Za-z0-9_]+"); } else { rx.setPattern("[A-Za-z0-9_.]+"); } mValidator = new QRegExpValidator( rx, this ); lineEdit->setValidator ( mValidator ); } mLayout->addWidget ( lineEdit );}void QgsGrassModuleOption::removeLineEdit(){ QgsDebugMsg("called."); if ( mLineEdits.size() < 2 ) return; delete mLineEdits.at(mLineEdits.size()-1); mLineEdits.pop_back(); }QString QgsGrassModuleOption::outputExists(){ QgsDebugMsg("called."); if ( !mIsOutput ) return QString(); QLineEdit *lineEdit = mLineEdits.at(0); QString value = lineEdit->text().trimmed(); QgsDebugMsg("mKey = " + mKey); QgsDebugMsg("value = " + value); QgsDebugMsg("mOutputElement = " + mOutputElement); if ( value.length() == 0 ) return QString(); QString path = QgsGrass::getDefaultGisdbase() + "/" + QgsGrass::getDefaultLocation() + "/" + QgsGrass::getDefaultMapset() + "/" + mOutputElement + "/" + value; QFileInfo fi(path); if ( fi.exists() ) { return (lineEdit->text()); } return QString();}QString QgsGrassModuleOption::value(){ QString value; if ( mControlType == LineEdit ) { for ( unsigned int i = 0; i < mLineEdits.size(); i++ ) { QLineEdit *lineEdit = mLineEdits.at(i); if( lineEdit->text().trimmed().length() > 0 ) { if ( value.length() > 0 ) value.append (","); value.append ( lineEdit->text().trimmed() ); } } } else if ( mControlType == ComboBox ) { value = mValues[mComboBox->currentItem()]; } else if ( mControlType == CheckBoxes ) { int cnt = 0; for ( unsigned int i = 0; i < mCheckBoxes.size(); i++ ) { if ( mCheckBoxes[i]->isChecked() ) { if ( cnt > 0 ) value.append ( "," ); value.append ( mValues[i] ); } } } return value;}QStringList QgsGrassModuleOption::options(){ QStringList list;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -