QGIS API Documentation  2.10.1-Pisa
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 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 
18 #include "qgsfeatureiterator.h"
19 #include "qgslogger.h"
20 #include "qgsmaplayerregistry.h"
22 #include "qgsvectorlayer.h"
23 
24 #include <QSettings>
25 
27  : QgsEditorWidgetFactory( name )
28 {
29 }
30 
32 {
33  return new QgsValueRelationWidgetWrapper( vl, fieldIdx, editor, parent );
34 }
35 
37 {
38  return new QgsValueRelationWidgetWrapper( vl, fieldIdx, 0, parent );
39 }
40 
42 {
43  return new QgsValueRelationConfigDlg( vl, fieldIdx, parent );
44 }
45 
47 {
48  Q_UNUSED( layer )
49  Q_UNUSED( fieldIdx )
50 
52 
53  cfg.insert( "Layer", configElement.attribute( "Layer" ) );
54  cfg.insert( "Key", configElement.attribute( "Key" ) );
55  cfg.insert( "Value", configElement.attribute( "Value" ) );
56  cfg.insert( "FilterExpression", configElement.attribute( "FilterExpression" ) );
57  cfg.insert( "OrderByValue", configElement.attribute( "OrderByValue" ) );
58  cfg.insert( "AllowMulti", configElement.attribute( "AllowMulti" ) );
59  cfg.insert( "AllowNull", configElement.attribute( "AllowNull" ) );
60  cfg.insert( "UseCompleter", configElement.attribute( "UseCompleter" ) );
61 
62  return cfg;
63 }
64 
65 void QgsValueRelationWidgetFactory::writeConfig( const QgsEditorWidgetConfig& config, QDomElement& configElement, QDomDocument& doc, const QgsVectorLayer* layer, int fieldIdx )
66 {
67  Q_UNUSED( doc )
68  Q_UNUSED( layer )
69  Q_UNUSED( fieldIdx )
70 
71  configElement.setAttribute( "Layer", config.value( "Layer" ).toString() );
72  configElement.setAttribute( "Key", config.value( "Key" ).toString() );
73  configElement.setAttribute( "Value", config.value( "Value" ).toString() );
74  configElement.setAttribute( "FilterExpression", config.value( "FilterExpression" ).toString() );
75  configElement.setAttribute( "OrderByValue", config.value( "OrderByValue" ).toBool() );
76  configElement.setAttribute( "AllowMulti", config.value( "AllowMulti" ).toBool() );
77  configElement.setAttribute( "AllowNull", config.value( "AllowNull" ).toBool() );
78  configElement.setAttribute( "UseCompleter", config.value( "UseCompleter" ).toBool() );
79 }
80 
81 QString QgsValueRelationWidgetFactory::representValue( QgsVectorLayer* vl, int fieldIdx, const QgsEditorWidgetConfig& config, const QVariant& cache, const QVariant& value ) const
82 {
83  Q_UNUSED( vl )
84  Q_UNUSED( fieldIdx )
85 
87 
88  if ( cache.isValid() )
89  {
91  }
92  else
93  {
95  }
96 
97  if ( config.value( "AllowMulti" ).toBool() )
98  {
99  QStringList keyList = value.toString().remove( QChar( '{' ) ).remove( QChar( '}' ) ).split( "," );
100  QStringList valueList;
101 
102  Q_FOREACH ( const QgsValueRelationWidgetWrapper::ValueRelationItem& item, vrCache )
103  {
104  if ( keyList.contains( item.first.toString() ) )
105  {
106  valueList << item.second;
107  }
108  }
109 
110  return valueList.join( ", " ).prepend( '{' ).append( '}' );
111  }
112  else
113  {
114  if ( value.isNull() )
115  {
116  QSettings settings;
117  return settings.value( "qgis/nullValue", "NULL" ).toString();
118  }
119 
120  Q_FOREACH ( const QgsValueRelationWidgetWrapper::ValueRelationItem& item, vrCache )
121  {
122  if ( item.first == value )
123  {
124  return item.second;
125  }
126  }
127  }
128 
129  return QString( "(%1)" ).arg( value.toString() );
130 }
131 
133 {
134  Q_UNUSED( vl )
135  Q_UNUSED( fieldIdx )
136 
137  return QVariant::fromValue<QgsValueRelationWidgetWrapper::ValueRelationCache>( QgsValueRelationWidgetWrapper::createCache( config ) );
138 }
139 
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.
bool isNull() const
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)
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.
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.
QgsEditorWidgetWrapper * createSearchWidget(QgsVectorLayer *vl, int fieldIdx, QWidget *parent) const override
Override in own factory to get something different than the default (a simple QgsFilterLineEdit) ...
bool isValid() const
iterator insert(const Key &key, const T &value)
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.
const T value(const Key &key) const