Skip to content

Commit a936340

Browse files
authored
Merge pull request #20 from sandbox-science/refactor
Refactor Codebase for Modularity, Correctness, and Maintainability
2 parents b8a56b8 + 8fbb4f4 commit a936340

File tree

12 files changed

+379
-270
lines changed

12 files changed

+379
-270
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ add_executable(${TARGET_NAME}
6060
include/CodeEditor.h
6161
include/Syntax.h
6262
include/Tree.h
63+
include/LineNumberArea.h
6364
)
6465

6566
qt_add_resources(APP_RESOURCES resources.qrc)

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ Please, check the [wiki](https://github.com/sandbox-science/CodeAstra/wiki) for
2727
- [x] Basic text editing
2828
- [x] Open a file
2929
- [x] Save file
30-
- [ ] Create a new file
31-
- [ ] File tree navigation
32-
- [ ] Syntax highlighting
30+
- [ ] Create a new file ~ in progress
31+
- [x] File tree navigation
32+
- [ ] Syntax highlighting ~ in progress
3333
- [ ] Plugin system
3434

3535
## To-Do

include/CodeEditor.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
#ifndef CODEEDITOR_H
2-
#define CODEEDITOR_H
1+
#pragma once
32

43
#include <QPlainTextEdit>
54
#include <QKeyEvent>
@@ -20,6 +19,9 @@ class CodeEditor : public QPlainTextEdit
2019
void lineNumberAreaPaintEvent(QPaintEvent *event);
2120
int lineNumberAreaWidth();
2221

22+
signals:
23+
void statusMessageChanged(const QString &message);
24+
2325
protected:
2426
void keyPressEvent(QKeyEvent *event) override;
2527
void resizeEvent(QResizeEvent *event) override;
@@ -30,7 +32,5 @@ private slots:
3032
void updateLineNumberArea(const QRect &rect, int dy);
3133

3234
private:
33-
QWidget *lineNumberArea;
34-
};
35-
36-
#endif // CODEEDITOR_H
35+
QWidget *m_lineNumberArea;
36+
};

include/LineNumberArea.h

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
#ifndef LINENUMBER_H
2-
#define LINENUMBER_H
1+
#pragma once
32

43
#include "CodeEditor.h"
54

@@ -10,21 +9,19 @@
109
class LineNumberArea : public QWidget
1110
{
1211
public:
13-
LineNumberArea(CodeEditor *editor) : QWidget(editor), codeEditor(editor) {}
12+
LineNumberArea(CodeEditor *editor) : QWidget(editor), codeEditor(editor) {}
1413

15-
QSize sizeHint() const override
16-
{
17-
return QSize(codeEditor->lineNumberAreaWidth(), 0);
18-
}
14+
QSize sizeHint() const override
15+
{
16+
return QSize(codeEditor->lineNumberAreaWidth(), 0);
17+
}
1918

2019
protected:
21-
void paintEvent(QPaintEvent *event) override
22-
{
23-
codeEditor->lineNumberAreaPaintEvent(event);
24-
}
20+
void paintEvent(QPaintEvent *event) override
21+
{
22+
codeEditor->lineNumberAreaPaintEvent(event);
23+
}
2524

2625
private:
27-
CodeEditor *codeEditor;
28-
};
29-
30-
#endif // LINENUMBER_H
26+
CodeEditor *codeEditor;
27+
};

include/MainWindow.h

Lines changed: 46 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,46 @@
1-
#ifndef MAINWINDOW_H
2-
#define MAINWINDOW_H
3-
4-
#include "CodeEditor.h"
5-
#include "Syntax.h"
6-
#include "Tree.h"
7-
8-
#include <QMainWindow>
9-
#include <QMenu>
10-
#include <QAction>
11-
#include <QIcon>
12-
#include <QKeySequence>
13-
#include <QDesktopServices>
14-
15-
class MainWindow : public QMainWindow
16-
{
17-
Q_OBJECT
18-
19-
public:
20-
explicit MainWindow(QWidget *parent = nullptr);
21-
virtual ~MainWindow();
22-
void loadFileInEditor(const QString &filePath);
23-
24-
private slots:
25-
void newFile();
26-
void openFile();
27-
void saveFile();
28-
void saveFileAs();
29-
void showAbout();
30-
31-
private:
32-
void createMenuBar();
33-
void createFileActions(QMenu *fileMenu);
34-
void createHelpActions(QMenu *helpMenu);
35-
void createAppActions(QMenu *appMenu);
36-
QAction *createAction(const QIcon &icon, const QString &text,
37-
const QKeySequence &shortcut, const QString &statusTip,
38-
void (MainWindow::*slot)());
39-
CodeEditor *editor;
40-
QString currentFileName;
41-
Syntax *syntax;
42-
Tree *tree;
43-
};
44-
45-
#endif // MAINWINDOW_H
1+
#pragma once
2+
3+
#include <QMainWindow>
4+
#include <QMenu>
5+
#include <QAction>
6+
#include <QIcon>
7+
#include <QKeySequence>
8+
#include <memory>
9+
#include <QFile>
10+
11+
class CodeEditor;
12+
class Syntax;
13+
class Tree;
14+
15+
class MainWindow : public QMainWindow
16+
{
17+
Q_OBJECT
18+
19+
public:
20+
explicit MainWindow(QWidget *parent = nullptr);
21+
virtual ~MainWindow();
22+
void loadFileInEditor(const QString &filePath);
23+
// Initialize the file tree view and set it as the central widget
24+
// of the main window, alongside the code editor
25+
void initTree();
26+
27+
private slots:
28+
void newFile();
29+
void openFile();
30+
void saveFile();
31+
void saveFileAs();
32+
void showAbout();
33+
34+
private:
35+
void createMenuBar();
36+
void createFileActions(QMenu *fileMenu);
37+
void createHelpActions(QMenu *helpMenu);
38+
void createAppActions(QMenu *appMenu);
39+
QAction *createAction(const QIcon &icon, const QString &text,
40+
const QKeySequence &shortcut, const QString &statusTip,
41+
void (MainWindow::*slot)());
42+
std::unique_ptr<CodeEditor> m_editor;
43+
std::unique_ptr<Syntax> m_syntax;
44+
std::unique_ptr<Tree> m_tree;
45+
QString m_currentFileName;
46+
};

include/Syntax.h

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
#ifndef SYNTAX_H
2-
#define SYNTAX_H
1+
#pragma once
32

43
#include <QTextCharFormat>
54
#include <QRegularExpression>
@@ -19,19 +18,26 @@ class Syntax : public QSyntaxHighlighter
1918
private:
2019
struct SyntaxRule
2120
{
22-
QRegularExpression pattern;
23-
QTextCharFormat format;
21+
QRegularExpression m_pattern;
22+
QTextCharFormat m_format;
2423
};
25-
QList<SyntaxRule> syntaxRules;
24+
QVector<SyntaxRule> m_syntaxRules;
2625

27-
QTextCharFormat keywordFormat;
28-
QTextCharFormat singleLineCommentFormat;
29-
QTextCharFormat quotationMark;
30-
QTextCharFormat functionFormat;
31-
QTextCharFormat parenthesisFormat;
32-
QTextCharFormat charFormat;
26+
QTextCharFormat m_keywordFormat;
27+
QTextCharFormat m_singleLineCommentFormat;
28+
QTextCharFormat m_quotationMark;
29+
QTextCharFormat m_functionFormat;
30+
QTextCharFormat m_parenthesisFormat;
31+
QTextCharFormat m_charFormat;
32+
QTextCharFormat m_iterationFormat;
3333

3434
void addPattern(const QString &pattern, const QTextCharFormat &format);
35-
};
3635

37-
#endif // SYNTAX_H
36+
// Initialization functions for different syntax highlighting rules
37+
void initKeywordRules();
38+
void initCommentRules();
39+
void initFunctionRules();
40+
void initParenthesisRules();
41+
void initCharRules();
42+
void initQuotationRules();
43+
};

include/Tree.h

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,33 @@
1-
#ifndef TREE_H
2-
#define TREE_H
1+
#pragma once
32

43
#include <QSplitter>
5-
#include <QTreeView>
6-
#include <QFileSystemModel>
74
#include <QObject>
5+
#include <memory>
86

9-
class MainWindow; // Forward declaration
7+
// Forward declarations
8+
class MainWindow;
9+
class QTreeView;
10+
class QFileSystemModel;
11+
class QFileIconProvider;
1012

1113
class Tree : public QObject
1214
{
1315
Q_OBJECT
1416

1517
public:
16-
Tree(QSplitter *splitter, MainWindow *mainWindow);
18+
explicit Tree(QSplitter *splitter, MainWindow *mainWindow);
1719
~Tree();
1820

1921
private:
2022
void showContextMenu(const QPoint &pos);
2123
void setupModel();
2224
void setupTree();
2325
void openFile(const QModelIndex &index);
24-
QString getDirectoryPath();
26+
QString getDirectoryPath() const;
2527

26-
QFileSystemModel *model;
27-
QTreeView *tree;
28-
MainWindow *mainWindow;
29-
};
28+
std::unique_ptr<QFileIconProvider> m_iconProvider;
29+
std::unique_ptr<QFileSystemModel> m_model;
30+
std::unique_ptr<QTreeView> m_tree;
3031

31-
#endif // TREE_H
32+
MainWindow *m_mainWindow;
33+
};

src/CodeEditor.cpp

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44

55
#include <QPainter>
66
#include <QTextBlock>
7+
#include <QStatusBar>
78

8-
CodeEditor::CodeEditor(QWidget *parent) : QPlainTextEdit(parent)
9+
CodeEditor::CodeEditor(QWidget *parent)
10+
: QPlainTextEdit(parent),
11+
m_lineNumberArea(new LineNumberArea(this))
912
{
10-
lineNumberArea = new LineNumberArea(this);
11-
1213
connect(this, &CodeEditor::blockCountChanged, this, &CodeEditor::updateLineNumberAreaWidth);
1314
connect(this, &CodeEditor::updateRequest, this, &CodeEditor::updateLineNumberArea);
1415
connect(this, &CodeEditor::cursorPositionChanged, this, &CodeEditor::highlightCurrentLine);
@@ -33,6 +34,7 @@ void CodeEditor::keyPressEvent(QKeyEvent *event)
3334
{
3435
case Qt::Key_I:
3536
mode = INSERT;
37+
emit statusMessageChanged("Insert mode activated");
3638
break;
3739
case Qt::Key_A:
3840
moveCursor(QTextCursor::Left);
@@ -46,14 +48,22 @@ void CodeEditor::keyPressEvent(QKeyEvent *event)
4648
case Qt::Key_W:
4749
moveCursor(QTextCursor::Up);
4850
break;
49-
case Qt::Key_Escape:
50-
mode = NORMAL;
51+
default:
52+
emit statusMessageChanged("Insert mode is not active. Press 'i' to enter insert mode.");
5153
break;
5254
}
5355
}
54-
else
56+
else if (mode == INSERT)
5557
{
56-
QPlainTextEdit::keyPressEvent(event);
58+
if (event->key() == Qt::Key_Escape)
59+
{
60+
mode = NORMAL;
61+
emit statusMessageChanged("Normal mode activated. Press 'escape' to return to normal mode.");
62+
}
63+
else
64+
{
65+
QPlainTextEdit::keyPressEvent(event);
66+
}
5767
}
5868
}
5969

@@ -82,11 +92,11 @@ void CodeEditor::updateLineNumberArea(const QRect &rect, int dy)
8292
{
8393
if (dy)
8494
{
85-
lineNumberArea->scroll(0, dy);
95+
m_lineNumberArea->scroll(0, dy);
8696
}
8797
else
8898
{
89-
lineNumberArea->update(0, rect.y(), lineNumberArea->width(), rect.height());
99+
m_lineNumberArea->update(0, rect.y(), m_lineNumberArea->width(), rect.height());
90100
}
91101

92102
if (rect.contains(viewport()->rect()))
@@ -100,7 +110,7 @@ void CodeEditor::resizeEvent(QResizeEvent *e)
100110
QPlainTextEdit::resizeEvent(e);
101111

102112
QRect cr = contentsRect();
103-
lineNumberArea->setGeometry(QRect(cr.left(), cr.top(), lineNumberAreaWidth(), cr.height()));
113+
m_lineNumberArea->setGeometry(QRect(cr.left(), cr.top(), lineNumberAreaWidth(), cr.height()));
104114
}
105115

106116
void CodeEditor::highlightCurrentLine()
@@ -128,13 +138,13 @@ void CodeEditor::highlightCurrentLine()
128138

129139
void CodeEditor::lineNumberAreaPaintEvent(QPaintEvent *event)
130140
{
131-
QPainter painter(lineNumberArea);
141+
QPainter painter(m_lineNumberArea);
132142

133143
// Match the background color of the editor
134144
painter.fillRect(event->rect(), palette().color(QPalette::Base));
135145

136146
// Draw a separating line between the number area and the text editor
137-
int separatorX = lineNumberArea->width() - 4;
147+
int separatorX = m_lineNumberArea->width() - 4;
138148
painter.drawLine(separatorX, event->rect().top(), separatorX, event->rect().bottom());
139149

140150
QTextBlock block = firstVisibleBlock();
@@ -152,7 +162,7 @@ void CodeEditor::lineNumberAreaPaintEvent(QPaintEvent *event)
152162
QString number = QString::number(blockNumber + 1);
153163
painter.setPen(Qt::darkGray);
154164

155-
painter.drawText(0, top + padding, lineNumberArea->width(), lineHeight,
165+
painter.drawText(0, top + padding, m_lineNumberArea->width(), lineHeight,
156166
Qt::AlignCenter, number);
157167
}
158168

0 commit comments

Comments
 (0)