Merging skeleton changes
[gonvert] / src / REPLACEME_qt.py
1 #!/usr/bin/env python
2 # -*- coding: UTF8 -*-
3
4 from __future__ import with_statement
5
6 import sys
7 import os
8 import simplejson
9 import logging
10
11 from PyQt4 import QtGui
12 from PyQt4 import QtCore
13
14 import constants
15 import maeqt
16 from util import misc as misc_utils
17 import unit_data
18
19
20 _moduleLogger = logging.getLogger(__name__)
21
22
23 IS_MAEMO = True
24
25
26 class REPLACEME(object):
27
28         _DATA_PATHS = [
29                 os.path.dirname(__file__),
30                 os.path.join(os.path.dirname(__file__), "../data"),
31                 os.path.join(os.path.dirname(__file__), "../lib"),
32                 '/usr/share/%s' % constants.__app_name__,
33                 '/usr/lib/%s' % constants.__app_name__,
34         ]
35
36         def __init__(self, app):
37                 self._dataPath = ""
38                 for dataPath in self._DATA_PATHS:
39                         appIconPath = os.path.join(dataPath, "pixmaps", "%s.png" %  constants.__app_name__)
40                         if os.path.isfile(appIconPath):
41                                 self._dataPath = dataPath
42                                 break
43                 else:
44                         raise RuntimeError("UI Descriptor not found!")
45                 self._app = app
46                 self._appIconPath = appIconPath
47                 self._recent = []
48                 self._hiddenCategories = set()
49                 self._hiddenUnits = {}
50                 self._clipboard = QtGui.QApplication.clipboard()
51
52                 self._mainWindow = None
53
54                 self._fullscreenAction = QtGui.QAction(None)
55                 self._fullscreenAction.setText("Fullscreen")
56                 self._fullscreenAction.setCheckable(True)
57                 self._fullscreenAction.setShortcut(QtGui.QKeySequence("CTRL+Enter"))
58                 self._fullscreenAction.toggled.connect(self._on_toggle_fullscreen)
59
60                 self._logAction = QtGui.QAction(None)
61                 self._logAction.setText("Log")
62                 self._logAction.setShortcut(QtGui.QKeySequence("CTRL+l"))
63                 self._logAction.triggered.connect(self._on_log)
64
65                 self._quitAction = QtGui.QAction(None)
66                 self._quitAction.setText("Quit")
67                 self._quitAction.setShortcut(QtGui.QKeySequence("CTRL+q"))
68                 self._quitAction.triggered.connect(self._on_quit)
69
70                 self._app.lastWindowClosed.connect(self._on_app_quit)
71                 self.load_settings()
72
73         def load_settings(self):
74                 try:
75                         with open(constants._user_settings_, "r") as settingsFile:
76                                 settings = simplejson.load(settingsFile)
77                 except IOError, e:
78                         _moduleLogger.info("No settings")
79                         settings = {}
80                 except ValueError:
81                         _moduleLogger.info("Settings were corrupt")
82                         settings = {}
83
84                 self._fullscreenAction.setChecked(settings.get("isFullScreen", False))
85
86         def save_settings(self):
87                 settings = {
88                         "isFullScreen": self._fullscreenAction.isChecked(),
89                 }
90                 with open(constants._user_settings_, "w") as settingsFile:
91                         simplejson.dump(settings, settingsFile)
92
93         @property
94         def appIconPath(self):
95                 return self._appIconPath
96
97         @property
98         def fullscreenAction(self):
99                 return self._fullscreenAction
100
101         @property
102         def logAction(self):
103                 return self._logAction
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._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_app_quit(self, checked = False):
117                 self.save_settings()
118
119         @misc_utils.log_exception(_moduleLogger)
120         def _on_child_close(self, obj = None):
121                 self._mainWindow = None
122
123         @misc_utils.log_exception(_moduleLogger)
124         def _on_toggle_fullscreen(self, checked = False):
125                 for window in self._walk_children():
126                         window.set_fullscreen(checked)
127
128         @misc_utils.log_exception(_moduleLogger)
129         def _on_log(self, checked = False):
130                 with open(constants._user_logpath_, "r") as f:
131                         logLines = f.xreadlines()
132                         log = "".join(logLines)
133                         self._clipboard.setText(log)
134
135         @misc_utils.log_exception(_moduleLogger)
136         def _on_quit(self, checked = False):
137                 self._close_windows()
138
139
140 class MainWindow(object):
141
142         def __init__(self, parent, app):
143                 self._app = app
144
145                 self._layout = QtGui.QVBoxLayout()
146
147                 centralWidget = QtGui.QWidget()
148                 centralWidget.setLayout(self._layout)
149
150                 self._window = QtGui.QMainWindow(parent)
151                 self._window.setAttribute(QtCore.Qt.WA_DeleteOnClose, True)
152                 maeqt.set_autorient(self._window, True)
153                 maeqt.set_stackable(self._window, True)
154                 self._window.setWindowTitle("%s" % constants.__pretty_app_name__)
155                 self._window.setWindowIcon(QtGui.QIcon(self._app.appIconPath))
156                 self._window.setCentralWidget(centralWidget)
157
158                 self._closeWindowAction = QtGui.QAction(None)
159                 self._closeWindowAction.setText("Close")
160                 self._closeWindowAction.setShortcut(QtGui.QKeySequence("CTRL+w"))
161                 self._closeWindowAction.triggered.connect(self._on_close_window)
162
163                 if IS_MAEMO:
164                         fileMenu = self._window.menuBar().addMenu("&File")
165
166                         viewMenu = self._window.menuBar().addMenu("&View")
167
168                         self._window.addAction(self._closeWindowAction)
169                         self._window.addAction(self._app.quitAction)
170                         self._window.addAction(self._app.fullscreenAction)
171                 else:
172                         fileMenu = self._window.menuBar().addMenu("&Units")
173                         fileMenu.addAction(self._closeWindowAction)
174                         fileMenu.addAction(self._app.quitAction)
175
176                         viewMenu = self._window.menuBar().addMenu("&View")
177                         viewMenu.addAction(self._app.fullscreenAction)
178
179                 self._window.addAction(self._app.logAction)
180
181                 self.set_fullscreen(self._app.fullscreenAction.isChecked())
182                 self._window.show()
183
184         @property
185         def window(self):
186                 return self._window
187
188         def walk_children(self):
189                 return ()
190
191         def show(self):
192                 self._window.show()
193                 for child in self.walk_children():
194                         child.show()
195
196         def hide(self):
197                 for child in self.walk_children():
198                         child.hide()
199                 self._window.hide()
200
201         def close(self):
202                 for child in self.walk_children():
203                         child.window.destroyed.disconnect(self._on_child_close)
204                         child.close()
205                 self._window.close()
206
207         def set_fullscreen(self, isFullscreen):
208                 if isFullscreen:
209                         self._window.showFullScreen()
210                 else:
211                         self._window.showNormal()
212                 for child in self.walk_children():
213                         child.set_fullscreen(isFullscreen)
214
215         @misc_utils.log_exception(_moduleLogger)
216         def _on_close_window(self, checked = True):
217                 self.close()
218
219
220 def run():
221         app = QtGui.QApplication([])
222         handle = REPLACEME(app)
223         return app.exec_()
224
225
226 if __name__ == "__main__":
227         logging.basicConfig(level = logging.DEBUG)
228         try:
229                 os.makedirs(constants._data_path_)
230         except OSError, e:
231                 if e.errno != 17:
232                         raise
233
234         val = run()
235         sys.exit(val)