@@ -20,6 +20,7 @@ Team Ostival (hello@ostival.org)
2020#include < QFileSystemWatcher>
2121#include < QMenu>
2222#include < QAction>
23+ #include < QMessageBox>
2324#include " LeftDockBuilder.h"
2425#include " TempFiles.h"
2526#include " config.h"
@@ -66,8 +67,6 @@ LeftDockBuilder::LeftDockBuilder(QMainWindow *mainWindow, QObject *parent)
6667 QListWidget *listWidget2 = new QListWidget; // Python Files
6768 listWidget2->setStyleSheet (MODERN_LIST_STYLE);
6869
69- // QString jsonpath = projectPath + "/" + projectName + "/" + projectName + ".ostival";
70-
7170 // To load src_files from JSON into the list
7271 auto updateListFromJson = [listWidget, listWidget1, listWidget2]() {
7372 QFile f (projectFile);
@@ -150,48 +149,61 @@ LeftDockBuilder::LeftDockBuilder(QMainWindow *mainWindow, QObject *parent)
150149 if (!item) return ;
151150
152151 QMenu menu;
153- QAction *deleteAction = menu.addAction (" Delete File" );
154152 QAction *setMainFile = menu.addAction (" Set Main File" );
153+ QAction *deleteAction = menu.addAction (" Delete File" );
155154 QAction *selectedAction = menu.exec (listWidget->viewport ()->mapToGlobal (pos));
156155
157156 if (selectedAction == deleteAction) {
157+ // Confirmation Dialog to delte the file
158158 QString fileName = item->text ();
159- QString filePath = projectPath + " /" + projectName + " /design_src/" + fileName;
160-
161- QFile file (filePath);
162- if (file.exists () && file.remove ()) {
163- qDebug () << " Deleted file:" << filePath;
164-
165- QFile f (projectFile);
166- if (f.open (QIODevice::ReadOnly)) {
167- QJsonDocument doc = QJsonDocument::fromJson (f.readAll ());
168- f.close ();
159+ QMessageBox msgBox;
160+ msgBox.setText (" Confirm Deletion" );
161+ msgBox.setInformativeText (QString (" Are you sure you want to delete the file %1?" ).arg (fileName));
162+ msgBox.setStandardButtons (QMessageBox::Yes | QMessageBox::No);
163+ msgBox.setDefaultButton (QMessageBox::No);
164+
165+ // Show the dialog and check the user's response
166+ if (msgBox.exec () == QMessageBox::Yes) {
167+ QString filePath = projectPath + " /" + projectName + " /design_src/" + fileName;
168+
169+ QFile file (filePath);
170+ if (file.exists () && file.remove ()) {
171+ qDebug () << " Deleted file:" << filePath;
172+
173+ QFile f (projectFile);
174+ if (f.open (QIODevice::ReadOnly)) {
175+ QJsonDocument doc = QJsonDocument::fromJson (f.readAll ());
176+ f.close ();
169177
170- QJsonObject obj = doc.object ();
171- QJsonArray arr = obj[" src_files" ].toArray ();
172- QJsonArray newArr;
173- for (auto v : arr) {
174- if (v.toString () != fileName) newArr.append (v.toString ());
178+ QJsonObject obj = doc.object ();
179+ QJsonArray arr = obj[" src_files" ].toArray ();
180+ QJsonArray newArr;
181+ for (auto v : arr) {
182+ if (v.toString () != fileName) newArr.append (v.toString ());
183+ }
184+ obj[" src_files" ] = newArr;
185+
186+ if (f.open (QIODevice::WriteOnly | QIODevice::Truncate)) {
187+ f.write (QJsonDocument (obj).toJson (QJsonDocument::Indented));
188+ f.close ();
189+ }
175190 }
176- obj[" src_files" ] = newArr;
177191
178- if (f. open (QIODevice::WriteOnly | QIODevice::Truncate)) {
179- f. write ( QJsonDocument (obj). toJson (QJsonDocument::Indented ));
180- f. close ();
181- }
192+ // Remove from list
193+ delete listWidget-> takeItem (listWidget-> row (item ));
194+ } else {
195+ qWarning () << " Failed to delete file: " << filePath;
182196 }
183-
184- // Remove from list
185- delete listWidget->takeItem (listWidget->row (item));
186197 } else {
187- qWarning () << " Failed to delete file:" << filePath;
198+ // User clicked No or closed the dialog. Do nothing.
199+ qDebug () << " Deletion of file:" << fileName << " cancelled by user." ;
188200 }
189201 } else if (selectedAction == setMainFile) {
190202 QString fileName = item->text ();
191203 mainDesignFile = fileName;
192204 qDebug () << " item selected as main file" << fileName;
193205 } else {
194- qWarning () << " Something is missing" ;
206+ qWarning () << " Something is missing or action was cancelled " ;
195207 }
196208 });
197209
0 commit comments