QGIS API Documentation 3.41.0-Master (cea29feecf2)
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#include "moc_qgsfindfilesbypatternwidget.cpp"
18#include "qgsgui.h"
19#include "qgssettings.h"
20
21#include <QDir>
22#include <QDirIterator>
23#include <QDialogButtonBox>
24
25
27 : QWidget( parent )
28{
29 setupUi( this );
30
31 mFolderWidget->setStorageMode( QgsFileWidget::GetDirectory );
32 mResultsTable->setColumnCount( 2 );
33
34 mResultsTable->setHorizontalHeaderLabels( QStringList() << tr( "File" ) << tr( "Directory" ) );
35 mResultsTable->horizontalHeader()->setSectionResizeMode( 0, QHeaderView::Stretch );
36 mResultsTable->horizontalHeader()->setSectionResizeMode( 1, QHeaderView::Stretch );
37
38 connect( mFindButton, &QPushButton::clicked, this, &QgsFindFilesByPatternWidget::find );
39
40 const QgsSettings settings;
41 mFolderWidget->setFilePath( settings.value( QStringLiteral( "qgis/lastFindRecursiveFolder" ) ).toString() );
42 mFindButton->setEnabled( !mFolderWidget->filePath().isEmpty() );
43 connect( mFolderWidget, &QgsFileWidget::fileChanged, this, [=]( const QString &filePath ) {
44 QgsSettings settings;
45 settings.setValue( QStringLiteral( "qgis/lastFindRecursiveFolder" ), filePath );
46 mFindButton->setEnabled( !filePath.isEmpty() );
47 } );
48}
49
50void QgsFindFilesByPatternWidget::find()
51{
52 mFindButton->setText( tr( "Cancel" ) );
53 disconnect( mFindButton, &QPushButton::clicked, this, &QgsFindFilesByPatternWidget::find );
54 connect( mFindButton, &QPushButton::clicked, this, &QgsFindFilesByPatternWidget::cancel );
55 mCanceled = false;
56
57 mResultsTable->setRowCount( 0 );
58 mFiles.clear();
59
60 const QString pattern = mPatternLineEdit->text();
61 const QString path = mFolderWidget->filePath();
62
63 const QDir startDir( path );
64
65 QStringList filter;
66 if ( !pattern.isEmpty() )
67 filter << pattern;
68
69 QDirIterator it( path, filter, QDir::AllEntries | QDir::NoSymLinks | QDir::NoDotAndDotDot, mRecursiveCheckBox->isChecked() ? QDirIterator::Subdirectories : QDirIterator::NoIteratorFlags );
70 while ( it.hasNext() )
71 {
72 const QString fullPath = it.next();
73 mFiles << fullPath;
74
75 const QFileInfo fi( fullPath );
76
77 const QString toolTip = QDir::toNativeSeparators( fullPath );
78 const QString fileName = fi.fileName();
79 const QString relativeDirectory = QDir::toNativeSeparators( startDir.relativeFilePath( fi.dir().absolutePath() ) );
80 const QString fullDirectory = QDir::toNativeSeparators( fi.dir().absolutePath() );
81
82 QTableWidgetItem *fileNameItem = new QTableWidgetItem( fileName );
83 fileNameItem->setToolTip( toolTip );
84 fileNameItem->setFlags( fileNameItem->flags() ^ Qt::ItemIsEditable );
85
86 QTableWidgetItem *directoryItem = new QTableWidgetItem( relativeDirectory );
87 directoryItem->setToolTip( fullDirectory );
88 directoryItem->setFlags( directoryItem->flags() ^ Qt::ItemIsEditable );
89
90 const int row = mResultsTable->rowCount();
91 mResultsTable->insertRow( row );
92 mResultsTable->setItem( row, 0, fileNameItem );
93 mResultsTable->setItem( row, 1, directoryItem );
94
95 QCoreApplication::processEvents();
96 if ( mCanceled )
97 break;
98 }
99
100 mFindButton->setText( tr( "Find Files" ) );
101 disconnect( mFindButton, &QPushButton::clicked, this, &QgsFindFilesByPatternWidget::cancel );
102 connect( mFindButton, &QPushButton::clicked, this, &QgsFindFilesByPatternWidget::find );
103
104 emit findComplete( mFiles );
105}
106
107void QgsFindFilesByPatternWidget::cancel()
108{
109 mCanceled = true;
110}
111
112
113//
114// QgsFindFilesByPatternDialog
115//
116
118 : QDialog( parent )
119{
120 setWindowTitle( tr( "Find Files by Pattern" ) );
121 setObjectName( "QgsFindFilesByPatternDialog" );
122
124
125 QVBoxLayout *vLayout = new QVBoxLayout();
126 mWidget = new QgsFindFilesByPatternWidget();
127
128 vLayout->addWidget( mWidget );
129 mButtonBox = new QDialogButtonBox( QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal );
130 connect( mButtonBox, &QDialogButtonBox::accepted, this, &QDialog::accept );
131 connect( mButtonBox, &QDialogButtonBox::rejected, this, &QDialog::reject );
132 vLayout->addWidget( mButtonBox );
133 setLayout( vLayout );
134
135 mButtonBox->button( QDialogButtonBox::Ok )->setEnabled( false );
136 connect( mWidget, &QgsFindFilesByPatternWidget::findComplete, this, [=]( const QStringList &files ) {
137 mButtonBox->button( QDialogButtonBox::Ok )->setEnabled( !files.empty() );
138 } );
139}
140
142{
143 return mWidget->files();
144}
@ 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.
QStringList files() const
Returns the list of files found by the dialog.
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:210
This class is a composition of two QSettings instances:
Definition qgssettings.h:64
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.