Skip to content

Commit

Permalink
Remove editor widget project loading code from gui
Browse files Browse the repository at this point in the history
  • Loading branch information
m-kuhn committed Dec 20, 2016
1 parent 9fd0802 commit c9f8ac2
Show file tree
Hide file tree
Showing 30 changed files with 2 additions and 775 deletions.
45 changes: 0 additions & 45 deletions python/gui/editorwidgets/core/qgseditorwidgetfactory.sip
Original file line number Diff line number Diff line change
Expand Up @@ -67,28 +67,6 @@ class QgsEditorWidgetFactory
*/
virtual QgsEditorConfigWidget* configWidget( QgsVectorLayer* vl, int fieldIdx, QWidget* parent ) const = 0 /Factory/;

/**
* Read the config from an XML file and map it to a proper {@link QVariantMap}.
*
* @param configElement The configuration element from the project file
* @param layer The layer for which this configuration applies
* @param fieldIdx The field on the layer for which this configuration applies
*
* @return A configuration object. This will be passed to your widget wrapper later on
*/
QVariantMap readEditorConfig( const QDomElement& configElement, QgsVectorLayer* layer, int fieldIdx );

/**
* Serialize your configuration and save it in a xml doc.
*
* @param config The configuration to serialize
* @param configElement The element, where you can write your configuration into
* @param doc The document. You can use this to create new nodes
* @param layer The layer for which this configuration applies
* @param fieldIdx The field on the layer for which this configuration applies
*/
virtual void writeConfig( const QVariantMap& config, QDomElement& configElement, QDomDocument& doc, const QgsVectorLayer* layer, int fieldIdx );

/**
* Check if this editor widget type supports a certain field.
*
Expand All @@ -110,17 +88,6 @@ class QgsEditorWidgetFactory
*/
//virtual QHash<const char*, int> supportedWidgetTypes();

/**
* Read the config from an XML file and map it to a proper {@link QVariantMap}.
*
* @param configElement The configuration element from the project file
* @param layer The layer for which this configuration applies
* @param fieldIdx The field on the layer for which this configuration applies
*
* @return A configuration object. This will be passed to your widget wrapper later on
*/
virtual QVariantMap readConfig( const QDomElement& configElement, QgsVectorLayer* layer, int fieldIdx );

/**
* This method allows disabling this editor widget type for a certain field.
* By default, it returns 5 for every fields.
Expand All @@ -140,16 +107,4 @@ class QgsEditorWidgetFactory
* @see supportsField( QgsVectorLayer* vl, fieldIdx )
*/
virtual unsigned int fieldScore( const QgsVectorLayer* vl, int fieldIdx ) const;

protected:

/**
* Copy the given config element to a XML attribute.
*/
static void config2xml( const QVariantMap& config, QDomElement& configElement, const QString& fieldName );

/**
* Copy the given XML attribute to the configuration element.
*/
static void xml2config( const QDomElement& configElement, QVariantMap& config, const QString& fieldName );
};
3 changes: 1 addition & 2 deletions src/core/qgsvectorlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1977,9 +1977,8 @@ bool QgsVectorLayer::writeSymbology( QDomNode& node, QDomDocument& doc, QString&
{
( void )writeStyle( node, doc, errorMessage );

QgsFields dataProviderFields = mDataProvider->fields();

QDomElement fieldConfigurationElement = doc.createElement( QStringLiteral( "fieldConfiguration" ) );
node.appendChild( fieldConfigurationElement );

int index = 0;
Q_FOREACH ( const QgsField& field, mFields )
Expand Down
48 changes: 0 additions & 48 deletions src/gui/editorwidgets/core/qgseditorwidgetfactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,57 +46,9 @@ QString QgsEditorWidgetFactory::name()
return mName;
}

QVariantMap QgsEditorWidgetFactory::readEditorConfig( const QDomElement& configElement, QgsVectorLayer* layer, int fieldIdx )
{
return readConfig( configElement, layer, fieldIdx );
}

void QgsEditorWidgetFactory::writeConfig( const QVariantMap& config, QDomElement& configElement, QDomDocument& doc, const QgsVectorLayer* layer, int fieldIdx )
{
Q_UNUSED( config );
Q_UNUSED( configElement );
Q_UNUSED( doc );
Q_UNUSED( layer );
Q_UNUSED( fieldIdx );
}

QVariantMap QgsEditorWidgetFactory::readConfig( const QDomElement& configElement, QgsVectorLayer* layer, int fieldIdx )
{
Q_UNUSED( configElement );
Q_UNUSED( layer );
Q_UNUSED( fieldIdx );

return QVariantMap();
}

unsigned int QgsEditorWidgetFactory::fieldScore( const QgsVectorLayer* vl, int fieldIdx ) const
{
Q_UNUSED( vl )
Q_UNUSED( fieldIdx )
return 5;
}

void QgsEditorWidgetFactory::config2xml( const QVariantMap& config, QDomElement& configElement, const QString& fieldName )
{
const QVariant value = config.value( fieldName );
if ( value.isValid() )
{
if ( value.type() == QVariant::Bool )
{
configElement.setAttribute( fieldName, value.toBool() ? "1" : "0" );
}
else
{
configElement.setAttribute( fieldName, value.toString() );
}
}
}

void QgsEditorWidgetFactory::xml2config( const QDomElement& configElement, QVariantMap& config, const QString& fieldName )
{
const QString value = configElement.attribute( fieldName );
if ( !value.isNull() )
{
config.insert( fieldName, value );
}
}
45 changes: 0 additions & 45 deletions src/gui/editorwidgets/core/qgseditorwidgetfactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,28 +84,6 @@ class GUI_EXPORT QgsEditorWidgetFactory
*/
virtual QgsEditorConfigWidget* configWidget( QgsVectorLayer* vl, int fieldIdx, QWidget* parent ) const = 0;

/**
* Read the config from an XML file and map it to a proper {@link QVariantMap}.
*
* @param configElement The configuration element from the project file
* @param layer The layer for which this configuration applies
* @param fieldIdx The field on the layer for which this configuration applies
*
* @return A configuration object. This will be passed to your widget wrapper later on
*/
QVariantMap readEditorConfig( const QDomElement& configElement, QgsVectorLayer* layer, int fieldIdx );

/**
* Serialize your configuration and save it in a xml doc.
*
* @param config The configuration to serialize
* @param configElement The element, where you can write your configuration into
* @param doc The document. You can use this to create new nodes
* @param layer The layer for which this configuration applies
* @param fieldIdx The field on the layer for which this configuration applies
*/
virtual void writeConfig( const QVariantMap& config, QDomElement& configElement, QDomDocument& doc, const QgsVectorLayer* layer, int fieldIdx );

/**
* Check if this editor widget type supports a certain field.
*
Expand All @@ -127,17 +105,6 @@ class GUI_EXPORT QgsEditorWidgetFactory
*/
virtual QHash<const char*, int> supportedWidgetTypes() { return QHash<const char*, int>(); }

/**
* Read the config from an XML file and map it to a proper {@link QVariantMap}.
*
* @param configElement The configuration element from the project file
* @param layer The layer for which this configuration applies
* @param fieldIdx The field on the layer for which this configuration applies
*
* @return A configuration object. This will be passed to your widget wrapper later on
*/
virtual QVariantMap readConfig( const QDomElement& configElement, QgsVectorLayer* layer, int fieldIdx );

/**
* This method allows disabling this editor widget type for a certain field.
* By default, it returns 5 for every fields.
Expand All @@ -158,18 +125,6 @@ class GUI_EXPORT QgsEditorWidgetFactory
*/
virtual unsigned int fieldScore( const QgsVectorLayer* vl, int fieldIdx ) const;

protected:

/**
* Copy the given config element to a XML attribute.
*/
static void config2xml( const QVariantMap& config, QDomElement& configElement, const QString& fieldName );

/**
* Copy the given XML attribute to the configuration element.
*/
static void xml2config( const QDomElement& configElement, QVariantMap& config, const QString& fieldName );

private:
QString mName;
};
Expand Down
163 changes: 0 additions & 163 deletions src/gui/editorwidgets/core/qgseditorwidgetregistry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,6 @@ void QgsEditorWidgetRegistry::initEditors( QgsMapCanvas *mapCanvas, QgsMessageBa

QgsEditorWidgetRegistry::QgsEditorWidgetRegistry()
{
// connect( QgsProject::instance(), SIGNAL( readMapLayer( QgsMapLayer*, const QDomElement& ) ), this, SLOT( readMapLayer( QgsMapLayer*, const QDomElement& ) ) );
// connect( QgsProject::instance(), SIGNAL( writeMapLayer( QgsMapLayer*, QDomElement&, QDomDocument& ) ), this, SLOT( writeMapLayer( QgsMapLayer*, QDomElement&, QDomDocument& ) ) );

connect( QgsProject::instance(), SIGNAL( layerWillBeRemoved( QgsMapLayer* ) ), this, SLOT( mapLayerWillBeRemoved( QgsMapLayer* ) ) );
connect( QgsProject::instance(), SIGNAL( layerWasAdded( QgsMapLayer* ) ), this, SLOT( mapLayerAdded( QgsMapLayer* ) ) );
}

QgsEditorWidgetRegistry::~QgsEditorWidgetRegistry()
Expand Down Expand Up @@ -222,164 +217,6 @@ bool QgsEditorWidgetRegistry::registerWidget( const QString& widgetId, QgsEditor
}
}

void QgsEditorWidgetRegistry::readMapLayer( QgsMapLayer* mapLayer, const QDomElement& layerElem )
{
if ( mapLayer->type() != QgsMapLayer::VectorLayer )
{
return;
}

QgsVectorLayer* vectorLayer = qobject_cast<QgsVectorLayer*>( mapLayer );
Q_ASSERT( vectorLayer );

QDomNodeList editTypeNodes = layerElem.namedItem( QStringLiteral( "edittypes" ) ).childNodes();

QgsEditFormConfig formConfig = vectorLayer->editFormConfig();

for ( int i = 0; i < editTypeNodes.size(); i++ )
{
QDomNode editTypeNode = editTypeNodes.at( i );
QDomElement editTypeElement = editTypeNode.toElement();

QString name = editTypeElement.attribute( QStringLiteral( "name" ) );

int idx = vectorLayer->fields().lookupField( name );
if ( idx == -1 )
continue;

QString ewv2Type = editTypeElement.attribute( QStringLiteral( "widgetv2type" ) );
QVariantMap cfg;

if ( mWidgetFactories.contains( ewv2Type ) )
{
formConfig.setWidgetType( name, ewv2Type );
QDomElement ewv2CfgElem = editTypeElement.namedItem( QStringLiteral( "widgetv2config" ) ).toElement();

if ( !ewv2CfgElem.isNull() )
{
cfg = mWidgetFactories[ewv2Type]->readEditorConfig( ewv2CfgElem, vectorLayer, idx );
}

formConfig.setReadOnly( idx, ewv2CfgElem.attribute( QStringLiteral( "fieldEditable" ), QStringLiteral( "1" ) ) != QLatin1String( "1" ) );
formConfig.setLabelOnTop( idx, ewv2CfgElem.attribute( QStringLiteral( "labelOnTop" ), QStringLiteral( "0" ) ) == QLatin1String( "1" ) );
if ( ewv2CfgElem.attribute( QStringLiteral( "notNull" ), QStringLiteral( "0" ) ) == QLatin1String( "1" ) )
{
// upgrade from older config
vectorLayer->setFieldConstraint( idx, QgsFieldConstraints::ConstraintNotNull );
}
if ( !ewv2CfgElem.attribute( QStringLiteral( "constraint" ), QString() ).isEmpty() )
{
// upgrade from older config
vectorLayer->setConstraintExpression( idx, ewv2CfgElem.attribute( QStringLiteral( "constraint" ), QString() ),
ewv2CfgElem.attribute( QStringLiteral( "constraintDescription" ), QString() ) );
}

formConfig.setWidgetConfig( name, cfg );
}
else
{
QgsMessageLog::logMessage( tr( "Unknown attribute editor widget '%1'" ).arg( ewv2Type ) );
}
}

vectorLayer->setEditFormConfig( formConfig );
}

void QgsEditorWidgetRegistry::writeMapLayer( QgsMapLayer* mapLayer, QDomElement& layerElem, QDomDocument& doc ) const
{
if ( mapLayer->type() != QgsMapLayer::VectorLayer )
{
return;
}

QgsVectorLayer* vectorLayer = qobject_cast<QgsVectorLayer*>( mapLayer );
if ( !vectorLayer )
{
return;
}

QDomNode editTypesNode = doc.createElement( QStringLiteral( "edittypes" ) );

QgsFields fields = vectorLayer->fields();
for ( int idx = 0; idx < fields.count(); ++idx )
{
const QgsField field = fields.at( idx );
const QString& widgetType = vectorLayer->editFormConfig().widgetType( field.name() );
if ( widgetType.isNull() )
{
// Don't save widget config if it is not manually edited
continue;
}
if ( !mWidgetFactories.contains( widgetType ) )
{
QgsMessageLog::logMessage( tr( "Could not save unknown editor widget type '%1'." ).arg( widgetType ) );
continue;
}


QDomElement editTypeElement = doc.createElement( QStringLiteral( "edittype" ) );
editTypeElement.setAttribute( QStringLiteral( "name" ), field.name() );
editTypeElement.setAttribute( QStringLiteral( "widgetv2type" ), widgetType );

if ( mWidgetFactories.contains( widgetType ) )
{
QDomElement ewv2CfgElem = doc.createElement( QStringLiteral( "widgetv2config" ) );
ewv2CfgElem.setAttribute( QStringLiteral( "fieldEditable" ), !vectorLayer->editFormConfig().readOnly( idx ) );
ewv2CfgElem.setAttribute( QStringLiteral( "labelOnTop" ), vectorLayer->editFormConfig().labelOnTop( idx ) );

mWidgetFactories[widgetType]->writeConfig( vectorLayer->editFormConfig().widgetConfig( field.name() ), ewv2CfgElem, doc, vectorLayer, idx );

editTypeElement.appendChild( ewv2CfgElem );
}

editTypesNode.appendChild( editTypeElement );
}

layerElem.appendChild( editTypesNode );
}

void QgsEditorWidgetRegistry::mapLayerWillBeRemoved( QgsMapLayer* mapLayer )
{
QgsVectorLayer* vl = qobject_cast<QgsVectorLayer*>( mapLayer );

if ( vl )
{
disconnect( vl, SIGNAL( readCustomSymbology( const QDomElement&, QString& ) ), this, SLOT( readSymbology( const QDomElement&, QString& ) ) );
disconnect( vl, SIGNAL( writeCustomSymbology( QDomElement&, QDomDocument&, QString& ) ), this, SLOT( writeSymbology( QDomElement&, QDomDocument&, QString& ) ) );
}
}

void QgsEditorWidgetRegistry::mapLayerAdded( QgsMapLayer* mapLayer )
{
QgsVectorLayer* vl = qobject_cast<QgsVectorLayer*>( mapLayer );

if ( vl )
{
connect( vl, SIGNAL( readCustomSymbology( const QDomElement&, QString& ) ), this, SLOT( readSymbology( const QDomElement&, QString& ) ) );
connect( vl, SIGNAL( writeCustomSymbology( QDomElement&, QDomDocument&, QString& ) ), this, SLOT( writeSymbology( QDomElement&, QDomDocument&, QString& ) ) );
}
}

void QgsEditorWidgetRegistry::readSymbology( const QDomElement& element, QString& errorMessage )
{
Q_UNUSED( errorMessage )
QgsVectorLayer* vl = qobject_cast<QgsVectorLayer*>( sender() );

Q_ASSERT( vl );

readMapLayer( vl, element );
}

void QgsEditorWidgetRegistry::writeSymbology( QDomElement& element, QDomDocument& doc, QString& errorMessage )
{
Q_UNUSED( errorMessage )
QgsVectorLayer* vl = qobject_cast<QgsVectorLayer*>( sender() );

Q_ASSERT( vl );

writeMapLayer( vl, element, doc );
}

QString QgsEditorWidgetRegistry::findSuitableWrapper( QWidget* editor, const QString& defaultWidget )
{
QMap<const char*, QPair<int, QString> >::ConstIterator it;
Expand Down
Loading

0 comments on commit c9f8ac2

Please sign in to comment.