QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
qgsvaluemapwidgetfactory.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsvaluemapwidgetfactory.cpp
3  --------------------------------------
4  Date : 5.1.2014
5  Copyright : (C) 2014 Matthias Kuhn
6  Email : matthias at opengis 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 
21 #include "qgsvaluemapconfigdlg.h"
22 
23 #include <QSettings>
24 
26  : QgsEditorWidgetFactory( name )
27 {
28 }
29 
30 
32 {
33  return new QgsValueMapWidgetWrapper( vl, fieldIdx, editor, parent );
34 }
35 
36 
38 {
39  return new QgsValueMapSearchWidgetWrapper( vl, fieldIdx, parent );
40 }
41 
43 {
44  return new QgsValueMapConfigDlg( vl, fieldIdx, parent );
45 }
46 
48 {
49  Q_UNUSED( layer )
50  Q_UNUSED( fieldIdx )
51 
53 
54  QDomNodeList nodes = configElement.elementsByTagName( "value" );
55 
56  for ( int i = 0; i < nodes.size(); ++i )
57  {
58  QDomElement elem = nodes.at( i ).toElement();
59  cfg.insert( elem.attribute( "key" ), elem.attribute( "value" ) );
60  }
61 
62  return cfg;
63 }
64 
65 void QgsValueMapWidgetFactory::writeConfig( const QgsEditorWidgetConfig& config, QDomElement& configElement, QDomDocument& doc, const QgsVectorLayer* layer, int fieldIdx )
66 {
67  Q_UNUSED( layer )
68  Q_UNUSED( fieldIdx )
69 
70  QgsEditorWidgetConfig::ConstIterator it = config.constBegin();
71 
72  while ( it != config.constEnd() )
73  {
74  QDomElement elem = doc.createElement( "value" );
75 
76  elem.setAttribute( "key", it.key() );
77  elem.setAttribute( "value", it.value().toString() );
78 
79  configElement.appendChild( elem );
80 
81  ++it;
82  }
83 }
84 
85 QString QgsValueMapWidgetFactory::representValue( QgsVectorLayer* vl, int fieldIdx, const QgsEditorWidgetConfig& config, const QVariant& cache, const QVariant& value ) const
86 {
87  Q_UNUSED( cache )
88 
89  QString valueInternalText;
90  QString valueDisplayText;
91  QSettings settings;
92  if ( value.isNull() )
93  valueInternalText = QString( VALUEMAP_NULL_TEXT );
94  else
95  valueInternalText = value.toString();
96 
97  return config.key( valueInternalText, QVariant( QString( "(%1)" ).arg( vl->fields().at( fieldIdx ).displayString( value ) ) ).toString() );
98 }
99 
100 QVariant QgsValueMapWidgetFactory::sortValue( QgsVectorLayer* vl, int fieldIdx, const QgsEditorWidgetConfig& config, const QVariant& cache, const QVariant& value ) const
101 {
102  return representValue( vl, fieldIdx, config, cache, value );
103 }
104 
105 Qt::AlignmentFlag QgsValueMapWidgetFactory::alignmentFlag( QgsVectorLayer* vl, int fieldIdx, const QgsEditorWidgetConfig& config ) const
106 {
107  Q_UNUSED( vl );
108  Q_UNUSED( fieldIdx );
109  Q_UNUSED( config );
110 
111  return Qt::AlignLeft;
112 }
113 
115 {
117  map.insert( QComboBox::staticMetaObject.className(), 10 );
118  return map;
119 }
QDomNodeList elementsByTagName(const QString &tagname) const
Manages an editor widget Widget and wrapper share the same parent.
QgsEditorConfigWidget * configWidget(QgsVectorLayer *vl, int fieldIdx, QWidget *parent) const override
Override this in your implementation.
This class should be subclassed for every configurable editor widget type.
Wraps a value map widget.
QDomNode appendChild(const QDomNode &newChild)
QVariant sortValue(QgsVectorLayer *vl, int fieldIdx, const QgsEditorWidgetConfig &config, const QVariant &cache, const QVariant &value) const override
If the default sort order should be overwritten for this widget, you can transform the value in here...
QString attribute(const QString &name, const QString &defValue) const
Manages an editor widget Widget and wrapper share the same parent.
QgsSearchWidgetWrapper * createSearchWidget(QgsVectorLayer *vl, int fieldIdx, QWidget *parent) const override
By default a simple QgsFilterLineEdit is returned as search widget.
QString representValue(QgsVectorLayer *vl, int fieldIdx, const QgsEditorWidgetConfig &config, const QVariant &cache, const QVariant &value) const override
Create a pretty String representation of the value.
const QgsField & at(int i) const
Get field at particular index (must be in range 0..N-1)
Definition: qgsfield.cpp:422
QgsFields fields() const
Returns the list of fields of this layer.
QDomElement toElement() const
Wraps a value map search widget.
Qt::AlignmentFlag alignmentFlag(QgsVectorLayer *vl, int fieldIdx, const QgsEditorWidgetConfig &config) const override
Return the alignment for a particular field.
QVariantMap QgsEditorWidgetConfig
Holds a set of configuration parameters for a editor widget wrapper.
bool isNull() const
void setAttribute(const QString &name, const QString &value)
Every attribute editor widget needs a factory, which inherits this class.
virtual QMap< const char *, int > supportedWidgetTypes() override
Returns a list of widget types which this editor widget supports.
QString displayString(const QVariant &v) const
Formats string for display.
Definition: qgsfield.cpp:188
QgsEditorWidgetConfig readConfig(const QDomElement &configElement, QgsVectorLayer *layer, int fieldIdx) override
Read the config from an XML file and map it to a proper QgsEditorWidgetConfig.
QgsValueMapWidgetFactory(const QString &name)
iterator insert(const Key &key, const T &value)
int size() const
QDomElement createElement(const QString &tagName)
Represents a vector layer which manages a vector based data sets.
QgsEditorWidgetWrapper * create(QgsVectorLayer *vl, int fieldIdx, QWidget *editor, QWidget *parent) const override
Override this in your implementation.
QString toString() const
void writeConfig(const QgsEditorWidgetConfig &config, QDomElement &configElement, QDomDocument &doc, const QgsVectorLayer *layer, int fieldIdx) override
Serialize your configuration and save it in a xml doc.
#define VALUEMAP_NULL_TEXT
QDomNode at(int index) const