Applying various optimizations
[gc-dialer] / 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(self._on_delayed_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 qapp(self):
78                 return self._qapp
79
80         @property
81         def constants(self):
82                 return self._constants
83
84         @property
85         def errorLog(self):
86                 return self._errorLog
87
88         @property
89         def fullscreenAction(self):
90                 return self._fullscreenAction
91
92         @property
93         def orientationAction(self):
94                 return self._orientationAction
95
96         @property
97         def logAction(self):
98                 return self._logAction
99
100         @property
101         def aboutAction(self):
102                 return self._aboutAction
103
104         @property
105         def quitAction(self):
106                 return self._quitAction
107
108         def _close_windows(self):
109                 if self._mainWindow is not None:
110                         self.save_settings()
111                         self._mainWindow.window.destroyed.disconnect(self._on_child_close)
112                         self._mainWindow.close()
113                         self._mainWindow = None
114
115         @misc_utils.log_exception(_moduleLogger)
116         def _on_delayed_start(self):
117                 self._mainWindow.start()
118
119         @misc_utils.log_exception(_moduleLogger)
120         def _on_app_quit(self, checked = False):
121                 if self._mainWindow is not None:
122                         self.save_settings()
123                         self._mainWindow.destroy()
124
125         @misc_utils.log_exception(_moduleLogger)
126         def _on_child_close(self, obj = None):
127                 if self._mainWindow is not None:
128                         self.save_settings()
129                         self._mainWindow = None
130
131         @misc_utils.log_exception(_moduleLogger)
132         def _on_toggle_fullscreen(self, checked = False):
133                 with qui_utils.notify_error(self._errorLog):
134                         self._mainWindow.set_fullscreen(checked)
135
136         @misc_utils.log_exception(_moduleLogger)
137         def _on_toggle_orientation(self, checked = False):
138                 with qui_utils.notify_error(self._errorLog):
139                         self._mainWindow.set_orientation(checked)
140
141         @misc_utils.log_exception(_moduleLogger)
142         def _on_about(self, checked = True):
143                 raise NotImplementedError("Booh")
144
145         @misc_utils.log_exception(_moduleLogger)
146         def _on_log(self, checked = False):
147                 with qui_utils.notify_error(self._errorLog):
148                         with open(self._constants._user_logpath_, "r") as f:
149                                 logLines = f.xreadlines()
150                                 log = "".join(logLines)
151                                 self._clipboard.setText(log)
152
153         @misc_utils.log_exception(_moduleLogger)
154         def _on_quit(self, checked = False):
155                 with qui_utils.notify_error(self._errorLog):
156                         self._close_windows()
157
158
159 class WindowWrapper(object):
160
161         def __init__(self, parent, app):
162                 self._app = app
163
164                 self._errorDisplay = qui_utils.ErrorDisplay(self._app.errorLog)
165
166                 self._layout = QtGui.QBoxLayout(QtGui.QBoxLayout.LeftToRight)
167                 self._layout.setContentsMargins(0, 0, 0, 0)
168
169                 self._superLayout = QtGui.QVBoxLayout()
170                 self._superLayout.addWidget(self._errorDisplay.toplevel)
171                 self._superLayout.setContentsMargins(0, 0, 0, 0)
172                 self._superLayout.addLayout(self._layout)
173
174                 centralWidget = QtGui.QWidget()
175                 centralWidget.setLayout(self._superLayout)
176                 centralWidget.setContentsMargins(0, 0, 0, 0)
177
178                 self._window = qui_utils.QSignalingMainWindow(parent)
179                 self._window.setAttribute(QtCore.Qt.WA_DeleteOnClose, True)
180                 qui_utils.set_stackable(self._window, True)
181                 self._window.setCentralWidget(centralWidget)
182
183                 self._closeWindowAction = QtGui.QAction(None)
184                 self._closeWindowAction.setText("Close")
185                 self._closeWindowAction.setShortcut(QtGui.QKeySequence("CTRL+w"))
186                 self._closeWindowAction.triggered.connect(self._on_close_window)
187
188                 self._window.addAction(self._closeWindowAction)
189                 self._window.addAction(self._app.quitAction)
190                 self._window.addAction(self._app.fullscreenAction)
191                 self._window.addAction(self._app.orientationAction)
192                 self._window.addAction(self._app.logAction)
193
194         @property
195         def window(self):
196                 return self._window
197
198         def walk_children(self):
199                 return ()
200
201         def start(self):
202                 pass
203
204         def close(self):
205                 for child in self.walk_children():
206                         child.window.destroyed.disconnect(self._on_child_close)
207                         child.close()
208                 self._window.close()
209
210         def destroy(self):
211                 pass
212
213         def show(self):
214                 self._window.show()
215                 for child in self.walk_children():
216                         child.show()
217                 self.set_fullscreen(self._app.fullscreenAction.isChecked())
218
219         def hide(self):
220                 for child in self.walk_children():
221                         child.hide()
222                 self._window.hide()
223
224         def set_fullscreen(self, isFullscreen):
225                 if self._window.isVisible():
226                         if isFullscreen:
227                                 self._window.showFullScreen()
228                         else:
229                                 self._window.showNormal()
230                 for child in self.walk_children():
231                         child.set_fullscreen(isFullscreen)
232
233         def set_orientation(self, isPortrait):
234                 if isPortrait:
235                         qui_utils.set_window_orientation(self.window, QtCore.Qt.Vertical)
236                 else:
237                         qui_utils.set_window_orientation(self.window, QtCore.Qt.Horizontal)
238                 for child in self.walk_children():
239                         child.set_orientation(isPortrait)
240
241         @misc_utils.log_exception(_moduleLogger)
242         def _on_child_close(self, obj = None):
243                 raise NotImplementedError("Booh")
244
245         @misc_utils.log_exception(_moduleLogger)
246         def _on_close_window(self, checked = True):
247                 with qui_utils.notify_error(self._errorLog):
248                         self.close()
249
250
251 class AutoFreezeWindowFeature(object):
252
253         def __init__(self, app, window):
254                 self._app = app
255                 self._window = window
256                 self._app.qapp.focusChanged.connect(self._on_focus_changed)
257                 if self._app.qapp.focusWidget() is not None:
258                         self._window.setUpdatesEnabled(True)
259                 else:
260                         self._window.setUpdatesEnabled(False)
261
262         def close(self):
263                 self._app.qapp.focusChanged.disconnect(self._on_focus_changed)
264                 self._window.setUpdatesEnabled(True)
265
266         @misc_utils.log_exception(_moduleLogger)
267         def _on_focus_changed(self, oldWindow, newWindow):
268                 with qui_utils.notify_error(self._app.errorLog):
269                         if oldWindow is None and newWindow is not None:
270                                 self._window.setUpdatesEnabled(True)
271                         elif oldWindow is not None and newWindow is None:
272                                 self._window.setUpdatesEnabled(False)