added selecting to browser window
[public/library.git] / browser.py
index da70bfe..6076052 100644 (file)
@@ -1,11 +1,13 @@
+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 = []
     # column definitions are in (label, weight, specified width) triples
     columnDefs = [('something',1,None)]
     mx = my = 0
@@ -21,6 +23,7 @@ class browserWindow:
 
     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()
@@ -52,16 +55,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 +112,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 +126,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()
 
 
 
@@ -153,6 +164,7 @@ class bookBrowser(browserWindow):
 
     def refreshBooks(self):
         self.entries = db.getBooks()
+        self.selected = map(lambda x:False, self.entries)
 
     def handleInput(self,ch):
         browserWindow.handleInput(self,ch)
@@ -166,16 +178,32 @@ class bookBrowser(browserWindow):
             self.viewSelection(book)
             self.refresh()
 
-def categoryBrowser():
-    columnDefs = [('ID',0,3),
-                  ('Category',100,None)]
+class categoryBrowser(browserWindow):
+    columnDefs = [('Category',100,None)]
+
 
     def refreshCategories(self):
         self.entries = []
         cats = db.getCategories()
         for c in cats:
             self.entries.append({'category':c})
+        self.sortByColumn('category')
+        self.selected = map(lambda x:False, self.entries)
+
+    def addCategory(self):
+        w = curses.newwin(1,1,10,10)
+        cf = categoryForm(w)
+        cats = cf.eventLoop()
+        print >> sys.stderr, cats
+        for c in cats:
+            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()