2017-12-19 18:05:57 +00:00
|
|
|
|
|
|
|
#include <QModelIndex>
|
|
|
|
|
2017-10-02 16:18:40 +00:00
|
|
|
#include "StringsWidget.h"
|
|
|
|
#include "ui_StringsWidget.h"
|
2017-03-29 10:18:37 +00:00
|
|
|
|
2017-10-01 19:09:42 +00:00
|
|
|
#include "MainWindow.h"
|
2018-10-17 07:55:53 +00:00
|
|
|
#include "common/Helpers.h"
|
2018-10-15 09:06:15 +00:00
|
|
|
#include "dialogs/XrefsDialog.h"
|
2017-04-13 15:14:02 +00:00
|
|
|
|
2018-10-16 06:25:09 +00:00
|
|
|
#include "WidgetShortcuts.h"
|
|
|
|
|
2018-10-15 09:06:15 +00:00
|
|
|
#include <QMenu>
|
|
|
|
#include <QClipboard>
|
2017-03-29 10:18:37 +00:00
|
|
|
|
2017-12-19 18:05:57 +00:00
|
|
|
StringsModel::StringsModel(QList<StringDescription> *strings, QObject *parent)
|
2018-03-21 20:32:32 +00:00
|
|
|
: QAbstractListModel(parent),
|
|
|
|
strings(strings)
|
2017-03-29 10:18:37 +00:00
|
|
|
{
|
2017-12-19 18:05:57 +00:00
|
|
|
}
|
2017-03-29 10:18:37 +00:00
|
|
|
|
2017-12-19 18:05:57 +00:00
|
|
|
int StringsModel::rowCount(const QModelIndex &) const
|
|
|
|
{
|
|
|
|
return strings->count();
|
|
|
|
}
|
2017-04-13 15:14:02 +00:00
|
|
|
|
2017-12-19 18:05:57 +00:00
|
|
|
int StringsModel::columnCount(const QModelIndex &) const
|
|
|
|
{
|
|
|
|
return Columns::COUNT;
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant StringsModel::data(const QModelIndex &index, int role) const
|
|
|
|
{
|
|
|
|
if (index.row() >= strings->count())
|
|
|
|
return QVariant();
|
|
|
|
|
|
|
|
const StringDescription &str = strings->at(index.row());
|
2017-11-26 21:31:27 +00:00
|
|
|
|
2018-03-21 20:32:32 +00:00
|
|
|
switch (role) {
|
|
|
|
case Qt::DisplayRole:
|
|
|
|
switch (index.column()) {
|
|
|
|
case OFFSET:
|
|
|
|
return RAddressString(str.vaddr);
|
|
|
|
case STRING:
|
|
|
|
return str.string;
|
|
|
|
case TYPE:
|
|
|
|
return str.type.toUpper();
|
|
|
|
case LENGTH:
|
|
|
|
return str.length;
|
|
|
|
case SIZE:
|
|
|
|
return str.size;
|
2017-12-19 18:05:57 +00:00
|
|
|
default:
|
|
|
|
return QVariant();
|
2018-03-21 20:32:32 +00:00
|
|
|
}
|
|
|
|
case StringDescriptionRole:
|
|
|
|
return QVariant::fromValue(str);
|
|
|
|
default:
|
|
|
|
return QVariant();
|
2017-12-19 18:05:57 +00:00
|
|
|
}
|
2017-04-13 15:14:02 +00:00
|
|
|
}
|
|
|
|
|
2017-12-19 18:05:57 +00:00
|
|
|
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 OFFSET:
|
|
|
|
return tr("Address");
|
|
|
|
case STRING:
|
|
|
|
return tr("String");
|
|
|
|
case TYPE:
|
|
|
|
return tr("Type");
|
|
|
|
case LENGTH:
|
|
|
|
return tr("Length");
|
|
|
|
case SIZE:
|
|
|
|
return tr("Size");
|
2017-12-19 18:05:57 +00:00
|
|
|
default:
|
|
|
|
return QVariant();
|
2018-03-21 20:32:32 +00:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
return QVariant();
|
2017-12-19 18:05:57 +00:00
|
|
|
}
|
|
|
|
}
|
2017-04-13 15:14:02 +00:00
|
|
|
|
2018-03-21 20:32:32 +00:00
|
|
|
StringsSortFilterProxyModel::StringsSortFilterProxyModel(StringsModel *source_model,
|
|
|
|
QObject *parent)
|
|
|
|
: QSortFilterProxyModel(parent)
|
2017-04-13 15:14:02 +00:00
|
|
|
{
|
2017-12-19 18:05:57 +00:00
|
|
|
setSourceModel(source_model);
|
2017-12-26 13:40:12 +00:00
|
|
|
setFilterCaseSensitivity(Qt::CaseInsensitive);
|
|
|
|
setSortCaseSensitivity(Qt::CaseInsensitive);
|
2017-12-19 18:05:57 +00:00
|
|
|
}
|
2017-12-02 19:15:12 +00:00
|
|
|
|
2017-12-19 18:05:57 +00:00
|
|
|
bool StringsSortFilterProxyModel::filterAcceptsRow(int row, const QModelIndex &parent) const
|
|
|
|
{
|
|
|
|
QModelIndex index = sourceModel()->index(row, 0, parent);
|
|
|
|
StringDescription str = index.data(StringsModel::StringDescriptionRole).value<StringDescription>();
|
|
|
|
return str.string.contains(filterRegExp());
|
|
|
|
}
|
2017-12-02 19:15:12 +00:00
|
|
|
|
2017-12-19 18:05:57 +00:00
|
|
|
bool StringsSortFilterProxyModel::lessThan(const QModelIndex &left, const QModelIndex &right) const
|
|
|
|
{
|
2018-03-21 20:32:32 +00:00
|
|
|
StringDescription left_str = left.data(
|
|
|
|
StringsModel::StringDescriptionRole).value<StringDescription>();
|
|
|
|
StringDescription right_str = right.data(
|
|
|
|
StringsModel::StringDescriptionRole).value<StringDescription>();
|
|
|
|
|
|
|
|
switch (left.column()) {
|
|
|
|
case StringsModel::OFFSET:
|
|
|
|
return left_str.vaddr < right_str.vaddr;
|
|
|
|
case StringsModel::STRING: // sort by string
|
|
|
|
return left_str.string < right_str.string;
|
|
|
|
case StringsModel::TYPE: // sort by type
|
|
|
|
return left_str.type < right_str.type;
|
|
|
|
case StringsModel::SIZE: // sort by size
|
|
|
|
return left_str.size < right_str.size;
|
|
|
|
case StringsModel::LENGTH: // sort by length
|
|
|
|
return left_str.length < right_str.length;
|
|
|
|
default:
|
|
|
|
break;
|
2017-04-13 15:14:02 +00:00
|
|
|
}
|
2017-12-19 18:05:57 +00:00
|
|
|
|
|
|
|
// fallback
|
|
|
|
return left_str.vaddr < right_str.vaddr;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-03-16 21:46:57 +00:00
|
|
|
StringsWidget::StringsWidget(MainWindow *main, QAction *action) :
|
|
|
|
CutterDockWidget(main, action),
|
2018-10-10 12:34:46 +00:00
|
|
|
ui(new Ui::StringsWidget),
|
|
|
|
tree(new CutterTreeWidget(this))
|
2017-12-19 18:05:57 +00:00
|
|
|
{
|
|
|
|
ui->setupUi(this);
|
|
|
|
|
2018-10-10 12:34:46 +00:00
|
|
|
// Add Status Bar footer
|
|
|
|
tree->addStatusBar(ui->verticalLayout);
|
|
|
|
|
2017-12-19 18:05:57 +00:00
|
|
|
qhelpers::setVerticalScrollMode(ui->stringsTreeView);
|
|
|
|
|
2018-10-16 06:25:09 +00:00
|
|
|
// 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);
|
|
|
|
} );
|
|
|
|
|
2017-12-19 18:56:18 +00:00
|
|
|
// Ctrl-F to show/hide the filter entry
|
|
|
|
QShortcut *search_shortcut = new QShortcut(QKeySequence::Find, this);
|
|
|
|
connect(search_shortcut, &QShortcut::activated, ui->quickFilterView, &QuickFilterView::showFilter);
|
|
|
|
search_shortcut->setContext(Qt::WidgetWithChildrenShortcut);
|
|
|
|
|
|
|
|
// Esc to clear the filter entry
|
2017-12-21 11:08:49 +00:00
|
|
|
QShortcut *clear_shortcut = new QShortcut(QKeySequence(Qt::Key_Escape), this);
|
2017-12-19 18:56:18 +00:00
|
|
|
connect(clear_shortcut, &QShortcut::activated, ui->quickFilterView, &QuickFilterView::clearFilter);
|
|
|
|
clear_shortcut->setContext(Qt::WidgetWithChildrenShortcut);
|
|
|
|
|
2018-10-15 09:06:15 +00:00
|
|
|
connect(ui->actionFilter, SIGNAL(triggered()), ui->quickFilterView, SLOT(showFilter()));
|
|
|
|
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);
|
|
|
|
|
2017-12-19 18:05:57 +00:00
|
|
|
model = new StringsModel(&strings, this);
|
|
|
|
proxy_model = new StringsSortFilterProxyModel(model, this);
|
|
|
|
ui->stringsTreeView->setModel(proxy_model);
|
|
|
|
ui->stringsTreeView->sortByColumn(StringsModel::OFFSET, Qt::AscendingOrder);
|
|
|
|
|
2018-03-21 20:32:32 +00:00
|
|
|
connect(ui->quickFilterView, SIGNAL(filterTextChanged(const QString &)), proxy_model,
|
|
|
|
SLOT(setFilterWildcard(const QString &)));
|
2017-12-19 18:56:18 +00:00
|
|
|
connect(ui->quickFilterView, SIGNAL(filterClosed()), ui->stringsTreeView, SLOT(setFocus()));
|
|
|
|
|
2018-10-10 12:34:46 +00:00
|
|
|
connect(ui->quickFilterView, &QuickFilterView::filterTextChanged, this, [this] {
|
|
|
|
tree->showItemsNumber(proxy_model->rowCount());
|
|
|
|
});
|
|
|
|
|
2017-12-19 18:05:57 +00:00
|
|
|
connect(Core(), SIGNAL(refreshAll()), this, SLOT(refreshStrings()));
|
|
|
|
}
|
|
|
|
|
|
|
|
StringsWidget::~StringsWidget() {}
|
|
|
|
|
|
|
|
void StringsWidget::on_stringsTreeView_doubleClicked(const QModelIndex &index)
|
|
|
|
{
|
2018-11-05 21:51:27 +00:00
|
|
|
if (!index.isValid()) {
|
2018-04-23 07:54:06 +00:00
|
|
|
return;
|
2018-11-05 21:51:27 +00:00
|
|
|
}
|
2018-04-23 07:54:06 +00:00
|
|
|
|
2017-12-19 18:05:57 +00:00
|
|
|
StringDescription str = index.data(StringsModel::StringDescriptionRole).value<StringDescription>();
|
2018-04-12 06:33:30 +00:00
|
|
|
Core()->seek(str.vaddr);
|
2017-04-13 15:14:02 +00:00
|
|
|
}
|
|
|
|
|
2017-12-19 18:05:57 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
void StringsWidget::stringSearchFinished(const QList<StringDescription> &strings)
|
2017-04-13 15:14:02 +00:00
|
|
|
{
|
2018-10-10 11:33:55 +00:00
|
|
|
model->beginResetModel();
|
2018-05-28 20:06:24 +00:00
|
|
|
this->strings = strings;
|
2018-10-10 11:33:55 +00:00
|
|
|
model->endResetModel();
|
2017-12-19 18:05:57 +00:00
|
|
|
|
2018-04-01 08:25:31 +00:00
|
|
|
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
|
|
|
|
2018-10-10 12:34:46 +00:00
|
|
|
tree->showItemsNumber(proxy_model->rowCount());
|
|
|
|
|
2018-05-28 20:06:24 +00:00
|
|
|
task = nullptr;
|
2017-03-29 10:18:37 +00:00
|
|
|
}
|
2018-10-15 09:06:15 +00:00
|
|
|
|
|
|
|
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>();
|
|
|
|
|
|
|
|
XrefsDialog *x = new XrefsDialog(this);
|
|
|
|
x->fillRefsForAddress(str.vaddr, RAddressString(str.vaddr), false);
|
|
|
|
x->setAttribute(Qt::WA_DeleteOnClose);
|
|
|
|
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) {
|
2018-10-15 09:06:15 +00:00
|
|
|
index = ui->stringsTreeView->model()->index(row, 1);
|
2018-11-05 21:51:27 +00:00
|
|
|
} else if (sender() == ui->actionCopy_Address) {
|
2018-10-15 09:06:15 +00:00
|
|
|
index = ui->stringsTreeView->model()->index(row, 0);
|
2018-11-05 21:51:27 +00:00
|
|
|
}
|
2018-10-15 09:06:15 +00:00
|
|
|
|
|
|
|
QClipboard *clipboard = QApplication::clipboard();
|
|
|
|
clipboard->setText(index.data().toString());
|
|
|
|
}
|