cutter/src/widgets/ZignaturesWidget.cpp

156 lines
4.7 KiB
C++
Raw Normal View History

2018-05-24 15:37:37 +00:00
#include "ZignaturesWidget.h"
#include "ui_ZignaturesWidget.h"
#include "MainWindow.h"
#include "utils/Helpers.h"
ZignaturesModel::ZignaturesModel(QList<ZignatureDescription> *zignatures, QObject *parent)
: QAbstractListModel(parent),
zignatures(zignatures)
{
}
int ZignaturesModel::rowCount(const QModelIndex &) const
{
return zignatures->count();
}
int ZignaturesModel::columnCount(const QModelIndex &) const
{
return ZignaturesModel::ColumnCount;
}
QVariant ZignaturesModel::data(const QModelIndex &index, int role) const
{
if (index.row() >= zignatures->count())
return QVariant();
const ZignatureDescription &zignature = zignatures->at(index.row());
switch (role) {
case Qt::DisplayRole:
switch (index.column()) {
case OffsetColumn:
return RAddressString(zignature.offset);
case NameColumn:
return zignature.name;
case ValueColumn:
return zignature.bytes;
default:
return QVariant();
}
case ZignatureDescriptionRole:
return QVariant::fromValue(zignature);
case Qt::ToolTipRole: {
QString tmp = QString("Graph:\n\n Cyclomatic complexity: " + RSizeString(zignature.cc) +
2018-09-30 20:00:53 +00:00
"\n Nodes / basicblocks: " + RSizeString(zignature.nbbs) +
"\n Edges: " + RSizeString(zignature.edges) +
"\n Ebbs: " + RSizeString(zignature.ebbs) +
"\n\nRefs:\n");
2018-05-24 15:37:37 +00:00
for (QString ref : zignature.refs) {
tmp.append("\n " + ref);
}
return tmp;
}
default:
return QVariant();
}
}
QVariant ZignaturesModel::headerData(int section, Qt::Orientation, int role) const
{
switch (role) {
case Qt::DisplayRole:
switch (section) {
case OffsetColumn:
return tr("Offset");
case NameColumn:
return tr("Name");
case ValueColumn:
return tr("Bytes");
default:
return QVariant();
}
default:
return QVariant();
}
}
ZignaturesProxyModel::ZignaturesProxyModel(ZignaturesModel *sourceModel, QObject *parent)
: QSortFilterProxyModel(parent)
{
setSourceModel(sourceModel);
}
bool ZignaturesProxyModel::filterAcceptsRow(int row, const QModelIndex &parent) const
{
QModelIndex index = sourceModel()->index(row, 0, parent);
2018-09-30 20:00:53 +00:00
ZignatureDescription item = index.data(
ZignaturesModel::ZignatureDescriptionRole).value<ZignatureDescription>();
2018-05-24 15:37:37 +00:00
return item.name.contains(filterRegExp());
}
bool ZignaturesProxyModel::lessThan(const QModelIndex &left, const QModelIndex &right) const
{
2018-09-30 20:00:53 +00:00
ZignatureDescription leftZignature = left.data(
ZignaturesModel::ZignatureDescriptionRole).value<ZignatureDescription>();
ZignatureDescription rightZignature = right.data(
ZignaturesModel::ZignatureDescriptionRole).value<ZignatureDescription>();
2018-05-24 15:37:37 +00:00
switch (left.column()) {
case ZignaturesModel::OffsetColumn:
return leftZignature.offset < rightZignature.offset;
case ZignaturesModel::NameColumn:
return leftZignature.name < rightZignature.name;
case ZignaturesModel::ValueColumn:
return leftZignature.bytes < rightZignature.bytes;
default:
break;
}
return leftZignature.offset < rightZignature.offset;
}
ZignaturesWidget::ZignaturesWidget(MainWindow *main, QAction *action) :
CutterDockWidget(main, action),
ui(new Ui::ZignaturesWidget)
{
ui->setupUi(this);
zignaturesModel = new ZignaturesModel(&zignatures, this);
zignaturesProxyModel = new ZignaturesProxyModel(zignaturesModel, this);
ui->zignaturesTreeView->setModel(zignaturesProxyModel);
ui->zignaturesTreeView->sortByColumn(ZignaturesModel::OffsetColumn, Qt::AscendingOrder);
setScrollMode();
connect(Core(), &CutterCore::refreshAll, this, &ZignaturesWidget::refreshZignatures);
}
ZignaturesWidget::~ZignaturesWidget() {}
void ZignaturesWidget::refreshZignatures()
{
zignaturesModel->beginResetModel();
2018-05-24 15:37:37 +00:00
zignatures = Core()->getAllZignatures();
zignaturesModel->endResetModel();
2018-05-24 15:37:37 +00:00
ui->zignaturesTreeView->resizeColumnToContents(0);
ui->zignaturesTreeView->resizeColumnToContents(1);
ui->zignaturesTreeView->resizeColumnToContents(2);
}
void ZignaturesWidget::setScrollMode()
{
qhelpers::setVerticalScrollMode(ui->zignaturesTreeView);
}
void ZignaturesWidget::on_zignaturesTreeView_doubleClicked(const QModelIndex &index)
{
2018-09-30 20:00:53 +00:00
ZignatureDescription item = index.data(
ZignaturesModel::ZignatureDescriptionRole).value<ZignatureDescription>();
2018-05-24 15:37:37 +00:00
Core()->seek(item.offset);
}