ACAV f0ba4b7c9529
Abstract Syntax Tree (AST) visualization tool for C, C++, and Objective-C
Loading...
Searching...
No Matches
SourceCodeView.h
Go to the documentation of this file.
1/*$!{
2* Aurora Clang AST Viewer (ACAV)
3*
4* Copyright (c) 2026 Min Liu
5* Copyright (c) 2026 Michael David Adams
6*
7* SPDX-License-Identifier: GPL-2.0-or-later
8*
9* This program is free software; you can redistribute it and/or modify
10* it under the terms of the GNU General Public License as published by
11* the Free Software Foundation; either version 2 of the License, or
12* (at your option) any later version.
13*
14* This program is distributed in the hope that it will be useful,
15* but WITHOUT ANY WARRANTY; without even the implied warranty of
16* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17* GNU General Public License for more details.
18*
19* You should have received a copy of the GNU General Public License along
20* with this program; if not, see <https://www.gnu.org/licenses/>.
21}$!*/
22
25#pragma once
26
27#include "common/FileManager.h"
28#include "core/SourceLocation.h"
29#include <QPlainTextEdit>
30#include <QPoint>
31#include <QString>
32#include <QSyntaxHighlighter>
33#include <QTextCursor>
34#include <QTextDocument>
35
36namespace acav {
37
38class LineNumberArea; // Forward declaration
39
45class SourceCodeView : public QPlainTextEdit {
46 Q_OBJECT
47
48public:
49 explicit SourceCodeView(QWidget *parent = nullptr);
50 ~SourceCodeView() override = default;
51
55 bool loadFile(const QString &filePath);
56
58 void clearView();
59
61 void applyFontSize(int pointSize);
62
64 QString currentFilePath() const { return currentFilePath_; }
65
67 FileID currentFileId() const { return currentFileId_; }
68
71 void setCurrentFileId(FileID fileId) { currentFileId_ = fileId; }
72
75 void highlightRange(const SourceRange &range, bool moveCursor = true);
76
78 void clearHighlight();
79
81 bool findNext(const QString &term,
82 QTextDocument::FindFlags flags = QTextDocument::FindFlags());
83
85 bool findPrevious(
86 const QString &term,
87 QTextDocument::FindFlags flags = QTextDocument::FindFlags());
88
91
94 int lineNumberAreaWidth() const;
95
99 void lineNumberAreaPaintEvent(QPaintEvent *event);
100
101signals:
103 void fileLoaded(const QString &filePath);
104
106 void fileLoadError(const QString &errorMessage);
107
112 void sourcePositionClicked(FileID fileId, unsigned line, unsigned column);
113
120 void sourceRangeSelected(FileID fileId, unsigned startLine,
121 unsigned startColumn, unsigned endLine,
122 unsigned endColumn);
123
124protected:
126 void resizeEvent(QResizeEvent *event) override;
127
129 void mousePressEvent(QMouseEvent *event) override;
130
132 void mouseReleaseEvent(QMouseEvent *event) override;
133
135 void keyPressEvent(QKeyEvent *event) override;
136
137private slots:
139 void updateLineNumberAreaWidth(int newBlockCount);
140
142 void updateLineNumberArea(const QRect &rect, int dy);
143
145 void highlightCurrentLine();
146
147private:
148 QString currentFilePath_;
149 FileID currentFileId_ = FileManager::InvalidFileID;
150 LineNumberArea *lineNumberArea_; // Owned via Qt parent-child
151 QSyntaxHighlighter *keywordHighlighter_ = nullptr; // Owned by document
152 QTextEdit::ExtraSelection navigationHighlight_; // Stores AST navigation highlight
153 QTextEdit::ExtraSelection searchHighlight_; // Stores search match highlight
154 QTextCursor searchCursor_;
155 QString lastSearchTerm_;
156 bool suppressCursorSignal_ = false;
157 bool mouseSelectionActive_ = false;
158
159 void setupEditor();
160 void updateHighlights(); // Update all highlights (navigation, etc.)
161 bool performFind(const QString &term, QTextDocument::FindFlags flags);
162 void setSearchHighlight(const QTextCursor &cursor);
163 void emitCursorPosition();
164 void emitSelectionRange();
165
166 int pressSelectionStart_ = 0;
167 int pressSelectionEnd_ = 0;
168 QPoint pressMousePos_;
169};
170
171} // namespace acav
Centralized file registry with path-to-FileID mapping.
std::size_t FileID
Type-safe identifier for registered files. 0 is reserved for invalid.
Definition FileManager.h:38
Source code location representation.
static constexpr FileID InvalidFileID
Reserved invalid FileID.
Definition FileManager.h:47
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).