This repository has been archived by the owner on Oct 17, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainview.py
131 lines (96 loc) · 4.07 KB
/
mainview.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
from cart import *
from settings import *
from os.path import isfile
from loginview import show_popup
from kivy.properties import BoundedNumericProperty, NumericProperty, ObjectProperty, StringProperty
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.uix.dropdown import DropDown
from kivy.uix.gridlayout import GridLayout
from kivy.uix.screenmanager import FadeTransition, CardTransition
from kivy.uix.scrollview import ScrollView
item_data_path = "res/items.p"
dismiss_msg = "\n\n[i]Click anywhere else to close"
class ToolBar(BoxLayout):
pass
class ToolBarButton(Button):
pass
class MainButton(Button):
pass
class DropDownButton(MainButton):
pass
class ProfileButton(ToolBarButton):
popup = ObjectProperty()
def __init__(self, **kwargs):
super(ProfileButton, self).__init__(**kwargs)
# create drop down menu
self.drop_down = DropDown(auto_width=False, width=2*self.width)
# add profile button to drop down
profile_btn = DropDownButton(text="Profile")
profile_btn.bind(on_release=lambda x: setattr(self.root.screen_manager, "current", "profile_screen"))
profile_btn.bind(on_release=self.drop_down.dismiss)
profile_btn.bind(on_release=lambda x: self.root.screen_manager.record())
self.drop_down.add_widget(profile_btn)
# add log out button to drop down
log_out_btn = DropDownButton(text="Log Out")
log_out_btn.bind(on_release=lambda x: setattr(self.root.screen_manager,
"transition",
FadeTransition(clearcolor=(.8, .8, 1, 1))))
log_out_btn.bind(on_release=lambda x: setattr(self.root.screen_manager, "current", "login_screen"))
log_out_btn.bind(on_release=lambda x: setattr(self.root.screen_manager,
"transition",
CardTransition(direction="down", mode="push")))
log_out_btn.bind(on_release=self.drop_down.dismiss)
self.drop_down.add_widget(log_out_btn)
# open drop down when mouse is released on button
self.bind(on_release=self.drop_down.open)
class ItemView(BoxLayout):
name = StringProperty()
img = StringProperty()
price = NumericProperty()
quantity = BoundedNumericProperty(0, min=0, max=99)
def __init__(self, name, img, price):
self.name = name
if isfile("res/item_img/" + img):
self.img = img
else:
self.img = "unknown_item.png"
self.price = price
super(ItemView, self).__init__()
def item_action(self, value):
if self.opacity != 1:
return
try:
self.quantity += value
Cart.item_action(self.name, value)
except ValueError:
show_popup("Operation not permitted!",
"Item quantity can not go below 0 or above 99")
class MainScrollView(ScrollView):
pass
class ItemLayout(GridLayout):
def __init__(self, **kwargs):
super(ItemLayout, self).__init__(**kwargs)
# dynamically add item views according to data file
Item.start() # start item class, load item data
for item_info in Item.data:
self.add_widget(ItemView(*item_info))
def filter(self, text):
# remove all children
self.clear_widgets()
# only add widget if pass filter
for item_info in Item.data:
name = item_info[0]
item_view = ItemView(*item_info)
if text.lower().replace(' ', '') in name.lower().replace(' ', ''): # case insensitive and space ignored
self.add_widget(item_view)
def refresh(self):
# load saved cart
saved_cart = Cart.load_cart()
for item_view in self.children:
for name, info in saved_cart.items():
if item_view.name == name:
item_view.quantity = info[1]
break
else:
item_view.quantity = 0