Commit 7229d008 authored by Tom Pillot's avatar Tom Pillot
Browse files

Improved timer

parent f15f5054
from PyQt5 import QtWidgets as qtw
from PyQt5 import QtCore as qtc
from PyQt5 import QtGui as qtg
from game.scoreboard_ui import ScoreWindowUi from game.scoreboard_ui import ScoreWindowUi
DURATION = 30
class ScoreWindow(ScoreWindowUi): class ScoreWindow(ScoreWindowUi):
def __init__(self): def __init__(self, teams):
super().__init__() super().__init__()
self.time_left = DURATION self.teams = teams
self.time_is_red = False
def timer_start(self): def timer_start(self, time):
self.time_left = DURATION self.time_edit.setTime(time)
self.timer.timeout.connect(self.timer_timeout) self.timer.timeout.connect(self.timer_timeout)
self.timer.start(1000) self.timer.start(1000)
self.update_gui() self.update_gui()
def timer_timeout(self): def timer_timeout(self):
self.time_left -= 1 self.timer.start(1000)
if self.time_left == 0: if self.time_edit.time() == qtc.QTime(0, 0, 0, 0):
self.time_left = DURATION if self.time_is_red:
self.update_gui() self.time_edit.setStyleSheet("color: black")
self.time_is_red = False
else:
self.time_edit.setStyleSheet("color: red")
self.time_is_red = True
else:
self.update_gui()
def update_gui(self): def update_gui(self):
self.timer_label.setText(str(self.time_left)) self.time_edit.setTime(self.time_edit.time().addSecs(-1))
def show_teams(self):
for team in self.teams.values():
self.list_widget.addItem(team.name)
...@@ -12,12 +12,28 @@ class ScoreWindowUi(qtw.QMainWindow): ...@@ -12,12 +12,28 @@ class ScoreWindowUi(qtw.QMainWindow):
self.setCentralWidget(widget) self.setCentralWidget(widget)
self.list_widget = qtw.QListWidget() self.list_widget = qtw.QListWidget()
self.timer_label = qtw.QLabel() self.time_edit = qtw.QTimeEdit()
self.timer_label.setFont(qtg.QFont("Arial", 80, qtg.QFont.Bold)) self.time_edit.setFont(qtg.QFont("Arial", 80, qtg.QFont.Bold))
self.time_edit.setDisplayFormat("mm:ss")
self.time_edit.setAlignment(qtc.Qt.AlignCenter)
self.time_edit.setReadOnly(True)
self.time_edit.setStyleSheet("""
QTimeEdit {
color: white;
selection-color: white;
selection-background-color: black;
background-color: black;
border-style: solid;
border-width: 4px;
border-color: rgb(100,100,100);
}
QTimeEdit::up-button {width: 0}
QTimeEdit::down-button {width: 0}
""")
self.timer = qtc.QTimer(self) self.timer = qtc.QTimer(self)
vlayout = qtw.QVBoxLayout() vlayout = qtw.QVBoxLayout()
vlayout.addWidget(self.timer_label, alignment=qtc.Qt.AlignCenter) vlayout.addWidget(self.time_edit, alignment=qtc.Qt.AlignCenter)
vlayout.addWidget(self.list_widget) vlayout.addWidget(self.list_widget)
widget.setLayout(vlayout) widget.setLayout(vlayout)
class Team:
def __init__(self, name, tag_id):
self.name = name
self.tag_id = tag_id
self.points = 0
def add_points(self, points_added):
self.points += points_added
...@@ -2,6 +2,7 @@ from PyQt5 import QtCore as qtc ...@@ -2,6 +2,7 @@ from PyQt5 import QtCore as qtc
from PyQt5 import QtGui as qtg from PyQt5 import QtGui as qtg
from game.detector import Detector from game.detector import Detector
from game.team_chooser_ui import TeamWindowUi from game.team_chooser_ui import TeamWindowUi
from game.team import Team
class TeamWindow(TeamWindowUi): class TeamWindow(TeamWindowUi):
...@@ -19,7 +20,7 @@ class TeamWindow(TeamWindowUi): ...@@ -19,7 +20,7 @@ class TeamWindow(TeamWindowUi):
super().__init__() super().__init__()
self.detector = Detector() self.detector = Detector()
self.tag_ids = {} self.teams = {} # The keys are the tag ids and the values are the teams
self.scan_button.clicked.connect(self.detect_tag) self.scan_button.clicked.connect(self.detect_tag)
self.add_button.clicked.connect(self.add_team) self.add_button.clicked.connect(self.add_team)
...@@ -41,6 +42,9 @@ class TeamWindow(TeamWindowUi): ...@@ -41,6 +42,9 @@ class TeamWindow(TeamWindowUi):
self.tag_label.setText(str(tag_id)) self.tag_label.setText(str(tag_id))
def add_team(self): def add_team(self):
"""
Add a new team to the table
"""
tag_str = self.tag_label.text() tag_str = self.tag_label.text()
name = self.name_input.text() name = self.name_input.text()
if self.is_valid(name, tag_str): if self.is_valid(name, tag_str):
...@@ -55,9 +59,12 @@ class TeamWindow(TeamWindowUi): ...@@ -55,9 +59,12 @@ class TeamWindow(TeamWindowUi):
self.tag_label.setText("") self.tag_label.setText("")
self.tag_label.setStyleSheet("") self.tag_label.setStyleSheet("")
self.name_input.setText("") self.name_input.setText("")
self.tag_ids[name] = tag_id self.teams[tag_id] = Team(name, tag_id)
def edit_team(self): def edit_team(self):
"""
Edit the selected team
"""
selected = self.table_view.selectedIndexes() selected = self.table_view.selectedIndexes()
if selected: if selected:
item = selected[0] item = selected[0]
...@@ -67,15 +74,18 @@ class TeamWindow(TeamWindowUi): ...@@ -67,15 +74,18 @@ class TeamWindow(TeamWindowUi):
self.set_label_color(tag_id) self.set_label_color(tag_id)
self.name_input.setText(name) self.name_input.setText(name)
self.table_model.takeRow(item.row()) self.table_model.takeRow(item.row())
del self.tag_ids[name] del self.teams[tag_id]
def delete_team(self): def delete_team(self):
"""
Remove the selected team from the table
"""
selected = self.table_view.selectedIndexes() selected = self.table_view.selectedIndexes()
if selected: if selected:
item = selected[0] item = selected[0]
name = self.table_model.data(self.table_model.index(item.row(), self.NAME)) tag_id = self.table_model.data(self.table_model.index(item.row(), self.TAG))
self.table_model.takeRow(item.row()) self.table_model.takeRow(item.row())
del self.tag_ids[name] del self.teams[tag_id]
def is_valid(self, new_name, new_tag_str): def is_valid(self, new_name, new_tag_str):
""" """
...@@ -90,11 +100,21 @@ class TeamWindow(TeamWindowUi): ...@@ -90,11 +100,21 @@ class TeamWindow(TeamWindowUi):
return False return False
new_tag_id = int(new_tag_str) new_tag_id = int(new_tag_str)
for name, tag_id in self.tag_ids.items(): for tag_id, team in self.teams.items():
if name == new_name: if team.name == new_name:
self.status_bar.showMessage("Une équipe avec ce nom existe déjà.", self.STATUS_TIME) self.status_bar.showMessage("Une équipe avec ce nom existe déjà.", self.STATUS_TIME)
return False return False
elif tag_id == new_tag_id: elif tag_id == new_tag_id:
self.status_bar.showMessage("Une équipe avec ce tag existe déjà.", self.STATUS_TIME) self.status_bar.showMessage("Une équipe avec ce tag existe déjà.", self.STATUS_TIME)
return False return False
return True return True
def can_start_activity(self):
"""
Check if the activity can be started
"""
if len(self.teams) > 0:
return True
else:
self.status_bar.showMessage("Veuillez ajouter au moins une équipe.", self.STATUS_TIME)
return False
...@@ -34,16 +34,19 @@ class TeamWindowUi(qtw.QMainWindow): ...@@ -34,16 +34,19 @@ class TeamWindowUi(qtw.QMainWindow):
self.table_view.setModel(self.table_model) self.table_view.setModel(self.table_model)
self.name_input = qtw.QLineEdit() self.name_input = qtw.QLineEdit()
self.tag_label = qtw.QLabel("")
self.tag_label.setAlignment(qtc.Qt.AlignCenter)
self.add_button = qtw.QPushButton("Ajouter") self.add_button = qtw.QPushButton("Ajouter")
self.edit_button = qtw.QPushButton("Modifier") self.edit_button = qtw.QPushButton("Modifier")
self.delete_button = qtw.QPushButton("Supprimer") self.delete_button = qtw.QPushButton("Supprimer")
self.scan_button = qtw.QPushButton("Scanner le tag") self.scan_button = qtw.QPushButton("Scanner le tag")
self.activity_box = qtw.QComboBox() self.activity_box = qtw.QComboBox()
self.time_edit = qtw.QTimeEdit()
self.start_button = qtw.QPushButton("Démarrer l'activité") self.start_button = qtw.QPushButton("Démarrer l'activité")
self.tag_label = qtw.QLabel("")
self.tag_label.setAlignment(qtc.Qt.AlignCenter)
self.time_edit = qtw.QTimeEdit()
self.time_edit.setDisplayFormat("mm'min 'ss's'")
hlayout1 = qtw.QHBoxLayout() hlayout1 = qtw.QHBoxLayout()
hlayout1.addWidget(qtw.QLabel("Nom de l'équipe :")) hlayout1.addWidget(qtw.QLabel("Nom de l'équipe :"))
hlayout1.addWidget(self.name_input) hlayout1.addWidget(self.name_input)
......
...@@ -10,7 +10,7 @@ class MainWindow(qtw.QWidget): ...@@ -10,7 +10,7 @@ class MainWindow(qtw.QWidget):
self.stacked_layout = qtw.QStackedLayout() self.stacked_layout = qtw.QStackedLayout()
self.team_window = TeamWindow() self.team_window = TeamWindow()
self.scoreboard_window = ScoreWindow() self.scoreboard_window = ScoreWindow(self.team_window.teams)
self.stacked_layout.addWidget(self.team_window) self.stacked_layout.addWidget(self.team_window)
self.stacked_layout.addWidget(self.scoreboard_window) self.stacked_layout.addWidget(self.scoreboard_window)
...@@ -18,8 +18,11 @@ class MainWindow(qtw.QWidget): ...@@ -18,8 +18,11 @@ class MainWindow(qtw.QWidget):
self.team_window.start_button.clicked.connect(self.start_activity) self.team_window.start_button.clicked.connect(self.start_activity)
def start_activity(self): def start_activity(self):
self.scoreboard_window.timer_start() if self.team_window.can_start_activity():
self.stacked_layout.setCurrentIndex(1) self.scoreboard_window.show_teams()
time = self.team_window.time_edit.time()
self.scoreboard_window.timer_start(time)
self.stacked_layout.setCurrentIndex(1)
if __name__ == '__main__': if __name__ == '__main__':
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment