Added settings view [WIP]

This commit is contained in:
Admar Schoonen 2023-01-06 22:39:31 +01:00
parent 6287e4210d
commit a407667cb3
3 changed files with 137 additions and 8 deletions

145
clock.py
View File

@ -8,6 +8,7 @@ import traceback
from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.label import Label
from kivy.uix.slider import Slider
from kivy.clock import Clock
from kivy.lang import Builder
from kivy.graphics import Color, Line, Rectangle
@ -17,8 +18,8 @@ from playsound import playsound
Builder.load_string('''
<MyClockWidget>:
on_pos: self.update_clock()
on_size: self.update_clock()
on_pos: self.update_display()
on_size: self.update_display()
FloatLayout
id: face
@ -43,6 +44,61 @@ Builder.load_string('''
size_hint: None, None
pos_hint: {"center_x":0.9, "center_y":0.1}
size: 0.1*min(root.size), 0.1*min(root.size)
FloatLayout
id: settings_button
size_hint: None, None
pos_hint: {"center_x":0.9, "center_y":0.9}
size: 0.1*min(root.size), 0.1*min(root.size)
FloatLayout
id: settings_menu
size_hint: None, None
pos_hint: {"center_x":0.5, "center_y":0.5}
size: 0.7*min(root.size), 0.7*min(root.size)
canvas:
Color:
rgb: 0.1, 0.1, 0.1
Rectangle:
size: self.size
pos: self.pos
GridLayout:
rows: 6
cols: 2
Label:
text:"Volume"
Slider:
id: volume_slider
min: 0
max: 20
value: 4
on_value: root.volume_slider_value(*args)
Label:
text:"Wake up brightness"
Slider:
id: wake_up_bightness_slider
min: 0
max: 20
value: 20
Label:
text:"Reading light brightness"
Slider:
id: reading_light_brightness_slider
min: 0
max: 20
value: 1
Label:
text:"Wifi network"
Label:
text:"wifi name"
Label:
text:"Wifi password"
Label:
text:"wifi password"
Label:
text:"Calendar URL"
Label:
text:"calendar url"
''')
Position = collections.namedtuple('Position', 'x y')
@ -65,6 +121,10 @@ class AlarmSettings():
sound_source = "Woodpecker Chirps - QuickSounds.com.mp3"
seconds_to_sunrise = 30 * 60
volume = 4
wake_up_brightness = 20
reading_light_brightness = 1
class MyClockWidget(FloatLayout):
grabbed = ""
face_numbers = []
@ -78,6 +138,15 @@ class MyClockWidget(FloatLayout):
# view can be "clock" "set_alarm" "settings" or "calendar"
view = "clock"
def hide_widget(self, widget, hide=True):
if hasattr(widget, 'saved_attrs'):
if not hide:
widget.height, widget.size_hint_y, widget.opacity, widget.disabled = widget.saved_attrs
del widget.saved_attrs
elif hide:
widget.saved_attrs = widget.height, widget.size_hint_y, widget.opacity, widget.disabled
widget.height, widget.size_hint_y, widget.opacity, widget.disabled = 0, None, 0, True
def draw_face(self):
"""
Add number labels when added in widget hierarchy
@ -106,6 +175,11 @@ class MyClockWidget(FloatLayout):
))
self.ids["face"].add_widget(self.face_numbers[i - 1])
def draw_settings(self):
Color(1, 1, 1)
# self.ids["settings_menu"].add_widget(Rectangle(pos=(10, 10), size=(500, 500)))
return
def update_face(self):
alarm_settings = App.get_running_app().alarm_settings
@ -125,6 +199,7 @@ class MyClockWidget(FloatLayout):
def on_parent(self, myclock, parent):
self.draw_face()
self.draw_settings()
def position_on_clock(self, fraction, length):
"""
@ -138,8 +213,6 @@ class MyClockWidget(FloatLayout):
)
def update_set_alarm_button(self):
set_alarm_button = self.ids["set_alarm_button"]
app = App.get_running_app()
alarm_settings = app.alarm_settings
@ -156,6 +229,23 @@ class MyClockWidget(FloatLayout):
Color(rgb[0], rgb[1], rgb[2])
Rectangle(size=set_alarm_button.size, pos=set_alarm_button.pos, source=source)
def update_settings_button(self):
app = App.get_running_app()
alarm_settings = app.alarm_settings
if (self.view == "settings"):
source = 'settings_visible.png'
rgb = [0.9, 0.0, 0.0]
else:
source = 'settings_not_visible.png'
rgb = [1.0, 1.0, 1.0]
settings_button = self.ids["settings_button"]
settings_button.canvas.clear()
with settings_button.canvas:
Color(rgb[0], rgb[1], rgb[2])
Rectangle(size=settings_button.size, pos=settings_button.pos, source=source)
def sun_rise(self):
alarm_settings = App.get_running_app().alarm_settings
@ -211,7 +301,9 @@ class MyClockWidget(FloatLayout):
self.check_sun_rise()
self.check_play_sound()
def update_clock(self, *args):
def update_clock(self):
self.hide_widget(self.ids["face"], False)
self.hide_widget(self.ids["hands"], False)
self.check_alarm()
"""
Redraw clock hands
@ -228,7 +320,6 @@ class MyClockWidget(FloatLayout):
hours_hand = self.position_on_clock(time.hour/12 + time.minute/720, length=0.35*hands.size[0])
self.update_face()
self.update_set_alarm_button()
hands.canvas.clear()
with hands.canvas:
@ -258,6 +349,27 @@ class MyClockWidget(FloatLayout):
Color(0.7, 0.7, 0.7)
Line(points=[hands.center_x, hands.center_y, seconds_hand.x, seconds_hand.y], width=1, cap="round")
def update_settings(self):
self.hide_widget(self.ids["settings_menu"], False)
def update_display(self, *args):
# Hide all dynamic widgets; will be enabled when updating respecive view
self.hide_widget(self.ids["face"], True)
self.hide_widget(self.ids["hands"], True)
self.hide_widget(self.ids["settings_menu"], True)
self.update_set_alarm_button()
self.update_settings_button()
if self.view == "clock" or self.view == "set_alarm":
self.update_clock()
elif self.view == "settings":
self.update_settings()
def volume_slider_value(self, *args):
alarm_settings = App.get_running_app().alarm_settings
alarm_settings.volume = int(args[1])
print(str(int(args[1])))
def on_alarm_button_pressed(self):
alarm_settings = App.get_running_app().alarm_settings
alarm_settings.alarm_modified = False
@ -270,6 +382,14 @@ class MyClockWidget(FloatLayout):
self.view = "set_alarm"
alarm_settings.alarm_activated = True
def on_settings_button_pressed(self):
print("settings button pressed from view " + self.view)
if self.view != "settings":
self.view = "settings"
else:
self.view = "clock"
print("view updated to " + self.view)
def on_touch_up(self, touch):
self.grabbed = ""
alarm_settings = App.get_running_app().alarm_settings
@ -277,6 +397,8 @@ class MyClockWidget(FloatLayout):
if (self.view == "set_alarm") and (self.grabbed == "hour" or self.grabbed == "minute"):
self.set_alarm_timeout_counter = 0
super(MyClockWidget, self).on_touch_up(touch)
def on_touch_move(self, touch):
alarm_settings = App.get_running_app().alarm_settings
self.alarm_set_timeout = 0
@ -334,6 +456,8 @@ class MyClockWidget(FloatLayout):
alarm_settings.alarm_time.month, alarm_settings.alarm_time.day, \
hour, alarm_settings.alarm_time.minute, alarm_settings.alarm_time.second, 0)
super(MyClockWidget, self).on_touch_move(touch)
def on_touch_down(self, touch):
global sound_process
@ -349,6 +473,8 @@ class MyClockWidget(FloatLayout):
if (0.85 <= touch.spos[0] <= 0.95) and (0.05 <= touch.spos[1] <= 0.15):
self.on_alarm_button_pressed()
elif (0.85 <= touch.spos[0] <= 0.95) and (0.85 <= touch.spos[1] <= 0.95):
self.on_settings_button_pressed()
elif sound_process is not None:
kill_sound_process()
elif self.view == "set_alarm":
@ -360,15 +486,18 @@ class MyClockWidget(FloatLayout):
(hours_hand.y - 0.1 * self.size[1] <= touch.pos[1] <= hours_hand.y + 0.1 * self.size[1]):
self.grabbed = "hour"
super(MyClockWidget, self).on_touch_down(touch)
class MyApp(App):
alarm_settings = AlarmSettings()
def build(self):
clock_widget = MyClockWidget()
# update initially, just after construction of the widget is complete
Clock.schedule_once(clock_widget.update_clock, 0)
Clock.schedule_once(clock_widget.update_display, 0)
# then update 60 times per second
Clock.schedule_interval(clock_widget.update_clock, 1.0/60.0)
Clock.schedule_interval(clock_widget.update_display, 1.0/60.0)
return clock_widget
def kill_sound_process():

BIN
settings_not_visible.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 24 KiB

BIN
settings_visible.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 29 KiB