Start of QT calculator
[ejpi] / src / ejpi_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
18
19 _moduleLogger = logging.getLogger(__name__)
20
21
22 IS_MAEMO = True
23
24
25 class Calculator(object):
26
27         def __init__(self, app):
28                 self._app = app
29                 self._recent = []
30                 self._hiddenCategories = set()
31                 self._hiddenUnits = {}
32                 self._clipboard = QtGui.QApplication.clipboard()
33
34                 self._mainWindow = None
35
36                 self._fullscreenAction = QtGui.QAction(None)
37                 self._fullscreenAction.setText("Fullscreen")
38                 self._fullscreenAction.setCheckable(True)
39                 self._fullscreenAction.setShortcut(QtGui.QKeySequence("CTRL+Enter"))
40                 self._fullscreenAction.toggled.connect(self._on_toggle_fullscreen)
41
42                 self._logAction = QtGui.QAction(None)
43                 self._logAction.setText("Log")
44                 self._logAction.setShortcut(QtGui.QKeySequence("CTRL+l"))
45                 self._logAction.triggered.connect(self._on_log)
46
47                 self._quitAction = QtGui.QAction(None)
48                 self._quitAction.setText("Quit")
49                 self._quitAction.setShortcut(QtGui.QKeySequence("CTRL+q"))
50                 self._quitAction.triggered.connect(self._on_quit)
51
52                 self._app.lastWindowClosed.connect(self._on_app_quit)
53                 self.load_settings()
54
55         def load_settings(self):
56                 try:
57                         with open(constants._user_settings_, "r") as settingsFile:
58                                 settings = simplejson.load(settingsFile)
59                 except IOError, e:
60                         _moduleLogger.info("No settings")
61                         settings = {}
62                 except ValueError:
63                         _moduleLogger.info("Settings were corrupt")
64                         settings = {}
65
66                 self._fullscreenAction.setChecked(settings.get("isFullScreen", False))
67
68         def save_settings(self):
69                 settings = {
70                         "isFullScreen": self._fullscreenAction.isChecked(),
71                 }
72                 with open(constants._user_settings_, "w") as settingsFile:
73                         simplejson.dump(settings, settingsFile)
74
75         @property
76         def fullscreenAction(self):
77                 return self._fullscreenAction
78
79         @property
80         def logAction(self):
81                 return self._logAction
82
83         @property
84         def quitAction(self):
85                 return self._quitAction
86
87         def _close_windows(self):
88                 if self._mainWindow is not None:
89                         self._mainWindow.window.destroyed.disconnect(self._on_child_close)
90                         self._mainWindow.close()
91                         self._mainWindow = None
92
93         @misc_utils.log_exception(_moduleLogger)
94         def _on_app_quit(self, checked = False):
95                 self.save_settings()
96
97         @misc_utils.log_exception(_moduleLogger)
98         def _on_child_close(self, obj = None):
99                 self._mainWindow = None
100
101         @misc_utils.log_exception(_moduleLogger)
102         def _on_toggle_fullscreen(self, checked = False):
103                 for window in self._walk_children():
104                         window.set_fullscreen(checked)
105
106         @misc_utils.log_exception(_moduleLogger)
107         def _on_log(self, checked = False):
108                 with open(constants._user_logpath_, "r") as f:
109                         logLines = f.xreadlines()
110                         log = "".join(logLines)
111                         self._clipboard.setText(log)
112
113         @misc_utils.log_exception(_moduleLogger)
114         def _on_quit(self, checked = False):
115                 self._close_windows()
116
117
118 class MainWindow(object):
119
120         def __init__(self, parent, app):
121                 self._app = app
122
123                 self._layout = QtGui.QVBoxLayout()
124
125                 centralWidget = QtGui.QWidget()
126                 centralWidget.setLayout(self._layout)
127
128                 self._window = QtGui.QMainWindow(parent)
129                 self._window.setAttribute(QtCore.Qt.WA_DeleteOnClose, True)
130                 maeqt.set_autorient(self._window, True)
131                 maeqt.set_stackable(self._window, True)
132                 self._window.setWindowTitle("%s" % constants.__pretty_app_name__)
133                 self._window.setCentralWidget(centralWidget)
134
135                 self._closeWindowAction = QtGui.QAction(None)
136                 self._closeWindowAction.setText("Close")
137                 self._closeWindowAction.setShortcut(QtGui.QKeySequence("CTRL+w"))
138                 self._closeWindowAction.triggered.connect(self._on_close_window)
139
140                 if IS_MAEMO:
141                         fileMenu = self._window.menuBar().addMenu("&File")
142
143                         viewMenu = self._window.menuBar().addMenu("&View")
144
145                         self._window.addAction(self._closeWindowAction)
146                         self._window.addAction(self._app.quitAction)
147                         self._window.addAction(self._app.fullscreenAction)
148                 else:
149                         fileMenu = self._window.menuBar().addMenu("&Units")
150                         fileMenu.addAction(self._closeWindowAction)
151                         fileMenu.addAction(self._app.quitAction)
152
153                         viewMenu = self._window.menuBar().addMenu("&View")
154                         viewMenu.addAction(self._app.fullscreenAction)
155
156                 self._window.addAction(self._app.logAction)
157
158                 self.set_fullscreen(self._app.fullscreenAction.isChecked())
159                 self._window.show()
160
161         @property
162         def window(self):
163                 return self._window
164
165         def walk_children(self):
166                 return ()
167
168         def show(self):
169                 self._window.show()
170                 for child in self.walk_children():
171                         child.show()
172
173         def hide(self):
174                 for child in self.walk_children():
175                         child.hide()
176                 self._window.hide()
177
178         def close(self):
179                 for child in self.walk_children():
180                         child.window.destroyed.disconnect(self._on_child_close)
181                         child.close()
182                 self._window.close()
183
184         def set_fullscreen(self, isFullscreen):
185                 if isFullscreen:
186                         self._window.showFullScreen()
187                 else:
188                         self._window.showNormal()
189                 for child in self.walk_children():
190                         child.set_fullscreen(isFullscreen)
191
192         @misc_utils.log_exception(_moduleLogger)
193         def _on_close_window(self, checked = True):
194                 self.close()
195
196
197 def run():
198         app = QtGui.QApplication([])
199         handle = Calculator(app)
200         return app.exec_()
201
202
203 if __name__ == "__main__":
204         logging.basicConfig(level = logging.DEBUG)
205         try:
206                 os.makedirs(constants._data_path_)
207         except OSError, e:
208                 if e.errno != 17:
209                         raise
210
211         val = run()
212         sys.exit(val)