forked from mxcube/BlissFramework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Qt4_GUISupervisor.py
436 lines (359 loc) · 18.2 KB
/
Qt4_GUISupervisor.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
#
# Project: MXCuBE
# https://github.com/mxcube.
#
# This file is part of MXCuBE software.
#
# MXCuBE is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# MXCuBE is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with MXCuBE. If not, see <http://www.gnu.org/licenses/>.
import os
import time
import stat
import sys
import cPickle
import logging
from PyQt4 import QtCore
from PyQt4 import QtGui
from HardwareRepository import HardwareRepository
from BlissFramework import Qt4_Configuration
from BlissFramework import Qt4_GUIBuilder
from BlissFramework.Utils import Qt4_GUIDisplay
from BlissFramework.Utils import PropertyBag
from BlissFramework.Qt4_BaseComponents import BlissWidget
import Qt4_Icons
import BlissFramework
LOAD_GUI_EVENT = QtCore.QEvent.MaxUser
class BlissSplashScreen(QtGui.QSplashScreen):
def __init__(self, pixmap):
QtGui.QSplashScreen.__init__(self, pixmap)
self.guiName = ' '
self.repaint()
def mousePressEvent(self,e):
e.accept()
def setGUIName(self, name):
self.guiName = str(name)
if len(self.guiName) == 0:
self.guiName = ' '
self.repaint()
def drawContents(self, painter):
x0 = 10
x1 = 390
y0 = 340
y1 = 340 + painter.fontMetrics().height()
pxsize = 14
painter.font().setPixelSize(pxsize)
painter.setPen(QtGui.QPen(QtCore.Qt.black))
painter.drawText(QtCore.QRect(QtCore.QPoint(x0, y0),
QtCore.QPoint(x1, y1)),
QtCore.Qt.AlignLeft | QtCore.Qt.AlignTop,
"Loading MXCuBE")
painter.font().setPixelSize(pxsize*2.5)
painter.font().setPixelSize(pxsize)
y0 = y1
y1 += 3 + painter.fontMetrics().height()
painter.drawText(QtCore.QRect(QtCore.QPoint(x0, y0),
QtCore.QPoint(x1, y1)),
QtCore.Qt.AlignLeft | QtCore.Qt.AlignBottom,
"Please wait...")
class GUISupervisor(QtGui.QWidget):
def __init__(self, designMode=False, showMaximized=False, noBorder=False):
QtGui.QWidget.__init__(self)
self.launchInDesignMode = designMode
self.hardwareRepository = HardwareRepository.HardwareRepository()
self.showMaximized = showMaximized
self.noBorder = noBorder
self.windows = []
#self.splashScreen = BlissSplashScreen(Qt4_Icons.load('splash'), QtCore.Qt.WA_DeleteOnClose)
self.splashScreen = BlissSplashScreen(Qt4_Icons.load('splash'))
self.splashScreen.show()
self.timestamp = 0
def load_gui(self, gui_config_file):
self.configuration = Qt4_Configuration.Configuration()
self.gui_config_file = gui_config_file
if self.gui_config_file:
if hasattr(self, "splashScreen"):
self.splashScreen.setGUIName(os.path.splitext(os.path.basename(gui_config_file))[0])
if os.path.exists(gui_config_file):
filestat = os.stat(gui_config_file)
self.timestamp = filestat[stat.ST_MTIME]
if filestat[stat.ST_SIZE] == 0:
# empty file
return self.newGUI()
#
# open existing file
#
try:
f = open(gui_config_file)
except:
logging.getLogger().exception("Cannot open file %s", gui_config_file)
QtGui.QMessageBox.warning(self, "Error", "Could not open file %s !" % gui_config_file, QtGui.QMessageBox.Ok)
else:
#
# find mnemonics to speed up loading
# (using the 'require' feature from Hardware Repository)
#
def getMnemonics(items_list):
mne_list = []
for item in items_list:
if "brick" in item:
try:
props = cPickle.loads(item["properties"])
except:
logging.getLogger().exception("could not load properties for %s", item["name"])
else:
item["properties"]=props
"""
try:
mne_list.append(props["mnemonic"])
except:
pass
"""
try:
for prop in props:
prop_value = prop.getValue()
if type(prop_value)==type('') and prop_value.startswith("/"):
mne_list.append(prop_value)
except:
logging.exception("could not build list of required hardware objects")
continue
mne_list += getMnemonics(item["children"])
return mne_list
raw_config = eval(f.read())
mnemonics = getMnemonics(raw_config)
self.hardwareRepository.require(mnemonics)
f.close()
try:
config = Qt4_Configuration.Configuration(raw_config)
except:
logging.getLogger().exception("Cannot read configuration from file %s", gui_config_file)
QtGui.QMessageBox.warning(self, "Error", "Could not read configuration\nfrom file %s" % gui_config_file, QtGui.QMessageBox.Ok)
else:
self.configuration = config
if len(self.configuration.windows) == 0:
self.launchInDesignMode = True
if self.launchInDesignMode:
self.framework = Qt4_GUIBuilder.GUIBuilder()
QtGui.QApplication.setActiveWindow(self.framework)
self.framework.filename = gui_config_file
self.framework.configuration = config
self.framework.setWindowTitle("GUI Builder - %s" % gui_config_file)
self.framework.gui_editor_window.set_configuration(config)
self.framework.gui_editor_window.draw_window_preview()
self.framework.show()
return self.framework
else:
main_window = self.execute(self.configuration)
return main_window
return self.newGUI()
def newGUI(self):
#
# new GUI
#
self.timestamp = 0
self.launchInDesignMode = True
self.framework = Qt4_GUIBuilder.GUIBuilder()
#tGui.QApplication.setMainWidget(self.framework)
QtGui.QApplication.setActiveWindow(self.framework)
self.framework.show()
self.framework.new_clicked(self.gui_config_file)
return self.framework
def reloadGUI(self):
msg = "Are you sure you want to reload the GUI ?\n" +\
"This will stop the current application and restart it."
if (QtGui.QMessageBox.question(self, "Reload GUI", msg,
QtGui.QMessageBox.Yes, QtGui.QMessageBox.No,
QtGui.QMessageBox.Cancel) == QtGui.QMessageBox.Yes):
self.finalize()
win0 = self.windows[0]
for window in self.windows[1:]:
window.close(True)
self.windows=[]
# for first window, delete all children one by one
# (instead of deleting the window itself : indeed it's causing
# a stop of the whole app. because of "last window closed" signal)
for child in win0.children():
if hasattr(child, "close"):
child.close(True)
self.execute(self.configuration)
# now let's destroy the window !
win0.close(True)
def execute(self, config):
#
# start in execution mode
#
self.windows = Qt4_GUIDisplay.display(config, noBorder=self.noBorder)
main_window = None
if len(self.windows) > 0:
main_window = self.windows[0]
main_window.configuration = config
QtGui.QApplication.setActiveWindow(main_window)
if self.noBorder:
main_window.move(0, 0)
w = QtGui.QApplication.desktop().width()
h = QtGui.QApplicaitoj.desktop().height()
main_window.resize(QtCore.QSize(w,h))
#
# make connections
#
widgets_dict = dict([(callable(w.objectName) and str(w.objectName()) or None, w) for w in QtGui.QApplication.allWidgets()])
def make_connections(items_list):
for item in items_list:
try:
sender = widgets_dict[item["name"]]
except KeyError:
logging.getLogger().error("Could not find receiver widget %s", item["name"])
else:
for connection in item["connections"]:
_receiver = connection["receiver"] or connection["receiverWindow"]
try:
receiver = widgets_dict[_receiver]
except KeyError:
logging.getLogger().error("Could not find receiver widget %s", _receiver)
else:
try:
slot = getattr(receiver, connection["slot"])
except AttributeError:
logging.getLogger().error("No slot '%s' in receiver %s", connection["slot"], _receiver)
else:
sender.connect(sender, QtCore.SIGNAL(connection["signal"]), slot)
make_connections(item["children"])
make_connections(config.windows_list)
#
# set run mode for every brick
#
BlissWidget.setRunMode(True)
if self.showMaximized:
main_window.showMaximized()
else:
main_window.show()
for window in self.windows:
"""window._fontSizeMinusKey = QtGui.QShortcut(window)
window._fontSizeMinusKey.setKey(QtCore.Qt.CTRL + QtCore.Qt.Key_Minus)
window._fontSizePlusKey = QtGui.QShortcut(window)
window._fontSizePlusKey.setKey(QtCore.Qt.CTRL + QtCore.Qt.Key_Plus)
window._fontSizeAsteriskKey = QtGui.QShortcut(window)
window._fontSizeAsteriskKey.setKey(QtCore.Qt.CTRL + QtCore.Qt.Key_Asterisk)
window._whatsThisKey = QtGui.QShortcut(window)
window._whatsThisKey.setKey(QtCore.Qt.Key_F1)
window._reloadKey = QtGui.QShortcut(window)
window._reloadKey.setKey(QtCore.Qt.CTRL + QtCore.Qt.SHIFT + QtCore.Qt.Key_F5)
window._splitterPositionAccel = QtGui.QShortcut(window)
for key in [QtCore.Qt.Key_F9, QtCore.Qt.Key_F10, QtCore.Qt.Key_F11, QtCore.Qt.Key_F12] :
window._splitterPositionAccel.setKey(QtCore.Qt.SHIFT+key)
window._splitterPositionAccel.setKey(QtCore.Qt.CTRL+key)
window._splitterPositionAccel.setKey(key)
QtCore.QObject.connect(window._splitterPositionAccel, QtCore.SIGNAL('activated()'),self.saveOrReloadSize)
QtCore.QObject.connect(window._fontSizeMinusKey, QtCore.SIGNAL('activated()'), self.changeFontSize)
QtCore.QObject.connect(window._whatsThisAccel, QtCore.SIGNAL('activated()'), BlissWidget.updateWhatsThis)
QtCore.QObject.connect(window._reloadAccel, QtCore.SIGNAL('activated()'), self.reloadGUI)"""
if window._show:
window.show()
if BlissWidget._menuBar:
BlissWidget._menuBar.set_exp_mode(False)
return main_window
def saveOrReloadSize(self,key) :
if key & (QtCore.Qt.SHIFT|QtCore.Qt.CTRL) :
key &= ~(QtCore.Qt.SHIFT|QtCore.Qt.CTRL)
keyname = 'F%d' % ((key - qt.Qt.Key_F9) + 9)
self.saveSize('_%d' % key)
QtGui.QMessageBox.information(self,'Position and size',
'This configuration is saved on key %s' %
keyname,
QtGui.QMessageBox.Default)
else:
for display in self.windows:
window = self.configuration.windows[str(display.name())]
Qt4_GUIDisplay.restoreSizes(self.configuration,window,display,configurationSuffix = '_%d' % key,moveWindowFlag = False)
def changeFontSize(self, mode):
widgets_dict = dict([(callable(w.name) and w.name() or None, w) for w in qt.QApplication.allWidgets()])
def setFontSize(item):
if mode == 1:
i = -1
elif mode == 2:
i = 1
else:
i = 0
if self.configuration.isBrick(item):
if i == 0:
item.brick['fontSize'] = 9
else:
item.brick['fontSize']=int(item.brick['fontSize'])+i
else:
prop = item.properties.properties.get('fontSize', None)
if prop is not None:
if i == 0:
prop.setValue(9)
else:
prop.setValue(prop.getValue()+i)
w = widgets_dict[item.name]
f = w.font()
f.setPointSize(prop.getValue())
w.setFont(f)
[setFontSize(child) for child in sum([self.configuration.findAllChildren(window) for window in self.configuration.windows_list], [])]
def finalize(self):
BlissWidget.setRunMode(False) # call .stop() for each brick
self.hardwareRepository.close()
QtGui.QApplication.sendPostedEvents()
QtGui.QApplication.processEvents()
self.saveSize()
def saveSize(self,configurationSuffix = '') :
if not self.launchInDesignMode:
# save windows positions
for window in self.windows:
window_cfg = self.configuration.windows[str(window.objectName())]
window_cfg["properties"].getProperty("x%s" % configurationSuffix).setValue(window.x())
window_cfg["properties"].getProperty("y%s" % configurationSuffix).setValue(window.y())
window_cfg["properties"].getProperty("w%s" % configurationSuffix).setValue(window.width())
window_cfg["properties"].getProperty("h%s" % configurationSuffix).setValue(window.height())
splitters = self.configuration.findAllChildrenWType("splitter", window_cfg)
if len(splitters):
for sw in window.queryList("QSplitter"):
try:
splitter = splitters[sw.name()]
splitter["properties"].getProperty("sizes%s" % configurationSuffix).setValue(sw.sizes())
except KeyError:
continue
# save GUI file only if it is not more recent
# (to prevent overwritting file if it has been modified in the meantime)
if self.gui_config_file and os.path.exists(self.gui_config_file):
ts = os.stat(self.gui_config_file)[stat.ST_MTIME]
if ts <= self.timestamp:
if configurationSuffix == '':
logging.getLogger().debug("saving configuration file to keep windows pos. and sizes")
self.configuration.save(self.gui_config_file)
def finishInit(self, gui_config_file):
while True:
try:
self.hardwareRepository.connect()
except:
logging.getLogger().exception('Timeout while trying to connect to Hardware Repository server.')
message = """Timeout while connecting to Hardware Repository server ;
make sure the Hardware Repository Server is running on host %s.""" % str(self.hardwareRepository.serverAddress).split(':')[0]
a = QtGui.QMessageBox.warning(None, 'Cannot connect to Hardware Repository', message, QtGui.QMessageBox.Retry, QtGui.QMessageBox.Cancel, QtGui.QMessageBox.NoButton)
if a == QtGui.QMessageBox.Cancel:
logging.getLogger().warning('Gave up trying to connect to Hardware Repository server.')
break
else:
logging.getLogger().info('Connected to Hardware Repository server %s', self.hardwareRepository.serverAddress)
break
try:
main_widget = None
main_widget=self.load_gui(gui_config_file)
if main_widget:
self.splashScreen.finish(main_widget)
del self.splashScreen
except:
logging.getLogger().exception("exception while loading GUI file")
QtGui.QApplication.exit()
def customEvent(self, event):
self.finishInit(event.data)