Guest User

Main.py file

a guest
Dec 7th, 2021
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.25 KB | None | 0 0
  1. ## IMPORTS
  2. import sys
  3. import os
  4. from PyQt5.sip import delete
  5. import PySide2
  6. import mysql.connector as mc
  7. from qt_material import *
  8. from PyQt5 import QtWidgets, uic
  9. from add_section_page import Ui_Add_Section_Window
  10. #####################################
  11.  
  12. # Import GUI File
  13. from ui_interface import *
  14. ######################################
  15.  
  16. # Main Window Class
  17. class MainWindow(QMainWindow):
  18.     def __init__(self):
  19.         QMainWindow.__init__(self)
  20.         self.ui = Ui_MainWindow()
  21.         self.ui.setupUi(self)
  22.         try:
  23.             self.DB = mc.connect(host="localhost", user="root", password="", database="timetable_manager")
  24.         except mc.Error as e:
  25.             print("Error")
  26.  
  27.         # Load Style Sheet
  28.         apply_stylesheet(app, theme="dark_cyan.xml")
  29.  
  30.         # Remove Window Title Bar
  31.         self.setWindowFlags(PySide2.QtCore.Qt.FramelessWindowHint)
  32.  
  33.         # Set main background to transparent
  34.         self.setAttribute(PySide2.QtCore.Qt.WA_TranslucentBackground)
  35.  
  36.         # Shadow Style Effect
  37.         self.shadow = QGraphicsDropShadowEffect(self)
  38.         self.shadow.setBlurRadius(50)
  39.         self.shadow.setXOffset(0)
  40.         self.shadow.setYOffset(0)
  41.         self.shadow.setColor(QColor(0, 92, 157, 550))
  42.        
  43.         # Apply shadow to central widget
  44.         self.ui.centralwidget.setGraphicsEffect(self.shadow)
  45.  
  46.         self.setWindowIcon(PySide2.QtGui.QIcon(":/icons/airplay.svg"))
  47.         # Set Window Title
  48.         self.setWindowTitle("Timetable Manager")
  49.  
  50.         # Window Size grip to resize window
  51.         QSizeGrip(self.ui.size_grip)
  52.  
  53.         # Minimize Window
  54.         self.ui.minimize_window_button.clicked.connect(lambda: self.showMinimized())
  55.  
  56.         # Close Window
  57.         self.ui.close_window_button.clicked.connect(lambda: self.close())
  58.  
  59.         # Maximize Window
  60.         self.ui.restore_window_button.clicked.connect(lambda: self.restore_or_maximize_window())
  61.        
  62.         # Navigate to Section Page
  63.         self.ui.section_menu_button.clicked.connect(lambda: self.navigate_to_section_page())
  64.         # Navigate to Course Page
  65.         self.ui.course_menu_button.clicked.connect(lambda: self.navigate_to_course_page())
  66.         self.ui.add_section_button.clicked.connect(self.executeAddSectionPage)
  67.         self.show()
  68.  
  69.     def navigate_to_section_page(self):
  70.         self.ui.stackedWidget.setCurrentWidget(self.ui.sections_page)
  71.         self.get_section_data()
  72.  
  73.     def on_section_table_click(self): # Whenever the Delete button gets clicked for section
  74.         selected = self.ui.section_table.selectionModel().selectedIndexes()[0]
  75.         msg = QMessageBox()
  76.         msg.setIcon(QMessageBox.Warning)
  77.         msg.setText("Are you sure you want to delete the selected Section?")
  78.         msg.setStandardButtons(QMessageBox.Yes | QMessageBox.Cancel)
  79.         if (msg.exec_() == 16384): # Means "YES"
  80.             mycursor = self.DB.cursor()
  81.             sql = "Delete from tbl_section where section_id = %s"
  82.             val = (int(self.ui.section_table.item(selected.row(), 0).text()),)
  83.             mycursor.execute(sql, val)
  84.             self.DB.commit()
  85.             self.ui.section_table.clear()
  86.             self.get_section_data()
  87.             print("Deleted")
  88.         else:
  89.             print("Not Deleted")
  90.  
  91.     def executeAddSectionPage(self):
  92.         self.window = QtWidgets.QMainWindow()
  93.         self.add_section_form = Ui_Add_Section_Window()
  94.         self.add_section_form.setupUi(self.window)
  95.         self.window.show()
  96.         self.add_section_form.section_add_form_button.clicked.connect(self.insert_section_info)
  97.  
  98.        
  99.     def insert_section_info(self):
  100.         id = self.add_section_form.section_id_add.text()
  101.         name = self.add_section_form.section_name_add.text()
  102.         mycursor = self.DB.cursor()
  103.         sql = "Insert into tbl_section (section_id, section_name) VALUES (%s, %s)"
  104.         val = (int(id), name)
  105.         mycursor.execute(sql, val)
  106.         self.DB.commit()
  107.         self.window.close()
  108.         self.get_section_data() # Reload the Table
  109.  
  110.     def get_section_data(self):
  111.         mycursor = self.DB.cursor()
  112.         Subquery = "Select * from tbl_section"
  113.         mycursor.execute(Subquery)
  114.  
  115.         numcols = len(mycursor.fetchall()[0])
  116.         mycursor.execute(Subquery)
  117.         numrows = len(mycursor.fetchall())
  118.         self.ui.section_table.setRowCount(numrows)
  119.         self.ui.section_table.setColumnCount(numcols+1)
  120.         mycursor.execute(Subquery)
  121.         tablerow = 0
  122.         for row in mycursor.fetchall():
  123.             tablecol = 0
  124.             button = QPushButton()
  125.             icon = QIcon()
  126.             icon.addFile(u":/icons/icons/icons8-remove-48.png", QSize(), QIcon.Normal, QIcon.Off)
  127.             button.setIcon(icon)
  128.             index = PySide2.QtCore.QPersistentModelIndex(self.ui.section_table.model().index(tablerow, tablecol))
  129.             self.ui.section_table.setCellWidget(tablerow, 2, button)
  130.             button.clicked.connect(lambda *args, index=index: self.on_section_table_click())
  131.             while tablecol < numcols:
  132.                 self.ui.section_table.setItem(tablerow, tablecol, PySide2.QtWidgets.QTableWidgetItem(str(row[tablecol])))
  133.                 tablecol += 1
  134.             tablerow += 1
  135.  
  136.     def navigate_to_course_page(self):
  137.         self.ui.stackedWidget.setCurrentWidget(self.ui.courses_page)
  138.         self.get_course_data()
  139.    
  140.     def get_course_data(self):
  141.         mycursor = self.DB.cursor()
  142.         Subquery = "Select course_name, course_code, course_type from tbl_course"
  143.         mycursor.execute(Subquery)
  144.  
  145.         numcols = len(mycursor.fetchall()[0])
  146.         mycursor.execute(Subquery)
  147.         numrows = len(mycursor.fetchall())
  148.         self.ui.course_table.setRowCount(numrows)
  149.         self.ui.course_table.setColumnCount(numcols+1)
  150.         mycursor.execute(Subquery)
  151.         tablerow = 0
  152.         for row in mycursor.fetchall():
  153.             tablecol = 0
  154.             layout = QHBoxLayout()
  155.             layout.setContentsMargins(0, 0, 0, 0)
  156.             layout.setSpacing(0)
  157.             delete_button = QPushButton()
  158.             icon_delete = QIcon()
  159.             icon_delete.addFile(u":/icons/icons/Delete-button.png", QSize(), QIcon.Normal, QIcon.Off)
  160.             delete_button.setIcon(icon_delete)
  161.             layout.addWidget(delete_button)
  162.             edit_button = QPushButton()
  163.             index = PySide2.QtCore.QPersistentModelIndex(self.ui.course_table.model().index(tablerow, tablecol))
  164.             edit_button.clicked.connect(lambda *args, index=index: self.on_course_edit_click())
  165.             icon_edit = QIcon()
  166.             icon_edit.addFile(u":/icons/icons/edit-button.png", QSize(), QIcon.Normal, QIcon.Off)
  167.             edit_button.setIcon(icon_edit)
  168.             layout.addWidget(edit_button)
  169.             cellWidget = QWidget()
  170.             cellWidget.setLayout(layout)
  171.            
  172.             self.ui.course_table.setCellWidget(tablerow, 3, cellWidget)
  173.             while tablecol < numcols:
  174.                 self.ui.course_table.setItem(tablerow, tablecol, PySide2.QtWidgets.QTableWidgetItem(str(row[tablecol])))
  175.                 tablecol += 1
  176.             tablerow += 1
  177.        
  178.         self.ui.course_table.horizontalHeader().setSectionResizeMode(0, QtWidgets.QHeaderView.Stretch)
  179.         self.ui.course_table.horizontalHeader().setSectionResizeMode(1, QtWidgets.QHeaderView.ResizeToContents)
  180.         self.ui.course_table.horizontalHeader().setSectionResizeMode(2, QtWidgets.QHeaderView.ResizeToContents)
  181.  
  182.     # For the Editing of Course
  183.     def on_course_edit_click(self):
  184.         print("to be declared")
  185.  
  186.     # For the Deletion of Course
  187.     def on_course_delete_button(self):
  188.         print("To be declared")
  189.    
  190.  
  191.     def restore_or_maximize_window(self):
  192.         if self.isMaximized():
  193.             self.showNormal()
  194.             #self.ui.restore_window_button.setIcon()
  195.         else:
  196.             self.showMaximized()
  197.             #self.ui.restore_window_button.setIcon()
  198.  
  199.     #def get_section_data(self):
  200.         # sqlquery = "SELECT * FROM tbl_section"
  201.         # cur = self.db.cursor()
  202.         # for row in cur.execute(sqlquery):
  203.         #     print(row)
  204.  
  205. ## EXECUTE APP  
  206. if __name__ == "__main__":
  207.     app = QApplication(sys.argv)
  208.     window = MainWindow()
  209.     sys.exit(app.exec_())
Advertisement
Add Comment
Please, Sign In to add comment