mirror of
https://github.com/rizinorg/cutter.git
synced 2025-01-19 10:58:51 +00:00
Refactor ImportsWidget to a view and a model (#438)
* Refactor ImportsWidget to a view and a model * Remove unused delegate
This commit is contained in:
parent
7181c81a2b
commit
e1f17db235
@ -8,26 +8,83 @@
|
|||||||
#include <QPen>
|
#include <QPen>
|
||||||
#include <QPainter>
|
#include <QPainter>
|
||||||
|
|
||||||
|
ImportsModel::ImportsModel(QList<ImportDescription> *imports, QObject *parent) :
|
||||||
|
QAbstractTableModel(parent),
|
||||||
|
imports(imports)
|
||||||
|
{}
|
||||||
|
|
||||||
void CMyDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
|
int ImportsModel::rowCount(const QModelIndex &parent) const
|
||||||
const QModelIndex &index) const
|
|
||||||
{
|
{
|
||||||
QStyleOptionViewItem itemOption(option);
|
return parent.isValid()? 0 : imports->count();
|
||||||
initStyleOption(&itemOption, index);
|
}
|
||||||
|
|
||||||
itemOption.rect.adjust(10, 0, 0,
|
int ImportsModel::columnCount(const QModelIndex&) const
|
||||||
0); // Make the item rectangle 10 pixels smaller from the left side.
|
{
|
||||||
|
return COUNT;
|
||||||
|
}
|
||||||
|
|
||||||
// Draw your item content.
|
QVariant ImportsModel::data(const QModelIndex &index, int role) const
|
||||||
QApplication::style()->drawControl(QStyle::CE_ItemViewItem, &itemOption, painter, nullptr);
|
{
|
||||||
|
const ImportDescription &import = imports->at(index.row());
|
||||||
|
switch(role)
|
||||||
|
{
|
||||||
|
case AddressRole:
|
||||||
|
return import.plt;
|
||||||
|
case Qt::ForegroundRole:
|
||||||
|
if(index.column() < COUNT)
|
||||||
|
if(banned.match(import.name).hasMatch())
|
||||||
|
return QColor(255, 129, 123);
|
||||||
|
break;
|
||||||
|
case Qt::DisplayRole:
|
||||||
|
switch(index.column())
|
||||||
|
{
|
||||||
|
case ADDRESS:
|
||||||
|
return RAddressString(import.plt);
|
||||||
|
case TYPE:
|
||||||
|
return import.type;
|
||||||
|
case SAFETY:
|
||||||
|
return banned.match(import.name).hasMatch()? tr("Unsafe") : QStringLiteral("");
|
||||||
|
case NAME:
|
||||||
|
return import.name;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return QVariant();
|
||||||
|
}
|
||||||
|
|
||||||
// And now you can draw a bottom border.
|
QVariant ImportsModel::headerData(int section, Qt::Orientation, int role) const
|
||||||
//painter->setPen(Qt::cyan);
|
{
|
||||||
QPen pen = painter->pen();
|
if(role == Qt::DisplayRole)
|
||||||
pen.setColor(Qt::white);
|
{
|
||||||
pen.setWidth(1);
|
switch(section)
|
||||||
painter->setPen(pen);
|
{
|
||||||
painter->drawLine(itemOption.rect.bottomLeft(), itemOption.rect.bottomRight());
|
case ADDRESS:
|
||||||
|
return tr("Address");
|
||||||
|
case TYPE:
|
||||||
|
return tr("Type");
|
||||||
|
case SAFETY:
|
||||||
|
return tr("Safety");
|
||||||
|
case NAME:
|
||||||
|
return tr("Name");
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return QVariant();
|
||||||
|
}
|
||||||
|
|
||||||
|
void ImportsModel::beginReload()
|
||||||
|
{
|
||||||
|
beginResetModel();
|
||||||
|
}
|
||||||
|
|
||||||
|
void ImportsModel::endReload()
|
||||||
|
{
|
||||||
|
endResetModel();
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -36,82 +93,35 @@ void CMyDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
|
|||||||
|
|
||||||
ImportsWidget::ImportsWidget(MainWindow *main, QAction *action) :
|
ImportsWidget::ImportsWidget(MainWindow *main, QAction *action) :
|
||||||
CutterDockWidget(main, action),
|
CutterDockWidget(main, action),
|
||||||
ui(new Ui::ImportsWidget)
|
ui(new Ui::ImportsWidget),
|
||||||
|
model(new ImportsModel(&imports, this))
|
||||||
{
|
{
|
||||||
ui->setupUi(this);
|
ui->setupUi(this);
|
||||||
|
|
||||||
ui->importsTreeWidget->sortByColumn(3, Qt::AscendingOrder);
|
ui->importsTreeView->setModel(model);
|
||||||
|
ui->importsTreeView->sortByColumn(3, Qt::AscendingOrder);
|
||||||
|
|
||||||
setScrollMode();
|
setScrollMode();
|
||||||
|
|
||||||
connect(Core(), SIGNAL(refreshAll()), this, SLOT(fillImports()));
|
connect(Core(), SIGNAL(refreshAll()), this, SLOT(refreshImports()));
|
||||||
}
|
}
|
||||||
|
|
||||||
ImportsWidget::~ImportsWidget() {}
|
ImportsWidget::~ImportsWidget() {}
|
||||||
|
|
||||||
void ImportsWidget::fillImports()
|
void ImportsWidget::refreshImports()
|
||||||
{
|
{
|
||||||
ui->importsTreeWidget->clear();
|
model->beginReload();
|
||||||
for (auto i : CutterCore::getInstance()->getAllImports()) {
|
imports = Core()->getAllImports();
|
||||||
QTreeWidgetItem *item = new QTreeWidgetItem();
|
model->endReload();
|
||||||
item->setText(0, RAddressString(i.plt));
|
qhelpers::adjustColumns(ui->importsTreeView, 4, 0);
|
||||||
item->setText(1, i.type);
|
|
||||||
item->setText(2, "");
|
|
||||||
item->setText(3, i.name);
|
|
||||||
item->setData(0, Qt::UserRole, QVariant::fromValue(i));
|
|
||||||
ui->importsTreeWidget->addTopLevelItem(item);
|
|
||||||
}
|
|
||||||
|
|
||||||
highlightUnsafe();
|
|
||||||
qhelpers::adjustColumns(ui->importsTreeWidget, 0, 10);
|
|
||||||
}
|
|
||||||
|
|
||||||
void ImportsWidget::highlightUnsafe()
|
|
||||||
{
|
|
||||||
static const QString
|
|
||||||
banned("[a-zA-Z_.]*(system|strcpy|strcpyA|strcpyW|wcscpy|_tcscpy|_mbscpy|StrCpy|StrCpyA|StrCpyW|lstrcpy|lstrcpyA|lstrcpyW"
|
|
||||||
\
|
|
||||||
"|_tccpy|_mbccpy|_ftcscpy|strcat|strcatA|strcatW|wcscat|_tcscat|_mbscat|StrCat|StrCatA|StrCatW|lstrcat|lstrcatA|"
|
|
||||||
\
|
|
||||||
"lstrcatW|StrCatBuff|StrCatBuffA|StrCatBuffW|StrCatChainW|_tccat|_mbccat|_ftcscat|sprintfW|sprintfA|wsprintf|wsprintfW|"
|
|
||||||
\
|
|
||||||
"wsprintfA|sprintf|swprintf|_stprintf|wvsprintf|wvsprintfA|wvsprintfW|vsprintf|_vstprintf|vswprintf|strncpy|wcsncpy|"
|
|
||||||
\
|
|
||||||
"_tcsncpy|_mbsncpy|_mbsnbcpy|StrCpyN|StrCpyNA|StrCpyNW|StrNCpy|strcpynA|StrNCpyA|StrNCpyW|lstrcpyn|lstrcpynA|lstrcpynW|"
|
|
||||||
\
|
|
||||||
"strncat|wcsncat|_tcsncat|_mbsncat|_mbsnbcat|StrCatN|StrCatNA|StrCatNW|StrNCat|StrNCatA|StrNCatW|lstrncat|lstrcatnA|"
|
|
||||||
\
|
|
||||||
"lstrcatnW|lstrcatn|gets|_getts|_gettws|IsBadWritePtr|IsBadHugeWritePtr|IsBadReadPtr|IsBadHugeReadPtr|IsBadCodePtr|"
|
|
||||||
\
|
|
||||||
"IsBadStringPtr|memcpy|RtlCopyMemory|CopyMemory|wmemcpy|wnsprintf|wnsprintfA|wnsprintfW|_snwprintf|_snprintf|_sntprintf|"
|
|
||||||
\
|
|
||||||
"_vsnprintf|vsnprintf|_vsnwprintf|_vsntprintf|wvnsprintf|wvnsprintfA|wvnsprintfW|strtok|_tcstok|wcstok|_mbstok|makepath|"
|
|
||||||
\
|
|
||||||
"_tmakepath| _makepath|_wmakepath|_splitpath|_tsplitpath|_wsplitpath|scanf|wscanf|_tscanf|sscanf|swscanf|_stscanf|snscanf|"
|
|
||||||
\
|
|
||||||
"snwscanf|_sntscanf|_itoa|_itow|_i64toa|_i64tow|_ui64toa|_ui64tot|_ui64tow|_ultoa|_ultot|_ultow|CharToOem|CharToOemA|CharToOemW|"
|
|
||||||
\
|
|
||||||
"OemToChar|OemToCharA|OemToCharW|CharToOemBuffA|CharToOemBuffW|alloca|_alloca|strlen|wcslen|_mbslen|_mbstrlen|StrLen|lstrlen|"
|
|
||||||
\
|
|
||||||
"ChangeWindowMessageFilter)");
|
|
||||||
|
|
||||||
QList<QTreeWidgetItem *> clist = ui->importsTreeWidget->findItems(banned, Qt::MatchRegExp, 4);
|
|
||||||
foreach (QTreeWidgetItem *item, clist) {
|
|
||||||
item->setText(2, "Unsafe");
|
|
||||||
//item->setBackgroundColor(4, QColor(255, 129, 123));
|
|
||||||
//item->setForeground(4, Qt::white);
|
|
||||||
item->setForeground(4, QColor(255, 129, 123));
|
|
||||||
}
|
|
||||||
//ui->importsTreeWidget->setStyleSheet("QTreeWidget::item { padding-left:10px; padding-top: 1px; padding-bottom: 1px; border-left: 10px; }");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void ImportsWidget::setScrollMode()
|
void ImportsWidget::setScrollMode()
|
||||||
{
|
{
|
||||||
qhelpers::setVerticalScrollMode(ui->importsTreeWidget);
|
qhelpers::setVerticalScrollMode(ui->importsTreeView);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ImportsWidget::on_importsTreeWidget_itemDoubleClicked(QTreeWidgetItem *item, int /* column */)
|
void ImportsWidget::on_importsTreeView_doubleClicked(const QModelIndex &index)
|
||||||
{
|
{
|
||||||
ImportDescription imp = item->data(0, Qt::UserRole).value<ImportDescription>();
|
Core()->seek(index.data(ImportsModel::AddressRole).toLongLong());
|
||||||
CutterCore::getInstance()->seek(imp.plt);
|
|
||||||
}
|
}
|
||||||
|
@ -3,10 +3,13 @@
|
|||||||
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
|
||||||
|
#include <QAbstractTableModel>
|
||||||
|
#include <QRegularExpression>
|
||||||
#include <QStyledItemDelegate>
|
#include <QStyledItemDelegate>
|
||||||
#include <QTreeWidgetItem>
|
#include <QTreeWidgetItem>
|
||||||
|
|
||||||
#include "CutterDockWidget.h"
|
#include "CutterDockWidget.h"
|
||||||
|
#include "Cutter.h"
|
||||||
|
|
||||||
class MainWindow;
|
class MainWindow;
|
||||||
class QTreeWidget;
|
class QTreeWidget;
|
||||||
@ -15,6 +18,44 @@ namespace Ui {
|
|||||||
class ImportsWidget;
|
class ImportsWidget;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class ImportsModel : public QAbstractTableModel
|
||||||
|
{
|
||||||
|
Q_OBJECT
|
||||||
|
|
||||||
|
private:
|
||||||
|
const QRegularExpression banned = QRegularExpression(QStringLiteral(
|
||||||
|
"\\A(\\w\\.)*(system|strcpy|strcpyA|strcpyW|wcscpy|_tcscpy|_mbscpy|StrCpy|StrCpyA|StrCpyW|lstrcpy|lstrcpyA|lstrcpyW"
|
||||||
|
"|_tccpy|_mbccpy|_ftcscpy|strcat|strcatA|strcatW|wcscat|_tcscat|_mbscat|StrCat|StrCatA|StrCatW|lstrcat|lstrcatA|"
|
||||||
|
"lstrcatW|StrCatBuff|StrCatBuffA|StrCatBuffW|StrCatChainW|_tccat|_mbccat|_ftcscat|sprintfW|sprintfA|wsprintf|wsprintfW|"
|
||||||
|
"wsprintfA|sprintf|swprintf|_stprintf|wvsprintf|wvsprintfA|wvsprintfW|vsprintf|_vstprintf|vswprintf|strncpy|wcsncpy|"
|
||||||
|
"_tcsncpy|_mbsncpy|_mbsnbcpy|StrCpyN|StrCpyNA|StrCpyNW|StrNCpy|strcpynA|StrNCpyA|StrNCpyW|lstrcpyn|lstrcpynA|lstrcpynW|"
|
||||||
|
"strncat|wcsncat|_tcsncat|_mbsncat|_mbsnbcat|StrCatN|StrCatNA|StrCatNW|StrNCat|StrNCatA|StrNCatW|lstrncat|lstrcatnA|"
|
||||||
|
"lstrcatnW|lstrcatn|gets|_getts|_gettws|IsBadWritePtr|IsBadHugeWritePtr|IsBadReadPtr|IsBadHugeReadPtr|IsBadCodePtr|"
|
||||||
|
"IsBadStringPtr|memcpy|RtlCopyMemory|CopyMemory|wmemcpy|wnsprintf|wnsprintfA|wnsprintfW|_snwprintf|_snprintf|_sntprintf|"
|
||||||
|
"_vsnprintf|vsnprintf|_vsnwprintf|_vsntprintf|wvnsprintf|wvnsprintfA|wvnsprintfW|strtok|_tcstok|wcstok|_mbstok|makepath|"
|
||||||
|
"_tmakepath| _makepath|_wmakepath|_splitpath|_tsplitpath|_wsplitpath|scanf|wscanf|_tscanf|sscanf|swscanf|_stscanf|snscanf|"
|
||||||
|
"snwscanf|_sntscanf|_itoa|_itow|_i64toa|_i64tow|_ui64toa|_ui64tot|_ui64tow|_ultoa|_ultot|_ultow|CharToOem|CharToOemA|CharToOemW|"
|
||||||
|
"OemToChar|OemToCharA|OemToCharW|CharToOemBuffA|CharToOemBuffW|alloca|_alloca|strlen|wcslen|_mbslen|_mbstrlen|StrLen|lstrlen|"
|
||||||
|
"ChangeWindowMessageFilter)\\z"
|
||||||
|
));
|
||||||
|
QList<ImportDescription> *imports;
|
||||||
|
|
||||||
|
public:
|
||||||
|
enum COLUMNS {ADDRESS = 0, TYPE, SAFETY, NAME, COUNT};
|
||||||
|
static const int AddressRole = Qt::UserRole;
|
||||||
|
|
||||||
|
ImportsModel(QList<ImportDescription> *imports, 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 ImportsWidget : public CutterDockWidget
|
class ImportsWidget : public CutterDockWidget
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
@ -24,24 +65,17 @@ public:
|
|||||||
~ImportsWidget();
|
~ImportsWidget();
|
||||||
|
|
||||||
private slots:
|
private slots:
|
||||||
void on_importsTreeWidget_itemDoubleClicked(QTreeWidgetItem *item, int column);
|
void on_importsTreeView_doubleClicked(const QModelIndex &index);
|
||||||
|
|
||||||
void fillImports();
|
void refreshImports();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::unique_ptr<Ui::ImportsWidget> ui;
|
std::unique_ptr<Ui::ImportsWidget> ui;
|
||||||
|
ImportsModel* model;
|
||||||
|
QList<ImportDescription> imports;
|
||||||
|
|
||||||
void highlightUnsafe();
|
void highlightUnsafe();
|
||||||
void setScrollMode();
|
void setScrollMode();
|
||||||
};
|
};
|
||||||
|
|
||||||
class CMyDelegate : public QStyledItemDelegate
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
explicit CMyDelegate(QObject *parent) : QStyledItemDelegate(parent) {}
|
|
||||||
|
|
||||||
void paint(QPainter *painter, const QStyleOptionViewItem &option,
|
|
||||||
const QModelIndex &index) const override;
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif // IMPORTSWIDGET_H
|
#endif // IMPORTSWIDGET_H
|
||||||
|
@ -28,9 +28,9 @@
|
|||||||
<number>0</number>
|
<number>0</number>
|
||||||
</property>
|
</property>
|
||||||
<item>
|
<item>
|
||||||
<widget class="QTreeWidget" name="importsTreeWidget">
|
<widget class="QTreeView" name="importsTreeView">
|
||||||
<property name="styleSheet">
|
<property name="styleSheet">
|
||||||
<string notr="true">QTreeWidget::item
|
<string notr="true">QTreeView::item
|
||||||
{
|
{
|
||||||
padding-top: 1px;
|
padding-top: 1px;
|
||||||
padding-bottom: 1px;
|
padding-bottom: 1px;
|
||||||
@ -48,26 +48,6 @@
|
|||||||
<property name="sortingEnabled">
|
<property name="sortingEnabled">
|
||||||
<bool>true</bool>
|
<bool>true</bool>
|
||||||
</property>
|
</property>
|
||||||
<column>
|
|
||||||
<property name="text">
|
|
||||||
<string>Address</string>
|
|
||||||
</property>
|
|
||||||
</column>
|
|
||||||
<column>
|
|
||||||
<property name="text">
|
|
||||||
<string>Type</string>
|
|
||||||
</property>
|
|
||||||
</column>
|
|
||||||
<column>
|
|
||||||
<property name="text">
|
|
||||||
<string>Safety</string>
|
|
||||||
</property>
|
|
||||||
</column>
|
|
||||||
<column>
|
|
||||||
<property name="text">
|
|
||||||
<string>Name</string>
|
|
||||||
</property>
|
|
||||||
</column>
|
|
||||||
</widget>
|
</widget>
|
||||||
</item>
|
</item>
|
||||||
</layout>
|
</layout>
|
||||||
|
Loading…
Reference in New Issue
Block a user