QGIS API Documentation  3.0.2-Girona (307d082)
qgsexternalresourceconfigdlg.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsexternalresourceconfigdlg.cpp
3  --------------------------------------
4  Date : 2015-11-26
5  Copyright : (C) 2015 Médéric Ribreux
6  Email : mederic.ribreux at medspx dot fr
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 
18 #include "qgsproject.h"
19 #include "qgssettings.h"
21 #include "qgsapplication.h"
22 #include "qgsvectorlayer.h"
24 #include "qgseditorwidgetwrapper.h"
25 
26 #include <QFileDialog>
27 
29 
31  : QgsEditorConfigWidget( vl, fieldIdx, parent )
32 {
33  setupUi( this );
34 
35  // By default, uncheck some options
36  mUseLink->setChecked( false );
37  mFullUrl->setChecked( false );
38 
39  QString defpath = QgsProject::instance()->fileName().isEmpty() ? QDir::homePath() : QgsProject::instance()->fileInfo().absolutePath();
40 
41  mRootPath->setPlaceholderText( QgsSettings().value( QStringLiteral( "/UI/lastExternalResourceWidgetDefaultPath" ), QDir::toNativeSeparators( QDir::cleanPath( defpath ) ) ).toString() );
42 
43  connect( mRootPathButton, &QToolButton::clicked, this, &QgsExternalResourceConfigDlg::chooseDefaultPath );
44 
45  initializeDataDefinedButton( mRootPathPropertyOverrideButton, QgsEditorWidgetWrapper::RootPath );
46  mRootPathPropertyOverrideButton->registerVisibleWidget( mRootPathExpression );
47  mRootPathPropertyOverrideButton->registerExpressionWidget( mRootPathExpression );
48  mRootPathPropertyOverrideButton->registerVisibleWidget( mRootPath, false );
49  mRootPathPropertyOverrideButton->registerEnabledWidget( mRootPathButton, false );
50 
51 
52  initializeDataDefinedButton( mDocumentViewerContentPropertyOverrideButton, QgsEditorWidgetWrapper::DocumentViewerContent );
53  mDocumentViewerContentPropertyOverrideButton->registerVisibleWidget( mDocumentViewerContentExpression );
54  mDocumentViewerContentPropertyOverrideButton->registerExpressionWidget( mDocumentViewerContentExpression );
55  mDocumentViewerContentPropertyOverrideButton->registerEnabledWidget( mDocumentViewerContentComboBox, false );
56 
57  // Activate Relative Default Path option only if Default Path is set
58  connect( mRootPath, &QLineEdit::textChanged, this, &QgsExternalResourceConfigDlg::enableRelativeDefault );
59  connect( mRootPathExpression, &QLineEdit::textChanged, this, &QgsExternalResourceConfigDlg::enableRelativeDefault );
60  connect( mRelativeGroupBox, &QGroupBox::toggled, this, &QgsExternalResourceConfigDlg::enableRelativeDefault );
61 
62  // set ids for StorageTypeButtons
63  mStorageButtonGroup->setId( mStoreFilesButton, QgsFileWidget::GetFile );
64  mStorageButtonGroup->setId( mStoreDirsButton, QgsFileWidget::GetDirectory );
65  mStoreFilesButton->setChecked( true );
66 
67  // set ids for RelativeButtons
68  mRelativeButtonGroup->setId( mRelativeProject, QgsFileWidget::RelativeProject );
69  mRelativeButtonGroup->setId( mRelativeDefault, QgsFileWidget::RelativeDefaultPath );
70  mRelativeProject->setChecked( true );
71 
72  connect( mFileWidgetGroupBox, &QGroupBox::toggled, this, &QgsEditorConfigWidget::changed );
73  connect( mFileWidgetButtonGroupBox, &QGroupBox::toggled, this, &QgsEditorConfigWidget::changed );
74  connect( mFileWidgetFilterLineEdit, &QLineEdit::textChanged, this, &QgsEditorConfigWidget::changed );
75  connect( mUseLink, &QGroupBox::toggled, this, &QgsEditorConfigWidget::changed );
76  connect( mFullUrl, &QAbstractButton::toggled, this, &QgsEditorConfigWidget::changed );
77  connect( mRootPath, &QLineEdit::textChanged, this, &QgsEditorConfigWidget::changed );
78  connect( mStorageButtonGroup, static_cast<void ( QButtonGroup::* )( int )>( &QButtonGroup::buttonClicked ), this, &QgsEditorConfigWidget::changed );
79  connect( mRelativeGroupBox, &QGroupBox::toggled, this, &QgsEditorConfigWidget::changed );
80  connect( mDocumentViewerGroupBox, &QGroupBox::toggled, this, &QgsEditorConfigWidget::changed );
81  connect( mDocumentViewerContentComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, [ = ]( int idx )
82  { mDocumentViewerContentSettingsWidget->setEnabled( ( QgsExternalResourceWidget::DocumentViewerContent )idx != QgsExternalResourceWidget::NoContent ); } );
83  connect( mDocumentViewerHeight, static_cast<void ( QSpinBox::* )( int )>( &QSpinBox::valueChanged ), this, &QgsEditorConfigWidget::changed );
84  connect( mDocumentViewerWidth, static_cast<void ( QSpinBox::* )( int )>( &QSpinBox::valueChanged ), this, &QgsEditorConfigWidget::changed );
85 
86  mDocumentViewerContentComboBox->addItem( tr( "No content" ), QgsExternalResourceWidget::NoContent );
87  mDocumentViewerContentComboBox->addItem( tr( "Image" ), QgsExternalResourceWidget::Image );
88  mDocumentViewerContentComboBox->addItem( tr( "Web view" ), QgsExternalResourceWidget::Web );
89 }
90 
91 void QgsExternalResourceConfigDlg::chooseDefaultPath()
92 {
93  QString dir;
94  if ( !mRootPath->text().isEmpty() )
95  {
96  dir = mRootPath->text();
97  }
98  else
99  {
100  dir = QgsSettings().value( QStringLiteral( "/UI/lastExternalResourceWidgetDefaultPath" ), QDir::toNativeSeparators( QDir::cleanPath( QgsProject::instance()->fileInfo().absolutePath() ) ) ).toString();
101  }
102 
103  QString rootName = QFileDialog::getExistingDirectory( this, tr( "Select a directory" ), dir, QFileDialog::ShowDirsOnly );
104 
105  if ( !rootName.isNull() )
106  mRootPath->setText( rootName );
107 }
108 
109 void QgsExternalResourceConfigDlg::enableRelativeDefault()
110 {
111  bool relativePathActive = false;
112 
113  if ( mRootPathPropertyOverrideButton->isActive() )
114  {
115  if ( !mRootPathExpression->text().isEmpty() )
116  relativePathActive = true;
117  }
118  else
119  {
120  if ( !mRootPath->text().isEmpty() )
121  relativePathActive = true;
122  }
123 
124  // Activate (or not) the RelativeDefault button if default path
125  if ( mRelativeGroupBox->isChecked() )
126  mRelativeDefault->setEnabled( relativePathActive );
127 
128  // If no default path, RelativeProj button enabled by default
129  if ( !relativePathActive )
130  mRelativeProject->toggle();
131 }
132 
134 {
135  QVariantMap cfg;
136 
137  cfg.insert( QStringLiteral( "FileWidget" ), mFileWidgetGroupBox->isChecked() );
138  cfg.insert( QStringLiteral( "FileWidgetButton" ), mFileWidgetButtonGroupBox->isChecked() );
139  cfg.insert( QStringLiteral( "FileWidgetFilter" ), mFileWidgetFilterLineEdit->text() );
140 
141  if ( mUseLink->isChecked() )
142  {
143  cfg.insert( QStringLiteral( "UseLink" ), mUseLink->isChecked() );
144  if ( mFullUrl->isChecked() )
145  cfg.insert( QStringLiteral( "FullUrl" ), mFullUrl->isChecked() );
146  }
147 
148  cfg.insert( QStringLiteral( "PropertyCollection" ), mPropertyCollection.toVariant( QgsWidgetWrapper::propertyDefinitions() ) );
149 
150  if ( !mRootPath->text().isEmpty() )
151  cfg.insert( QStringLiteral( "DefaultRoot" ), mRootPath->text() );
152 
153  // Save Storage Mode
154  cfg.insert( QStringLiteral( "StorageMode" ), mStorageButtonGroup->checkedId() );
155 
156  // Save Relative Paths option
157  if ( mRelativeGroupBox->isChecked() )
158  {
159  cfg.insert( QStringLiteral( "RelativeStorage" ), mRelativeButtonGroup->checkedId() );
160  }
161  else
162  {
163  cfg.insert( QStringLiteral( "RelativeStorage" ), ( int )QgsFileWidget::Absolute );
164  }
165 
166  cfg.insert( QStringLiteral( "DocumentViewer" ), mDocumentViewerContentComboBox->currentData().toInt() );
167  cfg.insert( QStringLiteral( "DocumentViewerHeight" ), mDocumentViewerHeight->value() );
168  cfg.insert( QStringLiteral( "DocumentViewerWidth" ), mDocumentViewerWidth->value() );
169 
170  return cfg;
171 }
172 
173 
175 {
176  if ( config.contains( QStringLiteral( "FileWidget" ) ) )
177  {
178  mFileWidgetGroupBox->setChecked( config.value( QStringLiteral( "FileWidget" ) ).toBool() );
179  }
180  if ( config.contains( QStringLiteral( "FileWidget" ) ) )
181  {
182  mFileWidgetButtonGroupBox->setChecked( config.value( QStringLiteral( "FileWidgetButton" ) ).toBool() );
183  }
184  if ( config.contains( QStringLiteral( "FileWidgetFilter" ) ) )
185  {
186  mFileWidgetFilterLineEdit->setText( config.value( QStringLiteral( "FileWidgetFilter" ) ).toString() );
187  }
188 
189  if ( config.contains( QStringLiteral( "UseLink" ) ) )
190  {
191  mUseLink->setChecked( config.value( QStringLiteral( "UseLink" ) ).toBool() );
192  if ( config.contains( QStringLiteral( "FullUrl" ) ) )
193  mFullUrl->setChecked( true );
194  }
195 
196  mPropertyCollection.loadVariant( config.value( QStringLiteral( "PropertyCollection" ) ), QgsWidgetWrapper::propertyDefinitions() );
198 
199  mRootPath->setText( config.value( QStringLiteral( "DefaultRoot" ) ).toString() );
200 
201  // relative storage
202  if ( config.contains( QStringLiteral( "RelativeStorage" ) ) )
203  {
204  int relative = config.value( QStringLiteral( "RelativeStorage" ) ).toInt();
206  {
207  mRelativeGroupBox->setChecked( false );
208  }
209  else
210  {
211  mRelativeGroupBox->setChecked( true );
212  mRelativeButtonGroup->button( relative )->setChecked( true );
213  }
214  }
215 
216  // set storage mode
217  if ( config.contains( QStringLiteral( "StorageMode" ) ) )
218  {
219  int mode = config.value( QStringLiteral( "StorageMode" ) ).toInt();
220  mStorageButtonGroup->button( mode )->setChecked( true );
221  }
222 
223  // Document viewer
224  if ( config.contains( QStringLiteral( "DocumentViewer" ) ) )
225  {
226  QgsExternalResourceWidget::DocumentViewerContent content = ( QgsExternalResourceWidget::DocumentViewerContent )config.value( QStringLiteral( "DocumentViewer" ) ).toInt();
227  int idx = mDocumentViewerContentComboBox->findData( content );
228  if ( idx >= 0 )
229  {
230  mDocumentViewerContentComboBox->setCurrentIndex( idx );
231  }
232  if ( config.contains( QStringLiteral( "DocumentViewerHeight" ) ) )
233  {
234  mDocumentViewerHeight->setValue( config.value( QStringLiteral( "DocumentViewerHeight" ) ).toInt() );
235  }
236  if ( config.contains( QStringLiteral( "DocumentViewerWidth" ) ) )
237  {
238  mDocumentViewerWidth->setValue( config.value( QStringLiteral( "DocumentViewerWidth" ) ).toInt() );
239  }
240  }
241 }
static const QgsPropertiesDefinition & propertyDefinitions()
Returns the editor widget property definitions.
This class should be subclassed for every configurable editor widget type.
This class is a composition of two QSettings instances:
Definition: qgssettings.h:57
Select a single file.
Definition: qgsfilewidget.h:66
QgsExternalResourceConfigDlg(QgsVectorLayer *vl, int fieldIdx, QWidget *parent=nullptr)
Constructor for QgsExternalResourceConfigDlg.
QgsPropertyCollection mPropertyCollection
Temporary property collection for config widgets.
void setConfig(const QVariantMap &config) override
Update the configuration widget to represent the given configuration.
Root path for external resource.
QVariant value() const override
Will be used to access the widget&#39;s value.
bool loadVariant(const QVariant &configuration, const QgsPropertiesDefinition &definitions) override
Loads this property collection from a QVariantMap, wrapped in a QVariant.
void updateDataDefinedButtons()
Updates all property override buttons to reflect the widgets&#39;s current properties.
Document type for external resource.
QVariantMap config() override
Create a configuration from the current GUI state.
void changed()
Emitted when the configuration of the widget is changed.
QFileInfo fileInfo() const
Returns QFileInfo object for the project&#39;s associated file.
Definition: qgsproject.cpp:441
QVariant toVariant(const QgsPropertiesDefinition &definitions) const override
Saves this property collection to a QVariantMap, wrapped in a QVariant.
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), const Section section=NoSection) const
Returns the value for setting key.
static QgsProject * instance()
Returns the QgsProject singleton instance.
Definition: qgsproject.cpp:383
int fieldIdx() const
Access the field index.
void initializeDataDefinedButton(QgsPropertyOverrideButton *button, QgsWidgetWrapper::Property key)
Registers a property override button, setting up its initial value, connections and description...
Represents a vector layer which manages a vector based data sets.
QString fileName
Definition: qgsproject.h:86
RelativeStorage
The RelativeStorage enum determines if path is absolute, relative to the current project path or rela...
Definition: qgsfilewidget.h:74