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() );
51 void 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 QDir startDir( path );
67 if ( !pattern.isEmpty() )
70 QDirIterator it( path, filter, QDir::AllEntries | QDir::NoSymLinks | QDir::NoDotAndDotDot, mRecursiveCheckBox->isChecked() ? QDirIterator::Subdirectories : QDirIterator::NoIteratorFlags );
72 while ( it.hasNext() )
74 const QString fullPath = it.next();
77 QFileInfo fi( fullPath );
79 const QString toolTip = QDir::toNativeSeparators( fullPath );
80 const QString fileName = fi.fileName();
81 const QString relativeDirectory = QDir::toNativeSeparators( startDir.relativeFilePath( fi.dir().absolutePath() ) );
82 const QString fullDirectory = QDir::toNativeSeparators( fi.dir().absolutePath() );
84 QTableWidgetItem *fileNameItem =
new QTableWidgetItem( fileName );
85 fileNameItem->setToolTip( toolTip );
86 fileNameItem->setFlags( fileNameItem->flags() ^ Qt::ItemIsEditable );
88 QTableWidgetItem *directoryItem =
new QTableWidgetItem( relativeDirectory );
89 directoryItem->setToolTip( fullDirectory );
90 directoryItem->setFlags( directoryItem->flags() ^ Qt::ItemIsEditable );
92 const int row = mResultsTable->rowCount();
93 mResultsTable->insertRow( row );
94 mResultsTable->setItem( row, 0, fileNameItem );
95 mResultsTable->setItem( row, 1, directoryItem );
97 QCoreApplication::processEvents();
102 mFindButton->setText( tr(
"Find Files" ) );
103 disconnect( mFindButton, &QPushButton::clicked,
this, &QgsFindFilesByPatternWidget::cancel );
104 connect( mFindButton, &QPushButton::clicked,
this, &QgsFindFilesByPatternWidget::find );
109 void QgsFindFilesByPatternWidget::cancel()
122 setWindowTitle( tr(
"Find Files by Pattern" ) );
123 setObjectName(
"QgsFindFilesByPatternDialog" );
127 QVBoxLayout *vLayout =
new QVBoxLayout();
130 vLayout->addWidget( mWidget );
131 mButtonBox =
new QDialogButtonBox( QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal );
132 connect( mButtonBox, &QDialogButtonBox::accepted,
this, &QDialog::accept );
133 connect( mButtonBox, &QDialogButtonBox::rejected,
this, &QDialog::reject );
134 vLayout->addWidget( mButtonBox );
135 setLayout( vLayout );
137 mButtonBox->button( QDialogButtonBox::Ok )->setEnabled(
false );
140 mButtonBox->button( QDialogButtonBox::Ok )->setEnabled( !files.empty() );
146 return mWidget->
files();
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.
QgsFindFilesByPatternDialog(QWidget *parent SIP_TRANSFERTHIS=nullptr)
Constructor for QgsFindFilesByPatternDialog, with the specified parent widget.
QStringList files() const
Returns the list of files found by the dialog.
void setValue(const QString &key, const QVariant &value, QgsSettings::Section section=QgsSettings::NoSection)
Sets the value of setting key to value.
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...