spacepaste

  1.  
  2. import sys
  3. from PyQt5.QtWidgets import QItemDelegate, QTreeWidget, QVBoxLayout, QLineEdit,\
  4. QMainWindow, QWidget, QTreeWidgetItem, QApplication
  5. from PyQt5.QtCore import QRegExp, Qt
  6. from PyQt5.QtGui import QRegExpValidator, QValidator
  7. class MyValidator(QValidator):
  8. def __init__(self, siblings, parent):
  9. super(MyValidator,self).__init__(parent)
  10. self.siblings = siblings
  11. def validate(self, s, pos):
  12. # look for invalid chars
  13. reg=QRegExp('[A-z0-9\[\]_-]+')
  14. regvd=QRegExpValidator(reg)
  15. regvd_rec=regvd.validate(s,pos)[0]
  16. print('regexp validate result:',regvd_rec)
  17. # check name conflicts
  18. '''
  19. if s in self.siblings:
  20. siblingvd_rec=0
  21. else:
  22. current_len=len(s)
  23. sub_strings=[sii[:current_len] for sii in self.siblings]
  24. if s in sub_strings:
  25. siblingvd_rec=1
  26. else:
  27. siblingvd_rec=2
  28. '''
  29. siblingvd_rec=2
  30. print('siblings validate result:',siblingvd_rec)
  31. if regvd_rec*siblingvd_rec==0:
  32. print('Invalid')
  33. return (QValidator.Invalid, s, pos)
  34. elif regvd_rec+siblingvd_rec==4:
  35. print('Acceptable')
  36. return (QValidator.Acceptable, s, pos)
  37. else:
  38. print('Intermediate')
  39. return (QValidator.Intermediate, s, pos)
  40. def fixup(self, s):
  41. pass
  42. class TreeWidgetDelegate(QItemDelegate):
  43. def __init__(self, siblings, parent=None):
  44. QItemDelegate.__init__(self, parent=parent)
  45. self.siblings=siblings
  46. self.parent=parent
  47. def createEditor(self, parent, option, index):
  48. editor = QLineEdit(parent)
  49. vd=MyValidator(self.siblings,self.parent)
  50. editor.setValidator(vd)
  51. return editor
  52. class MainWindow(QMainWindow):
  53. def __init__(self):
  54. super(self.__class__, self).__init__()
  55. frame=QWidget()
  56. self.setCentralWidget(frame)
  57. hl=QVBoxLayout()
  58. frame.setLayout(hl)
  59. self.tree=QTreeWidget(self)
  60. hl.addWidget(self.tree)
  61. # add treewidgetitems
  62. for ii in range(5):
  63. item=QTreeWidgetItem([str(ii)*3,])
  64. self.tree.addTopLevelItem(item)
  65. self.tree.itemDoubleClicked.connect(self.rename)
  66. self.tree.itemChanged.connect(self.checkName, Qt.QueuedConnection)
  67. self.show()
  68. def getSiblings(self,item):
  69. siblings=[self.tree.topLevelItem(ii).data(0,0) for ii in \
  70. range(self.tree.topLevelItemCount())]
  71. item_text=item.data(0,0)
  72. if item_text in siblings:
  73. siblings.remove(item_text)
  74. return siblings
  75. def rename(self):
  76. item=self.tree.selectedItems()
  77. if item:
  78. item=item[0]
  79. item.setFlags(item.flags() | Qt.ItemIsEditable)
  80. self.tree.scrollToItem(item)
  81. dele=TreeWidgetDelegate(self.getSiblings(item),self)
  82. self.tree.setItemDelegate(dele)
  83. self.tree.editItem(item)
  84. def checkName(self,item,column):
  85. text=item.data(0,0)
  86. siblings=self.getSiblings(item)
  87. print('checkName: slibings:', siblings)
  88. if text in siblings:
  89. print('checkName: ivalid')
  90. item.setData(0,0,'New_name_needed')
  91. self.tree.editItem(item)
  92. if __name__ == "__main__":
  93. app = QApplication(sys.argv)
  94. form = MainWindow()
  95. form.show()
  96. sys.exit(app.exec_())
  97.