Add context menu to breakpoint area
MODIFY
src/codeeditor.cpp
src/codeeditor.cpp
#include "codeeditor.h"
+#include <QAction>
+#include <QMenu>
#include <QPainter>
#include <QTextBlock>
@@ -142,7 +144,8 @@ void CodeEditor::breakpointAreaPaintEvent(QPaintEvent *event) {
}
}
-void CodeEditor::breakpointClick(QMouseEvent *event) {
+void CodeEditor::breakpointClick(QMouseEvent *event, int forceState) {
+
// Get line height
QTextBlock block = firstVisibleBlock();
auto height = blockBoundingRect(block).height();
@@ -163,11 +166,17 @@ void CodeEditor::breakpointClick(QMouseEvent *event) {
int blockNumber = block.blockNumber();
if (block.isValid()) {
// Set/unset breakpoint
+ if (forceState == 1) {
+ m_breakpoints.insert(blockNumber);
+ } else if (forceState == 2) {
+ m_breakpoints.remove(blockNumber);
+ } else {
if (m_breakpoints.contains(blockNumber)) {
m_breakpoints.remove(blockNumber);
} else {
m_breakpoints.insert(blockNumber);
}
+ }
repaint();
}
}
@@ -175,4 +184,37 @@ void CodeEditor::breakpointClick(QMouseEvent *event) {
BreakpointArea::BreakpointArea(CodeEditor *editor) : QWidget(editor) {
codeEditor = editor;
setCursor(Qt::PointingHandCursor);
+
+ m_removeAction = new QAction("Remove breakpoint", this);
+ m_removeAllAction = new QAction("Remove all breakpoints", this);
+ m_addAction = new QAction("Add breakpoint", this);
+
+ connect(m_removeAction, &QAction::triggered,
+ [=] { codeEditor->breakpointClick(m_event, 2); });
+ connect(m_addAction, &QAction::triggered,
+ [=] { codeEditor->breakpointClick(m_event, 1); });
+ connect(m_removeAllAction, &QAction::triggered, [=] {
+ codeEditor->clearBreakpoints();
+ repaint();
+ });
+
+ // Construct default mouseButtonEvent
+ m_event = new QMouseEvent(QEvent::MouseButtonRelease, QPoint(0, 0),
+ Qt::LeftButton, Qt::LeftButton, Qt::NoModifier);
+}
+
+void BreakpointArea::contextMenuEvent(QContextMenuEvent *event) {
+ // setup context menu
+ QMenu contextMenu;
+
+ // Translate event to a QMouseEvent in case add/remove single breakpoint is
+ // triggered
+ *m_event = QMouseEvent(QEvent::MouseButtonRelease, event->pos(),
+ Qt::LeftButton, Qt::LeftButton, Qt::NoModifier);
+
+ contextMenu.addAction(m_addAction);
+ contextMenu.addAction(m_removeAction);
+ contextMenu.addAction(m_removeAllAction);
+
+ contextMenu.exec(event->globalPos());
}

MODIFY
src/codeeditor.h
src/codeeditor.h
@@ -17,7 +17,8 @@ public:
void lineNumberAreaPaintEvent(QPaintEvent *event);
void breakpointAreaPaintEvent(QPaintEvent *event);
- void breakpointClick(QMouseEvent *event);
+ void breakpointClick(QMouseEvent *event, int forceState);
+ void clearBreakpoints() { m_breakpoints.clear(); }
int lineNumberAreaWidth();
protected:
@@ -84,13 +85,23 @@ protected:
codeEditor->breakpointAreaPaintEvent(event);
}
+ void contextMenuEvent(QContextMenuEvent *event) override;
+
private:
CodeEditor *codeEditor;
int m_blockHeight = 0;
void mouseReleaseEvent(QMouseEvent *event) {
- codeEditor->breakpointClick(event);
+ if (event->button() == Qt::LeftButton) {
+ codeEditor->breakpointClick(event, 0);
+ }
}
+
+ QAction *m_removeAction;
+ QAction *m_addAction;
+ QAction *m_removeAllAction;
+
+ QMouseEvent *m_event;
};
#endif // CODEEDITOR_H

Add context menu to breakpoint area