Commit 27fb8d86 authored by Tom Pillot's avatar Tom Pillot
Browse files

Add a scoreboard with a timer

parent 93058035
import sys
from PyQt5 import QtWidgets as qtw
from PyQt5 import QtCore as qtc
from PyQt5 import QtGui as qtg
from detector import Detector
from team import TeamWindow
from scoreboard import ScoreWindow
class AlignDelegate(qtw.QStyledItemDelegate):
def initStyleOption(self, option, index):
super(AlignDelegate, self).initStyleOption(option, index)
option.displayAlignment = qtc.Qt.AlignCenter
class MainWindow(qtw.QWidget):
def __init__(self):
super().__init__()
self.stacked_layout = qtw.QStackedLayout()
self.team_window = TeamWindow()
self.scoreboard_window = ScoreWindow()
class MainWindow(qtw.QMainWindow):
NAME, TAG = range(2)
self.stacked_layout.addWidget(self.team_window)
self.stacked_layout.addWidget(self.scoreboard_window)
def __init__(self, *args, **kargs):
super().__init__(*args, **kargs)
self.team_window.start_button.clicked.connect(self.start_activity)
self.detector = Detector()
self.tag_ids = {}
self.setWindowTitle("Liste des équipes")
widget = qtw.QWidget(self)
self.setCentralWidget(widget)
self.status_bar = qtw.QStatusBar()
self.setStatusBar(self.status_bar)
self.table_view = qtw.QTableView()
self.table_view.setEditTriggers(qtw.QAbstractItemView.NoEditTriggers)
self.table_view.horizontalHeader().setSectionResizeMode(qtw.QHeaderView.Stretch)
self.table_view.verticalHeader().setVisible(False)
# Align text to the center
delegate = AlignDelegate(self.table_view)
self.table_view.setItemDelegate(delegate)
self.tree_model = qtg.QStandardItemModel(0, 2, self.table_view)
self.tree_model.setHeaderData(self.NAME, qtc.Qt.Horizontal, "Nom")
self.tree_model.setHeaderData(self.TAG, qtc.Qt.Horizontal, "Tag")
self.table_view.setModel(self.tree_model)
self.name_input = qtw.QLineEdit()
self.tag_label = qtw.QLabel("")
self.add_button = qtw.QPushButton("Ajouter")
self.edit_button = qtw.QPushButton("Modifier")
self.delete_button = qtw.QPushButton("Supprimer")
self.scan_button = qtw.QPushButton("Scanner le tag")
self.activity_box = qtw.QComboBox()
self.time_edit = qtw.QTimeEdit()
self.start_button = qtw.QPushButton("Démarrer l'activité")
hlayout1 = qtw.QHBoxLayout()
hlayout1.addWidget(qtw.QLabel("Nom de l'équipe :"))
hlayout1.addWidget(self.name_input)
hlayout2 = qtw.QHBoxLayout()
hlayout2.addWidget(qtw.QLabel("Tag ID :"))
hlayout2.addWidget(self.tag_label)
hlayout3 = qtw.QHBoxLayout()
hlayout3.addLayout(hlayout2)
hlayout3.addWidget(self.scan_button)
hlayout4 = qtw.QHBoxLayout()
hlayout4.addWidget(self.add_button)
hlayout4.addWidget(self.edit_button)
hlayout4.addWidget(self.delete_button)
vlayout1 = qtw.QVBoxLayout()
vlayout1.addWidget(self.table_view)
vlayout1.addLayout(hlayout1)
vlayout1.addLayout(hlayout3)
vlayout1.addLayout(hlayout4)
vertical_spacer = qtw.QSpacerItem(20, 40, qtw.QSizePolicy.Minimum, qtw.QSizePolicy.Expanding)
vlayout2 = qtw.QVBoxLayout()
vlayout2.addWidget(qtw.QLabel("Choix de l'activité :"))
vlayout2.addWidget(self.activity_box)
vlayout2.addItem(vertical_spacer)
vlayout2.addWidget(qtw.QLabel("Choix du temps :"))
vlayout2.addWidget(self.time_edit)
vlayout2.addItem(vertical_spacer)
vlayout2.addWidget(self.start_button)
vlayout2.addStretch()
hlayout5 = qtw.QHBoxLayout()
hlayout5.addLayout(vlayout1)
# Add a vertical line
vline = qtw.QFrame()
vline.setFrameShape(qtw.QFrame.VLine)
vline.setMinimumWidth(10)
vline.setFrameShadow(qtw.QFrame.Sunken)
vline.setLineWidth(3)
hlayout5.addWidget(vline)
hlayout5.addLayout(vlayout2)
widget.setLayout(hlayout5)
self.scan_button.clicked.connect(self.detect_tag)
self.add_button.clicked.connect(self.add_team)
self.edit_button.clicked.connect(self.edit_team)
self.delete_button.clicked.connect(self.delete_team)
self.show()
def detect_tag(self):
tag_id = self.detector.detect_from_cam()
if tag_id:
self.tag_label.setText(str(tag_id))
def add_team(self):
if self.tag_label.text():
tag_id = int(self.tag_label.text())
name = self.name_input.text()
if self.is_valid(name, tag_id):
self.tree_model.insertRow(0)
self.tree_model.setData(self.tree_model.index(0, self.NAME), name)
self.tree_model.setData(self.tree_model.index(0, self.TAG), tag_id)
self.tag_label.setText("")
self.name_input.setText("")
self.tag_ids[name] = tag_id
def edit_team(self):
selected = self.table_view.selectedIndexes()
if selected:
item = selected[0]
name = self.tree_model.data(self.tree_model.index(item.row(), self.NAME))
tag_id = self.tree_model.data(self.tree_model.index(item.row(), self.TAG))
self.tag_label.setText(str(tag_id))
self.name_input.setText(name)
self.tree_model.takeRow(item.row())
del self.tag_ids[name]
def delete_team(self):
selected = self.table_view.selectedIndexes()
if selected:
item = selected[0]
name = self.tree_model.data(self.tree_model.index(item.row(), self.NAME))
self.tree_model.takeRow(item.row())
del self.tag_ids[name]
def is_valid(self, new_name, new_tag_id):
"""
Check that the name and the id are valid and not already used
"""
for name, tag_id in self.tag_ids.items():
if name == new_name:
self.status_bar.showMessage("Une équipe avec ce nom existe déjà.", 5000)
return False
elif tag_id == new_tag_id:
self.status_bar.showMessage("Une équipe avec ce tag existe déjà.", 5000)
return False
return True
def start_activity(self):
self.scoreboard_window.timer_start()
self.stacked_layout.setCurrentIndex(1)
if __name__ == '__main__':
......
import sys
from PyQt5 import QtWidgets as qtw
from PyQt5 import QtCore as qtc
from PyQt5 import QtGui as qtg
DURATION = 30
class ScoreWindow(qtw.QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("Scores")
widget = qtw.QWidget(self)
self.setCentralWidget(widget)
self.list_widget = qtw.QListWidget()
self.timer_label = qtw.QLabel()
self.timer_label.setFont(qtg.QFont("Arial", 80, qtg.QFont.Bold))
self.timer = qtc.QTimer(self)
self.time_left = DURATION
vlayout = qtw.QVBoxLayout()
vlayout.addWidget(self.timer_label, alignment=qtc.Qt.AlignCenter)
vlayout.addWidget(self.list_widget)
widget.setLayout(vlayout)
def timer_start(self):
self.time_left = DURATION
self.timer.timeout.connect(self.timer_timeout)
self.timer.start(1000)
self.update_gui()
def timer_timeout(self):
self.time_left -= 1
if self.time_left == 0:
self.time_left = DURATION
self.update_gui()
def update_gui(self):
self.timer_label.setText(str(self.time_left))
if __name__ == '__main__':
app = qtw.QApplication(sys.argv)
w = ScoreWindow()
sys.exit(app.exec_())
import sys
from PyQt5 import QtWidgets as qtw
from PyQt5 import QtCore as qtc
from PyQt5 import QtGui as qtg
from detector import Detector
class AlignDelegate(qtw.QStyledItemDelegate):
def initStyleOption(self, option, index):
super(AlignDelegate, self).initStyleOption(option, index)
option.displayAlignment = qtc.Qt.AlignCenter
class TeamWindow(qtw.QMainWindow):
NAME, TAG = range(2)
def __init__(self, *args, **kargs):
super().__init__(*args, **kargs)
self.detector = Detector()
self.tag_ids = {}
self.setWindowTitle("Liste des équipes")
widget = qtw.QWidget(self)
self.setCentralWidget(widget)
self.status_bar = qtw.QStatusBar()
self.setStatusBar(self.status_bar)
self.table_view = qtw.QTableView()
self.table_view.setEditTriggers(qtw.QAbstractItemView.NoEditTriggers)
self.table_view.horizontalHeader().setSectionResizeMode(qtw.QHeaderView.Stretch)
self.table_view.verticalHeader().setVisible(False)
# Align text to the center
delegate = AlignDelegate(self.table_view)
self.table_view.setItemDelegate(delegate)
self.tree_model = qtg.QStandardItemModel(0, 2, self.table_view)
self.tree_model.setHeaderData(self.NAME, qtc.Qt.Horizontal, "Nom")
self.tree_model.setHeaderData(self.TAG, qtc.Qt.Horizontal, "Tag")
self.table_view.setModel(self.tree_model)
self.name_input = qtw.QLineEdit()
self.tag_label = qtw.QLabel("")
self.add_button = qtw.QPushButton("Ajouter")
self.edit_button = qtw.QPushButton("Modifier")
self.delete_button = qtw.QPushButton("Supprimer")
self.scan_button = qtw.QPushButton("Scanner le tag")
self.activity_box = qtw.QComboBox()
self.time_edit = qtw.QTimeEdit()
self.start_button = qtw.QPushButton("Démarrer l'activité")
hlayout1 = qtw.QHBoxLayout()
hlayout1.addWidget(qtw.QLabel("Nom de l'équipe :"))
hlayout1.addWidget(self.name_input)
hlayout2 = qtw.QHBoxLayout()
hlayout2.addWidget(qtw.QLabel("Tag ID :"))
hlayout2.addWidget(self.tag_label)
hlayout3 = qtw.QHBoxLayout()
hlayout3.addLayout(hlayout2)
hlayout3.addWidget(self.scan_button)
hlayout4 = qtw.QHBoxLayout()
hlayout4.addWidget(self.add_button)
hlayout4.addWidget(self.edit_button)
hlayout4.addWidget(self.delete_button)
vlayout1 = qtw.QVBoxLayout()
vlayout1.addWidget(self.table_view)
vlayout1.addLayout(hlayout1)
vlayout1.addLayout(hlayout3)
vlayout1.addLayout(hlayout4)
vertical_spacer = qtw.QSpacerItem(20, 40, qtw.QSizePolicy.Minimum, qtw.QSizePolicy.Expanding)
vlayout2 = qtw.QVBoxLayout()
vlayout2.addWidget(qtw.QLabel("Choix de l'activité :"))
vlayout2.addWidget(self.activity_box)
vlayout2.addItem(vertical_spacer)
vlayout2.addWidget(qtw.QLabel("Choix du temps :"))
vlayout2.addWidget(self.time_edit)
vlayout2.addItem(vertical_spacer)
vlayout2.addWidget(self.start_button)
vlayout2.addStretch()
hlayout5 = qtw.QHBoxLayout()
hlayout5.addLayout(vlayout1)
# Add a vertical line
vline = qtw.QFrame()
vline.setFrameShape(qtw.QFrame.VLine)
vline.setMinimumWidth(10)
vline.setFrameShadow(qtw.QFrame.Sunken)
vline.setLineWidth(3)
hlayout5.addWidget(vline)
hlayout5.addLayout(vlayout2)
widget.setLayout(hlayout5)
self.scan_button.clicked.connect(self.detect_tag)
self.add_button.clicked.connect(self.add_team)
self.edit_button.clicked.connect(self.edit_team)
self.delete_button.clicked.connect(self.delete_team)
def detect_tag(self):
tag_id = self.detector.detect_from_cam()
if tag_id:
self.tag_label.setText(str(tag_id))
def add_team(self):
if self.tag_label.text():
tag_id = int(self.tag_label.text())
name = self.name_input.text()
if self.is_valid(name, tag_id):
self.tree_model.insertRow(0)
self.tree_model.setData(self.tree_model.index(0, self.NAME), name)
self.tree_model.setData(self.tree_model.index(0, self.TAG), tag_id)
self.tag_label.setText("")
self.name_input.setText("")
self.tag_ids[name] = tag_id
def edit_team(self):
selected = self.table_view.selectedIndexes()
if selected:
item = selected[0]
name = self.tree_model.data(self.tree_model.index(item.row(), self.NAME))
tag_id = self.tree_model.data(self.tree_model.index(item.row(), self.TAG))
self.tag_label.setText(str(tag_id))
self.name_input.setText(name)
self.tree_model.takeRow(item.row())
del self.tag_ids[name]
def delete_team(self):
selected = self.table_view.selectedIndexes()
if selected:
item = selected[0]
name = self.tree_model.data(self.tree_model.index(item.row(), self.NAME))
self.tree_model.takeRow(item.row())
del self.tag_ids[name]
def is_valid(self, new_name, new_tag_id):
"""
Check that the name and the id are valid and not already used
"""
for name, tag_id in self.tag_ids.items():
if name == new_name:
self.status_bar.showMessage("Une équipe avec ce nom existe déjà.", 5000)
return False
elif tag_id == new_tag_id:
self.status_bar.showMessage("Une équipe avec ce tag existe déjà.", 5000)
return False
return True
if __name__ == '__main__':
app = qtw.QApplication(sys.argv)
w = TeamWindow()
sys.exit(app.exec_())
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