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