Skip to content

Commit 9a513bd

Browse files
committed
fix bug on tree + update window title when new file is saved
1 parent c974f97 commit 9a513bd

File tree

3 files changed

+24
-7
lines changed

3 files changed

+24
-7
lines changed

include/FileManager.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,17 @@ class FileManager : public QObject
5252
static OperationResult duplicatePath(const QFileInfo &pathInfo);
5353
static OperationResult deletePath(const QFileInfo &pathInfo);
5454

55+
int buildUnsavedChangesMessage() const;
56+
bool hasUnsavedChanges();
57+
5558
public slots:
5659
void newFile();
5760
void saveFile();
5861
void saveFileAs();
5962
void openFile();
6063
void loadFileInEditor(const QString &filePath);
61-
bool isChanged(QString currentFileName);
64+
65+
bool promptUnsavedChanges();
6266

6367
QString getDirectoryPath() const;
6468

@@ -70,4 +74,5 @@ public slots:
7074
MainWindow *m_mainWindow;
7175
QSyntaxHighlighter *m_currentHighlighter = nullptr;
7276
QString m_currentFileName;
77+
bool m_isDirty = false;
7378
};

src/FileManager.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,15 @@ void FileManager::saveFile()
165165
}
166166
file.close();
167167

168+
if (m_mainWindow)
169+
{
170+
m_mainWindow->setWindowTitle("CodeAstra ~ " + QFileInfo(m_currentFileName).fileName());
171+
}
172+
else
173+
{
174+
qWarning() << "MainWindow is not initialized in FileManager.";
175+
}
176+
168177
m_isDirty = false;
169178
emit m_editor->statusMessageChanged("File saved successfully.");
170179
}

src/Tree.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,16 @@ void Tree::openFile(const QModelIndex &index)
6767
return;
6868
}
6969

70-
QString current_file = FileManager::getInstance().getCurrentFileName();
71-
bool isFileSaved = FileManager::getInstance().isChanged(current_file);
72-
73-
if (isFileSaved || current_file.isEmpty())
70+
FileManager &fm = FileManager::getInstance();
71+
if (fm.getCurrentFileName() != filePath)
7472
{
75-
FileManager::getInstance().setCurrentFileName(filePath);
76-
FileManager::getInstance().loadFileInEditor(filePath);
73+
if (!fm.promptUnsavedChanges())
74+
{
75+
return; // if user has cancelled
76+
}
77+
78+
fm.setCurrentFileName(filePath);
79+
fm.loadFileInEditor(filePath);
7780
}
7881
}
7982

0 commit comments

Comments
 (0)