Refactor StringsWidget to use QTreeView+QAbstractListModel #183

This commit is contained in:
Florian Märkl 2017-12-19 19:05:57 +01:00
parent 2522e6a378
commit 326bf70ff2
3 changed files with 180 additions and 46 deletions

View File

@ -1,5 +1,6 @@
#include <QTreeWidget>
#include <QAbstractItemView>
#include <QModelIndex>
#include "StringsWidget.h"
#include "ui_StringsWidget.h"
@ -7,48 +8,146 @@
#include "utils/Helpers.h"
StringsWidget::StringsWidget(MainWindow *main, QWidget *parent) :
StringsModel::StringsModel(QList<StringDescription> *strings, QObject *parent)
: QAbstractListModel(parent),
strings(strings)
{
}
int StringsModel::rowCount(const QModelIndex &) const
{
return strings->count();
}
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());
switch (role)
{
case Qt::DisplayRole:
switch (index.column())
{
case OFFSET:
return RAddressString(str.vaddr);
case STRING:
return str.string;
default:
return QVariant();
}
case StringDescriptionRole:
return QVariant::fromValue(str);
default:
return QVariant();
}
}
QVariant StringsModel::headerData(int section, Qt::Orientation, int role) const
{
switch (role)
{
case Qt::DisplayRole:
switch (section)
{
case OFFSET:
return tr("Address");
case STRING:
return tr("String");
default:
return QVariant();
}
default:
return QVariant();
}
}
void StringsModel::beginReload()
{
beginResetModel();
}
void StringsModel::endReload()
{
endResetModel();
}
StringsSortFilterProxyModel::StringsSortFilterProxyModel(StringsModel *source_model, QObject *parent)
: QSortFilterProxyModel(parent)
{
setSourceModel(source_model);
}
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());
}
bool StringsSortFilterProxyModel::lessThan(const QModelIndex &left, const QModelIndex &right) const
{
StringDescription left_str = left.data(StringsModel::StringDescriptionRole).value<StringDescription>();
StringDescription right_str = right.data(StringsModel::StringDescriptionRole).value<StringDescription>();
switch (left.column())
{
case StringsModel::OFFSET:
if (left_str.vaddr != right_str.vaddr)
return left_str.vaddr < right_str.vaddr;
// fallthrough
case StringsModel::STRING:
return left_str.string < right_str.string;
default:
break;
}
// fallback
return left_str.vaddr < right_str.vaddr;
}
StringsWidget::StringsWidget(QWidget *parent) :
QDockWidget(parent),
ui(new Ui::StringsWidget),
main(main)
ui(new Ui::StringsWidget)
{
ui->setupUi(this);
setScrollMode();
qhelpers::setVerticalScrollMode(ui->stringsTreeView);
ui->stringsTreeWidget->sortByColumn(1, Qt::AscendingOrder);
model = new StringsModel(&strings, this);
proxy_model = new StringsSortFilterProxyModel(model, this);
ui->stringsTreeView->setModel(proxy_model);
ui->stringsTreeView->sortByColumn(StringsModel::OFFSET, Qt::AscendingOrder);
connect(Core(), SIGNAL(refreshAll()), this, SLOT(fillTreeWidget()));
connect(Core(), SIGNAL(refreshAll()), this, SLOT(refreshStrings()));
}
StringsWidget::~StringsWidget() {}
void StringsWidget::on_stringsTreeWidget_itemDoubleClicked(QTreeWidgetItem *item, int column)
void StringsWidget::on_stringsTreeView_doubleClicked(const QModelIndex &index)
{
Q_UNUSED(column);
StringDescription str = item->data(0, Qt::UserRole).value<StringDescription>();
StringDescription str = index.data(StringsModel::StringDescriptionRole).value<StringDescription>();
CutterCore::getInstance()->seek(str.vaddr);
}
void StringsWidget::fillTreeWidget()
void StringsWidget::refreshStrings()
{
ui->stringsTreeWidget->clear();
for (auto i : CutterCore::getInstance()->getAllStrings())
{
QTreeWidgetItem *item = new QTreeWidgetItem();
model->beginReload();
strings = CutterCore::getInstance()->getAllStrings();
model->endReload();
item->setText(0, RAddressString(i.vaddr));
item->setText(1, i.string);
ui->stringsTreeWidget->insertTopLevelItem(0, item);
item->setData(0, Qt::UserRole, QVariant::fromValue(i));
}
qhelpers::adjustColumns(ui->stringsTreeWidget);
}
void StringsWidget::setScrollMode()
{
qhelpers::setVerticalScrollMode(ui->stringsTreeWidget);
ui->stringsTreeView->resizeColumnToContents(0);
ui->stringsTreeView->resizeColumnToContents(1);
}

View File

@ -3,6 +3,10 @@
#include <memory>
#include "cutter.h"
#include <QAbstractListModel>
#include <QSortFilterProxyModel>
#include <QDockWidget>
class MainWindow;
@ -13,24 +17,65 @@ namespace Ui
class StringsWidget;
}
class StringsModel: public QAbstractListModel
{
Q_OBJECT
private:
QList<StringDescription> *strings;
public:
enum Columns { OFFSET = 0, STRING, COUNT };
static const int StringDescriptionRole = Qt::UserRole;
StringsModel(QList<StringDescription> *strings, 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 beginReload();
void endReload();
};
class StringsSortFilterProxyModel : public QSortFilterProxyModel
{
Q_OBJECT
public:
StringsSortFilterProxyModel(StringsModel *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;
};
class StringsWidget : public QDockWidget
{
Q_OBJECT
public:
explicit StringsWidget(MainWindow *main, QWidget *parent = 0);
explicit StringsWidget(QWidget *parent = nullptr);
~StringsWidget();
private slots:
void on_stringsTreeWidget_itemDoubleClicked(QTreeWidgetItem *item, int column);
void on_stringsTreeView_doubleClicked(const QModelIndex &index);
void fillTreeWidget();
void refreshStrings();
private:
std::unique_ptr<Ui::StringsWidget> ui;
MainWindow *main;
void setScrollMode();
StringsModel *model;
StringsSortFilterProxyModel *proxy_model;
QList<StringDescription> strings;
};
#endif // STRINGSWIDGET_H

View File

@ -28,7 +28,7 @@
<number>0</number>
</property>
<item>
<widget class="QTreeWidget" name="stringsTreeWidget">
<widget class="QTreeView" name="stringsTreeView">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Preferred">
<horstretch>0</horstretch>
@ -54,16 +54,6 @@
<property name="sortingEnabled">
<bool>true</bool>
</property>
<column>
<property name="text">
<string>Address</string>
</property>
</column>
<column>
<property name="text">
<string>Name</string>
</property>
</column>
</widget>
</item>
</layout>