66
77__author__ = 'Mehmet Cagri Aksoy - github.com/mcagriaksoy'
88__annotations__ = 'AFCOM - Serial Communication GUI Program'
9- __version__ = '2024.12 '
9+ __version__ = '2025 - 1.4.0.0 '
1010__license__ = 'JGPLv3'
11- __status__ = 'Research '
11+ __status__ = 'Development '
1212
1313# IMPORTS
1414from os import path , system
4242SERIAL_DEVICE = Serial ()
4343PORTS = []
4444is_serial_port_established = False
45- nightModeEnabled = False
46- simpleViewEnabled = False
45+
46+ from winreg import OpenKey , HKEY_CURRENT_USER , QueryValueEx , ConnectRegistry , KEY_READ , KEY_WOW64_64KEY
47+
48+ def is_windows_dark_mode (self ):
49+ try :
50+ registry = ConnectRegistry (None , HKEY_CURRENT_USER )
51+ registry_key = OpenKey (registry , r'Software\Microsoft\Windows\CurrentVersion\Themes\Personalize' )
52+ value , _ = QueryValueEx (registry_key , 'AppsUseLightTheme' )
53+ return value == 0 # 0 means dark mode is enabled
54+ except WindowsError :
55+ return False
56+
57+ # simpleViewEnabled = False
58+
4759
4860def get_serial_port ():
4961 """ Lists serial port names
@@ -102,6 +114,7 @@ class MainWindow(QMainWindow):
102114 def __init__ (self ):
103115 """ Initialize Main Window """
104116 super (MainWindow , self ).__init__ ()
117+
105118 if PROGRAM_TYPE_DEBUG :
106119
107120 file_path = path .join ("ui/main_window.ui" )
@@ -117,6 +130,10 @@ def __init__(self):
117130 print ("UI File Found!" )
118131 self .ui = Ui_main_window ()
119132 self .ui .setupUi (self )
133+
134+ if (is_windows_dark_mode (self )):
135+ print ("Windows Dark Mode is enabled!" )
136+ # todo add dark mode support for the UI
120137
121138 PORTS = get_serial_port ()
122139
@@ -125,7 +142,7 @@ def __init__(self):
125142
126143 self .ui .start_button .clicked .connect (self .start_loop )
127144 self .ui .refresh_button .clicked .connect (self .refresh_port )
128-
145+ '''
129146 self.ui.command_edit_1.clicked.connect(self.command1)
130147 self.ui.command_edit_2.clicked.connect(self.command2)
131148 self.ui.command_edit_3.clicked.connect(self.command3)
@@ -135,49 +152,50 @@ def __init__(self):
135152 self.ui.saved_command_2.clicked.connect(self.move_command2_to_text)
136153 self.ui.saved_command_3.clicked.connect(self.move_command3_to_text)
137154 self.ui.saved_command_4.clicked.connect(self.move_command4_to_text)
155+ '''
138156
139- self .ui .clear_buffer_button .clicked .connect (self .clear_buffer )
157+ # When actionClear_Cache is triggered, clear the buffer
158+ self .ui .actionClear_Cache .triggered .connect (self .clear_buffer )
140159
141- self .ui .night_mode .clicked .connect (self .night_mode_clicked )
142- self .ui .view_change .clicked .connect (self .view_changes )
160+ self .ui .actionBasic_View .triggered .connect (self .basic_view_enabled )
161+ self .ui .actionAdvanced_View .triggered .connect (self .advanced_view_enabled )
162+ #self.ui.clear_buffer_button.clicked.connect(self.clear_buffer)
143163
144- self .ui .port_comboBox . addItems ( PORTS )
164+ # self.ui.view_change.clicked.connect(self.view_changes )
145165
166+ self .ui .port_comboBox .addItems (PORTS )
146167 self .ui .send_button .clicked .connect (self .on_send_data_button_clicked )
147168
148- self .ui .end_button .clicked .connect (self .on_end_button_clicked )
169+ def basic_view_enabled (self ):
170+ """ Hide specific layouts in the UI for basic view """
171+ # Hide all widgets in the verticalLayout_config
172+ for i in range (self .ui .verticalLayout_config .count ()):
173+ widget = self .ui .verticalLayout_config .itemAt (i ).widget ()
174+ if widget :
175+ widget .setVisible (False )
176+
177+ # Optionally, hide all widgets in the formLayout_config
178+ for i in range (self .ui .formLayout_config .count ()):
179+ widget = self .ui .formLayout_config .itemAt (i ).widget ()
180+ if widget :
181+ widget .setVisible (False )
149182
150- def view_changes (self ):
151- """ Change the window size """
152- global simpleViewEnabled
153- # Change the window size
154- if simpleViewEnabled == False :
155- self .resize (726 , 580 )
156- self .ui .view_change .setText (">>" )
157- simpleViewEnabled = True
158- else :
159- self .resize (929 , 580 )
160- self .ui .view_change .setText ("<<" )
161- simpleViewEnabled = False
162-
163- def night_mode_clicked (self ):
164- """ Night Mode """
165- #define static variable
166- global nightModeEnabled
167-
168- # Invert all colors
169- if nightModeEnabled == False :
170- self .setStyleSheet ("background-color: #2C2F33; color: #FFFFFF;" )
171- self .ui .night_mode .setText ("🌘 Day Mode" )
172- self .ui .tabWidget .setStyleSheet ("QWidget { background-color: #2C2F33; color: #FFFFFF; } QTabBar::tab { background: #2C2F33; color: #FFFFFF; }" )
173- nightModeEnabled = True
174- else :
175- self .setStyleSheet ("background-color: #FFFFFF; color: #000000;" )
176- self .ui .night_mode .setText ("🌘 Night Mode" )
177- self .ui .tabWidget .setStyleSheet ("QWidget { background-color: #FFFFFF; color: #000000; } QTabBar::tab { background: #FFFFFF; color: #000000; }" )
178- nightModeEnabled = False
179-
183+ def advanced_view_enabled (self ):
184+ """ Show specific layouts in the UI for advanced view """
185+ # Show all widgets in the verticalLayout_config
186+ for i in range (self .ui .verticalLayout_config .count ()):
187+ widget = self .ui .verticalLayout_config .itemAt (i ).widget ()
188+ if widget :
189+ widget .setVisible (True )
190+
191+ # Optionally, show all widgets in the formLayout_config
192+ for i in range (self .ui .formLayout_config .count ()):
193+ widget = self .ui .formLayout_config .itemAt (i ).widget ()
194+ if widget :
195+ widget .setVisible (True )
180196
197+
198+ '''
181199 def command1(self):
182200 """ Open the text input popup to save command for button 1 """
183201 self.command_edit(1)
@@ -228,7 +246,7 @@ def move_command4_to_text(self):
228246 """ Move the saved command to the text box """
229247 self.ui.send_data_text.setText(self.ui.saved_command_4.text())
230248 self.on_send_data_button_clicked()
231-
249+ '''
232250 def refresh_port (self ):
233251 """ Refresh the serial port list """
234252 PORTS = get_serial_port ()
@@ -318,10 +336,20 @@ def start_loop(self):
318336 return
319337
320338 global is_serial_port_established
339+
340+ if (is_serial_port_established == True ):
341+ is_serial_port_established = False
342+ self .on_end_button_clicked ()
343+ self .ui .start_button .setText ("START" )
344+ return
345+
321346 is_serial_port_established = True
347+ # change start_button to stop button
348+ self .ui .start_button .setText ("STOP" )
349+
322350 try :
323351 self .worker = Worker () # a new worker to perform those tasks
324- self .thread = QThread () # a new thread to run our background tasks in
352+ self .thread = QThread () # a new thread to run our background tasks in
325353 # move the worker into the thread, do this first before connecting the signals
326354 self .worker .moveToThread (self .thread )
327355 # begin our worker object's loop when the thread starts running
@@ -337,6 +365,8 @@ def start_loop(self):
337365 self .thread .finished .connect (self .thread .deleteLater )
338366 # start the thread
339367 self .thread .start ()
368+
369+
340370 except RuntimeError :
341371 self .print_message_on_screen ("Exception in Worker Thread!" )
342372
@@ -347,12 +377,12 @@ def stop_loop(self):
347377 # Disconnect the serial port and close it
348378 SERIAL_DEVICE .close ()
349379
350-
351380 def clear_buffer (self ):
352381 """ Clear the buffer """
353382 self .ui .data_textEdit .clear ()
354383 self .ui .send_data_text .clear ()
355384
385+
356386 def read_data_from_thread (self , serial_data ):
357387 """ Write the result to the text edit box"""
358388 # self.ui.data_textEdit.append("{}".format(i))
@@ -423,5 +453,4 @@ def start_ui_design():
423453 """ Start the UI Design """
424454 app = QApplication (argv ) # Create an instance
425455 window_object = MainWindow () # Create an instance of our class
426- window_object .show ()
427456 exit (app .exec ())
0 commit comments