From 4d99bc518a7b7d89df3c40f65ef19fc469a2c087 Mon Sep 17 00:00:00 2001 From: Thiago Franco de Moraes Date: Fri, 12 Jan 2018 14:58:15 -0200 Subject: [PATCH] Give the user the option to compact or not when saving the file. When the file is not compacted the file is saved faster. Also, save the surface file only one time. --- invesalius/constants.py | 2 ++ invesalius/control.py | 10 ++++++---- invesalius/data/surface.py | 16 ++++++++++++---- invesalius/gui/dialogs.py | 21 +++++++++++++++++++-- invesalius/project.py | 27 +++++++++++++++++++++------ 5 files changed, 60 insertions(+), 16 deletions(-) diff --git a/invesalius/constants.py b/invesalius/constants.py index 5f6b3e3..1fa7ba2 100644 --- a/invesalius/constants.py +++ b/invesalius/constants.py @@ -26,6 +26,8 @@ import itertools #from invesalius.project import Project INVESALIUS_VERSION = "3.1.1" +INVESALIUS_ACTUAL_FORMAT_VERSION = 1.1 + #--------------- # Measurements diff --git a/invesalius/control.py b/invesalius/control.py index 62b50cb..f0e5ee5 100644 --- a/invesalius/control.py +++ b/invesalius/control.py @@ -233,17 +233,19 @@ class Controller(): session = ses.Session() if saveas or session.temp_item: proj = prj.Project() - filepath = dialog.ShowSaveAsProjectDialog(proj.name) + filepath, compress = dialog.ShowSaveAsProjectDialog(proj.name) if filepath: #session.RemoveTemp() session.OpenProject(filepath) else: return else: + proj = prj.Project() + compress = proj.compress dirpath, filename = session.project_path filepath = os.path.join(dirpath, filename) - self.SaveProject(filepath) + self.SaveProject(filepath, compress) def ShowDialogCloseProject(self): @@ -335,7 +337,7 @@ class Controller(): path = pubsub_evt.data self.SaveProject(path) - def SaveProject(self, path=None): + def SaveProject(self, path=None, compress=False): Publisher.sendMessage('Begin busy cursor') session = ses.Session() if path: @@ -348,7 +350,7 @@ class Controller(): filename = filename.decode(const.FS_ENCODE) proj = prj.Project() - prj.Project().SavePlistProject(dirpath, filename) + prj.Project().SavePlistProject(dirpath, filename, compress) session.SaveProject() Publisher.sendMessage('End busy cursor') diff --git a/invesalius/data/surface.py b/invesalius/data/surface.py index 64d19ae..615bf2c 100644 --- a/invesalius/data/surface.py +++ b/invesalius/data/surface.py @@ -74,11 +74,19 @@ class Surface(): else: self.name = name + self.filename = None + def SavePlist(self, dir_temp, filelist): - filename = u'surface_%d' % self.index - vtp_filename = filename + u'.vtp' - vtp_filepath = os.path.join(dir_temp, vtp_filename) - pu.Export(self.polydata, vtp_filepath, bin=True) + if self.filename and os.path.exists(self.filename): + filename = u'surface_%d' % self.index + vtp_filename = filename + u'.vtp' + vtp_filepath = self.filename + else: + filename = u'surface_%d' % self.index + vtp_filename = filename + u'.vtp' + vtp_filepath = tempfile.mktemp() + pu.Export(self.polydata, vtp_filepath, bin=True) + self.filename = vtp_filepath filelist[vtp_filepath] = vtp_filename diff --git a/invesalius/gui/dialogs.py b/invesalius/gui/dialogs.py index 2394477..ad74866 100644 --- a/invesalius/gui/dialogs.py +++ b/invesalius/gui/dialogs.py @@ -221,6 +221,13 @@ class ProgressDialog(object): # --------- + +INV_NON_COMPRESSED = 0 +INV_COMPRESSED = 1 + +WILDCARD_INV_SAVE = _("InVesalius project (*.inv3)|*.inv3") + "|" + \ + _("InVesalius project compressed (*.inv3)|*.inv3") + WILDCARD_OPEN = "InVesalius 3 project (*.inv3)|*.inv3|" \ "All files (*.*)|*.*" @@ -425,7 +432,7 @@ def ShowSaveAsProjectDialog(default_filename=None): _("Save project as..."), # title "", # last used directory default_filename, - _("InVesalius project (*.inv3)|*.inv3"), + WILDCARD_INV_SAVE, wx.FD_SAVE|wx.FD_OVERWRITE_PROMPT) #dlg.SetFilterIndex(0) # default is VTI @@ -446,8 +453,9 @@ def ShowSaveAsProjectDialog(default_filename=None): if filename.split(".")[-1] != extension: filename = filename + "." + extension + wildcard = dlg.GetFilterIndex() os.chdir(current_dir) - return filename + return filename, wildcard == INV_COMPRESSED # Dialog for neuronavigation markers @@ -727,6 +735,15 @@ def ImportEmptyDirectory(dirpath): dlg.Destroy() +def ImportOldFormatInvFile(): + msg = _("File was created in a newer InVesalius version. Some functionalities may not work correctly.") + dlg = wx.MessageDialog(None, msg, + "InVesalius 3", + wx.ICON_INFORMATION | wx.OK) + dlg.ShowModal() + dlg.Destroy() + + def ImportInvalidFiles(ftype="DICOM"): if ftype == "Bitmap": msg = _("There are no Bitmap, JPEG, PNG or TIFF files in the selected folder.") diff --git a/invesalius/project.py b/invesalius/project.py index d26f643..6edd077 100644 --- a/invesalius/project.py +++ b/invesalius/project.py @@ -71,6 +71,8 @@ class Project(object): # TODO: Future ++ self.annotation_dict = {} + self.compress = False + # InVesalius related data # So we can find bugs and reproduce user-related problems self.invesalius_version = version.get_svn_revision() @@ -202,17 +204,20 @@ class Project(object): measures[str(m.index)] = item return measures - def SavePlistProject(self, dir_, filename): + def SavePlistProject(self, dir_, filename, compress=False): dir_temp = tempfile.mkdtemp().decode(const.FS_ENCODE) + self.compress = compress + filename_tmp = os.path.join(dir_temp, u'matrix.dat') filelist = {} project = { # Format info - "format_version": 1, + "format_version": const.INVESALIUS_ACTUAL_FORMAT_VERSION, "invesalius_version": const.INVESALIUS_VERSION, "date": datetime.datetime.now().isoformat(), + "compress": self.compress, # case info "name": self.name, # patient's name @@ -267,7 +272,7 @@ class Project(object): # Compressing and generating the .inv3 file path = os.path.join(dir_,filename) - Compress(dir_temp, path, filelist) + Compress(dir_temp, path, filelist, compress) # Removing the temp folder. shutil.rmtree(dir_temp) @@ -295,6 +300,11 @@ class Project(object): main_plist = os.path.join(dirpath ,'main.plist') project = plistlib.readPlist(main_plist) + format_version = project["format_version"] + if format_version > const.INVESALIUS_ACTUAL_FORMAT_VERSION: + from invesalius.gui.dialogs import ImportOldFormatInvFile + ImportOldFormatInvFile() + # case info self.name = project["name"] self.modality = project["modality"] @@ -304,6 +314,8 @@ class Project(object): self.threshold_range = project["scalar_range"] self.spacing = project["spacing"] + self.compress = project.get("compress", True) + # Opening the matrix containing the slices filepath = os.path.join(dirpath, project["matrix"]["filename"]) self.matrix_filename = filepath @@ -337,7 +349,7 @@ class Project(object): measure.Load(measurements[index]) self.measurement_dict[int(index)] = measure -def Compress(folder, filename, filelist): +def Compress(folder, filename, filelist, compress=False): tmpdir, tmpdir_ = os.path.split(folder) current_dir = os.path.abspath(".") temp_inv3 = tempfile.mktemp() @@ -348,7 +360,10 @@ def Compress(folder, filename, filelist): temp_inv3 = temp_inv3.decode(const.FS_ENCODE) #os.chdir(tmpdir) #file_list = glob.glob(os.path.join(tmpdir_,"*")) - tar = tarfile.open(temp_inv3, "w:gz") + if compress: + tar = tarfile.open(temp_inv3, "w:gz") + else: + tar = tarfile.open(temp_inv3, "w") for name in filelist: tar.add(name, arcname=os.path.join(tmpdir_, filelist[name])) tar.close() @@ -361,7 +376,7 @@ def Extract(filename, folder): folder = win32api.GetShortPathName(folder) folder = folder.decode(const.FS_ENCODE) - tar = tarfile.open(filename, "r:gz") + tar = tarfile.open(filename, "r") idir = os.path.split(tar.getnames()[0])[0].decode('utf8') os.mkdir(os.path.join(folder, idir)) filelist = [] -- libgit2 0.21.2