diff python/st-util.py @ 130:654093a9a1e3

Added icons, improved device explorer
author Windel Bouwman
date Sat, 19 Jan 2013 18:16:04 +0100
parents 9e350a7dde98
children 04e45faafd1d
line wrap: on
line diff
--- a/python/st-util.py	Fri Jan 18 12:52:11 2013 +0100
+++ b/python/st-util.py	Sat Jan 19 18:16:04 2013 +0100
@@ -21,7 +21,7 @@
    def __init__(self):
       super().__init__()
       self.chipPixmap = QPixmap('chip.png').scaled(32, 32)
-      self.usbPixmap = QPixmap('usb.png').scaled(32, 32)
+      self.hardwarePixmap = QPixmap('hardware.png').scaled(32, 32)
       self.refresh()
    def refresh(self):
       """ Check all usb interfaces for interfaces """
@@ -34,7 +34,6 @@
          return
       self.devices.append(device)
       self.modelReset.emit()
-
    def index(self, row, column, parent):
       if parent.isValid():
          ip = parent.internalPointer()
@@ -42,7 +41,6 @@
             devs = [d for d in self.devices if d.iface is ip]
             return self.createIndex(row, column, devs[row])
       else:
-         # root level
          iface = self.interfaces[row]
          return self.createIndex(row, column, iface)
       return idx
@@ -76,10 +74,54 @@
             return str(ip)
          elif role == Qt.DecorationRole:
             if isinstance(ip, Interface):
-               return self.usbPixmap
+               return self.hardwarePixmap
             if isinstance(ip, Device):
                return self.chipPixmap
 
+class DeviceExplorer(QTreeView):
+   """ Lists all interfaces plugged in and allows selection """
+   deviceSelected = pyqtSignal(Device)
+   def __init__(self):
+      super().__init__()
+      self.mdl = DeviceTreeModel()
+      self.setModel(self.mdl)
+      self.activated.connect(self.openItem)
+      self.header().close()
+      self.customContextMenuRequested.connect(self.openMenu)
+      self.setContextMenuPolicy(Qt.CustomContextMenu)
+   def openItem(self, idx):
+      if idx.isValid():
+         ip = idx.internalPointer()
+         if isinstance(ip, Interface):
+            if not ip.IsOpen:
+               ip.open()
+               # Try to get a device:
+               self.mdl.addDevice(ip.createDevice())
+         if isinstance(ip, Device):
+            self.deviceSelected.emit(ip)
+   def openMenu(self, pt):
+      idx = self.indexAt(pt)
+      menu = QMenu()
+      menu.addAction('Refresh', self.mdl.refresh)
+      if idx.isValid():
+         ip = idx.internalPointer()
+         if isinstance(ip, Interface):
+            if ip.IsOpen:
+               def closeInterface():
+                  self.mdl.closeInterface(ip)
+               menu.addAction('Close', closeInterface)
+            else:
+               def openInterface():
+                  ip.open()
+                  # Try to get a device:
+                  self.mdl.addDevice(ip.createDevice())
+               menu.addAction('Open', openInterface)
+         elif isinstance(ip, Device):
+            def selectDevice():
+               self.deviceSelected.emit(ip)
+            menu.addAction('Select', selectDevice)
+      menu.exec(self.mapToGlobal(pt))
+
 class StUtil(QMainWindow):
    connected = pyqtSignal(bool)
    def __init__(self):
@@ -104,23 +146,15 @@
 
       sb = self.statusBar()
 
-      tv = QTreeView()
-      self.setCentralWidget(tv)
-      self.mdl = DeviceTreeModel()
-      tv.setModel(self.mdl)
-      tv.activated.connect(self.openItem)
+      devexplr = DeviceExplorer()
+      self.setCentralWidget(devexplr)
+      devexplr.deviceSelected.connect(self.handleDeviceSelected)
 
       self.connected.connect(self.handleConnectedChange)
       self.connected.emit(False)
-   def openItem(self, idx):
-      if idx.isValid():
-         ip = idx.internalPointer()
-         if isinstance(ip, Interface):
-            ip.open()
-            # Try to get a device:
-            self.mdl.addDevice(ip.createDevice())
-         if isinstance(ip, Device):
-            self.device = ip
+   def handleDeviceSelected(self, dev):
+      self.dev = dev
+      self.handleConnectedChange(True)
    def handleConnectedChange(self, state):
       self.miscMenu.setEnabled(state)
       self.connectAction.setEnabled(not state)