QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
qgsvaluerelationwidgetfactory.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsvaluerelationwidgetfactory.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 
18 #include "qgsfeatureiterator.h"
19 #include "qgslogger.h"
20 #include "qgsmaplayerregistry.h"
23 #include "qgsvectorlayer.h"
24 
25 #include <QSettings>
26 
28  : QgsEditorWidgetFactory( name )
29 {
30 }
31 
33 {
34  return new QgsValueRelationWidgetWrapper( vl, fieldIdx, editor, parent );
35 }
36 
38 {
39  return new QgsValueRelationSearchWidgetWrapper( vl, fieldIdx, parent );
40 }
41 
43 {
44  return new QgsValueRelationConfigDlg( vl, fieldIdx, parent );
45 }
46 
48 {
49  Q_UNUSED( layer )
50  Q_UNUSED( fieldIdx )
51 
53 
54  cfg.insert( "Layer", configElement.attribute( "Layer" ) );
55  cfg.insert( "Key", configElement.attribute( "Key" ) );
56  cfg.insert( "Value", configElement.attribute( "Value" ) );
57  cfg.insert( "FilterExpression", configElement.attribute( "FilterExpression" ) );
58  cfg.insert( "OrderByValue", configElement.attribute( "OrderByValue" ) );
59  cfg.insert( "AllowMulti", configElement.attribute( "AllowMulti" ) );
60  cfg.insert( "AllowNull", configElement.attribute( "AllowNull" ) );
61  cfg.insert( "UseCompleter", configElement.attribute( "UseCompleter" ) );
62 
63  return cfg;
64 }
65 
66 void QgsValueRelationWidgetFactory::writeConfig( const QgsEditorWidgetConfig& config, QDomElement& configElement, QDomDocument& doc, const QgsVectorLayer* layer, int fieldIdx )
67 {
68  Q_UNUSED( doc )
69  Q_UNUSED( layer )
70  Q_UNUSED( fieldIdx )
71 
72  configElement.setAttribute( "Layer", config.value( "Layer" ).toString() );
73  configElement.setAttribute( "Key", config.value( "Key" ).toString() );
74  configElement.setAttribute( "Value", config.value( "Value" ).toString() );
75  configElement.setAttribute( "FilterExpression", config.value( "FilterExpression" ).toString() );
76  configElement.setAttribute( "OrderByValue", config.value( "OrderByValue" ).toBool() );
77  configElement.setAttribute( "AllowMulti", config.value( "AllowMulti" ).toBool() );
78  configElement.setAttribute( "AllowNull", config.value( "AllowNull" ).toBool() );
79  configElement.setAttribute( "UseCompleter", config.value( "UseCompleter" ).toBool() );
80 }
81 
82 QString QgsValueRelationWidgetFactory::representValue( QgsVectorLayer* vl, int fieldIdx, const QgsEditorWidgetConfig& config, const QVariant& cache, const QVariant& value ) const
83 {
84  Q_UNUSED( vl )
85  Q_UNUSED( fieldIdx )
86 
88 
89  if ( cache.isValid() )
90  {
92  }
93  else
94  {
96  }
97 
98  if ( config.value( "AllowMulti" ).toBool() )
99  {
100  QStringList keyList = value.toString().remove( QChar( '{' ) ).remove( QChar( '}' ) ).split( ',' );
101  QStringList valueList;
102 
103  Q_FOREACH ( const QgsValueRelationWidgetWrapper::ValueRelationItem& item, vrCache )
104  {
105  if ( keyList.contains( item.first.toString() ) )
106  {
107  valueList << item.second;
108  }
109  }
110 
111  return valueList.join( ", " ).prepend( '{' ).append( '}' );
112  }
113  else
114  {
115  if ( value.isNull() )
116  {
117  QSettings settings;
118  return settings.value( "qgis/nullValue", "NULL" ).toString();
119  }
120 
121  Q_FOREACH ( const QgsValueRelationWidgetWrapper::ValueRelationItem& item, vrCache )
122  {
123  if ( item.first == value )
124  {
125  return item.second;
126  }
127  }
128  }
129 
130  return QString( "(%1)" ).arg( value.toString() );
131 }
132 
133 QVariant QgsValueRelationWidgetFactory::sortValue( QgsVectorLayer* vl, int fieldIdx, const QgsEditorWidgetConfig& config, const QVariant& cache, const QVariant& value ) const
134 {
135  return representValue( vl, fieldIdx, config, cache, value );
136 }
137 
138 Qt::AlignmentFlag QgsValueRelationWidgetFactory::alignmentFlag( QgsVectorLayer* vl, int fieldIdx, const QgsEditorWidgetConfig& config ) const
139 {
140  Q_UNUSED( vl );
141  Q_UNUSED( fieldIdx );
142  Q_UNUSED( config );
143 
144  return Qt::AlignLeft;
145 }
146 
148 {
149  Q_UNUSED( vl )
150  Q_UNUSED( fieldIdx )
151 
152  return QVariant::fromValue<QgsValueRelationWidgetWrapper::ValueRelationCache>( QgsValueRelationWidgetWrapper::createCache( config ) );
153 }
154 
Manages an editor widget Widget and wrapper share the same parent.
QString & append(QChar ch)
This class should be subclassed for every configurable editor widget type.
QString attribute(const QString &name, const QString &defValue) const
Manages an editor widget Widget and wrapper share the same parent.
QStringList split(const QString &sep, SplitBehavior behavior, Qt::CaseSensitivity cs) const
QString & prepend(QChar ch)
bool contains(const QString &str, Qt::CaseSensitivity cs) const
T value() const
QString join(const QString &separator) const
QString & remove(int position, int n)
Wraps a value relation widget.
QgsEditorWidgetConfig readConfig(const QDomElement &configElement, QgsVectorLayer *layer, int fieldIdx) override
Read the config from an XML file and map it to a proper QgsEditorWidgetConfig.
QVariantMap QgsEditorWidgetConfig
Holds a set of configuration parameters for a editor widget wrapper.
bool isNull() const
Wraps a value relation search widget.
QgsEditorWidgetWrapper * create(QgsVectorLayer *vl, int fieldIdx, QWidget *editor, QWidget *parent) const override
Override this in your implementation.
void setAttribute(const QString &name, const QString &value)
QgsSearchWidgetWrapper * createSearchWidget(QgsVectorLayer *vl, int fieldIdx, QWidget *parent) const override
By default a simple QgsFilterLineEdit is returned as search widget.
Every attribute editor widget needs a factory, which inherits this class.
QVariant createCache(QgsVectorLayer *vl, int fieldIdx, const QgsEditorWidgetConfig &config) override
Create a cache for a given field.
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.
Qt::AlignmentFlag alignmentFlag(QgsVectorLayer *vl, int fieldIdx, const QgsEditorWidgetConfig &config) const override
Return the alignment for a particular field.
static ValueRelationCache createCache(const QgsEditorWidgetConfig &config)
QVariant value(const QString &key, const QVariant &defaultValue) 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.
QString name()
Return The human readable identifier name of this widget type.
bool isValid() const
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...
Represents a vector layer which manages a vector based data sets.
QString arg(qlonglong a, int fieldWidth, int base, const QChar &fillChar) const
QString toString() const
QgsEditorConfigWidget * configWidget(QgsVectorLayer *vl, int fieldIdx, QWidget *parent) const override
Override this in your implementation.