4 @todo Add preference file
5 @li enable/disable plugins
9 @todo Expand operations to support
10 @li mathml then to cairo?
12 @todo Expanded copy/paste (Unusure how far to go)
13 @li Copy formula, value, serialized, mathml, latex?
14 @li Paste serialized, value?
15 @bug Has the same Maemo tab color bug as DialCentral
17 Some useful things on Maemo
18 @li http://maemo.org/api_refs/4.1/libosso-2.16-1/group__Statesave.html
19 @li http://maemo.org/api_refs/4.1/libosso-2.16-1/group__Autosave.html
23 from __future__ import with_statement
41 from libraries import gtkpie
42 from libraries import gtkpieboard
50 _moduleLogger = logging.getLogger("ejpi_glade")
52 PLUGIN_SEARCH_PATHS = [
53 os.path.join(os.path.dirname(__file__), "plugins/"),
57 class ValueEntry(object):
59 def __init__(self, widget):
60 self.__widget = widget
61 self.__actualEntryDisplay = ""
64 value = self.__actualEntryDisplay.strip()
66 0 < value.find(whitespace)
67 for whitespace in string.whitespace
70 raise ValueError('Invalid input "%s"' % value)
73 def set_value(self, value):
76 0 < value.find(whitespace)
77 for whitespace in string.whitespace
79 raise ValueError('Invalid input "%s"' % value)
80 self.__actualEntryDisplay = value
81 self.__widget.set_text(value)
83 def append(self, value):
86 0 < value.find(whitespace)
87 for whitespace in string.whitespace
89 raise ValueError('Invalid input "%s"' % value)
90 self.set_value(self.get_value() + value)
93 value = self.get_value()[0:-1]
99 value = property(get_value, set_value, clear)
102 class Calculator(object):
105 '/usr/lib/ejpi/ejpi.glade',
106 os.path.join(os.path.dirname(__file__), "ejpi.glade"),
107 os.path.join(os.path.dirname(__file__), "../lib/ejpi.glade"),
110 _plugin_search_paths = [
111 "/usr/lib/ejpi/plugins/",
112 os.path.join(os.path.dirname(__file__), "plugins/"),
115 _user_data = os.path.expanduser("~/.%s/" % constants.__app_name__)
116 _user_settings = "%s/settings.ini" % _user_data
117 _user_history = "%s/history.stack" % _user_data
120 self.__constantPlugins = plugin_utils.ConstantPluginManager()
121 self.__constantPlugins.add_path(*self._plugin_search_paths)
122 for pluginName in ["Builtin", "Trigonometry", "Computer", "Alphabet"]:
124 pluginId = self.__constantPlugins.lookup_plugin(pluginName)
125 self.__constantPlugins.enable_plugin(pluginId)
127 warnings.warn("Failed to load plugin %s" % pluginName)
129 self.__operatorPlugins = plugin_utils.OperatorPluginManager()
130 self.__operatorPlugins.add_path(*self._plugin_search_paths)
131 for pluginName in ["Builtin", "Trigonometry", "Computer", "Alphabet"]:
133 pluginId = self.__operatorPlugins.lookup_plugin(pluginName)
134 self.__operatorPlugins.enable_plugin(pluginId)
136 warnings.warn("Failed to load plugin %s" % pluginName)
138 self.__keyboardPlugins = plugin_utils.KeyboardPluginManager()
139 self.__keyboardPlugins.add_path(*self._plugin_search_paths)
140 self.__activeKeyboards = {}
142 for path in self._glade_files:
143 if os.path.isfile(path):
144 self._widgetTree = gtk.glade.XML(path)
147 self.display_error_message("Cannot find ejpi.glade")
151 os.makedirs(self._user_data)
156 self._clipboard = gtk.clipboard_get()
157 self._window = self._widgetTree.get_widget("mainWindow")
160 self._isFullScreen = False
161 if hildon is not None:
162 self._app = hildon.Program()
163 oldWindow = self._window
164 self._window = hildon.Window()
165 oldWindow.get_child().reparent(self._window)
166 self._app.add_window(self._window)
167 hildon.hildon_helper_set_thumb_scrollbar(self._widgetTree.get_widget('scrollingHistory'), True)
169 gtkMenu = self._widgetTree.get_widget("mainMenubar")
171 for child in gtkMenu.get_children():
173 self._window.set_menu(menu)
176 self._window.connect("key-press-event", self._on_key_press)
177 self._window.connect("window-state-event", self._on_window_state_change)
179 pass # warnings.warn("No Hildon", UserWarning, 2)
181 self.__errorDisplay = gtk_toolbox.ErrorDisplay(self._widgetTree)
182 self.__userEntry = ValueEntry(self._widgetTree.get_widget("entryView"))
183 self.__stackView = self._widgetTree.get_widget("historyView")
185 self.__historyStore = gtkhistory.GtkCalcHistory(self.__stackView)
186 self.__history = history.RpnCalcHistory(
188 self.__userEntry, self.__errorDisplay,
189 self.__constantPlugins.constants, self.__operatorPlugins.operators
191 self.__load_history()
193 self.__sliceStyle = gtkpie.generate_pie_style(gtk.Button())
194 self.__handler = gtkpieboard.KeyboardHandler(self._on_entry_direct)
195 self.__handler.register_command_handler("push", self._on_push)
196 self.__handler.register_command_handler("unpush", self._on_unpush)
197 self.__handler.register_command_handler("backspace", self._on_entry_backspace)
198 self.__handler.register_command_handler("clear", self._on_entry_clear)
200 builtinKeyboardId = self.__keyboardPlugins.lookup_plugin("Builtin")
201 self.__keyboardPlugins.enable_plugin(builtinKeyboardId)
202 self.__builtinPlugin = self.__keyboardPlugins.keyboards["Builtin"].construct_keyboard()
203 self.__builtinKeyboard = self.__builtinPlugin.setup(self.__history, self.__sliceStyle, self.__handler)
204 self._widgetTree.get_widget("functionLayout").pack_start(self.__builtinKeyboard)
205 self.enable_plugin(self.__keyboardPlugins.lookup_plugin("Trigonometry"))
206 self.enable_plugin(self.__keyboardPlugins.lookup_plugin("Computer"))
207 self.enable_plugin(self.__keyboardPlugins.lookup_plugin("Alphabet"))
210 "on_calculator_quit": self._on_close,
211 "on_paste": self._on_paste,
212 "on_clear_history": self._on_clear_all,
213 "on_about": self._on_about_activate,
215 self._widgetTree.signal_autoconnect(callbackMapping)
216 self._widgetTree.get_widget("copyMenuItem").connect("activate", self._on_copy)
217 self._widgetTree.get_widget("copyEquationMenuItem").connect("activate", self._on_copy_equation)
220 self._window.set_title("%s" % constants.__pretty_app_name__)
221 self._window.connect("destroy", self._on_close)
222 self._window.show_all()
230 self._osso = osso.Context(constants.__app_name__, constants.__version__, False)
231 device = osso.DeviceState(self._osso)
232 device.set_device_state_callback(self._on_device_state_change, 0)
234 pass # warnings.warn("No OSSO", UserWarning, 2)
236 def display_error_message(self, msg):
237 error_dialog = gtk.MessageDialog(None, 0, gtk.MESSAGE_ERROR, gtk.BUTTONS_CLOSE, msg)
239 def close(dialog, response, editor):
240 editor.about_dialog = None
242 error_dialog.connect("response", close, self)
245 def enable_plugin(self, pluginId):
246 self.__keyboardPlugins.enable_plugin(pluginId)
247 pluginData = self.__keyboardPlugins.plugin_info(pluginId)
248 pluginName = pluginData[0]
249 plugin = self.__keyboardPlugins.keyboards[pluginName].construct_keyboard()
250 pluginKeyboard = plugin.setup(self.__history, self.__sliceStyle, self.__handler)
252 keyboardTabs = self._widgetTree.get_widget("pluginKeyboards")
253 keyboardTabs.append_page(pluginKeyboard, gtk.Label(pluginName))
254 keyboardPageNum = keyboardTabs.page_num(pluginKeyboard)
255 assert keyboardPageNum not in self.__activeKeyboards
256 self.__activeKeyboards[keyboardPageNum] = {
257 "pluginName": pluginName,
259 "pluginKeyboard": pluginKeyboard,
262 def __load_history(self):
265 with open(self._user_history, "rU") as f:
267 (part.strip() for part in line.split(" "))
268 for line in f.readlines()
273 self.__history.deserialize_stack(serialized)
275 def __save_history(self):
276 serialized = self.__history.serialize_stack()
277 with open(self._user_history, "w") as f:
278 for lineData in serialized:
279 line = " ".join(data for data in lineData)
280 f.write("%s\n" % line)
282 def _on_device_state_change(self, shutdown, save_unsaved_data, memory_low, system_inactivity, message, userData):
284 For system_inactivity, we have no background tasks to pause
286 @note Hildon specific
291 if save_unsaved_data or shutdown:
292 self.__save_history()
294 def _on_window_state_change(self, widget, event, *args):
296 @note Hildon specific
298 if event.new_window_state & gtk.gdk.WINDOW_STATE_FULLSCREEN:
299 self._isFullScreen = True
301 self._isFullScreen = False
303 def _on_close(self, *args, **kwds):
304 if self._osso is not None:
308 self.__save_history()
312 def _on_copy(self, *args):
314 equationNode = self.__history.history.peek()
315 result = str(equationNode.evaluate())
316 self._clipboard.set_text(result)
317 except StandardError, e:
318 self.__errorDisplay.push_exception()
320 def _on_copy_equation(self, *args):
322 equationNode = self.__history.history.peek()
323 equation = str(equationNode)
324 self._clipboard.set_text(equation)
325 except StandardError, e:
326 self.__errorDisplay.push_exception()
328 def _on_paste(self, *args):
329 contents = self._clipboard.wait_for_text()
330 self.__userEntry.append(contents)
332 def _on_key_press(self, widget, event, *args):
334 @note Hildon specific
336 if event.keyval == gtk.keysyms.F6:
337 if self._isFullScreen:
338 self._window.unfullscreen()
340 self._window.fullscreen()
342 def _on_push(self, *args):
343 self.__history.push_entry()
345 def _on_unpush(self, *args):
346 self.__historyStore.unpush()
348 def _on_entry_direct(self, keys, modifiers):
349 if "shift" in modifiers:
351 self.__userEntry.append(keys)
353 def _on_entry_backspace(self, *args):
354 self.__userEntry.pop()
356 def _on_entry_clear(self, *args):
357 self.__userEntry.clear()
359 def _on_clear_all(self, *args):
360 self.__history.clear()
362 def _on_about_activate(self, *args):
363 dlg = gtk.AboutDialog()
364 dlg.set_name(constants.__pretty_app_name__)
365 dlg.set_version(constants.__version__)
366 dlg.set_copyright("Copyright 2008 - LGPL")
368 ejpi A Touch Screen Optimized RPN Calculator for Maemo and Linux.
370 RPN: Stack based math, its fun
371 Buttons: Try both pressing and hold/drag
372 History: Try dragging things around, deleting them, etc
374 dlg.set_website("http://ejpi.garage.maemo.org")
375 dlg.set_authors(["Ed Page"])
383 failureCount, testCount = doctest.testmod()
385 print "Tests Successful"
391 def run_calculator():
392 gtk.gdk.threads_init()
394 gtkpie.IMAGES.add_path(os.path.join(os.path.dirname(__file__), "libraries/images"), )
395 if hildon is not None:
396 gtk.set_application_name(constants.__pretty_app_name__)
397 handle = Calculator()
401 class DummyOptions(object):
407 if __name__ == "__main__":
408 logging.basicConfig(level=logging.DEBUG)
409 if len(sys.argv) > 1:
415 if optparse is not None:
416 parser = optparse.OptionParser()
417 parser.add_option("-t", "--test", action="store_true", dest="test", help="Run tests")
418 (commandOptions, commandArgs) = parser.parse_args()
420 commandOptions = DummyOptions()
423 if commandOptions.test: