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

Merged
65 changes: 60 additions & 5 deletions src/FileManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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()};
}
Expand Down Expand Up @@ -349,4 +404,4 @@ OperationResult FileManager::duplicatePath(const QFileInfo &pathInfo)
qDebug() << "Duplicated file to:" << QString::fromStdString(dupPath.string());

return {true, dupPath.filename().string()};
}
}
21 changes: 11 additions & 10 deletions tests/test_mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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"
#include "test_mainwindow.moc"
2 changes: 1 addition & 1 deletion tests/test_syntax.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,4 @@ void TestSyntax::testLoadMissingKeywords()
}

QTEST_MAIN(TestSyntax)
#include "test_syntax.moc"
#include "test_syntax.moc"
Loading