Example #1
0
    def _draw_event(self, event: Event):
        """
        The events have already been split to the separate days. The event is for 1 day only.
        """
        day_number = (event.get_start_date(self.config) -
                      self.config.get_date_range()[0]).days
        x = self.__get_event_x(day_number)
        y = self.__get_event_y(event.start_time, event.end_time)
        p1 = (x[0] + style.line_day_width / 2, y[0])
        p2 = (x[1] - style.line_day_width / 2, y[1])
        draw_rounded_rectangle(self.event_draw, [p1, p2],
                               style.event_radius,
                               outline=event.style.event_border,
                               fill=event.style.event_fill,
                               width=style.event_border_width)

        if not self.config.legend and event.name is not None:
            text_size = style.event_name_font.getsize_multiline(event.name)
            title_pos = ((x[0] + x[1]) / 2 - text_size[0] / 2,
                         (y[0] + y[1]) / 2 - text_size[1] / 2)
            self.event_draw.multiline_text(title_pos,
                                           event.name,
                                           align='center',
                                           font=style.event_name_font,
                                           fill=style.event_name_color)
Example #2
0
def validate_event(event: Event, config: CalendarConfig):
    start_date, end_date = config.get_date_range()
    start_time = time(hour=config.get_hours_range()[0])
    end_hour = config.get_hours_range()[1]
    if not (start_date <= event.get_start_date(config) <= end_date):
        logging.warning("Event can't be shown, because it is not in configured date range: {} not in [{}, {}]".format(
            event.get_start_date(config).strftime('%Y-%m-%d'),
            start_date.strftime('%Y-%m-%d'),
            end_date.strftime('%Y-%m-%d')
        ))
    if event.start_time < start_time:
        logging.warning("Event can't be shown, because its start is before time range: {} is before {}".format(
            event.start_time.strftime('%H:%M'),
            start_time.strftime('%H:%M')
        ))
    if end_hour < 24 and time(hour=end_hour) < event.end_time:
        logging.warning("Event can't be shown, because its end is after time range: {} is before {}".format(
            event.end_time.strftime('%H:%M'),
            time(hour=end_hour).strftime('%H:%M')
        ))
Example #3
0
    def add_event(self, event: Event) -> None:
        """
        Skip the empty events with a duration of fewer than 0 seconds.
        Splits events, if needed, to the separate days. The event in the result list has to be for 1 day only.
        Cut the event's time out of the visible time range.
        Validate events.
        """
        if event.get_duration_seconds(self.config) < 1:
            logging.warning(
                f"Skipping event, the duration is too small: {event}")
            return
        end_date: date = event.get_end_date(self.config)
        start_date: date = event.get_start_date(self.config)
        if end_date < self.config.get_date_range()[0]:
            logging.warning(
                f"Skipping event, it ends before the visible range: {event}")
            return
        if start_date > self.config.get_date_range()[1]:
            logging.warning(
                f"Skipping event, it starts after the visible range: {event}")
            return

        if start_date == end_date or ((end_date - start_date).days == 1
                                      and event.end_time == time(0, 0)):
            self.__do_add_event(event)
        else:
            logging.debug(f'Splitting the event: {event}')
            iter_from: date = max(start_date, self.config.get_date_range()[0])
            iter_to: date = min(end_date, self.config.get_date_range()[1])
            for single_date in time_utils.date_range(iter_from, iter_to):
                next_date: date = single_date + timedelta(days=1)
                if single_date == start_date:
                    fr: datetime = datetime.combine(single_date,
                                                    event.start_time)
                    to: datetime = datetime.combine(next_date, time(0, 0))
                elif single_date == end_date:
                    fr: datetime = datetime.combine(single_date, time(0, 0))
                    to: datetime = datetime.combine(single_date,
                                                    event.end_time)
                else:
                    fr: datetime = datetime.combine(single_date, time(0, 0))
                    to: datetime = datetime.combine(next_date, time(0, 0))
                self.__do_add_event(
                    Event(name=event.name, style=event.style, start=fr,
                          end=to))
Example #4
0
 def _get_event_legend_text(self, event: Event) -> str:
     date_text = self._get_day_title(event.get_start_date(self.config))
     time_text = '{:%H:%M} - {:%H:%M}'.format(event.start_time,
                                              event.end_time)
     return '{}, {} - {}'.format(date_text, time_text, event.name)