/
qgsfilewidget.cpp
499 lines (433 loc) · 12.6 KB
/
qgsfilewidget.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
/***************************************************************************
qgsfilewidget.cpp
---------------------
begin : 17.12.2015
copyright : (C) 2015 by Denis Rouzaud
email : denis.rouzaud@gmail.com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "qgsfilewidget.h"
#include <QLineEdit>
#include <QToolButton>
#include <QLabel>
#include <QFileDialog>
#include <QGridLayout>
#include <QUrl>
#include <QDropEvent>
#include "qgssettings.h"
#include "qgsfilterlineedit.h"
#include "qgslogger.h"
#include "qgsproject.h"
#include "qgsapplication.h"
QgsFileWidget::QgsFileWidget( QWidget *parent )
: QWidget( parent )
{
setBackgroundRole( QPalette::Window );
setAutoFillBackground( true );
mLayout = new QHBoxLayout();
mLayout->setMargin( 0 );
// If displaying a hyperlink, use a QLabel
mLinkLabel = new QLabel( this );
// Make Qt opens the link with the OS defined viewer
mLinkLabel->setOpenExternalLinks( true );
// Label should always be enabled to be able to open
// the link on read only mode.
mLinkLabel->setEnabled( true );
mLinkLabel->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Preferred );
mLinkLabel->setTextFormat( Qt::RichText );
mLinkLabel->hide(); // do not show by default
// otherwise, use the traditional QLineEdit subclass
mLineEdit = new QgsFileDropEdit( this );
connect( mLineEdit, &QLineEdit::textChanged, this, &QgsFileWidget::textEdited );
mLayout->addWidget( mLineEdit );
mFileWidgetButton = new QToolButton( this );
mFileWidgetButton->setText( QChar( 0x2026 ) );
connect( mFileWidgetButton, &QAbstractButton::clicked, this, &QgsFileWidget::openFileDialog );
mLayout->addWidget( mFileWidgetButton );
setLayout( mLayout );
}
QString QgsFileWidget::filePath()
{
return mFilePath;
}
QStringList QgsFileWidget::splitFilePaths( const QString &path )
{
QStringList paths;
const QStringList pathParts = path.split( QRegExp( "\"\\s+\"" ), QString::SkipEmptyParts );
for ( const auto &pathsPart : pathParts )
{
QString cleaned = pathsPart;
cleaned.remove( QRegExp( "(^\\s*\")|(\"\\s*)" ) );
paths.append( cleaned );
}
return paths;
}
void QgsFileWidget::setFilePath( QString path )
{
if ( path == QgsApplication::nullRepresentation() )
{
path.clear();
}
//will trigger textEdited slot
mLineEdit->setValue( path );
}
void QgsFileWidget::setReadOnly( bool readOnly )
{
mFileWidgetButton->setEnabled( !readOnly );
mLineEdit->setEnabled( !readOnly );
}
QString QgsFileWidget::dialogTitle() const
{
return mDialogTitle;
}
void QgsFileWidget::setDialogTitle( const QString &title )
{
mDialogTitle = title;
}
QString QgsFileWidget::filter() const
{
return mFilter;
}
void QgsFileWidget::setFilter( const QString &filters )
{
mFilter = filters;
mLineEdit->setFilters( filters );
}
bool QgsFileWidget::fileWidgetButtonVisible() const
{
return mButtonVisible;
}
void QgsFileWidget::setFileWidgetButtonVisible( bool visible )
{
mButtonVisible = visible;
mFileWidgetButton->setVisible( visible );
}
void QgsFileWidget::textEdited( const QString &path )
{
mFilePath = path;
mLinkLabel->setText( toUrl( path ) );
// Show tooltip if multiple files are selected
if ( path.contains( QStringLiteral( "\" \"" ) ) )
{
mLineEdit->setToolTip( tr( "Selected files:<br><ul><li>%1</li></ul><br>" ).arg( splitFilePaths( path ).join( QStringLiteral( "</li><li>" ) ) ) );
}
else
{
mLineEdit->setToolTip( QString() );
}
emit fileChanged( mFilePath );
}
bool QgsFileWidget::useLink() const
{
return mUseLink;
}
void QgsFileWidget::setUseLink( bool useLink )
{
mUseLink = useLink;
mLinkLabel->setVisible( mUseLink );
mLineEdit->setVisible( !mUseLink );
if ( mUseLink )
{
mLayout->removeWidget( mLineEdit );
mLayout->insertWidget( 0, mLinkLabel );
}
else
{
mLayout->removeWidget( mLinkLabel );
mLayout->insertWidget( 0, mLineEdit );
}
}
bool QgsFileWidget::fullUrl() const
{
return mFullUrl;
}
void QgsFileWidget::setFullUrl( bool fullUrl )
{
mFullUrl = fullUrl;
}
QString QgsFileWidget::defaultRoot() const
{
return mDefaultRoot;
}
void QgsFileWidget::setDefaultRoot( const QString &defaultRoot )
{
mDefaultRoot = defaultRoot;
}
QgsFileWidget::StorageMode QgsFileWidget::storageMode() const
{
return mStorageMode;
}
void QgsFileWidget::setStorageMode( QgsFileWidget::StorageMode storageMode )
{
mStorageMode = storageMode;
mLineEdit->setStorageMode( storageMode );
}
QgsFileWidget::RelativeStorage QgsFileWidget::relativeStorage() const
{
return mRelativeStorage;
}
void QgsFileWidget::setRelativeStorage( QgsFileWidget::RelativeStorage relativeStorage )
{
mRelativeStorage = relativeStorage;
}
QgsFilterLineEdit *QgsFileWidget::lineEdit()
{
return mLineEdit;
}
void QgsFileWidget::openFileDialog()
{
QgsSettings settings;
QString oldPath;
// If we use fixed default path
if ( !mDefaultRoot.isEmpty() )
{
oldPath = QDir::cleanPath( mDefaultRoot );
}
// if we use a relative path option, we need to obtain the full path
else if ( !mFilePath.isEmpty() )
{
oldPath = relativePath( mFilePath, false );
}
// If there is no valid value, find a default path to use
QUrl url = QUrl::fromUserInput( oldPath );
if ( !url.isValid() )
{
QString defPath = QDir::cleanPath( QgsProject::instance()->fileInfo().absolutePath() );
if ( defPath.isEmpty() )
{
defPath = QDir::homePath();
}
oldPath = settings.value( QStringLiteral( "UI/lastFileNameWidgetDir" ), defPath ).toString();
}
// Handle Storage
QString fileName;
QStringList fileNames;
QString title;
switch ( mStorageMode )
{
case GetFile:
title = !mDialogTitle.isEmpty() ? mDialogTitle : tr( "Select a file" );
fileName = QFileDialog::getOpenFileName( this, title, QFileInfo( oldPath ).absoluteFilePath(), mFilter );
break;
case GetMultipleFiles:
title = !mDialogTitle.isEmpty() ? mDialogTitle : tr( "Select one or more files" );
fileNames = QFileDialog::getOpenFileNames( this, title, QFileInfo( oldPath ).absoluteFilePath(), mFilter );
break;
case GetDirectory:
title = !mDialogTitle.isEmpty() ? mDialogTitle : tr( "Select a directory" );
fileName = QFileDialog::getExistingDirectory( this, title, QFileInfo( oldPath ).absoluteFilePath(), QFileDialog::ShowDirsOnly );
break;
case SaveFile:
title = !mDialogTitle.isEmpty() ? mDialogTitle : tr( "Create or select a file" );
if ( !confirmOverwrite() )
{
fileName = QFileDialog::getSaveFileName( this, title, QFileInfo( oldPath ).absoluteFilePath(), mFilter, nullptr, QFileDialog::DontConfirmOverwrite );
}
else
{
fileName = QFileDialog::getSaveFileName( this, title, QFileInfo( oldPath ).absoluteFilePath(), mFilter );
}
break;
}
if ( fileName.isEmpty() && fileNames.isEmpty( ) )
return;
if ( mStorageMode != GetMultipleFiles )
{
fileName = QDir::toNativeSeparators( QDir::cleanPath( QFileInfo( fileName ).absoluteFilePath() ) );
}
else
{
for ( int i = 0; i < fileNames.length(); i++ )
{
fileNames.replace( i, QDir::toNativeSeparators( QDir::cleanPath( QFileInfo( fileNames.at( i ) ).absoluteFilePath() ) ) );
}
}
// Store the last used path:
switch ( mStorageMode )
{
case GetFile:
case SaveFile:
settings.setValue( QStringLiteral( "UI/lastFileNameWidgetDir" ), QFileInfo( fileName ).absolutePath() );
break;
case GetDirectory:
settings.setValue( QStringLiteral( "UI/lastFileNameWidgetDir" ), fileName );
break;
case GetMultipleFiles:
settings.setValue( QStringLiteral( "UI/lastFileNameWidgetDir" ), QFileInfo( fileNames.first( ) ).absolutePath() );
break;
}
// Handle relative Path storage
if ( mStorageMode != GetMultipleFiles )
{
fileName = relativePath( fileName, true );
setFilePath( fileName );
}
else
{
for ( int i = 0; i < fileNames.length(); i++ )
{
fileNames.replace( i, relativePath( fileNames.at( i ), true ) );
}
if ( fileNames.length() > 1 )
{
setFilePath( QStringLiteral( "\"%1\"" ).arg( fileNames.join( QStringLiteral( "\" \"" ) ) ) );
}
else
{
setFilePath( fileNames.first( ) );
}
}
}
QString QgsFileWidget::relativePath( const QString &filePath, bool removeRelative ) const
{
QString RelativePath;
if ( mRelativeStorage == RelativeProject )
{
RelativePath = QDir::toNativeSeparators( QDir::cleanPath( QgsProject::instance()->fileInfo().absolutePath() ) );
}
else if ( mRelativeStorage == RelativeDefaultPath && !mDefaultRoot.isEmpty() )
{
RelativePath = QDir::toNativeSeparators( QDir::cleanPath( mDefaultRoot ) );
}
if ( !RelativePath.isEmpty() )
{
if ( removeRelative )
{
return QDir::cleanPath( QDir( RelativePath ).relativeFilePath( filePath ) );
}
else
{
return QDir::cleanPath( QDir( RelativePath ).filePath( filePath ) );
}
}
return filePath;
}
QString QgsFileWidget::toUrl( const QString &path ) const
{
QString rep;
if ( path.isEmpty() )
{
return QgsApplication::nullRepresentation();
}
QString urlStr = relativePath( path, false );
QUrl url = QUrl::fromUserInput( urlStr );
if ( !url.isValid() || !url.isLocalFile() )
{
QgsDebugMsg( QString( "URL: %1 is not valid or not a local file!" ).arg( path ) );
rep = path;
}
QString pathStr = url.toString();
if ( mFullUrl )
{
rep = QStringLiteral( "<a href=\"%1\">%2</a>" ).arg( pathStr, path );
}
else
{
QString fileName = QFileInfo( urlStr ).fileName();
rep = QStringLiteral( "<a href=\"%1\">%2</a>" ).arg( pathStr, fileName );
}
return rep;
}
///@cond PRIVATE
QgsFileDropEdit::QgsFileDropEdit( QWidget *parent )
: QgsFilterLineEdit( parent )
{
mDragActive = false;
setAcceptDrops( true );
}
void QgsFileDropEdit::setFilters( const QString &filters )
{
mAcceptableExtensions.clear();
if ( filters.contains( QStringLiteral( "*.*" ) ) )
return; // everything is allowed!
QRegularExpression rx( QStringLiteral( "\\*\\.(\\w+)" ) );
QRegularExpressionMatchIterator i = rx.globalMatch( filters );
while ( i.hasNext() )
{
QRegularExpressionMatch match = i.next();
if ( match.hasMatch() )
{
mAcceptableExtensions << match.captured( 1 ).toLower();
}
}
}
QString QgsFileDropEdit::acceptableFilePath( QDropEvent *event ) const
{
QStringList paths;
if ( event->mimeData()->hasUrls() )
{
Q_FOREACH ( const QUrl &url, event->mimeData()->urls() )
{
QFileInfo file( url.toLocalFile() );
if ( ( mStorageMode != QgsFileWidget::GetDirectory && file.isFile() &&
( mAcceptableExtensions.isEmpty() || mAcceptableExtensions.contains( file.suffix(), Qt::CaseInsensitive ) ) )
|| ( mStorageMode == QgsFileWidget::GetDirectory && file.isDir() ) )
paths.append( file.filePath() );
}
}
if ( paths.size() > 1 )
{
return QStringLiteral( "\"%1\"" ).arg( paths.join( QStringLiteral( "\" \"" ) ) );
}
else if ( paths.size() == 1 )
{
return paths.first();
}
else
{
return QString();
}
}
void QgsFileDropEdit::dragEnterEvent( QDragEnterEvent *event )
{
QString filePath = acceptableFilePath( event );
if ( !filePath.isEmpty() )
{
event->acceptProposedAction();
mDragActive = true;
update();
}
else
{
event->ignore();
}
}
void QgsFileDropEdit::dragLeaveEvent( QDragLeaveEvent *event )
{
QgsFilterLineEdit::dragLeaveEvent( event );
event->accept();
mDragActive = false;
update();
}
void QgsFileDropEdit::dropEvent( QDropEvent *event )
{
QString filePath = acceptableFilePath( event );
if ( !filePath.isEmpty() )
{
setText( filePath );
selectAll();
setFocus( Qt::MouseFocusReason );
event->acceptProposedAction();
mDragActive = false;
update();
}
}
void QgsFileDropEdit::paintEvent( QPaintEvent *e )
{
QgsFilterLineEdit::paintEvent( e );
if ( mDragActive )
{
QPainter p( this );
int width = 2; // width of highlight rectangle inside frame
p.setPen( QPen( palette().highlight(), width ) );
QRect r = rect().adjusted( width, width, -width, -width );
p.drawRect( r );
}
}
///@endcond