From da46ba00980e3f96ae031c365cbb09312e2dbfc7 Mon Sep 17 00:00:00 2001 From: Derek Christ Date: Wed, 11 Aug 2021 11:49:34 +0200 Subject: [PATCH] Add ability to change the text in comments This commit lets the user change the text of already existing comments and also cleans up some of the comment deletion code. It's also now possible to delete comments with the delete key. --- .../traceAnalyzer/businessObjects/comment.h | 5 ++ .../presentation/commenttreewidget.cpp | 61 +++++++++++++++---- .../presentation/commenttreewidget.h | 22 +++++-- 3 files changed, 72 insertions(+), 16 deletions(-) diff --git a/DRAMSys/traceAnalyzer/businessObjects/comment.h b/DRAMSys/traceAnalyzer/businessObjects/comment.h index 9b6a8a9d..c52753f8 100644 --- a/DRAMSys/traceAnalyzer/businessObjects/comment.h +++ b/DRAMSys/traceAnalyzer/businessObjects/comment.h @@ -33,6 +33,7 @@ * Janik Schlemminger * Robert Gernhardt * Matthias Jung + * Derek Christ */ #ifndef COMMENT_H @@ -54,6 +55,10 @@ public: { return text; } + void changeText(QString newText) + { + text = newText; + } }; #endif // COMMENT_H diff --git a/DRAMSys/traceAnalyzer/presentation/commenttreewidget.cpp b/DRAMSys/traceAnalyzer/presentation/commenttreewidget.cpp index 17fee5e5..519e04e4 100644 --- a/DRAMSys/traceAnalyzer/presentation/commenttreewidget.cpp +++ b/DRAMSys/traceAnalyzer/presentation/commenttreewidget.cpp @@ -33,12 +33,49 @@ * Janik Schlemminger * Robert Gernhardt * Matthias Jung + * Derek Christ */ #include "commenttreewidget.h" #include #include #include +#include +#include +#include + +CommentTreeWidget::CommentTreeWidget(QWidget *parent) : QTreeWidget(parent), + deleteComment(new QAction("Delete comment", this)), + changeCommentText(new QAction("Change comment text", this)) +{ + connect(deleteComment, &QAction::triggered, this, [this](){ + for (QTreeWidgetItem *item : selectedItems()) + { + CommentTreeItem *commentItem = static_cast(item); + navigator->removeCommentAtTime(commentItem->Time()); + } + }); + + connect(changeCommentText, &QAction::triggered, this, [this](){ + for (QTreeWidgetItem *item : selectedItems()) + { + CommentTreeItem *commentItem = static_cast(item); + + bool ok; + QString newText = QInputDialog::getText(this, QString("Change comment text"), + QString("Change comment text"), QLineEdit::Normal, + QString(), &ok); + + if (ok) + { + deleteComment->trigger(); + navigator->insertComment({commentItem->Time(), newText}); + } + + commentsChanged(); + } + }); +} void CommentTreeWidget::init(TraceNavigator *navigator) { @@ -54,7 +91,6 @@ void CommentTreeWidget::init(TraceNavigator *navigator) QObject::connect(this, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(ContextMenuRequested(QPoint))); setContextMenuPolicy(Qt::CustomContextMenu); - deleteComment = new QAction("Delete comment", this); printComments(); } @@ -82,16 +118,9 @@ void CommentTreeWidget::itemDoubleClicked(QTreeWidgetItem *item, int /*column*/) void CommentTreeWidget::ContextMenuRequested(QPoint point) { - QMenu contextMenu; - contextMenu.addActions({deleteComment}); - QAction *selectedItem = contextMenu.exec(mapToGlobal(point)); - - if (selectedItem) { - for (QTreeWidgetItem *item : selectedItems()) { - CommentTreeItem *commentItem = static_cast(item); - navigator->removeCommentAtTime(commentItem->Time()); - } - } + QMenu contextMenu(this); + contextMenu.addActions({deleteComment, changeCommentText}); + contextMenu.exec(mapToGlobal(point)); } @@ -101,3 +130,13 @@ CommentTreeWidget::CommentTreeItem::CommentTreeItem(QTreeWidget *parent, this->setText(0, prettyFormatTime(comment.Time())); this->setText(1, comment.Text()); } + +void CommentTreeWidget::keyPressEvent(QKeyEvent *event) +{ + if (event->key() == Qt::Key_Delete) + deleteComment->trigger(); + else if (event->key() == Qt::Key_F2) + changeCommentText->trigger(); + + QTreeWidget::keyPressEvent(event); +} diff --git a/DRAMSys/traceAnalyzer/presentation/commenttreewidget.h b/DRAMSys/traceAnalyzer/presentation/commenttreewidget.h index 8a9d1c48..7429f75e 100644 --- a/DRAMSys/traceAnalyzer/presentation/commenttreewidget.h +++ b/DRAMSys/traceAnalyzer/presentation/commenttreewidget.h @@ -33,6 +33,7 @@ * Janik Schlemminger * Robert Gernhardt * Matthias Jung + * Derek Christ */ #ifndef COMMENTTREEWIDGET_H @@ -48,20 +49,23 @@ class CommentTreeWidget : public QTreeWidget Q_OBJECT public: - CommentTreeWidget(QWidget *parent = 0) : QTreeWidget(parent), - isInitialized(false) {} + CommentTreeWidget(QWidget *parent = nullptr); void init(TraceNavigator *navigator); -public Q_SLOTS: +protected: + void keyPressEvent(QKeyEvent *event) override; + +private Q_SLOTS: void commentsChanged(); - void itemDoubleClicked(QTreeWidgetItem *item, int column); void ContextMenuRequested(QPoint point); + void itemDoubleClicked(QTreeWidgetItem *item, int column); private: - bool isInitialized; + bool isInitialized = false; TraceNavigator *navigator; void printComments(); QAction *deleteComment; + QAction *changeCommentText; class CommentTreeItem : public QTreeWidgetItem { @@ -69,10 +73,18 @@ private: Comment comment; public: CommentTreeItem(QTreeWidget *parent, const Comment &comment); + QString Text() + { + return comment.Text(); + } traceTime Time() { return comment.Time(); } + void changeText(QString newText) + { + comment.changeText(newText); + } }; };