# HG changeset patch
# User Dan Smith <dsmith@danplanet.com>
# Date 1305832068 25200
# Node ID d309a938247a28a8f7bcefe6e83968332e771bb5
# Parent  c81b33ae97a70f40c57001ce400f4dfbc71d336c
Multiple banks-per-memory support for the UI

diff -r c81b33ae97a7 -r d309a938247a chirpui/editorset.py
--- a/chirpui/editorset.py	Thu May 19 11:21:55 2011 -0700
+++ b/chirpui/editorset.py	Thu May 19 12:07:48 2011 -0700
@@ -20,8 +20,8 @@
 import gobject
 
 from chirp import chirp_common, directory, csv, xml
-from chirpui import memedit, dstaredit, bankedit, common, importdialog
-from chirpui import inputdialog, reporting
+from chirpui import memedit, dstaredit, bankedit, membankedit
+from chirpui import inputdialog, reporting, common, importdialog
 
 class EditorSet(gtk.VBox):
     __gsignals__ = {
@@ -69,6 +69,8 @@
         else:
             self.banked = None
 
+        self.membanked = membankedit.MemoryBankEditor(self.rthread)
+
         lab = gtk.Label("Memories")
         self.tabs.append_page(self.memedit.root, lab)
         self.memedit.root.show()
@@ -79,8 +81,14 @@
             self.dstared.root.show()
             self.dstared.connect("changed", self.dstar_changed)
 
+        if self.membanked:
+            lab = gtk.Label("Banks")
+            self.tabs.append_page(self.membanked.root, lab)
+            self.membanked.root.show()
+            #self.membanked.connect("changed", self.
+
         if self.banked:
-            lab = gtk.Label("Banks")
+            lab = gtk.Label("Bank Names")
             self.tabs.append_page(self.banked.root, lab)
             self.banked.root.show()
             self.banked.connect("changed", self.banks_changed)
@@ -278,7 +286,7 @@
 
     def tab_selected(self, notebook, foo, pagenum):
         # Quick hack for D-STAR editor
-        if pagenum == 1:
+        if pagenum == 1 and self.dstared: # Yikes, this needs fixing
             self.dstared.focus()
 
     def set_read_only(self, read_only=True):
diff -r c81b33ae97a7 -r d309a938247a chirpui/membankedit.py
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/chirpui/membankedit.py	Thu May 19 12:07:48 2011 -0700
@@ -0,0 +1,133 @@
+#!/usr/bin/python
+#
+# Copyright 2011 Dan Smith <dsmith@danplanet.com>
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+import gtk
+import gobject
+
+from chirpui import common, miscwidgets
+
+LABEL_COLS = ["_used", "Loc", "Name", "Frequency"]
+
+class MemoryBankEditor(common.Editor):
+    def _render_member(self, _, rend, model, iter, colnum):
+        active, = model.get(iter, colnum)
+        rend.set_property("stock-id", active and gtk.STOCK_OK or 0)
+
+    def _clicked(self, view, event):
+        x, y = event.get_coords()
+        path = view.get_path_at_pos(int(x), int(y))
+        col = view.get_columns().index(path[1]) + 1 # For the empty
+        if col < len(LABEL_COLS):
+            return
+        iter = self._store.get_iter(path[0])
+        inuse, number, active = self._store.get(iter, 0,
+                                                LABEL_COLS.index("Loc"),
+                                                col)
+        if inuse:
+            bank = col - len(LABEL_COLS)
+            self._store.set(iter, col, not active)
+            print "FIXME: Add %i to Bank %i" % (number, bank)
+
+    def __make_view_store(self):
+        num = len(self._bank_list)
+
+        cols = (gobject.TYPE_BOOLEAN, # Empty
+                gobject.TYPE_INT,     # Number
+                gobject.TYPE_STRING,  # Name
+                gobject.TYPE_FLOAT)   # Frequency
+        cols += tuple([gobject.TYPE_BOOLEAN for x in range(0, num)])
+        self._store = gtk.ListStore(*cols)
+        self._view = gtk.TreeView(self._store)
+
+        for label in LABEL_COLS:
+            if label.startswith("_"):
+                continue
+            r = gtk.CellRendererText()
+            r.set_property("editable", False)
+            c = gtk.TreeViewColumn(label, r,
+                                   text=LABEL_COLS.index(label),
+                                   sensitive=0)
+            self._view.append_column(c)
+
+        for i in range(0, num):
+            r = gtk.CellRendererPixbuf()
+            r.set_property("stock-id", 0)
+            c = gtk.TreeViewColumn(self._bank_list[i], r, sensitive=0)
+            c.set_cell_data_func(r, self._render_member, i+len(LABEL_COLS))
+            c.set_resizable(True)
+            self._view.append_column(c)
+
+        self._view.connect("button_press_event", self._clicked)
+        self._view.set_rules_hint(True)
+
+        sw = gtk.ScrolledWindow()
+        sw.add(self._view)
+        sw.show()
+
+        self.root.pack_start(sw, 1, 1, 1)
+        self._view.show()
+
+    def _refresh_membership(self):
+        self._bank_members = {}
+        for i in range(0, len(self._bank_list)):
+            def cb(members):
+                self._bank_members[i] = members
+
+            job = common.RadioJob(cb, "get_bank_channels", i)
+            job.set_desc("Getting information for bank %i" % i)
+            self.rthread.submit(job)
+
+    def _add_memory(self, mem):
+        members = []
+        for bank, members in self._bank_members.items():
+            if mem.number in members:
+                members.append(bank)
+        
+        flags = [x in members for x in range(0, len(self._bank_list))]
+
+        iter = self._store.append((not mem.empty,
+                                   mem.number, mem.name, mem.freq) + \
+                                  tuple(flags))
+
+    def _refresh_memories(self):
+        self._store.clear()
+        rf = self.rthread.radio.get_features()
+        for i in range(0, 10):
+            job = common.RadioJob(self._add_memory, "get_memory", i)
+            job.set_desc("Getting memory %i (again)" % i)
+            self.rthread.submit(job)
+
+    def __init__(self, rthread):
+        common.Editor.__init__(self)
+        self.rthread = rthread
+
+        self._bank_list = []
+        self._bank_members = {}
+        self._store = None
+        self._view = None
+
+        self.root = gtk.VBox(3, False)
+
+        def cb(banks):
+            self._bank_list = list(banks)
+            self.__make_view_store()
+            self._refresh_membership()
+            self._refresh_memories()
+
+        job = common.RadioJob(cb, "get_banks")
+        job.set_desc("Retrieving bank list")
+        self.rthread.submit(job)
