Commits

Henning Schröder committed 6ddd80f

firefox addon manager example

Comments (0)

Files changed (2)

qtwidgets/widgetlist.py

 
     def __init__(self, parent=None):
         QTableWidget.__init__(self, 0, 1, parent)
+        self.setSelectionMode(QAbstractItemView.SingleSelection)
         self.verticalHeader().hide()
         self.horizontalHeader().hide()
         self.horizontalHeader().setResizeMode(QHeaderView.Custom)
         #self.horizontalHeader().setStretchLastSection(True)
         self.items = {}
+        self.connect(self, SIGNAL("clicked(const QModelIndex&)"), self.on_clicked)
+
+        
+    def on_clicked(self, index):
+        widget = self.indexWidget(index)
+        self.emit(SIGNAL("widget_clicked"), widget)
 
 
     def add_widget(self, widget):
         self.setIndexWidget(self.indexFromItem(item), widget)
         self.verticalHeader().resizeSection(pos, widget.height())
         self.items[widget] = item
+        widget.setMinimumHeight(widget.height())
+        widget.installEventFilter(self)
         return item
 
+    
+    def eventFilter(self, widget, event):
+        if isinstance(event, QResizeEvent):
+            item = self.item(widget)
+            index = self.indexFromItem(item)
+            self.verticalHeader().resizeSection(index.row(), widget.height())
+        return False
+
 
     def widget(self, item):
         return self.indexWidget(self.indexFromItem(item))

test/test_widgetlist.py

 
 
 def test_widgetlist():
+    # Firefox-like add-list
+    class AddOn(QWidget):
+        active = None
+        
+        def __init__(self, parent, name):
+            QWidget.__init__(self, parent)
+            self.hblayout = QHBoxLayout(self)
+            self.label = QLabel("Addon <b>%s</b>" % name, self)
+            
+            self.hblayout.addWidget(self.label)
+            self.hblayout.addStretch()
+            
+            self.buttons = QWidget(self)
+            self.hblayout.addWidget(self.buttons)
+            self.vblayout = QVBoxLayout(self.buttons)
+            
+            self.prefs = QPushButton("Preferences", self.buttons)
+            self.vblayout.addWidget(self.prefs)
+            self.uninstall = QPushButton("Uninstall", self.buttons)
+            self.vblayout.addWidget(self.uninstall)
+            
+            self.buttons.hide()            
+            self.setFixedHeight(self.sizeHint().height())
+           
+        def show_actions(self):
+            self.buttons.show()
+            self.setFixedHeight(self.sizeHint().height())
+            self.parent().update()
+            
+        def hide_actions(self):
+            self.buttons.hide()
+            self.setFixedHeight(self.sizeHint().height())
+
     import sys
     app = QApplication(sys.argv)
     win = WidgetList()
     win.resize(640,480)
     win.show()
-    b = QPushButton("Click me", win)
-    win.add_widget(b)
-    l = QLabel("<h1>Hello, world!</h1>", win)
+    def toggle(add_on):
+        if AddOn.active:
+            AddOn.active.hide_actions()
+        add_on.show_actions()
+        AddOn.active = add_on
+
+    win.connect(win, SIGNAL("widget_clicked"), toggle)
+    add_ons = []
+    for i in range(5):
+        a = AddOn(win, i)
+        win.add_widget(a)
+        add_ons.append(a)
+        #print a.sizeHint(), a.size(), a.label.size(), a.uninstall.size(), a.prefs.size()
+    l = QLabel("<a href='http://www.google.de'>Updates available</a>", win)
     win.insert_widget(0, l)
     app.exec_()
     print win.current_row_index()
 
-    
+
+
 if __name__ == "__main__":
     test_widgetlist()