mirror of
https://github.com/rizinorg/cutter.git
synced 2025-01-19 02:48:49 +00:00
Use QAbstractItemModel in FlagsWidget
This commit is contained in:
parent
6f16a5a95e
commit
4e4797511b
@ -9,6 +9,126 @@
|
|||||||
#include <QComboBox>
|
#include <QComboBox>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
FlagsModel::FlagsModel(QList<FlagDescription> *flags, MainWindow *main, QObject *parent)
|
||||||
|
: QAbstractListModel(parent),
|
||||||
|
main(main),
|
||||||
|
flags(flags)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
int FlagsModel::rowCount(const QModelIndex &parent) const
|
||||||
|
{
|
||||||
|
return flags->count();
|
||||||
|
}
|
||||||
|
|
||||||
|
int FlagsModel::columnCount(const QModelIndex &parent) const
|
||||||
|
{
|
||||||
|
return Columns::COUNT;
|
||||||
|
}
|
||||||
|
|
||||||
|
QVariant FlagsModel::data(const QModelIndex &index, int role) const
|
||||||
|
{
|
||||||
|
if(index.row() >= flags->count())
|
||||||
|
return QVariant();
|
||||||
|
|
||||||
|
const FlagDescription &flag = flags->at(index.row());
|
||||||
|
|
||||||
|
switch(role)
|
||||||
|
{
|
||||||
|
case Qt::DisplayRole:
|
||||||
|
switch(index.column())
|
||||||
|
{
|
||||||
|
case SIZE:
|
||||||
|
return RSizeString(flag.size);
|
||||||
|
case OFFSET:
|
||||||
|
return RAddressString(flag.offset);
|
||||||
|
case NAME:
|
||||||
|
return flag.name;
|
||||||
|
default:
|
||||||
|
return QVariant();
|
||||||
|
}
|
||||||
|
case FlagDescriptionRole:
|
||||||
|
return QVariant::fromValue(flag);
|
||||||
|
default:
|
||||||
|
return QVariant();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
QVariant FlagsModel::headerData(int section, Qt::Orientation, int role) const
|
||||||
|
{
|
||||||
|
switch(role)
|
||||||
|
{
|
||||||
|
case Qt::DisplayRole:
|
||||||
|
switch(section)
|
||||||
|
{
|
||||||
|
case SIZE:
|
||||||
|
return tr("Size");
|
||||||
|
case OFFSET:
|
||||||
|
return tr("Offset");
|
||||||
|
case NAME:
|
||||||
|
return tr("Name");
|
||||||
|
default:
|
||||||
|
return QVariant();
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
return QVariant();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void FlagsModel::beginReloadFlags()
|
||||||
|
{
|
||||||
|
beginResetModel();
|
||||||
|
}
|
||||||
|
|
||||||
|
void FlagsModel::endReloadFlags()
|
||||||
|
{
|
||||||
|
endResetModel();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
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>();
|
||||||
|
|
||||||
|
switch(left.column())
|
||||||
|
{
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
// fallback
|
||||||
|
return left_flag.offset < right_flag.offset;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
FlagsWidget::FlagsWidget(MainWindow *main, QWidget *parent) :
|
FlagsWidget::FlagsWidget(MainWindow *main, QWidget *parent) :
|
||||||
DockWidget(parent),
|
DockWidget(parent),
|
||||||
ui(new Ui::FlagsWidget),
|
ui(new Ui::FlagsWidget),
|
||||||
@ -16,7 +136,11 @@ FlagsWidget::FlagsWidget(MainWindow *main, QWidget *parent) :
|
|||||||
{
|
{
|
||||||
ui->setupUi(this);
|
ui->setupUi(this);
|
||||||
|
|
||||||
ui->flagsTreeWidget->hideColumn(0);
|
flags_model = new FlagsModel(&flags, main, this);
|
||||||
|
flags_proxy_model = new FlagsSortFilterProxyModel(flags_model, this);
|
||||||
|
connect(ui->filterLineEdit, SIGNAL(textChanged(const QString &)), flags_proxy_model, SLOT(setFilterWildcard(const QString &)));
|
||||||
|
ui->flagsTreeView->setModel(flags_proxy_model);
|
||||||
|
ui->flagsTreeView->sortByColumn(FlagsModel::OFFSET, Qt::AscendingOrder);
|
||||||
}
|
}
|
||||||
|
|
||||||
FlagsWidget::~FlagsWidget()
|
FlagsWidget::~FlagsWidget()
|
||||||
@ -33,21 +157,13 @@ void FlagsWidget::setup()
|
|||||||
|
|
||||||
void FlagsWidget::refresh()
|
void FlagsWidget::refresh()
|
||||||
{
|
{
|
||||||
setup();
|
refreshFlagspaces();
|
||||||
}
|
}
|
||||||
|
|
||||||
void FlagsWidget::clear()
|
void FlagsWidget::on_flagsTreeView_doubleClicked(const QModelIndex &index)
|
||||||
{
|
{
|
||||||
ui->flagsTreeWidget->clear();
|
FlagDescription flag = index.data(FlagsModel::FlagDescriptionRole).value<FlagDescription>();
|
||||||
}
|
this->main->seek(flag.offset, flag.name, true);
|
||||||
|
|
||||||
void FlagsWidget::on_flagsTreeWidget_itemDoubleClicked(QTreeWidgetItem *item, int column)
|
|
||||||
{
|
|
||||||
QNOTUSED(column);
|
|
||||||
|
|
||||||
QString offset = item->text(2);
|
|
||||||
QString name = item->text(3);
|
|
||||||
this->main->seek(offset, name, true);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void FlagsWidget::on_flagspaceCombo_currentTextChanged(const QString &arg1)
|
void FlagsWidget::on_flagspaceCombo_currentTextChanged(const QString &arg1)
|
||||||
@ -86,23 +202,20 @@ void FlagsWidget::refreshFlags()
|
|||||||
flagspace = flagspace_data.value<FlagspaceDescription>().name;
|
flagspace = flagspace_data.value<FlagspaceDescription>().name;
|
||||||
|
|
||||||
|
|
||||||
ui->flagsTreeWidget->clear();
|
flags_model->beginReloadFlags();
|
||||||
|
flags = main->core->getAllFlags(flagspace);
|
||||||
|
flags_model->endReloadFlags();
|
||||||
|
|
||||||
QStringList flags;
|
ui->flagsTreeView->resizeColumnToContents(0);
|
||||||
|
ui->flagsTreeView->resizeColumnToContents(1);
|
||||||
|
|
||||||
for (auto i : main->core->getAllFlags(flagspace))
|
QStringList flagNames;
|
||||||
{
|
for (auto i : flags)
|
||||||
QTreeWidgetItem *item = qhelpers::appendRow(ui->flagsTreeWidget, RSizeString(i.size), RAddressString(i.offset), i.name);
|
flagNames.append(i.name);
|
||||||
item->setData(0, Qt::UserRole, QVariant::fromValue(i));
|
main->refreshOmniBar(flagNames);
|
||||||
|
|
||||||
flags.append(i.name);
|
|
||||||
}
|
|
||||||
qhelpers::adjustColumns(ui->flagsTreeWidget);
|
|
||||||
|
|
||||||
main->refreshOmniBar(flags);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void FlagsWidget::setScrollMode()
|
void FlagsWidget::setScrollMode()
|
||||||
{
|
{
|
||||||
qhelpers::setVerticalScrollMode(ui->flagsTreeWidget);
|
qhelpers::setVerticalScrollMode(ui->flagsTreeView);
|
||||||
}
|
}
|
||||||
|
@ -1,11 +1,58 @@
|
|||||||
#ifndef FLAGSWIDGET_H
|
#ifndef FLAGSWIDGET_H
|
||||||
#define FLAGSWIDGET_H
|
#define FLAGSWIDGET_H
|
||||||
|
|
||||||
|
#include <QtCore/QAbstractItemModel>
|
||||||
|
#include <qrcore.h>
|
||||||
|
#include <QtCore/QSortFilterProxyModel>
|
||||||
#include "dockwidget.h"
|
#include "dockwidget.h"
|
||||||
|
|
||||||
class MainWindow;
|
class MainWindow;
|
||||||
class QTreeWidgetItem;
|
class QTreeWidgetItem;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
class FlagsModel: public QAbstractListModel
|
||||||
|
{
|
||||||
|
Q_OBJECT
|
||||||
|
|
||||||
|
private:
|
||||||
|
MainWindow *main;
|
||||||
|
QList<FlagDescription> *flags;
|
||||||
|
|
||||||
|
public:
|
||||||
|
enum Columns { OFFSET = 0, SIZE, NAME, COUNT };
|
||||||
|
static const int FlagDescriptionRole = Qt::UserRole;
|
||||||
|
|
||||||
|
FlagsModel(QList<FlagDescription> *flags, MainWindow *main, QObject *parent = 0);
|
||||||
|
|
||||||
|
int rowCount(const QModelIndex &parent = QModelIndex()) const;
|
||||||
|
int columnCount(const QModelIndex &parent = QModelIndex()) const;
|
||||||
|
|
||||||
|
QVariant data(const QModelIndex &index, int role) const;
|
||||||
|
QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;
|
||||||
|
|
||||||
|
void beginReloadFlags();
|
||||||
|
void endReloadFlags();
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
class FlagsSortFilterProxyModel : public QSortFilterProxyModel
|
||||||
|
{
|
||||||
|
Q_OBJECT
|
||||||
|
|
||||||
|
public:
|
||||||
|
FlagsSortFilterProxyModel(FlagsModel *source_model, QObject *parent = 0);
|
||||||
|
|
||||||
|
protected:
|
||||||
|
bool filterAcceptsRow(int row, const QModelIndex &parent) const override;
|
||||||
|
bool lessThan(const QModelIndex &left, const QModelIndex &right) const override;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
namespace Ui
|
namespace Ui
|
||||||
{
|
{
|
||||||
class FlagsWidget;
|
class FlagsWidget;
|
||||||
@ -20,21 +67,20 @@ public:
|
|||||||
~FlagsWidget();
|
~FlagsWidget();
|
||||||
|
|
||||||
void setup() override;
|
void setup() override;
|
||||||
|
|
||||||
void refresh() override;
|
void refresh() override;
|
||||||
|
|
||||||
void clear();
|
|
||||||
|
|
||||||
|
|
||||||
private slots:
|
private slots:
|
||||||
void on_flagsTreeWidget_itemDoubleClicked(QTreeWidgetItem *item, int column);
|
void on_flagsTreeView_doubleClicked(const QModelIndex &index);
|
||||||
|
|
||||||
void on_flagspaceCombo_currentTextChanged(const QString &arg1);
|
void on_flagspaceCombo_currentTextChanged(const QString &arg1);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Ui::FlagsWidget *ui;
|
Ui::FlagsWidget *ui;
|
||||||
MainWindow *main;
|
MainWindow *main;
|
||||||
|
|
||||||
|
FlagsModel *flags_model;
|
||||||
|
FlagsSortFilterProxyModel *flags_proxy_model;
|
||||||
|
QList<FlagDescription> flags;
|
||||||
|
|
||||||
void refreshFlags();
|
void refreshFlags();
|
||||||
void refreshFlagspaces();
|
void refreshFlagspaces();
|
||||||
void setScrollMode();
|
void setScrollMode();
|
||||||
|
@ -31,48 +31,9 @@
|
|||||||
<number>0</number>
|
<number>0</number>
|
||||||
</property>
|
</property>
|
||||||
<item>
|
<item>
|
||||||
<layout class="QHBoxLayout" name="horizontalLayout_17">
|
<widget class="QTreeView" name="flagsTreeView">
|
||||||
<property name="spacing">
|
|
||||||
<number>10</number>
|
|
||||||
</property>
|
|
||||||
<property name="leftMargin">
|
|
||||||
<number>10</number>
|
|
||||||
</property>
|
|
||||||
<property name="topMargin">
|
|
||||||
<number>0</number>
|
|
||||||
</property>
|
|
||||||
<property name="rightMargin">
|
|
||||||
<number>5</number>
|
|
||||||
</property>
|
|
||||||
<item>
|
|
||||||
<widget class="QLabel" name="flagspaceLabel">
|
|
||||||
<property name="text">
|
|
||||||
<string>Flagspace:</string>
|
|
||||||
</property>
|
|
||||||
</widget>
|
|
||||||
</item>
|
|
||||||
<item>
|
|
||||||
<widget class="QComboBox" name="flagspaceCombo"/>
|
|
||||||
</item>
|
|
||||||
<item>
|
|
||||||
<spacer name="horizontalSpacer">
|
|
||||||
<property name="orientation">
|
|
||||||
<enum>Qt::Horizontal</enum>
|
|
||||||
</property>
|
|
||||||
<property name="sizeHint" stdset="0">
|
|
||||||
<size>
|
|
||||||
<width>40</width>
|
|
||||||
<height>20</height>
|
|
||||||
</size>
|
|
||||||
</property>
|
|
||||||
</spacer>
|
|
||||||
</item>
|
|
||||||
</layout>
|
|
||||||
</item>
|
|
||||||
<item>
|
|
||||||
<widget class="QTreeWidget" name="flagsTreeWidget">
|
|
||||||
<property name="styleSheet">
|
<property name="styleSheet">
|
||||||
<string notr="true">QTreeWidget::item
|
<string notr="true">QTreeView::item
|
||||||
{
|
{
|
||||||
padding-left:10px;
|
padding-left:10px;
|
||||||
padding-top: 1px;
|
padding-top: 1px;
|
||||||
@ -80,13 +41,13 @@
|
|||||||
border-left: 10px;
|
border-left: 10px;
|
||||||
}
|
}
|
||||||
|
|
||||||
QTreeWidget::item:hover
|
QTreeView::item:hover
|
||||||
{
|
{
|
||||||
background: rgb(242, 246, 248);
|
background: rgb(242, 246, 248);
|
||||||
color: black;
|
color: black;
|
||||||
}
|
}
|
||||||
|
|
||||||
QTreeWidget::item:selected
|
QTreeView::item:selected
|
||||||
{
|
{
|
||||||
background: gray;
|
background: gray;
|
||||||
color: white;
|
color: white;
|
||||||
@ -98,40 +59,53 @@ QTreeWidget::item:selected
|
|||||||
<property name="dragEnabled">
|
<property name="dragEnabled">
|
||||||
<bool>true</bool>
|
<bool>true</bool>
|
||||||
</property>
|
</property>
|
||||||
|
<property name="indentation">
|
||||||
|
<number>0</number>
|
||||||
|
</property>
|
||||||
<property name="sortingEnabled">
|
<property name="sortingEnabled">
|
||||||
<bool>true</bool>
|
<bool>true</bool>
|
||||||
</property>
|
</property>
|
||||||
<property name="animated">
|
<property name="animated">
|
||||||
<bool>true</bool>
|
<bool>true</bool>
|
||||||
</property>
|
</property>
|
||||||
<property name="allColumnsShowFocus">
|
|
||||||
<bool>false</bool>
|
|
||||||
</property>
|
|
||||||
<property name="columnCount">
|
|
||||||
<number>4</number>
|
|
||||||
</property>
|
|
||||||
<column>
|
|
||||||
<property name="text">
|
|
||||||
<string>Dummy</string>
|
|
||||||
</property>
|
|
||||||
</column>
|
|
||||||
<column>
|
|
||||||
<property name="text">
|
|
||||||
<string>Size</string>
|
|
||||||
</property>
|
|
||||||
</column>
|
|
||||||
<column>
|
|
||||||
<property name="text">
|
|
||||||
<string>Offset</string>
|
|
||||||
</property>
|
|
||||||
</column>
|
|
||||||
<column>
|
|
||||||
<property name="text">
|
|
||||||
<string notr="true">Name</string>
|
|
||||||
</property>
|
|
||||||
</column>
|
|
||||||
</widget>
|
</widget>
|
||||||
</item>
|
</item>
|
||||||
|
<item>
|
||||||
|
<layout class="QHBoxLayout" name="horizontalLayout_17">
|
||||||
|
<property name="spacing">
|
||||||
|
<number>10</number>
|
||||||
|
</property>
|
||||||
|
<property name="leftMargin">
|
||||||
|
<number>0</number>
|
||||||
|
</property>
|
||||||
|
<property name="topMargin">
|
||||||
|
<number>0</number>
|
||||||
|
</property>
|
||||||
|
<property name="rightMargin">
|
||||||
|
<number>0</number>
|
||||||
|
</property>
|
||||||
|
<item>
|
||||||
|
<widget class="QLineEdit" name="filterLineEdit">
|
||||||
|
<property name="text">
|
||||||
|
<string/>
|
||||||
|
</property>
|
||||||
|
<property name="placeholderText">
|
||||||
|
<string>Quick Filter</string>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
|
<item>
|
||||||
|
<widget class="QLabel" name="flagspaceLabel">
|
||||||
|
<property name="text">
|
||||||
|
<string>Flagspace:</string>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
|
<item>
|
||||||
|
<widget class="QComboBox" name="flagspaceCombo"/>
|
||||||
|
</item>
|
||||||
|
</layout>
|
||||||
|
</item>
|
||||||
</layout>
|
</layout>
|
||||||
</widget>
|
</widget>
|
||||||
</widget>
|
</widget>
|
||||||
|
@ -351,8 +351,8 @@ FunctionsWidget::FunctionsWidget(MainWindow *main, QWidget *parent) :
|
|||||||
this, SLOT(showFunctionsContextMenu(const QPoint &)));
|
this, SLOT(showFunctionsContextMenu(const QPoint &)));
|
||||||
|
|
||||||
|
|
||||||
connect(ui->functionsTreeView, SIGNAL(doubleClicked(const QModelIndex &)), this, SLOT(on_functionsTreeView_itemDoubleClicked(const QModelIndex &)));
|
connect(ui->functionsTreeView, SIGNAL(doubleClicked(const QModelIndex &)), this, SLOT(functionsTreeView_doubleClicked(const QModelIndex &)));
|
||||||
connect(ui->nestedFunctionsTreeView, SIGNAL(doubleClicked(const QModelIndex &)), this, SLOT(on_functionsTreeView_itemDoubleClicked(const QModelIndex &)));
|
connect(ui->nestedFunctionsTreeView, SIGNAL(doubleClicked(const QModelIndex &)), this, SLOT(functionsTreeView_doubleClicked(const QModelIndex &)));
|
||||||
|
|
||||||
// Hide the tabs
|
// Hide the tabs
|
||||||
QTabBar *tabs = ui->tabWidget->tabBar();
|
QTabBar *tabs = ui->tabWidget->tabBar();
|
||||||
@ -410,7 +410,7 @@ QTreeView *FunctionsWidget::getCurrentTreeView()
|
|||||||
return ui->nestedFunctionsTreeView;
|
return ui->nestedFunctionsTreeView;
|
||||||
}
|
}
|
||||||
|
|
||||||
void FunctionsWidget::on_functionsTreeView_itemDoubleClicked(const QModelIndex &index)
|
void FunctionsWidget::functionsTreeView_doubleClicked(const QModelIndex &index)
|
||||||
{
|
{
|
||||||
FunctionDescription function = index.data(FunctionModel::FunctionDescriptionRole).value<FunctionDescription>();
|
FunctionDescription function = index.data(FunctionModel::FunctionDescriptionRole).value<FunctionDescription>();
|
||||||
this->main->seek(function.offset, function.name, true);
|
this->main->seek(function.offset, function.name, true);
|
||||||
|
@ -89,7 +89,7 @@ public:
|
|||||||
void refresh() override;
|
void refresh() override;
|
||||||
|
|
||||||
private slots:
|
private slots:
|
||||||
void on_functionsTreeView_itemDoubleClicked(const QModelIndex &index);
|
void functionsTreeView_doubleClicked(const QModelIndex &index);
|
||||||
void showFunctionsContextMenu(const QPoint &pt);
|
void showFunctionsContextMenu(const QPoint &pt);
|
||||||
|
|
||||||
void on_actionDisasAdd_comment_triggered();
|
void on_actionDisasAdd_comment_triggered();
|
||||||
|
Loading…
Reference in New Issue
Block a user