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
|
#------------------------------------------------------------------------------
# Copyright (c) 2011, Enthought, Inc.
# All rights reserved.
#
# This software is provided without warranty under the terms of the BSD
# license included in enthought/LICENSE.txt and may be redistributed only
# under the conditions described in the aforementioned license. The license
# is also available online at http://www.enthought.com/licenses/BSD.txt
# Thanks for using Enthought open source!
#
# Author: Evan Patterson
#------------------------------------------------------------------------------
""" Defines a Traits UI View that allows for the customization of Qt-specific
widget properties.
"""
# Standard library imports.
import logging
# System library imports.
from pyface.qt import QtGui
# Enthought library imports.
from traits.api import File, List, Str
from traitsui.view import View
# Logger.
logger = logging.getLogger(__name__)
class QtView(View):
""" A View that allows the specification of Qt style sheets.
"""
# An optional string containing a Qt style sheet.
style_sheet = Str
# An optional file path for a Qt style sheet.
style_sheet_path = File
# A list of trait names that defines the order for focus switching via
# Tab/Shift+Tab. If the view contains multiple items for a specified trait
# name, the order is undefined.
tab_order = List(Str)
#---------------------------------------------------------------------------
# Creates a UI user interface object:
#---------------------------------------------------------------------------
def ui(self, context, parent=None, kind=None, view_elements=None,
handler=None, id='', scrollable=None, args=None):
ui = super(QtView, self).ui(context, parent, kind, view_elements,
handler, id, scrollable, args)
if self.style_sheet:
ui.control.setStyleSheet(self.style_sheet)
if self.style_sheet_path:
try:
with open(self.style_sheet_path, 'r') as f:
ui.control.setStyleSheet(f.read())
except IOError:
logger.exception("Error loading Qt style sheet")
if len(self.tab_order) >= 2:
previous = self._get_editor_control(ui, self.tab_order[0])
for i in xrange(1, len(self.tab_order)):
current = self._get_editor_control(ui, self.tab_order[i])
QtGui.QWidget.setTabOrder(previous, current)
previous = current
return ui
#---------------------------------------------------------------------------
# Private interface:
#---------------------------------------------------------------------------
def _get_editor_control(self, ui, name):
control = None
editors = ui.get_editors(name)
if editors:
control = editors[0].control
else:
logger.warning("No item for '%s' trait" % name)
return control
|