3 from __future__ import with_statement
4 from __future__ import division
9 QtCore = qt_compat.QtCore
10 QtGui = qt_compat.import_module("QtGui")
12 from util import qui_utils
13 from util import misc as misc_utils
16 _moduleLogger = logging.getLogger(__name__)
19 class ApplicationWrapper(object):
21 DEFAULT_ORIENTATION = "Default"
22 AUTO_ORIENTATION = "Auto"
23 LANDSCAPE_ORIENTATION = "Landscape"
24 PORTRAIT_ORIENTATION = "Portrait"
26 def __init__(self, qapp, constants):
27 self._constants = constants
29 self._clipboard = QtGui.QApplication.clipboard()
31 self._errorLog = qui_utils.QErrorLog()
32 self._mainWindow = None
34 self._fullscreenAction = QtGui.QAction(None)
35 self._fullscreenAction.setText("Fullscreen")
36 self._fullscreenAction.setCheckable(True)
37 self._fullscreenAction.setShortcut(QtGui.QKeySequence("CTRL+Enter"))
38 self._fullscreenAction.toggled.connect(self._on_toggle_fullscreen)
40 self._orientation = self.DEFAULT_ORIENTATION
41 self._orientationAction = QtGui.QAction(None)
42 self._orientationAction.setText("Next Orientation")
43 self._orientationAction.setCheckable(True)
44 self._orientationAction.setShortcut(QtGui.QKeySequence("CTRL+o"))
45 self._orientationAction.triggered.connect(self._on_next_orientation)
47 self._logAction = QtGui.QAction(None)
48 self._logAction.setText("Log")
49 self._logAction.setShortcut(QtGui.QKeySequence("CTRL+l"))
50 self._logAction.triggered.connect(self._on_log)
52 self._quitAction = QtGui.QAction(None)
53 self._quitAction.setText("Quit")
54 self._quitAction.setShortcut(QtGui.QKeySequence("CTRL+q"))
55 self._quitAction.triggered.connect(self._on_quit)
57 self._aboutAction = QtGui.QAction(None)
58 self._aboutAction.setText("About")
59 self._aboutAction.triggered.connect(self._on_about)
61 self._qapp.lastWindowClosed.connect(self._on_app_quit)
62 self._mainWindow = self._new_main_window()
63 self._mainWindow.window.destroyed.connect(self._on_child_close)
67 self._mainWindow.show()
68 self._idleDelay = QtCore.QTimer()
69 self._idleDelay.setSingleShot(True)
70 self._idleDelay.setInterval(0)
71 self._idleDelay.timeout.connect(self._on_delayed_start)
72 self._idleDelay.start()
74 def load_settings(self):
75 raise NotImplementedError("Booh")
77 def save_settings(self):
78 raise NotImplementedError("Booh")
80 def _new_main_window(self):
81 raise NotImplementedError("Booh")
89 return self._constants
96 def fullscreenAction(self):
97 return self._fullscreenAction
100 def orientationAction(self):
101 return self._orientationAction
104 def orientation(self):
105 return self._orientation
109 return self._logAction
112 def aboutAction(self):
113 return self._aboutAction
116 def quitAction(self):
117 return self._quitAction
120 def _next_orientation(cls, current):
122 cls.DEFAULT_ORIENTATION: cls.AUTO_ORIENTATION,
123 cls.AUTO_ORIENTATION: cls.LANDSCAPE_ORIENTATION,
124 cls.LANDSCAPE_ORIENTATION: cls.PORTRAIT_ORIENTATION,
125 cls.PORTRAIT_ORIENTATION: cls.DEFAULT_ORIENTATION,
128 def _close_windows(self):
129 if self._mainWindow is not None:
131 self._mainWindow.window.destroyed.disconnect(self._on_child_close)
132 self._mainWindow.close()
133 self._mainWindow = None
135 @misc_utils.log_exception(_moduleLogger)
136 def _on_delayed_start(self):
137 self._mainWindow.start()
139 @misc_utils.log_exception(_moduleLogger)
140 def _on_app_quit(self, checked = False):
141 if self._mainWindow is not None:
143 self._mainWindow.destroy()
145 @misc_utils.log_exception(_moduleLogger)
146 def _on_child_close(self, obj = None):
147 if self._mainWindow is not None:
149 self._mainWindow = None
151 @misc_utils.log_exception(_moduleLogger)
152 def _on_toggle_fullscreen(self, checked = False):
153 with qui_utils.notify_error(self._errorLog):
154 self._mainWindow.set_fullscreen(checked)
156 @misc_utils.log_exception(_moduleLogger)
157 def _on_next_orientation(self, checked = False):
158 with qui_utils.notify_error(self._errorLog):
159 self._orientation = self._next_orientation(self._orientation)
160 self._mainWindow.update_orientation(self._orientation)
162 @misc_utils.log_exception(_moduleLogger)
163 def _on_about(self, checked = True):
164 raise NotImplementedError("Booh")
166 @misc_utils.log_exception(_moduleLogger)
167 def _on_log(self, checked = False):
168 with qui_utils.notify_error(self._errorLog):
169 with open(self._constants._user_logpath_, "r") as f:
170 logLines = f.xreadlines()
171 log = "".join(logLines)
172 self._clipboard.setText(log)
174 @misc_utils.log_exception(_moduleLogger)
175 def _on_quit(self, checked = False):
176 with qui_utils.notify_error(self._errorLog):
177 self._close_windows()
180 class WindowWrapper(object):
182 def __init__(self, parent, app):
185 self._errorDisplay = qui_utils.ErrorDisplay(self._app.errorLog)
187 self._layout = QtGui.QBoxLayout(QtGui.QBoxLayout.LeftToRight)
188 self._layout.setContentsMargins(0, 0, 0, 0)
190 self._superLayout = QtGui.QVBoxLayout()
191 self._superLayout.addWidget(self._errorDisplay.toplevel)
192 self._superLayout.setContentsMargins(0, 0, 0, 0)
193 self._superLayout.addLayout(self._layout)
195 centralWidget = QtGui.QWidget()
196 centralWidget.setLayout(self._superLayout)
197 centralWidget.setContentsMargins(0, 0, 0, 0)
199 self._window = qui_utils.QSignalingMainWindow(parent)
200 self._window.setAttribute(QtCore.Qt.WA_DeleteOnClose, True)
201 qui_utils.set_stackable(self._window, True)
202 self._window.setCentralWidget(centralWidget)
204 self._closeWindowAction = QtGui.QAction(None)
205 self._closeWindowAction.setText("Close")
206 self._closeWindowAction.setShortcut(QtGui.QKeySequence("CTRL+w"))
207 self._closeWindowAction.triggered.connect(self._on_close_window)
209 self._window.addAction(self._closeWindowAction)
210 self._window.addAction(self._app.quitAction)
211 self._window.addAction(self._app.fullscreenAction)
212 self._window.addAction(self._app.orientationAction)
213 self._window.addAction(self._app.logAction)
220 def windowOrientation(self):
221 geom = self._window.size()
222 if geom.width() <= geom.height():
223 return QtCore.Qt.Vertical
225 return QtCore.Qt.Horizontal
228 def idealWindowOrientation(self):
229 if self._app.orientation == self._app.LANDSCAPE_ORIENTATION:
230 windowOrientation = QtCore.Qt.Horizontal
231 elif self._app.orientation == self._app.PORTRAIT_ORIENTATION:
232 windowOrientation = QtCore.Qt.Vertical
234 windowOrientation = self.windowOrientation
235 return windowOrientation
237 def walk_children(self):
244 for child in self.walk_children():
245 child.window.destroyed.disconnect(self._on_child_close)
254 for child in self.walk_children():
256 self.set_fullscreen(self._app.fullscreenAction.isChecked())
259 for child in self.walk_children():
263 def set_fullscreen(self, isFullscreen):
264 if self._window.isVisible():
266 self._window.showFullScreen()
268 self._window.showNormal()
269 for child in self.walk_children():
270 child.set_fullscreen(isFullscreen)
272 def update_orientation(self, orientation):
273 if orientation == self._app.DEFAULT_ORIENTATION:
274 qui_utils.set_autorient(self.window, False)
275 qui_utils.set_window_orientation(self.window, None)
276 elif orientation == self._app.AUTO_ORIENTATION:
277 qui_utils.set_autorient(self.window, True)
278 qui_utils.set_window_orientation(self.window, None)
279 elif orientation == self._app.LANDSCAPE_ORIENTATION:
280 qui_utils.set_autorient(self.window, False)
281 qui_utils.set_window_orientation(self.window, QtCore.Qt.Horizontal)
282 elif orientation == self._app.PORTRAIT_ORIENTATION:
283 qui_utils.set_autorient(self.window, False)
284 qui_utils.set_window_orientation(self.window, QtCore.Qt.Vertical)
286 raise RuntimeError("Unknown orientation: %r" % orientation)
287 for child in self.walk_children():
288 child.update_orientation(orientation)
290 @misc_utils.log_exception(_moduleLogger)
291 def _on_child_close(self, obj = None):
292 raise NotImplementedError("Booh")
294 @misc_utils.log_exception(_moduleLogger)
295 def _on_close_window(self, checked = True):
296 with qui_utils.notify_error(self._errorLog):
300 class AutoFreezeWindowFeature(object):
302 def __init__(self, app, window):
304 self._window = window
305 self._app.qapp.focusChanged.connect(self._on_focus_changed)
306 if self._app.qapp.focusWidget() is not None:
307 self._window.setUpdatesEnabled(True)
309 self._window.setUpdatesEnabled(False)
312 self._app.qapp.focusChanged.disconnect(self._on_focus_changed)
313 self._window.setUpdatesEnabled(True)
315 @misc_utils.log_exception(_moduleLogger)
316 def _on_focus_changed(self, oldWindow, newWindow):
317 with qui_utils.notify_error(self._app.errorLog):
318 if oldWindow is None and newWindow is not None:
319 self._window.setUpdatesEnabled(True)
320 elif oldWindow is not None and newWindow is None:
321 self._window.setUpdatesEnabled(False)