1 from PyQt4.QtCore import QMimeData, QByteArray, Qt, QSize, QPoint, QVariant,SIGNAL
2 from PyQt4.QtGui import QTreeWidget, QImage, QDrag, QPixmap, QIcon, QPalette, QColor,QTreeWidgetItem
7 """ TreeWidget suitable for holding a list of strings.
9 MIME_TYPE =
"text/plain"
10 def __init__(self, headerLabel, dragEnabled=False, mimeType=None, parent=None):
13 QTreeWidget.__init__(self,parent)
15 self.setAutoFillBackground(
True)
17 lightBlueBackgroundColor = QColor(Qt.blue).lighter(195)
19 self.palette().setColor(QPalette.Base, lightBlueBackgroundColor)
20 self.palette().setColor(QPalette.Window, lightBlueBackgroundColor)
22 self.setColumnCount(1)
23 self.setHeaderLabels([headerLabel])
25 self.setDragEnabled(
True)
28 """ Fills items into list.
30 self.insertTopLevelItems(0, items)
33 """ Usual behavior of QWidget's setDragEnabled() function plus optional setting of mimeType.
35 QTreeWidget.setDragEnabled(dragEnabled)
39 """ Returns mime type which will be used to encode list entries while dragging.
44 """ Sets mime type of this widget to type if type is not None.
46 If type is None the default mime type MIME_TYPE will be used.
54 """ Returns self.mimeType() as single element of QStringList.
61 """ Returns QMimeData for drag and drop.
63 logging.debug(self.__class__.__name__ +
": mimeData()")
65 encodedData = QByteArray()
68 encodedData.append(item.text(0))
69 mime.setData(self.
mimeType(), encodedData)
73 """ Overwritten function of QTreeWidget.
75 This function creates a QDrag object representing the selected element of this TreeWidget.
77 logging.debug(self.__class__.__name__ +
": startDrag()")
78 indexes = self.selectedIndexes()
80 data = self.model().
mimeData(indexes)
84 drag.setMimeData(data)
85 if self.model().
data(indexes[0], Qt.DecorationRole).type() == QVariant.Icon:
86 icon = QIcon(self.model().
data(indexes[0], Qt.DecorationRole))
87 drag.setPixmap(icon.pixmap(QSize(50, 50)))
88 drag.setHotSpot(QPoint(drag.pixmap().
width()/2, drag.pixmap().height()/2))
89 defaultDropAction = Qt.IgnoreAction
90 drag.exec_(supportedActions, defaultDropAction)
93 QTreeWidget.mousePressEvent(self,event)
94 if event.button()==Qt.RightButton:
95 self.emit(SIGNAL(
"mouseRightPressed"), event.globalPos())