-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
1438 lines (1259 loc) · 68.4 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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import os
import sys
if sys.__stdout__ is None or sys.__stderr__ is None:
os.environ['KIVY_NO_CONSOLELOG'] = '1'
import kivy
import pandas as pd
import tabula
import io
import functools
from fpdf import FPDF
from PyPDF2 import PdfMerger
from kivy.config import Config
from kivy.app import App
from kivy.app import Builder
from kivy.uix.screenmanager import ScreenManager, Screen, WipeTransition
from kivy.uix.label import Label
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.uix.image import Image
from kivy.animation import Animation
from kivy.clock import Clock
from kivy.graphics import Rectangle
from kivy.graphics import Line
from kivy.uix.anchorlayout import AnchorLayout
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.gridlayout import GridLayout
from kivy.uix.scrollview import ScrollView
from kivy.core.window import Window
from kivy.properties import NumericProperty
from kivy.properties import StringProperty
from kivy.properties import BooleanProperty
from kivy.properties import ListProperty
from kivy.event import EventDispatcher
from kivy.animation import Animation
from HoverButton import HoverBehavior
from kivy.uix.behaviors.focus import FocusBehavior
from kivy.uix.textinput import TextInput
from plyer import filechooser
""" Absolute paths to relevant data/folders."""
main_script_directory = os.path.abspath(os.path.dirname(__file__))
csv_file_path = os.path.join(main_script_directory, "Components_data.csv")
lucida_font_path = r"C:\Windows\Fonts\l_10646.ttf"
images_directory = os.path.join(main_script_directory, "Images")
pdf_component_directory = os.path.join(main_script_directory, "PDF Component folder")
kivy_file_path = os.path.join(main_script_directory,"sanistore.kv")
Builder.load_file(kivy_file_path)
"""
Class that allows transitions between screens.
Using this we are preventing creating redundant switch screen methods within each class.
"""
class Transition:
def transition(self, screen_name):
self.manager.transition = WipeTransition()
self.manager.transition.duration = 1
self.manager.current = screen_name
def home_page(self, instance):
self.manager.transition.duration = 1
self.manager.current = "Second"
self.manager.transition = WipeTransition()
"""
Custom class HoverButton(original creator: 'Olivier POYEN') fills the role as a highlighter of a button.
Once mouse is hovering over a specified widget, its custom events (on_enter,on_leave) are fired,
allowing us to modify the properties of the widget.
"""
class HoverButton(Button, HoverBehavior):
def __init__(self, **kwargs):
super(HoverButton, self).__init__(**kwargs)
"""
Dictionary which consists of keys and values. Each key is a image before being selected with a mouse hover,
each value of a key is a image which is highlighted after it has been selected with a mouse hover.
Whenever we want to add additional button, that includes a highlight function, we just need to update the path within this dictionary.
"""
self.images_path = {f"{images_directory}/inventory_text.png": f"{images_directory}/inventory_text_selected.png",
f"{images_directory}/import_text.png": f"{images_directory}/import_text_selected.png",
f"{images_directory}/export_text.png":f"{images_directory}/export_text_selected.png",
f"{images_directory}/exit_button.png":f"{images_directory}/exit_button_selected.png",
f"{images_directory}/home_button_icon.png":f"{images_directory}/home_button_icon_selected.png",
f"{images_directory}/notebook_closed.png":f"{images_directory}/notebook_closed_selected.png",
f"{images_directory}/notebook_opened.png":f"{images_directory}/notebook_opened_selected.png",
f"{images_directory}/minus_sign.png":f"{images_directory}/minus_sign_selected.png",
f"{images_directory}/plus_sign.png":f"{images_directory}/plus_sign_selected.png",
f"{images_directory}/lock_icon.png":f"{images_directory}/unlocked_icon_selected.png",
f"{images_directory}/unlocked_icon.png":f"{images_directory}/lock_icon_selected.png",
f"{images_directory}/pencil_edit.png":f"{images_directory}/pencil_icon_selected.png",
f"{images_directory}/closed_folder.png":f"{images_directory}/opened_folder.png",
f"{images_directory}/arrow_pick.png":f"{images_directory}/arrow_pick_selected.png",
f"{images_directory}/back_arrow_icon.png":f"{images_directory}/back_arrow_icon_selected.png",
f"{images_directory}/expand_arrow.png":f"{images_directory}/expand_arrow_selected.png",
f"{images_directory}/closed_box.png":f"{images_directory}/closed_box_selected.png",
f"{images_directory}/return_box.png":f"{images_directory}/return_box_selected.png",
f"{images_directory}/ship_box.png":f"{images_directory}/ship_box_selected.png",
f"{images_directory}/select_pdf_button.png":f"{images_directory}/select_pdf_button_selected.png",
f"{images_directory}/select_component_button.png":f"{images_directory}/select_component_button_selected.png",
f"{images_directory}/pdf_merge_icon.png":f"{images_directory}/pdf_merge_icon_selected.png"}
"""
on_button_hover" method loops trough the 'images_path' dictionary and looks for a element that is equal to a instance attribute.
In this case it's looking for a background_normal within the "inventory_button" widget. Once it finds a equal string,
it changes the source image for a highlighted one.
"""
def on_button_hover(self, instance):
for key in self.images_path:
if instance.background_normal in key:
instance.background_normal = self.images_path[key]
"""
Similar method to the previous one. Once the mouse hover leaves the designated area of a button.
It loops trough the dictionary and after it finds a corresponding string, it changes it back to the non-highlighted image.
"""
def on_button_hover_exit(self, instance):
for key, value in self.images_path.items():
if instance.background_normal in value:
instance.background_normal = key
class WelcomeScreen(Screen, Transition):
def __init__(self, **kwargs):
super(WelcomeScreen, self).__init__(**kwargs)
self.logo_image = Image(
source=f"{images_directory}/Logo.png",
size_hint=(1.5,1.5),
opacity=0,
pos_hint={"center_x": .5, "center_y": 1})
self.ids.LY1.add_widget(self.logo_image)
fade_in_image = Animation(opacity=1, duration=1.5)
fade_in_image.start(self.logo_image)
# In order for this function to perform as inteded, lambda needs to be used here.
Clock.schedule_once(lambda dt: self.transition("Second"), 4)
class MainScreen(Screen, Transition):
def __init__(self, **kwargs):
super(MainScreen,self).__init__(**kwargs)
self.inventory_button = HoverButton(
background_normal=f"{images_directory}/inventory_text.png",
background_down=f"{images_directory}/inventory_text.png",
pos_hint={"center_x": .2, "center_y": .9},
size_hint=(.28, .1),
border=(0, 0, 0, 0))
self.inventory_button.bind(on_enter=self.inventory_button.on_button_hover, on_leave=self.inventory_button.on_button_hover_exit)
# In order for a transition to work, we need to combine this on_release event with lambda.
self.inventory_button.bind(on_release=lambda screen: self.transition("Third"))
self.export_button = HoverButton(
background_normal=f"{images_directory}/export_text.png",
background_down=f"{images_directory}/export_text.png",
pos_hint={"center_x": .162, "center_y": .8},
size_hint=(.28, .1),
border=(0, 0, 0, 0))
self.export_button.bind(on_enter=self.export_button.on_button_hover, on_leave=self.export_button.on_button_hover_exit)
self.export_button.bind(on_release=lambda screen: self.transition("Fifth"))
self.import_button = HoverButton(
background_normal=f"{images_directory}/import_text.png",
background_down=f"{images_directory}/import_text.png",
size_hint=(.28, .1),
pos_hint={"center_x": .154, "center_y": .7},
border=(0, 0, 0, 0))
self.import_button.bind(on_enter=self.import_button.on_button_hover, on_leave=self.import_button.on_button_hover_exit)
self.import_button.bind(on_release=lambda screen: self.transition("Fourth"))
self.exit_button = HoverButton(
background_normal=f"{images_directory}/exit_button.png",
background_down=f"{images_directory}/exit_button.png",
size_hint=(.06, .095),
pos_hint={"center_x": .94, "center_y": .09},
border=(0, 0, 0, 0))
self.exit_button.bind(on_enter=self.exit_button.on_button_hover, on_leave=self.exit_button.on_button_hover_exit)
self.exit_button.bind(on_release=self.exit_app)
self.ids.LY2.add_widget(self.inventory_button)
self.ids.LY2.add_widget(self.export_button)
self.ids.LY2.add_widget(self.import_button)
self.ids.LY2.add_widget(self.exit_button)
def exit_app(self, instance):
App.get_running_app().stop()
class InventoryScreen(Screen, Transition):
def __init__(self, **kwargs):
super(InventoryScreen, self).__init__(**kwargs)
self.home_button = HoverButton(
background_normal=f"{images_directory}/home_button_icon.png",
background_down=f"{images_directory}/home_button_icon.png",
size_hint=(.06,.095),
border=(0,0,0,0),
pos_hint={"center_x": .94,"center_y": .09})
self.home_button.bind(on_enter=self.home_button.on_button_hover, on_leave=self.home_button.on_button_hover_exit)
self.home_button.bind(on_release=self.home_page)
self.ids.LY3.add_widget(self.home_button)
self.current_state_image = Image(
source=f"{images_directory}/current_state.png",
size_hint=(.3, .5),
pos_hint={"center_x": .175, "center_y": .92})
self.ids.LY3.add_widget(self.current_state_image)
self.notebook_button = HoverButton(
background_normal=f"{images_directory}/notebook_closed.png",
background_down=f"{images_directory}/notebook_closed.png",
background_disabled_normal=f"{images_directory}/notebook_closed_disabled.png",
size_hint=(.04, .1),
border=(0, 0, 0, 0),
pos_hint={"center_x": .38, "center_y": .91})
self.notebook_button.bind(on_enter=self.notebook_button.on_button_hover, on_leave=self.notebook_button.on_button_hover_exit)
self.notebook_button.bind(on_release=self.background_change)
self.ids.LY3.add_widget(self.notebook_button)
self.lock_button = HoverButton(
background_normal=f"{images_directory}/lock_icon.png",
background_down=f"{images_directory}/unlocked_icon.png",
background_disabled_normal=f"{images_directory}/lock_icon_disabled.png",
size_hint=(.05, .09),
border=(0, 0, 0, 0),
pos_hint={"center_x": .45, "center_y": .91})
self.lock_button.bind(on_enter=self.lock_button.on_button_hover, on_leave=self.lock_button.on_button_hover_exit)
self.lock_button.bind(on_release=self.update_components)
self.ids.LY3.add_widget(self.lock_button)
"""
Method that controls the state of the "lock_button" widget. Once the background image of the lock_button is changed,
we are firing widgets depending on the background. In order for the widgets to display properly in the UI, once the state is changed,
all widgets are deleted and corresponding widgets take place immediately.
"""
def update_components(self, instance):
# Widgets are unlocked.
if instance.background_normal == f"{images_directory}/unlocked_icon_selected.png":
self.ids.LY4.clear_widgets()
self.notebook_button.disabled = True
instance.background_normal = f"{images_directory}/unlocked_icon.png"
instance.background_down = f"{images_directory}/lock_icon.png"
self.add_widgets(self.ids.LY4)
with open(csv_file_path, 'rb') as f:
contents = f.read()
self.df = pd.read_csv(io.BytesIO(contents), encoding='utf-8')
# self.df = pd.read_csv(csv_file_path, encoding='utf-8')
"""
'final_count' list is filled with 0 for each component in the dataframe.
Changes made to the component's amount are stored here and every component is under a corresponding index.
When user is finished with the changes by 'locking' the interface, 'save_data' method is fired, which updates
the original .csv database. Upon every return to 'unlocked' interface, new 'final_count' list with zero values is instantiated.
"""
self.final_count = [0 for _ in self.df["Mnozstvi"]]
# Widgets are locked.
if instance.background_normal == f"{images_directory}/lock_icon_selected.png":
self.save_data(self.final_count)
self.ids.LY4.clear_widgets()
self.notebook_button.disabled = False
instance.background_normal = f"{images_directory}/lock_icon.png"
instance.background_down = f"{images_directory}/unlocked_icon.png"
self.add_widgets(self.ids.LY4)
"""
Whenever we load this screen for the first time, "add_widgets" is fired that fills the screen with scrollable database
of components and their respective amount. This fucntion is also applied when we return to this screen. Further in the code
we will use a "on_leave" function that clears all of the widgets to prevent the widgets from overlapping.
"""
def on_pre_enter(self, *args):
with open(csv_file_path, 'rb') as f:
contents = f.read()
self.df = pd.read_csv(io.BytesIO(contents), encoding='utf-8')
self.add_widgets(self.ids.LY4)
"""
Method that changes visuals and also adds or clears widgets depending on the current notebook status.
This method is bound to a button click that traverses between an opened and closed.
Whenever we click a respective button and meet a condition thats states "notebook_closed",
we fire a "clear_widgets" function that deletes widgets from previous layout and adds new widgets to a new layout.
"""
def background_change(self, instance):
if instance.background_normal and instance.background_down == f"{images_directory}/notebook_closed.png":
instance.size_hint=(.08,.1)
instance.background_normal = f"{images_directory}/notebook_opened.png"
instance.background_down = f"{images_directory}/notebook_opened.png"
self.ids.LY4.clear_widgets()
self.add_widgets(self.ids.LY5)
"""
Same utility only the other way around. We delete widgets from new layout only to fire a "on_pre_enter" function,
that brings us back to the first layout with corresponding widgets included.
"""
elif instance.background_normal and instance.background_down == f"{images_directory}/notebook_opened.png":
instance.size_hint=(.04,.1)
instance.background_normal=f"{images_directory}/notebook_closed.png"
instance.background_down=f"{images_directory}/notebook_closed.png"
self.ids.LY5.clear_widgets()
self.on_pre_enter()
"""
Method that creates widgets depending on the current layout. We switch between "notebook_opened" and "notebook_closed".
With series of for loops we create a database of warehouse stock. Under first condition, the database is scrollable
and set into two columns.
"add_widgets" method now also servers as a bridge between locked and unlocked status of the stock.
Whenever a lock icon is clicked and becomes unlocked, additional widgets are instantiated ("minus_sign", "plus_sign", "previous_amount").
These widgets serve as a alternative mode that allows us to manually change the values on each component.
Unfortunately we face a necessary redundancy in the upcoming code, in each for loop widgets are duplicated to prevent an error:
"WidgetException: Cannot add <kivy.uix.label.Label object at 0x...>, it already has a parent".
"""
def add_widgets(self, layer):
""" Inteface is locked."""
if self.notebook_button.background_normal and self.notebook_button.background_down == f"{images_directory}/notebook_closed.png":
if self.lock_button.background_normal == f"{images_directory}/lock_icon.png":
self.lock_button.disabled = False
""" We created an empty list for later use."""
self.new_amount_list = []
for index, (component, amount) in enumerate(zip(self.df["Komponent"], self.df["Mnozstvi"])):
self.component_label= Label(
text=component,
font_size=20,
padding=(100,0,0,0),
bold=False)
self.amount_input = Label(
text=str(amount),
font_size=35,
padding=(60, 0, 0, 0),
bold=False)
self.minus_sign = HoverButton(
background_disabled_normal=f"{images_directory}/minus_sign_disabled.png",
size_hint=(None, None),
width=90,
height=80,
disabled=True,
padding=(0,0,0,0),
opacity=.2)
self.plus_sign = HoverButton(
background_disabled_normal=f"{images_directory}/plus_sign_disabled.png",
size_hint=(None, None),
width=80,
height=80,
disabled=True,
opacity=.2)
self.value_input_button = HoverButton(
background_disabled_normal=f"{images_directory}/pencil_edit.png",
border=(0, 0, 0, 0),
disabled=True,
opacity=(.05),
size_hint=(.25, .01),
padding=(0, 0, 50, 0))
""" Setting the number of columns explicitly to match the positioning of widgets."""
self.ids.LY4.cols = 5
self.ids.SW1.size_hint = (.484, .8)
layer.add_widget(self.component_label)
layer.add_widget(self.amount_input)
layer.add_widget(self.minus_sign)
layer.add_widget(self.plus_sign)
layer.add_widget(self.value_input_button)
for divider in range(5):
self.divider_line_3 = Image(
source=f"{images_directory}/divider_3.png",
size_hint_y=None,
size_hint_x=.1,
height=2,
width=2,
fit_mode="fill")
layer.add_widget(self.divider_line_3)
"""
Condition that lets us set specific widgets from disabled=True to False, alowing us to use the "minus_sign" and "plus_sign" widgets
to control the amount of the components. This condition is active only if the lock icon is pressed and becomes unlocked.
"""
if self.lock_button.background_normal == f"{images_directory}/unlocked_icon.png":
self.text_inputs_list = []
""" Interface is unlocked."""
for index, (component, amount) in enumerate(zip(self.df["Komponent"], self.df["Mnozstvi"])):
self.component_label_2 = Label(
text=component,
font_size=20,
padding=(175, 0, 0, 0),
bold=False)
self.amount_input_2 = Label(
text=str(amount),
font_size=35,
padding=(310, 0, 10, 0),
bold=False)
self.minus_sign_2 = HoverButton(
background_normal=f"{images_directory}/minus_sign.png",
background_down=f"{images_directory}/minus_sign.png",
size_hint=(None, None),
width=90,
height=80,
disabled=False,
opacity=1,
on_release=self.decrement_value)
self.minus_sign_2.bind(on_enter=self.minus_sign_2.on_button_hover, on_leave=self.minus_sign_2.on_button_hover_exit)
self.minus_sign_2.my_id = index
self.plus_sign_2 = HoverButton(
background_normal=f"{images_directory}/plus_sign.png",
background_down=f"{images_directory}/plus_sign.png",
size_hint=(None, None),
width=80,
height=80,
disabled=False,
opacity=1,
on_release=self.increment_value)
self.plus_sign_2.bind(on_enter=self.plus_sign_2.on_button_hover, on_leave=self.plus_sign_2.on_button_hover_exit)
self.plus_sign_2.my_id = index
"""
"new_amount" Label is a widget that the user can modify according to his needs with the use of "plus_sign_2", "minus_sign_2" and "value_input_button" buttons.
Each iteration of this Label is then added to a list to help us connect it with corresponding components.
"""
self.new_amount = Label(
text=str(0),
font_size=30,
padding=(110, 0, 0, 0),
disabled=False,
color=(1,1,1,0),
bold=True)
self.new_amount_list.append(self.new_amount)
""" Widget that allows the user to create a text_input field, which is automatically focused."""
self.value_input_button = HoverButton(
background_normal=f"{images_directory}/pencil_edit.png",
background_down=f"{images_directory}/pencil_edit.png",
border=(0, 0, 0, 0),
opacity=(.6),
size_hint=(.35, .01),
padding=(0, 0, 0, 0))
self.value_input_button.bind(on_enter=self.value_input_button.on_button_hover, on_leave=self.value_input_button.on_button_hover_exit)
self.value_input_button.bind(on_release=self.button_to_text)
self.value_input_button.my_id = index
"""
Creating a empty text_input field in which user can enter numerical value.
After user validates the field with an press of a 'enter' button. Value from the text field is transfered into "new_amount" widget.
After validation, text field becomes disabled and hidden.
"""
self.text_value_input = TextInput(
background_active=f"{images_directory}/border_icon.png",
background_disabled_normal=f"{images_directory}/border_icon.png",
multiline=False,
border=(0, 0, 0, 0),
halign="right",
input_filter="int",
font_size=30,
cursor_color=(0, 0, 0, 1),
foreground_color=(0, 0, 0, 1),
opacity=(0),
disabled=True,
size_hint=(.6, .2),
padding=(10, 22),
on_text_validate=self.custom_text_validate)
self.text_inputs_list.append(self.text_value_input)
self.text_value_input.my_id = index
self.text_value_input.bind(focus=self.unfocus_text_input)
"""Custom configuration of a layout to fit widgets correctly."""
self.ids.SW1.size_hint = (.55,.8)
self.ids.LY4.cols = 7
layer.add_widget(self.component_label_2)
layer.add_widget(self.amount_input_2)
layer.add_widget(self.new_amount)
layer.add_widget(self.minus_sign_2)
layer.add_widget(self.plus_sign_2)
layer.add_widget(self.value_input_button)
layer.add_widget(self.text_value_input)
iteration_count = 0
for divider in range(7):
iteration_count += 1
self.divider_line_2 = Image(
source=f"{images_directory}/divider_3.png",
size_hint_y=None,
size_hint_x=.1,
height=2,
width=2,
fit_mode="fill")
layer.add_widget(self.divider_line_2)
if iteration_count > 6:
self.divider_line_2.opacity = (0)
"""
In the second condition, we create a database into 4 columns to present a general overview of the warehouse stock.
We are also setting the "lock_button.disabled" to True. Meaning once we are in this overview, we are not able to modify the values.
"""
elif self.notebook_button.background_normal and self.notebook_button.background_down == f"{images_directory}/notebook_opened.png":
self.lock_button.disabled = True
iteration_count = 0
""" Interface is in a notebook overview. """
for component, amount in zip(self.df["Komponent"], self.df["Mnozstvi"]):
self.component_label= Label(
text=component,
size_hint=(.8,1),
font_size=17)
self.amount_input_3 = Label(
text=str(amount),
size_hint=(.8,1),
font_size=22,)
""" Once a certain amount of components is bellow critical number, value will now be displayed in red color."""
if amount < 11:
self.amount_input_3.color=(1,0,0,1)
layer.add_widget(self.component_label)
layer.add_widget(self.amount_input_3)
iteration_count += 1
"""
In order to preserve a correct division by "divider_line". We need to add a new variable "iteration_count".
This divider is created only after a for loop has iterated twice, leading to a desired result, which is:
"Component - Amount , Component - Amount" and all of that is then underlined with a divider.
"""
if iteration_count == 2:
for divider in range(4):
self.divider_line = Image(
source=f"{images_directory}/divider_2.png",
size_hint_y=None,
height=10,
width=5)
layer.add_widget(self.divider_line)
iteration_count = 0
"""
Method that allows us to change values of a "new_amount" Label. Each widget has its index as a 'Primary key',
to help us navigate between the iterations and control which component's value we want to change.
We are directly accessing an item in the "new_amount_list" which has been appended from the "new_amount" Label.
With the use of "index" variable we are able to connect the specific item in the list to a widget created by the loop.
Change of values in the "self.new_amount.text" Label is done by modifying the list items, not by accessing the "self.new_amount" widget directly.
"""
def increment_value(self, index_id):
index_plus = index_id.my_id
value = "plus"
if self.new_amount_list[index_plus].text == "":
self.new_amount_list[index_plus].text = str(0)
self.new_amount_list[index_plus].text = str(int(self.new_amount_list[index_plus].text) + 1)
self.final_count[index_plus] += 1
self.set_label_color(index_plus)
def decrement_value(self, index_id):
index_minus = index_id.my_id
value = "minus"
if self.new_amount_list[index_minus].text == "":
self.new_amount_list[index_minus].text = str(0)
self.new_amount_list[index_minus].text = str(int(self.new_amount_list[index_minus].text) - 1)
self.final_count[index_minus] -= 1
self.set_label_color(index_minus)
"""Method that allows us to display and immediately focus a new text input field."""
def button_to_text(self, index_id):
button_index = index_id.my_id
self.text_inputs_list[button_index].disabled = False
self.text_inputs_list[button_index].opacity = 1
self.text_inputs_list[button_index].focused = True
self.text_inputs_list[button_index].text = ""
"""
After pressing enter, while text input widget is active, this method will transfer the value into the "new_amount" widget.
To maintain proper functionality, if the "new_amount" value has not yet been modified (value = 0), new value will now be now added.
Although if user decides to correct his previous value, the "new_amount" will be reset and equal to zero.
"""
def custom_text_validate(self, index_id):
value = "custom"
self.text_index = index_id.my_id
self.final_count[self.text_index] = 0
self.text_inputs_list[self.text_index].disabled = True
self.text_inputs_list[self.text_index].focused = False
self.text_inputs_list[self.text_index].opacity = 0
self.user_amount = self.text_inputs_list[self.text_index].text
self.new_amount_list[self.text_index].text = str(self.user_amount)
if self.text_inputs_list[self.text_index].text == "":
self.user_amount = 0
self.set_label_color(self.text_index)
self.count_values(self.text_index, value)
"""
To prevent the user to make changes in the database while still 'unlocked interface', we created an empty list "self.final_count".
We want all the changes to the database occur only after we exit from the unlocked interface and enter into a locked interface (by clicking the lock_icon).
Instead of making changes directly to the core database, we modify values inside the "self.final_count".
Once user clicks a "plus_sign", "minus_sign" or "text_input_button", the list's components are updated on a corresponding index.
"""
def count_values(self, index, value):
if value == "plus":
self.final_count[index] += 1
elif value == "minus":
self.final_count[index] -= 1
elif value == "custom":
self.final_count[index] += int(self.user_amount)
""" Method which allows us to unfocus and disable a text input field as soon as it's not focused."""
def unfocus_text_input(self, index_id, value):
index = index_id.my_id
if not value:
self.text_inputs_list[index].disabled = True
self.text_inputs_list[index].opacity = 0
"""
Method that checks for the amount of components. As soon as value falls bellow 0, the color of the value turns red.
Other way around if the value is above zero, the color turns green.
"""
def set_label_color(self, index):
if self.new_amount_list[index].text > str(0):
self.new_amount_list[index].color = (0, 1, 0, 1)
elif self.new_amount_list[index].text < str(0):
self.new_amount_list[index].color = (1, 0, 0, 1)
else:
self.new_amount_list[index].color = (1, 1, 1, 0)
"""
Method that is fired whenever the user clicks on "lock_icon" button to store the changed data into the database.
We loop trough the CSV file and merge the pending changes in "self.final_count" list into the default database.
"""
def save_data(self, final_count):
for index, amount in enumerate(self.df["Mnozstvi"]):
self.df.at[index, "Mnozstvi"] += self.final_count[index]
self.df.to_csv(csv_file_path, encoding='utf-8', index=False)
""" Function that cleans the page of all the widgets, allowing us to return to the page without overlapping widgets."""
def on_leave(self, *args):
self.ids.LY4.clear_widgets()
self.ids.LY5.clear_widgets()
self.notebook_button.size_hint = (.04, .1)
self.notebook_button.background_normal = f"{images_directory}/notebook_closed.png"
self.notebook_button.background_down = f"{images_directory}/notebook_closed.png"
self.lock_button.background_normal = f"{images_directory}/lock_icon.png"
self.lock_button.background_down = f"{images_directory}/unlocked_icon.png"
self.notebook_button.disabled = False
class ImportScreen(Screen, Transition):
pdf_paths_list = ListProperty([])
component_file_text = StringProperty("")
component_path = None
def __init__(self,**kwargs):
super(ImportScreen, self).__init__(**kwargs)
self.anim = Animation(opacity=1, duration=.1)
self.anim_fade = Animation(opacity=0, duration=1)
self.home_button = HoverButton(
background_normal=f"{images_directory}/home_button_icon.png",
background_down=f"{images_directory}/home_button_icon.png",
size_hint = (.06, .095),
border=(0, 0, 0, 0),
pos_hint = {"center_x": .94, "center_y": .09})
self.home_button.bind(on_enter=self.home_button.on_button_hover, on_leave=self.home_button.on_button_hover_exit)
self.home_button.bind(on_release=self.home_page)
self.ids.LY6.add_widget(self.home_button)
self.select_pdf = HoverButton(
background_normal=f"{images_directory}/select_pdf_button.png",
background_down=f"{images_directory}/select_pdf_button.png",
border=(0, 0, 0, 0),
size_hint=(1,.1),
pos_hint={"center_x": .55, "center_y": .75})
self.select_pdf.bind(on_enter=self.select_pdf.on_button_hover, on_leave=self.select_pdf.on_button_hover_exit)
self.select_pdf.bind(on_release=self.choose_pdf_file)
self.select_pdf.bind(on_release=lambda clear_list: self.pdf_paths_list.clear())
self.select_pdf.bind(on_release=lambda clear_layout: self.ids.LY55.clear_widgets())
self.ids.LY6.add_widget(self.select_pdf)
self.select_component = HoverButton(
background_normal=f"{images_directory}/select_component_button.png",
background_down=f"{images_directory}/select_component_button.png",
border=(0, 0, 0, 0),
size_hint=(1, .1),
pos_hint={"center_x": .55, "center_y": .65})
self.select_component.bind(on_enter=self.select_component.on_button_hover, on_leave=self.select_component.on_button_hover_exit)
self.select_component.bind(on_release=self.choose_component_file)
self.ids.LY6.add_widget(self.select_component)
self.merge_pdf = HoverButton(
background_normal=f"{images_directory}/pdf_merge_icon.png",
background_down=f"{images_directory}/pdf_merge_icon_down.png",
background_disabled_normal=f"{images_directory}/pdf_merge_icon_disabled.png",
border=(0, 0, 0, 0),
size_hint=(.06, .09),
pos_hint={"center_x": .94, "center_y": .9})
self.merge_pdf.bind(on_enter=self.merge_pdf.on_button_hover, on_leave=self.merge_pdf.on_button_hover_exit)
self.merge_pdf.bind(on_release=self.merge_pdf_files)
self.ids.LY6.add_widget(self.merge_pdf)
self.merge_img = Image(
source=f"{images_directory}/merge_success_img.png",
pos_hint={"center_x": .85, "center_y": .6},
size_hint=(.3, .3),
border=(0, 0, 0, 0),
opacity=0)
self.ids.LY6.add_widget(self.merge_img)
self.error_pdf_img = Image(
source=f"{images_directory}/error_pdf.png",
pos_hint={"center_x": .28, "center_y": .8},
size_hint=(.3, .3),
allow_stretch=True,
border=(0, 0, 0, 0),
opacity=0)
self.ids.LY6.add_widget(self.error_pdf_img)
self.error_component_img = Image(
source=f"{images_directory}/error_component.png",
pos_hint={"center_x": .28, "center_y": .49},
size_hint=(.3, .3),
allow_stretch=True,
border=(0, 0, 0, 0),
opacity=0)
self.ids.LY6.add_widget(self.error_component_img)
"""
Two methods bellow allow us to choose .pdf files that will later be merged together.
Once user selects a .pdf file, lambda function is fired to call a "handle_selection" method.
Within the lambda function, two arguments are passed: "selection" and "method".
"Selection" is the selected file, while "method" keyword is the deciding argument.
With use of the "method" keyword, we are able to differentiate which of the two methods called "handle_selection".
User is able to select multiple .pdf files which are then appended to 'pdf_paths_list',
from which Labels with respective name are created.
"""
def choose_pdf_file(self, instance):
self.pdf_path = filechooser.open_file(
title="Select a file ...",
multiple=True,
filters=[("PDF Files", "*.pdf")],
on_selection=lambda selection: self.handle_selection(selection, method="pdf"))
if self.pdf_path:
for path in self.pdf_path:
self.pdf_paths_list.append(path)
self.anim = Animation(opacity=1, duration=1)
self.anim.start(self.ids.merge_docs)
pos_y = .42
for pdf in self.pdf_paths_list:
self.pdf_text_label = Label(
text=os.path.basename(pdf),
pos_hint={"center_x": .29, "center_y": pos_y},
size_hint=(.2, .2),
font_size=22)
self.ids.LY55.add_widget(self.pdf_text_label)
pos_y -= .03
def choose_component_file(self, instance):
self.component_path = filechooser.open_file(
title="Select a file ...",
filters=[("PDF Files", "*.pdf")],
on_selection=lambda selection: self.handle_selection(selection, method="component"))
"""
After "handle_selection" method is called, it recieves an keyword argument called "method".
Based on the recieved keyword arguments from the two methods above, it differentiates between the IF conditions and
transfers the "self.file_name" into the StringProperty accordingly. Allowing the Label to appear with the correct
selected file's name.
"""
def handle_selection(self, selection, method):
if selection:
# Selecting only the file name, seperating the rest of the path.
self.file_name = os.path.basename(selection[0])
if method == "pdf":
pass
if method == "component":
self.component_file_text = self.file_name
def merge_pdf_files(self, instance):
merger = PdfMerger()
if self.component_path is not None:
self.pdf_component = self.component_path
else:
""" If user closes the file browser window without selecting a file, pdf_component is no longer None, but becomes an empty list instead."""
self.pdf_component = None
if self.pdf_paths_list != [] and self.pdf_component is not None and self.pdf_component != []:
for pdf in self.pdf_paths_list:
merger.append(pdf)
merger.append(self.pdf_component[0])
user_home = os.path.expanduser("~")
desktop_path = os.path.join(user_home, "Desktop")
output_pdf_path = os.path.join(desktop_path, f"Komplet - {self.component_file_text}")
merger.write(output_pdf_path)
merger.close()
self.anim.start(self.merge_img)
self.anim.bind(on_complete=self.stop_anim)
if self.pdf_paths_list == []:
self.anim.start(self.error_pdf_img)
self.anim.bind(on_complete=self.stop_anim)
if self.pdf_component is None or self.pdf_component == []:
self.anim.start(self.error_component_img)
self.anim.bind(on_complete=self.stop_anim)
def stop_anim(self, dt, instance):
Clock.schedule_once(lambda dt: self.anim_fade.start(self.error_component_img),1)
Clock.schedule_once(lambda dt: self.anim_fade.start(self.error_pdf_img),1)
Clock.schedule_once(lambda dt: self.anim_fade.start(self.merge_img), 3)
def on_leave(self, *args):
self.ids.LY55.clear_widgets()
self.pdf_paths_list.clear()
self.component_file_text = ""
self.component_path = None
class ExportScreen(Screen, Transition):
"""
'data_store' is a variable that helps us control the data when screens are being switched.
When set to 'False', all of the data generated with button widgets will be deleted when we leave the ExportScreen.
When set to 'True', relevant data for the user will be kept in order to access them when the user decides to
go back to the ExportScreen to modify his selection.
"""
data_store = False
def __init__(self, **kwargs):
super(ExportScreen, self).__init__(**kwargs)
self.anim = Animation(opacity=1, duration=.2)
self.anim_fade = Animation(opacity=0, duration=1)
self.error_img = Image(
source=f"{images_directory}/error_number.png",
allow_stretch=True,
pos_hint={"center_x": .68, "center_y": .88},
size_hint=(.2, .2),
opacity=0)
self.ids.LY8.add_widget(self.error_img)
with open(csv_file_path, 'rb') as f:
contents = f.read()
self.df = pd.read_csv(io.BytesIO(contents), encoding='utf-8')
self.transfered_dict = {}
self.home_button = HoverButton(
background_normal=f"{images_directory}/home_button_icon.png",
background_down=f"{images_directory}/home_button_icon.png",
size_hint=(.06, .095),
border=(0, 0, 0, 0),
pos_hint={"center_x": .94, "center_y": .09})
self.home_button.bind(on_enter=self.home_button.on_button_hover, on_leave=self.home_button.on_button_hover_exit)
self.home_button.bind(on_release=self.home_page)
self.home_button.bind(on_release=lambda button: (setattr(ExportScreen,"data_store",False)))
self.ids.LY8.add_widget(self.home_button)
self.next_page = HoverButton(
background_normal=f"{images_directory}/closed_box.png",
background_down=f"{images_directory}/closed_box.png",
size_hint=(.07, .125),
border=(0, 0, 0, 0),
pos_hint={"center_x": .94, "center_y": .9})
self.ids.LY8.add_widget(self.next_page)
self.next_page.bind(on_enter=self.next_page.on_button_hover, on_leave=self.next_page.on_button_hover_exit)
self.next_page.bind(on_release=lambda page: self.transition("Sixth"))
self.next_page.bind(on_release=lambda button: (setattr(ExportScreen,"data_store", True)))
self.next_page.bind(on_press=lambda dict: FinalExportScreen().recieve_dictionary(self.transfered_dict))
self.next_page.bind(on_press=self.text_lose_focus)
def on_pre_enter(self, *args):
if not ExportScreen.data_store:
self.tuple_list = []
"""Placeholder list for items in Components_data.csv"""
self.children_list = ["x" for placeholder in range(37)]
for index, component in enumerate(self.df["Komponent"]):
self.component_label = Label(
text=component,
font_size=22,
size_hint=(1,None),
outline_color=(0, 0, 0, 1),
outline_width=2)
self.arrow_button = HoverButton(
background_normal=f"{images_directory}/arrow_pick.png",
background_down=f"{images_directory}/arrow_pick.png",
background_disabled_normal=f"{images_directory}/arrow_pick_disabled.png",
size_hint=(.2, .05),
border=(0, 0, 0, 0))
self.arrow_button.my_id = index
self.arrow_button.bind(on_enter=self.arrow_button.on_button_hover,on_leave=self.arrow_button.on_button_hover_exit)
self.arrow_button.bind(on_release=self.transfer_component)
"""'tuple_list' is storing our widgets and they are organized in a touples for later access."""
tuple_data = (self.component_label, self.arrow_button)
self.tuple_list.append(tuple_data)
self.ids.LY9.add_widget(self.component_label)
self.ids.LY9.add_widget(self.arrow_button)
else:
pass
"""
Each press of a "arrow_button" will now generate 3 new widgets and corresponding "component_label", "arrow_button" will become disabled.
All of the widgets are related to each other with the use of "component_index". We are using the "children_list" which is filled with appropriate amount
of placeholders to maintain the correct number of indicies.
"""
def transfer_component(self, index_id):
"""Each addition of a widget in the LY10 layout will increase the "size_hint" to match the layout's structure."""
update_size_hint = lambda: (0.4, self.ids.LY10.size_hint[1] + .12)
self.ids.LY10.size_hint = update_size_hint()
self.component_index = index_id.my_id
self.component_text = self.tuple_list[self.component_index][0]
self.component_button = self.tuple_list[self.component_index][1]
self.component_text.disabled = True
self.component_button.disabled = True
self.transfered_component = Label(
text=self.component_text.text,
markup=True,
font_size=25,
outline_color=(0, 0, 0, 1),
outline_width=2)
self.ids.LY10.add_widget(self.transfered_component)
"""
Insted of using on_text_validate, usage of 'focus' function allows us to store the widgets
in 'self.transfered_dict' without the need to press enter on every instance. Simply clicking out of
focus of the text input field will perform equally.
"""
self.amount_text = TextInput(
background_active=f"{images_directory}/border_icon_2.png",
background_normal=f"{images_directory}/border_icon_2.png",
multiline=False,
input_filter="int",
border=(0, 0, 0, 0),
cursor_color=(0, 0, 0, 1),
foreground_color=(0, 0, 0, 1),
font_size=30,
padding=(20,18,0,0),
halign="center",
size_hint=(.3,.4))
self.amount_text.bind(focus=self.text_input_validate)
self.amount_text.my_id = self.component_index
self.ids.LY10.add_widget(self.amount_text)
self.unselect_button = HoverButton(
background_normal=f"{images_directory}/back_arrow_icon.png",
background_down=f"{images_directory}/back_arrow_icon.png",
border=(0, 0, 0, 0),
size_hint=(.1,.02))
self.unselect_button.my_id = self.component_index
self.unselect_button.bind(on_release=self.clear_component)
self.unselect_button.bind(on_enter=self.unselect_button.on_button_hover, on_leave=self.unselect_button.on_button_hover_exit)
self.ids.LY10.add_widget(self.unselect_button)
"""Each placeholder in "children_list" at the specified index is being replaced with tuple consisting of (component, text, button)."""
self.children_list[self.component_index] = (self.transfered_component,self.amount_text,self.unselect_button)
"""
With "clear_component" method we are removing corresponding widgets. Each removal of a widget updates the GridLayout and decreases
the height attribute of size_hint by .12.
"""
def clear_component(self, index_id):
"""Each removal of a widget in the LY10 layout will decrease the "size_hint" to match the layout's structure."""
update_size_hint = lambda: (0.4, self.ids.LY10.size_hint[1] - .12)
self.ids.LY10.size_hint = update_size_hint()
self.component_index = index_id.my_id
"""In order to remove widgets correctly, we need find the correct index in the "children_list first and only then unpack the tuple."""
remove_children_0 = self.children_list[self.component_index][0]
remove_children_1 = self.children_list[self.component_index][1]
remove_children_2 = self.children_list[self.component_index][2]
"""We set the disable property of a "arrow_button" and "component_label" back to False so they can available again for use."""
self.tuple_list[self.component_index][0].disabled = False
self.tuple_list[self.component_index][1].disabled = False
self.ids.LY10.remove_widget(remove_children_0)
self.ids.LY10.remove_widget(remove_children_1)
self.ids.LY10.remove_widget(remove_children_2)
"""
Condition that checks if 'component_name' is in 'self.transfered_dict'.
If true, component will be removed from the dictionary along with the corresponding value.
"""
if self.children_list[self.component_index][0].text in self.transfered_dict:
self.transfered_dict.pop(self.children_list[self.component_index][0].text)
"""
Method that validates the text_field input. After text_input recieves a valid value, component aswell as the value are transfered