1# ------------------------------------------------------------------------------ 2# Copyright (c) 2011, Enthought, Inc. 3# All rights reserved. 4# 5# This software is provided without warranty under the terms of the BSD 6# license included in LICENSE.txt and may be redistributed only 7# under the conditions described in the aforementioned license. The license 8# is also available online at http://www.enthought.com/licenses/BSD.txt 9# Thanks for using Enthought open source! 10# 11# Author: Evan Patterson 12# ------------------------------------------------------------------------------ 13 14""" Defines a Traits UI View that allows for the customization of Qt-specific 15 widget properties. 16""" 17 18# Standard library imports. 19import logging 20 21# System library imports. 22from pyface.qt import QtGui 23 24# Enthought library imports. 25from traits.api import File, List, Str 26from traitsui.view import View 27from io import open 28 29# Logger. 30logger = logging.getLogger(__name__) 31 32 33class QtView(View): 34 """ A View that allows the specification of Qt style sheets. 35 """ 36 37 #: An optional string containing a Qt style sheet. 38 style_sheet = Str() 39 40 #: An optional file path for a Qt style sheet. 41 style_sheet_path = File() 42 43 #: A list of trait names that defines the order for focus switching via 44 #: Tab/Shift+Tab. If the view contains multiple items for a specified trait 45 # name, the order is undefined. 46 tab_order = List(Str) 47 48 # ------------------------------------------------------------------------- 49 # Creates a UI user interface object: 50 # ------------------------------------------------------------------------- 51 52 def ui( 53 self, 54 context, 55 parent=None, 56 kind=None, 57 view_elements=None, 58 handler=None, 59 id="", 60 scrollable=None, 61 args=None, 62 ): 63 ui = super(QtView, self).ui( 64 context, parent, kind, view_elements, handler, id, scrollable, args 65 ) 66 67 if self.style_sheet: 68 ui.control.setStyleSheet(self.style_sheet) 69 70 if self.style_sheet_path: 71 try: 72 with open(self.style_sheet_path, "r", encoding="utf8") as f: 73 ui.control.setStyleSheet(f.read()) 74 except IOError: 75 logger.exception("Error loading Qt style sheet") 76 77 if len(self.tab_order) >= 2: 78 previous = self._get_editor_control(ui, self.tab_order[0]) 79 for i in range(1, len(self.tab_order)): 80 current = self._get_editor_control(ui, self.tab_order[i]) 81 QtGui.QWidget.setTabOrder(previous, current) 82 previous = current 83 84 return ui 85 86 # ------------------------------------------------------------------------- 87 # Private interface: 88 # ------------------------------------------------------------------------- 89 90 def _get_editor_control(self, ui, name): 91 control = None 92 editors = ui.get_editors(name) 93 if editors: 94 control = editors[0].control 95 else: 96 logger.warning("No item for '%s' trait" % name) 97 return control 98