#!/usr/bin/python2.2
#^--- change the above line to point to your installation of Python
# $Id: //guest/mitch_stuart/perforce/utils/delta/p4DirDialog.py#1 $
from wxPython.wx import *
import p4Util
import pyUtil
class p4DirDialog(wxDialog):
def __init__(self, dirPath, parent, id, title="Select Directory",
pos=wxDefaultPosition, size=wxSize(300, 300),
style=wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER):
wxDialog.__init__(self, parent, id, title, pos, size, style)
self.treeCtrl = DirTreeCtrl(self, -1, size=wxSize(300, 300))
self.rootItemData = DirItemData(dirPath, dirPath)
self.rootItem = self.treeCtrl.AddRoot(self.rootItemData.name)
self.treeCtrl.SetPyData(self.rootItem, self.rootItemData)
self.loadChildren(self.rootItem)
self.treeCtrl.Expand(self.rootItem)
self.okButton = wxButton(self, wxID_OK, " OK ")
self.okButton.SetDefault()
self.cancelButton = wxButton(self, wxID_CANCEL, " Cancel ")
sizer = wxBoxSizer(wxVERTICAL)
sizer.Add(self.treeCtrl, 1, wxEXPAND)
buttonSizer = wxBoxSizer(wxHORIZONTAL)
buttonSizer.AddMany([self.okButton, self.cancelButton])
sizer.Add(buttonSizer, flag=wxALIGN_CENTER)
self.SetSizer(sizer)
sizer.Fit(self)
self.Center()
EVT_TREE_ITEM_EXPANDING(self, self.treeCtrl.GetId(), self.onItemExpanding)
def onItemExpanding(self, evt):
try:
wxBeginBusyCursor()
self.loadChildren(evt.GetItem())
finally:
wxEndBusyCursor()
def loadChildren(self, parentItem):
parentData = self.treeCtrl.GetPyData(parentItem)
if parentData.loadedChildren:
return
# print "Loading children for: %s" % parentData.fullPath
# Need to delete the current children (if any) before loading the
# children, because we create a fake child to force the "+" sign to
# appear when children are not loaded yet (see below).
self.treeCtrl.DeleteChildren(parentItem)
startPath = "%s/" % parentData.fullPath
startPathLen = len(startPath)
dirs = p4Util.execP4Marshal("dirs %s*" % startPath)
for dir in dirs:
fullPath = dir["dir"]
name = fullPath[startPathLen:]
item = self.treeCtrl.AppendItem(parentItem, name)
itemData = DirItemData(name, fullPath)
self.treeCtrl.SetPyData(item, itemData)
# Create a fake child to force the "+" sign to appear for this
# new item. When the user actually expands the item, we will
# determine if there are actually any children or not.
fakeChild = self.treeCtrl.AppendItem(item, "placeholder")
self.treeCtrl.SetPyData(fakeChild, None)
self.treeCtrl.SortChildren(parentItem)
parentData.loadedChildren = True
def getSelectedFullPath(self):
selectedItem = self.treeCtrl.GetSelection()
return self.treeCtrl.GetPyData(selectedItem).fullPath
class DirTreeCtrl(wxTreeCtrl):
def __init__(self, parent, id, pos=wxDefaultPosition, size=wxDefaultSize,
style=wxTR_HAS_BUTTONS):
wxTreeCtrl.__init__(self, parent, id, pos, size, style)
def OnCompareItems(self, item1, item2):
return pyUtil.cmpIgnoreCase(self.GetItemText(item1),
self.GetItemText(item2))
class DirItemData:
def __init__(self, name, fullPath):
self.name = name
self.fullPath = fullPath
self.loadedChildren = False
class MyApp(wxApp):
def OnInit(self):
frame = wxFrame(None, -1, "Test")
frame.Show(true)
self.SetTopWindow(frame)
dialog = p4DirDialog(gRootPath, frame, -1)
dialog.ShowModal()
return true
def main():
app = MyApp(0)
app.MainLoop()
if __name__ == '__main__':
gRootPath = "//depot"
main()