2012-03-20 01:00:53 -04:00
|
|
|
import curses
|
2013-10-21 19:38:21 -04:00
|
|
|
import sys
|
2012-03-20 01:00:53 -04:00
|
|
|
|
2013-10-25 17:25:07 -04:00
|
|
|
|
|
|
|
class TextEntry:
|
|
|
|
|
|
|
|
"""A part of a window that handles text entry.
|
|
|
|
Properties:
|
|
|
|
value holds the string that was entered
|
|
|
|
|
|
|
|
Public Methods:
|
|
|
|
set_geom(row,column,width) Sets the geometry in the window
|
|
|
|
set_value(string) Set the value and redraw
|
|
|
|
gain_focus() Gives it focus, moving cursor and changing the drawing
|
|
|
|
lose_focus() Takes focus, moving cursor to start, changing drawing
|
|
|
|
handle_input(ch) Pass this the ncurses key, and it manages input
|
|
|
|
redraw() Redraw the text entry (should never need to do this
|
|
|
|
"""
|
|
|
|
|
|
|
|
# Public
|
|
|
|
value = "" # Use the set_value function to set, but retrieve with value
|
|
|
|
|
|
|
|
# Should be Private
|
|
|
|
cursor = 0
|
|
|
|
start = 0
|
|
|
|
focus = False
|
|
|
|
x = 0
|
|
|
|
y = 0
|
|
|
|
width = 10
|
|
|
|
|
|
|
|
# Public methods
|
|
|
|
def __init__(self, parent_window, value=""):
|
|
|
|
self.w = parent_window
|
|
|
|
self.value = value
|
|
|
|
|
|
|
|
def set_geom(self,y,x,width):
|
|
|
|
self.x = x
|
|
|
|
self.y = y
|
|
|
|
self.width = width
|
|
|
|
|
|
|
|
def set_value(self,v):
|
|
|
|
self.value=v
|
|
|
|
self.cursor=len(v)
|
|
|
|
self.redraw()
|
|
|
|
|
|
|
|
def gain_focus(self):
|
|
|
|
#sys.stderr.write('I have focus!\n')
|
|
|
|
self.focus = True
|
|
|
|
self._mv_cursor(+len(self.value))
|
|
|
|
self.start = max(0,self.cursor-self.width)
|
|
|
|
self.redraw()
|
|
|
|
|
|
|
|
def lose_focus(self):
|
|
|
|
self.focus = False
|
|
|
|
self.cursor = 0
|
|
|
|
self.start = 0
|
|
|
|
self.redraw()
|
|
|
|
|
|
|
|
def handle_input(self,ch):
|
|
|
|
if ch==curses.KEY_LEFT:
|
|
|
|
self._mv_cursor(-1)
|
|
|
|
elif ch==curses.KEY_HOME:
|
|
|
|
self._set_cursor(0)
|
|
|
|
elif ch==curses.KEY_RIGHT:
|
|
|
|
self._mv_cursor(+1)
|
|
|
|
elif ch==curses.KEY_END:
|
|
|
|
self._set_cursor(len(self.value))
|
|
|
|
elif ch>=32 and ch<=126:
|
|
|
|
self._insert(curses.keyname(ch).decode('utf-8'))
|
|
|
|
elif ch==curses.KEY_BACKSPACE:
|
|
|
|
self._backspace()
|
|
|
|
elif ch==curses.KEY_DC:
|
|
|
|
self._delete()
|
|
|
|
|
|
|
|
def redraw(self):
|
|
|
|
self.w.addnstr(self.y,self.x, self.value[self.start:]+" "*self.width, self.width)
|
|
|
|
if self.focus:
|
|
|
|
self.w.chgat(self.y, self.x, self.width, curses.A_UNDERLINE)
|
|
|
|
curses.curs_set(1)
|
|
|
|
|
|
|
|
# Private functions
|
|
|
|
def _mv_cursor(self,delta):
|
|
|
|
self._set_cursor(self.cursor + delta)
|
|
|
|
|
|
|
|
def _set_cursor(self, new_c):
|
|
|
|
self.cursor = max(0, min(len(self.value), new_c))
|
|
|
|
self.start = max(0,self.cursor-self.width+1)
|
|
|
|
self.redraw()
|
|
|
|
# Place the drawn cursor in the correct spot
|
|
|
|
col = self.x + self.cursor - self.start
|
|
|
|
self.w.move(self.y,col)
|
|
|
|
|
|
|
|
def _insert(self,ch):
|
|
|
|
c = self.cursor
|
|
|
|
self.value = self.value[:c] +ch+ self.value[c:]
|
|
|
|
self._mv_cursor(+1)
|
|
|
|
|
|
|
|
def _backspace(self):
|
|
|
|
if self.cursor>0:
|
|
|
|
c = self.cursor
|
|
|
|
self.value=self.value[:c-1] + self.value[c:]
|
|
|
|
self._mv_cursor(-1)
|
|
|
|
|
|
|
|
def _delete(self):
|
|
|
|
c = self.cursor
|
|
|
|
self.value = self.value[:c] + self.value[c+1:]
|
|
|
|
self._mv_cursor(0)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class FormWindow:
|
|
|
|
|
|
|
|
"""General class for a Form Window.
|
|
|
|
|
|
|
|
To use, make the window for it, call the constructor, then call event_loop.
|
|
|
|
"""
|
|
|
|
|
|
|
|
# Private variables
|
2012-03-20 01:00:53 -04:00
|
|
|
mx = my = 0
|
|
|
|
hl = 0
|
2012-03-20 01:47:28 -04:00
|
|
|
bt = -1
|
2012-03-20 01:00:53 -04:00
|
|
|
left = 0
|
|
|
|
top = 2
|
2012-03-20 01:47:28 -04:00
|
|
|
row = 2
|
2012-03-24 14:06:02 -04:00
|
|
|
caption = "Form"
|
|
|
|
blabel = "Done"
|
|
|
|
labels = ["label1"]
|
2012-03-20 01:00:53 -04:00
|
|
|
|
2012-03-27 15:24:29 -04:00
|
|
|
commands = [('pU', 'top'),('pD', 'bottom'),('Es', 'cancel')]
|
|
|
|
|
2012-03-20 18:12:19 -04:00
|
|
|
|
2013-10-25 17:25:07 -04:00
|
|
|
# Public functions
|
2012-03-27 15:24:29 -04:00
|
|
|
def __init__(self,window,helpbar,book={}):
|
2012-03-20 01:00:53 -04:00
|
|
|
self.w = window
|
|
|
|
self.w.resize(len(self.labels)+6,50)
|
2012-03-27 15:24:29 -04:00
|
|
|
self.hb = helpbar
|
2013-10-25 17:25:07 -04:00
|
|
|
self._make_entries()
|
|
|
|
self._update_geometry()
|
|
|
|
self._set_entries(book)
|
|
|
|
|
|
|
|
def clear(self):
|
|
|
|
self.w.erase()
|
|
|
|
self.w.refresh()
|
|
|
|
|
|
|
|
def event_loop(self):
|
|
|
|
self.w.keypad(1)
|
|
|
|
self.refresh()
|
|
|
|
self.hl=0;
|
|
|
|
self.entries[self.hl].gain_focus()
|
|
|
|
|
|
|
|
ch = self.w.getch()
|
|
|
|
while ch != 27:
|
|
|
|
#sys.stderr.write(curses.keyname(ch).decode('utf-8'))
|
|
|
|
self.handle_input(ch)
|
|
|
|
if ch==10 or ch==curses.KEY_ENTER:
|
|
|
|
if self.bt==0:
|
|
|
|
return {}
|
|
|
|
elif self.bt==1:
|
|
|
|
return self.return_values()
|
|
|
|
else:
|
|
|
|
self._mv_focus(+1)
|
|
|
|
self.w.refresh()
|
|
|
|
ch = self.w.getch()
|
|
|
|
curses.curs_set(0)
|
|
|
|
return {}
|
2012-03-20 01:00:53 -04:00
|
|
|
|
2013-10-25 17:25:07 -04:00
|
|
|
def _make_entries(self):
|
|
|
|
self.entries = []
|
|
|
|
for e in range(len(self.labels)):
|
|
|
|
self.entries.append(TextEntry(self.w))
|
|
|
|
|
|
|
|
def _update_geometry(self):
|
2012-03-20 01:00:53 -04:00
|
|
|
(self.my, self.mx) = self.w.getmaxyx()
|
2012-03-20 12:52:39 -04:00
|
|
|
self.left=0
|
2012-03-20 01:00:53 -04:00
|
|
|
for l in self.labels:
|
|
|
|
self.left = max(len(l),self.left)
|
2012-03-20 12:52:39 -04:00
|
|
|
self.left += 4
|
2013-10-25 17:25:07 -04:00
|
|
|
width = self.mx-self.left-2
|
2012-03-20 01:00:53 -04:00
|
|
|
self.top = 2
|
2013-10-25 17:25:07 -04:00
|
|
|
for r in range(len(self.entries)):
|
|
|
|
self.entries[r].set_geom(r+self.top, self.left, width)
|
2012-03-20 01:00:53 -04:00
|
|
|
# next, the buttons
|
2013-10-25 17:25:07 -04:00
|
|
|
self.brow = self.top+len(self.labels)+1
|
2012-03-20 01:00:53 -04:00
|
|
|
self.bcol = [self.mx-len(self.blabel)-14, self.mx-len(self.blabel)-4]
|
|
|
|
self.bwidth = [8,len(self.blabel)+2]
|
|
|
|
|
2013-10-25 17:25:07 -04:00
|
|
|
def _set_entries(self,book):
|
|
|
|
e = 0
|
2012-03-20 01:00:53 -04:00
|
|
|
for l in self.labels:
|
2013-10-25 17:25:07 -04:00
|
|
|
#sys.stderr.write('updating label: '+l+'\n')
|
2012-03-20 01:00:53 -04:00
|
|
|
if l.lower() in book:
|
2013-10-25 17:25:07 -04:00
|
|
|
#sys.stderr.write(' '+l+' found\n')
|
|
|
|
self.entries[e].value = str(book[l.lower()])
|
2012-03-20 01:00:53 -04:00
|
|
|
else:
|
2013-10-25 17:25:07 -04:00
|
|
|
#sys.stderr.write(' '+l+' notfound\n')
|
|
|
|
self.entries[e].value = ""
|
|
|
|
e += 1
|
2012-03-20 01:00:53 -04:00
|
|
|
|
2013-10-25 17:25:07 -04:00
|
|
|
def redraw(self):
|
2012-03-20 01:47:28 -04:00
|
|
|
self.w.box()
|
2013-10-21 19:38:21 -04:00
|
|
|
self.w.addstr(0,(self.mx-len(self.caption))//2,self.caption)
|
2013-10-25 17:25:07 -04:00
|
|
|
r=0
|
2012-03-20 12:52:39 -04:00
|
|
|
for l in self.labels:
|
2012-03-20 01:47:28 -04:00
|
|
|
c = self.left-len(l)-2
|
2013-10-25 17:25:07 -04:00
|
|
|
self.w.addstr(r+self.top,c,l+":")
|
|
|
|
self.entries[r].redraw()
|
2012-03-20 01:47:28 -04:00
|
|
|
r+=1
|
2013-10-25 17:25:07 -04:00
|
|
|
self.w.addstr(self.brow,self.bcol[0], "<cancel> <"+self.blabel+">")
|
2012-03-20 01:47:28 -04:00
|
|
|
self.w.refresh()
|
2012-03-20 01:00:53 -04:00
|
|
|
|
2013-10-25 17:25:07 -04:00
|
|
|
def refresh(self):
|
|
|
|
self.hb.commands = self.commands
|
|
|
|
self.hb.refresh()
|
|
|
|
self._update_geometry()
|
|
|
|
self.redraw()
|
2012-03-20 12:52:39 -04:00
|
|
|
|
2013-10-25 17:25:07 -04:00
|
|
|
def _highlight_button(self):
|
|
|
|
self.w.chgat(self.brow, self.bcol[self.bt], self.bwidth[self.bt], curses.A_REVERSE)
|
|
|
|
curses.curs_set(0)
|
2012-03-20 01:00:53 -04:00
|
|
|
|
2013-10-25 17:25:07 -04:00
|
|
|
def _unhighlight_button(self):
|
|
|
|
self.w.chgat(self.brow,1,self.mx-2,curses.A_NORMAL)
|
2012-03-20 01:00:53 -04:00
|
|
|
|
2013-10-25 17:25:07 -04:00
|
|
|
def _mv_focus(self,delta):
|
|
|
|
if self.bt==-1:
|
|
|
|
self.entries[self.hl].lose_focus()
|
|
|
|
else:
|
|
|
|
self._unhighlight_button()
|
2012-03-20 01:00:53 -04:00
|
|
|
new = self.hl+delta
|
2013-10-25 17:25:07 -04:00
|
|
|
new = max(0, min(len(self.labels), new)) # the extra is for the buttons
|
2012-03-20 01:00:53 -04:00
|
|
|
self.hl = new
|
2012-03-20 01:47:28 -04:00
|
|
|
if new == len(self.labels):
|
2013-10-25 17:25:07 -04:00
|
|
|
self.bt = 1
|
2012-03-20 01:47:28 -04:00
|
|
|
self.bt = min(self.bt,1)
|
2013-10-25 17:25:07 -04:00
|
|
|
self._highlight_button()
|
2012-03-20 01:47:28 -04:00
|
|
|
else:
|
|
|
|
self.bt=-1
|
2013-10-25 17:25:07 -04:00
|
|
|
self.entries[self.hl].gain_focus()
|
2012-03-20 12:52:39 -04:00
|
|
|
|
|
|
|
|
2013-10-25 17:25:07 -04:00
|
|
|
def _return_values(self):
|
2012-03-20 12:52:39 -04:00
|
|
|
book = {}
|
2013-10-25 17:25:07 -04:00
|
|
|
for k,e in zip(self.labels, self.entries):
|
2012-03-20 12:52:39 -04:00
|
|
|
if v!="" and k.lower()!="publish date":
|
2013-10-25 17:25:07 -04:00
|
|
|
book[k.lower()]=e.value
|
2012-03-20 12:52:39 -04:00
|
|
|
return book
|
|
|
|
|
2012-03-20 01:47:28 -04:00
|
|
|
|
2013-10-25 17:25:07 -04:00
|
|
|
def handle_input(self,ch):
|
2012-03-24 14:06:02 -04:00
|
|
|
if ch==curses.KEY_UP:
|
2013-10-25 17:25:07 -04:00
|
|
|
self._mv_focus(-1)
|
2012-03-24 14:06:02 -04:00
|
|
|
elif ch==curses.KEY_PPAGE:
|
2013-10-25 17:25:07 -04:00
|
|
|
self._mv_focus(-len(self.labels))
|
2012-03-24 14:06:02 -04:00
|
|
|
elif ch==curses.KEY_DOWN:
|
2013-10-25 17:25:07 -04:00
|
|
|
self._mv_focus(+1)
|
2012-03-24 14:06:02 -04:00
|
|
|
elif ch==curses.KEY_NPAGE:
|
2013-10-25 17:25:07 -04:00
|
|
|
self._mv_focus(+len(self.labels))
|
2012-03-24 14:06:02 -04:00
|
|
|
elif ch==curses.KEY_LEFT:
|
|
|
|
if self.bt==-1:
|
2013-10-25 17:25:07 -04:00
|
|
|
self.entries[self.hl].handle_input(ch)
|
2012-03-24 14:06:02 -04:00
|
|
|
else:
|
2013-10-25 17:25:07 -04:00
|
|
|
self._unhighlight_button()
|
2012-03-24 14:06:02 -04:00
|
|
|
self.bt=0
|
2013-10-25 17:25:07 -04:00
|
|
|
self._highlight_button()
|
2012-03-24 14:06:02 -04:00
|
|
|
elif ch==curses.KEY_HOME:
|
|
|
|
if self.bt==-1:
|
2013-10-25 17:25:07 -04:00
|
|
|
self._mv_cursor(-len(self.entries[self.hl]))
|
2012-03-24 14:06:02 -04:00
|
|
|
elif ch==curses.KEY_RIGHT:
|
|
|
|
if self.bt==-1:
|
2013-10-25 17:25:07 -04:00
|
|
|
self.entries[self.hl].handle_input(ch)
|
2012-03-24 14:06:02 -04:00
|
|
|
else:
|
2013-10-25 17:25:07 -04:00
|
|
|
self._unhighlight_button()
|
2012-03-24 14:06:02 -04:00
|
|
|
self.bt=1
|
2013-10-25 17:25:07 -04:00
|
|
|
self._highlight_button()
|
|
|
|
else:
|
2012-03-24 14:06:02 -04:00
|
|
|
if self.bt==-1:
|
2013-10-25 17:25:07 -04:00
|
|
|
self.entries[self.hl].handle_input(ch)
|
2012-03-24 14:06:02 -04:00
|
|
|
|
2012-03-20 01:47:28 -04:00
|
|
|
|
2012-03-24 14:06:02 -04:00
|
|
|
|
|
|
|
|
2013-10-25 17:25:07 -04:00
|
|
|
class BookForm(FormWindow):
|
2012-03-24 14:06:02 -04:00
|
|
|
caption = "Add a Book"
|
|
|
|
blabel = "Add"
|
|
|
|
labels = ["ISBN", "LCCN", "Title", "Subtitle", "Authors", "Edition",
|
|
|
|
"Publisher", "Publish Date", "Publish Year", "Publish Month", "Publish location",
|
|
|
|
"Pages", "Pagination", "Weight"]
|
|
|
|
|
|
|
|
|
|
|
|
# redefineable functions lookup is called when 'enter' is pressed on ISBN
|
|
|
|
# and returns the looked-up book. Default returns nothing
|
2012-06-15 14:11:21 -04:00
|
|
|
def lookup_isbn(self,isbn):
|
2012-03-24 14:06:02 -04:00
|
|
|
return {'isbn':isbn}
|
2012-06-15 14:11:21 -04:00
|
|
|
|
|
|
|
def lookup_lccn(self,lccn):
|
|
|
|
return {'lccn':lccn}
|
2012-03-24 14:06:02 -04:00
|
|
|
|
2013-10-25 17:25:07 -04:00
|
|
|
def return_book(self):
|
|
|
|
return self.return_values()
|
2012-03-24 14:06:02 -04:00
|
|
|
|
2013-10-25 17:25:07 -04:00
|
|
|
def handle_input(self,ch):
|
2012-03-24 14:06:02 -04:00
|
|
|
if ch==10 or ch==curses.KEY_ENTER:
|
2012-06-15 14:11:21 -04:00
|
|
|
if self.hl==0: # lookup by isbn
|
2013-10-25 17:25:07 -04:00
|
|
|
book = self.lookup_isbn(self.entries[0].value)
|
2012-06-15 14:11:21 -04:00
|
|
|
if book != {}:
|
2013-10-25 17:25:07 -04:00
|
|
|
#sys.stderr.write('updating entries\n')
|
|
|
|
self._set_entries(book)
|
2012-06-15 14:11:21 -04:00
|
|
|
self.refresh()
|
2013-10-25 17:25:07 -04:00
|
|
|
self._mv_focus(+7)
|
2012-06-15 14:11:21 -04:00
|
|
|
if self.hl==1: # lookup by lccn
|
2013-10-25 17:25:07 -04:00
|
|
|
book = self.lookup_lccn(self.entries[1].value)
|
2012-03-24 14:06:02 -04:00
|
|
|
if book != {}:
|
2013-10-25 17:25:07 -04:00
|
|
|
self._set_entries(book)
|
2012-03-24 14:06:02 -04:00
|
|
|
self.refresh()
|
2013-10-25 17:25:07 -04:00
|
|
|
self._mv_focus(+6)
|
2013-10-11 20:15:10 -04:00
|
|
|
else:
|
2013-10-25 17:25:07 -04:00
|
|
|
FormWindow.handle_input(self,ch)
|
2012-03-24 21:30:41 -04:00
|
|
|
|
2013-10-25 17:25:07 -04:00
|
|
|
class CategoryForm(FormWindow):
|
2012-03-24 21:30:41 -04:00
|
|
|
caption = "Add a Category"
|
|
|
|
blabel = "Add"
|
|
|
|
labels = ["Category"]
|
|
|
|
|
2013-10-25 17:25:07 -04:00
|
|
|
def return_values(self):
|
2012-03-24 21:30:41 -04:00
|
|
|
return self.entries
|