cutter/src/widgets/ResourcesWidget.cpp
Nics c4e8a1c178 Small refactor for the widgets of Cutter (#405)
* Small refactor for the widgets of Cutter

This refactor include the following :

* Creation of a new class
Creation of a new class, named CutterWidget, that inherits from QDockWidget and
is used to represent all of the widgets of the main window.
The goal of this class is to regroup all the behaviour shared by the widgets of
Cutter.

For example : in the constructor, instructions corresponding of those
present in the macro **ADD_DOCK** (in MainWindow.cpp) are executed.
This was made because I think that the macro **ADD_DOCK** which is used
to construct the widgets does not take advantage of the object structure.

* Ensure that every widget has a parent
Some widgets were created using the constructor QDockWidget, but using
**nullptr** (default) as argument, thus they haven't got any parent.

The constructor of a CutterWidget takes as argument the MainWindow and an
action (optional) and calls the constructor of QDockWidget with the main
window as argument. This is valid under the assumption that it is mandatory
for every widget to have the main window as a parent.

* Constructors removal
The constructors of some widgets are not used anywhere and does not seem not
fullfill any current usecase. They were removed.

* Renaming CutterWidget to CutterDockWidget
2018-03-16 22:46:57 +01:00

122 lines
2.9 KiB
C++

#include "utils/Helpers.h"
#include "ResourcesWidget.h"
#include "MainWindow.h"
#include <QVBoxLayout>
ResourcesModel::ResourcesModel(QList<ResourcesDescription> *resources, QObject *parent)
: QAbstractListModel(parent),
resources(resources)
{
}
int ResourcesModel::rowCount(const QModelIndex &) const
{
return resources->count();
}
int ResourcesModel::columnCount(const QModelIndex &) const
{
return Columns::COUNT;
}
QVariant ResourcesModel::data(const QModelIndex &index, int role) const
{
const ResourcesDescription &res = resources->at(index.row());
switch (role)
{
case Qt::DisplayRole:
switch (index.column())
{
case NAME:
return res.name;
case VADDR:
return RAddressString(res.vaddr);
case INDEX:
return res.index;
case TYPE:
return res.type;
case SIZE:
return qhelpers::formatBytecount(res.size);
case LANG:
return res.lang;
default:
return QVariant();
}
case Qt::UserRole:
return QVariant::fromValue(res);
default:
return QVariant();
}
}
QVariant ResourcesModel::headerData(int section, Qt::Orientation, int role) const
{
switch (role)
{
case Qt::DisplayRole:
switch (section)
{
case NAME:
return tr("Name");
case VADDR:
return tr("Vaddr");
case INDEX:
return tr("Index");
case TYPE:
return tr("Type");
case SIZE:
return tr("Size");
case LANG:
return tr("Lang");
default:
return QVariant();
}
default:
return QVariant();
}
}
void ResourcesModel::beginReload()
{
beginResetModel();
}
void ResourcesModel::endReload()
{
endResetModel();
}
ResourcesWidget::ResourcesWidget(MainWindow *main, QAction *action) :
CutterDockWidget(main, action)
{
setObjectName("ResourcesWidget");
model = new ResourcesModel(&resources, this);
// Configure widget
this->setWindowTitle(tr("Resources"));
// Add resources tree view
view = new QTreeView(this);
view->setModel(model);
view->show();
this->setWidget(view);
connect(Core(), SIGNAL(refreshAll()), this, SLOT(refreshResources()));
connect(view, SIGNAL(doubleClicked(const QModelIndex &)), this, SLOT(onDoubleClicked(const QModelIndex &)));
}
void ResourcesWidget::refreshResources()
{
model->beginReload();
resources = Core()->getAllResources();
model->endReload();
}
void ResourcesWidget::onDoubleClicked(const QModelIndex &index)
{
ResourcesDescription res = index.data(Qt::UserRole).value<ResourcesDescription>();
CutterCore::getInstance()->seek(res.vaddr);
}