Commit 77b07e55 authored by Tom Pillot's avatar Tom Pillot
Browse files

Add team colors

parent 27fb8d86
.idea/ .idea/
__pycache__/ __pycache__/
venv/ venv/
apriltags.xcf
No preview for this file type
...@@ -4,6 +4,14 @@ from PyQt5 import QtCore as qtc ...@@ -4,6 +4,14 @@ from PyQt5 import QtCore as qtc
from PyQt5 import QtGui as qtg from PyQt5 import QtGui as qtg
from detector import Detector from detector import Detector
team_colors = {
1: "#FE3200", 2: "#FE6500", 3: "#FE9800", 4: "#FECB00", 5: "#FEFE00", 6: "#CBFE00",
24: "#98FE00", 25: "#65FE00", 26: "#32FE00", 27: "#00FE00", 28: "00FE32", 29: "#00FE65", 30: "#00FE98",
48: "#00FECB", 49: "#00FEFE", 50: "#00CBFE", 51: "#0098FE", 52: "#0065FE", 53: "#0032FE", 54: "#0000FE",
72: "#3200FE", 73: "#6500FE", 74: "#A100FE", 75: "#D400FE", 76: "#FE00F6", 77: "#FF00C3", 78: "#FE008F",
96: "#FE005D", 97: "#FE002A", 98: "#474747", 99: "#8C8C8C", 100: "#643030", 101: "#9A6565", 102: "#BC9F9F"
}
class AlignDelegate(qtw.QStyledItemDelegate): class AlignDelegate(qtw.QStyledItemDelegate):
def initStyleOption(self, option, index): def initStyleOption(self, option, index):
...@@ -13,6 +21,7 @@ class AlignDelegate(qtw.QStyledItemDelegate): ...@@ -13,6 +21,7 @@ class AlignDelegate(qtw.QStyledItemDelegate):
class TeamWindow(qtw.QMainWindow): class TeamWindow(qtw.QMainWindow):
NAME, TAG = range(2) NAME, TAG = range(2)
STATUS_TIME = 5000
def __init__(self, *args, **kargs): def __init__(self, *args, **kargs):
super().__init__(*args, **kargs) super().__init__(*args, **kargs)
...@@ -35,13 +44,14 @@ class TeamWindow(qtw.QMainWindow): ...@@ -35,13 +44,14 @@ class TeamWindow(qtw.QMainWindow):
delegate = AlignDelegate(self.table_view) delegate = AlignDelegate(self.table_view)
self.table_view.setItemDelegate(delegate) self.table_view.setItemDelegate(delegate)
self.tree_model = qtg.QStandardItemModel(0, 2, self.table_view) self.table_model = qtg.QStandardItemModel(0, 2, self.table_view)
self.tree_model.setHeaderData(self.NAME, qtc.Qt.Horizontal, "Nom") self.table_model.setHeaderData(self.NAME, qtc.Qt.Horizontal, "Nom")
self.tree_model.setHeaderData(self.TAG, qtc.Qt.Horizontal, "Tag") self.table_model.setHeaderData(self.TAG, qtc.Qt.Horizontal, "Tag")
self.table_view.setModel(self.tree_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 = 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")
...@@ -104,52 +114,76 @@ class TeamWindow(qtw.QMainWindow): ...@@ -104,52 +114,76 @@ class TeamWindow(qtw.QMainWindow):
self.edit_button.clicked.connect(self.edit_team) self.edit_button.clicked.connect(self.edit_team)
self.delete_button.clicked.connect(self.delete_team) self.delete_button.clicked.connect(self.delete_team)
def set_label_color(self, tag_id):
try:
color_hex = team_colors[tag_id]
self.tag_label.setStyleSheet(f"color: black; background-color: {color_hex}")
return True
except KeyError:
self.status_bar.showMessage("Le tag n'a pas été reconnu.", self.STATUS_TIME)
return False
def detect_tag(self): def detect_tag(self):
tag_id = self.detector.detect_from_cam() tag_id = self.detector.detect_from_cam()
if tag_id: if tag_id and self.set_label_color(tag_id):
self.tag_label.setText(str(tag_id)) self.tag_label.setText(str(tag_id))
def add_team(self): def add_team(self):
if self.tag_label.text(): tag_str = self.tag_label.text()
tag_id = int(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_id): tag_id = int(tag_str)
self.tree_model.insertRow(0) # Add the new team to the table
self.tree_model.setData(self.tree_model.index(0, self.NAME), name) self.table_model.insertRow(0)
self.tree_model.setData(self.tree_model.index(0, self.TAG), tag_id) self.table_model.setData(self.table_model.index(0, self.NAME), name)
self.tag_label.setText("") self.table_model.setData(self.table_model.index(0, self.TAG), tag_id)
self.name_input.setText("") self.table_model.setData(self.table_model.index(0, self.TAG),
self.tag_ids[name] = tag_id qtg.QBrush(qtg.QColor(team_colors[tag_id])), qtc.Qt.BackgroundRole)
# Delete the last input
self.tag_label.setText("")
self.tag_label.setStyleSheet("")
self.name_input.setText("")
self.tag_ids[name] = tag_id
def edit_team(self): def edit_team(self):
selected = self.table_view.selectedIndexes() selected = self.table_view.selectedIndexes()
if selected: if selected:
item = selected[0] item = selected[0]
name = self.tree_model.data(self.tree_model.index(item.row(), self.NAME)) name = self.table_model.data(self.table_model.index(item.row(), self.NAME))
tag_id = self.tree_model.data(self.tree_model.index(item.row(), self.TAG)) tag_id = self.table_model.data(self.table_model.index(item.row(), self.TAG))
self.tag_label.setText(str(tag_id)) self.tag_label.setText(str(tag_id))
self.set_label_color(tag_id)
self.name_input.setText(name) self.name_input.setText(name)
self.tree_model.takeRow(item.row()) self.table_model.takeRow(item.row())
del self.tag_ids[name] del self.tag_ids[name]
def delete_team(self): def delete_team(self):
selected = self.table_view.selectedIndexes() selected = self.table_view.selectedIndexes()
if selected: if selected:
item = selected[0] item = selected[0]
name = self.tree_model.data(self.tree_model.index(item.row(), self.NAME)) name = self.table_model.data(self.table_model.index(item.row(), self.NAME))
self.tree_model.takeRow(item.row()) self.table_model.takeRow(item.row())
del self.tag_ids[name] del self.tag_ids[name]
def is_valid(self, new_name, new_tag_id): def is_valid(self, new_name, new_tag_str):
""" """
Check that the name and the id are valid and not already used Check that the name and the id are valid and not already used
""" """
if not new_name: # Empty name
self.status_bar.showMessage("Veuillez entrer un nom d'équipe.", self.STATUS_TIME)
return False
if not new_tag_str: # Empty tag
self.status_bar.showMessage("Veuillez scanner le tag du Thymio.", self.STATUS_TIME)
return False
new_tag_id = int(new_tag_str)
for name, tag_id in self.tag_ids.items(): for name, tag_id in self.tag_ids.items():
if name == new_name: if name == new_name:
self.status_bar.showMessage("Une équipe avec ce nom existe déjà.", 5000) 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à.", 5000) self.status_bar.showMessage("Une équipe avec ce tag existe déjà.", self.STATUS_TIME)
return False return False
return True return True
......
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