Refactor RelocsWidget to a model and a view (#418)

This commit is contained in:
Abdel-Rahman A 2018-03-25 12:57:55 +02:00 committed by xarkes
parent adb311a122
commit 5001d395af
3 changed files with 118 additions and 45 deletions

View File

@ -4,45 +4,102 @@
#include "MainWindow.h" #include "MainWindow.h"
#include "utils/Helpers.h" #include "utils/Helpers.h"
RelocsModel::RelocsModel(QList<RelocDescription> *relocs, QObject *parent) :
QAbstractTableModel(parent),
relocs(relocs)
{}
int RelocsModel::rowCount(const QModelIndex &parent) const
{
return parent.isValid()? 0 : relocs->count();
}
int RelocsModel::columnCount(const QModelIndex&) const
{
return COUNT;
}
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:
return RAddressString(reloc.vaddr);
case TYPE:
return reloc.type;
case NAME:
return reloc.name;
default:
break;
}
default:
break;
}
return QVariant();
}
QVariant RelocsModel::headerData(int section, Qt::Orientation, int role) const
{
if(role == Qt::DisplayRole)
switch (section)
{
case VADDR:
return tr("Address");
case TYPE:
return tr("Type");
case NAME:
return tr("Name");
}
return QVariant();
}
void RelocsModel::beginReload()
{
beginResetModel();
}
void RelocsModel::endReload()
{
endResetModel();
}
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))
{ {
ui->setupUi(this); ui->setupUi(this);
ui->relocsTableView->setModel(model);
setScrollMode(); setScrollMode();
connect(Core(), SIGNAL(refreshAll()), this, SLOT(fillTreeWidget())); connect(Core(), SIGNAL(refreshAll()), this, SLOT(refreshRelocs()));
} }
RelocsWidget::~RelocsWidget() {} RelocsWidget::~RelocsWidget() {}
void RelocsWidget::on_relocsTreeWidget_itemDoubleClicked(QTreeWidgetItem *item, int column) void RelocsWidget::on_relocsTreeView_doubleClicked(const QModelIndex &index)
{ {
Q_UNUSED(column); Core()->seek(index.data(RelocsModel::AddressRole).toLongLong());
// Get offset and name of item double clicked
RelocDescription reloc = item->data(0, Qt::UserRole).value<RelocDescription>();
CutterCore::getInstance()->seek(reloc.vaddr);
} }
void RelocsWidget::fillTreeWidget() void RelocsWidget::refreshRelocs()
{ {
ui->relocsTreeWidget->clear(); model->beginReload();
relocs = Core()->getAllRelocs();
for (auto i : CutterCore::getInstance()->getAllRelocs()) { model->endReload();
QTreeWidgetItem *item = new QTreeWidgetItem(); ui->relocsTableView->resizeColumnsToContents();
item->setText(0, RAddressString(i.vaddr));
item->setText(1, i.type);
item->setText(2, i.name);
item->setData(0, Qt::UserRole, QVariant::fromValue(i));
ui->relocsTreeWidget->addTopLevelItem(item);
}
qhelpers::adjustColumns(ui->relocsTreeWidget, 0);
} }
void RelocsWidget::setScrollMode() void RelocsWidget::setScrollMode()
{ {
qhelpers::setVerticalScrollMode(ui->relocsTreeWidget); qhelpers::setVerticalScrollMode(ui->relocsTableView);
} }

View File

@ -2,16 +2,40 @@
#define RELOCSWIDGET_H #define RELOCSWIDGET_H
#include <memory> #include <memory>
#include <QAbstractTableModel>
#include "CutterDockWidget.h" #include "CutterDockWidget.h"
#include "Cutter.h"
class MainWindow; class MainWindow;
class QTreeWidgetItem;
namespace Ui { namespace Ui {
class RelocsWidget; class RelocsWidget;
} }
class RelocsModel : public QAbstractTableModel
{
Q_OBJECT
private:
QList<RelocDescription> *relocs;
public:
enum COLUMNS {VADDR = 0, TYPE, NAME, COUNT};
static const int AddressRole = Qt::UserRole;
RelocsModel(QList<RelocDescription> *relocs, QObject* parent = nullptr);
int rowCount(const QModelIndex &parent) const;
int columnCount(const QModelIndex &parent) const;
QVariant data(const QModelIndex &index, int role) const;
QVariant headerData(int section, Qt::Orientation orientation, int role) const;
void beginReload();
void endReload();
};
class RelocsWidget : public CutterDockWidget class RelocsWidget : public CutterDockWidget
{ {
Q_OBJECT Q_OBJECT
@ -21,12 +45,13 @@ public:
~RelocsWidget(); ~RelocsWidget();
private slots: private slots:
void on_relocsTreeWidget_itemDoubleClicked(QTreeWidgetItem *item, int column); void on_relocsTreeView_doubleClicked(const QModelIndex &index);
void refreshRelocs();
void fillTreeWidget();
private: private:
std::unique_ptr<Ui::RelocsWidget> ui; std::unique_ptr<Ui::RelocsWidget> ui;
RelocsModel *model;
QList<RelocDescription> relocs;
void setScrollMode(); void setScrollMode();
}; };

View File

@ -28,9 +28,9 @@
<number>0</number> <number>0</number>
</property> </property>
<item> <item>
<widget class="QTreeWidget" name="relocsTreeWidget"> <widget class="QTableView" name="relocsTableView">
<property name="styleSheet"> <property name="styleSheet">
<string notr="true">QTreeWidget::item <string notr="true">QTableView::item
{ {
padding-top: 1px; padding-top: 1px;
padding-bottom: 1px; padding-bottom: 1px;
@ -39,27 +39,18 @@
<property name="frameShape"> <property name="frameShape">
<enum>QFrame::NoFrame</enum> <enum>QFrame::NoFrame</enum>
</property> </property>
<property name="indentation"> <property name="showGrid">
<number>8</number> <bool>false</bool>
</property> </property>
<property name="sortingEnabled"> <property name="sortingEnabled">
<bool>true</bool> <bool>true</bool>
</property> </property>
<column> <attribute name="horizontalHeaderStretchLastSection">
<property name="text"> <bool>true</bool>
<string>Address</string> </attribute>
</property> <attribute name="verticalHeaderVisible">
</column> <bool>false</bool>
<column> </attribute>
<property name="text">
<string>Type</string>
</property>
</column>
<column>
<property name="text">
<string>Name</string>
</property>
</column>
</widget> </widget>
</item> </item>
</layout> </layout>