1 #include "CommentsDialog.h"
2 #include "ui_CommentsDialog.h"
3 
4 #include "core/Cutter.h"
5 
CommentsDialog(QWidget * parent)6 CommentsDialog::CommentsDialog(QWidget *parent) :
7     QDialog(parent),
8     ui(new Ui::CommentsDialog)
9 {
10     ui->setupUi(this);
11     setWindowFlags(windowFlags() & (~Qt::WindowContextHelpButtonHint));
12 
13     // Event filter for capturing Ctrl/Cmd+Return
14     ui->textEdit->installEventFilter(this);
15 }
16 
~CommentsDialog()17 CommentsDialog::~CommentsDialog() {}
18 
on_buttonBox_accepted()19 void CommentsDialog::on_buttonBox_accepted()
20 {
21 }
22 
on_buttonBox_rejected()23 void CommentsDialog::on_buttonBox_rejected()
24 {
25     close();
26 }
27 
getComment()28 QString CommentsDialog::getComment()
29 {
30     QString ret = ui->textEdit->document()->toPlainText();
31     return ret;
32 }
33 
setComment(const QString & comment)34 void CommentsDialog::setComment(const QString &comment)
35 {
36     ui->textEdit->document()->setPlainText(comment);
37 }
38 
addOrEditComment(RVA offset,QWidget * parent)39 void CommentsDialog::addOrEditComment(RVA offset, QWidget *parent)
40 {
41     QString oldComment = Core()->cmdRawAt("CC.", offset);
42     // Remove newline at the end added by cmd
43     oldComment.remove(oldComment.length() - 1, 1);
44     CommentsDialog c(parent);
45 
46     if (oldComment.isNull() || oldComment.isEmpty()) {
47         c.setWindowTitle(tr("Add Comment at %1").arg(RAddressString(offset)));
48     } else {
49         c.setWindowTitle(tr("Edit Comment at %1").arg(RAddressString(offset)));
50     }
51 
52     c.setComment(oldComment);
53     if (c.exec()) {
54         QString comment = c.getComment();
55         if (comment.isEmpty()) {
56             Core()->delComment(offset);
57         } else {
58             Core()->setComment(offset, comment);
59         }
60     }
61 }
62 
eventFilter(QObject *,QEvent * event)63 bool CommentsDialog::eventFilter(QObject */*obj*/, QEvent *event)
64 {
65     if (event -> type() == QEvent::KeyPress) {
66         QKeyEvent *keyEvent = static_cast <QKeyEvent *> (event);
67 
68         // Confirm comment by pressing Ctrl/Cmd+Return
69         if ((keyEvent -> modifiers() & Qt::ControlModifier) &&
70                 ((keyEvent -> key() == Qt::Key_Enter) || (keyEvent -> key() == Qt::Key_Return))) {
71             this->accept();
72             return true;
73         }
74     }
75 
76     return false;
77 }
78