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 );
346 if (
mFilter.contains( QLatin1String(
"(*.gdb *.GDB gdb)" ) ) &&
347 ( fileName.endsWith( QLatin1String(
"/gdb.gdb" ) ) ||
348 fileName.endsWith( QLatin1String(
"\\gdb.gdb" ) ) ) )
350 fileName.chop(
static_cast<int>( strlen(
"/gdb.gdb" ) ) );
361 if ( fileName.isEmpty() && fileNames.isEmpty( ) )
365 fileNames << fileName;
367 for (
int i = 0; i < fileNames.length(); i++ )
369 fileNames.replace( i, QDir::toNativeSeparators( QDir::cleanPath( QFileInfo( fileNames.at( i ) ).absoluteFilePath() ) ) );
378 settings.
setValue( QStringLiteral(
"UI/lastFileNameWidgetDir" ), QFileInfo( fileNames.first() ).absolutePath() );
381 settings.
setValue( QStringLiteral(
"UI/lastFileNameWidgetDir" ), fileNames.first() );
390 Q_ASSERT( fileNames.count() );
393 for (
int i = 0; i < fileNames.length(); i++ )
395 fileNames.replace( i,
relativePath( fileNames.at( i ),
true ) );
409 if ( filePaths.length() > 1 )
411 setFilePath( QStringLiteral(
"\"%1\"" ).arg( filePaths.join( QLatin1String(
"\" \"" ) ) ) );
422 QString RelativePath;
425 RelativePath = QDir::toNativeSeparators( QDir::cleanPath( QFileInfo(
QgsProject::instance()->absoluteFilePath() ).path() ) );
429 RelativePath = QDir::toNativeSeparators( QDir::cleanPath(
mDefaultRoot ) );
432 if ( !RelativePath.isEmpty() )
434 if ( removeRelative )
436 return QDir::cleanPath( QDir( RelativePath ).relativeFilePath(
filePath ) );
449 QSize size {
mLineEdit->minimumSizeHint() };
451 size.setWidth( size.width() + btnSize.width() );
452 size.setHeight( std::max( size.height(), btnSize.height() ) );
467 return QStringLiteral(
"<a>%1</a>" ).arg( path );
471 QUrl url = QUrl::fromUserInput( urlStr );
472 if ( !url.isValid() || !url.isLocalFile() )
474 QgsDebugMsgLevel( QStringLiteral(
"URL: %1 is not valid or not a local file!" ).arg( path ), 2 );
478 QString pathStr = url.toString();
481 rep = QStringLiteral(
"<a href=\"%1\">%2</a>" ).arg( pathStr, path );
485 QString fileName = QFileInfo( urlStr ).fileName();
486 rep = QStringLiteral(
"<a href=\"%1\">%2</a>" ).arg( pathStr, fileName );
496QgsFileDropEdit::QgsFileDropEdit( QWidget *parent )
499 setAcceptDrops(
true );
502void QgsFileDropEdit::setFilters(
const QString &filters )
504 mAcceptableExtensions.clear();
506 if ( filters.contains( QStringLiteral(
"*.*" ) ) )
509 const thread_local QRegularExpression rx( QStringLiteral(
"\\*\\.(\\w+)" ) );
510 QRegularExpressionMatchIterator i = rx.globalMatch( filters );
511 while ( i.hasNext() )
513 QRegularExpressionMatch match = i.next();
514 if ( match.hasMatch() )
516 mAcceptableExtensions << match.captured( 1 ).toLower();
521QStringList QgsFileDropEdit::acceptableFilePaths( QDropEvent *event )
const
523 QStringList rawPaths;
525 if ( event->mimeData()->hasUrls() )
527 const QList< QUrl > urls =
event->mimeData()->urls();
528 rawPaths.reserve( urls.count() );
529 for (
const QUrl &url : urls )
531 const QString local = url.toLocalFile();
532 if ( !rawPaths.contains( local ) )
533 rawPaths.append( local );
540 if ( !rawPaths.contains( u.uri ) )
541 rawPaths.append( u.uri );
544 if ( !event->mimeData()->text().isEmpty() && !rawPaths.contains( event->mimeData()->text() ) )
545 rawPaths.append( event->mimeData()->text() );
547 paths.reserve( rawPaths.count() );
548 for (
const QString &path : std::as_const( rawPaths ) )
550 QFileInfo file( path );
551 switch ( mStorageMode )
557 if ( file.isFile() && ( mAcceptableExtensions.isEmpty() || mAcceptableExtensions.contains( file.suffix(), Qt::CaseInsensitive ) ) )
558 paths.append( file.filePath() );
566 paths.append( file.filePath() );
567 else if ( file.isFile() )
570 paths.append( file.absolutePath() );
581QString QgsFileDropEdit::acceptableFilePath( QDropEvent *event )
const
583 const QStringList paths = acceptableFilePaths( event );
584 if ( paths.size() > 1 )
586 return QStringLiteral(
"\"%1\"" ).arg( paths.join( QLatin1String(
"\" \"" ) ) );
588 else if ( paths.size() == 1 )
590 return paths.first();
598void QgsFileDropEdit::dragEnterEvent( QDragEnterEvent *event )
600 QString filePath = acceptableFilePath( event );
601 if ( !filePath.isEmpty() )
603 event->acceptProposedAction();
604 setHighlighted(
true );
612void QgsFileDropEdit::dragLeaveEvent( QDragLeaveEvent *event )
614 QgsFilterLineEdit::dragLeaveEvent( event );
616 setHighlighted(
false );
619void QgsFileDropEdit::dropEvent( QDropEvent *event )
621 QString filePath = acceptableFilePath( event );
622 if ( !filePath.isEmpty() )
624 event->acceptProposedAction();
625 emit fileDropped( filePath );
628 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)