-
Notifications
You must be signed in to change notification settings - Fork 45
/
gui.py
1101 lines (901 loc) · 45.2 KB
/
gui.py
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from PyQt5.QtWidgets import (
QApplication, QWidget, QVBoxLayout, QPushButton, QLabel, QStackedWidget,
QPlainTextEdit, QHBoxLayout, QMessageBox, QListWidgetItem, QGridLayout,
QDialog, QLineEdit, QListWidget, QInputDialog, QCheckBox, QDialogButtonBox,
QComboBox, QColorDialog, QSlider, QMenu, QFileDialog
)
from PyQt5.QtGui import QFont, QIcon, QColor
from PyQt5.QtCore import Qt, QProcess, QSettings, pyqtSignal
import sys, re, copy
# --- Dialog Classes ---
class ClockStyleDialog(QDialog):
def __init__(self, parent=None):
super().__init__(parent)
self.setWindowTitle('Clock Style')
self.layout = QVBoxLayout(self)
# Available clock styles
self.clock_styles = ['Default', 'Christmas', 'Racing', 'Inverted Full Screen', 'Animated Hourglass', 'Frame 1', 'Frame 2', 'Frame 3']
self.selected_color = None # Variable to hold the selected color
# Clock style selection
self.clock_style_label = QLabel('Select Clock Style:')
self.clock_style_combo = QComboBox(self)
self.clock_style_combo.addItems(self.clock_styles)
# Checkboxes for additional options
self.show_date_checkbox = QCheckBox('Show Date', self)
self.show_24hr_checkbox = QCheckBox('24-Hour Format', self)
# Color picker button
self.color_picker_button = QPushButton('Choose Color', self)
self.color_picker_button.clicked.connect(self.open_color_dialog)
# Dialog button box (OK and Cancel)
self.button_box = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel, self)
self.button_box.accepted.connect(self.accept)
self.button_box.rejected.connect(self.reject)
# Add widgets to the layout
self.layout.addWidget(self.clock_style_label)
self.layout.addWidget(self.clock_style_combo)
self.layout.addWidget(self.show_date_checkbox)
self.layout.addWidget(self.show_24hr_checkbox)
self.layout.addWidget(self.color_picker_button)
self.layout.addWidget(self.button_box)
def open_color_dialog(self):
color_dialog = QColorDialog(self)
if color_dialog.exec_() == QColorDialog.Accepted:
self.selected_color = color_dialog.selectedColor()
self.color_picker_button.setStyleSheet(f"background-color: {self.selected_color.name()}")
def get_options(self):
if self.exec_() == QDialog.Accepted:
clock_style = self.clock_style_combo.currentText()
show_date = self.show_date_checkbox.isChecked()
show_24hr = self.show_24hr_checkbox.isChecked()
return clock_style, show_date, show_24hr, self.selected_color, True
else:
return None, None, None, None, False
class CustomInputDialog(QDialog):
def __init__(self, parent=None):
super().__init__(parent)
self.setWindowTitle('Input')
self.layout = QVBoxLayout(self)
self.label = QLabel('Name this device?', self)
self.layout.addWidget(self.label)
self.line_edit = QLineEdit(self)
self.layout.addWidget(self.line_edit)
self.button_layout = QHBoxLayout()
self.yes_button = QPushButton('Yes', self)
self.no_button = QPushButton('No', self)
self.button_layout.addWidget(self.yes_button)
self.button_layout.addWidget(self.no_button)
self.layout.addLayout(self.button_layout)
self.yes_button.clicked.connect(self.accept)
self.no_button.clicked.connect(self.reject)
def get_input(self):
if self.exec_() == QDialog.Accepted:
return self.line_edit.text(), True
else:
return '', False
class TextStyleDialog(QDialog):
def __init__(self, parent=None):
super().__init__(parent)
self.setWindowTitle('Text Settings')
self.layout = QVBoxLayout(self)
# Initialize selected colors
self.selected_text_color = None
self.selected_bg_color = None
# Text input
self.text_label = QLabel('Enter Text:')
self.text_input = QLineEdit(self)
self.layout.addWidget(self.text_label)
self.layout.addWidget(self.text_input)
# Text Size
self.text_size_label = QLabel('Text Size:')
self.text_size_combo = QComboBox(self)
self.text_size_combo.addItems(["16", "32"])
self.layout.addWidget(self.text_size_label)
self.layout.addWidget(self.text_size_combo)
# Text Mode
self.text_mode_label = QLabel('Text Style:')
self.text_mode_combo = QComboBox(self)
self.text_mode_combo.addItems(["", "Left", "Right", "Up", "Down", "Blink", "Breathe", "Snowflake", "Laser"])
self.layout.addWidget(self.text_mode_label)
self.layout.addWidget(self.text_mode_combo)
# Text Speed
self.text_speed_label = QLabel('Text Speed:')
self.text_speed_slider = QSlider(Qt.Horizontal)
self.text_speed_slider.setRange(0, 100)
self.layout.addWidget(self.text_speed_label)
self.layout.addWidget(self.text_speed_slider)
# Text Color Mode
self.text_color_mode_label = QLabel('Text Color Effect:')
self.text_color_mode_combo = QComboBox(self)
self.text_color_mode_combo.addItems(["", "Solid", "Rainbow", "Sunset", "Neon", "Calm"])
self.layout.addWidget(self.text_color_mode_label)
self.layout.addWidget(self.text_color_mode_combo)
# Text Color
self.text_color_button = QPushButton('Choose Text Color')
self.text_color_button.clicked.connect(self.choose_text_color)
self.layout.addWidget(self.text_color_button)
# Text Background Mode
self.text_bg_mode_label = QLabel('Background Style:')
self.text_bg_mode_combo = QComboBox(self)
self.text_bg_mode_combo.addItems(["None", "Solid"])
self.layout.addWidget(self.text_bg_mode_label)
self.layout.addWidget(self.text_bg_mode_combo)
# Text Background Color
self.text_bg_color_button = QPushButton('Choose Background Color')
self.text_bg_color_button.clicked.connect(self.choose_bg_color)
self.layout.addWidget(self.text_bg_color_button)
# Dialog buttons
self.button_box = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel, self)
self.button_box.accepted.connect(self.accept)
self.button_box.rejected.connect(self.reject)
self.layout.addWidget(self.button_box)
def choose_text_color(self):
color_dialog = QColorDialog(self)
if color_dialog.exec_() == QColorDialog.Accepted:
self.selected_text_color = color_dialog.selectedColor()
self.text_color_button.setStyleSheet(f"background-color: {self.selected_text_color.name()}")
def choose_bg_color(self):
color_dialog = QColorDialog(self)
if color_dialog.exec_() == QColorDialog.Accepted:
self.selected_bg_color = color_dialog.selectedColor()
self.text_bg_color_button.setStyleSheet(f"background-color: {self.selected_bg_color.name()}")
def get_settings(self):
if self.exec_() == QDialog.Accepted:
return (
self.text_input.text(),
int(self.text_size_combo.currentText()),
self.text_mode_combo.currentIndex(),
self.text_speed_slider.value(),
self.text_color_mode_combo.currentIndex(),
self.selected_text_color,
self.text_bg_mode_combo.currentIndex(),
self.selected_bg_color,
)
else:
return None
class ColorControlDialog(QDialog):
accepted = pyqtSignal(str, bool)
def __init__(self, parent=None, mac_address=None):
super().__init__(parent)
self.setWindowTitle('Color Control')
self.mac_address = mac_address
self.layout = QVBoxLayout(self)
# Initialize buttons
self.fullscreen_color_button = QPushButton('Fullscreen Color')
self.pixel_paint_button = QPushButton('Pixel Paint')
# Connect buttons to their respective slots
self.fullscreen_color_button.clicked.connect(lambda: self.choose_color(False))
self.pixel_paint_button.clicked.connect(self.open_pixel_paint_dialog)
# Add buttons to layout
self.layout.addWidget(self.fullscreen_color_button)
self.layout.addWidget(self.pixel_paint_button)
# Dialog button box (OK and Cancel)
self.button_box = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel, self)
self.button_box.accepted.connect(self.accept)
self.button_box.rejected.connect(self.reject)
self.layout.addWidget(self.button_box)
# Initialize variables for selected color and mode
self.selected_color = None
self.is_pixel_paint = False
def choose_color(self, is_pixel_paint):
color_dialog = QColorDialog(self)
if is_pixel_paint:
color_dialog.setOption(QColorDialog.ShowAlphaChannel)
if color_dialog.exec_() == QDialog.Accepted:
self.selected_color = color_dialog.selectedColor()
self.is_pixel_paint = is_pixel_paint
def open_pixel_paint_dialog(self):
dialog = PixelPaintDialog(self.mac_address)
dialog.exec_()
def accept(self):
if self.selected_color and not self.is_pixel_paint:
self.accepted.emit(self.selected_color.name(), self.is_pixel_paint)
super().accept()
class PixelPaintDialog(QDialog):
FAVORITES_KEY = "PixelPaintFavorites"
def __init__(self, mac_address):
super().__init__()
self.mac_address = mac_address
self.init_ui()
self.load_favorites()
self.finished.connect(self.save_favorites)
def init_ui(self):
self.setWindowTitle('Pixel Paint')
self.setGeometry(100, 100, 800, 800)
self.current_color = QColor(0, 0, 0)
self.grid_size = 32
self.cell_size = 20
self.grid = [[QColor(255, 255, 255) for _ in range(self.grid_size)] for _ in range(self.grid_size)]
self.undo_stack = []
self.current_stroke = []
self.setWindowIcon(QIcon('gui/idmc.ico'))
layout = QVBoxLayout()
toolbar_layout = QHBoxLayout()
self.color_buttons = []
common_colors = [
((254, 254, 254), "White"),
((255, 0, 0), "Red"),
((0, 255, 0), "Green"),
((0, 0, 255), "Blue"),
((255, 255, 0), "Yellow"),
((0, 255, 255), "Cyan"),
((255, 0, 255), "Magenta")
]
for color, label_text in common_colors:
button = QPushButton(label_text)
button.setProperty('color_data', color)
button.setStyleSheet(f"background-color: rgb{color}; border: 2px solid transparent;")
button.clicked.connect(lambda _, col=color: self.set_current_color(QColor(*col)))
toolbar_layout.addWidget(button)
self.color_buttons.append(button)
erase_button = QPushButton("Erase")
erase_button.setProperty('color_data', (255, 255, 255))
erase_button.setStyleSheet("background-color: white; border: 2px solid transparent;")
erase_button.clicked.connect(self.erase_cell)
toolbar_layout.addWidget(erase_button)
self.color_buttons.append(erase_button)
color_picker_button = QPushButton('Pick Color')
color_picker_button.clicked.connect(self.pick_color)
toolbar_layout.addWidget(color_picker_button)
layout.addLayout(toolbar_layout)
self.favorites_list = QListWidget()
self.favorites_list.setMaximumWidth(200)
self.favorites_list.setContextMenuPolicy(Qt.CustomContextMenu)
self.favorites_list.customContextMenuRequested.connect(self.show_favorites_context_menu)
button_layout = QHBoxLayout()
save_button = QPushButton('Save')
save_button.clicked.connect(self.save_grid)
button_layout.addWidget(save_button)
load_button = QPushButton('Load')
load_button.clicked.connect(self.load_grid)
button_layout.addWidget(load_button)
clear_button = QPushButton('Clear')
clear_button.clicked.connect(self.clear_grid)
button_layout.addWidget(clear_button)
undo_button = QPushButton('Undo')
undo_button.clicked.connect(self.undo)
button_layout.addWidget(undo_button)
layout.addLayout(button_layout)
self.grid_widget = QGridLayout()
self.grid_widget.setSpacing(0)
self.grid_labels = []
for row in range(self.grid_size):
row_labels = []
for col in range(self.grid_size):
label = QLabel()
label.setFixedSize(self.cell_size, self.cell_size)
label.setStyleSheet("border: 1px solid black; background-color: white;")
label.mousePressEvent = lambda event, r=row, c=col: self.paint_cell(r, c)
label.mouseMoveEvent = self.mouse_move_event
self.grid_widget.addWidget(label, row, col)
row_labels.append(label)
self.grid_labels.append(row_labels)
grid_container = QVBoxLayout()
grid_container.addLayout(self.grid_widget)
layout_with_favorites = QHBoxLayout()
layout_with_favorites.addWidget(self.favorites_list)
layout_with_favorites.addLayout(grid_container)
layout.addLayout(layout_with_favorites)
send_clear_layout = QHBoxLayout()
send_button = QPushButton('Send to Device')
send_button.clicked.connect(self.send_grid)
send_clear_layout.addWidget(send_button)
clear_device_button = QPushButton('Clear Device')
clear_device_button.clicked.connect(self.clear_device)
send_clear_layout.addWidget(clear_device_button)
layout.addLayout(send_clear_layout)
# Close button
close_button = QPushButton('Close')
close_button.clicked.connect(self.close)
layout.addWidget(close_button)
self.setLayout(layout)
def set_current_color(self, color):
self.current_color = color
for button in self.color_buttons:
if button.property('color_data') == (color.red(), color.green(), color.blue()):
button.setStyleSheet(f"background-color: rgb{color.red(), color.green(), color.blue()}; border: 2px solid gray;")
else:
button.setStyleSheet(f"background-color: rgb{button.property('color_data')}; border: 2px solid transparent;")
def pick_color(self):
color = QColorDialog.getColor()
if color.isValid():
self.set_current_color(color)
def erase_cell(self):
self.set_current_color(QColor(255, 255, 255))
def paint_cell(self, row, col):
if not self.current_stroke:
self.undo_stack.append([])
self.current_stroke.append((row, col))
self.grid[row][col] = self.current_color
self.update_cell_style(row, col)
def mouse_move_event(self, event):
if event.buttons() & Qt.LeftButton:
pos_in_dialog = self.mapFromGlobal(event.globalPos())
for row in range(self.grid_size):
for col in range(self.grid_size):
if self.grid_labels[row][col].geometry().contains(pos_in_dialog):
self.paint_cell(row, col)
return
def mousePressEvent(self, event):
if event.button() == Qt.LeftButton:
pos_in_dialog = self.mapFromGlobal(event.globalPos())
for row in range(self.grid_size):
for col in range(self.grid_size):
if self.grid_labels[row][col].geometry().contains(pos_in_dialog):
self.paint_cell(row, col)
return
def mouseReleaseEvent(self, event):
if self.current_stroke:
self.undo_stack[-1].extend(self.current_stroke)
self.current_stroke = []
def save_grid(self):
name, ok = QInputDialog.getText(self, 'Save Grid', 'Enter a name for this favorite:')
if ok and name:
self.favorites_list.addItem(name)
settings = QSettings("MyCompany", self.FAVORITES_KEY)
settings.setValue(name, self.grid)
self.save_favorites()
def load_grid(self):
current_item = self.favorites_list.currentItem()
if current_item:
name = current_item.text()
settings = QSettings("MyCompany", self.FAVORITES_KEY)
loaded_grid = settings.value(name)
if loaded_grid:
self.grid = loaded_grid
for row in range(self.grid_size):
for col in range(self.grid_size):
self.update_cell_style(row, col)
else:
QMessageBox.warning(self, 'Load Grid', f'Grid "{name}" not found.')
def clear_grid(self):
self.undo_stack.append(copy.deepcopy(self.grid))
for row in range(self.grid_size):
for col in range(self.grid_size):
self.grid[row][col] = QColor(255, 255, 255)
self.update_cell_style(row, col)
self.undo_stack[-1].append(None)
def undo(self):
if self.undo_stack:
stroke_to_undo = self.undo_stack.pop()
if stroke_to_undo and stroke_to_undo[-1] is None:
stroke_to_undo.pop()
self.grid = copy.deepcopy(stroke_to_undo)
for row in range(self.grid_size):
for col in range(self.grid_size):
self.update_cell_style(row, col)
else:
for row, col in stroke_to_undo:
self.grid[row][col] = QColor(255, 255, 255)
self.update_cell_style(row, col)
def send_grid(self):
commands = []
for row in range(self.grid_size):
for col in range(self.grid_size):
color = self.grid[row][col]
if color != QColor(255, 255, 255):
command = f"{col}-{row}-{color.red()}-{color.green()}-{color.blue()}"
commands.append(command)
if commands:
self.send_command_to_device(commands)
else:
QMessageBox.warning(self, 'Send Grid', 'No pixels to send.')
def send_command_to_device(self, commands):
command_array = ["--address", self.mac_address]
for command in commands:
command_array.extend(["--pixel-color", command])
self.run_command(command_array)
def send_clear_command_to_device(self, commands):
command_array = ["--address", self.mac_address]
self.run_command(command_array)
def run_command(self, command_array):
process = QProcess(self)
process.start("cmd.exe", ["/c", "py", "app.py"] + command_array)
process.waitForFinished()
def clear_device(self):
rgb_str = "0-0-0"
self.send_clear_command_to_device(["--fullscreen-color", rgb_str.replace('#', '')])
def update_cell_style(self, row, col):
color = self.grid[row][col]
border_style = "border: 1px solid black;" if color == QColor(255, 255, 255) else ""
self.grid_labels[row][col].setStyleSheet(f"background-color: {color.name()}; {border_style}")
def load_favorites(self):
settings = QSettings("MyCompany", self.FAVORITES_KEY)
favorite_names = settings.value("favoriteNames", [])
for name in favorite_names:
self.favorites_list.addItem(name)
def save_favorites(self):
settings = QSettings("MyCompany", self.FAVORITES_KEY)
favorite_names = [self.favorites_list.item(i).text() for i in range(self.favorites_list.count())]
settings.setValue("favoriteNames", favorite_names)
def show_favorites_context_menu(self, pos):
item = self.favorites_list.itemAt(pos)
if item:
menu = QMenu(self)
load_action = menu.addAction("Load")
delete_action = menu.addAction("Delete")
action = menu.exec_(self.favorites_list.mapToGlobal(pos))
if action == load_action:
self.load_grid()
elif action == delete_action:
self.delete_favorite(item)
def delete_favorite(self, item):
name = item.text()
settings = QSettings("MyCompany", self.FAVORITES_KEY)
settings.remove(name)
self.favorites_list.takeItem(self.favorites_list.row(item))
self.save_favorites()
class ScoreboardDialog(QDialog):
def __init__(self, device_page):
super().__init__()
self.device_page = device_page
self.left_score = 0
self.right_score = 0
self.init_ui()
def init_ui(self):
self.setWindowTitle("Scoreboard")
self.setWindowIcon(QIcon('gui/idmc.ico')) # Set title bar icon
self.setFixedSize(300, 200)
layout = QGridLayout()
# Score Labels (Centered)
self.left_score_label = QLabel(str(self.left_score))
self.left_score_label.setAlignment(Qt.AlignCenter) # Center the label horizontally
self.left_score_label.setFont(QFont("Arial", 36, QFont.Bold))
layout.addWidget(self.left_score_label, 1, 0)
self.right_score_label = QLabel(str(self.right_score))
self.right_score_label.setAlignment(Qt.AlignCenter) # Center the label horizontally
self.right_score_label.setFont(QFont("Arial", 36, QFont.Bold))
layout.addWidget(self.right_score_label, 1, 1) # Changed column to 1 for centering
# Increment Buttons (Above)
left_inc_button = QPushButton("+1")
left_inc_button.clicked.connect(lambda: self.adjust_score(True, True)) # True for left, True for increment
layout.addWidget(left_inc_button, 0, 0) # Row 0 for above
right_inc_button = QPushButton("+1")
right_inc_button.clicked.connect(lambda: self.adjust_score(False, True)) # False for right, True for increment
layout.addWidget(right_inc_button, 0, 1) # Row 0 for above
# Decrement Buttons (Below)
left_dec_button = QPushButton("-1")
left_dec_button.clicked.connect(lambda: self.adjust_score(True, False)) # True for left, False for decrement
layout.addWidget(left_dec_button, 2, 0) # Row 2 for below
right_dec_button = QPushButton("-1")
right_dec_button.clicked.connect(lambda: self.adjust_score(False, False)) # False for right, False for decrement
layout.addWidget(right_dec_button, 2, 1) # Row 2 for below
self.setLayout(layout)
def adjust_score(self, is_left, is_inc):
if is_left:
if is_inc or self.left_score > 0: # Increment only if score is above 0 or we are incrementing
self.left_score += 1 if is_inc else -1
self.left_score_label.setText(str(self.left_score))
else:
if is_inc or self.right_score > 0: # Increment only if score is above 0 or we are incrementing
self.right_score += 1 if is_inc else -1
self.right_score_label.setText(str(self.right_score))
self.send_score()
def send_score(self):
score_str = f"{self.left_score}-{self.right_score}"
self.device_page.run_command(["--address", self.device_page.mac_address, "--scoreboard", score_str])
# --- Main App Classes ---
class DevicePage(QWidget):
# --- UI Setup ---
def __init__(self, main_window, friendly_name, device_name, mac_address):
super().__init__()
self.main_window = main_window
self.friendly_name = friendly_name
self.device_name = device_name
self.mac_address = mac_address
self.first_output_received = False
self.last_command = None
self.clock_styles = ['Default', 'Christmas', 'Racing', 'Inverted Full Screen',
'Animated Hourglass', 'Frame 1', 'Frame 2', 'Frame 3']
self.init_ui()
self.flip_screen_state = False
def init_ui(self):
layout = QVBoxLayout()
layout.setContentsMargins(0, 0, 0, 0)
header_layout = QHBoxLayout()
header_layout.setContentsMargins(0, 0, 0, 0)
header_layout.setSpacing(0)
delete_button = QPushButton("Delete", self)
delete_button.clicked.connect(self.delete_device)
header_layout.addWidget(delete_button, 0)
label = QLabel(f" {self.friendly_name} MAC: {self.mac_address}", self)
label.setContentsMargins(0, 0, 0, 0)
header_layout.addWidget(label, 1)
back_button = QPushButton("Back", self)
back_button.clicked.connect(self.go_back_to_homepage)
header_layout.addWidget(back_button, 0)
layout.addLayout(header_layout)
# Grid layout for buttons
grid_layout = QGridLayout()
self.action_buttons = []
actions = [
("Clock Style", self.clock_control),
("Sync Time", self.sync_time),
("Set Time", self.set_time),
("Screen On", lambda: self.screen_control("on")),
("Screen Off", lambda: self.screen_control("off")),
("Stop Watch", self.chronograph_control),
("Countdown Timer", lambda: self.countdown_control(1)),
("Set Text", self.set_text),
("Color Studio", self.color_control),
("Scoreboard", self.open_scoreboard),
("Set Image", self.set_image),
("Set GIF", self.set_gif)
]
for index, (text, func) in enumerate(actions):
button = QPushButton(text, self)
button.clicked.connect(func)
grid_layout.addWidget(button, index // 5, index % 5)
self.action_buttons.append(button)
layout.addLayout(grid_layout)
console_widget = QWidget()
console_layout = QVBoxLayout(console_widget)
console_label = QLabel("Console:")
console_label.setFont(QFont("Arial", 12, QFont.Bold))
console_layout.addWidget(console_label)
self.console_output = QPlainTextEdit(self)
self.console_output.setReadOnly(True)
console_layout.addWidget(self.console_output)
layout.addWidget(console_widget)
self.setLayout(layout)
# --- Helpers ---
def go_back_to_homepage(self):
self.main_window.stacked_widget.setCurrentWidget(self.main_window.homepage)
def run_command(self, args):
self.console_output.clear()
self.process = QProcess(self)
self.process.setProcessChannelMode(QProcess.MergedChannels)
self.process.readyRead.connect(self.handle_ready_read)
self.process.finished.connect(self.process_finished)
self.process.start("cmd.exe", ["/c", "py", "app.py"] + args)
def handle_ready_read(self):
data = self.process.readAll()
stdout = bytes(data).decode("utf8").strip()
current_command = self.process.arguments()[2:]
output_lines = stdout.splitlines()
if output_lines:
first_line = output_lines[0]
rest_of_output = '\n'.join(output_lines[1:])
if self.last_command != current_command:
self.console_output.appendPlainText(f"Command: {' '.join(current_command)}\n")
self.last_command = current_command
self.console_output.appendPlainText(f"Output: {first_line}\n")
if rest_of_output:
self.console_output.appendPlainText(rest_of_output + '\n')
else:
self.console_output.appendPlainText(f"Command: {' '.join(current_command)}\nOutput:\n")
self.last_command = current_command
def process_finished(self):
pass
def hex_to_rgb(self, hex_color):
hex_color = hex_color.lstrip('#')
if len(hex_color) == 6:
return tuple(int(hex_color[i:i+2], 16) for i in (0, 2, 4))
elif len(hex_color) == 3:
return tuple(int(hex_color[i]*2, 16) for i in range(3))
else:
raise ValueError("Invalid hex color format")
def handle_color_control_accepted(self, color_name):
rgb_str = color_name.replace('#', '')
self.run_command(["--address", self.mac_address, "--fullscreen-color", rgb_str])
# --- Device Button Actions ---
def delete_device(self):
confirmation = QMessageBox.question(self, "Delete Device", f"Are you sure you want to delete {self.friendly_name}?",
QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
if confirmation == QMessageBox.Yes:
for i in range(self.main_window.configuration_page.device_list.count()):
item = self.main_window.configuration_page.device_list.item(i)
if item.toolTip() == self.mac_address:
self.main_window.configuration_page.device_list.takeItem(i)
break
button_to_remove = self.main_window.device_buttons.pop(self.mac_address, None)
if button_to_remove:
self.main_window.homepage_layout.removeWidget(button_to_remove)
button_to_remove.deleteLater()
del self.main_window.device_pages[self.mac_address]
self.main_window.save_device_settings()
self.main_window.stacked_widget.setCurrentWidget(self.main_window.homepage)
def clock_control(self):
dialog = ClockStyleDialog(self)
clock_style, show_date, show_24hr, color, ok_pressed = dialog.get_options()
if ok_pressed:
args = ["--address", self.mac_address, "--clock", str(self.clock_styles.index(clock_style)), "--sync-time"]
if show_date:
args.append("--clock-with-date")
if show_24hr:
args.append("--clock-24h")
if color:
rgb_str = f"{color.red()}-{color.green()}-{color.blue()}"
args.append(f"--clock-color={rgb_str}")
self.run_command(args)
def sync_time(self):
self.run_command(["--address", self.mac_address, "--sync-time"])
def set_time(self):
time, ok_pressed = QInputDialog.getText(self, "Set Time", "Enter the time (DD-MM-YYYY-HH:MM:SS):")
if ok_pressed:
self.run_command(["--address", self.mac_address, "--set-time", time])
pass
def screen_control(self, state):
self.run_command(["--address", self.mac_address, "--screen", state])
def chronograph_control(self):
options = ['Reset', '(Re)Start', 'Pause', 'Continue after Pause']
default_index = 1
option, ok_pressed = QInputDialog.getItem(self, "Stop Watch", "Select an option:", options, default_index, False)
if ok_pressed:
self.run_command(["--address", self.mac_address, "--chronograph", str(options.index(option))])
def countdown_control(self, state):
options = ['Disable', 'Start', 'Pause', 'Restart']
option, ok_pressed = QInputDialog.getItem(self, "Countdown Control",
"Select an option:", options, 0, False)
if ok_pressed and option == 'Start':
time, ok_pressed = QInputDialog.getText(self, "Set Time", "Enter the Countdown time:\n\n(eg: 5min. 30sec. = 5-30)")
else:
time = None
if ok_pressed:
command_args = ["--address", self.mac_address, "--countdown", str(options.index(option))]
if time is not None:
command_args.extend(["--countdown-time", time])
self.run_command(command_args)
else:
print("Countdown canceled, no command will be executed.")
def set_text(self):
dialog = TextStyleDialog(self)
settings = dialog.get_settings()
if settings is not None:
text, text_size, text_mode, text_speed, text_color_mode_index, text_color, text_bg_mode_index, text_bg_color = settings
text_mode = str(text_mode)
text_color_mode = str(text_color_mode_index)
text_bg_mode = str(text_bg_mode_index)
args = ["--address", self.mac_address, "--set-text", text, "--text-size", str(text_size), "--text-mode", text_mode, "--text-speed", str(text_speed), "--text-color-mode", text_color_mode]
if text_color:
rgb_text = f"{text_color.red()}-{text_color.green()}-{text_color.blue()}"
args.append(f"--text-color={rgb_text}")
if text_bg_color:
rgb_bg = f"{text_bg_color.red()}-{text_bg_color.green()}-{text_bg_color.blue()}"
args.append(f"--text-bg-color={rgb_bg}")
if text_bg_mode != "None":
args.append(f"--text-bg-mode={text_bg_mode}")
self.run_command(args)
else:
print("No text settings were provided.")
def color_control(self):
dialog = ColorControlDialog(self, self.mac_address)
if dialog.exec_() == QDialog.Accepted and dialog.selected_color:
r, g, b = dialog.selected_color.red(), dialog.selected_color.green(), dialog.selected_color.blue()
rgb_str = f"{r}-{g}-{b}"
self.run_command(["--address", self.mac_address, "--fullscreen-color", rgb_str])
def open_scoreboard(self):
dialog = ScoreboardDialog(self)
dialog.exec_()
def set_image(self):
options = QFileDialog.Options()
options |= QFileDialog.ReadOnly
file_path, _ = QFileDialog.getOpenFileName(self, "Select Image", "", "PNG Files (*.png);;All Files (*)", options=options)
if file_path:
# Image Size Selection Dialog
size_dialog = QDialog(self)
size_dialog.setWindowTitle("Pick Size")
layout = QVBoxLayout()
size_combo = QComboBox()
size_combo.addItems(["16x16", "32x32"])
layout.addWidget(size_combo)
button_box = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel)
button_box.accepted.connect(size_dialog.accept)
button_box.rejected.connect(size_dialog.reject)
layout.addWidget(button_box)
size_dialog.setLayout(layout)
if size_dialog.exec_() == QDialog.Accepted:
image_size = size_combo.currentText().split("x")[0]
self.run_command(["--address", self.mac_address, "--image", "true", "--set-image", file_path, "--process-image", image_size])
def set_gif(self):
confirmation = QMessageBox.question(self, "GIF Notice", "All GIFs are processed by default to ensure maximum compatibility. \n\nThis doesn't always work for all GIFs. \n\nGIFs closer to 32x32 or 16x16 have better chances of working.",
QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
if confirmation == QMessageBox.Yes:
options = QFileDialog.Options()
options |= QFileDialog.ReadOnly
file_path, _ = QFileDialog.getOpenFileName(self, "Select GIF", "", "GIF Files (*.gif);;All Files (*)", options=options)
if file_path:
size_dialog = QDialog(self)
size_dialog.setWindowTitle("Choose GIF Size")
layout = QVBoxLayout()
size_combo = QComboBox()
size_combo.addItems(["16x16", "32x32"])
layout.addWidget(size_combo)
button_box = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel)
button_box.accepted.connect(size_dialog.accept)
button_box.rejected.connect(size_dialog.reject)
layout.addWidget(button_box)
size_dialog.setLayout(layout)
if size_dialog.exec_() == QDialog.Accepted:
image_size = size_combo.currentText().split("x")[0]
self.run_command(["--address", self.mac_address, "--set-gif", file_path, "--process-gif", image_size])
class ConfigurationPage(QWidget):
def __init__(self, main_window):
super().__init__()
self.main_window = main_window
self.device_name = ""
self.layout = QHBoxLayout()
self.left_layout = QVBoxLayout()
console_label = QLabel("Console:")
console_label.setFont(QFont("Arial", 12, QFont.Bold))
self.left_layout.addWidget(console_label)
self.console_output = QPlainTextEdit(self)
self.console_output.setReadOnly(True)
self.left_layout.addWidget(self.console_output)
instruction_label = QLabel('Select a device from the list to add:')
instruction_label.setFont(QFont("Arial", 12, QFont.Bold))
self.left_layout.addWidget(instruction_label)
self.device_list = QListWidget()
self.device_list.itemClicked.connect(self.device_selected)
self.left_layout.addWidget(self.device_list)
self.layout.addLayout(self.left_layout)
self.right_layout = QVBoxLayout()
back_button = QPushButton('Back')
back_button.clicked.connect(self.go_back_to_homepage)
back_button.setFixedSize(150, 50)
self.right_layout.addWidget(back_button)
add_selected_device_button = QPushButton('Add Selected Device')
add_selected_device_button.clicked.connect(self.add_selected_device)
add_selected_device_button.setFixedSize(150, 50)
self.right_layout.addWidget(add_selected_device_button)
self.layout.addLayout(self.right_layout)
self.setLayout(self.layout)
def device_selected(self, item):
self.selected_device = item.text()
def add_selected_device(self):
if hasattr(self, 'selected_device'):
if self.selected_device in self.main_window.device_buttons:
QMessageBox.warning(self, 'Device Already Added', f'Device "{self.selected_device}" is already added to the homepage.')
return
dialog = CustomInputDialog(self)
friendly_name, ok = dialog.get_input()
if ok and friendly_name:
mac_address = self.device_list.currentItem().toolTip()
else:
mac_address = self.device_list.currentItem().toolTip()
friendly_name = self.selected_device
self.main_window.add_device_to_homepage(friendly_name, mac_address)
QMessageBox.information(self, 'Device Added', f'Device "{friendly_name}" has been added.')
self.main_window.save_device_settings()
else:
QMessageBox.warning(self, 'No Device Selected', 'Please select a device from the list.')
def go_back_to_homepage(self):
self.main_window.stacked_widget.setCurrentWidget(self.main_window.homepage)
def update_device_name(self, new_name):
self.device_name = new_name
def save_device_name(self):
self.main_window.save_device_settings()
class MainWindow(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle('iDotMatrix Controller')
self.setWindowIcon(QIcon('gui/idmc.ico'))
screen_geometry = QApplication.desktop().availableGeometry()
self.screen_center = screen_geometry.center()
self.layout = QVBoxLayout()
self.layout.setAlignment(Qt.AlignTop)
header_font = QFont("Arial", 18, QFont.Bold)
header_label = QLabel('iDotMatrix Controller')
header_label.setFont(header_font)
self.layout.addWidget(header_label)
self.stacked_widget = QStackedWidget()
self.layout.addWidget(self.stacked_widget)
self.homepage = QWidget()
self.homepage_layout = QVBoxLayout()
self.homepage_layout.setAlignment(Qt.AlignCenter)
add_device_button = QPushButton('Add Device')
add_device_button.setFixedSize(150, 50)
add_device_button.clicked.connect(self.show_configuration_page)
self.homepage_layout.addWidget(add_device_button)
self.homepage.setLayout(self.homepage_layout)
self.stacked_widget.addWidget(self.homepage)
self.configuration_page = ConfigurationPage(self)
self.stacked_widget.addWidget(self.configuration_page)
self.setLayout(self.layout)
self.resize(800, 600)
self.center_window()
self.device_pages = {}
self.device_buttons = {}
self.load_device_settings()
self.connect_device_buttons()
def center_window(self):
window_size = self.frameGeometry().size()
x = self.screen_center.x() - window_size.width() // 2
y = self.screen_center.y() - window_size.height() // 2
self.move(x, y)
def show_configuration_page(self):
self.stacked_widget.setCurrentWidget(self.configuration_page)
self.scan_for_devices()
def scan_for_devices(self):
self.configuration_page.console_output.clear()
self.output_str = ""
self.process = QProcess(self)
self.process.setProcessChannelMode(QProcess.MergedChannels)
self.process.readyRead.connect(self.handle_ready_read)
self.process.finished.connect(self.process_finished)
self.process.start("cmd.exe", ["/c", "py app.py --scan"])