cutter/src/widgets/ExportsWidget.cpp

169 lines
5.0 KiB
C++
Raw Normal View History

#include "ExportsWidget.h"
#include "ui_ExportsWidget.h"
2017-10-01 19:09:42 +00:00
#include "MainWindow.h"
#include "utils/Helpers.h"
ExportsModel::ExportsModel(QList<ExportDescription> *exports, QObject *parent)
2017-06-03 12:27:23 +00:00
: QAbstractListModel(parent),
exports(exports)
{
}
int ExportsModel::rowCount(const QModelIndex &) const
{
return exports->count();
}
int ExportsModel::columnCount(const QModelIndex &) const
{
return ExportsModel::ColumnCount;
}
QVariant ExportsModel::data(const QModelIndex &index, int role) const
{
2017-06-03 12:27:23 +00:00
if (index.row() >= exports->count())
return QVariant();
const ExportDescription &exp = exports->at(index.row());
2018-03-21 20:32:32 +00:00
switch (role) {
2017-06-03 12:27:23 +00:00
case Qt::DisplayRole:
2018-03-21 20:32:32 +00:00
switch (index.column()) {
case ExportsModel::OffsetColumn:
2017-06-03 12:27:23 +00:00
return RAddressString(exp.vaddr);
case ExportsModel::SizeColumn:
2017-06-03 12:27:23 +00:00
return RSizeString(exp.size);
case ExportsModel::TypeColumn:
2017-06-03 12:27:23 +00:00
return exp.type;
case ExportsModel::NameColumn:
2017-06-03 12:27:23 +00:00
return exp.name;
default:
return QVariant();
2017-06-03 12:27:23 +00:00
}
case ExportsModel::ExportDescriptionRole:
2017-06-03 12:27:23 +00:00
return QVariant::fromValue(exp);
default:
return QVariant();
}
}
QVariant ExportsModel::headerData(int section, Qt::Orientation, int role) const
{
2018-03-21 20:32:32 +00:00
switch (role) {
2017-06-03 12:27:23 +00:00
case Qt::DisplayRole:
2018-03-21 20:32:32 +00:00
switch (section) {
case ExportsModel::OffsetColumn:
2017-06-03 12:27:23 +00:00
return tr("Address");
case ExportsModel::SizeColumn:
2017-06-03 12:27:23 +00:00
return tr("Size");
case ExportsModel::TypeColumn:
2017-06-03 12:27:23 +00:00
return tr("Type");
case ExportsModel::NameColumn:
2017-06-03 12:27:23 +00:00
return tr("Name");
default:
return QVariant();
2017-06-03 12:27:23 +00:00
}
default:
return QVariant();
}
}
2018-04-24 18:40:40 +00:00
ExportsProxyModel::ExportsProxyModel(ExportsModel *source_model, QObject *parent)
2017-06-03 12:27:23 +00:00
: QSortFilterProxyModel(parent)
{
setSourceModel(source_model);
setFilterCaseSensitivity(Qt::CaseInsensitive);
setSortCaseSensitivity(Qt::CaseInsensitive);
}
2018-04-24 18:40:40 +00:00
bool ExportsProxyModel::filterAcceptsRow(int row, const QModelIndex &parent) const
{
QModelIndex index = sourceModel()->index(row, 0, parent);
2018-04-24 18:40:40 +00:00
auto exp = index.data(ExportsModel::ExportDescriptionRole).value<ExportDescription>();
return exp.name.contains(filterRegExp());
}
2018-04-24 18:40:40 +00:00
bool ExportsProxyModel::lessThan(const QModelIndex &left, const QModelIndex &right) const
{
2018-04-24 18:40:40 +00:00
auto leftExp = left.data(ExportsModel::ExportDescriptionRole).value<ExportDescription>();
auto rightExp = right.data(ExportsModel::ExportDescriptionRole).value<ExportDescription>();
2018-03-21 20:32:32 +00:00
switch (left.column()) {
case ExportsModel::SizeColumn:
2018-04-24 18:40:40 +00:00
if (leftExp.size != rightExp.size)
return leftExp.size < rightExp.size;
2017-06-03 12:27:23 +00:00
// fallthrough
case ExportsModel::OffsetColumn:
2018-04-24 18:40:40 +00:00
if (leftExp.vaddr != rightExp.vaddr)
return leftExp.vaddr < rightExp.vaddr;
2017-06-03 12:27:23 +00:00
// fallthrough
case ExportsModel::NameColumn:
2018-04-24 18:40:40 +00:00
return leftExp.name < rightExp.name;
case ExportsModel::TypeColumn:
2018-04-24 18:40:40 +00:00
if (leftExp.type != rightExp.type)
return leftExp.type < rightExp.type;
2017-06-03 12:27:23 +00:00
default:
break;
}
// fallback
2018-04-24 18:40:40 +00:00
return leftExp.vaddr < rightExp.vaddr;
}
ExportsWidget::ExportsWidget(MainWindow *main, QAction *action) :
CutterDockWidget(main, action),
ui(new Ui::ExportsWidget)
{
ui->setupUi(this);
exportsModel = new ExportsModel(&exports, this);
2018-04-24 18:40:40 +00:00
exportsProxyModel = new ExportsProxyModel(exportsModel, this);
ui->exportsTreeView->setModel(exportsProxyModel);
ui->exportsTreeView->sortByColumn(ExportsModel::OffsetColumn, 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 &)),
exportsProxyModel, SLOT(setFilterWildcard(const QString &)));
connect(ui->quickFilterView, SIGNAL(filterClosed()), ui->exportsTreeView, SLOT(setFocus()));
setScrollMode();
connect(Core(), SIGNAL(refreshAll()), this, SLOT(refreshExports()));
}
ExportsWidget::~ExportsWidget() {}
void ExportsWidget::refreshExports()
{
exportsModel->beginResetModel();
2018-04-12 06:33:30 +00:00
exports = Core()->getAllExports();
exportsModel->endResetModel();
qhelpers::adjustColumns(ui->exportsTreeView, 3, 0);
}
void ExportsWidget::setScrollMode()
{
qhelpers::setVerticalScrollMode(ui->exportsTreeView);
}
void ExportsWidget::on_exportsTreeView_doubleClicked(const QModelIndex &index)
{
if (!index.isValid())
return;
ExportDescription exp = index.data(ExportsModel::ExportDescriptionRole).value<ExportDescription>();
2018-04-12 06:33:30 +00:00
Core()->seek(exp.vaddr);
}