cutter/src/widgets/StringsWidget.cpp

295 lines
9.2 KiB
C++
Raw Normal View History

#include "StringsWidget.h"
#include "ui_StringsWidget.h"
#include "core/MainWindow.h"
2018-10-17 07:55:53 +00:00
#include "common/Helpers.h"
#include "dialogs/XrefsDialog.h"
#include "WidgetShortcuts.h"
#include <QClipboard>
2019-03-14 09:28:42 +00:00
#include <QMenu>
#include <QModelIndex>
#include <QShortcut>
StringsModel::StringsModel(QList<StringDescription> *strings, QObject *parent)
2018-03-21 20:32:32 +00:00
: QAbstractListModel(parent),
strings(strings)
{
}
int StringsModel::rowCount(const QModelIndex &) const
{
return strings->count();
}
int StringsModel::columnCount(const QModelIndex &) const
{
return StringsModel::ColumnCount;
}
QVariant StringsModel::data(const QModelIndex &index, int role) const
{
if (index.row() >= strings->count())
return QVariant();
const StringDescription &str = strings->at(index.row());
2018-03-21 20:32:32 +00:00
switch (role) {
case Qt::DisplayRole:
switch (index.column()) {
case StringsModel::OffsetColumn:
2018-03-21 20:32:32 +00:00
return RAddressString(str.vaddr);
case StringsModel::StringColumn:
2018-03-21 20:32:32 +00:00
return str.string;
case StringsModel::TypeColumn:
2018-03-21 20:32:32 +00:00
return str.type.toUpper();
case StringsModel::LengthColumn:
2018-03-21 20:32:32 +00:00
return str.length;
case StringsModel::SizeColumn:
2018-03-21 20:32:32 +00:00
return str.size;
case StringsModel::SectionColumn:
return str.section;
default:
return QVariant();
2018-03-21 20:32:32 +00:00
}
case StringDescriptionRole:
return QVariant::fromValue(str);
default:
return QVariant();
}
}
QVariant StringsModel::headerData(int section, Qt::Orientation, int role) const
{
2018-03-21 20:32:32 +00:00
switch (role) {
case Qt::DisplayRole:
switch (section) {
case StringsModel::OffsetColumn:
2018-03-21 20:32:32 +00:00
return tr("Address");
case StringsModel::StringColumn:
2018-03-21 20:32:32 +00:00
return tr("String");
case StringsModel::TypeColumn:
2018-03-21 20:32:32 +00:00
return tr("Type");
case StringsModel::LengthColumn:
2018-03-21 20:32:32 +00:00
return tr("Length");
case StringsModel::SizeColumn:
2018-03-21 20:32:32 +00:00
return tr("Size");
case StringsModel::SectionColumn:
return tr("Section");
default:
return QVariant();
2018-03-21 20:32:32 +00:00
}
default:
return QVariant();
}
}
StringsProxyModel::StringsProxyModel(StringsModel *sourceModel, QObject *parent)
2018-03-21 20:32:32 +00:00
: QSortFilterProxyModel(parent)
{
setSourceModel(sourceModel);
2017-12-26 13:40:12 +00:00
setFilterCaseSensitivity(Qt::CaseInsensitive);
setSortCaseSensitivity(Qt::CaseInsensitive);
}
2017-12-02 19:15:12 +00:00
bool StringsProxyModel::filterAcceptsRow(int row, const QModelIndex &parent) const
{
QModelIndex index = sourceModel()->index(row, 0, parent);
StringDescription str = index.data(StringsModel::StringDescriptionRole).value<StringDescription>();
if (selectedSection.isEmpty())
return str.string.contains(filterRegExp());
else
return selectedSection == str.section && str.string.contains(filterRegExp());
}
2017-12-02 19:15:12 +00:00
bool StringsProxyModel::lessThan(const QModelIndex &left, const QModelIndex &right) const
{
auto leftStr = left.data(StringsModel::StringDescriptionRole).value<StringDescription>();
auto rightStr = right.data(StringsModel::StringDescriptionRole).value<StringDescription>();
2018-03-21 20:32:32 +00:00
switch (left.column()) {
case StringsModel::OffsetColumn:
return leftStr.vaddr < rightStr.vaddr;
case StringsModel::StringColumn: // sort by string
return leftStr.string < rightStr.string;
case StringsModel::TypeColumn: // sort by type
return leftStr.type < rightStr.type;
case StringsModel::SizeColumn: // sort by size
return leftStr.size < rightStr.size;
case StringsModel::LengthColumn: // sort by length
return leftStr.length < rightStr.length;
case StringsModel::SectionColumn:
return leftStr.section < rightStr.section;
2018-03-21 20:32:32 +00:00
default:
break;
}
// fallback
return leftStr.vaddr < rightStr.vaddr;
}
StringsWidget::StringsWidget(MainWindow *main, QAction *action) :
CutterDockWidget(main, action),
ui(new Ui::StringsWidget),
tree(new CutterTreeWidget(this))
{
ui->setupUi(this);
ui->quickFilterView->setLabelText(tr("Section:"));
// Add Status Bar footer
tree->addStatusBar(ui->verticalLayout);
qhelpers::setVerticalScrollMode(ui->stringsTreeView);
// Shift-F12 to toggle strings window
QShortcut *toggle_shortcut = new QShortcut(widgetShortcuts["StringsWidget"], main);
connect(toggle_shortcut, &QShortcut::activated, this, [ = ] () {
toggleDockWidget(true);
main->updateDockActionChecked(action);
} );
connect(ui->actionCopy_String, SIGNAL(triggered()), this, SLOT(on_actionCopy()));
connect(ui->actionCopy_Address, SIGNAL(triggered()), this, SLOT(on_actionCopy()));
connect(ui->stringsTreeView, SIGNAL(customContextMenuRequested(const QPoint &)),
this, SLOT(showStringsContextMenu(const QPoint &)));
ui->actionFilter->setShortcut(QKeySequence::Find);
ui->stringsTreeView->setContextMenuPolicy(Qt::CustomContextMenu);
model = new StringsModel(&strings, this);
proxyModel = new StringsProxyModel(model, this);
ui->stringsTreeView->setModel(proxyModel);
ui->stringsTreeView->sortByColumn(StringsModel::OffsetColumn, Qt::AscendingOrder);
auto xRefShortcut = new QShortcut(QKeySequence{Qt::CTRL + Qt::Key_X}, this);
xRefShortcut->setContext(Qt::WidgetWithChildrenShortcut);
ui->actionX_refs->setShortcut(Qt::CTRL + Qt::Key_X);
connect(xRefShortcut, SIGNAL(activated()), this, SLOT(on_actionX_refs_triggered()));
connect(ui->quickFilterView, SIGNAL(filterTextChanged(const QString &)), proxyModel,
2018-03-21 20:32:32 +00:00
SLOT(setFilterWildcard(const QString &)));
2017-12-19 18:56:18 +00:00
connect(ui->quickFilterView, &ComboQuickFilterView::filterTextChanged, this, [this] {
tree->showItemsNumber(proxyModel->rowCount());
});
QShortcut *searchShortcut = new QShortcut(QKeySequence::Find, this);
connect(searchShortcut, &QShortcut::activated, ui->quickFilterView, &ComboQuickFilterView::showFilter);
searchShortcut->setContext(Qt::WidgetWithChildrenShortcut);
QShortcut *clearShortcut = new QShortcut(QKeySequence(Qt::Key_Escape), this);
connect(clearShortcut, &QShortcut::activated, ui->quickFilterView, &ComboQuickFilterView::clearFilter);
clearShortcut->setContext(Qt::WidgetWithChildrenShortcut);
connect(Core(), SIGNAL(refreshAll()), this, SLOT(refreshStrings()));
connect(
ui->quickFilterView->comboBox(), &QComboBox::currentTextChanged, this,
[this]() {
proxyModel->selectedSection = ui->quickFilterView->comboBox()->currentData().toString();
proxyModel->setFilterRegExp(proxyModel->filterRegExp());
tree->showItemsNumber(proxyModel->rowCount());
}
);
}
StringsWidget::~StringsWidget() {}
void StringsWidget::on_stringsTreeView_doubleClicked(const QModelIndex &index)
{
2018-11-05 21:51:27 +00:00
if (!index.isValid()) {
return;
2018-11-05 21:51:27 +00:00
}
StringDescription str = index.data(StringsModel::StringDescriptionRole).value<StringDescription>();
2018-04-12 06:33:30 +00:00
Core()->seek(str.vaddr);
}
void StringsWidget::refreshStrings()
2018-05-28 20:06:24 +00:00
{
if (task) {
task->wait();
}
task = QSharedPointer<StringsTask>(new StringsTask());
2018-09-30 20:00:53 +00:00
connect(task.data(), &StringsTask::stringSearchFinished, this,
&StringsWidget::stringSearchFinished);
2018-05-28 20:06:24 +00:00
Core()->getAsyncTaskManager()->start(task);
refreshSectionCombo();
}
void StringsWidget::refreshSectionCombo()
{
QComboBox *combo = ui->quickFilterView->comboBox();
combo->clear();
combo->addItem(tr("(all)"));
for (const QString &section : Core()->getSectionList()) {
combo->addItem(section, section);
}
proxyModel->selectedSection.clear();
2018-05-28 20:06:24 +00:00
}
void StringsWidget::stringSearchFinished(const QList<StringDescription> &strings)
{
model->beginResetModel();
2018-05-28 20:06:24 +00:00
this->strings = strings;
model->endResetModel();
qhelpers::adjustColumns(ui->stringsTreeView, 5, 0);
2018-02-09 14:22:45 +00:00
if (ui->stringsTreeView->columnWidth(1) > 300)
ui->stringsTreeView->setColumnWidth(1, 300);
2018-05-28 20:06:24 +00:00
tree->showItemsNumber(proxyModel->rowCount());
2018-05-28 20:06:24 +00:00
task = nullptr;
}
void StringsWidget::showStringsContextMenu(const QPoint &pt)
{
QMenu *menu = new QMenu(ui->stringsTreeView);
menu->clear();
menu->addAction(ui->actionCopy_String);
menu->addAction(ui->actionCopy_Address);
menu->addAction(ui->actionFilter);
menu->addSeparator();
menu->addAction(ui->actionX_refs);
menu->exec(ui->stringsTreeView->mapToGlobal(pt));
delete menu;
}
void StringsWidget::on_actionX_refs_triggered()
{
StringDescription str = ui->stringsTreeView->selectionModel()->currentIndex().data(
StringsModel::StringDescriptionRole).value<StringDescription>();
2019-03-24 19:35:40 +00:00
XrefsDialog x(nullptr);
x.fillRefsForAddress(str.vaddr, RAddressString(str.vaddr), false);
x.exec();
}
void StringsWidget::on_actionCopy()
{
QModelIndex current_item = ui->stringsTreeView->currentIndex();
int row = current_item.row();
QModelIndex index;
2018-11-05 21:51:27 +00:00
if (sender() == ui->actionCopy_String) {
index = ui->stringsTreeView->model()->index(row, 1);
2018-11-05 21:51:27 +00:00
} else if (sender() == ui->actionCopy_Address) {
index = ui->stringsTreeView->model()->index(row, 0);
2018-11-05 21:51:27 +00:00
}
QClipboard *clipboard = QApplication::clipboard();
clipboard->setText(index.data().toString());
}