17
17
#include " qgsexternalresourcewidget.h"
18
18
#include " qgsproject.h"
19
19
#include " qgssettings.h"
20
+ #include " qgsexpressionbuilderdialog.h"
21
+ #include " qgsapplication.h"
22
+ #include " qgsvectorlayer.h"
23
+ #include " qgspropertyoverridebutton.h"
20
24
21
25
#include < QFileDialog>
22
26
23
27
class QgsExternalResourceWidgetWrapper ;
24
28
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
+
25
44
QgsExternalResourceConfigDlg::QgsExternalResourceConfigDlg ( QgsVectorLayer *vl, int fieldIdx, QWidget *parent )
26
45
: QgsEditorConfigWidget( vl, fieldIdx, parent )
27
46
{
@@ -36,14 +55,17 @@ QgsExternalResourceConfigDlg::QgsExternalResourceConfigDlg( QgsVectorLayer *vl,
36
55
37
56
mRootPath ->setPlaceholderText ( QgsSettings ().value ( QStringLiteral ( " /UI/lastExternalResourceWidgetDefaultPath" ), QDir::toNativeSeparators ( QDir::cleanPath ( defpath ) ) ).toString () );
38
57
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 );
41
59
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 );
44
61
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 );
47
69
48
70
// set ids for StorageTypeButtons
49
71
mStorageButtonGroup ->setId ( mStoreFilesButton , QgsFileWidget::GetFile );
@@ -59,15 +81,15 @@ QgsExternalResourceConfigDlg::QgsExternalResourceConfigDlg( QgsVectorLayer *vl,
59
81
mDocumentViewerContentComboBox ->addItem ( tr ( " Web view" ), QgsExternalResourceWidget::Web );
60
82
61
83
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 );
64
86
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 );
68
90
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 );
71
93
connect ( mDocumentViewerContentComboBox , SIGNAL ( currentIndexChanged ( int ) ), this , SIGNAL ( changed () ) );
72
94
connect ( mDocumentViewerHeight , SIGNAL ( valueChanged ( int ) ), this , SIGNAL ( changed () ) );
73
95
connect ( mDocumentViewerWidth , SIGNAL ( valueChanged ( int ) ), this , SIGNAL ( changed () ) );
@@ -87,40 +109,44 @@ void QgsExternalResourceConfigDlg::chooseDefaultPath()
87
109
88
110
QString rootName = QFileDialog::getExistingDirectory ( this , tr ( " Select a directory" ), dir, QFileDialog::ShowDirsOnly );
89
111
90
- if ( rootName.isNull () )
91
- return ;
92
-
93
- mRootPath ->setText ( rootName );
112
+ if ( !rootName.isNull () )
113
+ mRootPath ->setText ( rootName );
94
114
}
95
115
96
- void QgsExternalResourceConfigDlg::enableRelativeDefault ()
116
+ void QgsExternalResourceConfigDlg::rootPathPropertyChanged ()
97
117
{
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 ();
101
119
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 () );
105
125
}
106
126
107
- void QgsExternalResourceConfigDlg::enableRelative ( bool state )
127
+ void QgsExternalResourceConfigDlg::enableRelativeDefault ( )
108
128
{
109
- if ( state )
129
+ bool relativePathActive = false ;
130
+
131
+ if ( mRootPathPropertyOverrideButton ->isActive () )
110
132
{
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 ;
116
135
}
117
136
else
118
137
{
119
- mRelativeProject -> setEnabled ( false );
120
- mRelativeDefault -> setEnabled ( false ) ;
138
+ if ( ! mRootPath -> text (). isEmpty () )
139
+ relativePathActive = true ;
121
140
}
122
- }
123
141
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
+ }
124
150
125
151
QVariantMap QgsExternalResourceConfigDlg::config ()
126
152
{
@@ -137,10 +163,17 @@ QVariantMap QgsExternalResourceConfigDlg::config()
137
163
cfg.insert ( QStringLiteral ( " FullUrl" ), mFullUrl ->isChecked () );
138
164
}
139
165
166
+ if ( mRootPathPropertyOverrideButton ->isActive () )
167
+ cfg.insert ( QStringLiteral ( " DefaultRootStyle" ), QStringLiteral ( " expression" ) );
168
+ else
169
+ cfg.insert ( QStringLiteral ( " DefaultRootStyle" ), QStringLiteral ( " path" ) );
170
+
171
+
140
172
if ( !mRootPath ->text ().isEmpty () )
141
- {
142
173
cfg.insert ( QStringLiteral ( " DefaultRoot" ), mRootPath ->text () );
143
- }
174
+
175
+ if ( !mRootPathExpression ->text ().isEmpty () )
176
+ cfg.insert ( QStringLiteral ( " DefaultRootExpression" ), mRootPathExpression ->toolTip () );
144
177
145
178
// Save Storage Mode
146
179
cfg.insert ( QStringLiteral ( " StorageMode" ), mStorageButtonGroup ->checkedId () );
@@ -192,10 +225,18 @@ void QgsExternalResourceConfigDlg::setConfig( const QVariantMap &config )
192
225
mFullUrl ->setChecked ( true );
193
226
}
194
227
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 );
199
240
200
241
// relative storage
201
242
if ( config.contains ( QStringLiteral ( " RelativeStorage" ) ) )
@@ -239,3 +280,19 @@ void QgsExternalResourceConfigDlg::setConfig( const QVariantMap &config )
239
280
}
240
281
}
241
282
}
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