def _copy(self): rows = sorted({ri.row() for ri in self.selectedIndexes()}) copy_object = [(ri, self._editor.value[ri]) for ri in rows] # copy_object = [ri.row(), self._editor.value[ri.row()]) for ri in self.selectedIndexes()] mt = self._editor.factory.mime_type pdata = dumps(copy_object) qmd = PyMimeData() qmd.MIME_TYPE = mt qmd.setData(unicode(mt), dumps(copy_object.__class__) + pdata) clipboard = QApplication.clipboard() clipboard.setMimeData(qmd)
def dropEvent(self, e): if self.is_external(): data = PyMimeData.coerce(e.mimeData()).instance() if not hasattr(data, '__iter__'): return df = self.drop_factory if not df: df = lambda x: x row = self.rowAt(e.pos().y()) n = len(self._editor.value) if row == -1: row = n model = self._editor.model if self._dragging: rows = [ri for ri, _ in data] model.moveRows(rows, row) else: with no_update(self._editor.object): for i, di in enumerate(reversed(data)): if isinstance(di, tuple): di = di[1] model.insertRow(row=row, obj=df(di)) e.accept() self._dragging = None else: super(_TableView, self).dropEvent(e)
def dropEvent(self, e): if self.is_external(): data = PyMimeData.coerce(e.mimeData()).instance() if not hasattr(data, '__iter__'): return df = self.drop_factory if not df: df = lambda x: x row = self.rowAt(e.pos().y()) n = len(self._editor.value) if row == -1: row = n model = self._editor.model if self._dragging: rows = [ri for ri, _ in data] model.moveRows(rows, row) else: data = [di for _, di in data] with no_update(self._editor.object): for i, di in enumerate(reversed(data)): if isinstance(di, tuple): di = di[1] model.insertRow(row=row, obj=df(di)) for i, di in enumerate(reversed(df(data))): model.insertRow(row=row, obj=di) e.accept() self._dragging = None else: super(_TableView, self).dropEvent(e)
def _copy(self): rows = sorted({ri.row() for ri in self.selectedIndexes()}) copy_object = [(ri, self._editor.value[ri].tocopy()) for ri in rows] # copy_object = [ri.row(), self._editor.value[ri.row()]) for ri in self.selectedIndexes()] mt = self._editor.factory.mime_type try: pdata = dumps(copy_object) except BaseException as e: print('tabular editor copy failed') self._editor.value[rows[0]].tocopy(verbose=True) return qmd = PyMimeData() qmd.MIME_TYPE = mt qmd.setData(six.text_type(mt), dumps(copy_object.__class__) + pdata) clipboard = QApplication.clipboard() clipboard.setMimeData(qmd)
def startDrag(self, actions): if self._editor.factory.drag_external: idxs = self.selectedIndexes() rows = sorted(list(set([idx.row() for idx in idxs]))) drag_object = [(ri, self._editor.value[ri]) for ri in rows] md = PyMimeData.coerce(drag_object) self._dragging = self.currentIndex() drag = QtGui.QDrag(self) drag.setMimeData(md) drag.exec_(actions) else: super(_TableView, self).startDrag(actions)
def dragEnterEvent(self, e): if self.is_external(): # Assume the drag is invalid. e.ignore() # Check what is being dragged. md = PyMimeData.coerce(e.mimeData()) if md is None: return # We might be able to handle it (but it depends on what the final # target is). e.acceptProposedAction() else: super(_myTableView, self).dragEnterEvent(e)
def dragEnterEvent(self, e): if self.is_external(): # Assume the drag is invalid. e.ignore() # Check what is being dragged. ed = e.mimeData() md = PyMimeData.coerce(ed) if md is None: return elif not hasattr(ed.instance(), '__iter__'): return # We might be able to handle it (but it depends on what the final # target is). e.acceptProposedAction() else: super(_TableView, self).dragEnterEvent(e)
def dropEvent(self, e): if self.is_external(): data = PyMimeData.coerce(e.mimeData()).instance() df = self.drop_factory if not df: df = lambda x: x row = self.rowAt(e.pos().y()) n = len(self._editor.value) if row == -1: row = n model = self._editor.model if self._dragging: rows = [ri for ri, _ in data] model.moveRows(rows, row) else: # self._editor._no_update = True # parent = QtCore.QModelIndex() # model.beginInsertRows(parent, row, row) # editor = self._editor # self._editor.object._no_update = True with no_update(self._editor.object): for i, (_, di) in enumerate(reversed(data)): # print 'insert' # obj = paste_func1(di) # editor.callx(editor.adapter.insert, editor.object, editor.name, row + i, obj) model.insertRow(row=row, obj=df(di)) # model.insertRow(row=row, obj=paste_func(data[0][1])) # self._editor.object._no_update = False # model.endInsertRows() # self._editor._no_update = False e.accept() self._dragging = None else: super(_myTableView, self).dropEvent(e)
def dragEnterEvent(self, e): if self.is_external(): # Assume the drag is invalid. e.ignore() # Check what is being dragged. ed = e.mimeData() md = PyMimeData.coerce(ed) if md is None: return else: try: if not hasattr(ed.instance(), '__iter__'): return except AttributeError: return # We might be able to handle it (but it depends on what the final # target is). e.acceptProposedAction() else: super(_TableView, self).dragEnterEvent(e)
def dropMimeData(self, mime_data, action, row, column, parent): if action == QtCore.Qt.IgnoreAction: return False # this is a drag from a tabular model data = mime_data.data(tabular_mime_type) if not data.isNull() and action == QtCore.Qt.MoveAction: id_and_rows = [int(di) for di in data.split(' ')] table_id = id_and_rows[0] # is it from ourself? if table_id == id(self): current_rows = id_and_rows[1:] self.moveRows(current_rows, parent.row()) return True # this is an external drag data = PyMimeData.coerce(mime_data).instance() if data is not None: if not isinstance(data, list): data = [data] editor = self._editor object = editor.object name = editor.name adapter = editor.adapter if row == -1 and parent.isValid(): # find correct row number row = parent.row() if row == -1 and adapter.len(object, name) == 0: # if empty list, target is after end of list row = 0 if all(adapter.get_can_drop(object, name, row, item) for item in data): for item in reversed(data): self.dropItem(item, row) return True return False
def mouseMoveEvent(self, event): """ Re-implemented to create a drag event when the mouse is moved for a sufficient distance while holding down mouse button. """ # go into the drag logic only if a drag_obj is active if self.drag_obj: # is the left mouse button still pressed? if not event.buttons()==QtCore.Qt.LeftButton: pass # has the mouse been dragged for sufficient distance? elif ((event.pos() - self.drag_obj.start_pos).manhattanLength() < QtGui.QApplication.startDragDistance()): pass # initiate drag else: drag = QtGui.QDrag(self.drag_obj.widget) mimedata = PyMimeData(data=self.drag_obj, pickle=False) drag.setPixmap(self.drag_obj.get_pixmap()) drag.setHotSpot(self.drag_obj.get_hotspot()) drag.setMimeData(mimedata) drag.exec_() self.drag_obj = None # deactivate the drag_obj again return return super(DraggableTabBar, self).mouseMoveEvent(event)
class _TableView(TableView): """ for drag and drop reference see https://github.com/enthought/traitsui/blob/master/traitsui/qt4/tree_editor.py """ paste_func = None drop_factory = None # link_copyable = True copyable = True # _copy_cache = None # _linked_copy_cache = None _dragging = None _cut_indices = None option_select = False drag_enabled = True def __init__(self, editor, layout=None, *args, **kw): super(_TableView, self).__init__(editor, *args, **kw) self.setItemDelegate(ItemDelegate(self)) # self.setup_consumer(main=True) editor = self._editor # # reimplement row height vheader = self.verticalHeader() # size = vheader.minimumSectionSize() height = None font = editor.adapter.get_font(editor.object, editor.name, 0) if font is not None: fnt = QtGui.QFont(font) size = QtGui.QFontMetrics(fnt) height = size.height() + 6 vheader.setFont(fnt) hheader = self.horizontalHeader() hheader.setFont(fnt) if editor.factory.row_height: height = editor.factory.row_height if height: vheader.setDefaultSectionSize(height) else: vheader.ResizeMode(QHeaderView.ResizeToContents) def set_bg_color(self, bgcolor): if isinstance(bgcolor, tuple): if len(bgcolor) == 3: bgcolor = 'rgb({},{},{})'.format(*bgcolor) elif len(bgcolor) == 4: bgcolor = 'rgba({},{},{},{})'.format(*bgcolor) elif isinstance(bgcolor, QColor): bgcolor = 'rgba({},{},{},{})'.format(*bgcolor.toTuple()) self.setStyleSheet( 'QTableView {{background-color: {}}}'.format(bgcolor)) def set_vertical_header_font(self, fnt): fnt = QtGui.QFont(fnt) vheader = self.verticalHeader() vheader.setFont(fnt) size = QtGui.QFontMetrics(fnt) vheader.setDefaultSectionSize(size.height() + 6) def set_horizontal_header_font(self, fnt): fnt = QtGui.QFont(fnt) vheader = self.horizontalHeader() vheader.setFont(fnt) def set_drag_enabled(self, d): if d: self.setDragDropMode(QtGui.QAbstractItemView.DragDrop) self.setDragEnabled(True) def startDrag(self, actions): if self._editor.factory.drag_external: idxs = self.selectedIndexes() rows = sorted(list(set([idx.row() for idx in idxs]))) drag_object = [(ri, self._editor.value[ri]) for ri in rows] md = PyMimeData.coerce(drag_object) self._dragging = self.currentIndex() drag = QtGui.QDrag(self) drag.setMimeData(md) drag.exec_(actions) else: super(_TableView, self).startDrag(actions) def dragEnterEvent(self, e): if self.is_external(): # Assume the drag is invalid. e.ignore() # Check what is being dragged. ed = e.mimeData() md = PyMimeData.coerce(ed) if md is None: return elif not hasattr(ed.instance(), '__iter__'): return # We might be able to handle it (but it depends on what the final # target is). e.acceptProposedAction() else: super(_TableView, self).dragEnterEvent(e) def dragMoveEvent(self, e): if self.is_external(): e.acceptProposedAction() else: super(_TableView, self).dragMoveEvent(e) def dropEvent(self, e): if self.is_external(): data = PyMimeData.coerce(e.mimeData()).instance() if not hasattr(data, '__iter__'): return df = self.drop_factory if not df: df = lambda x: x row = self.rowAt(e.pos().y()) n = len(self._editor.value) if row == -1: row = n model = self._editor.model if self._dragging: rows = [ri for ri, _ in data] model.moveRows(rows, row) else: data = [di for _, di in data] with no_update(self._editor.object): for i, di in enumerate(reversed(data)): if isinstance(di, tuple): di = di[1] model.insertRow(row=row, obj=df(di)) for i, di in enumerate(reversed(df(data))): model.insertRow(row=row, obj=di) e.accept() self._dragging = None else: super(_TableView, self).dropEvent(e) def is_external(self): # print 'is_external', self._editor.factory.drag_external and not self._dragging return self._editor.factory.drag_external # and not self._dragging def keyPressEvent(self, event): if event.matches(QtGui.QKeySequence.Copy): # self._copy_cache = [self._editor.value[ci.row()] for ci in # self.selectionModel().selectedRows()] # self._copy_cache = self._get_selection() # self._editor.copy_cache = self._copy_cache self._cut_indices = None # add the selected rows to the clipboard self._copy() elif event.matches(QtGui.QKeySequence.Cut): self._cut_indices = [ ci.row() for ci in self.selectionModel().selectedRows() ] # self._copy_cache = [self._editor.value[ci] for ci in self._cut_indices] # self._copy_cache = self._get_selection(self._cut_indices) # self._editor.copy_cache = self._copy_cache elif event.matches(QtGui.QKeySequence.Paste): if self.pastable: self._paste() else: self._editor.key_pressed = TabularKeyEvent(event) self._key_press_hook(event) # private def _copy(self): rows = sorted({ri.row() for ri in self.selectedIndexes()}) copy_object = [(ri, self._editor.value[ri].tocopy()) for ri in rows] # copy_object = [ri.row(), self._editor.value[ri.row()]) for ri in self.selectedIndexes()] mt = self._editor.factory.mime_type try: pdata = dumps(copy_object) except BaseException, e: print 'tabular editor copy failed' self._editor.value[rows[0]].tocopy(verbose=True) return qmd = PyMimeData() qmd.MIME_TYPE = mt qmd.setData(unicode(mt), dumps(copy_object.__class__) + pdata) clipboard = QApplication.clipboard() clipboard.setMimeData(qmd)