def test_cant_pickle(self): unpicklable = lambda: None md = PyMimeData(data=unpicklable) self.assertEqual(md._local_instance, unpicklable) self.assertTrue(md.hasFormat(PyMimeData.NOPICKLE_MIME_TYPE)) self.assertFalse(md.hasFormat(PyMimeData.MIME_TYPE)) self.assertEqual(bytes(md.data(PyMimeData.NOPICKLE_MIME_TYPE)), str(id(unpicklable)))
def test_pickle(self): md = PyMimeData(data=0) self.assertEqual(md._local_instance, 0) self.assertTrue(md.hasFormat(PyMimeData.MIME_TYPE)) self.assertFalse(md.hasFormat(PyMimeData.NOPICKLE_MIME_TYPE)) self.assertEqual(md.data(PyMimeData.MIME_TYPE).data(), dumps(int) + dumps(0))
def test_coerce_list_pymimedata_mixed(self): md1 = PyMimeData(data=0, pickle=False) md2 = PyMimeData(data=0) md = PyMimeData.coerce([md1, md2]) self.assertEqual(md._local_instance, [0, 0]) self.assertFalse(md.hasFormat(PyMimeData.MIME_TYPE)) self.assertTrue(md.hasFormat(PyMimeData.NOPICKLE_MIME_TYPE))
def test_pickle(self): md = PyMimeData(data=0) self.assertEqual(md._local_instance, 0) self.assertTrue(md.hasFormat(PyMimeData.MIME_TYPE)) self.assertFalse(md.hasFormat(PyMimeData.NOPICKLE_MIME_TYPE)) self.assertEqual(bytes(md.data(PyMimeData.MIME_TYPE)), dumps(int) + dumps(0))
def test_nopickle(self): md = PyMimeData(data=0, pickle=False) self.assertEqual(md._local_instance, 0) self.assertTrue(md.hasFormat(PyMimeData.NOPICKLE_MIME_TYPE)) self.assertFalse(md.hasFormat(PyMimeData.MIME_TYPE)) self.assertEqual(bytes(md.data(PyMimeData.NOPICKLE_MIME_TYPE)), str(id(0)))
def test_coerce_list_pymimedata(self): md = PyMimeData(data=0) md2 = PyMimeData.coerce([md]) self.assertEqual(md2._local_instance, [0]) self.assertTrue(md2.hasFormat(PyMimeData.MIME_TYPE)) self.assertFalse(md2.hasFormat(PyMimeData.NOPICKLE_MIME_TYPE)) self.assertEqual(bytes(md2.data(PyMimeData.MIME_TYPE)), dumps(list) + dumps([0]))
def test_nopickle(self): md = PyMimeData(data=0, pickle=False) self.assertEqual(md._local_instance, 0) self.assertTrue(md.hasFormat(PyMimeData.NOPICKLE_MIME_TYPE)) self.assertFalse(md.hasFormat(PyMimeData.MIME_TYPE)) self.assertEqual( md.data(PyMimeData.NOPICKLE_MIME_TYPE).data(), str2bytes(str(id(0))) )
def test_coerce_list_pymimedata(self): md = PyMimeData(data=0) md2 = PyMimeData.coerce([md]) self.assertEqual(md2._local_instance, [0]) self.assertTrue(md2.hasFormat(PyMimeData.MIME_TYPE)) self.assertFalse(md2.hasFormat(PyMimeData.NOPICKLE_MIME_TYPE)) self.assertEqual(bytes(md2.data(PyMimeData.MIME_TYPE)), dumps(list)+dumps([0]))
def test_subclass_coerce_pymimedata(self): md = PyMimeData(data=0) md2 = PMDSubclass.coerce(md) self.assertTrue(isinstance(md2, PMDSubclass)) self.assertTrue(md2.hasFormat(PyMimeData.MIME_TYPE)) self.assertFalse(md2.hasFormat(PyMimeData.NOPICKLE_MIME_TYPE)) self.assertEqual(bytes(md2.data(PyMimeData.MIME_TYPE)), dumps(int) + dumps(0))
def test_coerce_list(self): md = PyMimeData.coerce([0]) self.assertEqual(md._local_instance, [0]) self.assertTrue(md.hasFormat(PyMimeData.MIME_TYPE)) self.assertFalse(md.hasFormat(PyMimeData.NOPICKLE_MIME_TYPE)) self.assertEqual( md.data(PyMimeData.MIME_TYPE).data(), dumps(list) + dumps([0]))
def dropEvent(self, event): q_data = event.mimeData() if q_data.hasFormat(PyMimeData.MIME_TYPE): data = PyMimeData.coerce(q_data).instance() names = self._get_drop_names(data) self.model().add_columns(names) event.acceptProposedAction()
def test_coerce_object(self): md = PyMimeData.coerce(0) self.assertEqual(md._local_instance, 0) self.assertTrue(md.hasFormat(PyMimeData.MIME_TYPE)) self.assertFalse(md.hasFormat(PyMimeData.NOPICKLE_MIME_TYPE)) self.assertEqual( md.data(PyMimeData.MIME_TYPE).data(), dumps(int) + dumps(0))
def drop(self, event): accept = False mime_data = event.mime_data() if mime_data.has_format(PyMimeData.MIME_TYPE): data = PyMimeData.coerce(mime_data.q_data()).instance() accept = self.load_drop(data) if accept: event.accept_proposed_action()
def _create_drag_event(self, event): # If the control no longer exists, don't send mouse event if self.control is None: return None # If the event (if there is one) doesn't contain the mouse position, # modifiers and buttons then get sensible defaults. try: x = event.x() y = event.y() except AttributeError: pos = self.control.mapFromGlobal(QtGui.QCursor.pos()) x = pos.x() y = pos.y() self.control.handler.last_mouse_pos = (x, y) # extract an object from the event, if we can try: mimedata = event.mimeData() copy = event.proposedAction() == QtCore.Qt.CopyAction except AttributeError: # this is a DragLeave event return DragEvent(x=x, y=self._flip_y(y), obj=None, copy=False, window=self, mimedata=None) try: from traitsui.qt4.clipboard import PyMimeData except ImportError: # traitsui isn't available, just make mimedata available on event obj = None else: mimedata = PyMimeData.coerce(mimedata) obj = mimedata.instance() if obj is None: files = mimedata.localPaths() if files: try: # try to extract file info from mimedata # XXX this is for compatibility with what wx does from apptools.io.api import File obj = [File(path=path) for path in files] except ImportError: pass return DragEvent(x=x, y=self._flip_y(y), obj=obj, copy=copy, window=self, mimedata=mimedata)
def drop(self, event): mime_data = event.mime_data() if mime_data.has_format('text/plain'): text = mime_data.data('text/plain') elif mime_data.has_format(PyMimeData.MIME_TYPE): data = PyMimeData.coerce(mime_data.q_data()).instance() text = self.to_string(data[0]) self.insert_text(text)
def test_coerce_list(self): md = PyMimeData.coerce([0]) self.assertEqual(md._local_instance, [0]) self.assertTrue(md.hasFormat(PyMimeData.MIME_TYPE)) self.assertFalse(md.hasFormat(PyMimeData.NOPICKLE_MIME_TYPE)) self.assertEqual( md.data( PyMimeData.MIME_TYPE).data(), dumps(list) + dumps( [0]))
def dragEnterEvent(self, event): if not self.editable: return q_data = event.mimeData() if q_data.hasFormat(PyMimeData.MIME_TYPE): data = PyMimeData.coerce(q_data).instance() names = self._get_drop_names(data) if (names is not None and all([self.model().can_add_column(n) for n in names])): event.acceptProposedAction()
def mimeData(self, indexes): """ Reimplemented to generate PyMimeData data for the given indices. """ mode = self.selection_mode data = [] if mode == 'cell': data = ([self.map_to_row(i.row()) for i in indexes], [self.map_to_col(i.column()) for i in indexes]) elif mode == 'row': data = sorted({self.map_to_row(i.row()) for i in indexes}) elif mode == 'column': data = sorted({self.map_to_col(i.column()) for i in indexes}) mime_data = PyMimeData.coerce(data) return mime_data
def drag_enter(self, event): accept = False mime_data = event.mime_data() # Standard text mimetype if mime_data.has_format('text/plain'): accept = True # Support Traits UI table and tabular editors (single selection only). elif mime_data.has_format(PyMimeData.MIME_TYPE): data = PyMimeData.coerce(mime_data.q_data()).instance() accept = (isinstance(data, list) and len(data) == 1 and isinstance(data[0], self.valid_types)) if accept: event.accept_proposed_action()
def test_coerce_subclass(self): md = PMDSubclass(data=0) md2 = PyMimeData.coerce(md) self.assertEqual(md, md2)
def test_coerce_pymimedata(self): md = PyMimeData(data=0) md2 = PyMimeData.coerce(md) self.assertEqual(md, md2)
def test_instance(self): md = PyMimeData(data=0) self.assertEqual(md.instance(), 0)
def test_instance_type(self): md = PyMimeData(data=0) self.assertEqual(md.instanceType(), int)
def test_instance_type_nopickle(self): md = PyMimeData(data=0, pickle=False) # remove local instance to simulate cross-process md._local_instance = None self.assertEqual(md.instanceType(), None)
def test_coerce_object(self): md = PyMimeData.coerce(0) self.assertEqual(md._local_instance, 0) self.assertTrue(md.hasFormat(PyMimeData.MIME_TYPE)) self.assertFalse(md.hasFormat(PyMimeData.NOPICKLE_MIME_TYPE)) self.assertEqual(bytes(md.data(PyMimeData.MIME_TYPE)), dumps(int)+dumps(0))
def test_instance_type_unpickled(self): md = PyMimeData(data=0) # remove local instance to simulate cross-process md._local_instance = None self.assertEqual(md.instanceType(), int)
def test_coerce_QMimeData(self): md = QtCore.QMimeData() md.setText("test") md2 = PyMimeData.coerce(md) self.assertTrue(md2.hasText()) self.assertEqual(md2.text(), "test")
def test_coerce_unpicklable(self): unpicklable = lambda: None md = PyMimeData.coerce(unpicklable) self.assertEqual(md._local_instance, unpicklable) self.assertFalse(md.hasFormat(PyMimeData.MIME_TYPE)) self.assertTrue(md.hasFormat(PyMimeData.NOPICKLE_MIME_TYPE))