qt: add scroll
authorNiki Roo <niki@nikiroo.be>
Thu, 27 Mar 2025 19:34:38 +0000 (20:34 +0100)
committerNiki Roo <niki@nikiroo.be>
Thu, 27 Mar 2025 19:34:38 +0000 (20:34 +0100)
gamiki/qt/tiles.py

index bcb3ee02c1aed60735660b751467d47ddafb30e5..cc32bed2dc5585ba8d68ef7c7af0633d132e20d2 100755 (executable)
@@ -1,6 +1,6 @@
 #!/usr/bin/env python3
 
-# TODO: add QScrollArea
+# TODO: disable start() when one is running
 
 from sys     import argv, exit
 from enum    import IntFlag
@@ -32,7 +32,11 @@ class ListItem(QWidget):
         
         self.game = game
         self.coverSize = coverSize if (coverSize > 0) else 48  
-        self.pixmap=QPixmap(self.game["Icon"] if ("Icon" in self.game) else "")
+        
+        if (self.game.icon):
+            self.pixmap = QPixmap(self.game.get_icon("icon").as_posix())
+        else:
+            self.pixmap = QPixmap("")
        
         self.initUI()
         makeMouseAware(self, [self, self.cover])
@@ -94,7 +98,11 @@ class GridItem(QWidget):
         self.game = game
         self.coverSize = coverSize if (coverSize > 0) else 200 
         self.fixed = fixed
-        self.pixmap=QPixmap(self.game["Icon"] if ("Icon" in self.game) else "")
+        
+        if (self.game.icon):
+            self.pixmap = QPixmap(self.game.get_icon("square").as_posix())
+        else:
+            self.pixmap = QPixmap("")
        
         self.initUI()
         makeMouseAware(self, [self, self.cover])
@@ -177,38 +185,41 @@ class List(QWidget):
         self.initUI()
     
     def initUI(self):
-        layout = QVBoxLayout()
-        #layout.setSpacing(0)
+        area = QWidget()
+        layout = QVBoxLayout(area)
+        layout.setContentsMargins(0, 0, 0, 0)
         
         for game in self.games:
             cover = ListItem(game, self.icon_size)
             cover.onClick.connect(lambda game: self.onClick.emit(game))
             layout.addWidget(cover)
-        
         layout.addStretch()
         
-        self.setLayout(layout)
+        self._setScrollLayout(area)
     
-class Grid(QWidget):
-    onClick = pyqtSignal(Game)
+    def _setScrollLayout(self, area):
+        layout = QVBoxLayout()
+        layout.setContentsMargins(0, 0, 0, 0)
 
-    def __init__(self, games: list[Game], icon_size: int = 0):
-        super().__init__()
-        
-        self.games = games
-        self.icon_size = icon_size
-        
-        self.initUI()
-        
+        scroll = QScrollArea(self)
+        scroll.setWidgetResizable(True)
+        scroll.setWidget(area)
+    
+        layout.addWidget(scroll)
+    
+        self.setLayout(layout)    
+    
+class Grid(List):
     def initUI(self):
-        layout = FlowLayout()
+        area = QWidget()
+        layout = FlowLayout(area)
         
         for game in self.games:
             cover = GridItem(game, self.icon_size, True)
             cover.onClick.connect(lambda game: self.onClick.emit(game))
             layout.addWidget(cover)
         
-        self.setLayout(layout)
+        self._setScrollLayout(area)
 
 if __name__ == '__main__':
     app = QApplication(argv)