📄 qgsgrassmodule.cpp
字号:
if ( mHidden ) { list.push_back( mKey + "=" + mAnswer ); } else { list.push_back( mKey + "=" + value() ); } return list;}QString QgsGrassModuleOption::ready(){ QgsDebugMsg("called."); QString error; if ( mControlType == LineEdit ) { if ( mLineEdits.at(0)->text().trimmed().length() == 0 ) { error.append ( title() + tr(": missing value") ); } } return error;}QgsGrassModuleOption::~QgsGrassModuleOption(){}QgsGrassModuleFlag::QgsGrassModuleFlag ( QgsGrassModule *module, QString key, QDomElement &qdesc, QDomElement &gdesc, QDomNode &gnode, QWidget * parent) : QCheckBox ( parent ), QgsGrassModuleItem ( module, key, qdesc, gdesc, gnode ){ QgsDebugMsg("called."); if ( mHidden ) hide(); if ( mAnswer == "on" ) setChecked ( true ); else setChecked ( false ); setText ( mDescription );}QStringList QgsGrassModuleFlag::options(){ QStringList list; if ( isChecked() ) { list.push_back( "-" + mKey ); } return list;}QgsGrassModuleFlag::~QgsGrassModuleFlag(){}/************************** QgsGrassModuleInput ***************************/QgsGrassModuleInput::QgsGrassModuleInput ( QgsGrassModule *module, QgsGrassModuleStandardOptions *options, QString key, QDomElement &qdesc, QDomElement &gdesc, QDomNode &gnode, QWidget * parent) : QGroupBox ( parent ), QgsGrassModuleItem ( module, key, qdesc, gdesc, gnode ), mModuleStandardOptions(options), mVectorTypeOption(0), mVectorLayerOption(0), mRegionButton(0), mUpdate(false){ QgsDebugMsg("called."); mVectorTypeMask = GV_POINT | GV_LINE | GV_AREA; QString tit; if ( mDescription.isEmpty() ) { tit = "Input"; } 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 ( element == "vector" ) { mType = Vector; // Read type mask if "typeoption" is defined QString opt = qdesc.attribute("typeoption"); if ( ! opt.isNull() ) { QDomNode optNode = QgsGrassModule::nodeByKey ( gdesc, opt ); if ( optNode.isNull() ) { QMessageBox::warning( 0, tr("Warning"), tr("Cannot find typeoption ") + opt ); } else { mVectorTypeOption = opt; QDomNode valuesNode = optNode.namedItem ( "values" ); if ( valuesNode.isNull() ) { QMessageBox::warning( 0, tr("Warning"), tr("Cannot find values for typeoption ") + opt ); } else { mVectorTypeMask = 0; //GV_POINT | GV_LINE | GV_AREA; QDomElement valuesElem = valuesNode.toElement(); 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 ( val == "point" ) { mVectorTypeMask |= GV_POINT; } else if ( val == "line" ) { mVectorTypeMask |= GV_LINE; } else if ( val == "area" ) { mVectorTypeMask |= GV_AREA; } } } valueNode = valueNode.nextSibling(); } } } } // Read type mask defined in configuration opt = qdesc.attribute("typemask"); if ( ! opt.isNull() ) { int mask = 0; if ( opt.find("point") >= 0 ) { mask |= GV_POINT; } if ( opt.find("line") >= 0 ) { mask |= GV_LINE; } if ( opt.find("area") >= 0 ) { mask |= GV_AREA; } mVectorTypeMask &= mask; } // Read "layeroption" if defined opt = qdesc.attribute("layeroption"); if ( ! opt.isNull() ) { QDomNode optNode = QgsGrassModule::nodeByKey ( gdesc, opt ); if ( optNode.isNull() ) { QMessageBox::warning( 0, tr("Warning"), tr("Cannot find layeroption ") + opt ); } else { mVectorLayerOption = opt; } } // Read "mapid" mMapId = qdesc.attribute("mapid"); } else if ( element == "cell" ) { mType = Raster; } else { QMessageBox::warning( 0, tr("Warning"), tr("GRASS element ") + element + tr(" not supported") ); } if ( qdesc.attribute("update") == "yes" ) { mUpdate = true; } QHBoxLayout *l = new QHBoxLayout (this); mLayerComboBox = new QComboBox (); mLayerComboBox->setSizePolicy (QSizePolicy::Expanding, QSizePolicy:: Preferred ); l->addWidget ( mLayerComboBox ); QString region = qdesc.attribute("region"); if ( mType == Raster && QgsGrass::versionMajor() >= 6 && QgsGrass::versionMinor() >= 1 && region != "no" ) { QString iconPath = QgsApplication::themePath() + "/grass/"; mRegionButton = new QPushButton( QIcon(iconPath+"grass_set_region.png"), "" ); mRegionButton->setToolTip ( tr("Use region of this map") ); mRegionButton->setCheckable ( true ); mRegionButton->setSizePolicy (QSizePolicy::Minimum, QSizePolicy:: Preferred ); l->addWidget ( mRegionButton ); } // Of course, activated(int) is not enough, but there is no signal BEFORE the cobo is opened //connect ( mLayerComboBox, SIGNAL( activated(int) ), this, SLOT(updateQgisLayers()) ); // Connect to canvas QgsMapCanvas *canvas = mModule->qgisIface()->getMapCanvas(); connect ( canvas, SIGNAL(layersChanged()), this, SLOT(updateQgisLayers()) ); connect ( mLayerComboBox, SIGNAL(activated(int)), this, SLOT(changed(int)) ); if ( !mMapId.isEmpty() ) { QgsGrassModuleItem *item = mModuleStandardOptions->item(mMapId); if ( item ) { QgsGrassModuleInput *mapInput = dynamic_cast<QgsGrassModuleInput *>(item); connect ( mapInput, SIGNAL(valueChanged()), this, SLOT(updateQgisLayers()) ); } } mUsesRegion = false; if ( region.length() > 0 ) { if ( region == "yes" ) mUsesRegion = true; } else { if ( type() == Raster ) mUsesRegion = true; } // Fill in QGIS layers updateQgisLayers();}bool QgsGrassModuleInput::useRegion(){ QgsDebugMsg("called."); if ( mUsesRegion && mType == Raster && mRegionButton && mRegionButton->isChecked() ) { return true; } return false;}void QgsGrassModuleInput::updateQgisLayers(){ QgsDebugMsg("called."); QString current = mLayerComboBox->currentText (); mLayerComboBox->clear(); mMaps.resize(0); mVectorTypes.resize(0); mVectorLayerNames.resize(0); mMapLayers.resize(0); mVectorFields.resize(0); QgsMapCanvas *canvas = mModule->qgisIface()->getMapCanvas(); // Find map option QString sourceMap; if ( !mMapId.isEmpty() ) { QgsGrassModuleItem *item = mModuleStandardOptions->item(mMapId); if ( item ) { QgsGrassModuleInput *mapInput = dynamic_cast<QgsGrassModuleInput *>(item); sourceMap = mapInput->currentMap(); } } // Note: QDir::cleanPath is using '/' also on Windows //QChar sep = QDir::separator(); QChar sep = '/'; int nlayers = canvas->layerCount(); for ( int i = 0; i < nlayers; i++ ) { QgsMapLayer *layer = canvas->getZpos(i); QgsDebugMsg("layer->type() = " + QString::number(layer->type()) ); if ( mType == Vector && layer->type() == QgsMapLayer::VECTOR ) { QgsVectorLayer *vector = (QgsVectorLayer*)layer; QgsDebugMsg("vector->providerType() = " + vector->providerType()); if ( vector->providerType() != "grass" ) continue; //TODO dynamic_cast ? QgsGrassProvider *provider = (QgsGrassProvider *) vector->getDataProvider(); // Check type mask int geomType = provider->geometryType(); if ( (geomType == QGis::WKBPoint && !(mVectorTypeMask & GV_POINT) ) || (geomType == QGis::WKBLineString && !(mVectorTypeMask & GV_LINE) ) || (geomType == QGis::WKBPolygon && !(mVectorTypeMask & GV_AREA) ) ) { continue; } // TODO add map() mapset() location() gisbase() to grass provider QString source = QDir::cleanPath ( provider->dataSourceUri() ); QgsDebugMsg("source = " + source); // Check GISBASE and LOCATION QStringList split = QStringList::split ( sep, source ); if ( split.size() < 4 ) continue; split.pop_back(); // layer QString map = split.last(); split.pop_back(); // map QString mapset = split.last(); split.pop_back(); // mapset //QDir locDir ( sep + split.join ( QString(sep) ) ) ; //QString loc = locDir.canonicalPath(); QString loc = source.remove ( QRegExp("/[^/]+/[^/]+/[^/]+$") ); loc = QDir(loc).canonicalPath(); QDir curlocDir ( QgsGrass::getDefaultGisdbase() + sep + QgsGrass::getDefaultLocation() ); QString curloc = curlocDir.canonicalPath(); QgsDebugMsg("loc = " + loc); QgsDebugMsg("curloc = " + curloc); QgsDebugMsg("mapset = " + mapset); QgsDebugMsg("QgsGrass::getDefaultMapset() = " + QgsGrass::getDefaultMapset()); if ( loc != curloc ) continue; if ( mUpdate && mapset != QgsGrass::getDefaultMapset() ) continue; // Check if it comes from source map if necessary if ( !mMapId.isEmpty() ) { QString cm = map + "@" + mapset; if ( sourceMap != cm ) continue; } mMaps.push_back ( map + "@" + mapset ); QString type; if ( geomType == QGis::WKBPoint ) { type = "point"; } else if ( geomType == QGis::WKBLineString ) { type = "line"; } else if ( geomType == QGis::WKBPolygon ) { type = "area"; } else { type = "unknown"; } mVectorTypes.push_back ( type ); QString grassLayer = QString::number(provider->grassLayer()); QString label = layer->name() + " ( " + map + "@" + mapset + " " + grassLayer + " " + type + " )"; mLayerComboBox->insertItem( label ); if ( label == current ) mLayerComboBox->setCurrentText ( current ); mMapLayers.push_back ( vector ); mVectorLayerNames.push_back ( grassLayer ); // convert from QgsFieldMap to std::vector<QgsField> QgsFieldMap flds = vector->getDataProvider()->fields(); std::vector<QgsField> fields; for (QgsFieldMap::iterator it = flds.begin(); it != flds.end(); ++it) fields.push_back(it.value()); mVectorFields.push_back ( fields ); } else if ( mType == Raster && layer->type() == QgsMapLayer::RASTER ) { // Check if it is GRASS raster QString source = QDir::cleanDirPath ( layer->source() ); if ( source.contains( "cellhd" ) == 0 ) continue; // Most probably GRASS layer, check GISBASE and LOCATION QStringList split = QStringList::split ( sep, source ); if ( split.size() < 4 ) continue; QString map = split.last(); split.pop_back(); // map if ( split.last() != "cellhd" ) continue; split.pop_back(); // cellhd QString mapset = split.last(); split.pop_back(); // mapset //QDir locDir ( sep + split.join ( QString(sep) ) ) ; //QString loc = locDir.canonicalPath(); QString loc = source.remove ( QRegExp("/[^/]+/[^/]+/[^/]+$") ); loc = QDir(loc).canonicalPath(); QDir curlocDir ( QgsGrass::getDefaultGisdbase() + sep + QgsGrass::getDefaultLocation() ); QString curloc = curlocDir.canonicalPath(); if ( loc != curloc ) continue; if ( mUpdate && mapset != QgsGrass::getDefaultMapset() ) continue; mMaps.push_back ( map + "@" + mapset ); QString label = layer->name() + " ( " + map + "@" + mapset + " )"; mLayerComboBox->insertItem( label ); if ( label == current ) mLayerComboBox->setCurrentText ( current ); } }}QStringList QgsGrassModuleInput::options(){ QStringList list; QString opt; int c = mLayerComboBox->currentItem(); if (c < 0) // not found return list; unsigned current = c; // TODO: this is hack for network nodes, do it somehow better if ( mMapId.isEmpty() ) { opt = mKey + "="; if ( current < mMaps.size() ) { opt.append ( mMaps[current] ); } list.push_back( opt ); } if ( !mVectorTypeOpti
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -