18#include "moc_qgsfilewidget.cpp"
26#include <QRegularExpression>
41 mLayout->setContentsMargins( 0, 0, 0, 0 );
50 mLinkLabel->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Preferred );
58 mLineEdit->setToolTip( tr(
"Full path to the file(s), including name and extension" ) );
59 connect(
mLineEdit, &QLineEdit::textChanged,
this, &QgsFileWidget::textEdited );
60 connect(
mLineEdit, &QgsFileDropEdit::fileDropped,
this, &QgsFileWidget::fileDropped );
66 connect(
mLinkEditButton, &QToolButton::clicked,
this, &QgsFileWidget::editLink );
72 connect(
mFileWidgetButton, &QAbstractButton::clicked,
this, &QgsFileWidget::openFileDialog );
86 const thread_local QRegularExpression partsRegex = QRegularExpression( QStringLiteral(
"\"\\s+\"" ) );
87 const QStringList pathParts = path.split( partsRegex, Qt::SkipEmptyParts );
89 const thread_local QRegularExpression cleanRe( QStringLiteral(
"(^\\s*\")|(\"\\s*)" ) );
90 paths.reserve( pathParts.size() );
91 for (
const QString &pathsPart : pathParts )
93 QString cleaned = pathsPart;
94 cleaned.remove( cleanRe );
95 paths.append( cleaned );
160 return path.contains( QStringLiteral(
"\" \"" ) );
163void QgsFileWidget::textEdited(
const QString &path )
170 mLineEdit->setToolTip( tr(
"Selected files:<br><ul><li>%1</li></ul><br>" ).arg(
splitFilePaths( path ).join( QLatin1String(
"</li><li>" ) ) ) );
179void QgsFileWidget::editLink()
188void QgsFileWidget::fileDropped(
const QString &filePath )
192 mLineEdit->setFocus( Qt::MouseFocusReason );
271void QgsFileWidget::openFileDialog()
290 QUrl url = QUrl::fromUserInput( oldPath );
291 if ( !url.isValid() )
293 QString defPath = QDir::cleanPath( QFileInfo(
QgsProject::instance()->absoluteFilePath() ).path() );
294 if ( defPath.isEmpty() )
296 defPath = QDir::homePath();
298 oldPath = settings.
value( QStringLiteral(
"UI/lastFileNameWidgetDir" ), defPath ).toString();
303 QStringList fileNames;
320 fileName = QFileDialog::getExistingDirectory(
this, title, QFileInfo( oldPath ).absoluteFilePath(),
mOptions );
327 fileName = QFileDialog::getSaveFileName(
this, title, QFileInfo( oldPath ).absoluteFilePath(),
mFilter, &
mSelectedFilter,
mOptions | QFileDialog::DontConfirmOverwrite );
343 if (
mFilter.contains( QLatin1String(
"(*.gdb *.GDB gdb)" ) ) &&
344 ( fileName.endsWith( QLatin1String(
"/gdb.gdb" ) ) ||
345 fileName.endsWith( QLatin1String(
"\\gdb.gdb" ) ) ) )
347 fileName.chop(
static_cast<int>( strlen(
"/gdb.gdb" ) ) );
358 if ( fileName.isEmpty() && fileNames.isEmpty( ) )
362 fileNames << fileName;
364 for (
int i = 0; i < fileNames.length(); i++ )
366 fileNames.replace( i, QDir::toNativeSeparators( QDir::cleanPath( QFileInfo( fileNames.at( i ) ).absoluteFilePath() ) ) );
375 settings.
setValue( QStringLiteral(
"UI/lastFileNameWidgetDir" ), QFileInfo( fileNames.first() ).absolutePath() );
378 settings.
setValue( QStringLiteral(
"UI/lastFileNameWidgetDir" ), fileNames.first() );
387 Q_ASSERT( fileNames.count() );
390 for (
int i = 0; i < fileNames.length(); i++ )
392 fileNames.replace( i,
relativePath( fileNames.at( i ),
true ) );
406 if ( filePaths.length() > 1 )
408 setFilePath( QStringLiteral(
"\"%1\"" ).arg( filePaths.join( QLatin1String(
"\" \"" ) ) ) );
419 QString RelativePath;
422 RelativePath = QDir::toNativeSeparators( QDir::cleanPath( QFileInfo(
QgsProject::instance()->absoluteFilePath() ).path() ) );
426 RelativePath = QDir::toNativeSeparators( QDir::cleanPath(
mDefaultRoot ) );
429 if ( !RelativePath.isEmpty() )
431 if ( removeRelative )
433 return QDir::cleanPath( QDir( RelativePath ).relativeFilePath(
filePath ) );
446 QSize size {
mLineEdit->minimumSizeHint() };
448 size.setWidth( size.width() + btnSize.width() );
449 size.setHeight( std::max( size.height(), btnSize.height() ) );
464 return QStringLiteral(
"<a>%1</a>" ).arg( path );
468 QUrl url = QUrl::fromUserInput( urlStr );
469 if ( !url.isValid() || !url.isLocalFile() )
471 QgsDebugMsgLevel( QStringLiteral(
"URL: %1 is not valid or not a local file!" ).arg( path ), 2 );
475 QString pathStr = url.toString();
478 rep = QStringLiteral(
"<a href=\"%1\">%2</a>" ).arg( pathStr, path );
482 QString fileName = QFileInfo( urlStr ).fileName();
483 rep = QStringLiteral(
"<a href=\"%1\">%2</a>" ).arg( pathStr, fileName );
493QgsFileDropEdit::QgsFileDropEdit( QWidget *parent )
496 setAcceptDrops(
true );
499void QgsFileDropEdit::setFilters(
const QString &filters )
501 mAcceptableExtensions.clear();
503 if ( filters.contains( QStringLiteral(
"*.*" ) ) )
506 const thread_local QRegularExpression rx( QStringLiteral(
"\\*\\.(\\w+)" ) );
507 QRegularExpressionMatchIterator i = rx.globalMatch( filters );
508 while ( i.hasNext() )
510 QRegularExpressionMatch match = i.next();
511 if ( match.hasMatch() )
513 mAcceptableExtensions << match.captured( 1 ).toLower();
518QStringList QgsFileDropEdit::acceptableFilePaths( QDropEvent *event )
const
520 QStringList rawPaths;
522 if ( event->mimeData()->hasUrls() )
524 const QList< QUrl > urls =
event->mimeData()->urls();
525 rawPaths.reserve( urls.count() );
526 for (
const QUrl &url : urls )
528 const QString local = url.toLocalFile();
529 if ( !rawPaths.contains( local ) )
530 rawPaths.append( local );
537 if ( !rawPaths.contains( u.uri ) )
538 rawPaths.append( u.uri );
541 if ( !event->mimeData()->text().isEmpty() && !rawPaths.contains( event->mimeData()->text() ) )
542 rawPaths.append( event->mimeData()->text() );
544 paths.reserve( rawPaths.count() );
545 for (
const QString &path : std::as_const( rawPaths ) )
547 QFileInfo file( path );
548 switch ( mStorageMode )
554 if ( file.isFile() && ( mAcceptableExtensions.isEmpty() || mAcceptableExtensions.contains( file.suffix(), Qt::CaseInsensitive ) ) )
555 paths.append( file.filePath() );
563 paths.append( file.filePath() );
564 else if ( file.isFile() )
567 paths.append( file.absolutePath() );
578QString QgsFileDropEdit::acceptableFilePath( QDropEvent *event )
const
580 const QStringList paths = acceptableFilePaths( event );
581 if ( paths.size() > 1 )
583 return QStringLiteral(
"\"%1\"" ).arg( paths.join( QLatin1String(
"\" \"" ) ) );
585 else if ( paths.size() == 1 )
587 return paths.first();
595void QgsFileDropEdit::dragEnterEvent( QDragEnterEvent *event )
597 QString filePath = acceptableFilePath( event );
598 if ( !filePath.isEmpty() )
600 event->acceptProposedAction();
601 setHighlighted(
true );
609void QgsFileDropEdit::dragLeaveEvent( QDragLeaveEvent *event )
611 QgsFilterLineEdit::dragLeaveEvent( event );
613 setHighlighted(
false );
616void QgsFileDropEdit::dropEvent( QDropEvent *event )
618 QString filePath = acceptableFilePath( event );
619 if ( !filePath.isEmpty() )
621 event->acceptProposedAction();
622 emit fileDropped( filePath );
625 setHighlighted(
false );
static QString nullRepresentation()
Returns the string 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)