Skip to content
Snippets Groups Projects
sohstationviewer.py 2.24 KiB
Newer Older
Lan Dam's avatar
Lan Dam committed
#!/usr/bin/env python3
import platform
import os
import traceback

Kien Le's avatar
Kien Le committed
from PySide2 import QtWidgets
from PySide2.QtWidgets import QMessageBox
from sohstationviewer.view.main_window import MainWindow
from sohstationviewer.conf.config_processor import (
    ConfigProcessor,
    BadConfigError,
)
Kien Le's avatar
Kien Le committed
# Enable Layer-backing for MacOs version >= 11
# Only needed if using the pyside2 library with version>=5.15.
# Layer-backing is always enabled in pyside6.
os_name, version, *_ = platform.platform().split('-')
# if os_name == 'macOS' and version >= '11':
# mac OSX 11.6 appear to be 10.16 when read with python and still required this
# environment variable
if os_name == 'macOS':
    os.environ['QT_MAC_WANTS_LAYER'] = '1'


def main():
    app = QtWidgets.QApplication(sys.argv)
    wnd = MainWindow()
    config = ConfigProcessor()
    config.load_config()
    need_reset = False
    try:
        config.validate_config()
        config.apply_config(wnd)
    except (BadConfigError, ValueError) as e:
        bad_config_dialog = QMessageBox()
        bad_config_dialog.setText('Something went wrong when reading the '
                                  'config.')
        bad_config_dialog.setDetailedText(traceback.format_exc())
        bad_config_dialog.setInformativeText('Do you want to reset the config '
                                             'file?')
        bad_config_dialog.setStandardButtons(QMessageBox.Ok |
                                             QMessageBox.Close)
        bad_config_dialog.setDefaultButton(QMessageBox.Ok)
        bad_config_dialog.setIcon(QMessageBox.Critical)
        reset_choice = bad_config_dialog.exec_()
        if reset_choice == QMessageBox.Ok:
            need_reset = True
        else:
            sys.exit(1)
    if need_reset:
        try:
            config.reset()
        except OSError:
            QMessageBox.critical(None, 'Cannot reset config',
                                 'Config file cannot be reset.  Please '
                                 'ensure that it is not opened in another '
                                 'program.',
                                 QMessageBox.Close)
            sys.exit(1)
    config.apply_config(wnd)
Kien Le's avatar
Kien Le committed
    sys.exit(app.exec_())