From 61ee2fdbcce601a1f7e9be7388f665199df3ae14 Mon Sep 17 00:00:00 2001 From: "Nijsen, T" <s1006955@ru.nl> Date: Fri, 8 Nov 2019 11:07:35 +0100 Subject: [PATCH] Changed view to make it more workable to add new functionality. Added functionality for deleting files in the model and display update of the view when files are added or removed. --- mvc/controller.py | 25 ++++++++++++++ mvc/view.py | 85 +++++++++++++++++++++++++++++++++-------------- 2 files changed, 85 insertions(+), 25 deletions(-) diff --git a/mvc/controller.py b/mvc/controller.py index 1eb49fd4..a9f898c0 100644 --- a/mvc/controller.py +++ b/mvc/controller.py @@ -15,7 +15,9 @@ class ControllerClass: # Button bindings. Lambda is to pass argument to select_file self.view.button_source.bind("<Button>", lambda x: self.select_file(1)) + self.view.button_delete_source.bind("<Button>", lambda x: self.delete_file("source")) self.view.button_shadow.bind("<Button>", lambda x: self.select_file(2)) + self.view.button_delete_shadow.bind("<Button>", lambda x: self.delete_file("shadow")) self.view.comparison_button.bind("<Button>", lambda x: self.check_for_files()) self.view.save_button.bind("<Button>", @@ -122,3 +124,26 @@ class ControllerClass: option_str = self.view.save_options_view.curr_option self.model.set_save_pref(option_str.get()) self.view.save_options_view.hide() + + def delete_file(self, type_code): + """"Updates the view and model, it removes the currently selected + option in either source or shadow option, depending on what was + selected by the user. """ + if type_code == "source": + curr_entry = self.view.curr_source.get() + curr_source = curr_entry[:-4].upper() + if curr_source in self.model.file_path_source.keys(): + del self.model.file_path_source[curr_source] + self.view.remove_file_entry(curr_entry, "source") + else: + curr_entry = self.view.curr_shadow.get() + curr_shadow = curr_entry[:-4].upper() + curr_part = curr_shadow.split('_')[0] + curr_shadow = curr_shadow.split('_')[-1] + + for part_num, part_data in self.model.file_path_shadow.items(): + if curr_part == part_num: + curr_shadow_files = part_data.get_shadow_files() + if curr_shadow in curr_shadow_files.keys(): + del curr_shadow_files[curr_shadow] + self.view.remove_file_entry(curr_entry, "shadow") diff --git a/mvc/view.py b/mvc/view.py index 7639bc5a..b42549db 100644 --- a/mvc/view.py +++ b/mvc/view.py @@ -23,17 +23,37 @@ class ViewClass: # File data names, list of file name strings self.source_files_options = [] self.shadow_files_options = [] - self.curr_source = None - self.curr_shadow = None + self.curr_source = StringVar() + self.curr_source.set("No file selected") + self.curr_shadow = StringVar() + self.curr_shadow.set("No file selected") + + # Label for file selection + self.selection_label = Label(self.frame, + text="Select files to compare") + self.selection_label.grid(column=1, row=1, columnspan=2, pady=10, + padx=80) + + # LabelsFrames for groups source & shadow + self.tf1_label = LabelFrame(self.frame, text="Source: ") + self.tf1_label.grid(column=1, row=2, padx=15, pady=10) + self.tf2_label = LabelFrame(self.frame, text="Shadow: ") + self.tf2_label.grid(column=2, row=2, padx=15, pady=10) # Buttons (clicky bois) - self.button_source = Button(self.frame, text="Select source file...", + self.button_source = Button(self.tf1_label, text="Select source file...", width=17) - self.button_source.grid(column=1, row=2, pady=5) + self.button_source.grid(column=1, row=5, padx=5, pady=10) + + self.button_delete_source = Button(self.tf1_label, text="Delete source", width=17) + self.button_delete_source.grid(column=1, row=6, padx=5, pady=10) - self.button_shadow = Button(self.frame, text="Select shadowed file...", + self.button_shadow = Button(self.tf2_label, text="Select shadowed file...", width=17) - self.button_shadow.grid(column=1, row=3, pady=5) + self.button_shadow.grid(column=2, row=5, padx=5, pady=10) + + self.button_delete_shadow = Button(self.tf2_label, text="Delete shadow", width=17) + self.button_delete_shadow.grid(column=2, row=6, padx=5, pady=10) self.comparison_button = Button(self.frame, text="Compare") self.comparison_button.grid(column=1, row=4, pady=10) @@ -41,25 +61,15 @@ class ViewClass: self.save_button = Button(self.frame, text="Save result") self.save_button.grid(column=2, row=4, padx=40) - # Label for file selection - self.selection_label = Label(self.frame, - text="Select files to compare") - self.selection_label.grid(column=1, row=1, columnspan=2, pady=10, - padx=80) - - # Labels for text frames - self.tf1_label = Label(self.frame, text="Source: ") - self.tf1_label.grid(column=2, row=2) - self.tf2_label = Label(self.frame, text="Shadow: ") - self.tf2_label.grid(column=2, row=3) - # Combobox for file selection - self.filebox_1 = ttk.Combobox(self.frame, variable=self.curr_source, - postcommand=self.get_sources, width=20) - self.filebox_1.grid(column=3, row=2, sticky=W, padx=10) - self.filebox_2 = ttk.Combobox(self.frame, variable=self.curr_shadow, - postcommand=self.get_shadows, width=20) - self.filebox_2.grid(column=3, row=3, sticky=W, padx=10) + self.filebox_1 = ttk.Combobox(self.tf1_label, textvariable=self.curr_source, + postcommand=self.get_sources, state="readonly", + width=20) + self.filebox_1.grid(column=1, row=2, sticky=W, padx=10, pady=10) + self.filebox_2 = ttk.Combobox(self.tf2_label, textvariable=self.curr_shadow, + postcommand=self.get_shadows, state="readonly", + width=20) + self.filebox_2.grid(column=2, row=2, sticky=W, padx=10, pady=10) # Message label self.message_label = Label(self.frame, text="") @@ -81,13 +91,38 @@ class ViewClass: self.window.mainloop() def add_file_entry(self, entry, type_code): - """"Adds a file name to the combobox displaying the file options. + """"Adds a file name to the combobox values displaying the file options. + When a new file is added, it displays the last selected one. Type code optionsL 'source' & 'shadow'. """ if type_code == "source": self.source_files_options.append(entry) + self.get_sources() + self.curr_source.set(self.filebox_1['values'][-1]) else: self.shadow_files_options.append(entry) + self.get_shadows() + self.curr_shadow.set(self.filebox_2['values'][-1]) + + def remove_file_entry(self, entry, type_code): + """"Removes a file name to the combobox values displaying the file options. + When a new file is added, it displays the last selected one. + Type code optionsL 'source' & 'shadow'. + """ + if type_code == "source": + self.source_files_options.remove(entry) + self.get_sources() + if len(self.filebox_1['values']) > 0: + self.curr_source.set(self.filebox_1['values'][-1]) + else: + self.curr_source.set("No file selected") + else: + self.shadow_files_options.remove(entry) + self.get_shadows() + if len(self.filebox_2['values']) > 0: + self.curr_shadow.set(self.filebox_2['values'][-1]) + else: + self.curr_shadow.set("No file selected") def get_sources(self): self.filebox_1['values'] = self.source_files_options -- GitLab