QGIS API Documentation  2.11.0-Master
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  return ww;
99  }
100  }
101  return 0;
102 }
103 
105 {
106  if ( mWidgetFactories.contains( widgetId ) )
107  {
108  QgsSearchWidgetWrapper* ww = mWidgetFactories[widgetId]->createSearchWidget( vl, fieldIdx, parent );
109 
110  if ( ww )
111  {
112  ww->setConfig( config );
113  ww->setContext( context );
114  // Make sure that there is a widget created at this point
115  // so setValue() et al won't crash
116  ww->widget();
117  return ww;
118  }
119  }
120  return 0;
121 }
122 
124 {
125  if ( mWidgetFactories.contains( widgetId ) )
126  {
127  return mWidgetFactories[widgetId]->configWidget( vl, fieldIdx, parent );
128  }
129  return 0;
130 }
131 
133 {
134  if ( mWidgetFactories.contains( widgetId ) )
135  {
136  return mWidgetFactories[widgetId]->name();
137  }
138 
139  return QString();
140 }
141 
143 {
144  return mWidgetFactories;
145 }
146 
148 {
149  return mWidgetFactories.value( widgetId );
150 }
151 
153 {
154  if ( !widgetFactory )
155  {
156  QgsMessageLog::instance()->logMessage( "QgsEditorWidgetRegistry: Factory not valid." );
157  return false;
158  }
159  else if ( mWidgetFactories.contains( widgetId ) )
160  {
161  QgsMessageLog::instance()->logMessage( QString( "QgsEditorWidgetRegistry: Factory with id %1 already registered." ).arg( widgetId ) );
162  return false;
163  }
164  else
165  {
166  mWidgetFactories.insert( widgetId, widgetFactory );
167  return true;
168  }
169 }
170 
171 void QgsEditorWidgetRegistry::readMapLayer( QgsMapLayer* mapLayer, const QDomElement& layerElem )
172 {
173  if ( mapLayer->type() != QgsMapLayer::VectorLayer )
174  {
175  return;
176  }
177 
178  QgsVectorLayer* vectorLayer = qobject_cast<QgsVectorLayer*>( mapLayer );
179  Q_ASSERT( vectorLayer );
180 
181  QDomNodeList editTypeNodes = layerElem.namedItem( "edittypes" ).childNodes();
182 
183  for ( int i = 0; i < editTypeNodes.size(); i++ )
184  {
185  QDomNode editTypeNode = editTypeNodes.at( i );
186  QDomElement editTypeElement = editTypeNode.toElement();
187 
188  QString name = editTypeElement.attribute( "name" );
189 
190  int idx = vectorLayer->fieldNameIndex( name );
191  if ( idx == -1 )
192  continue;
193 
194  bool hasLegacyType;
195  QgsVectorLayer::EditType editType =
196  ( QgsVectorLayer::EditType ) editTypeElement.attribute( "type" ).toInt( &hasLegacyType );
197 
198  QString ewv2Type;
200 
201  if ( hasLegacyType && editType != QgsVectorLayer::EditorWidgetV2 )
202  {
204  ewv2Type = readLegacyConfig( vectorLayer, editTypeElement, cfg );
206  }
207  else
208  ewv2Type = editTypeElement.attribute( "widgetv2type" );
209 
210  if ( mWidgetFactories.contains( ewv2Type ) )
211  {
212  vectorLayer->setEditorWidgetV2( idx, ewv2Type );
213  QDomElement ewv2CfgElem = editTypeElement.namedItem( "widgetv2config" ).toElement();
214 
215  if ( !ewv2CfgElem.isNull() )
216  {
217  cfg = mWidgetFactories[ewv2Type]->readEditorConfig( ewv2CfgElem, vectorLayer, idx );
218  }
219 
220  vectorLayer->setFieldEditable( idx, ewv2CfgElem.attribute( "fieldEditable", "1" ) == "1" );
221  vectorLayer->setLabelOnTop( idx, ewv2CfgElem.attribute( "labelOnTop", "0" ) == "1" );
222  vectorLayer->setEditorWidgetV2Config( idx, cfg );
223  }
224  else
225  {
226  QgsMessageLog::logMessage( tr( "Unknown attribute editor widget '%1'" ).arg( ewv2Type ) );
227  }
228  }
229 }
230 
231 const QString QgsEditorWidgetRegistry::readLegacyConfig( QgsVectorLayer* vl, const QDomElement& editTypeElement, QgsEditorWidgetConfig& cfg )
232 {
233  QString name = editTypeElement.attribute( "name" );
234 
235  QgsVectorLayer::EditType editType = ( QgsVectorLayer::EditType ) editTypeElement.attribute( "type" ).toInt();
236 
238  return QgsLegacyHelpers::convertEditType( editType, cfg, vl, name, editTypeElement );
240 }
241 
242 void QgsEditorWidgetRegistry::writeMapLayer( QgsMapLayer* mapLayer, QDomElement& layerElem, QDomDocument& doc ) const
243 {
244  if ( mapLayer->type() != QgsMapLayer::VectorLayer )
245  {
246  return;
247  }
248 
249  QgsVectorLayer* vectorLayer = qobject_cast<QgsVectorLayer*>( mapLayer );
250  if ( !vectorLayer )
251  {
252  return;
253  }
254 
255  QDomNode editTypesNode = doc.createElement( "edittypes" );
256 
257  for ( int idx = 0; idx < vectorLayer->pendingFields().count(); ++idx )
258  {
259  const QgsField &field = vectorLayer->pendingFields()[ idx ];
260  const QString& widgetType = vectorLayer->editorWidgetV2( idx );
261  if ( !mWidgetFactories.contains( widgetType ) )
262  {
263  QgsMessageLog::logMessage( tr( "Could not save unknown editor widget type '%1'." ).arg( widgetType ) );
264  continue;
265  }
266 
267 
268  QDomElement editTypeElement = doc.createElement( "edittype" );
269  editTypeElement.setAttribute( "name", field.name() );
270  editTypeElement.setAttribute( "widgetv2type", widgetType );
271 
272  if ( mWidgetFactories.contains( widgetType ) )
273  {
274  QDomElement ewv2CfgElem = doc.createElement( "widgetv2config" );
275  ewv2CfgElem.setAttribute( "fieldEditable", vectorLayer->fieldEditable( idx ) );
276  ewv2CfgElem.setAttribute( "labelOnTop", vectorLayer->labelOnTop( idx ) );
277 
278  mWidgetFactories[widgetType]->writeConfig( vectorLayer->editorWidgetV2Config( idx ), ewv2CfgElem, doc, vectorLayer, idx );
279 
280  editTypeElement.appendChild( ewv2CfgElem );
281  }
282 
283  editTypesNode.appendChild( editTypeElement );
284  }
285 
286  layerElem.appendChild( editTypesNode );
287 }
288 
289 void QgsEditorWidgetRegistry::mapLayerAdded( QgsMapLayer* mapLayer )
290 {
291  QgsVectorLayer* vl = qobject_cast<QgsVectorLayer*>( mapLayer );
292 
293  if ( vl )
294  {
295  connect( vl, SIGNAL( readCustomSymbology( const QDomElement&, QString& ) ), this, SLOT( readSymbology( const QDomElement&, QString& ) ) );
296  connect( vl, SIGNAL( writeCustomSymbology( QDomElement&, QDomDocument&, QString& ) ), this, SLOT( writeSymbology( QDomElement&, QDomDocument&, QString& ) ) );
297  }
298 }
299 
300 void QgsEditorWidgetRegistry::readSymbology( const QDomElement& element, QString& errorMessage )
301 {
302  Q_UNUSED( errorMessage )
303  QgsVectorLayer* vl = qobject_cast<QgsVectorLayer*>( sender() );
304 
305  Q_ASSERT( vl );
306 
307  readMapLayer( vl, element );
308 }
309 
310 void QgsEditorWidgetRegistry::writeSymbology( QDomElement& element, QDomDocument& doc, QString& errorMessage )
311 {
312  Q_UNUSED( errorMessage )
313  QgsVectorLayer* vl = qobject_cast<QgsVectorLayer*>( sender() );
314 
315  Q_ASSERT( vl );
316 
317  writeMapLayer( vl, element, doc );
318 }
const QgsEditorWidgetConfig editorWidgetV2Config(int fieldIdx) const
Get the configuration for the editor widget used to represent the field at the given index...
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
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.
#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.
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
void setEditorWidgetV2(int attrIdx, const QString &widgetType)
Set the editor widget type for a field.
modularized edit widgets
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.
QDomNode at(int index) const
const T value(const Key &key) const