This repository has been archived by the owner on Sep 28, 2020. It is now read-only.
forked from guuse/fridget-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
462 lines (371 loc) · 22.6 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
import sys
import threading
from functools import partial
import fridgetresources
from PyQt5 import QtWidgets, uic
from PyQt5 import QtCore
from PyQt5.QtCore import QDate
from PyQt5.QtWidgets import QListWidgetItem, QScrollerProperties, QScroller
import settings
from scanLoopThread import ScanLoopThread
from platform_wrapper.models.product import Product
from platform_wrapper.models.products import Products
from platform_wrapper.platform_wrapper import PlatformWrapper
from settings import PAGE_INDEXES
from utils.label_utils import process_keypress_label
from widgets.ProductWidget import ProductWidget
try:
# Since RPi.GPIO doesn't work on windows we need to fake the library if we are developing on other OS
import RPi.GPIO
except (RuntimeError, ModuleNotFoundError):
import fake_rpigpio.RPi as RPi
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, platform_api: PlatformWrapper, *args, **kwargs):
super().__init__(*args, **kwargs)
uic.loadUi("resources/ui/main/fridgettwo.ui", self)
self.platform_api = platform_api
# Grab the main stacked widget, this stacked widget contains our different pages.
self.stacked_widget = self.findChild(QtWidgets.QStackedWidget, 'mainStackedWidget')
self.stacked_widget.setCurrentIndex(0)
# unlock_page
self.unlock_page = self.stacked_widget.findChild(QtWidgets.QWidget, 'unlockPage')
self.unlock_widget = self.unlock_page.findChild(QtWidgets.QWidget, 'unlockWidget')
# Switch to the users_page when the unlockWidget is clicked
self.unlock_widget.mouseReleaseEvent = partial(self.switch_page, dest="users_page")
# users_page
self.users_page = self.stacked_widget.findChild(QtWidgets.QWidget, 'usersPage')
self.user1widget = self.users_page.findChild(QtWidgets.QWidget, 'userOneWidget')
# Switch to the main_page when the user1widget is clicked
self.user1widget.mouseReleaseEvent = partial(self.switch_page, dest="main_page", load_box=411)
# main_page
self.main_menu_page = self.stacked_widget.findChild(QtWidgets.QWidget, 'mainMenuPage')
self.main_menu_switch_scan = self.main_menu_page.findChild(QtWidgets.QWidget, 'scanWidget')
# Switch to the scan_page when the scanWidget is clicked
self.main_menu_switch_scan.mouseReleaseEvent = partial(self.switch_page, dest="scan_page")
self.main_menu_switch_inv = self.main_menu_page.findChild(QtWidgets.QWidget, 'inventoryWidget')
# Switch to the inventory_page when the inventoryWidget is clicked
self.main_menu_switch_inv.mouseReleaseEvent = partial(self.switch_page, dest="inventory_page")
self.main_menu_switch_exp = self.main_menu_page.findChild(QtWidgets.QWidget, 'expirationWidget')
# Switch to the expirations_page when the experationWidget is clicked
self.main_menu_switch_exp.mouseReleaseEvent = partial(self.switch_page, dest="expirations_page")
self.main_menu_switch_users = self.main_menu_page.findChild(QtWidgets.QWidget, 'mainMenuToUsersWidget')
# Switch to the users_page when the mainMenuToUsersWidget is clicked
self.main_menu_switch_users.mouseReleaseEvent = partial(self.switch_page, dest="users_page")
# scan_page
self.scan_page = self.stacked_widget.findChild(QtWidgets.QWidget, 'scannerPage')
self.scan_page_switch_main_menu = self.scan_page.findChild(QtWidgets.QWidget, 'scanToMainMenuWidget')
# Switch to the main_page when the scanToMainMenuWidget is clicked (also, disable the scanner, which runs on
# a worker)
self.scan_page_switch_main_menu.mouseReleaseEvent = partial(self.switch_page, dest="main_page",
disable_worker=True)
self.scan_page_send_to_fridge = self.scan_page.findChild(QtWidgets.QWidget, 'sendToFridgeWidget')
# Send products to the fridge when the sendToFridgeWidget is clicked
self.scan_page_send_to_fridge.mouseReleaseEvent = self.send_products_to_box
# Grab the hidden LineEdit, used to score the scanned EAN
self.scan_page_input_label = self.scan_page.findChild(QtWidgets.QLineEdit, 'hiddenEanLineEdit')
# ListView for scanned items
self.scan_page_product_list_view = self.scan_page.findChild(QtWidgets.QListWidget, 'scannerListWidget')
# Custom Products
self.scan_page.findChild(QtWidgets.QWidget, 'noBarcodeWidget').mouseReleaseEvent = partial(self.switch_page,
dest="custom_product_page",
disable_worker=True)
# inventory_page
self.inventory_page = self.stacked_widget.findChild(QtWidgets.QWidget, 'inventoryPage')
self.inventory_page_switch_main_menu = self.inventory_page.findChild(QtWidgets.QWidget,
'inventoryToMainMenuWidget')
# Switch to the main_page when the inventoryToMainMenuWidget is clicked
self.inventory_page_switch_main_menu.mouseReleaseEvent = partial(self.switch_page, dest="main_page")
self.inventory_page.findChild(QtWidgets.QWidget, 'fruitsWidget').mouseReleaseEvent = partial(self.switch_page,
dest="fruit_page",
category="fruit")
self.inventory_page.findChild(QtWidgets.QWidget, 'vegetablesWidget').mouseReleaseEvent = partial(
self.switch_page, dest="vegetable_page", category="vegetable")
self.inventory_page.findChild(QtWidgets.QWidget, 'dairiesWidget').mouseReleaseEvent = partial(self.switch_page,
dest="dairy_page",
category="dairy")
self.inventory_page.findChild(QtWidgets.QWidget, 'meatsWidget').mouseReleaseEvent = partial(self.switch_page,
dest="meat_page",
category="meat")
self.inventory_page.findChild(QtWidgets.QWidget, 'spreadsWidget').mouseReleaseEvent = partial(self.switch_page,
dest="spread_page",
category="spread")
self.inventory_page.findChild(QtWidgets.QWidget, 'saucesWidget').mouseReleaseEvent = partial(self.switch_page,
dest="sauce_page",
category="sauce")
self.inventory_page.findChild(QtWidgets.QWidget, 'drinksWidget').mouseReleaseEvent = partial(self.switch_page,
dest="drink_page",
category="drink")
self.inventory_page.findChild(QtWidgets.QWidget, 'othersWidget').mouseReleaseEvent = partial(self.switch_page,
dest="other_page",
category="other")
# expiration_page
self.expirations_page = self.stacked_widget.findChild(QtWidgets.QWidget, 'expirationsPage')
self.expirations_page_switch_main_menu = self.expirations_page.findChild(QtWidgets.QWidget,
'expirationsToMainMenuPage')
self.exp_list_widget = self.expirations_page.findChild(QtWidgets.QListWidget, 'expirationsListWidget')
self.products = Products()
self.inventory_products = Products()
# custom_product_page
custom_product_page = self.stacked_widget.findChild(QtWidgets.QWidget, 'customProductNamePage')
self.custom_product_label = self.stacked_widget.findChild(QtWidgets.QLabel, 'productNameLabel')
self.custom_product_name_label = ""
next_widget = custom_product_page.findChild(QtWidgets.QWidget, 'nextNameWidget')
for key in settings.KEYBOARD_KEYS:
custom_product_page.findChild(QtWidgets.QWidget, key + 'Widget').mouseReleaseEvent = partial(self.update_custom_product_label, label=self.custom_product_label, key=key, next_widget=next_widget)
custom_product_page.findChild(QtWidgets.QWidget, 'backNameWidget').mouseReleaseEvent = partial(
self.switch_page,
dest="scan_page")
next_widget.mouseReleaseEvent = partial(
self.switch_page,
dest="custom_product_expiration_page")
# custom_product_expiration_page
custom_product_expiration_page = self.stacked_widget.findChild(QtWidgets.QWidget, 'customProductExpirationPage')
custom_product_expiration_page.findChild(QtWidgets.QWidget, 'backExpWidget').mouseReleaseEvent = partial(
self.switch_page,
dest="custom_product_page")
self.expiration_calender = custom_product_expiration_page.findChild(QtWidgets.QCalendarWidget,
'productExpirationCalenderWidget')
custom_product_expiration_page.findChild(QtWidgets.QWidget, 'nextExpWidget').mouseReleaseEvent = partial(
self.switch_page,
dest="custom_product_category_page"
)
# custom_product_category_page
custom_product_category_page = self.stacked_widget.findChild(QtWidgets.QWidget, 'customProductCategoryPage')
for category in settings.CATEGORIES:
custom_product_category_page.findChild(QtWidgets.QWidget, category + 'CustomCategoryWidget').mouseReleaseEvent = partial(
self.insert_custom_product,
category=category)
custom_product_category_page.findChild(QtWidgets.QWidget, 'backCategoryWidget').mouseReleaseEvent = partial(
self.switch_page,
dest="custom_product_expiration_page")
self._setup_calendar()
self._setup_scroll_bars()
# Configure the scanner thread
self.scanner_thread = ScanLoopThread()
self.scanner_thread.set_focus_signal.connect(self._set_focus)
#self.show()
self.showFullScreen()
self.setCursor(QtCore.Qt.BlankCursor)
def switch_page(self, event=None, dest: str = None, disable_worker: bool = False, load_box: int = None,
category: str = None, clearable_list=None):
"""Switch page
:event event: an event
:dest string: the destination page, see settings.py
:disable_worker bool: whether or not the main worker, used for the scanner, should be halted
:load_box int: which box should be loaded
:category string: the category, used when filtering
:clearable_list: list which should be cleared
"""
if category:
self.filter_products(category)
if load_box:
self.inventory_products = self.platform_api.get_products(load_box)
if clearable_list:
clearable_list.clear()
if PAGE_INDEXES[dest] == 13:
self.scanner_thread.start()
elif PAGE_INDEXES[dest] == 2 and disable_worker:
self.scan_page_product_list_view.clear()
self.products.products.clear()
elif PAGE_INDEXES[dest] == 12:
self.soon_expired_products()
if disable_worker:
self.scanner_thread.stop()
self.scanner_thread.exit()
self.stacked_widget.setCurrentIndex(PAGE_INDEXES[dest])
def filter_products(self, category):
"""Filter the inventory by a category and fill the ListWidget
We use generic page and ListWidget names, based on category.
Example, the page for Dairy is called dairyPage, the ListWidget is called dairyListWidget, etc.
:category string: string category
"""
filtered_products = self.inventory_products.filter_category(category)
page = self.stacked_widget.findChild(QtWidgets.QWidget, category + "Page")
list = page.findChild(QtWidgets.QListWidget, category + "ListWidget")
for product in filtered_products:
product_item = QListWidgetItem(list)
product_item_widget = ProductWidget(product, self, category)
product_item.setSizeHint(product_item_widget.size())
list.addItem(product_item)
list.setItemWidget(product_item, product_item_widget)
page.findChild(QtWidgets.QWidget, category + "BackWidget").mouseReleaseEvent = partial(self.switch_page,
dest="inventory_page",
clearable_list=list)
def soon_expired_products(self):
"""Filter the inventory by expiration days and fill the ListWidget"""
soon_expired_products = self.inventory_products.filter_exp(3)
for product in soon_expired_products:
product_item = QListWidgetItem(self.exp_list_widget)
product_item_widget = ProductWidget(product, self, "expirations")
product_item.setSizeHint(product_item_widget.size())
self.exp_list_widget.addItem(product_item)
self.exp_list_widget.setItemWidget(product_item, product_item_widget)
self.expirations_page.findChild(QtWidgets.QWidget, "expirationBackWidget").mouseReleaseEvent = partial(self.switch_page,
dest="main_page",
clearable_list=self.exp_list_widget)
def update_products_widget(self, product: Product, category: str, increase: bool = True, scanner_page: bool = True):
"""Update a product ListWidget
Used when the amount of a product is changed and the ListWidget and product require updating
:product Product: the Product object in question
:category string: the category (used to pick the right page and ListWidget)
:increase boolean: whether the amount has been increased or not
:scanner_page boolean: whether we are updating the inventory or simply the list of scanned products
"""
page = self.stacked_widget.findChild(QtWidgets.QWidget, category + "Page")
list = page.findChild(QtWidgets.QListWidget, category + "ListWidget")
all_items = list.findItems('', QtCore.Qt.MatchRegExp)
row = 0
widget = None
for item in all_items:
widget = list.itemWidget(item)
if list.itemWidget(item).product == product:
break
row += 1
if increase:
if widget:
widget.product.product_amount += 1
widget.productAmountLabel.setText(widget.product.product_amount.__str__())
else:
if widget and widget.product.product_amount == 1:
list.takeItem(row)
if scanner_page:
self.products.products.remove(widget.product)
else:
self.inventory_products.products.remove(widget.product)
else:
widget.product.product_amount -= 1
widget.productAmountLabel.setText(widget.product.product_amount.__str__())
def update_custom_product_label(self, event, label, key, next_widget):
"""Update the display value of the custom product label, used for naming a custom product
:event event: an event
:label QtLabel: the label
:key string: which key has been clicked
:next_widget: the widget that goes to the next page, disabled when the length is 0 (so that the user
cannot go to the next page with an empty name)
"""
self.custom_product_name_label = process_keypress_label(event=event, label=label, value=key)
if len(self.custom_product_name_label) > 0:
next_widget.setEnabled(True)
else:
next_widget.setEnabled(False)
def insert_custom_product(self, event, category):
custom_product = Product(
product_name=self.custom_product_name_label,
product_exp=QDate.currentDate().daysTo(self.expiration_calender.selectedDate()),
product_category=category
)
self.products.products.append(custom_product)
product_item = QListWidgetItem(self.scan_page_product_list_view)
product_item_widget = ProductWidget(custom_product, self, "scanner", local=True)
product_item.setSizeHint(product_item_widget.size())
self.scan_page_product_list_view.addItem(product_item)
self.scan_page_product_list_view.setItemWidget(product_item, product_item_widget)
# Clear the used variables
self.custom_product_name_label = ""
self.custom_product_label.clear()
self.expiration_calender.setSelectedDate(QDate.currentDate())
self.switch_page(event=None, dest="scan_page")
def add_to_scanned_list_table(self, ean: str):
"""Add a scanned product to the list
:ean string: the scanned ean
"""
product = self.platform_api.get_product_from_ean(ean)
if product is not None:
self.products.add_product(product)
product_item = QListWidgetItem(self.scan_page_product_list_view)
product_item_widget = ProductWidget(product, self, "scanner", local=True)
product_item.setSizeHint(product_item_widget.size())
self.scan_page_product_list_view.addItem(product_item)
self.scan_page_product_list_view.setItemWidget(product_item, product_item_widget)
self.scan_page_input_label.clear()
self.scan_page_product_list_view.scrollToBottom()
# Set the label clear again
self.scan_page_input_label.clear()
# And start the scanner again
self.scanner_thread.scanning = True
def send_products_to_box(self, event=None):
"""Send the scanned products to the box
"""
self.scanner_thread.stop()
self.scanner_thread.exit()
new_products = self.platform_api.add_products(self.products)
self.scan_page_product_list_view.clear()
self.products.products.clear()
self.inventory_products = new_products
self.scanner_thread.start()
def _setup_scroll_bars(self):
"""Set the properties of all scroll bars
Scroll bars are selected based on a unique prefix and a common suffix.
Example: dairy + ListWidget, where the 'ListWidget' suffix is the same for all other list widgets.
"""
for widget_prefix in settings.ALL_SCROLLABLE_LIST_WIDGETS_PREFIXES:
list_widget = self.stacked_widget.findChild(QtWidgets.QWidget, widget_prefix + "Page")\
.findChild(QtWidgets.QListWidget, widget_prefix + "ListWidget")
sp = QScrollerProperties()
sp.setScrollMetric(QScrollerProperties.DragVelocitySmoothingFactor, 0.6)
sp.setScrollMetric(QScrollerProperties.MinimumVelocity, 0.0)
sp.setScrollMetric(QScrollerProperties.MaximumVelocity, 0.2)
sp.setScrollMetric(QScrollerProperties.AcceleratingFlickMaximumTime, 0.1)
sp.setScrollMetric(QScrollerProperties.AcceleratingFlickSpeedupFactor, 1.2)
sp.setScrollMetric(QScrollerProperties.SnapPositionRatio, 0.2)
sp.setScrollMetric(QScrollerProperties.MaximumClickThroughVelocity, 1)
sp.setScrollMetric(QScrollerProperties.DragStartDistance, 0.001)
sp.setScrollMetric(QScrollerProperties.HorizontalOvershootPolicy, 1)
sp.setScrollMetric(QScrollerProperties.VerticalOvershootPolicy, 1)
scroller = QScroller.scroller(list_widget.viewport())
scroller.setScrollerProperties(sp)
scroller.grabGesture(list_widget.viewport(), QScroller.LeftMouseButtonGesture)
def _setup_calendar(self):
"""Set the properties of our expiration calendar
"""
for d in (QtCore.Qt.Saturday, QtCore.Qt.Sunday):
fmt = self.expiration_calender.weekdayTextFormat(d)
fmt.setForeground(QtCore.Qt.black)
self.expiration_calender.setWeekdayTextFormat(d, fmt)
def _clear_ean_label(self):
"""Clear the input label used by our barcode scanner
"""
self.scan_page_input_label.clear()
def _set_focus(self):
"""Set focus on the input label
"""
self.scan_page_input_label.setFocus()
def _processed_scanned_ean(self):
"""Process an EAN which has been scanned
"""
if len(self.scan_page_input_label.text()) == 13:
scanned_ean = self.scan_page_input_label.text()
self.add_to_scanned_list_table(scanned_ean)
else:
self.scan_page_input_label.clear()
self.scanner_thread.scanning = True
def closeEvent(self, *args, **kwargs):
"""Override the closeEvent method, we clean up GPIO and exit the app
"""
RPi.GPIO.cleanup()
app.exit()
sys.exit()
def keyPressEvent(self, event, *args, **kwargs):
"""Override the keyPressEvent method, if enter is pressed we know an EAN has been scanned
"""
super(MainWindow, self).keyPressEvent(event)
from PyQt5.QtCore import Qt
if event.key() == Qt.Key_Return and self.scanner_thread.scanning:
self.scanner_thread.scanning = False
self._processed_scanned_ean()
RPi.GPIO.setmode(RPi.GPIO.BCM)
RPi.GPIO.setup(settings.IR_PIN, RPi.GPIO.IN)
RPi.GPIO.setup(settings.SCANNER_PIN, RPi.GPIO.OUT)
platform_api = PlatformWrapper(api_key="")
settings.PLATFORM_API = platform_api
app = QtWidgets.QApplication(sys.argv)
window = MainWindow(settings.PLATFORM_API)
# window.showFullScreen()
sys._excepthook = sys.excepthook
def exception_hook(exctype, value, traceback):
RPi.GPIO.cleanup()
print(exctype, value, traceback)
sys._excepthook(exctype, value, traceback)
sys.exit(1)
sys.excepthook = exception_hook
app.exec_()