2017-10-02 16:18:40 +00:00
|
|
|
#include "CommentsWidget.h"
|
|
|
|
#include "ui_CommentsWidget.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"
|
2017-03-29 10:18:37 +00:00
|
|
|
|
2019-03-14 09:28:42 +00:00
|
|
|
#include <QMenu>
|
|
|
|
#include <QResizeEvent>
|
|
|
|
#include <QShortcut>
|
|
|
|
|
2018-05-01 18:54:34 +00:00
|
|
|
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
|
|
|
|
{
|
2019-01-13 15:41:08 +00:00
|
|
|
if (!parent.isValid()) {
|
|
|
|
return createIndex(row, column, (quintptr) 0);
|
|
|
|
}
|
2018-05-01 18:54:34 +00:00
|
|
|
|
|
|
|
return createIndex(row, column, (quintptr)(parent.row() + 1));
|
|
|
|
}
|
|
|
|
|
2019-01-13 15:41:08 +00:00
|
|
|
QModelIndex CommentsModel::parent(const QModelIndex &index) const {
|
2018-05-01 18:54:34 +00:00
|
|
|
/* Ignore invalid indexes and root nodes */
|
2019-01-13 15:41:08 +00:00
|
|
|
if (!index.isValid() || index.internalId() == 0) {
|
2018-05-01 18:54:34 +00:00
|
|
|
return QModelIndex();
|
2019-01-13 15:41:08 +00:00
|
|
|
}
|
2018-05-01 18:54:34 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-09-30 20:00:53 +00:00
|
|
|
int CommentsModel::columnCount(const QModelIndex &) const
|
2018-05-01 18:54:34 +00:00
|
|
|
{
|
2018-06-26 16:17:03 +00:00
|
|
|
return (isNested()
|
|
|
|
? static_cast<int>(CommentsModel::NestedColumnCount)
|
|
|
|
: static_cast<int>(CommentsModel::ColumnCount));
|
2018-05-01 18:54:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2018-09-30 20:00:53 +00:00
|
|
|
switch (role) {
|
2018-05-01 18:54:34 +00:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-03-16 21:46:57 +00:00
|
|
|
CommentsWidget::CommentsWidget(MainWindow *main, QAction *action) :
|
|
|
|
CutterDockWidget(main, action),
|
2017-04-13 15:14:02 +00:00
|
|
|
ui(new Ui::CommentsWidget),
|
2018-10-10 12:34:46 +00:00
|
|
|
main(main),
|
|
|
|
tree(new CutterTreeWidget(this))
|
2017-03-29 10:18:37 +00:00
|
|
|
{
|
|
|
|
ui->setupUi(this);
|
|
|
|
|
2018-10-10 12:34:46 +00:00
|
|
|
// Add Status Bar footer
|
|
|
|
tree->addStatusBar(ui->verticalLayout);
|
|
|
|
|
2018-05-01 18:54:34 +00:00
|
|
|
commentsModel = new CommentsModel(&comments, &nestedComments, this);
|
|
|
|
commentsProxyModel = new CommentsProxyModel(commentsModel, this);
|
|
|
|
ui->commentsTreeView->setModel(commentsProxyModel);
|
|
|
|
ui->commentsTreeView->sortByColumn(CommentsModel::CommentColumn, Qt::AscendingOrder);
|
|
|
|
|
|
|
|
// 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()));
|
2017-03-29 10:18:37 +00:00
|
|
|
|
2018-10-10 12:34:46 +00:00
|
|
|
connect(ui->quickFilterView, &QuickFilterView::filterTextChanged, this, [this] {
|
|
|
|
tree->showItemsNumber(commentsProxyModel->rowCount());
|
|
|
|
});
|
|
|
|
|
2018-05-01 18:54:34 +00:00
|
|
|
setScrollMode();
|
2017-03-29 10:18:37 +00:00
|
|
|
|
|
|
|
ui->actionHorizontal->setChecked(true);
|
|
|
|
this->setContextMenuPolicy(Qt::CustomContextMenu);
|
|
|
|
connect(this, SIGNAL(customContextMenuRequested(const QPoint &)),
|
|
|
|
this, SLOT(showTitleContextMenu(const QPoint &)));
|
|
|
|
|
2017-11-19 12:56:10 +00:00
|
|
|
connect(Core(), SIGNAL(commentsChanged()), this, SLOT(refreshTree()));
|
|
|
|
connect(Core(), SIGNAL(refreshAll()), this, SLOT(refreshTree()));
|
2017-03-29 10:18:37 +00:00
|
|
|
}
|
|
|
|
|
2017-10-02 09:41:28 +00:00
|
|
|
CommentsWidget::~CommentsWidget() {}
|
2017-03-29 10:18:37 +00:00
|
|
|
|
2018-05-01 18:54:34 +00:00
|
|
|
void CommentsWidget::on_commentsTreeView_doubleClicked(const QModelIndex &index)
|
2017-12-03 11:47:26 +00:00
|
|
|
{
|
2018-05-01 18:54:34 +00:00
|
|
|
if (!index.isValid())
|
2018-04-23 07:54:06 +00:00
|
|
|
return;
|
|
|
|
|
2018-05-01 18:54:34 +00:00
|
|
|
if (commentsModel->isNested() && !index.parent().isValid())
|
2017-12-03 11:47:26 +00:00
|
|
|
return;
|
2017-04-09 17:09:35 +00:00
|
|
|
|
2018-05-01 18:54:34 +00:00
|
|
|
auto comment = index.data(CommentsModel::CommentDescriptionRole).value<CommentDescription>();
|
2018-04-12 06:33:30 +00:00
|
|
|
Core()->seek(comment.offset);
|
2017-03-29 10:18:37 +00:00
|
|
|
}
|
|
|
|
|
2018-05-01 18:54:34 +00:00
|
|
|
void CommentsWidget::on_actionHorizontal_triggered()
|
2017-03-29 10:18:37 +00:00
|
|
|
{
|
2018-05-01 18:54:34 +00:00
|
|
|
commentsModel->setNested(false);
|
|
|
|
ui->commentsTreeView->setIndentation(8);
|
2017-03-29 10:18:37 +00:00
|
|
|
}
|
|
|
|
|
2018-05-01 18:54:34 +00:00
|
|
|
void CommentsWidget::on_actionVertical_triggered()
|
2017-03-29 10:18:37 +00:00
|
|
|
{
|
2018-05-01 18:54:34 +00:00
|
|
|
commentsModel->setNested(true);
|
|
|
|
ui->commentsTreeView->setIndentation(20);
|
2017-03-29 10:18:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CommentsWidget::showTitleContextMenu(const QPoint &pt)
|
|
|
|
{
|
|
|
|
// Set functions popup menu
|
|
|
|
QMenu *menu = new QMenu(this);
|
|
|
|
menu->clear();
|
|
|
|
menu->addAction(ui->actionHorizontal);
|
|
|
|
menu->addAction(ui->actionVertical);
|
|
|
|
|
2018-05-01 18:54:34 +00:00
|
|
|
if (!commentsModel->isNested()) {
|
2017-03-29 10:18:37 +00:00
|
|
|
ui->actionHorizontal->setChecked(true);
|
|
|
|
ui->actionVertical->setChecked(false);
|
2018-03-21 20:32:32 +00:00
|
|
|
} else {
|
2017-03-29 10:18:37 +00:00
|
|
|
ui->actionVertical->setChecked(true);
|
|
|
|
ui->actionHorizontal->setChecked(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
this->setContextMenuPolicy(Qt::CustomContextMenu);
|
|
|
|
|
|
|
|
menu->exec(this->mapToGlobal(pt));
|
|
|
|
delete menu;
|
|
|
|
}
|
|
|
|
|
2017-04-10 10:25:33 +00:00
|
|
|
void CommentsWidget::resizeEvent(QResizeEvent *event)
|
2017-04-09 19:55:06 +00:00
|
|
|
{
|
2018-03-21 20:32:32 +00:00
|
|
|
if (main->responsive && isVisible()) {
|
|
|
|
if (event->size().width() >= event->size().height()) {
|
2017-04-10 10:25:33 +00:00
|
|
|
// Set horizontal view (list)
|
|
|
|
on_actionHorizontal_triggered();
|
2018-03-21 20:32:32 +00:00
|
|
|
} else {
|
2017-04-10 10:25:33 +00:00
|
|
|
// Set vertical view (Tree)
|
|
|
|
on_actionVertical_triggered();
|
2017-03-29 10:18:37 +00:00
|
|
|
}
|
|
|
|
}
|
2017-04-10 10:25:33 +00:00
|
|
|
QDockWidget::resizeEvent(event);
|
2017-03-29 10:18:37 +00:00
|
|
|
}
|
2017-04-13 15:14:02 +00:00
|
|
|
|
2018-05-01 18:54:34 +00:00
|
|
|
void CommentsWidget::refreshTree()
|
2017-12-03 11:47:26 +00:00
|
|
|
{
|
2018-10-10 11:33:55 +00:00
|
|
|
commentsModel->beginResetModel();
|
2017-04-28 13:09:40 +00:00
|
|
|
|
2018-05-01 18:54:34 +00:00
|
|
|
comments = Core()->getAllComments("CCu");
|
|
|
|
nestedComments.clear();
|
2018-11-26 22:34:34 +00:00
|
|
|
for (const CommentDescription &comment : comments) {
|
2018-05-01 18:54:34 +00:00
|
|
|
QString fcnName = Core()->cmdFunctionAt(comment.offset);
|
|
|
|
nestedComments[fcnName].append(comment);
|
2017-12-03 11:47:26 +00:00
|
|
|
}
|
2018-05-01 18:54:34 +00:00
|
|
|
|
2018-10-10 11:33:55 +00:00
|
|
|
commentsModel->endResetModel();
|
2018-05-01 18:54:34 +00:00
|
|
|
|
|
|
|
qhelpers::adjustColumns(ui->commentsTreeView, 3, 0);
|
2018-10-10 12:34:46 +00:00
|
|
|
|
|
|
|
tree->showItemsNumber(commentsProxyModel->rowCount());
|
2017-12-03 11:47:26 +00:00
|
|
|
}
|
2017-04-28 13:09:40 +00:00
|
|
|
|
2018-05-01 18:54:34 +00:00
|
|
|
void CommentsWidget::setScrollMode()
|
2017-04-13 15:14:02 +00:00
|
|
|
{
|
2018-05-01 18:54:34 +00:00
|
|
|
qhelpers::setVerticalScrollMode(ui->commentsTreeView);
|
2017-04-13 15:14:02 +00:00
|
|
|
}
|