@@ -24,6 +24,8 @@ void FileManager::initialize(CodeEditor *editor, MainWindow *mainWindow)
24
24
{
25
25
m_editor = editor;
26
26
m_mainWindow = mainWindow;
27
+
28
+ connect (m_editor, &QPlainTextEdit::textChanged, this , [this ](){m_isDirty = true ;});
27
29
}
28
30
29
31
QString FileManager::getCurrentFileName () const
@@ -36,52 +38,7 @@ void FileManager::setCurrentFileName(const QString fileName)
36
38
m_currentFileName = fileName;
37
39
}
38
40
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)
85
42
{
86
43
const auto lastSaved = file.lastModified ();
87
44
const auto now = QDateTime::currentDateTime ();
@@ -96,49 +53,89 @@ QString lastSaved(const QFileInfo& file)
96
53
return QString::number (days) + " days ago" ;
97
54
}
98
55
99
- bool FileManager::isChanged (QString currentFileName )
56
+ bool FileManager::hasUnsavedChanges ( )
100
57
{
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)
104
59
{
105
60
return false ;
106
61
}
107
62
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 ())
113
65
{
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))
125
68
{
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
+ }
127
76
}
77
+ }
78
+
79
+ return true ; // nothing changed
80
+ }
128
81
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 ())
130
89
{
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 + " ." ;
134
92
}
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
+ {
136
115
saveFile ();
137
116
}
117
+ else if (option == QMessageBox::Cancel)
118
+ {
119
+ return false ;
120
+ }
138
121
122
+ // if discard selected, continue without saving.
139
123
return true ;
140
124
}
141
125
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
+
142
139
void FileManager::saveFile ()
143
140
{
144
141
if (m_currentFileName.isEmpty ())
@@ -147,7 +144,7 @@ void FileManager::saveFile()
147
144
return ;
148
145
}
149
146
150
- qDebug () << " Saving file:" << m_currentFileName;
147
+ // qDebug() << "Saving file:" << m_currentFileName;
151
148
152
149
QFile file (m_currentFileName);
153
150
if (!file.open (QFile::WriteOnly | QFile::Text))
@@ -168,6 +165,7 @@ void FileManager::saveFile()
168
165
}
169
166
file.close ();
170
167
168
+ m_isDirty = false ;
171
169
emit m_editor->statusMessageChanged (" File saved successfully." );
172
170
}
173
171
@@ -195,7 +193,7 @@ void FileManager::openFile()
195
193
" All Files (*);;C++ Files (*.cpp *.h);;Text Files (*.txt)" );
196
194
if (!fileName.isEmpty ())
197
195
{
198
- qDebug () << " Opening file: " << fileName;
196
+ // qDebug() << "Opening file: " << fileName;
199
197
m_currentFileName = fileName;
200
198
loadFileInEditor (fileName);
201
199
}
@@ -207,7 +205,8 @@ void FileManager::openFile()
207
205
208
206
void FileManager::loadFileInEditor (const QString &filePath)
209
207
{
210
- qDebug () << " Loading file:" << filePath;
208
+ // qDebug() << "Loading file:" << filePath;
209
+
211
210
QFile file (filePath);
212
211
if (!file.open (QIODevice::ReadOnly | QIODevice::Text))
213
212
{
@@ -218,7 +217,9 @@ void FileManager::loadFileInEditor(const QString &filePath)
218
217
QTextStream in (&file);
219
218
if (m_editor)
220
219
{
220
+ m_editor->blockSignals (true );
221
221
m_editor->setPlainText (in.readAll ());
222
+ m_editor->blockSignals (false );
222
223
223
224
delete m_currentHighlighter;
224
225
@@ -241,6 +242,7 @@ void FileManager::loadFileInEditor(const QString &filePath)
241
242
qWarning () << " MainWindow is not initialized in FileManager." ;
242
243
}
243
244
245
+ m_isDirty = false ;
244
246
}
245
247
246
248
QString FileManager::getFileExtension () const
@@ -375,7 +377,6 @@ OperationResult FileManager::newFile(const QFileInfo &pathInfo, QString newFileP
375
377
{
376
378
file.close ();
377
379
}
378
- qDebug () << " New file created." ;
379
380
380
381
FileManager::getInstance ().setCurrentFileName (QString::fromStdString (filePath.string ()));
381
382
return {true , filePath.filename ().string () + " created successfully." };
0 commit comments