11from loguru import logger
22
3- from qtpy import QtWidgets , QtCore
4- from .dock_widget import CloseButton , MinimizeButton
3+ from qtpy import QtWidgets
54
5+ from .tab_widget import ABTabWidget
66
7- class CentralTabBar (QtWidgets .QTabBar ):
8- """Custom tab bar for the CentralTabWidget."""
9-
10- def __init__ (self , parent = None ):
11- super ().__init__ (parent )
12- self .setMovable (True )
13- self .setDocumentMode (True )
147
15-
16- class CentralTabWidget (QtWidgets .QTabWidget ):
8+ class CentralTabWidget (ABTabWidget ):
179 """
1810 A custom QTabWidget that manages groups of tabs and their associated pages.
1911
2012 This widget allows for organizing tabs into groups, dynamically adding pages to groups,
2113 and ensuring that each page has a unique object name.
2214 """
2315
24- def __init__ (self , * args ):
25- """
26- Initialize the CentralTabWidget.
27-
28- Args:
29- *args: Additional positional arguments passed to the parent QTabWidget.
30- """
31- super ().__init__ (* args )
32-
33- # Use custom tab bar with close/minimize buttons
34- self .setTabBar (CentralTabBar (self ))
35-
36- def addTab (self , widget , label , show_minimize = False ):
37- """Override addTab to add custom buttons to each tab.
38-
39- Args:
40- widget: The widget to add as a tab
41- label: The label for the tab
42- show_minimize: If True, show minimize button; if False, show close button
43- """
44- index = super ().addTab (widget , label )
45- self ._add_tab_buttons (index , show_minimize )
46- return index
47-
48- def insertTab (self , index , widget , label , show_minimize = False ):
49- """Override insertTab to add custom buttons to each tab.
50-
51- Args:
52- index: The index at which to insert the tab
53- widget: The widget to add as a tab
54- label: The label for the tab
55- show_minimize: If True, show minimize button; if False, show close button
56- """
57- index = super ().insertTab (index , widget , label )
58- self ._add_tab_buttons (index , show_minimize )
59- return index
60-
61- def _add_tab_buttons (self , index , show_minimize = False ):
62- """Add close OR minimize button to a tab (mutually exclusive).
63-
64- Args:
65- index: The index of the tab
66- show_minimize: If True, show minimize button; otherwise show close button
67- """
68- widget = self .widget (index )
69- if not widget :
70- return
71-
72- # Create a widget to hold the button
73- button_widget = QtWidgets .QWidget ()
74- button_layout = QtWidgets .QHBoxLayout (button_widget )
75- button_layout .setContentsMargins (0 , 0 , 0 , 0 )
76- button_layout .setSpacing (2 )
77-
78- # Add either minimize or close button (mutually exclusive)
79- if show_minimize :
80- minimize_btn = MinimizeButton (button_widget )
81- minimize_btn .clicked .connect (lambda w = widget : self ._minimize_tab_by_widget (w ))
82- button_layout .addWidget (minimize_btn )
83- else :
84- close_btn = CloseButton (button_widget )
85- close_btn .clicked .connect (lambda w = widget : self ._close_tab_by_widget (w ))
86- button_layout .addWidget (close_btn )
87-
88- # Set the button widget on the tab
89- self .tabBar ().setTabButton (index , QtWidgets .QTabBar .ButtonPosition .RightSide , button_widget )
90-
91- def _close_tab_by_widget (self , widget ):
92- """Handle close button click using the widget reference."""
93- index = self .indexOf (widget )
94- if index >= 0 :
95- self .tabCloseRequested .emit (index )
96-
97- def _minimize_tab_by_widget (self , widget ):
98- """Handle minimize button click using the widget reference."""
99- index = self .indexOf (widget )
100- if index >= 0 :
101- self .tabCloseRequested .emit (index )
102-
10316 @property
10417 def groups (self ):
10518 """
@@ -152,7 +65,7 @@ def addToGroup(self, group: str, page: QtWidgets.QWidget):
15265 page .deleteLater () # Clean up the newly created page since it already exists
15366
15467
155- class GroupTabWidget (QtWidgets . QTabWidget ):
68+ class GroupTabWidget (ABTabWidget ):
15669 """
15770 A custom QTabWidget that represents a group of tabs.
15871
@@ -169,9 +82,6 @@ def __init__(self, name: str, *args):
16982 *args: Additional positional arguments passed to the parent QTabWidget.
17083 """
17184 super ().__init__ (* args )
172- self .setMovable (True ) # Allow tabs to be rearranged.
173- self .setTabsClosable (True ) # Allow tabs to be closed.
174- self .setDocumentMode (True ) # Enable document mode for a more modern appearance.
17585
17686 self .setObjectName (name ) # Set the object name for the widget.
17787
0 commit comments