Skip to content

Commit c974f97

Browse files
committed
[Refactor + Update] Big refactor + update logic
1 parent 5fd2c7c commit c974f97

File tree

1 file changed

+77
-76
lines changed

1 file changed

+77
-76
lines changed

src/FileManager.cpp

Lines changed: 77 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ void FileManager::initialize(CodeEditor *editor, MainWindow *mainWindow)
2424
{
2525
m_editor = editor;
2626
m_mainWindow = mainWindow;
27+
28+
connect(m_editor, &QPlainTextEdit::textChanged, this, [this](){m_isDirty = true;});
2729
}
2830

2931
QString FileManager::getCurrentFileName() const
@@ -36,52 +38,7 @@ void FileManager::setCurrentFileName(const QString fileName)
3638
m_currentFileName = fileName;
3739
}
3840

39-
void FileManager::newFile()
40-
{
41-
QString currentFileName = getCurrentFileName();
42-
bool isFileSaved = !currentFileName.isEmpty();
43-
bool isTextEditorEmpty = this->m_editor->toPlainText().isEmpty();
44-
45-
if (!isFileSaved && !isTextEditorEmpty)
46-
{
47-
QMessageBox promptBox;
48-
promptBox.setWindowTitle("Save Current File");
49-
promptBox.setText("Would you like to save the file?");
50-
promptBox.setInformativeText("The document will be lost if you don't save it!");
51-
promptBox.setStandardButtons(QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
52-
promptBox.setDefaultButton(QMessageBox::Save);
53-
54-
int option = promptBox.exec();
55-
56-
if (option == QMessageBox::Cancel)
57-
{
58-
return;
59-
}
60-
61-
else if (option == QMessageBox::Discard)
62-
{
63-
// TODO: add the logic to discard the changes
64-
}
65-
}
66-
67-
else if (isFileSaved)
68-
{
69-
isChanged(currentFileName);
70-
}
71-
72-
if (!m_currentFileName.isEmpty())
73-
{
74-
m_currentFileName = "";
75-
m_editor->clear();
76-
m_mainWindow->setWindowTitle("Code Astra");
77-
}
78-
79-
saveFile();
80-
m_currentFileName = getCurrentFileName();
81-
loadFileInEditor(m_currentFileName);
82-
}
83-
84-
QString lastSaved(const QFileInfo& file)
41+
QString getLastSaved(const QFileInfo& file)
8542
{
8643
const auto lastSaved = file.lastModified();
8744
const auto now = QDateTime::currentDateTime();
@@ -96,49 +53,89 @@ QString lastSaved(const QFileInfo& file)
9653
return QString::number(days) + " days ago";
9754
}
9855

99-
bool FileManager::isChanged(QString currentFileName)
56+
bool FileManager::hasUnsavedChanges()
10057
{
101-
// Read from saved file and compare to current file
102-
QFile file(currentFileName);
103-
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
58+
if(!m_isDirty)
10459
{
10560
return false;
10661
}
10762

108-
QTextStream in(&file);
109-
QString savedFileContents = in.readAll();
110-
file.close();
111-
112-
if (savedFileContents != this->m_editor->toPlainText())
63+
// Additional safeguard if content still matches file on disk
64+
if (!m_currentFileName.isEmpty())
11365
{
114-
QString timeSinceSave = lastSaved(QFileInfo(file));
115-
116-
QMessageBox promptBox;
117-
promptBox.setWindowTitle("Changes Detected");
118-
promptBox.setText("Would you like to save your changes?");
119-
promptBox.setInformativeText("The document has been modified. It was last edited " + timeSinceSave + ".");
120-
promptBox.setStandardButtons(QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
121-
promptBox.setDefaultButton(QMessageBox::Save);
122-
123-
int option = promptBox.exec();
124-
if (option == QMessageBox::Cancel)
66+
QFile file(m_currentFileName);
67+
if (file.open(QIODevice::ReadOnly | QIODevice::Text))
12568
{
126-
return false;
69+
QTextStream in(&file);
70+
QString diskContents = in.readAll();
71+
if (diskContents == m_editor->toPlainText())
72+
{
73+
m_isDirty = false;
74+
return false;
75+
}
12776
}
77+
}
78+
79+
return true; // nothing changed
80+
}
12881

129-
if (option == QMessageBox::Discard)
82+
int FileManager::buildUnsavedChangesMessage() const
83+
{
84+
QString infoText = "Your changes will be lost if you don't save.";
85+
if (!m_currentFileName.isEmpty())
86+
{
87+
QFileInfo file(m_currentFileName);
88+
if (file.exists())
13089
{
131-
// If the user selects the option 'Discard',
132-
// the changes will not be saved.
133-
return true;
90+
QString timeSinceSave = getLastSaved(file);
91+
infoText = "The document has been modified. It was last edited " + timeSinceSave + ".";
13492
}
135-
93+
}
94+
95+
QMessageBox promptBox;
96+
promptBox.setWindowTitle("Unsaved changes");
97+
promptBox.setText("Would you like to save your changes?");
98+
promptBox.setInformativeText(infoText);
99+
promptBox.setStandardButtons(QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
100+
promptBox.setDefaultButton(QMessageBox::Save);
101+
102+
return promptBox.exec();
103+
}
104+
105+
bool FileManager::promptUnsavedChanges()
106+
{
107+
if (!hasUnsavedChanges())
108+
{
109+
return true;
110+
}
111+
112+
int option = buildUnsavedChangesMessage();
113+
if (option == QMessageBox::Save)
114+
{
136115
saveFile();
137116
}
117+
else if (option == QMessageBox::Cancel)
118+
{
119+
return false;
120+
}
138121

122+
// if discard selected, continue without saving.
139123
return true;
140124
}
141125

126+
void FileManager::newFile()
127+
{
128+
if (!promptUnsavedChanges())
129+
{
130+
return;
131+
}
132+
133+
m_currentFileName = "";
134+
m_editor->clear();
135+
m_mainWindow->setWindowTitle("Untitle ~ Code Astra");
136+
m_isDirty = false;
137+
}
138+
142139
void FileManager::saveFile()
143140
{
144141
if (m_currentFileName.isEmpty())
@@ -147,7 +144,7 @@ void FileManager::saveFile()
147144
return;
148145
}
149146

150-
qDebug() << "Saving file:" << m_currentFileName;
147+
// qDebug() << "Saving file:" << m_currentFileName;
151148

152149
QFile file(m_currentFileName);
153150
if (!file.open(QFile::WriteOnly | QFile::Text))
@@ -168,6 +165,7 @@ void FileManager::saveFile()
168165
}
169166
file.close();
170167

168+
m_isDirty = false;
171169
emit m_editor->statusMessageChanged("File saved successfully.");
172170
}
173171

@@ -195,7 +193,7 @@ void FileManager::openFile()
195193
"All Files (*);;C++ Files (*.cpp *.h);;Text Files (*.txt)");
196194
if (!fileName.isEmpty())
197195
{
198-
qDebug() << "Opening file: " << fileName;
196+
// qDebug() << "Opening file: " << fileName;
199197
m_currentFileName = fileName;
200198
loadFileInEditor(fileName);
201199
}
@@ -207,7 +205,8 @@ void FileManager::openFile()
207205

208206
void FileManager::loadFileInEditor(const QString &filePath)
209207
{
210-
qDebug() << "Loading file:" << filePath;
208+
// qDebug() << "Loading file:" << filePath;
209+
211210
QFile file(filePath);
212211
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
213212
{
@@ -218,7 +217,9 @@ void FileManager::loadFileInEditor(const QString &filePath)
218217
QTextStream in(&file);
219218
if (m_editor)
220219
{
220+
m_editor->blockSignals(true);
221221
m_editor->setPlainText(in.readAll());
222+
m_editor->blockSignals(false);
222223

223224
delete m_currentHighlighter;
224225

@@ -241,6 +242,7 @@ void FileManager::loadFileInEditor(const QString &filePath)
241242
qWarning() << "MainWindow is not initialized in FileManager.";
242243
}
243244

245+
m_isDirty = false;
244246
}
245247

246248
QString FileManager::getFileExtension() const
@@ -375,7 +377,6 @@ OperationResult FileManager::newFile(const QFileInfo &pathInfo, QString newFileP
375377
{
376378
file.close();
377379
}
378-
qDebug() << "New file created.";
379380

380381
FileManager::getInstance().setCurrentFileName(QString::fromStdString(filePath.string()));
381382
return {true, filePath.filename().string() + " created successfully."};

0 commit comments

Comments
 (0)