Add sorting options to resource inspector, including 'recently used'

This commit is contained in:
baldurk
2021-10-18 17:11:36 +01:00
parent 6dd63c6aec
commit 469221ab15
3 changed files with 94 additions and 2 deletions
+55 -1
View File
@@ -33,6 +33,7 @@
static const int ResourceIdRole = Qt::UserRole;
static const int FilterRole = Qt::UserRole + 1;
static const int LastAccessSortRole = Qt::UserRole + 2;
class ResourceListItemModel : public QAbstractItemModel
{
@@ -48,6 +49,13 @@ public:
emit endResetModel();
}
void bumpLastUse(ResourceId id) { m_LastUse[id] = ++m_LastUseIdx; }
void resetLastUse()
{
m_LastUseIdx = 1;
m_LastUse.clear();
}
QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override
{
if(row < 0 || row >= rowCount())
@@ -88,6 +96,9 @@ public:
if(role == FilterRole)
return ToQStr(desc.type) + lit(" ") + m_Ctx.GetResourceName(desc.resourceId);
if(role == LastAccessSortRole)
return m_LastUse[desc.resourceId];
}
}
@@ -96,8 +107,34 @@ public:
private:
ICaptureContext &m_Ctx;
QMap<ResourceId, uint32_t> m_LastUse;
uint32_t m_LastUseIdx = 1;
};
bool ResourceSorterModel::lessThan(const QModelIndex &source_left, const QModelIndex &source_right) const
{
if(m_Sort == SortType::Creation)
{
return source_left.data(ResourceIdRole).value<ResourceId>() <
source_right.data(ResourceIdRole).value<ResourceId>();
}
else if(m_Sort == SortType::LastAccess)
{
uint a = source_left.data(LastAccessSortRole).toUInt();
uint b = source_right.data(LastAccessSortRole).toUInt();
// if they're different, sort by access. Otherwise fall through to alphabetical
// we invert the reason, so that high values (recent access) are first
if(a != b)
return a > b;
return QCollatorSortFilterProxyModel::lessThan(source_left, source_right);
}
return QCollatorSortFilterProxyModel::lessThan(source_left, source_right);
}
ResourceInspector::ResourceInspector(ICaptureContext &ctx, QWidget *parent)
: QFrame(parent), ui(new Ui::ResourceInspector), m_Ctx(ctx)
{
@@ -113,7 +150,7 @@ ResourceInspector::ResourceInspector(ICaptureContext &ctx, QWidget *parent)
m_ResourceModel = new ResourceListItemModel(this, m_Ctx);
m_FilterModel = new QCollatorSortFilterProxyModel(this);
m_FilterModel = new ResourceSorterModel(this);
m_FilterModel->setSourceModel(m_ResourceModel);
m_FilterModel->setFilterCaseSensitivity(Qt::CaseInsensitive);
m_FilterModel->setFilterRole(FilterRole);
@@ -121,6 +158,10 @@ ResourceInspector::ResourceInspector(ICaptureContext &ctx, QWidget *parent)
m_FilterModel->collator()->setNumericMode(true);
m_FilterModel->collator()->setCaseSensitivity(Qt::CaseInsensitive);
ui->sortType->addItems(
{tr("Sort alphabetically"), tr("Sort by creation time"), tr("Sort by recently viewed")});
ui->sortType->adjustSize();
ui->resourceList->setModel(m_FilterModel);
m_ChunksModel = new StructuredDataItemModel(this);
@@ -193,6 +234,8 @@ ResourceInspector::ResourceInspector(ICaptureContext &ctx, QWidget *parent)
vertical->addWidget(ui->titleWidget);
vertical->addWidget(ui->dockarea);
ui->resourceListFilter->setPlaceholderText(tr("Filter..."));
Inspect(ResourceId());
m_Ctx.AddCaptureViewer(this);
@@ -229,6 +272,11 @@ void ResourceInspector::Inspect(ResourceId id)
m_ResourceModel->reset();
}
m_ResourceModel->bumpLastUse(id);
m_FilterModel->invalidate();
m_FilterModel->sort(0);
if(m_Ctx.HasResourceCustomName(id))
ui->resetName->show();
else
@@ -420,6 +468,7 @@ void ResourceInspector::OnCaptureClosed()
ui->viewContents->hide();
m_ResourceModel->reset();
m_ResourceModel->resetLastUse();
m_ChunksModel->setObjects({});
ui->initChunks->clearInternalExpansions();
@@ -493,6 +542,11 @@ void ResourceInspector::on_resetName_clicked()
Inspect(id);
}
void ResourceInspector::on_sortType_currentIndexChanged(int index)
{
m_FilterModel->setSortType((ResourceSorterModel::SortType)index);
}
void ResourceInspector::on_cancelResourceListFilter_clicked()
{
ui->resourceListFilter->setText(QString());
+36 -1
View File
@@ -26,6 +26,7 @@
#include <QFrame>
#include "Code/Interface/QRDInterface.h"
#include "Code/QRDUtils.h"
namespace Ui
{
@@ -39,6 +40,39 @@ class ResourceListItemModel;
class StructuredDataItemModel;
class RichTextViewDelegate;
class ResourceSorterModel : public QCollatorSortFilterProxyModel
{
Q_OBJECT
public:
enum SortType
{
Alphabetical = 0,
Creation,
LastAccess,
};
explicit ResourceSorterModel(QObject *parent = Q_NULLPTR) : QCollatorSortFilterProxyModel(parent)
{
}
virtual ~ResourceSorterModel() {}
void setSortType(SortType type)
{
if(m_Sort != type)
{
m_Sort = type;
invalidate();
sort(0);
}
}
protected:
virtual bool lessThan(const QModelIndex &source_left,
const QModelIndex &source_right) const override;
private:
SortType m_Sort = SortType::Alphabetical;
};
class ResourceInspector : public QFrame, public IResourceInspector, public ICaptureViewer
{
Q_OBJECT
@@ -62,6 +96,7 @@ public slots:
void on_renameResource_clicked();
void on_resourceNameEdit_keyPress(QKeyEvent *event);
void on_resetName_clicked();
void on_sortType_currentIndexChanged(int index);
void on_cancelResourceListFilter_clicked();
void on_resourceListFilter_textChanged(const QString &text);
@@ -89,7 +124,7 @@ private:
ResourceId m_Resource;
ResourceListItemModel *m_ResourceModel;
int m_ResourceCacheID = -1;
QCollatorSortFilterProxyModel *m_FilterModel;
ResourceSorterModel *m_FilterModel;
StructuredDataItemModel *m_ChunksModel;
RichTextViewDelegate *m_delegate;
};
+3
View File
@@ -75,6 +75,9 @@
</item>
</layout>
</item>
<item>
<widget class="QComboBox" name="sortType"/>
</item>
<item>
<widget class="RDListView" name="resourceList">
<property name="frameShape">