Browse Source

Made the device list self-updating

Previously, the device list only loaded once on startup.  This was poor
behaviour, as devices can be attached and detached at any time.

Now the attached devices are polled once per second, and the list is
updated accordingly.  The code behind it is a bit messy because the
list model interacts directly with parts of the GUI, but it works
pretty well nevertheless.  There's no mechanism to kick us out of the
configuration screen if the device we're configuring is detached, but
this can be addressed later.  For now, I'm reasonably happy with this
new feature.
Clara Hobbs 7 years ago
parent
commit
1c2280dc47
1 changed files with 46 additions and 14 deletions
  1. 46
    14
      pd-buddy-gtk.py

+ 46
- 14
pd-buddy-gtk.py View File

@@ -6,7 +6,7 @@ import serial
6 6
 import serial.tools.list_ports
7 7
 import gi
8 8
 gi.require_version('Gtk', '3.0')
9
-from gi.repository import Gtk
9
+from gi.repository import Gtk, Gio, GObject, GLib
10 10
 
11 11
 
12 12
 def update_header_func(row, before, data):
@@ -37,22 +37,59 @@ def pdb_send_message(sp, message):
37 37
     return answer
38 38
 
39 39
 
40
+class ListRowModel(GObject.GObject):
41
+
42
+    def __init__(self, serport):
43
+        GObject.GObject.__init__(self)
44
+        self.serport = serport
45
+
46
+
47
+class SelectListStore(Gio.ListStore):
48
+
49
+    def __init__(self, stack, list_empty, list_frame):
50
+        Gio.ListStore.__init__(self)
51
+
52
+        self.stack = stack
53
+        self.list_empty = list_empty
54
+        self.list_frame = list_frame
55
+
56
+        self.update_items()
57
+        GLib.timeout_add(1000, self.update_items)
58
+
59
+    def update_items(self):
60
+        # Clear the old entries
61
+        self.remove_all()
62
+
63
+        # Search for the serial ports
64
+        for serport in serial.tools.list_ports.grep("1209:0001"):
65
+            self.append(ListRowModel(serport))
66
+
67
+        # Set the visible child
68
+        # FIXME: This is rather poor organization
69
+        if self.get_n_items():
70
+            self.stack.set_visible_child(self.list_frame)
71
+        else:
72
+            self.stack.set_visible_child(self.list_empty)
73
+
74
+        return True
75
+
76
+
40 77
 class SelectListRow(Gtk.ListBoxRow):
41 78
 
42
-    def __init__(self, serial_port):
79
+    def __init__(self, lrm):
43 80
         Gtk.EventBox.__init__(self)
44 81
 
45
-        self.serial_port = serial_port
82
+        self.serial_port = lrm.serport
46 83
 
47 84
         self._builder = Gtk.Builder()
48 85
         self._builder.add_from_file("data/select-list-row.ui")
49 86
         self._builder.connect_signals(self)
50 87
 
51 88
         name = self._builder.get_object("name")
52
-        name.set_text(serial_port.description)
89
+        name.set_text(self.serial_port.description)
53 90
 
54 91
         device = self._builder.get_object("device")
55
-        device.set_text(serial_port.device)
92
+        device.set_text(self.serial_port.device)
56 93
 
57 94
         self.add(self._builder.get_object("grid"))
58 95
         self.show_all()
@@ -70,13 +107,12 @@ class Handler:
70 107
         # Get the list
71 108
         sl = self.builder.get_object("select-list")
72 109
         ss = self.builder.get_object("select-stack")
110
+        se = self.builder.get_object("select-none")
73 111
         sf = self.builder.get_object("select-frame")
74 112
 
75
-        # Search for the serial ports
76
-        for serport in serial.tools.list_ports.grep("1209:0001"):
77
-            # Show the list if we have a serial port
78
-            ss.set_visible_child(sf)
79
-            sl.insert(SelectListRow(serport), -1)
113
+        liststore = SelectListStore(ss, se, sf)
114
+
115
+        sl.bind_model(liststore, SelectListRow)
80 116
 
81 117
         # Add separators to the list
82 118
         sl.set_header_func(update_header_func, None)
@@ -135,10 +171,6 @@ class Handler:
135 171
         st.set_visible_child(select)
136 172
 
137 173
     def on_header_sink_save_clicked(self, button):
138
-        combo = self.builder.get_object("voltage-combobox")
139
-        spin = self.builder.get_object("current-spinbutton")
140
-        print("{} V".format(combo.get_active_text()))
141
-        print("{} A".format(spin.get_value()))
142 174
         rev = self.builder.get_object("header-sink-save-revealer")
143 175
         rev.set_reveal_child(False)
144 176
 

Loading…
Cancel
Save