Kaydet (Commit) 3669242a authored tarafından Guido van Rossum's avatar Guido van Rossum

Major breakthrough in selection -- drag-select multiple cells now

works.  Also row and column selection works (sort of).  The DEL
key deletes the selected rectangle.  sys.argv[1] used by test_gui().
üst 4f759d85
...@@ -401,7 +401,10 @@ class FormulaCell(BaseCell): ...@@ -401,7 +401,10 @@ class FormulaCell(BaseCell):
def recalc(self, rexec): def recalc(self, rexec):
if self.value is None: if self.value is None:
try: try:
self.value = rexec.r_eval(self.translated) # A hack to evaluate expressions using true division
rexec.r_exec("from __future__ import division\n" +
"__value__ = eval(%s)" % repr(self.translated))
self.value = rexec.r_eval("__value__")
except: except:
exc = sys.exc_info()[0] exc = sys.exc_info()[0]
if hasattr(exc, "__name__"): if hasattr(exc, "__name__"):
...@@ -490,7 +493,6 @@ class SheetGUI: ...@@ -490,7 +493,6 @@ class SheetGUI:
TO DO: TO DO:
- clear multiple cells - clear multiple cells
- Select rows or columns
- Insert, clear, remove rows or columns - Insert, clear, remove rows or columns
- Show new contents while typing - Show new contents while typing
- Scroll bars - Scroll bars
...@@ -535,6 +537,7 @@ class SheetGUI: ...@@ -535,6 +537,7 @@ class SheetGUI:
self.entry.bind("<Shift-Return>", self.shift_return_event) self.entry.bind("<Shift-Return>", self.shift_return_event)
self.entry.bind("<Tab>", self.tab_event) self.entry.bind("<Tab>", self.tab_event)
self.entry.bind("<Shift-Tab>", self.shift_tab_event) self.entry.bind("<Shift-Tab>", self.shift_tab_event)
self.entry.bind("<Delete>", self.delete_event)
# Now create the cell grid # Now create the cell grid
self.makegrid(rows, columns) self.makegrid(rows, columns)
# Select the top-left cell # Select the top-left cell
...@@ -544,11 +547,22 @@ class SheetGUI: ...@@ -544,11 +547,22 @@ class SheetGUI:
# Copy the sheet cells to the GUI cells # Copy the sheet cells to the GUI cells
self.sync() self.sync()
def delete_event(self, event):
if self.cornerxy != self.currentxy and self.cornerxy is not None:
self.sheet.clearcells(*(self.currentxy + self.cornerxy))
else:
self.sheet.clearcell(*self.currentxy)
self.sync()
self.entry.delete(0, 'end')
return "break"
def makegrid(self, rows, columns): def makegrid(self, rows, columns):
"""Helper to create the grid of GUI cells. """Helper to create the grid of GUI cells.
The edge (x==0 or y==0) is filled with labels; the rest is real cells. The edge (x==0 or y==0) is filled with labels; the rest is real cells.
""" """
self.rows = rows
self.columns = columns
self.gridcells = {} self.gridcells = {}
# Create the top row of labels # Create the top row of labels
for x in range(1, columns+1): for x in range(1, columns+1):
...@@ -556,11 +570,23 @@ class SheetGUI: ...@@ -556,11 +570,23 @@ class SheetGUI:
cell = Tk.Label(self.cellgrid, text=colnum2name(x), relief='raised') cell = Tk.Label(self.cellgrid, text=colnum2name(x), relief='raised')
cell.grid_configure(column=x, row=0, sticky='WE') cell.grid_configure(column=x, row=0, sticky='WE')
self.gridcells[x, 0] = cell self.gridcells[x, 0] = cell
cell.__x = x
cell.__y = 0
cell.bind("<ButtonPress-1>", self.selectcolumn)
cell.bind("<B1-Motion>", self.extendcolumn)
cell.bind("<ButtonRelease-1>", self.extendcolumn)
cell.bind("<Shift-Button-1>", self.extendcolumn)
# Create the leftmost column of labels # Create the leftmost column of labels
for y in range(1, rows+1): for y in range(1, rows+1):
cell = Tk.Label(self.cellgrid, text=str(y), relief='raised') cell = Tk.Label(self.cellgrid, text=str(y), relief='raised')
cell.grid_configure(column=0, row=y, sticky='WE') cell.grid_configure(column=0, row=y, sticky='WE')
self.gridcells[0, y] = cell self.gridcells[0, y] = cell
cell.__x = 0
cell.__y = y
cell.bind("<ButtonPress-1>", self.selectrow)
cell.bind("<B1-Motion>", self.extendrow)
cell.bind("<ButtonRelease-1>", self.extendrow)
cell.bind("<Shift-Button-1>", self.extendrow)
# Create the real cells # Create the real cells
for x in range(1, columns+1): for x in range(1, columns+1):
for y in range(1, rows+1): for y in range(1, rows+1):
...@@ -568,12 +594,56 @@ class SheetGUI: ...@@ -568,12 +594,56 @@ class SheetGUI:
bg='white', fg='black') bg='white', fg='black')
cell.grid_configure(column=x, row=y, sticky='NWSE') cell.grid_configure(column=x, row=y, sticky='NWSE')
self.gridcells[x, y] = cell self.gridcells[x, y] = cell
def helper(event, self=self, x=x, y=y): cell.__x = x
self.setcurrent(x, y) cell.__y = y
cell.bind("<Button-1>", helper) # Bind mouse events
def shelper(event, self=self, x=x, y=y): cell.bind("<ButtonPress-1>", self.press)
self.setcorner(x, y) cell.bind("<B1-Motion>", self.motion)
cell.bind("<Shift-Button-1>", shelper) cell.bind("<ButtonRelease-1>", self.release)
cell.bind("<Shift-Button-1>", self.release)
def selectcolumn(self, event):
x, y = self.whichxy(event)
self.setcurrent(x, 1)
self.setcorner(x, self.rows)
def extendcolumn(self, event):
x, y = self.whichxy(event)
if x > 0:
self.setcurrent(self.currentxy[0], 1)
self.setcorner(x, self.rows)
def selectrow(self, event):
x, y = self.whichxy(event)
self.setcurrent(1, y)
self.setcorner(self.columns, y)
def extendrow(self, event):
x, y = self.whichxy(event)
if y > 0:
self.setcurrent(1, self.currentxy[1])
self.setcorner(self.columns, y)
def press(self, event):
x, y = self.whichxy(event)
if x > 0 and y > 0:
self.setcurrent(x, y)
def motion(self, event):
x, y = self.whichxy(event)
if x > 0 and y > 0:
self.setcorner(x, y)
release = motion
def whichxy(self, event):
w = self.cellgrid.winfo_containing(event.x_root, event.y_root)
if w is not None and isinstance(w, Tk.Label):
try:
return w.__x, w.__y
except AttributeError:
pass
return 0, 0
def save(self): def save(self):
self.sheet.save(self.filename) self.sheet.save(self.filename)
...@@ -600,7 +670,7 @@ class SheetGUI: ...@@ -600,7 +670,7 @@ class SheetGUI:
self.cornerxy = None self.cornerxy = None
gridcell = self.gridcells.get(self.currentxy) gridcell = self.gridcells.get(self.currentxy)
if gridcell is not None: if gridcell is not None:
gridcell['bg'] = 'lightBlue' gridcell['bg'] = 'yellow'
def setcorner(self, x, y): def setcorner(self, x, y):
if self.currentxy is None or self.currentxy == (x, y): if self.currentxy is None or self.currentxy == (x, y):
...@@ -619,6 +689,9 @@ class SheetGUI: ...@@ -619,6 +689,9 @@ class SheetGUI:
gridcell = self.gridcells.get((x, y)) gridcell = self.gridcells.get((x, y))
if gridcell is not None: if gridcell is not None:
gridcell['bg'] = 'lightBlue' gridcell['bg'] = 'lightBlue'
gridcell = self.gridcells.get(self.currentxy)
if gridcell is not None:
gridcell['bg'] = 'yellow'
name1 = cellname(*self.currentxy) name1 = cellname(*self.currentxy)
name2 = cellname(*self.cornerxy) name2 = cellname(*self.cornerxy)
self.beacon['text'] = "%s:%s" % (name1, name2) self.beacon['text'] = "%s:%s" % (name1, name2)
...@@ -732,7 +805,11 @@ def test_basic(): ...@@ -732,7 +805,11 @@ def test_basic():
def test_gui(): def test_gui():
"GUI test." "GUI test."
g = SheetGUI() if sys.argv[1:]:
filename = sys.argv[1]
else:
filename = "sheet1.xml"
g = SheetGUI(filename)
g.root.mainloop() g.root.mainloop()
if __name__ == '__main__': if __name__ == '__main__':
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment