Guest User

Untitled

a guest
Oct 12th, 2013
171
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from PySide import QtGui
  2. from PySide import QtCore
  3.  
  4. class SpinBoxDelegate(QtGui.QItemDelegate):
  5.     def __init__(self, parent=None):
  6.         super(SpinBoxDelegate, self).__init__(parent)
  7.  
  8.     def createEditor(self, parent, option, index):
  9.         editor = QtGui.QSpinBox(parent)
  10.         editor.setMinimum(0)
  11.         editor.setMaximum(100)
  12.         editor.installEventFilter(self)
  13.  
  14.         return editor
  15.    
  16.     def setEditorData(self, spinBox, index):
  17.         value = index.model().data(index, QtCore.Qt.DisplayPropertyRole)
  18.         spinBox.setValue(value)
  19.  
  20.     def setModelData(self, spinBox, model, index):
  21.         spinBox.interpretText()
  22.         value = spinBox.value()
  23.         model.setData(index, data)
  24.  
  25.     def updateEditorGeometry(self, editor, option, index):
  26.         editor.setGeometry(option.rect)
  27.  
  28.        
  29. class ToDoProxyModel(QtGui.QSortFilterProxyModel):
  30.     '''Proxy Model for filtering and sorting - not yet in use'''
  31.    
  32.     def __init__(self, parent=None):
  33.         super(ToDoProxyModel, self).__init__(parent)
  34.  
  35. class ToDoPanel(QtGui.QWidget):
  36.     '''Panel to create and manage a to-do list'''
  37.     def __init__(self, parent=None):
  38.         super(ToDoPanel, self).__init__(parent)
  39.         self.setupModel()
  40.         self.buildUI()
  41.         self.connectSignalsWithSlots()
  42.         # ADD THE FIRST TASK
  43.         self.addTask()
  44.  
  45.     def setupModel(self):
  46.         #self.STATUSROLE = QtCore.Qt.UserRole + 1
  47.         #self.PRIORITYROLE = QtCore.Qt.UserRole + 2
  48.         self.model = QtGui.QStandardItemModel()
  49.  
  50.     def buildUI(self):
  51.         ## LAYOUT
  52.         layout = QtGui.QVBoxLayout()
  53.         self.setLayout(layout)
  54.        
  55.         ## TOOL BAR
  56.         toolLayout = QtGui.QHBoxLayout()
  57.         self.addButton = QtGui.QPushButton('add task')
  58.         toolLayout.addWidget(self.addButton)
  59.        
  60.         ## TABLE VIEW
  61.         tableView = QtGui.QTableView()
  62.         tableView.setModel(self.model)
  63.         tableView.setSortingEnabled(True)
  64.         tableView.setSelectionBehavior(QtGui.QAbstractItemView.SelectRows)
  65.         priorityDelegate = SpinBoxDelegate()
  66.         tableView.setItemDelegate(priorityDelegate)
  67.  
  68.         ## CREATE UI
  69.         layout.addLayout(toolLayout)
  70.         layout.addWidget(tableView)
  71.  
  72.     def addTask(self):
  73.         row = self.model.rowCount()
  74.         newTitle = QtGui.QStandardItem('new task')
  75.         newPriority = QtGui.QStandardItem(1)
  76.         newStatus = QtGui.QStandardItem(False)
  77.         self.model.setItem(row, 0, newTitle)
  78.         self.model.setItem(row, 1, newPriority) # NEEDS SPIN BOX
  79.         self.model.setItem(row, 2, newStatus) # NEEDS CHECKBOX
  80.  
  81.     def connectSignalsWithSlots(self):
  82.         self.addButton.clicked.connect(self.addTask)
  83.  
  84.  
  85.  
  86. if __name__ == '__main__':
  87.     import sys
  88.     app = QtGui.QApplication([])
  89.     w = ToDoPanel()
  90.     w.resize(400, 200)
  91.     w.show()
  92.     sys.exit(app.exec_())
RAW Paste Data