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(); endResetModel();
} }
ExportsSortFilterProxyModel::ExportsSortFilterProxyModel(ExportsModel *source_model, ExportsProxyModel::ExportsProxyModel(ExportsModel *source_model, QObject *parent)
QObject *parent)
: QSortFilterProxyModel(parent) : QSortFilterProxyModel(parent)
{ {
setSourceModel(source_model); setSourceModel(source_model);
@ -87,40 +86,39 @@ ExportsSortFilterProxyModel::ExportsSortFilterProxyModel(ExportsModel *source_mo
setSortCaseSensitivity(Qt::CaseInsensitive); 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); 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()); 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( auto leftExp = left.data(ExportsModel::ExportDescriptionRole).value<ExportDescription>();
ExportsModel::ExportDescriptionRole).value<ExportDescription>(); auto rightExp = right.data(ExportsModel::ExportDescriptionRole).value<ExportDescription>();
ExportDescription right_exp = right.data(
ExportsModel::ExportDescriptionRole).value<ExportDescription>();
switch (left.column()) { switch (left.column()) {
case ExportsModel::SizeColumn: case ExportsModel::SizeColumn:
if (left_exp.size != right_exp.size) if (leftExp.size != rightExp.size)
return left_exp.size < right_exp.size; return leftExp.size < rightExp.size;
// fallthrough // fallthrough
case ExportsModel::OffsetColumn: case ExportsModel::OffsetColumn:
if (left_exp.vaddr != right_exp.vaddr) if (leftExp.vaddr != rightExp.vaddr)
return left_exp.vaddr < right_exp.vaddr; return leftExp.vaddr < rightExp.vaddr;
// fallthrough // fallthrough
case ExportsModel::NameColumn: case ExportsModel::NameColumn:
return left_exp.name < right_exp.name; return leftExp.name < rightExp.name;
case ExportsModel::TypeColumn: case ExportsModel::TypeColumn:
if (left_exp.type != right_exp.type) if (leftExp.type != rightExp.type)
return left_exp.type < right_exp.type; return leftExp.type < rightExp.type;
default: default:
break; break;
} }
// fallback // 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); ui->setupUi(this);
exportsModel = new ExportsModel(&exports, this); exportsModel = new ExportsModel(&exports, this);
exportsProxyModel = new ExportsSortFilterProxyModel(exportsModel, this); exportsProxyModel = new ExportsProxyModel(exportsModel, this);
ui->exportsTreeView->setModel(exportsProxyModel); ui->exportsTreeView->setModel(exportsProxyModel);
ui->exportsTreeView->sortByColumn(ExportsModel::OffsetColumn, Qt::AscendingOrder); ui->exportsTreeView->sortByColumn(ExportsModel::OffsetColumn, Qt::AscendingOrder);

View File

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

View File

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

View File

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

View File

@ -16,7 +16,7 @@ int RelocsModel::rowCount(const QModelIndex &parent) const
int RelocsModel::columnCount(const QModelIndex&) const int RelocsModel::columnCount(const QModelIndex&) const
{ {
return COUNT; return RelocsModel::ColumnCount;
} }
QVariant RelocsModel::data(const QModelIndex &index, int role) const 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()); const RelocDescription &reloc = relocs->at(index.row());
switch (role) switch (role)
{ {
case AddressRole:
return reloc.vaddr;
case Qt::DisplayRole: case Qt::DisplayRole:
switch (index.column()) switch (index.column())
{ {
case VADDR: case RelocsModel::VAddrColumn:
return RAddressString(reloc.vaddr); return RAddressString(reloc.vaddr);
case TYPE: case RelocsModel::TypeColumn:
return reloc.type; return reloc.type;
case NAME: case RelocsModel::NameColumn:
return reloc.name; return reloc.name;
default: default:
break; break;
} }
case RelocsModel::RelocDescriptionRole:
return QVariant::fromValue(reloc);
case RelocsModel::AddressRole:
return reloc.vaddr;
default: default:
break; break;
} }
@ -49,11 +51,11 @@ QVariant RelocsModel::headerData(int section, Qt::Orientation, int role) const
if (role == Qt::DisplayRole) if (role == Qt::DisplayRole)
switch (section) switch (section)
{ {
case VADDR: case RelocsModel::VAddrColumn:
return tr("Address"); return tr("Address");
case TYPE: case RelocsModel::TypeColumn:
return tr("Type"); return tr("Type");
case NAME: case RelocsModel::NameColumn:
return tr("Name"); return tr("Name");
} }
return QVariant(); return QVariant();
@ -69,15 +71,71 @@ void RelocsModel::endReload()
endResetModel(); 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) : RelocsWidget::RelocsWidget(MainWindow *main, QAction *action) :
CutterDockWidget(main, action), CutterDockWidget(main, action),
ui(new Ui::RelocsWidget), ui(new Ui::RelocsWidget),
model(new RelocsModel(&relocs, this)) relocsModel(new RelocsModel(&relocs, this)),
relocsProxyModel(new RelocsProxyModel(relocsModel, this))
{ {
ui->setupUi(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(); setScrollMode();
@ -96,9 +154,9 @@ void RelocsWidget::on_relocsTreeView_doubleClicked(const QModelIndex &index)
void RelocsWidget::refreshRelocs() void RelocsWidget::refreshRelocs()
{ {
model->beginReload(); relocsModel->beginReload();
relocs = Core()->getAllRelocs(); relocs = Core()->getAllRelocs();
model->endReload(); relocsModel->endReload();
qhelpers::adjustColumns(ui->relocsTreeView, 3, 0); qhelpers::adjustColumns(ui->relocsTreeView, 3, 0);
} }

View File

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

View File

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