forked from panda3d/panda3d
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTaskManagerPanel.py
More file actions
221 lines (191 loc) · 7.77 KB
/
TaskManagerPanel.py
File metadata and controls
221 lines (191 loc) · 7.77 KB
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
"""Undocumented Module"""
__all__ = ['TaskManagerPanel', 'TaskManagerWidget']
from direct.tkwidgets.AppShell import *
from direct.showbase.DirectObject import DirectObject
import Pmw
from tkinter import *
from tkinter.messagebox import askokcancel
class TaskManagerPanel(AppShell):
# Override class variables here
appname = 'TaskManager Panel'
frameWidth = 300
frameHeight = 400
usecommandarea = 0
usestatusarea = 0
def __init__(self, taskMgr, parent = None, **kw):
INITOPT = Pmw.INITOPT
optiondefs = (
('title', self.appname, None),
)
self.defineoptions(kw, optiondefs)
self.taskMgr = taskMgr
# Call superclass initialization function
AppShell.__init__(self, parent = parent)
self.initialiseoptions(TaskManagerPanel)
def createInterface(self):
# FILE MENU
# Get a handle on the file menu so commands can be inserted
# before quit item
self.taskMgrWidget = TaskManagerWidget(
self.interior(), self.taskMgr)
def onDestroy(self, event):
self.taskMgrWidget.onDestroy()
class TaskManagerWidget(DirectObject):
"""
TaskManagerWidget class: this class contains methods for creating
a panel to control taskManager tasks.
"""
def __init__(self, parent, taskMgr):
"""
TaskManagerWidget class pops up a control panel to view/delete
tasks managed by the taskManager.
"""
# Make sure TK mainloop is running
from direct.showbase import TkGlobal
# Record parent (used by ok cancel dialog boxes)
self.parent = parent
# Record taskManager
self.taskMgr = taskMgr
# Init current task
self.currentTask = None
self.__taskDict = {}
# Create widgets
# Create a listbox
self.taskListBox = Pmw.ScrolledListBox(
parent,
labelpos = NW, label_text = 'Tasks:',
label_font=('MSSansSerif', 10, 'bold'),
listbox_takefocus = 1,
items = [],
selectioncommand = self.setCurrentTask)
self.taskListBox.pack(expand = 1, fill = BOTH)
self._popupMenu = Menu(self.taskListBox.component('listbox'),
tearoff = 0)
self._popupMenu.add_command(
label = 'Remove Task',
command = self.removeCurrentTask)
self._popupMenu.add_command(
label = 'Remove Matching Tasks',
command = self.removeMatchingTasks)
# Controls Frame
controlsFrame = Frame(parent)
self.removeButton = Button(controlsFrame, text = 'Remove Task',
command = self.removeCurrentTask)
#self.removeButton.pack(expand = 1, fill = X, side = LEFT)
self.removeButton.grid(row = 0, column = 0, sticky = EW)
self.removeMatchingButton = Button(controlsFrame,
text = 'Remove Matching Tasks',
command = self.removeMatchingTasks)
#self.removeMatchingButton.pack(expand = 1, fill = X, side = LEFT)
self.removeMatchingButton.grid(row = 0, column = 1, sticky = EW)
self.taskMgrVerbose = IntVar()
self.taskMgrVerbose.set(0)
self.update = Button(
controlsFrame,
text = 'Update',
command = self.updateTaskListBox)
#self.update.pack(expand = 1, fill = X, side = LEFT)
self.update.grid(row = 1, column = 0, sticky = EW)
self.dynamicUpdate = Checkbutton(
controlsFrame,
text = 'Dynamic Update',
variable = self.taskMgrVerbose,
command = self.toggleTaskMgrVerbose)
#self.dynamicUpdate.pack(expand = 1, fill = X, side = LEFT)
self.dynamicUpdate.grid(row = 1, column = 1, sticky = EW)
# Pack frames
controlsFrame.pack(fill = X)
controlsFrame.grid_columnconfigure(0, weight = 1)
controlsFrame.grid_columnconfigure(1, weight = 1)
# Add hook to spawnTaskEvents
self.accept('TaskManager-spawnTask', self.spawnTaskHook)
self.accept('TaskManager-removeTask', self.removeTaskHook)
# Get listbox
listbox = self.taskListBox.component('listbox')
# Bind updates to arrow buttons
listbox.bind('<KeyRelease-Up>', self.setCurrentTask)
listbox.bind('<KeyRelease-Down>', self.setCurrentTask)
listbox.bind('<ButtonPress-3>', self.popupMenu)
# And grab focus (to allow keyboard navigation)
listbox.focus_set()
# Update listbox values
self.updateTaskListBox()
def popupMenu(self, event):
"""
listbox = self.taskListBox.component('listbox')
index = listbox.nearest(event.y)
listbox.selection_clear(0)
listbox.activate(index)
self.taskListBox.select_set(index)
self.setCurrentTask()
"""
self._popupMenu.post(event.widget.winfo_pointerx(),
event.widget.winfo_pointery())
return "break"
def setCurrentTask(self, event = None):
if len(self.taskListBox.curselection()) > 0: # [gjeon] to avoid crash when nothing is selected
index = int(self.taskListBox.curselection()[0])
self.currentTask = self.__taskDict[index]
else:
self.currentTask = None
def updateTaskListBox(self):
# Get a list of task names
taskNames = []
self.__taskDict = {}
tasks = self.taskMgr.getTasks()
tasks.sort(key = lambda t: t.getName())
count = 0
for task in tasks:
taskNames.append(task.getName())
self.__taskDict[count] = task
count += 1
if taskNames:
self.taskListBox.setlist(taskNames)
# And set current index (so keypresses will start with index 0)
self.taskListBox.component('listbox').activate(0)
# Select first item
#self.taskListBox.select_set(0) # [gjeon] commented out to avoid focus problem with other lists
self.setCurrentTask()
def toggleTaskMgrVerbose(self):
if self.taskMgrVerbose.get():
self.updateTaskListBox()
def spawnTaskHook(self, task):
if self.taskMgrVerbose.get():
self.updateTaskListBox()
def removeTaskHook(self, task):
if self.taskMgrVerbose.get():
self.updateTaskListBox()
def removeCurrentTask(self):
if self.currentTask:
name = self.currentTask.name
ok = 1
if ((name == 'dataLoop') or
(name == 'resetPrevTransform') or
(name == 'tkLoop') or
(name == 'eventManager') or
(name == 'igLoop')):
ok = askokcancel('TaskManagerControls',
'Remove: %s?' % name,
parent = self.parent,
default = 'cancel')
if ok:
self.taskMgr.remove(self.currentTask)
self.updateTaskListBox()
def removeMatchingTasks(self):
name = self.taskListBox.getcurselection()[0]
ok = 1
if ((name == 'dataLoop') or
(name == 'resetPrevTransform') or
(name == 'tkLoop') or
(name == 'eventManager') or
(name == 'igLoop')):
ok = askokcancel('TaskManagerControls',
'Remove tasks named: %s?' % name,
parent = self.parent,
default = 'cancel')
if ok:
self.taskMgr.remove(name)
self.updateTaskListBox()
def onDestroy(self):
self.ignore('TaskManager-spawnTask')
self.ignore('TaskManager-removeTask')