Untitled

mail@pastecode.io avatar
unknown
python
a month ago
7.3 kB
2
Indexable
Never
import sys
import psycopg2
from PyQt5.uic import loadUi
from PyQt5.QtCore import QSize
from PyQt5.QtWidgets import QDialog, QMainWindow, QApplication, QStackedWidget, QGridLayout, QFrame, QLabel, QWidget, QMessageBox, QPushButton, QVBoxLayout
from ui_staff import StaffTab
from ui_schedule1 import ScheduleTab
from ui_login import Ui_Form
from wesdw import widgets  # Ensure this imports the correct widget class

# Database connection setup
conn = psycopg2.connect(host='localhost', dbname='insurgents', user='postgres', password='123456', port='5432')
cur = conn.cursor()

class AddSchedScreen(QWidget):  # Changed to QWidget
    def __init__(self, stacked_widget, date):
        super(AddSchedScreen, self).__init__()
        self.stacked_widget = stacked_widget
        self.date = date
        self.setMinimumSize(1006, 575)
        self.setWindowTitle('Add Schedule Screen')

        # Use the MainWindow content from grip.py
        self.initUI()

    def initUI(self):
        grid_layout = QGridLayout(self)  # Creating a grid layout

        self.frame2 = QFrame(self)
        self.frame = QFrame(self)

        self.frame2.setStyleSheet("background-color:#f2f2f2;")

        # Create the back button
        self.back_button = QPushButton("Back", self.frame2)
        self.back_button.setStyleSheet("background-color: #B10303; color: white; padding: 5px 10px; border-radius: 5px;")
        self.back_button.setMinimumSize(QSize(50, 30))

        # Add layout manager to frame2
        self.frame2_layout = QVBoxLayout(self.frame2)
        self.frame2_layout.addWidget(self.back_button)

        self.block = QLabel(self.frame)
        self.frame.setStyleSheet("background-color: #ECE6E6;")
        self.block.setStyleSheet("background-color: green;")

        self.f = widgets(self.date, self.frame)
        self.f.setStyleSheet("background-color:lightgrey;")

        grid_layout.addWidget(self.frame, 1, 0)
        grid_layout.addWidget(self.frame2, 0, 0)
        self.setLayout(grid_layout)

        # Connect the back button to the slot or function
        self.back_button.clicked.connect(self.on_back_button_clicked)

    def on_back_button_clicked(self):
        # Find the ScheduleScreen and set it as the current widget
        for i in range(self.stacked_widget.count()):
            widget = self.stacked_widget.widget(i)
            if isinstance(widget, ScheduleScreen):
                self.stacked_widget.setCurrentIndex(i)
                return

    def resizeEvent(self, event):
        frame_width = int(self.height() * 0.8)
        self.frame.setFixedHeight(frame_width)
        self.f.setGeometry(0, 0, int(self.frame.width()), int(self.frame.height()))

        button_width = min(int(self.frame2.width() * 0.2), 100)  # Set a maximum width for the button
        button_height = int(button_width * 0.4)  # Adjust the height proportionally
        self.back_button.setFixedSize(button_width, button_height)

        super().resizeEvent(event)  # Ensure the base class resizeEvent is also called

class StaffScreen(QMainWindow):  # Changed to QMainWindow
    def __init__(self, stacked_widget):
        super(StaffScreen, self).__init__()
        self.ui = StaffTab()
        self.ui.setupUi(self)  # Set up the UI
        self.stacked_widget = stacked_widget
        self.ui.schedbtn.clicked.connect(self.open_schedule)  # Connect to the open_schedule method
        
    def open_schedule(self):
        sched = self.get_schedule_screen()
        self.stacked_widget.setCurrentWidget(sched)

    def get_schedule_screen(self):
        # Check if ScheduleScreen already exists in stacked_widget
        for i in range(self.stacked_widget.count()):
            widget = self.stacked_widget.widget(i)
            if isinstance(widget, ScheduleScreen):
                return widget
        # If not, create a new one
        sched = ScheduleScreen(self.stacked_widget)
        self.stacked_widget.addWidget(sched)
        return sched

class ScheduleScreen(QMainWindow):
    def __init__(self, stacked_widget):
        super(ScheduleScreen, self).__init__()
        self.ui = ScheduleTab()
        self.ui.setupUi(self)  # Set up the UI
        self.stacked_widget = stacked_widget
        self.add_sched_screen = None
        # Ensure the button is correctly referenced
        if hasattr(self.ui, 'staffbtn'):
            self.ui.staffbtn.clicked.connect(self.open_staff)
        else:
            print("Error: 'staffbtn' not found in UI setup")

        self.ui.calendarWidget.selectionChanged.connect(self.ui.printSelectedDate)
        self.ui.addschedbtn.clicked.connect(self.open_addsched)

    def open_staff(self):
        staff = self.get_staff_screen()
        self.stacked_widget.setCurrentWidget(staff)

    def get_staff_screen(self):
        # Check if StaffScreen already exists in stacked_widget
        for i in range(self.stacked_widget.count()):
            widget = self.stacked_widget.widget(i)
            if isinstance(widget, StaffScreen):
                return widget
        # If not, create a new one
        staff = StaffScreen(self.stacked_widget)
        self.stacked_widget.addWidget(staff)
        return staff

    def open_addsched(self):
        # Remove existing AddSchedScreen if it exists
        if self.add_sched_screen is not None:
            self.stacked_widget.removeWidget(self.add_sched_screen)
            self.add_sched_screen.deleteLater()
            self.add_sched_screen = None

        date = self.ui.selected_date
        self.add_sched_screen = AddSchedScreen(self.stacked_widget, date)
        self.stacked_widget.addWidget(self.add_sched_screen)
        self.stacked_widget.setCurrentWidget(self.add_sched_screen)

class LoginScreen(QDialog):
    def __init__(self, stacked_widget):
        super(LoginScreen, self).__init__()
        self.ui = Ui_Form()  # Instantiate the UI class
        self.ui.setupUi(self)  # Set up the UI
        self.stacked_widget = stacked_widget
        self.ui.btnLogin.clicked.connect(self.loginfunction)

    def loginfunction(self):
        user = self.ui.inputEmail.text()
        password = self.ui.inputPass.text()

        cur.execute("""SELECT password FROM users WHERE username = %s""", (user,))
        result_pass = cur.fetchone()

        if len(user) == 0 and len(password) == 0:
            self.ui.error.setText('Input all field')
        else:
            if result_pass and result_pass[0] == password:
                self.open_staff()
            else:
                self.ui.error.setText('Invalid Email or Password')

    def open_schedule(self):
        sched = ScheduleScreen(self.stacked_widget)  # Pass the stacked widget instance
        self.stacked_widget.addWidget(sched)
        self.stacked_widget.setCurrentWidget(sched)
        
    def open_staff(self):
        staff = StaffScreen(self.stacked_widget)
        self.stacked_widget.addWidget(staff)
        self.stacked_widget.setCurrentWidget(staff)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    widget = QStackedWidget()
    login = LoginScreen(widget)
    widget.addWidget(login)
    widget.show()
    sys.exit(app.exec_())
Leave a Comment