2018-05-03 07:52:30 +00:00
|
|
|
#include "SectionsWidget.h"
|
2017-03-29 10:18:37 +00:00
|
|
|
|
2018-11-10 09:17:52 +00:00
|
|
|
#include "CutterTreeView.h"
|
2017-10-01 19:09:42 +00:00
|
|
|
#include "MainWindow.h"
|
2018-10-20 18:20:06 +00:00
|
|
|
#include "QuickFilterView.h"
|
2018-10-17 07:55:53 +00:00
|
|
|
#include "common/Helpers.h"
|
2017-04-13 15:14:02 +00:00
|
|
|
|
2018-05-03 07:52:30 +00:00
|
|
|
SectionsModel::SectionsModel(QList<SectionDescription> *sections, QObject *parent)
|
|
|
|
: QAbstractListModel(parent),
|
|
|
|
sections(sections)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
int SectionsModel::rowCount(const QModelIndex &) const
|
|
|
|
{
|
|
|
|
return sections->count();
|
|
|
|
}
|
|
|
|
|
|
|
|
int SectionsModel::columnCount(const QModelIndex &) const
|
|
|
|
{
|
|
|
|
return SectionsModel::ColumnCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant SectionsModel::data(const QModelIndex &index, int role) const
|
|
|
|
{
|
|
|
|
// TODO: create unique colors, e. g. use HSV color space and rotate in H for 360/size
|
|
|
|
static const QList<QColor> colors = { QColor("#1ABC9C"), //TURQUOISE
|
|
|
|
QColor("#2ECC71"), //EMERALD
|
|
|
|
QColor("#3498DB"), //PETER RIVER
|
|
|
|
QColor("#9B59B6"), //AMETHYST
|
|
|
|
QColor("#34495E"), //WET ASPHALT
|
|
|
|
QColor("#F1C40F"), //SUN FLOWER
|
|
|
|
QColor("#E67E22"), //CARROT
|
|
|
|
QColor("#E74C3C"), //ALIZARIN
|
|
|
|
QColor("#ECF0F1"), //CLOUDS
|
|
|
|
QColor("#BDC3C7"), //SILVER
|
|
|
|
QColor("#95A5A6") //COBCRETE
|
|
|
|
};
|
|
|
|
|
|
|
|
if (index.row() >= sections->count())
|
|
|
|
return QVariant();
|
|
|
|
|
|
|
|
const SectionDescription §ion = sections->at(index.row());
|
|
|
|
|
|
|
|
switch (role) {
|
|
|
|
case Qt::DisplayRole:
|
|
|
|
switch (index.column()) {
|
|
|
|
case SectionsModel::NameColumn:
|
|
|
|
return section.name;
|
|
|
|
case SectionsModel::SizeColumn:
|
|
|
|
return section.size;
|
|
|
|
case SectionsModel::AddressColumn:
|
|
|
|
return RAddressString(section.vaddr);
|
|
|
|
case SectionsModel::EndAddressColumn:
|
|
|
|
return RAddressString(section.vaddr + section.size);
|
2018-05-05 10:11:44 +00:00
|
|
|
case SectionsModel::EntropyColumn:
|
|
|
|
return section.entropy;
|
2018-05-03 07:52:30 +00:00
|
|
|
default:
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
case Qt::DecorationRole:
|
|
|
|
if (index.column() == 0)
|
|
|
|
return colors[index.row() % colors.size()];
|
|
|
|
return QVariant();
|
|
|
|
case SectionsModel::SectionDescriptionRole:
|
|
|
|
return QVariant::fromValue(section);
|
|
|
|
default:
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant SectionsModel::headerData(int section, Qt::Orientation, int role) const
|
|
|
|
{
|
|
|
|
switch (role) {
|
|
|
|
case Qt::DisplayRole:
|
|
|
|
switch (section) {
|
|
|
|
case SectionsModel::NameColumn:
|
|
|
|
return tr("Name");
|
|
|
|
case SectionsModel::SizeColumn:
|
|
|
|
return tr("Size");
|
|
|
|
case SectionsModel::AddressColumn:
|
|
|
|
return tr("Address");
|
|
|
|
case SectionsModel::EndAddressColumn:
|
2018-10-20 18:20:06 +00:00
|
|
|
return tr("End Address");
|
2018-05-05 10:11:44 +00:00
|
|
|
case SectionsModel::EntropyColumn:
|
|
|
|
return tr("Entropy");
|
2018-05-03 07:52:30 +00:00
|
|
|
default:
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SectionsProxyModel::SectionsProxyModel(SectionsModel *sourceModel, QObject *parent)
|
|
|
|
: QSortFilterProxyModel(parent)
|
|
|
|
{
|
|
|
|
setSourceModel(sourceModel);
|
|
|
|
setFilterCaseSensitivity(Qt::CaseInsensitive);
|
|
|
|
setSortCaseSensitivity(Qt::CaseInsensitive);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SectionsProxyModel::lessThan(const QModelIndex &left, const QModelIndex &right) const
|
|
|
|
{
|
|
|
|
auto leftSection = left.data(SectionsModel::SectionDescriptionRole).value<SectionDescription>();
|
|
|
|
auto rightSection = right.data(SectionsModel::SectionDescriptionRole).value<SectionDescription>();
|
|
|
|
|
|
|
|
switch (left.column()) {
|
|
|
|
case SectionsModel::NameColumn:
|
|
|
|
return leftSection.name < rightSection.name;
|
|
|
|
case SectionsModel::SizeColumn:
|
|
|
|
return leftSection.size < rightSection.size;
|
|
|
|
case SectionsModel::AddressColumn:
|
|
|
|
case SectionsModel::EndAddressColumn:
|
|
|
|
return leftSection.vaddr < rightSection.vaddr;
|
2018-05-05 10:11:44 +00:00
|
|
|
case SectionsModel::EntropyColumn:
|
|
|
|
return leftSection.entropy < rightSection.entropy;
|
|
|
|
|
2018-05-03 07:52:30 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
SectionsWidget::SectionsWidget(MainWindow *main, QAction *action) :
|
|
|
|
CutterDockWidget(main, action),
|
2017-04-13 15:14:02 +00:00
|
|
|
main(main)
|
2017-03-29 10:18:37 +00:00
|
|
|
{
|
2018-09-01 13:32:48 +00:00
|
|
|
setObjectName("SectionsWidget");
|
2018-08-27 11:19:30 +00:00
|
|
|
setWindowTitle(QStringLiteral("Sections"));
|
2018-05-03 07:52:30 +00:00
|
|
|
|
2018-11-10 09:17:52 +00:00
|
|
|
sectionsTable = new CutterTreeView;
|
2018-05-03 07:52:30 +00:00
|
|
|
sectionsModel = new SectionsModel(§ions, this);
|
2018-08-27 11:19:30 +00:00
|
|
|
auto proxyModel = new SectionsProxyModel(sectionsModel, this);
|
2017-04-09 17:09:35 +00:00
|
|
|
|
2018-08-27 11:19:30 +00:00
|
|
|
sectionsTable->setModel(proxyModel);
|
|
|
|
sectionsTable->setIndentation(10);
|
|
|
|
sectionsTable->setSortingEnabled(true);
|
|
|
|
sectionsTable->sortByColumn(SectionsModel::NameColumn, Qt::AscendingOrder);
|
2018-02-02 15:56:39 +00:00
|
|
|
|
2018-08-27 11:19:30 +00:00
|
|
|
connect(sectionsTable, SIGNAL(doubleClicked(const QModelIndex &)),
|
|
|
|
this, SLOT(onSectionsDoubleClicked(const QModelIndex &)));
|
|
|
|
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
|
2018-05-03 07:52:30 +00:00
|
|
|
|
2018-08-27 11:19:30 +00:00
|
|
|
connect(Core(), SIGNAL(refreshAll()), this, SLOT(refreshSections()));
|
2018-10-20 18:20:06 +00:00
|
|
|
quickFilterView = new QuickFilterView(this, false);
|
|
|
|
quickFilterView->setObjectName(QStringLiteral("quickFilterView"));
|
|
|
|
QSizePolicy sizePolicy1(QSizePolicy::Preferred, QSizePolicy::Maximum);
|
|
|
|
sizePolicy1.setHorizontalStretch(0);
|
|
|
|
sizePolicy1.setVerticalStretch(0);
|
|
|
|
sizePolicy1.setHeightForWidth(quickFilterView->sizePolicy().hasHeightForWidth());
|
|
|
|
quickFilterView->setSizePolicy(sizePolicy1);
|
|
|
|
|
|
|
|
QShortcut *search_shortcut = new QShortcut(QKeySequence::Find, this);
|
|
|
|
connect(search_shortcut, &QShortcut::activated, quickFilterView, &QuickFilterView::showFilter);
|
|
|
|
search_shortcut->setContext(Qt::WidgetWithChildrenShortcut);
|
|
|
|
|
|
|
|
QShortcut *clear_shortcut = new QShortcut(QKeySequence(Qt::Key_Escape), this);
|
|
|
|
connect(clear_shortcut, &QShortcut::activated, quickFilterView, &QuickFilterView::clearFilter);
|
|
|
|
clear_shortcut->setContext(Qt::WidgetWithChildrenShortcut);
|
|
|
|
|
|
|
|
connect(quickFilterView, SIGNAL(filterTextChanged(const QString &)), proxyModel,
|
|
|
|
SLOT(setFilterWildcard(const QString &)));
|
|
|
|
connect(quickFilterView, SIGNAL(filterClosed()), sectionsTable, SLOT(setFocus()));
|
|
|
|
dockWidgetContents = new QWidget(this);
|
|
|
|
QVBoxLayout *layout = new QVBoxLayout();
|
|
|
|
layout->addWidget(sectionsTable);
|
|
|
|
layout->addWidget(quickFilterView);
|
|
|
|
layout->setMargin(0);
|
|
|
|
dockWidgetContents->setLayout(layout);
|
|
|
|
setWidget(dockWidgetContents);
|
2018-05-03 07:52:30 +00:00
|
|
|
}
|
|
|
|
|
2018-08-27 11:19:30 +00:00
|
|
|
SectionsWidget::~SectionsWidget() {}
|
2017-04-13 15:14:02 +00:00
|
|
|
|
2018-05-03 07:52:30 +00:00
|
|
|
void SectionsWidget::refreshSections()
|
2017-03-29 10:18:37 +00:00
|
|
|
{
|
2018-10-10 11:33:55 +00:00
|
|
|
sectionsModel->beginResetModel();
|
2018-05-03 07:52:30 +00:00
|
|
|
sections = Core()->getAllSections();
|
2018-10-10 11:33:55 +00:00
|
|
|
sectionsModel->endResetModel();
|
2017-03-29 10:18:37 +00:00
|
|
|
|
2018-05-03 07:52:30 +00:00
|
|
|
qhelpers::adjustColumns(sectionsTable, SectionsModel::ColumnCount, 0);
|
|
|
|
}
|
2017-03-29 10:18:37 +00:00
|
|
|
|
2018-02-02 15:56:39 +00:00
|
|
|
void SectionsWidget::onSectionsDoubleClicked(const QModelIndex &index)
|
|
|
|
{
|
2018-04-23 07:54:06 +00:00
|
|
|
if (!index.isValid())
|
|
|
|
return;
|
|
|
|
|
2018-05-03 07:52:30 +00:00
|
|
|
auto section = index.data(SectionsModel::SectionDescriptionRole).value<SectionDescription>();
|
|
|
|
Core()->seek(section.vaddr);
|
|
|
|
}
|