Skip to content

Commit

Permalink
fix and simplify sun rotation
Browse files Browse the repository at this point in the history
  • Loading branch information
Amit Chaudhary committed Jun 28, 2024
1 parent eb5e40a commit f799992
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 36 deletions.
14 changes: 7 additions & 7 deletions po/mousam.pot
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2024-06-27 08:50+0530\n"
"POT-Creation-Date: 2024-06-28 09:06+0530\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
Expand Down Expand Up @@ -250,15 +250,15 @@ msgstr ""
msgid "United States AQI standard"
msgstr ""

#: src/frontendCardDayNight.py:78
#: src/frontendCardDayNight.py:66
msgid "Sunrise & Sunset"
msgstr ""

#: src/frontendCardDayNight.py:89
#: src/frontendCardDayNight.py:77
msgid "Sunrise"
msgstr ""

#: src/frontendCardDayNight.py:102
#: src/frontendCardDayNight.py:90
msgid "Sunset"
msgstr ""

Expand Down Expand Up @@ -378,12 +378,12 @@ msgstr ""
msgid "Now"
msgstr ""

#: src/frontendUiDrawDayNight.py:105
#: src/frontendUiDrawDayNight.py:98
msgid "Horizon"
msgstr ""

#: src/frontendUiDrawDayNight.py:115
msgid "Midnight"
#: src/frontendUiDrawDayNight.py:108
msgid "Night"
msgstr ""

#: src/mousam.py:71
Expand Down
42 changes: 25 additions & 17 deletions src/frontendCardDayNight.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from datetime import datetime
import gi

gi.require_version("Gtk", "4.0")
gi.require_version("Adw", "1")
from gi.repository import Gtk
Expand Down Expand Up @@ -31,16 +32,13 @@ def get_sunset_sunrise_degree(self):

sunrise_ts, sunset_ts = 0, 0
for i, data in enumerate(daily_data.time.get("data")):
date_ = int(
datetime.fromtimestamp(
data + time_diff
).strftime(r"%d")
)
date_ = int(datetime.fromtimestamp(data + time_diff).strftime(r"%d"))
if date_ == datetime.today().date().day:
sunrise_ts = daily_data.sunrise.get("data")[i]
sunset_ts = daily_data.sunset.get("data")[i]
break

target_dt = datetime.fromtimestamp(target_time)
sunrise_dt = datetime.fromtimestamp(sunrise_ts - time_diff)
sunset_dt = datetime.fromtimestamp(sunset_ts - time_diff)

Expand All @@ -51,18 +49,8 @@ def get_sunset_sunrise_degree(self):
sunrise = sunrise_dt.strftime("%H:%M")
sunset = sunset_dt.strftime("%H:%M")

# Caclulate Sun rotation
degree = 0
# For Day
if target_time < (sunset_ts - time_diff):
degree = (target_time - (sunrise_ts - time_diff)) * 180 / (sunset_ts - sunrise_ts)
degree = degree + 180

# For Night
else:
degree = (target_time - (sunset_ts-time_diff))*180/(86400-(sunset_ts-sunrise_ts))

return sunrise, sunset, degree
angle = self._calculate_sun_rotation(target_dt,sunrise_dt,sunset_dt)
return sunrise, sunset, angle

def create_card(self):
card = Gtk.Grid(margin_top=10, margin_start=5, margin_bottom=0)
Expand Down Expand Up @@ -117,3 +105,23 @@ def create_card(self):

obj = DrawDayNight(self.degree, 200, 100)
card_icon.attach(obj.img_box, 0, 1, 1, 1)

# Sun Rotation
def _calculate_sun_rotation(self,target_dt,sunrise_dt,sunset_dt):
angle = 0
target_ctime_hr = target_dt.hour + (target_dt.minute / 60)
target_sunrise_hr = sunrise_dt.hour + (sunrise_dt.minute / 60)
target_sunset_hr = sunset_dt.hour + (sunset_dt.minute / 60)

# day
if target_ctime_hr > target_sunrise_hr and target_ctime_hr < target_sunset_hr:
angle = (target_ctime_hr-target_sunrise_hr)*180/(target_sunset_hr-target_sunrise_hr)
angle += 180 # Sun is above the horizon

# Night
else:
if target_ctime_hr < target_sunrise_hr:
target_ctime_hr += 24 # Adjust for times after midnight
angle = (target_ctime_hr - target_sunset_hr)*180/(24-(target_sunset_hr-target_sunrise_hr))

return angle
16 changes: 4 additions & 12 deletions src/frontendUiDrawDayNight.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ def __init__(self, angle, width, height):
self.drawing_area = Gtk.DrawingArea()
self.drawing_area.set_size_request(self.width + 20, self.height + 20)
self.drawing_area.set_css_classes(["drawing-padding"])
# self.drawing_area.connect("draw", self.on_draw)
self.drawing_area.set_draw_func(self.on_draw, None)

self.img_box = Gtk.Box()
Expand Down Expand Up @@ -64,13 +63,6 @@ def on_draw(self, widget, cr, width, height, data):
) # Full circle (0 to 2π)
context.stroke()

# Midnight Mark
context.set_dash([1, 0])
context.set_source_rgba(0.5, 0.5, 0.5, 0.7)
context.move_to(center_x, center_y + outer_radius / 1.2)
context.line_to(center_x, center_y + outer_radius)
context.stroke()

# Clock
context.select_font_face(
"Sans", cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_NORMAL
Expand Down Expand Up @@ -112,9 +104,9 @@ def on_draw(self, widget, cr, width, height, data):
# Display the text along the circular path
context.show_text(text)

text2 = _("Midnight")
text2 = _("Night")
# Calculate the position for text placement
text_x = center_x - 30
text_x = center_x - 20
text_y = center_y + outer_radius * 1.3
# Move the text cursor to the calculated position
context.move_to(text_x, text_y)
Expand All @@ -129,9 +121,9 @@ def on_draw(self, widget, cr, width, height, data):
upper_limit = abs(1.2 - (1 - (360 - 170) / 90))
lower_limit = abs(1.2 - (1 - (180 - 170) / 90))
yellow = upper_limit - yellow + lower_limit
context.set_source_rgba(1, yellow, 0, 1.5) # Red
context.set_source_rgba(1, yellow, 0, 1.5) # color = function of y (height of sun)
else:
context.set_source_rgba(0.9, 0.9, 0.9, 1.0) # Red
context.set_source_rgba(0.9, 0.9, 0.9, 1.0) # Whitish(for moon)

# Convert to radian
sun_angle_rad = math.radians(
Expand Down

0 comments on commit f799992

Please sign in to comment.