bool QgsFieldExpressionWidget::isExpressionValid( const QString expressionStr ) { QgsVectorLayer* vl = layer(); QgsExpression expression( expressionStr ); expression.prepare( vl ? vl->pendingFields() : QgsFields() ); return !expression.hasParserError(); }
void QgsFieldModel::updateModel() { beginResetModel(); mExpression = QList<QString>(); if ( mLayer ) mFields = mLayer->pendingFields(); else mFields = QgsFields(); endResetModel(); }
QgsVectorLayer *QgsNewMemoryLayerDialog::runAndCreateLayer( QWidget *parent, const QgsCoordinateReferenceSystem &defaultCrs ) { QgsNewMemoryLayerDialog dialog( parent ); dialog.setCrs( defaultCrs ); if ( dialog.exec() == QDialog::Rejected ) { return nullptr; } QgsWkbTypes::Type geometrytype = dialog.selectedType(); QString name = dialog.layerName().isEmpty() ? tr( "New scratch layer" ) : dialog.layerName(); QgsVectorLayer *newLayer = QgsMemoryProviderUtils::createMemoryLayer( name, QgsFields(), geometrytype, dialog.crs() ); return newLayer; }
void QgsWFSFeatureHitsAsyncRequest::hitsReplyFinished() { if ( mErrorCode == NoError ) { QByteArray data = response(); QgsGmlStreamingParser gmlParser( QLatin1String( "" ), QLatin1String( "" ), QgsFields() ); QString errorMsg; if ( gmlParser.processData( data, true, errorMsg ) ) { mNumberMatched = ( gmlParser.numberMatched() >= 0 ) ? gmlParser.numberMatched() : gmlParser.numberReturned(); } else { QgsMessageLog::logMessage( errorMsg, tr( "WFS" ) ); } } emit gotHitsResponse(); }
void QgsFieldModel::updateModel() { if ( mLayer ) { if ( mFields.toList() != mLayer->pendingFields().toList() ) { beginResetModel(); mFields = mLayer->pendingFields(); endResetModel(); } else emit dataChanged( index( 0, 0 ), index( rowCount(), 0 ) ); } else { beginResetModel(); mFields = QgsFields(); endResetModel(); } }
void QgsFieldModel::updateModel() { if ( mLayer ) { QgsFields newFields = mLayer->fields(); if ( mFields.toList() != newFields.toList() ) { // Try to handle two special cases: addition of a new field and removal of a field. // It would be better to listen directly to attributeAdded/attributeDeleted // so we would not have to check for addition/removal here. if ( mFields.count() == newFields.count() - 1 ) { QgsFields tmpNewFields = newFields; tmpNewFields.remove( tmpNewFields.count() - 1 ); if ( mFields.toList() == tmpNewFields.toList() ) { // the only change is a new field at the end beginInsertRows( QModelIndex(), mFields.count(), mFields.count() ); mFields = newFields; endInsertRows(); return; } } if ( mFields.count() == newFields.count() + 1 ) { QgsFields tmpOldFields = mFields; tmpOldFields.remove( tmpOldFields.count() - 1 ); if ( tmpOldFields.toList() == newFields.toList() ) { // the only change is a field removed at the end beginRemoveRows( QModelIndex(), mFields.count() - 1, mFields.count() - 1 ); mFields = newFields; endRemoveRows(); return; } for ( int i = 0; i < newFields.count(); ++i ) { if ( mFields.at( i ) != newFields.at( i ) ) { QgsFields tmpOldFields = mFields; tmpOldFields.remove( i ); if ( tmpOldFields.toList() != newFields.toList() ) break; // the change is more complex - go with general case // the only change is a field removed at index i beginRemoveRows( QModelIndex(), i, i ); mFields = newFields; endRemoveRows(); return; } } } // general case with reset - not good - resets selections beginResetModel(); mFields = mLayer->fields(); endResetModel(); } else emit dataChanged( index( 0, 0 ), index( rowCount(), 0 ) ); } else { beginResetModel(); mFields = QgsFields(); endResetModel(); } }
QVariant QgsFieldModel::data( const QModelIndex &index, int role ) const { if ( !index.isValid() ) return QVariant(); int exprIdx = index.row() - mFields.count(); switch ( role ) { case FieldNameRole: { if ( exprIdx >= 0 ) { return ""; } QgsField field = mFields[index.row()]; return field.name(); } case ExpressionRole: { if ( exprIdx >= 0 ) { return mExpression[exprIdx]; } else { QgsField field = mFields[index.row()]; return field.name(); } } case FieldIndexRole: { if ( exprIdx >= 0 ) { return QVariant(); } return index.row(); } case IsExpressionRole: { return exprIdx >= 0; } case ExpressionValidityRole: { if ( exprIdx >= 0 ) { QgsExpression exp( mExpression[exprIdx] ); exp.prepare( mLayer ? mLayer->pendingFields() : QgsFields() ); return !exp.hasParserError(); } return true; } case FieldTypeRole: { if ( exprIdx < 0 ) { QgsField field = mFields[index.row()]; return ( int )field.type(); } return QVariant(); } case Qt::DisplayRole: case Qt::EditRole: { if ( exprIdx >= 0 ) { return mExpression[exprIdx]; } else if ( role == Qt::EditRole ) { return mFields[index.row()].name(); } else if ( mLayer ) { return mLayer->attributeDisplayName( index.row() ); } else return QVariant(); } case Qt::ForegroundRole: { if ( exprIdx >= 0 ) { // if expression, test validity QgsExpression exp( mExpression[exprIdx] ); exp.prepare( mLayer ? mLayer->pendingFields() : QgsFields() ); if ( exp.hasParserError() ) { return QBrush( QColor( Qt::red ) ); } } return QVariant(); } case Qt::FontRole: { if ( exprIdx >= 0 ) { // if the line is an expression, set it as italic QFont font = QFont(); font.setItalic( true ); return font; } return QVariant(); } default: return QVariant(); } }
QgsVectorLayerImport::ImportError QgsVectorLayerImport::importLayer( QgsVectorLayer* layer, const QString& uri, const QString& providerKey, const QgsCoordinateReferenceSystem *destCRS, bool onlySelected, QString *errorMessage, bool skipAttributeCreation, QMap<QString, QVariant> *options, QProgressDialog *progress ) { const QgsCoordinateReferenceSystem* outputCRS; QgsCoordinateTransform* ct = nullptr; bool shallTransform = false; if ( !layer ) return ErrInvalidLayer; if ( destCRS && destCRS->isValid() ) { // This means we should transform outputCRS = destCRS; shallTransform = true; } else { // This means we shouldn't transform, use source CRS as output (if defined) outputCRS = &layer->crs(); } bool overwrite = false; bool forceSinglePartGeom = false; if ( options ) { overwrite = options->take( "overwrite" ).toBool(); forceSinglePartGeom = options->take( "forceSinglePartGeometryType" ).toBool(); } QgsFields fields = skipAttributeCreation ? QgsFields() : layer->fields(); QGis::WkbType wkbType = layer->wkbType(); // Special handling for Shapefiles if ( layer->providerType() == "ogr" && layer->storageType() == "ESRI Shapefile" ) { // convert field names to lowercase for ( int fldIdx = 0; fldIdx < fields.count(); ++fldIdx ) { fields[fldIdx].setName( fields.at( fldIdx ).name().toLower() ); } if ( !forceSinglePartGeom ) { // convert wkbtype to multipart (see #5547) switch ( wkbType ) { case QGis::WKBPoint: wkbType = QGis::WKBMultiPoint; break; case QGis::WKBLineString: wkbType = QGis::WKBMultiLineString; break; case QGis::WKBPolygon: wkbType = QGis::WKBMultiPolygon; break; case QGis::WKBPoint25D: wkbType = QGis::WKBMultiPoint25D; break; case QGis::WKBLineString25D: wkbType = QGis::WKBMultiLineString25D; break; case QGis::WKBPolygon25D: wkbType = QGis::WKBMultiPolygon25D; break; default: break; } } } QgsVectorLayerImport * writer = new QgsVectorLayerImport( uri, providerKey, fields, wkbType, outputCRS, overwrite, options, progress ); // check whether file creation was successful ImportError err = writer->hasError(); if ( err != NoError ) { if ( errorMessage ) *errorMessage = writer->errorMessage(); delete writer; return err; } if ( errorMessage ) { errorMessage->clear(); } QgsAttributeList allAttr = skipAttributeCreation ? QgsAttributeList() : layer->attributeList(); QgsFeature fet; QgsFeatureRequest req; if ( wkbType == QGis::WKBNoGeometry ) req.setFlags( QgsFeatureRequest::NoGeometry ); if ( skipAttributeCreation ) req.setSubsetOfAttributes( QgsAttributeList() ); QgsFeatureIterator fit = layer->getFeatures( req ); const QgsFeatureIds& ids = layer->selectedFeaturesIds(); // Create our transform if ( destCRS ) ct = new QgsCoordinateTransform( layer->crs(), *destCRS ); // Check for failure if ( !ct ) shallTransform = false; int n = 0; if ( errorMessage ) { *errorMessage = QObject::tr( "Feature write errors:" ); } if ( progress ) { progress->setRange( 0, layer->featureCount() ); } // write all features while ( fit.nextFeature( fet ) ) { if ( progress && progress->wasCanceled() ) { if ( errorMessage ) { *errorMessage += '\n' + QObject::tr( "Import was canceled at %1 of %2" ).arg( progress->value() ).arg( progress->maximum() ); } break; } if ( writer->errorCount() > 1000 ) { if ( errorMessage ) { *errorMessage += '\n' + QObject::tr( "Stopping after %1 errors" ).arg( writer->errorCount() ); } break; } if ( onlySelected && !ids.contains( fet.id() ) ) continue; if ( shallTransform ) { try { if ( fet.constGeometry() ) { fet.geometry()->transform( *ct ); } } catch ( QgsCsException &e ) { delete ct; delete writer; QString msg = QObject::tr( "Failed to transform a point while drawing a feature with ID '%1'. Writing stopped. (Exception: %2)" ) .arg( fet.id() ).arg( e.what() ); QgsMessageLog::logMessage( msg, QObject::tr( "Vector import" ) ); if ( errorMessage ) *errorMessage += '\n' + msg; return ErrProjection; } } if ( skipAttributeCreation ) { fet.initAttributes( 0 ); } if ( !writer->addFeature( fet ) ) { if ( writer->hasError() && errorMessage ) { *errorMessage += '\n' + writer->errorMessage(); } } n++; if ( progress ) { progress->setValue( n ); } } // flush the buffer to be sure that all features are written if ( !writer->flushBuffer() ) { if ( writer->hasError() && errorMessage ) { *errorMessage += '\n' + writer->errorMessage(); } } int errors = writer->errorCount(); if ( !writer->createSpatialIndex() ) { if ( writer->hasError() && errorMessage ) { *errorMessage += '\n' + writer->errorMessage(); } } delete writer; if ( shallTransform ) { delete ct; } if ( errorMessage ) { if ( errors > 0 ) { *errorMessage += '\n' + QObject::tr( "Only %1 of %2 features written." ).arg( n - errors ).arg( n ); } else { errorMessage->clear(); } } return errors == 0 ? NoError : ErrFeatureWriteFailed; }
QgsSymbolV2* QgsFeatureRendererV2::symbolForFeature( QgsFeature& feature ) { QgsRenderContext context; context.setExpressionContext( QgsExpressionContextUtils::createFeatureBasedContext( feature, QgsFields() ) ); return symbolForFeature( feature, context ); }
bool QgsFieldExpressionWidget::isValidExpression( QString *expressionError ) const { QString temp; QgsVectorLayer* vl = layer(); return QgsExpression::isValid( currentText(), vl ? vl->pendingFields() : QgsFields(), expressionError ? *expressionError : temp ); }
QVariant QgsSymbolLayerV2::evaluateDataDefinedProperty( const QString &property, const QgsFeature* feature, const QVariant& defaultVal, bool *ok ) const { if ( ok ) *ok = false; QgsDataDefined* dd = getDataDefinedProperty( property ); if ( !dd || !dd->isActive() ) return defaultVal; if ( dd->useExpression() ) { if ( dd->expression() ) { QgsExpressionContext context = feature ? QgsExpressionContextUtils::createFeatureBasedContext( *feature, QgsFields() ) : QgsExpressionContext(); QVariant result = dd->expression()->evaluate( &context ); if ( result.isValid() ) { if ( ok ) *ok = true; return result; } else return defaultVal; } else { return defaultVal; } } else if ( feature && !dd->field().isEmpty() && !mFields.isEmpty() ) { int attributeIndex = mFields.fieldNameIndex( dd->field() ); if ( attributeIndex >= 0 ) { if ( ok ) *ok = true; return feature->attribute( attributeIndex ); } } return defaultVal; }