From ab6cf631f4079153180c03224cdf0ef1b3b5a9c9 Mon Sep 17 00:00:00 2001 From: Eberhard Graether Date: Sat, 20 Oct 2018 00:09:57 +0200 Subject: [PATCH] ui: Added Copy action to code view context menu --- src/lib_gui/qt/element/QtCodeArea.cpp | 31 +++++++++++++++++++-------- src/lib_gui/qt/element/QtCodeArea.h | 7 ++++-- 2 files changed, 27 insertions(+), 11 deletions(-) diff --git a/src/lib_gui/qt/element/QtCodeArea.cpp b/src/lib_gui/qt/element/QtCodeArea.cpp index a7c08a63..7da960d4 100644 --- a/src/lib_gui/qt/element/QtCodeArea.cpp +++ b/src/lib_gui/qt/element/QtCodeArea.cpp @@ -101,6 +101,7 @@ QtCodeArea::QtCodeArea( connect(this, &QtCodeArea::blockCountChanged, this, &QtCodeArea::updateLineNumberAreaWidth); connect(this, &QtCodeArea::updateRequest, this, &QtCodeArea::updateLineNumberArea); + connect(this, &QtCodeArea::copyAvailable, this, &QtCodeArea::setCopyAvailable); // MouseWheelOverScrollbarFilter is deleted by parent. horizontalScrollBar()->installEventFilter(new MouseWheelOverScrollbarFilter()); @@ -593,10 +594,10 @@ void QtCodeArea::resizeEvent(QResizeEvent *e) void QtCodeArea::mousePressEvent(QMouseEvent* event) { - clearSelection(); - if (event->button() == Qt::LeftButton) { + clearSelection(); + m_oldMousePosition = event->pos(); m_panningDistance = 0; @@ -735,6 +736,7 @@ void QtCodeArea::contextMenuEvent(QContextMenuEvent* event) menu.addSeparator(); menu.addFileActions(getSourceLocationFile()->getFilePath()); menu.addSeparator(); + menu.addAction(m_copyAction); menu.addAction(m_setIDECursorPositionAction); menu.show(); } @@ -784,6 +786,18 @@ void QtCodeArea::updateLineNumberArea(const QRect &rect, int dy) } } +void QtCodeArea::setIDECursorPosition() +{ + std::pair lineColumn = toLineColumn(this->cursorForPosition(m_eventPosition).position()); + + MessageMoveIDECursor(getSourceLocationFile()->getFilePath(), lineColumn.first, lineColumn.second).dispatch(); +} + +void QtCodeArea::setCopyAvailable(bool yes) +{ + m_copyAction->setEnabled(yes); +} + void QtCodeArea::clearSelection() { QTextCursor cursor = textCursor(); @@ -803,13 +817,6 @@ void QtCodeArea::setNewTextCursor(const QTextCursor& cursor) verticalScrollBar()->setValue(verticalValue); } -void QtCodeArea::setIDECursorPosition() -{ - std::pair lineColumn = toLineColumn(this->cursorForPosition(m_eventPosition).position()); - - MessageMoveIDECursor(getSourceLocationFile()->getFilePath(), lineColumn.first, lineColumn.second).dispatch(); -} - void QtCodeArea::activateErrors(const std::vector& annotations) { std::vector errorIds; @@ -851,6 +858,12 @@ void QtCodeArea::annotateText() void QtCodeArea::createActions() { + m_copyAction = new QAction(tr("Copy Selection"), this); + m_copyAction->setStatusTip(tr("Copy selection to clipboard")); + m_copyAction->setToolTip(tr("Copy selection to clipboard")); + m_copyAction->setEnabled(false); + connect(m_copyAction, &QAction::triggered, this, &QPlainTextEdit::copy); + m_setIDECursorPositionAction = new QAction(tr("Show in IDE (Ctrl + Left Click)"), this); #if defined(Q_OS_MAC) m_setIDECursorPositionAction->setText(tr("Show in IDE (Cmd + Left Click)")); diff --git a/src/lib_gui/qt/element/QtCodeArea.h b/src/lib_gui/qt/element/QtCodeArea.h index 2eef6ffa..2b58e207 100644 --- a/src/lib_gui/qt/element/QtCodeArea.h +++ b/src/lib_gui/qt/element/QtCodeArea.h @@ -107,11 +107,13 @@ protected: private slots: void updateLineNumberAreaWidth(int newBlockCount = 0); void updateLineNumberArea(const QRect&, int); - void clearSelection(); - void setNewTextCursor(const QTextCursor& cursor); void setIDECursorPosition(); + void setCopyAvailable(bool yes); private: + void clearSelection(); + void setNewTextCursor(const QTextCursor& cursor); + void activateErrors(const std::vector& annotations); void annotateText(); @@ -128,6 +130,7 @@ private: QPoint m_oldMousePosition; int m_panningDistance; + QAction* m_copyAction; QAction* m_setIDECursorPositionAction; QPoint m_eventPosition; // is needed for IDE cursor control via context menu // the position where the context menu is opened needs to be stored]