Skip to content

Commit c56588e

Browse files
committed
Save feature form configuration in QgsEditFormConfig
1 parent 0f6256e commit c56588e

File tree

4 files changed

+253
-213
lines changed

4 files changed

+253
-213
lines changed

src/core/qgseditformconfig.cpp

+206-11
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ QgsEditFormConfig::QgsEditFormConfig( QObject* parent )
55
: QObject( parent )
66
, mEditorLayout( GeneratedLayout )
77
, mInitCodeSource( CodeSourceNone )
8-
, mFeatureFormSuppress( SuppressDefault )
8+
, mSuppressForm( SuppressDefault )
99
{
1010
connect( QgsProject::instance()->relationManager(), SIGNAL( relationsLoaded() ), this, SLOT( onRelationsLoaded() ) );
1111
}
@@ -28,20 +28,19 @@ QgsEditorWidgetConfig QgsEditFormConfig::widgetConfig( int fieldIdx ) const
2828
if ( fieldIdx < 0 || fieldIdx >= mFields.count() )
2929
return QgsEditorWidgetConfig();
3030

31-
return mEditorWidgetV2Configs.value( mFields[fieldIdx].name() );
31+
return mWidgetConfigs.value( mFields[fieldIdx].name() );
3232
}
3333

34-
QgsEditorWidgetConfig QgsEditFormConfig::widgetConfig( const QString& fieldName ) const
34+
QgsEditorWidgetConfig QgsEditFormConfig::widgetConfig( const QString& widgetName ) const
3535
{
36-
return mEditorWidgetV2Configs.value( fieldName );
36+
return mWidgetConfigs.value( widgetName );
3737
}
3838

3939
void QgsEditFormConfig::setFields( const QgsFields& fields )
4040
{
4141
mFields = fields;
4242
}
4343

44-
4544
void QgsEditFormConfig::setWidgetType( int attrIdx, const QString& widgetType )
4645
{
4746
if ( attrIdx >= 0 && attrIdx < mFields.count() )
@@ -51,12 +50,12 @@ void QgsEditFormConfig::setWidgetType( int attrIdx, const QString& widgetType )
5150
void QgsEditFormConfig::setWidgetConfig( int attrIdx, const QgsEditorWidgetConfig& config )
5251
{
5352
if ( attrIdx >= 0 && attrIdx < mFields.count() )
54-
mEditorWidgetV2Configs[ mFields.at( attrIdx ).name()] = config;
53+
mWidgetConfigs[ mFields.at( attrIdx ).name()] = config;
5554
}
5655

57-
QString QgsEditFormConfig::uiForm() const
56+
void QgsEditFormConfig::setWidgetConfig( const QString& widgetName, const QgsEditorWidgetConfig& config )
5857
{
59-
return mEditForm;
58+
mWidgetConfigs[widgetName] = config;
6059
}
6160

6261
void QgsEditFormConfig::setUiForm( const QString& ui )
@@ -69,10 +68,10 @@ void QgsEditFormConfig::setUiForm( const QString& ui )
6968
{
7069
setLayout( UiFileLayout );
7170
}
72-
mEditForm = ui;
71+
mUiFormPath = ui;
7372
}
7473

75-
bool QgsEditFormConfig::readOnly( int idx )
74+
bool QgsEditFormConfig::readOnly( int idx ) const
7675
{
7776
if ( idx >= 0 && idx < mFields.count() )
7877
{
@@ -85,7 +84,7 @@ bool QgsEditFormConfig::readOnly( int idx )
8584
return false;
8685
}
8786

88-
bool QgsEditFormConfig::labelOnTop( int idx )
87+
bool QgsEditFormConfig::labelOnTop( int idx ) const
8988
{
9089
if ( idx >= 0 && idx < mFields.count() )
9190
return mLabelOnTop.value( mFields.at( idx ).name(), false );
@@ -105,6 +104,202 @@ void QgsEditFormConfig::setLabelOnTop( int idx, bool onTop )
105104
mLabelOnTop[ mFields.at( idx ).name()] = onTop;
106105
}
107106

107+
void QgsEditFormConfig::readXml( const QDomNode& node )
108+
{
109+
QDomNode editFormNode = node.namedItem( "editform" );
110+
if ( !editFormNode.isNull() )
111+
{
112+
QDomElement e = editFormNode.toElement();
113+
mUiFormPath = QgsProject::instance()->readPath( e.text() );
114+
}
115+
116+
QDomNode editFormInitNode = node.namedItem( "editforminit" );
117+
if ( !editFormInitNode.isNull() )
118+
{
119+
mInitFunction = editFormInitNode.toElement().text();
120+
}
121+
122+
QDomNode editFormInitCodeSourceNode = node.namedItem( "editforminitcodesource" );
123+
if ( !editFormInitCodeSourceNode.isNull() || ( !editFormInitCodeSourceNode.isNull() && !editFormInitCodeSourceNode.toElement().text().isEmpty() ) )
124+
{
125+
setInitCodeSource(( QgsEditFormConfig::PythonInitCodeSource ) editFormInitCodeSourceNode.toElement().text().toInt() );
126+
}
127+
128+
QDomNode editFormInitCodeNode = node.namedItem( "editforminitcode" );
129+
if ( !editFormInitCodeNode.isNull() )
130+
{
131+
setInitCode( editFormInitCodeNode.toElement().text() );
132+
}
133+
134+
// Temporary < 2.12 b/w compatibility "dot" support patch
135+
// @see: https://github.com/qgis/QGIS/pull/2498
136+
// For b/w compatibility, check if there's a dot in the function name
137+
// and if yes, transform it in an import statement for the module
138+
// and set the PythonInitCodeSource to CodeSourceDialog
139+
int dotPos = mInitFunction.lastIndexOf( '.' );
140+
if ( dotPos >= 0 ) // It's a module
141+
{
142+
setInitCodeSource( QgsEditFormConfig::CodeSourceDialog );
143+
setInitFunction( mInitFunction.mid( dotPos + 1 ) );
144+
setInitCode( QString( "from %1 import %2\n" ).arg( mInitFunction.left( dotPos ), mInitFunction.mid( dotPos + 1 ) ) );
145+
}
146+
147+
QDomNode editFormInitFilePathNode = node.namedItem( "editforminitfilepath" );
148+
if ( !editFormInitFilePathNode.isNull() || ( !editFormInitFilePathNode.isNull() && !editFormInitFilePathNode.toElement().text().isEmpty() ) )
149+
{
150+
setInitFilePath( editFormInitFilePathNode.toElement().text() );
151+
}
152+
153+
QDomNode fFSuppNode = node.namedItem( "featformsuppress" );
154+
if ( fFSuppNode.isNull() )
155+
{
156+
mSuppressForm = QgsEditFormConfig::SuppressDefault;
157+
}
158+
else
159+
{
160+
QDomElement e = fFSuppNode.toElement();
161+
mSuppressForm = ( QgsEditFormConfig::FeatureFormSuppress )e.text().toInt();
162+
}
163+
164+
// tab display
165+
QDomNode editorLayoutNode = node.namedItem( "editorlayout" );
166+
if ( editorLayoutNode.isNull() )
167+
{
168+
mEditorLayout = QgsEditFormConfig::GeneratedLayout;
169+
}
170+
else
171+
{
172+
if ( editorLayoutNode.toElement().text() == "uifilelayout" )
173+
{
174+
mEditorLayout = QgsEditFormConfig::UiFileLayout;
175+
}
176+
else if ( editorLayoutNode.toElement().text() == "tablayout" )
177+
{
178+
mEditorLayout = QgsEditFormConfig::TabLayout;
179+
}
180+
else
181+
{
182+
mEditorLayout = QgsEditFormConfig::GeneratedLayout;
183+
}
184+
}
185+
186+
// tabs and groups display info
187+
clearTabs();
188+
QDomNode attributeEditorFormNode = node.namedItem( "attributeEditorForm" );
189+
QDomNodeList attributeEditorFormNodeList = attributeEditorFormNode.toElement().childNodes();
190+
191+
for ( int i = 0; i < attributeEditorFormNodeList.size(); i++ )
192+
{
193+
QDomElement elem = attributeEditorFormNodeList.at( i ).toElement();
194+
195+
QgsAttributeEditorElement *attributeEditorWidget = attributeEditorElementFromDomElement( elem, this );
196+
addTab( attributeEditorWidget );
197+
}
198+
}
199+
200+
void QgsEditFormConfig::writeXml( QDomNode& node ) const
201+
{
202+
QDomDocument doc( node.ownerDocument() );
203+
204+
QDomElement efField = doc.createElement( "editform" );
205+
QDomText efText = doc.createTextNode( QgsProject::instance()->writePath( uiForm() ) );
206+
efField.appendChild( efText );
207+
node.appendChild( efField );
208+
209+
QDomElement efiField = doc.createElement( "editforminit" );
210+
if ( !initFunction().isEmpty() )
211+
efiField.appendChild( doc.createTextNode( initFunction() ) );
212+
node.appendChild( efiField );
213+
214+
QDomElement eficsField = doc.createElement( "editforminitcodesource" );
215+
eficsField.appendChild( doc.createTextNode( QString::number( initCodeSource() ) ) );
216+
node.appendChild( eficsField );
217+
218+
QDomElement efifpField = doc.createElement( "editforminitfilepath" );
219+
efifpField.appendChild( doc.createTextNode( QgsProject::instance()->writePath( initFilePath() ) ) );
220+
node.appendChild( efifpField );
221+
222+
223+
QDomElement eficField = doc.createElement( "editforminitcode" );
224+
eficField.appendChild( doc.createCDATASection( initCode() ) );
225+
node.appendChild( eficField );
226+
227+
QDomElement fFSuppElem = doc.createElement( "featformsuppress" );
228+
QDomText fFSuppText = doc.createTextNode( QString::number( suppress() ) );
229+
fFSuppElem.appendChild( fFSuppText );
230+
node.appendChild( fFSuppElem );
231+
232+
// tab display
233+
QDomElement editorLayoutElem = doc.createElement( "editorlayout" );
234+
switch ( layout() )
235+
{
236+
case QgsEditFormConfig::UiFileLayout:
237+
editorLayoutElem.appendChild( doc.createTextNode( "uifilelayout" ) );
238+
break;
239+
240+
case QgsEditFormConfig::TabLayout:
241+
editorLayoutElem.appendChild( doc.createTextNode( "tablayout" ) );
242+
break;
243+
244+
case QgsEditFormConfig::GeneratedLayout:
245+
default:
246+
editorLayoutElem.appendChild( doc.createTextNode( "generatedlayout" ) );
247+
break;
248+
}
249+
250+
node.appendChild( editorLayoutElem );
251+
252+
// tabs and groups of edit form
253+
if ( tabs().size() > 0 )
254+
{
255+
QDomElement tabsElem = doc.createElement( "attributeEditorForm" );
256+
257+
for ( QList< QgsAttributeEditorElement* >::const_iterator it = tabs().constBegin(); it != tabs().constEnd(); ++it )
258+
{
259+
QDomElement attributeEditorWidgetElem = ( *it )->toDomElement( doc );
260+
tabsElem.appendChild( attributeEditorWidgetElem );
261+
}
262+
263+
node.appendChild( tabsElem );
264+
}
265+
}
266+
267+
QgsAttributeEditorElement* QgsEditFormConfig::attributeEditorElementFromDomElement( QDomElement &elem, QObject* parent )
268+
{
269+
QgsAttributeEditorElement* newElement = NULL;
270+
271+
if ( elem.tagName() == "attributeEditorContainer" )
272+
{
273+
QgsAttributeEditorContainer* container = new QgsAttributeEditorContainer( elem.attribute( "name" ), parent );
274+
275+
QDomNodeList childNodeList = elem.childNodes();
276+
277+
for ( int i = 0; i < childNodeList.size(); i++ )
278+
{
279+
QDomElement childElem = childNodeList.at( i ).toElement();
280+
QgsAttributeEditorElement *myElem = attributeEditorElementFromDomElement( childElem, container );
281+
if ( myElem )
282+
container->addChildElement( myElem );
283+
}
284+
285+
newElement = container;
286+
}
287+
else if ( elem.tagName() == "attributeEditorField" )
288+
{
289+
QString name = elem.attribute( "name" );
290+
int idx = mFields.fieldNameIndex( name );
291+
newElement = new QgsAttributeEditorField( name, idx, parent );
292+
}
293+
else if ( elem.tagName() == "attributeEditorRelation" )
294+
{
295+
// At this time, the relations are not loaded
296+
// So we only grab the id and delegate the rest to onRelationsLoaded()
297+
QString name = elem.attribute( "name" );
298+
newElement = new QgsAttributeEditorRelation( name, elem.attribute( "relation", "[None]" ), parent );
299+
}
300+
return newElement;
301+
}
302+
108303
void QgsEditFormConfig::onRelationsLoaded()
109304
{
110305
Q_FOREACH ( QgsAttributeEditorElement* elem, mAttributeEditorElements )

0 commit comments

Comments
 (0)