Skip to content

Commit dd9e8d5

Browse files
committed
Merge branch 'main' into commentCode
2 parents 6c53714 + a936340 commit dd9e8d5

File tree

12 files changed

+378
-265
lines changed

12 files changed

+378
-265
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: 9 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>
@@ -22,6 +21,12 @@ class CodeEditor : public QPlainTextEdit
2221
QString getCurrentFileName() const { return currentFileName; }
2322
void setCurrentFileName(const QString &fileName) { currentFileName = fileName; }
2423

24+
signals:
25+
void statusMessageChanged(const QString &message);
26+
27+
signals:
28+
void statusMessageChanged(const QString &message);
29+
2530
protected:
2631
void keyPressEvent(QKeyEvent *event) override;
2732
void resizeEvent(QResizeEvent *event) override;
@@ -32,13 +37,11 @@ private slots:
3237
void updateLineNumberArea(const QRect &rect, int dy);
3338

3439
private:
35-
QWidget *lineNumberArea;
40+
QWidget *m_lineNumberArea;
3641
QString currentFileName;
3742
QString getFileExtension();
3843
void addLanguageSymbol(QTextCursor &cursor, const QString &commentSymbol);
3944
void commentSelection(QTextCursor &cursor, const QString &commentSymbol);
4045
void commentLine(QTextCursor &cursor, const QString &commentSymbol);
4146
void addComment();
42-
};
43-
44-
#endif // CODEEDITOR_H
47+
};

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 & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +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-
Syntax *syntax;
41-
Tree *tree;
42-
};
43-
44-
#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);
@@ -38,6 +39,7 @@ void CodeEditor::keyPressEvent(QKeyEvent *event)
3839
{
3940
case Qt::Key_I:
4041
mode = INSERT;
42+
emit statusMessageChanged("Insert mode activated");
4143
break;
4244
case Qt::Key_A:
4345
moveCursor(QTextCursor::Left);
@@ -51,14 +53,22 @@ void CodeEditor::keyPressEvent(QKeyEvent *event)
5153
case Qt::Key_W:
5254
moveCursor(QTextCursor::Up);
5355
break;
54-
case Qt::Key_Escape:
55-
mode = NORMAL;
56+
default:
57+
emit statusMessageChanged("Insert mode is not active. Press 'i' to enter insert mode.");
5658
break;
5759
}
5860
}
59-
else
61+
else if (mode == INSERT)
6062
{
61-
QPlainTextEdit::keyPressEvent(event);
63+
if (event->key() == Qt::Key_Escape)
64+
{
65+
mode = NORMAL;
66+
emit statusMessageChanged("Normal mode activated. Press 'escape' to return to normal mode.");
67+
}
68+
else
69+
{
70+
QPlainTextEdit::keyPressEvent(event);
71+
}
6272
}
6373
}
6474

@@ -190,11 +200,11 @@ void CodeEditor::updateLineNumberArea(const QRect &rect, int dy)
190200
{
191201
if (dy)
192202
{
193-
lineNumberArea->scroll(0, dy);
203+
m_lineNumberArea->scroll(0, dy);
194204
}
195205
else
196206
{
197-
lineNumberArea->update(0, rect.y(), lineNumberArea->width(), rect.height());
207+
m_lineNumberArea->update(0, rect.y(), m_lineNumberArea->width(), rect.height());
198208
}
199209

200210
if (rect.contains(viewport()->rect()))
@@ -208,7 +218,7 @@ void CodeEditor::resizeEvent(QResizeEvent *e)
208218
QPlainTextEdit::resizeEvent(e);
209219

210220
QRect cr = contentsRect();
211-
lineNumberArea->setGeometry(QRect(cr.left(), cr.top(), lineNumberAreaWidth(), cr.height()));
221+
m_lineNumberArea->setGeometry(QRect(cr.left(), cr.top(), lineNumberAreaWidth(), cr.height()));
212222
}
213223

214224
void CodeEditor::highlightCurrentLine()
@@ -236,13 +246,13 @@ void CodeEditor::highlightCurrentLine()
236246

237247
void CodeEditor::lineNumberAreaPaintEvent(QPaintEvent *event)
238248
{
239-
QPainter painter(lineNumberArea);
249+
QPainter painter(m_lineNumberArea);
240250

241251
// Match the background color of the editor
242252
painter.fillRect(event->rect(), palette().color(QPalette::Base));
243253

244254
// Draw a separating line between the number area and the text editor
245-
int separatorX = lineNumberArea->width() - 4;
255+
int separatorX = m_lineNumberArea->width() - 4;
246256
painter.drawLine(separatorX, event->rect().top(), separatorX, event->rect().bottom());
247257

248258
QTextBlock block = firstVisibleBlock();
@@ -260,7 +270,7 @@ void CodeEditor::lineNumberAreaPaintEvent(QPaintEvent *event)
260270
QString number = QString::number(blockNumber + 1);
261271
painter.setPen(Qt::darkGray);
262272

263-
painter.drawText(0, top + padding, lineNumberArea->width(), lineHeight,
273+
painter.drawText(0, top + padding, m_lineNumberArea->width(), lineHeight,
264274
Qt::AlignCenter, number);
265275
}
266276

0 commit comments

Comments
 (0)