-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
331 lines (277 loc) · 14.7 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
from kivy.config import Config
Config.set('kivy', 'window_icon', r'C:\Tu\ruta\raíz\del\proyecto\logo.png')
from kivy.uix.gridlayout import GridLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.uix.spinner import Spinner
from kivymd.app import MDApp
from kivymd.uix.button import MDFloatingActionButton, MDRaisedButton, MDIconButton, MDFlatButton
from kivymd.uix.textfield import MDTextField
from kivymd.uix.list import MDList, ThreeLineListItem
from kivymd.uix.selectioncontrol import MDCheckbox
from kivy.uix.scrollview import ScrollView
from kivymd.uix.dialog import MDDialog
from kivymd.theming import ThemeManager
from kivy.core.window import Window
from kivy.lang import Builder
import uuid
import csv
import re
import uuid
import csv
from kivy.uix.checkbox import MDCheckbox
class ShoppingListManager:
def __init__(self):
# Inicializa un diccionario vacío para almacenar los elementos de la lista de compras categorizados
self.category_items = {}
def add_item(self, article, quantity, price_per_unit, category):
try:
# Convierte la cantidad y el precio por unidad a números de punto flotante
quantity = float(quantity)
price_per_unit = float(price_per_unit)
# Verifica si la cantidad y el precio son valores positivos
if quantity <= 0 or price_per_unit <= 0:
raise ValueError("La cantidad y el precio deben ser números positivos")
except ValueError as e:
raise ValueError("Ingrese valores numéricos válidos")
# Calcula el precio total del artículo
total_price = quantity * price_per_unit
# Formatea el precio total como una cadena con dos decimales y comas para separar los miles
formatted_price = "{:,.2f}".format(total_price)
# Genera un ID único para el artículo usando uuid
article_id = str(uuid.uuid4())
# Crea un diccionario que representa el artículo
item = {
"id": article_id,
"article": article,
"quantity": quantity,
"price_per_unit": price_per_unit,
}
# Crea un widget de checkbox con KivyMD y lo añade al diccionario del artículo
checkbox = MDCheckbox(active=True, size_hint_x=None, width=45)
item["checkbox"] = checkbox
# Verifica si la categoría ya existe en el diccionario de elementos y añade el artículo
# bajo esa categoría, o crea una nueva categoría si no existe
if category not in self.category_items:
self.category_items[category] = []
self.category_items[category].append(item)
# Devuelve el artículo creado y el precio formateado
return item, formatted_price
def delete_item(self, article_id):
# Itera sobre las categorías y los elementos para encontrar y eliminar el artículo con el ID dado
for category, items in self.category_items.items():
for item in items:
if item["id"] == article_id:
items.remove(item)
return
def export_list(self, selected_items, file_path):
try:
# Calcula el precio total de todos los elementos seleccionados
total_price = sum(item["quantity"] * item["price_per_unit"] for item in selected_items)
# Abre un archivo CSV para escribir la lista de compras
with open(file_path, "w", newline="", encoding="utf-8") as csvfile:
csv_writer = csv.writer(csvfile)
# Escribe la primera fila con los encabezados de columna
csv_writer.writerow(["Artículo", "Cantidad", "Precio"])
# Itera sobre los elementos seleccionados y escribe cada uno en una fila del CSV
for item in selected_items:
article = item["article"]
quantity = item["quantity"]
price_per_unit = item["price_per_unit"]
csv_writer.writerow([article, quantity, price_per_unit])
# Escribe la fila final con el precio total
csv_writer.writerow(["Total", "", total_price])
# Retorna verdadero si la exportación fue exitosa
return True
except Exception as e:
# Si hay algún error durante la exportación, eleva una excepción con un mensaje de error
raise Exception("Error al exportar la lista:", e)
class ShopperApp(MDApp):
article_input = None
quantity_input = None
price_input = None
category_spinner = None
# Define la lista de categorías personalizadas
categorias = ['Frutas y verduras', 'Lácteos', 'Carne y pescado', 'Cereales y derivados', 'Bebidas', 'Sin gluten', 'Vegano', 'Snacks']
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.theme_cls = ThemeManager()
self.theme_cls.theme_style = "Light"
self.shopping_list_manager = ShoppingListManager()
self.total_label = Label()
def build(self):
# Establecer el icono de la aplicación
Window.set_icon('icono.ico')
# Cargar el archivo KV
root = Builder.load_file("shopper.kv")
# Asignar los widgets de entrada a los atributos de la clase
self.article_input = root.ids.article_input
self.quantity_input = root.ids.quantity_input
self.price_input = root.ids.price_input
self.category_spinner = root.ids.category_spinner
# Asignar self.list_layout después de cargar el archivo KV
self.list_layout = root.ids.list_layout
return root
def add_item(self, instance, article_input, quantity_input, price_input, category_spinner):
article = article_input.text.strip()
quantity = quantity_input.text.strip()
price_per_unit = price_input.text.strip()
category = category_spinner.text
try:
item, formatted_price = self.shopping_list_manager.add_item(article, quantity, price_per_unit, category)
if "checkbox" not in item:
checkbox = MDCheckbox(active=True, size_hint_x=None, width=45)
item["checkbox"] = checkbox
else:
checkbox = item["checkbox"]
item_container = BoxLayout(orientation='horizontal', spacing=15, size_hint_y=None, height=65)
item_container.add_widget(ThreeLineListItem(text=article, secondary_text=f"Cantidad: {quantity}", tertiary_text=f"Precio: €{formatted_price}"))
item_container.add_widget(checkbox)
item_container.add_widget(MDIconButton(icon="delete", on_release=lambda x, i=item["id"]: self.delete_item(i)))
item_container.article_id = item["id"]
# Agregar el artículo al layout de la categoría correspondiente
category_layout = self.find_or_create_category_layout(category)
category_layout.add_widget(item_container)
self.update_total()
self.clear_inputs()
except ValueError as e:
self.show_error_dialog(str(e))
def find_or_create_category_layout(self, category):
# Verificar si ya existe un layout para la categoría
for child in self.list_layout.children:
if isinstance(child, GridLayout) and child.id == category:
return child
# Si no existe, crear un nuevo layout para la categoría
category_layout = GridLayout(cols=1, spacing=5, size_hint_y=None)
category_layout.bind(minimum_height=category_layout.setter('height'))
category_layout.id = category
# Añadir el texto de la categoría con el color adecuado según el tema
category_label = Label(text=category, font_size=18, bold=True, size_hint_y=None, height=30)
if self.theme_cls.theme_style == "Dark":
category_label.color = (1, 1, 1, 1) # Establecer el color del texto en blanco en modo oscuro
else:
category_label.color = (0, 0, 0, 1) # Establecer el color del texto en negro en modo claro
self.list_layout.add_widget(category_label)
self.list_layout.add_widget(category_layout)
return category_layout
def delete_item(self, article_id):
# Buscar y eliminar el artículo solo si el checkbox está activado
for category, items in self.shopping_list_manager.category_items.items():
for item in items:
if item["id"] == article_id:
if not item["checkbox"].active: # Verificar si el checkbox está activado
self.show_error_dialog("Debe seleccionar el elemento para eliminar.")
return
items.remove(item)
category_layout = self.find_or_create_category_layout(category)
for child in category_layout.children:
if hasattr(child, "article_id") and child.article_id == article_id:
category_layout.remove_widget(child)
self.update_total()
return
def refresh_list(self):
self.list_layout.clear_widgets()
for category, items in self.shopping_list_manager.category_items.items():
if items: # Verificar si la lista de elementos no está vacía
category_layout = self.find_or_create_category_layout(category) # Encontrar o crear el layout de la categoría
category_layout.clear_widgets() # Borrar todos los widgets existentes en el layout de la categoría
# Agregar el texto de la categoría al layout (solo una vez)
category_label = Label(text=category, font_size=18, bold=True, size_hint_y=None, height=30)
category_layout.add_widget(category_label)
# Agregar los widgets de los artículos al layout de la categoría
for item in items:
article = item["article"]
quantity = item["quantity"]
price_per_unit = item["price_per_unit"]
total_price = quantity * price_per_unit
formatted_price = "{:,.2f}".format(total_price)
checkbox = MDCheckbox(active=True, size_hint_x=None, width=45) # Crear un nuevo checkbox
item["checkbox"] = checkbox # Actualizar el checkbox en el diccionario del artículo
item_container = BoxLayout(orientation='horizontal', spacing=15, size_hint_y=None, height=65)
item_container.add_widget(ThreeLineListItem(text=article, secondary_text=f"Cantidad: {quantity}", tertiary_text=f"Precio: €{formatted_price}"))
item_container.add_widget(checkbox) # Agregar el nuevo checkbox
delete_button = MDIconButton(icon="delete", on_release=lambda x, i=item["id"]: self.delete_item(i))
item_container.add_widget(delete_button)
# Agregar el contenedor del artículo al layout de la categoría
category_layout.add_widget(item_container)
else:
# Si la lista de elementos está vacía, no agregar el texto de la categoría
pass
def update_total(self):
total_price = sum(item["quantity"] * item["price_per_unit"] for category, items in self.shopping_list_manager.category_items.items() for item in items)
formatted_total = "{:,.2f}".format(total_price)
self.total_label.text = f"Total: €{formatted_total}"
def clear_inputs(self):
self.article_input.text = ""
self.quantity_input.text = ""
self.price_input.text = ""
self.category_spinner.text = ""
def clear_list(self, instance):
self.shopping_list_manager.category_items.clear()
self.refresh_list()
def export_list(self, instance):
selected_items = [item for category, items in self.shopping_list_manager.category_items.items() for item in items if item["checkbox"].active]
if not selected_items:
self.show_error_dialog("No hay artículos seleccionados para exportar.")
return
try:
file_path = self.user_file_path_dialog()
if file_path:
if self.shopping_list_manager.export_list(selected_items, file_path):
self.show_confirmation_dialog("Lista exportada exitosamente.")
else:
self.show_error_dialog("Error al exportar la lista.")
except Exception as e:
self.show_error_dialog(str(e))
def user_file_path_dialog(self):
return "shopping_list.csv"
def show_confirmation_dialog(self, message):
dialog = MDDialog(
text=message,
buttons=[
MDRaisedButton(
text="OK",
on_release=lambda *args: dialog.dismiss()
)
]
)
dialog.open()
def show_error_dialog(self, message):
dialog = MDDialog(
text=message,
buttons=[
MDRaisedButton(
text="OK",
on_release=lambda *args: dialog.dismiss()
)
]
)
dialog.open()
def toggle_dark_mode(self, *args):
if self.theme_cls.theme_style == "Light":
self.theme_cls.theme_style = "Dark"
self.theme_cls.primary_palette = "BlueGray"
self.root.ids.theme_toggle_button.text = "Light mode"
self.root.ids.theme_toggle_button.text_color = (1, 1, 1, 1)
else:
self.theme_cls.theme_style = "Light"
self.theme_cls.primary_palette = "Blue"
self.root.ids.theme_toggle_button.text = "Dark mode"
self.root.ids.theme_toggle_button.text_color = (0, 0, 0, 1)
self.update_welcome_label_color()
self.update_category_label_color()
def update_welcome_label_color(self):
if self.theme_cls.theme_style == "Dark":
self.root.ids.welcome_label.color = (1, 1, 1, 1)
else:
self.root.ids.welcome_label.color = (0, 0, 0, 1)
def update_category_label_color(self):
for child in self.root.ids.list_layout.children:
if isinstance(child, Label):
if self.theme_cls.theme_style == "Dark":
child.color = (1, 1, 1, 1)
else:
child.color = (0, 0, 0, 1)
if __name__ == "__main__":
ShopperApp().run()