forked from AG_QGIS/Plugin_SN_Basis
85 lines
2.4 KiB
Python
85 lines
2.4 KiB
Python
"""
|
||
sn_basis/functions/dialog_wrapper.py – Benutzer-Dialoge (Qt5/6/Mock-kompatibel)
|
||
"""
|
||
from typing import Any
|
||
from typing import Literal, Optional
|
||
from sn_basis.functions.qt_wrapper import (
|
||
QMessageBox, YES, NO, CANCEL, QT_VERSION, exec_dialog, ICON_QUESTION,
|
||
|
||
)
|
||
|
||
def ask_yes_no(
|
||
title: str,
|
||
message: str,
|
||
default: bool = True,
|
||
parent: Any = None,
|
||
) -> bool:
|
||
"""
|
||
Stellt Ja/Nein-Frage. Funktioniert in PyQt5/6 UND Mock-Modus.
|
||
"""
|
||
try:
|
||
if QT_VERSION == 0: # Mock-Modus
|
||
print(f"🔍 Mock-Modus: ask_yes_no('{title}') → {default}")
|
||
return default
|
||
|
||
# ✅ KORREKT: Verwende YES/NO-Aliase aus qt_wrapper!
|
||
buttons = YES | NO
|
||
default_button = YES if default else NO
|
||
|
||
result = QMessageBox.question(
|
||
parent, title, message, buttons, default_button
|
||
)
|
||
|
||
# ✅ int(result) == int(YES) funktioniert Qt5/6/Mock
|
||
print(f"DEBUG ask_yes_no: result={result}, YES={YES}, match={int(result) == int(YES)}")
|
||
return int(result) == int(YES)
|
||
|
||
except Exception as e:
|
||
print(f"⚠️ ask_yes_no Fehler: {e}")
|
||
return default
|
||
|
||
|
||
OverwriteDecision = Optional[Literal["overwrite", "append", "cancel"]]
|
||
|
||
|
||
def ask_overwrite_append_cancel_custom(
|
||
parent,
|
||
title: str,
|
||
message: str,
|
||
) -> Literal["overwrite", "append", "cancel"]:
|
||
"""Zeigt Dialog mit benutzerdefinierten Buttons: Überschreiben/Anhängen/Abbrechen.
|
||
|
||
Parameters
|
||
----------
|
||
parent :
|
||
Eltern-Widget oder None.
|
||
title : str
|
||
Dialog-Titel.
|
||
message : str
|
||
Hauptmeldung mit Erklärung.
|
||
|
||
Returns
|
||
-------
|
||
Literal["overwrite", "append", "cancel"]
|
||
Genaue Entscheidung des Nutzers.
|
||
"""
|
||
msg = QMessageBox(parent)
|
||
msg.setIcon(ICON_QUESTION)
|
||
msg.setWindowTitle(title)
|
||
msg.setText(message)
|
||
|
||
# Eigene Buttons mit exakten Texten
|
||
overwrite_btn = msg.addButton("Überschreiben", QMessageBox.ButtonRole.AcceptRole)
|
||
append_btn = msg.addButton("Anhängen", QMessageBox.ButtonRole.ActionRole)
|
||
cancel_btn = msg.addButton("Abbrechen", QMessageBox.ButtonRole.RejectRole)
|
||
|
||
exec_dialog(msg)
|
||
|
||
clicked = msg.clickedButton()
|
||
if clicked == overwrite_btn:
|
||
return "overwrite"
|
||
elif clicked == append_btn:
|
||
return "append"
|
||
else: # cancel_btn
|
||
return "cancel"
|