Browse Source

GUI update

Rebuild mostly
Maarten van den Berg 6 years ago
parent
commit
3bc31270e0
1 changed files with 71 additions and 15 deletions
  1. 71 15
      piket_client/gui.py

+ 71 - 15
piket_client/gui.py

1
 """
1
 """
2
 Provides the graphical front-end for Piket.
2
 Provides the graphical front-end for Piket.
3
 """
3
 """
4
+import logging
4
 import os
5
 import os
5
 import sys
6
 import sys
6
 
7
 
18
     QWidget,
19
     QWidget,
19
 )
20
 )
20
 from PySide2.QtGui import QIcon
21
 from PySide2.QtGui import QIcon
21
-from PySide2.QtCore import QSize, Qt, QObject, Signal, Slot
22
+from PySide2.QtCore import QObject, QSize, Qt, Signal, Slot
22
 
23
 
23
 # pylint: enable=E0611
24
 # pylint: enable=E0611
24
 
25
 
29
 
30
 
30
 from piket_client.sound import PLOP_WAVE
31
 from piket_client.sound import PLOP_WAVE
31
 from piket_client.model import Person, ConsumptionType
32
 from piket_client.model import Person, ConsumptionType
33
+import piket_client.logger
34
+
35
+LOG = logging.getLogger(__name__)
32
 
36
 
33
 
37
 
34
 def plop() -> None:
38
 def plop() -> None:
49
         self.clicked.connect(self.process_click)
53
         self.clicked.connect(self.process_click)
50
 
54
 
51
     @Slot(str)
55
     @Slot(str)
52
-    def new_active_id(self, new_id: str):
56
+    def new_active_id(self, new_id: str) -> None:
57
+        """ Change the active ConsumptionType id, update the label. """
53
         self.active_id = new_id
58
         self.active_id = new_id
54
         self.setText(self.current_label)
59
         self.setText(self.current_label)
55
 
60
 
56
-    @property
57
-    def active_consumption_type_id(self) -> str:
58
-        return self.parent().active_consumption_type_id
61
+    @Slot()
62
+    def rebuild(self) -> None:
63
+        """ Refresh the Person object and the label. """
64
+        self.person = self.person.reload()
65
+        self.setText(self.current_label)
59
 
66
 
60
     @property
67
     @property
61
     def current_count(self) -> int:
68
     def current_count(self) -> int:
69
+        """ Return the count of the currently active ConsumptionType for this
70
+        Person. """
62
         return self.person.consumptions.get(self.active_id, 0)
71
         return self.person.consumptions.get(self.active_id, 0)
63
 
72
 
64
     @property
73
     @property
81
 
90
 
82
     new_id_set = Signal(str)
91
     new_id_set = Signal(str)
83
 
92
 
84
-    def __init__(self, consumption_type_id) -> None:
93
+    def __init__(self, consumption_type_id: str) -> None:
85
         super().__init__()
94
         super().__init__()
86
 
95
 
87
         self.layout = None
96
         self.layout = None
97
+        self.layout = QGridLayout()
98
+        self.setLayout(self.layout)
88
         self.active_consumption_type_id = consumption_type_id
99
         self.active_consumption_type_id = consumption_type_id
89
         self.init_ui()
100
         self.init_ui()
90
 
101
 
91
     @Slot(str)
102
     @Slot(str)
92
     def consumption_type_changed(self, new_id: str):
103
     def consumption_type_changed(self, new_id: str):
104
+        """ Process a change of the consumption type and propagate to the
105
+        contained buttons. """
93
         self.active_consumption_type_id = new_id
106
         self.active_consumption_type_id = new_id
94
         self.new_id_set.emit(new_id)
107
         self.new_id_set.emit(new_id)
95
 
108
 
96
     def init_ui(self) -> None:
109
     def init_ui(self) -> None:
97
         """ Initialize UI: build GridLayout, retrieve People and build a button
110
         """ Initialize UI: build GridLayout, retrieve People and build a button
98
         for each. """
111
         for each. """
99
-        self.layout = QGridLayout()
100
 
112
 
101
-        for index, person in enumerate(Person.get_all()):
113
+        ps = Person.get_all()
114
+        num_columns = round(len(ps) / 10) + 1
115
+
116
+        if self.layout:
117
+            LOG.debug("Removing %s widgets for rebuild", self.layout.count())
118
+            for index in range(self.layout.count()):
119
+                item = self.layout.itemAt(0)
120
+                LOG.debug("Removing item %s: %s", index, item)
121
+                if item:
122
+                    w = item.widget()
123
+                    LOG.debug("Person %s", w.person)
124
+                    self.layout.removeItem(item)
125
+                    w.deleteLater()
126
+
127
+        for index, person in enumerate(ps):
102
             button = NameButton(person, self.active_consumption_type_id, self)
128
             button = NameButton(person, self.active_consumption_type_id, self)
103
             self.new_id_set.connect(button.new_active_id)
129
             self.new_id_set.connect(button.new_active_id)
104
-            self.layout.addWidget(button, index // 2, index % 2)
105
-
106
-        self.setLayout(self.layout)
130
+            self.layout.addWidget(button, index // num_columns, index % num_columns)
107
 
131
 
108
 
132
 
109
 class PiketMainWindow(QMainWindow):
133
 class PiketMainWindow(QMainWindow):
157
         ag = QActionGroup(self.toolbar)
181
         ag = QActionGroup(self.toolbar)
158
         ag.setExclusive(True)
182
         ag.setExclusive(True)
159
 
183
 
160
-        for ct in ConsumptionType.get_all():
184
+        cts = ConsumptionType.get_all()
185
+        if not cts:
186
+            self.show_keyboard()
187
+            name, ok = QInputDialog.getItem(
188
+                self,
189
+                "Consumptietype toevoegen",
190
+                (
191
+                    "Dit lijkt de eerste keer te zijn dat Piket start. Wat wil je "
192
+                    "tellen? Je kunt later meer typen toevoegen."
193
+                ),
194
+                ["Bier", "Wijn", "Cola"],
195
+                current=0,
196
+                editable=True,
197
+            )
198
+            self.hide_keyboard()
199
+
200
+            if ok and name:
201
+                c_type = ConsumptionType(name=name)
202
+                c_type = c_type.create()
203
+
204
+                cts.append(c_type)
205
+
206
+            else:
207
+                QMessageBox.critical(
208
+                    self,
209
+                    "Kan niet doorgaan",
210
+                    (
211
+                        "Je drukte op 'Annuleren' of voerde geen naam in, dus ik"
212
+                        "sluit af."
213
+                    ),
214
+                )
215
+                sys.exit()
216
+
217
+        for ct in cts:
161
             action = QAction(self.load_icon(ct.icon or "beer_bottle.svg"), ct.name, ag)
218
             action = QAction(self.load_icon(ct.icon or "beer_bottle.svg"), ct.name, ag)
162
             action.setCheckable(True)
219
             action.setCheckable(True)
163
             action.setData(str(ct.consumption_type_id))
220
             action.setData(str(ct.consumption_type_id))
202
         self.hide_keyboard()
259
         self.hide_keyboard()
203
         if ok and name:
260
         if ok and name:
204
             person = Person(name=name)
261
             person = Person(name=name)
205
-            person = person.save()
262
+            person = person.create()
206
 
263
 
207
-            self.main_widget = NameButtons()
208
-            self.setCentralWidget(self.main_widget)
264
+            self.main_widget.init_ui()
209
 
265
 
210
     @staticmethod
266
     @staticmethod
211
     def create_spacer() -> QWidget:
267
     def create_spacer() -> QWidget: