1717#include " qgsexternalresourcewidget.h"
1818#include " qgsproject.h"
1919#include " qgssettings.h"
20+ #include " qgsexpressionbuilderdialog.h"
21+ #include " qgsapplication.h"
22+ #include " qgsvectorlayer.h"
23+ #include " qgspropertyoverridebutton.h"
2024
2125#include < QFileDialog>
2226
2327class QgsExternalResourceWidgetWrapper ;
2428
29+ const QgsPropertiesDefinition &QgsExternalResourceConfigDlg::propertyDefinitions ()
30+ {
31+ static QgsPropertiesDefinition propertyDefinitions;
32+
33+ if ( propertyDefinitions.isEmpty () )
34+ {
35+ propertyDefinitions = QgsPropertiesDefinition
36+ {
37+ { RootPath, QgsPropertyDefinition ( " propertyRootPath" , QgsPropertyDefinition::DataTypeString, QObject::tr ( " Root path" ), QString () ) }
38+ };
39+ }
40+
41+ return propertyDefinitions;
42+ }
43+
2544QgsExternalResourceConfigDlg::QgsExternalResourceConfigDlg ( QgsVectorLayer *vl, int fieldIdx, QWidget *parent )
2645 : QgsEditorConfigWidget( vl, fieldIdx, parent )
2746{
@@ -36,14 +55,17 @@ QgsExternalResourceConfigDlg::QgsExternalResourceConfigDlg( QgsVectorLayer *vl,
3655
3756 mRootPath ->setPlaceholderText ( QgsSettings ().value ( QStringLiteral ( " /UI/lastExternalResourceWidgetDefaultPath" ), QDir::toNativeSeparators ( QDir::cleanPath ( defpath ) ) ).toString () );
3857
39- // Add connection to button for choosing default path
40- connect ( mRootPathButton , SIGNAL ( clicked () ), this , SLOT ( chooseDefaultPath () ) );
58+ connect ( mRootPathButton , &QToolButton::clicked, this , &QgsExternalResourceConfigDlg::chooseDefaultPath );
4159
42- // Activate Relative Default Path option only if Default Path is set
43- connect ( mRootPath , SIGNAL ( textChanged ( const QString & ) ), this , SLOT ( enableRelativeDefault () ) );
60+ mRootPathPropertyOverrideButton ->init ( RootPath, mPropertyCollection , propertyDefinitions (), vl );
4461
45- // Dynamic GroupBox for relative paths option
46- connect ( mRelativeGroupBox , SIGNAL ( toggled ( bool ) ), this , SLOT ( enableRelative ( bool ) ) );
62+ mRootPathPropertyOverrideButton ->setVectorLayer ( vl );
63+ connect ( mRootPathPropertyOverrideButton , &QgsPropertyOverrideButton::changed, this , &QgsExternalResourceConfigDlg::rootPathPropertyChanged );
64+
65+ // Activate Relative Default Path option only if Default Path is set
66+ connect ( mRootPath , &QLineEdit::textChanged, this , &QgsExternalResourceConfigDlg::enableRelativeDefault );
67+ connect ( mRootPathExpression , &QLineEdit::textChanged, this , &QgsExternalResourceConfigDlg::enableRelativeDefault );
68+ connect ( mRelativeGroupBox , &QGroupBox::toggled, this , &QgsExternalResourceConfigDlg::enableRelativeDefault );
4769
4870 // set ids for StorageTypeButtons
4971 mStorageButtonGroup ->setId ( mStoreFilesButton , QgsFileWidget::GetFile );
@@ -59,15 +81,15 @@ QgsExternalResourceConfigDlg::QgsExternalResourceConfigDlg( QgsVectorLayer *vl,
5981 mDocumentViewerContentComboBox ->addItem ( tr ( " Web view" ), QgsExternalResourceWidget::Web );
6082
6183
62- connect ( mFileWidgetGroupBox , SIGNAL ( toggled ( bool ) ) , this , SIGNAL ( changed () ) );
63- connect ( mFileWidgetButtonGroupBox , SIGNAL ( toggled ( bool ) ) , this , SIGNAL ( changed () ) );
84+ connect ( mFileWidgetGroupBox , &QGroupBox:: toggled, this , &QgsEditorConfigWidget:: changed );
85+ connect ( mFileWidgetButtonGroupBox , &QGroupBox:: toggled, this , &QgsEditorConfigWidget:: changed );
6486 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 () ) );
87+ connect ( mUseLink , &QGroupBox:: toggled, this , &QgsEditorConfigWidget:: changed );
88+ connect ( mFullUrl , &QAbstractButton:: toggled, this , &QgsEditorConfigWidget:: changed );
89+ connect ( mRootPath , &QLineEdit:: textChanged, this , &QgsEditorConfigWidget:: changed );
6890 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 () ) );
91+ connect ( mRelativeGroupBox , &QGroupBox:: toggled, this , &QgsEditorConfigWidget:: changed );
92+ connect ( mDocumentViewerGroupBox , &QGroupBox:: toggled, this , &QgsEditorConfigWidget:: changed );
7193 connect ( mDocumentViewerContentComboBox , SIGNAL ( currentIndexChanged ( int ) ), this , SIGNAL ( changed () ) );
7294 connect ( mDocumentViewerHeight , SIGNAL ( valueChanged ( int ) ), this , SIGNAL ( changed () ) );
7395 connect ( mDocumentViewerWidth , SIGNAL ( valueChanged ( int ) ), this , SIGNAL ( changed () ) );
@@ -87,40 +109,44 @@ void QgsExternalResourceConfigDlg::chooseDefaultPath()
87109
88110 QString rootName = QFileDialog::getExistingDirectory ( this , tr ( " Select a directory" ), dir, QFileDialog::ShowDirsOnly );
89111
90- if ( rootName.isNull () )
91- return ;
92-
93- mRootPath ->setText ( rootName );
112+ if ( !rootName.isNull () )
113+ mRootPath ->setText ( rootName );
94114}
95115
96- void QgsExternalResourceConfigDlg::enableRelativeDefault ()
116+ void QgsExternalResourceConfigDlg::rootPathPropertyChanged ()
97117{
98- // Activate (or not) the RelativeDefault button if default path
99- if ( mRelativeGroupBox ->isChecked () )
100- mRelativeDefault ->setEnabled ( !mRootPath ->text ().isEmpty () );
118+ QgsProperty prop = mRootPathPropertyOverrideButton ->toProperty ();
101119
102- // If no default path, RelativeProj button enabled by default
103- if ( mRootPath ->text ().isEmpty () )
104- mRelativeProject ->toggle ();
120+ setRootPathExpression ( prop.expressionString () );
121+
122+ mRootPathExpression ->setVisible ( prop.isActive () );
123+ mRootPath ->setVisible ( !prop.isActive () );
124+ mRootPathButton ->setEnabled ( !prop.isActive () );
105125}
106126
107- void QgsExternalResourceConfigDlg::enableRelative ( bool state )
127+ void QgsExternalResourceConfigDlg::enableRelativeDefault ( )
108128{
109- if ( state )
129+ bool relativePathActive = false ;
130+
131+ if ( mRootPathPropertyOverrideButton ->isActive () )
110132 {
111- mRelativeProject ->setEnabled ( true );
112- if ( mRootPath ->text ().isEmpty () )
113- mRelativeDefault ->setEnabled ( false );
114- else
115- mRelativeDefault ->setEnabled ( true );
133+ if ( !mRootPathExpression ->text ().isEmpty () )
134+ relativePathActive = true ;
116135 }
117136 else
118137 {
119- mRelativeProject -> setEnabled ( false );
120- mRelativeDefault -> setEnabled ( false ) ;
138+ if ( ! mRootPath -> text (). isEmpty () )
139+ relativePathActive = true ;
121140 }
122- }
123141
142+ // Activate (or not) the RelativeDefault button if default path
143+ if ( mRelativeGroupBox ->isChecked () )
144+ mRelativeDefault ->setEnabled ( relativePathActive );
145+
146+ // If no default path, RelativeProj button enabled by default
147+ if ( !relativePathActive )
148+ mRelativeProject ->toggle ();
149+ }
124150
125151QVariantMap QgsExternalResourceConfigDlg::config ()
126152{
@@ -137,10 +163,17 @@ QVariantMap QgsExternalResourceConfigDlg::config()
137163 cfg.insert ( QStringLiteral ( " FullUrl" ), mFullUrl ->isChecked () );
138164 }
139165
166+ if ( mRootPathPropertyOverrideButton ->isActive () )
167+ cfg.insert ( QStringLiteral ( " DefaultRootStyle" ), QStringLiteral ( " expression" ) );
168+ else
169+ cfg.insert ( QStringLiteral ( " DefaultRootStyle" ), QStringLiteral ( " path" ) );
170+
171+
140172 if ( !mRootPath ->text ().isEmpty () )
141- {
142173 cfg.insert ( QStringLiteral ( " DefaultRoot" ), mRootPath ->text () );
143- }
174+
175+ if ( !mRootPathExpression ->text ().isEmpty () )
176+ cfg.insert ( QStringLiteral ( " DefaultRootExpression" ), mRootPathExpression ->toolTip () );
144177
145178 // Save Storage Mode
146179 cfg.insert ( QStringLiteral ( " StorageMode" ), mStorageButtonGroup ->checkedId () );
@@ -192,10 +225,18 @@ void QgsExternalResourceConfigDlg::setConfig( const QVariantMap &config )
192225 mFullUrl ->setChecked ( true );
193226 }
194227
195- if ( config.contains ( QStringLiteral ( " DefaultRoot" ) ) )
196- {
197- mRootPath ->setText ( config.value ( QStringLiteral ( " DefaultRoot" ) ).toString () );
198- }
228+ mRootPath ->setText ( config.value ( QStringLiteral ( " DefaultRoot" ) ).toString () );
229+ setRootPathExpression ( config.value ( QStringLiteral ( " DefaultRootExpression" ) ).toString () );
230+
231+ bool rootPathIsExpression = config.value ( QStringLiteral ( " DefaultRootStyle" ) ) == QStringLiteral ( " expression" );
232+
233+ QgsProperty prop = mRootPathPropertyOverrideButton ->toProperty ();
234+ prop.setActive ( rootPathIsExpression );
235+ mRootPathPropertyOverrideButton ->setToProperty ( prop );
236+ rootPathPropertyChanged ();
237+
238+ mRootPathExpression ->setVisible ( rootPathIsExpression );
239+ mRootPath ->setVisible ( !rootPathIsExpression );
199240
200241 // relative storage
201242 if ( config.contains ( QStringLiteral ( " RelativeStorage" ) ) )
@@ -239,3 +280,19 @@ void QgsExternalResourceConfigDlg::setConfig( const QVariantMap &config )
239280 }
240281 }
241282}
283+
284+ void QgsExternalResourceConfigDlg::setRootPathExpression ( const QString &expression )
285+ {
286+ mRootPathExpression ->setToolTip ( expression );
287+ mRootPathPropertyOverrideButton ->setText ( expression );
288+
289+ QgsProperty prop = mRootPathPropertyOverrideButton ->toProperty ();
290+ prop.setExpressionString ( expression );
291+ mRootPathPropertyOverrideButton ->setToProperty ( prop );
292+
293+ QgsExpression exp ( expression );
294+ QgsExpressionContext ctx = layer ()->createExpressionContext ();
295+
296+ mRootPathExpression ->setText ( exp.evaluate ( &ctx ).toString () );
297+ enableRelativeDefault ();
298+ }
0 commit comments