Skip to content

Commit

Permalink
Create a default tab layout if none is configured
Browse files Browse the repository at this point in the history
  • Loading branch information
m-kuhn committed Aug 19, 2016
1 parent 5c20f07 commit 633c1a3
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 20 deletions.
37 changes: 28 additions & 9 deletions src/core/qgseditformconfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,21 @@ QgsEditorWidgetConfig QgsEditFormConfig::widgetConfig( const QString& widgetName
return d->mWidgetConfigs.value( widgetName );
}

void QgsEditFormConfig::setFields( const QgsFields& fields )
void QgsEditFormConfig::setFields( const QgsFields& fields, const QMap<QString, QString>& attributeAliasMap )
{
d.detach();
d->mFields = fields;
d->mAttributeAliasMap = attributeAliasMap;

if ( !d->mConfiguredRootContainer )
{
d->mInvisibleRootContainer->clear();
for ( int i = 0; i < d->mFields.size(); ++i )
{
QgsAttributeEditorField* field = new QgsAttributeEditorField( d->mAttributeAliasMap.value( d->mFields.at( i ).name(), d->mFields.at( i ).name() ), i, d->mInvisibleRootContainer );
d->mInvisibleRootContainer->addChildElement( field );
}
}
}

void QgsEditFormConfig::onRelationsLoaded()
Expand Down Expand Up @@ -411,16 +422,24 @@ void QgsEditFormConfig::readXml( const QDomNode& node )
}

// tabs and groups display info
clearTabs();
QDomNode attributeEditorFormNode = node.namedItem( "attributeEditorForm" );
QDomNodeList attributeEditorFormNodeList = attributeEditorFormNode.toElement().childNodes();

for ( int i = 0; i < attributeEditorFormNodeList.size(); i++ )
if ( !attributeEditorFormNode.isNull() )
{
QDomElement elem = attributeEditorFormNodeList.at( i ).toElement();
QDomNodeList attributeEditorFormNodeList = attributeEditorFormNode.toElement().childNodes();

QgsAttributeEditorElement *attributeEditorWidget = attributeEditorElementFromDomElement( elem, this );
addTab( attributeEditorWidget );
if ( attributeEditorFormNodeList.size() )
{
d->mConfiguredRootContainer = true;
clearTabs();

for ( int i = 0; i < attributeEditorFormNodeList.size(); i++ )
{
QDomElement elem = attributeEditorFormNodeList.at( i ).toElement();

QgsAttributeEditorElement* attributeEditorWidget = attributeEditorElementFromDomElement( elem, nullptr );
addTab( attributeEditorWidget );
}
}
}


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

// tabs and groups of edit form
if ( tabs().size() > 0 )
if ( tabs().size() > 0 && d->mConfiguredRootContainer )
{
QDomElement tabsElem = doc.createElement( "attributeEditorForm" );

Expand Down
16 changes: 7 additions & 9 deletions src/core/qgseditformconfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -241,12 +241,12 @@ class CORE_EXPORT QgsEditFormConfig
QgsEditorWidgetConfig widgetConfig( const QString& widgetName ) const;

/**
* Remove the configuration for the editor widget used to represent the field at the given index
*
* @param fieldIdx The index of the field
*
* @return true if successful, false if the field does not exist
*/
* Remove the configuration for the editor widget used to represent the field at the given index
*
* @param fieldIdx The index of the field
*
* @return true if successful, false if the field does not exist
*/
bool removeWidgetConfig( int fieldIdx );

/**
Expand Down Expand Up @@ -411,10 +411,8 @@ class CORE_EXPORT QgsEditFormConfig
/**
* Used internally to set the fields when they change.
* This should only be called from QgsVectorLayer for synchronization reasons
*
* @param fields The fields
*/
void setFields( const QgsFields& fields );
void setFields( const QgsFields& fields, const QMap<QString, QString>& attributeAliasMap );

/**
* Will be called by friend class QgsVectorLayer
Expand Down
5 changes: 5 additions & 0 deletions src/core/qgseditformconfig_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
#ifndef QGSEDITFORMCONFIG_P_H
#define QGSEDITFORMCONFIG_P_H

#include <QMap>

#include "qgsfield.h"
#include "qgseditformconfig.h"

Expand All @@ -24,6 +26,7 @@ class QgsEditFormConfigPrivate : public QSharedData
public:
QgsEditFormConfigPrivate()
: mInvisibleRootContainer( new QgsAttributeEditorContainer( QString::null, nullptr ) )
, mConfiguredRootContainer( false )
, mEditorLayout( QgsEditFormConfig::GeneratedLayout )
, mInitCodeSource( QgsEditFormConfig::CodeSourceNone )
, mSuppressForm( QgsEditFormConfig::SuppressDefault )
Expand All @@ -32,6 +35,7 @@ class QgsEditFormConfigPrivate : public QSharedData
QgsEditFormConfigPrivate( const QgsEditFormConfigPrivate& o )
: QSharedData( o )
, mInvisibleRootContainer( static_cast<QgsAttributeEditorContainer*>( o.mInvisibleRootContainer->clone( nullptr ) ) )
, mConfiguredRootContainer( o.mConfiguredRootContainer )
, mConstraints( o.mConstraints )
, mConstraintsDescription( o.mConstraintsDescription )
, mFieldEditables( o.mFieldEditables )
Expand Down Expand Up @@ -84,6 +88,7 @@ class QgsEditFormConfigPrivate : public QSharedData
QgsEditFormConfig::FeatureFormSuppress mSuppressForm;

QgsFields mFields;
QMap<QString, QString> mAttributeAliasMap;

};

Expand Down
6 changes: 4 additions & 2 deletions src/core/qgsvectorlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2048,6 +2048,7 @@ void QgsVectorLayer::remAttributeAlias( int attIndex )
if ( mAttributeAliasMap.contains( name ) )
{
mAttributeAliasMap.remove( name );
mEditFormConfig.setFields( mUpdatedFields, mAttributeAliasMap );
emit layerModified();
}
}
Expand All @@ -2068,6 +2069,7 @@ void QgsVectorLayer::addAttributeAlias( int attIndex, const QString& aliasString
QString name = fields().at( attIndex ).name();

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

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

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

return mAttributeAliasMap.value( name, QString() );
return mAttributeAliasMap.value( name );
}

QString QgsVectorLayer::attributeDisplayName( int attributeIndex ) const
Expand Down Expand Up @@ -2781,7 +2783,7 @@ void QgsVectorLayer::updateFields()
if ( oldFields != mUpdatedFields )
{
emit updatedFields();
mEditFormConfig.setFields( mUpdatedFields );
mEditFormConfig.setFields( mUpdatedFields, mAttributeAliasMap );
}
}

Expand Down

0 comments on commit 633c1a3

Please sign in to comment.