QGIS API Documentation 3.99.0-Master (e9821da5c6b)
Loading...
Searching...
No Matches
qgsfindfilesbypatternwidget.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsfindfilesbypatternwidget.cpp
3 -----------------------------
4 begin : April 2019
5 copyright : (C) 2019 by Nyall Dawson
6 email : nyall dot dawson at gmail dot com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
17
18#include "qgsgui.h"
19#include "qgssettings.h"
20
21#include <QDialogButtonBox>
22#include <QDir>
23#include <QDirIterator>
24#include <QString>
25
26#include "moc_qgsfindfilesbypatternwidget.cpp"
27
28using namespace Qt::StringLiterals;
29
31 : QWidget( parent )
32{
33 setupUi( this );
34
35 mFolderWidget->setStorageMode( QgsFileWidget::GetDirectory );
36 mResultsTable->setColumnCount( 2 );
37
38 mResultsTable->setHorizontalHeaderLabels( QStringList() << tr( "File" ) << tr( "Directory" ) );
39 mResultsTable->horizontalHeader()->setSectionResizeMode( 0, QHeaderView::Stretch );
40 mResultsTable->horizontalHeader()->setSectionResizeMode( 1, QHeaderView::Stretch );
41
42 connect( mFindButton, &QPushButton::clicked, this, &QgsFindFilesByPatternWidget::find );
43
44 const QgsSettings settings;
45 mFolderWidget->setFilePath( settings.value( u"qgis/lastFindRecursiveFolder"_s ).toString() );
46 mFindButton->setEnabled( !mFolderWidget->filePath().isEmpty() );
47 connect( mFolderWidget, &QgsFileWidget::fileChanged, this, [this]( const QString &filePath ) {
48 QgsSettings settings;
49 settings.setValue( u"qgis/lastFindRecursiveFolder"_s, filePath );
50 mFindButton->setEnabled( !filePath.isEmpty() );
51 } );
52}
53
54void QgsFindFilesByPatternWidget::find()
55{
56 mFindButton->setText( tr( "Cancel" ) );
57 disconnect( mFindButton, &QPushButton::clicked, this, &QgsFindFilesByPatternWidget::find );
58 connect( mFindButton, &QPushButton::clicked, this, &QgsFindFilesByPatternWidget::cancel );
59 mCanceled = false;
60
61 mResultsTable->setRowCount( 0 );
62 mFiles.clear();
63
64 const QString pattern = mPatternLineEdit->text();
65 const QString path = mFolderWidget->filePath();
66
67 const QDir startDir( path );
68
69 QStringList filter;
70 if ( !pattern.isEmpty() )
71 filter << pattern;
72
73 QDirIterator it( path, filter, QDir::AllEntries | QDir::NoSymLinks | QDir::NoDotAndDotDot, mRecursiveCheckBox->isChecked() ? QDirIterator::Subdirectories : QDirIterator::NoIteratorFlags );
74 while ( it.hasNext() )
75 {
76 const QString fullPath = it.next();
77 mFiles << fullPath;
78
79 const QFileInfo fi( fullPath );
80
81 const QString toolTip = QDir::toNativeSeparators( fullPath );
82 const QString fileName = fi.fileName();
83 const QString relativeDirectory = QDir::toNativeSeparators( startDir.relativeFilePath( fi.dir().absolutePath() ) );
84 const QString fullDirectory = QDir::toNativeSeparators( fi.dir().absolutePath() );
85
86 QTableWidgetItem *fileNameItem = new QTableWidgetItem( fileName );
87 fileNameItem->setToolTip( toolTip );
88 fileNameItem->setFlags( fileNameItem->flags() ^ Qt::ItemIsEditable );
89
90 QTableWidgetItem *directoryItem = new QTableWidgetItem( relativeDirectory );
91 directoryItem->setToolTip( fullDirectory );
92 directoryItem->setFlags( directoryItem->flags() ^ Qt::ItemIsEditable );
93
94 const int row = mResultsTable->rowCount();
95 mResultsTable->insertRow( row );
96 mResultsTable->setItem( row, 0, fileNameItem );
97 mResultsTable->setItem( row, 1, directoryItem );
98
99 QCoreApplication::processEvents();
100 if ( mCanceled )
101 break;
102 }
103
104 mFindButton->setText( tr( "Find Files" ) );
105 disconnect( mFindButton, &QPushButton::clicked, this, &QgsFindFilesByPatternWidget::cancel );
106 connect( mFindButton, &QPushButton::clicked, this, &QgsFindFilesByPatternWidget::find );
107
108 emit findComplete( mFiles );
109}
110
111void QgsFindFilesByPatternWidget::cancel()
112{
113 mCanceled = true;
114}
115
116
117//
118// QgsFindFilesByPatternDialog
119//
120
122 : QDialog( parent )
123{
124 setWindowTitle( tr( "Find Files by Pattern" ) );
125 setObjectName( "QgsFindFilesByPatternDialog" );
126
128
129 QVBoxLayout *vLayout = new QVBoxLayout();
130 mWidget = new QgsFindFilesByPatternWidget();
131
132 vLayout->addWidget( mWidget );
133 mButtonBox = new QDialogButtonBox( QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal );
134 connect( mButtonBox, &QDialogButtonBox::accepted, this, &QDialog::accept );
135 connect( mButtonBox, &QDialogButtonBox::rejected, this, &QDialog::reject );
136 vLayout->addWidget( mButtonBox );
137 setLayout( vLayout );
138
139 mButtonBox->button( QDialogButtonBox::Ok )->setEnabled( false );
140 connect( mWidget, &QgsFindFilesByPatternWidget::findComplete, this, [this]( const QStringList &files ) {
141 mButtonBox->button( QDialogButtonBox::Ok )->setEnabled( !files.empty() );
142 } );
143}
144
146{
147 return mWidget->files();
148}
@ GetDirectory
Select a directory.
void fileChanged(const QString &path)
Emitted whenever the current file or directory path is changed.
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.
A reusable widget for finding files (recursively) by file pattern.
void findComplete(const QStringList &files)
Emitted after files are found in the dialog.
QgsFindFilesByPatternWidget(QWidget *parent SIP_TRANSFERTHIS=nullptr)
Constructor for QgsFindFilesByPatternWidget, 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...
Definition qgsgui.cpp:224
Stores settings for use within QGIS.
Definition qgssettings.h:68
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.