Skip to content

Commit 633c1a3

Browse files
committedAug 19, 2016
Create a default tab layout if none is configured
1 parent 5c20f07 commit 633c1a3

File tree

4 files changed

+44
-20
lines changed

4 files changed

+44
-20
lines changed
 

‎src/core/qgseditformconfig.cpp

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,21 @@ QgsEditorWidgetConfig QgsEditFormConfig::widgetConfig( const QString& widgetName
5353
return d->mWidgetConfigs.value( widgetName );
5454
}
5555

56-
void QgsEditFormConfig::setFields( const QgsFields& fields )
56+
void QgsEditFormConfig::setFields( const QgsFields& fields, const QMap<QString, QString>& attributeAliasMap )
5757
{
5858
d.detach();
5959
d->mFields = fields;
60+
d->mAttributeAliasMap = attributeAliasMap;
61+
62+
if ( !d->mConfiguredRootContainer )
63+
{
64+
d->mInvisibleRootContainer->clear();
65+
for ( int i = 0; i < d->mFields.size(); ++i )
66+
{
67+
QgsAttributeEditorField* field = new QgsAttributeEditorField( d->mAttributeAliasMap.value( d->mFields.at( i ).name(), d->mFields.at( i ).name() ), i, d->mInvisibleRootContainer );
68+
d->mInvisibleRootContainer->addChildElement( field );
69+
}
70+
}
6071
}
6172

6273
void QgsEditFormConfig::onRelationsLoaded()
@@ -411,16 +422,24 @@ void QgsEditFormConfig::readXml( const QDomNode& node )
411422
}
412423

413424
// tabs and groups display info
414-
clearTabs();
415425
QDomNode attributeEditorFormNode = node.namedItem( "attributeEditorForm" );
416-
QDomNodeList attributeEditorFormNodeList = attributeEditorFormNode.toElement().childNodes();
417-
418-
for ( int i = 0; i < attributeEditorFormNodeList.size(); i++ )
426+
if ( !attributeEditorFormNode.isNull() )
419427
{
420-
QDomElement elem = attributeEditorFormNodeList.at( i ).toElement();
428+
QDomNodeList attributeEditorFormNodeList = attributeEditorFormNode.toElement().childNodes();
421429

422-
QgsAttributeEditorElement *attributeEditorWidget = attributeEditorElementFromDomElement( elem, this );
423-
addTab( attributeEditorWidget );
430+
if ( attributeEditorFormNodeList.size() )
431+
{
432+
d->mConfiguredRootContainer = true;
433+
clearTabs();
434+
435+
for ( int i = 0; i < attributeEditorFormNodeList.size(); i++ )
436+
{
437+
QDomElement elem = attributeEditorFormNodeList.at( i ).toElement();
438+
439+
QgsAttributeEditorElement* attributeEditorWidget = attributeEditorElementFromDomElement( elem, nullptr );
440+
addTab( attributeEditorWidget );
441+
}
442+
}
424443
}
425444

426445

@@ -510,7 +529,7 @@ void QgsEditFormConfig::writeXml( QDomNode& node ) const
510529
node.appendChild( editorLayoutElem );
511530

512531
// tabs and groups of edit form
513-
if ( tabs().size() > 0 )
532+
if ( tabs().size() > 0 && d->mConfiguredRootContainer )
514533
{
515534
QDomElement tabsElem = doc.createElement( "attributeEditorForm" );
516535

‎src/core/qgseditformconfig.h

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -241,12 +241,12 @@ class CORE_EXPORT QgsEditFormConfig
241241
QgsEditorWidgetConfig widgetConfig( const QString& widgetName ) const;
242242

243243
/**
244-
* Remove the configuration for the editor widget used to represent the field at the given index
245-
*
246-
* @param fieldIdx The index of the field
247-
*
248-
* @return true if successful, false if the field does not exist
249-
*/
244+
* Remove the configuration for the editor widget used to represent the field at the given index
245+
*
246+
* @param fieldIdx The index of the field
247+
*
248+
* @return true if successful, false if the field does not exist
249+
*/
250250
bool removeWidgetConfig( int fieldIdx );
251251

252252
/**
@@ -411,10 +411,8 @@ class CORE_EXPORT QgsEditFormConfig
411411
/**
412412
* Used internally to set the fields when they change.
413413
* This should only be called from QgsVectorLayer for synchronization reasons
414-
*
415-
* @param fields The fields
416414
*/
417-
void setFields( const QgsFields& fields );
415+
void setFields( const QgsFields& fields, const QMap<QString, QString>& attributeAliasMap );
418416

419417
/**
420418
* Will be called by friend class QgsVectorLayer

‎src/core/qgseditformconfig_p.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
#ifndef QGSEDITFORMCONFIG_P_H
1717
#define QGSEDITFORMCONFIG_P_H
1818

19+
#include <QMap>
20+
1921
#include "qgsfield.h"
2022
#include "qgseditformconfig.h"
2123

@@ -24,6 +26,7 @@ class QgsEditFormConfigPrivate : public QSharedData
2426
public:
2527
QgsEditFormConfigPrivate()
2628
: mInvisibleRootContainer( new QgsAttributeEditorContainer( QString::null, nullptr ) )
29+
, mConfiguredRootContainer( false )
2730
, mEditorLayout( QgsEditFormConfig::GeneratedLayout )
2831
, mInitCodeSource( QgsEditFormConfig::CodeSourceNone )
2932
, mSuppressForm( QgsEditFormConfig::SuppressDefault )
@@ -32,6 +35,7 @@ class QgsEditFormConfigPrivate : public QSharedData
3235
QgsEditFormConfigPrivate( const QgsEditFormConfigPrivate& o )
3336
: QSharedData( o )
3437
, mInvisibleRootContainer( static_cast<QgsAttributeEditorContainer*>( o.mInvisibleRootContainer->clone( nullptr ) ) )
38+
, mConfiguredRootContainer( o.mConfiguredRootContainer )
3539
, mConstraints( o.mConstraints )
3640
, mConstraintsDescription( o.mConstraintsDescription )
3741
, mFieldEditables( o.mFieldEditables )
@@ -84,6 +88,7 @@ class QgsEditFormConfigPrivate : public QSharedData
8488
QgsEditFormConfig::FeatureFormSuppress mSuppressForm;
8589

8690
QgsFields mFields;
91+
QMap<QString, QString> mAttributeAliasMap;
8792

8893
};
8994

‎src/core/qgsvectorlayer.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2048,6 +2048,7 @@ void QgsVectorLayer::remAttributeAlias( int attIndex )
20482048
if ( mAttributeAliasMap.contains( name ) )
20492049
{
20502050
mAttributeAliasMap.remove( name );
2051+
mEditFormConfig.setFields( mUpdatedFields, mAttributeAliasMap );
20512052
emit layerModified();
20522053
}
20532054
}
@@ -2068,6 +2069,7 @@ void QgsVectorLayer::addAttributeAlias( int attIndex, const QString& aliasString
20682069
QString name = fields().at( attIndex ).name();
20692070

20702071
mAttributeAliasMap.insert( name, aliasString );
2072+
mEditFormConfig.setFields( mUpdatedFields, mAttributeAliasMap );
20712073
emit layerModified(); // TODO[MD]: should have a different signal?
20722074
}
20732075

@@ -2078,7 +2080,7 @@ QString QgsVectorLayer::attributeAlias( int attributeIndex ) const
20782080

20792081
QString name = fields().at( attributeIndex ).name();
20802082

2081-
return mAttributeAliasMap.value( name, QString() );
2083+
return mAttributeAliasMap.value( name );
20822084
}
20832085

20842086
QString QgsVectorLayer::attributeDisplayName( int attributeIndex ) const
@@ -2781,7 +2783,7 @@ void QgsVectorLayer::updateFields()
27812783
if ( oldFields != mUpdatedFields )
27822784
{
27832785
emit updatedFields();
2784-
mEditFormConfig.setFields( mUpdatedFields );
2786+
mEditFormConfig.setFields( mUpdatedFields, mAttributeAliasMap );
27852787
}
27862788
}
27872789

0 commit comments

Comments
 (0)
Please sign in to comment.