Adding support for PySide's naming scheme
[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 import qt_compat
9 QtCore = qt_compat.QtCore
10 QtGui = qt_compat.import_module("QtGui")
11
12 from util import qui_utils
13 from util import misc as misc_utils
14
15
16 _moduleLogger = logging.getLogger(__name__)
17
18
19 class ApplicationWrapper(object):
20
21         def __init__(self, qapp, constants):
22                 self._constants = constants
23                 self._qapp = qapp
24                 self._clipboard = QtGui.QApplication.clipboard()
25
26                 self._errorLog = qui_utils.QErrorLog()
27                 self._mainWindow = None
28
29                 self._fullscreenAction = QtGui.QAction(None)
30                 self._fullscreenAction.setText("Fullscreen")
31                 self._fullscreenAction.setCheckable(True)
32                 self._fullscreenAction.setShortcut(QtGui.QKeySequence("CTRL+Enter"))
33                 self._fullscreenAction.toggled.connect(self._on_toggle_fullscreen)
34
35                 self._orientationAction = QtGui.QAction(None)
36                 self._orientationAction.setText("Orientation")
37                 self._orientationAction.setCheckable(True)
38                 self._orientationAction.setShortcut(QtGui.QKeySequence("CTRL+o"))
39                 self._orientationAction.toggled.connect(self._on_toggle_orientation)
40
41                 self._logAction = QtGui.QAction(None)
42                 self._logAction.setText("Log")
43                 self._logAction.setShortcut(QtGui.QKeySequence("CTRL+l"))
44                 self._logAction.triggered.connect(self._on_log)
45
46                 self._quitAction = QtGui.QAction(None)
47                 self._quitAction.setText("Quit")
48                 self._quitAction.setShortcut(QtGui.QKeySequence("CTRL+q"))
49                 self._quitAction.triggered.connect(self._on_quit)
50
51                 self._aboutAction = QtGui.QAction(None)
52                 self._aboutAction.setText("About")
53                 self._aboutAction.triggered.connect(self._on_about)
54
55                 self._qapp.lastWindowClosed.connect(self._on_app_quit)
56                 self._mainWindow = self._new_main_window()
57                 self._mainWindow.window.destroyed.connect(self._on_child_close)
58
59                 self.load_settings()
60
61                 self._mainWindow.show()
62                 self._idleDelay = QtCore.QTimer()
63                 self._idleDelay.setSingleShot(True)
64                 self._idleDelay.setInterval(0)
65                 self._idleDelay.timeout.connect(self._on_delayed_start)
66                 self._idleDelay.start()
67
68         def load_settings(self):
69                 raise NotImplementedError("Booh")
70
71         def save_settings(self):
72                 raise NotImplementedError("Booh")
73
74         def _new_main_window(self):
75                 raise NotImplementedError("Booh")
76
77         @property
78         def qapp(self):
79                 return self._qapp
80
81         @property
82         def constants(self):
83                 return self._constants
84
85         @property
86         def errorLog(self):
87                 return self._errorLog
88
89         @property
90         def fullscreenAction(self):
91                 return self._fullscreenAction
92
93         @property
94         def orientationAction(self):
95                 return self._orientationAction
96
97         @property
98         def logAction(self):
99                 return self._logAction
100
101         @property
102         def aboutAction(self):
103                 return self._aboutAction
104
105         @property
106         def quitAction(self):
107                 return self._quitAction
108
109         def _close_windows(self):
110                 if self._mainWindow is not None:
111                         self.save_settings()
112                         self._mainWindow.window.destroyed.disconnect(self._on_child_close)
113                         self._mainWindow.close()
114                         self._mainWindow = None
115
116         @misc_utils.log_exception(_moduleLogger)
117         def _on_delayed_start(self):
118                 self._mainWindow.start()
119
120         @misc_utils.log_exception(_moduleLogger)
121         def _on_app_quit(self, checked = False):
122                 if self._mainWindow is not None:
123                         self.save_settings()
124                         self._mainWindow.destroy()
125
126         @misc_utils.log_exception(_moduleLogger)
127         def _on_child_close(self, obj = None):
128                 if self._mainWindow is not None:
129                         self.save_settings()
130                         self._mainWindow = None
131
132         @misc_utils.log_exception(_moduleLogger)
133         def _on_toggle_fullscreen(self, checked = False):
134                 with qui_utils.notify_error(self._errorLog):
135                         self._mainWindow.set_fullscreen(checked)
136
137         @misc_utils.log_exception(_moduleLogger)
138         def _on_toggle_orientation(self, checked = False):
139                 with qui_utils.notify_error(self._errorLog):
140                         self._mainWindow.set_orientation(checked)
141
142         @misc_utils.log_exception(_moduleLogger)
143         def _on_about(self, checked = True):
144                 raise NotImplementedError("Booh")
145
146         @misc_utils.log_exception(_moduleLogger)
147         def _on_log(self, checked = False):
148                 with qui_utils.notify_error(self._errorLog):
149                         with open(self._constants._user_logpath_, "r") as f:
150                                 logLines = f.xreadlines()
151                                 log = "".join(logLines)
152                                 self._clipboard.setText(log)
153
154         @misc_utils.log_exception(_moduleLogger)
155         def _on_quit(self, checked = False):
156                 with qui_utils.notify_error(self._errorLog):
157                         self._close_windows()
158
159
160 class WindowWrapper(object):
161
162         def __init__(self, parent, app):
163                 self._app = app
164
165                 self._errorDisplay = qui_utils.ErrorDisplay(self._app.errorLog)
166
167                 self._layout = QtGui.QBoxLayout(QtGui.QBoxLayout.LeftToRight)
168                 self._layout.setContentsMargins(0, 0, 0, 0)
169
170                 self._superLayout = QtGui.QVBoxLayout()
171                 self._superLayout.addWidget(self._errorDisplay.toplevel)
172                 self._superLayout.setContentsMargins(0, 0, 0, 0)
173                 self._superLayout.addLayout(self._layout)
174
175                 centralWidget = QtGui.QWidget()
176                 centralWidget.setLayout(self._superLayout)
177                 centralWidget.setContentsMargins(0, 0, 0, 0)
178
179                 self._window = qui_utils.QSignalingMainWindow(parent)
180                 self._window.setAttribute(QtCore.Qt.WA_DeleteOnClose, True)
181                 qui_utils.set_stackable(self._window, True)
182                 self._window.setCentralWidget(centralWidget)
183
184                 self._closeWindowAction = QtGui.QAction(None)
185                 self._closeWindowAction.setText("Close")
186                 self._closeWindowAction.setShortcut(QtGui.QKeySequence("CTRL+w"))
187                 self._closeWindowAction.triggered.connect(self._on_close_window)
188
189                 self._window.addAction(self._closeWindowAction)
190                 self._window.addAction(self._app.quitAction)
191                 self._window.addAction(self._app.fullscreenAction)
192                 self._window.addAction(self._app.orientationAction)
193                 self._window.addAction(self._app.logAction)
194
195         @property
196         def window(self):
197                 return self._window
198
199         def walk_children(self):
200                 return ()
201
202         def start(self):
203                 pass
204
205         def close(self):
206                 for child in self.walk_children():
207                         child.window.destroyed.disconnect(self._on_child_close)
208                         child.close()
209                 self._window.close()
210
211         def destroy(self):
212                 pass
213
214         def show(self):
215                 self._window.show()
216                 for child in self.walk_children():
217                         child.show()
218                 self.set_fullscreen(self._app.fullscreenAction.isChecked())
219
220         def hide(self):
221                 for child in self.walk_children():
222                         child.hide()
223                 self._window.hide()
224
225         def set_fullscreen(self, isFullscreen):
226                 if self._window.isVisible():
227                         if isFullscreen:
228                                 self._window.showFullScreen()
229                         else:
230                                 self._window.showNormal()
231                 for child in self.walk_children():
232                         child.set_fullscreen(isFullscreen)
233
234         def set_orientation(self, isPortrait):
235                 if isPortrait:
236                         qui_utils.set_window_orientation(self.window, QtCore.Qt.Vertical)
237                 else:
238                         qui_utils.set_window_orientation(self.window, QtCore.Qt.Horizontal)
239                 for child in self.walk_children():
240                         child.set_orientation(isPortrait)
241
242         @misc_utils.log_exception(_moduleLogger)
243         def _on_child_close(self, obj = None):
244                 raise NotImplementedError("Booh")
245
246         @misc_utils.log_exception(_moduleLogger)
247         def _on_close_window(self, checked = True):
248                 with qui_utils.notify_error(self._errorLog):
249                         self.close()
250
251
252 class AutoFreezeWindowFeature(object):
253
254         def __init__(self, app, window):
255                 self._app = app
256                 self._window = window
257                 self._app.qapp.focusChanged.connect(self._on_focus_changed)
258                 if self._app.qapp.focusWidget() is not None:
259                         self._window.setUpdatesEnabled(True)
260                 else:
261                         self._window.setUpdatesEnabled(False)
262
263         def close(self):
264                 self._app.qapp.focusChanged.disconnect(self._on_focus_changed)
265                 self._window.setUpdatesEnabled(True)
266
267         @misc_utils.log_exception(_moduleLogger)
268         def _on_focus_changed(self, oldWindow, newWindow):
269                 with qui_utils.notify_error(self._app.errorLog):
270                         if oldWindow is None and newWindow is not None:
271                                 self._window.setUpdatesEnabled(True)
272                         elif oldWindow is not None and newWindow is None:
273                                 self._window.setUpdatesEnabled(False)