Skip to content
Permalink
Browse files

Reimplemented GotoModel

  • Loading branch information...
Dax89 committed May 16, 2019
1 parent 9da5456 commit 7d59634420b3b9cbb7511555ccc011a58cf5bb71
@@ -1,12 +1,11 @@
#include "gotodialog.h"
#include "ui_gotodialog.h"
#include "../../models/gotomodel.h"

GotoDialog::GotoDialog(const REDasm::DisassemblerPtr& disassembler, QWidget *parent) : QDialog(parent), ui(new Ui::GotoDialog), m_disassembler(disassembler), m_address(0), m_validaddress(false)
{
ui->setupUi(this);

m_gotomodel = ListingFilterModel::createFilter<GotoModel>(ui->tvFunctions);
m_gotomodel = new GotoFilterModel(ui->tvFunctions);
m_gotomodel->setDisassembler(disassembler);

ui->tvFunctions->setModel(m_gotomodel);
@@ -16,7 +15,7 @@ GotoDialog::GotoDialog(const REDasm::DisassemblerPtr& disassembler, QWidget *par
connect(ui->leAddress, &QLineEdit::textChanged, this, [=](const QString) { this->validateEntry(); });
connect(ui->leAddress, &QLineEdit::returnPressed, this, &GotoDialog::accept);

connect(ui->tvFunctions, &QTableView::doubleClicked, this, &GotoDialog::onSymbolSelected);
connect(ui->tvFunctions, &QTableView::doubleClicked, this, &GotoDialog::onItemSelected);
connect(ui->tvFunctions, &QTableView::doubleClicked, this, &GotoDialog::accept);

connect(ui->pbGoto, &QPushButton::clicked, this, &GotoDialog::accept);
@@ -35,18 +34,18 @@ void GotoDialog::validateEntry()
{
m_validaddress = false;
ui->pbGoto->setEnabled(false);
m_gotomodel->clearFilter();
m_gotomodel->setFilterFixedString(QString());
return;
}

m_address = s.toULongLong(&ok, 16);
ui->pbGoto->setEnabled(ok);
m_validaddress = ok;
m_gotomodel->setFilter(s);
m_gotomodel->setFilterFixedString(s);
}

void GotoDialog::onSymbolSelected(const QModelIndex &index)
void GotoDialog::onItemSelected(const QModelIndex &index)
{
m_validaddress = false;
emit symbolSelected(index);
emit symbolSelected(m_gotomodel->mapToSource(index));
}
@@ -3,7 +3,7 @@

#include <QDialog>
#include <redasm/disassembler/disassemblerapi.h>
#include "../../models/listingfiltermodel.h"
#include "../../models/gotomodel/gotofiltermodel.h"

namespace Ui {
class GotoDialog;
@@ -23,7 +23,7 @@ class GotoDialog : public QDialog
void validateEntry();

private slots:
void onSymbolSelected(const QModelIndex& index);
void onItemSelected(const QModelIndex& index);

signals:
void gotoAddress(address_t address);
@@ -32,7 +32,7 @@ class GotoDialog : public QDialog
private:
Ui::GotoDialog *ui;
REDasm::DisassemblerPtr m_disassembler;
ListingFilterModel* m_gotomodel;
GotoFilterModel* m_gotomodel;
address_t m_address;
bool m_validaddress;
};
@@ -0,0 +1,32 @@
#include "gotofiltermodel.h"

GotoFilterModel::GotoFilterModel(QObject *parent) : QSortFilterProxyModel(parent)
{
this->setFilterKeyColumn(-1);
this->setFilterCaseSensitivity(Qt::CaseInsensitive);
this->setSourceModel(new GotoModel(this));
}

void GotoFilterModel::setDisassembler(const REDasm::DisassemblerPtr &disassembler) { static_cast<GotoModel*>(this->sourceModel())->setDisassembler(disassembler); }

bool GotoFilterModel::filterAcceptsRow(int sourcerow, const QModelIndex &sourceparent) const
{
const REDasm::ListingItem* item = reinterpret_cast<const REDasm::ListingItem*>(this->sourceModel()->index(sourcerow, 0, sourceparent).internalPointer());

if(!item)
return false;

switch(item->type)
{
case REDasm::ListingItem::SegmentItem:
case REDasm::ListingItem::FunctionItem:
case REDasm::ListingItem::SymbolItem:
case REDasm::ListingItem::TypeItem:
return QSortFilterProxyModel::filterAcceptsRow(sourcerow, sourceparent);

default:
break;
}

return false;
}
@@ -0,0 +1,19 @@
#ifndef GOTOFILTERMODEL_H
#define GOTOFILTERMODEL_H

#include <QSortFilterProxyModel>
#include "gotomodel.h"

class GotoFilterModel : public QSortFilterProxyModel
{
Q_OBJECT

public:
explicit GotoFilterModel(QObject *parent = nullptr);
void setDisassembler(const REDasm::DisassemblerPtr &disassembler);

protected:
bool filterAcceptsRow(int sourcerow, const QModelIndex &sourceparent) const override;
};

#endif // GOTOFILTERMODEL_H
@@ -1,16 +1,22 @@
#include "gotomodel.h"
#include "../themeprovider.h"
#include <redasm/plugins/loader.h>
#include "../../themeprovider.h"
#include <redasm/disassembler/disassembler.h>

GotoModel::GotoModel(QObject *parent) : ListingItemModel(REDasm::ListingItem::AllItems, parent) { }
GotoModel::~GotoModel() { EVENT_DISCONNECT(m_disassembler->document(), changed, this); }
GotoModel::GotoModel(QObject *parent) : DisassemblerModel(parent) { }

void GotoModel::setDisassembler(const REDasm::DisassemblerPtr &disassembler)
{
this->beginResetModel();
DisassemblerModel::setDisassembler(disassembler);
this->endResetModel();
}

QVariant GotoModel::data(const QModelIndex &index, int role) const
{
if(!m_disassembler)
return QVariant();

const REDasm::ListingItem* item = this->item(index);
const REDasm::ListingItem* item = reinterpret_cast<const REDasm::ListingItem*>(index.internalPointer());

if(!item)
return QVariant();
@@ -59,7 +65,16 @@ QVariant GotoModel::headerData(int section, Qt::Orientation orientation, int rol
return DisassemblerModel::headerData(section, orientation, role);
}

QModelIndex GotoModel::index(int row, int column, const QModelIndex &parent) const
{
if(!m_disassembler)
return QModelIndex();

return this->createIndex(row, column, m_disassembler->document()->itemAt(row));
}

int GotoModel::columnCount(const QModelIndex &) const { return 3; }
int GotoModel::rowCount(const QModelIndex &) const { return m_disassembler ? m_disassembler->document()->size() : 0; }

QColor GotoModel::itemColor(const REDasm::ListingItem *item) const
{
@@ -124,20 +139,3 @@ QString GotoModel::itemType(const REDasm::ListingItem *item) const

return QString();
}

bool GotoModel::isItemAllowed(const REDasm::ListingItem *item) const
{
switch(item->type)
{
case REDasm::ListingItem::SegmentItem:
case REDasm::ListingItem::FunctionItem:
case REDasm::ListingItem::SymbolItem:
case REDasm::ListingItem::TypeItem:
return true;

default:
break;
}

return false;
}
@@ -1,28 +1,27 @@
#ifndef GOTOMODEL_H
#define GOTOMODEL_H

#include "listingitemmodel.h"
#include "../listingitemmodel.h"

class GotoModel : public ListingItemModel
class GotoModel : public DisassemblerModel
{
Q_OBJECT

public:
explicit GotoModel(QObject *parent = nullptr);
~GotoModel();
void setDisassembler(const REDasm::DisassemblerPtr &disassembler) override;

public:
QVariant data(const QModelIndex &index, int role) const override;
QVariant headerData(int section, Qt::Orientation orientation, int role) const override;
QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override;
int columnCount(const QModelIndex&) const override;
int rowCount(const QModelIndex&) const override;

private:
QColor itemColor(const REDasm::ListingItem* item) const;
QString itemName(const REDasm::ListingItem* item) const;
QString itemType(const REDasm::ListingItem* item) const;

protected:
bool isItemAllowed(const REDasm::ListingItem* item) const override;
};

#endif // GOTOMODEL_H
@@ -484,6 +484,11 @@ const REDasm::ListingItem *DisassemblerView::itemFromIndex(const QModelIndex &in
if(filtermodel)
return filtermodel->item(index);

const GotoModel* gotomodel = dynamic_cast<const GotoModel*>(index.model());

if(gotomodel)
return reinterpret_cast<REDasm::ListingItem*>(index.internalPointer());

return nullptr;
}

0 comments on commit 7d59634

Please sign in to comment.
You can’t perform that action at this time.