feat: apply visible three-second defaults to unattended prompts

This commit is contained in:
阿白叔 committed 2026-11-16 14:36:56 +08:00
1 parent e4875b4eca
commit efb8cae478
29 files changed
+675 -60

No files matched your search

@@ -8,6 +8,7 @@ from PySide6.QtWidgets import (QComboBox, QDialog, QFileDialog, QFormLayout,
from .workers import ServiceWorker
from .backup_notice import MANUAL_BACKUP_NOTICE
from . import timed_prompt
class RecoverySignals(QObject):
@@ -88,10 +89,10 @@ class BackupRecoveryDialog(QDialog):
except Exception as error:
self.status_label.setText(str(error))
return
if QMessageBox.question(self, '确认从备份恢复',
if timed_prompt.question(self, '确认从备份恢复',
f'{MANUAL_BACKUP_NOTICE}\n\n目标库:{request.lmc_file}\nCBB:{name}\n来源:{source}'
'\n\n将创建用于校验的隔离库副本,校验通过后替换目标工程。确认继续?',
QMessageBox.Yes | QMessageBox.No, QMessageBox.No) != QMessageBox.Yes:
QMessageBox.Yes | QMessageBox.No, QMessageBox.Yes) != QMessageBox.Yes:
return
self.busy = True
self.stop.clear()
+8 -5
View File
@@ -47,6 +47,8 @@ except ImportError:
from cbb_refresh_verify.ui.workers import ServiceWorker
from cbb_refresh_verify.library_reader import read_library_fast, scan_open_library_status
from cbb_refresh_verify.batch_status import authoritative_status_map
from .timed_prompt import arm_default
from . import timed_prompt
from cbb_refresh_verify.defaults import DEFAULT_TIMEOUT_SECONDS
from cbb_refresh_verify.network_path import LibraryPathLease, select_library_file
from cbb_refresh_verify.verify_runner import (VerifyRequest, preflight_skip_reason, run_verify,
@@ -1173,21 +1175,21 @@ class MainWindow(QWidget):
from .backup_notice import MANUAL_BACKUP_NOTICE
if self.busy or not self.loaded_path or self._native_backup_same_library() or not names:
return
if QMessageBox.question(self, '确认批量校验',
if timed_prompt.question(self, '确认批量校验',
f'{MANUAL_BACKUP_NOTICE}\n\n目标库:{self.loaded_path}\n'
f'将校验 {len(names)} 个可见非 READY CBB 并保存结果。确认继续?',
QMessageBox.Yes | QMessageBox.No, QMessageBox.No) == QMessageBox.Yes:
QMessageBox.Yes | QMessageBox.No, QMessageBox.Yes) == QMessageBox.Yes:
self._run(names)
def repair_icdb_all(self):
if self.busy or not self.loaded_path: return
names = self._maintenance_names()
if not names: return
answer = QMessageBox.question(self, 'ICDB 批量检查',
answer = timed_prompt.question(self, 'ICDB 批量检查',
f'当前中心库:{self.loaded_path}\n\n'
f'将检查 {len(names)} 个可见非 READY CBB'
f'{"(仅勾选项)" if self.selected_records() else ""}。\n'
'仅检查 ICDB 状态,不执行修复或回退。\n\n是否开始?', QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
'仅检查 ICDB 状态,不执行修复或回退。\n\n是否开始?', QMessageBox.Yes | QMessageBox.No, QMessageBox.Yes)
if answer == QMessageBox.Yes:
self._run(names, maintenance=True)
@@ -1557,6 +1559,8 @@ class MainWindow(QWidget):
if previous is not None:
previous.close()
summary = QMessageBox(self)
summary.setStandardButtons(QMessageBox.Close)
arm_default(summary, summary.button(QMessageBox.Close))
setattr(self, attribute, summary)
summary.setAttribute(Qt.WA_DeleteOnClose)
summary.finished.connect(lambda _: self._forget_result_summary(summary))
@@ -1669,7 +1673,6 @@ class MainWindow(QWidget):
self._resume_batch_action.clicked.connect(self._resume_released_batch)
self._resume_batch_action.setEnabled(False)
if self._reimport_offers:
self.summary.setStandardButtons(QMessageBox.Close)
self._reimport_summary_action = self.summary.addButton('选择副本恢复…', QMessageBox.ActionRole)
self._reimport_summary_action.clicked.connect(self.reimport_failed)
self._reimport_summary_action.setEnabled(False) # Enabled after the worker finishes.
@@ -9,6 +9,7 @@ from PySide6.QtWidgets import (QCheckBox, QDialog, QFileDialog, QHBoxLayout, QLa
from .workers import ServiceWorker
from .backup_notice import MANUAL_BACKUP_NOTICE
from . import timed_prompt
from ..management import run_management_batch
RESULT_LABELS = {
@@ -163,10 +164,10 @@ class ManagementDialog(QDialog):
if self.recovery_from:
description = ('将用所选副本替换这些 CBB。操作会先移除原条目,再导入副本;'
'导入失败时需要用备份恢复。请确认副本版本。')
answer = QMessageBox.question(self, '确认管理操作',
answer = timed_prompt.question(self, '确认管理操作',
f'{MANUAL_BACKUP_NOTICE}\n{description}\n\n目标库:{self.template.library}\n{preview}\n\n'
'确认继续执行?', QMessageBox.Yes | QMessageBox.No,
QMessageBox.No)
QMessageBox.Yes)
if answer != QMessageBox.Yes:
return
self.busy = True; self.executed = True
+5 -2
View File
@@ -1,4 +1,5 @@
from PySide6.QtWidgets import QMessageBox
from .timed_prompt import arm_default
class PcbOpenDialog(QMessageBox):
@@ -13,9 +14,11 @@ class PcbOpenDialog(QMessageBox):
self.continue_button = self.addButton('确认无人使用,继续打开', QMessageBox.ActionRole)
self.skip_button = self.addButton('跳过本项', QMessageBox.ActionRole)
self.cancel_button = self.addButton('取消本批次', QMessageBox.RejectRole)
self.setDefaultButton(self.cancel_button)
self.setDefaultButton(self.skip_button)
self.setEscapeButton(self.cancel_button)
self.setModal(True)
self.buttonClicked.connect(lambda b: choice.choose('continue' if b is self.continue_button else
'skip' if b is self.skip_button else 'cancel'))
'skip' if b is self.skip_button else 'cancel',
origin=self.property('selectionOrigin') or 'user'))
self.finished.connect(lambda _: choice.choose('cancel'))
arm_default(self, self.skip_button)
@@ -1,5 +1,6 @@
"""Show the native version dates without preselecting a recovery version."""
"""Prefer the authorized last manually saved version after a visible countdown."""
from PySide6.QtWidgets import QMessageBox
from .timed_prompt import arm_default
class PcbRestoreDialog(QMessageBox):
@@ -14,7 +15,7 @@ class PcbRestoreDialog(QMessageBox):
'原生程序检测到上次异常退出,需要选择 PCB 恢复版本。\n'
f'最后手动保存:{manual["date"]}\n自动保存:{auto["date"]}\n\n'
'选择历史版本可能丢失此版本之后的修改。选择后继续校验、保存和状态确认;恢复版本仍需核对。'
'\n取消将停止本项并保留原生现场,不自动选取版本。')
'\n默认选择最后手动保存;该版本不可用时取消本项。')
self.manual_button = self.addButton(f'最后手动保存({manual["date"]})', QMessageBox.ActionRole)
self.auto_button = self.addButton(f'自动保存({auto["date"]})', QMessageBox.ActionRole)
self.manual_button.setEnabled(manual['enabled'])
@@ -25,7 +26,9 @@ class PcbRestoreDialog(QMessageBox):
self.buttonClicked.connect(self._chosen)
self.finished.connect(lambda _: choice.choose('cancel'))
self.setModal(True)
arm_default(self, self.manual_button if manual['enabled'] else self.cancel_button)
def _chosen(self, button):
self.choice.choose('last_user_saved' if button is self.manual_button else
'autosave' if button is self.auto_button else 'cancel')
'autosave' if button is self.auto_button else 'cancel',
origin=self.property('selectionOrigin') or 'user')
@@ -7,6 +7,8 @@ from PySide6.QtWidgets import (QDialog, QVBoxLayout, QHBoxLayout, QLabel, QPushB
QTableWidget, QTableWidgetItem, QHeaderView, QMessageBox)
from .workers import ServiceWorker
from .timed_prompt import arm_default
from . import timed_prompt
from ..residual_sessions import force_block_reason, release_targets_complete
@@ -128,6 +130,8 @@ class ResidualReleaseDialog(QDialog):
details.extend(c.get('text', '') for c in window.get('children', [])
if c.get('class') == 'Static')
box.setDetailedText('\n'.join(details))
box.setStandardButtons(QMessageBox.Close)
arm_default(box, box.button(QMessageBox.Close))
box.show()
def cancel_native(self):
@@ -136,10 +140,10 @@ class ResidualReleaseDialog(QDialog):
if row < 0: return
session = self.plan['sessions'][row]
if not session.get('cancel_prompt'): return
answer = QMessageBox.question(self, '请求原生取消',
answer = timed_prompt.question(self, '请求原生取消',
f'请求 {session["name"]} (PID {session["pid"]}) 当前显示的取消按钮?\n'
'取消后的工程状态仍需核对;不会强制结束进程。',
QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
QMessageBox.Yes | QMessageBox.No, QMessageBox.Yes)
if answer != QMessageBox.Yes: return
from cbb_refresh_verify.residual_sessions import request_native_cancel
self.before_release.emit(); self.stop.clear()
@@ -187,9 +191,9 @@ class ResidualReleaseDialog(QDialog):
session = self.plan['sessions'][row]
prompt = session.get('prompt')
if not prompt: return
choice = QMessageBox.question(self, '关闭工程前保存', prompt['text']+
choice = timed_prompt.question(self, '关闭工程前保存', prompt['text']+
'\n\n是:保存后关闭;否:放弃未保存更改;取消:保留会话。',
QMessageBox.Yes | QMessageBox.No | QMessageBox.Cancel, QMessageBox.Cancel)
QMessageBox.Yes | QMessageBox.No | QMessageBox.Cancel, QMessageBox.Yes)
code = {QMessageBox.Yes: 6, QMessageBox.No: 7, QMessageBox.Cancel: 2}[choice]
from cbb_refresh_verify.residual_sessions import answer_save_question
self._start(lambda: answer_save_question(session, code),
@@ -233,7 +237,7 @@ class ResidualReleaseDialog(QDialog):
targets = [r for r in self.plan['sessions'] if not force_block_reason(r)]
if not targets: return
names = '\n'.join(f'{r.get("project", "")} — {r["name"]} (PID {r["pid"]})' for r in targets)
answer = QMessageBox.warning(self, '强制结束客户端',
answer = timed_prompt.warning(self, '强制结束客户端',
'仅强制结束以下客户端。客户端中其他已打开工程也会退出;未保存的更改会丢失,正在写入的工程可能需要恢复。'
'\n数据库服务器和归属未知的进程不会被强制结束。\n\n'+names+
'\n\n仍要结束这些进程吗?', QMessageBox.Yes | QMessageBox.Cancel, QMessageBox.Cancel)
+96
View File
@@ -0,0 +1,96 @@
"""Visible, one-shot defaults for confirmation prompts in unattended batches."""
from math import ceil
import logging
from PySide6.QtCore import QElapsedTimer, QEvent, QObject, QTimer, Slot
from PySide6.QtWidgets import QMessageBox
DEFAULT_TIMEOUT_MS = 3000
class _Countdown(QObject):
def __init__(self, box, button):
super().__init__(box)
self.box, self.button = box, button
self.label = button.text()
self.duration = DEFAULT_TIMEOUT_MS
self.elapsed = QElapsedTimer()
self.timer = QTimer(self)
self.timer.setInterval(50)
self.timer.timeout.connect(self.tick)
self.done = False
box.setDefaultButton(button)
box.buttonClicked.connect(self.stop)
box.finished.connect(self.stop)
box.installEventFilter(self)
if box.isVisible():
self.start()
@Slot()
def start(self):
if self.done:
return
self.elapsed.start()
self.timer.start()
self.button.setText(f'{self.label}({ceil(self.duration / 1000)} 秒)')
def eventFilter(self, watched, event):
if event.type() == QEvent.Show:
self.start()
elif event.type() in (QEvent.Hide, QEvent.Close):
self.timer.stop()
return False
@Slot()
def stop(self):
self.done = True
self.timer.stop()
self.button.setText(self.label)
@Slot()
def tick(self):
if self.done or not self.box.isVisible():
self.timer.stop()
return
remaining = self.duration - self.elapsed.elapsed()
if remaining > 0:
self.button.setText(f'{self.label}({ceil(remaining / 1000)} 秒)')
return
self.timer.stop()
if not self.button.isEnabled():
self.box.setProperty('selectionOrigin', 'timeout_default')
self.box.reject()
return
self.box.setProperty('selectionOrigin', 'timeout_default')
self.button.click()
def arm_default(box, button):
countdown = _Countdown(box, button)
box._default_countdown = countdown
return countdown
def _ask(icon, parent, title, text, buttons, default):
box = QMessageBox(icon, title, text, buttons, parent)
arm_default(box, box.button(default))
try:
result = QMessageBox.StandardButton(box.exec())
owner = parent
while owner is not None and getattr(owner, 'logging_context', None) is None:
owner = owner.parentWidget()
context = getattr(owner, 'logging_context', None)
logger = context.logger if context is not None else logging.getLogger('cbb_refresh_verify')
logger.info('prompt_decision title=%s result=%s origin=%s', title, result.name,
box.property('selectionOrigin') or 'user')
return result
finally:
box.deleteLater()
def question(parent, title, text, buttons, default):
return _ask(QMessageBox.Question, parent, title, text, buttons, default)
def warning(parent, title, text, buttons, default):
return _ask(QMessageBox.Warning, parent, title, text, buttons, default)
@@ -1,4 +1,5 @@
from PySide6.QtWidgets import QMessageBox
from .timed_prompt import arm_default
class WriteOwnerDialog(QMessageBox):
@@ -15,9 +16,11 @@ class WriteOwnerDialog(QMessageBox):
self.continue_button = self.addButton('已确认未使用此库,继续', QMessageBox.ActionRole)
self.skip_button = self.addButton('跳过本项', QMessageBox.ActionRole)
self.cancel_button = self.addButton('取消本批次', QMessageBox.RejectRole)
self.setDefaultButton(self.cancel_button)
self.setDefaultButton(self.skip_button)
self.setEscapeButton(self.cancel_button)
self.setModal(True)
self.buttonClicked.connect(lambda b: choice.choose('continue' if b is self.continue_button else
'skip' if b is self.skip_button else 'cancel'))
'skip' if b is self.skip_button else 'cancel',
origin=self.property('selectionOrigin') or 'user'))
self.finished.connect(lambda _: choice.choose('cancel'))
arm_default(self, self.skip_button)