updating from browser works now
[public/library.git] / browser.py
1 import curses
2 import dbLayer as db
3 from bookForm import bookForm
4
5 class browserWindow:
6     hl=0
7     books = []
8     topline = 0
9     columns = [('ID',3),
10                ('Title',30),
11                ('Authors',20),
12                ('ISBN',13)]
13     mx = my = 0
14
15     # redefinable functions
16     def updateSelection(self,book):
17         bookid = book['id']
18         
19         w=curses.newwin(1,1,20,20)
20         bf=bookForm(w)
21         bf.caption='Update Book '+str(bookid)
22         bf.blabel='update'
23         bf.updateEntries(book)
24         newbook = bf.eventLoop()
25         if len(newbook)!=0:
26             db.updateBook(newbook,bookid)
27
28
29     def __init__(self,window):
30         self.w = window
31         self.updateGeometry()
32         self.refreshBooks()
33
34     def refreshBooks(self):
35         self.books = db.getBooks()
36
37     def sortByColumn(self, col):
38         self.books.sort() # key=dict.get(col))
39
40     def updateGeometry(self):
41         (self.my,self.mx)=self.w.getmaxyx()
42         self.pageSize = self.my-3
43         # maybe recalculate column widths here.
44
45     def refresh(self):
46         self.displayHeader()
47         for r in range(0,self.pageSize):
48             self.displayRow(r)
49         self.w.refresh()
50         self.highlight()
51
52     def displayHeader(self):
53         cursor = 0
54         for header,width in self.columns:
55             self.w.addnstr(0,cursor,header,width)
56             self.w.addstr(1,cursor,"-"*width)
57             cursor += width+1
58
59     def displayRow(self,row):
60         if self.topline+row < len(self.books):
61             book = self.books[self.topline+row]
62             cursor = 0
63             for k,width in self.columns:
64                 if k.lower() in book:
65                     self.w.addnstr(row+2,cursor,str(book[k.lower()])+" "*width,width)
66                 cursor += width+1
67         else:
68             self.w.addstr(row+2,0," "*self.mx)
69
70     def highlight(self):
71         row = self.hl-self.topline+2
72         if row > 1 and row < self.my:
73             self.w.chgat(row,0,self.mx,curses.A_REVERSE)
74
75     def unHighlight(self):
76         row = self.hl-self.topline+2
77         if row > 1 and row < self.my:
78             self.w.chgat(row,0,self.mx,curses.A_NORMAL)
79
80     def mvHighlight(self,delta):
81         new = self.hl+delta
82         new = max(new,0)
83         new = min(new,len(self.books)-1)
84         self.unHighlight()
85         self.hl = new
86         self.highlight()
87     
88     def scroll(self,delta):
89         self.unHighlight()
90         self.topline += delta
91         self.topline = max(self.topline,0)
92         self.topline = min(self.topline,len(self.books)-1)
93         self.refresh()
94
95     def startBrowser(self):
96         self.w.keypad(1)
97         self.refresh()
98
99         ch = self.w.getch()
100         while ch != 27 and ch != 113:
101             if ch == curses.KEY_UP:
102                 if self.hl == self.topline:
103                     self.scroll(-self.pageSize/2-1)
104                 self.mvHighlight(-1)
105             elif ch == curses.KEY_DOWN:
106                 if self.hl == self.topline+self.pageSize-1:
107                     self.scroll(+self.pageSize/2+1)
108                 self.mvHighlight(+1)
109             elif ch == curses.KEY_PPAGE:
110                 self.scroll(-self.pageSize)
111                 self.mvHighlight(-self.pageSize)
112             elif ch == curses.KEY_NPAGE:
113                 self.scroll(+self.pageSize)
114                 self.mvHighlight(+self.pageSize)
115             
116             elif ch == 117:
117                 book = self.books[self.hl]
118                 self.updateSelection(book)
119                 self.books[self.hl]=db.getBookByID(book['id'])
120                 self.refresh()
121
122             self.w.refresh()
123             ch = self.w.getch()
124
125
126
127
128
129