66
77import six
88from Qt .QtCore import QByteArray , QMimeData , QPoint , QRect , Qt
9- from Qt .QtGui import QColor , QCursor , QDrag , QPainter , QPalette , QPixmap , QRegion
9+ from Qt .QtGui import QColor , QCursor , QDrag , QPixmap , QRegion
1010from Qt .QtWidgets import (
1111 QApplication ,
1212 QFileDialog ,
1313 QInputDialog ,
1414 QMenu ,
1515 QSizePolicy ,
16- QStyle ,
17- QStyleOptionTab ,
1816 QTabBar ,
1917)
2018
@@ -48,16 +46,20 @@ class DragTabBar(QTabBar):
4846
4947 """
5048
49+ # the normalColor is set to an invalid color name. When QTabBar.setTabTextColor
50+ # is called with an invalid color name, it reverts to the QTabBar foreground
51+ # role instead
52+ normalColor = QColor ("invalidcolor" )
53+
5154 # These Qt Properties can be customized using style sheets.
52- normalColor = QtPropertyInit ('_normalColor' , QColor ("lightgrey" ))
53- linkedColor = QtPropertyInit ('_linkedColor' , QColor ("turquoise" ))
54- missingLinkedColor = QtPropertyInit ('_missingLinkedColor' , QColor ("red" ))
55- dirtyColor = QtPropertyInit ('_dirtyColor' , QColor ("yellow" ))
56- dirtyLinkedColor = QtPropertyInit ('_dirtyLinkedColor' , QColor ("goldenrod" ))
57- changedColor = QtPropertyInit ('_changedColor' , QColor ("darkorchid" ))
58- changedLinkedColor = QtPropertyInit ('_changedLinkedColor' , QColor ("darkviolet" ))
59- orphanedColor = QtPropertyInit ('_orphanedColor' , QColor ("crimson" ))
60- orphanedLinkedColor = QtPropertyInit ('_orphanedLinkedColor' , QColor ("firebrick" ))
55+ linkedColor = QtPropertyInit ('_linkedColor' , QColor ("grey" ))
56+ missingLinkedColor = QtPropertyInit ('_missingLinkedColor' , QColor ("grey" ))
57+ dirtyColor = QtPropertyInit ('_dirtyColor' , QColor ("grey" ))
58+ dirtyLinkedColor = QtPropertyInit ('_dirtyLinkedColor' , QColor ("grey" ))
59+ changedColor = QtPropertyInit ('_changedColor' , QColor ("grey" ))
60+ changedLinkedColor = QtPropertyInit ('_changedLinkedColor' , QColor ("grey" ))
61+ orphanedColor = QtPropertyInit ('_orphanedColor' , QColor ("grey" ))
62+ orphanedLinkedColor = QtPropertyInit ('_orphanedLinkedColor' , QColor ("grey" ))
6163
6264 def __init__ (self , parent = None , mime_type = 'DragTabBar' ):
6365 super (DragTabBar , self ).__init__ (parent = parent )
@@ -70,7 +72,7 @@ def __init__(self, parent=None, mime_type='DragTabBar'):
7072 self .fg_color_map = {}
7173 self .bg_color_map = {}
7274
73- def updateColors (self ):
75+ def updateColorMap (self ):
7476 """This cannot be called during __init__, otherwise all bg colors will
7577 be default, and not read from the style sheet. So instead, the first
7678 time we need self.bg_color_map, we check if it has values, and call this
@@ -92,7 +94,7 @@ def updateColors(self):
9294 "1" : "black" ,
9395 }
9496
95- def get_color_and_tooltip (self , index ):
97+ def getColorAndToolTip (self , index ):
9698 """Determine the color and tooltip based on the state of the workbox.
9799
98100 Args:
@@ -131,13 +133,13 @@ def get_color_and_tooltip(self, index):
131133 state = TabStates .OrphanedLinked
132134 toolTip = (
133135 "Linked workbox is either newly added, or orphaned by "
134- "saving in another PrEditor instance"
136+ "being removed in another PrEditor instance and saved. "
135137 )
136138 else :
137139 state = TabStates .Orphaned
138140 toolTip = (
139141 "Workbox is either newly added, or orphaned by "
140- "saving in another PrEditor instance"
142+ "being removed in another PrEditor instance and saved. "
141143 )
142144 elif widget .__is_dirty__ ():
143145 if filename :
@@ -199,52 +201,30 @@ def get_color_and_tooltip(self, index):
199201 color = self .bg_color_map .get (state )
200202 return color , toolTip
201203
202- def paintEvent (self , event ):
203- """Override of .paintEvent to handle custom tab colors and toolTips.
204-
205- If self.bg_color_map has not yet been populated, do so by calling
206- self.updateColors(). We do not call self.updateColor in this class's
207- __init__ because the QtPropertyInit won't be able to read the properties
208- from the stylesheet at that point.
204+ def updateColorAndToolTip (self , index ):
205+ """Update the color and tooltip for the tab at index, based of various
206+ factors about the workbox.
209207
210208 Args:
211- event (QEvent ): The event passed to this event handler.
209+ index (int ): The index of the tab to color, and possibly, set toolTip
212210 """
213- if not self .bg_color_map :
214- self .updateColors ()
215-
216- style = self .style ()
217- painter = QPainter (self )
218- option = QStyleOptionTab ()
219-
220- isLight = self .normalColor .value () >= 128
211+ self .updateColorMap ()
221212
222- # Update the the parent GroupTabWidget
223- self .parent ().parent ().parent ().update ()
213+ color , toolTip = self .getColorAndToolTip (index )
214+ self .setTabTextColor (index , color )
215+ self .setTabToolTip (index , toolTip )
224216
217+ def updateColorsAndToolTips (self ):
218+ """Update the color and tooltip for all the tabs in this tabBar. Also,
219+ update the tabBar above it.
220+ """
225221 for index in range (self .count ()):
226- # color_name, toolTip = self.get_color_and_tooltip(index)
227- color , toolTip = self .get_color_and_tooltip (index )
228- self .setTabToolTip (index , toolTip )
229-
230- # Get colors
231- # color = QColor(color_name)
232- if isLight :
233- fillColor = color .lighter (175 )
234- color = color .darker (250 )
235- else :
236- fillColor = color .darker (250 )
237- color = color .lighter (175 )
238- # Pick white or black for text, based on lightness of fillColor
239- fg_idx = int (fillColor .value () >= 128 )
240- fg_color_name = self .fg_color_map .get (str (fg_idx ))
241- fg_color = QColor (fg_color_name )
242-
243- self .initStyleOption (option , index )
244- option .palette .setColor (QPalette .ColorRole .WindowText , fg_color )
245- option .palette .setColor (QPalette .ColorRole .Window , color )
246- option .palette .setColor (QPalette .ColorRole .Button , fillColor )
247- style .drawControl (QStyle .ControlElement .CE_TabBarTab , option , painter )
222+ self .updateColorAndToolTip (index )
223+
224+ parentIdx = self .window ().indexOfWorkboxOrTabGroup (self .parent ())
225+ if parentIdx is not None :
226+ tabBar = self .parent ().__tab_widget__ ().tabBar ()
227+ tabBar .updateColorAndToolTip (parentIdx )
248228
249229 def mouseMoveEvent (self , event ): # noqa: N802
250230 if not self ._mime_data :
@@ -365,6 +345,7 @@ def rename_tab(self):
365345
366346 if success :
367347 self .setTabText (self ._context_menu_tab , name )
348+ self .updateColorsAndToolTips ()
368349
369350 def tab_menu (self , pos , popup = True ):
370351 """Creates the custom context menu for the tab bar. To customize the menu
@@ -455,6 +436,7 @@ def link_file(self, workbox):
455436
456437 name = Path (filename ).name
457438 self .setTabText (self ._context_menu_tab , name )
439+ self .updateColorsAndToolTips ()
458440 self .update ()
459441 self .window ().setWorkboxFontBasedOnConsole (workbox = workbox )
460442
@@ -481,6 +463,7 @@ def save_and_link_file(self, workbox):
481463 name = Path (filename ).name
482464
483465 self .setTabText (self ._context_menu_tab , name )
466+ self .updateColorsAndToolTips ()
484467 self .update ()
485468 self .window ().setWorkboxFontBasedOnConsole (workbox = workbox )
486469 workbox .__set_last_workbox_name__ (workbox .__workbox_name__ ())
@@ -507,6 +490,7 @@ def unlink_file(self, workbox):
507490 workbox .__set_filename__ ("" )
508491 name = self .parent ().default_title
509492 self .setTabText (self ._context_menu_tab , name )
493+ self .updateColorsAndToolTips ()
510494
511495 def copyFilename (self , workbox ):
512496 """Copy the given workbox's filename to the clipboard
0 commit comments