Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

hiertest.py 3.7KB

1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import os
  2. import commctrl
  3. import win32ui
  4. from pywin.mfc import docview, window
  5. from pywin.tools import hierlist
  6. # directory listbox
  7. # This has obvious limitations - doesnt track subdirs, etc. Demonstrates
  8. # simple use of Python code for querying the tree as needed.
  9. # Only use strings, and lists of strings (from curdir())
  10. class DirHierList(hierlist.HierList):
  11. def __init__(self, root, listBoxID=win32ui.IDC_LIST1):
  12. hierlist.HierList.__init__(self, root, win32ui.IDB_HIERFOLDERS, listBoxID)
  13. def GetText(self, item):
  14. return os.path.basename(item)
  15. def GetSubList(self, item):
  16. if os.path.isdir(item):
  17. ret = [os.path.join(item, fname) for fname in os.listdir(item)]
  18. else:
  19. ret = None
  20. return ret
  21. # if the item is a dir, it is expandable.
  22. def IsExpandable(self, item):
  23. return os.path.isdir(item)
  24. def GetSelectedBitmapColumn(self, item):
  25. return self.GetBitmapColumn(item) + 6 # Use different color for selection
  26. class TestDocument(docview.Document):
  27. def __init__(self, template):
  28. docview.Document.__init__(self, template)
  29. self.hierlist = hierlist.HierListWithItems(
  30. HLIFileDir("\\"), win32ui.IDB_HIERFOLDERS, win32ui.AFX_IDW_PANE_FIRST
  31. )
  32. class HierListView(docview.TreeView):
  33. def OnInitialUpdate(self):
  34. rc = self._obj_.OnInitialUpdate()
  35. self.hierList = self.GetDocument().hierlist
  36. self.hierList.HierInit(self.GetParent())
  37. self.hierList.SetStyle(
  38. commctrl.TVS_HASLINES | commctrl.TVS_LINESATROOT | commctrl.TVS_HASBUTTONS
  39. )
  40. return rc
  41. class HierListFrame(window.MDIChildWnd):
  42. pass
  43. def GetTestRoot():
  44. tree1 = ("Tree 1", [("Item 1", "Item 1 data"), "Item 2", 3])
  45. tree2 = ("Tree 2", [("Item 2.1", "Item 2 data"), "Item 2.2", 2.3])
  46. return ("Root", [tree1, tree2, "Item 3"])
  47. def demoboth():
  48. template = docview.DocTemplate(
  49. win32ui.IDR_PYTHONTYPE, TestDocument, HierListFrame, HierListView
  50. )
  51. template.OpenDocumentFile(None).SetTitle("Hierlist demo")
  52. demomodeless()
  53. def demomodeless():
  54. testList2 = DirHierList("\\")
  55. dlg = hierlist.HierDialog("hier list test", testList2)
  56. dlg.CreateWindow()
  57. def demodlg():
  58. testList2 = DirHierList("\\")
  59. dlg = hierlist.HierDialog("hier list test", testList2)
  60. dlg.DoModal()
  61. def demo():
  62. template = docview.DocTemplate(
  63. win32ui.IDR_PYTHONTYPE, TestDocument, HierListFrame, HierListView
  64. )
  65. template.OpenDocumentFile(None).SetTitle("Hierlist demo")
  66. #
  67. # Demo/Test for HierList items.
  68. #
  69. # Easy to make a better directory program.
  70. #
  71. class HLIFileDir(hierlist.HierListItem):
  72. def __init__(self, filename):
  73. self.filename = filename
  74. hierlist.HierListItem.__init__(self)
  75. def GetText(self):
  76. try:
  77. return "%-20s %d bytes" % (
  78. os.path.basename(self.filename),
  79. os.stat(self.filename)[6],
  80. )
  81. except os.error as details:
  82. return "%-20s - %s" % (self.filename, details[1])
  83. def IsExpandable(self):
  84. return os.path.isdir(self.filename)
  85. def GetSubList(self):
  86. ret = []
  87. for newname in os.listdir(self.filename):
  88. if newname not in (".", ".."):
  89. ret.append(HLIFileDir(os.path.join(self.filename, newname)))
  90. return ret
  91. def demohli():
  92. template = docview.DocTemplate(
  93. win32ui.IDR_PYTHONTYPE,
  94. TestDocument,
  95. hierlist.HierListFrame,
  96. hierlist.HierListView,
  97. )
  98. template.OpenDocumentFile(None).SetTitle("Hierlist demo")
  99. if __name__ == "__main__":
  100. import demoutils
  101. if demoutils.HaveGoodGUI():
  102. demoboth()
  103. else:
  104. demodlg()