@@ -5,7 +5,7 @@ QgsEditFormConfig::QgsEditFormConfig( QObject* parent )
5
5
: QObject( parent )
6
6
, mEditorLayout( GeneratedLayout )
7
7
, mInitCodeSource( CodeSourceNone )
8
- , mFeatureFormSuppress ( SuppressDefault )
8
+ , mSuppressForm ( SuppressDefault )
9
9
{
10
10
connect ( QgsProject::instance ()->relationManager (), SIGNAL ( relationsLoaded () ), this , SLOT ( onRelationsLoaded () ) );
11
11
}
@@ -28,20 +28,19 @@ QgsEditorWidgetConfig QgsEditFormConfig::widgetConfig( int fieldIdx ) const
28
28
if ( fieldIdx < 0 || fieldIdx >= mFields .count () )
29
29
return QgsEditorWidgetConfig ();
30
30
31
- return mEditorWidgetV2Configs .value ( mFields [fieldIdx].name () );
31
+ return mWidgetConfigs .value ( mFields [fieldIdx].name () );
32
32
}
33
33
34
- QgsEditorWidgetConfig QgsEditFormConfig::widgetConfig ( const QString& fieldName ) const
34
+ QgsEditorWidgetConfig QgsEditFormConfig::widgetConfig ( const QString& widgetName ) const
35
35
{
36
- return mEditorWidgetV2Configs .value ( fieldName );
36
+ return mWidgetConfigs .value ( widgetName );
37
37
}
38
38
39
39
void QgsEditFormConfig::setFields ( const QgsFields& fields )
40
40
{
41
41
mFields = fields;
42
42
}
43
43
44
-
45
44
void QgsEditFormConfig::setWidgetType ( int attrIdx, const QString& widgetType )
46
45
{
47
46
if ( attrIdx >= 0 && attrIdx < mFields .count () )
@@ -51,12 +50,12 @@ void QgsEditFormConfig::setWidgetType( int attrIdx, const QString& widgetType )
51
50
void QgsEditFormConfig::setWidgetConfig ( int attrIdx, const QgsEditorWidgetConfig& config )
52
51
{
53
52
if ( attrIdx >= 0 && attrIdx < mFields .count () )
54
- mEditorWidgetV2Configs [ mFields .at ( attrIdx ).name ()] = config;
53
+ mWidgetConfigs [ mFields .at ( attrIdx ).name ()] = config;
55
54
}
56
55
57
- QString QgsEditFormConfig::uiForm () const
56
+ void QgsEditFormConfig::setWidgetConfig ( const QString& widgetName, const QgsEditorWidgetConfig& config )
58
57
{
59
- return mEditForm ;
58
+ mWidgetConfigs [widgetName] = config ;
60
59
}
61
60
62
61
void QgsEditFormConfig::setUiForm ( const QString& ui )
@@ -69,10 +68,10 @@ void QgsEditFormConfig::setUiForm( const QString& ui )
69
68
{
70
69
setLayout ( UiFileLayout );
71
70
}
72
- mEditForm = ui;
71
+ mUiFormPath = ui;
73
72
}
74
73
75
- bool QgsEditFormConfig::readOnly ( int idx )
74
+ bool QgsEditFormConfig::readOnly ( int idx ) const
76
75
{
77
76
if ( idx >= 0 && idx < mFields .count () )
78
77
{
@@ -85,7 +84,7 @@ bool QgsEditFormConfig::readOnly( int idx )
85
84
return false ;
86
85
}
87
86
88
- bool QgsEditFormConfig::labelOnTop ( int idx )
87
+ bool QgsEditFormConfig::labelOnTop ( int idx ) const
89
88
{
90
89
if ( idx >= 0 && idx < mFields .count () )
91
90
return mLabelOnTop .value ( mFields .at ( idx ).name (), false );
@@ -105,6 +104,202 @@ void QgsEditFormConfig::setLabelOnTop( int idx, bool onTop )
105
104
mLabelOnTop [ mFields .at ( idx ).name ()] = onTop;
106
105
}
107
106
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
+
108
303
void QgsEditFormConfig::onRelationsLoaded ()
109
304
{
110
305
Q_FOREACH ( QgsAttributeEditorElement* elem, mAttributeEditorElements )
0 commit comments