Skip to content

Commit ffd4665

Browse files
committed
Change from coloring tabs with paintEvent to setTabTextColor
Running in Houdini, the tab bar draws poorly, so remove overriding paintEvent, and instead use setTabTextColor and setTabTextToolTip. This means updating colors must be triggered, it doesn't come for free as in paintEvent.
1 parent 0d544f4 commit ffd4665

File tree

4 files changed

+86
-57
lines changed

4 files changed

+86
-57
lines changed

preditor/gui/drag_tab_bar.py

Lines changed: 40 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,13 @@
66

77
import six
88
from 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
1010
from 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

preditor/gui/group_tab_widget/one_tab_widget.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ def removeTab(self, index): # noqa: N802
8484
if hasattr(self, 'default_tab') and not self.count():
8585
self.addTab(*self.default_tab())
8686
self.update_closable_tabs()
87+
self.window().updateTabColorsAndToolTips()
8788

8889
def showEvent(self, event): # noqa: N802
8990
super(OneTabWidget, self).showEvent(event)

preditor/gui/loggerwindow.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,10 @@ def __init__(self, parent, name=None, run_workbox=False, standalone=False):
299299
self.uiClosePreferencesBTN.clicked.connect(self.update_workbox_stack)
300300
self.uiClosePreferencesBTN.clicked.connect(self.update_window_settings)
301301

302+
# Preferences
303+
self.uiExtraTooltipInfoCHK.toggled.connect(self.updateTabColorsAndToolTips)
304+
self.updateTabColorsAndToolTips()
305+
302306
"""Set various icons"""
303307
self.uiClearLogACT.setIcon(QIcon(resourcePath('img/close-thick.png')))
304308
self.uiNewWorkboxACT.setIcon(QIcon(resourcePath('img/file-plus.png')))
@@ -646,6 +650,7 @@ def change_to_workbox_version_text(self, versionType):
646650
txt = "{} [{}/{}]".format(filename, idx, count)
647651
self.setStatusText(txt)
648652
self.autoHideStatusText()
653+
self.updateTabColorsAndToolTips()
649654

650655
def openSetPreferredTextEditorDialog(self):
651656
dlg = SetTextEditorPathDialog(parent=self)
@@ -1154,6 +1159,36 @@ def prefsPath(self, name='preditor_pref.json'):
11541159
path = prefs.prefs_path(name, core_name=self.name)
11551160
return path
11561161

1162+
def indexOfWorkboxOrTabGroup(self, widget):
1163+
"""For the given widget, the the index of it's tab widget that contains
1164+
it.
1165+
1166+
Args:
1167+
widget (GroupedTabWidget, WorkboxMixin): The workbox or tab group
1168+
for which to find it's index
1169+
1170+
Returns:
1171+
tabIdx (int, None): The found tab index or None
1172+
"""
1173+
tabIdx = None
1174+
if not (widget.parent() and widget.parent().parent()):
1175+
return tabIdx
1176+
1177+
grandParent = widget.parent().parent()
1178+
for index in range(grandParent.count()):
1179+
curWidget = grandParent.widget(index)
1180+
if curWidget == widget:
1181+
tabIdx = index
1182+
break
1183+
return tabIdx
1184+
1185+
def updateTabColorsAndToolTips(self):
1186+
"""Go thru all the tab groups and update their text color and toolTips."""
1187+
group = self.uiWorkboxTAB
1188+
for index in range(self.uiWorkboxTAB.count()):
1189+
grouped = group.widget(index)
1190+
grouped.tabBar().updateColorsAndToolTips()
1191+
11571192
def linkedFileChanged(self, filename):
11581193
"""Slot for responding to the file watcher's signal. Handle updating this
11591194
PrEditor instance accordingly.
@@ -1185,6 +1220,7 @@ def linkedFileChanged(self, filename):
11851220
filename = editor.__filename__()
11861221
if filename and Path(filename).is_file():
11871222
editor.__set_file_monitoring_enabled__(True)
1223+
self.updateTabColorsAndToolTips()
11881224

11891225
def closeEvent(self, event):
11901226
self.recordPrefs()
@@ -1848,6 +1884,7 @@ def setStyleSheet(self, stylesheet, recordPrefs=True):
18481884

18491885
# Notify widgets that the styleSheet has changed
18501886
self.styleSheetChanged.emit(stylesheet)
1887+
self.updateTabColorsAndToolTips()
18511888

18521889
def setCaseSensitive(self, state):
18531890
"""Set completer case-sensivity"""

preditor/gui/workbox_mixin.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
import chardet
1414
import Qt as Qt_py
15-
from Qt.QtCore import Qt
15+
from Qt.QtCore import Qt, Signal
1616
from Qt.QtWidgets import QMessageBox, QStackedWidget
1717

1818
from ..prefs import (
@@ -76,6 +76,8 @@ class WorkboxMixin(object):
7676
_warning_text = None
7777
"""When a user is picking this Workbox class, show a warning with this text."""
7878

79+
workboxSaved = Signal()
80+
7981
def __init__(
8082
self,
8183
parent=None,
@@ -120,6 +122,9 @@ def __init__(
120122
self.__set_changed_by_instance__(False)
121123
self._changed_saved = False
122124

125+
self.textChanged.connect(self._tab_widget.tabBar().updateColorsAndToolTips)
126+
self.workboxSaved.connect(self._tab_widget.tabBar().updateColorsAndToolTips)
127+
123128
def __auto_reload_on_change__(self):
124129
"""Whether the option to auto-reload linked files is set
125130
@@ -849,6 +854,8 @@ def __save_prefs__(
849854
self.__set_last_workbox_name__(self.__workbox_name__())
850855
self.__set_last_saved_text__(self.__text__())
851856

857+
self.workboxSaved.emit()
858+
852859
return ret
853860

854861
@classmethod

0 commit comments

Comments
 (0)