Added home and end to browser window.
[public/library.git] / browser.py
index fec2a62..2db9e49 100644 (file)
@@ -1,13 +1,13 @@
 import sys
 import curses
-import dbLayer as db
+import db_layer as db
 from form import bookForm,categoryForm
 
 class browserWindow:
     hl=0
     topline = 0
     entries = []
-    selected = []
+    selected = list()
     commands = [(' /', 'search'), (' n', 'find next'), (' N', 'find previous'), (' q', 'quit')]
     cs = []
     # column definitions are in (label, weight, specified width) triples
@@ -26,19 +26,19 @@ class browserWindow:
 
     def sortByColumn(self, col):
         self.entries.sort(key=lambda k: k.get(col)) # key=dict.get(col))
-        self.selected = map(lambda x: False, self.selected)
+        self.selected = list(map(lambda x: False, self.selected))
 
     def updateGeometry(self):
         (self.my,self.mx)=self.w.getmaxyx()
         (y,x) = self.w.getbegyx()
-        self.cx = x + self.mx/2
-        self.cy = y + self.my/2
-        self.pageSize = self.my-3
+        self.cx = x + self.mx//2
+        self.cy = y + self.my//2
+        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:
@@ -50,12 +50,13 @@ class browserWindow:
             if value!=None:
                 cols.append((label,value))
             else:
-                cols.append((label,available_space*weight/total_weights))
+                cols.append((label,available_space*weight//total_weights))
         self.columns=cols
 
     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)
@@ -68,40 +69,41 @@ class browserWindow:
 
     def centreChild(self,child):
         (y,x)=child.getmaxyx()
-        child.mvwin(self.cy-y/2,self.cx-x/2)
+        child.mvwin(self.cy-y//2,self.cx-x//2)
 
 
     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
+            self.w.addnstr(row+3, 1, " "*self.mx,self.mx-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
@@ -185,11 +187,11 @@ class browserWindow:
     def handleInput(self,ch):
         if ch == curses.KEY_UP or ch == 107 or ch == 16:
             if self.hl == self.topline:
-                self.scroll(-self.pageSize/2-1)
+                self.scroll(-self.pageSize//2-1)
             self.mvHighlight(-1)
         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.scroll(+self.pageSize//2+1)
             self.mvHighlight(+1)
         elif ch == curses.KEY_PPAGE:
             self.scroll(-self.pageSize)
@@ -197,6 +199,12 @@ class browserWindow:
         elif ch == curses.KEY_NPAGE:
             self.scroll(+self.pageSize)
             self.mvHighlight(+self.pageSize)
+        elif ch == curses.KEY_HOME:
+            self.scroll(-len(self.entries))
+            self.mvHighlight(-len(self.entries))
+        elif ch == curses.KEY_END:
+            self.scroll(len(self.entries))
+            self.mvHighlight(len(self.entries))
         elif ch == 47: # forward slash
             string = self.hb.getSearch()
             hl = self.search(string)
@@ -222,6 +230,15 @@ class browserWindow:
                 self.mvHighlight(delta)
             else:
                 self.hb.display(self.last_search+' not found')
+        elif ch == 270: # F6 Sorts
+            w = curses.newwin(40,20,20,20)
+            cl = columnSelector(w,self.hb)
+            self.centreChild(w)
+            col = cl.eventLoop()
+            cl.clear()
+            self.sortByColumn(col)
+            self.clear()
+            self.refresh()
         elif ch == 32:
             if len(self.selected)>0:
                 self.selected[self.hl] = not self.selected[self.hl]
@@ -265,7 +282,7 @@ class trashBrowser(browserWindow):
 
     def refreshBooks(self):
         self.entries = db.getRemovedBooks()
-        self.selected = map(lambda x:False, self.entries)
+        self.selected = list(map(lambda x:False, self.entries))
 
     def handleInput(self,ch):
         browserWindow.handleInput(self,ch)
@@ -345,11 +362,11 @@ class bookBrowser(browserWindow):
 
     def refreshBooks(self):
         self.entries = db.getBooks()
-        self.selected = map(lambda x:False, self.entries)
+        self.selected = list(map(lambda x:False, self.entries))
 
     def refreshBooksInCategory(self,cat):
         self.entries = db.getBooksByCategory(cat)
-        self.selected = map(lambda x:False, self.entries)
+        self.selected = list(map(lambda x:False, self.entries))
 
     def handleInput(self,ch):
         browserWindow.handleInput(self,ch)
@@ -386,7 +403,7 @@ class categoryBrowser(browserWindow):
     def refreshCategories(self):
         self.entries = db.getCategories()
         self.sortByColumn('category')
-        self.selected = map(lambda x:False, self.entries)
+        self.selected = list(map(lambda x:False, self.entries))
 
     def addCategory(self):
         w = curses.newwin(1,1,10,10)
@@ -446,7 +463,7 @@ class categorySelector(browserWindow):
         self.refreshSelected()
 
     def refreshSelected(self):
-        self.original = map(lambda x:False, self.entries)
+        self.original = list(map(lambda x:False, self.entries))
         cats = db.getBookCategories(self.book)
         cats.sort()
         cats.sort(key=lambda k: k.get('category')) # key=dict.get(col))
@@ -495,3 +512,37 @@ class categorySelector(browserWindow):
             self.updateCategories()
             return 113
 
+
+
+class columnSelector(browserWindow):
+    columnDefs = [('Column',100,None)]
+    entries = [{'column': 'id'}, {'column': 'isbn'}, {'column': 'lccn'},
+            {'column': 'title'}, {'column': 'subtitle'}, {'column': 'authors'}, 
+            {'column': 'edition'}, {'column': 'publisher'}, 
+            {'column': 'publish year'}, {'column': 'publish month'}, 
+            {'column': 'publish location'}, {'column': 'pages'}, {'column': 'pagination'}, 
+            {'column': 'weight'}, {'column': 'last updated'}]
+
+    def __init__(self,window,helpbar):
+        self.selected = [False,False,False,False,False,False,False,
+                         False,False,False,False,False,False,False,False]
+        browserWindow.__init__(self,window,helpbar)
+
+
+    def eventLoop(self):
+        self.w.keypad(1)
+        self.refresh()
+
+        ch = self.w.getch()
+        while ch != 27 and ch != 113:
+            ch = self.handleInput(ch)
+            if ch==10:
+                col = self.entries[self.hl]
+                return col['column']
+            self.w.refresh()
+            ch = self.w.getch()
+            self.hb.refresh()
+    
+    def handleInput(self,ch):
+        browserWindow.handleInput(self,ch)
+        return ch