2017-10-02 16:18:40 +00:00
|
|
|
#include "StringsWidget.h"
|
|
|
|
#include "ui_StringsWidget.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"
|
2018-10-16 06:25:09 +00:00
|
|
|
#include "WidgetShortcuts.h"
|
|
|
|
|
2018-10-15 09:06:15 +00:00
|
|
|
#include <QClipboard>
|
2019-03-14 09:28:42 +00:00
|
|
|
#include <QMenu>
|
|
|
|
#include <QModelIndex>
|
|
|
|
#include <QShortcut>
|
2017-03-29 10:18:37 +00:00
|
|
|
|
2017-12-19 18:05:57 +00:00
|
|
|
StringsModel::StringsModel(QList<StringDescription> *strings, QObject *parent)
|
2019-09-01 21:30:25 +00:00
|
|
|
: AddressableItemModel<QAbstractListModel>(parent),
|
2018-03-21 20:32:32 +00:00
|
|
|
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
|
|
|
|
{
|
2018-11-17 19:17:16 +00:00
|
|
|
return StringsModel::ColumnCount;
|
2017-12-19 18:05:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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()) {
|
2018-11-17 19:17:16 +00:00
|
|
|
case StringsModel::OffsetColumn:
|
2018-03-21 20:32:32 +00:00
|
|
|
return RAddressString(str.vaddr);
|
2018-11-17 19:17:16 +00:00
|
|
|
case StringsModel::StringColumn:
|
2018-03-21 20:32:32 +00:00
|
|
|
return str.string;
|
2018-11-17 19:17:16 +00:00
|
|
|
case StringsModel::TypeColumn:
|
2018-03-21 20:32:32 +00:00
|
|
|
return str.type.toUpper();
|
2018-11-17 19:17:16 +00:00
|
|
|
case StringsModel::LengthColumn:
|
2019-09-01 21:30:25 +00:00
|
|
|
return QString::number(str.length);
|
2018-11-17 19:17:16 +00:00
|
|
|
case StringsModel::SizeColumn:
|
2019-09-01 21:30:25 +00:00
|
|
|
return QString::number(str.size);
|
2018-11-17 19:17:16 +00:00
|
|
|
case StringsModel::SectionColumn:
|
|
|
|
return str.section;
|
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) {
|
2018-11-17 19:17:16 +00:00
|
|
|
case StringsModel::OffsetColumn:
|
2018-03-21 20:32:32 +00:00
|
|
|
return tr("Address");
|
2018-11-17 19:17:16 +00:00
|
|
|
case StringsModel::StringColumn:
|
2018-03-21 20:32:32 +00:00
|
|
|
return tr("String");
|
2018-11-17 19:17:16 +00:00
|
|
|
case StringsModel::TypeColumn:
|
2018-03-21 20:32:32 +00:00
|
|
|
return tr("Type");
|
2018-11-17 19:17:16 +00:00
|
|
|
case StringsModel::LengthColumn:
|
2018-03-21 20:32:32 +00:00
|
|
|
return tr("Length");
|
2018-11-17 19:17:16 +00:00
|
|
|
case StringsModel::SizeColumn:
|
2018-03-21 20:32:32 +00:00
|
|
|
return tr("Size");
|
2018-11-17 19:17:16 +00:00
|
|
|
case StringsModel::SectionColumn:
|
|
|
|
return tr("Section");
|
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
|
|
|
|
2019-09-01 21:30:25 +00:00
|
|
|
RVA StringsModel::address(const QModelIndex &index) const
|
|
|
|
{
|
|
|
|
const StringDescription &str = strings->at(index.row());
|
|
|
|
return str.vaddr;
|
|
|
|
}
|
|
|
|
|
2020-01-20 18:36:32 +00:00
|
|
|
const StringDescription *StringsModel::description(const QModelIndex &index) const
|
|
|
|
{
|
|
|
|
return &strings->at(index.row());
|
|
|
|
}
|
|
|
|
|
2018-11-17 19:17:16 +00:00
|
|
|
StringsProxyModel::StringsProxyModel(StringsModel *sourceModel, QObject *parent)
|
2019-09-01 21:30:25 +00:00
|
|
|
: AddressableFilterProxyModel(sourceModel, parent)
|
2017-04-13 15:14:02 +00:00
|
|
|
{
|
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
|
|
|
|
2018-11-17 19:17:16 +00:00
|
|
|
bool StringsProxyModel::filterAcceptsRow(int row, const QModelIndex &parent) const
|
2017-12-19 18:05:57 +00:00
|
|
|
{
|
|
|
|
QModelIndex index = sourceModel()->index(row, 0, parent);
|
|
|
|
StringDescription str = index.data(StringsModel::StringDescriptionRole).value<StringDescription>();
|
2018-11-17 19:17:16 +00:00
|
|
|
if (selectedSection.isEmpty())
|
|
|
|
return str.string.contains(filterRegExp());
|
|
|
|
else
|
|
|
|
return selectedSection == str.section && str.string.contains(filterRegExp());
|
2017-12-19 18:05:57 +00:00
|
|
|
}
|
2017-12-02 19:15:12 +00:00
|
|
|
|
2018-11-17 19:17:16 +00:00
|
|
|
bool StringsProxyModel::lessThan(const QModelIndex &left, const QModelIndex &right) const
|
2017-12-19 18:05:57 +00:00
|
|
|
{
|
2020-01-20 18:36:32 +00:00
|
|
|
auto model = static_cast<StringsModel *>(sourceModel());
|
|
|
|
auto leftStr = model->description(left);
|
|
|
|
auto rightStr = model->description(right);
|
2018-03-21 20:32:32 +00:00
|
|
|
|
|
|
|
switch (left.column()) {
|
2018-11-17 19:17:16 +00:00
|
|
|
case StringsModel::OffsetColumn:
|
2020-01-20 18:36:32 +00:00
|
|
|
return leftStr->vaddr < rightStr->vaddr;
|
2018-11-17 19:17:16 +00:00
|
|
|
case StringsModel::StringColumn: // sort by string
|
2020-01-20 18:36:32 +00:00
|
|
|
return leftStr->string < rightStr->string;
|
2018-11-17 19:17:16 +00:00
|
|
|
case StringsModel::TypeColumn: // sort by type
|
2020-01-20 18:36:32 +00:00
|
|
|
return leftStr->type < rightStr->type;
|
2018-11-17 19:17:16 +00:00
|
|
|
case StringsModel::SizeColumn: // sort by size
|
2020-01-20 18:36:32 +00:00
|
|
|
return leftStr->size < rightStr->size;
|
2018-11-17 19:17:16 +00:00
|
|
|
case StringsModel::LengthColumn: // sort by length
|
2020-01-20 18:36:32 +00:00
|
|
|
return leftStr->length < rightStr->length;
|
2018-11-17 19:17:16 +00:00
|
|
|
case StringsModel::SectionColumn:
|
2020-01-20 18:36:32 +00:00
|
|
|
return leftStr->section < rightStr->section;
|
2018-03-21 20:32:32 +00:00
|
|
|
default:
|
|
|
|
break;
|
2017-04-13 15:14:02 +00:00
|
|
|
}
|
2017-12-19 18:05:57 +00:00
|
|
|
|
|
|
|
// fallback
|
2020-01-20 18:36:32 +00:00
|
|
|
return leftStr->vaddr < rightStr->vaddr;
|
2017-12-19 18:05:57 +00:00
|
|
|
}
|
|
|
|
|
2020-05-22 11:49:34 +00:00
|
|
|
StringsWidget::StringsWidget(MainWindow *main) :
|
|
|
|
CutterDockWidget(main),
|
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-11-17 19:17:16 +00:00
|
|
|
ui->quickFilterView->setLabelText(tr("Section:"));
|
2017-12-19 18:05:57 +00:00
|
|
|
|
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);
|
2018-11-26 18:54:39 +00:00
|
|
|
connect(toggle_shortcut, &QShortcut::activated, this, [ = ] () {
|
|
|
|
toggleDockWidget(true);
|
2020-05-22 11:49:34 +00:00
|
|
|
});
|
2018-10-16 06:25:09 +00:00
|
|
|
|
2018-10-15 09:06:15 +00:00
|
|
|
connect(ui->actionCopy_String, SIGNAL(triggered()), this, SLOT(on_actionCopy()));
|
|
|
|
|
|
|
|
ui->actionFilter->setShortcut(QKeySequence::Find);
|
|
|
|
|
|
|
|
ui->stringsTreeView->setContextMenuPolicy(Qt::CustomContextMenu);
|
|
|
|
|
2017-12-19 18:05:57 +00:00
|
|
|
model = new StringsModel(&strings, this);
|
2018-11-17 19:17:16 +00:00
|
|
|
proxyModel = new StringsProxyModel(model, this);
|
2019-09-01 21:30:25 +00:00
|
|
|
ui->stringsTreeView->setMainWindow(main);
|
2018-11-17 19:17:16 +00:00
|
|
|
ui->stringsTreeView->setModel(proxyModel);
|
2020-01-20 18:36:32 +00:00
|
|
|
ui->stringsTreeView->sortByColumn(-1, Qt::AscendingOrder);
|
2017-12-19 18:05:57 +00:00
|
|
|
|
2019-09-01 21:30:25 +00:00
|
|
|
//
|
|
|
|
auto menu = ui->stringsTreeView->getItemContextMenu();
|
|
|
|
menu->addAction(ui->actionCopy_String);
|
2018-11-26 18:54:39 +00:00
|
|
|
|
2018-11-17 19:17:16 +00:00
|
|
|
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
|
|
|
|
2018-11-17 19:17:16 +00:00
|
|
|
connect(ui->quickFilterView, &ComboQuickFilterView::filterTextChanged, this, [this] {
|
|
|
|
tree->showItemsNumber(proxyModel->rowCount());
|
2018-10-10 12:34:46 +00:00
|
|
|
});
|
2018-11-26 18:54:39 +00:00
|
|
|
|
2018-11-29 21:59:34 +00:00
|
|
|
QShortcut *searchShortcut = new QShortcut(QKeySequence::Find, this);
|
2020-01-20 18:36:32 +00:00
|
|
|
connect(searchShortcut, &QShortcut::activated, ui->quickFilterView,
|
|
|
|
&ComboQuickFilterView::showFilter);
|
2018-11-29 21:59:34 +00:00
|
|
|
searchShortcut->setContext(Qt::WidgetWithChildrenShortcut);
|
|
|
|
|
|
|
|
QShortcut *clearShortcut = new QShortcut(QKeySequence(Qt::Key_Escape), this);
|
2019-09-01 21:30:25 +00:00
|
|
|
connect(clearShortcut, &QShortcut::activated, this, [this]() {
|
|
|
|
ui->quickFilterView->clearFilter();
|
|
|
|
ui->stringsTreeView->setFocus();
|
|
|
|
});
|
2018-11-29 21:59:34 +00:00
|
|
|
clearShortcut->setContext(Qt::WidgetWithChildrenShortcut);
|
|
|
|
|
2019-12-11 22:18:40 +00:00
|
|
|
connect(Core(), &CutterCore::refreshAll, this, &StringsWidget::refreshStrings);
|
|
|
|
connect(Core(), &CutterCore::codeRebased, this, &StringsWidget::refreshStrings);
|
2018-11-17 19:17:16 +00:00
|
|
|
|
|
|
|
connect(
|
|
|
|
ui->quickFilterView->comboBox(), &QComboBox::currentTextChanged, this,
|
|
|
|
[this]() {
|
|
|
|
proxyModel->selectedSection = ui->quickFilterView->comboBox()->currentData().toString();
|
|
|
|
proxyModel->setFilterRegExp(proxyModel->filterRegExp());
|
|
|
|
tree->showItemsNumber(proxyModel->rowCount());
|
|
|
|
}
|
|
|
|
);
|
2020-01-20 18:36:32 +00:00
|
|
|
|
|
|
|
auto header = ui->stringsTreeView->header();
|
|
|
|
header->setSectionResizeMode(QHeaderView::ResizeMode::ResizeToContents);
|
|
|
|
header->setSectionResizeMode(StringsModel::StringColumn, QHeaderView::ResizeMode::Stretch);
|
|
|
|
header->setStretchLastSection(false);
|
|
|
|
header->setResizeContentsPrecision(256);
|
2017-12-19 18:05:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
StringsWidget::~StringsWidget() {}
|
|
|
|
|
|
|
|
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);
|
2018-11-17 19:17:16 +00:00
|
|
|
|
|
|
|
refreshSectionCombo();
|
|
|
|
}
|
|
|
|
|
|
|
|
void StringsWidget::refreshSectionCombo()
|
|
|
|
{
|
|
|
|
QComboBox *combo = ui->quickFilterView->comboBox();
|
|
|
|
|
|
|
|
combo->clear();
|
|
|
|
combo->addItem(tr("(all)"));
|
|
|
|
|
2018-11-26 22:34:34 +00:00
|
|
|
for (const QString §ion : Core()->getSectionList()) {
|
2018-11-17 19:17:16 +00:00
|
|
|
combo->addItem(section, section);
|
|
|
|
}
|
|
|
|
|
|
|
|
proxyModel->selectedSection.clear();
|
2018-05-28 20:06:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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-11-17 19:17:16 +00:00
|
|
|
tree->showItemsNumber(proxyModel->rowCount());
|
2018-10-10 12:34:46 +00:00
|
|
|
|
2019-10-06 17:35:44 +00:00
|
|
|
task.clear();
|
2017-03-29 10:18:37 +00:00
|
|
|
}
|
2018-10-15 09:06:15 +00:00
|
|
|
|
|
|
|
void StringsWidget::on_actionCopy()
|
|
|
|
{
|
|
|
|
QModelIndex current_item = ui->stringsTreeView->currentIndex();
|
|
|
|
int row = current_item.row();
|
|
|
|
|
|
|
|
QModelIndex index;
|
|
|
|
|
2019-09-01 21:30:25 +00:00
|
|
|
index = ui->stringsTreeView->model()->index(row, 1);
|
2018-10-15 09:06:15 +00:00
|
|
|
|
|
|
|
QClipboard *clipboard = QApplication::clipboard();
|
|
|
|
clipboard->setText(index.data().toString());
|
|
|
|
}
|