-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.py
306 lines (245 loc) · 10.7 KB
/
controller.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import gui
import os
import presentation
import plugins
import util
import ape
import pushy
import json
import cuecumber
class Controller(QObject):
def __init__(self):
QObject.__init__(self)
self.loadDefaultSettings()
self.screens = ScreenController()
self.mainview = gui.MainWindow(self)
self.beamview = gui.BeamWindow(self)
self.prefview = gui.PreferencesWindow(self)
self.streamStarted = False
self.bcconnection = None
self.presentation = None
self.currentslide = None
# setup the main window
self.mainview.setCentralWidget(gui.LoadPresentationWidget(self))
# enable the default pointers
self.pointers = {}
self.drawingpointers = []
for pointerplugin in plugins.pointer:
pointer = pointerplugin.pointer(self)
self.pointers[pointerplugin] = pointer
if pointerplugin.enabledefault:
self.enablePointer(pointerplugin)
self.drawing = DrawingController(self.drawingpointers,self.beamview,self)
# application
def start(self):
if self.screens.countScreens() > 1:
self.screens.moveToScreen(1,self.beamview)
#self.beamview.showFullScreen()
self.beamview.show()
self.mainview.show()
def quitApplication(self):
self.beamview.close()
self.mainview.close()
for (pointermodule, pointer) in self.pointers.items():
pointer.disable()
def mainClosed(self):
question = QMessageBox.question(self.mainview, self.tr("Application Close"), self.tr("Are you sure to quit?"), QMessageBox.No, QMessageBox.Yes)
if question == QMessageBox.Yes:
self.beamview.close()
return True
else:
return False
def aboutApp(self):
QMessageBox.about(self.mainview, self.tr("emcee"),self.tr("emcee\n\nQt version: %s\nPyQt version: %s\nApplication version: %s" % (QT_VERSION_STR, PYQT_VERSION_STR, QApplication.applicationVersion())))
def setStatus(self,message):
self.mainview.status.showMessage(message)
def loadDefaultSettings(self):
settings = QSettings()
defaults = {"pointercolor": QColor(255, 0, 0),
"pointersize": 20,
"linecolor": QColor(0,0,0),
"linewidth": 10,
}
for (k,v) in defaults.items():
settings.setValue(k,settings.value(k,v))
# presentations
def newPresentation(self):
self.mainview.setCentralWidget(gui.NewPresentationWidget(self))
def closePresentation(self):
if self.presentation and self.presentation.saveneeded:
print "Todo: Ask for save"
self.mainview.showToolbar(False)
self.mainview.setCentralWidget(gui.LoadPresentationWidget(self))
def openPresentation(self):
filename = QFileDialog.getOpenFileName(self.mainview, self.tr("Open Presentation"), os.getcwd(), self.tr("emcee Presentation (*.paj)"))
if filename:
pres = presentation.Presentation(str(filename))
self.setPresentation(pres)
self.presentation.emit(SIGNAL("changed()"))
def openRemotePresentation(self):
print "NYI: Remote Open"
def savePresentation(self):
if self.presentation:
if self.presentation.filename:
self.presentation.save()
else:
self.saveAsPresentation()
def saveAsPresentation(self):
if self.presentation:
filename = QFileDialog.getSaveFileName(self.mainview, self.tr("Save Presentation"), os.getcwd() + "/" + self.presentation.suggestedFileName() + ".paj", self.tr("emcee Presentation (*.paj)"))
if filename:
self.presentation.save(str(filename))
def setPresentation(self,presentation):
self.presentation = presentation
self.currentslide = presentation.defaultSlide
self.nextslide = presentation.defaultSlide
self.mainview.setTitle(presentation.title)
self.mainview.setCentralWidget(gui.PresentationWidget(self))
self.mainview.showToolbar(True)
self.emit(SIGNAL("updateSlides()"))
def createPresentation(self,title,name,email,forclass,organization):
p = presentation.Presentation()
p.title = str(title) if len(title) else str(p.title)
p.name = str(name)
p.email = str(email)
p.forclass = str(forclass)
p.organization = str(organization)
self.setPresentation(p)
# slide control
def addSlide(self, mimetype):
plugin = plugins.mimehandlers[mimetype]
if plugin.source == "file":
filename = QFileDialog.getOpenFileName(None, self.tr("Select file"), os.getcwd(), "%s (*.%s)" % (plugin.name, plugin.filetype))
if filename:
source = presentation.Source(str(filename),mimetype)
self.presentation.addSource(source)
elif plugin.source == "internal":
source = presentation.Source(None,mimetype)
self.presentation.addSource(source)
def nextSlide(self):
if self.presentation:
self.setCurrentSlide(self.presentation.nextindex)
self.setNextSlide(self.presentation.nextindex+1)
self.emit(SIGNAL("setNextSlide(int)"),self.presentation.nextindex)
def previousSlide(self):
if self.presentation:
self.setNextSlide(self.presentation.currentindex)
self.setCurrentSlide(self.presentation.currentindex-1)
self.emit(SIGNAL("setNextSlide(int)"),self.presentation.nextindex)
def setCurrentSlide(self, index):
if index >= 0 and index < len(self.presentation.slides):
self.presentation.currentindex = index
self.currentslide = self.presentation.getSlide(index)
else:
self.currentslide = self.presentation.defaultSlide
self.emit(SIGNAL("updateSlides()"))
self.drawing.drawClear()
if self.streamStarted:
cuecumber.cuepointInject({"type": "slides/change", "identifier": self.currentslide.source.identifier(), "index": self.presentation.currentindex})
if self.bcconnection:
self.bcconnection.send({"type": "slides/change", "identifier": self.currentslide.source.identifier(), "index": self.presentation.currentindex})
def setNextSlide(self, index):
if index >= 0 and index < len(self.presentation.slides):
self.presentation.nextindex = index
self.nextslide = self.presentation.getSlide(index)
else:
self.nextslide = self.presentation.defaultSlide
self.emit(SIGNAL("updateSlides()"))
# chat
def chatSend(self,chat):
if len(self.presentation.name):
who = self.presentation.name
else:
who = "Teacher"
if self.bcconnection:
self.bcconnection.send({"type": "chat/public-msg", "msg": unicode(chat), "nickname": unicode(who)})
self.emit(SIGNAL("chatRecieved(QString)"),"<b>%s</b>: %s" % (who,chat))
else:
self.emit(SIGNAL("chatRecieved(QString)"),"<i>Not connected</i>" )
# XXX
# ape
# def apeRecieved(self,obj,var):
# jvar = json.loads(var)
# if jvar["type"] == "chat/public-msg":
# self.emit(SIGNAL("chatRecieved(QString)"),"<b>%s</b>: %s" % (jvar["nickname"],jvar["msg"]))
def pushyRecieved(self,obj,var):
print "Received: " + str(var)
# broadcast
def startBroadcast(self):
settings = QSettings()
# TODO re-instate the comet connection
# it connects but doesn't send correctly
# (it needs to nest the json as the .msg of another json string, and include the session_id)
# (we actually don't need session_ids to be sent for a raw connection)
# self.bcconnection = pushy.PushyClient('pushyl.uprimer.org', 4000, 'stream', callback=self.pushyRecieved)
# self.bcconnection.connect()
self.streamStarted = True
cuecumber.startStream()
def endBroadcast(self):
cuecumber.stopStream()
if self.bcconnection:
self.bcconnection.close()
self.bcconnection = None
# pointers
def enablePointer(self,pointerplugin):
if self.pointers[pointerplugin].enable() and pointerplugin.capabilities.count("draw"):
self.drawingpointers.append(self.pointers[pointerplugin])
def disablePointer(self,pointer):
self.pointers[pointer].disable()
class ScreenController():
def __init__(self):
self.desktop = QDesktopWidget()
self.geometry = []
primary = self.desktop.screenGeometry(self.desktop.primaryScreen()).topLeft()
for i in range(0,self.desktop.numScreens()):
self.geometry.append(self.desktop.screenGeometry(i).topLeft()-primary)
def moveToScreen(self,screen,widget):
widget.move(self.geometry[screen])
def countScreens(self):
return self.desktop.numScreens()
class DrawingController(QThread):
def __init__(self,pointers,beamview,controller):
QThread.__init__(self)
self.pointers = pointers
self.beamview = beamview
self.controller = controller
self.drawing = False
self.points = []
self.current = -1
self.start()
def drawBegin(self):
self.current += 1
self.points.append([])
self.points[self.current] = []
self.drawing = True
def drawEnd(self):
if self.drawing:
self.drawing = False
if self.controller.bcconnection:
self.controller.bcconnection.send({"type": "draw/lines", "lines": self.points})
def drawClear(self):
self.points = []
self.current = -1
self.beamview.update()
if self.controller.bcconnection:
self.controller.bcconnection.send({"type": "draw/clear"})
def run(self):
while True:
if len(self.pointers):
pointerset = False
for pointer in self.pointers:
xy = pointer.xy()
if xy:
if self.drawing:
self.points[self.current].append((xy[0],xy[1]))
self.beamview.setPointer(xy[0],xy[1])
pointerset = True
break
if not pointerset:
self.beamview.clearPointer()
self.beamview.setPaths(self.points)
self.msleep(33)
else:
self.sleep(10)