113 lines
3.8 KiB
Python
113 lines
3.8 KiB
Python
"""
|
|
Ribbon widget for pyPhotoAlbum
|
|
"""
|
|
|
|
from PyQt6.QtWidgets import QWidget, QTabWidget, QHBoxLayout, QVBoxLayout, QPushButton, QLabel, QFrame
|
|
from PyQt6.QtCore import Qt
|
|
|
|
|
|
class RibbonWidget(QWidget):
|
|
"""A ribbon-style toolbar using QTabWidget"""
|
|
|
|
def __init__(self, main_window, ribbon_config=None, parent=None):
|
|
super().__init__(parent)
|
|
self.main_window = main_window
|
|
|
|
# Use provided config or fall back to importing the old one
|
|
if ribbon_config is None:
|
|
from ribbon_config import RIBBON_CONFIG
|
|
self.ribbon_config = RIBBON_CONFIG
|
|
else:
|
|
self.ribbon_config = ribbon_config
|
|
|
|
# Main layout
|
|
main_layout = QVBoxLayout()
|
|
main_layout.setContentsMargins(0, 0, 0, 0)
|
|
main_layout.setSpacing(0)
|
|
self.setLayout(main_layout)
|
|
|
|
# Create tab widget
|
|
self.tab_widget = QTabWidget()
|
|
self.tab_widget.setDocumentMode(True)
|
|
main_layout.addWidget(self.tab_widget)
|
|
|
|
# Build ribbon from config
|
|
self._build_ribbon()
|
|
|
|
def _build_ribbon(self):
|
|
"""Build the ribbon UI from configuration"""
|
|
for tab_name, tab_config in self.ribbon_config.items():
|
|
tab_widget = self._create_tab(tab_config)
|
|
self.tab_widget.addTab(tab_widget, tab_name)
|
|
|
|
def _create_tab(self, tab_config):
|
|
"""Create a tab widget with groups and actions"""
|
|
tab_widget = QWidget()
|
|
tab_layout = QHBoxLayout()
|
|
tab_layout.setContentsMargins(5, 5, 5, 5)
|
|
tab_layout.setSpacing(10)
|
|
tab_widget.setLayout(tab_layout)
|
|
|
|
# Create groups
|
|
for group_config in tab_config.get("groups", []):
|
|
group_widget = self._create_group(group_config)
|
|
tab_layout.addWidget(group_widget)
|
|
|
|
# Add stretch to push groups to the left
|
|
tab_layout.addStretch()
|
|
|
|
return tab_widget
|
|
|
|
def _create_group(self, group_config):
|
|
"""Create a group of actions"""
|
|
group_widget = QFrame()
|
|
group_layout = QVBoxLayout()
|
|
group_layout.setContentsMargins(5, 5, 5, 5)
|
|
group_layout.setSpacing(5)
|
|
group_widget.setLayout(group_layout)
|
|
|
|
# Create actions layout
|
|
actions_layout = QHBoxLayout()
|
|
actions_layout.setSpacing(5)
|
|
|
|
for action_config in group_config.get("actions", []):
|
|
button = self._create_action_button(action_config)
|
|
actions_layout.addWidget(button)
|
|
|
|
group_layout.addLayout(actions_layout)
|
|
|
|
# Add group label
|
|
group_label = QLabel(group_config.get("name", ""))
|
|
group_label.setAlignment(Qt.AlignmentFlag.AlignCenter)
|
|
group_label.setStyleSheet("font-size: 10px; color: gray;")
|
|
group_layout.addWidget(group_label)
|
|
|
|
# Add separator frame
|
|
group_widget.setFrameShape(QFrame.Shape.Box)
|
|
group_widget.setFrameShadow(QFrame.Shadow.Sunken)
|
|
group_widget.setLineWidth(1)
|
|
|
|
return group_widget
|
|
|
|
def _create_action_button(self, action_config):
|
|
"""Create a button for an action"""
|
|
button = QPushButton(action_config.get("label", ""))
|
|
button.setToolTip(action_config.get("tooltip", ""))
|
|
button.setMinimumSize(60, 40)
|
|
|
|
# Connect to action
|
|
action_name = action_config.get("action")
|
|
if action_name:
|
|
button.clicked.connect(lambda: self._execute_action(action_name))
|
|
|
|
return button
|
|
|
|
def _execute_action(self, action_name):
|
|
"""Execute an action by calling the corresponding method on main window"""
|
|
if hasattr(self.main_window, action_name):
|
|
method = getattr(self.main_window, action_name)
|
|
if callable(method):
|
|
method()
|
|
else:
|
|
print(f"Warning: Action '{action_name}' not implemented in main window")
|