Advertisement
tbttfox

DummyDelegate.py

Sep 7th, 2014
298
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.80 KB | None | 0 0
  1.  
  2. from PyQt4.QtCore import QAbstractTableModel, QModelIndex, pyqtSignal, pyqtSlot, QPoint, Qt
  3. from PyQt4.QtGui import ( QTableView, QItemDelegate, QComboBox,
  4.                             QLineEdit, QToolButton, QHBoxLayout, QFileDialog,
  5.                             QStyle, QRegion, QWidget, QVBoxLayout,
  6.                             QApplication, QMouseEvent)
  7.  
  8. class TableModel(QAbstractTableModel):
  9.     def rowCount(self, parent=QModelIndex()):
  10.         return 5
  11.     def columnCount(self, parent=QModelIndex()):
  12.         return 4
  13.  
  14.     def data(self, index, role = Qt.DisplayRole):
  15.         if index.isValid() and role == Qt.DisplayRole:
  16.             return "{0:02d}".format(index.row())
  17.         return None
  18.        
  19.     def setData(self, index, value, role = Qt.DisplayRole):
  20.         print "setData", index.row(), index.column(), value
  21.  
  22.     def flags(self, index):
  23.         return Qt.ItemIsEditable | Qt.ItemIsEnabled
  24.  
  25. class TableView(QTableView):
  26.     def mousePressEvent(self, event):
  27.         if event.button() == Qt.LeftButton:
  28.             index = self.indexAt(event.pos())
  29.             if index.isValid():
  30.                 self.setCurrentIndex(index)
  31.                 self.edit(index) #allow one-click editing
  32.         else:
  33.             super(TableView, self).mousePressEvent(event)
  34.  
  35. class ComboDelegate(QItemDelegate):
  36.     def __init__(self, parent):
  37.         super(ComboDelegate, self).__init__(parent)
  38.         #gotta provide a parent to the helper to get the correct style
  39.         self._helperCombo = QComboBox(parent)
  40.         self._helperCombo.setVisible(False)
  41.  
  42.     def createEditor(self, parent, option, index):
  43.         return QComboBox(parent)
  44.  
  45.     def setEditorData(self, editor, index):
  46.         editor.addItems( ["00", "01", "02", "03", "04"] )
  47.         editor.showPopup() #allow one-click editing, but only for comboBoxes :(
  48.  
  49.     def setModelData(self, editor, model, index):
  50.         model.setData(index, editor.currentIndex())
  51.  
  52.     def paint(self, painter, option, index):
  53.         # Draws the _helperCombo as the background
  54.         painter.save()
  55.         painter.translate(option.rect.topLeft())
  56.         self._helperCombo.resize(option.rect.size())
  57.         self._helperCombo.render(painter, QPoint(), QRegion(), QWidget.DrawChildren)
  58.         painter.restore()
  59.  
  60.         option.rect.adjust(1,1,-1,-1) # because the widget fits inside the cell
  61.         super(ComboDelegate, self).paint(painter, option, index)
  62.  
  63. class FilePickerWidget( QWidget ):
  64.     def __init__( self, parent=None ):
  65.         super(FilePickerWidget, self).__init__(parent)
  66.  
  67.         self.uiFilenameTXT = QLineEdit(self)
  68.         self.uiPickFileBTN = QToolButton(self)
  69.         self.uiPickFileBTN.setText('...')
  70.         layout = QHBoxLayout(self)
  71.         layout.addWidget(self.uiFilenameTXT)
  72.         layout.addWidget(self.uiPickFileBTN)
  73.         layout.setContentsMargins(0, 0, 0, 0)
  74.         self.setLayout(layout)
  75.         self.uiPickFileBTN.clicked.connect( self.pickPath )
  76.  
  77.     def filePath( self ):
  78.         return self.uiFilenameTXT.text()
  79.        
  80.     def pickPath( self ):
  81.         filepath = QFileDialog.getOpenFileName( self, "Pick File...", self.uiFilenameTXT.text(), "All Files (*.*)")
  82.         if filepath:
  83.             self.uiFilenameTXT.setText( filepath )
  84.             self.emitFilenamePicked()
  85.  
  86.     def setFilePath( self, filePath ):
  87.         self.uiFilenameTXT.setText( filePath )
  88.  
  89.  
  90.  
  91.  
  92. ### THIS IS THE DELEGATE THAT I NEED HELP WITH
  93. class FilePickerDelegate(QItemDelegate):
  94.     def __init__(self, parent):
  95.         super(FilePickerDelegate, self).__init__(parent)
  96.         #gotta set the parent of the helper to get the correct style
  97.         self._helperPicker = FilePickerWidget(parent)
  98.         self._helperPicker.setVisible(False)
  99.  
  100.     def createEditor(self, parent, option, index):
  101.         return FilePickerWidget(parent)
  102.  
  103.     def setEditorData(self, editor, index):
  104.         editor.setFilePath(r'01')
  105.  
  106.     def setModelData(self, editor, model, index):
  107.         model.setData(index, editor.filePath())
  108.  
  109.     def paint(self, painter, option, index):
  110.         # Draws the self._helperPicker as the background
  111.         painter.save()
  112.         painter.translate(option.rect.topLeft())
  113.         self._helperPicker.resize(option.rect.size())
  114.         self._helperPicker.render(painter, QPoint(), QRegion(), QWidget.DrawChildren)
  115.         painter.restore()
  116.  
  117.         #continue on with the regularly scheduled program
  118.         super(FilePickerDelegate, self).paint(painter, option, index)
  119.  
  120.     def editorEvent(self, event, model, option, index):
  121.         if event.type() & event.MouseButtonPress:
  122.             #recreate what happens normally with the delegate
  123.             par = self.parent().viewport()
  124.             ed = self.createEditor(par, option, index)
  125.             self.setEditorData(ed, index)
  126.             pos = event.pos() - option.rect.topLeft()
  127.             ev = QMouseEvent(event.type(), pos, event.button(), event.buttons(), event.modifiers())
  128.             #ed.mousePressEvent(ev) ### Doesn't work
  129.             return True
  130.         return super(FilePickerDelegate, self).editorEvent(event, model, option, index)
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137. class TestWidget(QWidget):
  138.     def __init__(self, parent=None):
  139.         QWidget.__init__(self, parent)
  140.         self.tableModel = TableModel(self)
  141.  
  142.         self.table = TableView(self)
  143.         self.table.setItemDelegate(ComboDelegate(self.table))
  144.         self.table.setItemDelegateForColumn(3, FilePickerDelegate(self.table))
  145.         self.table.setMouseTracking(True)
  146.         self.table.setModel(self.tableModel)
  147.         #self.table.setEditTriggers(self.table.AllEditTriggers)
  148.  
  149.         l = QVBoxLayout(self)
  150.         l.addWidget(self.table)
  151.  
  152. if __name__ == "__main__":
  153.     from sys import argv, exit
  154.     a = QApplication(argv)
  155.     w = TestWidget()
  156.     w.show()
  157.     w.raise_()
  158.     exit(a.exec_())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement