cutter/src/widgets/FlagsWidget.cpp

281 lines
7.9 KiB
C++
Raw Normal View History

#include "FlagsWidget.h"
#include "ui_FlagsWidget.h"
#include "core/MainWindow.h"
2018-03-11 16:40:52 +00:00
#include "dialogs/RenameDialog.h"
#include "dialogs/XrefsDialog.h"
2018-10-17 07:55:53 +00:00
#include "common/Helpers.h"
2019-03-14 09:28:42 +00:00
#include <QComboBox>
#include <QMenu>
#include <QShortcut>
#include <QTreeWidget>
2017-05-06 10:46:28 +00:00
FlagsModel::FlagsModel(QList<FlagDescription> *flags, QObject *parent)
2017-05-04 14:16:21 +00:00
: QAbstractListModel(parent),
flags(flags)
{
}
2017-05-06 10:46:28 +00:00
int FlagsModel::rowCount(const QModelIndex &) const
2017-05-04 14:16:21 +00:00
{
return flags->count();
}
2017-05-06 10:46:28 +00:00
int FlagsModel::columnCount(const QModelIndex &) const
2017-05-04 14:16:21 +00:00
{
return Columns::COUNT;
}
QVariant FlagsModel::data(const QModelIndex &index, int role) const
{
2017-06-03 12:27:23 +00:00
if (index.row() >= flags->count())
2017-05-04 14:16:21 +00:00
return QVariant();
const FlagDescription &flag = flags->at(index.row());
2018-03-21 20:32:32 +00:00
switch (role) {
2017-06-03 12:27:23 +00:00
case Qt::DisplayRole:
2018-03-21 20:32:32 +00:00
switch (index.column()) {
2017-06-03 12:27:23 +00:00
case SIZE:
return RSizeString(flag.size);
case OFFSET:
return RAddressString(flag.offset);
case NAME:
return flag.name;
2017-05-04 14:16:21 +00:00
default:
return QVariant();
2017-06-03 12:27:23 +00:00
}
case FlagDescriptionRole:
return QVariant::fromValue(flag);
default:
return QVariant();
2017-05-04 14:16:21 +00:00
}
}
QVariant FlagsModel::headerData(int section, Qt::Orientation, int role) const
{
2018-03-21 20:32:32 +00:00
switch (role) {
2017-06-03 12:27:23 +00:00
case Qt::DisplayRole:
2018-03-21 20:32:32 +00:00
switch (section) {
2017-06-03 12:27:23 +00:00
case SIZE:
return tr("Size");
case OFFSET:
return tr("Offset");
case NAME:
return tr("Name");
2017-05-04 14:16:21 +00:00
default:
return QVariant();
2017-06-03 12:27:23 +00:00
}
default:
return QVariant();
2017-05-04 14:16:21 +00:00
}
}
FlagsSortFilterProxyModel::FlagsSortFilterProxyModel(FlagsModel *source_model, QObject *parent)
: QSortFilterProxyModel(parent)
{
setSourceModel(source_model);
}
bool FlagsSortFilterProxyModel::filterAcceptsRow(int row, const QModelIndex &parent) const
{
QModelIndex index = sourceModel()->index(row, 0, parent);
FlagDescription flag = index.data(FlagsModel::FlagDescriptionRole).value<FlagDescription>();
return flag.name.contains(filterRegExp());
}
bool FlagsSortFilterProxyModel::lessThan(const QModelIndex &left, const QModelIndex &right) const
{
FlagDescription left_flag = left.data(FlagsModel::FlagDescriptionRole).value<FlagDescription>();
FlagDescription right_flag = right.data(FlagsModel::FlagDescriptionRole).value<FlagDescription>();
2018-03-21 20:32:32 +00:00
switch (left.column()) {
2017-06-03 12:27:23 +00:00
case FlagsModel::SIZE:
if (left_flag.size != right_flag.size)
return left_flag.size < right_flag.size;
// fallthrough
case FlagsModel::OFFSET:
if (left_flag.offset != right_flag.offset)
return left_flag.offset < right_flag.offset;
// fallthrough
case FlagsModel::NAME:
return left_flag.name < right_flag.name;
default:
break;
2017-05-04 14:16:21 +00:00
}
// fallback
return left_flag.offset < right_flag.offset;
}
FlagsWidget::FlagsWidget(MainWindow *main, QAction *action) :
CutterDockWidget(main, action),
ui(new Ui::FlagsWidget),
main(main),
tree(new CutterTreeWidget(this))
{
ui->setupUi(this);
// Add Status Bar footer
tree->addStatusBar(ui->verticalLayout);
2017-05-06 10:46:28 +00:00
flags_model = new FlagsModel(&flags, this);
2017-05-04 14:16:21 +00:00
flags_proxy_model = new FlagsSortFilterProxyModel(flags_model, this);
2018-03-21 20:32:32 +00:00
connect(ui->filterLineEdit, SIGNAL(textChanged(const QString &)), flags_proxy_model,
SLOT(setFilterWildcard(const QString &)));
2017-05-04 14:16:21 +00:00
ui->flagsTreeView->setModel(flags_proxy_model);
ui->flagsTreeView->sortByColumn(FlagsModel::OFFSET, Qt::AscendingOrder);
// Ctrl-F to move the focus to the Filter search box
QShortcut *searchShortcut = new QShortcut(QKeySequence::Find, this);
2018-10-04 15:33:19 +00:00
connect(searchShortcut, SIGNAL(activated()), ui->filterLineEdit, SLOT(setFocus()));
searchShortcut->setContext(Qt::WidgetWithChildrenShortcut);
// Esc to clear the filter entry
QShortcut *clearShortcut = new QShortcut(QKeySequence(Qt::Key_Escape), this);
connect(clearShortcut, &QShortcut::activated, [this] {
if (ui->filterLineEdit->text().isEmpty()) {
ui->flagsTreeView->setFocus();
} else {
ui->filterLineEdit->setText("");
}
});
clearShortcut->setContext(Qt::WidgetWithChildrenShortcut);
connect(ui->filterLineEdit, &QLineEdit::textChanged, this, [this] {
tree->showItemsNumber(flags_proxy_model->rowCount());
});
auto xRefShortcut = new QShortcut(QKeySequence{Qt::CTRL + Qt::Key_X}, this);
xRefShortcut->setContext(Qt::WidgetWithChildrenShortcut);
ui->actionXrefs->setShortcut(Qt::CTRL + Qt::Key_X);
connect(xRefShortcut, SIGNAL(activated()), this, SLOT(on_actionXrefs_triggered()));
setScrollMode();
2018-03-11 16:40:52 +00:00
ui->flagsTreeView->setContextMenuPolicy(Qt::CustomContextMenu);
2018-03-21 20:32:32 +00:00
connect(ui->flagsTreeView, SIGNAL(customContextMenuRequested(const QPoint &)), this,
SLOT(showContextMenu(const QPoint &)));
2018-03-11 16:40:52 +00:00
connect(Core(), SIGNAL(flagsChanged()), this, SLOT(flagsChanged()));
connect(Core(), SIGNAL(refreshAll()), this, SLOT(refreshFlagspaces()));
}
FlagsWidget::~FlagsWidget() {}
2017-05-04 14:16:21 +00:00
void FlagsWidget::on_flagsTreeView_doubleClicked(const QModelIndex &index)
{
if (!index.isValid())
return;
2017-05-04 14:16:21 +00:00
FlagDescription flag = index.data(FlagsModel::FlagDescriptionRole).value<FlagDescription>();
2018-04-12 06:33:30 +00:00
Core()->seek(flag.offset);
}
void FlagsWidget::on_flagspaceCombo_currentTextChanged(const QString &arg1)
{
2017-10-01 18:08:12 +00:00
Q_UNUSED(arg1);
refreshFlags();
}
2018-03-11 16:40:52 +00:00
void FlagsWidget::on_actionRename_triggered()
{
2018-03-21 20:32:32 +00:00
FlagDescription flag = ui->flagsTreeView->selectionModel()->currentIndex().data(
FlagsModel::FlagDescriptionRole).value<FlagDescription>();
2018-03-11 16:40:52 +00:00
RenameDialog r(this);
r.setName(flag.name);
if (r.exec()) {
QString new_name = r.getName();
2018-04-12 06:33:30 +00:00
Core()->renameFlag(flag.name, new_name);
2018-03-11 16:40:52 +00:00
}
}
void FlagsWidget::on_actionDelete_triggered()
{
2018-03-21 20:32:32 +00:00
FlagDescription flag = ui->flagsTreeView->selectionModel()->currentIndex().data(
FlagsModel::FlagDescriptionRole).value<FlagDescription>();
2018-03-11 16:40:52 +00:00
Core()->delFlag(flag.name);
}
void FlagsWidget::on_actionXrefs_triggered()
{
FlagDescription flag = ui->flagsTreeView->selectionModel()->currentIndex().data(
FlagsModel::FlagDescriptionRole).value<FlagDescription>();
XrefsDialog xresfDialog(nullptr);
xresfDialog.fillRefsForAddress(flag.offset, RAddressString(flag.offset), false);
xresfDialog.exec();
}
2018-03-11 16:40:52 +00:00
void FlagsWidget::showContextMenu(const QPoint &pt)
{
QMenu *menu = new QMenu(ui->flagsTreeView);
menu->addAction(ui->actionRename);
menu->addAction(ui->actionDelete);
menu->addSeparator();
menu->addAction(ui->actionXrefs);
2018-03-11 16:40:52 +00:00
menu->exec(ui->flagsTreeView->mapToGlobal(pt));
delete menu;
}
void FlagsWidget::flagsChanged()
{
refreshFlagspaces();
}
void FlagsWidget::refreshFlagspaces()
{
int cur_idx = ui->flagspaceCombo->currentIndex();
if (cur_idx < 0)
cur_idx = 0;
ui->flagspaceCombo->clear();
ui->flagspaceCombo->addItem(tr("(all)"));
for (const FlagspaceDescription &i : Core()->getAllFlagspaces()) {
ui->flagspaceCombo->addItem(i.name, QVariant::fromValue(i));
}
if (cur_idx > 0)
ui->flagspaceCombo->setCurrentIndex(cur_idx);
refreshFlags();
}
void FlagsWidget::refreshFlags()
{
QString flagspace;
QVariant flagspace_data = ui->flagspaceCombo->currentData();
2017-06-03 12:27:23 +00:00
if (flagspace_data.isValid())
flagspace = flagspace_data.value<FlagspaceDescription>().name;
flags_model->beginResetModel();
2018-04-12 06:33:30 +00:00
flags = Core()->getAllFlags(flagspace);
flags_model->endResetModel();
qhelpers::adjustColumns(ui->flagsTreeView, 2, 0);
tree->showItemsNumber(flags_proxy_model->rowCount());
// TODO: this is not a very good place for the following:
2017-05-04 14:16:21 +00:00
QStringList flagNames;
for (const FlagDescription &i : flags)
2017-05-04 14:16:21 +00:00
flagNames.append(i.name);
main->refreshOmniBar(flagNames);
}
void FlagsWidget::setScrollMode()
{
2017-05-04 14:16:21 +00:00
qhelpers::setVerticalScrollMode(ui->flagsTreeView);
}