8577e634896335e1215de0692801684ec1c01f9a
[ejpi] / src / util / qwrappers.py
1 #!/usr/bin/env python
2
3 from __future__ import with_statement
4 from __future__ import division
5
6 import logging
7
8 from PyQt4 import QtGui
9 from PyQt4 import QtCore
10
11 from util import qui_utils
12 from util import misc as misc_utils
13
14
15 _moduleLogger = logging.getLogger(__name__)
16
17
18 class ApplicationWrapper(object):
19
20         def __init__(self, qapp, constants):
21                 self._constants = constants
22                 self._qapp = qapp
23                 self._clipboard = QtGui.QApplication.clipboard()
24
25                 self._errorLog = qui_utils.QErrorLog()
26                 self._mainWindow = None
27
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)
33
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)
39
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)
44
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)
49
50                 self._aboutAction = QtGui.QAction(None)
51                 self._aboutAction.setText("About")
52                 self._aboutAction.triggered.connect(self._on_about)
53
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)
57
58                 self.load_settings()
59
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()
66
67         def load_settings(self):
68                 raise NotImplementedError("Booh")
69
70         def save_settings(self):
71                 raise NotImplementedError("Booh")
72
73         def _new_main_window(self):
74                 raise NotImplementedError("Booh")
75
76         @property
77         def constants(self):
78                 return self._constants
79
80         @property
81         def errorLog(self):
82                 return self._errorLog
83
84         @property
85         def fullscreenAction(self):
86                 return self._fullscreenAction
87
88         @property
89         def orientationAction(self):
90                 return self._orientationAction
91
92         @property
93         def logAction(self):
94                 return self._logAction
95
96         @property
97         def aboutAction(self):
98                 return self._aboutAction
99
100         @property
101         def quitAction(self):
102                 return self._quitAction
103
104         def _close_windows(self):
105                 if self._mainWindow is not None:
106                         self.save_settings()
107                         self._mainWindow.window.destroyed.disconnect(self._on_child_close)
108                         self._mainWindow.close()
109                         self._mainWindow = None
110
111         @misc_utils.log_exception(_moduleLogger)
112         def _on_app_quit(self, checked = False):
113                 if self._mainWindow is not None:
114                         self.save_settings()
115                         self._mainWindow.destroy()
116
117         @misc_utils.log_exception(_moduleLogger)
118         def _on_child_close(self, obj = None):
119                 if self._mainWindow is not None:
120                         self.save_settings()
121                         self._mainWindow = None
122
123         @misc_utils.log_exception(_moduleLogger)
124         def _on_toggle_fullscreen(self, checked = False):
125                 with qui_utils.notify_error(self._errorLog):
126                         self._mainWindow.set_fullscreen(checked)
127
128         @misc_utils.log_exception(_moduleLogger)
129         def _on_toggle_orientation(self, checked = False):
130                 with qui_utils.notify_error(self._errorLog):
131                         self._mainWindow.set_orientation(checked)
132
133         @misc_utils.log_exception(_moduleLogger)
134         def _on_about(self, checked = True):
135                 raise NotImplementedError("Booh")
136
137         @misc_utils.log_exception(_moduleLogger)
138         def _on_log(self, checked = False):
139                 with qui_utils.notify_error(self._errorLog):
140                         with open(self._constants._user_logpath_, "r") as f:
141                                 logLines = f.xreadlines()
142                                 log = "".join(logLines)
143                                 self._clipboard.setText(log)
144
145         @misc_utils.log_exception(_moduleLogger)
146         def _on_quit(self, checked = False):
147                 with qui_utils.notify_error(self._errorLog):
148                         self._close_windows()
149
150
151 class WindowWrapper(object):
152
153         def __init__(self, parent, app):
154                 self._app = app
155
156                 self._errorDisplay = qui_utils.ErrorDisplay(self._app.errorLog)
157
158                 self._layout = QtGui.QBoxLayout(QtGui.QBoxLayout.LeftToRight)
159                 self._layout.setContentsMargins(0, 0, 0, 0)
160                 self._layout.addWidget(self._errorDisplay.toplevel)
161
162                 centralWidget = QtGui.QWidget()
163                 centralWidget.setLayout(self._layout)
164                 centralWidget.setContentsMargins(0, 0, 0, 0)
165
166                 self._window = QtGui.QMainWindow(parent)
167                 self._window.setAttribute(QtCore.Qt.WA_DeleteOnClose, True)
168                 qui_utils.set_stackable(self._window, True)
169                 self._window.setCentralWidget(centralWidget)
170
171                 self._closeWindowAction = QtGui.QAction(None)
172                 self._closeWindowAction.setText("Close")
173                 self._closeWindowAction.setShortcut(QtGui.QKeySequence("CTRL+w"))
174                 self._closeWindowAction.triggered.connect(self._on_close_window)
175
176                 self._window.addAction(self._closeWindowAction)
177                 self._window.addAction(self._app.quitAction)
178                 self._window.addAction(self._app.fullscreenAction)
179                 self._window.addAction(self._app.orientationAction)
180                 self._window.addAction(self._app.logAction)
181
182         @property
183         def window(self):
184                 return self._window
185
186         def walk_children(self):
187                 return ()
188
189         def start(self):
190                 pass
191
192         def close(self):
193                 for child in self.walk_children():
194                         child.window.destroyed.disconnect(self._on_child_close)
195                         child.close()
196                 self._window.close()
197
198         def destroy(self):
199                 pass
200
201         def show(self):
202                 self.set_fullscreen(self._app.fullscreenAction.isChecked())
203                 self._window.show()
204                 for child in self.walk_children():
205                         child.show()
206
207         def hide(self):
208                 for child in self.walk_children():
209                         child.hide()
210                 self._window.hide()
211
212         def set_fullscreen(self, isFullscreen):
213                 if isFullscreen:
214                         self._window.showFullScreen()
215                 else:
216                         self._window.showNormal()
217                 for child in self.walk_children():
218                         child.set_fullscreen(isFullscreen)
219
220         def set_orientation(self, isPortrait):
221                 if isPortrait:
222                         qui_utils.set_window_orientation(self.window, QtCore.Qt.Vertical)
223                 else:
224                         qui_utils.set_window_orientation(self.window, QtCore.Qt.Horizontal)
225                 for child in self.walk_children():
226                         child.set_orientation(isPortrait)
227
228         @misc_utils.log_exception(_moduleLogger)
229         def _on_child_close(self):
230                 raise NotImplementedError("Booh")
231
232         @misc_utils.log_exception(_moduleLogger)
233         def _on_close_window(self, checked = True):
234                 with qui_utils.notify_error(self._errorLog):
235                         self.close()