示例#1
0
class SettingsDialog(QDialog, BaseUI):
    """
    Dialog which allows the user to modify their preferences for PyMODA.
    """
    def __init__(self):
        self.settings = Settings()

        self.checkbox_default: QCheckBox = None
        self.line_cache_loc: QLineEdit = None
        self.btn_browse: QPushButton = None
        self.btn_open_logs: QPushButton = None

        super().__init__()

    def setup_ui(self) -> None:
        uic.loadUi(resources.get("layout:dialog_settings.ui"), self)

        self.btn_browse.clicked.connect(self.browse_for_folder)
        self.btn_open_logs.clicked.connect(self.on_open_logs_clicked)

        cache_loc = self.settings.get_pymodalib_cache()
        self.line_cache_loc.setText(
            cache_loc if cache_loc != "None" else cache_loc)

    def run(self) -> None:
        if QDialog.Accepted == self.exec():
            self.settings.set_pymodalib_cache(self.get_location())
            print("Settings saved.")
        else:
            print("Settings not saved.")

    def browse_for_folder(self) -> None:
        dialog = QFileDialog()
        dialog.setFileMode(QFileDialog.DirectoryOnly)

        if QDialog.Accepted == dialog.exec():
            cache_location = dialog.selectedFiles()[0]
            self.line_cache_loc.setText(cache_location)

    def get_location(self) -> Optional[str]:
        text = self.line_cache_loc.text()
        return text if text != "None" else None

    @staticmethod
    def on_open_logs_clicked() -> None:
        location = file_utils.pymoda_path

        if OS.is_windows():
            os.startfile(location)
        elif OS.is_linux():
            subprocess.Popen(["xdg-open", location])
        elif OS.is_mac_os():
            subprocess.Popen(["open", location])
示例#2
0
class PyMODAlibCacheDialog(QDialog, BaseUI):
    """
    A dialog which allows the sampling frequency to be entered.
    """

    def __init__(self):
        self.settings = Settings()

        self.checkbox_default: QCheckBox = None
        self.line_location: QLineEdit = None
        self.btn_browse: QPushButton = None

        super(PyMODAlibCacheDialog, self).__init__()

    def setup_ui(self) -> None:
        uic.loadUi(resources.get("layout:dialog_pymodalib_cache.ui"), self)

        self.btn_browse.clicked.connect(self.browse_for_folder)
        self.checkbox_default.toggled.connect(self.update_ok_status)
        self.line_location.textChanged.connect(self.update_ok_status)

        self.set_ok_enabled(False)

    def run(self) -> None:
        self.exec()
        self.settings.set_pymodalib_cache(self.get_location())

    def browse_for_folder(self) -> None:
        dialog = QFileDialog()
        dialog.setFileMode(QFileDialog.DirectoryOnly)

        if QDialog.Accepted == dialog.exec():
            cache_location = dialog.selectedFiles()[0]
            self.line_location.setText(cache_location)

            self.update_ok_status()

    def get_location(self) -> Optional[str]:
        if self.checkbox_default.isChecked():
            return None

        return self.line_location.text()

    def update_ok_status(self) -> None:
        line_location = self.get_location()
        default = self.checkbox_default.isChecked()

        for w in (self.btn_browse, self.line_location):
            w.setEnabled(not default)

        self.set_ok_enabled(
            default or bool(line_location and os.path.exists(line_location))
        )

    def set_ok_enabled(self, enabled: bool) -> None:
        """
        Sets the "ok" button in the dialog as enabled or disabled.

        :param enabled: whether to set the button as enabled, not disabled
        """
        self.buttonBox.button(QDialogButtonBox.Ok).setEnabled(enabled)