/usr/share/pyshared/traitsui/qt4/clipboard.py is in python-traitsui 4.1.0-1.
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 | #------------------------------------------------------------------------------
# Copyright (c) 2007, Riverbank Computing Limited
# All rights reserved.
#
# This software is provided without warranty under the terms of the BSD license.
# However, when used with the GPL version of PyQt the additional terms described in the PyQt GPL exception also apply
#
# Author: Riverbank Computing Limited
#------------------------------------------------------------------------------
""" Implements a wrapper around the PyQt clipboard that handles Python objects
using pickle.
"""
#-------------------------------------------------------------------------------
# Imports:
#-------------------------------------------------------------------------------
from cPickle import dumps, load, loads
from cStringIO import StringIO
from pyface.qt import QtCore, QtGui
from traits.api import HasTraits, Instance, Property
#-------------------------------------------------------------------------------
# 'PyMimeData' class:
#-------------------------------------------------------------------------------
class PyMimeData(QtCore.QMimeData):
""" The PyMimeData wraps a Python instance as MIME data.
"""
# The MIME type for instances.
MIME_TYPE = 'application/x-ets-qt4-instance'
NOPICKLE_MIME_TYPE = 'application/x-ets-qt4-instance-no-pickle'
def __init__(self, data=None, pickle=True):
""" Initialise the instance.
"""
QtCore.QMimeData.__init__(self)
# Keep a local reference to be returned if possible.
self._local_instance = data
if pickle:
if data is not None:
# We may not be able to pickle the data.
try:
pdata = dumps(data)
except:
return
# This format (as opposed to using a single sequence) allows the
# type to be extracted without unpickling the data itself.
self.setData(self.MIME_TYPE, dumps(data.__class__) + pdata)
else:
self.setData(self.NOPICKLE_MIME_TYPE, str(id(data)))
@classmethod
def coerce(cls, md):
""" Coerce a QMimeData instance to a PyMimeData instance if possible.
"""
# See if the data is already of the right type. If it is then we know
# we are in the same process.
if isinstance(md, cls):
return md
# See if the data type is supported.
if not md.hasFormat(cls.MIME_TYPE):
return None
nmd = cls()
nmd.setData(cls.MIME_TYPE, md.data())
return nmd
def instance(self):
""" Return the instance.
"""
if self._local_instance is not None:
return self._local_instance
io = StringIO(str(self.data(self.MIME_TYPE)))
try:
# Skip the type.
load(io)
# Recreate the instance.
return load(io)
except:
pass
return None
def instanceType(self):
""" Return the type of the instance.
"""
if self._local_instance is not None:
return self._local_instance.__class__
try:
if self.hasFormat(self.MIME_TYPE):
return loads(str(self.data(self.MIME_TYPE)))
except:
pass
return None
#-------------------------------------------------------------------------------
# '_Clipboard' class:
#-------------------------------------------------------------------------------
class _Clipboard(HasTraits):
""" The _Clipboard class provides a wrapper around the PyQt clipboard.
"""
#---------------------------------------------------------------------------
# Trait definitions:
#---------------------------------------------------------------------------
# The instance on the clipboard (if any).
instance = Property
# Set if the clipboard contains an instance.
has_instance = Property
# The type of the instance on the clipboard (if any).
instance_type = Property
# The application clipboard.
clipboard = Instance(QtGui.QClipboard)
#---------------------------------------------------------------------------
# Instance property methods:
#---------------------------------------------------------------------------
def _get_instance(self):
""" The instance getter.
"""
md = PyMimeData.coerce(self.clipboard.mimeData())
if md is None:
return None
return md.instance()
def _set_instance(self, data):
""" The instance setter.
"""
self.clipboard.setMimeData(PyMimeData(data))
def _get_has_instance(self):
""" The has_instance getter.
"""
return self.clipboard.mimeData().hasFormat(PyMimeData.MIME_TYPE)
def _get_instance_type(self):
""" The instance_type getter.
"""
md = PyMimeData.coerce(self.clipboard.mimeData())
if md is None:
return None
return md.instanceType()
#---------------------------------------------------------------------------
# Other trait handlers:
#---------------------------------------------------------------------------
def _clipboard_default(self):
""" Initialise the clipboard.
"""
return QtGui.QApplication.clipboard()
#-------------------------------------------------------------------------------
# The singleton clipboard instance.
#-------------------------------------------------------------------------------
clipboard = _Clipboard()
|