LED-mapper/localization.py

99 lines
4.0 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from __future__ import annotations
from PySide6.QtWidgets import QDialogButtonBox, QMessageBox, QWidget
_DIALOG_BUTTON_TEXTS = {
QDialogButtonBox.StandardButton.Ok: "ОК",
QDialogButtonBox.StandardButton.Cancel: "Отмена",
QDialogButtonBox.StandardButton.Yes: "Да",
QDialogButtonBox.StandardButton.No: "Нет",
QDialogButtonBox.StandardButton.Save: "Сохранить",
QDialogButtonBox.StandardButton.SaveAll: "Сохранить все",
QDialogButtonBox.StandardButton.Open: "Открыть",
QDialogButtonBox.StandardButton.Close: "Закрыть",
QDialogButtonBox.StandardButton.Apply: "Применить",
QDialogButtonBox.StandardButton.Reset: "Сбросить",
QDialogButtonBox.StandardButton.RestoreDefaults: "По умолчанию",
QDialogButtonBox.StandardButton.Discard: "Не сохранять",
QDialogButtonBox.StandardButton.Abort: "Прервать",
QDialogButtonBox.StandardButton.Retry: "Повторить",
QDialogButtonBox.StandardButton.Ignore: "Игнорировать",
}
_MESSAGE_BUTTON_TEXTS = {
QMessageBox.StandardButton.Ok: "ОК",
QMessageBox.StandardButton.Cancel: "Отмена",
QMessageBox.StandardButton.Yes: "Да",
QMessageBox.StandardButton.No: "Нет",
QMessageBox.StandardButton.Save: "Сохранить",
QMessageBox.StandardButton.SaveAll: "Сохранить все",
QMessageBox.StandardButton.Open: "Открыть",
QMessageBox.StandardButton.Close: "Закрыть",
QMessageBox.StandardButton.Apply: "Применить",
QMessageBox.StandardButton.Reset: "Сбросить",
QMessageBox.StandardButton.RestoreDefaults: "По умолчанию",
QMessageBox.StandardButton.Discard: "Не сохранять",
QMessageBox.StandardButton.Abort: "Прервать",
QMessageBox.StandardButton.Retry: "Повторить",
QMessageBox.StandardButton.Ignore: "Игнорировать",
}
def localize_dialog_buttons(button_box: QDialogButtonBox) -> None:
for button in button_box.buttons():
standard_button = button_box.standardButton(button)
text = _DIALOG_BUTTON_TEXTS.get(standard_button)
if text:
button.setText(text)
def localize_message_box(message_box: QMessageBox) -> None:
for button in message_box.buttons():
standard_button = message_box.standardButton(button)
text = _MESSAGE_BUTTON_TEXTS.get(standard_button)
if text:
button.setText(text)
def _show_message(
parent: QWidget | None,
icon: QMessageBox.Icon,
title: str,
text: str,
buttons: QMessageBox.StandardButton = QMessageBox.StandardButton.Ok,
default_button: QMessageBox.StandardButton = QMessageBox.StandardButton.NoButton,
) -> QMessageBox.StandardButton:
message_box = QMessageBox(parent)
message_box.setIcon(icon)
message_box.setWindowTitle(title)
message_box.setText(text)
message_box.setStandardButtons(buttons)
if default_button != QMessageBox.StandardButton.NoButton:
message_box.setDefaultButton(default_button)
localize_message_box(message_box)
return QMessageBox.StandardButton(message_box.exec())
def show_information(parent: QWidget | None, title: str, text: str) -> QMessageBox.StandardButton:
return _show_message(parent, QMessageBox.Icon.Information, title, text)
def show_warning(parent: QWidget | None, title: str, text: str) -> QMessageBox.StandardButton:
return _show_message(parent, QMessageBox.Icon.Warning, title, text)
def show_critical(parent: QWidget | None, title: str, text: str) -> QMessageBox.StandardButton:
return _show_message(parent, QMessageBox.Icon.Critical, title, text)
def ask_question(parent: QWidget | None, title: str, text: str) -> QMessageBox.StandardButton:
return _show_message(
parent,
QMessageBox.Icon.Question,
title,
text,
QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No,
QMessageBox.StandardButton.No,
)