Start migration to MVVM

This commit is contained in:
2024-02-02 15:30:33 +01:00
parent fe1cea9e2b
commit 8acc18c7fe
115 changed files with 942 additions and 820 deletions

View File

@@ -0,0 +1,4 @@
class ImageSourceEnum(enumerate):
IMAGESET = "image_set"
HEICFILE = "heic_file"
SOURCEFOLDER = "source_folder"

View File

@@ -0,0 +1,5 @@
from enum import Enum
class NetworkLocationProvider(Enum):
GEOJS = "https://get.geojs.io/v1/ip/geo.json"
IPAPI = "http://ip-api.com/json/?fields=61439"

View File

@@ -0,0 +1,4 @@
class PeriodSourceEnum(enumerate):
NETWORKLOCATION = "network_location"
CUSTOMLOCATION = "custom_location"
CUSTOMTIMEPERIODS = "custom_time_periods"

View File

@@ -0,0 +1,110 @@
#!/usr/bin/python3
from scripts.cinnamon_pref_handler import *
from scripts.suntimes import *
from datetime import datetime, time
from enums.PeriodSourceEnum import *
from scripts.location import *
from gi.repository import Gio
from PIL import Image
class Loop():
def __init__(self) -> None:
self.prefs = Cinnamon_Pref_Handler()
self.suntimes = Suntimes()
self.location = Location()
self.background_settings = Gio.Settings.new("org.cinnamon.desktop.background")
# Position should estimate by network
if self.prefs.period_source == PeriodSourceEnum.NETWORKLOCATION:
current_location = self.location.get_location()
self.suntimes.calc_suntimes(float(current_location["latitude"]), float(current_location["longitude"]))
self.start_times = self.suntimes.day_periods
# Position is given by user
elif self.prefs.period_source == PeriodSourceEnum.CUSTOMLOCATION:
self.suntimes.calc_suntimes(float(self.prefs.latitude_custom), float(self.prefs.longitude_custom))
self.start_times = self.suntimes.day_periods
# No position, concrete times
else:
def string_to_time_converter(raw_str: str) -> time:
hour = raw_str[0:raw_str.find(":")]
minute = raw_str[raw_str.find(":") + 1:]
return time(hour=int(hour), minute=int(minute))
self.start_times = [
string_to_time_converter(self.prefs.period_custom_start_time[0]),
string_to_time_converter(self.prefs.period_custom_start_time[1]),
string_to_time_converter(self.prefs.period_custom_start_time[2]),
string_to_time_converter(self.prefs.period_custom_start_time[3]),
string_to_time_converter(self.prefs.period_custom_start_time[4]),
string_to_time_converter(self.prefs.period_custom_start_time[5]),
string_to_time_converter(self.prefs.period_custom_start_time[6]),
string_to_time_converter(self.prefs.period_custom_start_time[7]),
string_to_time_converter(self.prefs.period_custom_start_time[8]),
string_to_time_converter(self.prefs.period_custom_start_time[9])
]
def exchange_image(self):
""" Replace the desktop image
"""
# Get the time of day
time_now = time(datetime.now().hour, datetime.now().minute)
# Assign the last image as fallback
self.current_image_uri = self.prefs.source_folder + self.prefs.period_images[9]
for i in range(0, 9):
# Replace the image URI, if it's not the last time period of the day
if self.start_times[i] <= time_now and time_now < self.start_times[i + 1]:
self.current_image_uri = self.prefs.source_folder + self.prefs.period_images[i]
break
# Set the background
self.background_settings['picture-uri'] = "file://" + self.current_image_uri
# Set background stretching
self.background_settings['picture-options'] = self.prefs.picture_aspect
self.set_background_gradient()
def set_background_gradient(self):
""" Setting a gradient background to hide images, which are not high enough
"""
# Load the image
try:
im = Image.open(self.current_image_uri)
pix = im.load()
# Width and height of the current setted image
width, height = im.size
# Color of the top and bottom pixel in the middle of the image
top_color = pix[width / 2,0]
bottom_color = pix[width / 2, height - 1]
# Create the gradient
self.background_settings['color-shading-type'] = "vertical"
if self.prefs.dynamic_background_color:
self.background_settings['primary-color'] = f"#{top_color[0]:x}{top_color[1]:x}{top_color[2]:x}"
self.background_settings['secondary-color'] = f"#{bottom_color[0]:x}{bottom_color[1]:x}{bottom_color[2]:x}"
else:
self.background_settings['primary-color'] = "#000000"
self.background_settings['secondary-color'] = "#000000"
except:
self.background_settings['primary-color'] = "#000000"
self.background_settings['secondary-color'] = "#000000"
# Needed for JavaScript
if __name__ == "__main__":
l = Loop()
l.exchange_image()

View File

@@ -0,0 +1,81 @@
import os, json
from pathlib import Path
from service.display import *
from service.cinnamon_pref_handler import *
from service.suntimes import *
from service.time_bar_chart import *
from enums.PeriodSourceEnum import *
class Main_View_Model:
def __init__(self) -> None:
# Paths
self.WORKING_DIR = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
self.RES_DIR = self.WORKING_DIR + "/res"
self.IMAGES_DIR = self.RES_DIR + "/images"
self.GLADE_URI = self.RES_DIR + "/preferences.glade"
self.TIMEBAR_URI = self.WORKING_DIR + "/src/time_bar.svg"
self.TIMEBAR_URI_POLYLINES = self.WORKING_DIR + "/src/time_bar_polylines.svg"
self.PREF_URI = os.path.expanduser("~") + \
"/.config/cinnamon/spices/cinnamon-dynamic-wallpaper@TobiZog/cinnamon-dynamic-wallpaper@TobiZog.json"
# Datasets
self.image_sets = ["aurora", "beach", "bitday", "cliffs", "earth", "gradient", "lakeside", "mountains", "sahara"]
self.picture_aspects = ["centered", "scaled", "stretched", "zoom", "spanned"]
self.network_location_provider = ["geojs.io", "ip-api.com"]
# 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()
# Breakpoint for smaller UI
self.breakpoint_ui = 1000
def refresh_charts(self):
# Stores the start times of the periods in minutes since midnight
time_periods_min = []
if self.cinnamon_prefs.period_source == PeriodSourceEnum.CUSTOMTIMEPERIODS:
for i in range(0, 10):
time_str = self.cinnamon_prefs.period_custom_start_time[i]
time_periods_min.append(int(time_str[0:2]) * 60 + int(time_str[3:5]))
else:
if self.cinnamon_prefs.period_source == PeriodSourceEnum.NETWORKLOCATION:
self.suntimes.calc_suntimes(float(self.cinnamon_prefs.latitude_auto), float(self.cinnamon_prefs.longitude_auto))
else:
pass
# todo self.suntimes.calc_suntimes(float(self.ui.etr_latitude.get_text()), float(self.ui.etr_longitude.get_text()))
# Get all time periods. Store the minutes to the list and print the values to the text views
for i in range(0, 10):
time_range_now = self.suntimes.day_periods[i]
if i != 9:
time_range_next = self.suntimes.day_periods[i + 1]
else:
time_range_next = time(hour=23, minute=59)
# todo self.ui.etr_periods[i].set_text(
# str(time_range_now.hour).rjust(2, '0') + ":" + str(time_range_now.minute).rjust(2, '0') + \
# " - " + str(time_range_next.hour).rjust(2, '0') + ":" + str(time_range_next.minute).rjust(2, '0'))
time_periods_min.append(time_range_now.hour * 60 + time_range_now.minute)
# Create time bar
# Reduce size for small displays
if self.screen_height < self.breakpoint_ui:
bar_width = 1150
bar_height = 110
else:
bar_width = 1300
bar_height = 150
self.time_bar_chart.create_bar_chart_with_polylines(self.TIMEBAR_URI_POLYLINES, bar_width, bar_height, time_periods_min)
self.time_bar_chart.create_bar_chart(self.TIMEBAR_URI, bar_width, bar_height, time_periods_min)

View File

@@ -0,0 +1,57 @@
#!/usr/bin/python3
############################################################
# Imports #
############################################################
# Packages
import gi, os, subprocess, time
from datetime import timedelta
# Local scripts
from scripts import cinnamon_pref_handler, dialogs, display, images, location, suntimes, time_bar_chart, ui
from loop import *
from enums.ImageSourceEnum import ImageSourceEnum
from enums.PeriodSourceEnum import PeriodSourceEnum
from enums.NetworkLocationProvider import NetworkLocationProvider
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk, GdkPixbuf
class Preferences:
""" Preference window class
"""
############################################################
# Lifecycle #
############################################################
def __init__(self) -> None:
# UI helper object
self.ui = ui.UI(self.builder)
# Layout breakpoint for smaller displays
self.smaller_ui_height = 1000
def show(self):
""" Display the window to the screen
"""
self.ui.show_main_window(self.builder, self.smaller_ui_height, self.prefs)
# Time diagram
try:
self.refresh_chart()
except:
self.dialogs.message_dialog("Error on creating time bar!")
if __name__ == "__main__":
Preferences().show()

View File

@@ -0,0 +1,100 @@
import os, json
class Cinnamon_Pref_Handler:
""" Class to work with the Cinnamon Extension preference format
"""
def __init__(self) -> None:
# Location of the Cinnamon preference file since Cinnamon 5.4
self.pref_location = os.path.expanduser("~") + \
"/.config/cinnamon/spices/cinnamon-dynamic-wallpaper@TobiZog/cinnamon-dynamic-wallpaper@TobiZog.json"
self.load_preferences()
def load_preferences(self):
""" Load the JSON preferences to the Preference object
"""
with open(self.pref_location, "r") as pref_file:
pref_data = json.load(pref_file)
self.picture_aspect = pref_data['picture_aspect']['value']
self.dynamic_background_color = pref_data['dynamic_background_color']['value']
self.image_source = pref_data['image_source']['value']
self.selected_image_set = pref_data['selected_image_set']['value']
self.source_folder = pref_data['source_folder']['value']
self.period_images = [
pref_data['period_0_image']['value'],
pref_data['period_1_image']['value'],
pref_data['period_2_image']['value'],
pref_data['period_3_image']['value'],
pref_data['period_4_image']['value'],
pref_data['period_5_image']['value'],
pref_data['period_6_image']['value'],
pref_data['period_7_image']['value'],
pref_data['period_8_image']['value'],
pref_data['period_9_image']['value']
]
self.period_source = pref_data['period_source']['value']
self.location_refresh_intervals = pref_data['location_refresh_intervals']['value']
self.latitude_auto = pref_data['latitude_auto']['value']
self.longitude_auto = pref_data['longitude_auto']['value']
self.latitude_custom = pref_data['latitude_custom']['value']
self.longitude_custom = pref_data['longitude_custom']['value']
self.period_custom_start_time = [
pref_data['period_0_custom_start_time']['value'],
pref_data['period_1_custom_start_time']['value'],
pref_data['period_2_custom_start_time']['value'],
pref_data['period_3_custom_start_time']['value'],
pref_data['period_4_custom_start_time']['value'],
pref_data['period_5_custom_start_time']['value'],
pref_data['period_6_custom_start_time']['value'],
pref_data['period_7_custom_start_time']['value'],
pref_data['period_8_custom_start_time']['value'],
pref_data['period_9_custom_start_time']['value']
]
def store_preferences(self):
""" Store the values of the Preference object to the JSON file
"""
with open(self.pref_location, "r") as pref_file:
pref_data = json.load(pref_file)
pref_data['picture_aspect']['value'] = self.picture_aspect
pref_data['dynamic_background_color']['value'] = self.dynamic_background_color
pref_data['image_source']['value'] = self.image_source
pref_data['selected_image_set']['value'] = self.selected_image_set
pref_data['source_folder']['value'] = self.source_folder
pref_data['period_0_image']['value'] = self.period_images[0]
pref_data['period_1_image']['value'] = self.period_images[1]
pref_data['period_2_image']['value'] = self.period_images[2]
pref_data['period_3_image']['value'] = self.period_images[3]
pref_data['period_4_image']['value'] = self.period_images[4]
pref_data['period_5_image']['value'] = self.period_images[5]
pref_data['period_6_image']['value'] = self.period_images[6]
pref_data['period_7_image']['value'] = self.period_images[7]
pref_data['period_8_image']['value'] = self.period_images[8]
pref_data['period_9_image']['value'] = self.period_images[9]
pref_data['period_source']['value'] = self.period_source
pref_data['location_refresh_intervals']['value'] = self.location_refresh_intervals
pref_data['latitude_auto']['value'] = self.latitude_auto
pref_data['longitude_auto']['value'] = self.longitude_auto
pref_data['latitude_custom']['value'] = self.latitude_custom
pref_data['longitude_custom']['value'] = self.longitude_custom
pref_data['period_0_custom_start_time']['value'] = self.period_custom_start_time[0]
pref_data['period_1_custom_start_time']['value'] = self.period_custom_start_time[1]
pref_data['period_2_custom_start_time']['value'] = self.period_custom_start_time[2]
pref_data['period_3_custom_start_time']['value'] = self.period_custom_start_time[3]
pref_data['period_4_custom_start_time']['value'] = self.period_custom_start_time[4]
pref_data['period_5_custom_start_time']['value'] = self.period_custom_start_time[5]
pref_data['period_6_custom_start_time']['value'] = self.period_custom_start_time[6]
pref_data['period_7_custom_start_time']['value'] = self.period_custom_start_time[7]
pref_data['period_8_custom_start_time']['value'] = self.period_custom_start_time[8]
pref_data['period_9_custom_start_time']['value'] = self.period_custom_start_time[9]
# Write to file
with open(self.pref_location, "w") as pref_file:
json.dump(pref_data, pref_file, separators=(',', ':'), indent=4)

View File

@@ -0,0 +1,20 @@
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

View File

@@ -0,0 +1,60 @@
import os
class Images:
""" Class for image operations
"""
def __init__(self) -> None:
pass
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 = os.path.abspath(os.path.join(os.path.dirname(os.path.abspath(__file__)), os.pardir)) + \
"/images/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

View File

@@ -0,0 +1,29 @@
import urllib.request, json
from enums.NetworkLocationProvider import NetworkLocationProvider
class Location():
""" Class to handle location requests
"""
def __init__(self):
pass
def get_location(self, provider: NetworkLocationProvider) -> dict:
""" Request the location via network
Returns:
dict: latitude and longitude
"""
request = urllib.request.urlopen(provider.value)
data = json.load(request)
if provider == NetworkLocationProvider.GEOJS:
return {
"latitude": data["latitude"],
"longitude": data["longitude"]
}
elif provider == NetworkLocationProvider.IPAPI:
return {
"latitude": data["lat"],
"longitude": data["lon"]
}

View File

@@ -0,0 +1,146 @@
from math import pi, sin, asin, acos, cos, floor, atan, tan
from datetime import datetime, timezone, time
class Suntimes:
""" Calculates all time periods based on latitude and longitude
Inspired by https://github.com/SatAgro/suntime
author: TobiZog
"""
def __init__(self) -> None:
""" Initialization
Args:
latitude (float): Latitude of the position
longitude (float): Longitude of the position
"""
self.today = datetime.now()
def calc_suntimes(self, latitude: float, longitude: float) -> None:
""" Start the calculation process
Args:
latitude (float): Current latitude
longitude (float): Current longitude
"""
self.latitude = latitude
self.longitude = longitude
self.calc_sun_events()
def to_range(self, value: float, range_max: float) -> float:
""" Converting a variable to a given range
Args:
value (float): The given value
range_max (float): Upper boundary
Returns:
float: Corrected value inside range 0 to range_max
"""
if value < 0:
return value + range_max
elif value >= range_max:
return value - range_max
else:
return value
def calc_sun_events(self):
""" Parent sun event calculater. Calls calc_sunrise_sunset_time() for every time period
"""
civial_dawn_start = self.calc_sunrise_sunset_time(True, 96)
sunrise_start = self.calc_sunrise_sunset_time(True)
morning_start = self.calc_sunrise_sunset_time(True, 89.167)
sunset_start = self.calc_sunrise_sunset_time(False, 89.167)
civial_dusk_start = self.calc_sunrise_sunset_time(False)
night_start = self.calc_sunrise_sunset_time(False, 96)
light_period_duration = (sunset_start - morning_start) / 8
noon_start = morning_start + 3 * light_period_duration
afternoon_start = morning_start + 5 * light_period_duration
evening_start = morning_start + 7 * light_period_duration
self.day_periods = [
time(hour=0, minute=0),
time(civial_dawn_start.hour, civial_dawn_start.minute),
time(sunrise_start.hour, sunrise_start.minute),
time(morning_start.hour, morning_start.minute),
time(noon_start.hour, noon_start.minute),
time(afternoon_start.hour, afternoon_start.minute),
time(evening_start.hour, evening_start.minute),
time(sunset_start.hour, sunset_start.minute),
time(civial_dusk_start.hour, civial_dusk_start.minute),
time(night_start.hour, night_start.minute),
]
def calc_sunrise_sunset_time(self, is_sunrise: bool, zenith=90.833) -> datetime:
""" Calculate all values to estimate the day periods
"""
RAD = pi / 180
# Day of the year
day_of_year = self.today.timetuple().tm_yday
# 2
lng_hour = self.longitude / 15
if is_sunrise:
t = day_of_year + ((6 - lng_hour) / 24)
else:
t = day_of_year + ((18 - lng_hour) / 24)
# 3
M = (0.9856 * t) - 3.289
# 4
L = self.to_range(M + (1.916 * sin(RAD * M)) + (0.020 * sin(RAD * 2 * M)) + 282.634, 360)
# 5
RA = self.to_range((1 / RAD) * atan(0.91764 * tan(RAD * L)), 360)
RA += ((floor(L / 90)) * 90 - (floor(RA / 90)) * 90)
RA /= 15
# 6
sin_dec = 0.39782 * sin(RAD * L)
cos_dec = cos(asin(sin_dec))
# 7a
cos_h = (cos(RAD * zenith) - (sin_dec * sin(RAD * self.latitude))) / (cos_dec * cos(RAD * self.latitude))
# The sun rises or sets never
if cos_h > 1 or cos_h < -1:
return None
# 7b
if is_sunrise:
H = 360 - (1 / RAD) * acos(cos_h)
else: #setting
H = (1 / RAD) * acos(cos_h)
H = H / 15
# 8
T = H + RA - (0.06571 * t) - 6.622
# 9
UT = T - lng_hour
UT = self.to_range(UT, 24) # UTC time in decimal format (e.g. 23.23)
# 10
hr = self.to_range(int(UT), 24)
min = round((UT - int(UT))*60, 0)
if min == 60:
hr += 1
min = 0
res = datetime(self.today.year, self.today.month, self.today.day, hr, int(min))
return res.replace(tzinfo=timezone.utc).astimezone(tz=None)

View File

@@ -0,0 +1,173 @@
import math
class Time_Bar_Chart:
""" Class to handle the creation of the day time period bar
"""
def __init__(self) -> None:
self.image_code = []
self.colors = [
"00193d",
"05597f",
"54babf",
"9ec3a1",
"ffb95e",
"fcae4e",
"f37f73",
"b45bbc",
"7e38ce",
"00285f"
]
self.bar_pos_x = []
def create_bar_chart_with_polylines(self, save_location: str, image_width: int, image_height: int, times: list):
""" Create a time bar chart WITH polylines
Args:
save_location (str): Absolute path to store
image_width (int): Width of the image in pixel
image_height (int): Height of the image in pixel
times (list): List of start times of the periods in minutes since midnight
"""
self.create_bar(image_width, image_height, times)
self.create_polylines(image_width, image_height)
self.create_time_markers(image_width, image_height)
# Write to file
self.image_code.insert(0, '<svg xmlns="http://www.w3.org/2000/svg" width="%s" height="%s">' % (image_width, image_height))
self.image_code.append('</svg>')
file = open(save_location, "w")
for i in self.image_code:
file.write(i + '\n')
self.image_code.clear()
self.bar_pos_x.clear()
def create_bar_chart(self, save_location: str, image_width: int, image_height: int, times: list):
""" Create a time bar chart WITHOUT polylines
Args:
save_location (str): Absolute path to store
image_width (int): Width of the image in pixel
image_height (int): Height of the image in pixel
times (list): List of start times of the periods in minutes since midnight
"""
self.create_bar(image_width, image_height, times)
self.create_time_markers(image_width, image_height)
# Write to file
self.image_code.insert(0, '<svg xmlns="http://www.w3.org/2000/svg" width="%s" height="%s">' % (image_width, image_height))
self.image_code.append('</svg>')
file = open(save_location, "w")
for i in self.image_code:
file.write(i + '\n')
self.image_code.clear()
self.bar_pos_x.clear()
def create_bar(self, image_width: int, image_height: int, times: list):
""" Generates the code for the horizontal multi-color bar chart
Args:
image_width (int): Total width of the image
image_height (int): Total height of the image
times (list): List of start times of the periods, in minutes
"""
x = 0
y = 40
width = 0
height = image_height - 80
if times[len(times) - 1] != 1440:
times.append(1440)
# Adding the bar parts
for i in range(1, len(times)):
width = math.ceil((((100 / 1440) * (times[i] - times[i - 1]) / 100) * image_width))
self.image_code.append(
'<rect fill="#%s" x="%s" y="%s" width="%s" height="%s"/>' % (self.colors[i - 1], x, y, width, height)
)
self.bar_pos_x.append(x)
x += width
def create_time_markers(self, image_width: int, image_height: int):
""" Generates the code for the vertical hour markers
Args:
image_width (int): Total width of the image
image_height (int): Total height of the image
"""
for i in range(0, 8):
# 3 hour vertical line
self.image_code.append(
'<line x1="%s" y1="40" x2="%s" y2="%s" stroke="white" stroke-width="2" />' %
(i * (image_width // 8), i * (image_width // 8), image_height - 40)
)
# The two hours between the 3 hour lines
for j in range(1, 3):
self.image_code.append(
'<line x1="%s" y1="40" x2="%s" y2="%s" stroke="white" stroke-width="0.5" />' %
(i * (image_width // 8) + image_width // 24 * j, i * (image_width // 8) + image_width // 24 * j, image_height - 40)
)
# Time labels
self.image_code.append(
'<text x="%s" y="%s" fill="white" font-size="20" font-family="Liberation Sans">%s</text>' %
(i * (image_width // 8) + 5, image_height - 45, i * 3)
)
def create_polylines(self, image_width: int, image_height: int):
""" Generates the code for the polylines which connect the images with the bar sections
Args:
image_width (int): Total width of the image
image_height (int): Total height of the image
"""
bar_x_start = 0
self.bar_pos_x.append(image_width)
for i in range(0, len(self.bar_pos_x) - 1):
# X-Middle of a bar
bar_mid = bar_x_start + (self.bar_pos_x[i + 1] - bar_x_start) / 2
# Position of the image in the window
image_x = (image_width - 32) / 10 + ((i // 2) % 5) * image_width / 5
# i == 0, 2, 4, ... => Upper Polylines
if (i % 2 == 0):
polyline_y = 0
else:
polyline_y = image_height
if i == 0 or i == 8:
polyline_x = 30
elif i == 2 or i == 6:
polyline_x = 20
elif i == 1 or i == 9:
polyline_x = image_height - 30
elif i == 3 or i == 7:
polyline_x = image_height - 20
elif i == 5:
polyline_x = image_height - 10
else:
polyline_x = 10
self.image_code.append(
'<polyline points="%s,%s %s,%s %s,%s %s,%s" stroke="#%s" fill="none" stroke-width="5" />' %
(image_x, polyline_y, image_x, polyline_x, bar_mid, polyline_x, bar_mid, image_height / 2, self.colors[i])
)
# Store the end point of the bar as start point of the next
bar_x_start = self.bar_pos_x[i + 1]

View File

@@ -0,0 +1,6 @@
#!/usr/bin/python3
import ui.main_window
main = ui.main_window.Main_Window()
main.show()

View File

@@ -0,0 +1,61 @@
import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk
class Dialogs(Gtk.Window):
""" All used Gtk dialogs
Args:
Gtk (Gtk.Window): Window of Gtk application
"""
def __init__(self) -> None:
super().__init__()
def source_folder_dialog(self) -> str:
""" Display a FileChooser dialog where the user choose a folder
Returns:
str: Absolute path to the selected folder
"""
dialog = Gtk.FileChooserDialog(
title= "Please choose a folder with images",
parent=self,
action=Gtk.FileChooserAction.SELECT_FOLDER
)
dialog.add_buttons(
Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL, "Select", Gtk.ResponseType.OK
)
dialog.set_default_size(800, 400)
response = dialog.run()
if response == Gtk.ResponseType.OK:
location = dialog.get_filename()
elif response == Gtk.ResponseType.CANCEL:
location = ""
dialog.destroy()
return location
def message_dialog(self, message: str):
""" Displaying a Gtk Message dialog to the user
Args:
message (str): Message which appear in the dialog
"""
dialog = Gtk.MessageDialog(
transient_for=self,
flags=0,
message_type=Gtk.MessageType.INFO,
buttons=Gtk.ButtonsType.OK,
text=message
)
dialog.run()
dialog.destroy()

View File

@@ -0,0 +1,712 @@
############################################################
# Imports #
############################################################
# GTK
import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk, GdkPixbuf
# Packages
import time, subprocess
from datetime import timedelta
# Local scripts
from model.main_view_model import *
from service.images import *
from service.location import *
from service.suntimes import *
from service.time_bar_chart import *
from ui.dialogs import *
from enums.ImageSourceEnum import *
from enums.NetworkLocationProvider import *
from enums.PeriodSourceEnum import *
class Main_Window:
############################################################
# Lifecycle #
############################################################
def __init__(self) -> None:
""" Initialize all UI components which should be handleable
Args:
builder (Gtk.Builder): Gtk self.builder resource
"""
# View Model
self.view_model = Main_View_Model()
# Glade
self.builder = Gtk.Builder()
self.builder.add_from_file(self.view_model.GLADE_URI)
self.builder.connect_signals(self)
# Objects from scripts
self.images = Images()
self.dialogs = Dialogs()
self.location = Location()
self.suntimes = Suntimes()
self.time_bar_chart = Time_Bar_Chart()
# Page 1: Image Configuration
# Toggle Buttons
self.tb_image_set: Gtk.ToggleButton = self.builder.get_object("tb_image_set")
self.tb_heic_file: Gtk.ToggleButton = self.builder.get_object("tb_heic_file")
self.tb_source_folder: Gtk.ToggleButton = self.builder.get_object("tb_source_folder")
self.img_tb_image_set: Gtk.Image = self.builder.get_object("img_tb_image_set")
self.img_tb_heic_file: Gtk.Image = self.builder.get_object("img_tb_heic_file")
self.img_tb_source_folder: Gtk.Image = self.builder.get_object("img_tb_source_folder")
# Image set
self.lbr_image_set: Gtk.ListBoxRow = self.builder.get_object("lbr_image_set")
self.cb_image_set: Gtk.ComboBox = self.builder.get_object("cb_image_set")
# HEIC file
self.lbr_heic_file: Gtk.ListBoxRow = self.builder.get_object("lbr_heic_file")
# Source folder
self.lbr_source_folder: Gtk.ListBoxRow = self.builder.get_object("lbr_source_folder")
self.btn_source_folder: Gtk.Button = self.builder.get_object("btn_source_folder")
self.lbl_source_folder: Gtk.Label = self.builder.get_object("lbl_source_folder")
# Time bar chart
self.img_bar_images: Gtk.Image = self.builder.get_object("img_bar_images")
self.etr_periods: list[Gtk.Entry] = [
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"),
self.builder.get_object("etr_period_7"), self.builder.get_object("etr_period_8"),
self.builder.get_object("etr_period_9"), self.builder.get_object("etr_period_10"),
]
self.img_periods: list[Gtk.Image] = [
self.builder.get_object("img_period_0"), self.builder.get_object("img_period_1"),
self.builder.get_object("img_period_2"), self.builder.get_object("img_period_3"),
self.builder.get_object("img_period_4"), self.builder.get_object("img_period_5"),
self.builder.get_object("img_period_6"), self.builder.get_object("img_period_7"),
self.builder.get_object("img_period_8"), self.builder.get_object("img_period_9"),
]
self.cb_periods: list[Gtk.ComboBox] = [
self.builder.get_object("cb_period_0"), self.builder.get_object("cb_period_1"),
self.builder.get_object("cb_period_2"), self.builder.get_object("cb_period_3"),
self.builder.get_object("cb_period_4"), self.builder.get_object("cb_period_5"),
self.builder.get_object("cb_period_6"), self.builder.get_object("cb_period_7"),
self.builder.get_object("cb_period_8"), self.builder.get_object("cb_period_9"),
]
#### Page 2: Location & Times
# Toggle Buttons
self.tb_network_location: Gtk.ToggleButton = self.builder.get_object("tb_network_location")
self.tb_custom_location: Gtk.ToggleButton = self.builder.get_object("tb_custom_location")
self.tb_time_periods: Gtk.ToggleButton = self.builder.get_object("tb_time_periods")
self.img_tb_network_location: Gtk.Image = self.builder.get_object("img_tb_network_location")
self.img_tb_custom_location: Gtk.Image = self.builder.get_object("img_tb_custom_location")
self.img_tb_time_periods: Gtk.Image = self.builder.get_object("img_tb_time_periods")
# Network Location
self.lbr_network_refresh_time: Gtk.ListBoxRow = self.builder.get_object("lbr_network_refresh_time")
self.spb_network_refresh_time: Gtk.SpinButton = self.builder.get_object("spb_network_refresh_time")
self.lbr_network_provider: Gtk.ListBoxRow = self.builder.get_object("lbr_network_provider")
self.cb_network_provider: Gtk.ComboBox = self.builder.get_object("cb_network_provider")
self.lbr_current_location: Gtk.ListBoxRow = self.builder.get_object("lbr_current_location")
self.lb_current_location: Gtk.Label = self.builder.get_object("lb_current_location")
# Custom location
self.lbr_custom_location_longitude: Gtk.ListBoxRow = self.builder.get_object("lbr_custom_location_longitude")
self.lbr_custom_location_latitude: Gtk.ListBoxRow = self.builder.get_object("lbr_custom_location_latitude")
self.lbr_time_periods: Gtk.ListBoxRow = self.builder.get_object("lbr_time_periods")
self.etr_longitude: Gtk.Entry = self.builder.get_object("etr_longitude")
self.etr_latitude: Gtk.Entry = self.builder.get_object("etr_latitude")
self.img_bar_times: Gtk.Image = self.builder.get_object("img_bar_times")
self.spb_periods_hour: list[Gtk.SpinButton] = [
self.builder.get_object("spb_period_1_hour"),
self.builder.get_object("spb_period_2_hour"),
self.builder.get_object("spb_period_3_hour"),
self.builder.get_object("spb_period_4_hour"),
self.builder.get_object("spb_period_5_hour"),
self.builder.get_object("spb_period_6_hour"),
self.builder.get_object("spb_period_7_hour"),
self.builder.get_object("spb_period_8_hour"),
self.builder.get_object("spb_period_9_hour"),
]
self.spb_periods_minute: list[Gtk.SpinButton] = [
self.builder.get_object("spb_period_1_minute"),
self.builder.get_object("spb_period_2_minute"),
self.builder.get_object("spb_period_3_minute"),
self.builder.get_object("spb_period_4_minute"),
self.builder.get_object("spb_period_5_minute"),
self.builder.get_object("spb_period_6_minute"),
self.builder.get_object("spb_period_7_minute"),
self.builder.get_object("spb_period_8_minute"),
self.builder.get_object("spb_period_9_minute")
]
self.lb_period_end: list[Gtk.Label] = [
self.builder.get_object("lb_period_0_end"), self.builder.get_object("lb_period_1_end"),
self.builder.get_object("lb_period_2_end"), self.builder.get_object("lb_period_3_end"),
self.builder.get_object("lb_period_4_end"), self.builder.get_object("lb_period_5_end"),
self.builder.get_object("lb_period_6_end"), self.builder.get_object("lb_period_7_end"),
self.builder.get_object("lb_period_8_end"), self.builder.get_object("lb_period_9_end"),
]
# Page 3: Behaviour
self.cb_picture_aspect: Gtk.ComboBox = self.builder.get_object("cb_picture_aspect")
self.sw_dynamic_background_color: Gtk.Switch = self.builder.get_object("sw_dynamic_background_color")
def show(self):
self.builder.get_object("window_main").show_all()
# Smaller UI handling
if self.view_model.screen_height < self.view_model.breakpoint_ui:
self.img_tb_image_set.clear()
self.img_tb_heic_file.clear()
self.img_tb_source_folder.clear()
self.img_tb_network_location.clear()
self.img_tb_custom_location.clear()
self.img_tb_time_periods.clear()
# 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)
# 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)
# Page 3: Behaviour
self.add_items_to_combo_box(self.cb_picture_aspect, self.view_model.picture_aspects)
self.set_active_combobox_item(self.cb_picture_aspect, self.view_model.cinnamon_prefs.picture_aspect)
self.sw_dynamic_background_color.set_active(self.view_model.cinnamon_prefs.dynamic_background_color)
# Show the main window
Gtk.main()
############################################################
# UI Helper #
############################################################
def set_active_combobox_item(self, combobox: Gtk.ComboBox, active_item: str):
""" Change active item in combobox by String value
Args:
combobox (Gtk.ComboBoxText): ComboBox to set active
active_item (str): String item to set active
"""
list_store = combobox.get_model()
for i in range(0, len(list_store)):
row = list_store[i]
if row[0] == active_item:
combobox.set_active(i)
def add_items_to_combo_box(self, combobox: Gtk.ComboBox, items: list):
""" Add items to a combo box
Args:
combobox (Gtk.ComboBox): ComboBox where to add the options
items (list): Possible options
"""
model = combobox.get_model()
store = Gtk.ListStore(str)
for image_set in items:
store.append([image_set])
combobox.set_model(store)
if model == None:
renderer_text = Gtk.CellRendererText()
combobox.pack_start(renderer_text, True)
combobox.add_attribute(renderer_text, "text", 0)
def load_image_options_to_combo_boxes(self, options: list):
""" Add a list of Strings to all image option comboboxes
Args:
options (list): All possible options
"""
options.insert(0, "")
for combobox in self.cb_periods:
self.add_items_to_combo_box(combobox, options)
def load_image_to_preview(self, image_preview: Gtk.Image, image_src: str):
""" Scales the image to a lower resoultion and put them into the time bar chart
Args:
image_preview (Gtk.Image): Gtk Image where it will be displayed
image_src (str): Absolute path to the image
"""
try:
pixbuf = GdkPixbuf.Pixbuf.new_from_file(image_src)
# Scaling the images smaller for screens
if self.view_model.screen_height < self.view_model.breakpoint_ui:
pixbuf = pixbuf.scale_simple(221, 128, GdkPixbuf.InterpType.BILINEAR)
else:
pixbuf = pixbuf.scale_simple(260, 150, GdkPixbuf.InterpType.BILINEAR)
image_preview.set_from_pixbuf(pixbuf)
except:
pass
def refresh_charts(self):
self.view_model.refresh_charts()
# Load to the views
pixbuf = GdkPixbuf.Pixbuf.new_from_file(self.view_model.TIMEBAR_URI_POLYLINES)
self.img_bar_images.set_from_pixbuf(pixbuf)
pixbuf2 = GdkPixbuf.Pixbuf.new_from_file(self.view_model.TIMEBAR_URI)
self.img_bar_times.set_from_pixbuf(pixbuf2)
############################################################
# Callbacks #
############################################################
## Image Configuration
# +-----------+-----------+---------------+
# | Image Set | HEIC file | Source Folder |
# +-----------+-----------+---------------+
def on_toggle_button_image_set_clicked(self, button: Gtk.ToggleButton):
""" Clicked on ToggleButton "Image Set"
Args:
button (Gtk.ToggleButton): Clicked ToggleButton
"""
if button.get_active():
self.view_model.cinnamon_prefs.image_source = ImageSourceEnum.IMAGESET
self.tb_heic_file.set_active(False)
self.tb_source_folder.set_active(False)
self.lbr_image_set.set_visible(True)
self.lbr_heic_file.set_visible(False)
self.lbr_source_folder.set_visible(False)
self.set_active_combobox_item(self.cb_image_set, self.view_model.cinnamon_prefs.selected_image_set)
for i, combobox in enumerate(self.cb_periods):
selected_image_name = self.view_model.cinnamon_prefs.period_images[i]
self.set_active_combobox_item(combobox, selected_image_name)
# Make the comboboxes invisible
for combobox in self.cb_periods:
combobox.set_visible(False)
def on_toggle_button_heic_file_clicked(self, button: Gtk.ToggleButton):
""" Clicked on ToggleButton "Heic file"
Args:
button (Gtk.ToggleButton): Clicked ToggleButton
"""
if button.get_active():
self.view_model.cinnamon_prefs.image_source = ImageSourceEnum.HEICFILE
self.tb_image_set.set_active(False)
self.tb_source_folder.set_active(False)
self.lbr_image_set.set_visible(False)
self.lbr_heic_file.set_visible(True)
self.lbr_source_folder.set_visible(False)
# Make the comboboxes visible
for combobox in self.cb_periods:
combobox.set_visible(True)
# Load images from source folder
files = self.images.get_images_from_folder(self.view_model.cinnamon_prefs.source_folder)
if len(files) != 0:
self.load_image_options_to_combo_boxes(files)
# Load the values for the images from the preferences
for i in range(0, 10):
self.set_active_combobox_item(self.cb_periods[i], self.view_model.cinnamon_prefs.period_images[i])
else:
print("No image files!")
def on_toggle_button_source_folder_clicked(self, button: Gtk.ToggleButton):
""" Clicked on ToggleButton "Source Folder"
Args:
button (Gtk.ToggleButton): Clicked ToggleButton
"""
if button.get_active():
self.view_model.cinnamon_prefs.image_source = ImageSourceEnum.SOURCEFOLDER
self.tb_image_set.set_active(False)
self.tb_heic_file.set_active(False)
self.lbr_image_set.set_visible(False)
self.lbr_heic_file.set_visible(False)
self.lbr_source_folder.set_visible(True)
# Make the comboboxes visible
for combobox in self.cb_periods:
combobox.set_visible(True)
# 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)
if len(files) != 0:
self.load_image_options_to_combo_boxes(files)
# Load the values for the images from the preferences
for i in range(0, 10):
self.set_active_combobox_item(self.cb_periods[i], self.view_model.cinnamon_prefs.period_images[i])
else:
print("No image files!")
# +------------------------------------+
# | Select an image set | aurora ▼ |
# +------------------------------------+
def on_cb_image_set_changed(self, combobox: Gtk.ComboBox):
""" User select on of the included image sets
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:
# Get the selected value
model = combobox.get_model()
selected_image_set = model[tree_iter][0]
# 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
# and so on....
for i in range(0, 10):
self.cb_periods[i].set_active(i + 1)
# +----------------------------------------------+
# | Select a heic file to import | (None) 📄 |
# +----------------------------------------------+
def on_fc_heic_file_file_set(self, fc_button: Gtk.FileChooser):
""" User has a heic file selected with the FileChooserDialog
Args:
fc_button (Gtk.FileChooser): Parameter about the selected file
"""
# The the absolute path to the heic file
file_path: str = fc_button.get_filename()
# Extract the heic file
result = self.images.extract_heic_file(file_path)
# Update the preferences
self.view_model.cinnamon_prefs.selected_image_set = ""
self.view_model.cinnamon_prefs.source_folder = self.view_model.IMAGES_DIR + "/extracted_images/"
# 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)
self.load_image_options_to_combo_boxes(image_names)
else:
self.dialogs.message_dialog("Error during extraction")
# +------------------------------------------------------------+
# | Select a source folder | 📂 Open file selection dialog |
# | /home/developer/Downloads/
# +------------------------------------------------------------+
def on_btn_source_folder_clicked(self, button: Gtk.Button):
""" Button to choose an image source folder was clicked
Args:
button (Gtk.Button): The clicked button
"""
folder = self.dialogs.source_folder_dialog()
files = self.images.get_images_from_folder(folder)
# Update the preferences
self.view_model.cinnamon_prefs.selected_image_set = ""
self.view_model.cinnamon_prefs.source_folder = folder + "/"
# Update the label
self.lbl_source_folder.set_label(folder)
# Update the image comboboxes
self.load_image_options_to_combo_boxes(files)
# Load the values for the images from the preferences
for i in range(0, 10):
self.cb_periods[i].set_active(0)
if len(files) == 1:
self.dialogs.message_dialog("No image files found!")
def on_cb_period_preview_changed(self, combobox: Gtk.ComboBox):
""" User select an image from the ComboBox for the time period
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]
# 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
self.load_image_to_preview(self.img_periods[period_index], image_path)
## Location & Times
def on_toggle_button_network_location_clicked(self, button: Gtk.ToggleButton):
""" User clicks on the ToggleButton for the network location
Args:
button (Gtk.ToggleButton): Clicked ToggleButton
"""
if button.get_active():
self.view_model.cinnamon_prefs.period_source = PeriodSourceEnum.NETWORKLOCATION
self.tb_custom_location.set_active(False)
self.tb_time_periods.set_active(False)
self.lbr_network_refresh_time.set_visible(True)
self.lbr_current_location.set_visible(True)
self.lbr_custom_location_longitude.set_visible(False)
self.lbr_custom_location_latitude.set_visible(False)
self.lbr_time_periods.set_visible(False)
self.spb_network_refresh_time.set_value(self.view_model.cinnamon_prefs.location_refresh_intervals)
# Display the location in the UI
current_location = self.location.get_location(NetworkLocationProvider.GEOJS)
self.lb_current_location.set_text("Latitude: " + current_location["latitude"] + \
", Longitude: " + current_location["longitude"])
# Store the location to the preferences
self.view_model.cinnamon_prefs.latitude_auto = float(current_location["latitude"])
self.view_model.cinnamon_prefs.longitude_auto = float(current_location["longitude"])
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.tb_network_location.set_active(False)
self.tb_time_periods.set_active(False)
self.lbr_network_refresh_time.set_visible(False)
self.lbr_current_location.set_visible(False)
self.lbr_custom_location_longitude.set_visible(True)
self.lbr_custom_location_latitude.set_visible(True)
self.lbr_time_periods.set_visible(False)
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))
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.tb_network_location.set_active(False)
self.tb_custom_location.set_active(False)
self.lbr_network_refresh_time.set_visible(False)
self.lbr_current_location.set_visible(False)
self.lbr_custom_location_longitude.set_visible(False)
self.lbr_custom_location_latitude.set_visible(False)
self.lbr_time_periods.set_visible(True)
for i in range(0, 9):
pref_value = self.view_model.cinnamon_prefs.period_custom_start_time[i + 1]
time_parts = [int(pref_value[0:pref_value.find(":")]), int(pref_value[pref_value.find(":") + 1:])]
self.spb_periods_hour[i].set_value(time_parts[0])
self.spb_periods_minute[i].set_value(time_parts[1])
def on_spb_period_value_changed(self, spin_button: Gtk.SpinButton):
""" Callback if one of the time spinners (minute or hour) will be clicked
(1) (2) (3)
Previous period Current period Next period
12:34 - 14:40 14:41 - 16:20 16:21 - 17:30
^
Variable to change
Args:
spin_button (Gtk.SpinButton): SpinButton which was changed
"""
spin_button_name = Gtk.Buildable.get_name(spin_button)
index = int(spin_button_name[11:12]) - 1
# Determe time string and store to prefs
time_current_start = datetime(2024,1,1, int(self.spb_periods_hour[index].get_value()), int(self.spb_periods_minute[index].get_value()))
time_current_start_str = str(time_current_start.hour).rjust(2, '0') + ":" + str(time_current_start.minute).rjust(2, '0')
self.view_model.cinnamon_prefs.period_custom_start_time[index + 1] = time_current_start_str
time_previous_end = time_current_start - timedelta(minutes=1)
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()
def on_spb_network_location_refresh_time_changed(self, spin_button: Gtk.SpinButton):
""" User changed the refresh time of network location estimation
Args:
spin_button (Gtk.SpinButton): The used SpinButton
"""
self.view_model.cinnamon_prefs.location_refresh_intervals = spin_button.get_value()
def on_etr_longitude_changed(self, entry: Gtk.Entry):
""" User changes the value of the longitude Entry
Args:
entry (Gtk.Entry): The manipulated Entry object
"""
try:
self.view_model.cinnamon_prefs.longitude_custom = float(entry.get_text())
self.refresh_chart()
except:
pass
def on_etr_latitude_changed(self, entry: Gtk.Entry):
""" User changes the value of the latitude Entry
Args:
entry (Gtk.Entry): The manipulated Entry object
"""
try:
self.view_model.cinnamon_prefs.latitude_custom = float(entry.get_text())
self.refresh_chart()
except:
pass
# Behaviour
def on_cb_picture_aspect_changed(self, combobox: Gtk.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]
def on_sw_dynamic_background_color_state_set(self, switch: Gtk.Switch, state):
self.view_model.cinnamon_prefs.dynamic_background_color = state
# About
def on_cinnamon_spices_website_button_clicked(self, _: Gtk.Button):
""" Callback for the button to navigate to the Cinnamon Spices web page of this project
Args:
button (Gtk.Button): Button which was clicked
"""
subprocess.Popen(["xdg-open", "https://cinnamon-spices.linuxmint.com/extensions/view/97"])
def on_github_website_button_clicked(self, _: Gtk.Button):
""" Callback for the button to navigate to the GitHub web page of this project
Args:
button (Gtk.Button): Button which was clicked
"""
subprocess.Popen(["xdg-open", "https://github.com/TobiZog/cinnamon-dynamic-wallpaper"])
def on_create_issue_button_clicked(self, _: Gtk.Button):
""" Callback for the button to navigate to the Issues page on GitHub of this project
Args:
button (Gtk.Button): Button which was clicked
"""
subprocess.Popen(["xdg-open", "https://github.com/TobiZog/cinnamon-dynamic-wallpaper/issues/new"])
def on_ok(self, *args):
""" Callback for the OK button in the top bar
"""
try:
self.on_apply()
except:
pass
# Close the window
self.on_destroy()
def on_apply(self, *args):
""" Callback for the Apply button in the top bar
"""
# Store all values to the JSON file
self.view_model.cinnamon_prefs.store_preferences()
# Use the new settings
# todo loop = Loop()
#loop.exchange_image()
def on_destroy(self, *args):
""" Lifecycle handler when window will be destroyed
"""
Gtk.main_quit()