21#include <QDirIterator>
22#include <QDialogButtonBox>
31 mResultsTable->setColumnCount( 2 );
33 mResultsTable->setHorizontalHeaderLabels( QStringList() << tr(
"File" ) << tr(
"Directory" ) );
34 mResultsTable->horizontalHeader()->setSectionResizeMode( 0, QHeaderView::Stretch );
35 mResultsTable->horizontalHeader()->setSectionResizeMode( 1, QHeaderView::Stretch );
37 connect( mFindButton, &QPushButton::clicked,
this, &QgsFindFilesByPatternWidget::find );
40 mFolderWidget->setFilePath( settings.
value( QStringLiteral(
"qgis/lastFindRecursiveFolder" ) ).toString() );
41 mFindButton->setEnabled( !mFolderWidget->filePath().isEmpty() );
45 settings.
setValue( QStringLiteral(
"qgis/lastFindRecursiveFolder" ), filePath );
46 mFindButton->setEnabled( !filePath.isEmpty() );
51void QgsFindFilesByPatternWidget::find()
53 mFindButton->setText( tr(
"Cancel" ) );
54 disconnect( mFindButton, &QPushButton::clicked,
this, &QgsFindFilesByPatternWidget::find );
55 connect( mFindButton, &QPushButton::clicked,
this, &QgsFindFilesByPatternWidget::cancel );
58 mResultsTable->setRowCount( 0 );
61 const QString pattern = mPatternLineEdit->text();
62 const QString path = mFolderWidget->filePath();
64 const QDir startDir( path );
67 if ( !pattern.isEmpty() )
70 QDirIterator it( path, filter, QDir::AllEntries | QDir::NoSymLinks | QDir::NoDotAndDotDot, mRecursiveCheckBox->isChecked() ? QDirIterator::Subdirectories : QDirIterator::NoIteratorFlags );
71 while ( it.hasNext() )
73 const QString fullPath = it.next();
76 const QFileInfo fi( fullPath );
78 const QString toolTip = QDir::toNativeSeparators( fullPath );
79 const QString fileName = fi.fileName();
80 const QString relativeDirectory = QDir::toNativeSeparators( startDir.relativeFilePath( fi.dir().absolutePath() ) );
81 const QString fullDirectory = QDir::toNativeSeparators( fi.dir().absolutePath() );
83 QTableWidgetItem *fileNameItem =
new QTableWidgetItem( fileName );
84 fileNameItem->setToolTip( toolTip );
85 fileNameItem->setFlags( fileNameItem->flags() ^ Qt::ItemIsEditable );
87 QTableWidgetItem *directoryItem =
new QTableWidgetItem( relativeDirectory );
88 directoryItem->setToolTip( fullDirectory );
89 directoryItem->setFlags( directoryItem->flags() ^ Qt::ItemIsEditable );
91 const int row = mResultsTable->rowCount();
92 mResultsTable->insertRow( row );
93 mResultsTable->setItem( row, 0, fileNameItem );
94 mResultsTable->setItem( row, 1, directoryItem );
96 QCoreApplication::processEvents();
101 mFindButton->setText( tr(
"Find Files" ) );
102 disconnect( mFindButton, &QPushButton::clicked,
this, &QgsFindFilesByPatternWidget::cancel );
103 connect( mFindButton, &QPushButton::clicked,
this, &QgsFindFilesByPatternWidget::find );
108void QgsFindFilesByPatternWidget::cancel()
121 setWindowTitle( tr(
"Find Files by Pattern" ) );
122 setObjectName(
"QgsFindFilesByPatternDialog" );
126 QVBoxLayout *vLayout =
new QVBoxLayout();
129 vLayout->addWidget( mWidget );
130 mButtonBox =
new QDialogButtonBox( QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal );
131 connect( mButtonBox, &QDialogButtonBox::accepted,
this, &QDialog::accept );
132 connect( mButtonBox, &QDialogButtonBox::rejected,
this, &QDialog::reject );
133 vLayout->addWidget( mButtonBox );
134 setLayout( vLayout );
136 mButtonBox->button( QDialogButtonBox::Ok )->setEnabled(
false );
139 mButtonBox->button( QDialogButtonBox::Ok )->setEnabled( !
files.empty() );
145 return mWidget->
files();
QStringList files() const
Returns the list of files found by the dialog.
QgsFindFilesByPatternDialog(QWidget *parent SIP_TRANSFERTHIS=nullptr)
Constructor for QgsFindFilesByPatternDialog, with the specified parent widget.
static void enableAutoGeometryRestore(QWidget *widget, const QString &key=QString())
Register the widget to allow its position to be automatically saved and restored when open and closed...
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.