Improve search behavior in the file browser (#7679)
This commit is contained in:
@@ -27,8 +27,8 @@
|
||||
|
||||
#include <QDir>
|
||||
#include <QMutex>
|
||||
#include <memory>
|
||||
|
||||
#include "FileSearchJob.h"
|
||||
#include "embed.h"
|
||||
|
||||
#include <QTreeWidget>
|
||||
@@ -43,7 +43,6 @@ class QProgressBar;
|
||||
namespace lmms
|
||||
{
|
||||
|
||||
class FileSearch;
|
||||
class InstrumentTrack;
|
||||
class PlayHandle;
|
||||
class TrackContainer;
|
||||
@@ -53,6 +52,7 @@ namespace gui
|
||||
|
||||
class FileBrowserTreeWidget;
|
||||
class FileItem;
|
||||
class FileSearchJob;
|
||||
|
||||
class FileBrowser : public SideBarWidget
|
||||
{
|
||||
@@ -77,20 +77,6 @@ public:
|
||||
|
||||
~FileBrowser() override = default;
|
||||
|
||||
static QStringList excludedPaths()
|
||||
{
|
||||
static auto s_excludedPaths = QStringList{
|
||||
#ifdef LMMS_BUILD_LINUX
|
||||
"/bin", "/boot", "/dev", "/etc", "/proc", "/run", "/sbin",
|
||||
"/sys"
|
||||
#endif
|
||||
#ifdef LMMS_BUILD_WIN32
|
||||
"C:\\Windows"
|
||||
#endif
|
||||
};
|
||||
return s_excludedPaths;
|
||||
}
|
||||
|
||||
static QDir::Filters dirFilters() { return QDir::AllDirs | QDir::Files | QDir::NoDotAndDotDot | QDir::Hidden; }
|
||||
static QDir::SortFlags sortFlags() { return QDir::LocaleAware | QDir::DirsFirst | QDir::Name | QDir::IgnoreCase; }
|
||||
|
||||
@@ -107,10 +93,10 @@ private:
|
||||
void saveDirectoriesStates();
|
||||
void restoreDirectoriesStates();
|
||||
|
||||
void foundSearchMatch(FileSearch* search, const QString& match);
|
||||
void searchCompleted(FileSearch* search);
|
||||
void onSearch(const QString& filter);
|
||||
void displaySearch(bool on);
|
||||
void onSearchMatch(const QString& path);
|
||||
void onSearchStarted();
|
||||
void onSearchFinished();
|
||||
|
||||
void addContentCheckBox();
|
||||
|
||||
@@ -120,8 +106,8 @@ private:
|
||||
QLineEdit * m_filterEdit;
|
||||
Type m_type;
|
||||
|
||||
std::shared_ptr<FileSearch> m_currentSearch;
|
||||
QProgressBar* m_searchIndicator = nullptr;
|
||||
FileSearchJob m_searchJob;
|
||||
|
||||
QString m_directories; //!< Directories to search, split with '*'
|
||||
QString m_filter; //!< Filter as used in QDir::match()
|
||||
@@ -141,9 +127,6 @@ private:
|
||||
QString m_previousFilterValue;
|
||||
} ;
|
||||
|
||||
|
||||
|
||||
|
||||
class FileBrowserTreeWidget : public QTreeWidget
|
||||
{
|
||||
Q_OBJECT
|
||||
@@ -200,16 +183,22 @@ private slots:
|
||||
void updateDirectory(QTreeWidgetItem* item);
|
||||
} ;
|
||||
|
||||
|
||||
|
||||
class Directory : public QTreeWidgetItem
|
||||
class FileBrowserWidgetItem : public QTreeWidgetItem
|
||||
{
|
||||
public:
|
||||
Directory(const QString& filename, const QString& path, const QString& filter, bool disableEntryPopulation = false);
|
||||
FileBrowserWidgetItem(const QStringList& strings, int type, QTreeWidget* parent = nullptr);
|
||||
virtual QString fullName(QString path = QString{}) const = 0;
|
||||
};
|
||||
|
||||
|
||||
class Directory : public FileBrowserWidgetItem
|
||||
{
|
||||
public:
|
||||
Directory(const QString& filename, const QString& path, const QString& filter);
|
||||
|
||||
void update();
|
||||
|
||||
inline QString fullName( QString path = QString() )
|
||||
QString fullName(QString path = QString{}) const override
|
||||
{
|
||||
if( path.isEmpty() )
|
||||
{
|
||||
@@ -247,13 +236,12 @@ private:
|
||||
QString m_filter;
|
||||
|
||||
int m_dirCount;
|
||||
bool m_disableEntryPopulation = false;
|
||||
} ;
|
||||
|
||||
|
||||
|
||||
|
||||
class FileItem : public QTreeWidgetItem
|
||||
class FileItem : public FileBrowserWidgetItem
|
||||
{
|
||||
public:
|
||||
enum class FileType
|
||||
@@ -282,7 +270,7 @@ public:
|
||||
const QString & path );
|
||||
FileItem( const QString & name, const QString & path );
|
||||
|
||||
QString fullName() const
|
||||
QString fullName(QString path = QString{}) const override
|
||||
{
|
||||
return QFileInfo(m_path, text(0)).absoluteFilePath();
|
||||
}
|
||||
|
||||
@@ -1,73 +0,0 @@
|
||||
/*
|
||||
* FileSearch.h - File system search task
|
||||
*
|
||||
* Copyright (c) 2024 saker
|
||||
*
|
||||
* This file is part of LMMS - https://lmms.io
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public
|
||||
* License along with this program (see COPYING); if not, write to the
|
||||
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
||||
* Boston, MA 02110-1301 USA.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef LMMS_FILE_SEARCH_H
|
||||
#define LMMS_FILE_SEARCH_H
|
||||
|
||||
#include <QDir>
|
||||
#include <QObject>
|
||||
#include <atomic>
|
||||
|
||||
namespace lmms {
|
||||
//! A Qt object that encapsulates the operation of searching the file system.
|
||||
class FileSearch : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
//! Number of milliseconds the search waits before signaling a matching result.
|
||||
static constexpr int MillisecondsBetweenResults = 1;
|
||||
|
||||
//! Create a `FileSearch` object that uses the specified string filter `filter` and extension filters in
|
||||
//! `extensions` to search within the given `paths`.
|
||||
//! `excludedPaths`, `dirFilters`, and `sortFlags` can optionally be specified to exclude certain directories, filter
|
||||
//! out certain types of entries, and sort the matches.
|
||||
FileSearch(const QString& filter, const QStringList& paths, const QStringList& extensions,
|
||||
const QStringList& excludedPaths = {}, QDir::Filters dirFilters = QDir::Filters{},
|
||||
QDir::SortFlags sortFlags = QDir::SortFlags{});
|
||||
|
||||
//! Execute the search, emitting the `foundResult` signal when matches are found.
|
||||
void operator()();
|
||||
|
||||
//! Cancel the search.
|
||||
void cancel();
|
||||
|
||||
signals:
|
||||
//! Emitted when a result is found when searching the file system.
|
||||
void foundMatch(FileSearch* search, const QString& match);
|
||||
|
||||
//! Emitted when the search completes.
|
||||
void searchCompleted(FileSearch* search);
|
||||
|
||||
private:
|
||||
static auto isPathExcluded(const QString& path) -> bool;
|
||||
QString m_filter;
|
||||
QStringList m_paths;
|
||||
QStringList m_extensions;
|
||||
QStringList m_excludedPaths;
|
||||
QDir::Filters m_dirFilters;
|
||||
QDir::SortFlags m_sortFlags;
|
||||
std::atomic<bool> m_cancel = false;
|
||||
};
|
||||
} // namespace lmms
|
||||
#endif // LMMS_FILE_SEARCH_H
|
||||
78
include/FileSearchJob.h
Normal file
78
include/FileSearchJob.h
Normal file
@@ -0,0 +1,78 @@
|
||||
/*
|
||||
* FileSearchJob.h
|
||||
*
|
||||
* Copyright (c) 2025 saker <sakertooth@gmail.com>
|
||||
*
|
||||
* This file is part of LMMS - https://lmms.io
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public
|
||||
* License along with this program (see COPYING); if not, write to the
|
||||
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
||||
* Boston, MA 02110-1301 USA.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef LMMS_GUI_FILE_SEARCH_JOB_H
|
||||
#define LMMS_GUI_FILE_SEARCH_JOB_H
|
||||
|
||||
#include <QDir>
|
||||
#include <QObject>
|
||||
#include <QString>
|
||||
#include <future>
|
||||
|
||||
namespace lmms::gui {
|
||||
//! The `FileSearchJob` class allows for searching for files on the filesystem.
|
||||
//! Searching occurs on a background thread, and results are emitted as a Qt slot back to the user.
|
||||
class FileSearchJob : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
//! Represents a search task to be carried out by the search job.
|
||||
struct Task
|
||||
{
|
||||
QString filter; //! The filter to be tokenized.
|
||||
QStringList paths; //! The list of paths to search recursively through.
|
||||
QStringList extensions; //! The list of allowed extensions.
|
||||
QFlags<QDir::Filter> dirFilters; //! The directory filter flag.
|
||||
};
|
||||
|
||||
//! Create a search job with the given @p parent (if any).
|
||||
FileSearchJob(QObject* parent = nullptr);
|
||||
|
||||
//! Stop processing and destroys the object.
|
||||
~FileSearchJob();
|
||||
|
||||
//! Commit to searching with the given @p task.
|
||||
//! Cancels any previous search.
|
||||
//! Callers can connect to the provided signals to interact with the search and its progress.
|
||||
void search(Task task);
|
||||
|
||||
signals:
|
||||
//! Emitted when the search job has found a matching path.
|
||||
void foundMatch(const QString& path);
|
||||
|
||||
//! Emitted when the search job has started searching.
|
||||
void started();
|
||||
|
||||
//! Emitted when the search job has finished searching.
|
||||
void finished();
|
||||
|
||||
private:
|
||||
Q_DISABLE_MOVE(FileSearchJob)
|
||||
void runSearch(Task task);
|
||||
std::future<void> m_task;
|
||||
std::atomic_flag m_stop = ATOMIC_FLAG_INIT;
|
||||
};
|
||||
} // namespace lmms::gui
|
||||
|
||||
#endif // LMMS_GUI_FILE_SEARCH_JOB_H
|
||||
Reference in New Issue
Block a user