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 const thread_local QRegularExpression partsRegex = QRegularExpression( QStringLiteral(
"\"\\s+\"" ) );
86 const QStringList pathParts = path.split( partsRegex, Qt::SkipEmptyParts );
88 const thread_local QRegularExpression cleanRe( QStringLiteral(
"(^\\s*\")|(\"\\s*)" ) );
89 paths.reserve( pathParts.size() );
90 for (
const QString &pathsPart : pathParts )
92 QString cleaned = pathsPart;
93 cleaned.remove( cleanRe );
94 paths.append( cleaned );
159 return path.contains( QStringLiteral(
"\" \"" ) );
162void QgsFileWidget::textEdited(
const QString &path )
169 mLineEdit->setToolTip( tr(
"Selected files:<br><ul><li>%1</li></ul><br>" ).arg(
splitFilePaths( path ).join( QLatin1String(
"</li><li>" ) ) ) );
178void QgsFileWidget::editLink()
187void QgsFileWidget::fileDropped(
const QString &filePath )
191 mLineEdit->setFocus( Qt::MouseFocusReason );
270void QgsFileWidget::openFileDialog()
289 QUrl url = QUrl::fromUserInput( oldPath );
290 if ( !url.isValid() )
292 QString defPath = QDir::cleanPath( QFileInfo(
QgsProject::instance()->absoluteFilePath() ).path() );
293 if ( defPath.isEmpty() )
295 defPath = QDir::homePath();
297 oldPath = settings.
value( QStringLiteral(
"UI/lastFileNameWidgetDir" ), defPath ).toString();
302 QStringList fileNames;
319 fileName = QFileDialog::getExistingDirectory(
this, title, QFileInfo( oldPath ).absoluteFilePath(),
mOptions );
326 fileName = QFileDialog::getSaveFileName(
this, title, QFileInfo( oldPath ).absoluteFilePath(),
mFilter, &
mSelectedFilter,
mOptions | QFileDialog::DontConfirmOverwrite );
342 if (
mFilter.contains( QLatin1String(
"(*.gdb *.GDB gdb)" ) ) &&
343 ( fileName.endsWith( QLatin1String(
"/gdb.gdb" ) ) ||
344 fileName.endsWith( QLatin1String(
"\\gdb.gdb" ) ) ) )
346 fileName.chop(
static_cast<int>( strlen(
"/gdb.gdb" ) ) );
357 if ( fileName.isEmpty() && fileNames.isEmpty( ) )
361 fileNames << fileName;
363 for (
int i = 0; i < fileNames.length(); i++ )
365 fileNames.replace( i, QDir::toNativeSeparators( QDir::cleanPath( QFileInfo( fileNames.at( i ) ).absoluteFilePath() ) ) );
374 settings.
setValue( QStringLiteral(
"UI/lastFileNameWidgetDir" ), QFileInfo( fileNames.first() ).absolutePath() );
377 settings.
setValue( QStringLiteral(
"UI/lastFileNameWidgetDir" ), fileNames.first() );
386 Q_ASSERT( fileNames.count() );
389 for (
int i = 0; i < fileNames.length(); i++ )
391 fileNames.replace( i,
relativePath( fileNames.at( i ),
true ) );
405 if ( filePaths.length() > 1 )
407 setFilePath( QStringLiteral(
"\"%1\"" ).arg( filePaths.join( QLatin1String(
"\" \"" ) ) ) );
418 QString RelativePath;
421 RelativePath = QDir::toNativeSeparators( QDir::cleanPath( QFileInfo(
QgsProject::instance()->absoluteFilePath() ).path() ) );
425 RelativePath = QDir::toNativeSeparators( QDir::cleanPath(
mDefaultRoot ) );
428 if ( !RelativePath.isEmpty() )
430 if ( removeRelative )
432 return QDir::cleanPath( QDir( RelativePath ).relativeFilePath(
filePath ) );
445 QSize size {
mLineEdit->minimumSizeHint() };
447 size.setWidth( size.width() + btnSize.width() );
448 size.setHeight( std::max( size.height(), btnSize.height() ) );
463 return QStringLiteral(
"<a>%1</a>" ).arg( path );
467 QUrl url = QUrl::fromUserInput( urlStr );
468 if ( !url.isValid() || !url.isLocalFile() )
470 QgsDebugMsgLevel( QStringLiteral(
"URL: %1 is not valid or not a local file!" ).arg( path ), 2 );
474 QString pathStr = url.toString();
477 rep = QStringLiteral(
"<a href=\"%1\">%2</a>" ).arg( pathStr, path );
481 QString fileName = QFileInfo( urlStr ).fileName();
482 rep = QStringLiteral(
"<a href=\"%1\">%2</a>" ).arg( pathStr, fileName );
492QgsFileDropEdit::QgsFileDropEdit( QWidget *parent )
495 setAcceptDrops(
true );
498void QgsFileDropEdit::setFilters(
const QString &filters )
500 mAcceptableExtensions.clear();
502 if ( filters.contains( QStringLiteral(
"*.*" ) ) )
505 const thread_local QRegularExpression rx( QStringLiteral(
"\\*\\.(\\w+)" ) );
506 QRegularExpressionMatchIterator i = rx.globalMatch( filters );
507 while ( i.hasNext() )
509 QRegularExpressionMatch match = i.next();
510 if ( match.hasMatch() )
512 mAcceptableExtensions << match.captured( 1 ).toLower();
517QStringList QgsFileDropEdit::acceptableFilePaths( QDropEvent *event )
const
519 QStringList rawPaths;
521 if ( event->mimeData()->hasUrls() )
523 const QList< QUrl > urls =
event->mimeData()->urls();
524 rawPaths.reserve( urls.count() );
525 for (
const QUrl &url : urls )
527 const QString local = url.toLocalFile();
528 if ( !rawPaths.contains( local ) )
529 rawPaths.append( local );
536 if ( !rawPaths.contains( u.uri ) )
537 rawPaths.append( u.uri );
540 if ( !event->mimeData()->text().isEmpty() && !rawPaths.contains( event->mimeData()->text() ) )
541 rawPaths.append( event->mimeData()->text() );
543 paths.reserve( rawPaths.count() );
544 for (
const QString &path : std::as_const( rawPaths ) )
546 QFileInfo file( path );
547 switch ( mStorageMode )
553 if ( file.isFile() && ( mAcceptableExtensions.isEmpty() || mAcceptableExtensions.contains( file.suffix(), Qt::CaseInsensitive ) ) )
554 paths.append( file.filePath() );
562 paths.append( file.filePath() );
563 else if ( file.isFile() )
566 paths.append( file.absolutePath() );
577QString QgsFileDropEdit::acceptableFilePath( QDropEvent *event )
const
579 const QStringList paths = acceptableFilePaths( event );
580 if ( paths.size() > 1 )
582 return QStringLiteral(
"\"%1\"" ).arg( paths.join( QLatin1String(
"\" \"" ) ) );
584 else if ( paths.size() == 1 )
586 return paths.first();
594void QgsFileDropEdit::dragEnterEvent( QDragEnterEvent *event )
596 QString filePath = acceptableFilePath( event );
597 if ( !filePath.isEmpty() )
599 event->acceptProposedAction();
600 setHighlighted(
true );
608void QgsFileDropEdit::dragLeaveEvent( QDragLeaveEvent *event )
610 QgsFilterLineEdit::dragLeaveEvent( event );
612 setHighlighted(
false );
615void QgsFileDropEdit::dropEvent( QDropEvent *event )
617 QString filePath = acceptableFilePath( event );
618 if ( !filePath.isEmpty() )
620 event->acceptProposedAction();
621 emit fileDropped( filePath );
624 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)