CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
SimpleDraggableTreeWidget.py
Go to the documentation of this file.
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
3 
4 import logging
5 
6 class SimpleDraggableTreeWidget(QTreeWidget):
7  """ TreeWidget suitable for holding a list of strings.
8  """
9  MIME_TYPE = "text/plain"
10  def __init__(self, headerLabel, dragEnabled=False, mimeType=None, parent=None):
11  """ Constructor.
12  """
13  QTreeWidget.__init__(self,parent)
14  self.setMimeType(mimeType)
15  self.setAutoFillBackground(True)
16  #print "color roles", QPalette.Base, QPalette.Window, self.backgroundRole()
17  lightBlueBackgroundColor = QColor(Qt.blue).lighter(195)
18  #lightBlueBackgroundColor = QColor(Qt.red)
19  self.palette().setColor(QPalette.Base, lightBlueBackgroundColor) # OS X
20  self.palette().setColor(QPalette.Window, lightBlueBackgroundColor)
21 
22  self.setColumnCount(1)
23  self.setHeaderLabels([headerLabel])
24  if dragEnabled:
25  self.setDragEnabled(True)
26 
27  def populate(self, items):
28  """ Fills items into list.
29  """
30  self.insertTopLevelItems(0, items)
31 
32  def setDragEnable(self, dragEnabled, mimeType=None):
33  """ Usual behavior of QWidget's setDragEnabled() function plus optional setting of mimeType.
34  """
35  QTreeWidget.setDragEnabled(dragEnabled)
36  self.setMimeType(mimeType)
37 
38  def mimeType(self):
39  """ Returns mime type which will be used to encode list entries while dragging.
40  """
41  return self._mimeType
42 
43  def setMimeType(self, mimeType):
44  """ Sets mime type of this widget to type if type is not None.
45 
46  If type is None the default mime type MIME_TYPE will be used.
47  """
48  if mimeType:
49  self._mimeType = mimeType
50  else:
51  self._mimeType = self.MIME_TYPE
52 
53  def mimeTypes(self):
54  """ Returns self.mimeType() as single element of QStringList.
55  """
56  list = QStringList()
57  list << self.mimeType()
58  return list
59 
60  def mimeData(self, items):
61  """ Returns QMimeData for drag and drop.
62  """
63  logging.debug(self.__class__.__name__ + ": mimeData()")
64  mime = QMimeData()
65  encodedData = QByteArray()
66 
67  for item in items:
68  encodedData.append(item.text(0))
69  mime.setData(self.mimeType(), encodedData)
70  return mime
71 
72  def startDrag(self, supportedActions):
73  """ Overwritten function of QTreeWidget.
74 
75  This function creates a QDrag object representing the selected element of this TreeWidget.
76  """
77  logging.debug(self.__class__.__name__ +": startDrag()")
78  indexes = self.selectedIndexes()
79  if len(indexes) > 0:
80  data = self.model().mimeData(indexes)
81  if not data:
82  return
83  drag = QDrag(self)
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)) # center icon in respect to cursor
89  defaultDropAction = Qt.IgnoreAction
90  drag.exec_(supportedActions, defaultDropAction)
91 
92  def mousePressEvent(self,event):
93  QTreeWidget.mousePressEvent(self,event)
94  if event.button()==Qt.RightButton:
95  self.emit(SIGNAL("mouseRightPressed"), event.globalPos())
96 
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:82