Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enable search in Browse & Recording views #11014

Merged
merged 5 commits into from
Nov 1, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/library/browse/browsefeature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ BrowseFeature::BrowseFeature(
: LibraryFeature(pLibrary, pConfig, QString("computer")),
m_pTrackCollection(pLibrary->trackCollectionManager()->internalCollection()),
m_browseModel(this, pLibrary->trackCollectionManager(), pRecordingManager),
m_proxyModel(&m_browseModel),
m_proxyModel(&m_browseModel, true),
m_pSidebarModel(new FolderTreeModel(this)),
m_pLastRightClickedItem(nullptr) {
connect(this,
Expand Down Expand Up @@ -243,7 +243,7 @@ void BrowseFeature::activate() {
void BrowseFeature::activateChild(const QModelIndex& index) {
TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
qDebug() << "BrowseFeature::activateChild " << item->getLabel() << " "
<< item->getData();
<< item->getData().toString();

QString path = item->getData().toString();
if (path == QUICK_LINK_NODE || path == DEVICE_NODE) {
Expand All @@ -267,7 +267,10 @@ void BrowseFeature::activateChild(const QModelIndex& index) {
m_browseModel.setPath(std::move(dirAccess));
}
emit showTrackModel(&m_proxyModel);
emit disableSearch();
// Search is restored in Library::slotShowTrackModel, disable it where it's useless
if (path == QUICK_LINK_NODE || path == DEVICE_NODE) {
emit disableSearch();
}
emit enableCoverArtDisplay(false);
}

Expand Down
4 changes: 3 additions & 1 deletion src/library/browse/browsetablemodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@ CoverInfo BrowseTableModel::getCoverInfo(const QModelIndex& index) const {
return CoverInfo();
}
}

const QVector<int> BrowseTableModel::getTrackRows(TrackId trackId) const {
Q_UNUSED(trackId);
// We can't implement this as it stands.
Expand Down Expand Up @@ -378,7 +379,8 @@ TrackModel::Capabilities BrowseTableModel::getCapabilities() const {
}

QString BrowseTableModel::modelKey(bool noSearch) const {
// Browse feature does currently not support searching.
// Searching is handled by the proxy model, so if there is an active search
// modelkey is composed there, too.
Q_UNUSED(noSearch);
return QStringLiteral("browse:") + m_currentDirectory;
}
Expand Down
52 changes: 37 additions & 15 deletions src/library/proxytrackmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <QVariant>

#include "library/searchqueryparser.h"
#include "util/assert.h"

ProxyTrackModel::ProxyTrackModel(QAbstractItemModel* pTrackModel,
Expand Down Expand Up @@ -77,7 +78,14 @@ void ProxyTrackModel::search(const QString& searchText, const QString& extraFilt
}

QString ProxyTrackModel::modelKey(bool noSearch) const {
return m_pTrackModel ? m_pTrackModel->modelKey(noSearch) : QString();
if (m_pTrackModel) {
if (m_bHandleSearches) {
return m_pTrackModel->modelKey(true) + QStringLiteral("#") + currentSearch();
} else {
return m_pTrackModel->modelKey(noSearch);
}
}
return QString();
}

const QString ProxyTrackModel::currentSearch() const {
Expand Down Expand Up @@ -147,26 +155,40 @@ bool ProxyTrackModel::filterAcceptsRow(int sourceRow,
return false;
}

if (m_currentSearch.trimmed().isEmpty()) {
return true;
}

QStringList tokens = SearchQueryParser::splitQueryIntoWords(
m_currentSearch.trimmed());
ronso0 marked this conversation as resolved.
Show resolved Hide resolved
tokens.removeDuplicates();

const QList<int>& filterColumns = m_pTrackModel->searchColumns();
QAbstractItemModel* itemModel =
dynamic_cast<QAbstractItemModel*>(m_pTrackModel);
bool rowMatches = false;

QRegularExpression filter = filterRegularExpression();
QListIterator<int> iter(filterColumns);

while (!rowMatches && iter.hasNext()) {
int i = iter.next();
QModelIndex index = itemModel->index(sourceRow, i, sourceParent);
QVariant data = itemModel->data(index);
if (data.canConvert<QString>()) {
QString strData = data.toString();
if (strData.contains(filter)) {
rowMatches = true;

for (QString token : tokens) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
for (QString token : tokens) {
for (const auto& token : std::as_const(tokens)) {

Not sure if std::as_const() is strictly required though...

QListIterator<int> iter(filterColumns);
bool tokenMatch = false;
while (iter.hasNext() && !tokenMatch) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can also become a range based loop with a break statement below for early abbort.

int i = iter.next();
QModelIndex index = itemModel->index(sourceRow, i, sourceParent);
QString strData = itemModel->data(index).toString();
if (!strData.isEmpty()) {
QString tokNoQuotes = token;
tokNoQuotes.remove('\"');
if (strData.contains(tokNoQuotes, Qt::CaseInsensitive)) {
tokenMatch = true;
tokens.removeOne(token);
}
}
}
}
return rowMatches;

if (tokens.length() > 0) {
return false;
}
return true;
}

QString ProxyTrackModel::getModelSetting(const QString& name) {
Expand Down
2 changes: 1 addition & 1 deletion src/library/recording/dlgrecording.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ DlgRecording::DlgRecording(
parent->getTrackTableBackgroundColorOpacity(),
true)),
m_browseModel(this, pLibrary->trackCollectionManager(), pRecordingManager),
m_proxyModel(&m_browseModel),
m_proxyModel(&m_browseModel, true),
m_bytesRecordedStr("--"),
m_durationRecordedStr("--:--"),
m_pRecordingManager(pRecordingManager) {
Expand Down
2 changes: 1 addition & 1 deletion src/library/recording/recordingfeature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,6 @@ void RecordingFeature::bindLibraryWidget(WLibrary* pLibraryWidget,
void RecordingFeature::activate() {
emit refreshBrowseModel();
emit switchToView(kViewName);
emit disableSearch();
emit requestRestoreSearch();
emit enableCoverArtDisplay(false);
}