-
Notifications
You must be signed in to change notification settings - Fork 25
/
notebook.py
252 lines (203 loc) · 6.71 KB
/
notebook.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# The Admin4 Project
# (c) 2013-2022 Andreas Pflug
#
# Licensed under the Apache License,
# see LICENSE.TXT for conditions of usage
import wx
import adm
import wh
import logger
from wh import Menu
class _TimerOwner:
def __init__(self):
self.refreshDisplay=None
self.timer=None
def SetRefreshTimer(self, proc, timeout=1):
"""
SetRefreshTimer(self, proc, timeout=1)
Sets the refresh timer to <timeout> seconds
when elapsed, <proc> is called
When <timeout> is 0, the timer is stopped
"""
if proc != self.refreshDisplay or not timeout:
if self.timer:
self.timer.Stop()
self.refreshDisplay=proc
if proc and timeout:
self.timer=wh.Timer(self, self.OnTimer)
self.timer.Start(int(timeout*1000))
else:
self.timer=None
def OnTimer(self, _ev=None):
if self.refreshDisplay:
self.refreshDisplay()
else:
self.timer.Stop()
self.timer=None
class Notebook(wx.Notebook, adm.MenuOwner, _TimerOwner):
def __init__(self, parentWin, size=wx.DefaultSize):
wx.Notebook.__init__(self, parentWin, size=size)
_TimerOwner.__init__(self)
self.node=None
self.currentPage=0
self.Bind(wx.EVT_NOTEBOOK_PAGE_CHANGED, self.OnPageChange)
self.knownPages={}
self.isDetached=False
def OnTimer(self, _ev=None):
if self.refreshDisplay and self.node:
self.refreshDisplay(self.node, self.GetTopLevelParent() == self.node.detachedWindow)
else:
if self.timer:
self.timer.Stop()
self.timer=None
def Freeze(self):
return
# TODO we don't like this
if not wx.version().endswith(" osx-cocoa") or not wx.version().startswith("3.0"):
super(Notebook, self).Freeze()
def Thaw(self):
return
# TODO we don't like this
if not wx.version().endswith(" osx-cocoa") or not wx.version().startswith("3.0"):
super(Notebook, self).Thaw()
def AppendPage(self, page):
self.DoInsertPage(page, None)
def PrependPage(self, page):
self.DoInsertPage(page, self.propPageIndex)
self.propPageIndex += 1
def DoInsertPage(self, page, pos):
if not isinstance(page, wx.Window):
page=page(self)
ctl=page.GetControl()
if pos == None:
self.AddPage(ctl, page.name)
self.pages.append(page)
else:
self.InsertPage(pos, ctl, page.name)
self.pages.insert(pos, page)
if isinstance(ctl, wx.ListCtrl):
ctl.Bind(wx.EVT_LIST_ITEM_ACTIVATED, self.OnItemDoubleClick)
ctl.Bind(wx.EVT_LIST_ITEM_RIGHT_CLICK, self.OnItemRightClick)
ctl.Bind(wx.EVT_LIST_COL_CLICK, self.OnColClick)
if wx.Platform == "__WXMSW__":
ctl.Bind(wx.EVT_RIGHT_UP, self.OnItemRightClick)
def Set(self, node):
self.node=node
self.Freeze()
self.DeleteAllPages()
pagecount=self.GetPageCount()
if not pagecount:
self.propPageIndex=0
self.pages=[]
self.AppendPage(adm.PropertyPage)
for n in range(pagecount-1, -1, -1):
if n != self.propPageIndex:
self.RemovePage(n)
del self.pages[n]
self.propPageIndex=0
if hasattr(node, "nodeinfo"):
dashboardClass=node.nodeinfo().get("dashboard")
if dashboardClass:
self.PrependPage(dashboardClass)
if hasattr(node, "moduleinfo"):
addPages=node.moduleinfo().get("pages", [])
if not isinstance(addPages, list):
addPages=[addPages]
for pageClass in addPages:
if node.IsPageAvailable(pageClass, self.isDetached):
self.AppendPage(pageClass)
if self.currentPage >= self.GetPageCount():
self.currentPage=0
self.SetSelection(self.currentPage)
self.doDisplay()
self.Thaw()
def OnPageChange(self, ev):
self.currentPage=ev.GetSelection()
self.doDisplay()
def doDisplay(self):
if self.currentPage >= len(self.pages):
return
page=self.pages[self.currentPage]
if self.refreshDisplay != page.Display:
self.SetRefreshTimer(None)
if self.node:
page.Display(self.node, self.GetTopLevelParent() == self.node.detachedWindow)
page.GetControl().Show()
def OnCall(self, evt):
"""
OnCall(wx.Event)
calls registered procedures from an event using appropriate arguments
It handles:
@staticmethod DoSomething(parentNotebook, currentPage)
@staticmethod DoSomething(parentNotebook)
DoSomething(self, parentNotebook, currentPage)
DoSomething(self, parentNotebook)
"""
cid=evt.GetId()
proc=self.GetMenuProc(cid)
if proc:
args=self.GetCallArgs(proc)
if len(args) and args[0] == "self":
del args[0]
ci=[proc.__module__]
if hasattr(proc, "_classname_"):
ci.append(proc._classname_)
ci.append(proc.__name__)
logger.debug("Calling ID %d: %s", cid, ".".join(ci))
if len(args) == 2:
page=self.pages[self.GetSelection()]
proc(self, page)
else:
proc(self)
def OnColClick(self, evt):
if self.node:
page=self.pages[self.currentPage]
if page and hasattr(page, "OnColClick"):
page.OnColClick(evt)
def OnItemRightClick(self, evt):
if self.node:
if hasattr(evt, 'GetIndex'):
index=evt.GetIndex()
else:
index=-1
page=self.pages[self.GetSelection()]
if not hasattr(evt, 'page'):
evt.page=page
if hasattr(page, 'menus'):
cm=Menu(self)
menus=page.menus
for cls in menus:
if hasattr(cls, "CheckAvailableOn") and not cls.CheckAvailableOn(page):
continue
cls.OnExecute._classname_=cls.__name__
item=cm.Add(cls.OnExecute, cls.name, cls.help)
if hasattr(cls, "CheckEnabled") and not cls.CheckEnabled(page):
cm.Enable(item, False)
if cm.GetMenuItemCount():
cm.Popup(evt)
elif hasattr(self.node, "OnItemRightClick"):
evt.currentPage = page
self.node.OnItemRightClick(evt)
elif hasattr(self.node, "GetItemNode"):
node=self.node.GetItemNode(evt.page, index)
if node:
node.RefreshVolatile()
node.GetProperties()
w=adm.GetCurrentFrame(self)
cm=w.GetContextMenu(node)
w.currentNode=node
cm.Popup(evt)
w.currentNode=None
def OnItemDoubleClick(self, evt):
if self.node:
page=self.pages[self.currentPage]
if hasattr(self.node, "OnItemDoubleClick"):
evt.currentPage = page
self.node.OnItemDoubleClick(evt)
elif hasattr(page, "OnItemDoubleClick"):
page.OnItemDoubleClick(evt)
elif hasattr(self.node, "GetItemNode"):
node=self.node.GetItemNode(evt.currentPage, evt.GetIndex())
if node and hasattr(node, "Edit"):
node.GetProperties()
node.Edit(self)