borders around browser windows.
[public/library.git] / browser.py
index 7b8f9f2..4bc6d57 100644 (file)
@@ -33,12 +33,12 @@ class browserWindow:
         (y,x) = self.w.getbegyx()
         self.cx = x + self.mx/2
         self.cy = y + self.my/2
-        self.pageSize = self.my-3
+        self.pageSize = self.my-4
         self.calcColWidths()
 
     def calcColWidths(self):
         total_weights = 0
-        available_space = self.mx - len(self.columnDefs)
+        available_space = self.mx - len(self.columnDefs) -2
         cols = []
         for label,weight,value in self.columnDefs:
             if value!=None:
@@ -56,6 +56,7 @@ class browserWindow:
     def refresh(self):
         self.hb.commands = self.commands
         self.hb.refresh()
+        self.w.box()
         self.displayHeader()
         for r in range(0,self.pageSize):
             self.displayRow(r)
@@ -72,36 +73,36 @@ class browserWindow:
 
 
     def displayHeader(self):
-        cursor = 1
+        cursor = 2
         for header,width in self.columns:
-            self.w.addnstr(0,cursor,header+" "*width,width)
-            self.w.addstr(1,cursor,"-"*width)
+            self.w.addnstr(1,cursor,header+" "*width,width)
+            self.w.addstr(2,cursor,"-"*width)
             cursor += width+1
 
     def displayRow(self,row):
         if self.topline+row < len(self.entries):
             entry = self.entries[self.topline+row]
-            cursor = 1
+            cursor = 2
             if self.selected[self.topline+row]:
-                self.w.addstr(row+2, 0, "*")
+                self.w.addstr(row+3, 1, "*")
             else:
-                self.w.addstr(row+2, 0, " ")
+                self.w.addstr(row+3, 1, " ")
             for k,width in self.columns:
                 if k.lower() in entry:
-                    self.w.addnstr(row+2,cursor,str(entry[k.lower()])+" "*width,width)
+                    self.w.addnstr(row+3,cursor,str(entry[k.lower()])+" "*width,width)
                 cursor += width+1
         else:
-            self.w.addstr(row+2,0," "*self.mx)
+            self.w.addstr(row+3,1," "*(self.mx-2))
 
     def highlight(self):
-        row = self.hl-self.topline+2
+        row = self.hl-self.topline+3
         if row > 1 and row < self.my:
-            self.w.chgat(row,0,self.mx,curses.A_REVERSE)
+            self.w.chgat(row,1,self.mx-2,curses.A_REVERSE)
 
     def unHighlight(self):
-        row = self.hl-self.topline+2
+        row = self.hl-self.topline+3
         if row > 1 and row < self.my:
-            self.w.chgat(row,0,self.mx,curses.A_NORMAL)
+            self.w.chgat(row,1,self.mx-2,curses.A_NORMAL)
 
     def mvHighlight(self,delta):
         new = self.hl+delta
@@ -114,8 +115,8 @@ class browserWindow:
     def scroll(self,delta):
         self.unHighlight()
         self.topline += delta
-        self.topline = max(self.topline,0)
         self.topline = min(self.topline,len(self.entries)-1)
+        self.topline = max(self.topline,0)
         self.refresh()
 
     def search(self, string):
@@ -223,12 +224,78 @@ class browserWindow:
             else:
                 self.hb.display(self.last_search+' not found')
         elif ch == 32:
-            self.selected[self.hl] = not self.selected[self.hl]
+            if len(self.selected)>0:
+                self.selected[self.hl] = not self.selected[self.hl]
             self.displayRow(self.hl-self.topline)
             self.highlight()
 
 
 
+class trashBrowser(browserWindow):
+    columnDefs = [('ID',0,3),
+                  ('ISBN',0,13),
+                  ('Authors',30,None),
+                  ('Title',60,None)]
+    
+    cs = [(' r', 'restore selected'), (' d', 'delete selected')]
+    
+    # redefinable functions
+    def viewSelection(self,book):
+        bookid = book['id']
+        w=curses.newwin(1,1,20,20)
+        bf = bookForm(w,self.hb,book)
+        self.centreChild(w)
+        bf.caption='Viewing Book '+str(bookid)
+        bf.blabel='done'
+        bf.eventLoop()
+        bf.clear()
+
+    def restoreSelected(self):
+        books = []
+        for sel,book in zip(self.selected, self.entries):
+            if sel:
+                books.append(book)
+        db.restoreBooks(books)
+
+    def delSelected(self):
+        books = []
+        for sel,book in zip(self.selected, self.entries):
+            if sel:
+                books.append(book)
+        db.deleteBooks(books)
+
+    def refreshBooks(self):
+        self.entries = db.getRemovedBooks()
+        self.selected = map(lambda x:False, self.entries)
+
+    def handleInput(self,ch):
+        browserWindow.handleInput(self,ch)
+        if ch == 10:
+            book = self.entries[self.hl]
+            self.viewSelection(book)
+            self.refresh()
+        if ch==114: #restore books
+            count=0
+            for s in self.selected[0:self.hl-1]:
+                if s:
+                    count+=1
+            self.restoreSelected()
+            self.refreshBooks()
+            self.refresh()
+            self.scroll(-count)
+            self.mvHighlight(-count)
+        if ch==100: # delete books
+            count=0
+            for s in self.selected[0:self.hl-1]:
+                if s:
+                    count+=1
+            self.delSelected()
+            self.refreshBooks()
+            self.refresh()
+            self.scroll(-count)
+            self.mvHighlight(-count)
+        return ch
+
 class bookBrowser(browserWindow):
     columnDefs = [('ID',0,3),
                   ('ISBN',0,13),
@@ -281,6 +348,10 @@ class bookBrowser(browserWindow):
         self.entries = db.getBooks()
         self.selected = map(lambda x:False, self.entries)
 
+    def refreshBooksInCategory(self,cat):
+        self.entries = db.getBooksByCategory(cat)
+        self.selected = map(lambda x:False, self.entries)
+
     def handleInput(self,ch):
         browserWindow.handleInput(self,ch)
         if ch == 117: #update on 'u'
@@ -327,6 +398,14 @@ class categoryBrowser(browserWindow):
             db.addCategory(c)
         cf.clear()
 
+    def viewCategory(self):
+        w = curses.newwin(20,80,20,20)
+        b = bookBrowser(w,self.hb)
+        self.centreChild(w)
+        b.refreshBooksInCategory(self.entries[self.hl])
+        b.eventLoop()
+        b.clear()
+
     def delSelected(self):
         categories = []
         for sel,cat in zip(self.selected, self.entries):
@@ -340,6 +419,9 @@ class categoryBrowser(browserWindow):
             self.addCategory()
             self.refreshCategories()
             self.refresh()
+        if ch ==10:
+            self.viewCategory()
+            self.refresh()
         if ch==100:
             count=0
             for s in self.selected[0:self.hl-1]: