4
4
import backend
5
5
backend .connect_database ()
6
6
employee_data = None
7
+ # Page Constants (for reference)
8
+ HOME_PAGE = 0
9
+ ADMIN_PAGE = 1
10
+ EMPLOYEE_PAGE = 2
11
+ ADMIN_MENU_PAGE = 3
12
+ ADD_EMPLOYEE_PAGE = 4
13
+ UPDATE_EMPLOYEE_PAGE1 = 5
14
+ UPDATE_EMPLOYEE_PAGE2 = 6
15
+ EMPLOYEE_LIST_PAGE = 7
16
+ ADMIN_TOTAL_MONEY = 8
17
+ # -------------------------------------------------------------------------------------------------------------
18
+ # === Reusable UI Component Functions ===
19
+ # -------------------------------------------------------------------------------------------------------------
20
+
7
21
def create_styled_frame (parent , min_size = None , style = "" ):
8
22
"""Create a styled QFrame with optional minimum size and custom style."""
9
23
frame = QtWidgets .QFrame (parent )
@@ -133,7 +147,23 @@ def on_reject():
133
147
button_box .rejected .connect (on_reject )
134
148
135
149
dialog .exec_ ()
150
+ # -------------------------------------------------------------------------------------------------------------
151
+ # === Page Creation Functions ==
152
+ # -------------------------------------------------------------------------------------------------------------
153
+ def create_page_with_header (parent , title_text ):
154
+ """Create a page with a styled header and return the page + main layout."""
155
+ page = QtWidgets .QWidget (parent )
156
+ main_layout = QtWidgets .QVBoxLayout (page )
157
+ main_layout .setContentsMargins (20 , 20 , 20 , 20 )
158
+ main_layout .setSpacing (20 )
136
159
160
+ header_frame = create_styled_frame (page , style = "background-color: #ffffff; border-radius: 10px; padding: 10px;" )
161
+ header_layout = QtWidgets .QVBoxLayout (header_frame )
162
+ title_label = create_styled_label (header_frame , title_text , font_size = 30 )
163
+ header_layout .addWidget (title_label , 0 , QtCore .Qt .AlignHCenter | QtCore .Qt .AlignTop )
164
+
165
+ main_layout .addWidget (header_frame , 0 , QtCore .Qt .AlignTop )
166
+ return page , main_layout
137
167
def get_employee_name (parent , name_field_text = "Enter Employee Name" ):
138
168
page , main_layout = create_page_with_header (parent , "Employee Data Update" )
139
169
@@ -170,7 +200,7 @@ def on_search_button_clicked():
170
200
cur .execute ("SELECT * FROM staff WHERE name = ?" , (entered_name ,))
171
201
employee_data = cur .fetchone ()
172
202
print (f"Employee Data: { employee_data } " )
173
- parent .setCurrentIndex (6 )
203
+ parent .setCurrentIndex (UPDATE_EMPLOYEE_PAGE2 )
174
204
175
205
# if employee_data:
176
206
# QtWidgets.QMessageBox.information(parent, "Employee Found",
@@ -191,7 +221,7 @@ def on_search_button_clicked():
191
221
192
222
def create_login_page (parent ,title , name_field_text = "Name :" , password_field_text = "Password :" , submit_text = "Submit" ,):
193
223
"""Create a login page with a title, name and password fields, and a submit button."""
194
- page , main_layout = create_page_with_header (parent , "Admin Menu" )
224
+ page , main_layout = create_page_with_header (parent , title )
195
225
196
226
# Content frame
197
227
content_frame = create_styled_frame (page )
@@ -293,21 +323,6 @@ def create_home_page(parent, on_admin_clicked, on_employee_clicked, on_exit_clic
293
323
294
324
return page
295
325
296
- def create_page_with_header (parent , title_text ):
297
- """Create a page with a styled header and return the page + main layout."""
298
- page = QtWidgets .QWidget (parent )
299
- main_layout = QtWidgets .QVBoxLayout (page )
300
- main_layout .setContentsMargins (20 , 20 , 20 , 20 )
301
- main_layout .setSpacing (20 )
302
-
303
- header_frame = create_styled_frame (page , style = "background-color: #ffffff; border-radius: 10px; padding: 10px;" )
304
- header_layout = QtWidgets .QVBoxLayout (header_frame )
305
- title_label = create_styled_label (header_frame , title_text , font_size = 30 )
306
- header_layout .addWidget (title_label , 0 , QtCore .Qt .AlignHCenter | QtCore .Qt .AlignTop )
307
-
308
- main_layout .addWidget (header_frame , 0 , QtCore .Qt .AlignTop )
309
- return page , main_layout
310
-
311
326
def create_admin_menu_page (parent ):
312
327
page , main_layout = create_page_with_header (parent , "Admin Menu" )
313
328
@@ -392,7 +407,7 @@ def create_add_employee_page(parent, title, submit_text="Submit",update_btn:bool
392
407
background-color: #5a6268;
393
408
}
394
409
""" )
395
- back_btn .clicked .connect (lambda : parent .setCurrentIndex (3 ))
410
+ back_btn .clicked .connect (lambda : parent .setCurrentIndex (ADMIN_MENU_PAGE ))
396
411
main_layout .addWidget (back_btn , 0 ,alignment = QtCore .Qt .AlignLeft )
397
412
if update_btn :
398
413
return page , name_edit , password_edit , salary_edit , position_edit , update_button
@@ -471,7 +486,7 @@ def show_employee_list_page(parent, title):
471
486
background-color: #5a6268;
472
487
}
473
488
""" )
474
- back_button .clicked .connect (lambda : parent .setCurrentIndex (3 ))
489
+ back_button .clicked .connect (lambda : parent .setCurrentIndex (ADMIN_MENU_PAGE ))
475
490
476
491
content_layout .addWidget (table_frame )
477
492
main_layout .addWidget (back_button , alignment = QtCore .Qt .AlignLeft )
@@ -505,10 +520,14 @@ def show_total_money(parent, title):
505
520
background-color: #5a6268;
506
521
}
507
522
""" )
508
- back_button .clicked .connect (lambda : parent .setCurrentIndex (3 ))
523
+ back_button .clicked .connect (lambda : parent .setCurrentIndex (ADMIN_MENU_PAGE ))
509
524
content_layout .addWidget (back_button , alignment = QtCore .Qt .AlignCenter )
510
525
main_layout .addWidget (content_frame )
511
526
return page
527
+
528
+ # -------------------------------------------------------------------------------------------------------------
529
+ # === Main Window Setup ===
530
+ # -------------------------------------------------------------------------------------------------------------
512
531
513
532
def setup_main_window (main_window ):
514
533
"""Set up the main window with a stacked widget containing home, admin, and employee pages."""
@@ -523,10 +542,10 @@ def setup_main_window(main_window):
523
542
524
543
# Create pages
525
544
def switch_to_admin ():
526
- stacked_widget .setCurrentIndex (1 )
545
+ stacked_widget .setCurrentIndex (ADMIN_PAGE )
527
546
528
547
def switch_to_employee ():
529
- stacked_widget .setCurrentIndex (2 )
548
+ stacked_widget .setCurrentIndex (EMPLOYEE_PAGE )
530
549
531
550
def exit_app ():
532
551
QtWidgets .QApplication .quit ()
@@ -537,7 +556,7 @@ def admin_login_menu_page(name, password):
537
556
success = backend .check_admin (name , password )
538
557
if success :
539
558
QtWidgets .QMessageBox .information (stacked_widget , "Login Successful" , f"Welcome, { name } !" )
540
- stacked_widget .setCurrentIndex (3 )
559
+ stacked_widget .setCurrentIndex (ADMIN_MENU_PAGE )
541
560
else :
542
561
QtWidgets .QMessageBox .warning (stacked_widget , "Login Failed" , "Incorrect name or password." )
543
562
except Exception as e :
@@ -606,37 +625,37 @@ def update_employee_data(name, password, salary, position, name_to_update):
606
625
stacked_widget
607
626
)
608
627
609
- add_button .clicked .connect (lambda : stacked_widget .setCurrentIndex (4 ))
610
- update_button .clicked .connect (lambda : stacked_widget .setCurrentIndex (5 ))
611
- list_button .clicked .connect (lambda : stacked_widget .setCurrentIndex (7 ))
612
- back_button .clicked .connect (lambda : stacked_widget .setCurrentIndex (0 ))
613
- money_button .clicked .connect (lambda : stacked_widget .setCurrentIndex (8 ))
628
+ add_button .clicked .connect (lambda : stacked_widget .setCurrentIndex (ADD_EMPLOYEE_PAGE ))
629
+ update_button .clicked .connect (lambda : stacked_widget .setCurrentIndex (UPDATE_EMPLOYEE_PAGE ))
630
+ list_button .clicked .connect (lambda : stacked_widget .setCurrentIndex (EMPLOYEE_LIST_PAGE ))
631
+ back_button .clicked .connect (lambda : stacked_widget .setCurrentIndex (HOME_PAGE ))
632
+ money_button .clicked .connect (lambda : stacked_widget .setCurrentIndex (ADMIN_TOTAL_MONEY ))
614
633
# Create Add Employee Page
615
634
add_employee_page , emp_name , emp_password , emp_salary , emp_position , emp_submit = create_add_employee_page (
616
635
stacked_widget ,
617
636
title = "Add Employee"
618
637
)
619
638
620
639
# Update Employee Page
621
- update_employee_page1 = get_employee_name (stacked_widget )
640
+ u_employee_page1 = get_employee_name (stacked_widget )
622
641
# apply the update_employee_data function to the submit button
623
642
624
- update_employee_page2 , update_employee_name , update_employee_password , update_employee_salary , update_employee_position , update_employee_update = create_add_employee_page (stacked_widget ,"Update Employee Details" ,update_btn = True )
643
+ u_employee_page2 , u_employee_name , u_employee_password , u_employee_salary , u_employee_position , u_employee_update = create_add_employee_page (stacked_widget ,"Update Employee Details" ,update_btn = True )
625
644
def populate_employee_data ():
626
645
global employee_data
627
646
if employee_data :
628
647
print ("employee_data is not None" )
629
- update_employee_name .setText (str (employee_data [0 ])) # Name
630
- update_employee_password .setText (str (employee_data [1 ])) # Password
631
- update_employee_salary .setText (str (employee_data [2 ])) # Salary
632
- update_employee_position .setText (str (employee_data [3 ])) # Position
648
+ u_employee_name .setText (str (employee_data [0 ])) # Name
649
+ u_employee_password .setText (str (employee_data [1 ])) # Password
650
+ u_employee_salary .setText (str (employee_data [2 ])) # Salary
651
+ u_employee_position .setText (str (employee_data [3 ])) # Position
633
652
else :
634
653
# Clear fields if no employee data is available
635
654
print ("employee_data is None" )
636
- update_employee_name .clear ()
637
- update_employee_password .clear ()
638
- update_employee_salary .clear ()
639
- update_employee_position .clear ()
655
+ u_employee_name .clear ()
656
+ u_employee_password .clear ()
657
+ u_employee_salary .clear ()
658
+ u_employee_position .clear ()
640
659
QtWidgets .QMessageBox .warning (stacked_widget , "No Data" , "No employee data available to display." )
641
660
def on_page_changed (index ):
642
661
if index == 6 : # update_employee_page2 is at index 6
@@ -668,12 +687,12 @@ def update_employee_data(name, password, salary, position, name_to_update):
668
687
show_popup_message (stacked_widget , "Employee updated successfully." , 3 , False )
669
688
except Exception as e :
670
689
show_popup_message (stacked_widget , f"Error updating employee: { str (e )} " , 5 )
671
- update_employee_update .clicked .connect (
690
+ u_employee_update .clicked .connect (
672
691
lambda : update_employee_data (
673
- update_employee_name .text ().strip (),
674
- update_employee_password .text ().strip (),
675
- update_employee_salary .text ().strip (),
676
- update_employee_position .text ().strip (),
692
+ u_employee_name .text ().strip (),
693
+ u_employee_password .text ().strip (),
694
+ u_employee_salary .text ().strip (),
695
+ u_employee_position .text ().strip (),
677
696
employee_data [0 ] if employee_data else ""
678
697
)
679
698
)
@@ -703,8 +722,8 @@ def update_employee_data(name, password, salary, position, name_to_update):
703
722
stacked_widget .addWidget (employee_page )#2
704
723
stacked_widget .addWidget (admin_menu_page )#3
705
724
stacked_widget .addWidget (add_employee_page )#4
706
- stacked_widget .addWidget (update_employee_page1 )#5
707
- stacked_widget .addWidget (update_employee_page2 )#6
725
+ stacked_widget .addWidget (u_employee_page1 )#5
726
+ stacked_widget .addWidget (u_employee_page2 )#6
708
727
stacked_widget .addWidget (employee_list_page )#7
709
728
stacked_widget .addWidget (admin_total_money )#8
710
729
@@ -714,7 +733,7 @@ def update_employee_data(name, password, salary, position, name_to_update):
714
733
main_window .setCentralWidget (central_widget )
715
734
716
735
# Set initial page
717
- stacked_widget .setCurrentIndex (3 )
736
+ stacked_widget .setCurrentIndex (EMPLOYEE_PAGE )
718
737
719
738
return stacked_widget , {
720
739
"admin_name" : admin_name ,
@@ -736,6 +755,7 @@ def main():
736
755
737
756
main_window .show ()
738
757
sys .exit (app .exec_ ())
758
+ # -------------------------------------------------------------------------------------------------------------
739
759
740
760
if __name__ == "__main__" :
741
761
main ()
0 commit comments