-
-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathtimer_input.py
More file actions
203 lines (175 loc) · 7.64 KB
/
timer_input.py
File metadata and controls
203 lines (175 loc) · 7.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#############################################################################
#
# OnAirScreen
# Copyright (c) 2012-2026 Sascha Ludwig, astrastudio.de
# All rights reserved.
#
# timer_input.py
# This file is part of OnAirScreen
#
# You may use this file under the terms of the BSD license as follows:
#
# "Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in
# the documentation and/or other materials provided with the
# distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
#
#############################################################################
"""
Timer Input Dialog for OnAirScreen
This module provides a dialog for entering timer values in various formats.
"""
import re
import logging
from typing import Callable, Optional
from PyQt6.QtWidgets import QDialog, QLineEdit, QVBoxLayout, QLabel
from PyQt6.QtCore import pyqtSignal, QObject
from exceptions import WidgetAccessError, ValueValidationError, log_exception
logger = logging.getLogger(__name__)
class TimerInputDialog(QDialog):
"""
Dialog for entering timer values
Supports formats:
- "2,10" or "2.10" for 2 minutes 10 seconds
- "30" for 30 seconds only
"""
timer_set = pyqtSignal(int) # Emitted with total seconds when timer is set
def __init__(self, parent=None):
"""
Initialize the timer input dialog
Args:
parent: Parent widget
"""
super().__init__(parent)
self.resize(200, 100)
self.setWindowTitle("Please enter timer")
self.timeEdit = QLineEdit("Enter timer here")
self.timeEdit.selectAll()
self.infoLabel = QLabel("Examples:\nenter 2,10 for 2:10 minutes\nenter 30 for 30 seconds")
layout = QVBoxLayout()
layout.addWidget(self.infoLabel)
layout.addWidget(self.timeEdit)
self.setLayout(layout)
self.timeEdit.setFocus()
self.timeEdit.returnPressed.connect(self._parse_and_emit)
def _parse_and_emit(self) -> None:
"""Parse input and emit timer_set signal"""
try:
total_seconds = self._parse_timer_input()
if total_seconds is not None:
self.timer_set.emit(total_seconds)
self.hide()
except Exception as e:
from exceptions import OnAirScreenError, WidgetError
if isinstance(e, OnAirScreenError):
log_exception(logger, e)
else:
error = WidgetError(f"Error parsing timer input: {e}")
log_exception(logger, error)
def _parse_timer_input(self) -> Optional[int]:
"""
Parse timer input from dialog and return total seconds
Handles formats:
- "2,10" or "2.10" for 2 minutes 10 seconds
- "30" for 30 seconds only
Returns:
Total seconds, or None if input is invalid
"""
# Get and validate input text
try:
text = str(self.timeEdit.text()).strip()
except (AttributeError, RuntimeError) as e:
error = WidgetAccessError(
f"Error getting timer input text: {e}",
widget_name="timeEdit",
attribute="text"
)
log_exception(logger, error, use_exc_info=False)
return None
# Validate input is not empty
if not text or text == "Enter timer here":
logger.warning("parse_timer_input: Empty or default input, ignoring")
return None
minutes = 0
seconds = 0
parsed = False
# Try comma format: "2,10" for 2 minutes 10 seconds
if re.match('^[0-9]+,[0-9]+$', text):
try:
parts = text.split(",")
if len(parts) == 2:
minutes = int(parts[0])
seconds = int(parts[1])
# Validate seconds are in valid range (0-59)
if seconds < 0 or seconds >= 60:
logger.warning(f"parse_timer_input: Invalid seconds value {seconds}, must be 0-59")
return None
parsed = True
except (ValueError, IndexError) as e:
error = ValueValidationError(
f"parse_timer_input: Error parsing comma format '{text}': {e}",
value=text
)
log_exception(logger, error, use_exc_info=False)
return None
# Try dot format: "2.10" for 2 minutes 10 seconds
elif re.match(r'^[0-9]+\.[0-9]+$', text):
try:
parts = text.split(".")
if len(parts) == 2:
minutes = int(parts[0])
seconds = int(parts[1])
# Validate seconds are in valid range (0-59)
if seconds < 0 or seconds >= 60:
logger.warning(f"parse_timer_input: Invalid seconds value {seconds}, must be 0-59")
return None
parsed = True
except (ValueError, IndexError) as e:
error = ValueValidationError(
f"parse_timer_input: Error parsing dot format '{text}': {e}",
value=text
)
log_exception(logger, error, use_exc_info=False)
return None
# Try seconds-only format: "30" for 30 seconds
elif re.match('^[0-9]+$', text):
try:
seconds = int(text)
if seconds < 0:
logger.warning(f"parse_timer_input: Negative seconds value {seconds}")
return None
parsed = True
except ValueError as e:
logger.error(f"parse_timer_input: Error parsing seconds format '{text}': {e}")
return None
# If no format matched, show error
if not parsed:
logger.warning(f"parse_timer_input: Invalid input format '{text}'. Expected: '2,10', '2.10', or '30'")
return None
# Calculate total seconds
total_seconds = (minutes * 60) + seconds
# Validate total is reasonable (max 24 hours = 86400 seconds)
if total_seconds > 86400:
logger.warning(f"parse_timer_input: Timer value too large: {total_seconds} seconds (max 86400)")
return None
logger.info(f"parse_timer_input: Parsed timer to {minutes}:{seconds:02d} ({total_seconds} seconds)")
return total_seconds