Debian Codegraph @

Xpjproject/pjsip-apps/src/pygui/chatgui.py

raw symbols SCIP
1 #
2 # pjsua Python GUI Demo
3 #
4 # Copyright (C)2013 Teluu Inc. (http://www.teluu.com)
5 #
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #
20 import sys
21 if sys.version_info[0] >= 3: # Python 3
22 import tkinter as tk
23 from tkinter import ttk
24 from tkinter import messagebox as msgbox
25 else:
26 import Tkinter as tk
27 import ttk
28 import tkMessageBox as msgbox
29
30
31 class TextObserver:
32 def onSendMessage(self, msg):
33 pass
34 def onStartTyping(self):
35 pass
36 def onStopTyping(self):
37 pass
38
39 class TextFrame(ttk.Frame):
40 def __init__(self, master, observer):
41 ttk.Frame.__init__(self, master)
42 self._observer = observer
43 self._isTyping = False
44 self._createWidgets()
45
46 def _onSendMessage(self, event):
47 send_text = self._typingBox.get("1.0", tk.END).strip()
48 if send_text == '':
49 return
50
51 self.addMessage('me: ' + send_text)
52 self._typingBox.delete("0.0", tk.END)
53 self._onTyping(None)
54
55 # notify app for sending message
56 self._observer.onSendMessage(send_text)
57
58 def _onTyping(self, event):
59 # notify app for typing indication
60 is_typing = self._typingBox.get("1.0", tk.END).strip() != ''
61 if is_typing != self._isTyping:
62 self._isTyping = is_typing
63 if is_typing:
64 self._observer.onStartTyping()
65 else:
66 self._observer.onStopTyping()
67
68 def _createWidgets(self):
69 self.rowconfigure(0, weight=1)
70 self.rowconfigure(1, weight=0)
71 self.rowconfigure(2, weight=0)
72 self.columnconfigure(0, weight=1)
73 self.columnconfigure(1, weight=0)
74
75 self._text = tk.Text(self, width=50, height=30, font=("Arial", "10"))
76 self._text.grid(row=0, column=0, sticky='nswe')
77 self._text.config(state=tk.DISABLED)
78 self._text.tag_config("info", foreground="darkgray", font=("Arial", "9", "italic"))
79
80 scrl = ttk.Scrollbar(self, orient=tk.VERTICAL, command=self._text.yview)
81 self._text.config(yscrollcommand=scrl.set)
82 scrl.grid(row=0, column=1, sticky='nsw')
83
84 self._typingBox = tk.Text(self, width=50, height=1, font=("Arial", "10"))
85 self._typingBox.grid(row=1, columnspan=2, sticky='we', pady=0)
86
87 self._statusBar = tk.Label(self, anchor='w', font=("Arial", "8", "italic"))
88 self._statusBar.grid(row=2, columnspan=2, sticky='we')
89
90 self._typingBox.bind('<Return>', self._onSendMessage)
91 self._typingBox.bind("<Key>", self._onTyping)
92 self._typingBox.focus_set()
93
94 def addMessage(self, msg, is_chat = True):
95 self._text.config(state=tk.NORMAL)
96 if is_chat:
97 self._text.insert(tk.END, msg+'\r\n')
98 else:
99 self._text.insert(tk.END, msg+'\r\n', 'info')
100 self._text.config(state=tk.DISABLED)
101 self._text.yview(tk.END)
102
103 def setTypingIndication(self, who, is_typing):
104 if is_typing:
105 self._statusBar['text'] = "'%s' is typing.." % (who)
106 else:
107 self._statusBar['text'] = ''
108
109 class AudioState:
110 NULL, INITIALIZING, CONNECTED, DISCONNECTED, FAILED = range(5)
111
112 class AudioObserver:
113 def onHangup(self, peer_uri):
114 pass
115 def onHold(self, peer_uri):
116 pass
117 def onUnhold(self, peer_uri):
118 pass
119 def onRxMute(self, peer_uri, is_muted):
120 pass
121 def onRxVol(self, peer_uri, vol_pct):
122 pass
123 def onTxMute(self, peer_uri, is_muted):
124 pass
125
126
127 class AudioFrame(ttk.Labelframe):
128 def __init__(self, master, peer_uri, observer):
129 ttk.Labelframe.__init__(self, master, text=peer_uri)
130 self.peerUri = peer_uri
131 self._observer = observer
132 self._initFrame = None
133 self._callFrame = None
134 self._rxMute = False
135 self._txMute = False
136 self._state = AudioState.NULL
137
138 self._createInitWidgets()
139 self._createWidgets()
140
141 def updateState(self, state):
142 if self._state == state:
143 return
144
145 if state == AudioState.INITIALIZING:
146 self._callFrame.pack_forget()
147 self._initFrame.pack(fill=tk.BOTH)
148 self._btnCancel.pack(side=tk.TOP)
149 self._lblInitState['text'] = 'Intializing..'
150
151 elif state == AudioState.CONNECTED:
152 self._initFrame.pack_forget()
153 self._callFrame.pack(fill=tk.BOTH)
154 else:
155 self._callFrame.pack_forget()
156 self._initFrame.pack(fill=tk.BOTH)
157 if state == AudioState.FAILED:
158 self._lblInitState['text'] = 'Failed'
159 else:
160 self._lblInitState['text'] = 'Normal cleared'
161 self._btnCancel.pack_forget()
162
163 self._btnHold['text'] = 'Hold'
164 self._btnHold.config(state=tk.NORMAL)
165 self._rxMute = False
166 self._txMute = False
167 self.btnRxMute['text'] = 'Mute'
168 self.btnTxMute['text'] = 'Mute'
169 self.rxVol.set(5.0)
170
171 # save last state
172 self._state = state
173
174 def setStatsText(self, stats_str):
175 self.stat.config(state=tk.NORMAL)
176 self.stat.delete("0.0", tk.END)
177 self.stat.insert(tk.END, stats_str)
178 self.stat.config(state=tk.DISABLED)
179
180 def _onHold(self):
181 self._btnHold.config(state=tk.DISABLED)
182 # notify app
183 if self._btnHold['text'] == 'Hold':
184 self._observer.onHold(self.peerUri)
185 self._btnHold['text'] = 'Unhold'
186 else:
187 self._observer.onUnhold(self.peerUri)
188 self._btnHold['text'] = 'Hold'
189 self._btnHold.config(state=tk.NORMAL)
190
191 def _onHangup(self):
192 # notify app
193 self._observer.onHangup(self.peerUri)
194
195 def _onRxMute(self):
196 # notify app
197 self._rxMute = not self._rxMute
198 self._observer.onRxMute(self.peerUri, self._rxMute)
199 self.btnRxMute['text'] = 'Unmute' if self._rxMute else 'Mute'
200
201 def _onRxVol(self, event):
202 # notify app
203 vol = self.rxVol.get()
204 self._observer.onRxVol(self.peerUri, vol*10.0)
205
206 def _onTxMute(self):
207 # notify app
208 self._txMute = not self._txMute
209 self._observer.onTxMute(self.peerUri, self._txMute)
210 self.btnTxMute['text'] = 'Unmute' if self._txMute else 'Mute'
211
212 def _createInitWidgets(self):
213 self._initFrame = ttk.Frame(self)
214 #self._initFrame.pack(fill=tk.BOTH)
215
216
217 self._lblInitState = tk.Label(self._initFrame, font=("Arial", "12"), text='')
218 self._lblInitState.pack(side=tk.TOP, fill=tk.X, expand=1)
219
220 # Operation: cancel/kick
221 self._btnCancel = ttk.Button(self._initFrame, text = 'Cancel', command=self._onHangup)
222 self._btnCancel.pack(side=tk.TOP)
223
224 def _createWidgets(self):
225 self._callFrame = ttk.Frame(self)
226 #self._callFrame.pack(fill=tk.BOTH)
227
228 # toolbar
229 toolbar = ttk.Frame(self._callFrame)
230 toolbar.pack(side=tk.TOP, fill=tk.X)
231 self._btnHold = ttk.Button(toolbar, text='Hold', command=self._onHold)
232 self._btnHold.pack(side=tk.LEFT, fill=tk.Y)
233 #self._btnXfer = ttk.Button(toolbar, text='Transfer..')
234 #self._btnXfer.pack(side=tk.LEFT, fill=tk.Y)
235 self._btnHangUp = ttk.Button(toolbar, text='Hangup', command=self._onHangup)
236 self._btnHangUp.pack(side=tk.LEFT, fill=tk.Y)
237
238 # volume tool
239 vol_frm = ttk.Frame(self._callFrame)
240 vol_frm.pack(side=tk.TOP, fill=tk.X)
241
242 self.rxVolFrm = ttk.Labelframe(vol_frm, text='RX volume')
243 self.rxVolFrm.pack(side=tk.LEFT, fill=tk.Y)
244
245 self.btnRxMute = ttk.Button(self.rxVolFrm, width=8, text='Mute', command=self._onRxMute)
246 self.btnRxMute.pack(side=tk.LEFT)
247 self.rxVol = tk.Scale(self.rxVolFrm, orient=tk.HORIZONTAL, from_=0.0, to=10.0, showvalue=1) #, tickinterval=10.0, showvalue=1)
248 self.rxVol.set(5.0)
249 self.rxVol.bind("<ButtonRelease-1>", self._onRxVol)
250 self.rxVol.pack(side=tk.LEFT)
251
252 self.txVolFrm = ttk.Labelframe(vol_frm, text='TX volume')
253 self.txVolFrm.pack(side=tk.RIGHT, fill=tk.Y)
254
255 self.btnTxMute = ttk.Button(self.txVolFrm, width=8, text='Mute', command=self._onTxMute)
256 self.btnTxMute.pack(side=tk.LEFT)
257
258 # stat
259 self.stat = tk.Text(self._callFrame, width=10, height=2, bg='lightgray', relief=tk.FLAT, font=("Courier", "9"))
260 self.stat.insert(tk.END, 'stat here')
261 self.stat.pack(side=tk.BOTTOM, fill=tk.BOTH, expand=1)
262
263
264 class ChatObserver(TextObserver, AudioObserver):
265 def onAddParticipant(self):
266 pass
267 def onStartAudio(self):
268 pass
269 def onStopAudio(self):
270 pass
271 def onCloseWindow(self):
272 pass
273
274 class ChatFrame(tk.Toplevel):
275 """
276 Room
277 """
278 def __init__(self, observer):
279 tk.Toplevel.__init__(self)
280 self.protocol("WM_DELETE_WINDOW", self._onClose)
281 self._observer = observer
282
283 self._text = None
284 self._text_shown = True
285
286 self._audioEnabled = False
287 self._audioFrames = []
288 self._createWidgets()
289
290 def _createWidgets(self):
291 # toolbar
292 self.toolbar = ttk.Frame(self)
293 self.toolbar.pack(side=tk.TOP, fill=tk.BOTH)
294
295 btnText = ttk.Button(self.toolbar, text='Show/hide text', command=self._onShowHideText)
296 btnText.pack(side=tk.LEFT, fill=tk.Y)
297 btnAudio = ttk.Button(self.toolbar, text='Start/stop audio', command=self._onStartStopAudio)
298 btnAudio.pack(side=tk.LEFT, fill=tk.Y)
299
300 ttk.Separator(self.toolbar, orient=tk.VERTICAL).pack(side=tk.LEFT, fill=tk.Y, padx = 4)
301
302 btnAdd = ttk.Button(self.toolbar, text='Add participant..', command=self._onAddParticipant)
303 btnAdd.pack(side=tk.LEFT, fill=tk.Y)
304
305 # media frame
306 self.media = ttk.Frame(self)
307 self.media.pack(side=tk.BOTTOM, fill=tk.BOTH, expand=1)
308
309 # create Text Chat frame
310 self.media_left = ttk.Frame(self.media)
311 self._text = TextFrame(self.media_left, self._observer)
312 self._text.pack(fill=tk.BOTH, expand=1)
313 self.media_left.pack(side=tk.LEFT, fill=tk.BOTH, expand=1)
314
315 # create other media frame
316 self.media_right = ttk.Frame(self.media)
317
318 def _arrangeMediaFrames(self):
319 if len(self._audioFrames) == 0:
320 self.media_right.pack_forget()
321 return
322
323 self.media_right.pack(side=tk.RIGHT, fill=tk.BOTH, expand=1)
324 MAX_ROWS = 3
325 row_num = 0
326 col_num = 1
327 for frm in self._audioFrames:
328 frm.grid(row=row_num, column=col_num, sticky='nsew', padx=5, pady=5)
329 row_num += 1
330 if row_num >= MAX_ROWS:
331 row_num = 0
332 col_num += 1
333
334 def _onShowHideText(self):
335 self.textShowHide(not self._text_shown)
336
337 def _onAddParticipant(self):
338 self._observer.onAddParticipant()
339
340 def _onStartStopAudio(self):
341 self._audioEnabled = not self._audioEnabled
342 if self._audioEnabled:
343 self._observer.onStartAudio()
344 else:
345 self._observer.onStopAudio()
346 self.enableAudio(self._audioEnabled)
347
348 def _onClose(self):
349 self._observer.onCloseWindow()
350
351 # APIs
352
353 def bringToFront(self):
354 self.deiconify()
355 self.lift()
356 self._text._typingBox.focus_set()
357
358 def textAddMessage(self, msg, is_chat = True):
359 self._text.addMessage(msg, is_chat)
360
361 def textSetTypingIndication(self, who, is_typing = True):
362 self._text.setTypingIndication(who, is_typing)
363
364 def addParticipant(self, participant_uri):
365 aud_frm = AudioFrame(self.media_right, participant_uri, self._observer)
366 self._audioFrames.append(aud_frm)
367
368 def delParticipant(self, participant_uri):
369 for aud_frm in self._audioFrames:
370 if participant_uri == aud_frm.peerUri:
371 self._audioFrames.remove(aud_frm)
372 # need to delete aud_frm manually?
373 aud_frm.destroy()
374 return
375
376 def textShowHide(self, show = True):
377 if show:
378 self.media_left.pack(side=tk.LEFT, fill=tk.BOTH, expand=1)
379 self._text._typingBox.focus_set()
380 else:
381 self.media_left.pack_forget()
382 self._text_shown = show
383
384 def enableAudio(self, is_enabled = True):
385 if is_enabled:
386 self._arrangeMediaFrames()
387 else:
388 self.media_right.pack_forget()
389 self._audioEnabled = is_enabled
390
391 def audioUpdateState(self, participant_uri, state):
392 for aud_frm in self._audioFrames:
393 if participant_uri == aud_frm.peerUri:
394 aud_frm.updateState(state)
395 break
396 if state >= AudioState.DISCONNECTED and len(self._audioFrames) == 1:
397 self.enableAudio(False)
398 else:
399 self.enableAudio(True)
400
401 def audioSetStatsText(self, participant_uri, stats_str):
402 for aud_frm in self._audioFrames:
403 if participant_uri == aud_frm.peerUri:
404 aud_frm.setStatsText(stats_str)
405 break
406
407 if __name__ == '__main__':
408 root = tk.Tk()
409 root.title("Chat")
410 root.columnconfigure(0, weight=1)
411 root.rowconfigure(0, weight=1)
412
413 obs = ChatObserver()
414 dlg = ChatFrame(obs)
415 #dlg = TextFrame(root)
416 #dlg = AudioFrame(root)
417
418 #dlg.pack(fill=tk.BOTH, expand=1)
419 root.mainloop()