From 5001d395af70c7d0fc087f44804c16cf50ce2f75 Mon Sep 17 00:00:00 2001 From: Abdel-Rahman A Date: Sun, 25 Mar 2018 12:57:55 +0200 Subject: [PATCH] Refactor RelocsWidget to a model and a view (#418) --- src/widgets/RelocsWidget.cpp | 101 +++++++++++++++++++++++++++-------- src/widgets/RelocsWidget.h | 33 ++++++++++-- src/widgets/RelocsWidget.ui | 29 ++++------ 3 files changed, 118 insertions(+), 45 deletions(-) diff --git a/src/widgets/RelocsWidget.cpp b/src/widgets/RelocsWidget.cpp index 29d9d528..97b577f2 100644 --- a/src/widgets/RelocsWidget.cpp +++ b/src/widgets/RelocsWidget.cpp @@ -4,45 +4,102 @@ #include "MainWindow.h" #include "utils/Helpers.h" +RelocsModel::RelocsModel(QList *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(); - 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); } diff --git a/src/widgets/RelocsWidget.h b/src/widgets/RelocsWidget.h index c526a4d4..25b881c2 100644 --- a/src/widgets/RelocsWidget.h +++ b/src/widgets/RelocsWidget.h @@ -2,16 +2,40 @@ #define RELOCSWIDGET_H #include +#include #include "CutterDockWidget.h" +#include "Cutter.h" class MainWindow; -class QTreeWidgetItem; namespace Ui { class RelocsWidget; } +class RelocsModel : public QAbstractTableModel +{ + Q_OBJECT + +private: + QList *relocs; + +public: + enum COLUMNS {VADDR = 0, TYPE, NAME, COUNT}; + static const int AddressRole = Qt::UserRole; + + RelocsModel(QList *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; + RelocsModel *model; + QList relocs; void setScrollMode(); }; diff --git a/src/widgets/RelocsWidget.ui b/src/widgets/RelocsWidget.ui index 15b5fed6..ce25461b 100644 --- a/src/widgets/RelocsWidget.ui +++ b/src/widgets/RelocsWidget.ui @@ -28,9 +28,9 @@ 0 - + - QTreeWidget::item + QTableView::item { padding-top: 1px; padding-bottom: 1px; @@ -39,27 +39,18 @@ QFrame::NoFrame - - 8 + + false true - - - Address - - - - - Type - - - - - Name - - + + true + + + false +