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
119 def set_orientation(self, orientation):
120 self._orientation = orientation
121 self._mainWindow.update_orientation(self._orientation)
124 def _next_orientation(cls, current):
126 cls.DEFAULT_ORIENTATION: cls.AUTO_ORIENTATION,
127 cls.AUTO_ORIENTATION: cls.LANDSCAPE_ORIENTATION,
128 cls.LANDSCAPE_ORIENTATION: cls.PORTRAIT_ORIENTATION,
129 cls.PORTRAIT_ORIENTATION: cls.DEFAULT_ORIENTATION,
132 def _close_windows(self):
133 if self._mainWindow is not None:
135 self._mainWindow.window.destroyed.disconnect(self._on_child_close)
136 self._mainWindow.close()
137 self._mainWindow = None
139 @misc_utils.log_exception(_moduleLogger)
140 def _on_delayed_start(self):
141 self._mainWindow.start()
143 @misc_utils.log_exception(_moduleLogger)
144 def _on_app_quit(self, checked = False):
145 if self._mainWindow is not None:
147 self._mainWindow.destroy()
149 @misc_utils.log_exception(_moduleLogger)
150 def _on_child_close(self, obj = None):
151 if self._mainWindow is not None:
153 self._mainWindow = None
155 @misc_utils.log_exception(_moduleLogger)
156 def _on_toggle_fullscreen(self, checked = False):
157 with qui_utils.notify_error(self._errorLog):
158 self._mainWindow.set_fullscreen(checked)
160 @misc_utils.log_exception(_moduleLogger)
161 def _on_next_orientation(self, checked = False):
162 with qui_utils.notify_error(self._errorLog):
163 self.set_orientation(self._next_orientation(self._orientation))
165 @misc_utils.log_exception(_moduleLogger)
166 def _on_about(self, checked = True):
167 raise NotImplementedError("Booh")
169 @misc_utils.log_exception(_moduleLogger)
170 def _on_log(self, checked = False):
171 with qui_utils.notify_error(self._errorLog):
172 with open(self._constants._user_logpath_, "r") as f:
173 logLines = f.xreadlines()
174 log = "".join(logLines)
175 self._clipboard.setText(log)
177 @misc_utils.log_exception(_moduleLogger)
178 def _on_quit(self, checked = False):
179 with qui_utils.notify_error(self._errorLog):
180 self._close_windows()
183 class WindowWrapper(object):
185 def __init__(self, parent, app):
188 self._errorDisplay = qui_utils.ErrorDisplay(self._app.errorLog)
190 self._layout = QtGui.QBoxLayout(QtGui.QBoxLayout.LeftToRight)
191 self._layout.setContentsMargins(0, 0, 0, 0)
193 self._superLayout = QtGui.QVBoxLayout()
194 self._superLayout.addWidget(self._errorDisplay.toplevel)
195 self._superLayout.setContentsMargins(0, 0, 0, 0)
196 self._superLayout.addLayout(self._layout)
198 centralWidget = QtGui.QWidget()
199 centralWidget.setLayout(self._superLayout)
200 centralWidget.setContentsMargins(0, 0, 0, 0)
202 self._window = qui_utils.QSignalingMainWindow(parent)
203 self._window.setAttribute(QtCore.Qt.WA_DeleteOnClose, True)
204 qui_utils.set_stackable(self._window, True)
205 self._window.setCentralWidget(centralWidget)
207 self._closeWindowAction = QtGui.QAction(None)
208 self._closeWindowAction.setText("Close")
209 self._closeWindowAction.setShortcut(QtGui.QKeySequence("CTRL+w"))
210 self._closeWindowAction.triggered.connect(self._on_close_window)
212 self._window.addAction(self._closeWindowAction)
213 self._window.addAction(self._app.quitAction)
214 self._window.addAction(self._app.fullscreenAction)
215 self._window.addAction(self._app.orientationAction)
216 self._window.addAction(self._app.logAction)
223 def windowOrientation(self):
224 geom = self._window.size()
225 if geom.width() <= geom.height():
226 return QtCore.Qt.Vertical
228 return QtCore.Qt.Horizontal
231 def idealWindowOrientation(self):
232 if self._app.orientation == self._app.AUTO_ORIENTATION:
233 windowOrientation = self.windowOrientation
234 elif self._app.orientation == self._app.DEFAULT_ORIENTATION:
235 windowOrientation = qui_utils.screen_orientation()
236 elif self._app.orientation == self._app.LANDSCAPE_ORIENTATION:
237 windowOrientation = QtCore.Qt.Horizontal
238 elif self._app.orientation == self._app.PORTRAIT_ORIENTATION:
239 windowOrientation = QtCore.Qt.Vertical
241 raise RuntimeError("Bad! No %r for you" % self._app.orientation)
242 return windowOrientation
244 def walk_children(self):
251 for child in self.walk_children():
252 child.window.destroyed.disconnect(self._on_child_close)
261 for child in self.walk_children():
263 self.set_fullscreen(self._app.fullscreenAction.isChecked())
266 for child in self.walk_children():
270 def set_fullscreen(self, isFullscreen):
271 if self._window.isVisible():
273 self._window.showFullScreen()
275 self._window.showNormal()
276 for child in self.walk_children():
277 child.set_fullscreen(isFullscreen)
279 def update_orientation(self, orientation):
280 if orientation == self._app.DEFAULT_ORIENTATION:
281 qui_utils.set_autorient(self.window, False)
282 qui_utils.set_window_orientation(self.window, None)
283 elif orientation == self._app.AUTO_ORIENTATION:
284 qui_utils.set_autorient(self.window, True)
285 qui_utils.set_window_orientation(self.window, None)
286 elif orientation == self._app.LANDSCAPE_ORIENTATION:
287 qui_utils.set_autorient(self.window, False)
288 qui_utils.set_window_orientation(self.window, QtCore.Qt.Horizontal)
289 elif orientation == self._app.PORTRAIT_ORIENTATION:
290 qui_utils.set_autorient(self.window, False)
291 qui_utils.set_window_orientation(self.window, QtCore.Qt.Vertical)
293 raise RuntimeError("Unknown orientation: %r" % orientation)
294 for child in self.walk_children():
295 child.update_orientation(orientation)
297 @misc_utils.log_exception(_moduleLogger)
298 def _on_child_close(self, obj = None):
299 raise NotImplementedError("Booh")
301 @misc_utils.log_exception(_moduleLogger)
302 def _on_close_window(self, checked = True):
303 with qui_utils.notify_error(self._errorLog):
307 class AutoFreezeWindowFeature(object):
309 def __init__(self, app, window):
311 self._window = window
312 self._app.qapp.focusChanged.connect(self._on_focus_changed)
313 if self._app.qapp.focusWidget() is not None:
314 self._window.setUpdatesEnabled(True)
316 self._window.setUpdatesEnabled(False)
319 self._app.qapp.focusChanged.disconnect(self._on_focus_changed)
320 self._window.setUpdatesEnabled(True)
322 @misc_utils.log_exception(_moduleLogger)
323 def _on_focus_changed(self, oldWindow, newWindow):
324 with qui_utils.notify_error(self._app.errorLog):
325 if oldWindow is None and newWindow is not None:
326 self._window.setUpdatesEnabled(True)
327 elif oldWindow is not None and newWindow is None:
328 self._window.setUpdatesEnabled(False)