QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
qgsexternalresourcewidgetfactory.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsexternalresourcewidgetfactory.cpp
3 
4  ---------------------
5  begin : 16.12.2015
6  copyright : (C) 2015 by Denis Rouzaud
7  email : [email protected]
8  ***************************************************************************
9  * *
10  * This program is free software; you can redistribute it and/or modify *
11  * it under the terms of the GNU General Public License as published by *
12  * the Free Software Foundation; either version 2 of the License, or *
13  * (at your option) any later version. *
14  * *
15  ***************************************************************************/
16 
18 #include "qgsfilewidget.h"
19 
21  : QgsEditorWidgetFactory( name )
22 {
23 }
24 
26 {
27  return new QgsExternalResourceWidgetWrapper( vl, fieldIdx, editor, parent );
28 }
29 
31 {
32  return new QgsExternalResourceConfigDlg( vl, fieldIdx, parent );
33 }
34 
35 void QgsExternalResourceWidgetFactory::writeConfig( const QgsEditorWidgetConfig& config, QDomElement& configElement, QDomDocument& doc, const QgsVectorLayer* layer, int fieldIdx )
36 {
37  Q_UNUSED( doc )
38  Q_UNUSED( layer )
39  Q_UNUSED( fieldIdx )
40 
41  configElement.setAttribute( "FileWidget", config.value( "FileWidget", true ).toBool() );
42  configElement.setAttribute( "FileWidgetButton", config.value( "FileWidgetButton", true ).toBool() );
43 
44 
45  // Non mandatory options are not saved into project file (to save some space).
46  if ( config.contains( "UseLink" ) )
47  configElement.setAttribute( "UseLink", config.value( "UseLink" ).toBool() );
48 
49  if ( config.contains( "FullUrl" ) )
50  configElement.setAttribute( "FullUrl", config.value( "FullUrl" ).toBool() );
51 
52  if ( config.contains( "DefaultRoot" ) )
53  configElement.setAttribute( "DefaultRoot", config.value( "DefaultRoot" ).toString() );
54 
55  if ( config.contains( "RelativeStorage" ) )
56  configElement.setAttribute( "RelativeStorage" , config.value( "RelativeStorage" ).toString() );
57 
58  if ( config.contains( "DocumentViewer" ) )
59  configElement.setAttribute( "DocumentViewer", config.value( "DocumentViewer" ).toInt() );
60 
61  if ( config.contains( "DocumentViewerWidth" ) )
62  configElement.setAttribute( "DocumentViewerWidth", config.value( "DocumentViewerWidth" ).toInt() );
63 
64  if ( config.contains( "DocumentViewerHeight" ) )
65  configElement.setAttribute( "DocumentViewerHeight", config.value( "DocumentViewerHeight" ).toInt() );
66 
67  if ( config.contains( "FileWidgetFilter" ) )
68  configElement.setAttribute( "FileWidgetFilter", config.value( "FileWidgetFilter" ).toString() );
69 
70  configElement.setAttribute( "StorageMode", config.value( "StorageMode" ).toString() );
71 }
72 
73 QgsEditorWidgetConfig QgsExternalResourceWidgetFactory::readConfig( const QDomElement& configElement, QgsVectorLayer* layer, int fieldIdx )
74 {
75  Q_UNUSED( layer )
76  Q_UNUSED( fieldIdx )
77 
79 
80  if ( configElement.hasAttribute( "FileWidgetButton" ) )
81  cfg.insert( "FileWidgetButton", configElement.attribute( "FileWidgetButton" ) == "1" );
82 
83  if ( configElement.hasAttribute( "FileWidget" ) )
84  cfg.insert( "FileWidget", configElement.attribute( "FileWidget" ) == "1" );
85 
86  if ( configElement.hasAttribute( "UseLink" ) )
87  cfg.insert( "UseLink", configElement.attribute( "UseLink" ) == "1" );
88 
89  if ( configElement.hasAttribute( "FullUrl" ) )
90  cfg.insert( "FullUrl", configElement.attribute( "FullUrl" ) == "1" );
91 
92  if ( configElement.hasAttribute( "DefaultRoot" ) )
93  cfg.insert( "DefaultRoot", configElement.attribute( "DefaultRoot" ) );
94 
95  if ( configElement.hasAttribute( "RelativeStorage" ) )
96  {
97  if (( configElement.attribute( "RelativeStorage" ).toInt() == QgsFileWidget::RelativeDefaultPath && configElement.hasAttribute( "DefaultRoot" ) ) ||
98  configElement.attribute( "RelativeStorage" ).toInt() == QgsFileWidget::RelativeProject )
99  cfg.insert( "RelativeStorage" , configElement.attribute( "RelativeStorage" ).toInt() );
100  }
101 
102  if ( configElement.hasAttribute( "DocumentViewer" ) )
103  cfg.insert( "DocumentViewer", configElement.attribute( "DocumentViewer" ) );
104 
105  if ( configElement.hasAttribute( "DocumentViewerWidth" ) )
106  cfg.insert( "DocumentViewerWidth", configElement.attribute( "DocumentViewerWidth" ) );
107 
108  if ( configElement.hasAttribute( "DocumentViewerHeight" ) )
109  cfg.insert( "DocumentViewerHeight", configElement.attribute( "DocumentViewerHeight" ) );
110 
111  if ( configElement.hasAttribute( "FileWidgetFilter" ) )
112  cfg.insert( "FileWidgetFilter", configElement.attribute( "FileWidgetFilter" ) );
113 
114 
115  cfg.insert( "StorageMode", configElement.attribute( "StorageMode", "Files" ) );
116 
117  return cfg;
118 }
119 
120 bool QgsExternalResourceWidgetFactory::isFieldSupported( QgsVectorLayer* vl, int fieldIdx )
121 {
122  if ( vl->fields().at( fieldIdx ).type() == QVariant::String )
123  return true;
124 
125  return false;
126 }
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.
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.
QgsEditorConfigWidget * configWidget(QgsVectorLayer *vl, int fieldIdx, QWidget *parent) const override
Override this in your implementation.
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.
QVariantMap QgsEditorWidgetConfig
Holds a set of configuration parameters for a editor widget wrapper.
bool hasAttribute(const QString &name) const
void setAttribute(const QString &name, const QString &value)
int toInt(bool *ok, int base) const
Every attribute editor widget needs a factory, which inherits this class.
QgsEditorWidgetWrapper * create(QgsVectorLayer *vl, int fieldIdx, QWidget *editor, QWidget *parent) const override
Override this in your implementation.
Represents a vector layer which manages a vector based data sets.
QVariant::Type type() const
Gets variant type of the field as it will be retrieved from data source.
Definition: qgsfield.cpp:97