team_chooser.py 4.13 KB
Newer Older
Tom Pillot's avatar
Tom Pillot committed
1
2
from PyQt5 import QtCore as qtc
from PyQt5 import QtGui as qtg
3
4
from game.detector import Detector
from game.team_chooser_ui import TeamWindowUi
Tom Pillot's avatar
Tom Pillot committed
5

Tom Pillot's avatar
Tom Pillot committed
6

Tom Pillot's avatar
Tom Pillot committed
7
class TeamWindow(TeamWindowUi):
Tom Pillot's avatar
Tom Pillot committed
8
    NAME, TAG = range(2)
Tom Pillot's avatar
Tom Pillot committed
9
    STATUS_TIME = 5000
10
11
12
13
14
15
16
    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"
    }
Tom Pillot's avatar
Tom Pillot committed
17

Tom Pillot's avatar
Tom Pillot committed
18
19
    def __init__(self):
        super().__init__()
Tom Pillot's avatar
Tom Pillot committed
20
21
22
23
24
25
26
27
28

        self.detector = Detector()
        self.tag_ids = {}

        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)

Tom Pillot's avatar
Tom Pillot committed
29
30
    def set_label_color(self, tag_id):
        try:
31
            color_hex = self.team_colors[tag_id]
Tom Pillot's avatar
Tom Pillot committed
32
33
34
35
36
37
            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

Tom Pillot's avatar
Tom Pillot committed
38
39
    def detect_tag(self):
        tag_id = self.detector.detect_from_cam()
Tom Pillot's avatar
Tom Pillot committed
40
        if tag_id and self.set_label_color(tag_id):
Tom Pillot's avatar
Tom Pillot committed
41
42
43
            self.tag_label.setText(str(tag_id))

    def add_team(self):
Tom Pillot's avatar
Tom Pillot committed
44
45
46
47
48
49
50
51
52
        tag_str = self.tag_label.text()
        name = self.name_input.text()
        if self.is_valid(name, tag_str):
            tag_id = int(tag_str)
            # Add the new team to the table
            self.table_model.insertRow(0)
            self.table_model.setData(self.table_model.index(0, self.NAME), name)
            self.table_model.setData(self.table_model.index(0, self.TAG), tag_id)
            self.table_model.setData(self.table_model.index(0, self.TAG),
53
                                     qtg.QBrush(qtg.QColor(self.team_colors[tag_id])), qtc.Qt.BackgroundRole)
Tom Pillot's avatar
Tom Pillot committed
54
55
56
57
58
            # Delete the last input
            self.tag_label.setText("")
            self.tag_label.setStyleSheet("")
            self.name_input.setText("")
            self.tag_ids[name] = tag_id
Tom Pillot's avatar
Tom Pillot committed
59
60
61
62
63

    def edit_team(self):
        selected = self.table_view.selectedIndexes()
        if selected:
            item = selected[0]
Tom Pillot's avatar
Tom Pillot committed
64
65
            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))
Tom Pillot's avatar
Tom Pillot committed
66
            self.tag_label.setText(str(tag_id))
Tom Pillot's avatar
Tom Pillot committed
67
            self.set_label_color(tag_id)
Tom Pillot's avatar
Tom Pillot committed
68
            self.name_input.setText(name)
Tom Pillot's avatar
Tom Pillot committed
69
            self.table_model.takeRow(item.row())
Tom Pillot's avatar
Tom Pillot committed
70
71
72
73
74
75
            del self.tag_ids[name]

    def delete_team(self):
        selected = self.table_view.selectedIndexes()
        if selected:
            item = selected[0]
Tom Pillot's avatar
Tom Pillot committed
76
77
            name = self.table_model.data(self.table_model.index(item.row(), self.NAME))
            self.table_model.takeRow(item.row())
Tom Pillot's avatar
Tom Pillot committed
78
79
            del self.tag_ids[name]

Tom Pillot's avatar
Tom Pillot committed
80
    def is_valid(self, new_name, new_tag_str):
Tom Pillot's avatar
Tom Pillot committed
81
82
83
        """
        Check that the name and the id are valid and not already used
        """
Tom Pillot's avatar
Tom Pillot committed
84
85
86
87
88
89
90
91
92
        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)
Tom Pillot's avatar
Tom Pillot committed
93
94
        for name, tag_id in self.tag_ids.items():
            if name == new_name:
Tom Pillot's avatar
Tom Pillot committed
95
                self.status_bar.showMessage("Une équipe avec ce nom existe déjà.", self.STATUS_TIME)
Tom Pillot's avatar
Tom Pillot committed
96
97
                return False
            elif tag_id == new_tag_id:
Tom Pillot's avatar
Tom Pillot committed
98
                self.status_bar.showMessage("Une équipe avec ce tag existe déjà.", self.STATUS_TIME)
Tom Pillot's avatar
Tom Pillot committed
99
100
                return False
        return True