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 "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) :
CutterDockWidget(main, action),
ui(new Ui::RelocsWidget)
ui(new Ui::RelocsWidget),
model(new RelocsModel(&relocs, this))
{
ui->setupUi(this);
ui->relocsTableView->setModel(model);
setScrollMode();
connect(Core(), SIGNAL(refreshAll()), this, SLOT(fillTreeWidget()));
connect(Core(), SIGNAL(refreshAll()), this, SLOT(refreshRelocs()));
}
RelocsWidget::~RelocsWidget() {}
void RelocsWidget::on_relocsTreeWidget_itemDoubleClicked(QTreeWidgetItem *item, int column)
void RelocsWidget::on_relocsTreeView_doubleClicked(const QModelIndex &index)
{
Q_UNUSED(column);
// Get offset and name of item double clicked
RelocDescription reloc = item->data(0, Qt::UserRole).value<RelocDescription>();
CutterCore::getInstance()->seek(reloc.vaddr);
Core()->seek(index.data(RelocsModel::AddressRole).toLongLong());
}
void RelocsWidget::fillTreeWidget()
void RelocsWidget::refreshRelocs()
{
ui->relocsTreeWidget->clear();
for (auto i : CutterCore::getInstance()->getAllRelocs()) {
QTreeWidgetItem *item = new QTreeWidgetItem();
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);
model->beginReload();
relocs = Core()->getAllRelocs();
model->endReload();
ui->relocsTableView->resizeColumnsToContents();
}
void RelocsWidget::setScrollMode()
{
qhelpers::setVerticalScrollMode(ui->relocsTreeWidget);
qhelpers::setVerticalScrollMode(ui->relocsTableView);
}

View File

@ -2,16 +2,40 @@
#define RELOCSWIDGET_H
#include <memory>
#include <QAbstractTableModel>
#include "CutterDockWidget.h"
#include "Cutter.h"
class MainWindow;
class QTreeWidgetItem;
namespace Ui {
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
{
Q_OBJECT
@ -21,12 +45,13 @@ public:
~RelocsWidget();
private slots:
void on_relocsTreeWidget_itemDoubleClicked(QTreeWidgetItem *item, int column);
void fillTreeWidget();
void on_relocsTreeView_doubleClicked(const QModelIndex &index);
void refreshRelocs();
private:
std::unique_ptr<Ui::RelocsWidget> ui;
RelocsModel *model;
QList<RelocDescription> relocs;
void setScrollMode();
};

View File

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