summaryrefslogtreecommitdiffstats
path: root/main.py
blob: d832789694c538ba42c9b457827fb214601db22e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
"""
   Copyright 2023 Suchinton Chakravarty

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
"""

import sys
import os

from PyQt5 import uic, QtCore, QtWidgets
from PyQt5.QtWidgets import QApplication, QPushButton, QWidget
from functools import partial
from PyQt5 import QtGui
from PyQt5.QtCore import Qt

current_dir = os.path.dirname(os.path.abspath(__file__))
Form, Base = uic.loadUiType(os.path.join(current_dir, "Main_Window.ui"))

from extras.UI_Handeler import *
from Widgets.Dashboard import Dashboard

class MainWindow(Base, Form):
    """
    The main window of the AGL Demo Control Panel application.
    Inherits from the Base and Form classes.
    """

    # signal to stop the thread
    stop_thread_signal = QtCore.pyqtSignal()
    start_thread_signal = QtCore.pyqtSignal()

    def __init__(self, parent=None):
        """
        Initializes the MainWindow object.
        Sets up the UI, window flags, and geometry.
        Connects signals and slots for window controls and widget navigation.
        Initializes the Dashboard object and connects its signals.
        """
        super(self.__class__, self).__init__(parent)
        self.setupUi(self)
        self.setWindowFlags(QtCore.Qt.FramelessWindowHint)
        self.setAttribute(QtCore.Qt.WA_TranslucentBackground)

        self.setStyle(QtWidgets.QStyleFactory.create('Fusion'))

        self.headerContainer = self.findChild(QWidget, 'headerContainer')
        self.headerContainer.DoubleClickMaximize = lambda: UI_Handeler.toggleMaximized(self)
        self.headerContainer.mouseMoveEvent = lambda event: UI_Handeler.moveWindow(self, event)
        self.headerContainer.mousePressEvent = lambda event: UI_Handeler.mousePressEvent(self, event)
        self.headerContainer.mouseReleaseEvent = lambda event: UI_Handeler.mouseReleaseEvent(self, event)

        self.leftMenuSubContainer = self.findChild(QWidget, 'leftMenuSubContainer')
        self.dashboardButton = self.findChild(QPushButton, 'dashboardButton')
        UI_Handeler.Hide_Navbar(self,bool_arg=True)

        self.stackedWidget.currentChanged.connect(lambda: UI_Handeler.subscribe_VSS_Signals(self) if UI_Handeler.set_instance(self) else None)

        self.notificationContent = self.findChild(QWidget, 'notificationContent')

        # Window Controls
        closeButton = self.findChild(QPushButton, 'closeBtn')
        minimizeButton = self.findChild(QPushButton, 'minimizeBtn')
        maximizeButton = self.findChild(QPushButton, 'maximizeBtn')

        # make the close button also end all threads
        closeButton.clicked.connect(lambda: [self.close(), self.stop_thread_signal.emit()])
        minimizeButton.clicked.connect(self.showMinimized)
        maximizeButton.clicked.connect(lambda: UI_Handeler.toggleMaximized(self))

        # Widget Navigation
        Navigation_buttons = ( self.dashboardButton,
                    self.icButton, 
                    self.hvacButton, 
                    self.steeringCtrlButton,
                    self.settingsBtn) 
        
        NavigationButtons = QtWidgets.QButtonGroup(self)
        NavigationButtons.setExclusive(True)

        for i, button in enumerate(Navigation_buttons):
            button.setCheckable(True)
            NavigationButtons.addButton(button)
            button.clicked.connect(partial(UI_Handeler.animateSwitch, self, i))

        self.stackedWidget.currentChanged.connect(self.handleChangedPage)
        
        self.stop_thread_signal.connect(self.stackedWidget.widget(0).feed_kuksa.stop)

        self.stackedWidget.setCurrentIndex(0)
        self.dashboardButton.setChecked(True)
        UI_Handeler.Hide_Navbar(self,bool_arg=False)

        self.Dashboard = Dashboard()
        self.Dashboard.tileClickedSignal.connect(self.handleTileClicked)

        self.current_page = self.stackedWidget.currentIndex()

    def handleTileClicked(self):
        """
        Handles the tile clicked signal from the Dashboard object.
        Shows the navbar.
        """
        UI_Handeler.Hide_Navbar(self,bool_arg=False)

    def handleChangedPage(self, index):
        """
        Handles the change of pages in the stacked widget.
        Stops the previous thread and starts the new one.
        If the index is 0, the navbar is not hidden. Otherwise, it is hidden.
        """
        if index == 0:
            UI_Handeler.Hide_Navbar(self,bool_arg=False)
        else:
            UI_Handeler.Hide_Navbar(self,bool_arg=True)
        try:
            self.stop_thread_signal.connect(self.stackedWidget.widget(self.current_page).feed_kuksa.stop)
            self.stop_thread_signal.emit()
        except:
            pass

        self.current_page = self.stackedWidget.currentIndex()

        try:
            self.start_thread_signal.connect(self.stackedWidget.widget(self.current_page).feed_kuksa.start)
            self.start_thread_signal.emit()
        except:
            pass

if __name__ == '__main__':
    app = QApplication(sys.argv)
    app.setApplicationName("AGL Demo Control Panel")
    app.setWindowIcon(QtGui.QIcon(':/Images/Images/Automotive_Grade_Linux_logo.svg'))
    window = MainWindow()
    window.show()
    sys.exit(app.exec_())