Advertisement
Guest User

Python

a guest
Jan 10th, 2018
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 87.63 KB | None | 0 0
  1. from PyQt5.QtGui import *
  2. from PyQt5.QtCore import *
  3. from PyQt5.QtWidgets import *
  4.  
  5. import sys
  6. # noinspection PyUnresolvedReferences
  7. import ImageResources
  8. import pickle
  9. # noinspection PyUnresolvedReferences
  10. import sqlite3
  11.  
  12. from StyleSheet import *
  13. from CreateDatabase import *
  14. from SortResults import *
  15.  
  16.  
  17. class ChamberMainApplication(QApplication):
  18.     def __init__(self):
  19.         super().__init__(sys.argv)
  20.         self.connection = sqlite3.connect("ChamberOfTrade.db")
  21.         self.cursor = self.connection.cursor()
  22.         self.checkDatabaseExistence()
  23.         self.checkPaymentFileExistence()
  24.  
  25.     def checkDatabaseExistence(self):
  26.         """used to check if the database already exists. if not, it creates a sample"""
  27.         results = self.executeSQL("select name from sqlite_master where name=?", ("Contacts",))
  28.         if len(results) != 1:
  29.             create_database()
  30.  
  31.     # noinspection PyMethodMayBeStatic
  32.     def checkPaymentFileExistence(self):
  33.         """checks if the payments file already exists, and if not creates one"""
  34.         file_check = open("Payments.dat", "ab")
  35.         file_check.close()
  36.  
  37.     def executeSQL(self, sql, parameters):
  38.         """executes any SQL in the program"""
  39.         if parameters != "":
  40.             self.cursor.execute(sql, parameters)
  41.         else:
  42.             self.cursor.execute(sql)
  43.         results = self.cursor.fetchall()
  44.         self.connection.commit()
  45.         return results
  46.  
  47.     # noinspection PyUnboundLocalVariable
  48.     def checkReferentialIntegrity(self, identifier, table):
  49.         """checks if an item is in use elsewhere in the database before deletion"""
  50.         id_valid = True
  51.         if table == "Contact":
  52.             temp_data = self.executeSQL("SELECT ContactID FROM Businesses", "")
  53.         elif table == "Business":
  54.             temp_data = self.executeSQL("SELECT BusinessID FROM Adverts", "")
  55.         for row in range(0, len(temp_data)):
  56.             if identifier == temp_data[row][0]:
  57.                 id_valid = False
  58.         return id_valid
  59.  
  60.  
  61. class NavigationToolBar(QToolBar):
  62.     def __init__(self):
  63.         super().__init__()
  64.         self.spacer1 = QWidget()
  65.         self.spacer2 = QWidget()
  66.         self.spacer3 = QWidget()
  67.         self.spacer4 = QWidget()
  68.  
  69.         self.logoPixmap = QPixmap(":/Logo.png")
  70.         self.logoLabel = QLabel()
  71.  
  72.         self.mainMenuButton = QPushButton("Main Menu")
  73.         self.exitButton = QPushButton("Exit")
  74.  
  75.         self.spacer1.setFixedSize(10, 10)
  76.         self.spacer2.setFixedSize(10, 10)
  77.         self.spacer3.setFixedSize(10, 10)
  78.         self.spacer4.setFixedSize(10, 10)
  79.  
  80.         self.setMovable(False)
  81.  
  82.         self.logoLabel.setPixmap(self.logoPixmap.scaledToWidth(60, 1))
  83.         self.logoLabel.setAlignment(Qt.AlignCenter)
  84.  
  85.         self.mainMenuButton.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Fixed)
  86.         self.exitButton.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Fixed)
  87.  
  88.         self.addWidget(self.spacer1)
  89.         self.addWidget(self.mainMenuButton)
  90.         self.addWidget(self.spacer2)
  91.         self.addWidget(self.logoLabel)
  92.         self.addWidget(self.spacer3)
  93.         self.addWidget(self.exitButton)
  94.         self.addWidget(self.spacer4)
  95.  
  96.         assign_style_sheet(self, "body")
  97.  
  98.         # connections
  99.         self.mainMenuButton.clicked.connect(lambda: self.mainMenuClicked())
  100.         self.exitButton.clicked.connect(lambda: self.exitClicked())
  101.  
  102.     # noinspection PyMethodMayBeStatic
  103.     def exitClicked(self):
  104.         exit_dialog = ExitDialog()
  105.         button_clicked = exit_dialog.exec_()
  106.         if button_clicked == 16384:
  107.             sys.exit()
  108.  
  109.     # noinspection PyMethodMayBeStatic
  110.     def mainMenuClicked(self):
  111.         chamber_main_window.changeView("MainMenu")
  112.  
  113.  
  114. class ChamberMainWindow(QMainWindow):
  115.     def __init__(self):
  116.         super().__init__()
  117.         self.navigationToolBar = NavigationToolBar()
  118.         self.tempView = QWidget()
  119.  
  120.         self.addToolBar(self.navigationToolBar)
  121.         self.setWindowTitle("Chamber of Trade Management System")
  122.         self.view = QWidget()
  123.  
  124.         self.changeView("MainMenu")
  125.  
  126.     # noinspection PyUnboundLocalVariable,PyAttributeOutsideInit
  127.     def changeView(self, new_view):
  128.         self.view = None
  129.         self.tempView = QWidget()
  130.         self.setCentralWidget(self.tempView)
  131.         self.setCentralWidget(self.tempView)
  132.         self.tempView.setFixedSize(100, 100)
  133.         self.adjustSize()
  134.         self.hide()
  135.         if new_view == "Login":
  136.             self.view = LoginViewWidget()
  137.             self.size = [350, 550]
  138.         elif new_view == "MainMenu":
  139.             self.view = MainMenuViewWidget()
  140.             self.size = [350, 630]
  141.         elif new_view == "ViewDatabase":
  142.             self.view = ViewDatabaseViewWidget()
  143.             self.size = [350, 630]
  144.         elif new_view == "SearchDatabase":
  145.             self.view = SearchDatabaseViewWidget()
  146.             self.size = [300, 650]
  147.         elif new_view == "OtherDatabase":
  148.             self.view = OtherDatabaseViewWidget()
  149.             self.size = [400, 480]
  150.         elif new_view == "ViewFinances":
  151.             self.view = ViewFinancesViewWidget()
  152.             self.size = [400, 500]
  153.         elif new_view == "AddPayment":
  154.             self.view = AddPaymentViewWidget()
  155.             self.size = [320, 320]
  156.  
  157.         self.setCentralWidget(self.view)
  158.         self.resize(100, 100)
  159.         self.resize(self.size[0], self.size[1])
  160.         self.show()
  161.  
  162.         if new_view == "Login" or new_view == "MainMenu":
  163.             self.navigationToolBar.mainMenuButton.setEnabled(False)
  164.         else:
  165.             self.navigationToolBar.mainMenuButton.setEnabled(True)
  166.  
  167.  
  168. class LoginViewWidget(QWidget):
  169.     def __init__(self):
  170.         super().__init__()
  171.         self.loginAttemptsRemaining = 3
  172.  
  173.         self.businessNameLabel = QLabel("Tadley and District Chamber of Trade")
  174.         self.usernameLabel = QLabel("Username:")
  175.         self.passwordLabel = QLabel("Password:")
  176.  
  177.         self.logoPixmap = QPixmap(":/Logo.png")
  178.         self.logoLabel = QLabel()
  179.  
  180.         self.usernameLineEdit = QLineEdit()
  181.         self.passwordLineEdit = QLineEdit()
  182.  
  183.         self.loginButton = QPushButton("Log In")
  184.  
  185.         self.spacer = QWidget()
  186.  
  187.         self.detailsLayout = QFormLayout()
  188.         self.loginLayout = QVBoxLayout()
  189.  
  190.         self.businessNameLabel.setWordWrap(True)
  191.         self.businessNameLabel.setAlignment(Qt.AlignCenter)
  192.         assign_style_sheet(self.businessNameLabel, "title")
  193.  
  194.         self.passwordLineEdit.setEchoMode(QLineEdit.Password)
  195.  
  196.         assign_style_sheet(self.usernameLabel, "heading2")
  197.         assign_style_sheet(self.passwordLabel, "heading2")
  198.         assign_style_sheet(self.usernameLineEdit, "body")
  199.         assign_style_sheet(self.passwordLineEdit, "body")
  200.         assign_style_sheet(self.loginButton, "body")
  201.  
  202.         self.logoLabel.setFixedHeight(250)
  203.         self.logoLabel.setPixmap(self.logoPixmap.scaledToWidth(220, 1))
  204.         self.logoLabel.setAlignment(Qt.AlignCenter)
  205.  
  206.         self.spacer.setFixedSize(3, 3)
  207.  
  208.         self.loginLayout.setSpacing(10)
  209.  
  210.         self.loginButton.setFixedWidth(150)
  211.  
  212.         self.passwordLineEdit.setFixedWidth(180)
  213.         self.usernameLineEdit.setFixedWidth(180)
  214.  
  215.         self.detailsLayout.addRow(self.usernameLabel, self.usernameLineEdit)
  216.         self.detailsLayout.addRow(self.passwordLabel, self.passwordLineEdit)
  217.         self.detailsLayout.setFormAlignment(Qt.AlignCenter)
  218.  
  219.         self.loginLayout.addWidget(self.businessNameLabel)
  220.         self.loginLayout.addWidget(self.logoLabel)
  221.         self.loginLayout.addLayout(self.detailsLayout)
  222.         self.loginLayout.addWidget(self.spacer)
  223.         self.loginLayout.addWidget(self.loginButton)
  224.         self.loginLayout.setAlignment(self.detailsLayout, Qt.AlignCenter)
  225.  
  226.         self.loginLayout.setAlignment(self.loginButton, Qt.AlignCenter)
  227.  
  228.         self.setLayout(self.loginLayout)
  229.  
  230.         # connections
  231.         self.loginButton.clicked.connect(lambda: self.loginClicked())
  232.  
  233.     def loginClicked(self):
  234.         actual_username, actual_password = self.readLoginFile()
  235.         entered_username = self.usernameLineEdit.text()
  236.         entered_password = self.passwordLineEdit.text()
  237.         if entered_username == "" or entered_password == "":
  238.             login_empty_fields_dialog = LoginEmptyFieldsDialog()
  239.             login_empty_fields_dialog.exec()
  240.             self.usernameLineEdit.setText("")
  241.             self.passwordLineEdit.setText("")
  242.         elif actual_username == entered_username and actual_password == entered_password:
  243.             chamber_main_window.changeView("MainMenu")
  244.         else:
  245.             self.loginAttemptsRemaining -= 1
  246.             failed_login_dialog = FailedLoginDialog(self.loginAttemptsRemaining)
  247.             failed_login_dialog.exec()
  248.             if self.loginAttemptsRemaining == 0:
  249.                 sys.exit()
  250.             else:
  251.                 self.usernameLineEdit.setText("")
  252.                 self.passwordLineEdit.setText("")
  253.  
  254.     # noinspection PyMethodMayBeStatic
  255.     def readLoginFile(self):
  256.         login_file = open("LoginDetails.txt", "r")
  257.         login_text = login_file.readlines()
  258.         username, password = login_text[0].split(",")
  259.         return username, password
  260.  
  261.  
  262. class MainMenuViewWidget(QWidget):
  263.     def __init__(self):
  264.         super().__init__()
  265.         self.businessNameLabel = QLabel("Tadley and District Chamber of Trade")
  266.         self.mainMenuLabel = QLabel("Main Menu")
  267.         assign_style_sheet(self.mainMenuLabel, "heading1")
  268.         assign_style_sheet(self.businessNameLabel, "title")
  269.  
  270.         self.viewDatabaseButton = QPushButton("View Database")
  271.         self.searchDatabaseButton = QPushButton("Search Database")
  272.         self.otherDatabaseButton = QPushButton("Add /  Edit / Delete Entry")
  273.         self.viewFinancesButton = QPushButton("View Finances")
  274.         self.addPaymentButton = QPushButton("Add Payment")
  275.  
  276.         assign_style_sheet(self.viewDatabaseButton, "body")
  277.         assign_style_sheet(self.searchDatabaseButton, "body")
  278.         assign_style_sheet(self.otherDatabaseButton, "body")
  279.         assign_style_sheet(self.viewFinancesButton, "body")
  280.         assign_style_sheet(self.addPaymentButton, "body")
  281.  
  282.         self.logoPixmap = QPixmap(":/Logo.png")
  283.         self.logoLabel = QLabel()
  284.  
  285.         self.financesButtonsLayout = QHBoxLayout()
  286.         self.databaseButtonsLayout = QGridLayout()
  287.  
  288.         self.layout = QVBoxLayout()
  289.  
  290.         self.databaseGroupBox = QGroupBox("Business Database")
  291.         self.financesGroupBox = QGroupBox("Finances")
  292.  
  293.         self.businessNameLabel.setAlignment(Qt.AlignCenter)
  294.         self.businessNameLabel.setWordWrap(True)
  295.  
  296.         self.mainMenuLabel.setAlignment(Qt.AlignCenter)
  297.  
  298.         self.otherDatabaseButton.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Fixed)
  299.  
  300.         self.logoLabel.setAlignment(Qt.AlignCenter)
  301.         self.logoLabel.setPixmap(self.logoPixmap.scaledToWidth(200, 1))
  302.  
  303.         self.financesButtonsLayout.addWidget(self.addPaymentButton)
  304.         self.financesButtonsLayout.addWidget(self.viewFinancesButton)
  305.  
  306.         self.databaseButtonsLayout.addWidget(self.viewDatabaseButton, 0, 0)
  307.         self.databaseButtonsLayout.addWidget(self.searchDatabaseButton, 0, 1)
  308.         self.databaseButtonsLayout.addWidget(self.otherDatabaseButton, 1, 0, 1, 2)
  309.  
  310.         assign_style_sheet(self.databaseGroupBox, "heading2")
  311.         self.databaseGroupBox.setAlignment(Qt.AlignCenter)
  312.         self.databaseGroupBox.setLayout(self.databaseButtonsLayout)
  313.  
  314.         assign_style_sheet(self.financesGroupBox, "heading2")
  315.         self.financesGroupBox.setAlignment(Qt.AlignCenter)
  316.         self.financesGroupBox.setLayout(self.financesButtonsLayout)
  317.  
  318.         self.layout.setSpacing(10)
  319.  
  320.         self.layout.addWidget(self.businessNameLabel)
  321.         self.layout.addWidget(self.mainMenuLabel)
  322.         self.layout.addWidget(self.logoLabel)
  323.         self.layout.addWidget(self.databaseGroupBox)
  324.         self.layout.addWidget(self.financesGroupBox)
  325.  
  326.         self.setLayout(self.layout)
  327.  
  328.         # connections
  329.         self.viewDatabaseButton.clicked.connect(lambda: self.viewDatabaseClicked())
  330.         self.searchDatabaseButton.clicked.connect(lambda: self.searchDatabaseClicked())
  331.         self.otherDatabaseButton.clicked.connect(lambda: self.otherDatabaseClicked())
  332.         self.addPaymentButton.clicked.connect(lambda: self.addPaymentClicked())
  333.         self.viewFinancesButton.clicked.connect(lambda: self.viewFinancesClicked())
  334.  
  335.     # noinspection PyMethodMayBeStatic
  336.     def viewDatabaseClicked(self):
  337.         chamber_main_window.changeView("ViewDatabase")
  338.  
  339.     # noinspection PyMethodMayBeStatic
  340.     def searchDatabaseClicked(self):
  341.         chamber_main_window.changeView("SearchDatabase")
  342.  
  343.     # noinspection PyMethodMayBeStatic
  344.     def otherDatabaseClicked(self):
  345.         chamber_main_window.changeView("OtherDatabase")
  346.  
  347.     # noinspection PyMethodMayBeStatic
  348.     def addPaymentClicked(self):
  349.         chamber_main_window.changeView("AddPayment")
  350.  
  351.     # noinspection PyMethodMayBeStatic
  352.     def viewFinancesClicked(self):
  353.         chamber_main_window.changeView("ViewFinances")
  354.  
  355.  
  356. class ViewDatabaseViewWidget(QWidget):
  357.     def __init__(self):
  358.         super().__init__()
  359.         self.databaseTabs = QTabWidget()
  360.         self.businessTab = DatabaseTabWidget("Businesses")
  361.         self.contactsTab = DatabaseTabWidget("Contacts")
  362.         self.adverts2018Tab = DatabaseTabWidget("Adverts")
  363.  
  364.         self.viewDatabaseLabel = QLabel("View Database")
  365.  
  366.         self.viewDatabaseLayout = QVBoxLayout()
  367.  
  368.         self.viewDatabaseLabel.setAlignment(Qt.AlignCenter)
  369.  
  370.         assign_style_sheet(self.viewDatabaseLabel, "heading1")
  371.         assign_style_sheet(self.databaseTabs, "heading2")
  372.  
  373.         self.databaseTabs.addTab(self.contactsTab, "Contacts")
  374.         self.databaseTabs.addTab(self.businessTab, "Businesses")
  375.         self.databaseTabs.addTab(self.adverts2018Tab, "Adverts 2018")
  376.  
  377.         self.viewDatabaseLayout.setSpacing(10)
  378.  
  379.         self.viewDatabaseLayout.addWidget(self.viewDatabaseLabel)
  380.         self.viewDatabaseLayout.addWidget(self.databaseTabs)
  381.  
  382.         self.setLayout(self.viewDatabaseLayout)
  383.  
  384.  
  385. class SearchDatabaseViewWidget(QWidget):
  386.     def __init__(self):
  387.         super().__init__()
  388.         self.getSearchDetails()
  389.  
  390.         self.searchDatabaseLabel = QLabel("Search Database")
  391.         self.details1Label = QLabel(
  392.             "Search results are displayed below.\nIf there are no results, please try another search.")
  393.         self.details2Label = QLabel("You can change the results order in the bar to the left.")
  394.         self.searchDetailsLabel = QLabel(
  395.             "Searching by: " + str(self.searchMethod) + "\nSearching for: " + str(self.searchData))
  396.  
  397.         self.newSearchButton = QPushButton("New Search")
  398.         self.helpButton = QPushButton("Help")
  399.  
  400.         self.orderByWidget = OrderByWidget()
  401.         self.tableWidget = SearchTableWidget(self.searchMethod, self.searchData)
  402.  
  403.         self.buttonsLayout = QHBoxLayout()
  404.         self.smallLayout = QHBoxLayout()
  405.         self.layout = QVBoxLayout()
  406.  
  407.         self.searchDatabaseLabel.setAlignment(Qt.AlignCenter)
  408.         self.details1Label.setAlignment(Qt.AlignCenter)
  409.         self.details2Label.setAlignment(Qt.AlignCenter)
  410.         self.searchDetailsLabel.setAlignment(Qt.AlignCenter)
  411.  
  412.         assign_style_sheet(self.searchDatabaseLabel, "heading1")
  413.         assign_style_sheet(self.details1Label, "body")
  414.         assign_style_sheet(self.details2Label, "body")
  415.         assign_style_sheet(self.searchDetailsLabel, "heading2")
  416.         assign_style_sheet(self.helpButton, "body")
  417.         assign_style_sheet(self.newSearchButton, "body")
  418.  
  419.         self.buttonsLayout.setSpacing(20)
  420.         self.smallLayout.setSpacing(10)
  421.         self.layout.setSpacing(10)
  422.  
  423.         self.buttonsLayout.addWidget(self.newSearchButton)
  424.         self.buttonsLayout.addWidget(self.helpButton)
  425.  
  426.         self.smallLayout.addWidget(self.tableWidget)
  427.         self.smallLayout.addWidget(self.orderByWidget)
  428.  
  429.         self.layout.addWidget(self.searchDatabaseLabel)
  430.         self.layout.addWidget(self.searchDetailsLabel)
  431.         self.layout.addWidget(self.details1Label)
  432.         self.layout.addWidget(self.details2Label)
  433.         self.layout.addLayout(self.smallLayout)
  434.         self.layout.addLayout(self.buttonsLayout)
  435.  
  436.         self.setLayout(self.layout)
  437.  
  438.         # connections
  439.         self.newSearchButton.clicked.connect(lambda: self.newSearchClicked())
  440.         self.helpButton.clicked.connect(lambda: self.helpClicked())
  441.         self.orderByWidget.updateButton.clicked.connect(lambda: self.updateResultsClicked())
  442.  
  443.     # noinspection PyAttributeOutsideInit
  444.     def getSearchDetails(self):
  445.         values = False
  446.         search_by_dialog = SearchByDialog()
  447.         while not values:
  448.             search_by_dialog.exec()
  449.             self.searchMethod = search_by_dialog.searchByComboBox.currentText()
  450.             if self.searchMethod == "Contact Surname":
  451.                 self.searchData = search_by_dialog.surnameLineEdit.text()
  452.             elif self.searchMethod == "Business Name":
  453.                 self.searchData = search_by_dialog.nameLineEdit.text()
  454.             elif self.searchMethod == "Category":
  455.                 self.searchData = search_by_dialog.categoryComboBox.currentText()
  456.             elif self.searchMethod == "Advert Paid":
  457.                 self.searchData = search_by_dialog.paidComboBox.currentText()
  458.             elif self.searchMethod == "Advert Option":
  459.                 self.searchData = search_by_dialog.advertOptionComboBox.currentText()
  460.  
  461.             if len(self.searchData) > 0:
  462.                 values = True
  463.             else:
  464.                 search_empty_dialog = SearchEmptyDialog()
  465.                 search_empty_dialog.exec()
  466.  
  467.     # noinspection PyMethodMayBeStatic
  468.     def helpClicked(self):
  469.         search_help_dialog = SearchHelpDialog()
  470.         search_help_dialog.exec()
  471.  
  472.     # noinspection PyMethodMayBeStatic
  473.     def newSearchClicked(self):
  474.         chamber_main_window.changeView("SearchDatabase")
  475.  
  476.     # noinspection PyUnboundLocalVariable
  477.     def updateResultsClicked(self):
  478.         order = self.orderByWidget.radioButtonWidget.radioButtonGroup.checkedId()
  479.         order_by = self.orderByWidget.orderByComboBox.currentText()
  480.  
  481.         while self.tableWidget.rowCount() > 0:
  482.             self.tableWidget.removeRow(0)
  483.  
  484.         if order_by == "Contact ID":
  485.             key_col = 0
  486.         elif order_by == "First Name":
  487.             key_col = 2
  488.         elif order_by == "Surname":
  489.             key_col = 3
  490.         elif order_by == "Address 2":
  491.             key_col = 5
  492.         elif order_by == "Town":
  493.             key_col = 6
  494.         elif order_by == "Business ID":
  495.             key_col = 8
  496.         elif order_by == "Business Name":
  497.             key_col = 9
  498.         elif order_by == "Category 1":
  499.             key_col = 13
  500.         elif order_by == "Category 2":
  501.             key_col = 14
  502.         elif order_by == "Advert No.":
  503.             key_col = 15
  504.         elif order_by == "Advert Size":
  505.             key_col = 16
  506.         elif order_by == "Print Type":
  507.             key_col = 17
  508.         elif order_by == "Paid?":
  509.             key_col = 18
  510.         elif order_by == "Payment Method":
  511.             key_col = 19
  512.         elif order_by == "Receipt?":
  513.             key_col = 20
  514.         elif order_by == "Option":
  515.             key_col = 21
  516.  
  517.         self.tableWidget.populateTable(self.searchMethod, self.searchData, order, key_col)
  518.  
  519.  
  520. class OtherDatabaseViewWidget(QWidget):
  521.     def __init__(self):
  522.         super().__init__()
  523.         self.getSearchDetails()
  524.  
  525.         if self.method == "Add":
  526.             self.methodLabel = QLabel("Add Entry")
  527.             self.enterButton = QPushButton("Add Entry")
  528.         elif self.method == "Edit":
  529.             self.methodLabel = QLabel("Edit Entry")
  530.             self.enterButton = QPushButton("Save Edit")
  531.         elif self.method == "Delete":
  532.             self.methodLabel = QLabel("Delete Entry")
  533.             self.enterButton = QPushButton("Delete Entry")
  534.         self.tableLabel = QLabel(self.table)
  535.         self.helpButton = QPushButton("Help")
  536.         self.clearButton = QPushButton("Clear")
  537.         self.changeButton = QPushButton("Change Options")
  538.  
  539.         self.buttonsLayout = QGridLayout()
  540.         self.layout = QVBoxLayout()
  541.  
  542.         if self.table == "Contact":
  543.             self.formDetailsWidget = ContactFormDetailsWidget()
  544.         elif self.table == "Business":
  545.             self.formDetailsWidget = BusinessFormDetailsWidget()
  546.         elif self.table == "Advert":
  547.             self.formDetailsWidget = AdvertFormDetailsWidget()
  548.  
  549.         if self.method == "Edit" or self.method == "Delete":
  550.             self.fillFormDetails()
  551.  
  552.         if self.method == "Delete":
  553.             self.clearButton.setEnabled(False)
  554.  
  555.         assign_style_sheet(self.methodLabel, "heading1")
  556.         assign_style_sheet(self.tableLabel, "heading2")
  557.         assign_style_sheet(self.helpButton, "body")
  558.         assign_style_sheet(self.clearButton, "body")
  559.         assign_style_sheet(self.changeButton, "body")
  560.         assign_style_sheet(self.enterButton, "body")
  561.  
  562.         self.methodLabel.setAlignment(Qt.AlignCenter)
  563.         self.tableLabel.setAlignment(Qt.AlignCenter)
  564.  
  565.         self.formDetailsWidget.setContentsMargins(10, 0, 10, 0)
  566.  
  567.         self.buttonsLayout.addWidget(self.clearButton, 1, 0)
  568.         self.buttonsLayout.addWidget(self.enterButton, 0, 0)
  569.         self.buttonsLayout.addWidget(self.changeButton, 0, 1)
  570.         self.buttonsLayout.addWidget(self.helpButton, 1, 1)
  571.  
  572.         self.layout.addWidget(self.methodLabel)
  573.         self.layout.addWidget(self.tableLabel)
  574.         self.layout.addWidget(self.formDetailsWidget)
  575.         self.layout.addLayout(self.buttonsLayout)
  576.  
  577.         self.buttonsLayout.setSpacing(10)
  578.         self.buttonsLayout.setContentsMargins(5, 5, 5, 5)
  579.  
  580.         self.setLayout(self.layout)
  581.  
  582.         # connections
  583.         self.clearButton.clicked.connect(lambda: self.clearClicked())
  584.         self.enterButton.clicked.connect(lambda: self.enterClicked())
  585.         self.changeButton.clicked.connect(lambda: self.changeOptionsClicked())
  586.         self.helpButton.clicked.connect(lambda: self.helpClicked())
  587.  
  588.     # noinspection PyAttributeOutsideInit
  589.     def getSearchDetails(self):
  590.         choices_dialog = ChoicesDialog()
  591.         choices_dialog.exec()
  592.         self.method = choices_dialog.selectMethodComboBox.currentText()
  593.         self.table = choices_dialog.selectTypeComboBox.currentText()
  594.  
  595.     # noinspection PyAttributeOutsideInit,PyUnboundLocalVariable
  596.     def fillFormDetails(self):
  597.         search_id_dialog = SearchIDDialog(self.table)
  598.         search_id_dialog.exec()
  599.  
  600.         search_id = search_id_dialog.idComboBox.currentText()
  601.         search_id_list = search_id.split()
  602.         search_id = str(search_id_list[0])
  603.  
  604.         if self.table == "Contact":
  605.             sql = "SELECT * FROM Contacts WHERE ContactID = ?"
  606.         elif self.table == "Business":
  607.             sql = "SELECT * FROM Businesses WHERE BusinessID = ?"
  608.         elif self.table == "Advert":
  609.             sql = "SELECT * FROM Adverts WHERE AdvertNo = ?"
  610.  
  611.         data = [search_id]
  612.         details = chamber_main_application.executeSQL(sql, data)
  613.         self.formDetailsWidget.setData(details, self.method)
  614.  
  615.     def helpClicked(self):
  616.         help_dialog = HelpDialog(self.table, self.method)
  617.         help_dialog.exec()
  618.  
  619.     # noinspection PyMethodMayBeStatic
  620.     def changeOptionsClicked(self):
  621.         chamber_main_window.changeView("OtherDatabase")
  622.  
  623.     def clearClicked(self):
  624.         self.formDetailsWidget.clearData()
  625.  
  626.     # noinspection PyUnboundLocalVariable
  627.     def enterClicked(self):
  628.         data_valid, data, errors = self.formDetailsWidget.validateData(self.method, self.table)
  629.         if data_valid:
  630.             if self.method == "Add":
  631.                 if self.table == "Contact":
  632.                     sql = "INSERT INTO Contacts VALUES (?,?,?,?,?,?,?,?)"
  633.                 elif self.table == "Business":
  634.                     sql = "INSERT INTO Businesses VALUES (?,?,?,?,?,?,?,?)"
  635.                 elif self.table == "Advert":
  636.                     sql = "INSERT INTO Adverts VALUES (?,?,?,?,?,?,?,?,?)"
  637.                 sql_parameters = data
  638.             elif self.method == "Delete":
  639.                 sql_parameters = [data[0]]
  640.                 if self.table == "Contact":
  641.                     sql = "DELETE FROM Contacts WHERE ContactID = ?"
  642.                 elif self.table == "Business":
  643.                     sql = "DELETE FROM Businesses WHERE BusinessID = ?"
  644.                 elif self.table == "Advert":
  645.                     sql = "DELETE FROM Adverts WHERE AdvertNo = ?"
  646.             elif self.method == "Edit":
  647.                 if self.table == "Contact":
  648.                     sql = "UPDATE Contacts SET Title = ?,FirstName = ?,Surname = ?,Address1 = ?,Address2 = ?," \
  649.                           "Town = ?, Postcode =? WHERE ContactID = ?"
  650.                 elif self.table == "Business":
  651.                     sql = "UPDATE Businesses SET Name = ?,ContactID = ?,Email = ?,Telephone = ?,Website = ?," \
  652.                           "Category1 = ?,Category2 = ? WHERE BusinessID = ?"
  653.                 elif self.table == "Advert":
  654.                     sql = "UPDATE Adverts SET BusinessID = ?,Size = ?,PrintType = ?,Paid = ?,PaymentMethod = ?," \
  655.                           "Receipt = ?,Option = ?,Details = ? WHERE AdvertNo = ?"
  656.                 update_id = data.pop(0)
  657.                 data.append(update_id)
  658.                 sql_parameters = data
  659.             chamber_main_application.executeSQL(sql, sql_parameters)
  660.  
  661.             successful_dialog = EntrySuccessfulDialog(self.method)
  662.             successful_dialog.exec()
  663.             chamber_main_window.changeView("MainMenu")
  664.  
  665.         else:
  666.             failed_dialog = FailedEntryDialog(errors, self.method)
  667.             failed_dialog.exec()
  668.  
  669.  
  670. class ViewFinancesViewWidget(QWidget):
  671.     def __init__(self):
  672.         super().__init__()
  673.         self.readPaymentFile()
  674.         self.titleLabel = QLabel("View Payments")
  675.         self.detailsLabel = QLabel(
  676.             "Note: Payments are displayed from newest to oldest\n if no details are displayed, the file was empty")
  677.  
  678.         self.tableWidget = QTableWidget()
  679.  
  680.         self.layout = QVBoxLayout()
  681.  
  682.         assign_style_sheet(self.titleLabel, "heading1")
  683.         assign_style_sheet(self.tableWidget.horizontalHeader(), "heading2")
  684.         assign_style_sheet(self.detailsLabel, "body")
  685.         assign_style_sheet(self.tableWidget, "body")
  686.  
  687.         self.titleLabel.setAlignment(Qt.AlignCenter)
  688.         self.detailsLabel.setAlignment(Qt.AlignCenter)
  689.  
  690.         self.tableWidget.setRowCount(self.maxLength)
  691.         self.tableWidget.setColumnCount(self.maxWidth)
  692.         self.tableWidget.setFixedHeight(350)
  693.         self.tableWidget.setFixedWidth(600)
  694.         header_labels = ["Direction", "Business", "Date", "Amount", "Details"]
  695.         self.tableWidget.setHorizontalHeaderLabels(header_labels)
  696.         self.tableWidget.setEditTriggers(QAbstractItemView.NoEditTriggers)
  697.         for i in range(0, len(header_labels)):
  698.             self.tableWidget.horizontalHeader().setSectionResizeMode(i, QHeaderView.ResizeToContents)
  699.  
  700.         self.populatePaymentsTable()
  701.  
  702.         self.layout.addWidget(self.titleLabel)
  703.         self.layout.addWidget(self.detailsLabel)
  704.         self.layout.addWidget(self.tableWidget)
  705.         self.layout.setAlignment(self.tableWidget, Qt.AlignCenter)
  706.  
  707.         self.setLayout(self.layout)
  708.  
  709.     # noinspection PyAttributeOutsideInit
  710.     def readPaymentFile(self):
  711.         payment_file = open("Payments.dat", "rb")
  712.         self.paymentsList = []
  713.         proceed = True
  714.  
  715.         while proceed:
  716.             try:
  717.                 payment = pickle.load(payment_file)
  718.                 self.paymentsList.append(payment)
  719.             except EOFError:
  720.                 proceed = False
  721.         payment_file.close()
  722.         self.maxWidth = 5
  723.         self.maxLength = len(self.paymentsList)
  724.  
  725.         self.paymentsList = sorted(self.paymentsList, key=lambda item: item[2])
  726.         self.paymentsList = self.paymentsList[::-1]
  727.  
  728.     def populatePaymentsTable(self):
  729.         for row in range(0, self.maxLength):
  730.             # noinspection SpellCheckingInspection
  731.             self.paymentsList[row][2] = self.paymentsList[row][2].toString("dd.MM.yyyy")
  732.             for column in range(0, self.maxWidth):
  733.                 item = QTableWidgetItem(self.paymentsList[row][column])
  734.                 self.tableWidget.setItem(row, column, item)
  735.  
  736.  
  737. class AddPaymentViewWidget(QWidget):
  738.     def __init__(self):
  739.         super().__init__()
  740.         self.titleLabel = QLabel("Add Payment")
  741.         self.directionLabel = QLabel("Direction")
  742.         self.businessLabel = QLabel("Business:")
  743.         self.dateLabel = QLabel("Date:")
  744.         self.amountLabel = QLabel("Amount:")
  745.         self.advertPaymentLabel = QLabel("Advert Payment")
  746.         self.detailsLabel = QLabel("Details:")
  747.  
  748.         self.directionComboBox = DirectionComboBox()
  749.         self.businessIDComboBox = BusinessIDComboBox()
  750.         self.dateWidget = QDateEdit()
  751.         self.amountLineEdit = QLineEdit()
  752.         self.advertPaymentCheckBox = QCheckBox()
  753.         self.detailsPlainTextEdit = QPlainTextEdit()
  754.  
  755.         self.savePaymentButton = QPushButton("Save Payment")
  756.         self.clearButton = QPushButton("Clear")
  757.  
  758.         self.detailsLayout = QFormLayout()
  759.         self.buttonsLayout = QHBoxLayout()
  760.         self.mainLayout = QVBoxLayout()
  761.  
  762.         self.titleLabel.setAlignment(Qt.AlignCenter)
  763.  
  764.         self.amountLineEdit.setText("0.00")
  765.         self.dateWidget.setCalendarPopup(True)
  766.         self.dateWidget.setDate(QDate.currentDate())
  767.         self.dateWidget.setMaximumDate(QDate.currentDate())
  768.  
  769.         self.detailsLayout.addRow(self.directionLabel, self.directionComboBox)
  770.         self.detailsLayout.addRow(self.businessLabel, self.businessIDComboBox)
  771.         self.detailsLayout.addRow(self.dateLabel, self.dateWidget)
  772.         self.detailsLayout.addRow(self.amountLabel, self.amountLineEdit)
  773.         self.detailsLayout.addRow(self.advertPaymentLabel, self.advertPaymentCheckBox)
  774.         self.detailsLayout.addRow(self.detailsLabel, self.detailsPlainTextEdit)
  775.         self.detailsLayout.setSpacing(10)
  776.         self.detailsLayout.setContentsMargins(5, 5, 5, 5)
  777.  
  778.         self.buttonsLayout.addWidget(self.clearButton)
  779.         self.buttonsLayout.addWidget(self.savePaymentButton)
  780.         self.buttonsLayout.setSpacing(10)
  781.         self.buttonsLayout.setContentsMargins(5, 5, 5, 5)
  782.  
  783.         self.mainLayout.addWidget(self.titleLabel)
  784.         self.mainLayout.addLayout(self.detailsLayout)
  785.         self.mainLayout.addLayout(self.buttonsLayout)
  786.         self.mainLayout.setSpacing(10)
  787.         self.mainLayout.setContentsMargins(10, 10, 10, 10)
  788.  
  789.         self.setLayout(self.mainLayout)
  790.  
  791.         assign_style_sheet(self, "body")
  792.         assign_style_sheet(self, "body2)")
  793.         assign_style_sheet(self.titleLabel, "heading1")
  794.  
  795.         self.advertPaymentCheckBox.setStyleSheet("QCheckBox::indicator {width: 30px; height: 30px;}"
  796.                                                  "QCheckBox::indicator:checked {image: url(:/Checked.png);}"
  797.                                                  "QCheckBox::indicator:unchecked {image: url(:/Unchecked.png);}"
  798.                                                  "QCheckBox::indicator:checked:disabled {image: url(:/Disabled.png);}"
  799.                                                  "QCheckBox::indicator:unchecked:disabled {image: url(:/Disabled.png);}"
  800.                                                  )
  801.  
  802.         # connections
  803.         self.clearButton.clicked.connect(lambda: self.clearClicked())
  804.         self.savePaymentButton.clicked.connect(lambda: self.savePaymentClicked())
  805.         self.directionComboBox.currentIndexChanged.connect(lambda: self.checkAdvertPayment())
  806.  
  807.     def checkAdvertPayment(self):
  808.         if self.directionComboBox.currentText() == "Incoming":
  809.             self.advertPaymentCheckBox.setEnabled(True)
  810.         else:
  811.             self.advertPaymentCheckBox.setEnabled(False)
  812.  
  813.     def savePaymentClicked(self):
  814.         direction = self.directionComboBox.currentText()
  815.         business_id = self.businessIDComboBox.currentText()
  816.         business_id = self.businessIDComboBox.businessIDList[self.businessIDComboBox.businessList.index(business_id)]
  817.         date = self.dateWidget.date()
  818.         amount = self.amountLineEdit.text()
  819.         details = self.detailsPlainTextEdit.toPlainText()
  820.         details = details.title()
  821.  
  822.         if direction == "Outgoing":
  823.             advert_payment = False
  824.         else:
  825.             advert_payment = self.advertPaymentCheckBox.isChecked()
  826.  
  827.         if advert_payment and details == "":
  828.             details = "ADVERT PAYMENT"
  829.         elif advert_payment:
  830.             details += " ADVERT PAYMENT"
  831.         elif details == "":
  832.             details = "N/A"
  833.  
  834.         data_valid, data, errors = self.validateUserInput(direction, business_id, date, amount, details)
  835.         if data_valid:
  836.             payment_file = open("Payments.dat", "ab")
  837.             pickle.dump(data, payment_file)
  838.             payment_file.close()
  839.  
  840.             entry_successful_dialog = EntrySuccessfulDialog("AddPayment")
  841.             entry_successful_dialog.exec()
  842.             chamber_main_window.changeView("MainMenu")
  843.         else:
  844.             failed_entry_dialog = FailedEntryDialog(errors, "AddPayment")
  845.             failed_entry_dialog.exec()
  846.  
  847.     # noinspection PyListCreation,PyUnusedLocal
  848.     def validateUserInput(self, direction, business_id, date, amount, details):
  849.         data_valid = True
  850.         errors = []
  851.         data = []
  852.  
  853.         data.append(direction)
  854.         data.append(business_id)
  855.  
  856.         if self.dateWidget.validate:
  857.             data.append(date)
  858.         else:
  859.             errors.append(date)
  860.             data_valid = False
  861.  
  862.         amount_valid = False
  863.         number = False
  864.         for i in range(0, len(amount)):
  865.             if amount[i] in ["1", "2", "3", "4", "5", "6", "7", "8", "9"]:
  866.                 number = True
  867.         try:
  868.             int_amount = float(amount)
  869.             if int_amount <= 0:
  870.                 number = False
  871.         except ValueError:
  872.             number = False
  873.         amount += "."
  874.         amount_list = amount.split(".")
  875.         amount_list[1] = "00"
  876.         if len(amount_list) != 2:
  877.             amount_list = amount_list[:2]
  878.         amount = ".".join(amount_list)
  879.  
  880.         if not number:
  881.             amount_valid = False
  882.             errors.append("Amount")
  883.         else:
  884.             amount_valid = True
  885.             data.append(str(amount))
  886.             data.append(details)
  887.  
  888.         if not amount_valid:
  889.             data_valid = False
  890.  
  891.         return data_valid, data, errors
  892.  
  893.     def clearClicked(self):
  894.         self.directionComboBox.setCurrentIndex(0)
  895.         self.businessIDComboBox.setCurrentIndex(0)
  896.         self.amountLineEdit.setText("0.00")
  897.         self.detailsPlainTextEdit.setPlainText("")
  898.         self.advertPaymentCheckBox.setCheckState(0)
  899.  
  900.  
  901. class ExitDialog(QMessageBox):
  902.     def __init__(self):
  903.         super().__init__()
  904.         self.setIcon(QMessageBox.Question)
  905.         self.setText("Are you sure you want to exit?\t\n")
  906.         self.setStandardButtons(QMessageBox.Cancel | QMessageBox.Yes)
  907.         assign_style_sheet(self, "body")
  908.  
  909.  
  910. class LoginEmptyFieldsDialog(QMessageBox):
  911.     def __init__(self):
  912.         super().__init__()
  913.         self.setIcon(QMessageBox.Warning)
  914.         self.setText("Please enter the login details\nand try again.")
  915.         self.setStandardButtons(QMessageBox.Ok)
  916.         assign_style_sheet(self, "body")
  917.  
  918.  
  919. class SearchEmptyDialog(QMessageBox):
  920.     def __init__(self):
  921.         super().__init__()
  922.         self.setIcon(QMessageBox.Warning)
  923.         self.setText("Please check search criteria\nand try again")
  924.         self.setStandardButtons(QMessageBox.Ok)
  925.         assign_style_sheet(self, "body")
  926.  
  927.  
  928. class FailedLoginDialog(QMessageBox):
  929.     def __init__(self, attempts_left):
  930.         super().__init__()
  931.         self.setIcon(QMessageBox.Warning)
  932.         self.setText("Incorrect Login Attempt\t\t")
  933.         if attempts_left > 0:
  934.             attempts_string = ("You have " + str(attempts_left) + " attempt(s) remaining")
  935.         else:
  936.             attempts_string = "You have no attempts remaining\n- the system will now close."
  937.         self.setInformativeText(attempts_string)
  938.         self.setStandardButtons(QMessageBox.Ok)
  939.         assign_style_sheet(self, "body")
  940.  
  941.  
  942. class FailedEntryDialog(QMessageBox):
  943.     def __init__(self, errors, method):
  944.         super().__init__()
  945.         self.setIcon(QMessageBox.Warning)
  946.         if method == "Add":
  947.             self.setText("~ Error when adding entry ~")
  948.             self.setWindowTitle("Adding Entry - Error")
  949.         elif method == "Edit":
  950.             self.setText("~ Error when updating entry ~")
  951.             self.setWindowTitle("Updating Entry - Error")
  952.         elif method == "Delete":
  953.             self.setText("~ Error when deleting entry ~")
  954.             self.setWindowTitle("Deleting Entry - Error")
  955.         elif method == "AddPayment":
  956.             self.setText("~ Error when saving Payment ~")
  957.             self.setWindowTitle("Saving payment - Error")
  958.  
  959.         self.setInformativeText("Please alter the details listed below and try again.")
  960.  
  961.         details = "Please check/alter the details below:\n"
  962.         for item in errors:
  963.             details += item + "\n"
  964.         self.setDetailedText(details)
  965.         self.setStandardButtons(QMessageBox.Ok)
  966.  
  967.         assign_style_sheet(self, "body")
  968.  
  969.  
  970. class HelpDialog(QDialog):
  971.     def __init__(self, form_type, method):
  972.         super().__init__()
  973.         self.helpTitleLabel = QLabel("Help")
  974.         self.buttonHelpLabel = QLabel("Button Help")
  975.         self.formHelpLabel = QLabel("Form Help")
  976.         self.closeButton = QPushButton("Close")
  977.  
  978.         self.spacer1 = QWidget()
  979.         self.spacer2 = QWidget()
  980.  
  981.         if method == "Add":
  982.             self.buttonHelpText = QLabel(
  983.                 "Enter:\t\tAdds entry to database\nChange Table:\tChange the entry type\nClear:\t\tReset the form")
  984.         elif method == "Edit":
  985.             self.buttonHelpText = QLabel(
  986.                 "Enter:\t\tCommits the edit to database\nChange Table:\tChange the entry type\nClear:\t\tReset"
  987.                 " the form")
  988.         elif method == "Delete":
  989.             self.buttonHelpText = QLabel(
  990.                 "Enter:\t\tDeletes the entry\nChange Table:\tChange the entry type\nClear:\t\tReset the form")
  991.         if form_type == "Contact":
  992.             self.formHelpText = QLabel(
  993.                 "Contact ID:\tEnter as Mary Hancock = MH01\n\t\tEnsure this is unique\nPostcode:\tEnter with space")
  994.         elif form_type == "Business":
  995.             self.formHelpText = QLabel(
  996.                 "Business ID:\tEnter as Showoffs = Sho01\n\t\tEnsure this is unique\nCategories:\tTo add a new "
  997.                 "category,\n\t\ttype it in and press enter")
  998.         elif form_type == "Advert":
  999.             self.formHelpText = QLabel("Advert Number:\tEnsure this is unique")
  1000.  
  1001.         self.layout = QVBoxLayout()
  1002.  
  1003.         self.helpTitleLabel.setAlignment(Qt.AlignCenter)
  1004.         self.spacer1.setFixedSize(20, 10)
  1005.         self.spacer2.setFixedSize(20, 10)
  1006.         self.layout.setContentsMargins(20, 20, 20, 20)
  1007.  
  1008.         self.layout.addWidget(self.helpTitleLabel)
  1009.         self.layout.addWidget(self.formHelpLabel)
  1010.         self.layout.addWidget(self.formHelpText)
  1011.         self.layout.addWidget(self.spacer1)
  1012.         self.layout.addWidget(self.buttonHelpLabel)
  1013.         self.layout.addWidget(self.buttonHelpText)
  1014.         self.layout.addWidget(self.spacer2)
  1015.         self.layout.addWidget(self.closeButton)
  1016.  
  1017.         self.layout.setAlignment(self.closeButton, Qt.AlignCenter)
  1018.  
  1019.         self.setLayout(self.layout)
  1020.  
  1021.         assign_style_sheet(self, "body")
  1022.         assign_style_sheet(self.helpTitleLabel, "heading1")
  1023.         assign_style_sheet(self.buttonHelpLabel, "heading2")
  1024.         assign_style_sheet(self.formHelpLabel, "heading2")
  1025.         assign_style_sheet(self.buttonHelpText, "body")
  1026.         assign_style_sheet(self.formHelpText, "body")
  1027.         assign_style_sheet(self.closeButton, "body")
  1028.  
  1029.         # connections
  1030.         self.closeButton.clicked.connect(lambda: self.close())
  1031.  
  1032.  
  1033. class SearchHelpDialog(QDialog):
  1034.     def __init__(self):
  1035.         super().__init__()
  1036.         self.helpTitleLabel = QLabel("Help")
  1037.         self.helpTextLabel = QLabel(
  1038.             "To perform a new search, click 'New Search'\n\nTo sort results, select from the drop-down\nhow you wish "
  1039.             "to sort them,\nthen select ascending/descending.\n")
  1040.         self.closeButton = QPushButton("Close")
  1041.  
  1042.         self.layout = QVBoxLayout()
  1043.         self.layout.setContentsMargins(20, 20, 20, 20)
  1044.         self.layout.setSpacing(10)
  1045.  
  1046.         self.layout.addWidget(self.helpTitleLabel)
  1047.         self.layout.addWidget(self.helpTextLabel)
  1048.         self.layout.addWidget(self.closeButton)
  1049.  
  1050.         self.helpTitleLabel.setAlignment(Qt.AlignCenter)
  1051.         self.layout.setAlignment(self.closeButton, Qt.AlignCenter)
  1052.  
  1053.         self.setLayout(self.layout)
  1054.  
  1055.         assign_style_sheet(self, "body")
  1056.         assign_style_sheet(self.helpTitleLabel, "heading1")
  1057.  
  1058.         # connections
  1059.         self.closeButton.clicked.connect(lambda: self.close())
  1060.  
  1061.  
  1062. class SearchIDDialog(QDialog):
  1063.     def __init__(self, id_type):
  1064.         super().__init__()
  1065.         self.submitButton = QPushButton("Next")
  1066.         self.layout = QVBoxLayout()
  1067.         if id_type == "Contact":
  1068.             self.instructionLabel = QLabel("Select the Contact ID:")
  1069.             self.idComboBox = ContactIDComboBox()
  1070.         elif id_type == "Business":
  1071.             self.instructionLabel = QLabel("Select the Business ID:")
  1072.             self.idComboBox = BusinessIDComboBox()
  1073.         elif id_type == "Advert":
  1074.             self.instructionLabel = QLabel("Select the Advert ID:")
  1075.             self.idComboBox = AdvertIDComboBox()
  1076.  
  1077.         self.instructionLabel.setAlignment(Qt.AlignCenter)
  1078.         self.submitButton.setFixedWidth(90)
  1079.         self.layout.setSpacing(10)
  1080.  
  1081.         self.layout.addWidget(self.instructionLabel)
  1082.         self.layout.addWidget(self.idComboBox)
  1083.         self.layout.addWidget(self.submitButton)
  1084.  
  1085.         self.setLayout(self.layout)
  1086.  
  1087.         self.layout.setAlignment(self.submitButton, Qt.AlignCenter)
  1088.  
  1089.         assign_style_sheet(self, "body")
  1090.  
  1091.         # connections
  1092.         self.submitButton.clicked.connect(lambda: self.close())
  1093.  
  1094.  
  1095. class ChoicesDialog(QDialog):
  1096.     def __init__(self):
  1097.         super().__init__()
  1098.         self.selectMethodComboBox = QComboBox()
  1099.         self.selectTypeComboBox = QComboBox()
  1100.         self.methodLabel = QLabel("Select Method:")
  1101.         self.typeLabel = QLabel("Select entry type:")
  1102.         self.submitButton = QPushButton("Next")
  1103.         self.spacer = QWidget()
  1104.  
  1105.         self.layout = QVBoxLayout()
  1106.  
  1107.         self.methodList = ["Add", "Edit", "Delete"]
  1108.         self.typeList = ["Contact", "Business", "Advert"]
  1109.  
  1110.         self.selectMethodComboBox.insertItems(0, self.methodList)
  1111.         self.selectTypeComboBox.insertItems(0, self.typeList)
  1112.  
  1113.         self.selectMethodComboBox.setFixedWidth(100)
  1114.         self.selectTypeComboBox.setFixedWidth(120)
  1115.         self.submitButton.setFixedWidth(90)
  1116.         self.spacer.setFixedSize(5, 5)
  1117.  
  1118.         self.layout.addWidget(self.methodLabel)
  1119.         self.layout.addWidget(self.selectMethodComboBox)
  1120.         self.layout.addWidget(self.spacer)
  1121.         self.layout.addWidget(self.typeLabel)
  1122.         self.layout.addWidget(self.selectTypeComboBox)
  1123.         self.layout.addWidget(self.submitButton)
  1124.  
  1125.         self.layout.setAlignment(self.selectMethodComboBox, Qt.AlignCenter)
  1126.         self.layout.setAlignment(self.selectTypeComboBox, Qt.AlignCenter)
  1127.         self.layout.setAlignment(self.submitButton, Qt.AlignCenter)
  1128.         self.layout.setSpacing(10)
  1129.  
  1130.         self.methodLabel.setAlignment(Qt.AlignCenter)
  1131.         self.typeLabel.setAlignment(Qt.AlignCenter)
  1132.  
  1133.         self.setLayout(self.layout)
  1134.  
  1135.         assign_style_sheet(self, "body")
  1136.         assign_style_sheet(self.methodLabel, "heading2")
  1137.         assign_style_sheet(self.typeLabel, "heading2")
  1138.  
  1139.         # connections
  1140.         self.submitButton.clicked.connect(lambda: self.close())
  1141.  
  1142.  
  1143. class EntrySuccessfulDialog(QDialog):
  1144.     def __init__(self, method):
  1145.         super().__init__()
  1146.         self.returnLabel = QLabel("Click OK to return to main menu")
  1147.         self.okButton = QPushButton("OK")
  1148.  
  1149.         if method == "Add":
  1150.             self.methodLabel = QLabel("Entry added to database")
  1151.         elif method == "Edit":
  1152.             self.methodLabel = QLabel("Entry updated in database")
  1153.         elif method == "Delete":
  1154.             self.methodLabel = QLabel("Entry deleted from database")
  1155.         elif method == "AddPayment":
  1156.             self.methodLabel = QLabel("Payment saved")
  1157.  
  1158.         self.layout = QVBoxLayout()
  1159.  
  1160.         self.methodLabel.setAlignment(Qt.AlignCenter)
  1161.         self.returnLabel.setAlignment(Qt.AlignCenter)
  1162.  
  1163.         self.layout.addWidget(self.methodLabel)
  1164.         self.layout.addWidget(self.returnLabel)
  1165.         self.layout.addWidget(self.okButton)
  1166.  
  1167.         self.okButton.setFixedWidth(90)
  1168.  
  1169.         self.layout.setSpacing(10)
  1170.         self.layout.setContentsMargins(10, 10, 10, 10)
  1171.         self.layout.setAlignment(self.okButton, Qt.AlignCenter)
  1172.  
  1173.         self.setLayout(self.layout)
  1174.  
  1175.         assign_style_sheet(self.methodLabel, "heading2")
  1176.         assign_style_sheet(self.returnLabel, "body")
  1177.  
  1178.         # connections
  1179.         self.okButton.clicked.connect(lambda: self.close())
  1180.  
  1181.  
  1182. class SearchByDialog(QDialog):
  1183.     def __init__(self):
  1184.         super().__init__()
  1185.         self.searchByLabel = QLabel("Select search by method:")
  1186.         self.surnameLabel = QLabel("Enter contact's surname:")
  1187.         self.nameLabel = QLabel("Enter business name:")
  1188.         self.categoryLabel = QLabel("Select the category:")
  1189.         self.paidLabel = QLabel("Select paid option:")
  1190.         self.advertOptionLabel = QLabel("Select advert option:")
  1191.  
  1192.         self.searchByComboBox = QComboBox()
  1193.         self.surnameLineEdit = QLineEdit()
  1194.         self.nameLineEdit = QLineEdit()
  1195.         self.categoryComboBox = CategoryComboBox(1)
  1196.         self.paidComboBox = YesNoComboBox()
  1197.         self.advertOptionComboBox = AdvertOptionComboBox()
  1198.         self.submitButton = QPushButton("Search")
  1199.         self.spacer = QWidget()
  1200.  
  1201.         self.surnameLayout = QVBoxLayout()
  1202.         self.nameLayout = QVBoxLayout()
  1203.         self.categoryLayout = QVBoxLayout()
  1204.         self.paidLayout = QVBoxLayout()
  1205.         self.advertOptionLayout = QVBoxLayout()
  1206.         self.layout = QVBoxLayout()
  1207.         self.stackedLayout = QStackedLayout()
  1208.  
  1209.         self.surnameWidget = QWidget()
  1210.         self.nameWidget = QWidget()
  1211.         self.categoryWidget = QWidget()
  1212.         self.paidWidget = QWidget()
  1213.         self.advertOptionWidget = QWidget()
  1214.         self.searchForWidget = QWidget()
  1215.  
  1216.         self.searchByList = ["Contact Surname", "Business Name", "Category", "Advert Paid", "Advert Option"]
  1217.         self.searchByComboBox.insertItems(0, self.searchByList)
  1218.  
  1219.         self.searchByComboBox.setFixedWidth(150)
  1220.         self.categoryComboBox.setEditable(False)
  1221.         self.paidComboBox.setFixedWidth(70)
  1222.         self.submitButton.setFixedWidth(100)
  1223.         self.spacer.setFixedSize(5, 5)
  1224.  
  1225.         self.searchByLabel.setAlignment(Qt.AlignCenter)
  1226.         self.surnameLabel.setAlignment(Qt.AlignCenter)
  1227.         self.nameLabel.setAlignment(Qt.AlignCenter)
  1228.         self.categoryLabel.setAlignment(Qt.AlignCenter)
  1229.         self.paidLabel.setAlignment(Qt.AlignCenter)
  1230.         self.advertOptionLabel.setAlignment(Qt.AlignCenter)
  1231.  
  1232.         self.surnameLayout.addWidget(self.surnameLabel)
  1233.         self.surnameLayout.addWidget(self.surnameLineEdit)
  1234.         self.surnameLayout.setAlignment(self.surnameLineEdit, Qt.AlignCenter)
  1235.  
  1236.         self.nameLayout.addWidget(self.nameLabel)
  1237.         self.nameLayout.addWidget(self.nameLineEdit)
  1238.         self.nameLayout.setAlignment(self.nameLineEdit, Qt.AlignCenter)
  1239.  
  1240.         self.categoryLayout.addWidget(self.categoryLabel)
  1241.         self.categoryLayout.addWidget(self.categoryComboBox)
  1242.         self.categoryLayout.setAlignment(self.categoryComboBox, Qt.AlignCenter)
  1243.  
  1244.         self.paidLayout.addWidget(self.paidLabel)
  1245.         self.paidLayout.addWidget(self.paidComboBox)
  1246.         self.paidLayout.setAlignment(self.paidComboBox, Qt.AlignCenter)
  1247.  
  1248.         self.advertOptionLayout.addWidget(self.advertOptionLabel)
  1249.         self.advertOptionLayout.addWidget(self.advertOptionComboBox)
  1250.         self.advertOptionLayout.setAlignment(self.advertOptionComboBox, Qt.AlignCenter)
  1251.  
  1252.         self.surnameWidget.setLayout(self.surnameLayout)
  1253.         self.nameWidget.setLayout(self.nameLayout)
  1254.         self.categoryWidget.setLayout(self.categoryLayout)
  1255.         self.paidWidget.setLayout(self.paidLayout)
  1256.         self.advertOptionWidget.setLayout(self.advertOptionLayout)
  1257.  
  1258.         self.stackedLayout.addWidget(self.surnameWidget)
  1259.         self.stackedLayout.addWidget(self.nameWidget)
  1260.         self.stackedLayout.addWidget(self.categoryWidget)
  1261.         self.stackedLayout.addWidget(self.paidWidget)
  1262.         self.stackedLayout.addWidget(self.advertOptionWidget)
  1263.         self.searchForWidget.setLayout(self.stackedLayout)
  1264.  
  1265.         self.layout.addWidget(self.searchByLabel)
  1266.         self.layout.addWidget(self.searchByComboBox)
  1267.         self.layout.addWidget(self.spacer)
  1268.         self.layout.addWidget(self.searchForWidget)
  1269.         self.layout.addWidget(self.submitButton)
  1270.  
  1271.         self.layout.setAlignment(self.searchByComboBox, Qt.AlignCenter)
  1272.         self.layout.setAlignment(self.searchForWidget, Qt.AlignCenter)
  1273.         self.layout.setAlignment(self.submitButton, Qt.AlignCenter)
  1274.         self.layout.setSpacing(10)
  1275.  
  1276.         self.setLayout(self.layout)
  1277.  
  1278.         self.stackedLayout.setCurrentWidget(self.surnameWidget)
  1279.  
  1280.         assign_style_sheet(self, "body")
  1281.         assign_style_sheet(self.surnameLabel, "heading2")
  1282.         assign_style_sheet(self.nameLabel, "heading2")
  1283.         assign_style_sheet(self.categoryLabel, "heading2")
  1284.         assign_style_sheet(self.paidLabel, "heading2")
  1285.         assign_style_sheet(self.advertOptionLabel, "heading2")
  1286.         assign_style_sheet(self.searchByLabel, "heading2")
  1287.  
  1288.         # connections
  1289.         self.submitButton.clicked.connect(lambda: self.close())
  1290.         self.searchByComboBox.currentIndexChanged.connect(self.setSearchForLayout)
  1291.  
  1292.     def setSearchForLayout(self):
  1293.         search_method = self.searchByComboBox.currentText()
  1294.         if search_method == "Contact Surname":
  1295.             self.stackedLayout.setCurrentWidget(self.surnameWidget)
  1296.         elif search_method == "Business Name":
  1297.             self.stackedLayout.setCurrentWidget(self.nameWidget)
  1298.         elif search_method == "Category":
  1299.             self.stackedLayout.setCurrentWidget(self.categoryWidget)
  1300.         elif search_method == "Advert Paid":
  1301.             self.stackedLayout.setCurrentWidget(self.paidWidget)
  1302.         elif search_method == "Advert Option":
  1303.             self.stackedLayout.setCurrentWidget(self.advertOptionWidget)
  1304.  
  1305.  
  1306. class TitleComboBox(QComboBox):
  1307.     def __init__(self):
  1308.         super().__init__()
  1309.         self.titleList = ["Miss", "Mr", "Mrs", "Ms"]
  1310.         self.insertItems(0, self.titleList)
  1311.  
  1312.  
  1313. class ContactIDComboBox(QComboBox):
  1314.     def __init__(self):
  1315.         super().__init__()
  1316.         sql = "SELECT ContactID,FirstName,Surname FROM Contacts"
  1317.         contact_data = chamber_main_application.executeSQL(sql, "")
  1318.         self.contactList = []
  1319.         self.contactIDList = []
  1320.         for row in range(0, len(contact_data)):
  1321.             item = str(contact_data[row][0]) + " - " + str(contact_data[row][1]) + " " + str(contact_data[row][2])
  1322.             self.contactList.append(item)
  1323.             item = str(contact_data[row][0])
  1324.             self.contactIDList.append(item)
  1325.         mergeSort(self.contactList)
  1326.         mergeSort(self.contactIDList)
  1327.         self.insertItems(0, self.contactList)
  1328.  
  1329.  
  1330. class CategoryComboBox(QComboBox):
  1331.     def __init__(self, no):
  1332.         super().__init__()
  1333.         sql = "SELECT Category1, Category2 FROM Businesses"
  1334.         self.setEditable(True)
  1335.         self.setInsertPolicy(QComboBox.InsertAlphabetically)
  1336.         category_data = chamber_main_application.executeSQL(sql, "")
  1337.         self.categoryList = []
  1338.         for column in range(0, 2):
  1339.             for row in range(0, len(category_data)):
  1340.                 if category_data[row][column] not in self.categoryList and category_data[row][column] != "":
  1341.                     self.categoryList.append(category_data[row][column])
  1342.         mergeSort(self.categoryList)
  1343.         if no != 1:
  1344.             self.categoryList.append(" - ")
  1345.         self.insertItems(0, self.categoryList)
  1346.  
  1347.  
  1348. class AdvertNumberLineEdit(QLineEdit):
  1349.     def __init__(self):
  1350.         super().__init__()
  1351.         sql = "SELECT AdvertNo FROM Adverts"
  1352.         advert_no_data = chamber_main_application.executeSQL(sql, "")
  1353.         self.temp = 0
  1354.         for row in range(0, len(advert_no_data)):
  1355.             if advert_no_data[row][0] > self.temp:
  1356.                 self.temp = advert_no_data[row][0]
  1357.         self.temp = str(self.temp + 1)
  1358.         self.setText(self.temp)
  1359.  
  1360.  
  1361. class BusinessIDComboBox(QComboBox):
  1362.     def __init__(self):
  1363.         super().__init__()
  1364.         sql = "SELECT BusinessID,Name FROM Businesses"
  1365.         business_data = chamber_main_application.executeSQL(sql, "")
  1366.         self.businessList = []
  1367.         self.businessIDList = []
  1368.         for row in range(0, len(business_data)):
  1369.             item = str(business_data[row][0]) + " - " + str(business_data[row][1])
  1370.             self.businessList.append(item)
  1371.             item = business_data[row][0]
  1372.             self.businessIDList.append(item)
  1373.         mergeSort(self.businessList)
  1374.         mergeSort(self.businessIDList)
  1375.         self.insertItems(0, self.businessList)
  1376.  
  1377.  
  1378. class AdvertIDComboBox(QComboBox):
  1379.     def __init__(self):
  1380.         super().__init__()
  1381.         sql = "SELECT AdvertNo,Name FROM Adverts,Businesses WHERE Adverts.BusinessID = Businesses.BusinessID"
  1382.         advert_data = chamber_main_application.executeSQL(sql, "")
  1383.         self.advertList = []
  1384.         self.advertIDList = []
  1385.         for row in range(0, len(advert_data)):
  1386.             item = str(advert_data[row][0]) + " - " + str(advert_data[row][1])
  1387.             self.advertList.append(item)
  1388.             item = str(advert_data[row][0])
  1389.             self.advertIDList.append(item)
  1390.         mergeSort(self.advertList)
  1391.         mergeSort(self.advertIDList)
  1392.         self.insertItems(0, self.advertList)
  1393.  
  1394.  
  1395. class SizeComboBox(QComboBox):
  1396.     def __init__(self):
  1397.         super().__init__()
  1398.         self.sizeList = ["Full Page", "Half Page", "Special"]
  1399.         self.insertItems(0, self.sizeList)
  1400.  
  1401.  
  1402. class PrintTypeComboBox(QComboBox):
  1403.     def __init__(self):
  1404.         super().__init__()
  1405.         self.printTypeList = ["Colour", "Monochrome"]
  1406.         self.insertItems(0, self.printTypeList)
  1407.  
  1408.  
  1409. class YesNoComboBox(QComboBox):
  1410.     def __init__(self):
  1411.         super().__init__()
  1412.         self.yesNoList = ["Yes", "No"]
  1413.         self.insertItems(0, self.yesNoList)
  1414.  
  1415.  
  1416. class DirectionComboBox(QComboBox):
  1417.     def __init__(self):
  1418.         super().__init__()
  1419.         self.directionList = ["Incoming", "Outgoing"]
  1420.         self.insertItems(0, self.directionList)
  1421.  
  1422.  
  1423. class PaymentMethodComboBox(QComboBox):
  1424.     def __init__(self):
  1425.         super().__init__()
  1426.         self.paymentMethodList = ["Cash", "Cheque", "Online"]
  1427.         self.insertItems(0, self.paymentMethodList)
  1428.  
  1429.  
  1430. class AdvertOptionComboBox(QComboBox):
  1431.     def __init__(self):
  1432.         super().__init__()
  1433.         self.advertOptionList = ["New", "Changes", "No Changes"]
  1434.         self.insertItems(0, self.advertOptionList)
  1435.  
  1436.  
  1437. class BusinessComboBox(QComboBox):
  1438.     def __init__(self):
  1439.         super().__init__()
  1440.         self.businessEntityList = ["Business ID", "Name", "Contact ID", "Email",
  1441.                                    "Telephone", "Website", "Category 1", "Category 2"]
  1442.         self.insertItems(0, self.businessEntityList)
  1443.  
  1444.  
  1445. class AdvertComboBox(QComboBox):
  1446.     def __init__(self):
  1447.         super().__init__()
  1448.         self.advertEntityList = ["Advert Number", "Business ID", "Size", "Print Type",
  1449.                                  "Paid", "Payment Method", "Receipt", "Option", "Details"]
  1450.         self.insertItems(0, self.advertEntityList)
  1451.  
  1452.  
  1453. class ContactComboBox(QComboBox):
  1454.     def __init__(self):
  1455.         super().__init__()
  1456.         self.contactEntityList = ["Contact ID", "Title", "First Name", "Surname",
  1457.                                   "Address 1", "Address 2", "Town", "Postcode"]
  1458.         self.insertItems(0, self.contactEntityList)
  1459.  
  1460.  
  1461. class OrderByComboBox(QComboBox):
  1462.     def __init__(self):
  1463.         super().__init__()
  1464.         self.orderByList = ["Contact ID", "First Name", "Surname", "Address 2", "Town", "Business ID", "Business Name",
  1465.                             "Category 1", "Category 2", "Advert No.", "Advert Size", "Print Type", "Paid?",
  1466.                             "Payment Method", "Receipt?", "Option"]
  1467.         self.insertItems(0, self.orderByList)
  1468.  
  1469.  
  1470. class DatabaseTabWidget(QWidget):
  1471.     def __init__(self, table_type):
  1472.         super().__init__()
  1473.         self.tableWidget = ViewTableWidget(table_type)
  1474.  
  1475.         self.tabLayout = QVBoxLayout()
  1476.  
  1477.         self.tableWidget.setEditTriggers(QAbstractItemView.NoEditTriggers)
  1478.         self.tableWidget.verticalHeader().setVisible(False)
  1479.  
  1480.         self.tableWidget.setFixedWidth(1100)
  1481.         self.tableWidget.setFixedHeight(400)
  1482.  
  1483.         self.tabLayout.addWidget(self.tableWidget)
  1484.         self.tabLayout.setAlignment(Qt.AlignCenter)
  1485.  
  1486.         self.setLayout(self.tabLayout)
  1487.  
  1488.         assign_style_sheet(self.tableWidget.horizontalHeader(), "heading2")
  1489.  
  1490.  
  1491. class OrderByWidget(QWidget):
  1492.     def __init__(self):
  1493.         super().__init__()
  1494.         self.orderLabel = QLabel("Order:")
  1495.         self.orderByLabel = QLabel("Order By:")
  1496.  
  1497.         self.orderByComboBox = OrderByComboBox()
  1498.         self.updateButton = QPushButton("Update")
  1499.         self.radioButtonWidget = RadioButtonWidget()
  1500.  
  1501.         self.layout = QVBoxLayout()
  1502.  
  1503.         assign_style_sheet(self, "body")
  1504.         assign_style_sheet(self.orderLabel, "heading2")
  1505.         assign_style_sheet(self.orderByLabel, "heading2")
  1506.  
  1507.         self.layout.addWidget(self.orderLabel)
  1508.         self.layout.addWidget(self.radioButtonWidget)
  1509.         self.layout.addWidget(self.orderByLabel)
  1510.         self.layout.addWidget(self.orderByComboBox)
  1511.         self.layout.addWidget(self.updateButton)
  1512.  
  1513.         self.layout.setAlignment(self.orderLabel, Qt.AlignCenter)
  1514.         self.layout.setAlignment(self.radioButtonWidget, Qt.AlignCenter)
  1515.         self.layout.setAlignment(self.orderByLabel, Qt.AlignCenter)
  1516.         self.layout.setAlignment(self.orderByComboBox, Qt.AlignCenter)
  1517.         self.layout.setAlignment(self.updateButton, Qt.AlignCenter)
  1518.  
  1519.         self.setLayout(self.layout)
  1520.  
  1521.  
  1522. class RadioButtonWidget(QWidget):
  1523.     def __init__(self):
  1524.         super().__init__()
  1525.         self.radioButtonGroup = QButtonGroup()
  1526.         self.radioButtonList = []
  1527.         self.radioButtonLayout = QVBoxLayout()
  1528.  
  1529.         self.radioButtonList.append(QRadioButton("Ascending"))
  1530.         self.radioButtonList.append(QRadioButton("Descending"))
  1531.         self.radioButtonList[0].setChecked(True)
  1532.  
  1533.         counter = 1
  1534.         for item in self.radioButtonList:
  1535.             self.radioButtonLayout.addWidget(item)
  1536.             self.radioButtonGroup.addButton(item)
  1537.             self.radioButtonGroup.setId(item, counter)
  1538.             counter += 1
  1539.  
  1540.         self.setLayout(self.radioButtonLayout)
  1541.  
  1542.  
  1543. class SearchTableWidget(QTableWidget):
  1544.     def __init__(self, method, data):
  1545.         super().__init__()
  1546.         assign_style_sheet(self, "body")
  1547.         self.setFixedWidth(1000)
  1548.         self.setFixedHeight(350)
  1549.         self.setEditTriggers(QAbstractItemView.NoEditTriggers)
  1550.         self.verticalHeader().setVisible(False)
  1551.         self.setColumnCount(23)
  1552.         header_labels = ["Contact ID", "Title", "First Name", "Surname", "Address 1", "Address 2", "Town",
  1553.                          "Postcode", "Business ID", "Business Name", "Email", "Telephone", "Website",
  1554.                          "Category 1", "Category 2", "Advert No.", "Advert Size", "Print Type", "Paid?",
  1555.                          "Payment Method", "Receipt?", "Option", "Details"]
  1556.         self.setHorizontalHeaderLabels(header_labels)
  1557.         self.populateTable(method, data, 1, 0)
  1558.  
  1559.         for i in range(0, len(header_labels)):
  1560.             self.horizontalHeader().setSectionResizeMode(i, QHeaderView.ResizeToContents)
  1561.  
  1562.         assign_style_sheet(self, "body")
  1563.         assign_style_sheet(self.horizontalHeader(), "heading2")
  1564.  
  1565.     # noinspection PyUnboundLocalVariable
  1566.     def populateTable(self, method, data, order, key_col):
  1567.         if method == "Contact Surname":
  1568.             sql = "SELECT * FROM Contacts,Businesses,Adverts WHERE Surname=? AND Contacts.ContactID = " \
  1569.                   "Businesses.ContactID AND Businesses.BusinessID = Adverts.BusinessID"
  1570.         elif method == "Business Name":
  1571.             sql = "SELECT * FROM Contacts,Businesses,Adverts WHERE Name=? AND Contacts.ContactID = " \
  1572.                   "Businesses.ContactID AND Businesses.BusinessID = Adverts.BusinessID"
  1573.         elif method == "Category":
  1574.             sql = "SELECT * FROM Contacts,Businesses,Adverts WHERE (Category1=? OR Category2=?) AND Contacts.ContactID " \
  1575.                   "= Businesses.ContactID AND Businesses.BusinessID = Adverts.BusinessID"
  1576.         elif method == "Advert Paid":
  1577.             sql = "SELECT * FROM Contacts,Businesses,Adverts WHERE Paid=? AND Contacts.ContactID = " \
  1578.                   "Businesses.ContactID AND Businesses.BusinessID = Adverts.BusinessID"
  1579.         elif method == "Advert Option":
  1580.             sql = "SELECT * FROM Contacts,Businesses,Adverts WHERE Option=? AND Contacts.ContactID = " \
  1581.                   "Businesses.ContactID AND Businesses.BusinessID = Adverts.BusinessID"
  1582.  
  1583.         # noinspection PyUnboundLocalVariable
  1584.         temp_data = data
  1585.         if method == "Category":
  1586.             data = [temp_data, temp_data]
  1587.         else:
  1588.             data = [temp_data]
  1589.         temp_results = chamber_main_application.executeSQL(sql, data)
  1590.         results = []
  1591.         for row in range(0, len(temp_results)):
  1592.             temp_list = []
  1593.             for column in range(0, 25):
  1594.                 item = str(temp_results[row][column])
  1595.                 if column != 10 and column != 18:
  1596.                     temp_list.append(item)
  1597.             results.append(temp_list)
  1598.  
  1599.         results = sortList(results, order, key_col)
  1600.  
  1601.         for row in range(0, len(results)):
  1602.             self.insertRow(row)
  1603.             for column in range(0, 23):
  1604.                 if column == 11:
  1605.                     item = QTableWidgetItem("0" + str(results[row][column]))
  1606.                 else:
  1607.                     item = QTableWidgetItem(str(results[row][column]))
  1608.                 self.setItem(row, column, item)
  1609.  
  1610.  
  1611. class ViewTableWidget(QTableWidget):
  1612.     # noinspection PyUnboundLocalVariable
  1613.     def __init__(self, table_type):
  1614.         super().__init__()
  1615.         if table_type == "Contacts":
  1616.             sql = "SELECT * FROM Contacts ORDER BY ContactID"
  1617.             column_count = 8
  1618.             headers = ["Contact ID", "Title", "First Name", "Surname", "Address 1", "Address 2", "Town", "Postcode"]
  1619.         elif table_type == "Businesses":
  1620.             sql = "SELECT * FROM Businesses ORDER BY BusinessID"
  1621.             column_count = 8
  1622.             headers = ["Business ID", "Name", "Contact ID", "Email", "Telephone", "Website", "Category 1", "Category 2"]
  1623.         elif table_type == "Adverts":
  1624.             sql = "SELECT * FROM Adverts ORDER BY AdvertNo"
  1625.             column_count = 9
  1626.             headers = ["Advert No", "Business ID", "Size", "Print Type", "Paid", "Payment Method", "Receipt", "Option",
  1627.                        "Details"]
  1628.  
  1629.         self.setColumnCount(column_count)
  1630.         self.setHorizontalHeaderLabels(headers)
  1631.         for i in range(0, column_count):
  1632.             self.horizontalHeader().setSectionResizeMode(i, QHeaderView.ResizeToContents)
  1633.  
  1634.         results = chamber_main_application.executeSQL(sql, "")
  1635.  
  1636.         for row in range(0, len(results)):
  1637.             self.insertRow(row)
  1638.             for column in range(0, column_count):
  1639.                 if table_type == "Businesses" and column == 4:
  1640.                     item = QTableWidgetItem("0"+str(results[row][column]))
  1641.                 else:
  1642.                     item = QTableWidgetItem(str(results[row][column]))
  1643.                 self.setItem(row, column, item)
  1644.  
  1645.         assign_style_sheet(self, "body")
  1646.  
  1647.  
  1648. class FormDetailsWidget(QWidget):
  1649.     def __init__(self):
  1650.         super().__init__()
  1651.  
  1652.     # noinspection PyMethodMayBeStatic
  1653.     def validateText(self, data):
  1654.         if data.isalpha() and len(data) > 0:
  1655.             return True
  1656.         else:
  1657.             return False
  1658.  
  1659.     # noinspection PyMethodMayBeStatic
  1660.     def validateNumber(self, data):
  1661.         if not data.isalpha() and len(data) > 0:
  1662.             return True
  1663.         else:
  1664.             return False
  1665.  
  1666.  
  1667. class ContactFormDetailsWidget(FormDetailsWidget):
  1668.     def __init__(self):
  1669.         super().__init__()
  1670.         self.errors = []
  1671.         self.data = []
  1672.  
  1673.         self.contactIDLabel = QLabel("Contact ID:")
  1674.         self.titleLabel = QLabel("Title:")
  1675.         self.firstNameLabel = QLabel("First Name:")
  1676.         self.surnameLabel = QLabel("Surname:")
  1677.         self.address1Label = QLabel("Address Line 1:")
  1678.         self.address2Label = QLabel("Address Line 2:")
  1679.         self.townLabel = QLabel("Town:")
  1680.         self.postcodeLabel = QLabel("Postcode:")
  1681.  
  1682.         self.contactIDLineEdit = QLineEdit()
  1683.         self.titleComboBox = TitleComboBox()
  1684.         self.firstNameLineEdit = QLineEdit()
  1685.         self.surnameLineEdit = QLineEdit()
  1686.         self.address1LineEdit = QLineEdit()
  1687.         self.address2LineEdit = QLineEdit()
  1688.         self.townLineEdit = QLineEdit()
  1689.         self.postcodeLineEdit = QLineEdit()
  1690.  
  1691.         self.layout = QFormLayout()
  1692.  
  1693.         assign_style_sheet(self, "body")
  1694.  
  1695.         self.layout.addRow(self.contactIDLabel, self.contactIDLineEdit)
  1696.         self.layout.addRow(self.titleLabel, self.titleComboBox)
  1697.         self.layout.addRow(self.firstNameLabel, self.firstNameLineEdit)
  1698.         self.layout.addRow(self.surnameLabel, self.surnameLineEdit)
  1699.         self.layout.addRow(self.address1Label, self.address1LineEdit)
  1700.         self.layout.addRow(self.address2Label, self.address2LineEdit)
  1701.         self.layout.addRow(self.townLabel, self.townLineEdit)
  1702.         self.layout.addRow(self.postcodeLabel, self.postcodeLineEdit)
  1703.         self.layout.setLabelAlignment(Qt.AlignRight)
  1704.  
  1705.         self.setLayout(self.layout)
  1706.  
  1707.     def clearData(self):
  1708.         self.contactIDLineEdit.setText("")
  1709.         self.titleComboBox.setCurrentIndex(0)
  1710.         self.firstNameLineEdit.setText("")
  1711.         self.surnameLineEdit.setText("")
  1712.         self.address1LineEdit.setText("")
  1713.         self.address2LineEdit.setText("")
  1714.         self.townLineEdit.setText("")
  1715.         self.postcodeLineEdit.setText("")
  1716.  
  1717.     # noinspection PyAttributeOutsideInit
  1718.     def getEntries(self):
  1719.         self.contactID = self.contactIDLineEdit.text()
  1720.         self.title = self.titleComboBox.currentText()
  1721.         self.firstName = self.firstNameLineEdit.text()
  1722.         self.firstName = self.firstName.title()
  1723.         self.surname = self.surnameLineEdit.text()
  1724.         self.surname = self.surname.title()
  1725.         self.address1 = self.address1LineEdit.text()
  1726.         self.address1 = self.address1.title()
  1727.         self.address2 = self.address2LineEdit.text()
  1728.         self.address2 = self.address2.title()
  1729.         self.town = self.townLineEdit.text()
  1730.         self.town = self.town.title()
  1731.         self.postcode = self.postcodeLineEdit.text()
  1732.  
  1733.     def validateData(self, method, table):
  1734.         data_valid = True
  1735.  
  1736.         self.getEntries()
  1737.  
  1738.         if method == "Add":
  1739.             if not self.validateContactID():
  1740.                 data_valid = False
  1741.                 self.errors.append("Contact ID")
  1742.             else:
  1743.                 self.data.append(self.contactID)
  1744.                 self.data.append(self.title)
  1745.  
  1746.         elif method == "Edit":
  1747.             self.data.append(self.contactID)
  1748.             self.data.append(self.title)
  1749.  
  1750.         elif method == "Delete":
  1751.             checked = chamber_main_application.checkReferentialIntegrity(self.contactID, table)
  1752.             if checked:
  1753.                 self.data.append(self.contactID)
  1754.                 self.data.append(self.title)
  1755.                 self.data.append(self.firstName)
  1756.                 self.data.append(self.surname)
  1757.                 self.data.append(self.address1)
  1758.                 self.data.append(self.address2)
  1759.                 self.data.append(self.town)
  1760.                 self.data.append(self.postcode)
  1761.             else:
  1762.                 data_valid = False
  1763.                 self.errors.append("Contact in use in another table")
  1764.  
  1765.         if method != "Delete":
  1766.             if not self.validateText(self.firstName):
  1767.                 data_valid = False
  1768.                 self.errors.append("First Name")
  1769.             else:
  1770.                 self.data.append(self.firstName)
  1771.  
  1772.             if not self.validateText(self.surname):
  1773.                 data_valid = False
  1774.                 self.errors.append("Surname")
  1775.             else:
  1776.                 self.data.append(self.surname)
  1777.  
  1778.             if self.address1 == "":
  1779.                 data_valid = False
  1780.                 self.errors.append("Address 1")
  1781.             else:
  1782.                 self.data.append(self.address1)
  1783.                 self.data.append(self.address2)
  1784.  
  1785.             if not self.validateText(self.town):
  1786.                 data_valid = False
  1787.                 self.errors.append("Town")
  1788.             else:
  1789.                 self.data.append(self.town)
  1790.  
  1791.             if len(self.postcode) not in range(5, 9):
  1792.                 data_valid = False
  1793.                 self.errors.append("Postcode")
  1794.             else:
  1795.                 self.data.append(self.postcode)
  1796.  
  1797.         return data_valid, self.data, self.errors
  1798.  
  1799.     # noinspection PyAttributeOutsideInit,PyUnboundLocalVariable
  1800.     def validateContactID(self):
  1801.         data_valid = True
  1802.         chars_list = list(self.contactID)
  1803.         valid_list = []
  1804.  
  1805.         if len(chars_list) == 4:
  1806.             letter_list = chars_list[:2]
  1807.             number_list = chars_list[2:]
  1808.         elif len(chars_list) == 5:
  1809.             letter_list = chars_list[:3]
  1810.             number_list = chars_list[2:]
  1811.         else:
  1812.             data_valid = False
  1813.             letter_list = []
  1814.             number_list = []
  1815.  
  1816.         for char in letter_list:
  1817.             if not char.isalpha():
  1818.                 data_valid = False
  1819.             else:
  1820.                 char = char.upper()
  1821.                 valid_list.append(char)
  1822.  
  1823.         for char in number_list:
  1824.             if not char.isdigit():
  1825.                 data_valid = False
  1826.             else:
  1827.                 valid_list.append(char)
  1828.  
  1829.         if data_valid:
  1830.             self.contactID = "".join(valid_list)
  1831.         sql = "SELECT ContactID FROM Contacts"
  1832.         data = chamber_main_application.executeSQL(sql, "")
  1833.         contact_id_list = []
  1834.         for item in data:
  1835.             contact_id_list.append(item[0])
  1836.         for item in contact_id_list:
  1837.             if self.contactID == item:
  1838.                 data_valid = False
  1839.  
  1840.         return data_valid
  1841.  
  1842.     def setData(self, data_list, method):
  1843.         self.contactIDLineEdit.setText(data_list[0][0])
  1844.         self.titleComboBox.setCurrentIndex(self.titleComboBox.titleList.index(data_list[0][1]))
  1845.         self.firstNameLineEdit.setText(data_list[0][2])
  1846.         self.surnameLineEdit.setText(data_list[0][3])
  1847.         self.address1LineEdit.setText(data_list[0][4])
  1848.         self.address2LineEdit.setText(data_list[0][5])
  1849.         self.townLineEdit.setText(data_list[0][6])
  1850.         self.postcodeLineEdit.setText(data_list[0][7])
  1851.  
  1852.         if method == "Delete":
  1853.             self.contactIDLineEdit.setReadOnly(True)
  1854.             self.firstNameLineEdit.setReadOnly(True)
  1855.             self.surnameLineEdit.setReadOnly(True)
  1856.             self.address1LineEdit.setReadOnly(True)
  1857.             self.address2LineEdit.setReadOnly(True)
  1858.             self.townLineEdit.setReadOnly(True)
  1859.             self.postcodeLineEdit.setReadOnly(True)
  1860.             self.titleComboBox.clear()
  1861.             self.titleComboBox.addItem(data_list[0][1])
  1862.  
  1863.  
  1864. class BusinessFormDetailsWidget(FormDetailsWidget):
  1865.     def __init__(self):
  1866.         super().__init__()
  1867.         self.errors = []
  1868.         self.data = []
  1869.  
  1870.         self.businessIDLabel = QLabel("Business ID:")
  1871.         self.businessNameLabel = QLabel("Business Name:")
  1872.         self.contactIDLabel = QLabel("Contact ID:")
  1873.         self.emailLabel = QLabel("Email:")
  1874.         self.telephoneLabel = QLabel("Telephone:")
  1875.         self.webAddressLabel = QLabel("Web Address:")
  1876.         self.category1Label = QLabel("Category 1:")
  1877.         self.category2Label = QLabel("Category 2:")
  1878.  
  1879.         self.businessIDLineEdit = QLineEdit()
  1880.         self.businessNameLineEdit = QLineEdit()
  1881.         self.contactIDComboBox = ContactIDComboBox()
  1882.         self.emailLineEdit = QLineEdit()
  1883.         self.telephoneLineEdit = QLineEdit()
  1884.         self.webAddressLineEdit = QLineEdit()
  1885.         self.category1ComboBox = CategoryComboBox(1)
  1886.         self.category2ComboBox = CategoryComboBox(2)
  1887.  
  1888.         self.layout = QFormLayout()
  1889.  
  1890.         assign_style_sheet(self, "body")
  1891.  
  1892.         self.layout.addRow(self.businessIDLabel, self.businessIDLineEdit)
  1893.         self.layout.addRow(self.businessNameLabel, self.businessNameLineEdit)
  1894.         self.layout.addRow(self.contactIDLabel, self.contactIDComboBox)
  1895.         self.layout.addRow(self.emailLabel, self.emailLineEdit)
  1896.         self.layout.addRow(self.telephoneLabel, self.telephoneLineEdit)
  1897.         self.layout.addRow(self.webAddressLabel, self.webAddressLineEdit)
  1898.         self.layout.addRow(self.category1Label, self.category1ComboBox)
  1899.         self.layout.addRow(self.category2Label, self.category2ComboBox)
  1900.         self.layout.setLabelAlignment(Qt.AlignRight)
  1901.  
  1902.         self.setLayout(self.layout)
  1903.  
  1904.     def clearData(self):
  1905.         self.businessIDLineEdit.setText("")
  1906.         self.businessNameLineEdit.setText("")
  1907.         self.contactIDComboBox.setCurrentIndex(0)
  1908.         self.emailLineEdit.setText("")
  1909.         self.telephoneLineEdit.setText("")
  1910.         self.webAddressLineEdit.setText("")
  1911.         self.category1ComboBox.setCurrentIndex(0)
  1912.         self.category2ComboBox.setCurrentIndex(0)
  1913.  
  1914.     # noinspection PyAttributeOutsideInit
  1915.     def getEntries(self):
  1916.         self.businessID = self.businessIDLineEdit.text()
  1917.         self.businessName = self.businessNameLineEdit.text()
  1918.         self.businessName = self.businessName.title()
  1919.         self.contactID = self.contactIDComboBox.currentText()
  1920.         self.email = self.emailLineEdit.text()
  1921.         self.telephone = self.telephoneLineEdit.text()
  1922.         self.webAddress = self.webAddressLineEdit.text()
  1923.         self.category1 = self.category1ComboBox.currentText()
  1924.         self.category1 = self.category1.title()
  1925.         self.category2 = self.category2ComboBox.currentText()
  1926.         self.category2 = self.category2.title()
  1927.  
  1928.         if self.category2 == " - ":
  1929.             self.category2 = ""
  1930.  
  1931.         self.tempContactID = self.contactID.split()
  1932.         self.contactID = self.tempContactID[0]
  1933.  
  1934.     def validateData(self, method, table):
  1935.         data_valid = True
  1936.  
  1937.         self.getEntries()
  1938.  
  1939.         if method == "Add":
  1940.             if not self.validateBusinessID():
  1941.                 data_valid = False
  1942.                 self.errors.append("BusinessID")
  1943.             else:
  1944.                 self.data.append(self.businessID)
  1945.  
  1946.         elif method == "Edit":
  1947.             self.data.append(self.businessID)
  1948.  
  1949.         elif method == "Delete":
  1950.             checked = chamber_main_application.checkReferentialIntegrity(self.businessID, table)
  1951.             if checked:
  1952.                 self.data.append(self.businessID)
  1953.                 self.data.append(self.businessName)
  1954.                 self.data.append(self.contactID)
  1955.                 self.data.append(self.email)
  1956.                 self.data.append(self.telephone)
  1957.                 self.data.append(self.webAddress)
  1958.                 self.data.append(self.category1)
  1959.                 self.data.append(self.category2)
  1960.             else:
  1961.                 data_valid = False
  1962.                 self.errors.append("Business in use in another table")
  1963.  
  1964.         if method != "Delete":
  1965.             if self.businessName == "":
  1966.                 self.errors.append("Business Name")
  1967.             else:
  1968.                 self.data.append(self.businessName)
  1969.                 self.data.append(self.contactID)
  1970.  
  1971.             if not self.validateEmail():
  1972.                 data_valid = False
  1973.                 self.errors.append("Email")
  1974.             else:
  1975.                 self.data.append(self.email)
  1976.  
  1977.             if not self.validateNumber(self.telephone) or len(self.telephone) not in range(10, 13):
  1978.                 data_valid = False
  1979.                 self.errors.append("Telephone")
  1980.             else:
  1981.                 self.data.append(self.telephone)
  1982.  
  1983.             if not self.validateWebAddress:
  1984.                 data_valid = False
  1985.                 self.errors.append("Web Address")
  1986.             else:
  1987.                 self.data.append(self.webAddress)
  1988.  
  1989.             if self.category1 == self.category2:
  1990.                 data_valid = False
  1991.                 self.errors.append("Category 2")
  1992.             else:
  1993.                 self.data.append(self.category1)
  1994.                 self.data.append(self.category2)
  1995.  
  1996.         return data_valid, self.data, self.errors
  1997.  
  1998.     # noinspection PyAttributeOutsideInit,PyUnboundLocalVariable
  1999.     def validateBusinessID(self):
  2000.         data_valid = True
  2001.         chars_list = list(self.businessID)
  2002.         valid_list = []
  2003.  
  2004.         if len(chars_list) == 5:
  2005.             number_list = chars_list[3:]
  2006.             letter_list = chars_list[:3]
  2007.         else:
  2008.             data_valid = False
  2009.  
  2010.         for char in letter_list:
  2011.             if not char.isalpha():
  2012.                 data_valid = False
  2013.             else:
  2014.                 char = char.upper()
  2015.                 valid_list.append(char)
  2016.  
  2017.         for char in number_list:
  2018.             if not char.isdigit():
  2019.                 data_valid = False
  2020.             else:
  2021.                 valid_list.append(char)
  2022.         if data_valid:
  2023.             self.businessID = "".join(valid_list)
  2024.  
  2025.         sql = "SELECT BusinessID FROM Businesses"
  2026.         data = chamber_main_application.executeSQL(sql, "")
  2027.         business_id_list = []
  2028.         for item in data:
  2029.             business_id_list.append(item[0])
  2030.         for item in business_id_list:
  2031.             if self.businessID == item:
  2032.                 data_valid = False
  2033.  
  2034.         return data_valid
  2035.  
  2036.     def validateEmail(self):
  2037.         data_valid = False
  2038.         for char in self.email:
  2039.             if char == "@":
  2040.                 data_valid = True
  2041.  
  2042.         if len(self.email) == 0:
  2043.             data_valid = False
  2044.         return data_valid
  2045.  
  2046.     def validateWebAddress(self):
  2047.         data_valid = False
  2048.         web_list = self.webAddress.split(".")
  2049.         endings_list = ["com", "net", "uk", "org", "gov"]
  2050.         if web_list[-1] not in endings_list:
  2051.             data_valid = False
  2052.         return data_valid
  2053.  
  2054.     def setData(self, data_list, method):
  2055.         self.businessIDLineEdit.setText(data_list[0][0])
  2056.         self.businessNameLineEdit.setText(data_list[0][1])
  2057.         self.contactIDComboBox.setCurrentIndex(self.contactIDComboBox.contactIDList.index(data_list[0][2]))
  2058.         self.emailLineEdit.setText(data_list[0][3])
  2059.         self.telephoneLineEdit.setText("0" + str(data_list[0][4]))
  2060.         self.webAddressLineEdit.setText(data_list[0][5])
  2061.         self.category1ComboBox.setCurrentIndex(self.category1ComboBox.categoryList.index(data_list[0][6]))
  2062.         if data_list[0][7] != "":
  2063.             self.category2ComboBox.setCurrentIndex(self.category2ComboBox.categoryList.index(data_list[0][7]))
  2064.         else:
  2065.             self.category2ComboBox.setCurrentIndex(len(self.category2ComboBox.categoryList) - 1)
  2066.  
  2067.         if method == "Delete":
  2068.             self.businessIDLineEdit.setReadOnly(True)
  2069.             self.businessNameLineEdit.setReadOnly(True)
  2070.             self.emailLineEdit.setReadOnly(True)
  2071.             self.telephoneLineEdit.setReadOnly(True)
  2072.             self.webAddressLineEdit.setReadOnly(True)
  2073.             self.contactIDComboBox.clear()
  2074.             self.contactIDComboBox.addItem(data_list[0][2])
  2075.             self.category1ComboBox.clear()
  2076.             self.category1ComboBox.addItem(data_list[0][6])
  2077.             self.category1ComboBox.setEditable(False)
  2078.             self.category2ComboBox.clear()
  2079.             self.category2ComboBox.addItem(data_list[0][7])
  2080.             self.category2ComboBox.setEditable(False)
  2081.  
  2082.  
  2083. class AdvertFormDetailsWidget(FormDetailsWidget):
  2084.     def __init__(self):
  2085.         super().__init__()
  2086.         self.errors = []
  2087.         self.data = []
  2088.  
  2089.         self.advertNumberLabel = QLabel("Advert Number:")
  2090.         self.businessIDLabel = QLabel("Business ID:")
  2091.         self.sizeLabel = QLabel("Size:")
  2092.         self.printTypeLabel = QLabel("Print Type:")
  2093.         self.paidLabel = QLabel("Paid?:")
  2094.         self.paymentMethodLabel = QLabel("Payment Method:")
  2095.         self.receiptLabel = QLabel("Receipt?:")
  2096.         self.advertOptionLabel = QLabel("Advert Option:")
  2097.         self.detailsLabel = QLabel("Details:")
  2098.  
  2099.         self.advertNumberLineEdit = AdvertNumberLineEdit()
  2100.         self.businessIDComboBox = BusinessIDComboBox()
  2101.         self.sizeComboBox = SizeComboBox()
  2102.         self.printTypeComboBox = PrintTypeComboBox()
  2103.         self.paidComboBox = YesNoComboBox()
  2104.         self.paymentMethodComboBox = PaymentMethodComboBox()
  2105.         self.receiptComboBox = YesNoComboBox()
  2106.         self.advertOptionComboBox = AdvertOptionComboBox()
  2107.         self.detailsPlainTextEdit = QPlainTextEdit()
  2108.  
  2109.         self.layout = QFormLayout()
  2110.  
  2111.         assign_style_sheet(self, "body")
  2112.  
  2113.         self.layout.addRow(self.advertNumberLabel, self.advertNumberLineEdit)
  2114.         self.layout.addRow(self.businessIDLabel, self.businessIDComboBox)
  2115.         self.layout.addRow(self.sizeLabel, self.sizeComboBox)
  2116.         self.layout.addRow(self.printTypeLabel, self.printTypeComboBox)
  2117.         self.layout.addRow(self.paidLabel, self.paidComboBox)
  2118.         self.layout.addRow(self.paymentMethodLabel, self.paymentMethodComboBox)
  2119.         self.layout.addRow(self.receiptLabel, self.receiptComboBox)
  2120.         self.layout.addRow(self.advertOptionLabel, self.advertOptionComboBox)
  2121.         self.layout.addRow(self.detailsLabel, self.detailsPlainTextEdit)
  2122.         self.layout.setLabelAlignment(Qt.AlignRight)
  2123.  
  2124.         self.setLayout(self.layout)
  2125.  
  2126.         # connections
  2127.         self.paidComboBox.currentIndexChanged.connect(self.checkPayment)
  2128.  
  2129.     def clearData(self):
  2130.         self.advertNumberLineEdit.setText(self.advertNumberLineEdit.temp)
  2131.         self.businessIDComboBox.setCurrentIndex(0)
  2132.         self.sizeComboBox.setCurrentIndex(0)
  2133.         self.printTypeComboBox.setCurrentIndex(0)
  2134.         self.paidComboBox.setCurrentIndex(0)
  2135.         self.paymentMethodComboBox.setCurrentIndex(0)
  2136.         self.receiptComboBox.setCurrentIndex(0)
  2137.         self.advertOptionComboBox.setCurrentIndex(0)
  2138.         self.detailsPlainTextEdit.setPlainText("")
  2139.  
  2140.     # noinspection PyAttributeOutsideInit
  2141.     def getEntries(self):
  2142.         self.advertNumber = self.advertNumberLineEdit.text()
  2143.         self.businessID = self.businessIDComboBox.currentText()
  2144.         self.size = self.sizeComboBox.currentText()
  2145.         self.printType = self.printTypeComboBox.currentText()
  2146.         self.paid = self.paidComboBox.currentText()
  2147.  
  2148.         if self.paid == "Yes":
  2149.             self.paymentMethod = self.paymentMethodComboBox.currentText()
  2150.             self.receipt = self.receiptComboBox.currentText()
  2151.         else:
  2152.             self.paymentMethod = ""
  2153.             self.receipt = ""
  2154.         self.advertOption = self.advertOptionComboBox.currentText()
  2155.         self.details = self.detailsPlainTextEdit.toPlainText()
  2156.         self.details = self.details.title()
  2157.  
  2158.         self.tempBusinessID = self.businessID.split()
  2159.         self.businessID = self.tempBusinessID[0]
  2160.  
  2161.     # noinspection PyUnusedLocal
  2162.     def validateData(self, method, table):
  2163.         data_valid = True
  2164.  
  2165.         self.getEntries()
  2166.  
  2167.         if method == "Add":
  2168.             if not self.validateAdvertNumber():
  2169.                 data_valid = False
  2170.                 self.errors.append("Advert Number")
  2171.             else:
  2172.                 self.data.append(self.advertNumber)
  2173.  
  2174.         else:
  2175.             self.data.append(self.advertNumber)
  2176.  
  2177.         self.data.append(self.businessID)
  2178.         self.data.append(self.size)
  2179.         self.data.append(self.printType)
  2180.         self.data.append(self.paid)
  2181.         self.data.append(self.paymentMethod)
  2182.         self.data.append(self.receipt)
  2183.         self.data.append(self.advertOption)
  2184.         self.data.append(self.details)
  2185.  
  2186.         return data_valid, self.data, self.errors
  2187.  
  2188.     def validateAdvertNumber(self):
  2189.         data_valid = True
  2190.         if not self.validateNumber(self.advertNumber):
  2191.             data_valid = False
  2192.  
  2193.         sql = "SELECT AdvertNo FROM Adverts"
  2194.         data = chamber_main_application.executeSQL(sql, "")
  2195.         advert_id_list = []
  2196.         for item in data:
  2197.             advert_id_list.append(item[0])
  2198.         for item in advert_id_list:
  2199.             if self.advertNumber == item:
  2200.                 data_valid = False
  2201.  
  2202.         return data_valid
  2203.  
  2204.     def checkPayment(self):
  2205.         if self.paidComboBox.currentText() == "No":
  2206.             self.paymentMethodComboBox.setEnabled(False)
  2207.             self.receiptComboBox.setEnabled(False)
  2208.         elif self.paidComboBox.currentText() == "Yes":
  2209.             self.paymentMethodComboBox.setEnabled(True)
  2210.             self.receiptComboBox.setEnabled(True)
  2211.  
  2212.     def setData(self, data_list, method):
  2213.         self.advertNumberLineEdit.setText(str(data_list[0][0]))
  2214.         self.businessIDComboBox.setCurrentIndex(self.businessIDComboBox.businessIDList.index(data_list[0][1]))
  2215.         self.sizeComboBox.setCurrentIndex(self.sizeComboBox.sizeList.index(data_list[0][2]))
  2216.         self.printTypeComboBox.setCurrentIndex(self.printTypeComboBox.printTypeList.index(data_list[0][3]))
  2217.         self.paidComboBox.setCurrentIndex(self.paidComboBox.yesNoList.index(data_list[0][4]))
  2218.         if self.paidComboBox.currentText() == "Yes":
  2219.             self.paymentMethodComboBox.setCurrentIndex(self.paymentMethodComboBox.paymentMethodList.index(
  2220.                 data_list[0][5]))
  2221.             self.receiptComboBox.setCurrentIndex(self.receiptComboBox.yesNoList.index(data_list[0][6]))
  2222.         self.advertOptionComboBox.setCurrentIndex(self.advertOptionComboBox.advertOptionList.index(data_list[0][7]))
  2223.         self.detailsPlainTextEdit.setPlainText(data_list[0][8])
  2224.  
  2225.         if method == "Delete":
  2226.             self.advertNumberLineEdit.setReadOnly(True)
  2227.             self.detailsPlainTextEdit.setReadOnly(True)
  2228.             self.businessIDComboBox.clear()
  2229.             self.businessIDComboBox.addItem(data_list[0][1])
  2230.             self.sizeComboBox.clear()
  2231.             self.sizeComboBox.addItem(data_list[0][2])
  2232.             self.printTypeComboBox.clear()
  2233.             self.printTypeComboBox.addItem(data_list[0][3])
  2234.             self.paidComboBox.clear()
  2235.             self.paidComboBox.addItem(data_list[0][4])
  2236.             self.paymentMethodComboBox.clear()
  2237.             self.paymentMethodComboBox.addItem(data_list[0][5])
  2238.             self.receiptComboBox.clear()
  2239.             self.receiptComboBox.addItem(data_list[0][6])
  2240.             self.advertOptionComboBox.clear()
  2241.             self.advertOptionComboBox.addItem(data_list[0][7])
  2242.  
  2243.  
  2244. # Main Program
  2245. chamber_main_application = ChamberMainApplication()
  2246. chamber_main_window = ChamberMainWindow()
  2247. chamber_main_window.show()
  2248. chamber_main_window.raise_()
  2249. chamber_main_application.exec_()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement