|
| 1 | +# SPDX-License-Identifier: Apache-2.0 |
| 2 | +# Copyright 2026 Ague Samuel Amen |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | +""" |
| 17 | +LockDialog — Dialog pour gérer et reconstruire à partir de fichiers lock. |
| 18 | +""" |
| 19 | + |
| 20 | +from __future__ import annotations |
| 21 | + |
| 22 | +import os |
| 23 | +from pathlib import Path |
| 24 | +from typing import Any |
| 25 | + |
| 26 | +import yaml |
| 27 | +from PySide6.QtCore import Qt |
| 28 | +from PySide6.QtGui import QFont |
| 29 | +from PySide6.QtWidgets import ( |
| 30 | + QDialog, |
| 31 | + QHBoxLayout, |
| 32 | + QLabel, |
| 33 | + QListWidget, |
| 34 | + QListWidgetItem, |
| 35 | + QMessageBox, |
| 36 | + QPlainTextEdit, |
| 37 | + QPushButton, |
| 38 | + QSplitter, |
| 39 | + QVBoxLayout, |
| 40 | + QWidget, |
| 41 | +) |
| 42 | + |
| 43 | +from Core.Locking import load_yaml_file |
| 44 | + |
| 45 | + |
| 46 | +class LockDialog(QDialog): |
| 47 | + """Dialog pour lister les fichiers .lock.yml et relancer un build.""" |
| 48 | + |
| 49 | + def __init__(self, gui): |
| 50 | + super().__init__(gui) |
| 51 | + self.gui = gui |
| 52 | + self.setWindowTitle(gui.tr("Gestion des Verrous (Locks)", "Lock Management")) |
| 53 | + self.resize(900, 600) |
| 54 | + |
| 55 | + layout = QVBoxLayout(self) |
| 56 | + |
| 57 | + ws = getattr(self.gui, "workspace_dir", None) |
| 58 | + ws_label = QLabel(gui.tr(f"Workspace: {ws}", f"Workspace: {ws}")) |
| 59 | + ws_label.setStyleSheet("color: #888;") |
| 60 | + layout.addWidget(ws_label) |
| 61 | + |
| 62 | + splitter = QSplitter(Qt.Orientation.Horizontal) |
| 63 | + |
| 64 | + # Liste des verrous |
| 65 | + self.list_widget = QListWidget() |
| 66 | + self.list_widget.itemSelectionChanged.connect(self._on_selection_changed) |
| 67 | + splitter.addWidget(self.list_widget) |
| 68 | + |
| 69 | + # Détails du verrou |
| 70 | + self.details_view = QPlainTextEdit() |
| 71 | + self.details_view.setReadOnly(True) |
| 72 | + self.details_view.setFont(QFont("Consolas", 10)) |
| 73 | + splitter.addWidget(self.details_view) |
| 74 | + |
| 75 | + splitter.setSizes([300, 600]) |
| 76 | + layout.addWidget(splitter, 1) |
| 77 | + |
| 78 | + # Actions |
| 79 | + btn_layout = QHBoxLayout() |
| 80 | + self.btn_rebuild = QPushButton(gui.tr("Reconstruire (Rebuild)", "Rebuild")) |
| 81 | + self.btn_rebuild.setEnabled(False) |
| 82 | + self.btn_rebuild.clicked.connect(self._do_rebuild) |
| 83 | + |
| 84 | + self.btn_open_dir = QPushButton(gui.tr("Ouvrir dossier", "Open Directory")) |
| 85 | + self.btn_open_dir.clicked.connect(self._open_lock_dir) |
| 86 | + |
| 87 | + btn_close = QPushButton(gui.tr("Fermer", "Close")) |
| 88 | + btn_close.clicked.connect(self.close) |
| 89 | + |
| 90 | + btn_layout.addWidget(self.btn_rebuild) |
| 91 | + btn_layout.addWidget(self.btn_open_dir) |
| 92 | + btn_layout.addStretch(1) |
| 93 | + btn_layout.addWidget(btn_close) |
| 94 | + layout.addLayout(btn_layout) |
| 95 | + |
| 96 | + self._refresh_list() |
| 97 | + |
| 98 | + def _get_lock_dir(self) -> Path | None: |
| 99 | + ws = getattr(self.gui, "workspace_dir", None) |
| 100 | + if not ws: |
| 101 | + return None |
| 102 | + return Path(ws) / ".ark" / "lock" |
| 103 | + |
| 104 | + def _refresh_list(self): |
| 105 | + self.list_widget.clear() |
| 106 | + lock_dir = self._get_lock_dir() |
| 107 | + if not lock_dir or not lock_dir.exists(): |
| 108 | + return |
| 109 | + |
| 110 | + locks = sorted(lock_dir.glob("*.lock.yml"), key=lambda p: p.stat().st_mtime, reverse=True) |
| 111 | + for path in locks: |
| 112 | + item = QListWidgetItem(path.name) |
| 113 | + item.setData(Qt.ItemDataRole.UserRole, str(path)) |
| 114 | + self.list_widget.addItem(item) |
| 115 | + |
| 116 | + def _on_selection_changed(self): |
| 117 | + items = self.list_widget.selectedItems() |
| 118 | + if not items: |
| 119 | + self.details_view.clear() |
| 120 | + self.btn_rebuild.setEnabled(False) |
| 121 | + return |
| 122 | + |
| 123 | + path = items[0].data(Qt.ItemDataRole.UserRole) |
| 124 | + try: |
| 125 | + data = load_yaml_file(Path(path)) |
| 126 | + # Formatage simplifié pour l'affichage |
| 127 | + display = yaml.safe_dump(data, allow_unicode=True, sort_keys=False) |
| 128 | + self.details_view.setPlainText(display) |
| 129 | + self.btn_rebuild.setEnabled(True) |
| 130 | + except Exception as e: |
| 131 | + self.details_view.setPlainText(f"Error loading lock: {e}") |
| 132 | + self.btn_rebuild.setEnabled(False) |
| 133 | + |
| 134 | + def _do_rebuild(self): |
| 135 | + items = self.list_widget.selectedItems() |
| 136 | + if not items: |
| 137 | + return |
| 138 | + |
| 139 | + path = items[0].data(Qt.ItemDataRole.UserRole) |
| 140 | + answer = QMessageBox.question( |
| 141 | + self, |
| 142 | + self.gui.tr("Confirmer Rebuild", "Confirm Rebuild"), |
| 143 | + self.gui.tr( |
| 144 | + "Voulez-vous reconstruire le projet à partir de ce verrou ?", |
| 145 | + "Do you want to rebuild the project using this lock file?" |
| 146 | + ) |
| 147 | + ) |
| 148 | + if answer != QMessageBox.StandardButton.Yes: |
| 149 | + return |
| 150 | + |
| 151 | + self.close() |
| 152 | + # On délègue le rebuild à la fenêtre principale |
| 153 | + if hasattr(self.gui, "rebuild_from_lock"): |
| 154 | + self.gui.rebuild_from_lock(Path(path)) |
| 155 | + else: |
| 156 | + QMessageBox.critical(self, "Error", "rebuild_from_lock method not implemented in GUI") |
| 157 | + |
| 158 | + def _open_lock_dir(self): |
| 159 | + lock_dir = self._get_lock_dir() |
| 160 | + if not lock_dir or not lock_dir.exists(): |
| 161 | + return |
| 162 | + |
| 163 | + import platform |
| 164 | + import subprocess |
| 165 | + system = platform.system() |
| 166 | + if system == "Windows": |
| 167 | + os.startfile(str(lock_dir)) |
| 168 | + elif system == "Darwin": |
| 169 | + subprocess.run(["open", str(lock_dir)]) |
| 170 | + else: |
| 171 | + subprocess.run(["xdg-open", str(lock_dir)]) |
| 172 | + |
| 173 | + |
| 174 | +def open_lock_dialog(gui): |
| 175 | + """Helper pour ouvrir le dialog.""" |
| 176 | + dlg = LockDialog(gui) |
| 177 | + dlg.exec() |
0 commit comments