diff --git a/src/FileManager.cpp b/src/FileManager.cpp index 7d35e1b..ab3d5af 100644 --- a/src/FileManager.cpp +++ b/src/FileManager.cpp @@ -37,7 +37,63 @@ 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(); + } + } + + if (!m_currentFileName.isEmpty()) + { + setCurrentFileName(""); + m_editor->clear(); + m_mainWindow->setWindowTitle("Code Astra ~ untitled"); + } + } void FileManager::saveFile() @@ -235,14 +291,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()}; } @@ -349,4 +404,4 @@ OperationResult FileManager::duplicatePath(const QFileInfo &pathInfo) qDebug() << "Duplicated file to:" << QString::fromStdString(dupPath.string()); return {true, dupPath.filename().string()}; -} \ No newline at end of file +} diff --git a/tests/test_mainwindow.cpp b/tests/test_mainwindow.cpp index 14567c0..91df095 100644 --- a/tests/test_mainwindow.cpp +++ b/tests/test_mainwindow.cpp @@ -38,7 +38,7 @@ void TestMainWindow::cleanupTestCase() void TestMainWindow::testWindowTitle() { - QCOMPARE(mainWindow->windowTitle(), "CodeAstra ~ Code Editor"); + QCOMPARE_EQ(mainWindow->windowTitle(), "CodeAstra ~ Code Editor"); } void TestMainWindow::testEditorInitialization() @@ -83,24 +83,25 @@ 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); - + // Simulate triggering the action action->trigger(); QCOMPARE_EQ(slotCalled, true); } QTEST_MAIN(TestMainWindow) -#include "test_mainwindow.moc" \ No newline at end of file +#include "test_mainwindow.moc" diff --git a/tests/test_syntax.cpp b/tests/test_syntax.cpp index 0166bbe..eeabf54 100644 --- a/tests/test_syntax.cpp +++ b/tests/test_syntax.cpp @@ -102,4 +102,4 @@ void TestSyntax::testLoadMissingKeywords() } QTEST_MAIN(TestSyntax) -#include "test_syntax.moc" \ No newline at end of file +#include "test_syntax.moc"