[030aeb]: / dosma / gui / gui_utils / filedialog_reader.py

Download this file

74 lines (51 with data), 2.1 kB

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import os
from tkinter import StringVar, filedialog
from dosma.core.io import format_io_utils as fio_utils
from dosma.gui.preferences_viewer import PreferencesManager
class FileDialogReader:
SUPPORTED_FORMATS = (("nifti files", "*.nii\.gz"), ("dicom files", "*.dcm")) # noqa: W605
__base_filepath = "../"
def __init__(self, string_var: StringVar = None):
self.preferences = PreferencesManager()
self.string_var = string_var
def load_volume(self, title="Select volume file(s)"):
filepath = self.get_volume_filepath(title)
im = fio_utils.generic_load(filepath, 1)
return im
def get_volume_filepath(self, title="Select path", im_type: fio_utils.ImageDataFormat = None):
filetypes = None
if im_type is fio_utils.ImageDataFormat.dicom:
filetypes = ((im_type.name, "*.dcm"),)
files = filedialog.askopenfilenames(
initialdir=self.__base_filepath, title=title, filetypes=filetypes
)
if len(files) == 0:
return
filepath = files[0]
self.__base_filepath = os.path.dirname(filepath)
if filepath.endswith(".dcm"):
filepath = os.path.dirname(filepath)
if self.string_var:
self.string_var.set(filepath)
return filepath
def get_filepath(self, title="Select file"):
file_str = filedialog.askopenfilename(initialdir=self.__base_filepath, title=title)
if not file_str:
return
if self.string_var:
self.string_var.set(file_str)
return file_str
def get_dirpath(self, title="Select directory"):
file_str = filedialog.askdirectory(initialdir=self.__base_filepath, title=title)
if not file_str:
return
if self.string_var:
self.string_var.set(file_str)
return file_str
def get_save_dirpath(self):
file_str = filedialog.askdirectory(initialdir=self.__base_filepath, mustexist=False)
if not file_str:
return
if self.string_var:
self.string_var.set(file_str)
return file_str