20 #include <QToolButton>
22 #include <QGridLayout>
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(
"\" \"" ) );
166 void 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>" ) ) ) );
182 void QgsFileWidget::editLink()
191 void QgsFileWidget::fileDropped(
const QString &filePath )
195 mLineEdit->setFocus( Qt::MouseFocusReason );
274 void 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 | QFileDialog::ShowDirsOnly );
330 fileName = QFileDialog::getSaveFileName(
this, title, QFileInfo( oldPath ).absoluteFilePath(),
mFilter, &
mSelectedFilter,
mOptions | QFileDialog::DontConfirmOverwrite );
345 if ( fileName.isEmpty() && fileNames.isEmpty( ) )
349 fileNames << fileName;
351 for (
int i = 0; i < fileNames.length(); i++ )
353 fileNames.replace( i, QDir::toNativeSeparators( QDir::cleanPath( QFileInfo( fileNames.at( i ) ).absoluteFilePath() ) ) );
362 settings.
setValue( QStringLiteral(
"UI/lastFileNameWidgetDir" ), QFileInfo( fileNames.first() ).absolutePath() );
365 settings.
setValue( QStringLiteral(
"UI/lastFileNameWidgetDir" ), fileNames.first() );
374 Q_ASSERT( fileNames.count() );
377 for (
int i = 0; i < fileNames.length(); i++ )
379 fileNames.replace( i,
relativePath( fileNames.at( i ),
true ) );
393 if ( filePaths.length() > 1 )
395 setFilePath( QStringLiteral(
"\"%1\"" ).arg( filePaths.join( QLatin1String(
"\" \"" ) ) ) );
406 QString RelativePath;
409 RelativePath = QDir::toNativeSeparators( QDir::cleanPath( QFileInfo(
QgsProject::instance()->absoluteFilePath() ).path() ) );
413 RelativePath = QDir::toNativeSeparators( QDir::cleanPath(
mDefaultRoot ) );
416 if ( !RelativePath.isEmpty() )
418 if ( removeRelative )
420 return QDir::cleanPath( QDir( RelativePath ).relativeFilePath(
filePath ) );
435 if ( path.isEmpty() )
442 return QStringLiteral(
"<a>%1</a>" ).arg( path );
446 QUrl url = QUrl::fromUserInput( urlStr );
447 if ( !url.isValid() || !url.isLocalFile() )
449 QgsDebugMsgLevel( QStringLiteral(
"URL: %1 is not valid or not a local file!" ).arg( path ), 2 );
453 QString pathStr = url.toString();
456 rep = QStringLiteral(
"<a href=\"%1\">%2</a>" ).arg( pathStr, path );
460 QString fileName = QFileInfo( urlStr ).fileName();
461 rep = QStringLiteral(
"<a href=\"%1\">%2</a>" ).arg( pathStr, fileName );
471 QgsFileDropEdit::QgsFileDropEdit( QWidget *parent )
474 setAcceptDrops(
true );
477 void QgsFileDropEdit::setFilters(
const QString &filters )
479 mAcceptableExtensions.clear();
481 if ( filters.contains( QStringLiteral(
"*.*" ) ) )
484 QRegularExpression rx( QStringLiteral(
"\\*\\.(\\w+)" ) );
485 QRegularExpressionMatchIterator i = rx.globalMatch( filters );
486 while ( i.hasNext() )
488 QRegularExpressionMatch match = i.next();
489 if ( match.hasMatch() )
491 mAcceptableExtensions << match.captured( 1 ).toLower();
496 QStringList QgsFileDropEdit::acceptableFilePaths( QDropEvent *event )
const
498 QStringList rawPaths;
500 if ( event->mimeData()->hasUrls() )
502 const QList< QUrl > urls =
event->mimeData()->urls();
503 rawPaths.reserve( urls.count() );
504 for (
const QUrl &url : urls )
506 const QString local = url.toLocalFile();
507 if ( !rawPaths.contains( local ) )
508 rawPaths.append( local );
515 if ( !rawPaths.contains( u.uri ) )
516 rawPaths.append( u.uri );
519 if ( !event->mimeData()->text().isEmpty() && !rawPaths.contains( event->mimeData()->text() ) )
520 rawPaths.append( event->mimeData()->text() );
522 paths.reserve( rawPaths.count() );
523 for (
const QString &path : std::as_const( rawPaths ) )
525 QFileInfo file( path );
526 switch ( mStorageMode )
532 if ( file.isFile() && ( mAcceptableExtensions.isEmpty() || mAcceptableExtensions.contains( file.suffix(), Qt::CaseInsensitive ) ) )
533 paths.append( file.filePath() );
541 paths.append( file.filePath() );
542 else if ( file.isFile() )
545 paths.append( file.absolutePath() );
556 QString QgsFileDropEdit::acceptableFilePath( QDropEvent *event )
const
558 const QStringList paths = acceptableFilePaths( event );
559 if ( paths.size() > 1 )
561 return QStringLiteral(
"\"%1\"" ).arg( paths.join( QLatin1String(
"\" \"" ) ) );
563 else if ( paths.size() == 1 )
565 return paths.first();
573 void QgsFileDropEdit::dragEnterEvent( QDragEnterEvent *event )
575 QString filePath = acceptableFilePath( event );
576 if ( !filePath.isEmpty() )
578 event->acceptProposedAction();
579 setHighlighted(
true );
587 void QgsFileDropEdit::dragLeaveEvent( QDragLeaveEvent *event )
589 QgsFilterLineEdit::dragLeaveEvent( event );
591 setHighlighted(
false );
594 void QgsFileDropEdit::dropEvent( QDropEvent *event )
596 QString filePath = acceptableFilePath( event );
597 if ( !filePath.isEmpty() )
599 event->acceptProposedAction();
600 emit fileDropped( filePath );
603 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)