Galerie : photo centrée sur le fond blanc (QPushButton + icon)

Remplacé QLabel par QPushButton avec setIcon — l'icône est
automatiquement centrée horizontalement et verticalement.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-03-21 23:47:21 +01:00
parent 7b1f0686ea
commit 2ea9c12350

View File

@@ -32,22 +32,23 @@ class PhotoCard(QFrame):
layout.setContentsMargins(4, 4, 4, 4) layout.setContentsMargins(4, 4, 4, 4)
layout.setSpacing(4) layout.setSpacing(4)
# Conteneur miniature (pour superposer la pastille) # Miniature cliquable
thumb_container = QWidget() self.thumb_btn = QPushButton()
thumb_container.setFixedSize(200, 140) self.thumb_btn.setFixedSize(200, 140)
thumb_container.setCursor(Qt.CursorShape.PointingHandCursor) self.thumb_btn.setCursor(Qt.CursorShape.PointingHandCursor)
thumb_container.mousePressEvent = lambda e: self._edit() self.thumb_btn.setStyleSheet("""
QPushButton {
background: #f8f8f8;
border: none;
border-radius: 6px;
}
QPushButton:pressed { background: #eee; }
""")
self.thumb_btn.clicked.connect(self._edit)
thumb_layout = QVBoxLayout(thumb_container)
thumb_layout.setContentsMargins(0, 0, 0, 0)
thumb_layout.setSpacing(0)
self.thumb_label = QLabel()
self.thumb_label.setAlignment(Qt.AlignmentFlag.AlignCenter)
self.thumb_label.setFixedSize(200, 140)
self._pixmap_original = QPixmap(self.path) self._pixmap_original = QPixmap(self.path)
self._update_thumbnail() self._update_thumbnail()
thumb_layout.addWidget(self.thumb_label) layout.addWidget(self.thumb_btn)
# Pastille orange (ratio ≠ 3:2) # Pastille orange (ratio ≠ 3:2)
self.ratio_dot = QLabel() self.ratio_dot = QLabel()
@@ -59,11 +60,10 @@ class PhotoCard(QFrame):
} }
""") """)
self.ratio_dot.hide() self.ratio_dot.hide()
self.ratio_dot.setParent(thumb_container) self.ratio_dot.setParent(self.thumb_btn)
self.ratio_dot.move(182, 4) self.ratio_dot.move(182, 4)
self._check_ratio() self._check_ratio()
layout.addWidget(thumb_container)
# Barre du bas : − quantité + (centré) # Barre du bas : − quantité + (centré)
bar = QHBoxLayout() bar = QHBoxLayout()
@@ -153,30 +153,31 @@ class PhotoCard(QFrame):
self.qty_label.setStyleSheet("color: #333;") self.qty_label.setStyleSheet("color: #333;")
def _update_thumbnail(self): def _update_thumbnail(self):
"""Redimensionne la miniature pour remplir le label fixe.""" """Redimensionne la miniature et la centre sur le bouton."""
if self._pixmap_original.isNull(): if self._pixmap_original.isNull():
return return
w = self.thumb_label.width() w = self.thumb_btn.width()
h = self.thumb_label.height() h = self.thumb_btn.height()
if w > 10 and h > 10: if w > 10 and h > 10:
scaled = self._pixmap_original.scaled( scaled = self._pixmap_original.scaled(
w, h, w - 4, h - 4,
Qt.AspectRatioMode.KeepAspectRatio, Qt.AspectRatioMode.KeepAspectRatio,
Qt.TransformationMode.SmoothTransformation Qt.TransformationMode.SmoothTransformation
) )
self.thumb_label.setPixmap(scaled) self.thumb_btn.setIcon(QIcon(scaled))
self.thumb_btn.setIconSize(QSize(w - 4, h - 4))
def apply_responsive(self, window_height): def apply_responsive(self, window_height):
"""Adapte les tailles au responsive.""" """Adapte les tailles au responsive."""
thumb_h = max(100, int(window_height * 0.16)) thumb_h = max(100, int(window_height * 0.16))
thumb_w = max(140, int(thumb_h * 1.45)) thumb_w = max(140, int(thumb_h * 1.45))
self.thumb_label.setFixedSize(thumb_w, thumb_h) self.thumb_btn.setFixedSize(thumb_w, thumb_h)
self.thumb_label.parent().setFixedSize(thumb_w, thumb_h)
self._update_thumbnail() self._update_thumbnail()
# Pastille orange # Pastille orange
dot_size = max(10, int(window_height * 0.018)) dot_size = max(10, int(window_height * 0.018))
self.ratio_dot.setFixedSize(dot_size, dot_size) self.ratio_dot.setFixedSize(dot_size, dot_size)
self.ratio_dot.setParent(self.thumb_btn)
self.ratio_dot.move(thumb_w - dot_size - 4, 4) self.ratio_dot.move(thumb_w - dot_size - 4, 4)
self.ratio_dot.setStyleSheet(f""" self.ratio_dot.setStyleSheet(f"""
QLabel {{ QLabel {{
@@ -184,6 +185,7 @@ class PhotoCard(QFrame):
border-radius: {dot_size // 2}px; border-radius: {dot_size // 2}px;
}} }}
""") """)
self.ratio_dot.raise_()
btn_size = max(28, int(window_height * 0.04)) btn_size = max(28, int(window_height * 0.04))
font_size = max(12, int(window_height * 0.02)) font_size = max(12, int(window_height * 0.02))