Categorizing and uncategorizing based on selection
[public/library.git] / library / interface / browser.py
index d3e380b..93d6577 100644 (file)
@@ -336,7 +336,7 @@ class bookBrowser(browserWindow):
                   ('Authors',30,None),
                   ('Title',60,None)]
 
-    cs = [(' u', 'update'), (' d', 'delete selected'), (' c', 'categorize')]
+    cs = [(' u', 'update'), (' d', 'delete selected'), (' c', 'categorize'), (' a', 'add selected to category')]
 
 
     # redefinable functions
@@ -380,12 +380,28 @@ class bookBrowser(browserWindow):
                 books.append(book)
         db.removeBooks(books)
 
+    @catch_error
+    def addBooksToCategory(self):
+        books = []
+        for sel,book in zip(self.selected, self.entries):
+            if sel:
+                books.append(book)
+        w = curses.newwin(1,1)
+        cs = singleCategorySelector(w,self.hb,40,50)
+        self.centreChild(w)
+        cs.refreshCategories()
+        cat = cs.eventLoop()
+        cs.clear()
+        self.refreshBooks()
+        if cat:
+            db.categorizeBooks(cat, books)
+
     def refreshBooks(self):
         self.load_data(db.get_books())
 
-    def refreshBooksInCategory(self,cat):
-        self.refreshBooks = lambda : self.load_data(db.getBooksByCategory(cat))
-        self.refreshBooks()
+    #def refreshBooksInCategory(self,cat):
+    #    self.refreshBooks = lambda : self.load_data(db.getBooksByCategory(cat))
+    #    self.refreshBooks()
 
     def refreshBooksUncategorized(self):
         self.refreshBooks = lambda : self.load_data(db.getUncategorizedBooks())
@@ -415,6 +431,9 @@ class bookBrowser(browserWindow):
             book = self.highlightedEntry()
             self.categorizeSelection(book)
             self.refresh()
+        elif ch == 97: #a
+            self.addBooksToCategory()
+            self.refresh()
         if ch == 100:
             count=0
             for s in self.selected[0:self.hl-1]:
@@ -427,6 +446,30 @@ class bookBrowser(browserWindow):
             self.mvHighlight(-count)
         return ch
 
+#a pretty ugly hack in order to be able to remove books from this category
+class bookBrowserInCategory(bookBrowser):
+    cs = [(' u', 'update'), (' d', 'delete selected'), (' c', 'categorize'), (' a', 'add selected to category'), (' r', 'uncategorize selected')]
+
+    def refreshBooksInCategory(self,cat):
+        self.cat = cat
+        self.refreshBooks = lambda : self.load_data(db.getBooksByCategory(cat))
+        self.refreshBooks()
+
+    @catch_error
+    def uncategorizeBooks(self):
+        books = []
+        for sel,book in zip(self.selected, self.entries):
+            if sel:
+                books.append(book)
+        db.uncategorizeBooks(books, self.cat)
+
+    def handleInput(self,ch):
+        if ch == 114: #r
+            self.uncategorizeBooks()
+            self.refreshBooks()
+            self.refresh()
+        bookBrowser.handleInput(self,ch)
+
 class categoryBrowser(browserWindow):
     columnDefs = [('Category',100,None)]
     cs = [(' a', 'add category'), (' d', 'delete selected')]
@@ -446,7 +489,7 @@ class categoryBrowser(browserWindow):
 
     def viewCategory(self):
         w = curses.newwin(3,5)
-        b = bookBrowser(w,self.hb)
+        b = bookBrowserInCategory(w,self.hb)
         self.centreChild(w)
         b.refreshBooksInCategory(self.highlightedEntry())
         b.eventLoop()
@@ -545,6 +588,27 @@ class categorySelector(browserWindow):
             return 113
 
 
+class singleCategorySelector(categorySelector):
+    columnDefs = [('Category',100,None)]
+    cs = []
+    commands = [(' /', 'search'), (' n', 'find next'), (' N', 'find previous'),
+                (' q', 'quit'), ('Enter', 'done')]
+
+    def handleInput(self,ch):
+        return browserWindow.handleInput(self,ch,True)
+
+    def eventLoop(self):
+        self.w.keypad(1)
+        self.refresh()
+
+        ch = self.w.getch()
+        while ch != 27 and ch != 113:
+            if ch == 10:
+                return self.highlightedEntry()
+            self.handleInput(ch)
+            self.w.refresh()
+            ch = self.w.getch()
+            self.hb.refresh()
 
 class columnSelector(browserWindow):
     columnDefs = [('Column',100,None)]