| 1 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 2 |
#~ Imports |
|---|
| 3 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 4 |
|
|---|
| 5 |
import weakref |
|---|
| 6 |
from wxPython import wx |
|---|
| 7 |
from RBFoundation.SubObs.Event import ObjectEventProperty |
|---|
| 8 |
|
|---|
| 9 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 10 |
#~ Definitions |
|---|
| 11 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 12 |
|
|---|
| 13 |
class wxListItemBehaviorBase(object): |
|---|
| 14 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 15 |
#~ Public Methods |
|---|
| 16 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 17 |
|
|---|
| 18 |
def SetListModel(self, listmodel): |
|---|
| 19 |
try: |
|---|
| 20 |
self.listmodel = weakref.proxy(listmodel) |
|---|
| 21 |
except TypeError: |
|---|
| 22 |
self.listmodel = listmodel |
|---|
| 23 |
|
|---|
| 24 |
def GetListItem(self): |
|---|
| 25 |
return self.GetListItems()[0] |
|---|
| 26 |
|
|---|
| 27 |
def GetListItems(self): |
|---|
| 28 |
try: |
|---|
| 29 |
return self.listitems |
|---|
| 30 |
except AttributeError: |
|---|
| 31 |
self.listitems = self.CreateListItems() |
|---|
| 32 |
return self.listitems |
|---|
| 33 |
|
|---|
| 34 |
def CreateListItems(self): |
|---|
| 35 |
return [self.NewListItem(True)] |
|---|
| 36 |
|
|---|
| 37 |
def NewListItem(self, addData=False): |
|---|
| 38 |
listitem = wx.wxListItem() |
|---|
| 39 |
if addData: |
|---|
| 40 |
mask = listitem.GetMask() |
|---|
| 41 |
mask |= wx.wxLIST_MASK_DATA |
|---|
| 42 |
listitem.SetMask(mask) |
|---|
| 43 |
listitem.SetData(id(self)) |
|---|
| 44 |
return listitem |
|---|
| 45 |
|
|---|
| 46 |
#def OnDrag(self, evt): pass |
|---|
| 47 |
#def OnRightDrag(self, evt): pass |
|---|
| 48 |
#def OnLabelEdit(self, evt): pass |
|---|
| 49 |
#def OnEndLabelEdit(self, evt): pass |
|---|
| 50 |
#def OnDeleteItem(self, evt): pass |
|---|
| 51 |
#def OnSelectItem(self, evt): pass |
|---|
| 52 |
#def OnDeselectItem(self, evt): pass |
|---|
| 53 |
#def OnActivateItem(self, evt): pass |
|---|
| 54 |
#def OnFocusedItem(self, evt): pass |
|---|
| 55 |
#def OnMiddleClick(self, evt): pass |
|---|
| 56 |
#def OnRightClick(self, evt): pass |
|---|
| 57 |
#def OnKeyDown(self, evt): pass |
|---|
| 58 |
|
|---|
| 59 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 60 |
|
|---|
| 61 |
class wxReflectedListModel(object): |
|---|
| 62 |
OnAddedNode = ObjectEventProperty() # listmodel, listnode |
|---|
| 63 |
OnCleared = ObjectEventProperty() # listmodel |
|---|
| 64 |
|
|---|
| 65 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 66 |
#~ Constants / Variables / Etc. |
|---|
| 67 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 68 |
|
|---|
| 69 |
listwin = None |
|---|
| 70 |
imagelistkindmap = { |
|---|
| 71 |
'normal':wx.wxIMAGE_LIST_NORMAL, |
|---|
| 72 |
'small':wx.wxIMAGE_LIST_SMALL, |
|---|
| 73 |
'state':wx.wxIMAGE_LIST_STATE, |
|---|
| 74 |
} |
|---|
| 75 |
|
|---|
| 76 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 77 |
#~ Public Methods |
|---|
| 78 |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 79 |
|
|---|
| 80 |
def __init__(self, listwin=None): |
|---|
| 81 |
self.imagelists = {} |
|---|
| 82 |
if listwin is not None: |
|---|
| 83 |
self.SetListWindow(listwin) |
|---|
| 84 |
|
|---|
| 85 |
def SetImageList(self, imagelist, kind='normal'): |
|---|
| 86 |
self.imagelists[kind] = imagelist |
|---|
| 87 |
self.listwin.SetImageList(imagelist, self.imagelistkindmap[kind]) |
|---|
| 88 |
|
|---|
| 89 |
def ClearList(self): |
|---|
| 90 |
self.listwin.DeleteAllItems() |
|---|
| 91 |
for imagelist in self.imagelists.itervalues(): |
|---|
| 92 |
imagelist.RemoveAll() |
|---|
| 93 |
self._IDtoItem = {} |
|---|
| 94 |
self.OnCleared() |
|---|
| 95 |
|
|---|
| 96 |
def AppendItem(self, listbehavioritem): |
|---|
| 97 |
listbehavioritem.SetListModel(self) |
|---|
| 98 |
listitems = listbehavioritem.GetListItems() |
|---|
| 99 |
key = listitems[0].GetData() |
|---|
| 100 |
self._IDtoItem[key] = listbehavioritem |
|---|
| 101 |
row = self.listwin.InsertItem(listitems[0]) |
|---|
| 102 |
for listitem in listitems: |
|---|
| 103 |
listitem.SetId(row) |
|---|
| 104 |
self.listwin.SetItem(listitem) |
|---|
| 105 |
self.OnAddedNode(listbehavioritem) |
|---|
| 106 |
|
|---|
| 107 |
def SetListWindow(self, listwin): |
|---|
| 108 |
self.listwin = listwin |
|---|
| 109 |
if self.listwin is not None: |
|---|
| 110 |
listid = self.listwin.GetId() |
|---|
| 111 |
wx.EVT_LIST_BEGIN_DRAG(self.listwin, listid, self._CallbackReflection('OnDrag')) |
|---|
| 112 |
wx.EVT_LIST_BEGIN_RDRAG(self.listwin, listid, self._CallbackReflection('OnRightDrag')) |
|---|
| 113 |
wx.EVT_LIST_BEGIN_LABEL_EDIT(self.listwin, listid, self._CallbackReflection('OnLabelEdit')) |
|---|
| 114 |
wx.EVT_LIST_END_LABEL_EDIT(self.listwin, listid, self._CallbackReflection('OnEndLabelEdit')) |
|---|
| 115 |
wx.EVT_LIST_DELETE_ITEM(self.listwin, listid, self._CallbackReflection('OnDeleteItem')) |
|---|
| 116 |
wx.EVT_LIST_ITEM_SELECTED(self.listwin, listid, self._CallbackReflection('OnSelectItem')) |
|---|
| 117 |
wx.EVT_LIST_ITEM_DESELECTED(self.listwin, listid, self._CallbackReflection('OnDeselectItem')) |
|---|
| 118 |
wx.EVT_LIST_ITEM_ACTIVATED(self.listwin, listid, self._CallbackReflection('OnActivateItem')) |
|---|
| 119 |
wx.EVT_LIST_ITEM_FOCUSED(self.listwin, listid, self._CallbackReflection('OnFocusedItem')) |
|---|
| 120 |
wx.EVT_LIST_ITEM_MIDDLE_CLICK(self.listwin, listid, self._CallbackReflection('OnMiddleClick')) |
|---|
| 121 |
wx.EVT_LIST_ITEM_RIGHT_CLICK(self.listwin, listid, self._CallbackReflection('OnRightClick')) |
|---|
| 122 |
wx.EVT_LIST_KEY_DOWN(self.listwin, listid, self._CallbackReflection('OnKeyDown')) |
|---|
| 123 |
|
|---|
| 124 |
class _Reflection(object): |
|---|
| 125 |
def __init__(self, listmodel, callname): |
|---|
| 126 |
self.listmodel = listmodel |
|---|
| 127 |
self.callname = callname |
|---|
| 128 |
def __call__(self, evt): |
|---|
| 129 |
try: |
|---|
| 130 |
item = self.listmodel._IDtoItem[evt.GetData()] |
|---|
| 131 |
except KeyError: |
|---|
| 132 |
evt.Skip() |
|---|
| 133 |
return |
|---|
| 134 |
except weakref.ReferenceError: |
|---|
| 135 |
evt.Skip() |
|---|
| 136 |
return |
|---|
| 137 |
try: callback = getattr(item, self.callname) |
|---|
| 138 |
except AttributeError: evt.Skip() |
|---|
| 139 |
else: return callback(evt) |
|---|
| 140 |
|
|---|
| 141 |
def _CallbackReflection(self, *args, **kw): |
|---|
| 142 |
return self._Reflection(weakref.proxy(self), *args, **kw) |
|---|
| 143 |
|
|---|