clock.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. # -*- coding: utf-8 -*-
  2. # Copyright (c) 2014-2016 Tomek Wójcik <tomek@bthlabs.pl>
  3. #
  4. # Permission is hereby granted, free of charge, to any person obtaining a copy
  5. # of this software and associated documentation files (the "Software"), to deal
  6. # in the Software without restriction, including without limitation the rights
  7. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. # copies of the Software, and to permit persons to whom the Software is
  9. # furnished to do so, subject to the following conditions:
  10. #
  11. # The above copyright notice and this permission notice shall be included in
  12. # all copies or substantial portions of the Software.
  13. #
  14. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. # THE SOFTWARE.
  21. #
  22. """
  23. pie_time.cards.clock
  24. ====================
  25. This module containse the ClockCard class.
  26. """
  27. import datetime
  28. import pygame
  29. from pie_time.card import AbstractCard
  30. class ClockCard(AbstractCard):
  31. """
  32. The clock card.
  33. This card displays a digital clock and date.
  34. **Settings dictionary keys**:
  35. * **time_format** (*string*) - time format string (*strftime()*
  36. compatible). Defaults to :py:attr:`pie_time.cards.ClockCard.TIME_FORMAT`
  37. * **time_blink** (*boolean*) - if set to ``True`` the semicolons will
  38. blink. Defaults to ``True``.
  39. * **time_color** (*tuple*) - time text color. Defaults to
  40. :py:attr:`pie_time.cards.ClockCard.GREEN`
  41. * **date_format** (*string*) - date format string (*strftime()*
  42. compatible). Defaults to :py:attr:`pie_time.cards.ClockCard.DATE_FORMAT`
  43. * **date_color** (*tuple*) - date text color. Defaults to
  44. :py:attr:`pie_time.cards.ClockCard.GREEN`
  45. """
  46. #: Green color for text
  47. GREEN = (96, 253, 108)
  48. #: Default time format
  49. TIME_FORMAT = '%I:%M %p'
  50. #: Default date format
  51. DATE_FORMAT = '%a, %d %b %Y'
  52. def initialize(self):
  53. self._time_font = pygame.font.Font(
  54. self.path_for_resource('PTM55FT.ttf'), 63
  55. )
  56. self._date_font = pygame.font.Font(
  57. self.path_for_resource('opensans-light.ttf'), 36
  58. )
  59. self._now = None
  60. self._current_interval = 0
  61. def _render_time(self, now):
  62. time_format = self._settings.get('time_format', self.TIME_FORMAT)
  63. if self._settings.get('time_blink', True) and now.second % 2 == 1:
  64. time_format = time_format.replace(':', ' ')
  65. current_time = now.strftime(time_format)
  66. text = self._time_font.render(
  67. current_time, True, self._settings.get('time_color', self.GREEN)
  68. )
  69. return text
  70. def _render_date(self, now):
  71. date_format = self._settings.get('date_format', self.DATE_FORMAT)
  72. current_date = now.strftime(date_format)
  73. text = self._date_font.render(
  74. current_date, True, self._settings.get('date_color', self.GREEN)
  75. )
  76. return text
  77. def _update_now(self):
  78. if self._now is None:
  79. self._now = datetime.datetime.now()
  80. self._current_interval = 0
  81. return True
  82. else:
  83. self._current_interval += self._app._clock.get_time()
  84. if self._current_interval >= 1000:
  85. self._now = datetime.datetime.now()
  86. self._current_interval = self._current_interval - 1000
  87. return True
  88. return False
  89. def show(self):
  90. self._now = None
  91. def tick(self):
  92. now_updated = self._update_now()
  93. if now_updated:
  94. time_text = self._render_time(self._now)
  95. date_text = self._render_date(self._now)
  96. time_text_size = time_text.get_size()
  97. date_text_size = date_text.get_size()
  98. time_text_origin_y = (
  99. (self.height - time_text_size[1] - date_text_size[1]) / 2.0
  100. )
  101. time_text_rect = (
  102. (self.width - time_text_size[0]) / 2.0,
  103. time_text_origin_y,
  104. time_text_size[0],
  105. time_text_size[1]
  106. )
  107. date_text_rect = (
  108. (self.width - date_text_size[0]) / 2.0,
  109. time_text_origin_y + time_text_size[1],
  110. date_text_size[0],
  111. date_text_size[1]
  112. )
  113. self.surface.fill(self.background_color)
  114. self.surface.blit(time_text, time_text_rect)
  115. self.surface.blit(date_text, date_text_rect)