Sfoglia il codice sorgente

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 anni fa
parent
commit
1c2280dc47
1 ha cambiato i file con 46 aggiunte e 14 eliminazioni
  1. 46
    14
      pd-buddy-gtk.py

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

6
 import serial.tools.list_ports
6
 import serial.tools.list_ports
7
 import gi
7
 import gi
8
 gi.require_version('Gtk', '3.0')
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
 def update_header_func(row, before, data):
12
 def update_header_func(row, before, data):
37
     return answer
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
 class SelectListRow(Gtk.ListBoxRow):
77
 class SelectListRow(Gtk.ListBoxRow):
41
 
78
 
42
-    def __init__(self, serial_port):
79
+    def __init__(self, lrm):
43
         Gtk.EventBox.__init__(self)
80
         Gtk.EventBox.__init__(self)
44
 
81
 
45
-        self.serial_port = serial_port
82
+        self.serial_port = lrm.serport
46
 
83
 
47
         self._builder = Gtk.Builder()
84
         self._builder = Gtk.Builder()
48
         self._builder.add_from_file("data/select-list-row.ui")
85
         self._builder.add_from_file("data/select-list-row.ui")
49
         self._builder.connect_signals(self)
86
         self._builder.connect_signals(self)
50
 
87
 
51
         name = self._builder.get_object("name")
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
         device = self._builder.get_object("device")
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
         self.add(self._builder.get_object("grid"))
94
         self.add(self._builder.get_object("grid"))
58
         self.show_all()
95
         self.show_all()
70
         # Get the list
107
         # Get the list
71
         sl = self.builder.get_object("select-list")
108
         sl = self.builder.get_object("select-list")
72
         ss = self.builder.get_object("select-stack")
109
         ss = self.builder.get_object("select-stack")
110
+        se = self.builder.get_object("select-none")
73
         sf = self.builder.get_object("select-frame")
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
         # Add separators to the list
117
         # Add separators to the list
82
         sl.set_header_func(update_header_func, None)
118
         sl.set_header_func(update_header_func, None)
135
         st.set_visible_child(select)
171
         st.set_visible_child(select)
136
 
172
 
137
     def on_header_sink_save_clicked(self, button):
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
         rev = self.builder.get_object("header-sink-save-revealer")
174
         rev = self.builder.get_object("header-sink-save-revealer")
143
         rev.set_reveal_child(False)
175
         rev.set_reveal_child(False)
144
 
176
 

Loading…
Annulla
Salva