-
-
Notifications
You must be signed in to change notification settings - Fork 3k
/
qgseditorwidgetregistry.cpp
378 lines (316 loc) · 13 KB
/
qgseditorwidgetregistry.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/***************************************************************************
qgseditorwidgetregistry.cpp
--------------------------------------
Date : 24.4.2013
Copyright : (C) 2013 Matthias Kuhn
Email : matthias dot kuhn at gmx dot ch
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "qgseditorwidgetregistry.h"
#include "qgsattributeeditorcontext.h"
#include "qgslegacyhelpers.h"
#include "qgsmessagelog.h"
#include "qgsproject.h"
#include "qgsvectorlayer.h"
#include "qgsmaplayerregistry.h"
// Editors
#include "qgsclassificationwidgetwrapperfactory.h"
#include "qgsrangewidgetfactory.h"
#include "qgsuniquevaluewidgetfactory.h"
#include "qgsfilenamewidgetfactory.h"
#include "qgsvaluemapwidgetfactory.h"
#include "qgsenumerationwidgetfactory.h"
#include "qgshiddenwidgetfactory.h"
#include "qgscheckboxwidgetfactory.h"
#include "qgstexteditwidgetfactory.h"
#include "qgsvaluerelationwidgetfactory.h"
#include "qgsuuidwidgetfactory.h"
#include "qgsphotowidgetfactory.h"
#ifdef WITH_QTWEBKIT
#include "qgswebviewwidgetfactory.h"
#endif
#include "qgscolorwidgetfactory.h"
#include "qgsrelationreferencefactory.h"
#include "qgsdatetimeeditfactory.h"
QgsEditorWidgetRegistry* QgsEditorWidgetRegistry::instance()
{
static QgsEditorWidgetRegistry sInstance;
return &sInstance;
}
void QgsEditorWidgetRegistry::initEditors( QgsMapCanvas *mapCanvas, QgsMessageBar *messageBar )
{
QgsEditorWidgetRegistry *reg = instance();
reg->registerWidget( "Classification", new QgsClassificationWidgetWrapperFactory( tr( "Classification" ) ) );
reg->registerWidget( "Range", new QgsRangeWidgetFactory( tr( "Range" ) ) );
reg->registerWidget( "UniqueValues", new QgsUniqueValueWidgetFactory( tr( "Unique Values" ) ) );
reg->registerWidget( "FileName", new QgsFileNameWidgetFactory( tr( "File Name" ) ) );
reg->registerWidget( "ValueMap", new QgsValueMapWidgetFactory( tr( "Value Map" ) ) );
reg->registerWidget( "Enumeration", new QgsEnumerationWidgetFactory( tr( "Enumeration" ) ) );
reg->registerWidget( "Hidden", new QgsHiddenWidgetFactory( tr( "Hidden" ) ) );
reg->registerWidget( "CheckBox", new QgsCheckboxWidgetFactory( tr( "Check Box" ) ) );
reg->registerWidget( "TextEdit", new QgsTextEditWidgetFactory( tr( "Text Edit" ) ) );
reg->registerWidget( "ValueRelation", new QgsValueRelationWidgetFactory( tr( "Value Relation" ) ) );
reg->registerWidget( "UuidGenerator", new QgsUuidWidgetFactory( tr( "Uuid Generator" ) ) );
reg->registerWidget( "Photo", new QgsPhotoWidgetFactory( tr( "Photo" ) ) );
#ifdef WITH_QTWEBKIT
reg->registerWidget( "WebView", new QgsWebViewWidgetFactory( tr( "Web View" ) ) );
#endif
reg->registerWidget( "Color", new QgsColorWidgetFactory( tr( "Color" ) ) );
reg->registerWidget( "RelationReference", new QgsRelationReferenceFactory( tr( "Relation Reference" ), mapCanvas, messageBar ) );
reg->registerWidget( "DateTime", new QgsDateTimeEditFactory( tr( "Date/Time" ) ) );
}
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( QgsMapLayerRegistry::instance(), SIGNAL( layerWasAdded( QgsMapLayer* ) ), this, SLOT( mapLayerAdded( QgsMapLayer* ) ) );
}
QgsEditorWidgetRegistry::~QgsEditorWidgetRegistry()
{
qDeleteAll( mWidgetFactories.values() );
}
QgsEditorWidgetWrapper* QgsEditorWidgetRegistry::create( const QString& widgetId, QgsVectorLayer* vl, int fieldIdx, const QgsEditorWidgetConfig& config, QWidget* editor, QWidget* parent, const QgsAttributeEditorContext &context )
{
if ( mWidgetFactories.contains( widgetId ) )
{
QgsEditorWidgetWrapper* ww = mWidgetFactories[widgetId]->create( vl, fieldIdx, editor, parent );
if ( ww )
{
ww->setConfig( config );
ww->setContext( context );
// Make sure that there is a widget created at this point
// so setValue() et al won't crash
ww->widget();
// If we tried to set a widget which is not supported by this wrapper
if ( !ww->valid() )
{
delete ww;
QString wid = findSuitableWrapper( editor, "TextEdit" );
ww = mWidgetFactories[wid]->create( vl, fieldIdx, editor, parent );
ww->setConfig( config );
ww->setContext( context );
}
return ww;
}
}
return 0;
}
QgsSearchWidgetWrapper* QgsEditorWidgetRegistry::createSearchWidget( const QString& widgetId, QgsVectorLayer* vl, int fieldIdx, const QgsEditorWidgetConfig& config, QWidget* parent, const QgsAttributeEditorContext &context )
{
if ( mWidgetFactories.contains( widgetId ) )
{
QgsSearchWidgetWrapper* ww = mWidgetFactories[widgetId]->createSearchWidget( vl, fieldIdx, parent );
if ( ww )
{
ww->setConfig( config );
ww->setContext( context );
// Make sure that there is a widget created at this point
// so setValue() et al won't crash
ww->widget();
return ww;
}
}
return 0;
}
QgsEditorConfigWidget* QgsEditorWidgetRegistry::createConfigWidget( const QString& widgetId, QgsVectorLayer* vl, int fieldIdx, QWidget* parent )
{
if ( mWidgetFactories.contains( widgetId ) )
{
return mWidgetFactories[widgetId]->configWidget( vl, fieldIdx, parent );
}
return 0;
}
QString QgsEditorWidgetRegistry::name( const QString& widgetId )
{
if ( mWidgetFactories.contains( widgetId ) )
{
return mWidgetFactories[widgetId]->name();
}
return QString();
}
const QMap<QString, QgsEditorWidgetFactory*>& QgsEditorWidgetRegistry::factories()
{
return mWidgetFactories;
}
QgsEditorWidgetFactory* QgsEditorWidgetRegistry::factory( const QString& widgetId )
{
return mWidgetFactories.value( widgetId );
}
bool QgsEditorWidgetRegistry::registerWidget( const QString& widgetId, QgsEditorWidgetFactory* widgetFactory )
{
if ( !widgetFactory )
{
QgsMessageLog::instance()->logMessage( "QgsEditorWidgetRegistry: Factory not valid." );
return false;
}
else if ( mWidgetFactories.contains( widgetId ) )
{
QgsMessageLog::instance()->logMessage( QString( "QgsEditorWidgetRegistry: Factory with id %1 already registered." ).arg( widgetId ) );
return false;
}
else
{
mWidgetFactories.insert( widgetId, widgetFactory );
// Use this factory as default where it provides the heighest priority
QMap<const char*, int> types = widgetFactory->supportedWidgetTypes();
QMap<const char*, int>::ConstIterator it;
it = types.constBegin();
for ( ; it != types.constEnd(); ++it )
{
if ( it.value() > mFactoriesByType[it.key()].first )
{
mFactoriesByType[it.key()] = qMakePair( it.value(), widgetId );
}
}
return true;
}
}
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( "edittypes" ).childNodes();
for ( int i = 0; i < editTypeNodes.size(); i++ )
{
QDomNode editTypeNode = editTypeNodes.at( i );
QDomElement editTypeElement = editTypeNode.toElement();
QString name = editTypeElement.attribute( "name" );
int idx = vectorLayer->fieldNameIndex( name );
if ( idx == -1 )
continue;
bool hasLegacyType;
QgsVectorLayer::EditType editType =
( QgsVectorLayer::EditType ) editTypeElement.attribute( "type" ).toInt( &hasLegacyType );
QString ewv2Type;
QgsEditorWidgetConfig cfg;
if ( hasLegacyType && editType != QgsVectorLayer::EditorWidgetV2 )
{
Q_NOWARN_DEPRECATED_PUSH
ewv2Type = readLegacyConfig( vectorLayer, editTypeElement, cfg );
Q_NOWARN_DEPRECATED_POP
}
else
ewv2Type = editTypeElement.attribute( "widgetv2type" );
if ( mWidgetFactories.contains( ewv2Type ) )
{
vectorLayer->setEditorWidgetV2( idx, ewv2Type );
QDomElement ewv2CfgElem = editTypeElement.namedItem( "widgetv2config" ).toElement();
if ( !ewv2CfgElem.isNull() )
{
cfg = mWidgetFactories[ewv2Type]->readEditorConfig( ewv2CfgElem, vectorLayer, idx );
}
vectorLayer->setFieldEditable( idx, ewv2CfgElem.attribute( "fieldEditable", "1" ) == "1" );
vectorLayer->setLabelOnTop( idx, ewv2CfgElem.attribute( "labelOnTop", "0" ) == "1" );
vectorLayer->setEditorWidgetV2Config( idx, cfg );
}
else
{
QgsMessageLog::logMessage( tr( "Unknown attribute editor widget '%1'" ).arg( ewv2Type ) );
}
}
}
const QString QgsEditorWidgetRegistry::readLegacyConfig( QgsVectorLayer* vl, const QDomElement& editTypeElement, QgsEditorWidgetConfig& cfg )
{
QString name = editTypeElement.attribute( "name" );
QgsVectorLayer::EditType editType = ( QgsVectorLayer::EditType ) editTypeElement.attribute( "type" ).toInt();
Q_NOWARN_DEPRECATED_PUSH
return QgsLegacyHelpers::convertEditType( editType, cfg, vl, name, editTypeElement );
Q_NOWARN_DEPRECATED_POP
}
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( "edittypes" );
QgsFields fields = vectorLayer->fields();
for ( int idx = 0; idx < fields.count(); ++idx )
{
const QgsField &field = fields[ idx ];
const QString& widgetType = vectorLayer->editorWidgetV2( idx );
if ( !mWidgetFactories.contains( widgetType ) )
{
QgsMessageLog::logMessage( tr( "Could not save unknown editor widget type '%1'." ).arg( widgetType ) );
continue;
}
QDomElement editTypeElement = doc.createElement( "edittype" );
editTypeElement.setAttribute( "name", field.name() );
editTypeElement.setAttribute( "widgetv2type", widgetType );
if ( mWidgetFactories.contains( widgetType ) )
{
QDomElement ewv2CfgElem = doc.createElement( "widgetv2config" );
ewv2CfgElem.setAttribute( "fieldEditable", vectorLayer->fieldEditable( idx ) );
ewv2CfgElem.setAttribute( "labelOnTop", vectorLayer->labelOnTop( idx ) );
mWidgetFactories[widgetType]->writeConfig( vectorLayer->editorWidgetV2Config( idx ), ewv2CfgElem, doc, vectorLayer, idx );
editTypeElement.appendChild( ewv2CfgElem );
}
editTypesNode.appendChild( editTypeElement );
}
layerElem.appendChild( editTypesNode );
}
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;
QString widgetid;
int weight = 0;
it = mFactoriesByType.constBegin();
for ( ; it != mFactoriesByType.constEnd(); ++it )
{
if ( editor->staticMetaObject.className() == it.key() )
{
// if it's a perfect match: return it directly
return it.value().second;
}
else if ( editor->inherits( it.key() ) )
{
// if it's a subclass, continue evaluating, maybe we find a more-specific or one with more weight
if ( it.value().first > weight )
{
weight = it.value().first;
widgetid = it.value().second;
}
}
}
if ( widgetid.isNull() )
widgetid = defaultWidget;
return widgetid;
}