Skip to content

File Manager Singleton #23

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 17 commits into from
Mar 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
2533fed
[Refactor] Remove unused file handling methods and reorganize member …
chrisdedman Mar 21, 2025
38b19c8
[Refactor] Update Tree class constructor to use FileManager instead o…
chrisdedman Mar 21, 2025
ddc3af4
[Refactor] Update Tree class constructor to use FileManager instead o…
chrisdedman Mar 21, 2025
6b06bae
[Refactor] Integrate FileManager into MainWindow for improved file ha…
chrisdedman Mar 21, 2025
1497e7f
[Feature] Implement FileManager class for handling file operations in…
chrisdedman Mar 21, 2025
2c18d5a
[Refactor] Add FileManager source and header files to CMakeLists for …
chrisdedman Mar 21, 2025
ec2e9fb
Merge branch 'main' into refactorfileManager
chrisdedman Mar 21, 2025
2df95b1
Merge branch 'main' into refactorfileManager
chrisdedman Mar 21, 2025
cc1c56f
[Refactor] Adjust formatting and organization in FileManager class fo…
chrisdedman Mar 23, 2025
a61e4dd
[Refactor] Improve FileManager class initialization and file handling…
chrisdedman Mar 23, 2025
7894c2d
[Refactor] Remove unused current file name methods and update FileMan…
chrisdedman Mar 23, 2025
741250b
[Refactor] Integrate FileManager instance into CodeEditor and remove …
chrisdedman Mar 23, 2025
7de8ed4
[Refactor] Remove unused QFile include and adjust FileManager pointer…
chrisdedman Mar 23, 2025
999cc44
[Refactor] Replace FileManager instance creation with singleton acces…
chrisdedman Mar 23, 2025
c198be0
[Refactor] Update openFile method to use FileManager singleton for fi…
chrisdedman Mar 23, 2025
bbae134
[Refactor] Add class documentation for classes header file
chrisdedman Mar 23, 2025
f1eef64
[Refactor] Remove unnecessary newline in Syntax constructor for impro…
chrisdedman Mar 23, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,13 @@ add_executable(${TARGET_NAME}
src/CodeEditor.cpp
src/Syntax.cpp
src/Tree.cpp
src/FileManager.cpp
include/MainWindow.h
include/CodeEditor.h
include/Syntax.h
include/Tree.h
include/LineNumberArea.h
include/FileManager.h
)

qt_add_resources(APP_RESOURCES resources.qrc)
Expand Down
15 changes: 11 additions & 4 deletions include/CodeEditor.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@
#include <QPlainTextEdit>
#include <QKeyEvent>

class FileManager; // Forward declaration

/**
* @class CodeEditor
* @brief A custom code editor widget that extends QPlainTextEdit.
*
* The CodeEditor class provides a code editor with line number area, syntax highlighting,
* and basic editing modes (NORMAL and INSERT). It emits signals for status messages and
* handles key press and resize events.
*/
class CodeEditor : public QPlainTextEdit
{
Q_OBJECT
Expand All @@ -18,8 +28,6 @@ class CodeEditor : public QPlainTextEdit
Mode mode = NORMAL;
void lineNumberAreaPaintEvent(QPaintEvent *event);
int lineNumberAreaWidth();
QString getCurrentFileName() const { return m_currentFileName; }
void setCurrentFileName(const QString &fileName) { m_currentFileName = fileName; }

signals:
void statusMessageChanged(const QString &message);
Expand All @@ -35,9 +43,8 @@ private slots:

private:
QWidget *m_lineNumberArea;
QString m_currentFileName;
FileManager *m_fileManager;

QString getFileExtension();
void addLanguageSymbol(QTextCursor &cursor, const QString &commentSymbol);
void commentSelection(QTextCursor &cursor, const QString &commentSymbol);
void commentLine(QTextCursor &cursor, const QString &commentSymbol);
Expand Down
53 changes: 53 additions & 0 deletions include/FileManager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#pragma once

#include <QObject>
#include <memory>

class CodeEditor;
class MainWindow;

/**
* @class FileManager
* @brief Manages file operations such as creating, saving, and opening files.
*
* The FileManager class is a singleton that handles multiple file-related operations
* within the application. It interacts with the CodeEditor and MainWindow classes
* to perform tasks such as creating new files, saving existing files, and opening
* files from the filesystem. The class ensures that only one instance of FileManager
* exists and provides a global point of access to it.
*/
class FileManager : public QObject
{
Q_OBJECT

public:
static FileManager &getInstance(CodeEditor *editor = nullptr, MainWindow *mainWindow = nullptr)
{
static FileManager instance(editor, mainWindow);
return instance;
}

FileManager(const FileManager &) = delete;
FileManager &operator=(const FileManager &) = delete;

QString getFileExtension() const;
QString getCurrentFileName() const;

void setCurrentFileName(const QString fileName);
void initialize(CodeEditor *editor, MainWindow *mainWindow);

public slots:
void newFile();
void saveFile();
void saveFileAs();
void openFile();
void loadFileInEditor(const QString &filePath);

private:
FileManager(CodeEditor *editor, MainWindow *mainWindow);
~FileManager();

CodeEditor *m_editor;
MainWindow *m_mainWindow;
QString m_currentFileName;
};
9 changes: 9 additions & 0 deletions include/LineNumberArea.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@
#include <QPaintEvent>
#include <QSize>

/**
* @class LineNumberArea
* @brief A widget that displays line numbers for the CodeEditor.
*
* The LineNumberArea class is a QWidget that is used to display line numbers
* alongside the CodeEditor widget.
*
* @note This class is intended to be used as a part of the CodeEditor widget.
*/
class LineNumberArea : public QWidget
{
public:
Expand Down
22 changes: 14 additions & 8 deletions include/MainWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,41 +6,47 @@
#include <QIcon>
#include <QKeySequence>
#include <memory>
#include <QFile>

class CodeEditor;
class Syntax;
class Tree;
class FileManager;

/**
* @class MainWindow
* @brief The MainWindow class represents the main UI window of the application.
*
* This class is responsible for initializing and managing the main components
* of the application, including the file tree view, code editor, and menu bar.
*/
class MainWindow : public QMainWindow
{
Q_OBJECT

public:
explicit MainWindow(QWidget *parent = nullptr);
virtual ~MainWindow();
void loadFileInEditor(const QString &filePath);

// Initialize the file tree view and set it as the central widget
// of the main window, alongside the code editor
void initTree();

private slots:
void newFile();
void openFile();
void saveFile();
void saveFileAs();
void showAbout();

private:
void createMenuBar();
void createFileActions(QMenu *fileMenu);
void createHelpActions(QMenu *helpMenu);
void createAppActions(QMenu *appMenu);

QAction *createAction(const QIcon &icon, const QString &text,
const QKeySequence &shortcut, const QString &statusTip,
void (MainWindow::*slot)());
const std::function<void()> &slot);

std::unique_ptr<CodeEditor> m_editor;
std::unique_ptr<Syntax> m_syntax;
std::unique_ptr<Tree> m_tree;
QString m_currentFileName;

FileManager *m_fileManager;
};
11 changes: 11 additions & 0 deletions include/Syntax.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,17 @@
#include <QSyntaxHighlighter>
#include <QTextDocument>

/**
* @class Syntax
* @brief A class for syntax highlighting in a QTextDocument.
*
* This class inherits from QSyntaxHighlighter and provides functionality
* to highlight different syntax elements such as keywords, comments,
* functions, parentheses, characters, and quotations in a QTextDocument.
*
* The Syntax class uses regular expressions to define patterns for different
* syntax elements and applies corresponding text formats to them.
*/
class Syntax : public QSyntaxHighlighter
{
Q_OBJECT
Expand Down
14 changes: 11 additions & 3 deletions include/Tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,37 @@
#include <memory>

// Forward declarations
class MainWindow;
class QTreeView;
class QFileSystemModel;
class QFileIconProvider;
class FileManager;

/**
* @class Tree
* @brief A class that represents a tree view for displaying the file system.
*
* The Tree class is responsible for creating and managing a tree view that displays
* the file system.
*/
class Tree : public QObject
{
Q_OBJECT

public:
explicit Tree(QSplitter *splitter, MainWindow *mainWindow);
explicit Tree(QSplitter *splitter, FileManager *FileManager);
~Tree();

private:
void showContextMenu(const QPoint &pos);
void setupModel();
void setupTree();
void openFile(const QModelIndex &index);

QString getDirectoryPath() const;

std::unique_ptr<QFileIconProvider> m_iconProvider;
std::unique_ptr<QFileSystemModel> m_model;
std::unique_ptr<QTreeView> m_tree;

MainWindow *m_mainWindow;
FileManager * m_FileManager;
};
23 changes: 6 additions & 17 deletions src/CodeEditor.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "CodeEditor.h"
#include "MainWindow.h"
#include "LineNumberArea.h"
#include "FileManager.h"

#include <QPainter>
#include <QTextBlock>
Expand All @@ -9,7 +10,8 @@

CodeEditor::CodeEditor(QWidget *parent)
: QPlainTextEdit(parent),
m_lineNumberArea(new LineNumberArea(this))
m_lineNumberArea(new LineNumberArea(this)),
m_fileManager(&FileManager::getInstance())
{
connect(this, &CodeEditor::blockCountChanged, this, &CodeEditor::updateLineNumberAreaWidth);
connect(this, &CodeEditor::updateRequest, this, &CodeEditor::updateLineNumberArea);
Expand Down Expand Up @@ -73,18 +75,6 @@ void CodeEditor::keyPressEvent(QKeyEvent *event)
}
}

QString CodeEditor::getFileExtension()
{
QString filePath = getCurrentFileName();
if (!QFile::exists(filePath))
{
return QString();
}

// Extract the file extension from the file path
return QFileInfo(filePath).suffix().toLower();
}

void CodeEditor::addLanguageSymbol(QTextCursor &cursor, const QString &commentSymbol)
{
if (cursor.hasSelection())
Expand Down Expand Up @@ -116,7 +106,7 @@ void CodeEditor::commentSelection(QTextCursor &cursor, const QString &commentSym

if (lineText.startsWith(commentSymbol))
{
cursor.movePosition(QTextCursor::Right, QTextCursor::KeepAnchor, 3);
cursor.movePosition(QTextCursor::Right, QTextCursor::KeepAnchor, commentSymbol.length() + 1);
cursor.removeSelectedText();
}
else
Expand All @@ -136,7 +126,7 @@ void CodeEditor::commentLine(QTextCursor &cursor, const QString &commentSymbol)

if (lineText.startsWith(commentSymbol))
{
lineText.remove(0, 3);
lineText.remove(0, commentSymbol.length() + 1);
}
else
{
Expand All @@ -149,7 +139,7 @@ void CodeEditor::commentLine(QTextCursor &cursor, const QString &commentSymbol)
void CodeEditor::addComment()
{
QTextCursor cursor = textCursor();
QString fileExtension = getFileExtension();
QString fileExtension = m_fileManager->getFileExtension();
qDebug() << "File Extension:" << fileExtension;

if (fileExtension == "cpp" || fileExtension == "h" ||
Expand All @@ -172,7 +162,6 @@ void CodeEditor::addComment()
else
{
qDebug() << "Unsupported file extension for commenting.";
return;
}
}

Expand Down
Loading
Loading