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
|
#-------------------------------------------------------------------------------
#
# Copyright (c) 2008, 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
# Date: 06/24/2000
#
#-------------------------------------------------------------------------------
""" Traits UI button editor for SVG images.
"""
#-------------------------------------------------------------------------------
# Imports:
#-------------------------------------------------------------------------------
import os.path
from enthought.traits.api import Bool, Any, Str
from enthought.traits.ui.qt4.editor import Editor
from PyQt4 import QtCore, QtGui, Qt
# add the Qt's installed dir plugins to the library path so the iconengines
# plugin will be found:
qt_plugins_dir = os.path.join(os.path.dirname(Qt.__file__), 'plugins')
Qt.QCoreApplication.addLibraryPath(qt_plugins_dir)
#-------------------------------------------------------------------------------
# 'SVGButtonEditor' class:
#-------------------------------------------------------------------------------
class SVGButtonEditor(Editor):
icon = Any
toggled_icon = Any
toggle_label = Str
toggle_tooltip = Str
toggle_state = Bool
#---------------------------------------------------------------------------
# Editor interface
#---------------------------------------------------------------------------
def init(self, parent):
""" Finishes initializing the editor by creating the underlying toolkit
widget.
"""
self.icon = QtGui.QIcon(self.factory.filename)
if self.factory.toggle_filename:
self.toggled_icon = QtGui.QIcon(self.factory.toggle_filename)
if self.factory.toggle_label != '':
self.toggle_label = self.factory.toggle_label
else:
self.toggle_label = self.factory.label
if self.factory.toggle_tooltip != '':
self.toggle_tooltip = self.factory.toggle_tooltip
else:
self.toggle_tooltip = self.factory.tooltip
control = self.control = QtGui.QToolButton()
control.setAutoRaise(True)
control.setIcon(self.icon)
control.setText(self.factory.label)
control.setIconSize(QtCore.QSize(self.factory.width, self.factory.height))
if self.factory.label:
control.setToolButtonStyle(QtCore.Qt.ToolButtonTextUnderIcon)
else:
control.setToolButtonStyle(QtCore.Qt.ToolButtonIconOnly)
if self.factory.toggle:
control.setCheckable(True)
control.toggled.connect(self._toggle_button)
QtCore.QObject.connect(control, QtCore.SIGNAL('clicked()'),
self.update_object)
if self.factory.tooltip:
control.setToolTip(self.factory.tooltip)
else:
self.set_tooltip()
def prepare(self, parent):
""" Finishes setting up the editor. This differs from the base class
in that self.update_editor() is not called at the end, which
would fire an event.
"""
name = self.extended_name
if name != 'None':
self.context_object.on_trait_change(self._update_editor, name,
dispatch = 'ui')
self.init(parent)
self._sync_values()
def update_object (self):
""" Handles the user clicking the button by setting the factory value
on the object.
"""
self.value = self.factory.value
def update_editor(self):
""" Updates the editor when the object trait changes externally to the
editor.
"""
pass
def _toggle_button(self):
self.toggle_state = not self.toggle_state
if self.toggle_state and self.toggled_icon:
self.control.setIcon(self.toggled_icon)
self.control.setText(self.toggle_label)
self.control.setToolTip(self.toggle_tooltip)
elif not self.toggle_state and self.toggled_icon:
self.control.setIcon(self.icon)
self.control.setText(self.factory.label)
self.control.setToolTip(self.factory.tooltip)
|