Skip to content

New file feature #32

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
61 changes: 57 additions & 4 deletions src/FileManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,61 @@ void FileManager::setCurrentFileName(const QString fileName)

void FileManager::newFile()
{
// Logic to create a new file
QString currentFileName = getCurrentFileName();
bool isFileSaved = !currentFileName.isEmpty();
bool isTextEditorEmpty = this->m_editor->toPlainText().isEmpty();
// File has not been saved and the text editor is not empty
if (!isFileSaved && !isTextEditorEmpty)
{
// Create box to prompt user to save changes to file
QMessageBox promptBox;
promptBox.setWindowTitle("Save Current File");
promptBox.setText("Would you like to save the file?");
promptBox.setStandardButtons(QMessageBox::Save | QMessageBox::Cancel);
promptBox.setDefaultButton(QMessageBox::Save);

int option = promptBox.exec();
// return if the user hit Cancel button
if (option == QMessageBox::Cancel)
{
return;
}

saveFile();
}
// File has been previously saved
else if (isFileSaved)
{
// Read from saved file and compare to current file
QFile file(currentFileName);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
return;
QTextStream in(&file);
QString savedFileContents = in.readAll();
file.close();
if (savedFileContents != this->m_editor->toPlainText().trimmed())
{
// Create box to prompt user to save changes to file
QMessageBox promptBox;
promptBox.setWindowTitle("Changes Detected");
promptBox.setText("Would you like to save the current changes to the file?");
promptBox.setStandardButtons(QMessageBox::Save | QMessageBox::Cancel);
promptBox.setDefaultButton(QMessageBox::Save);
int option = promptBox.exec();
// return if the user hit Cancel button
if (option == QMessageBox::Cancel)
{
return;
}
saveFile();
}
}
else {
setCurrentFileName("");
m_editor->clear();
m_mainWindow->setWindowTitle("Code Astra ~ untitled");
}
Comment on lines +89 to +93
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As mentioned in the comment in the conversation, this would be better in that case (please, see the latest comment I left after you updated from my previous review):

    if( !m_currentFileName.isEmpty())
    {
        setCurrentFileName("");
        m_editor->clear();
        m_mainWindow->setWindowTitle("Code Astra ~ untitled");
    }


}

void FileManager::saveFile()
Expand Down Expand Up @@ -235,14 +289,13 @@ OperationResult FileManager::deletePath(const QFileInfo &pathInfo)
}

std::filesystem::path pathToDelete = pathInfo.absoluteFilePath().toStdString();

// Validate the input path
if (!isValidPath(pathToDelete))
{
return {false, "ERROR: invalid file path." + pathToDelete.filename().string()};
}

if (!QFile::moveToTrash(pathToDelete))
QString qPathToDelete = QString::fromStdString(pathToDelete.string());
if (!QFile::moveToTrash(qPathToDelete))
{
return {false, "ERROR: failed to delete: " + pathToDelete.string()};
}
Expand Down
41 changes: 21 additions & 20 deletions tests/test_mainwindow.cpp
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See comment left on the previous conversation on this file. Thanks

Original file line number Diff line number Diff line change
Expand Up @@ -51,55 +51,56 @@ void TestMainWindow::testMenuBar()
{
QMenuBar *menuBar = mainWindow->menuBar();
QVERIFY2(menuBar != nullptr, "MainWindow must have a QMenuBar.");
QCOMPARE_EQ(menuBar->actions().size(), 3); // File, Help, CodeAstra
QCOMPARE(menuBar->actions().size(), 3); // File, Help, CodeAstra

QMenu *fileMenu = menuBar->findChild<QMenu *>("File");
QVERIFY2(fileMenu != nullptr, "QMenuBar must contain a 'File' menu.");
QCOMPARE_EQ(fileMenu->title(), "File");
QCOMPARE_H(fileMenu->title(), "File");

QMenu *helpMenu = menuBar->findChild<QMenu *>("Help");
QVERIFY2(helpMenu != nullptr, "QMenuBar must contain a 'Help' menu.");
QCOMPARE_EQ(helpMenu->title(), "Help");
QCOMPARE_H(helpMenu->title(), "Help");

QMenu *appMenu = menuBar->findChild<QMenu *>("CodeAstra");
QVERIFY2(appMenu != nullptr, "QMenuBar must contain a 'CodeAstra' menu.");
QCOMPARE_EQ(appMenu->title(), "CodeAstra");
QCOMPARE_H(appMenu->title(), "CodeAstra");
}

void TestMainWindow::testInitTree()
{
QSplitter *splitter = dynamic_cast<QSplitter *>(mainWindow->centralWidget());
QVERIFY2(splitter != nullptr, "Central widget should be a QSplitter.");

QCOMPARE_EQ(splitter->handleWidth(), 5);
QCOMPARE_EQ(splitter->childrenCollapsible(), false);
QCOMPARE_EQ(splitter->opaqueResize(), true);
QCOMPARE_H(splitter->handleWidth(), 5);
QCOMPARE_H(splitter->childrenCollapsible(), false);
QCOMPARE_H(splitter->opaqueResize(), true);

QList<int> sizes = splitter->sizes();
QCOMPARE_EQ(sizes.size(), 2);
QCOMPARE_H(sizes.size(), 2);
}

void TestMainWindow::testCreateAction()
{
// Mock parameters for createAction
QIcon icon;
QString text = "Test Action";
QString text = "Test Action";
QKeySequence shortcut = QKeySequence(Qt::CTRL | Qt::Key_T);
QString statusTip = "This is a test action";
bool slotCalled = false;

auto slot = [&slotCalled]() { slotCalled = true; };

QString statusTip = "This is a test action";
bool slotCalled = false;

auto slot = [&slotCalled]()
{ slotCalled = true; };

QAction *action = mainWindow->createAction(icon, text, shortcut, statusTip, slot);

QVERIFY2(action != nullptr, "Action should be successfully created.");
QCOMPARE_EQ(action->text(), text);
QCOMPARE_EQ(action->shortcuts().first(), shortcut);
QCOMPARE_EQ(action->statusTip(), statusTip);
QCOMPARE_H(action->text(), text);
QCOMPARE_H(action->shortcuts().first(), shortcut);
QCOMPARE_H(action->statusTip(), statusTip);

// Simulate triggering the action
action->trigger();
QCOMPARE_EQ(slotCalled, true);
QCOMPARE_H(slotCalled, true);
}

QTEST_MAIN(TestMainWindow)
Expand Down
16 changes: 8 additions & 8 deletions tests/test_syntax.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,17 +78,17 @@ void TestSyntax::testLoadValidSyntaxRules()

// Check the first rule
const auto &rule1 = syntax->m_syntaxRules[0];
QCOMPARE_EQ(rule1.m_pattern.pattern(), "\\bint\\b");
QCOMPARE_EQ(rule1.m_format.foreground().color(), QColor("#ff0000"));
QCOMPARE_EQ(rule1.m_format.fontWeight(), QFont::Bold);
QCOMPARE_NE(rule1.m_format.fontItalic(), true);
QCOMPARE_H(rule1.m_pattern.pattern(), "\\bint\\b");
QCOMPARE_H(rule1.m_format.foreground().color(), QColor("#ff0000"));
QCOMPARE_H(rule1.m_format.fontWeight(), QFont::Bold);
QCOMPARE_H(rule1.m_format.fontItalic(), true);

// Check the second rule
const auto &rule2 = syntax->m_syntaxRules[1];
QCOMPARE_EQ(rule2.m_pattern.pattern(), "\\bfloat\\b");
QCOMPARE_EQ(rule2.m_format.foreground().color(), QColor("#00ff00"));
QCOMPARE_EQ(rule2.m_format.fontWeight(), QFont::Normal);
QCOMPARE_EQ(rule2.m_format.fontItalic(), true);
QCOMPARE_H(rule2.m_pattern.pattern(), "\\bfloat\\b");
QCOMPARE_H(rule2.m_format.foreground().color(), QColor("#00ff00"));
QCOMPARE_H(rule2.m_format.fontWeight(), QFont::Normal);
QCOMPARE_H(rule2.m_format.fontItalic(), true);
}

void TestSyntax::testLoadMissingKeywords()
Expand Down
Loading