4 from __future__ import with_statement
12 from PyQt4 import QtGui
13 from PyQt4 import QtCore
17 from util import misc as misc_utils
19 from libraries import qtpie
20 from libraries import qtpieboard
26 _moduleLogger = logging.getLogger(__name__)
32 PLUGIN_SEARCH_PATHS = [
33 os.path.join(os.path.dirname(__file__), "plugins/"),
37 class Calculator(object):
39 def __init__(self, app):
42 self._hiddenCategories = set()
43 self._hiddenUnits = {}
44 self._clipboard = QtGui.QApplication.clipboard()
45 self._mainWindow = None
47 self._fullscreenAction = QtGui.QAction(None)
48 self._fullscreenAction.setText("Fullscreen")
49 self._fullscreenAction.setCheckable(True)
50 self._fullscreenAction.setShortcut(QtGui.QKeySequence("CTRL+Enter"))
51 self._fullscreenAction.toggled.connect(self._on_toggle_fullscreen)
53 self._logAction = QtGui.QAction(None)
54 self._logAction.setText("Log")
55 self._logAction.setShortcut(QtGui.QKeySequence("CTRL+l"))
56 self._logAction.triggered.connect(self._on_log)
58 self._quitAction = QtGui.QAction(None)
59 self._quitAction.setText("Quit")
60 self._quitAction.setShortcut(QtGui.QKeySequence("CTRL+q"))
61 self._quitAction.triggered.connect(self._on_quit)
63 self._app.lastWindowClosed.connect(self._on_app_quit)
66 self._mainWindow = MainWindow(None, self)
67 self._mainWindow.window.destroyed.connect(self._on_child_close)
69 def load_settings(self):
71 with open(constants._user_settings_, "r") as settingsFile:
72 settings = simplejson.load(settingsFile)
74 _moduleLogger.info("No settings")
77 _moduleLogger.info("Settings were corrupt")
80 self._fullscreenAction.setChecked(settings.get("isFullScreen", False))
82 def save_settings(self):
84 "isFullScreen": self._fullscreenAction.isChecked(),
86 with open(constants._user_settings_, "w") as settingsFile:
87 simplejson.dump(settings, settingsFile)
90 def fullscreenAction(self):
91 return self._fullscreenAction
95 return self._logAction
99 return self._quitAction
101 def _close_windows(self):
102 if self._mainWindow is not None:
103 self._mainWindow.window.destroyed.disconnect(self._on_child_close)
104 self._mainWindow.close()
105 self._mainWindow = None
107 @misc_utils.log_exception(_moduleLogger)
108 def _on_app_quit(self, checked = False):
111 @misc_utils.log_exception(_moduleLogger)
112 def _on_child_close(self, obj = None):
113 self._mainWindow = None
115 @misc_utils.log_exception(_moduleLogger)
116 def _on_toggle_fullscreen(self, checked = False):
117 for window in self._walk_children():
118 window.set_fullscreen(checked)
120 @misc_utils.log_exception(_moduleLogger)
121 def _on_log(self, checked = False):
122 with open(constants._user_logpath_, "r") as f:
123 logLines = f.xreadlines()
124 log = "".join(logLines)
125 self._clipboard.setText(log)
127 @misc_utils.log_exception(_moduleLogger)
128 def _on_quit(self, checked = False):
129 self._close_windows()
132 class QErrorDisplay(object):
137 icon = QtGui.QIcon.fromTheme("gtk-dialog-error")
138 self._severityIcon = icon.pixmap(32, 32)
139 self._severityLabel = QtGui.QLabel()
140 self._severityLabel.setPixmap(self._severityIcon)
142 self._message = QtGui.QLabel()
143 self._message.setText("Boo")
145 icon = QtGui.QIcon.fromTheme("gtk-close")
146 self._closeIcon = icon.pixmap(32, 32)
147 self._closeLabel = QtGui.QLabel()
148 self._closeLabel.setPixmap(self._closeIcon)
150 self._controlLayout = QtGui.QHBoxLayout()
151 self._controlLayout.addWidget(self._severityLabel)
152 self._controlLayout.addWidget(self._message)
153 self._controlLayout.addWidget(self._closeLabel)
155 self._topLevelLayout = QtGui.QHBoxLayout()
159 return self._topLevelLayout
161 def push_message(self, message):
162 self._messages.append(message)
163 if 1 == len(self._messages):
164 self._show_message(message)
166 def push_exception(self):
167 userMessage = str(sys.exc_info()[1])
168 _moduleLogger.exception(userMessage)
169 self.push_message(userMessage)
171 def pop_message(self):
172 del self._messages[0]
173 if 0 == len(self._messages):
176 self._message.setText(self._messages[0])
178 def _on_close(self, *args):
181 def _show_message(self, message):
182 self._message.setText(message)
183 self._topLevelLayout.addLayout(self._controlLayout)
185 def _hide_message(self):
186 self._message.setText("")
187 self._topLevelLayout.removeItem(self._controlLayout)
190 class QValueEntry(object):
193 self._widget = QtGui.QLineEdit("")
194 self._widget.setInputMethodHints(QtCore.Qt.ImhPreferNumbers)
195 self._actualEntryDisplay = ""
202 value = self._actualEntryDisplay.strip()
204 0 < value.find(whitespace)
205 for whitespace in string.whitespace
208 raise ValueError('Invalid input "%s"' % value)
211 def set_value(self, value):
212 value = value.strip()
214 0 < value.find(whitespace)
215 for whitespace in string.whitespace
217 raise ValueError('Invalid input "%s"' % value)
218 self._actualEntryDisplay = value
219 self._widget.setText(value)
221 def append(self, value):
222 value = value.strip()
224 0 < value.find(whitespace)
225 for whitespace in string.whitespace
227 raise ValueError('Invalid input "%s"' % value)
228 self.set_value(self.get_value() + value)
231 value = self.get_value()[0:-1]
232 self.set_value(value)
237 value = property(get_value, set_value, clear)
240 class MainWindow(object):
242 _plugin_search_paths = [
243 "/opt/epi/lib/plugins/",
244 "/usr/lib/ejpi/plugins/",
245 os.path.join(os.path.dirname(__file__), "plugins/"),
248 _user_history = "%s/history.stack" % constants._data_path_
250 def __init__(self, parent, app):
253 self._historyView = qhistory.QCalcHistory()
255 self._errorDisplay = QErrorDisplay()
257 self._userEntry = QValueEntry()
259 self._controlLayout = QtGui.QVBoxLayout()
260 self._controlLayout.addLayout(self._errorDisplay.toplevel)
261 self._controlLayout.addWidget(self._historyView.toplevel)
262 self._controlLayout.addWidget(self._userEntry.toplevel)
264 self._pluginKeyboardSpot = QtGui.QVBoxLayout()
265 self._inputLayout = QtGui.QVBoxLayout()
266 self._inputLayout.addLayout(self._pluginKeyboardSpot)
268 self._layout = QtGui.QHBoxLayout()
269 self._layout.addLayout(self._controlLayout)
270 self._layout.addLayout(self._inputLayout)
272 centralWidget = QtGui.QWidget()
273 centralWidget.setLayout(self._layout)
275 self._window = QtGui.QMainWindow(parent)
276 self._window.setAttribute(QtCore.Qt.WA_DeleteOnClose, True)
277 maeqt.set_autorient(self._window, True)
278 maeqt.set_stackable(self._window, True)
279 self._window.setWindowTitle("%s" % constants.__pretty_app_name__)
280 self._window.setCentralWidget(centralWidget)
281 self._window.destroyed.connect(self._on_close_window)
283 self._closeWindowAction = QtGui.QAction(None)
284 self._closeWindowAction.setText("Close")
285 self._closeWindowAction.setShortcut(QtGui.QKeySequence("CTRL+w"))
286 self._closeWindowAction.triggered.connect(self._on_close_window)
289 #fileMenu = self._window.menuBar().addMenu("&File")
291 #viewMenu = self._window.menuBar().addMenu("&View")
293 self._window.addAction(self._closeWindowAction)
294 self._window.addAction(self._app.quitAction)
295 self._window.addAction(self._app.fullscreenAction)
297 fileMenu = self._window.menuBar().addMenu("&Units")
298 fileMenu.addAction(self._closeWindowAction)
299 fileMenu.addAction(self._app.quitAction)
301 viewMenu = self._window.menuBar().addMenu("&View")
302 viewMenu.addAction(self._app.fullscreenAction)
304 self._window.addAction(self._app.logAction)
306 self._constantPlugins = plugin_utils.ConstantPluginManager()
307 self._constantPlugins.add_path(*self._plugin_search_paths)
308 for pluginName in ["Builtin", "Trigonometry", "Computer", "Alphabet"]:
310 pluginId = self._constantPlugins.lookup_plugin(pluginName)
311 self._constantPlugins.enable_plugin(pluginId)
313 _moduleLogger.info("Failed to load plugin %s" % pluginName)
315 self._operatorPlugins = plugin_utils.OperatorPluginManager()
316 self._operatorPlugins.add_path(*self._plugin_search_paths)
317 for pluginName in ["Builtin", "Trigonometry", "Computer", "Alphabet"]:
319 pluginId = self._operatorPlugins.lookup_plugin(pluginName)
320 self._operatorPlugins.enable_plugin(pluginId)
322 _moduleLogger.info("Failed to load plugin %s" % pluginName)
324 self._keyboardPlugins = plugin_utils.KeyboardPluginManager()
325 self._keyboardPlugins.add_path(*self._plugin_search_paths)
326 self._activeKeyboards = []
328 self._history = history.RpnCalcHistory(
330 self._userEntry, self._errorDisplay,
331 self._constantPlugins.constants, self._operatorPlugins.operators
335 # Basic keyboard stuff
336 self._handler = qtpieboard.KeyboardHandler(self._on_entry_direct)
337 self._handler.register_command_handler("push", self._on_push)
338 self._handler.register_command_handler("unpush", self._on_unpush)
339 self._handler.register_command_handler("backspace", self._on_entry_backspace)
340 self._handler.register_command_handler("clear", self._on_entry_clear)
343 builtinKeyboardId = self._keyboardPlugins.lookup_plugin("Builtin")
344 self._keyboardPlugins.enable_plugin(builtinKeyboardId)
345 self._builtinPlugin = self._keyboardPlugins.keyboards["Builtin"].construct_keyboard()
346 self._builtinKeyboard = self._builtinPlugin.setup(self._history, self._handler)
347 self._inputLayout.addLayout(self._builtinKeyboard.toplevel)
350 self.enable_plugin(self._keyboardPlugins.lookup_plugin("Trigonometry"))
351 self.enable_plugin(self._keyboardPlugins.lookup_plugin("Computer"))
352 self.enable_plugin(self._keyboardPlugins.lookup_plugin("Alphabet"))
353 self._set_plugin_kb(0)
355 self.set_fullscreen(self._app.fullscreenAction.isChecked())
362 def walk_children(self):
367 for child in self.walk_children():
371 for child in self.walk_children():
376 for child in self.walk_children():
377 child.window.destroyed.disconnect(self._on_child_close)
381 def set_fullscreen(self, isFullscreen):
383 self._window.showFullScreen()
385 self._window.showNormal()
386 for child in self.walk_children():
387 child.set_fullscreen(isFullscreen)
389 def enable_plugin(self, pluginId):
390 self._keyboardPlugins.enable_plugin(pluginId)
391 pluginData = self._keyboardPlugins.plugin_info(pluginId)
392 pluginName = pluginData[0]
393 plugin = self._keyboardPlugins.keyboards[pluginName].construct_keyboard()
394 pluginKeyboard = plugin.setup(self._history, self._handler)
396 self._activeKeyboards.append({
397 "pluginName": pluginName,
399 "pluginKeyboard": pluginKeyboard,
402 def _set_plugin_kb(self, pluginIndex):
403 plugin = self._activeKeyboards[pluginIndex]
404 # @todo self._pluginButton.set_label(plugin["pluginName"])
406 for i in xrange(self._pluginKeyboardSpot.count()):
407 self._pluginKeyboardSpot.removeItem(self._pluginKeyboardSpot.itemAt(i))
408 pluginKeyboard = plugin["pluginKeyboard"]
409 self._pluginKeyboardSpot.addItem(pluginKeyboard.toplevel)
411 def _load_history(self):
414 with open(self._user_history, "rU") as f:
416 (part.strip() for part in line.split(" "))
417 for line in f.readlines()
422 self._history.deserialize_stack(serialized)
424 def _save_history(self):
425 serialized = self._history.serialize_stack()
426 with open(self._user_history, "w") as f:
427 for lineData in serialized:
428 line = " ".join(data for data in lineData)
429 f.write("%s\n" % line)
431 @misc_utils.log_exception(_moduleLogger)
432 def _on_entry_direct(self, keys, modifiers):
433 if "shift" in modifiers:
435 self._userEntry.append(keys)
437 @misc_utils.log_exception(_moduleLogger)
438 def _on_push(self, *args):
439 self._history.push_entry()
441 @misc_utils.log_exception(_moduleLogger)
442 def _on_unpush(self, *args):
443 self._historyStore.unpush()
445 @misc_utils.log_exception(_moduleLogger)
446 def _on_entry_backspace(self, *args):
447 self._userEntry.pop()
449 @misc_utils.log_exception(_moduleLogger)
450 def _on_entry_clear(self, *args):
451 self._userEntry.clear()
453 @misc_utils.log_exception(_moduleLogger)
454 def _on_clear_all(self, *args):
455 self._history.clear()
457 @misc_utils.log_exception(_moduleLogger)
458 def _on_close_window(self, checked = True):
463 app = QtGui.QApplication([])
464 handle = Calculator(app)
469 if __name__ == "__main__":
470 logging.basicConfig(level = logging.DEBUG)
472 os.makedirs(constants._data_path_)