-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd.py
285 lines (240 loc) · 9.2 KB
/
add.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
import os
from kivy.properties import StringProperty, NumericProperty, ListProperty, ObjectProperty
from kivy.uix.scatter import Scatter
from kivy.uix.image import AsyncImage
from kivy.uix.scatterlayout import ScatterLayout
from kivy.uix.stencilview import StencilView
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.relativelayout import RelativeLayout
from kivy.uix.dropdown import DropDown
from kivymd.app import MDApp
from kivymd.uix.list import TwoLineListItem, OneLineIconListItem
from kivymd.uix.dialog import MDDialog
from kivymd.uix.card import MDCard
from kivymd.uix.button import MDFloatingActionButton, \
MDRectangleFlatIconButton, \
MDFlatButton, \
MDRaisedButton
class CropLayout(BoxLayout, StencilView):
path = StringProperty()
class NewFoodButton(MDFloatingActionButton):
def on_release(self):
app = MDApp.get_running_app()
sm = app.root.ids.screen_manager
sids = sm.screens[-1].ids
names = ['name1', 'name2', 'cook_time', 'quick_release', 'tips', 'notes']
sids.food_type.text = 'Choose ingredient type'
sids.card.background = ''
sids.card._no_ripple_effect = False
sids.card_icon.icon = 'image-plus'
sids.card_label.text = 'UPLOAD IMAGE'
# Ensure no leftover crop layout
try:
sids.card.remove_widget(app.crop_layout)
except:
print('No existing crop layout')
for name in names:
sids[name].text = ''
sm.transition.direction = 'left'
sm.current = 'new_food_screen'
class FoodDropDown(DropDown):
items = ListProperty()
def create_menu_items(self):
"""Creates menu items."""
for data in self.items:
item = FoodListItem(
text=data.get("text", ""),
icon=data.get("icon", ""),
divider='Full',
dropdown=self,
)
self.add_widget(item)
class FoodListItem(OneLineIconListItem):
icon = StringProperty()
dropdown = ObjectProperty()
class PhotoCard(MDCard):
img_name = StringProperty()
def on_press(self):
print(1)
# Consider asking for android permissions here?
def on_release(self):
app = MDApp.get_running_app()
app.file_manager_open()
sm = app.root.ids.screen_manager
sids = sm.screens[-1].ids
sids.card_icon.icon = ''
sids.card_label.text = ''
class DiscardButton(MDRectangleFlatIconButton):
dialog = None
def show_discard_dialog(self):
if not self.dialog:
self.dialog = MDDialog(
title='Discard New Ingredient?',
text='Any information you entered will not be saved',
buttons=[
MDFlatButton(
text='CANCEL',
text_color=self.theme_cls.primary_color,
on_release=self.my_callback
),
MDRaisedButton(
text='DISCARD',
md_bg_color=self.theme_cls.primary_color,
on_release=self.my_callback
)
],
size_hint=[.8, .5],
auto_dismiss=False
)
self.dialog.open()
def my_callback(self, popup_widget):
app = MDApp.get_running_app()
sm = app.root.ids.screen_manager
if popup_widget.text == 'DISCARD':
self.dialog.dismiss()
self.dialog = None
sm.nav_dict['home_screen'].on_release(direction='right', back=True)
else:
self.dialog.dismiss()
self.dialog = None
return popup_widget.text
class AddIngredientButton(MDRectangleFlatIconButton):
dialog = None
screen_dict = {
"Vegetables": "veggie_screen",
"Meat & Eggs": "meat_screen",
"Rice & Grains": "grain_screen",
"Beans & Lentils": "bean_screen",
"Seafood": "fish_screen"
}
def show_add_dialog(self):
if not self.dialog:
self.dialog = MDDialog(
title='Add New Ingredient?',
text='Custom ingredients can be deleted at any time',
buttons=[
MDFlatButton(
text='CANCEL',
text_color=self.theme_cls.primary_color,
on_release=self.my_callback
),
MDRaisedButton(
text='ADD',
md_bg_color=self.theme_cls.primary_color,
on_release=self.my_callback
)
],
size_hint=[.8, .5],
auto_dismiss=False
)
self.dialog.open()
def show_success_dialog(self):
if not self.dialog:
self.dialog = MDDialog(
title='Success!',
text='You successfully added a new ingredient',
buttons=[
MDFlatButton(
text='OK',
text_color=self.theme_cls.primary_color,
on_release=self.my_callback
)
],
size_hint=[.8, .5],
auto_dismiss=False
)
self.dialog.open()
def show_error_dialog(self):
if not self.dialog:
self.dialog = MDDialog(
title='Error:',
text='Please select ingredient type',
buttons=[
MDFlatButton(
text='GO BACK',
text_color=self.theme_cls.primary_color,
on_release=self.my_callback
)
],
size_hint=[.8, .5],
auto_dismiss=False
)
self.dialog.open()
def my_callback(self, popup_widget):
app = MDApp.get_running_app()
sm = app.root.ids.screen_manager
snames = self.screen_dict
sids = sm.screens[-1].ids
if popup_widget.text == 'ADD':
try:
picture = sids.card.img_name
dir_path = os.path.abspath('.')
sids.card.export_to_png(f'{dir_path}/images/{sids.card.img_name}')
screen_name = snames[sids.food_type.text]
self.dialog.dismiss()
self.dialog = None
vals = (sids.name1.text, sids.name2.text, sids.cook_time.text, sids.quick_release.text, \
sids.tips.text, sids.notes.text, screen_name, picture, True)
query = '''
INSERT INTO ingredients (name1,name2,cook_time,quick_release,more_info,notes,screen,picture,user_added)
VALUES (?,?,?,?,?,?,?,?,?);
'''
app.cursor.execute(query, vals)
app.conn.commit()
sm.get_screen(screen_name).add_tile()
self.show_success_dialog()
except:
# They did not choose a food type
self.dialog.dismiss()
self.dialog = None
self.show_error_dialog()
elif popup_widget.text == 'OK':
# remove crop layout
sids.card.remove_widget(app.crop_layout)
self.dialog.dismiss()
self.dialog = None
sm.nav_dict['home_screen'].on_release()
else:
self.dialog.dismiss()
self.dialog = None
return popup_widget.text
class DeleteButton(MDRectangleFlatIconButton):
dialog = None
def show_delete_dialog(self):
if not self.dialog:
self.dialog = MDDialog(
title='Delete Custom Ingredient?',
text='Custom ingredient will be deleted permenantly and cannot be recovered',
buttons=[
MDFlatButton(
text='CANCEL',
text_color=self.theme_cls.primary_color,
on_release=self.my_callback
),
MDRaisedButton(
text='DELETE',
md_bg_color=self.theme_cls.primary_color,
on_release=self.my_callback
)
],
size_hint=[.8, .5],
auto_dismiss=False
)
self.dialog.open()
def my_callback(self, popup_widget):
app = MDApp.get_running_app()
sm = app.root.ids.screen_manager
detail_screen = sm.screens[-2]
dir_path = os.path.abspath('.')
img_path = f'{dir_path}/{detail_screen.ids.card.background}'
if popup_widget.text == 'DELETE':
detail_screen.ids.rl.remove_widget(app.delete_btn)
detail_screen.ref_screen.remove_tile(detail_screen.tile)
os.remove(img_path)
self.dialog.dismiss()
self.dialog = None
sm.nav_dict['home_screen'].on_release(direction='right')
else:
self.dialog.dismiss()
self.dialog = None
return popup_widget.text