Improving exception outputing
[ejpi] / src / gtkhistory.py
1 #!/usr/bin/env python
2
3 """
4 http://www.grigoriev.ru/svgmath/ (MathML->SVG in Python)
5 http://helm.cs.unibo.it/mml-widget/ (MathML widget in C++)
6
7 @bug Columns auto-expand with large items but when they are gone, the columns don't auto-shrink
8 """
9
10
11 import gobject
12 import gtk
13
14
15 import history
16 import operation
17
18
19 class GtkCalcHistory(history.AbstractHistory):
20
21         BUTTON_IDX = 0
22         VIEW_DATA_IDX = 1
23         VIEW_RESULT_IDX = 2
24         DATA_IDX = 3
25         RESULT_IDX = 4
26
27         def __init__(self, view):
28                 super(GtkCalcHistory, self).__init__()
29                 self.__prettyRenderer = operation.render_number()
30                 self._historyView = view
31
32                 # stock-id, display, value
33                 self.__historyStore = gtk.ListStore(
34                         gobject.TYPE_STRING, # stock id for pixbuf
35                         gobject.TYPE_STRING, # view of data
36                         gobject.TYPE_STRING, # view of result
37                         object, # data
38                         object, # result
39                 )
40                 self._historyView.set_model(self.__historyStore)
41
42                 # create the TreeViewColumns to display the data
43                 self.__closeColumn = gtk.TreeViewColumn('')
44                 self._historyView.append_column(self.__closeColumn)
45
46                 self.__historyColumn = gtk.TreeViewColumn('History')
47                 self.__historyColumn.set_sort_column_id(0)
48                 self.__historyColumn.set_expand(True)
49                 self._historyView.append_column(self.__historyColumn)
50
51                 self.__resultColumn = gtk.TreeViewColumn('')
52                 self.__resultColumn.set_sort_column_id(0)
53                 self._historyView.append_column(self.__resultColumn)
54
55                 # create a CellRenderers to render the data
56                 self.__closeCell = gtk.CellRendererPixbuf()
57                 self.__closeColumn.pack_start(self.__closeCell, False)
58                 self.__closeColumn.set_attributes(self.__closeCell, stock_id=0)
59
60                 self.__expressionCell = gtk.CellRendererText()
61                 self.__historyColumn.pack_start(self.__expressionCell, True)
62                 self.__historyColumn.set_attributes(self.__expressionCell, text=1)
63
64                 self.__valueCell = gtk.CellRendererText()
65                 self.__resultColumn.pack_end(self.__valueCell, False)
66                 self.__resultColumn.set_attributes(self.__valueCell, text=2)
67
68                 self._historyView.set_reorderable(True)
69                 self._historyView.connect("row-activated", self._on_close_activated)
70
71         def push(self, node):
72                 simpleNode = node.simplify()
73                 self.__historyStore.prepend([
74                         gtk.STOCK_CLOSE,
75                         operation.render_operation(self.__prettyRenderer, node),
76                         operation.render_operation(self.__prettyRenderer, simpleNode),
77                         node,
78                         simpleNode
79                 ])
80
81         def pop(self):
82                 if len(self.__historyStore) == 0:
83                         raise IndexError("Not enough items in the history for the operation")
84
85                 row = self.__historyStore[0]
86                 data = row[self.DATA_IDX]
87                 del self.__historyStore[0]
88
89                 return data
90
91         def peek(self):
92                 if len(self.__historyStore) == 0:
93                         raise IndexError("Not enough items in the history for the operation")
94                 row = self.__historyStore[0]
95                 data = row[self.DATA_IDX]
96                 return data
97
98         def clear(self):
99                 self.__historyStore.clear()
100
101         def __len__(self):
102                 return len(self.__historyStore)
103
104         def __iter__(self):
105                 for row in iter(self.__historyStore):
106                         data = row[self.DATA_IDX]
107                         yield data
108
109         def _on_close_activated(self, treeView, path, viewColumn):
110                 if viewColumn is self.__closeColumn:
111                         del self.__historyStore[path[0]]
112                 elif viewColumn is self.__resultColumn:
113                         row = self.__historyStore[path[0]]
114                         data = row[self.RESULT_IDX]
115                         self.push(data)
116                 elif viewColumn is self.__historyColumn:
117                         row = self.__historyStore[path[0]]
118                         data = row[self.DATA_IDX]
119                         self.push(data)
120                 else:
121                         assert False