QGIS API Documentation  2.8.2-Wien
 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"
23 #include "qgsvectorlayer.h"
24 
25 #include <QSettings>
26 
28  : QgsEditorWidgetFactory( name )
29 {
30 }
31 
32 QgsEditorWidgetWrapper* QgsValueRelationWidgetFactory::create( QgsVectorLayer* vl, int fieldIdx, QWidget* editor, QWidget* parent ) const
33 {
34  return new QgsValueRelationWidgetWrapper( vl, fieldIdx, editor, parent );
35 }
36 
38 {
39  return new QgsValueRelationConfigDlg( vl, fieldIdx, parent );
40 }
41 
42 QgsEditorWidgetConfig QgsValueRelationWidgetFactory::readConfig( const QDomElement& configElement, QgsVectorLayer* layer, int fieldIdx )
43 {
44  Q_UNUSED( layer )
45  Q_UNUSED( fieldIdx )
46 
48 
49  cfg.insert( "Layer", configElement.attribute( "Layer" ) );
50  cfg.insert( "Key", configElement.attribute( "Key" ) );
51  cfg.insert( "Value", configElement.attribute( "Value" ) );
52  cfg.insert( "FilterExpression", configElement.attribute( "FilterExpression" ) );
53  cfg.insert( "OrderByValue", configElement.attribute( "OrderByValue" ) );
54  cfg.insert( "AllowMulti", configElement.attribute( "AllowMulti" ) );
55  cfg.insert( "AllowNull", configElement.attribute( "AllowNull" ) );
56 
57  return cfg;
58 }
59 
60 void QgsValueRelationWidgetFactory::writeConfig( const QgsEditorWidgetConfig& config, QDomElement& configElement, QDomDocument& doc, const QgsVectorLayer* layer, int fieldIdx )
61 {
62  Q_UNUSED( doc )
63  Q_UNUSED( layer )
64  Q_UNUSED( fieldIdx )
65 
66  configElement.setAttribute( "Layer", config.value( "Layer" ).toString() );
67  configElement.setAttribute( "Key", config.value( "Key" ).toString() );
68  configElement.setAttribute( "Value", config.value( "Value" ).toString() );
69  configElement.setAttribute( "FilterExpression", config.value( "FilterExpression" ).toString() );
70  configElement.setAttribute( "OrderByValue", config.value( "OrderByValue" ).toBool() );
71  configElement.setAttribute( "AllowMulti", config.value( "AllowMulti" ).toBool() );
72  configElement.setAttribute( "AllowNull", config.value( "AllowNull" ).toBool() );
73 }
74 
75 QString QgsValueRelationWidgetFactory::representValue( QgsVectorLayer* vl, int fieldIdx, const QgsEditorWidgetConfig& config, const QVariant& cache, const QVariant& value ) const
76 {
77  Q_UNUSED( vl )
78  Q_UNUSED( fieldIdx )
79 
81 
82  if ( cache.isValid() )
83  {
85  }
86  else
87  {
89  }
90 
91  if ( config.value( "AllowMulti" ).toBool() )
92  {
93  QStringList keyList = value.toString().remove( QChar( '{' ) ).remove( QChar( '}' ) ).split( "," );
94  QStringList valueList;
95 
96  Q_FOREACH ( const QgsValueRelationWidgetWrapper::ValueRelationItem& item, vrCache )
97  {
98  if ( keyList.contains( item.first.toString() ) )
99  {
100  valueList << item.second;
101  }
102  }
103 
104  return valueList.join( ", " ).prepend( '{' ).append( '}' );
105  }
106  else
107  {
108  if ( value.isNull() )
109  {
110  QSettings settings;
111  return settings.value( "qgis/nullValue", "NULL" ).toString();
112  }
113 
114  Q_FOREACH ( const QgsValueRelationWidgetWrapper::ValueRelationItem& item, vrCache )
115  {
116  if ( item.first == value )
117  {
118  return item.second;
119  }
120  }
121  }
122 
123  return QString( "(%1)" ).arg( value.toString() );
124 }
125 
127 {
128  Q_UNUSED( vl )
129  Q_UNUSED( fieldIdx )
130 
131  return QVariant::fromValue<QgsValueRelationWidgetWrapper::ValueRelationCache>( QgsValueRelationWidgetWrapper::createCache( config ) );
132 }
133