Commits

Alain Leufroy committed 0349323

[gui.qt] booktree: display and handle the booktree

Comments (0)

Files changed (2)

structure/book_tree.py

         self._book_names.append(name)
         book = Book(name, self._persister, create=True, title=title)
         self.books.append(book)
+        return book
 
     def _check_title_duplication(self, title):
         for book in self.books:
             if title == book.title:
                 raise ValueError("a book has already the title %s" % title)
 
+    def _book_index_from_name(self, book_name):
+        """Return the index of the book from its name"""
+        return (index
+                for index, book in enumerate(self.books)
+                if book.name == book_name).next()
+
     def delete_book(self, i_book):
         book = self.books[i_book]
         book.remove_data()
         del self.books[i_book]
         del self._book_names[i_book]
 
+    def delete_book_from_name(self, book_name):
+        """Delete the book named ``book_name``"""
+        self.delete_book(self._book_index_from_name(book_name))
+
     def retitle_book(self, i_book, title):
         # XXX: should we call this "rename_book"?
         # for the moment:
             self._check_title_duplication(title)
             self.books[i_book].title = title
 
+    def retitle_book_from_name(self, book_name, title):
+        """Change the title of the book named ``book_name``"""
+        self.retitle_book(self._book_index_from_name(book_name), title)

ui/gui/qt/qt_ui.py

 This module contains the Qt user interface controller.
 """
 import sys
-from PyQt4 import QtGui
+from functools import partial
+from PyQt4 import QtGui, QtCore
 from limma.structure.structure import Structure
 from limma.ui.ui import AbstractUI
 from limma.ui.gui.qt.mainwindow import MainWindow
+from limma.ui.gui.qt.booktree import BookTree
+
+BOOKNAMEROLE = QtCore.Qt.StatusTipRole # role used to store the book name
 
 class QtUI(AbstractUI):
     """UI controller that synchronize data and the Qt UI"""
         self._struct = Structure(conf)
 
     def loop(self):
+        """Start the main loop."""
         app = QtGui.QApplication(sys.argv)
         mainwindow = MainWindow()
+        self.setup_ui(mainwindow)
         mainwindow.show()
         app.exec_()
+
+    def setup_ui(self, mainwindow):
+        """setup the uiser interface."""
+        booktree = BookTree()
+        mainwindow.setCentralWidget(booktree)
+        self._setup_ui_booktree(booktree)
+
+    def _setup_ui_booktree(self, booktree):
+        """Setup the booktree of the user interface."""
+        # populate the book tree
+        if self._struct.book_tree.books:
+            for book in self._struct.book_tree.books:
+                item = booktree.add(book.title)
+                item.setData(BOOKNAMEROLE, unicode(book.name))
+            booktree.select(0)
+        # handle deletion
+        booktree.removed.connect(self._on_book_removed)
+        # handle edition
+        booktree.edited.connect(self._on_book_edited)
+
+    def _on_book_removed(self, item):
+        """handle removed book item requested by user"""
+        book_name = item.data(BOOKNAMEROLE).toPyObject()
+        self._struct.book_tree.delete_book_from_name(book_name)
+
+    def _on_book_edited(self, item):
+        """handle edited book requested by user."""
+        book_name = item.data(BOOKNAMEROLE).toPyObject()
+        title = unicode(item.text())
+        if book_name is None: # if not yet exists => add
+            book = self._struct.book_tree.add_book(title)
+            item.setData(BOOKNAMEROLE, unicode(book.name))
+        else:
+            # retitle tolerate no change
+            self._struct.book_tree.retitle_book_from_name(book_name, title)