初始化:图形化界面骨架(文件选择、语法高亮、自动缩进、查找/替换)

This commit is contained in:
hym
2025-11-27 21:03:34 +08:00
parent 8657b085c8
commit db3715b9a0
10 changed files with 1863 additions and 0 deletions

1305
ui/codeeditorwidget.cpp Normal file

File diff suppressed because it is too large Load Diff

132
ui/codeeditorwidget.h Normal file
View File

@@ -0,0 +1,132 @@
#pragma once
#include <QWidget>
#include <QPlainTextEdit>
#include <QColor>
#include <QHash>
#include <QVector>
#include <QSet>
#include <QStringList>
#include <QTextCursor>
class LineNumberArea;
class CSyntaxHighlighter;
class CompletionPopup;
class FindReplaceBar;
// 每个字符的背景信息
struct CharBackgroundInfo
{
int column = 0; // 这一行中的列号(从 0 开始)
QColor color;
qreal alpha = 1.0; // 0.0 ~ 1.0
};
// 每一行的背景信息
struct LineBackgroundInfo
{
QColor color;
qreal alpha = 1.0; // 0.0 ~ 1.0
};
// 真正的代码编辑器,继承 QPlainTextEdit
class CodeEditor : public QPlainTextEdit
{
Q_OBJECT
public:
explicit CodeEditor(QWidget *parent = nullptr);
int lineNumberAreaWidth() const;
// 预留接口:设置行/字符背景(带透明度)
void setLineBackground(int line, const QColor &color, qreal alpha = 1.0);
void clearLineBackground(int line);
void clearAllLineBackgrounds();
void setCharBackground(int line, int column, const QColor &color, qreal alpha = 1.0);
void clearCharBackground(int line, int column);
void clearAllCharBackgrounds();
// 行号区域的绘制 & 点击
void lineNumberAreaPaintEvent(QPaintEvent *event);
void lineNumberAreaMousePressEvent(QMouseEvent *event);
void searchNext(const QString &text);
signals:
// 断点状态变化(暂时只发信号,不做具体逻辑)
void breakpointToggled(int line, bool enabled);
protected:
void resizeEvent(QResizeEvent *event) override;
void keyPressEvent(QKeyEvent *event) override;
private slots:
void updateLineNumberAreaWidth(int newBlockCount);
void updateLineNumberArea(const QRect &rect, int dy);
void highlightCurrentLine();
// ✅ 新增:来自联想窗口的补全插入
void insertCompletion(const QString &completion);
private:
void refreshExtraSelections();
void handleReturnKey(QKeyEvent *event);
void toggleBreakpoint(int line);
// ✅ 新增:联想相关辅助函数
void initCompletion();
void showCompletion(const QString &prefix);
void hideCompletion();
QString wordUnderCursor() const;
void rebuildSearchMatches();
void applySearchHighlights();
private:
LineNumberArea *m_lineNumberArea = nullptr;
CSyntaxHighlighter *m_highlighter = nullptr;
QSet<int> m_breakpoints;
QHash<int, LineBackgroundInfo> m_lineBackgrounds;
QHash<int, QVector<CharBackgroundInfo>> m_charBackgrounds;
// ✅ 新增:联想窗口和候选词列表
CompletionPopup *m_completionPopup = nullptr;
QStringList m_completionWords;
QString m_searchText;
QVector<QTextCursor> m_searchMatches;
int m_currentMatchIndex = -1;
};
// 对外仍然叫 CodeEditorWidget内部包一个 CodeEditor
class CodeEditorWidget : public QWidget
{
Q_OBJECT
public:
explicit CodeEditorWidget(QWidget *parent = nullptr);
void loadFromFile(const QString &filePath);
bool save();
bool saveAs(const QString &filePath);
QString currentText() const;
void clearEditor();
// 代理到内部 CodeEditor 的接口(以后你可以直接拿来做可视化用)
void setLineBackground(int line, const QColor &color, qreal alpha = 1.0);
void clearLineBackground(int line);
void setCharBackground(int line, int column, const QColor &color, qreal alpha = 1.0);
void clearCharBackground(int line, int column);
private:
CodeEditor *m_editor = nullptr;
QString m_currentFilePath;
FindReplaceBar *m_findBar = nullptr;
};

51
ui/filebrowserwidget.cpp Normal file
View File

@@ -0,0 +1,51 @@
#include "filebrowserwidget.h"
#include <QTreeView>
#include <QVBoxLayout>
#include <QDir>
FileBrowserWidget::FileBrowserWidget(QWidget *parent)
: QWidget(parent)
{
setupUi();
connectSignals();
// 默认浏览当前工作目录
setRootPath(QDir::currentPath());
}
void FileBrowserWidget::setupUi()
{
m_model = new QFileSystemModel(this);
m_model->setRootPath(QString()); // 稍后在 setRootPath 里设置
m_view = new QTreeView(this);
m_view->setModel(m_model);
m_view->setHeaderHidden(true);
m_view->setAnimated(true);
auto *layout = new QVBoxLayout(this);
layout->setContentsMargins(0, 0, 0, 0);
layout->addWidget(m_view);
setLayout(layout);
}
void FileBrowserWidget::connectSignals()
{
connect(m_view, &QTreeView::doubleClicked,
this, [this](const QModelIndex &index) {
if (!m_model)
return;
QString path = m_model->filePath(index);
if (QFileInfo(path).isFile())
emit fileOpenRequested(path);
});
}
void FileBrowserWidget::setRootPath(const QString &path)
{
if (!m_model)
return;
QModelIndex idx = m_model->setRootPath(path);
m_view->setRootIndex(idx);
}

26
ui/filebrowserwidget.h Normal file
View File

@@ -0,0 +1,26 @@
#pragma once
#include <QWidget>
#include <QFileSystemModel>
class QTreeView;
class FileBrowserWidget : public QWidget
{
Q_OBJECT
public:
explicit FileBrowserWidget(QWidget *parent = nullptr);
void setRootPath(const QString &path);
signals:
// 当用户双击或选择某个文件时,通知 MainWindow/CodeEditor 打开
void fileOpenRequested(const QString &filePath);
private:
QFileSystemModel *m_model = nullptr;
QTreeView *m_view = nullptr;
void setupUi();
void connectSignals();
};