2017-10-02 16:18:40 +00:00
|
|
|
#include "ImportsWidget.h"
|
2019-08-19 13:35:25 +00:00
|
|
|
#include "ui_ListDockWidget.h"
|
2019-03-14 09:28:42 +00:00
|
|
|
#include "WidgetShortcuts.h"
|
2019-02-22 16:50:45 +00:00
|
|
|
#include "core/MainWindow.h"
|
2018-10-17 07:55:53 +00:00
|
|
|
#include "common/Helpers.h"
|
2017-04-13 15:14:02 +00:00
|
|
|
|
|
|
|
#include <QPainter>
|
2019-03-14 09:28:42 +00:00
|
|
|
#include <QPen>
|
|
|
|
#include <QShortcut>
|
|
|
|
#include <QTreeWidget>
|
2017-04-13 15:14:02 +00:00
|
|
|
|
2018-04-12 06:32:31 +00:00
|
|
|
ImportsModel::ImportsModel(QList<ImportDescription> *imports, QObject *parent) :
|
2019-08-19 13:35:25 +00:00
|
|
|
AddressableItemModel(parent),
|
2018-04-12 06:32:31 +00:00
|
|
|
imports(imports)
|
|
|
|
{}
|
2017-03-29 10:18:37 +00:00
|
|
|
|
2018-04-12 06:32:31 +00:00
|
|
|
int ImportsModel::rowCount(const QModelIndex &parent) const
|
2017-03-29 10:18:37 +00:00
|
|
|
{
|
2018-09-30 20:00:53 +00:00
|
|
|
return parent.isValid() ? 0 : imports->count();
|
2018-04-12 06:32:31 +00:00
|
|
|
}
|
|
|
|
|
2018-09-30 20:00:53 +00:00
|
|
|
int ImportsModel::columnCount(const QModelIndex &) const
|
2018-04-12 06:32:31 +00:00
|
|
|
{
|
2018-04-23 07:53:35 +00:00
|
|
|
return ImportsModel::ColumnCount;
|
2018-04-12 06:32:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QVariant ImportsModel::data(const QModelIndex &index, int role) const
|
|
|
|
{
|
|
|
|
const ImportDescription &import = imports->at(index.row());
|
2018-09-30 20:00:53 +00:00
|
|
|
switch (role) {
|
2018-04-12 06:32:31 +00:00
|
|
|
case Qt::ForegroundRole:
|
2018-04-23 07:53:35 +00:00
|
|
|
if (index.column() < ImportsModel::ColumnCount) {
|
2018-12-11 18:23:01 +00:00
|
|
|
// Red color for unsafe functions
|
2018-04-23 07:53:35 +00:00
|
|
|
if (banned.match(import.name).hasMatch())
|
2018-12-11 18:23:01 +00:00
|
|
|
return Config()->getColor("gui.item_unsafe");
|
|
|
|
// Grey color for symbols at offset 0 which can only be filled at runtime
|
|
|
|
if (import.plt == 0)
|
|
|
|
return Config()->getColor("gui.item_invalid");
|
2018-04-23 07:53:35 +00:00
|
|
|
}
|
2018-04-12 06:32:31 +00:00
|
|
|
break;
|
|
|
|
case Qt::DisplayRole:
|
2018-09-30 20:00:53 +00:00
|
|
|
switch (index.column()) {
|
2018-04-23 07:53:35 +00:00
|
|
|
case ImportsModel::AddressColumn:
|
2018-04-12 06:32:31 +00:00
|
|
|
return RAddressString(import.plt);
|
2018-04-23 07:53:35 +00:00
|
|
|
case ImportsModel::TypeColumn:
|
2018-04-12 06:32:31 +00:00
|
|
|
return import.type;
|
2018-04-23 07:53:35 +00:00
|
|
|
case ImportsModel::SafetyColumn:
|
2018-09-30 20:00:53 +00:00
|
|
|
return banned.match(import.name).hasMatch() ? tr("Unsafe") : QStringLiteral("");
|
2018-04-23 07:53:35 +00:00
|
|
|
case ImportsModel::NameColumn:
|
2018-04-12 06:32:31 +00:00
|
|
|
return import.name;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
2018-04-23 07:53:35 +00:00
|
|
|
case ImportsModel::ImportDescriptionRole:
|
|
|
|
return QVariant::fromValue(import);
|
|
|
|
case ImportsModel::AddressRole:
|
|
|
|
return import.plt;
|
2018-04-12 06:32:31 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant ImportsModel::headerData(int section, Qt::Orientation, int role) const
|
|
|
|
{
|
2018-09-30 20:00:53 +00:00
|
|
|
if (role == Qt::DisplayRole) {
|
|
|
|
switch (section) {
|
2018-04-23 07:53:35 +00:00
|
|
|
case ImportsModel::AddressColumn:
|
2018-04-12 06:32:31 +00:00
|
|
|
return tr("Address");
|
2018-04-23 07:53:35 +00:00
|
|
|
case ImportsModel::TypeColumn:
|
2018-04-12 06:32:31 +00:00
|
|
|
return tr("Type");
|
2018-04-23 07:53:35 +00:00
|
|
|
case ImportsModel::SafetyColumn:
|
2018-04-12 06:32:31 +00:00
|
|
|
return tr("Safety");
|
2018-04-23 07:53:35 +00:00
|
|
|
case ImportsModel::NameColumn:
|
2018-04-12 06:32:31 +00:00
|
|
|
return tr("Name");
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
2019-08-19 13:35:25 +00:00
|
|
|
RVA ImportsModel::address(const QModelIndex &index) const
|
|
|
|
{
|
|
|
|
const ImportDescription &import = imports->at(index.row());
|
|
|
|
return import.plt;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString ImportsModel::name(const QModelIndex &index) const
|
|
|
|
{
|
|
|
|
const ImportDescription &import = imports->at(index.row());
|
|
|
|
return import.name;
|
|
|
|
}
|
|
|
|
|
2018-04-24 18:40:40 +00:00
|
|
|
ImportsProxyModel::ImportsProxyModel(ImportsModel *sourceModel, QObject *parent)
|
2019-08-19 13:35:25 +00:00
|
|
|
: AddressableFilterProxyModel(sourceModel, parent)
|
2018-04-23 07:53:35 +00:00
|
|
|
{
|
|
|
|
setFilterCaseSensitivity(Qt::CaseInsensitive);
|
|
|
|
setSortCaseSensitivity(Qt::CaseInsensitive);
|
|
|
|
}
|
|
|
|
|
2018-04-24 18:40:40 +00:00
|
|
|
bool ImportsProxyModel::filterAcceptsRow(int row, const QModelIndex &parent) const
|
2018-04-23 07:53:35 +00:00
|
|
|
{
|
2018-04-24 18:40:40 +00:00
|
|
|
QModelIndex index = sourceModel()->index(row, 0, parent);
|
|
|
|
auto import = index.data(ImportsModel::ImportDescriptionRole).value<ImportDescription>();
|
2018-04-23 07:53:35 +00:00
|
|
|
|
|
|
|
return import.name.contains(filterRegExp());
|
|
|
|
}
|
|
|
|
|
2018-04-24 18:40:40 +00:00
|
|
|
bool ImportsProxyModel::lessThan(const QModelIndex &left, const QModelIndex &right) const
|
2018-04-23 07:53:35 +00:00
|
|
|
{
|
|
|
|
if (!left.isValid() || !right.isValid())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (left.parent().isValid() || right.parent().isValid())
|
|
|
|
return false;
|
|
|
|
|
2018-04-24 18:40:40 +00:00
|
|
|
auto leftImport = left.data(ImportsModel::ImportDescriptionRole).value<ImportDescription>();
|
|
|
|
auto rightImport = right.data(ImportsModel::ImportDescriptionRole).value<ImportDescription>();
|
2018-04-23 07:53:35 +00:00
|
|
|
|
|
|
|
switch (left.column()) {
|
|
|
|
case ImportsModel::AddressColumn:
|
|
|
|
return leftImport.plt < rightImport.plt;
|
|
|
|
case ImportsModel::TypeColumn:
|
|
|
|
return leftImport.type < rightImport.type;
|
|
|
|
case ImportsModel::SafetyColumn:
|
|
|
|
break;
|
|
|
|
case ImportsModel::NameColumn:
|
|
|
|
return leftImport.name < rightImport.name;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-03-29 10:18:37 +00:00
|
|
|
/*
|
|
|
|
* Imports Widget
|
|
|
|
*/
|
|
|
|
|
2018-03-16 21:46:57 +00:00
|
|
|
ImportsWidget::ImportsWidget(MainWindow *main, QAction *action) :
|
2019-08-19 13:35:25 +00:00
|
|
|
ListDockWidget(main, action),
|
2018-04-23 07:53:35 +00:00
|
|
|
importsModel(new ImportsModel(&imports, this)),
|
2019-08-19 13:35:25 +00:00
|
|
|
importsProxyModel(new ImportsProxyModel(importsModel, this))
|
2017-03-29 10:18:37 +00:00
|
|
|
{
|
2019-08-19 13:35:25 +00:00
|
|
|
setWindowTitle(tr("Imports"));
|
|
|
|
setObjectName("ImportsWidget");
|
2018-04-23 07:53:35 +00:00
|
|
|
|
2019-08-19 13:35:25 +00:00
|
|
|
setModels(importsProxyModel);
|
|
|
|
ui->treeView->sortByColumn(ImportsModel::NameColumn, Qt::AscendingOrder);
|
2018-10-16 06:25:09 +00:00
|
|
|
QShortcut *toggle_shortcut = new QShortcut(widgetShortcuts["ImportsWidget"], main);
|
|
|
|
connect(toggle_shortcut, &QShortcut::activated, this, [=] (){
|
|
|
|
toggleDockWidget(true);
|
|
|
|
main->updateDockActionChecked(action);
|
|
|
|
} );
|
|
|
|
|
2018-04-12 06:32:31 +00:00
|
|
|
connect(Core(), SIGNAL(refreshAll()), this, SLOT(refreshImports()));
|
2017-04-13 15:14:02 +00:00
|
|
|
}
|
|
|
|
|
2017-11-19 12:56:10 +00:00
|
|
|
ImportsWidget::~ImportsWidget() {}
|
2017-03-29 10:18:37 +00:00
|
|
|
|
2018-04-12 06:32:31 +00:00
|
|
|
void ImportsWidget::refreshImports()
|
2017-04-09 19:55:06 +00:00
|
|
|
{
|
2018-10-10 11:33:55 +00:00
|
|
|
importsModel->beginResetModel();
|
2018-04-12 06:32:31 +00:00
|
|
|
imports = Core()->getAllImports();
|
2018-10-10 11:33:55 +00:00
|
|
|
importsModel->endResetModel();
|
2019-08-19 13:35:25 +00:00
|
|
|
qhelpers::adjustColumns(ui->treeView, 4, 0);
|
2017-05-19 07:45:26 +00:00
|
|
|
}
|