cutter/src/widgets/ClassesWidget.cpp

275 lines
7.5 KiB
C++
Raw Normal View History

2017-12-23 16:42:42 +00:00
#include <QList>
#include "ClassesWidget.h"
#include "MainWindow.h"
2017-12-23 16:42:42 +00:00
#include "ui_ClassesWidget.h"
#include "utils/Helpers.h"
ClassesModel::ClassesModel(QList<ClassDescription> *classes, QObject *parent)
: QAbstractItemModel(parent),
classes(classes)
{
}
QModelIndex ClassesModel::index(int row, int column, const QModelIndex &parent) const
{
if (!parent.isValid())
return createIndex(row, column, (quintptr)0); // root function nodes have id = 0
return createIndex(row, column, (quintptr)parent.row() + 1); // sub-nodes have id = class index + 1
}
QModelIndex ClassesModel::parent(const QModelIndex &index) const
{
if (!index.isValid() || index.column() != 0)
return QModelIndex();
if (index.internalId() == 0) // root function node
return QModelIndex();
else // sub-node
return this->index((int)(index.internalId() - 1), 0);
}
int ClassesModel::rowCount(const QModelIndex &parent) const
{
2018-03-21 20:32:32 +00:00
if (!parent.isValid()) { // root
2017-12-23 16:42:42 +00:00
return classes->count();
}
2018-03-21 20:32:32 +00:00
if (parent.internalId() == 0) { // methods/fields
2017-12-23 16:42:42 +00:00
const ClassDescription *cls = &classes->at(parent.row());
return cls->methods.length() + cls->fields.length();
}
return 0; // below methods/fields
}
int ClassesModel::columnCount(const QModelIndex &) const
{
return Columns::COUNT;
}
QVariant ClassesModel::data(const QModelIndex &index, int role) const
{
const ClassDescription *cls;
const ClassMethodDescription *meth = nullptr;
const ClassFieldDescription *field = nullptr;
2018-03-21 20:32:32 +00:00
if (index.internalId() == 0) { // class row
if (index.row() >= classes->count()) {
2017-12-23 16:42:42 +00:00
return QVariant();
}
cls = &classes->at(index.row());
2018-03-21 20:32:32 +00:00
} else { // method/field row
2017-12-23 16:42:42 +00:00
cls = &classes->at(static_cast<int>(index.internalId() - 1));
2018-03-21 20:32:32 +00:00
if (index.row() >= cls->methods.length() + cls->fields.length()) {
2017-12-23 16:42:42 +00:00
return QVariant();
}
2018-03-21 20:32:32 +00:00
if (index.row() < cls->methods.length()) {
2017-12-23 16:42:42 +00:00
meth = &cls->methods[index.row()];
2018-03-21 20:32:32 +00:00
} else {
2017-12-23 16:42:42 +00:00
field = &cls->fields[index.row() - cls->methods.length()];
}
}
2018-03-21 20:32:32 +00:00
if (meth) {
switch (role) {
case Qt::DisplayRole:
switch (index.column()) {
case NAME:
2017-12-23 16:42:42 +00:00
return meth->name;
2018-03-21 20:32:32 +00:00
case TYPE:
return tr("method");
case OFFSET:
return RAddressString(meth->addr);
2017-12-23 16:42:42 +00:00
default:
return QVariant();
2018-03-21 20:32:32 +00:00
}
case OffsetRole:
return QVariant::fromValue(meth->addr);
case NameRole:
return meth->name;
case TypeRole:
return QVariant::fromValue(METHOD);
default:
return QVariant();
2017-12-23 16:42:42 +00:00
}
2018-03-21 20:32:32 +00:00
} else if (field) {
switch (role) {
case Qt::DisplayRole:
switch (index.column()) {
case NAME:
2017-12-23 16:42:42 +00:00
return field->name;
2018-03-21 20:32:32 +00:00
case TYPE:
return tr("field");
case OFFSET:
return RAddressString(field->addr);
2017-12-23 16:42:42 +00:00
default:
return QVariant();
2018-03-21 20:32:32 +00:00
}
case OffsetRole:
return QVariant::fromValue(field->addr);
case NameRole:
return field->name;
case TypeRole:
return QVariant::fromValue(FIELD);
default:
return QVariant();
2017-12-23 16:42:42 +00:00
}
2018-03-21 20:32:32 +00:00
} else {
switch (role) {
case Qt::DisplayRole:
switch (index.column()) {
case NAME:
2017-12-23 16:42:42 +00:00
return cls->name;
2018-03-21 20:32:32 +00:00
case TYPE:
return tr("class");
case OFFSET:
return RAddressString(cls->addr);
2017-12-23 16:42:42 +00:00
default:
return QVariant();
2018-03-21 20:32:32 +00:00
}
case OffsetRole:
return QVariant::fromValue(cls->addr);
case NameRole:
return cls->name;
case TypeRole:
return QVariant::fromValue(CLASS);
default:
return QVariant();
2017-12-23 16:42:42 +00:00
}
}
}
QVariant ClassesModel::headerData(int section, Qt::Orientation, int role) const
{
2018-03-21 20:32:32 +00:00
switch (role) {
2017-12-23 16:42:42 +00:00
case Qt::DisplayRole:
2018-03-21 20:32:32 +00:00
switch (section) {
2017-12-23 16:42:42 +00:00
case NAME:
return tr("Name");
case TYPE:
return tr("Type");
case OFFSET:
return tr("Offset");
default:
return QVariant();
}
default:
return QVariant();
}
}
void ClassesModel::beginReload()
{
beginResetModel();
}
void ClassesModel::endReload()
{
endResetModel();
}
2018-03-21 20:32:32 +00:00
ClassesSortFilterProxyModel::ClassesSortFilterProxyModel(ClassesModel *source_model,
QObject *parent)
2017-12-23 16:42:42 +00:00
: QSortFilterProxyModel(parent)
{
setSourceModel(source_model);
}
bool ClassesSortFilterProxyModel::filterAcceptsRow(int row, const QModelIndex &parent) const
{
QModelIndex index = sourceModel()->index(row, 0, parent);
return index.data(ClassesModel::NameRole).toString().contains(filterRegExp());
}
bool ClassesSortFilterProxyModel::lessThan(const QModelIndex &left, const QModelIndex &right) const
{
2018-03-21 20:32:32 +00:00
switch (left.column()) {
case ClassesModel::OFFSET: {
2017-12-23 16:42:42 +00:00
RVA left_offset = left.data(ClassesModel::OffsetRole).toULongLong();
RVA right_offset = right.data(ClassesModel::OffsetRole).toULongLong();
if (left_offset != right_offset)
return left_offset < right_offset;
}
// fallthrough
2018-03-21 20:32:32 +00:00
case ClassesModel::TYPE: {
2017-12-23 16:42:42 +00:00
auto left_type = left.data(ClassesModel::TypeRole).value<ClassesModel::RowType>();
auto right_type = right.data(ClassesModel::TypeRole).value<ClassesModel::RowType>();
if (left_type != right_type)
return left_type < right_type;
}
// fallthrough
case ClassesModel::NAME:
default:
QString left_name = left.data(ClassesModel::NameRole).toString();
QString right_name = right.data(ClassesModel::NameRole).toString();
return left_name < right_name;
}
}
ClassesWidget::ClassesWidget(MainWindow *main, QAction *action) :
CutterDockWidget(main, action),
2018-02-04 14:32:18 +00:00
ui(new Ui::ClassesWidget)
2017-12-23 16:42:42 +00:00
{
ui->setupUi(this);
model = new ClassesModel(&classes, this);
proxy_model = new ClassesSortFilterProxyModel(model, this);
ui->classesTreeView->setModel(proxy_model);
ui->classesTreeView->sortByColumn(ClassesModel::TYPE, Qt::AscendingOrder);
connect(Core(), SIGNAL(refreshAll()), this, SLOT(refreshClasses()));
2018-03-11 15:57:38 +00:00
connect(Core(), SIGNAL(flagsChanged()), this, SLOT(flagsChanged()));
connect(ui->classSourceCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(refreshClasses()));
2017-12-23 16:42:42 +00:00
}
ClassesWidget::~ClassesWidget() {}
2018-03-11 15:57:38 +00:00
ClassesWidget::Source ClassesWidget::getSource()
{
2018-03-21 20:32:32 +00:00
if (ui->classSourceCombo->currentIndex() == 1) {
2018-03-11 15:57:38 +00:00
return Source::FLAGS;
2018-03-21 20:32:32 +00:00
} else {
2018-03-11 15:57:38 +00:00
return Source::BIN;
}
}
void ClassesWidget::flagsChanged()
{
2018-03-21 20:32:32 +00:00
if (getSource() == Source::FLAGS) {
2018-03-11 15:57:38 +00:00
refreshClasses();
}
}
2017-12-23 16:42:42 +00:00
void ClassesWidget::refreshClasses()
{
model->beginReload();
2018-03-11 15:57:38 +00:00
classes = getSource() == Source::BIN
? CutterCore::getInstance()->getAllClassesFromBin()
: CutterCore::getInstance()->getAllClassesFromFlags();
2017-12-23 16:42:42 +00:00
model->endReload();
ui->classesTreeView->resizeColumnToContents(0);
ui->classesTreeView->resizeColumnToContents(1);
ui->classesTreeView->resizeColumnToContents(2);
ui->classesTreeView->setColumnWidth(0, 200);
}
void ClassesWidget::on_classesTreeView_doubleClicked(const QModelIndex &index)
{
RVA offset = index.data(ClassesModel::OffsetRole).value<RVA>();
CutterCore::getInstance()->seek(offset);
}