Adding custom times configuration with live bar chart (#5)
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@@ -1,7 +1,8 @@
|
||||
#!/usr/bin/python3
|
||||
|
||||
# Imports
|
||||
import gi, os, subprocess
|
||||
import gi, os, subprocess, time
|
||||
from datetime import timedelta
|
||||
from scripts.time_bar_chart import Time_Bar_Chart
|
||||
from scripts.cinnamon_pref_handler import *
|
||||
from scripts.suntimes import *
|
||||
@@ -39,7 +40,6 @@ class Preferences:
|
||||
self.suntimes = Suntimes(float(self.c_prefs.prefs[PrefenceEnums.LATITUDE_AUTO]),
|
||||
float(self.c_prefs.prefs[PrefenceEnums.LONGITUDE_AUTO]))
|
||||
|
||||
|
||||
########## UI objects ##########
|
||||
|
||||
## Image Configuration
|
||||
@@ -49,7 +49,7 @@ class Preferences:
|
||||
self.lbr_image_set = self.builder.get_object("lbr_image_set")
|
||||
self.lbr_heic_file = self.builder.get_object("lbr_heic_file")
|
||||
self.lbr_source_folder = self.builder.get_object("lbr_source_folder")
|
||||
self.img_bar = self.builder.get_object("img_bar")
|
||||
self.img_bar_images = self.builder.get_object("img_bar_images")
|
||||
self.sw_expand_over_all_displays = self.builder.get_object("sw_expand_over_all_displays")
|
||||
self.sw_show_on_lock_screen = self.builder.get_object("sw_show_on_lock_screen")
|
||||
self.etr_periods = [
|
||||
@@ -73,6 +73,36 @@ class Preferences:
|
||||
self.lbr_time_periods = self.builder.get_object("lbr_time_periods")
|
||||
self.etr_longitude = self.builder.get_object("etr_longitude")
|
||||
self.etr_latitude = self.builder.get_object("etr_latitude")
|
||||
self.img_bar_times = self.builder.get_object("img_bar_times")
|
||||
self.spb_periods_hour = [
|
||||
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 = [
|
||||
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 = [
|
||||
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"),
|
||||
]
|
||||
|
||||
|
||||
def show(self):
|
||||
@@ -101,28 +131,11 @@ class Preferences:
|
||||
elif self.c_prefs.prefs[PrefenceEnums.PERIOD_SOURCE] == PeriodSourceEnum.CUSTOMTIMEPERIODS:
|
||||
self.tb_time_periods.set_active(True)
|
||||
|
||||
|
||||
|
||||
########## Time diagram ##########
|
||||
|
||||
# Stores the start times of the periods in minutes since midnight
|
||||
time_periods_min = []
|
||||
|
||||
# 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 = self.suntimes.get_time_period(i)
|
||||
self.etr_periods[i].set_text(str(time_range[0].hour).rjust(2, '0') + ":" + str(time_range[0].minute).rjust(2, '0') +\
|
||||
" - " + str(time_range[1].hour).rjust(2, '0') + ":" + str(time_range[1].minute).rjust(2, '0'))
|
||||
|
||||
time_periods_min.append(time_range[0].hour * 60 + time_range[0].minute)
|
||||
|
||||
# Create time bar
|
||||
self.time_bar_chart.create_bar_chart(PREFERENCES_URI, 1200, 150, time_periods_min)
|
||||
|
||||
# Load to the view
|
||||
pixbuf = GdkPixbuf.Pixbuf.new_from_file(PREFERENCES_URI + "/time_bar.svg")
|
||||
self.img_bar.set_from_pixbuf(pixbuf)
|
||||
|
||||
# Time diagram
|
||||
try:
|
||||
self.refresh_chart()
|
||||
except:
|
||||
pass
|
||||
|
||||
# Show the main window
|
||||
Gtk.main()
|
||||
@@ -134,8 +147,43 @@ class Preferences:
|
||||
Gtk.main_quit()
|
||||
|
||||
|
||||
#################### Local methods ####################
|
||||
|
||||
|
||||
def refresh_chart(self):
|
||||
# Stores the start times of the periods in minutes since midnight
|
||||
time_periods_min = []
|
||||
|
||||
if self.c_prefs.prefs[PrefenceEnums.PERIOD_SOURCE] == PeriodSourceEnum.CUSTOMTIMEPERIODS:
|
||||
for i in range(0, 10):
|
||||
time_str = self.c_prefs.prefs["period_%s_custom_start_time" % i]
|
||||
|
||||
time_periods_min.append(int(time_str[0:2]) * 60 + int(time_str[3:5]))
|
||||
else:
|
||||
# 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 = self.suntimes.get_time_period(i)
|
||||
self.etr_periods[i].set_text(
|
||||
str(time_range[0].hour).rjust(2, '0') + ":" + str(time_range[0].minute).rjust(2, '0') +\
|
||||
" - " + str(time_range[1].hour).rjust(2, '0') + ":" + str(time_range[1].minute).rjust(2, '0'))
|
||||
|
||||
time_periods_min.append(time_range[0].hour * 60 + time_range[0].minute)
|
||||
|
||||
# Create time bar
|
||||
self.time_bar_chart.create_bar_chart_with_polylines(PREFERENCES_URI, 1200, 150, time_periods_min)
|
||||
self.time_bar_chart.create_bar_chart(PREFERENCES_URI, 1200, 150, time_periods_min)
|
||||
|
||||
# Load to the views
|
||||
pixbuf = GdkPixbuf.Pixbuf.new_from_file(PREFERENCES_URI + "/time_bar_polylines.svg")
|
||||
self.img_bar_images.set_from_pixbuf(pixbuf)
|
||||
|
||||
pixbuf2 = GdkPixbuf.Pixbuf.new_from_file(PREFERENCES_URI + "/time_bar.svg")
|
||||
self.img_bar_times.set_from_pixbuf(pixbuf2)
|
||||
|
||||
|
||||
#################### Callbacks ####################
|
||||
|
||||
|
||||
## Image Configuration
|
||||
|
||||
def on_toggle_button_image_set_clicked(self, button):
|
||||
@@ -201,6 +249,8 @@ class Preferences:
|
||||
self.c_prefs.prefs[PrefenceEnums.LATITUDE_AUTO] = float(location["latitude"])
|
||||
self.c_prefs.prefs[PrefenceEnums.LONGITUDE_AUTO] = float(location["longitude"])
|
||||
|
||||
self.refresh_chart()
|
||||
|
||||
|
||||
def on_toggle_button_custom_location_clicked(self, button):
|
||||
if button.get_active():
|
||||
@@ -231,6 +281,35 @@ class Preferences:
|
||||
self.lbr_time_periods.set_visible(True)
|
||||
|
||||
|
||||
for i in range(0, 9):
|
||||
pref_value = self.c_prefs.prefs["period_%s_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):
|
||||
spin_button_name = Gtk.Buildable.get_name(spin_button)
|
||||
index = int(spin_button_name[11:12]) - 1
|
||||
hours = self.spb_periods_hour[index].get_value()
|
||||
minutes = self.spb_periods_minute[index].get_value()
|
||||
|
||||
time_value = datetime(2024,1,1, int(hours), int(minutes))
|
||||
time_str = str(time_value.hour).rjust(2, '0') + ":" + str(time_value.minute).rjust(2, '0')
|
||||
|
||||
self.c_prefs.prefs["period_%s_custom_start_time" % (index + 1)] = time_str
|
||||
|
||||
next_time = time_value - timedelta(minutes=1)
|
||||
time_str = str(next_time.hour).rjust(2, '0') + ":" + str(next_time.minute).rjust(2, '0')
|
||||
|
||||
# Update the start time of the previous period
|
||||
self.lb_period_end[index].set_text(time_str)
|
||||
|
||||
self.refresh_chart()
|
||||
|
||||
|
||||
def on_spb_network_location_refresh_time_changed(self, spin_button):
|
||||
self.c_prefs.prefs[PrefenceEnums.LOCATION_REFRESH_INTERVALS] = spin_button.get_value()
|
||||
|
||||
|
||||
@@ -20,7 +20,7 @@ class Time_Bar_Chart:
|
||||
self.bar_pos_x = []
|
||||
|
||||
|
||||
def create_bar_chart(self, save_location: str, image_width: int, image_height: int, times: list):
|
||||
def create_bar_chart_with_polylines(self, save_location: str, image_width: int, image_height: int, times: list):
|
||||
""" Create a time bar chart
|
||||
|
||||
Args:
|
||||
@@ -36,10 +36,29 @@ class Time_Bar_Chart:
|
||||
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 + "/time_bar_polylines.svg", "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):
|
||||
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 + "/time_bar.svg", "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
|
||||
@@ -53,6 +72,8 @@ class Time_Bar_Chart:
|
||||
y = 40
|
||||
width = 0
|
||||
height = image_height - 80
|
||||
|
||||
if times[len(times) - 1] != 1440:
|
||||
times.append(1440)
|
||||
|
||||
# Adding the bar parts
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="1200" height="150">
|
||||
<rect fill="#00193d" x="0" y="40" width="52" height="70"/>
|
||||
<rect fill="#05597f" x="52" y="40" width="51" height="70"/>
|
||||
<rect fill="#54babf" x="103" y="40" width="51" height="70"/>
|
||||
<rect fill="#bfe3c2" x="154" y="40" width="51" height="70"/>
|
||||
<rect fill="#ffbf6b" x="205" y="40" width="51" height="70"/>
|
||||
<rect fill="#fdb55c" x="256" y="40" width="51" height="70"/>
|
||||
<rect fill="#f37f73" x="307" y="40" width="51" height="70"/>
|
||||
<rect fill="#b45bbc" x="358" y="40" width="51" height="70"/>
|
||||
<rect fill="#7e38ce" x="409" y="40" width="501" height="70"/>
|
||||
<rect fill="#00285f" x="910" y="40" width="292" height="70"/>
|
||||
<polyline points="116.8,0 116.8,30 26.0,30 26.0,75.0" stroke="#00193d" fill="none" stroke-width="5" />
|
||||
<polyline points="116.8,150 116.8,120 77.5,120 77.5,75.0" stroke="#05597f" fill="none" stroke-width="5" />
|
||||
<polyline points="356.8,0 356.8,20 128.5,20 128.5,75.0" stroke="#54babf" fill="none" stroke-width="5" />
|
||||
<polyline points="356.8,150 356.8,130 179.5,130 179.5,75.0" stroke="#bfe3c2" fill="none" stroke-width="5" />
|
||||
<polyline points="596.8,0 596.8,10 230.5,10 230.5,75.0" stroke="#ffbf6b" fill="none" stroke-width="5" />
|
||||
<polyline points="596.8,150 596.8,140 281.5,140 281.5,75.0" stroke="#fdb55c" fill="none" stroke-width="5" />
|
||||
<polyline points="836.8,0 836.8,20 332.5,20 332.5,75.0" stroke="#f37f73" fill="none" stroke-width="5" />
|
||||
<polyline points="836.8,150 836.8,130 383.5,130 383.5,75.0" stroke="#b45bbc" fill="none" stroke-width="5" />
|
||||
<polyline points="1076.8,0 1076.8,30 659.5,30 659.5,75.0" stroke="#7e38ce" fill="none" stroke-width="5" />
|
||||
<polyline points="1076.8,150 1076.8,120 1055.0,120 1055.0,75.0" stroke="#00285f" fill="none" stroke-width="5" />
|
||||
<line x1="0" y1="40" x2="0" y2="110" stroke="gray" stroke-width="2" />
|
||||
<text x="5" y="105" fill="gray" font-size="20" font-family="Liberation Sans">0</text>
|
||||
<line x1="150" y1="40" x2="150" y2="110" stroke="gray" stroke-width="2" />
|
||||
<text x="155" y="105" fill="gray" font-size="20" font-family="Liberation Sans">3</text>
|
||||
<line x1="300" y1="40" x2="300" y2="110" stroke="gray" stroke-width="2" />
|
||||
<text x="305" y="105" fill="gray" font-size="20" font-family="Liberation Sans">6</text>
|
||||
<line x1="450" y1="40" x2="450" y2="110" stroke="gray" stroke-width="2" />
|
||||
<text x="455" y="105" fill="gray" font-size="20" font-family="Liberation Sans">9</text>
|
||||
<line x1="600" y1="40" x2="600" y2="110" stroke="gray" stroke-width="2" />
|
||||
<text x="605" y="105" fill="gray" font-size="20" font-family="Liberation Sans">12</text>
|
||||
<line x1="750" y1="40" x2="750" y2="110" stroke="gray" stroke-width="2" />
|
||||
<text x="755" y="105" fill="gray" font-size="20" font-family="Liberation Sans">15</text>
|
||||
<line x1="900" y1="40" x2="900" y2="110" stroke="gray" stroke-width="2" />
|
||||
<text x="905" y="105" fill="gray" font-size="20" font-family="Liberation Sans">18</text>
|
||||
<line x1="1050" y1="40" x2="1050" y2="110" stroke="gray" stroke-width="2" />
|
||||
<text x="1055" y="105" fill="gray" font-size="20" font-family="Liberation Sans">21</text>
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 3.0 KiB |
@@ -87,44 +87,44 @@
|
||||
"type": "generic",
|
||||
"default": 0
|
||||
},
|
||||
"period_0_start_time": {
|
||||
"period_0_custom_start_time": {
|
||||
"type": "generic",
|
||||
"default": ""
|
||||
"default": "00:00"
|
||||
},
|
||||
"period_1_start_time": {
|
||||
"period_1_custom_start_time": {
|
||||
"type": "generic",
|
||||
"default": ""
|
||||
"default": "05:00"
|
||||
},
|
||||
"period_2_start_time": {
|
||||
"period_2_custom_start_time": {
|
||||
"type": "generic",
|
||||
"default": ""
|
||||
"default": "07:00"
|
||||
},
|
||||
"period_3_start_time": {
|
||||
"period_3_custom_start_time": {
|
||||
"type": "generic",
|
||||
"default": ""
|
||||
"default": "09:00"
|
||||
},
|
||||
"period_4_start_time": {
|
||||
"period_4_custom_start_time": {
|
||||
"type": "generic",
|
||||
"default": ""
|
||||
"default": "11:00"
|
||||
},
|
||||
"period_5_start_time": {
|
||||
"period_5_custom_start_time": {
|
||||
"type": "generic",
|
||||
"default": ""
|
||||
"default": "13:00"
|
||||
},
|
||||
"period_6_start_time": {
|
||||
"period_6_custom_start_time": {
|
||||
"type": "generic",
|
||||
"default": ""
|
||||
"default": "15:00"
|
||||
},
|
||||
"period_7_start_time": {
|
||||
"period_7_custom_start_time": {
|
||||
"type": "generic",
|
||||
"default": ""
|
||||
"default": "17:00"
|
||||
},
|
||||
"period_8_start_time": {
|
||||
"period_8_custom_start_time": {
|
||||
"type": "generic",
|
||||
"default": ""
|
||||
"default": "19:00"
|
||||
},
|
||||
"period_9_start_time": {
|
||||
"period_9_custom_start_time": {
|
||||
"type": "generic",
|
||||
"default": ""
|
||||
"default": "21:00"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user