2017-10-02 16:18:40 +00:00
|
|
|
#include "CommentsWidget.h"
|
2019-08-19 13:35:25 +00:00
|
|
|
#include "ui_ListDockWidget.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,
|
2019-08-19 13:35:25 +00:00
|
|
|
QList<CommentGroup> *nestedComments,
|
2018-05-01 18:54:34 +00:00
|
|
|
QObject *parent)
|
2019-08-19 13:35:25 +00:00
|
|
|
: AddressableItemModel<>(parent),
|
2018-05-01 18:54:34 +00:00
|
|
|
comments(comments),
|
|
|
|
nestedComments(nestedComments),
|
|
|
|
nested(false)
|
|
|
|
{}
|
|
|
|
|
|
|
|
bool CommentsModel::isNested() const
|
|
|
|
{
|
|
|
|
return nested;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CommentsModel::setNested(bool nested)
|
|
|
|
{
|
|
|
|
beginResetModel();
|
|
|
|
this->nested = nested;
|
|
|
|
endResetModel();
|
|
|
|
}
|
|
|
|
|
2019-08-19 13:35:25 +00:00
|
|
|
RVA CommentsModel::address(const QModelIndex &index) const
|
|
|
|
{
|
|
|
|
if (isNested()) {
|
|
|
|
if (index.internalId() != 0) {
|
|
|
|
auto &group = nestedComments->at(index.parent().row());
|
|
|
|
return group.comments.at(index.row()).offset;
|
|
|
|
} else {
|
|
|
|
return nestedComments->at(index.row()).offset;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return comments->at(index.row()).offset;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-01 18:54:34 +00:00
|
|
|
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-08-19 13:35:25 +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) {
|
2019-08-19 13:35:25 +00:00
|
|
|
return nestedComments->at(parent.row()).comments.size();
|
2018-05-01 18:54:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-08-19 13:35:25 +00:00
|
|
|
QString groupName;
|
2018-05-01 18:54:34 +00:00
|
|
|
CommentDescription comment;
|
|
|
|
if (isNested()) {
|
2019-08-19 13:35:25 +00:00
|
|
|
auto &group = nestedComments->at(commentIndex);
|
|
|
|
groupName = group.name;
|
2018-05-01 18:54:34 +00:00
|
|
|
if (isSubnode) {
|
2019-08-19 13:35:25 +00:00
|
|
|
comment = group.comments.at(index.row());
|
2018-05-01 18:54:34 +00:00
|
|
|
}
|
|
|
|
} 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) {
|
2019-08-19 13:35:25 +00:00
|
|
|
return groupName;
|
2018-05-01 18:54:34 +00:00
|
|
|
}
|
|
|
|
} 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)
|
2019-08-19 13:35:25 +00:00
|
|
|
: AddressableFilterProxyModel(sourceModel, parent)
|
2018-05-01 18:54:34 +00:00
|
|
|
{
|
|
|
|
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) :
|
2019-08-19 13:35:25 +00:00
|
|
|
ListDockWidget(main, action),
|
|
|
|
actionHorizontal(tr("Horizontal"), this),
|
|
|
|
actionVertical(tr("Vertical"), this)
|
2017-03-29 10:18:37 +00:00
|
|
|
{
|
2019-08-19 13:35:25 +00:00
|
|
|
setWindowTitle(tr("Comments"));
|
|
|
|
setObjectName("CommentsWidget");
|
2018-10-10 12:34:46 +00:00
|
|
|
|
2018-05-01 18:54:34 +00:00
|
|
|
commentsModel = new CommentsModel(&comments, &nestedComments, this);
|
|
|
|
commentsProxyModel = new CommentsProxyModel(commentsModel, this);
|
2019-08-19 13:35:25 +00:00
|
|
|
setModels(commentsProxyModel);
|
|
|
|
ui->treeView->sortByColumn(CommentsModel::CommentColumn, Qt::AscendingOrder);
|
|
|
|
|
|
|
|
titleContextMenu = new QMenu(this);
|
|
|
|
auto viewTypeGroup = new QActionGroup(titleContextMenu);
|
|
|
|
actionHorizontal.setCheckable(true);
|
|
|
|
actionHorizontal.setActionGroup(viewTypeGroup);
|
|
|
|
connect(&actionHorizontal, &QAction::toggled, this, &CommentsWidget::onActionHorizontalToggled);
|
|
|
|
actionVertical.setCheckable(true);
|
|
|
|
actionVertical.setActionGroup(viewTypeGroup);
|
|
|
|
connect(&actionVertical, &QAction::toggled, this, &CommentsWidget::onActionVerticalToggled);
|
|
|
|
titleContextMenu->addActions(viewTypeGroup->actions());
|
|
|
|
|
|
|
|
|
|
|
|
actionHorizontal.setChecked(true);
|
2017-03-29 10:18:37 +00:00
|
|
|
this->setContextMenuPolicy(Qt::CustomContextMenu);
|
2019-08-19 13:35:25 +00:00
|
|
|
connect(this, &QWidget::customContextMenuRequested,
|
|
|
|
this, &CommentsWidget::showTitleContextMenu);
|
2017-03-29 10:18:37 +00:00
|
|
|
|
2019-12-11 22:18:40 +00:00
|
|
|
connect(Core(), &CutterCore::codeRebased, this, &CommentsWidget::refreshTree);
|
|
|
|
connect(Core(), &CutterCore::commentsChanged, this, &CommentsWidget::refreshTree);
|
|
|
|
connect(Core(), &CutterCore::refreshAll, this, &CommentsWidget::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
|
|
|
|
2019-08-19 13:35:25 +00:00
|
|
|
void CommentsWidget::onActionHorizontalToggled(bool checked)
|
2017-12-03 11:47:26 +00:00
|
|
|
{
|
2019-08-19 13:35:25 +00:00
|
|
|
if (checked) {
|
|
|
|
commentsModel->setNested(false);
|
|
|
|
ui->treeView->setIndentation(8);
|
|
|
|
}
|
2017-03-29 10:18:37 +00:00
|
|
|
}
|
|
|
|
|
2019-08-19 13:35:25 +00:00
|
|
|
void CommentsWidget::onActionVerticalToggled(bool checked)
|
2017-03-29 10:18:37 +00:00
|
|
|
{
|
2019-08-19 13:35:25 +00:00
|
|
|
if (checked) {
|
|
|
|
commentsModel->setNested(true);
|
|
|
|
ui->treeView->setIndentation(20);
|
|
|
|
}
|
2017-03-29 10:18:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CommentsWidget::showTitleContextMenu(const QPoint &pt)
|
|
|
|
{
|
2019-08-19 13:35:25 +00:00
|
|
|
titleContextMenu->exec(this->mapToGlobal(pt));
|
2017-03-29 10:18:37 +00:00
|
|
|
}
|
|
|
|
|
2017-04-10 10:25:33 +00:00
|
|
|
void CommentsWidget::resizeEvent(QResizeEvent *event)
|
2017-04-09 19:55:06 +00:00
|
|
|
{
|
2019-08-19 13:35:25 +00:00
|
|
|
if (mainWindow->responsive && isVisible()) {
|
2018-03-21 20:32:32 +00:00
|
|
|
if (event->size().width() >= event->size().height()) {
|
2017-04-10 10:25:33 +00:00
|
|
|
// Set horizontal view (list)
|
2019-08-19 13:35:25 +00:00
|
|
|
actionHorizontal.setChecked(true);
|
2018-03-21 20:32:32 +00:00
|
|
|
} else {
|
2017-04-10 10:25:33 +00:00
|
|
|
// Set vertical view (Tree)
|
2019-08-19 13:35:25 +00:00
|
|
|
actionVertical.setChecked(true);
|
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();
|
2019-08-19 13:35:25 +00:00
|
|
|
QMap<QString, size_t> nestedCommentMapping;
|
2018-11-26 22:34:34 +00:00
|
|
|
for (const CommentDescription &comment : comments) {
|
2019-08-19 13:35:25 +00:00
|
|
|
RVA offset = RVA_INVALID;
|
|
|
|
QString fcnName = Core()->nearestFlag(comment.offset, &offset);
|
|
|
|
auto nestedCommentIt = nestedCommentMapping.find(fcnName);
|
|
|
|
if (nestedCommentIt == nestedCommentMapping.end()) {
|
|
|
|
nestedCommentMapping.insert(fcnName, nestedComments.size());
|
|
|
|
nestedComments.push_back({fcnName, offset, {comment}});
|
|
|
|
} else {
|
|
|
|
auto &commentGroup = nestedComments[nestedCommentIt.value()];
|
|
|
|
commentGroup.comments.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
|
|
|
|
2019-08-19 13:35:25 +00:00
|
|
|
qhelpers::adjustColumns(ui->treeView, 3, 0);
|
2017-12-03 11:47:26 +00:00
|
|
|
}
|
2017-04-28 13:09:40 +00:00
|
|
|
|