4 from __future__ import with_statement
11 from PyQt4 import QtGui
12 from PyQt4 import QtCore
16 from util import misc as misc_utils
21 _moduleLogger = logging.getLogger(__name__)
27 class Calculator(object):
29 def __init__(self, app):
32 self._hiddenCategories = set()
33 self._hiddenUnits = {}
34 self._clipboard = QtGui.QApplication.clipboard()
36 self._mainWindow = None
38 self._fullscreenAction = QtGui.QAction(None)
39 self._fullscreenAction.setText("Fullscreen")
40 self._fullscreenAction.setCheckable(True)
41 self._fullscreenAction.setShortcut(QtGui.QKeySequence("CTRL+Enter"))
42 self._fullscreenAction.toggled.connect(self._on_toggle_fullscreen)
44 self._logAction = QtGui.QAction(None)
45 self._logAction.setText("Log")
46 self._logAction.setShortcut(QtGui.QKeySequence("CTRL+l"))
47 self._logAction.triggered.connect(self._on_log)
49 self._quitAction = QtGui.QAction(None)
50 self._quitAction.setText("Quit")
51 self._quitAction.setShortcut(QtGui.QKeySequence("CTRL+q"))
52 self._quitAction.triggered.connect(self._on_quit)
54 self._app.lastWindowClosed.connect(self._on_app_quit)
57 self._mainWindow = MainWindow(None, self)
58 self._mainWindow.window.destroyed.connect(self._on_child_close)
60 def load_settings(self):
62 with open(constants._user_settings_, "r") as settingsFile:
63 settings = simplejson.load(settingsFile)
65 _moduleLogger.info("No settings")
68 _moduleLogger.info("Settings were corrupt")
71 self._fullscreenAction.setChecked(settings.get("isFullScreen", False))
73 def save_settings(self):
75 "isFullScreen": self._fullscreenAction.isChecked(),
77 with open(constants._user_settings_, "w") as settingsFile:
78 simplejson.dump(settings, settingsFile)
81 def fullscreenAction(self):
82 return self._fullscreenAction
86 return self._logAction
90 return self._quitAction
92 def _close_windows(self):
93 if self._mainWindow is not None:
94 self._mainWindow.window.destroyed.disconnect(self._on_child_close)
95 self._mainWindow.close()
96 self._mainWindow = None
98 @misc_utils.log_exception(_moduleLogger)
99 def _on_app_quit(self, checked = False):
102 @misc_utils.log_exception(_moduleLogger)
103 def _on_child_close(self, obj = None):
104 self._mainWindow = None
106 @misc_utils.log_exception(_moduleLogger)
107 def _on_toggle_fullscreen(self, checked = False):
108 for window in self._walk_children():
109 window.set_fullscreen(checked)
111 @misc_utils.log_exception(_moduleLogger)
112 def _on_log(self, checked = False):
113 with open(constants._user_logpath_, "r") as f:
114 logLines = f.xreadlines()
115 log = "".join(logLines)
116 self._clipboard.setText(log)
118 @misc_utils.log_exception(_moduleLogger)
119 def _on_quit(self, checked = False):
120 self._close_windows()
123 class MainWindow(object):
125 def __init__(self, parent, app):
128 self._historyView = qhistory.QCalcHistory()
130 self._layout = QtGui.QVBoxLayout()
131 self._layout.addWidget(self._historyView.toplevel)
133 centralWidget = QtGui.QWidget()
134 centralWidget.setLayout(self._layout)
136 self._window = QtGui.QMainWindow(parent)
137 self._window.setAttribute(QtCore.Qt.WA_DeleteOnClose, True)
138 maeqt.set_autorient(self._window, True)
139 maeqt.set_stackable(self._window, True)
140 self._window.setWindowTitle("%s" % constants.__pretty_app_name__)
141 self._window.setCentralWidget(centralWidget)
143 self._closeWindowAction = QtGui.QAction(None)
144 self._closeWindowAction.setText("Close")
145 self._closeWindowAction.setShortcut(QtGui.QKeySequence("CTRL+w"))
146 self._closeWindowAction.triggered.connect(self._on_close_window)
149 #fileMenu = self._window.menuBar().addMenu("&File")
151 #viewMenu = self._window.menuBar().addMenu("&View")
153 self._window.addAction(self._closeWindowAction)
154 self._window.addAction(self._app.quitAction)
155 self._window.addAction(self._app.fullscreenAction)
157 fileMenu = self._window.menuBar().addMenu("&Units")
158 fileMenu.addAction(self._closeWindowAction)
159 fileMenu.addAction(self._app.quitAction)
161 viewMenu = self._window.menuBar().addMenu("&View")
162 viewMenu.addAction(self._app.fullscreenAction)
164 self._window.addAction(self._app.logAction)
166 self.set_fullscreen(self._app.fullscreenAction.isChecked())
173 def walk_children(self):
178 for child in self.walk_children():
182 for child in self.walk_children():
187 for child in self.walk_children():
188 child.window.destroyed.disconnect(self._on_child_close)
192 def set_fullscreen(self, isFullscreen):
194 self._window.showFullScreen()
196 self._window.showNormal()
197 for child in self.walk_children():
198 child.set_fullscreen(isFullscreen)
200 @misc_utils.log_exception(_moduleLogger)
201 def _on_close_window(self, checked = True):
206 app = QtGui.QApplication([])
207 handle = Calculator(app)
211 if __name__ == "__main__":
212 logging.basicConfig(level = logging.DEBUG)
214 os.makedirs(constants._data_path_)