QGIS API Documentation  3.20.0-Odense (decaadbb31)
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() : QFileInfo( QgsProject::instance()->absoluteFilePath() ).path();
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  QString path = QFileInfo( QgsProject::instance()->absoluteFilePath() ).path();
101  dir = QgsSettings().value( QStringLiteral( "/UI/lastExternalResourceWidgetDefaultPath" ), QDir::toNativeSeparators( QDir::cleanPath( path ) ) ).toString();
102  }
103 
104  QString rootName = QFileDialog::getExistingDirectory( this, tr( "Select a directory" ), dir, QFileDialog::ShowDirsOnly );
105 
106  if ( !rootName.isNull() )
107  mRootPath->setText( rootName );
108 }
109 
110 void QgsExternalResourceConfigDlg::enableRelativeDefault()
111 {
112  bool relativePathActive = false;
113 
114  if ( mRootPathPropertyOverrideButton->isActive() )
115  {
116  if ( !mRootPathExpression->text().isEmpty() )
117  relativePathActive = true;
118  }
119  else
120  {
121  if ( !mRootPath->text().isEmpty() )
122  relativePathActive = true;
123  }
124 
125  // Activate (or not) the RelativeDefault button if default path
126  if ( mRelativeGroupBox->isChecked() )
127  mRelativeDefault->setEnabled( relativePathActive );
128 
129  // If no default path, RelativeProj button enabled by default
130  if ( !relativePathActive )
131  mRelativeProject->toggle();
132 }
133 
135 {
136  QVariantMap cfg;
137 
138  cfg.insert( QStringLiteral( "FileWidget" ), mFileWidgetGroupBox->isChecked() );
139  cfg.insert( QStringLiteral( "FileWidgetButton" ), mFileWidgetButtonGroupBox->isChecked() );
140  cfg.insert( QStringLiteral( "FileWidgetFilter" ), mFileWidgetFilterLineEdit->text() );
141 
142  if ( mUseLink->isChecked() )
143  {
144  cfg.insert( QStringLiteral( "UseLink" ), mUseLink->isChecked() );
145  if ( mFullUrl->isChecked() )
146  cfg.insert( QStringLiteral( "FullUrl" ), mFullUrl->isChecked() );
147  }
148 
149  cfg.insert( QStringLiteral( "PropertyCollection" ), mPropertyCollection.toVariant( QgsWidgetWrapper::propertyDefinitions() ) );
150 
151  if ( !mRootPath->text().isEmpty() )
152  cfg.insert( QStringLiteral( "DefaultRoot" ), mRootPath->text() );
153 
154  // Save Storage Mode
155  cfg.insert( QStringLiteral( "StorageMode" ), mStorageButtonGroup->checkedId() );
156 
157  // Save Relative Paths option
158  if ( mRelativeGroupBox->isChecked() )
159  {
160  cfg.insert( QStringLiteral( "RelativeStorage" ), mRelativeButtonGroup->checkedId() );
161  }
162  else
163  {
164  cfg.insert( QStringLiteral( "RelativeStorage" ), static_cast<int>( QgsFileWidget::Absolute ) );
165  }
166 
167  cfg.insert( QStringLiteral( "DocumentViewer" ), mDocumentViewerContentComboBox->currentData().toInt() );
168  cfg.insert( QStringLiteral( "DocumentViewerHeight" ), mDocumentViewerHeight->value() );
169  cfg.insert( QStringLiteral( "DocumentViewerWidth" ), mDocumentViewerWidth->value() );
170 
171  return cfg;
172 }
173 
174 
176 {
177  if ( config.contains( QStringLiteral( "FileWidget" ) ) )
178  {
179  mFileWidgetGroupBox->setChecked( config.value( QStringLiteral( "FileWidget" ) ).toBool() );
180  }
181  if ( config.contains( QStringLiteral( "FileWidget" ) ) )
182  {
183  mFileWidgetButtonGroupBox->setChecked( config.value( QStringLiteral( "FileWidgetButton" ) ).toBool() );
184  }
185  if ( config.contains( QStringLiteral( "FileWidgetFilter" ) ) )
186  {
187  mFileWidgetFilterLineEdit->setText( config.value( QStringLiteral( "FileWidgetFilter" ) ).toString() );
188  }
189 
190  if ( config.contains( QStringLiteral( "UseLink" ) ) )
191  {
192  mUseLink->setChecked( config.value( QStringLiteral( "UseLink" ) ).toBool() );
193  if ( config.contains( QStringLiteral( "FullUrl" ) ) )
194  mFullUrl->setChecked( true );
195  }
196 
197  mPropertyCollection.loadVariant( config.value( QStringLiteral( "PropertyCollection" ) ), QgsWidgetWrapper::propertyDefinitions() );
199 
200  mRootPath->setText( config.value( QStringLiteral( "DefaultRoot" ) ).toString() );
201 
202  // relative storage
203  if ( config.contains( QStringLiteral( "RelativeStorage" ) ) )
204  {
205  int relative = config.value( QStringLiteral( "RelativeStorage" ) ).toInt();
207  {
208  mRelativeGroupBox->setChecked( false );
209  }
210  else
211  {
212  mRelativeGroupBox->setChecked( true );
213  mRelativeButtonGroup->button( relative )->setChecked( true );
214  }
215  }
216 
217  // set storage mode
218  if ( config.contains( QStringLiteral( "StorageMode" ) ) )
219  {
220  int mode = config.value( QStringLiteral( "StorageMode" ) ).toInt();
221  mStorageButtonGroup->button( mode )->setChecked( true );
222  }
223 
224  // Document viewer
225  if ( config.contains( QStringLiteral( "DocumentViewer" ) ) )
226  {
227  QgsExternalResourceWidget::DocumentViewerContent content = ( QgsExternalResourceWidget::DocumentViewerContent )config.value( QStringLiteral( "DocumentViewer" ) ).toInt();
228  int idx = mDocumentViewerContentComboBox->findData( content );
229  if ( idx >= 0 )
230  {
231  mDocumentViewerContentComboBox->setCurrentIndex( idx );
232  }
233  if ( config.contains( QStringLiteral( "DocumentViewerHeight" ) ) )
234  {
235  mDocumentViewerHeight->setValue( config.value( QStringLiteral( "DocumentViewerHeight" ) ).toInt() );
236  }
237  if ( config.contains( QStringLiteral( "DocumentViewerWidth" ) ) )
238  {
239  mDocumentViewerWidth->setValue( config.value( QStringLiteral( "DocumentViewerWidth" ) ).toInt() );
240  }
241  }
242 }
This class should be subclassed for every configurable editor widget type.
void updateDataDefinedButtons()
Updates all property override buttons to reflect the widgets's current properties.
QgsPropertyCollection mPropertyCollection
Temporary property collection for config widgets.
void initializeDataDefinedButton(QgsPropertyOverrideButton *button, QgsWidgetWrapper::Property key)
Registers a property override button, setting up its initial value, connections and description.
void changed()
Emitted when the configuration of the widget is changed.
int fieldIdx() const
Access the field index.
void setConfig(const QVariantMap &config) override
Update the configuration widget to represent the given configuration.
QVariantMap config() override
Create a configuration from the current GUI state.
QgsExternalResourceConfigDlg(QgsVectorLayer *vl, int fieldIdx, QWidget *parent=nullptr)
Constructor for QgsExternalResourceConfigDlg.
QVariant value() const override
Will be used to access the widget's value.
@ GetFile
Select a single file.
Definition: qgsfilewidget.h:66
@ GetDirectory
Select a directory.
Definition: qgsfilewidget.h:67
RelativeStorage
The RelativeStorage enum determines if path is absolute, relative to the current project path or rela...
Definition: qgsfilewidget.h:77
static QgsProject * instance()
Returns the QgsProject singleton instance.
Definition: qgsproject.cpp:467
QString fileName
Definition: qgsproject.h:102
bool loadVariant(const QVariant &configuration, const QgsPropertiesDefinition &definitions) override
Loads this property collection from a QVariantMap, wrapped in a QVariant.
QVariant toVariant(const QgsPropertiesDefinition &definitions) const override
Saves this property collection to a QVariantMap, wrapped in a QVariant.
Represents a vector layer which manages a vector based data sets.
static const QgsPropertiesDefinition & propertyDefinitions()
Returns the editor widget property definitions.
@ DocumentViewerContent
Document type for external resource.
@ RootPath
Root path for external resource.
QVariantMap config() const
Returns the whole config.