QGIS API Documentation  2.14.0-Essen
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 void QgsExternalResourceConfigDlg::chooseDefaultPath()
63 {
64  QString dir;
65  if ( !mRootPath->text().isEmpty() )
66  {
67  dir = mRootPath->text();
68  }
69  else
70  {
71  dir = QSettings().value( "/UI/lastExternalResourceWidgetDefaultPath", QDir::toNativeSeparators( QDir::cleanPath( QgsProject::instance()->fileInfo().absolutePath() ) ) ).toString();
72  }
73 
74  QString rootName = QFileDialog::getExistingDirectory( this, tr( "Select a directory" ), dir, QFileDialog::ShowDirsOnly );
75 
76  if ( rootName.isNull() )
77  return;
78 
79  mRootPath->setText( rootName );
80 }
81 
82 void QgsExternalResourceConfigDlg::enableRelativeDefault()
83 {
84  // Activate (or not) the RelativeDefault button if default path
85  if ( mRelativeGroupBox->isChecked() )
86  mRelativeDefault->setEnabled( !mRootPath->text().isEmpty() );
87 
88  // If no default path, RelativeProj button enabled by default
89  if ( mRootPath->text().isEmpty() )
90  mRelativeProject->toggle();
91 }
92 
93 void QgsExternalResourceConfigDlg::enableRelative( bool state )
94 {
95  if ( state )
96  {
97  mRelativeProject->setEnabled( true );
98  if ( mRootPath->text().isEmpty() )
99  mRelativeDefault->setEnabled( false );
100  else
101  mRelativeDefault->setEnabled( true );
102  }
103  else
104  {
105  mRelativeProject->setEnabled( false );
106  mRelativeDefault->setEnabled( false );
107  }
108 }
109 
110 
112 {
114 
115  cfg.insert( "FileWidget", mFileWidgetGroupBox->isChecked() );
116  cfg.insert( "FileWidgetButton", mFileWidgetButtonGroupBox->isChecked() );
117  cfg.insert( "FileWidgetFilter", mFileWidgetFilterLineEdit->text() );
118 
119  if ( mUseLink->isChecked() )
120  {
121  cfg.insert( "UseLink", mUseLink->isChecked() );
122  if ( mFullUrl->isChecked() )
123  cfg.insert( "FullUrl", mFullUrl->isChecked() );
124  }
125 
126  if ( !mRootPath->text().isEmpty() )
127  {
128  cfg.insert( "DefaultRoot", mRootPath->text() );
129  }
130 
131  // Save Storage Mode
132  cfg.insert( "StorageMode", mStorageButtonGroup->checkedId() );
133 
134  // Save Relative Paths option
135  if ( mRelativeGroupBox->isChecked() )
136  {
137  cfg.insert( "RelativeStorage", mRelativeButtonGroup->checkedId() );
138  }
139  else
140  {
141  cfg.insert( "RelativeStorage", ( int )QgsFileWidget::Absolute );
142  }
143 
144  if ( mDocumentViewerGroupBox->isChecked() )
145  {
146  cfg.insert( "DocumentViewer", mDocumentViewerContentComboBox->itemData( mDocumentViewerContentComboBox->currentIndex() ).toInt() );
147  cfg.insert( "DocumentViewerHeight", mDocumentViewerHeight->value() );
148  cfg.insert( "DocumentViewerWidth", mDocumentViewerWidth->value() );
149  }
150  else
151  {
152  cfg.insert( "DocumentViewer", ( int )QgsExternalResourceWidget::NoContent );
153  }
154 
155  return cfg;
156 }
157 
158 
160 {
161  if ( config.contains( "FileWidget" ) )
162  {
163  mFileWidgetGroupBox->setChecked( config.value( "FileWidget" ).toBool() );
164  }
165  if ( config.contains( "FileWidget" ) )
166  {
167  mFileWidgetButtonGroupBox->setChecked( config.value( "FileWidgetButton" ).toBool() );
168  }
169  if ( config.contains( "FileWidgetFilter" ) )
170  {
171  mFileWidgetFilterLineEdit->setText( config.value( "FileWidgetFilter" ).toString() );
172  }
173 
174  if ( config.contains( "UseLink" ) )
175  {
176  mUseLink->setChecked( config.value( "UseLink" ).toBool() );
177  if ( config.contains( "FullUrl" ) )
178  mFullUrl->setChecked( true );
179  }
180 
181  if ( config.contains( "DefaultRoot" ) )
182  {
183  mRootPath->setText( config.value( "DefaultRoot" ).toString() );
184  }
185 
186  // relative storage
187  if ( config.contains( "RelativeStorage" ) )
188  {
189  int relative = config.value( "RelativeStorage" ).toInt();
191  {
192  mRelativeGroupBox->setChecked( false );
193  }
194  else
195  {
196  mRelativeGroupBox->setChecked( true );
197  mRelativeButtonGroup->button( relative )->setChecked( true );
198  }
199  }
200 
201  // set storage mode
202  if ( config.contains( "StorageMode" ) )
203  {
204  int mode = config.value( "StorageMode" ).toInt();
205  mStorageButtonGroup->button( mode )->setChecked( true );
206  }
207 
208  // Document viewer
209  if ( config.contains( "DocumentViewer" ) )
210  {
212  mDocumentViewerGroupBox->setChecked( content != QgsExternalResourceWidget::NoContent );
213  int idx = mDocumentViewerContentComboBox->findData( content );
214  if ( idx >= 0 )
215  {
216  mDocumentViewerContentComboBox->setCurrentIndex( idx );
217  }
218  if ( config.contains( "DocumentViewerHeight" ) )
219  {
220  mDocumentViewerHeight->setValue( config.value( "DocumentViewerHeight" ).toInt() );
221  }
222  if ( config.contains( "DocumentViewerWidth" ) )
223  {
224  mDocumentViewerWidth->setValue( config.value( "DocumentViewerWidth" ).toInt() );
225  }
226  }
227 }
QString toNativeSeparators(const QString &pathName)
QString getExistingDirectory(QWidget *parent, const QString &caption, const QString &dir, QFlags< QFileDialog::Option > options)
int fieldIdx() const
Access the field index.
void setupUi(QWidget *widget)
bool contains(const Key &key) const
This class should be subclassed for every configurable editor widget type.
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.
bool isEmpty() const
QString fileName() const
Returns file name.
Definition: qgsproject.cpp:431
QgsExternalResourceConfigDlg(QgsVectorLayer *vl, int fieldIdx, QWidget *parent=0)
QString cleanPath(const QString &path)
QVariant value(const QString &key, const QVariant &defaultValue) const
static QgsProject * instance()
access to canonical QgsProject instance
Definition: qgsproject.cpp:381
iterator insert(const Key &key, const T &value)
QFileInfo fileInfo() const
Returns QFileInfo object for the project&#39;s associated file.
Definition: qgsproject.cpp:436
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
RelativeStorage
The RelativeStorage enum determines if path is absolute, relative to the current project path or rela...
Definition: qgsfilewidget.h:56
const T value(const Key &key) const