summaryrefslogtreecommitdiff
path: root/main.py
blob: 414d06fb5fb39b865fce257b2ffe7ea355ae249e (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
"""
main.py  —  LabUI entry point.

Run:
    python main.py

Requirements:
    pip install PyQt6 pyqtgraph numpy pyserial
"""

import sys, os
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))

from PyQt6.QtWidgets import QApplication
from PyQt6.QtCore import qInstallMessageHandler, QtMsgType, QEvent
from ui.main_window import MainWindow


class LabUIApplication(QApplication):
    """Catches the macOS "open with" launch event (QFileOpenEvent) for a
    double-clicked .labui file — delivered via this event, not argv, and
    can arrive before or after MainWindow exists, so it's buffered."""

    def __init__(self, argv):
        super().__init__(argv)
        self.file_to_open: str | None = None
        self.main_window = None

    def event(self, e):
        if e.type() == QEvent.Type.FileOpen:
            path = e.file()
            if self.main_window is not None:
                self.main_window.load_profile_path(path)
            else:
                self.file_to_open = path
            return True
        return super().event(e)


def _qt_message_handler(msg_type, context, message):
    """Silence one specific, harmless Qt warning: the native windowsvista
    style's combo-box/menu popup code calls QFont::setPointSize() on a
    pixel-sized font (our QSS uses px font-size, so pointSize() is -1 by
    design) — cosmetic console noise only, doesn't affect rendering.
    Everything else still prints normally."""
    if "QFont::setPointSize" in message:
        return
    if msg_type == QtMsgType.QtWarningMsg:
        print(f"Warning: {message}", file=sys.stderr)
    elif msg_type == QtMsgType.QtCriticalMsg:
        print(f"Critical: {message}", file=sys.stderr)
    elif msg_type == QtMsgType.QtFatalMsg:
        print(f"Fatal: {message}", file=sys.stderr)
    else:
        print(message, file=sys.stderr)


def main():
    qInstallMessageHandler(_qt_message_handler)
    app = LabUIApplication(sys.argv)
    app.setApplicationName("LabUI")

    qss = os.path.join(os.path.dirname(os.path.abspath(__file__)), "ui", "style_dark.qss")
    if os.path.exists(qss):
        with open(qss) as f:
            app.setStyleSheet(f.read())

    win = MainWindow()
    app.main_window = win
    win.show()

    # Opened via double-click / CLI argv (Windows, Linux) or the buffered
    # macOS FileOpen event (see LabUIApplication.event above).
    path = app.file_to_open or (sys.argv[1] if len(sys.argv) > 1 else None)
    if path and path.lower().endswith(".labui") and os.path.isfile(path):
        win.load_profile_path(path)

    sys.exit(app.exec())


if __name__ == "__main__":
    main()