QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
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 
20 #include <QFileDialog>
21 #include <QSettings>
22 
24 
26  : QgsEditorConfigWidget( vl, fieldIdx, parent )
27 {
28  setupUi( this );
29 
30  // By default, uncheck some options
31  mUseLink->setChecked( false );
32  mFullUrl->setChecked( false );
33  mDocumentViewerGroupBox->setChecked( false );
34 
36 
37  mRootPath->setPlaceholderText( QSettings().value( "/UI/lastExternalResourceWidgetDefaultPath", QDir::toNativeSeparators( QDir::cleanPath( defpath ) ) ).toString() );
38 
39  // Add connection to button for choosing default path
40  connect( mRootPathButton, SIGNAL( clicked() ), this, SLOT( chooseDefaultPath() ) );
41 
42  // Activate Relative Default Path option only if Default Path is set
43  connect( mRootPath, SIGNAL( textChanged( const QString & ) ), this, SLOT( enableRelativeDefault() ) );
44 
45  // Dynamic GroupBox for relative paths option
46  connect( mRelativeGroupBox, SIGNAL( toggled( bool ) ), this, SLOT( enableRelative( bool ) ) );
47 
48  // set ids for StorageTypeButtons
49  mStorageButtonGroup->setId( mStoreFilesButton, QgsFileWidget::GetFile );
50  mStorageButtonGroup->setId( mStoreDirsButton, QgsFileWidget::GetDirectory );
51  mStoreFilesButton->setChecked( true );
52 
53  // set ids for RelativeButtons
54  mRelativeButtonGroup->setId( mRelativeProject, QgsFileWidget::RelativeProject );
55  mRelativeButtonGroup->setId( mRelativeDefault, QgsFileWidget::RelativeDefaultPath );
56  mRelativeProject->setChecked( true );
57 
58  mDocumentViewerContentComboBox->addItem( tr( "Image" ), QgsExternalResourceWidget::Image );
59  mDocumentViewerContentComboBox->addItem( tr( "Web view" ), QgsExternalResourceWidget::Web );
60 
61 
62  connect( mFileWidgetGroupBox, SIGNAL( toggled( bool ) ), this, SIGNAL( changed() ) );
63  connect( mFileWidgetButtonGroupBox, SIGNAL( toggled( bool ) ), this, SIGNAL( changed() ) );
64  connect( mFileWidgetFilterLineEdit, SIGNAL( textChanged( QString ) ), this, SIGNAL( changed() ) );
65  connect( mUseLink, SIGNAL( toggled( bool ) ), this, SIGNAL( changed() ) );
66  connect( mFullUrl, SIGNAL( toggled( bool ) ), this, SIGNAL( changed() ) );
67  connect( mRootPath, SIGNAL( textChanged( QString ) ), this, SIGNAL( changed() ) );
68  connect( mStorageButtonGroup, SIGNAL( buttonClicked( int ) ), this, SIGNAL( changed() ) );
69  connect( mRelativeGroupBox, SIGNAL( toggled( bool ) ), this, SIGNAL( changed() ) );
70  connect( mDocumentViewerGroupBox, SIGNAL( toggled( bool ) ), this, SIGNAL( changed() ) );
71  connect( mDocumentViewerContentComboBox, SIGNAL( currentIndexChanged( int ) ), this, SIGNAL( changed() ) );
72  connect( mDocumentViewerHeight, SIGNAL( valueChanged( int ) ), this, SIGNAL( changed() ) );
73  connect( mDocumentViewerWidth, SIGNAL( valueChanged( int ) ), this, SIGNAL( changed() ) );
74 }
75 
76 void QgsExternalResourceConfigDlg::chooseDefaultPath()
77 {
78  QString dir;
79  if ( !mRootPath->text().isEmpty() )
80  {
81  dir = mRootPath->text();
82  }
83  else
84  {
85  dir = QSettings().value( "/UI/lastExternalResourceWidgetDefaultPath", QDir::toNativeSeparators( QDir::cleanPath( QgsProject::instance()->fileInfo().absolutePath() ) ) ).toString();
86  }
87 
88  QString rootName = QFileDialog::getExistingDirectory( this, tr( "Select a directory" ), dir, QFileDialog::ShowDirsOnly );
89 
90  if ( rootName.isNull() )
91  return;
92 
93  mRootPath->setText( rootName );
94 }
95 
96 void QgsExternalResourceConfigDlg::enableRelativeDefault()
97 {
98  // Activate (or not) the RelativeDefault button if default path
99  if ( mRelativeGroupBox->isChecked() )
100  mRelativeDefault->setEnabled( !mRootPath->text().isEmpty() );
101 
102  // If no default path, RelativeProj button enabled by default
103  if ( mRootPath->text().isEmpty() )
104  mRelativeProject->toggle();
105 }
106 
107 void QgsExternalResourceConfigDlg::enableRelative( bool state )
108 {
109  if ( state )
110  {
111  mRelativeProject->setEnabled( true );
112  if ( mRootPath->text().isEmpty() )
113  mRelativeDefault->setEnabled( false );
114  else
115  mRelativeDefault->setEnabled( true );
116  }
117  else
118  {
119  mRelativeProject->setEnabled( false );
120  mRelativeDefault->setEnabled( false );
121  }
122 }
123 
124 
126 {
128 
129  cfg.insert( "FileWidget", mFileWidgetGroupBox->isChecked() );
130  cfg.insert( "FileWidgetButton", mFileWidgetButtonGroupBox->isChecked() );
131  cfg.insert( "FileWidgetFilter", mFileWidgetFilterLineEdit->text() );
132 
133  if ( mUseLink->isChecked() )
134  {
135  cfg.insert( "UseLink", mUseLink->isChecked() );
136  if ( mFullUrl->isChecked() )
137  cfg.insert( "FullUrl", mFullUrl->isChecked() );
138  }
139 
140  if ( !mRootPath->text().isEmpty() )
141  {
142  cfg.insert( "DefaultRoot", mRootPath->text() );
143  }
144 
145  // Save Storage Mode
146  cfg.insert( "StorageMode", mStorageButtonGroup->checkedId() );
147 
148  // Save Relative Paths option
149  if ( mRelativeGroupBox->isChecked() )
150  {
151  cfg.insert( "RelativeStorage", mRelativeButtonGroup->checkedId() );
152  }
153  else
154  {
155  cfg.insert( "RelativeStorage", ( int )QgsFileWidget::Absolute );
156  }
157 
158  if ( mDocumentViewerGroupBox->isChecked() )
159  {
160  cfg.insert( "DocumentViewer", mDocumentViewerContentComboBox->itemData( mDocumentViewerContentComboBox->currentIndex() ).toInt() );
161  cfg.insert( "DocumentViewerHeight", mDocumentViewerHeight->value() );
162  cfg.insert( "DocumentViewerWidth", mDocumentViewerWidth->value() );
163  }
164  else
165  {
166  cfg.insert( "DocumentViewer", ( int )QgsExternalResourceWidget::NoContent );
167  }
168 
169  return cfg;
170 }
171 
172 
174 {
175  if ( config.contains( "FileWidget" ) )
176  {
177  mFileWidgetGroupBox->setChecked( config.value( "FileWidget" ).toBool() );
178  }
179  if ( config.contains( "FileWidget" ) )
180  {
181  mFileWidgetButtonGroupBox->setChecked( config.value( "FileWidgetButton" ).toBool() );
182  }
183  if ( config.contains( "FileWidgetFilter" ) )
184  {
185  mFileWidgetFilterLineEdit->setText( config.value( "FileWidgetFilter" ).toString() );
186  }
187 
188  if ( config.contains( "UseLink" ) )
189  {
190  mUseLink->setChecked( config.value( "UseLink" ).toBool() );
191  if ( config.contains( "FullUrl" ) )
192  mFullUrl->setChecked( true );
193  }
194 
195  if ( config.contains( "DefaultRoot" ) )
196  {
197  mRootPath->setText( config.value( "DefaultRoot" ).toString() );
198  }
199 
200  // relative storage
201  if ( config.contains( "RelativeStorage" ) )
202  {
203  int relative = config.value( "RelativeStorage" ).toInt();
205  {
206  mRelativeGroupBox->setChecked( false );
207  }
208  else
209  {
210  mRelativeGroupBox->setChecked( true );
211  mRelativeButtonGroup->button( relative )->setChecked( true );
212  }
213  }
214 
215  // set storage mode
216  if ( config.contains( "StorageMode" ) )
217  {
218  int mode = config.value( "StorageMode" ).toInt();
219  mStorageButtonGroup->button( mode )->setChecked( true );
220  }
221 
222  // Document viewer
223  if ( config.contains( "DocumentViewer" ) )
224  {
226  mDocumentViewerGroupBox->setChecked( content != QgsExternalResourceWidget::NoContent );
227  int idx = mDocumentViewerContentComboBox->findData( content );
228  if ( idx >= 0 )
229  {
230  mDocumentViewerContentComboBox->setCurrentIndex( idx );
231  }
232  if ( config.contains( "DocumentViewerHeight" ) )
233  {
234  mDocumentViewerHeight->setValue( config.value( "DocumentViewerHeight" ).toInt() );
235  }
236  if ( config.contains( "DocumentViewerWidth" ) )
237  {
238  mDocumentViewerWidth->setValue( config.value( "DocumentViewerWidth" ).toInt() );
239  }
240  }
241 }
QString toNativeSeparators(const QString &pathName)
QString getExistingDirectory(QWidget *parent, const QString &caption, const QString &dir, QFlags< QFileDialog::Option > options)
void setupUi(QWidget *widget)
This class should be subclassed for every configurable editor widget type.
void valueChanged()
Will call the value() method to determine the emitted value.
void setConfig(const QgsEditorWidgetConfig &config) override
Update the configuration widget to represent the given configuration.
QgsEditorWidgetConfig config() override
Create a configuration from the current GUI state.
QString homePath()
QString tr(const char *sourceText, const char *disambiguation, int n)
bool isNull() const
QVariant value() const override
Will be used to access the widget&#39;s value.
QVariantMap QgsEditorWidgetConfig
Holds a set of configuration parameters for a editor widget wrapper.
bool isEmpty() const
QgsExternalResourceConfigDlg(QgsVectorLayer *vl, int fieldIdx, QWidget *parent=0)
void changed()
Emitted when the configuration of the widget is changed.
QString cleanPath(const QString &path)
QVariant value(const QString &key, const QVariant &defaultValue) const
QFileInfo fileInfo() const
Returns QFileInfo object for the project&#39;s associated file.
Definition: qgsproject.cpp:442
static QgsProject * instance()
Returns the QgsProject singleton instance.
Definition: qgsproject.cpp:382
int fieldIdx() const
Access the field index.
QString absolutePath() const
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QObject * parent() const
Represents a vector layer which manages a vector based data sets.
QString toString() const
QString fileName
Definition: qgsproject.h:75
RelativeStorage
The RelativeStorage enum determines if path is absolute, relative to the current project path or rela...
Definition: qgsfilewidget.h:56