Commits

Alain Leufroy  committed 801e2c2

[gui.qt] add book definition

[gui.qt] book -> booktree

  • Participants
  • Parent commits 7f8a8dd

Comments (0)

Files changed (2)

File ui/gui/qt/booktree.py

+# -*- coding: utf-8 -*-
+"""
+This module contains the Qt user interface for a book Tree.
+"""
+
+from PyQt4 import QtGui, QtCore
+from limma.ui.gui.qt import UiFromDesigner
+
+__all__ = ['BookTree']
+
+class BookTree(UiFromDesigner, QtGui.QWidget):
+    """A Qt implementation of the Book Tree user interface."""
+    __ui__ = 'booktree'
+    selected = QtCore.pyqtSignal(QtGui.QListWidgetItem)
+    edited = QtCore.pyqtSignal(QtGui.QListWidgetItem)
+    removed = QtCore.pyqtSignal(QtGui.QListWidgetItem)
+
+    def __init__(self, title='Book Tree', *ags, **kws):
+        """
+        :title: the title of the book tree
+        """
+        super(BookTree, self).__init__(*ags, **kws)
+        self._menu = None
+        self._ui.label.setText(title)
+        self._ui.books.itemClicked.connect(self._emit_selected)
+        self._ui.books.itemChanged.connect(self._emit_edited)
+        self._setup_actions()
+
+    def _setup_actions(self):
+        """implement the user actions"""
+        self._menu = QtGui.QMenu(self)
+        self._menu.addAction(unicode('Add')).setData('add')
+        self._menu.addAction(unicode('Remove')).setData('remove')
+        self._menu.addAction(unicode('Retitle')).setData('retitle')
+        self._ui.actions.setMenu(self._menu)
+        self._menu.triggered.connect(self._process_action)
+
+    def _process_action(self, action):
+        """Process user action"""
+        action = action.data().toPyObject()
+
+        if action == 'remove':
+            index = self._ui.books.currentRow()
+            item = self._ui.books.takeItem(index)
+            self._emit_removed(item)
+            self._emit_selected()
+        elif action == 'retitle':
+            item = self._ui.books.currentItem()
+            self._ui.books.editItem(item)
+        elif action == 'add':
+            # XXX: this is not robust
+            temp_title = unicode('New %i' % (self._ui.books.count() + 1))
+            item = self.add(temp_title)
+            self._ui.books.editItem(item)
+            self._ui.books.setCurrentItem(item)
+
+    def _emit_selected(self, item=None):
+        """emit the selected event"""
+        if not item:
+            item = self._ui.books.currentItem()
+        self.selected.emit(item)
+
+    def _emit_edited(self, item):
+        """emit the edited event."""
+        self.edited.emit(item)
+
+    def _emit_removed(self, item):
+        """emit the removed event."""
+        self.removed.emit(item)
+
+    def add(self, title):
+        """
+        Add a sheet entry titled ``title``.
+        The ``selected`` signal is emitted at the end of the process.
+        """
+        item = QtGui.QListWidgetItem(title)
+        item.setFlags(QtCore.Qt.ItemIsEnabled |
+                      QtCore.Qt.ItemIsSelectable |
+                      QtCore.Qt.ItemIsEditable)
+        self._ui.books.addItem(item) # here to prevent emmiting signal
+        return item
+
+    def select(self, index):
+        """Select the sheet at position ``index``"""
+        self._ui.books.setCurrentRow(index)
+        self._emit_selected()
+
+
+
+if __name__ == "__main__":
+    import sys
+    from limma.ui.gui.qt import get_ui
+    from limma.ui.gui.qt.mainwindow import MainWindow
+    app = QtGui.QApplication(sys.argv)
+    main_window = MainWindow()
+
+    book_tree = BookTree(title='The title of the book Tree',
+                         parent=main_window)
+    book_tree.add('The First Book')
+    book_tree.add('The second Book')
+
+    def hook(signal_name):
+        def callback(item, signal_name=signal_name):
+            print '%s: %s' % (
+                signal_name,
+                item.text() if item else 'No one',
+            )
+        return callback
+
+    book_tree.selected.connect(hook('selected'))
+    book_tree.edited.connect(hook('edited'))
+    book_tree.removed.connect(hook('removed'))
+
+    book_tree.select(0)
+
+    main_window.setCentralWidget(book_tree)
+    main_window.show()
+
+    sys.exit(app.exec_())

File ui/gui/qt/uis/booktree.ui

+<?xml version="1.0" encoding="UTF-8"?>
+<ui version="4.0">
+ <class>book_tree</class>
+ <widget class="QWidget" name="book_tree">
+  <property name="geometry">
+   <rect>
+    <x>0</x>
+    <y>0</y>
+    <width>602</width>
+    <height>534</height>
+   </rect>
+  </property>
+  <property name="sizePolicy">
+   <sizepolicy hsizetype="Minimum" vsizetype="Minimum">
+    <horstretch>0</horstretch>
+    <verstretch>0</verstretch>
+   </sizepolicy>
+  </property>
+  <property name="windowTitle">
+   <string>Form</string>
+  </property>
+  <layout class="QVBoxLayout" name="verticalLayout">
+   <item>
+    <layout class="QHBoxLayout" name="horizontalLayout">
+     <item>
+      <widget class="QToolButton" name="actions">
+       <property name="sizePolicy">
+        <sizepolicy hsizetype="Preferred" vsizetype="Minimum">
+         <horstretch>0</horstretch>
+         <verstretch>0</verstretch>
+        </sizepolicy>
+       </property>
+       <property name="text">
+        <string>Actions</string>
+       </property>
+       <property name="popupMode">
+        <enum>QToolButton::InstantPopup</enum>
+       </property>
+       <property name="toolButtonStyle">
+        <enum>Qt::ToolButtonTextBesideIcon</enum>
+       </property>
+       <property name="autoRaise">
+        <bool>false</bool>
+       </property>
+       <property name="arrowType">
+        <enum>Qt::DownArrow</enum>
+       </property>
+      </widget>
+     </item>
+     <item>
+      <widget class="QLabel" name="label">
+       <property name="sizePolicy">
+        <sizepolicy hsizetype="Minimum" vsizetype="Preferred">
+         <horstretch>0</horstretch>
+         <verstretch>0</verstretch>
+        </sizepolicy>
+       </property>
+       <property name="text">
+        <string>Book title</string>
+       </property>
+      </widget>
+     </item>
+    </layout>
+   </item>
+   <item>
+    <widget class="QListWidget" name="books">
+     <property name="sizePolicy">
+      <sizepolicy hsizetype="Minimum" vsizetype="Expanding">
+       <horstretch>0</horstretch>
+       <verstretch>0</verstretch>
+      </sizepolicy>
+     </property>
+     <property name="frameShape">
+      <enum>QFrame::StyledPanel</enum>
+     </property>
+     <property name="tabKeyNavigation">
+      <bool>true</bool>
+     </property>
+     <property name="alternatingRowColors">
+      <bool>true</bool>
+     </property>
+     <property name="selectionRectVisible">
+      <bool>true</bool>
+     </property>
+     <property name="currentRow">
+      <number>-1</number>
+     </property>
+     <property name="sortingEnabled">
+      <bool>true</bool>
+     </property>
+    </widget>
+   </item>
+  </layout>
+ </widget>
+ <resources/>
+ <connections/>
+</ui>