void QgsOverlayAnalyzer::combineFieldLists( QgsFieldMap& fieldListA, QgsFieldMap fieldListB ) { QList<QString> names; QMap<int, QgsField>::const_iterator j = fieldListA.constBegin(); while ( j != fieldListA.constEnd() ) { names.append( j.value().name() ); ++j; } QMap<int, QgsField>::const_iterator i = fieldListB.constBegin(); int count = 0; int fcount = fieldListA.size(); QgsField field; while ( i != fieldListB.constEnd() ) { field = i.value(); while ( names.contains( field.name() ) ) { QString name = field.name(); name.append( "_" ).append( QString( count ) ); field = QgsField( name, field.type() ); ++count; } fieldListA.insert( fcount, field ); count = 0; ++fcount; ++i; } }
bool QgsZonalStatisticsDialog::prefixIsValid( const QString& prefix ) const { QgsVectorLayer* vl = polygonLayer(); if ( !vl ) { return false; } QgsVectorDataProvider* dp = vl->dataProvider(); if ( !dp ) { return false; } QgsFieldMap providerFieldMap = dp->fields(); QgsFieldMap::const_iterator it = providerFieldMap.constBegin(); QString currentFieldName; for ( ; it != providerFieldMap.constEnd(); ++it ) { currentFieldName = it.value().name(); if ( currentFieldName == ( prefix + "mean" ) || currentFieldName == ( prefix + "sum" ) || currentFieldName == ( prefix + "count" ) ) { return false; } } return true; }
QgsDelAttrDialog::QgsDelAttrDialog( const QgsVectorLayer* vl ): QDialog() { setupUi( this ); if ( vl ) { listBox2->clear(); const QgsFieldMap layerAttributes = vl->pendingFields(); QgsFieldMap::const_iterator attIt = layerAttributes.constBegin(); for ( ; attIt != layerAttributes.constEnd(); ++attIt ) { QListWidgetItem* item = new QListWidgetItem( attIt.value().name(), listBox2 ); item->setData( Qt::UserRole, attIt.key() ); } } }
void QgsFieldCalculator::populateFields() { if ( !mVectorLayer ) return; const QgsFieldMap fieldMap = mVectorLayer->pendingFields(); QgsFieldMap::const_iterator fieldIt = fieldMap.constBegin(); for ( ; fieldIt != fieldMap.constEnd(); ++fieldIt ) { QString fieldName = fieldIt.value().name(); //insert into field list and field combo box mFieldMap.insert( fieldName, fieldIt.key() ); mExistingFieldComboBox->addItem( fieldName ); } }
void QgsComposerAttributeTable::initializeAliasMap() { mFieldAliasMap.clear(); if ( mVectorLayer ) { QgsFieldMap fieldMap = mVectorLayer->pendingFields(); QgsFieldMap::const_iterator it = fieldMap.constBegin(); for ( ; it != fieldMap.constEnd(); ++it ) { QString currentAlias = mVectorLayer->attributeAlias( it.key() ); if ( !currentAlias.isEmpty() ) { mFieldAliasMap.insert( it.key(), currentAlias ); } } } }
QMap<int, QString> QgsComposerAttributeTable::getHeaderLabels() const { QMap<int, QString> header; if ( mVectorLayer ) { QgsFieldMap vectorFields = mVectorLayer->pendingFields(); QgsFieldMap::const_iterator fieldIt = vectorFields.constBegin(); for ( ; fieldIt != vectorFields.constEnd(); ++fieldIt ) { if ( mDisplayAttributes.size() > 0 && !mDisplayAttributes.contains( fieldIt.key() ) ) { continue; } header.insert( fieldIt.key(), attributeDisplayName( fieldIt.key(), fieldIt.value().name() ) ); } } return header; }
void QgsFieldCalculator::accept() { // Set up QgsDistanceArea each time we (re-)calculate QgsDistanceArea myDa; myDa.setSourceCrs( mVectorLayer->crs().srsid() ); myDa.setEllipsoidalMode( QgisApp::instance()->mapCanvas()->mapRenderer()->hasCrsTransformEnabled() ); myDa.setEllipsoid( QgsProject::instance()->readEntry( "Measure", "/Ellipsoid", GEO_NONE ) ); QString calcString = builder->expressionText(); QgsExpression exp( calcString ); if ( !mVectorLayer || !mVectorLayer->isEditable() ) return; if ( ! exp.prepare( mVectorLayer->pendingFields() ) ) { QMessageBox::critical( 0, tr( "Evaluation error" ), exp.evalErrorString() ); return; } mVectorLayer->beginEditCommand( "Field calculator" ); //update existing field if ( mUpdateExistingGroupBox->isChecked() || !mNewFieldGroupBox->isEnabled() ) { QMap<QString, int>::const_iterator fieldIt = mFieldMap.find( mExistingFieldComboBox->currentText() ); if ( fieldIt != mFieldMap.end() ) { mAttributeId = fieldIt.value(); } } else { //create new field QgsField newField( mOutputFieldNameLineEdit->text(), ( QVariant::Type ) mOutputFieldTypeComboBox->itemData( mOutputFieldTypeComboBox->currentIndex(), Qt::UserRole ).toInt(), mOutputFieldTypeComboBox->itemData( mOutputFieldTypeComboBox->currentIndex(), Qt::UserRole + 1 ).toString(), mOutputFieldWidthSpinBox->value(), mOutputFieldPrecisionSpinBox->value() ); if ( !mVectorLayer->addAttribute( newField ) ) { QMessageBox::critical( 0, tr( "Provider error" ), tr( "Could not add the new field to the provider." ) ); mVectorLayer->destroyEditCommand(); return; } //get index of the new field const QgsFieldMap fieldList = mVectorLayer->pendingFields(); QgsFieldMap::const_iterator it = fieldList.constBegin(); for ( ; it != fieldList.constEnd(); ++it ) { if ( it.value().name() == mOutputFieldNameLineEdit->text() ) { mAttributeId = it.key(); break; } } } if ( mAttributeId == -1 ) { mVectorLayer->destroyEditCommand(); return; } //go through all the features and change the new attribute QgsFeature feature; bool calculationSuccess = true; QString error; bool onlySelected = mOnlyUpdateSelectedCheckBox->isChecked(); QgsFeatureIds selectedIds = mVectorLayer->selectedFeaturesIds(); // block layerModified signals (that would trigger table update) mVectorLayer->blockSignals( true ); bool useGeometry = exp.needsGeometry(); int rownum = 1; mVectorLayer->select( mVectorLayer->pendingAllAttributesList(), QgsRectangle(), useGeometry, false ); while ( mVectorLayer->nextFeature( feature ) ) { if ( onlySelected ) { if ( !selectedIds.contains( feature.id() ) ) { continue; } } exp.setCurrentRowNumber( rownum ); exp.setGeomCalculator( myDa ); QVariant value = exp.evaluate( &feature ); if ( exp.hasEvalError() ) { calculationSuccess = false; error = exp.evalErrorString(); break; } else { mVectorLayer->changeAttributeValue( feature.id(), mAttributeId, value, false ); } rownum++; } // stop blocking layerModified signals and make sure that one layerModified signal is emitted mVectorLayer->blockSignals( false ); mVectorLayer->setModified( true, false ); if ( !calculationSuccess ) { QMessageBox::critical( 0, tr( "Error" ), tr( "An error occured while evaluating the calculation string:\n%1" ).arg( error ) ); mVectorLayer->destroyEditCommand(); return; } mVectorLayer->endEditCommand(); QDialog::accept(); }
bool QgsFeatureAction::addFeature() { if ( !mLayer || !mLayer->isEditable() ) return false; QgsVectorDataProvider *provider = mLayer->dataProvider(); QSettings settings; bool reuseLastValues = settings.value( "/qgis/digitizing/reuseLastValues", false ).toBool(); QgsDebugMsg( QString( "reuseLastValues: %1" ).arg( reuseLastValues ) ); // add the fields to the QgsFeature const QgsFieldMap fields = mLayer->pendingFields(); for ( QgsFieldMap::const_iterator it = fields.constBegin(); it != fields.constEnd(); ++it ) { if ( reuseLastValues && mLastUsedValues.contains( mLayer ) && mLastUsedValues[ mLayer ].contains( it.key() ) ) { QgsDebugMsg( QString( "reusing %1 for %2" ).arg( mLastUsedValues[ mLayer ][ it.key()].toString() ).arg( it.key() ) ); mFeature.addAttribute( it.key(), mLastUsedValues[ mLayer ][ it.key()] ); } else { mFeature.addAttribute( it.key(), provider->defaultValue( it.key() ) ); } } bool res = false; mLayer->beginEditCommand( text() ); // show the dialog to enter attribute values bool isDisabledAttributeValuesDlg = settings.value( "/qgis/digitizing/disable_enter_attribute_values_dialog", false ).toBool(); if ( isDisabledAttributeValuesDlg ) { res = mLayer->addFeature( mFeature ); } else { QgsAttributeMap origValues; if ( reuseLastValues ) origValues = mFeature.attributeMap(); QgsAttributeDialog *dialog = newDialog( false ); if ( dialog->exec() ) { if ( reuseLastValues ) { for ( QgsFieldMap::const_iterator it = fields.constBegin(); it != fields.constEnd(); ++it ) { const QgsAttributeMap &newValues = mFeature.attributeMap(); if ( newValues.contains( it.key() ) && origValues.contains( it.key() ) && origValues[ it.key()] != newValues[ it.key()] ) { QgsDebugMsg( QString( "saving %1 for %2" ).arg( mLastUsedValues[ mLayer ][ it.key()].toString() ).arg( it.key() ) ); mLastUsedValues[ mLayer ][ it.key()] = newValues[ it.key()]; } } } res = mLayer->addFeature( mFeature ); } else { QgsDebugMsg( "Adding feature to layer failed" ); res = false; } } if ( res ) mLayer->endEditCommand(); else mLayer->destroyEditCommand(); return res; }
QgsPointDisplacementRendererWidget::QgsPointDisplacementRendererWidget( QgsVectorLayer* layer, QgsStyleV2* style, QgsFeatureRendererV2* renderer ): \ QgsRendererV2Widget( layer, style ), mEmbeddedRendererWidget( 0 ) { if ( !layer ) { return; } //the renderer only applies to point vector layers if ( layer->wkbType() != QGis::WKBPoint && layer->wkbType() != QGis::WKBPoint25D ) { //setup blank dialog mRenderer = 0; setupBlankUi( layer->name() ); return; } setupUi( this ); if ( renderer && renderer->type() == "pointDisplacement" ) { mRenderer = dynamic_cast<QgsPointDisplacementRenderer*>( renderer->clone() ); } else { mRenderer = new QgsPointDisplacementRenderer(); } blockAllSignals( true ); //insert attributes into combo box if ( layer ) { const QgsFieldMap layerAttributes = layer->pendingFields(); QgsFieldMap::const_iterator it = layerAttributes.constBegin(); for ( ; it != layerAttributes.constEnd(); ++it ) { mLabelFieldComboBox->addItem( it.value().name() ); } mLabelFieldComboBox->addItem( tr( "None" ) ); QString currentLabelAttribute = mRenderer->labelAttributeName(); if ( !currentLabelAttribute.isEmpty() ) { mLabelFieldComboBox->setCurrentIndex( mLabelFieldComboBox->findText( currentLabelAttribute ) ); } else { mLabelFieldComboBox->setCurrentIndex( mLabelFieldComboBox->findText( tr( "None" ) ) ); } } //insert possible renderer types QStringList rendererList = QgsRendererV2Registry::instance()->renderersList(); QStringList::const_iterator it = rendererList.constBegin(); for ( ; it != rendererList.constEnd(); ++it ) { if ( *it != "pointDisplacement" ) { QgsRendererV2AbstractMetadata* m = QgsRendererV2Registry::instance()->rendererMetadata( *it ); mRendererComboBox->addItem( m->icon(), m->visibleName(), *it ); } } mCircleWidthSpinBox->setValue( mRenderer->circleWidth() ); mCircleColorButton->setColor( mRenderer->circleColor() ); mLabelColorButton->setColor( mRenderer->labelColor() ); mCircleModificationSpinBox->setValue( mRenderer->circleRadiusAddition() ); mDistanceSpinBox->setValue( mRenderer->tolerance() ); //scale dependent labelling mMaxScaleDenominatorEdit->setText( QString::number( mRenderer->maxLabelScaleDenominator() ) ); mMaxScaleDenominatorEdit->setValidator( new QDoubleValidator( mMaxScaleDenominatorEdit ) ); if ( mRenderer->maxLabelScaleDenominator() > 0 ) { mScaleDependentLabelsCheckBox->setCheckState( Qt::Checked ); } else { mScaleDependentLabelsCheckBox->setCheckState( Qt::Unchecked ); mMaxScaleDenominatorEdit->setEnabled( false ); } blockAllSignals( false ); //set the appropriate renderer dialog if ( mRenderer && mRenderer->embeddedRenderer() ) { QString rendererName = mRenderer->embeddedRenderer()->type(); int rendererIndex = mRendererComboBox->findData( rendererName ); if ( rendererIndex != -1 ) { mRendererComboBox->setCurrentIndex( rendererIndex ); on_mRendererComboBox_currentIndexChanged( rendererIndex ); } } updateCenterIcon(); }
void QgsFieldCalculator::accept() { if ( mVectorLayer && mVectorLayer->isEditable() ) { QString calcString = mExpressionTextEdit->toPlainText(); //create QgsExpression QgsExpression exp( calcString ); if ( exp.hasParserError() ) { //expression not valid QMessageBox::critical( 0, tr( "Syntax error" ), tr( QString( "Invalid expression syntax. The error message of the parser is: '" + exp.parserErrorString() + "'" ).toLocal8Bit().data() ) ); return; } if ( ! exp.prepare( mVectorLayer->pendingFields() ) ) { QMessageBox::critical( 0, tr( "Evaluation error" ), exp.evalErrorString() ); return; } mVectorLayer->beginEditCommand( "Field calculator" ); //update existing field if ( mUpdateExistingFieldCheckBox->checkState() == Qt::Checked ) { QMap<QString, int>::const_iterator fieldIt = mFieldMap.find( mExistingFieldComboBox->currentText() ); if ( fieldIt != mFieldMap.end() ) { mAttributeId = fieldIt.value(); } } //create new field else { //create new field QgsField newField( mOutputFieldNameLineEdit->text(), ( QVariant::Type ) mOutputFieldTypeComboBox->itemData( mOutputFieldTypeComboBox->currentIndex(), Qt::UserRole ).toInt(), mOutputFieldTypeComboBox->itemData( mOutputFieldTypeComboBox->currentIndex(), Qt::UserRole + 1 ).toString(), mOuputFieldWidthSpinBox->value(), mOutputFieldPrecisionSpinBox->value() ); if ( !mVectorLayer->addAttribute( newField ) ) { QMessageBox::critical( 0, tr( "Provider error" ), tr( "Could not add the new field to the provider." ) ); mVectorLayer->destroyEditCommand(); return; } //get index of the new field const QgsFieldMap fieldList = mVectorLayer->pendingFields(); QgsFieldMap::const_iterator it = fieldList.constBegin(); for ( ; it != fieldList.constEnd(); ++it ) { if ( it.value().name() == mOutputFieldNameLineEdit->text() ) { mAttributeId = it.key(); break; } } } if ( mAttributeId == -1 ) { mVectorLayer->destroyEditCommand(); return; } //go through all the features and change the new attribute QgsFeature feature; bool calculationSuccess = true; QString error; bool onlySelected = ( mOnlyUpdateSelectedCheckBox->checkState() == Qt::Checked ); QgsFeatureIds selectedIds = mVectorLayer->selectedFeaturesIds(); // block layerModified signals (that would trigger table update) mVectorLayer->blockSignals( true ); bool useGeometry = exp.needsGeometry(); int rownum = 1; mVectorLayer->select( mVectorLayer->pendingAllAttributesList(), QgsRectangle(), useGeometry, false ); while ( mVectorLayer->nextFeature( feature ) ) { if ( onlySelected ) { if ( !selectedIds.contains( feature.id() ) ) { continue; } } exp.setCurrentRowNumber( rownum ); QVariant value = exp.evaluate( &feature ); if ( exp.hasEvalError() ) { calculationSuccess = false; error = exp.evalErrorString(); break; } else { mVectorLayer->changeAttributeValue( feature.id(), mAttributeId, value, false ); } rownum++; } // stop blocking layerModified signals and make sure that one layerModified signal is emitted mVectorLayer->blockSignals( false ); mVectorLayer->setModified( true, false ); if ( !calculationSuccess ) { QMessageBox::critical( 0, tr( "Error" ), tr( "An error occured while evaluating the calculation string:\n%1" ).arg( error ) ); mVectorLayer->destroyEditCommand(); return; } mVectorLayer->endEditCommand(); } QDialog::accept(); }