QGIS API Documentation  2.11.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
qgseditorwidgetregistry.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgseditorwidgetregistry.cpp
3  --------------------------------------
4  Date : 24.4.2013
5  Copyright : (C) 2013 Matthias Kuhn
6  Email : matthias dot kuhn at gmx dot ch
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 
17 
19 #include "qgslegacyhelpers.h"
20 #include "qgsmessagelog.h"
21 #include "qgsproject.h"
22 #include "qgsvectorlayer.h"
23 #include "qgsmaplayerregistry.h"
24 
25 // Editors
27 #include "qgsrangewidgetfactory.h"
32 #include "qgshiddenwidgetfactory.h"
36 #include "qgsuuidwidgetfactory.h"
37 #include "qgsphotowidgetfactory.h"
39 #include "qgscolorwidgetfactory.h"
41 #include "qgsdatetimeeditfactory.h"
42 
44 {
45  static QgsEditorWidgetRegistry sInstance;
46  return &sInstance;
47 }
48 
50 {
52  reg->registerWidget( "Classification", new QgsClassificationWidgetWrapperFactory( tr( "Classification" ) ) );
53  reg->registerWidget( "Range", new QgsRangeWidgetFactory( tr( "Range" ) ) );
54  reg->registerWidget( "UniqueValues", new QgsUniqueValueWidgetFactory( tr( "Unique Values" ) ) );
55  reg->registerWidget( "FileName", new QgsFileNameWidgetFactory( tr( "File Name" ) ) );
56  reg->registerWidget( "ValueMap", new QgsValueMapWidgetFactory( tr( "Value Map" ) ) );
57  reg->registerWidget( "Enumeration", new QgsEnumerationWidgetFactory( tr( "Enumeration" ) ) );
58  reg->registerWidget( "Hidden", new QgsHiddenWidgetFactory( tr( "Hidden" ) ) );
59  reg->registerWidget( "CheckBox", new QgsCheckboxWidgetFactory( tr( "Check Box" ) ) );
60  reg->registerWidget( "TextEdit", new QgsTextEditWidgetFactory( tr( "Text Edit" ) ) );
61  reg->registerWidget( "ValueRelation", new QgsValueRelationWidgetFactory( tr( "Value Relation" ) ) );
62  reg->registerWidget( "UuidGenerator", new QgsUuidWidgetFactory( tr( "Uuid Generator" ) ) );
63  reg->registerWidget( "Photo", new QgsPhotoWidgetFactory( tr( "Photo" ) ) );
64 #ifdef WITH_QTWEBKIT
65  reg->registerWidget( "WebView", new QgsWebViewWidgetFactory( tr( "Web View" ) ) );
66 #endif
67  reg->registerWidget( "Color", new QgsColorWidgetFactory( tr( "Color" ) ) );
68  reg->registerWidget( "RelationReference", new QgsRelationReferenceFactory( tr( "Relation Reference" ), mapCanvas, messageBar ) );
69  reg->registerWidget( "DateTime", new QgsDateTimeEditFactory( tr( "Date/Time" ) ) );
70 }
71 
73 {
74  connect( QgsProject::instance(), SIGNAL( readMapLayer( QgsMapLayer*, const QDomElement& ) ), this, SLOT( readMapLayer( QgsMapLayer*, const QDomElement& ) ) );
75  connect( QgsProject::instance(), SIGNAL( writeMapLayer( QgsMapLayer*, QDomElement&, QDomDocument& ) ), this, SLOT( writeMapLayer( QgsMapLayer*, QDomElement&, QDomDocument& ) ) );
76 
77  connect( QgsMapLayerRegistry::instance(), SIGNAL( layerWasAdded( QgsMapLayer* ) ), this, SLOT( mapLayerAdded( QgsMapLayer* ) ) );
78 }
79 
81 {
82  qDeleteAll( mWidgetFactories.values() );
83 }
84 
85 QgsEditorWidgetWrapper* QgsEditorWidgetRegistry::create( const QString& widgetId, QgsVectorLayer* vl, int fieldIdx, const QgsEditorWidgetConfig& config, QWidget* editor, QWidget* parent, const QgsAttributeEditorContext &context )
86 {
87  if ( mWidgetFactories.contains( widgetId ) )
88  {
89  QgsEditorWidgetWrapper* ww = mWidgetFactories[widgetId]->create( vl, fieldIdx, editor, parent );
90 
91  if ( ww )
92  {
93  ww->setConfig( config );
94  ww->setContext( context );
95  // Make sure that there is a widget created at this point
96  // so setValue() et al won't crash
97  ww->widget();
98 
99  // If we tried to set a widget which is not supported by this wrapper
100  if ( !ww->valid() )
101  {
102  delete ww;
103  QString wid = findSuitableWrapper( editor );
104  ww = mWidgetFactories[wid]->create( vl, fieldIdx, editor, parent );
105  ww->setConfig( config );
106  ww->setContext( context );
107  }
108 
109  return ww;
110  }
111  }
112 
113  return 0;
114 }
115 
117 {
118  if ( mWidgetFactories.contains( widgetId ) )
119  {
120  QgsSearchWidgetWrapper* ww = mWidgetFactories[widgetId]->createSearchWidget( vl, fieldIdx, parent );
121 
122  if ( ww )
123  {
124  ww->setConfig( config );
125  ww->setContext( context );
126  // Make sure that there is a widget created at this point
127  // so setValue() et al won't crash
128  ww->widget();
129  return ww;
130  }
131  }
132  return 0;
133 }
134 
136 {
137  if ( mWidgetFactories.contains( widgetId ) )
138  {
139  return mWidgetFactories[widgetId]->configWidget( vl, fieldIdx, parent );
140  }
141  return 0;
142 }
143 
145 {
146  if ( mWidgetFactories.contains( widgetId ) )
147  {
148  return mWidgetFactories[widgetId]->name();
149  }
150 
151  return QString();
152 }
153 
155 {
156  return mWidgetFactories;
157 }
158 
160 {
161  return mWidgetFactories.value( widgetId );
162 }
163 
165 {
166  if ( !widgetFactory )
167  {
168  QgsMessageLog::instance()->logMessage( "QgsEditorWidgetRegistry: Factory not valid." );
169  return false;
170  }
171  else if ( mWidgetFactories.contains( widgetId ) )
172  {
173  QgsMessageLog::instance()->logMessage( QString( "QgsEditorWidgetRegistry: Factory with id %1 already registered." ).arg( widgetId ) );
174  return false;
175  }
176  else
177  {
178  mWidgetFactories.insert( widgetId, widgetFactory );
179 
180  // Use this factory as default where it provides the heighest priority
181  QMap<const char*, int> types = widgetFactory->supportedWidgetTypes();
183  it = types.constBegin();
184 
185  for ( ; it != types.constEnd(); ++it )
186  {
187  if ( it.value() > mFactoriesByType[it.key()].first )
188  {
189  mFactoriesByType[it.key()] = qMakePair( it.value(), widgetFactory );
190  }
191  }
192 
193  return true;
194  }
195 }
196 
197 void QgsEditorWidgetRegistry::readMapLayer( QgsMapLayer* mapLayer, const QDomElement& layerElem )
198 {
199  if ( mapLayer->type() != QgsMapLayer::VectorLayer )
200  {
201  return;
202  }
203 
204  QgsVectorLayer* vectorLayer = qobject_cast<QgsVectorLayer*>( mapLayer );
205  Q_ASSERT( vectorLayer );
206 
207  QDomNodeList editTypeNodes = layerElem.namedItem( "edittypes" ).childNodes();
208 
209  for ( int i = 0; i < editTypeNodes.size(); i++ )
210  {
211  QDomNode editTypeNode = editTypeNodes.at( i );
212  QDomElement editTypeElement = editTypeNode.toElement();
213 
214  QString name = editTypeElement.attribute( "name" );
215 
216  int idx = vectorLayer->fieldNameIndex( name );
217  if ( idx == -1 )
218  continue;
219 
220  bool hasLegacyType;
221  QgsVectorLayer::EditType editType =
222  ( QgsVectorLayer::EditType ) editTypeElement.attribute( "type" ).toInt( &hasLegacyType );
223 
224  QString ewv2Type;
226 
227  if ( hasLegacyType && editType != QgsVectorLayer::EditorWidgetV2 )
228  {
230  ewv2Type = readLegacyConfig( vectorLayer, editTypeElement, cfg );
232  }
233  else
234  ewv2Type = editTypeElement.attribute( "widgetv2type" );
235 
236  if ( mWidgetFactories.contains( ewv2Type ) )
237  {
238  vectorLayer->setEditorWidgetV2( idx, ewv2Type );
239  QDomElement ewv2CfgElem = editTypeElement.namedItem( "widgetv2config" ).toElement();
240 
241  if ( !ewv2CfgElem.isNull() )
242  {
243  cfg = mWidgetFactories[ewv2Type]->readEditorConfig( ewv2CfgElem, vectorLayer, idx );
244  }
245 
246  vectorLayer->setFieldEditable( idx, ewv2CfgElem.attribute( "fieldEditable", "1" ) == "1" );
247  vectorLayer->setLabelOnTop( idx, ewv2CfgElem.attribute( "labelOnTop", "0" ) == "1" );
248  vectorLayer->setEditorWidgetV2Config( idx, cfg );
249  }
250  else
251  {
252  QgsMessageLog::logMessage( tr( "Unknown attribute editor widget '%1'" ).arg( ewv2Type ) );
253  }
254  }
255 }
256 
257 const QString QgsEditorWidgetRegistry::readLegacyConfig( QgsVectorLayer* vl, const QDomElement& editTypeElement, QgsEditorWidgetConfig& cfg )
258 {
259  QString name = editTypeElement.attribute( "name" );
260 
261  QgsVectorLayer::EditType editType = ( QgsVectorLayer::EditType ) editTypeElement.attribute( "type" ).toInt();
262 
264  return QgsLegacyHelpers::convertEditType( editType, cfg, vl, name, editTypeElement );
266 }
267 
268 void QgsEditorWidgetRegistry::writeMapLayer( QgsMapLayer* mapLayer, QDomElement& layerElem, QDomDocument& doc ) const
269 {
270  if ( mapLayer->type() != QgsMapLayer::VectorLayer )
271  {
272  return;
273  }
274 
275  QgsVectorLayer* vectorLayer = qobject_cast<QgsVectorLayer*>( mapLayer );
276  if ( !vectorLayer )
277  {
278  return;
279  }
280 
281  QDomNode editTypesNode = doc.createElement( "edittypes" );
282 
283  for ( int idx = 0; idx < vectorLayer->pendingFields().count(); ++idx )
284  {
285  const QgsField &field = vectorLayer->pendingFields()[ idx ];
286  const QString& widgetType = vectorLayer->editorWidgetV2( idx );
287  if ( !mWidgetFactories.contains( widgetType ) )
288  {
289  QgsMessageLog::logMessage( tr( "Could not save unknown editor widget type '%1'." ).arg( widgetType ) );
290  continue;
291  }
292 
293 
294  QDomElement editTypeElement = doc.createElement( "edittype" );
295  editTypeElement.setAttribute( "name", field.name() );
296  editTypeElement.setAttribute( "widgetv2type", widgetType );
297 
298  if ( mWidgetFactories.contains( widgetType ) )
299  {
300  QDomElement ewv2CfgElem = doc.createElement( "widgetv2config" );
301  ewv2CfgElem.setAttribute( "fieldEditable", vectorLayer->fieldEditable( idx ) );
302  ewv2CfgElem.setAttribute( "labelOnTop", vectorLayer->labelOnTop( idx ) );
303 
304  mWidgetFactories[widgetType]->writeConfig( vectorLayer->editorWidgetV2Config( idx ), ewv2CfgElem, doc, vectorLayer, idx );
305 
306  editTypeElement.appendChild( ewv2CfgElem );
307  }
308 
309  editTypesNode.appendChild( editTypeElement );
310  }
311 
312  layerElem.appendChild( editTypesNode );
313 }
314 
315 void QgsEditorWidgetRegistry::mapLayerAdded( QgsMapLayer* mapLayer )
316 {
317  QgsVectorLayer* vl = qobject_cast<QgsVectorLayer*>( mapLayer );
318 
319  if ( vl )
320  {
321  connect( vl, SIGNAL( readCustomSymbology( const QDomElement&, QString& ) ), this, SLOT( readSymbology( const QDomElement&, QString& ) ) );
322  connect( vl, SIGNAL( writeCustomSymbology( QDomElement&, QDomDocument&, QString& ) ), this, SLOT( writeSymbology( QDomElement&, QDomDocument&, QString& ) ) );
323  }
324 }
325 
326 void QgsEditorWidgetRegistry::readSymbology( const QDomElement& element, QString& errorMessage )
327 {
328  Q_UNUSED( errorMessage )
329  QgsVectorLayer* vl = qobject_cast<QgsVectorLayer*>( sender() );
330 
331  Q_ASSERT( vl );
332 
333  readMapLayer( vl, element );
334 }
335 
336 void QgsEditorWidgetRegistry::writeSymbology( QDomElement& element, QDomDocument& doc, QString& errorMessage )
337 {
338  Q_UNUSED( errorMessage )
339  QgsVectorLayer* vl = qobject_cast<QgsVectorLayer*>( sender() );
340 
341  Q_ASSERT( vl );
342 
343  writeMapLayer( vl, element, doc );
344 }
345 
346 QString QgsEditorWidgetRegistry::findSuitableWrapper( QWidget* editor )
347 {
349 
350  QString widgetid;
351  int weight = 0;
352 
353  it = mFactoriesByType.constBegin();
354  for ( ; it != mFactoriesByType.constEnd(); ++it )
355  {
356  if ( editor->staticMetaObject.className() == it.key() )
357  {
358  // if it's a perfect match: return it directly
359  return it.value().second->name();
360  }
361  else if ( editor->inherits( it.key() ) )
362  {
363  // if it's a subclass, continue evaluating, maybe we find a more-specific or one with more weight
364  if ( it.value().first > weight )
365  {
366  weight = it.value().first;
367  widgetid = it.value().second->name();
368  }
369  }
370  }
371 
372  return widgetid;
373 }
const QgsEditorWidgetConfig editorWidgetV2Config(int fieldIdx) const
Get the configuration for the editor widget used to represent the field at the given index...
virtual bool valid()=0
Return true if the widget has been properly initialized.
const QString & name() const
Gets the name of the field.
Definition: qgsfield.cpp:69
Manages an editor widget Widget and wrapper share the same parent.
bool fieldEditable(int idx)
Is edit widget editable.
Base class for all map layer types.
Definition: qgsmaplayer.h:49
QgsMapLayer::LayerType type() const
Get the type of the layer.
Definition: qgsmaplayer.cpp:93
virtual QMap< const char *, int > supportedWidgetTypes()
Returns a list of widget types which this editor widget supports.
bool contains(const Key &key) const
This class should be subclassed for every configurable editor widget type.
QList< T > values() const
QDomNode appendChild(const QDomNode &newChild)
QgsEditorWidgetFactory * factory(const QString &widgetId)
Get a factory for the given widget type id.
QString attribute(const QString &name, const QString &defValue) const
This class manages all known edit widget factories.
This class contains context information for attribute editor widgets.
QObject * sender() const
Manages an editor widget Widget and wrapper share the same parent.
const_iterator constBegin() const
#define Q_NOWARN_DEPRECATED_PUSH
Definition: qgis.h:464
A bar for displaying non-blocking messages to the user.
Definition: qgsmessagebar.h:42
QgsEditorConfigWidget * createConfigWidget(const QString &widgetId, QgsVectorLayer *vl, int fieldIdx, QWidget *parent)
Creates a configuration widget.
QDomNodeList childNodes() const
static QgsEditorWidgetRegistry * instance()
This class is a singleton and has therefore to be accessed with this method instead of a constructor...
QString tr(const char *sourceText, const char *disambiguation, int n)
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:105
static void logMessage(QString message, QString tag=QString::null, MessageLevel level=WARNING)
add a message to the instance (and create it if necessary)
QDomElement toElement() const
void setContext(const QgsAttributeEditorContext &context)
Set the context in which this widget is shown.
const char * name() const
void setConfig(const QgsEditorWidgetConfig &config)
Will set the config of this wrapper to the specified config.
void setAttribute(const QString &name, const QString &value)
QgsEditorWidgetWrapper * create(const QString &widgetId, QgsVectorLayer *vl, int fieldIdx, const QgsEditorWidgetConfig &config, QWidget *editor, QWidget *parent, const QgsAttributeEditorContext &context=QgsAttributeEditorContext())
Create an attribute editor widget wrapper of a given type for a given field.
const QString editorWidgetV2(int fieldIdx) const
Get the id for the editor widget used to represent the field at the given index.
int toInt(bool *ok, int base) const
const QMap< QString, QgsEditorWidgetFactory * > & factories()
Get access to all registered factories.
const_iterator constEnd() const
Every attribute editor widget needs a factory, which inherits this class.
int count() const
Return number of items.
Definition: qgsfield.cpp:283
Encapsulate a field in an attribute table or data source.
Definition: qgsfield.h:38
bool labelOnTop(int idx)
Label widget on top.
QDomNode namedItem(const QString &name) const
void setEditorWidgetV2Config(int attrIdx, const QgsEditorWidgetConfig &config)
Set the editor widget config for a field.
bool isNull() const
#define Q_NOWARN_DEPRECATED_POP
Definition: qgis.h:465
const Key key(const T &value) const
void setEditorWidgetV2(int attrIdx, const QString &widgetType)
Set the editor widget type for a field.
QgsSearchWidgetWrapper * createSearchWidget(const QString &widgetId, QgsVectorLayer *vl, int fieldIdx, const QgsEditorWidgetConfig &config, QWidget *parent, const QgsAttributeEditorContext &context=QgsAttributeEditorContext())
static QgsMapLayerRegistry * instance()
Returns the instance pointer, creating the object on the first call.
static Q_DECL_DEPRECATED const QString convertEditType(QgsVectorLayer::EditType editType, QgsEditorWidgetConfig &cfg, QgsVectorLayer *vl, const QString &name, const QDomElement &editTypeElement=QDomElement())
static QgsMessageLog * instance()
static QgsProject * instance()
access to canonical QgsProject instance
Definition: qgsproject.cpp:351
void setFieldEditable(int idx, bool editable)
Set edit widget editable.
iterator insert(const Key &key, const T &value)
QWidget * widget()
Access the widget managed by this wrapper.
static void initEditors(QgsMapCanvas *mapCanvas=0, QgsMessageBar *messageBar=0)
Registers all the default widgets.
const QgsFields & pendingFields() const
Returns field list in the to-be-committed state.
void setLabelOnTop(int idx, bool onTop)
Label widget on top.
bool registerWidget(const QString &widgetId, QgsEditorWidgetFactory *widgetFactory)
Register a new widget factory with the given id.
int size() const
QDomElement createElement(const QString &tagName)
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
Represents a vector layer which manages a vector based data sets.
int fieldNameIndex(const QString &fieldName) const
Returns the index of a field name or -1 if the field does not exist.
modularized edit widgets
QDomNode at(int index) const
const T value(const Key &key) const