25#include <QRegularExpression>
40 mLayout->setContentsMargins( 0, 0, 0, 0 );
49 mLinkLabel->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Preferred );
57 mLineEdit->setToolTip( tr(
"Full path to the file(s), including name and extension" ) );
58 connect(
mLineEdit, &QLineEdit::textChanged,
this, &QgsFileWidget::textEdited );
59 connect(
mLineEdit, &QgsFileDropEdit::fileDropped,
this, &QgsFileWidget::fileDropped );
65 connect(
mLinkEditButton, &QToolButton::clicked,
this, &QgsFileWidget::editLink );
71 connect(
mFileWidgetButton, &QAbstractButton::clicked,
this, &QgsFileWidget::openFileDialog );
85#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0)
86 const QStringList pathParts = path.split( QRegExp(
"\"\\s+\"" ), QString::SkipEmptyParts );
88 const thread_local QRegularExpression partsRegex = QRegularExpression( QStringLiteral(
"\"\\s+\"" ) );
89 const QStringList pathParts = path.split( partsRegex, Qt::SkipEmptyParts );
92 const thread_local QRegularExpression cleanRe( QStringLiteral(
"(^\\s*\")|(\"\\s*)" ) );
93 paths.reserve( pathParts.size() );
94 for (
const QString &pathsPart : pathParts )
96 QString cleaned = pathsPart;
97 cleaned.remove( cleanRe );
98 paths.append( cleaned );
163 return path.contains( QStringLiteral(
"\" \"" ) );
166void QgsFileWidget::textEdited(
const QString &path )
173 mLineEdit->setToolTip( tr(
"Selected files:<br><ul><li>%1</li></ul><br>" ).arg(
splitFilePaths( path ).join( QLatin1String(
"</li><li>" ) ) ) );
182void QgsFileWidget::editLink()
191void QgsFileWidget::fileDropped(
const QString &filePath )
195 mLineEdit->setFocus( Qt::MouseFocusReason );
274void QgsFileWidget::openFileDialog()
293 QUrl url = QUrl::fromUserInput( oldPath );
294 if ( !url.isValid() )
296 QString defPath = QDir::cleanPath( QFileInfo(
QgsProject::instance()->absoluteFilePath() ).path() );
297 if ( defPath.isEmpty() )
299 defPath = QDir::homePath();
301 oldPath = settings.
value( QStringLiteral(
"UI/lastFileNameWidgetDir" ), defPath ).toString();
306 QStringList fileNames;
323 fileName = QFileDialog::getExistingDirectory(
this, title, QFileInfo( oldPath ).absoluteFilePath(),
mOptions );
330 fileName = QFileDialog::getSaveFileName(
this, title, QFileInfo( oldPath ).absoluteFilePath(),
mFilter, &
mSelectedFilter,
mOptions | QFileDialog::DontConfirmOverwrite );
349 if ( fileName.isEmpty() && fileNames.isEmpty( ) )
353 fileNames << fileName;
355 for (
int i = 0; i < fileNames.length(); i++ )
357 fileNames.replace( i, QDir::toNativeSeparators( QDir::cleanPath( QFileInfo( fileNames.at( i ) ).absoluteFilePath() ) ) );
366 settings.
setValue( QStringLiteral(
"UI/lastFileNameWidgetDir" ), QFileInfo( fileNames.first() ).absolutePath() );
369 settings.
setValue( QStringLiteral(
"UI/lastFileNameWidgetDir" ), fileNames.first() );
378 Q_ASSERT( fileNames.count() );
381 for (
int i = 0; i < fileNames.length(); i++ )
383 fileNames.replace( i,
relativePath( fileNames.at( i ),
true ) );
397 if ( filePaths.length() > 1 )
399 setFilePath( QStringLiteral(
"\"%1\"" ).arg( filePaths.join( QLatin1String(
"\" \"" ) ) ) );
410 QString RelativePath;
413 RelativePath = QDir::toNativeSeparators( QDir::cleanPath( QFileInfo(
QgsProject::instance()->absoluteFilePath() ).path() ) );
417 RelativePath = QDir::toNativeSeparators( QDir::cleanPath(
mDefaultRoot ) );
420 if ( !RelativePath.isEmpty() )
422 if ( removeRelative )
424 return QDir::cleanPath( QDir( RelativePath ).relativeFilePath(
filePath ) );
439 if ( path.isEmpty() )
446 return QStringLiteral(
"<a>%1</a>" ).arg( path );
450 QUrl url = QUrl::fromUserInput( urlStr );
451 if ( !url.isValid() || !url.isLocalFile() )
453 QgsDebugMsgLevel( QStringLiteral(
"URL: %1 is not valid or not a local file!" ).arg( path ), 2 );
457 QString pathStr = url.toString();
460 rep = QStringLiteral(
"<a href=\"%1\">%2</a>" ).arg( pathStr, path );
464 QString fileName = QFileInfo( urlStr ).fileName();
465 rep = QStringLiteral(
"<a href=\"%1\">%2</a>" ).arg( pathStr, fileName );
475QgsFileDropEdit::QgsFileDropEdit( QWidget *parent )
478 setAcceptDrops(
true );
481void QgsFileDropEdit::setFilters(
const QString &filters )
483 mAcceptableExtensions.clear();
485 if ( filters.contains( QStringLiteral(
"*.*" ) ) )
488 QRegularExpression rx( QStringLiteral(
"\\*\\.(\\w+)" ) );
489 QRegularExpressionMatchIterator i = rx.globalMatch( filters );
490 while ( i.hasNext() )
492 QRegularExpressionMatch match = i.next();
493 if ( match.hasMatch() )
495 mAcceptableExtensions << match.captured( 1 ).toLower();
500QStringList QgsFileDropEdit::acceptableFilePaths( QDropEvent *event )
const
502 QStringList rawPaths;
504 if ( event->mimeData()->hasUrls() )
506 const QList< QUrl > urls =
event->mimeData()->urls();
507 rawPaths.reserve( urls.count() );
508 for (
const QUrl &url : urls )
510 const QString local = url.toLocalFile();
511 if ( !rawPaths.contains( local ) )
512 rawPaths.append( local );
519 if ( !rawPaths.contains( u.uri ) )
520 rawPaths.append( u.uri );
523 if ( !event->mimeData()->text().isEmpty() && !rawPaths.contains( event->mimeData()->text() ) )
524 rawPaths.append( event->mimeData()->text() );
526 paths.reserve( rawPaths.count() );
527 for (
const QString &path : std::as_const( rawPaths ) )
529 QFileInfo file( path );
530 switch ( mStorageMode )
536 if ( file.isFile() && ( mAcceptableExtensions.isEmpty() || mAcceptableExtensions.contains( file.suffix(), Qt::CaseInsensitive ) ) )
537 paths.append( file.filePath() );
545 paths.append( file.filePath() );
546 else if ( file.isFile() )
549 paths.append( file.absolutePath() );
560QString QgsFileDropEdit::acceptableFilePath( QDropEvent *event )
const
562 const QStringList paths = acceptableFilePaths( event );
563 if ( paths.size() > 1 )
565 return QStringLiteral(
"\"%1\"" ).arg( paths.join( QLatin1String(
"\" \"" ) ) );
567 else if ( paths.size() == 1 )
569 return paths.first();
577void QgsFileDropEdit::dragEnterEvent( QDragEnterEvent *event )
579 QString filePath = acceptableFilePath( event );
580 if ( !filePath.isEmpty() )
582 event->acceptProposedAction();
583 setHighlighted(
true );
591void QgsFileDropEdit::dragLeaveEvent( QDragLeaveEvent *event )
593 QgsFilterLineEdit::dragLeaveEvent( event );
595 setHighlighted(
false );
598void QgsFileDropEdit::dropEvent( QDropEvent *event )
600 QString filePath = acceptableFilePath( event );
601 if ( !filePath.isEmpty() )
603 event->acceptProposedAction();
604 emit fileDropped( filePath );
607 setHighlighted(
false );
static QString nullRepresentation()
This string is used to represent the value NULL throughout QGIS.
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
static QString addExtensionFromFilter(const QString &fileName, const QString &filter)
Ensures that a fileName ends with an extension from the specified filter string.
QLineEdit subclass with built in support for clearing the widget's value and handling custom null val...
Trick to keep a widget focused and avoid QT crashes.
A QgsFilterLineEdit subclass with the ability to "highlight" the edges of the widget.
QList< QgsMimeDataUtils::Uri > UriList
static UriList decodeUriList(const QMimeData *data)
static QgsProject * instance()
Returns the QgsProject singleton instance.
This class is a composition of two QSettings instances:
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
void setValue(const QString &key, const QVariant &value, QgsSettings::Section section=QgsSettings::NoSection)
Sets the value of setting key to value.
#define QgsDebugMsgLevel(str, level)