Last active
May 10, 2023 19:40
-
-
Save fredrikaverpil/74fa824b506f67efaf15 to your computer and use it in GitHub Desktop.
Get and set QPalette data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from PySide import QtGui | |
#from PyQt4 import QtGui | |
import pickle | |
STYLE = 'plastique' | |
GROUPS = ['Disabled', 'Active', 'Inactive', 'Normal'] | |
ROLES = [ | |
'AlternateBase', | |
'Background', | |
'Base', | |
'Button', | |
'ButtonText', | |
'BrightText', | |
'Dark', | |
'Foreground', | |
'Highlight', | |
'HighlightedText', | |
'Light', | |
'Link', | |
'LinkVisited', | |
'Mid', | |
'Midlight', | |
'Shadow', | |
'ToolTipBase', | |
'ToolTipText', | |
'Text', | |
'Window', | |
'WindowText' | |
] | |
def getPaletteInfo(): | |
palette = QtGui.QApplication.palette() | |
# ColorGroups | |
groups = [] | |
for name in dir(QtGui.QPalette): | |
if isinstance(getattr(QtGui.QPalette, name), QtGui.QPalette.ColorGroup): | |
if name != 'All' and name != 'NColorGroups' and name != 'Current': | |
print 'ColorGroup: ' + name | |
groups.append(name) | |
# ColorRoles | |
roles = [] | |
for name in dir(QtGui.QPalette): | |
if isinstance(getattr(QtGui.QPalette, name), QtGui.QPalette.ColorRole): | |
if name != 'NColorRoles' and name != 'NoRole': | |
print 'ColorRole: ' + name | |
roles.append(name) | |
#build a dict with all the colors | |
result = {} | |
for role in ROLES: | |
for group in GROUPS: | |
qGrp = getattr(QtGui.QPalette, group) | |
qRl = getattr(QtGui.QPalette, role) | |
result['%s:%s' % (role, group)] = palette.color(qGrp, qRl).rgba() | |
return result | |
def setPaletteFromDct(dct): | |
palette = QtGui.QPalette() | |
for role in ROLES: | |
for group in GROUPS: | |
color = QtGui.QColor(dct['%s:%s' % (role, group)]) | |
qGrp = getattr(QtGui.QPalette, group) | |
qRl = getattr(QtGui.QPalette, role) | |
palette.setColor(qGrp, qRl, color) | |
QtGui.QApplication.setPalette(palette) | |
def setStylePlastique(): | |
QtGui.QApplication.setStyle(STYLE) | |
def setMayaTweaks(): | |
base_palette = QtGui.QApplication.palette() | |
# Set custom colors | |
LIGHT_COLOR = QtGui.QColor(100, 100, 100) | |
MID_COLOR = QtGui.QColor(68, 68, 68) | |
# Create a new palette | |
tab_palette = QtGui.QPalette(base_palette) | |
tab_palette.setBrush(QtGui.QPalette.Window, QtGui.QBrush(LIGHT_COLOR)) | |
tab_palette.setBrush(QtGui.QPalette.Button, QtGui.QBrush(MID_COLOR)) | |
# Define the widgets that needs tweaking | |
widget_palettes = {} | |
widget_palettes["QTabBar"] = tab_palette | |
widget_palettes["QTabWidget"] = tab_palette | |
# Set the new tweaked palette | |
for name, palette in widget_palettes.items(): | |
QtGui.QApplication.setPalette(palette, name) | |
def write_pickle(data): | |
# write | |
with open('c:/qpalette.pickle', 'wb') as handle: | |
pickle.dump(data, handle) | |
def read_pickle(): | |
# read | |
with open('c:/qpalette.pickle', 'rb') as handle: | |
data = pickle.load(handle) | |
return data | |
# Example: fetch palette data | |
data = getPaletteInfo() | |
print data | |
write_pickle(data) | |
# Example: read and set palette | |
#data = read_pickle() | |
#print data | |
#setPaletteFromDct(data) | |
#setStylePlastique() | |
#setMayaTweaks() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment