help bar more sensible now
[public/library.git] / browser.py
index da70bfe..adcdc55 100644 (file)
@@ -1,11 +1,14 @@
+import sys
 import curses
 import dbLayer as db
-from bookForm import bookForm
+from form import bookForm,categoryForm
 
 class browserWindow:
     hl=0
-    entries = []
     topline = 0
+    entries = []
+    selected = []
+    commands = [(' q', 'quit')]
     # column definitions are in (label, weight, specified width) triples
     columnDefs = [('something',1,None)]
     mx = my = 0
@@ -15,12 +18,14 @@ class browserWindow:
         self.w.erase()
         self.w.refresh()
 
-    def __init__(self,window):
+    def __init__(self,window,helpbar):
         self.w = window
+        self.hb = helpbar
         self.updateGeometry()
 
     def sortByColumn(self, col):
         self.entries.sort() # key=dict.get(col))
+        self.selected = map(lambda x: False, self.selected)
 
     def updateGeometry(self):
         (self.my,self.mx)=self.w.getmaxyx()
@@ -45,6 +50,8 @@ class browserWindow:
         self.columns=cols
 
     def refresh(self):
+        self.hb.commands = self.commands
+        self.hb.refresh()
         self.displayHeader()
         for r in range(0,self.pageSize):
             self.displayRow(r)
@@ -52,16 +59,20 @@ class browserWindow:
         self.highlight()
 
     def displayHeader(self):
-        cursor = 0
+        cursor = 1
         for header,width in self.columns:
-            self.w.addnstr(0,cursor,header,width)
+            self.w.addnstr(0,cursor,header+" "*width,width)
             self.w.addstr(1,cursor,"-"*width)
             cursor += width+1
 
     def displayRow(self,row):
         if self.topline+row < len(self.entries):
             entry = self.entries[self.topline+row]
-            cursor = 0
+            cursor = 1
+            if self.selected[self.topline+row]:
+                self.w.addstr(row+2, 0, "*")
+            else:
+                self.w.addstr(row+2, 0, " ")
             for k,width in self.columns:
                 if k.lower() in entry:
                     self.w.addnstr(row+2,cursor,str(entry[k.lower()])+" "*width,width)
@@ -105,11 +116,11 @@ class browserWindow:
             ch = self.w.getch()
 
     def handleInput(self,ch):
-        if ch == curses.KEY_UP:
+        if ch == curses.KEY_UP or ch == 107 or ch == 16:
             if self.hl == self.topline:
                 self.scroll(-self.pageSize/2-1)
             self.mvHighlight(-1)
-        elif ch == curses.KEY_DOWN:
+        elif ch == curses.KEY_DOWN or ch == 106 or ch == 14:
             if self.hl == self.topline+self.pageSize-1:
                 self.scroll(+self.pageSize/2+1)
             self.mvHighlight(+1)
@@ -119,6 +130,10 @@ class browserWindow:
         elif ch == curses.KEY_NPAGE:
             self.scroll(+self.pageSize)
             self.mvHighlight(+self.pageSize)
+        elif ch == 32:
+            self.selected[self.hl] = not self.selected[self.hl]
+            self.displayRow(self.hl-self.topline)
+            self.highlight()
 
 
 
@@ -127,15 +142,17 @@ class bookBrowser(browserWindow):
                   ('ISBN',0,13),
                   ('Authors',30,None),
                   ('Title',60,None)]
+    
+    commands = [(' u', 'update'), (' d', 'delete selected'), (' q', 'quit')]
+    
     # redefinable functions
     def updateSelection(self,book):
         bookid = book['id']
         
         w=curses.newwin(1,1,20,20)
-        bf=bookForm(w)
+        bf=bookForm(w,self.hb,book)
         bf.caption='Update Book '+str(bookid)
         bf.blabel='update'
-        bf.updateEntries(book)
         newbook = bf.eventLoop()
         if len(newbook)!=0:
             db.updateBook(newbook,bookid)
@@ -144,21 +161,21 @@ class bookBrowser(browserWindow):
     def viewSelection(self,book):
         bookid = book['id']
         w=curses.newwin(1,1,20,20)
-        bf = bookForm(w)
+        bf = bookForm(w,self.hb,book)
         bf.caption='Viewing Book '+str(bookid)
         bf.blabel='done'
-        bf.updateEntries(book)
         bf.eventLoop()
         bf.clear()
 
     def refreshBooks(self):
         self.entries = db.getBooks()
+        self.selected = map(lambda x:False, self.entries)
 
     def handleInput(self,ch):
         browserWindow.handleInput(self,ch)
         if ch == 117: #update on 'u'
             book = self.entries[self.hl]
-            self.updateSelection(entries)
+            self.updateSelection(book)
             self.entries[self.hl]=db.getBookByID(book['id'])
             self.refresh()
         elif ch == 10:
@@ -166,16 +183,30 @@ class bookBrowser(browserWindow):
             self.viewSelection(book)
             self.refresh()
 
-def categoryBrowser():
-    columnDefs = [('ID',0,3),
-                  ('Category',100,None)]
+class categoryBrowser(browserWindow):
+    columnDefs = [('Category',100,None)]
+    commands = [(' a', 'add category'), (' d', 'delete selected'), (' q', 'quit')]
+
 
     def refreshCategories(self):
-        self.entries = []
-        cats = db.getCategories()
+        self.entries = db.getCategories()
+        self.sortByColumn('category')
+        self.selected = map(lambda x:False, self.entries)
+
+    def addCategory(self):
+        w = curses.newwin(1,1,10,10)
+        cf = categoryForm(w,self.hb)
+        cats = cf.eventLoop()
+        print >> sys.stderr, cats
         for c in cats:
-            self.entries.append({'category':c})
+            print >> sys.stderr, "adding "+str(c)
+            db.addCategory(c)
+        cf.clear()
 
     def handleInput(self,ch):
         browserWindow.handleInput(self,ch)
+        if ch==97:
+            self.addCategory()
+            self.refreshCategories()
+            self.refresh()