Log In
New Account
  
Home My Page Project Cloud Code Snippets Project Openings e^(j pi)+1=0
Summary SCM
1 #!/usr/bin/env python
3 """
4 http://www.grigoriev.ru/svgmath/ (MathML->SVG in Python)
5 http://helm.cs.unibo.it/mml-widget/ (MathML widget in C++)
6 """
8 import logging
10 import gobject
11 import pango
12 import gtk
15 import gtk_toolbox
16 import hildonize
17 import history
18 import operation
21 _moduleLogger = logging.getLogger("gtkhistory")
24 class GtkCalcHistory(history.AbstractHistory):
26         BUTTON_IDX = 0
27         VIEW_DATA_IDX = 1
28         VIEW_RESULT_IDX = 2
29         DATA_IDX = 3
30         RESULT_IDX = 4
32         def __init__(self, view):
33                 super(GtkCalcHistory, self).__init__()
34                 self.__prettyRenderer = operation.render_number()
35                 self._historyView = view
37                 # stock-id, display, value
38                 self.__historyStore = gtk.ListStore(
39                         gobject.TYPE_STRING, # stock id for pixbuf
40                         gobject.TYPE_STRING, # view of data
41                         gobject.TYPE_STRING, # view of result
42                         object, # data
43                         object, # result
44                 )
45                 self._historyView.set_model(self.__historyStore)
47                 # create the TreeViewColumns to display the data
48                 self.__closeColumn = gtk.TreeViewColumn('')
49                 self._historyView.append_column(self.__closeColumn)
51                 self.__historyColumn = gtk.TreeViewColumn('History')
52                 self.__historyColumn.set_sort_column_id(0)
53                 self.__historyColumn.set_expand(True)
54                 self._historyView.append_column(self.__historyColumn)
56                 self.__resultColumn = gtk.TreeViewColumn('')
57                 self.__resultColumn.set_sort_column_id(0)
58                 self._historyView.append_column(self.__resultColumn)
60                 # create a CellRenderers to render the data
61                 self.__closeCell = gtk.CellRendererPixbuf()
62                 hildonize.set_pix_cell_thumb_selectable(self.__closeCell)
63                 self.__closeColumn.pack_start(self.__closeCell, False)
64                 self.__closeColumn.set_attributes(self.__closeCell, stock_id=0)
66                 self.__expressionCell = gtk.CellRendererText()
67                 self.__expressionCell.set_property("ellipsize", pango.ELLIPSIZE_MIDDLE)
68                 self.__historyColumn.pack_start(self.__expressionCell, True)
69                 self.__historyColumn.set_attributes(self.__expressionCell, text=1)
71                 self.__valueCell = gtk.CellRendererText()
72                 self.__valueCell.set_property("ellipsize", pango.ELLIPSIZE_NONE)
73                 self.__resultColumn.pack_end(self.__valueCell, False)
74                 self.__resultColumn.set_attributes(self.__valueCell, text=2)
76                 self._historyView.set_reorderable(True)
77                 self._historyView.get_selection().set_mode(gtk.SELECTION_SINGLE)
78                 self._historyView.connect("row-activated", self._on_close_activated)
80         def push(self, node):
81                 simpleNode = node.simplify()
82                 self.__historyStore.append([
83                         gtk.STOCK_CLOSE,
84                         operation.render_operation(self.__prettyRenderer, node),
85                         operation.render_operation(self.__prettyRenderer, simpleNode),
86                         node,
87                         simpleNode
88                 ])
89                 selection = self._historyView.get_selection()
90                 selectionPath = (len(self.__historyStore)-1, )
91                 selection.select_path(selectionPath)
92                 self._historyView.scroll_to_cell(selectionPath)
94         def pop(self):
95                 if len(self.__historyStore) == 0:
96                         raise IndexError("Not enough items in the history for the operation")
98                 row = self.__historyStore[-1]
99                 data = row[self.DATA_IDX]
100                 del self.__historyStore[-1]
102                 return data
104         def peek(self):
105                 if len(self.__historyStore) == 0:
106                         raise IndexError("Not enough items in the history for the operation")
107                 row = self.__historyStore[-1]
108                 data = row[self.DATA_IDX]
109                 return data
111         def clear(self):
112                 self.__historyStore.clear()
114         def __len__(self):
115                 return len(self.__historyStore)
117         def __iter__(self):
118                 for row in iter(self.__historyStore):
119                         data = row[self.DATA_IDX]
120                         yield data
122         @gtk_toolbox.log_exception(_moduleLogger)
123         def _on_close_activated(self, treeView, path, viewColumn):
124                 if viewColumn is self.__closeColumn:
125                         del self.__historyStore[path[0]]
126                 elif viewColumn is self.__resultColumn:
127                         row = self.__historyStore[path[0]]
128                         data = row[self.RESULT_IDX]
129                         self.push(data)
130                 elif viewColumn is self.__historyColumn:
131                         row = self.__historyStore[path[0]]
132                         data = row[self.DATA_IDX]
133                         self.push(data)
134                 else:
135                         assert False

Terms of Use    Privacy Policy    Contribution Guidelines    Feedback

Powered By GForge Collaborative Development Environment