20 #include <QToolButton>
22 #include <QGridLayout>
25 #include <QRegularExpression>
39 setBackgroundRole( QPalette::Window );
40 setAutoFillBackground(
true );
42 mLayout =
new QHBoxLayout();
43 mLayout->setContentsMargins( 0, 0, 0, 0 );
46 mLinkLabel =
new QLabel(
this );
48 mLinkLabel->setOpenExternalLinks(
true );
51 mLinkLabel->setEnabled(
true );
52 mLinkLabel->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Preferred );
53 mLinkLabel->setTextFormat( Qt::RichText );
55 mLinkEditButton =
new QToolButton(
this );
57 connect( mLinkEditButton, &QToolButton::clicked,
this, &QgsFileWidget::editLink );
58 mLinkEditButton->hide();
61 mLineEdit =
new QgsFileDropEdit(
this );
62 mLineEdit->setDragEnabled(
true );
63 mLineEdit->setToolTip( tr(
"Full path to the file(s), including name and extension" ) );
64 connect( mLineEdit, &QLineEdit::textChanged,
this, &QgsFileWidget::textEdited );
65 mLayout->addWidget( mLineEdit );
67 mFileWidgetButton =
new QToolButton(
this );
68 mFileWidgetButton->setText( QChar( 0x2026 ) );
69 mFileWidgetButton->setToolTip( tr(
"Browse" ) );
70 connect( mFileWidgetButton, &QAbstractButton::clicked,
this, &QgsFileWidget::openFileDialog );
71 mLayout->addWidget( mFileWidgetButton );
84 const QStringList pathParts = path.split( QRegExp(
"\"\\s+\"" ), QString::SkipEmptyParts );
85 for (
const auto &pathsPart : pathParts )
87 QString cleaned = pathsPart;
88 cleaned.remove( QRegExp(
"(^\\s*\")|(\"\\s*)" ) );
89 paths.append( cleaned );
97 mLineEdit->setValue( path );
102 if ( mReadOnly == readOnly )
105 mReadOnly = readOnly;
117 mDialogTitle = title;
128 mLineEdit->setFilters( filters );
143 return mButtonVisible;
148 mButtonVisible = visible;
149 mFileWidgetButton->setVisible( visible );
152 void QgsFileWidget::textEdited(
const QString &path )
155 mLinkLabel->setText( toUrl( path ) );
157 if ( path.contains( QStringLiteral(
"\" \"" ) ) )
159 mLineEdit->setToolTip( tr(
"Selected files:<br><ul><li>%1</li></ul><br>" ).arg(
splitFilePaths( path ).join( QLatin1String(
"</li><li>" ) ) ) );
163 mLineEdit->setToolTip( QString() );
168 void QgsFileWidget::editLink()
170 if ( !mUseLink || mReadOnly )
173 mIsLinkEdited = !mIsLinkEdited;
224 return mRelativeStorage;
237 void QgsFileWidget::updateLayout()
239 mLayout->removeWidget( mLineEdit );
240 mLayout->removeWidget( mLinkLabel );
241 mLayout->removeWidget( mLinkEditButton );
243 mLinkEditButton->setVisible( mUseLink && !mReadOnly );
245 mFileWidgetButton->setEnabled( !mReadOnly );
246 mLineEdit->setEnabled( !mReadOnly );
248 if ( mUseLink && !mIsLinkEdited )
250 mLayout->insertWidget( 0, mLinkLabel );
251 mLineEdit->setVisible(
false );
252 mLinkLabel->setVisible(
true );
256 mLayout->insertWidget( 1, mLinkEditButton );
262 mLayout->insertWidget( 0, mLineEdit );
263 mLineEdit->setVisible(
true );
264 mLinkLabel->setVisible(
false );
268 mLayout->insertWidget( 1, mLinkEditButton );
274 void QgsFileWidget::openFileDialog()
281 if ( !mFilePath.isEmpty() )
283 oldPath = relativePath( mFilePath,
false );
287 else if ( !mDefaultRoot.isEmpty() )
289 oldPath = QDir::cleanPath( mDefaultRoot );
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;
311 switch ( mStorageMode )
314 title = !mDialogTitle.isEmpty() ? mDialogTitle : tr(
"Select a file" );
315 fileName = QFileDialog::getOpenFileName(
this, title, QFileInfo( oldPath ).absoluteFilePath(), mFilter, &mSelectedFilter, mOptions );
318 title = !mDialogTitle.isEmpty() ? mDialogTitle : tr(
"Select one or more files" );
319 fileNames = QFileDialog::getOpenFileNames(
this, title, QFileInfo( oldPath ).absoluteFilePath(), mFilter, &mSelectedFilter, mOptions );
322 title = !mDialogTitle.isEmpty() ? mDialogTitle : tr(
"Select a directory" );
323 fileName = QFileDialog::getExistingDirectory(
this, title, QFileInfo( oldPath ).absoluteFilePath(), mOptions | QFileDialog::ShowDirsOnly );
327 title = !mDialogTitle.isEmpty() ? mDialogTitle : tr(
"Create or select a file" );
330 fileName = QFileDialog::getSaveFileName(
this, title, QFileInfo( oldPath ).absoluteFilePath(), mFilter, &mSelectedFilter, mOptions | QFileDialog::DontConfirmOverwrite );
334 fileName = QFileDialog::getSaveFileName(
this, title, QFileInfo( oldPath ).absoluteFilePath(), mFilter, &mSelectedFilter, mOptions );
345 if ( fileName.isEmpty() && fileNames.isEmpty( ) )
350 fileName = QDir::toNativeSeparators( QDir::cleanPath( QFileInfo( fileName ).absoluteFilePath() ) );
354 for (
int i = 0; i < fileNames.length(); i++ )
356 fileNames.replace( i, QDir::toNativeSeparators( QDir::cleanPath( QFileInfo( fileNames.at( i ) ).absoluteFilePath() ) ) );
361 switch ( mStorageMode )
365 settings.
setValue( QStringLiteral(
"UI/lastFileNameWidgetDir" ), QFileInfo( fileName ).absolutePath() );
368 settings.
setValue( QStringLiteral(
"UI/lastFileNameWidgetDir" ), fileName );
371 settings.
setValue( QStringLiteral(
"UI/lastFileNameWidgetDir" ), QFileInfo( fileNames.first( ) ).absolutePath() );
378 fileName = relativePath( fileName,
true );
383 for (
int i = 0; i < fileNames.length(); i++ )
385 fileNames.replace( i, relativePath( fileNames.at( i ),
true ) );
387 if ( fileNames.length() > 1 )
389 setFilePath( QStringLiteral(
"\"%1\"" ).arg( fileNames.join( QLatin1String(
"\" \"" ) ) ) );
399 QString QgsFileWidget::relativePath(
const QString &filePath,
bool removeRelative )
const
401 QString RelativePath;
404 RelativePath = QDir::toNativeSeparators( QDir::cleanPath( QFileInfo(
QgsProject::instance()->absoluteFilePath() ).path() ) );
408 RelativePath = QDir::toNativeSeparators( QDir::cleanPath( mDefaultRoot ) );
411 if ( !RelativePath.isEmpty() )
413 if ( removeRelative )
415 return QDir::cleanPath( QDir( RelativePath ).relativeFilePath(
filePath ) );
427 QString QgsFileWidget::toUrl(
const QString &path )
const
430 if ( path.isEmpty() )
435 QString urlStr = relativePath( path,
false );
436 QUrl url = QUrl::fromUserInput( urlStr );
437 if ( !url.isValid() || !url.isLocalFile() )
439 QgsDebugMsgLevel( QStringLiteral(
"URL: %1 is not valid or not a local file!" ).arg( path ), 2 );
443 QString pathStr = url.toString();
446 rep = QStringLiteral(
"<a href=\"%1\">%2</a>" ).arg( pathStr, path );
450 QString fileName = QFileInfo( urlStr ).fileName();
451 rep = QStringLiteral(
"<a href=\"%1\">%2</a>" ).arg( pathStr, fileName );
462 QgsFileDropEdit::QgsFileDropEdit( QWidget *parent )
465 setAcceptDrops(
true );
468 void QgsFileDropEdit::setFilters(
const QString &filters )
470 mAcceptableExtensions.clear();
472 if ( filters.contains( QStringLiteral(
"*.*" ) ) )
475 QRegularExpression rx( QStringLiteral(
"\\*\\.(\\w+)" ) );
476 QRegularExpressionMatchIterator i = rx.globalMatch( filters );
477 while ( i.hasNext() )
479 QRegularExpressionMatch match = i.next();
480 if ( match.hasMatch() )
482 mAcceptableExtensions << match.captured( 1 ).toLower();
487 QString QgsFileDropEdit::acceptableFilePath( QDropEvent *event )
const
489 QStringList rawPaths;
491 if ( event->mimeData()->hasUrls() )
493 const QList< QUrl > urls =
event->mimeData()->urls();
494 rawPaths.reserve( urls.count() );
495 for (
const QUrl &url : urls )
497 const QString local = url.toLocalFile();
498 if ( !rawPaths.contains( local ) )
499 rawPaths.append( local );
506 if ( !rawPaths.contains( u.uri ) )
507 rawPaths.append( u.uri );
510 if ( !event->mimeData()->text().isEmpty() && !rawPaths.contains( event->mimeData()->text() ) )
511 rawPaths.append( event->mimeData()->text() );
513 paths.reserve( rawPaths.count() );
514 for (
const QString &path : qgis::as_const( rawPaths ) )
516 QFileInfo file( path );
517 switch ( mStorageMode )
523 if ( file.isFile() && ( mAcceptableExtensions.isEmpty() || mAcceptableExtensions.contains( file.suffix(), Qt::CaseInsensitive ) ) )
524 paths.append( file.filePath() );
532 paths.append( file.filePath() );
533 else if ( file.isFile() )
536 paths.append( file.absolutePath() );
544 if ( paths.size() > 1 )
546 return QStringLiteral(
"\"%1\"" ).arg( paths.join( QLatin1String(
"\" \"" ) ) );
548 else if ( paths.size() == 1 )
550 return paths.first();
558 void QgsFileDropEdit::dragEnterEvent( QDragEnterEvent *event )
560 QString filePath = acceptableFilePath( event );
561 if ( !filePath.isEmpty() )
563 event->acceptProposedAction();
564 setHighlighted(
true );
572 void QgsFileDropEdit::dragLeaveEvent( QDragLeaveEvent *event )
574 QgsFilterLineEdit::dragLeaveEvent( event );
576 setHighlighted(
false );
579 void QgsFileDropEdit::dropEvent( QDropEvent *event )
581 QString filePath = acceptableFilePath( event );
582 if ( !filePath.isEmpty() )
586 setFocus( Qt::MouseFocusReason );
587 event->acceptProposedAction();
588 setHighlighted(
false );
static QString nullRepresentation()
This string is used to represent the value NULL throughout QGIS.
static QIcon getThemeIcon(const QString &name)
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)