29#include <QPlainTextEdit>
32#include <QSyntaxHighlighter>
34#include <QTextDocument>
45class SourceCodeView :
public QPlainTextEdit {
49 explicit SourceCodeView(QWidget *parent =
nullptr);
50 ~SourceCodeView()
override =
default;
55 bool loadFile(
const QString &filePath);
82 QTextDocument::FindFlags flags = QTextDocument::FindFlags());
87 QTextDocument::FindFlags flags = QTextDocument::FindFlags());
121 unsigned startColumn,
unsigned endLine,
139 void updateLineNumberAreaWidth(
int newBlockCount);
142 void updateLineNumberArea(
const QRect &rect,
int dy);
145 void highlightCurrentLine();
148 QString currentFilePath_;
151 QSyntaxHighlighter *keywordHighlighter_ =
nullptr;
152 QTextEdit::ExtraSelection navigationHighlight_;
153 QTextEdit::ExtraSelection searchHighlight_;
154 QTextCursor searchCursor_;
155 QString lastSearchTerm_;
156 bool suppressCursorSignal_ =
false;
157 bool mouseSelectionActive_ =
false;
160 void updateHighlights();
161 bool performFind(
const QString &term, QTextDocument::FindFlags flags);
162 void setSearchHighlight(
const QTextCursor &cursor);
163 void emitCursorPosition();
164 void emitSelectionRange();
166 int pressSelectionStart_ = 0;
167 int pressSelectionEnd_ = 0;
168 QPoint pressMousePos_;
Centralized file registry with path-to-FileID mapping.
std::size_t FileID
Type-safe identifier for registered files. 0 is reserved for invalid.
Source code location representation.
static constexpr FileID InvalidFileID
Reserved invalid FileID.
Widget for displaying line numbers beside SourceCodeView.
void keyPressEvent(QKeyEvent *event) override
Override to emit navigation on keyboard movement.
bool findPrevious(const QString &term, QTextDocument::FindFlags flags=QTextDocument::FindFlags())
Find previous occurrence of the term and highlight it.
void fileLoadError(const QString &errorMessage)
Emitted when file loading fails.
bool loadFile(const QString &filePath)
Load and display a source file.
QString currentFilePath() const
Get the currently loaded file path.
void sourcePositionClicked(FileID fileId, unsigned line, unsigned column)
Emitted when user clicks in editor.
void clearSearchHighlight()
Clear any active search highlight.
void resizeEvent(QResizeEvent *event) override
Override to resize line number area with editor.
void mouseReleaseEvent(QMouseEvent *event) override
Override to detect mouse selection end.
void highlightRange(const SourceRange &range, bool moveCursor=true)
Highlight source range with light green background.
int lineNumberAreaWidth() const
Calculate required width for line number area.
void sourceRangeSelected(FileID fileId, unsigned startLine, unsigned startColumn, unsigned endLine, unsigned endColumn)
Emitted when user selects a range in the editor.
void mousePressEvent(QMouseEvent *event) override
Override to detect mouse press for selection tracking.
void fileLoaded(const QString &filePath)
Emitted when a file is successfully loaded.
bool findNext(const QString &term, QTextDocument::FindFlags flags=QTextDocument::FindFlags())
Find next occurrence of the term and highlight it.
void applyFontSize(int pointSize)
Update font size and refresh editor metrics.
FileID currentFileId() const
Get the current file ID.
void lineNumberAreaPaintEvent(QPaintEvent *event)
Paint line numbers for visible text blocks Called by LineNumberArea::paintEvent().
void setCurrentFileId(FileID fileId)
Set the current file ID Should be called after loading a file.
void clearView()
Clear the view.
void clearHighlight()
Clear current highlight.
Represents a span of source code (begin to end).