Add QuickFilter to RelocsWidget (#452)

This commit is contained in:
Paul I 2018-04-24 21:40:40 +03:00 committed by xarkes
parent a815f8f182
commit d58e69d411
7 changed files with 133 additions and 58 deletions

View File

@ -78,8 +78,7 @@ void ExportsModel::endReloadExports()
endResetModel();
}
ExportsSortFilterProxyModel::ExportsSortFilterProxyModel(ExportsModel *source_model,
QObject *parent)
ExportsProxyModel::ExportsProxyModel(ExportsModel *source_model, QObject *parent)
: QSortFilterProxyModel(parent)
{
setSourceModel(source_model);
@ -87,40 +86,39 @@ ExportsSortFilterProxyModel::ExportsSortFilterProxyModel(ExportsModel *source_mo
setSortCaseSensitivity(Qt::CaseInsensitive);
}
bool ExportsSortFilterProxyModel::filterAcceptsRow(int row, const QModelIndex &parent) const
bool ExportsProxyModel::filterAcceptsRow(int row, const QModelIndex &parent) const
{
QModelIndex index = sourceModel()->index(row, 0, parent);
ExportDescription exp = index.data(ExportsModel::ExportDescriptionRole).value<ExportDescription>();
auto exp = index.data(ExportsModel::ExportDescriptionRole).value<ExportDescription>();
return exp.name.contains(filterRegExp());
}
bool ExportsSortFilterProxyModel::lessThan(const QModelIndex &left, const QModelIndex &right) const
bool ExportsProxyModel::lessThan(const QModelIndex &left, const QModelIndex &right) const
{
ExportDescription left_exp = left.data(
ExportsModel::ExportDescriptionRole).value<ExportDescription>();
ExportDescription right_exp = right.data(
ExportsModel::ExportDescriptionRole).value<ExportDescription>();
auto leftExp = left.data(ExportsModel::ExportDescriptionRole).value<ExportDescription>();
auto rightExp = right.data(ExportsModel::ExportDescriptionRole).value<ExportDescription>();
switch (left.column()) {
case ExportsModel::SizeColumn:
if (left_exp.size != right_exp.size)
return left_exp.size < right_exp.size;
if (leftExp.size != rightExp.size)
return leftExp.size < rightExp.size;
// fallthrough
case ExportsModel::OffsetColumn:
if (left_exp.vaddr != right_exp.vaddr)
return left_exp.vaddr < right_exp.vaddr;
if (leftExp.vaddr != rightExp.vaddr)
return leftExp.vaddr < rightExp.vaddr;
// fallthrough
case ExportsModel::NameColumn:
return left_exp.name < right_exp.name;
return leftExp.name < rightExp.name;
case ExportsModel::TypeColumn:
if (left_exp.type != right_exp.type)
return left_exp.type < right_exp.type;
if (leftExp.type != rightExp.type)
return leftExp.type < rightExp.type;
default:
break;
}
// fallback
return left_exp.vaddr < right_exp.vaddr;
return leftExp.vaddr < rightExp.vaddr;
}
@ -132,7 +130,7 @@ ExportsWidget::ExportsWidget(MainWindow *main, QAction *action) :
ui->setupUi(this);
exportsModel = new ExportsModel(&exports, this);
exportsProxyModel = new ExportsSortFilterProxyModel(exportsModel, this);
exportsProxyModel = new ExportsProxyModel(exportsModel, this);
ui->exportsTreeView->setModel(exportsProxyModel);
ui->exportsTreeView->sortByColumn(ExportsModel::OffsetColumn, Qt::AscendingOrder);

View File

@ -32,7 +32,7 @@ public:
enum Column { OffsetColumn = 0, SizeColumn, TypeColumn, NameColumn, ColumnCount };
enum Role { ExportDescriptionRole = Qt::UserRole };
ExportsModel(QList<ExportDescription> *exports, QObject *parent = 0);
ExportsModel(QList<ExportDescription> *exports, QObject *parent = Q_NULLPTR);
int rowCount(const QModelIndex &parent = QModelIndex()) const;
int columnCount(const QModelIndex &parent = QModelIndex()) const;
@ -46,12 +46,12 @@ public:
class ExportsSortFilterProxyModel : public QSortFilterProxyModel
class ExportsProxyModel : public QSortFilterProxyModel
{
Q_OBJECT
public:
ExportsSortFilterProxyModel(ExportsModel *source_model, QObject *parent = 0);
ExportsProxyModel(ExportsModel *source_model, QObject *parent = Q_NULLPTR);
protected:
bool filterAcceptsRow(int row, const QModelIndex &parent) const override;
@ -77,7 +77,7 @@ private:
std::unique_ptr<Ui::ExportsWidget> ui;
ExportsModel *exportsModel;
ExportsSortFilterProxyModel *exportsProxyModel;
ExportsProxyModel *exportsProxyModel;
QList<ExportDescription> exports;
void setScrollMode();

View File

@ -90,7 +90,7 @@ void ImportsModel::endReload()
endResetModel();
}
ImportsSortFilterProxyModel::ImportsSortFilterProxyModel(ImportsModel *sourceModel, QObject *parent)
ImportsProxyModel::ImportsProxyModel(ImportsModel *sourceModel, QObject *parent)
: QSortFilterProxyModel(parent)
{
setSourceModel(sourceModel);
@ -98,30 +98,24 @@ ImportsSortFilterProxyModel::ImportsSortFilterProxyModel(ImportsModel *sourceMod
setSortCaseSensitivity(Qt::CaseInsensitive);
}
bool ImportsSortFilterProxyModel::filterAcceptsRow(int row, const QModelIndex &parent) const
bool ImportsProxyModel::filterAcceptsRow(int row, const QModelIndex &parent) const
{
QModelIndex index;
ImportDescription import;
index = sourceModel()->index(row, 0, parent);
import = index.data(ImportsModel::ImportDescriptionRole).value<ImportDescription>();
QModelIndex index = sourceModel()->index(row, 0, parent);
auto import = index.data(ImportsModel::ImportDescriptionRole).value<ImportDescription>();
return import.name.contains(filterRegExp());
}
bool ImportsSortFilterProxyModel::lessThan(const QModelIndex &left, const QModelIndex &right) const
bool ImportsProxyModel::lessThan(const QModelIndex &left, const QModelIndex &right) const
{
ImportDescription leftImport;
ImportDescription rightImport;
if (!left.isValid() || !right.isValid())
return false;
if (left.parent().isValid() || right.parent().isValid())
return false;
leftImport = left.data(ImportsModel::ImportDescriptionRole).value<ImportDescription>();
rightImport = right.data(ImportsModel::ImportDescriptionRole).value<ImportDescription>();
auto leftImport = left.data(ImportsModel::ImportDescriptionRole).value<ImportDescription>();
auto rightImport = right.data(ImportsModel::ImportDescriptionRole).value<ImportDescription>();
switch (left.column()) {
case ImportsModel::AddressColumn:
@ -147,7 +141,7 @@ ImportsWidget::ImportsWidget(MainWindow *main, QAction *action) :
CutterDockWidget(main, action),
ui(new Ui::ImportsWidget),
importsModel(new ImportsModel(&imports, this)),
importsProxyModel(new ImportsSortFilterProxyModel(importsModel, this))
importsProxyModel(new ImportsProxyModel(importsModel, this))
{
ui->setupUi(this);

View File

@ -57,12 +57,12 @@ public:
void endReload();
};
class ImportsSortFilterProxyModel : public QSortFilterProxyModel
class ImportsProxyModel : public QSortFilterProxyModel
{
Q_OBJECT
public:
ImportsSortFilterProxyModel(ImportsModel *sourceModel, QObject *parent = Q_NULLPTR);
ImportsProxyModel(ImportsModel *sourceModel, QObject *parent = Q_NULLPTR);
protected:
bool filterAcceptsRow(int row, const QModelIndex &parent) const override;
@ -85,7 +85,7 @@ private slots:
private:
std::unique_ptr<Ui::ImportsWidget> ui;
ImportsModel *importsModel;
ImportsSortFilterProxyModel *importsProxyModel;
ImportsProxyModel *importsProxyModel;
QList<ImportDescription> imports;
void highlightUnsafe();

View File

@ -11,12 +11,12 @@ RelocsModel::RelocsModel(QList<RelocDescription> *relocs, QObject *parent) :
int RelocsModel::rowCount(const QModelIndex &parent) const
{
return parent.isValid()? 0 : relocs->count();
return parent.isValid() ? 0 : relocs->count();
}
int RelocsModel::columnCount(const QModelIndex&) const
{
return COUNT;
return RelocsModel::ColumnCount;
}
QVariant RelocsModel::data(const QModelIndex &index, int role) const
@ -24,20 +24,22 @@ QVariant RelocsModel::data(const QModelIndex &index, int role) const
const RelocDescription &reloc = relocs->at(index.row());
switch (role)
{
case AddressRole:
return reloc.vaddr;
case Qt::DisplayRole:
switch (index.column())
{
case VADDR:
case RelocsModel::VAddrColumn:
return RAddressString(reloc.vaddr);
case TYPE:
case RelocsModel::TypeColumn:
return reloc.type;
case NAME:
case RelocsModel::NameColumn:
return reloc.name;
default:
break;
}
case RelocsModel::RelocDescriptionRole:
return QVariant::fromValue(reloc);
case RelocsModel::AddressRole:
return reloc.vaddr;
default:
break;
}
@ -46,14 +48,14 @@ QVariant RelocsModel::data(const QModelIndex &index, int role) const
QVariant RelocsModel::headerData(int section, Qt::Orientation, int role) const
{
if(role == Qt::DisplayRole)
if (role == Qt::DisplayRole)
switch (section)
{
case VADDR:
case RelocsModel::VAddrColumn:
return tr("Address");
case TYPE:
case RelocsModel::TypeColumn:
return tr("Type");
case NAME:
case RelocsModel::NameColumn:
return tr("Name");
}
return QVariant();
@ -69,15 +71,71 @@ void RelocsModel::endReload()
endResetModel();
}
RelocsProxyModel::RelocsProxyModel(RelocsModel *sourceModel, QObject *parent)
: QSortFilterProxyModel(parent)
{
setSourceModel(sourceModel);
setFilterCaseSensitivity(Qt::CaseInsensitive);
setSortCaseSensitivity(Qt::CaseInsensitive);
}
bool RelocsProxyModel::filterAcceptsRow(int row, const QModelIndex &parent) const
{
QModelIndex index = sourceModel()->index(row, 0, parent);
auto reloc = index.data(RelocsModel::RelocDescriptionRole).value<RelocDescription>();
return reloc.name.contains(filterRegExp());
}
bool RelocsProxyModel::lessThan(const QModelIndex &left, const QModelIndex &right) const
{
if (!left.isValid() || !right.isValid())
return false;
if (left.parent().isValid() || right.parent().isValid())
return false;
auto leftReloc = left.data(RelocsModel::RelocDescriptionRole).value<RelocDescription>();
auto rightReloc = right.data(RelocsModel::RelocDescriptionRole).value<RelocDescription>();
switch (left.column()) {
case RelocsModel::VAddrColumn:
return leftReloc.vaddr < rightReloc.vaddr;
case RelocsModel::TypeColumn:
return leftReloc.type < rightReloc.type;
case RelocsModel::NameColumn:
return leftReloc.name < rightReloc.name;
default:
break;
}
return false;
}
RelocsWidget::RelocsWidget(MainWindow *main, QAction *action) :
CutterDockWidget(main, action),
ui(new Ui::RelocsWidget),
model(new RelocsModel(&relocs, this))
relocsModel(new RelocsModel(&relocs, this)),
relocsProxyModel(new RelocsProxyModel(relocsModel, this))
{
ui->setupUi(this);
ui->relocsTreeView->setModel(model);
ui->relocsTreeView->setModel(relocsProxyModel);
ui->relocsTreeView->sortByColumn(RelocsModel::NameColumn, Qt::AscendingOrder);
// Ctrl-F to show/hide the filter entry
QShortcut *searchShortcut = new QShortcut(QKeySequence::Find, this);
connect(searchShortcut, &QShortcut::activated, ui->quickFilterView, &QuickFilterView::showFilter);
searchShortcut->setContext(Qt::WidgetWithChildrenShortcut);
// Esc to clear the filter entry
QShortcut *clearShortcut = new QShortcut(QKeySequence(Qt::Key_Escape), this);
connect(clearShortcut, &QShortcut::activated, ui->quickFilterView, &QuickFilterView::clearFilter);
clearShortcut->setContext(Qt::WidgetWithChildrenShortcut);
connect(ui->quickFilterView, SIGNAL(filterTextChanged(const QString &)),
relocsProxyModel, SLOT(setFilterWildcard(const QString &)));
connect(ui->quickFilterView, SIGNAL(filterClosed()), ui->relocsTreeView, SLOT(setFocus()));
setScrollMode();
@ -96,9 +154,9 @@ void RelocsWidget::on_relocsTreeView_doubleClicked(const QModelIndex &index)
void RelocsWidget::refreshRelocs()
{
model->beginReload();
relocsModel->beginReload();
relocs = Core()->getAllRelocs();
model->endReload();
relocsModel->endReload();
qhelpers::adjustColumns(ui->relocsTreeView, 3, 0);
}

View File

@ -3,6 +3,7 @@
#include <memory>
#include <QAbstractTableModel>
#include <QSortFilterProxyModel>
#include "CutterDockWidget.h"
#include "Cutter.h"
@ -21,10 +22,10 @@ private:
QList<RelocDescription> *relocs;
public:
enum COLUMNS {VADDR = 0, TYPE, NAME, COUNT};
static const int AddressRole = Qt::UserRole;
enum Column { VAddrColumn = 0, TypeColumn, NameColumn, ColumnCount };
enum Role { RelocDescriptionRole = Qt::UserRole, AddressRole };
RelocsModel(QList<RelocDescription> *relocs, QObject* parent = nullptr);
RelocsModel(QList<RelocDescription> *relocs, QObject* parent = Q_NULLPTR);
int rowCount(const QModelIndex &parent) const;
int columnCount(const QModelIndex &parent) const;
@ -36,6 +37,18 @@ public:
void endReload();
};
class RelocsProxyModel : public QSortFilterProxyModel
{
Q_OBJECT
public:
RelocsProxyModel(RelocsModel *sourceModel, QObject *parent = Q_NULLPTR);
protected:
bool filterAcceptsRow(int row, const QModelIndex &parent) const override;
bool lessThan(const QModelIndex &left, const QModelIndex &right) const override;
};
class RelocsWidget : public CutterDockWidget
{
Q_OBJECT
@ -50,7 +63,8 @@ private slots:
private:
std::unique_ptr<Ui::RelocsWidget> ui;
RelocsModel *model;
RelocsModel *relocsModel;
RelocsProxyModel *relocsProxyModel;
QList<RelocDescription> relocs;
void setScrollMode();

View File

@ -47,9 +47,20 @@
</property>
</widget>
</item>
<item>
<widget class="QuickFilterView" name="quickFilterView" native="true"/>
</item>
</layout>
</widget>
</widget>
<customwidgets>
<customwidget>
<class>QuickFilterView</class>
<extends>QWidget</extends>
<header>widgets/QuickFilterView.h</header>
<container>1</container>
</customwidget>
</customwidgets>
<resources/>
<connections/>
</ui>