Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some (micro) optimizations of feature form #5647

Merged
merged 2 commits into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 55 additions & 28 deletions src/core/attributeformmodelbase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "attributeformmodel.h"
#include "attributeformmodelbase.h"

#include <QDirIterator>
#include <QRegularExpression>
#include <qgsattributeeditorelement.h>
#include <qgsattributeeditorfield.h>
Expand All @@ -34,13 +35,31 @@
#include <qgsvectorlayer.h>
#include <qgsvectorlayerutils.h>

Q_GLOBAL_STATIC( QStringList, sSupportedEditorWidgets );

AttributeFormModelBase::AttributeFormModelBase( QObject *parent )
: QStandardItemModel( 0, 1, parent )
{
connect( QgsProject::instance(), &QgsProject::mapThemeCollectionChanged, this, &AttributeFormModelBase::onMapThemeCollectionChanged );
if ( QgsProject::instance()->mapThemeCollection() )
{
onMapThemeCollectionChanged();
}

if ( sSupportedEditorWidgets->isEmpty() )
{
QDirIterator it( ":qml/editorwidgets" );
while ( it.hasNext() )
{
it.next();
const QFileInfo fileInfo = it.fileInfo();
if ( fileInfo.isFile() )
{
sSupportedEditorWidgets->append( fileInfo.baseName() );
}
}
sSupportedEditorWidgets->append( QStringLiteral( "RelationEditor" ) );
}
}

void AttributeFormModelBase::onMapThemeCollectionChanged()
Expand Down Expand Up @@ -964,60 +983,68 @@ void AttributeFormModelBase::setConstraintsSoftValid( bool constraintsSoftValid
QgsEditorWidgetSetup AttributeFormModelBase::findBest( const int fieldIndex )
{
QgsFields fields = mLayer->fields();

//make the default one
QgsEditorWidgetSetup setup = QgsEditorWidgetSetup( QStringLiteral( "TextEdit" ), QVariantMap() );

if ( fieldIndex >= 0 && fieldIndex < fields.count() )
{
//when field has a configured setup, take it
setup = mLayer->editorWidgetSetup( fieldIndex );
if ( !setup.isNull() )
return setup;
QgsEditorWidgetSetup configuredSetup = mLayer->editorWidgetSetup( fieldIndex );
if ( !configuredSetup.isNull() )
{
if ( !sSupportedEditorWidgets->contains( configuredSetup.type() ) )
{
return QgsEditorWidgetSetup( QStringLiteral( "TextEdit" ), QVariantMap() );
}
return configuredSetup;
nirvn marked this conversation as resolved.
Show resolved Hide resolved
}

//when it's a provider field with default value clause, take Textedit
//when it's a provider field with default value clause, take Textedit
#if _QGIS_VERSION_INT >= 33800
if ( fields.fieldOrigin( fieldIndex ) == Qgis::FieldOrigin::Provider )
#else
if ( fields.fieldOrigin( fieldIndex ) == QgsFields::OriginProvider )
#endif
{
int providerOrigin = fields.fieldOriginIndex( fieldIndex );
const int providerOrigin = fields.fieldOriginIndex( fieldIndex );
if ( !mLayer->dataProvider()->defaultValueClause( providerOrigin ).isEmpty() )
return setup;
{
return QgsEditorWidgetSetup( QStringLiteral( "TextEdit" ), QVariantMap() );
}
}

//find the best one
if ( !mLayer->referencingRelations( fieldIndex ).isEmpty() )
{
QgsRelation relation = mLayer->referencingRelations( fieldIndex )[0];
QVariantMap config;
config.insert( QStringLiteral( "Relation" ), relation.id() );
config.insert( QStringLiteral( "AllowAddFeatures" ), false );
config.insert( QStringLiteral( "ShowOpenFormButton" ), true );
return QgsEditorWidgetSetup( QStringLiteral( "RelationReference" ), config );
}

// Find the best one based on field type
const QgsField field = fields.at( fieldIndex );
//on a boolean type take "CheckBox"
if ( field.type() == QMetaType::Bool )
setup = QgsEditorWidgetSetup( QStringLiteral( "CheckBox" ), QVariantMap() );
//on a date or time type take "DateTime"
if ( field.isDateOrTime() )
{
// on a boolean type, take "CheckBox"
return QgsEditorWidgetSetup( QStringLiteral( "CheckBox" ), QVariantMap() );
}
else if ( field.isDateOrTime() )
{
// on a time types, take "DateTime"
QVariantMap config;
config.insert( QStringLiteral( "field_format" ), QgsDateTimeFieldFormatter::defaultFormat( field.type() ) );
config.insert( QStringLiteral( "display_format" ), QgsDateTimeFieldFormatter::defaultFormat( field.type() ) );
config.insert( QStringLiteral( "calendar_popup" ), true );
config.insert( QStringLiteral( "allow_null" ), true );
setup = QgsEditorWidgetSetup( QStringLiteral( "DateTime" ), config );
return QgsEditorWidgetSetup( QStringLiteral( "DateTime" ), config );
}
//on numeric types take "Range"
if ( field.type() == QMetaType::Int || field.type() == QMetaType::Double || field.isNumeric() )
setup = QgsEditorWidgetSetup( QStringLiteral( "Range" ), QVariantMap() );
//if it's a foreign key configured in a relation take "RelationReference"
if ( !mLayer->referencingRelations( fieldIndex ).isEmpty() )
else if ( field.type() == QMetaType::Int || field.type() == QMetaType::Double || field.isNumeric() )
{
QgsRelation relation = mLayer->referencingRelations( fieldIndex )[0];
QVariantMap config;
config.insert( QStringLiteral( "Relation" ), relation.id() );
config.insert( QStringLiteral( "AllowAddFeatures" ), false );
config.insert( QStringLiteral( "ShowOpenFormButton" ), true );
setup = QgsEditorWidgetSetup( QStringLiteral( "RelationReference" ), config );
// on numeric types, take "Range"
return QgsEditorWidgetSetup( QStringLiteral( "Range" ), QVariantMap() );
}
}

return setup;
return QgsEditorWidgetSetup( QStringLiteral( "TextEdit" ), QVariantMap() );
}

bool AttributeFormModelBase::hasTabs() const
Expand Down
2 changes: 1 addition & 1 deletion src/qml/FeatureForm.qml
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ Page {
property string itemType: Type

active: (Type === 'container' && GroupIndex !== undefined && GroupIndex.valid) || ((Type === 'text' || Type === 'html' || Type === 'qml') && form.model.featureModel.modelMode != FeatureModel.MultiFeatureModel)
height: active ? item.childrenRect.height : 0
height: status == Loader.Ready ? item.childrenRect.height : 0
anchors {
left: parent.left
right: parent.right
Expand Down
Loading