Skip to content

Commit

Permalink
Add Progress bar for File/Save Project functionality , replacing busy…
Browse files Browse the repository at this point in the history
… cursor
  • Loading branch information
Abo-Omar-74 committed Jun 10, 2024
1 parent 1f96fd7 commit ca7828f
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 24 deletions.
66 changes: 42 additions & 24 deletions invesalius/control.py
Original file line number Diff line number Diff line change
Expand Up @@ -367,34 +367,52 @@ def OnSaveProject(self, filepath):
self.SaveProject(filepath)

def SaveProject(self, path=None, compress=False):
Publisher.sendMessage('Begin busy cursor')
session = ses.Session()
if path:
dirpath, filename = os.path.split(path)
else:
dirpath, filename = session.GetState('project_path')

if isinstance(filename, str):
filename = utils.decode(filename, const.FS_ENCODE)
progress_dialog = dialog.ProgressBarHandler(self.frame, "Saving Project", "Initializing...", max_value=100)

proj = prj.Project()
try:
prj.Project().SavePlistProject(dirpath, filename, compress)
except PermissionError as err:
if wx.GetApp() is None:
print("Error: Permission denied, you don't have permission to write at {}".format(dirpath))
session = ses.Session()

if path:
dirpath, filename = os.path.split(path)
else:
dlg = dialogs.ErrorMessageBox(
None,
"Save project error",
"It was not possible to save because you don't have permission to write at {}\n{}".format(dirpath, err)
)
dlg.ShowModal()
dlg.Destroy()
else:
session.SaveProject((dirpath, filename))
dirpath, filename = session.GetState('project_path')

Publisher.sendMessage('End busy cursor')
if isinstance(filename, str):
filename = utils.decode(filename, const.FS_ENCODE)

proj = prj.Project()

# Update progress dialog
Publisher.sendMessage("Update Progress bar", value=30, msg="Preparing to save project...")

try:
prj.Project().SavePlistProject(dirpath, filename, compress)
except PermissionError as err:
if wx.GetApp() is None:
print("Error: Permission denied, you don't have permission to write at {}".format(dirpath))
else:
dlg = dialogs.ErrorMessageBox(
None,
"Save project error",
"It was not possible to save because you don't have permission to write at {}\n{}".format(dirpath,
err)
)
dlg.ShowModal()
dlg.Destroy()
else:
# Update progress dialog
Publisher.sendMessage("Update Progress bar", value=70, msg="Saving project data...")

session.SaveProject((dirpath, filename))

# Update progress dialog
Publisher.sendMessage("Update Progress bar", value=100, msg="Project saved successfully!")

except Exception as e:
wx.MessageBox(f"Error: {e}", "Error", wx.OK | wx.ICON_ERROR)
Publisher.sendMessage("Close Progress bar")
finally:
Publisher.sendMessage("Close Progress bar")

def CloseProject(self):
Publisher.sendMessage('Enable style', style=const.STATE_DEFAULT)
Expand Down
39 changes: 39 additions & 0 deletions invesalius/gui/dialogs.py
Original file line number Diff line number Diff line change
Expand Up @@ -6545,3 +6545,42 @@ def FitSizers(self):

def GetPath(self):
return self.path

class ProgressBarHandler(wx.ProgressDialog):
def __init__(self, parent, title="Progress Dialog", msg="Initializing...", max_value=None):
super(ProgressBarHandler , self).__init__(title,msg,parent=parent,style=wx.PD_APP_MODAL|wx.PD_CAN_ABORT|wx.PD_AUTO_HIDE)

self.max_value = max_value

self.Bind(wx.EVT_CLOSE, self.close)

# self.Show()

# Subscribe to the channels
Publisher.subscribe(self.update, "Update Progress bar")
Publisher.subscribe(self.close, "Close Progress bar")

def wasCancelled(self):
return super().WasCancelled()

def update(self, value, msg=None):
if self.WasCancelled():
return

if self.max_value is None:
self.pulse(msg)
else:
# value must be less than or equal max_value
if value > self.max_value: value = self.max_value
super().Update(int(value), msg)

def close(self):
if self.IsShown():
self.Destroy()

def pulse(self, msg=None):
#if self.IsShown():
if msg is None:
super().Pulse()
else:
super().Pulse(msg)

0 comments on commit ca7828f

Please sign in to comment.