[#9] Implemented directories drag&drop on the QT side.

--HG--
extra : convert_revision : svn%3Ac306627e-7827-47d3-bdf0-9a457c9553a1/trunk%40224
This commit is contained in:
hsoft 2009-10-29 12:02:49 +00:00
parent 607ab86188
commit 88127d8b8d
2 changed files with 47 additions and 10 deletions

View File

@ -16,9 +16,18 @@
<layout class="QVBoxLayout" name="verticalLayout"> <layout class="QVBoxLayout" name="verticalLayout">
<item> <item>
<widget class="QTreeView" name="treeView"> <widget class="QTreeView" name="treeView">
<property name="acceptDrops">
<bool>true</bool>
</property>
<property name="editTriggers"> <property name="editTriggers">
<set>QAbstractItemView::DoubleClicked|QAbstractItemView::EditKeyPressed|QAbstractItemView::SelectedClicked</set> <set>QAbstractItemView::DoubleClicked|QAbstractItemView::EditKeyPressed|QAbstractItemView::SelectedClicked</set>
</property> </property>
<property name="dragDropOverwriteMode">
<bool>true</bool>
</property>
<property name="dragDropMode">
<enum>QAbstractItemView::DropOnly</enum>
</property>
<property name="uniformRowHeights"> <property name="uniformRowHeights">
<bool>true</bool> <bool>true</bool>
</property> </property>
@ -59,6 +68,9 @@
<property name="text"> <property name="text">
<string>Remove</string> <string>Remove</string>
</property> </property>
<property name="shortcut">
<string>Del</string>
</property>
</widget> </widget>
</item> </item>
<item> <item>

View File

@ -7,7 +7,9 @@
# which should be included with this package. The terms are also available at # which should be included with this package. The terms are also available at
# http://www.hardcoded.net/licenses/hs_license # http://www.hardcoded.net/licenses/hs_license
from PyQt4.QtCore import QModelIndex, Qt, QRect, QEvent, QPoint import urllib
from PyQt4.QtCore import QModelIndex, Qt, QRect, QEvent, QPoint, QUrl
from PyQt4.QtGui import QComboBox, QStyledItemDelegate, QMouseEvent, QApplication, QBrush from PyQt4.QtGui import QComboBox, QStyledItemDelegate, QMouseEvent, QApplication, QBrush
from qtlib.tree_model import TreeNode, TreeModel from qtlib.tree_model import TreeNode, TreeModel
@ -47,7 +49,7 @@ class DirectoryNode(TreeNode):
return DirectoryNode(self.model, self, ref, row) return DirectoryNode(self.model, self, ref, row)
def _getChildren(self): def _getChildren(self):
return self.model._dirs.get_subfolders(self.ref) return self.model.dirs.get_subfolders(self.ref)
@property @property
def name(self): def name(self):
@ -59,14 +61,15 @@ class DirectoryNode(TreeNode):
class DirectoriesModel(TreeModel): class DirectoriesModel(TreeModel):
def __init__(self, app): def __init__(self, app):
self._dirs = app.directories self.app = app
self.dirs = app.directories
TreeModel.__init__(self) TreeModel.__init__(self)
def _createNode(self, ref, row): def _createNode(self, ref, row):
return DirectoryNode(self, None, ref, row) return DirectoryNode(self, None, ref, row)
def _getChildren(self): def _getChildren(self):
return self._dirs return self.dirs
def columnCount(self, parent): def columnCount(self, parent):
return 2 return 2
@ -79,21 +82,35 @@ class DirectoriesModel(TreeModel):
if index.column() == 0: if index.column() == 0:
return node.name return node.name
else: else:
return STATES[self._dirs.get_state(node.ref)] return STATES[self.dirs.get_state(node.ref)]
elif role == Qt.EditRole and index.column() == 1: elif role == Qt.EditRole and index.column() == 1:
return self._dirs.get_state(node.ref) return self.dirs.get_state(node.ref)
elif role == Qt.ForegroundRole: elif role == Qt.ForegroundRole:
state = self._dirs.get_state(node.ref) state = self.dirs.get_state(node.ref)
if state == 1: if state == 1:
return QBrush(Qt.blue) return QBrush(Qt.blue)
elif state == 2: elif state == 2:
return QBrush(Qt.red) return QBrush(Qt.red)
return None return None
def dropMimeData(self, mimeData, action, row, column, parentIndex):
# the data in mimeData is urlencoded **in utf-8**!!! which means that urllib.unquote has
# to be called on the utf-8 encoded string, and *only then*, decoded to unicode.
if not mimeData.hasFormat('text/uri-list'):
return False
data = str(mimeData.data('text/uri-list'))
unquoted = urllib.unquote(data)
urls = unicode(unquoted, 'utf-8').split('\r\n')
paths = [unicode(QUrl(url).toLocalFile()) for url in urls if url]
for path in paths:
self.app.add_directory(path)
self.reset()
return True
def flags(self, index): def flags(self, index):
if not index.isValid(): if not index.isValid():
return 0 return Qt.ItemIsEnabled | Qt.ItemIsDropEnabled
result = Qt.ItemIsEnabled | Qt.ItemIsSelectable result = Qt.ItemIsEnabled | Qt.ItemIsSelectable | Qt.ItemIsDropEnabled
if index.column() == 1: if index.column() == 1:
result |= Qt.ItemIsEditable result |= Qt.ItemIsEditable
return result return result
@ -104,10 +121,18 @@ class DirectoriesModel(TreeModel):
return HEADERS[section] return HEADERS[section]
return None return None
def mimeTypes(self):
return ['text/uri-list']
def setData(self, index, value, role): def setData(self, index, value, role):
if not index.isValid() or role != Qt.EditRole or index.column() != 1: if not index.isValid() or role != Qt.EditRole or index.column() != 1:
return False return False
node = index.internalPointer() node = index.internalPointer()
self._dirs.set_state(node.ref, value) self.dirs.set_state(node.ref, value)
return True return True
def supportedDropActions(self):
# Normally, the correct action should be ActionLink, but the drop doesn't work. It doesn't
# work with ActionMove either. So screw that, and accept anything.
return Qt.ActionMask