ui: context menu
Context menu for code view added, including option to set IDE cursor.
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
#include <qapplication.h>
|
||||
#include <QFont>
|
||||
#include <QHBoxLayout>
|
||||
#include <qmenu.h>
|
||||
#include <QPainter>
|
||||
#include <QPushButton>
|
||||
#include <QToolTip>
|
||||
@@ -84,6 +85,8 @@ QtCodeArea::QtCodeArea(
|
||||
, m_hoveredAnnotation(nullptr)
|
||||
, m_digits(0)
|
||||
, m_panningValue(-1)
|
||||
, m_setIDECursorPositionAction(nullptr)
|
||||
, m_eventPosition(0, 0)
|
||||
{
|
||||
setObjectName("code_area");
|
||||
setReadOnly(true);
|
||||
@@ -115,10 +118,17 @@ QtCodeArea::QtCodeArea(
|
||||
|
||||
// MouseWheelOverScrollbarFilter is deleted by parent.
|
||||
horizontalScrollBar()->installEventFilter(new MouseWheelOverScrollbarFilter(this));
|
||||
|
||||
createActions();
|
||||
}
|
||||
|
||||
QtCodeArea::~QtCodeArea()
|
||||
{
|
||||
if (m_setIDECursorPositionAction != nullptr)
|
||||
{
|
||||
m_setIDECursorPositionAction->disconnect();
|
||||
delete m_setIDECursorPositionAction;
|
||||
}
|
||||
}
|
||||
|
||||
QSize QtCodeArea::sizeHint() const
|
||||
@@ -282,8 +292,9 @@ void QtCodeArea::mouseReleaseEvent(QMouseEvent* event)
|
||||
|
||||
if (Qt::KeyboardModifier::ControlModifier && QApplication::keyboardModifiers())
|
||||
{
|
||||
std::pair<int, int> lineColumn = toLineColumn(this->cursorForPosition(event->pos()).position());
|
||||
MessageMoveIDECursor(m_locationFile->getFilePath().str(), lineColumn.first, lineColumn.second).dispatch();
|
||||
// std::pair<int, int> lineColumn = toLineColumn(this->cursorForPosition(event->pos()).position());
|
||||
m_eventPosition = event->pos();
|
||||
setIDECursorPosition();
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -348,6 +359,18 @@ void QtCodeArea::mouseMoveEvent(QMouseEvent* event)
|
||||
}
|
||||
}
|
||||
|
||||
void QtCodeArea::contextMenuEvent(QContextMenuEvent* event)
|
||||
{
|
||||
if (m_setIDECursorPositionAction != nullptr)
|
||||
{
|
||||
m_eventPosition = event->pos();
|
||||
|
||||
QMenu menu(this);
|
||||
menu.addAction(m_setIDECursorPositionAction);
|
||||
menu.exec(event->globalPos());
|
||||
}
|
||||
}
|
||||
|
||||
void QtCodeArea::updateLineNumberAreaWidth(int /* newBlockCount */)
|
||||
{
|
||||
setViewportMargins(lineNumberAreaWidth(), 0, 0, 0);
|
||||
@@ -377,6 +400,13 @@ void QtCodeArea::clearSelection()
|
||||
setTextCursor(cursor);
|
||||
}
|
||||
|
||||
void QtCodeArea::setIDECursorPosition()
|
||||
{
|
||||
std::pair<int, int> lineColumn = toLineColumn(this->cursorForPosition(m_eventPosition).position());
|
||||
|
||||
MessageMoveIDECursor(m_locationFile->getFilePath().str(), lineColumn.first, lineColumn.second).dispatch();
|
||||
}
|
||||
|
||||
QtCodeArea::ScopeAnnotation::ScopeAnnotation()
|
||||
: startLine(0)
|
||||
, endLine(0)
|
||||
@@ -639,3 +669,11 @@ int QtCodeArea::endTextEditPosition() const
|
||||
|
||||
return position - 1;
|
||||
}
|
||||
|
||||
void QtCodeArea::createActions()
|
||||
{
|
||||
m_setIDECursorPositionAction = new QAction(tr("Set IDE Cursor"), this);
|
||||
m_setIDECursorPositionAction->setStatusTip(tr("Set the IDE Cursor to this code position"));
|
||||
m_setIDECursorPositionAction->setToolTip(tr("Set the IDE Cursor to this code position"));
|
||||
connect(m_setIDECursorPositionAction, SIGNAL(triggered()), this, SLOT(setIDECursorPosition()));
|
||||
}
|
||||
@@ -89,10 +89,13 @@ protected:
|
||||
virtual void mousePressEvent(QMouseEvent* event);
|
||||
virtual void mouseMoveEvent(QMouseEvent* event);
|
||||
|
||||
virtual void contextMenuEvent(QContextMenuEvent* event) Q_DECL_OVERRIDE;
|
||||
|
||||
private slots:
|
||||
void updateLineNumberAreaWidth(int newBlockCount);
|
||||
void updateLineNumberArea(const QRect &, int);
|
||||
void clearSelection();
|
||||
void setIDECursorPosition();
|
||||
|
||||
private:
|
||||
struct Annotation
|
||||
@@ -130,6 +133,8 @@ private:
|
||||
int startTextEditPosition() const;
|
||||
int endTextEditPosition() const;
|
||||
|
||||
void createActions();
|
||||
|
||||
QtCodeFile* m_fileWidget;
|
||||
|
||||
QWidget* m_lineNumberArea;
|
||||
@@ -147,6 +152,10 @@ private:
|
||||
|
||||
int m_digits;
|
||||
int m_panningValue; // just for horizontal panning
|
||||
|
||||
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
|
||||
};
|
||||
|
||||
#endif // QT_CODE_AREA_H
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#include "qt/element/QtCodeSnippet.h"
|
||||
|
||||
#include <QBoxLayout>
|
||||
#include <qmenu.h>
|
||||
#include <QPushButton>
|
||||
|
||||
#include "utility/messaging/type/MessageShowScope.h"
|
||||
@@ -139,6 +140,15 @@ bool QtCodeSnippet::isActive() const
|
||||
return m_codeArea->isActive();
|
||||
}
|
||||
|
||||
void QtCodeSnippet::contextMenuEvent(QContextMenuEvent* event)
|
||||
{
|
||||
QMenu menu(this);
|
||||
menu.addAction(new QAction("Bar", this));
|
||||
menu.addAction(new QAction("Brew Tea", this));
|
||||
menu.addAction(new QAction("Translate", this));
|
||||
menu.exec(event->globalPos());
|
||||
}
|
||||
|
||||
void QtCodeSnippet::clickedTitle()
|
||||
{
|
||||
if (m_titleId > 0)
|
||||
|
||||
@@ -42,6 +42,9 @@ public:
|
||||
|
||||
bool isActive() const;
|
||||
|
||||
protected:
|
||||
virtual void contextMenuEvent(QContextMenuEvent* event) Q_DECL_OVERRIDE;
|
||||
|
||||
private slots:
|
||||
void clickedTitle();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user