/usr/share/pyshared/libqtopensesame/misc/theme.py is in opensesame 0.27.4-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 | #-*- coding:utf-8 -*-
"""
This file is part of OpenSesame.
OpenSesame is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenSesame is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with OpenSesame. If not, see <http://www.gnu.org/licenses/>.
"""
import os.path
import imp
from libopensesame import debug, misc
from libqtopensesame.misc import config
from PyQt4 import QtGui, QtCore
available_themes = [u'default', u'gnome']
class theme:
"""Handles the GUI theme"""
default_icon_size = 32
def __init__(self, main_window, theme=None):
"""
Constructor
Arguments:
main_window -- the main_window object
Keyword arguments:
theme -- the theme to be used or None to use config (default=None)
"""
self.main_window = main_window
if theme == None:
self.theme = config.get_config(u"theme")
else:
self.theme = theme
self.theme_folder = misc.resource(os.path.join(u"theme", \
self.theme))
debug.msg(u"theme = '%s' (%s)" % (self.theme, self.theme_folder))
if self.theme_folder == None or not os.path.exists(self.theme_folder):
debug.msg(u"theme '%s' does not exist, using 'default'" % theme, \
reason=u"warning")
self.theme = u"default"
self.theme_folder = misc.resource(os.path.join(u"theme", \
self.theme))
self.theme_info = os.path.join(self.theme_folder, u"__theme__.py")
if os.path.exists(self.theme_info):
info = imp.load_source(self.theme, self.theme_info)
self._qss = path = \
open(os.path.join(self.theme_folder, info.qss)).read()
self._icon_map = info.icon_map
self._icon_theme = info.icon_theme
self.load_icon_map()
self.apply_theme(self.main_window)
def apply_theme(self, widget):
"""
Apply the theme to a QWidget, i.e. load the stylesheet and the icons
Arguments:
widget -- a QWidget
"""
widget.setStyleSheet(self._qss)
if hasattr(widget, u"ui"):
self.load_icons(widget.ui)
def qicon(self, icon):
"""
Get an icon from the theme
Arguments:
icon -- the icon name
Returns:
A QIcon
"""
if icon in self.icon_map:
name, size = self.icon_map[icon]
else:
name = icon
return QtGui.QIcon.fromTheme(name, QtGui.QIcon(os.path.join( \
misc.resource(u"theme"), u"fallback.png")))
def qpixmap(self, icon, size=None):
"""
Get an icon from the theme
Arguments:
icon -- the icon name
Keyword arguments:
size -- the size of the icon or None for default (default=None)
Returns:
A QPixmap
"""
if size == None:
if icon in self.icon_map:
name, size = self.icon_map[icon]
else:
name = icon
size = self.default_icon_size
else:
if icon in self.icon_map:
name = self.icon_map[icon][0]
else:
name = icon
return QtGui.QIcon.fromTheme(name, QtGui.QIcon(os.path.join( \
misc.resource(u"theme"), u"fallback.png"))).pixmap(size)
def qlabel(self, icon):
"""
Get an icon from the theme
Arguments:
icon -- the icon name
Returns:
A QLabel
"""
l = QtGui.QLabel()
l.setPixmap(self.qpixmap(icon))
return l
def load_icon_map(self):
"""Load the icon map"""
if os.path.exists(os.path.join(self.theme_folder, self._icon_theme)):
debug.msg(u"using custom icon theme")
QtGui.QIcon.setThemeSearchPaths(QtGui.QIcon.themeSearchPaths() \
+ [self.theme_folder])
QtGui.QIcon.setThemeName(self._icon_theme)
else:
debug.msg(u"using default icon theme, icons may be missing", \
reason=u"warning")
self.icon_map = {}
path = os.path.join(self.theme_folder, self._icon_map)
debug.msg(path)
for l in open(path):
l = l.split(",")
if len(l) == 3:
try:
size = int(l[2])
except:
size = 32
alias = l[0].strip()
name = l[1].strip()
if alias in self.icon_map:
debug.msg(u"alias '%s' already in icon map, overwriting" % \
alias, reason=u"warning")
self.icon_map[alias] = name, size
def load_icons(self, ui):
"""
Add icons to all icon supporting widgets in a ui object
Arguments:
ui -- the ui object to load icons into
"""
debug.msg()
for i in dir(ui):
if i in self.icon_map:
a = getattr(ui, i)
if hasattr(a, u"setIcon"):
a.setIcon(self.qicon(i))
elif hasattr(a, u"setPixmap"):
a.setPixmap(self.qpixmap(i))
def set_toolbar_size(self, size):
"""
Control the size of the icons in the toolbar
Arguments:
size -- a size in pixels
"""
self.main_window.ui.toolbar_main.setIconSize(QtCore.QSize(size, size))
self.main_window.ui.toolbar_items.setIconSize(QtCore.QSize(size, size))
self.main_window.ui.toolbar_items.build()
|