Recursive Import Dialog: Cleaned up code

This commit is contained in:
Jacob Støren
2019-08-19 11:44:19 +02:00
parent 3d9ed39795
commit 2d2ecc3031
2 changed files with 117 additions and 148 deletions

View File

@@ -42,7 +42,7 @@
#include <vector>
#define DEFAULT_DIALOG_FIND_HEIGHT 350
#define RECURSIVE_FILESEARCH_DEFAULT_DIALOG_HEIGHT 350
#define FIND_BUTTON_FIND_TEXT "Find"
#define FILES_FOUND_TEXT "Files Found"
@@ -54,10 +54,44 @@ static const QChar SEPARATOR = RiaFilePathTools::SEPARATOR;
//--------------------------------------------------------------------------------------------------
/// Internal functions
//--------------------------------------------------------------------------------------------------
static QStringList prefixStrings(const QStringList& strings, const QString& prefix);
static QStringList trimLeftStrings(const QStringList& strings, const QString& trimText);
static void sortStringsByLength(QStringList& strings, bool ascending = true);
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RicRecursiveFileSearchDialogResult RicRecursiveFileSearchDialog::runRecursiveSearchDialog(QWidget *parent,
const QString &caption,
const QString &dir,
const QString &pathFilter ,
const QString &fileNameFilter,
const QStringList &fileExtensions)
{
RicRecursiveFileSearchDialog dialog(parent);
dialog.setWindowTitle(caption);
QString pathFilterText = dir;
RiaFilePathTools::appendSeparatorIfNo(pathFilterText);
pathFilterText += pathFilter;
dialog.m_pathFilterField->setText(QDir::toNativeSeparators(pathFilterText));
dialog.m_fileFilterField->setText(fileNameFilter);
dialog.m_fileExtensions = trimLeftStrings(fileExtensions, ".");
dialog.updateEffectiveFilter();
dialog.clearFileList();
dialog.setOkButtonEnabled(false);
dialog.resize(800, 150);
dialog.exec();
return RicRecursiveFileSearchDialogResult(dialog.result() == QDialog::Accepted,
dialog.m_foundFiles,
dialog.rootDirWithEndSeparator(),
dialog.pathFilterWithoutStartSeparator(),
dialog.fileNameFilter());
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
@@ -189,38 +223,6 @@ RicRecursiveFileSearchDialog::~RicRecursiveFileSearchDialog()
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RicRecursiveFileSearchDialogResult RicRecursiveFileSearchDialog::runRecursiveSearchDialog(QWidget *parent,
const QString &caption,
const QString &dir,
const QString &pathFilter ,
const QString &fileNameFilter,
const QStringList &fileExtensions)
{
RicRecursiveFileSearchDialog dialog(parent);
dialog.setWindowTitle(caption);
QString pathFilterText = dir;
RiaFilePathTools::appendSeparatorIfNo(pathFilterText);
pathFilterText += pathFilter;
dialog.m_pathFilterField->setText(QDir::toNativeSeparators(pathFilterText));
dialog.m_fileFilterField->setText(fileNameFilter);
dialog.m_fileExtensions = trimLeftStrings(fileExtensions, ".");
dialog.updateEffectiveFilter();
dialog.clearFileList();
dialog.setOkButtonEnabled(false);
dialog.resize(800, 150);
dialog.exec();
return RicRecursiveFileSearchDialogResult(dialog.result() == QDialog::Accepted,
dialog.m_foundFiles,
dialog.rootDirWithEndSeparator(),
dialog.pathFilterWithoutStartSeparator(),
dialog.fileNameFilter());
}
QString RicRecursiveFileSearchDialog::cleanTextFromPathFilterField() const
{
QString pathFilterText = m_pathFilterField->text().trimmed();
@@ -277,16 +279,6 @@ QStringList RicRecursiveFileSearchDialog::fileExtensions() const
return exts;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QString RicRecursiveFileSearchDialog::fileExtensionsText() const
{
QString extFromFilter = extensionFromFileNameFilter();
if (!extFromFilter.isEmpty()) return "";
else return prefixStrings(fileExtensions(), ".").join(" | ");
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
@@ -392,55 +384,6 @@ QStringList RicRecursiveFileSearchDialog::findMatchingFiles()
return findFilesInDirs(dirs);
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QStringList RicRecursiveFileSearchDialog::buildDirectoryListRecursive(const QString& currentDir, int level)
{
QStringList allDirs;
if (m_isCancelPressed) return allDirs;
QString currPathFilter = pathFilterWithoutStartSeparator();
bool subStringFilter = false;
// Optimizing for speed by a refined match at first directory level
if (level == 1)
{
QString pathFilter = this->pathFilterWithoutStartSeparator();
if (!pathFilter.startsWith("*"))
{
int wildcardIndex = pathFilter.indexOf(QRegExp(QString("[*%1]").arg(SEPARATOR)));
if (wildcardIndex >= 0)
{
currPathFilter = pathFilter.left(wildcardIndex + 1);
subStringFilter = true;
}
}
}
QString currRelPath = RiaFilePathTools::relativePath(rootDirWithEndSeparator(), currentDir);
if (pathFilterMatch(currPathFilter, currRelPath))
{
allDirs.push_back(currentDir);
}
else if(level == 1 && subStringFilter)
{
return QStringList();
}
QDir qdir(currentDir);
QStringList subDirs = qdir.entryList(QDir::Dirs | QDir::NoDotAndDotDot);
for (QString subDir : subDirs)
{
QString subDirFullPath = qdir.absoluteFilePath(subDir);
updateStatus(SEARCHING_FOR_DIRS, subDirFullPath);
QApplication::processEvents();
allDirs += buildDirectoryListRecursive(subDirFullPath, level + 1);
}
return m_isCancelPressed ? QStringList() : allDirs;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
@@ -548,17 +491,6 @@ QStringList RicRecursiveFileSearchDialog::createFileNameFilterList()
return nameFilter;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
bool RicRecursiveFileSearchDialog::pathFilterMatch(const QString& pathFilter, const QString& relPath)
{
QString pattern = pathFilter;
if (relPath.endsWith(SEPARATOR) && !pathFilter.endsWith(SEPARATOR)) pattern += SEPARATOR;
QRegExp regexp(pattern, Qt::CaseInsensitive, QRegExp::Wildcard);
return regexp.exactMatch(relPath);
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
@@ -717,7 +649,7 @@ void RicRecursiveFileSearchDialog::slotFindOrCancelButtonClicked()
m_searchRootLabel->setVisible(true);
m_searchRootContentLabel->setVisible(true);
if(height() < DEFAULT_DIALOG_FIND_HEIGHT) resize(width(), DEFAULT_DIALOG_FIND_HEIGHT);
if(height() < RECURSIVE_FILESEARCH_DEFAULT_DIALOG_HEIGHT) resize(width(), RECURSIVE_FILESEARCH_DEFAULT_DIALOG_HEIGHT);
}
m_findOrCancelButton->setText("Cancel");
@@ -795,26 +727,6 @@ void RicRecursiveFileSearchDialog::slotBrowseButtonClicked()
/// Internal functions
//--------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QStringList prefixStrings(const QStringList& strings, const QString& prefix)
{
QStringList prefixedStrings;
for (auto string : strings)
{
if (!string.startsWith(prefix))
{
prefixedStrings.append(prefix + string);
}
else
{
prefixedStrings.append(string);
}
}
return prefixedStrings;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
@@ -861,3 +773,63 @@ void sortStringsByLength(QStringList& strings, bool ascending /*= true*/)
}
//--------------------------------------------------------------------------------------------------
/// Obsolete
//--------------------------------------------------------------------------------------------------
QStringList RicRecursiveFileSearchDialog::buildDirectoryListRecursive(const QString& currentDir, int level)
{
QStringList allDirs;
if (m_isCancelPressed) return allDirs;
QString currPathFilter = pathFilterWithoutStartSeparator();
bool subStringFilter = false;
// Optimizing for speed by a refined match at first directory level
if (level == 1)
{
QString pathFilter = this->pathFilterWithoutStartSeparator();
if (!pathFilter.startsWith("*"))
{
int wildcardIndex = pathFilter.indexOf(QRegExp(QString("[*%1]").arg(SEPARATOR)));
if (wildcardIndex >= 0)
{
currPathFilter = pathFilter.left(wildcardIndex + 1);
subStringFilter = true;
}
}
}
QString currRelPath = RiaFilePathTools::relativePath(rootDirWithEndSeparator(), currentDir);
if (pathFilterMatch(currPathFilter, currRelPath))
{
allDirs.push_back(currentDir);
}
else if(level == 1 && subStringFilter)
{
return QStringList();
}
QDir qdir(currentDir);
QStringList subDirs = qdir.entryList(QDir::Dirs | QDir::NoDotAndDotDot);
for (QString subDir : subDirs)
{
QString subDirFullPath = qdir.absoluteFilePath(subDir);
updateStatus(SEARCHING_FOR_DIRS, subDirFullPath);
QApplication::processEvents();
allDirs += buildDirectoryListRecursive(subDirFullPath, level + 1);
}
return m_isCancelPressed ? QStringList() : allDirs;
}
//--------------------------------------------------------------------------------------------------
/// Obsolete
//--------------------------------------------------------------------------------------------------
bool RicRecursiveFileSearchDialog::pathFilterMatch(const QString& pathFilter, const QString& relPath)
{
QString pattern = pathFilter;
if (relPath.endsWith(SEPARATOR) && !pathFilter.endsWith(SEPARATOR)) pattern += SEPARATOR;
QRegExp regexp(pattern, Qt::CaseInsensitive, QRegExp::Wildcard);
return regexp.exactMatch(relPath);
}

View File

@@ -45,8 +45,6 @@ class RicRecursiveFileSearchDialog : public QDialog
enum Status {SEARCHING_FOR_DIRS, SEARCHING_FOR_FILES, NO_FILES_FOUND};
public:
RicRecursiveFileSearchDialog(QWidget* parent);
~RicRecursiveFileSearchDialog() override;
static RicRecursiveFileSearchDialogResult runRecursiveSearchDialog(QWidget *parent = nullptr,
const QString& caption = QString(),
@@ -56,51 +54,47 @@ public:
const QStringList& fileExtensions = QStringList());
private:
RicRecursiveFileSearchDialog(QWidget* parent);
~RicRecursiveFileSearchDialog() override;
QString cleanTextFromPathFilterField() const;
QString rootDirWithEndSeparator() const;
QString pathFilterWithoutStartSeparator() const;
QString fileNameFilter() const;
QStringList fileExtensions() const;
QString fileExtensionsText() const;
QString extensionFromFileNameFilter() const;
void setOkButtonEnabled(bool enabled);
void warningIfInvalidCharacters();
void updateEffectiveFilter();
void updateStatus(Status status, const QString& extraText = "");
void updateFileListWidget();
void clearFileList();
void updateStatus(Status status, const QString& extraText = "");
// File search methods
QStringList findMatchingFiles();
QStringList buildDirectoryListRecursive(const QString& currentDir, int level = 0);
void buildDirectoryListRecursiveSimple(const QString& currentDir,
const QString& currentPathFilter,
QStringList* accumulatedDirs);
QStringList findFilesInDirs(const QStringList& dirs);
QStringList createFileNameFilterList();
bool pathFilterMatch(const QString& pathFilter, const QString& relPath);
void updateEffectiveFilter();
void setOkButtonEnabled(bool enabled);
void warningIfInvalidCharacters();
private slots:
void slotFilterChanged(const QString& text);
void slotBrowseButtonClicked();
void slotFindOrCancelButtonClicked();
void slotFileListCustomMenuRequested(const QPoint& point);
void slotCopyFileItemText();
void slotToggleFileListItems();
void slotTurnOffFileListItems();
void slotTurnOnFileListItems();
void slotCopyFileItemText();
void slotFindOrCancelButtonClicked();
void slotDialogOkClicked();
void slotDialogCancelClicked();
void slotBrowseButtonClicked();
private:
@@ -113,21 +107,24 @@ private:
QLabel* m_effectiveFilterLabel;
QLabel* m_effectiveFilterContentLabel;
QPushButton* m_findOrCancelButton;
QGroupBox* m_outputGroup;
QLabel* m_searchRootLabel;
QLabel* m_searchRootContentLabel;
QLabel* m_fileListLabel;
QListWidget* m_fileListWidget;
QPushButton* m_findOrCancelButton;
QDialogButtonBox* m_buttons;
QStringList m_foundFiles;
QStringList m_fileExtensions;
bool m_isCancelPressed;
// Obsolete. Here for reference if this search mode is needed later
QStringList buildDirectoryListRecursive(const QString& currentDir, int level = 0);
bool pathFilterMatch(const QString& pathFilter, const QString& relPath);
};