Refactor CommentsWidget to use Model/View architecture (#469)

Add QuickFilter panel to CommentsWidget
This commit is contained in:
Paul I 2018-05-01 21:54:34 +03:00 committed by xarkes
parent f5f2c4702e
commit 41d2fdc690
3 changed files with 338 additions and 383 deletions

View File

@ -1,12 +1,229 @@
#include <QTreeWidget>
#include <QMenu>
#include <QResizeEvent>
#include <QDebug>
#include "CommentsWidget.h"
#include "ui_CommentsWidget.h"
#include "MainWindow.h"
#include "utils/Helpers.h"
CommentsModel::CommentsModel(QList<CommentDescription> *comments,
QMap<QString, QList<CommentDescription> > *nestedComments,
QObject *parent)
: QAbstractItemModel(parent),
comments(comments),
nestedComments(nestedComments),
nested(false)
{}
bool CommentsModel::isNested() const
{
return nested;
}
void CommentsModel::setNested(bool nested)
{
beginResetModel();
this->nested = nested;
endResetModel();
}
QModelIndex CommentsModel::index(int row, int column, const QModelIndex &parent) const
{
if (!parent.isValid())
return createIndex(row, column, (quintptr)0);
return createIndex(row, column, (quintptr)(parent.row() + 1));
}
QModelIndex CommentsModel::parent(const QModelIndex &index) const
{
/* Ignore invalid indexes and root nodes */
if (!index.isValid() || index.internalId() == 0)
return QModelIndex();
return this->index((int)(index.internalId() - 1), 0);
}
int CommentsModel::rowCount(const QModelIndex &parent) const
{
if (!parent.isValid())
return (isNested() ? nestedComments->size() : comments->count());
if (isNested() && parent.internalId() == 0) {
QString fcnName = nestedComments->keys().at(parent.row());
return nestedComments->operator[](fcnName).size();
}
return 0;
}
int CommentsModel::columnCount(const QModelIndex&) const
{
return (isNested() ? CommentsModel::NestedColumnCount : CommentsModel::ColumnCount);
}
QVariant CommentsModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid() || (index.internalId() != 0 && !index.parent().isValid()))
return QVariant();
int commentIndex;
bool isSubnode;
if (index.internalId() != 0) {
/* Subnode */
commentIndex = index.parent().row();
isSubnode = true;
} else {
/* Root node */
commentIndex = index.row();
isSubnode = false;
}
QString offset;
CommentDescription comment;
if (isNested()) {
offset = nestedComments->keys().at(commentIndex);
if (isSubnode) {
comment = nestedComments->operator[](offset).at(index.row());
}
} else {
comment = comments->at(commentIndex);
}
switch (role)
{
case Qt::DisplayRole:
if (isNested()) {
if (isSubnode) {
switch (index.column()) {
case OffsetNestedColumn:
return RAddressString(comment.offset);
case CommentNestedColumn:
return comment.name;
default:
break;
}
} else if (index.column() == OffsetNestedColumn) {
return offset;
}
} else {
switch (index.column()) {
case CommentsModel::OffsetColumn:
return RAddressString(comment.offset);
case CommentsModel::FunctionColumn:
return Core()->cmdFunctionAt(comment.offset);
case CommentsModel::CommentColumn:
return comment.name;
default:
break;
}
}
break;
case CommentsModel::CommentDescriptionRole:
if (isNested() && index.internalId() == 0) {
break;
}
return QVariant::fromValue(comment);
default:
break;
}
return QVariant();
}
QVariant CommentsModel::headerData(int section, Qt::Orientation, int role) const
{
if (role == Qt::DisplayRole) {
if (isNested()) {
switch (section) {
case CommentsModel::OffsetNestedColumn:
return tr("Function/Offset");
case CommentsModel::CommentNestedColumn:
return tr("Comment");
default:
break;
}
} else {
switch (section) {
case CommentsModel::OffsetColumn:
return tr("Offset");
case CommentsModel::FunctionColumn:
return tr("Function");
case CommentsModel::CommentColumn:
return tr("Comment");
default:
break;
}
}
}
return QVariant();
}
void CommentsModel::beginReloadComments()
{
beginResetModel();
}
void CommentsModel::endReloadComments()
{
endResetModel();
}
CommentsProxyModel::CommentsProxyModel(CommentsModel *sourceModel, QObject *parent)
: QSortFilterProxyModel(parent)
{
setSourceModel(sourceModel);
setFilterCaseSensitivity(Qt::CaseInsensitive);
setSortCaseSensitivity(Qt::CaseInsensitive);
}
bool CommentsProxyModel::filterAcceptsRow(int row, const QModelIndex &parent) const
{
CommentsModel *srcModel = static_cast<CommentsModel *>(sourceModel());
if (srcModel->isNested()) {
// Disable filtering
return true;
}
QModelIndex index = sourceModel()->index(row, 0, parent);
auto comment = index.data(CommentsModel::CommentDescriptionRole).value<CommentDescription>();
return comment.name.contains(filterRegExp());
}
bool CommentsProxyModel::lessThan(const QModelIndex &left, const QModelIndex &right) const
{
CommentsModel *srcModel = static_cast<CommentsModel *>(sourceModel());
if (srcModel->isNested()) {
// Disable sorting
return false;
}
if (!left.isValid() || !right.isValid())
return false;
if (left.parent().isValid() || right.parent().isValid())
return false;
auto leftComment = left.data(CommentsModel::CommentDescriptionRole).value<CommentDescription>();
auto rightComment = right.data(CommentsModel::CommentDescriptionRole).value<CommentDescription>();
switch (left.column()) {
case CommentsModel::OffsetColumn:
return leftComment.offset < rightComment.offset;
case CommentsModel::FunctionColumn:
return Core()->cmdFunctionAt(leftComment.offset) < Core()->cmdFunctionAt(rightComment.offset);
case CommentsModel::CommentColumn:
return leftComment.name < rightComment.name;
default:
break;
}
return false;
}
CommentsWidget::CommentsWidget(MainWindow *main, QAction *action) :
CutterDockWidget(main, action),
ui(new Ui::CommentsWidget),
@ -14,13 +231,27 @@ CommentsWidget::CommentsWidget(MainWindow *main, QAction *action) :
{
ui->setupUi(this);
ui->commentsTreeWidget->sortByColumn(2, Qt::AscendingOrder);
commentsModel = new CommentsModel(&comments, &nestedComments, this);
commentsProxyModel = new CommentsProxyModel(commentsModel, this);
ui->commentsTreeView->setModel(commentsProxyModel);
ui->commentsTreeView->sortByColumn(CommentsModel::CommentColumn, Qt::AscendingOrder);
QTabBar *tabs = ui->tabWidget->tabBar();
tabs->setVisible(false);
// Ctrl-F to show/hide the filter entry
QShortcut *searchShortcut = new QShortcut(QKeySequence::Find, this);
connect(searchShortcut, &QShortcut::activated, ui->quickFilterView, &QuickFilterView::showFilter);
searchShortcut->setContext(Qt::WidgetWithChildrenShortcut);
// Esc to clear the filter entry
QShortcut *clearShortcut = new QShortcut(QKeySequence(Qt::Key_Escape), this);
connect(clearShortcut, &QShortcut::activated, ui->quickFilterView, &QuickFilterView::clearFilter);
clearShortcut->setContext(Qt::WidgetWithChildrenShortcut);
connect(ui->quickFilterView, SIGNAL(filterTextChanged(const QString &)),
commentsProxyModel, SLOT(setFilterWildcard(const QString &)));
connect(ui->quickFilterView, SIGNAL(filterClosed()), ui->commentsTreeView, SLOT(setFocus()));
setScrollMode();
// Use a custom context menu on the dock title bar
//this->title_bar = this->titleBarWidget();
ui->actionHorizontal->setChecked(true);
this->setContextMenuPolicy(Qt::CustomContextMenu);
connect(this, SIGNAL(customContextMenuRequested(const QPoint &)),
@ -28,47 +259,32 @@ CommentsWidget::CommentsWidget(MainWindow *main, QAction *action) :
connect(Core(), SIGNAL(commentsChanged()), this, SLOT(refreshTree()));
connect(Core(), SIGNAL(refreshAll()), this, SLOT(refreshTree()));
// Hide the buttons frame
ui->frame->hide();
}
CommentsWidget::~CommentsWidget() {}
void CommentsWidget::on_commentsTreeWidget_itemDoubleClicked(QTreeWidgetItem *item, int column)
void CommentsWidget::on_commentsTreeView_doubleClicked(const QModelIndex &index)
{
if (column < 0)
if (!index.isValid())
return;
// Get offset and name of item double clicked
CommentDescription comment = item->data(0, Qt::UserRole).value<CommentDescription>();
if (commentsModel->isNested() && !index.parent().isValid())
return;
auto comment = index.data(CommentsModel::CommentDescriptionRole).value<CommentDescription>();
Core()->seek(comment.offset);
}
void CommentsWidget::on_nestedCmtsTreeWidget_itemDoubleClicked(QTreeWidgetItem *item, int column)
void CommentsWidget::on_actionHorizontal_triggered()
{
if (column < 0)
return;
// don't react on top-level items
if (item->parent() == nullptr) {
return;
}
// Get offset and name of item double clicked
CommentDescription comment = item->data(0, Qt::UserRole).value<CommentDescription>();
Core()->seek(comment.offset);
commentsModel->setNested(false);
ui->commentsTreeView->setIndentation(8);
}
void CommentsWidget::on_toolButton_clicked()
void CommentsWidget::on_actionVertical_triggered()
{
ui->tabWidget->setCurrentIndex(0);
}
void CommentsWidget::on_toolButton_2_clicked()
{
ui->tabWidget->setCurrentIndex(1);
commentsModel->setNested(true);
ui->commentsTreeView->setIndentation(20);
}
void CommentsWidget::showTitleContextMenu(const QPoint &pt)
@ -79,7 +295,7 @@ void CommentsWidget::showTitleContextMenu(const QPoint &pt)
menu->addAction(ui->actionHorizontal);
menu->addAction(ui->actionVertical);
if (ui->tabWidget->currentIndex() == 0) {
if (!commentsModel->isNested()) {
ui->actionHorizontal->setChecked(true);
ui->actionVertical->setChecked(false);
} else {
@ -93,18 +309,6 @@ void CommentsWidget::showTitleContextMenu(const QPoint &pt)
delete menu;
}
void CommentsWidget::on_actionHorizontal_triggered()
{
ui->tabWidget->setCurrentIndex(0);
}
void CommentsWidget::on_actionVertical_triggered()
{
ui->tabWidget->setCurrentIndex(1);
}
void CommentsWidget::resizeEvent(QResizeEvent *event)
{
if (main->responsive && isVisible()) {
@ -119,61 +323,23 @@ void CommentsWidget::resizeEvent(QResizeEvent *event)
QDockWidget::resizeEvent(event);
}
/*
*
QMap<QString, QList<QList<QString>>> CutterCore::getNestedComments()
{
QMap<QString, QList<QList<QString>>> ret;
QString comments = cmd("CC~CCu");
for (QString line : comments.split("\n"))
{
QStringList fields = line.split("CCu");
if (fields.length() == 2)
{
QList<QString> tmp = QList<QString>();
tmp << fields[1].split("\"")[1].trimmed();
tmp << fields[0].trimmed();
QString fcn_name = this->cmdFunctionAt(fields[0].trimmed());
ret[fcn_name].append(tmp);
}
}
return ret;
}
*/
void CommentsWidget::refreshTree()
{
ui->nestedCmtsTreeWidget->clear();
QList<CommentDescription> comments = Core()->getAllComments("CCu");
QMap<QString, QList<CommentDescription>> nestedComments;
commentsModel->beginReloadComments();
comments = Core()->getAllComments("CCu");
nestedComments.clear();
for (CommentDescription comment : comments) {
QString fcn_name = Core()->cmdFunctionAt(comment.offset);
QTreeWidgetItem *item = new QTreeWidgetItem();
item->setText(0, RAddressString(comment.offset));
item->setText(1, fcn_name);
item->setText(2, comment.name);
item->setData(0, Qt::UserRole, QVariant::fromValue(comment));
ui->commentsTreeWidget->addTopLevelItem(item);
nestedComments[fcn_name].append(comment);
QString fcnName = Core()->cmdFunctionAt(comment.offset);
nestedComments[fcnName].append(comment);
}
qhelpers::adjustColumns(ui->commentsTreeWidget, 0);
// Add nested comments
ui->nestedCmtsTreeWidget->clear();
for (auto functionName : nestedComments.keys()) {
QTreeWidgetItem *item = new QTreeWidgetItem(ui->nestedCmtsTreeWidget);
item->setText(0, functionName);
for (CommentDescription comment : nestedComments.value(functionName)) {
QTreeWidgetItem *it = new QTreeWidgetItem();
it->setText(0, RAddressString(comment.offset));
it->setText(1, comment.name);
it->setData(0, Qt::UserRole, QVariant::fromValue(comment));
item->addChild(it);
}
ui->nestedCmtsTreeWidget->addTopLevelItem(item);
}
qhelpers::adjustColumns(ui->nestedCmtsTreeWidget, 0);
commentsModel->endReloadComments();
qhelpers::adjustColumns(ui->commentsTreeView, 3, 0);
}
void CommentsWidget::setScrollMode()
{
qhelpers::setVerticalScrollMode(ui->commentsTreeView);
}

View File

@ -2,7 +2,10 @@
#define COMMENTSWIDGET_H
#include <memory>
#include <QAbstractItemModel>
#include <QSortFilterProxyModel>
#include "Cutter.h"
#include "CutterDockWidget.h"
class MainWindow;
@ -12,6 +15,51 @@ namespace Ui {
class CommentsWidget;
}
class CommentsModel : public QAbstractItemModel
{
Q_OBJECT
private:
QList<CommentDescription> *comments;
QMap<QString, QList<CommentDescription>> *nestedComments;
bool nested;
public:
enum Column { OffsetColumn = 0, FunctionColumn, CommentColumn, ColumnCount };
enum NestedColumn { OffsetNestedColumn = 0, CommentNestedColumn, NestedColumnCount };
enum Role { CommentDescriptionRole = Qt::UserRole, FunctionRole };
CommentsModel(QList<CommentDescription> *comments, QMap<QString, QList<CommentDescription>> *nestedComments,
QObject *parent = Q_NULLPTR);
QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const;
QModelIndex parent(const QModelIndex &index) const;
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 beginReloadComments();
void endReloadComments();
bool isNested() const;
void setNested(bool nested);
};
class CommentsProxyModel : public QSortFilterProxyModel
{
Q_OBJECT
public:
CommentsProxyModel(CommentsModel *sourceModel, QObject *parent = Q_NULLPTR);
protected:
bool filterAcceptsRow(int row, const QModelIndex &parent) const override;
bool lessThan(const QModelIndex &left, const QModelIndex &right) const override;
};
class CommentsWidget : public CutterDockWidget
{
Q_OBJECT
@ -24,11 +72,7 @@ protected:
void resizeEvent(QResizeEvent *event) override;
private slots:
void on_commentsTreeWidget_itemDoubleClicked(QTreeWidgetItem *item, int column);
void on_nestedCmtsTreeWidget_itemDoubleClicked(QTreeWidgetItem *item, int column);
void on_toolButton_clicked();
void on_toolButton_2_clicked();
void on_commentsTreeView_doubleClicked(const QModelIndex &index);
void on_actionHorizontal_triggered();
void on_actionVertical_triggered();
@ -39,7 +83,15 @@ private slots:
private:
std::unique_ptr<Ui::CommentsWidget> ui;
MainWindow *main;
MainWindow *main;
CommentsModel *commentsModel;
CommentsProxyModel *commentsProxyModel;
QList<CommentDescription> comments;
QMap<QString, QList<CommentDescription>> nestedComments;
void setScrollMode();
};
#endif // COMMENTSWIDGET_H

View File

@ -14,7 +14,7 @@
<string notr="true">Comments</string>
</property>
<widget class="QWidget" name="dockWidgetContents">
<layout class="QHBoxLayout" name="horizontalLayout">
<layout class="QVBoxLayout" name="verticalLayout">
<property name="spacing">
<number>0</number>
</property>
@ -31,283 +31,17 @@
<number>0</number>
</property>
<item>
<widget class="QFrame" name="frame">
<property name="styleSheet">
<string notr="true">background-color: #333;</string>
<widget class="QTreeView" name="commentsTreeView">
<property name="sortingEnabled">
<bool>true</bool>
</property>
<property name="frameShape">
<enum>QFrame::NoFrame</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Raised</enum>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<property name="spacing">
<number>5</number>
</property>
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>10</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<widget class="QToolButton" name="toolButton">
<property name="toolTip">
<string notr="true">Show plain list</string>
</property>
<property name="styleSheet">
<string notr="true">QToolButton { /* all types of tool button */
border: 5px solid #333;
border-left: 10px solid #333;
border-right: 10px solid #333;
border-radius: 0px;
background-color: #333;
}
QToolButton:hover {
border: 5px solid #444;
border-left: 10px solid #444;
border-right: 10px solid #444;
border-radius: 0px;
background-color: #444;
}
QToolButton:checked {
border: 5px solid #2180a9;
border-left: 10px solid #2180a9;
border-right: 10px solid #2180a9;
border-radius: 0px;
background-color: #2180a9;
}
QToolTip {
background-color: #444;
border: 3px solid #444;
color: rgb(232, 232, 232);
}</string>
</property>
<property name="text">
<string/>
</property>
<property name="icon">
<iconset resource="../resources.qrc">
<normaloff>:/img/icons/hexdump_light.svg</normaloff>
<normalon>:/img/icons/hexdump_white.svg</normalon>
<activeon>:/img/icons/hexdump_white.svg</activeon>:/img/icons/hexdump_light.svg</iconset>
</property>
<property name="checkable">
<bool>true</bool>
</property>
<property name="checked">
<bool>true</bool>
</property>
<attribute name="buttonGroup">
<string notr="true">buttonGroup</string>
</attribute>
</widget>
</item>
<item>
<widget class="QToolButton" name="toolButton_2">
<property name="toolTip">
<string notr="true">Show grouped list</string>
</property>
<property name="styleSheet">
<string notr="true">QToolButton { /* all types of tool button */
border: 5px solid #333;
border-left: 10px solid #333;
border-right: 10px solid #333;
border-radius: 0px;
background-color: #333;
}
QToolButton:hover {
border: 5px solid #444;
border-radius: 0px;
background-color: #444;
}
QToolButton:checked {
border: 5px solid #2180a9;
border-radius: 0px;
background-color: #2180a9;
}
QToolTip {
background-color: #444;
border: 3px solid #444;
color: rgb(232, 232, 232);
}</string>
</property>
<property name="text">
<string notr="true"/>
</property>
<property name="icon">
<iconset resource="../resources.qrc">
<normaloff>:/img/icons/disas_light.svg</normaloff>
<normalon>:/img/icons/disas_white.svg</normalon>
<activeon>:/img/icons/disas_white.svg</activeon>:/img/icons/disas_light.svg</iconset>
</property>
<property name="checkable">
<bool>true</bool>
</property>
<attribute name="buttonGroup">
<string notr="true">buttonGroup</string>
</attribute>
</widget>
</item>
<item>
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
</layout>
<attribute name="headerShowSortIndicator" stdset="0">
<bool>true</bool>
</attribute>
</widget>
</item>
<item>
<widget class="QTabWidget" name="tabWidget">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="styleSheet">
<string notr="true">QTabWidget::pane { /* The tab widget frame */
border-top: 0px;
}</string>
</property>
<property name="tabPosition">
<enum>QTabWidget::West</enum>
</property>
<property name="currentIndex">
<number>0</number>
</property>
<widget class="QWidget" name="plain">
<attribute name="title">
<string>Plain</string>
</attribute>
<layout class="QVBoxLayout" name="verticalLayout_2">
<property name="spacing">
<number>0</number>
</property>
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<widget class="QTreeWidget" name="commentsTreeWidget">
<property name="styleSheet">
<string notr="true">QTreeWidget::item
{
padding-top: 1px;
padding-bottom: 1px;
}</string>
</property>
<property name="frameShape">
<enum>QFrame::NoFrame</enum>
</property>
<property name="indentation">
<number>8</number>
</property>
<property name="sortingEnabled">
<bool>true</bool>
</property>
<column>
<property name="text">
<string>Offset</string>
</property>
</column>
<column>
<property name="text">
<string>Function</string>
</property>
</column>
<column>
<property name="text">
<string>Comment</string>
</property>
</column>
</widget>
</item>
</layout>
</widget>
<widget class="QWidget" name="nested">
<attribute name="title">
<string>Nested</string>
</attribute>
<layout class="QVBoxLayout" name="verticalLayout_3">
<property name="spacing">
<number>0</number>
</property>
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<widget class="QTreeWidget" name="nestedCmtsTreeWidget">
<property name="styleSheet">
<string notr="true">QTreeWidget::item
{
padding-top: 1px;
padding-bottom: 1px;
}
QToolTip {
background-color: #444;
border: 3px solid #444;
color: rgb(232, 232, 232);
font: 11pt &quot;Monaco&quot;;
}</string>
</property>
<property name="frameShape">
<enum>QFrame::NoFrame</enum>
</property>
<column>
<property name="text">
<string>Function/Offset</string>
</property>
</column>
<column>
<property name="text">
<string>Comment</string>
</property>
</column>
</widget>
</item>
</layout>
</widget>
</widget>
<widget class="QuickFilterView" name="quickFilterView" native="true"/>
</item>
</layout>
</widget>
@ -334,11 +68,14 @@ QToolTip {
</property>
</action>
</widget>
<resources>
<include location="../resources.qrc"/>
</resources>
<customwidgets>
<customwidget>
<class>QuickFilterView</class>
<extends>QWidget</extends>
<header>widgets/QuickFilterView.h</header>
<container>1</container>
</customwidget>
</customwidgets>
<resources/>
<connections/>
<buttongroups>
<buttongroup name="buttonGroup"/>
</buttongroups>
</ui>