@@ -77,6 +77,16 @@ QString QgsFileWidget::filePath()
77
77
return mFilePath ;
78
78
}
79
79
80
+ QStringList QgsFileWidget::splitFilePaths ( const QString &path )
81
+ {
82
+ QStringList paths;
83
+ for ( auto pathsPart : path.split ( QRegExp ( " \"\\ s+\" " ), QString::SkipEmptyParts ) )
84
+ {
85
+ paths.append ( pathsPart.remove ( QRegExp ( " (^\\ s*\" )|(\"\\ s*)" ) ) );
86
+ }
87
+ return paths;
88
+ }
89
+
80
90
void QgsFileWidget::setFilePath ( QString path )
81
91
{
82
92
if ( path == QgsApplication::nullRepresentation () )
@@ -86,6 +96,7 @@ void QgsFileWidget::setFilePath( QString path )
86
96
87
97
// will trigger textEdited slot
88
98
mLineEdit ->setValue ( path );
99
+
89
100
}
90
101
91
102
void QgsFileWidget::setReadOnly ( bool readOnly )
@@ -130,6 +141,15 @@ void QgsFileWidget::textEdited( const QString &path )
130
141
{
131
142
mFilePath = path;
132
143
mLinkLabel ->setText ( toUrl ( path ) );
144
+ // Show tooltip if multiple files are selected
145
+ if ( path.contains ( QStringLiteral ( " \" \" " ) ) )
146
+ {
147
+ mLineEdit ->setToolTip ( tr ( " Selected files:<br><ul><li>%1</li></ul><br>" ).arg ( splitFilePaths ( path ).join ( QStringLiteral ( " </li><li>" ) ) ) );
148
+ }
149
+ else
150
+ {
151
+ mLineEdit ->setToolTip ( QString () );
152
+ }
133
153
emit fileChanged ( mFilePath );
134
154
}
135
155
@@ -231,39 +251,75 @@ void QgsFileWidget::openFileDialog()
231
251
232
252
// Handle Storage
233
253
QString fileName;
254
+ QStringList fileNames;
234
255
QString title;
235
- if ( mStorageMode == GetFile )
236
- {
237
- title = !mDialogTitle .isEmpty () ? mDialogTitle : tr ( " Select a file" );
238
- fileName = QFileDialog::getOpenFileName ( this , title, QFileInfo ( oldPath ).absoluteFilePath (), mFilter );
239
- }
240
- else if ( mStorageMode == GetDirectory )
256
+
257
+ switch ( mStorageMode )
241
258
{
242
- title = !mDialogTitle .isEmpty () ? mDialogTitle : tr ( " Select a directory" );
243
- fileName = QFileDialog::getExistingDirectory ( this , title, QFileInfo ( oldPath ).absoluteFilePath (), QFileDialog::ShowDirsOnly );
259
+ case GetFile:
260
+ title = !mDialogTitle .isEmpty () ? mDialogTitle : tr ( " Select a file" );
261
+ fileName = QFileDialog::getOpenFileName ( this , title, QFileInfo ( oldPath ).absoluteFilePath (), mFilter );
262
+ break ;
263
+ case GetMultipleFiles:
264
+ title = !mDialogTitle .isEmpty () ? mDialogTitle : tr ( " Select one ore more files" );
265
+ fileNames = QFileDialog::getOpenFileNames ( this , title, QFileInfo ( oldPath ).absoluteFilePath (), mFilter );
266
+ break ;
267
+ case GetDirectory:
268
+ title = !mDialogTitle .isEmpty () ? mDialogTitle : tr ( " Select a directory" );
269
+ fileName = QFileDialog::getExistingDirectory ( this , title, QFileInfo ( oldPath ).absoluteFilePath (), QFileDialog::ShowDirsOnly );
270
+ break ;
244
271
}
245
272
246
- if ( fileName.isEmpty () )
273
+ if ( fileName.isEmpty () && fileNames. isEmpty ( ) )
247
274
return ;
248
275
276
+ if ( mStorageMode != GetMultipleFiles )
277
+ {
278
+ fileName = QDir::toNativeSeparators ( QDir::cleanPath ( QFileInfo ( fileName ).absoluteFilePath () ) );
279
+ }
280
+ else
281
+ {
282
+ for ( int i = 0 ; i < fileNames.length (); i++ )
283
+ {
284
+ fileNames.replace ( i, QDir::toNativeSeparators ( QDir::cleanPath ( QFileInfo ( fileNames.at ( i ) ).absoluteFilePath () ) ) ) ;
285
+ }
286
+ }
249
287
250
- fileName = QDir::toNativeSeparators ( QDir::cleanPath ( QFileInfo ( fileName ).absoluteFilePath () ) );
251
288
// Store the last used path:
289
+ switch ( mStorageMode )
290
+ {
291
+ case GetFile:
292
+ settings.setValue ( QStringLiteral ( " UI/lastFileNameWidgetDir" ), QFileInfo ( fileName ).absolutePath () );
293
+ break ;
294
+ case GetDirectory:
295
+ settings.setValue ( QStringLiteral ( " UI/lastFileNameWidgetDir" ), fileName );
296
+ break ;
297
+ case GetMultipleFiles:
298
+ settings.setValue ( QStringLiteral ( " UI/lastFileNameWidgetDir" ), QFileInfo ( fileNames.first ( ) ).absolutePath () );
299
+ break ;
300
+ }
252
301
253
- if ( mStorageMode == GetFile )
302
+ // Handle relative Path storage
303
+ if ( mStorageMode != GetMultipleFiles )
254
304
{
255
- settings.setValue ( QStringLiteral ( " UI/lastFileNameWidgetDir" ), QFileInfo ( fileName ).absolutePath () );
305
+ fileName = relativePath ( fileName, true );
306
+ setFilePath ( fileName );
256
307
}
257
- else if ( mStorageMode == GetDirectory )
308
+ else
258
309
{
259
- settings.setValue ( QStringLiteral ( " UI/lastFileNameWidgetDir" ), fileName );
310
+ for ( int i = 0 ; i < fileNames.length (); i++ )
311
+ {
312
+ fileNames.replace ( i, relativePath ( fileNames.at ( i ), true ) );
313
+ }
314
+ if ( fileNames.length () > 1 )
315
+ {
316
+ setFilePath ( QStringLiteral ( " \" %1\" " ).arg ( fileNames.join ( " \" \" " ) ) );
317
+ }
318
+ else
319
+ {
320
+ setFilePath ( fileNames.first ( ) );
321
+ }
260
322
}
261
-
262
- // Handle relative Path storage
263
- fileName = relativePath ( fileName, true );
264
-
265
- // Keep the new value
266
- setFilePath ( fileName );
267
323
}
268
324
269
325
@@ -327,7 +383,6 @@ QString QgsFileWidget::toUrl( const QString &path ) const
327
383
328
384
329
385
330
-
331
386
// /@cond PRIVATE
332
387
333
388
@@ -359,16 +414,30 @@ void QgsFileDropEdit::setFilters( const QString &filters )
359
414
360
415
QString QgsFileDropEdit::acceptableFilePath ( QDropEvent *event ) const
361
416
{
362
- QString path ;
417
+ QStringList paths ;
363
418
if ( event->mimeData ()->hasUrls () )
364
419
{
365
- QFileInfo file ( event->mimeData ()->urls ().first ().toLocalFile () );
366
- if ( ( mStorageMode == QgsFileWidget::GetFile && file.isFile () &&
367
- ( mAcceptableExtensions .isEmpty () || mAcceptableExtensions .contains ( file.suffix (), Qt::CaseInsensitive ) ) )
368
- || ( mStorageMode == QgsFileWidget::GetDirectory && file.isDir () ) )
369
- path = file.filePath ();
420
+ Q_FOREACH ( const QUrl &url, event->mimeData ()->urls () )
421
+ {
422
+ QFileInfo file ( url.toLocalFile () );
423
+ if ( ( mStorageMode != QgsFileWidget::GetDirectory && file.isFile () &&
424
+ ( mAcceptableExtensions .isEmpty () || mAcceptableExtensions .contains ( file.suffix (), Qt::CaseInsensitive ) ) )
425
+ || ( mStorageMode == QgsFileWidget::GetDirectory && file.isDir () ) )
426
+ paths.append ( file.filePath () );
427
+ }
428
+ }
429
+ if ( paths.size () > 1 )
430
+ {
431
+ return QStringLiteral ( " \" %1\" " ).arg ( paths.join ( " \" \" " ) );
432
+ }
433
+ else if ( paths.size () == 1 )
434
+ {
435
+ return paths.first ();
436
+ }
437
+ else
438
+ {
439
+ return QString ();
370
440
}
371
- return path;
372
441
}
373
442
374
443
void QgsFileDropEdit::dragEnterEvent ( QDragEnterEvent *event )
0 commit comments