Skip to content

Commit 466cd79

Browse files
authored
Merge pull request #32 from franciscomartinez45/new-file-feature
New file feature
2 parents a857638 + e231f0d commit 466cd79

File tree

3 files changed

+72
-16
lines changed

3 files changed

+72
-16
lines changed

src/FileManager.cpp

Lines changed: 60 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,63 @@ void FileManager::setCurrentFileName(const QString fileName)
3737

3838
void FileManager::newFile()
3939
{
40-
// Logic to create a new file
40+
QString currentFileName = getCurrentFileName();
41+
bool isFileSaved = !currentFileName.isEmpty();
42+
bool isTextEditorEmpty = this->m_editor->toPlainText().isEmpty();
43+
// File has not been saved and the text editor is not empty
44+
if (!isFileSaved && !isTextEditorEmpty)
45+
{
46+
// Create box to prompt user to save changes to file
47+
QMessageBox promptBox;
48+
promptBox.setWindowTitle("Save Current File");
49+
promptBox.setText("Would you like to save the file?");
50+
promptBox.setStandardButtons(QMessageBox::Save | QMessageBox::Cancel);
51+
promptBox.setDefaultButton(QMessageBox::Save);
52+
53+
int option = promptBox.exec();
54+
// return if the user hit Cancel button
55+
if (option == QMessageBox::Cancel)
56+
{
57+
return;
58+
}
59+
60+
saveFile();
61+
}
62+
// File has been previously saved
63+
else if (isFileSaved)
64+
{
65+
// Read from saved file and compare to current file
66+
QFile file(currentFileName);
67+
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
68+
return;
69+
QTextStream in(&file);
70+
QString savedFileContents = in.readAll();
71+
file.close();
72+
if (savedFileContents != this->m_editor->toPlainText().trimmed())
73+
{
74+
// Create box to prompt user to save changes to file
75+
QMessageBox promptBox;
76+
promptBox.setWindowTitle("Changes Detected");
77+
promptBox.setText("Would you like to save the current changes to the file?");
78+
promptBox.setStandardButtons(QMessageBox::Save | QMessageBox::Cancel);
79+
promptBox.setDefaultButton(QMessageBox::Save);
80+
int option = promptBox.exec();
81+
// return if the user hit Cancel button
82+
if (option == QMessageBox::Cancel)
83+
{
84+
return;
85+
}
86+
saveFile();
87+
}
88+
}
89+
90+
if (!m_currentFileName.isEmpty())
91+
{
92+
setCurrentFileName("");
93+
m_editor->clear();
94+
m_mainWindow->setWindowTitle("Code Astra ~ untitled");
95+
}
96+
4197
}
4298

4399
void FileManager::saveFile()
@@ -235,14 +291,13 @@ OperationResult FileManager::deletePath(const QFileInfo &pathInfo)
235291
}
236292

237293
std::filesystem::path pathToDelete = pathInfo.absoluteFilePath().toStdString();
238-
239294
// Validate the input path
240295
if (!isValidPath(pathToDelete))
241296
{
242297
return {false, "ERROR: invalid file path." + pathToDelete.filename().string()};
243298
}
244-
245-
if (!QFile::moveToTrash(pathToDelete))
299+
QString qPathToDelete = QString::fromStdString(pathToDelete.string());
300+
if (!QFile::moveToTrash(qPathToDelete))
246301
{
247302
return {false, "ERROR: failed to delete: " + pathToDelete.string()};
248303
}
@@ -349,4 +404,4 @@ OperationResult FileManager::duplicatePath(const QFileInfo &pathInfo)
349404
qDebug() << "Duplicated file to:" << QString::fromStdString(dupPath.string());
350405

351406
return {true, dupPath.filename().string()};
352-
}
407+
}

tests/test_mainwindow.cpp

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ void TestMainWindow::cleanupTestCase()
3838

3939
void TestMainWindow::testWindowTitle()
4040
{
41-
QCOMPARE(mainWindow->windowTitle(), "CodeAstra ~ Code Editor");
41+
QCOMPARE_EQ(mainWindow->windowTitle(), "CodeAstra ~ Code Editor");
4242
}
4343

4444
void TestMainWindow::testEditorInitialization()
@@ -83,24 +83,25 @@ void TestMainWindow::testCreateAction()
8383
{
8484
// Mock parameters for createAction
8585
QIcon icon;
86-
QString text = "Test Action";
86+
QString text = "Test Action";
8787
QKeySequence shortcut = QKeySequence(Qt::CTRL | Qt::Key_T);
88-
QString statusTip = "This is a test action";
89-
bool slotCalled = false;
90-
91-
auto slot = [&slotCalled]() { slotCalled = true; };
92-
88+
QString statusTip = "This is a test action";
89+
bool slotCalled = false;
90+
91+
auto slot = [&slotCalled]()
92+
{ slotCalled = true; };
93+
9394
QAction *action = mainWindow->createAction(icon, text, shortcut, statusTip, slot);
94-
95+
9596
QVERIFY2(action != nullptr, "Action should be successfully created.");
9697
QCOMPARE_EQ(action->text(), text);
9798
QCOMPARE_EQ(action->shortcuts().first(), shortcut);
9899
QCOMPARE_EQ(action->statusTip(), statusTip);
99-
100+
100101
// Simulate triggering the action
101102
action->trigger();
102103
QCOMPARE_EQ(slotCalled, true);
103104
}
104105

105106
QTEST_MAIN(TestMainWindow)
106-
#include "test_mainwindow.moc"
107+
#include "test_mainwindow.moc"

tests/test_syntax.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,4 +102,4 @@ void TestSyntax::testLoadMissingKeywords()
102102
}
103103

104104
QTEST_MAIN(TestSyntax)
105-
#include "test_syntax.moc"
105+
#include "test_syntax.moc"

0 commit comments

Comments
 (0)