From 34e44f4b3c0b5f91b00e1b789e58349bf2e86965 Mon Sep 17 00:00:00 2001 From: tobias Date: Sun, 4 Feb 2024 14:45:12 +0100 Subject: [PATCH] Observer pattern, Bugfixes --- .../5.4/extension.js | 3 +- .../5.4/res/preferences.glade | 20 +- .../5.4/src/main.py | 2 +- .../5.4/src/model/main_view_model.py | 112 ++++++-- .../5.4/src/service/display.py | 20 -- .../5.4/src/service/suntimes.py | 4 - .../5.4/src/{ui => view}/dialogs.py | 3 +- .../5.4/src/{ui => view}/main_window.py | 263 ++++++++++-------- 8 files changed, 248 insertions(+), 179 deletions(-) delete mode 100644 cinnamon-dynamic-wallpaper@TobiZog/5.4/src/service/display.py rename cinnamon-dynamic-wallpaper@TobiZog/5.4/src/{ui => view}/dialogs.py (95%) rename cinnamon-dynamic-wallpaper@TobiZog/5.4/src/{ui => view}/main_window.py (79%) diff --git a/cinnamon-dynamic-wallpaper@TobiZog/5.4/extension.js b/cinnamon-dynamic-wallpaper@TobiZog/5.4/extension.js index 43e6c51..ee7477d 100644 --- a/cinnamon-dynamic-wallpaper@TobiZog/5.4/extension.js +++ b/cinnamon-dynamic-wallpaper@TobiZog/5.4/extension.js @@ -127,8 +127,7 @@ CinnamonDynamicWallpaperExtension.prototype = { notification.addButton("open-settings", _("Open settings")); notification.connect("action-invoked", () => - Util.spawnCommandLine("/usr/bin/env python3 " + - DIRECTORY.path + "/preferences.py")); + Util.spawnCommandLine("/usr/bin/env python3 " + DIRECTORY.path + "/src/main.py")); } // Put all together diff --git a/cinnamon-dynamic-wallpaper@TobiZog/5.4/res/preferences.glade b/cinnamon-dynamic-wallpaper@TobiZog/5.4/res/preferences.glade index 6478fe2..0f3435a 100644 --- a/cinnamon-dynamic-wallpaper@TobiZog/5.4/res/preferences.glade +++ b/cinnamon-dynamic-wallpaper@TobiZog/5.4/res/preferences.glade @@ -517,7 +517,7 @@ True False - 00:00 - 07:35 + 00:00 - 00:59 @@ -531,7 +531,7 @@ True False - 08:15 - 08:43 + 02:00 - 02:59 @@ -545,7 +545,7 @@ True False - 11:13 - 13:12 + 04:00 - 04:59 @@ -559,7 +559,7 @@ True False - 14:42 - 15:41 + 06:00 - 06:59 @@ -573,7 +573,7 @@ True False - 16:13 - 16:52 + 08:00 - 08:59 @@ -789,7 +789,7 @@ True False - 07:35 - 08:14 + 01:00 - 01:59 @@ -803,7 +803,7 @@ True False - 08:44 - 11:12 + 03:00 - 03:59 @@ -817,7 +817,7 @@ True False - 13:13 - 14:41 + 05:00 - 05:59 @@ -831,7 +831,7 @@ True False - 15:41 - 16:12 + 07:00 - 07:59 @@ -845,7 +845,7 @@ True False - 16:53 - 23:59 + 09:00 - 09:59 diff --git a/cinnamon-dynamic-wallpaper@TobiZog/5.4/src/main.py b/cinnamon-dynamic-wallpaper@TobiZog/5.4/src/main.py index 5b5175a..dc5bd96 100755 --- a/cinnamon-dynamic-wallpaper@TobiZog/5.4/src/main.py +++ b/cinnamon-dynamic-wallpaper@TobiZog/5.4/src/main.py @@ -1,7 +1,7 @@ #!/usr/bin/python3 import sys -from ui.main_window import * +from view.main_window import * from model.main_view_model import * if __name__ == "__main__": diff --git a/cinnamon-dynamic-wallpaper@TobiZog/5.4/src/model/main_view_model.py b/cinnamon-dynamic-wallpaper@TobiZog/5.4/src/model/main_view_model.py index e962098..a26479e 100644 --- a/cinnamon-dynamic-wallpaper@TobiZog/5.4/src/model/main_view_model.py +++ b/cinnamon-dynamic-wallpaper@TobiZog/5.4/src/model/main_view_model.py @@ -1,8 +1,7 @@ -import os, json +import os from PIL import Image -from gi.repository import Gio +from gi.repository import Gio, Gdk -from service.display import * from service.cinnamon_pref_handler import * from service.suntimes import * from service.time_bar_chart import * @@ -10,7 +9,12 @@ from service.location import * from enums.PeriodSourceEnum import * class Main_View_Model: + """ The main ViewModel for the application + """ + def __init__(self) -> None: + """ Initialization + """ # Paths self.WORKING_DIR = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) self.RES_DIR = self.WORKING_DIR + "/res" @@ -27,7 +31,6 @@ class Main_View_Model: self.network_location_provider = ["geojs.io", "ip-api.com", "ipwho.is"] # Objects from scripts - self.screen_height = Display().get_screen_height() self.cinnamon_prefs = Cinnamon_Pref_Handler() self.time_bar_chart = Time_Bar_Chart() self.suntimes = Suntimes() @@ -35,11 +38,15 @@ class Main_View_Model: self.background_settings = Gio.Settings.new("org.cinnamon.desktop.background") - # Breakpoint for smaller UI + # Other Variables + self.display = Gdk.Display.get_default() + self.screen_height = self.display.get_monitor(0).get_geometry().height self.breakpoint_ui = 1000 def refresh_charts(self): + """ Refreshes the two variants of the time bar charts + """ # Stores the start times of the periods in minutes since midnight time_periods_min = [] @@ -89,7 +96,7 @@ class Main_View_Model: return current_location['success'] - def string_to_time_converter(raw_str: str) -> time: + def string_to_time_converter(self, raw_str: str) -> time: """ Convert a time string like "12:34" to a time object Args: @@ -102,23 +109,32 @@ class Main_View_Model: minute = raw_str[raw_str.find(":") + 1:] return time(hour=int(hour), minute=int(minute)) + + + def time_to_string_converter(self, time: time) -> str: + """ Convert a time object to a string like "12:34" + + Args: + time (time): Given time object to convert + + Returns: + str: Converted string + """ + return "{:0>2}:{:0>2}".format(time.hour, time.minute) - def calulate_time_periods(self) -> list: + def calulate_time_periods(self) -> list[time]: + """ Calculate the ten time periods based on the period source in the preferences + + Returns: + list[time]: Time periods + """ + result = [] + if self.cinnamon_prefs.period_source == PeriodSourceEnum.CUSTOMTIMEPERIODS: # User uses custom time periods - return [ - self.string_to_time_converter(self.cinnamon_prefs.period_custom_start_time[0]), - self.string_to_time_converter(self.cinnamon_prefs.period_custom_start_time[1]), - self.string_to_time_converter(self.cinnamon_prefs.period_custom_start_time[2]), - self.string_to_time_converter(self.cinnamon_prefs.period_custom_start_time[3]), - self.string_to_time_converter(self.cinnamon_prefs.period_custom_start_time[4]), - self.string_to_time_converter(self.cinnamon_prefs.period_custom_start_time[5]), - self.string_to_time_converter(self.cinnamon_prefs.period_custom_start_time[6]), - self.string_to_time_converter(self.cinnamon_prefs.period_custom_start_time[7]), - self.string_to_time_converter(self.cinnamon_prefs.period_custom_start_time[8]), - self.string_to_time_converter(self.cinnamon_prefs.period_custom_start_time[9]) - ] + for i in range(0, 10): + result.append(self.string_to_time_converter(self.cinnamon_prefs.period_custom_start_time[i])) else: # Time periods have to be estimate by coordinates if self.cinnamon_prefs.period_source == PeriodSourceEnum.NETWORKLOCATION: @@ -131,7 +147,9 @@ class Main_View_Model: self.suntimes.calc_suntimes(self.cinnamon_prefs.latitude_custom, self.cinnamon_prefs.longitude_custom) # Return the time periods - return self.suntimes.day_periods + result = self.suntimes.day_periods + + return result def refresh_image(self): @@ -149,7 +167,7 @@ class Main_View_Model: # Replace the image URI, if it's not the last time period of the day if start_times[i] <= time_now and time_now < start_times[i + 1]: self.current_image_uri = self.cinnamon_prefs.source_folder + self.cinnamon_prefs.period_images[i] - break + break # Set the background self.background_settings['picture-uri'] = "file://" + self.current_image_uri @@ -157,6 +175,58 @@ class Main_View_Model: # Set background stretching self.background_settings['picture-options'] = self.cinnamon_prefs.picture_aspect + + def get_images_from_folder(self, URI: str) -> list: + """ List all images in a folder + + Args: + URI (str): Absolute path of the folder + + Returns: + list: List of file names which are images + """ + items = [] + + for file in os.listdir(URI): + if file.endswith(("jpg", "jpeg", "png", "bmp", "svg")): + items.append(file) + + items.sort() + return items + + + def extract_heic_file(self, file_uri: str) -> bool: + """ Extract a heic file to an internal folder + + Args: + file_uri (str): Absolute path to the heic file + + Returns: + bool: Extraction was successful + """ + try: + extract_folder = self.IMAGES_DIR + "/extracted_images/" + + file_name: str = file_uri[file_uri.rfind("/") + 1:] + file_name = file_name[:file_name.rfind(".")] + + # Create the buffer folder if its not existing + try: + os.mkdir(extract_folder) + except: + pass + + # Cleanup the folder + for file in self.get_images_from_folder(extract_folder): + os.remove(extract_folder + file) + + # Extract the HEIC file + os.system("heif-convert '" + file_uri + "' '" + extract_folder + file_name + ".jpg'") + + return True + except: + return False + def set_background_gradient(self): """ Setting a gradient background to hide images, which are not high enough diff --git a/cinnamon-dynamic-wallpaper@TobiZog/5.4/src/service/display.py b/cinnamon-dynamic-wallpaper@TobiZog/5.4/src/service/display.py deleted file mode 100644 index ddd2274..0000000 --- a/cinnamon-dynamic-wallpaper@TobiZog/5.4/src/service/display.py +++ /dev/null @@ -1,20 +0,0 @@ -import gi -gi.require_version("Gtk", "3.0") -from gi.repository import Gdk - -class Display: - """ Handling display informations and actions - """ - def __init__(self) -> None: - self.display = Gdk.Display.get_default() - - - def get_screen_height(self) -> int: - """ Estimate the height resolution of the primary display - - Returns: - int: Height in pixel - """ - geometry = self.display.get_monitor(0).get_geometry() - - return geometry.height diff --git a/cinnamon-dynamic-wallpaper@TobiZog/5.4/src/service/suntimes.py b/cinnamon-dynamic-wallpaper@TobiZog/5.4/src/service/suntimes.py index 02e8fde..65e6200 100644 --- a/cinnamon-dynamic-wallpaper@TobiZog/5.4/src/service/suntimes.py +++ b/cinnamon-dynamic-wallpaper@TobiZog/5.4/src/service/suntimes.py @@ -10,10 +10,6 @@ class Suntimes: """ def __init__(self) -> None: """ Initialization - - Args: - latitude (float): Latitude of the position - longitude (float): Longitude of the position """ self.today = datetime.now() diff --git a/cinnamon-dynamic-wallpaper@TobiZog/5.4/src/ui/dialogs.py b/cinnamon-dynamic-wallpaper@TobiZog/5.4/src/view/dialogs.py similarity index 95% rename from cinnamon-dynamic-wallpaper@TobiZog/5.4/src/ui/dialogs.py rename to cinnamon-dynamic-wallpaper@TobiZog/5.4/src/view/dialogs.py index c0a8cd8..636c3e0 100644 --- a/cinnamon-dynamic-wallpaper@TobiZog/5.4/src/ui/dialogs.py +++ b/cinnamon-dynamic-wallpaper@TobiZog/5.4/src/view/dialogs.py @@ -32,11 +32,10 @@ class Dialogs(Gtk.Window): dialog.set_default_size(800, 400) response = dialog.run() + location = "" if response == Gtk.ResponseType.OK: location = dialog.get_filename() - elif response == Gtk.ResponseType.CANCEL: - location = "" dialog.destroy() diff --git a/cinnamon-dynamic-wallpaper@TobiZog/5.4/src/ui/main_window.py b/cinnamon-dynamic-wallpaper@TobiZog/5.4/src/view/main_window.py similarity index 79% rename from cinnamon-dynamic-wallpaper@TobiZog/5.4/src/ui/main_window.py rename to cinnamon-dynamic-wallpaper@TobiZog/5.4/src/view/main_window.py index 16f3b13..646362b 100644 --- a/cinnamon-dynamic-wallpaper@TobiZog/5.4/src/ui/main_window.py +++ b/cinnamon-dynamic-wallpaper@TobiZog/5.4/src/view/main_window.py @@ -8,17 +8,13 @@ gi.require_version("Gtk", "3.0") from gi.repository import Gtk, GdkPixbuf # Packages -import time, subprocess +import subprocess from datetime import timedelta # Local scripts from model.main_view_model import * -from service.images import * -from service.suntimes import * -from service.time_bar_chart import * -from ui.dialogs import * +from view.dialogs import * from enums.ImageSourceEnum import * -from enums.NetworkLocationProvider import * from enums.PeriodSourceEnum import * @@ -43,10 +39,8 @@ class Main_Window: # Objects from scripts - self.images = Images() self.dialogs = Dialogs() - self.suntimes = Suntimes() - self.time_bar_chart = Time_Bar_Chart() + # Page 1: Image Configuration @@ -72,7 +66,7 @@ class Main_Window: # Time bar chart self.img_bar_images: Gtk.Image = self.builder.get_object("img_bar_images") - self.etr_periods: list[Gtk.Entry] = [ + self.etr_periods: list[Gtk.Label] = [ self.builder.get_object("etr_period_1"), self.builder.get_object("etr_period_2"), self.builder.get_object("etr_period_3"), self.builder.get_object("etr_period_4"), self.builder.get_object("etr_period_5"), self.builder.get_object("etr_period_6"), @@ -173,17 +167,11 @@ class Main_Window: # Page 1: Image Configuration self.add_items_to_combo_box(self.cb_image_set, self.view_model.image_sets) - - self.tb_image_set.set_active(self.view_model.cinnamon_prefs.image_source == ImageSourceEnum.IMAGESET) - self.tb_heic_file.set_active(self.view_model.cinnamon_prefs.image_source == ImageSourceEnum.HEICFILE) - self.tb_source_folder.set_active(self.view_model.cinnamon_prefs.image_source == ImageSourceEnum.SOURCEFOLDER) + self.image_source = self.image_source # This triggers the @image_source.setter # Page 2: Location & Times self.add_items_to_combo_box(self.cb_network_provider, self.view_model.network_location_provider) - - self.tb_network_location.set_active(self.view_model.cinnamon_prefs.period_source == PeriodSourceEnum.NETWORKLOCATION) - self.tb_custom_location.set_active(self.view_model.cinnamon_prefs.period_source == PeriodSourceEnum.CUSTOMLOCATION) - self.tb_time_periods.set_active(self.view_model.cinnamon_prefs.period_source == PeriodSourceEnum.CUSTOMTIMEPERIODS) + self.period_source = self.period_source # This triggers the @period_source.setter # Page 3: Behaviour self.add_items_to_combo_box(self.cb_picture_aspect, self.view_model.picture_aspects) @@ -195,6 +183,81 @@ class Main_Window: Gtk.main() + ############################################################ + # Observer # + ############################################################ + + + @property + def selected_image_set(self): + return self.view_model.cinnamon_prefs.selected_image_set + + @selected_image_set.setter + def selected_image_set(self, new_value): + # Save to the preferences + self.view_model.cinnamon_prefs.selected_image_set = new_value + + # Refresh images + image_names = self.view_model.get_images_from_folder(self.view_model.cinnamon_prefs.source_folder) + self.load_image_options_to_combo_boxes(image_names) + + # Image sets have the same names for the images: + # 9.jpg = Period 0 + # 1.jpg = Period 1 + # 2.jpg = Period 2... + for i in range(0, 10): + self.cb_periods[i].set_active(i + 1) + + + @property + def image_source(self): + return self.view_model.cinnamon_prefs.image_source + + @image_source.setter + def image_source(self, new_value): + self.view_model.cinnamon_prefs.image_source = new_value + + # Disable the wrong ToggleButtons + self.tb_image_set.set_active(new_value == ImageSourceEnum.IMAGESET) + self.tb_heic_file.set_active(new_value == ImageSourceEnum.HEICFILE) + self.tb_source_folder.set_active(new_value == ImageSourceEnum.SOURCEFOLDER) + + # Show or hide ListBoxRows + self.lbr_image_set.set_visible(new_value == ImageSourceEnum.IMAGESET) + self.lbr_heic_file.set_visible(new_value == ImageSourceEnum.HEICFILE) + self.lbr_source_folder.set_visible(new_value == ImageSourceEnum.SOURCEFOLDER) + + # Make the comboboxes invisible + for combobox in self.cb_periods: + combobox.set_visible(new_value != ImageSourceEnum.IMAGESET) + + + @property + def period_source(self): + return self.view_model.cinnamon_prefs.period_source + + + @period_source.setter + def period_source(self, new_value): + self.view_model.cinnamon_prefs.period_source = new_value + + self.tb_network_location.set_active(new_value == PeriodSourceEnum.NETWORKLOCATION) + self.tb_custom_location.set_active(new_value == PeriodSourceEnum.CUSTOMLOCATION) + self.tb_time_periods.set_active(new_value == PeriodSourceEnum.CUSTOMTIMEPERIODS) + + # Show/Hide the right ListBoxRows + self.lbr_network_refresh_time.set_visible(new_value == PeriodSourceEnum.NETWORKLOCATION) + self.lbr_network_provider.set_visible(new_value == PeriodSourceEnum.NETWORKLOCATION) + self.lbr_current_location.set_visible(new_value == PeriodSourceEnum.NETWORKLOCATION) + self.lbr_custom_location_longitude.set_visible(new_value == PeriodSourceEnum.CUSTOMLOCATION) + self.lbr_custom_location_latitude.set_visible(new_value == PeriodSourceEnum.CUSTOMLOCATION) + self.lbr_time_periods.set_visible(new_value == PeriodSourceEnum.CUSTOMTIMEPERIODS) + + self.refresh_charts_and_times() + + + + ############################################################ # UI Helper # ############################################################ @@ -214,6 +277,21 @@ class Main_Window: combobox.set_active(i) + def get_active_combobox_item(self, combobox: Gtk.ComboBox) -> str: + """ Request the current selected combobox label + + Args: + combobox (Gtk.ComboBox): ComboBox where to get value from + + Returns: + str: Selected value + """ + tree_iter = combobox.get_active_iter() + + model = combobox.get_model() + return model[tree_iter][0] + + def add_items_to_combo_box(self, combobox: Gtk.ComboBox, items: list): """ Add items to a combo box @@ -265,13 +343,24 @@ class Main_Window: image_preview.set_from_pixbuf(pixbuf) except: - self.dialogs.message_dialog("Error on load images. Please check the configuration!", Gtk.MessageType.ERROR) + pass - def refresh_charts(self): + def refresh_charts_and_times(self): """ Refresh the charts and put them to the image views """ self.view_model.refresh_charts() + start_times = self.view_model.calulate_time_periods() + + for i in range(0, 10): + label_txt = self.view_model.time_to_string_converter(start_times[i]) + " - " + + if i != 9: + label_txt += self.view_model.time_to_string_converter(start_times[i + 1]) + else: + label_txt += "23:59" + + self.etr_periods[i].set_text(label_txt) # Load to the views pixbuf = GdkPixbuf.Pixbuf.new_from_file(self.view_model.TIMEBAR_URI_POLYLINES) @@ -287,22 +376,6 @@ class Main_Window: # Callbacks # ############################################################ - ## Image Configuration - - def show_image_configuration_entries(self, button_id: int): - self.tb_image_set.set_active(button_id == 1) - self.tb_heic_file.set_active(button_id == 2) - self.tb_source_folder.set_active(button_id == 3) - - self.lbr_image_set.set_visible(button_id == 1) - self.lbr_heic_file.set_visible(button_id == 2) - self.lbr_source_folder.set_visible(button_id == 3) - - # Make the comboboxes invisible - for combobox in self.cb_periods: - combobox.set_visible(button_id != 1) - - # +-----------+-----------+---------------+ # | Image Set | HEIC file | Source Folder | # +-----------+-----------+---------------+ @@ -314,10 +387,9 @@ class Main_Window: button (Gtk.ToggleButton): Clicked ToggleButton """ if button.get_active(): - self.view_model.cinnamon_prefs.image_source = ImageSourceEnum.IMAGESET - self.show_image_configuration_entries(1) + self.image_source = ImageSourceEnum.IMAGESET - self.set_active_combobox_item(self.cb_image_set, self.view_model.cinnamon_prefs.selected_image_set) + self.set_active_combobox_item(self.cb_image_set, self.selected_image_set) for i, combobox in enumerate(self.cb_periods): selected_image_name = self.view_model.cinnamon_prefs.period_images[i] @@ -331,11 +403,10 @@ class Main_Window: button (Gtk.ToggleButton): Clicked ToggleButton """ if button.get_active(): - self.view_model.cinnamon_prefs.image_source = ImageSourceEnum.HEICFILE - self.show_image_configuration_entries(2) + self.image_source = ImageSourceEnum.HEICFILE # Load images from source folder - files = self.images.get_images_from_folder(self.view_model.cinnamon_prefs.source_folder) + files = self.view_model.get_images_from_folder(self.view_model.cinnamon_prefs.source_folder) if len(files) != 0: self.load_image_options_to_combo_boxes(files) @@ -354,15 +425,14 @@ class Main_Window: button (Gtk.ToggleButton): Clicked ToggleButton """ if button.get_active(): - self.view_model.cinnamon_prefs.image_source = ImageSourceEnum.SOURCEFOLDER - self.show_image_configuration_entries(3) + self.image_source = ImageSourceEnum.SOURCEFOLDER # Load the source folder to the view # This will update the comboboxes in the preview to contain the right items self.lbl_source_folder.set_label(self.view_model.cinnamon_prefs.source_folder) # Load files from saved source folder - files = self.images.get_images_from_folder(self.view_model.cinnamon_prefs.source_folder) + files = self.view_model.get_images_from_folder(self.view_model.cinnamon_prefs.source_folder) if len(files) != 0: self.load_image_options_to_combo_boxes(files) @@ -385,29 +455,16 @@ class Main_Window: Args: combobox (Gtk.ComboBox): The used ComboBox """ - tree_iter = combobox.get_active_iter() - - if tree_iter is not None and self.view_model.cinnamon_prefs.image_source == ImageSourceEnum.IMAGESET: + if self.view_model.cinnamon_prefs.image_source == ImageSourceEnum.IMAGESET: # Get the selected value - model = combobox.get_model() - selected_image_set = model[tree_iter][0] + selected_image_set = self.get_active_combobox_item(combobox) # Store to the preferences - self.view_model.cinnamon_prefs.selected_image_set = selected_image_set self.view_model.cinnamon_prefs.source_folder = \ self.view_model.IMAGES_DIR + "/included_image_sets/" + selected_image_set + "/" - # Load all possible options to the comboboxes - image_names = self.images.get_images_from_folder(self.view_model.cinnamon_prefs.source_folder) - self.load_image_options_to_combo_boxes(image_names) - - # Image sets have the same names for the images: - # 9.jpg = Period 0 - # 1.jpg = Period 1 - # 2.jpg = Period 2... - for i in range(0, 10): - self.cb_periods[i].set_active(i + 1) - + self.selected_image_set = selected_image_set + # +----------------------------------------------+ # | Select a heic file to import | (None) 📄 | @@ -423,7 +480,7 @@ class Main_Window: file_path: str = fc_button.get_filename() # Extract the heic file - result = self.images.extract_heic_file(file_path) + result = self.view_model.extract_heic_file(file_path) # Update the preferences self.view_model.cinnamon_prefs.selected_image_set = "" @@ -432,7 +489,7 @@ class Main_Window: # Load images only if the extraction was successfully if result: # Collect all extracted images and push them to the comboboxes - image_names = self.images.get_images_from_folder(self.view_model.cinnamon_prefs.source_folder) + image_names = self.view_model.get_images_from_folder(self.view_model.cinnamon_prefs.source_folder) self.load_image_options_to_combo_boxes(image_names) else: self.dialogs.message_dialog("Error during extraction!", Gtk.MessageType.ERROR) @@ -450,7 +507,7 @@ class Main_Window: button (Gtk.Button): The clicked button """ folder = self.dialogs.source_folder_dialog() - files = self.images.get_images_from_folder(folder) + files = self.view_model.get_images_from_folder(folder) # Update the preferences self.view_model.cinnamon_prefs.selected_image_set = "" @@ -476,45 +533,22 @@ class Main_Window: Args: combobox (Gtk.ComboBox): The used ComboBox """ - tree_iter = combobox.get_active_iter() - combobox_name = Gtk.Buildable.get_name(combobox) period_index = int(combobox_name[10:11]) - if tree_iter is not None: - # Get the selected value - model = combobox.get_model() - image_file_name = model[tree_iter][0] + # Get the selected value + image_file_name = self.get_active_combobox_item(combobox) - # Store selection to preferences - self.view_model.cinnamon_prefs.period_images[period_index] = image_file_name + # Store selection to preferences + self.view_model.cinnamon_prefs.period_images[period_index] = image_file_name - # Build up image path - image_path = self.view_model.cinnamon_prefs.source_folder + image_file_name + # Build up image path + image_path = self.view_model.cinnamon_prefs.source_folder + image_file_name - self.load_image_to_preview(self.img_periods[period_index], image_path) + self.load_image_to_preview(self.img_periods[period_index], image_path) ## Location & Times - - def show_location_times_entries(self, button_id: int): - """ Show or hide parts of the Locations & Times menu - - Args: - button_id (int): ID of the button, 1 = Network, 2 = Custom Location, 3 = Custom Time Periods - """ - self.tb_network_location.set_active(button_id == 1) - self.tb_custom_location.set_active(button_id == 2) - self.tb_time_periods.set_active(button_id == 3) - - # Show/Hide the right ListBoxRows - self.lbr_network_refresh_time.set_visible(button_id == 1) - self.lbr_network_provider.set_visible(button_id == 1) - self.lbr_current_location.set_visible(button_id == 1) - self.lbr_custom_location_longitude.set_visible(button_id == 2) - self.lbr_custom_location_latitude.set_visible(button_id == 2) - self.lbr_time_periods.set_visible(button_id == 3) - def on_toggle_button_network_location_clicked(self, button: Gtk.ToggleButton): """ User clicks on the ToggleButton for the network location @@ -523,19 +557,15 @@ class Main_Window: button (Gtk.ToggleButton): Clicked ToggleButton """ if button.get_active(): - self.view_model.cinnamon_prefs.period_source = PeriodSourceEnum.NETWORKLOCATION - self.show_location_times_entries(1) + self.period_source = PeriodSourceEnum.NETWORKLOCATION self.spb_network_refresh_time.set_value(self.view_model.cinnamon_prefs.location_refresh_intervals) self.set_active_combobox_item(self.cb_network_provider, self.view_model.cinnamon_prefs.network_location_provider) - self.refresh_charts() - def on_toggle_button_custom_location_clicked(self, button: Gtk.ToggleButton): if button.get_active(): - self.view_model.cinnamon_prefs.period_source = PeriodSourceEnum.CUSTOMLOCATION - self.show_location_times_entries(2) + self.period_source = PeriodSourceEnum.CUSTOMLOCATION self.etr_latitude.set_text(str(self.view_model.cinnamon_prefs.latitude_custom)) self.etr_longitude.set_text(str(self.view_model.cinnamon_prefs.longitude_custom)) @@ -543,8 +573,7 @@ class Main_Window: def on_toggle_button_time_periods_clicked(self, button: Gtk.ToggleButton): if button.get_active(): - self.view_model.cinnamon_prefs.period_source = PeriodSourceEnum.CUSTOMTIMEPERIODS - self.show_location_times_entries(3) + self.period_source = PeriodSourceEnum.CUSTOMTIMEPERIODS for i in range(0, 9): pref_value = self.view_model.cinnamon_prefs.period_custom_start_time[i + 1] @@ -580,7 +609,7 @@ class Main_Window: self.lb_period_end[index].set_text(str(time_previous_end.hour).rjust(2, '0') + ":" + str(time_previous_end.minute).rjust(2, '0')) - self.refresh_charts() + self.refresh_charts_and_times() def on_spb_network_location_refresh_time_changed(self, spin_button: Gtk.SpinButton): @@ -598,11 +627,7 @@ class Main_Window: Args: combobox (Gtk.ComboBox): The used ComboBox """ - tree_iter = combobox.get_active_iter() - - if tree_iter is not None: - model = combobox.get_model() - self.view_model.cinnamon_prefs.network_location_provider = model[tree_iter][0] + self.view_model.cinnamon_prefs.network_location_provider = self.get_active_combobox_item(combobox) success = self.view_model.refresh_location() @@ -622,7 +647,7 @@ class Main_Window: """ try: self.view_model.cinnamon_prefs.longitude_custom = float(entry.get_text()) - self.refresh_charts() + self.refresh_charts_and_times() except: pass @@ -635,7 +660,7 @@ class Main_Window: """ try: self.view_model.cinnamon_prefs.latitude_custom = float(entry.get_text()) - self.refresh_charts() + self.refresh_charts_and_times() except: pass @@ -648,11 +673,7 @@ class Main_Window: Args: combobox (Gtk.ComboBox): The used ComboBox """ - tree_iter = combobox.get_active_iter() - - if tree_iter is not None: - model = combobox.get_model() - self.view_model.cinnamon_prefs.picture_aspect = model[tree_iter][0] + self.view_model.cinnamon_prefs.picture_aspect = self.get_active_combobox_item(combobox) def on_sw_dynamic_background_color_state_set(self, _: Gtk.Switch, state: bool): @@ -700,7 +721,10 @@ class Main_Window: try: self.on_apply() except: - pass + self.dialogs.message_dialog( + "Error on apply the settings. Please check the settings and contact the developer.", + Gtk.MessageType.ERROR + ) # Close the window self.on_destroy() @@ -713,8 +737,9 @@ class Main_Window: self.view_model.cinnamon_prefs.store_preferences() # Use the new settings - # todo loop = Loop() - #loop.exchange_image() + self.view_model.refresh_image() + self.view_model.set_background_gradient() + def on_destroy(self, *args): """ Lifecycle handler when window will be destroyed