QGIS API Documentation  2.10.1-Pisa
 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 "qgseditorwidgetfactory.h"
20 #include "qgslegacyhelpers.h"
21 #include "qgsmessagelog.h"
22 #include "qgsproject.h"
23 #include "qgsvectorlayer.h"
24 #include "qgsmaplayerregistry.h"
25 
26 // Editors
28 #include "qgsrangewidgetfactory.h"
33 #include "qgshiddenwidgetfactory.h"
37 #include "qgsuuidwidgetfactory.h"
38 #include "qgsphotowidgetfactory.h"
40 #include "qgscolorwidgetfactory.h"
42 #include "qgsdatetimeeditfactory.h"
43 
45 {
46  static QgsEditorWidgetRegistry sInstance;
47  return &sInstance;
48 }
49 
51 {
53  reg->registerWidget( "Classification", new QgsClassificationWidgetWrapperFactory( tr( "Classification" ) ) );
54  reg->registerWidget( "Range", new QgsRangeWidgetFactory( tr( "Range" ) ) );
55  reg->registerWidget( "UniqueValues", new QgsUniqueValueWidgetFactory( tr( "Unique Values" ) ) );
56  reg->registerWidget( "FileName", new QgsFileNameWidgetFactory( tr( "File Name" ) ) );
57  reg->registerWidget( "ValueMap", new QgsValueMapWidgetFactory( tr( "Value Map" ) ) );
58  reg->registerWidget( "Enumeration", new QgsEnumerationWidgetFactory( tr( "Enumeration" ) ) );
59  reg->registerWidget( "Hidden", new QgsHiddenWidgetFactory( tr( "Hidden" ) ) );
60  reg->registerWidget( "CheckBox", new QgsCheckboxWidgetFactory( tr( "Check Box" ) ) );
61  reg->registerWidget( "TextEdit", new QgsTextEditWidgetFactory( tr( "Text Edit" ) ) );
62  reg->registerWidget( "ValueRelation", new QgsValueRelationWidgetFactory( tr( "Value Relation" ) ) );
63  reg->registerWidget( "UuidGenerator", new QgsUuidWidgetFactory( tr( "Uuid Generator" ) ) );
64  reg->registerWidget( "Photo", new QgsPhotoWidgetFactory( tr( "Photo" ) ) );
65 #ifdef WITH_QTWEBKIT
66  reg->registerWidget( "WebView", new QgsWebViewWidgetFactory( tr( "Web View" ) ) );
67 #endif
68  reg->registerWidget( "Color", new QgsColorWidgetFactory( tr( "Color" ) ) );
69  reg->registerWidget( "RelationReference", new QgsRelationReferenceFactory( tr( "Relation Reference" ), mapCanvas, messageBar ) );
70  reg->registerWidget( "DateTime", new QgsDateTimeEditFactory( tr( "Date/Time" ) ) );
71 }
72 
74 {
75  connect( QgsProject::instance(), SIGNAL( readMapLayer( QgsMapLayer*, const QDomElement& ) ), this, SLOT( readMapLayer( QgsMapLayer*, const QDomElement& ) ) );
76  connect( QgsProject::instance(), SIGNAL( writeMapLayer( QgsMapLayer*, QDomElement&, QDomDocument& ) ), this, SLOT( writeMapLayer( QgsMapLayer*, QDomElement&, QDomDocument& ) ) );
77 
78  connect( QgsMapLayerRegistry::instance(), SIGNAL( layerWasAdded( QgsMapLayer* ) ), this, SLOT( mapLayerAdded( QgsMapLayer* ) ) );
79 }
80 
82 {
83  qDeleteAll( mWidgetFactories.values() );
84 }
85 
86 QgsEditorWidgetWrapper* QgsEditorWidgetRegistry::create( const QString& widgetId, QgsVectorLayer* vl, int fieldIdx, const QgsEditorWidgetConfig& config, QWidget* editor, QWidget* parent, const QgsAttributeEditorContext &context )
87 {
88  if ( mWidgetFactories.contains( widgetId ) )
89  {
90  QgsEditorWidgetWrapper* ww = mWidgetFactories[widgetId]->create( vl, fieldIdx, editor, parent );
91 
92  if ( ww )
93  {
94  ww->setConfig( config );
95  ww->setContext( context );
96  // Make sure that there is a widget created at this point
97  // so setValue() et al won't crash
98  ww->widget();
99  return ww;
100  }
101  }
102  return 0;
103 }
104 
106 {
107  if ( mWidgetFactories.contains( widgetId ) )
108  {
109  QgsEditorWidgetWrapper* ww = mWidgetFactories[widgetId]->createSearchWidget( vl, fieldIdx, parent );
110 
111  if ( ww )
112  {
113  ww->setConfig( config );
114  ww->setContext( context );
115  // Make sure that there is a widget created at this point
116  // so setValue() et al won't crash
117  ww->widget();
118  return ww;
119  }
120  }
121  return 0;
122 }
123 
125 {
126  if ( mWidgetFactories.contains( widgetId ) )
127  {
128  return mWidgetFactories[widgetId]->configWidget( vl, fieldIdx, parent );
129  }
130  return 0;
131 }
132 
134 {
135  if ( mWidgetFactories.contains( widgetId ) )
136  {
137  return mWidgetFactories[widgetId]->name();
138  }
139 
140  return QString();
141 }
142 
144 {
145  return mWidgetFactories;
146 }
147 
149 {
150  return mWidgetFactories.value( widgetId );
151 }
152 
154 {
155  if ( !widgetFactory )
156  {
157  QgsMessageLog::instance()->logMessage( "QgsEditorWidgetRegistry: Factory not valid." );
158  return false;
159  }
160  else if ( mWidgetFactories.contains( widgetId ) )
161  {
162  QgsMessageLog::instance()->logMessage( QString( "QgsEditorWidgetRegistry: Factory with id %1 already registered." ).arg( widgetId ) );
163  return false;
164  }
165  else
166  {
167  mWidgetFactories.insert( widgetId, widgetFactory );
168  return true;
169  }
170 }
171 
172 void QgsEditorWidgetRegistry::readMapLayer( QgsMapLayer* mapLayer, const QDomElement& layerElem )
173 {
174  if ( mapLayer->type() != QgsMapLayer::VectorLayer )
175  {
176  return;
177  }
178 
179  QgsVectorLayer* vectorLayer = qobject_cast<QgsVectorLayer*>( mapLayer );
180  Q_ASSERT( vectorLayer );
181 
182  QDomNodeList editTypeNodes = layerElem.namedItem( "edittypes" ).childNodes();
183 
184  for ( int i = 0; i < editTypeNodes.size(); i++ )
185  {
186  QDomNode editTypeNode = editTypeNodes.at( i );
187  QDomElement editTypeElement = editTypeNode.toElement();
188 
189  QString name = editTypeElement.attribute( "name" );
190 
191  int idx = vectorLayer->fieldNameIndex( name );
192  if ( idx == -1 )
193  continue;
194 
195  bool hasLegacyType;
196  QgsVectorLayer::EditType editType =
197  ( QgsVectorLayer::EditType ) editTypeElement.attribute( "type" ).toInt( &hasLegacyType );
198 
199  QString ewv2Type;
201 
202  if ( hasLegacyType && editType != QgsVectorLayer::EditorWidgetV2 )
203  {
205  ewv2Type = readLegacyConfig( vectorLayer, editTypeElement, cfg );
207  }
208  else
209  ewv2Type = editTypeElement.attribute( "widgetv2type" );
210 
211  if ( mWidgetFactories.contains( ewv2Type ) )
212  {
213  vectorLayer->setEditorWidgetV2( idx, ewv2Type );
214  QDomElement ewv2CfgElem = editTypeElement.namedItem( "widgetv2config" ).toElement();
215 
216  if ( !ewv2CfgElem.isNull() )
217  {
218  cfg = mWidgetFactories[ewv2Type]->readEditorConfig( ewv2CfgElem, vectorLayer, idx );
219  }
220 
221  vectorLayer->setFieldEditable( idx, ewv2CfgElem.attribute( "fieldEditable", "1" ) == "1" );
222  vectorLayer->setLabelOnTop( idx, ewv2CfgElem.attribute( "labelOnTop", "0" ) == "1" );
223  vectorLayer->setEditorWidgetV2Config( idx, cfg );
224  }
225  else
226  {
227  QgsMessageLog::logMessage( tr( "Unknown attribute editor widget '%1'" ).arg( ewv2Type ) );
228  }
229  }
230 }
231 
232 const QString QgsEditorWidgetRegistry::readLegacyConfig( QgsVectorLayer* vl, const QDomElement& editTypeElement, QgsEditorWidgetConfig& cfg )
233 {
234  QString name = editTypeElement.attribute( "name" );
235 
236  QgsVectorLayer::EditType editType = ( QgsVectorLayer::EditType ) editTypeElement.attribute( "type" ).toInt();
237 
239  return QgsLegacyHelpers::convertEditType( editType, cfg, vl, name, editTypeElement );
241 }
242 
243 void QgsEditorWidgetRegistry::writeMapLayer( QgsMapLayer* mapLayer, QDomElement& layerElem, QDomDocument& doc ) const
244 {
245  if ( mapLayer->type() != QgsMapLayer::VectorLayer )
246  {
247  return;
248  }
249 
250  QgsVectorLayer* vectorLayer = qobject_cast<QgsVectorLayer*>( mapLayer );
251  if ( !vectorLayer )
252  {
253  return;
254  }
255 
256  QDomNode editTypesNode = doc.createElement( "edittypes" );
257 
258  for ( int idx = 0; idx < vectorLayer->pendingFields().count(); ++idx )
259  {
260  const QgsField &field = vectorLayer->pendingFields()[ idx ];
261  const QString& widgetType = vectorLayer->editorWidgetV2( idx );
262  if ( !mWidgetFactories.contains( widgetType ) )
263  {
264  QgsMessageLog::logMessage( tr( "Could not save unknown editor widget type '%1'." ).arg( widgetType ) );
265  continue;
266  }
267 
268 
269  QDomElement editTypeElement = doc.createElement( "edittype" );
270  editTypeElement.setAttribute( "name", field.name() );
271  editTypeElement.setAttribute( "widgetv2type", widgetType );
272 
273  if ( mWidgetFactories.contains( widgetType ) )
274  {
275  QDomElement ewv2CfgElem = doc.createElement( "widgetv2config" );
276  ewv2CfgElem.setAttribute( "fieldEditable", vectorLayer->fieldEditable( idx ) );
277  ewv2CfgElem.setAttribute( "labelOnTop", vectorLayer->labelOnTop( idx ) );
278 
279  mWidgetFactories[widgetType]->writeConfig( vectorLayer->editorWidgetV2Config( idx ), ewv2CfgElem, doc, vectorLayer, idx );
280 
281  editTypeElement.appendChild( ewv2CfgElem );
282  }
283 
284  editTypesNode.appendChild( editTypeElement );
285  }
286 
287  layerElem.appendChild( editTypesNode );
288 }
289 
290 void QgsEditorWidgetRegistry::mapLayerAdded( QgsMapLayer* mapLayer )
291 {
292  QgsVectorLayer* vl = qobject_cast<QgsVectorLayer*>( mapLayer );
293 
294  if ( vl )
295  {
296  connect( vl, SIGNAL( readCustomSymbology( const QDomElement&, QString& ) ), this, SLOT( readSymbology( const QDomElement&, QString& ) ) );
297  connect( vl, SIGNAL( writeCustomSymbology( QDomElement&, QDomDocument&, QString& ) ), this, SLOT( writeSymbology( QDomElement&, QDomDocument&, QString& ) ) );
298  }
299 }
300 
301 void QgsEditorWidgetRegistry::readSymbology( const QDomElement& element, QString& errorMessage )
302 {
303  Q_UNUSED( errorMessage )
304  QgsVectorLayer* vl = qobject_cast<QgsVectorLayer*>( sender() );
305 
306  Q_ASSERT( vl );
307 
308  readMapLayer( vl, element );
309 }
310 
311 void QgsEditorWidgetRegistry::writeSymbology( QDomElement& element, QDomDocument& doc, QString& errorMessage )
312 {
313  Q_UNUSED( errorMessage )
314  QgsVectorLayer* vl = qobject_cast<QgsVectorLayer*>( sender() );
315 
316  Q_ASSERT( vl );
317 
318  writeMapLayer( vl, element, doc );
319 }
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
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.
QgsEditorWidgetWrapper * createSearchWidget(const QString &widgetId, QgsVectorLayer *vl, int fieldIdx, const QgsEditorWidgetConfig &config, QWidget *parent, const QgsAttributeEditorContext &context=QgsAttributeEditorContext())
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.
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