Skip to content

Commit

Permalink
[ui] Various selection and tree model improvements and fixes
Browse files Browse the repository at this point in the history
 - Auto select the first learned address on the learn dialogs
 - Auto select the first process on the process library
 - Fix that filtering in the library would sometimes show up the whole root partition / C drive
  • Loading branch information
jcelerier committed Jul 30, 2022
1 parent bdedc4b commit 97017b2
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,15 @@ class LearnWidget : public QListWidget {
{
const auto& [emplaced_it, value] = messages.emplace(addr.address, as->value);
auto dist = std::distance(messages.begin(), emplaced_it);
this->insertItem(dist, new QListWidgetItem{textFromMessage(addr.address, as->value)});
auto new_item = new QListWidgetItem{textFromMessage(addr.address, as->value)};
this->insertItem(dist, new_item);
this->scrollToBottom();

// Select the first message to be learned
if(messages.size() == 1)
{
new_item->setSelected(true);
}
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,16 @@ struct ItemModelFilterLineEdit final : public score::SearchLineEdit
if (!text().isEmpty())
{
m_view.expandAll();

if(m_proxy.hasChildren())
{
auto item_to_select = QModelIndex();
while(m_proxy.hasChildren(item_to_select))
{
item_to_select = m_proxy.index(0, 0, item_to_select);
}
m_view.setCurrentIndex(item_to_select);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ ProjectLibraryWidget::ProjectLibraryWidget(
QWidget* parent)
: QWidget{parent}
, m_model{new FileSystemModel{ctx, this}}
, m_proxy{new RecursiveFilterProxy{this}}
, m_proxy{new FileSystemRecursiveFilterProxy{this}}
{
auto lay = new score::MarginLess<QVBoxLayout>;
setStatusTip(QObject::tr(
Expand Down
53 changes: 49 additions & 4 deletions src/plugins/score-plugin-library/Library/RecursiveFilterProxy.hpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
#pragma once
#include <score/tools/Debug.hpp>

#include <QFileSystemModel>
#include <QSortFilterProxyModel>

namespace Library
{
class RecursiveFilterProxy final : public QSortFilterProxyModel
class RecursiveFilterProxy : public QSortFilterProxyModel
{
public:
using QSortFilterProxyModel::QSortFilterProxyModel;

private:
protected:
bool
filterAcceptsRow(int srcRow, const QModelIndex& srcParent) const override
{
Expand All @@ -34,10 +35,11 @@ class RecursiveFilterProxy final : public QSortFilterProxyModel
bool filterAcceptsRowItself(int srcRow, const QModelIndex& srcParent) const
{
QModelIndex index = sourceModel()->index(srcRow, 0, srcParent);
const QVariant& data = sourceModel()->data(index);
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
return sourceModel()->data(index).toString().contains(filterRegExp());
return data.toString().contains(filterRegExp());
#else
return sourceModel()->data(index).toString().contains(
return data.toString().contains(
filterRegularExpression());
#endif
}
Expand Down Expand Up @@ -67,4 +69,47 @@ class RecursiveFilterProxy final : public QSortFilterProxyModel
return false;
}
};

class FileSystemRecursiveFilterProxy final : public RecursiveFilterProxy
{
public:
using RecursiveFilterProxy::RecursiveFilterProxy;

QString fixedRoot{};
private:
QFileSystemModel* sourceModel() const noexcept
{ return static_cast<QFileSystemModel*>(QSortFilterProxyModel::sourceModel());}
bool
filterAcceptsRow(int srcRow, const QModelIndex& srcParent) const override
{
QModelIndex index = sourceModel()->index(srcRow, 0, srcParent);
if(!sourceModel()->filePath(index).startsWith(fixedRoot))
{
return false;
}

if (filterAcceptsRowItself(srcRow, srcParent))
{
return true;
}

QModelIndex root_index = sourceModel()->index(fixedRoot);
// Accept if any of the parents is accepted on its own
for (QModelIndex parent = srcParent; parent.isValid();
parent = parent.parent())
{
// We went up to the root. We know that we are a child due to the startsWith.
if (parent == root_index)
return false;

if (filterAcceptsRowItself(parent.row(), parent.parent()))
{
return true;
}
}

// Accept if any of the children is accepted on its own
return hasAcceptedChildren(srcRow, srcParent);
}
};
}
18 changes: 14 additions & 4 deletions src/plugins/score-plugin-library/Library/SystemLibraryWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ SystemLibraryWidget::SystemLibraryWidget(
QWidget* parent)
: QWidget{parent}
, m_model{new FileSystemModel{ctx, this}}
, m_proxy{new QSortFilterProxyModel{this}}
, m_proxy{new FileSystemRecursiveFilterProxy{this}}
, m_preview{this}
{
#if QT_VERSION >= QT_VERSION_CHECK(5, 10, 0)
Expand Down Expand Up @@ -142,9 +142,19 @@ SystemLibraryWidget::~SystemLibraryWidget() { }
void SystemLibraryWidget::setRoot(QString path)
{
auto idx = m_model->setRootPath(path);
m_tv.setRootIndex(m_proxy->mapFromSource(idx));
for (int i = 1; i < m_model->columnCount(); ++i)
m_tv.hideColumn(i);
((FileSystemRecursiveFilterProxy*)m_proxy)->fixedRoot = path;
if(idx.isValid())
{
m_tv.setRootIndex(m_proxy->mapFromSource(idx));
for (int i = 1; i < m_model->columnCount(); ++i)
m_tv.hideColumn(i);

m_tv.setEnabled(true);
}
else
{
m_tv.setEnabled(false);
}
}

}

0 comments on commit 97017b2

Please sign in to comment.