3 from __future__ import with_statement
4 from __future__ import division
8 from PyQt4 import QtGui
9 from PyQt4 import QtCore
11 from util import qui_utils
12 from util import misc as misc_utils
15 _moduleLogger = logging.getLogger(__name__)
18 class ApplicationWrapper(object):
20 def __init__(self, qapp, constants):
21 self._constants = constants
23 self._clipboard = QtGui.QApplication.clipboard()
25 self._errorLog = qui_utils.QErrorLog()
26 self._mainWindow = None
28 self._fullscreenAction = QtGui.QAction(None)
29 self._fullscreenAction.setText("Fullscreen")
30 self._fullscreenAction.setCheckable(True)
31 self._fullscreenAction.setShortcut(QtGui.QKeySequence("CTRL+Enter"))
32 self._fullscreenAction.toggled.connect(self._on_toggle_fullscreen)
34 self._orientationAction = QtGui.QAction(None)
35 self._orientationAction.setText("Orientation")
36 self._orientationAction.setCheckable(True)
37 self._orientationAction.setShortcut(QtGui.QKeySequence("CTRL+o"))
38 self._orientationAction.toggled.connect(self._on_toggle_orientation)
40 self._logAction = QtGui.QAction(None)
41 self._logAction.setText("Log")
42 self._logAction.setShortcut(QtGui.QKeySequence("CTRL+l"))
43 self._logAction.triggered.connect(self._on_log)
45 self._quitAction = QtGui.QAction(None)
46 self._quitAction.setText("Quit")
47 self._quitAction.setShortcut(QtGui.QKeySequence("CTRL+q"))
48 self._quitAction.triggered.connect(self._on_quit)
50 self._aboutAction = QtGui.QAction(None)
51 self._aboutAction.setText("About")
52 self._aboutAction.triggered.connect(self._on_about)
54 self._qapp.lastWindowClosed.connect(self._on_app_quit)
55 self._mainWindow = self._new_main_window()
56 self._mainWindow.window.destroyed.connect(self._on_child_close)
60 self._mainWindow.show()
61 self._idleDelay = QtCore.QTimer()
62 self._idleDelay.setSingleShot(True)
63 self._idleDelay.setInterval(0)
64 self._idleDelay.timeout.connect(lambda: self._mainWindow.start())
65 self._idleDelay.start()
67 def load_settings(self):
68 raise NotImplementedError("Booh")
70 def save_settings(self):
71 raise NotImplementedError("Booh")
73 def _new_main_window(self):
74 raise NotImplementedError("Booh")
82 return self._constants
89 def fullscreenAction(self):
90 return self._fullscreenAction
93 def orientationAction(self):
94 return self._orientationAction
98 return self._logAction
101 def aboutAction(self):
102 return self._aboutAction
105 def quitAction(self):
106 return self._quitAction
108 def _close_windows(self):
109 if self._mainWindow is not None:
111 self._mainWindow.window.destroyed.disconnect(self._on_child_close)
112 self._mainWindow.close()
113 self._mainWindow = None
115 @misc_utils.log_exception(_moduleLogger)
116 def _on_app_quit(self, checked = False):
117 if self._mainWindow is not None:
119 self._mainWindow.destroy()
121 @misc_utils.log_exception(_moduleLogger)
122 def _on_child_close(self, obj = None):
123 if self._mainWindow is not None:
125 self._mainWindow = None
127 @misc_utils.log_exception(_moduleLogger)
128 def _on_toggle_fullscreen(self, checked = False):
129 with qui_utils.notify_error(self._errorLog):
130 self._mainWindow.set_fullscreen(checked)
132 @misc_utils.log_exception(_moduleLogger)
133 def _on_toggle_orientation(self, checked = False):
134 with qui_utils.notify_error(self._errorLog):
135 self._mainWindow.set_orientation(checked)
137 @misc_utils.log_exception(_moduleLogger)
138 def _on_about(self, checked = True):
139 raise NotImplementedError("Booh")
141 @misc_utils.log_exception(_moduleLogger)
142 def _on_log(self, checked = False):
143 with qui_utils.notify_error(self._errorLog):
144 with open(self._constants._user_logpath_, "r") as f:
145 logLines = f.xreadlines()
146 log = "".join(logLines)
147 self._clipboard.setText(log)
149 @misc_utils.log_exception(_moduleLogger)
150 def _on_quit(self, checked = False):
151 with qui_utils.notify_error(self._errorLog):
152 self._close_windows()
155 class WindowWrapper(object):
157 def __init__(self, parent, app):
160 self._errorDisplay = qui_utils.ErrorDisplay(self._app.errorLog)
162 self._layout = QtGui.QBoxLayout(QtGui.QBoxLayout.LeftToRight)
163 self._layout.setContentsMargins(0, 0, 0, 0)
165 self._superLayout = QtGui.QVBoxLayout()
166 self._superLayout.addWidget(self._errorDisplay.toplevel)
167 self._superLayout.setContentsMargins(0, 0, 0, 0)
168 self._superLayout.addLayout(self._layout)
170 centralWidget = QtGui.QWidget()
171 centralWidget.setLayout(self._superLayout)
172 centralWidget.setContentsMargins(0, 0, 0, 0)
174 self._window = QtGui.QMainWindow(parent)
175 self._window.setAttribute(QtCore.Qt.WA_DeleteOnClose, True)
176 qui_utils.set_stackable(self._window, True)
177 self._window.setCentralWidget(centralWidget)
179 self._closeWindowAction = QtGui.QAction(None)
180 self._closeWindowAction.setText("Close")
181 self._closeWindowAction.setShortcut(QtGui.QKeySequence("CTRL+w"))
182 self._closeWindowAction.triggered.connect(self._on_close_window)
184 self._window.addAction(self._closeWindowAction)
185 self._window.addAction(self._app.quitAction)
186 self._window.addAction(self._app.fullscreenAction)
187 self._window.addAction(self._app.orientationAction)
188 self._window.addAction(self._app.logAction)
194 def walk_children(self):
201 for child in self.walk_children():
202 child.window.destroyed.disconnect(self._on_child_close)
210 self.set_fullscreen(self._app.fullscreenAction.isChecked())
212 for child in self.walk_children():
216 for child in self.walk_children():
220 def set_fullscreen(self, isFullscreen):
222 self._window.showFullScreen()
224 self._window.showNormal()
225 for child in self.walk_children():
226 child.set_fullscreen(isFullscreen)
228 def set_orientation(self, isPortrait):
230 qui_utils.set_window_orientation(self.window, QtCore.Qt.Vertical)
232 qui_utils.set_window_orientation(self.window, QtCore.Qt.Horizontal)
233 for child in self.walk_children():
234 child.set_orientation(isPortrait)
236 @misc_utils.log_exception(_moduleLogger)
237 def _on_child_close(self):
238 raise NotImplementedError("Booh")
240 @misc_utils.log_exception(_moduleLogger)
241 def _on_close_window(self, checked = True):
242 with qui_utils.notify_error(self._errorLog):
246 class AutoFreezeWindowFeature(object):
248 def __init__(self, app, window):
250 self._window = window
251 self._app.qapp.focusChanged.connect(self._on_focus_changed)
252 if self._app.qapp.focusWidget() is not None:
253 self._window.setUpdatesEnabled(True)
255 self._window.setUpdatesEnabled(False)
258 self._app.qapp.focusChanged.disconnect(self._on_focus_changed)
259 self._window.setUpdatesEnabled(True)
261 @misc_utils.log_exception(_moduleLogger)
262 def _on_focus_changed(self, oldWindow, newWindow):
263 with qui_utils.notify_error(self._app.errorLog):
264 if oldWindow is None and newWindow is not None:
265 self._window.setUpdatesEnabled(True)
266 elif oldWindow is not None and newWindow is None:
267 self._window.setUpdatesEnabled(False)