# Introduction to Qt - demonstrating Business - GUI communication. main = ex11_main.py
# by Anthony Vogelaar, Perth Scotland
# rev 0.10  2024-04-02  Initial release

from    PySide6.QtWidgets   import  QApplication, QWidget, QVBoxLayout, QHBoxLayout, QLabel, QPushButton
from    PySide6.QtCore      import  Qt

# ================================== GUI =========================================

class GUI(QWidget):

    # initialise GUI background
    def __init__(self, owner):
        self.owner = owner
        self.app   = QApplication([])
        super().__init__()
        self.setWindowTitle('The Title')

        lo_main = QVBoxLayout()
        self.label = QLabel('label', self)
        self.label.setAlignment(Qt.AlignCenter)
        self.label.setStyleSheet("font-size: 20px; font-weight: bold; color: red")
        lo_main.addWidget(self.label)

        lo_buttons = QHBoxLayout()
        for caption in('Date', 'Hour', 'Minute', 'Second'):
            btn = QPushButton(caption)
            btn.clicked.connect(self.onBtnClick)
            lo_buttons.addWidget(btn)

        lo_main.addLayout(lo_buttons)
        self.setLayout(lo_main)
        self.show()


    # handle buttons onClick event, send dictionary to controller
    def onBtnClick(self):
        self.owner.doCommand(dict(btn_click=self.sender().text()))


    # handle incoming dictionary commands (from controller)
    def doCommand(self, d):
        for k, v in d.items():
            if k == 'msg': self.label.setText(v)

# ================================= Test =====================================
    
if __name__ == '__main__':

    import  os, sys;            os.system('clear')
    from    PySide6.QtWidgets   import  QApplication

    class Controller:

        def __init__(self):
            gui = GUI(self)
            sys.exit(gui.app.exec())


        def doCommand(self, flag):
            print('onBtnClick')


    Controller()

# ================================= End ==========================================
