Skip to content

Instantly share code, notes, and snippets.

@davvid
Created February 13, 2023 09:38
Show Gist options
  • Save davvid/5dd2231af1fb744701f8e90a1076593e to your computer and use it in GitHub Desktop.
Save davvid/5dd2231af1fb744701f8e90a1076593e to your computer and use it in GitHub Desktop.
qtpy-based mimedata drop+drop debugging tool
#!/usr/bin/env python3
from __future__ import absolute_import, division, unicode_literals
import sys
from pprint import pprint
from qtpy import QtCore
from qtpy import QtGui
from qtpy import QtWidgets
from qtpy.QtCore import Qt
def decode_data(barray):
data = []
stream = QtCore.QDataStream(barray)
while not stream.atEnd():
item = {}
stream.readInt32() # row
stream.readInt32() # column
map_items = stream.readInt32()
for i in range(map_items):
key = stream.readInt32()
value = stream.readQVariant()
item[Qt.ItemDataRole(key)] = value
data.append(item)
return data
class TreeModel(QtGui.QStandardItemModel):
INTERNAL_FORMAT = 'application/x-qabstractitemmodeldatalist'
def __init__(self, parent=None):
super(TreeModel, self).__init__(parent)
def dropMimeData(self, mimedata, action, row, column, parent):
if mimedata.hasFormat(self.INTERNAL_FORMAT):
encoded_data = mimedata.data(self.INTERNAL_FORMAT)
items = decode_data(encoded_data)
if not items:
return QtGui.QStandardItemModel.dropMimeData(
self, mimedata, action, row, column, parent
)
# Assuming that we get at least one item, and that it defines
# text that we can display.
text = items[0][Qt.DisplayRole]
for row in range(self.rowCount()):
name = self.item(row, 0).text()
if name == text:
number_item = self.item(row, 1)
number = int(number_item.text())
number_item.setText(str(number + 1))
break
else:
name_item = QtGui.QStandardItem(text)
number_item = QtGui.QStandardItem('1')
self.appendRow([name_item, number_item])
return True
return QtGui.QStandardItemModel.dropMimeData(
self, mimedata, action, row, column, parent)
class TreeView(QtWidgets.QTreeView):
def __init__(self, parent=None):
super(TreeView, self).__init__(parent)
self.setHeaderHidden(True)
self.setRootIsDecorated(False)
self.setAcceptDrops(True)
def dragEnterEvent(self, event):
# print('# Drag Enter')
event.acceptProposedAction()
def dragMoveEvent(self, event):
# print('# Drag Move')
event.acceptProposedAction()
def dropEvent(self, event):
super(TreeView, self).dropEvent(event)
print('# Drop Event Formats')
mimedata = event.mimeData()
pprint(mimedata.formats())
files = [url.toLocalFile() for url in mimedata.urls()]
if files:
print('# Files')
pprint(files)
if mimedata.hasText():
print('# Text')
print(mimedata.text())
for fmt in mimedata.formats():
print('# %s' % fmt)
print(bytes(mimedata.data(fmt)).decode('utf-8', errors='backslashreplace'))
def main():
app = QtWidgets.QApplication(sys.argv)
window = QtWidgets.QWidget()
listmodel = QtCore.QStringListModel(['X', 'Y'])
listview = QtWidgets.QListView()
listview.setModel(listmodel)
listview.setDragEnabled(True)
treemodel = TreeModel()
treeview = TreeView()
treeview.setModel(treemodel)
layout = QtWidgets.QHBoxLayout(window)
layout.addWidget(listview)
layout.addWidget(treeview)
window.show()
return app.exec_()
if __name__ == '__main__':
sys.exit(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment