[caribou: 1/15] Add Column model. Use Gee collections (I give up).



commit 458fb0f30376fa795828bdbf550730057a3c1e24
Author: Eitan Isaacson <eitan monotonous org>
Date:   Sat May 28 12:52:20 2011 -0700

    Add Column model. Use Gee collections (I give up).

 configure.ac                      |    3 +-
 libcaribou/Makefile.am            |    5 ++-
 libcaribou/column-model.vala      |   25 ++++++++++++++++++++
 libcaribou/group-model.vala       |    2 +-
 libcaribou/json-deserializer.vala |   36 +++++++++++++++++-----------
 libcaribou/key-model.vala         |   13 +++++-----
 libcaribou/keyboard-model.vala    |    4 +-
 libcaribou/level-model.vala       |   46 +++++++++++++++++++++++++------------
 libcaribou/row-model.vala         |   37 +++++++++++++++++++++++------
 9 files changed, 122 insertions(+), 49 deletions(-)
---
diff --git a/configure.ac b/configure.ac
index 3c0b55c..36c9983 100644
--- a/configure.ac
+++ b/configure.ac
@@ -45,7 +45,8 @@ PKG_CHECK_MODULES(LIBCARIBOU, [
   xtst,
   x11,
   libxklavier,
-  json-glib-1.0
+  json-glib-1.0,
+  gee-1.0
   ])
 AC_SUBST(LIBCARIBOU_CFLAGS)
 AC_SUBST(LIBCARIBOU_LIBS)
diff --git a/libcaribou/Makefile.am b/libcaribou/Makefile.am
index 9e157e9..ad7af54 100644
--- a/libcaribou/Makefile.am
+++ b/libcaribou/Makefile.am
@@ -9,8 +9,8 @@ libcaribou_la_VALAFLAGS = \
 	-H caribou.h --vapi caribou-1.0.vapi \
 	-h caribou-internals.h \
 	--vapidir=. \
-	--pkg x11 --pkg json-glib-1.0 --pkg gdk-3.0 --pkg gio-2.0 \
-	--pkg libxklavier --pkg external-libs  --pkg gdk-x11-3.0 \
+	--pkg x11 --pkg json-glib-1.0 --pkg gdk-3.0 --pkg gio-2.0 --pkg gee-1.0 \
+	--pkg libxklavier --pkg external-libs --pkg gdk-x11-3.0 \
 	--internal-vapi caribou-internals-1.0.vapi \
 	--library caribou-1.0 --gir _Caribou-1.0.gir \
 	$(VALAFLAGS)
@@ -37,6 +37,7 @@ libcaribou_la_SOURCES = \
 	level-model.vala \
 	row-model.vala \
 	key-model.vala \
+	column-model.vala \
 	util.vala \
 	json-deserializer.vala
 
diff --git a/libcaribou/column-model.vala b/libcaribou/column-model.vala
new file mode 100644
index 0000000..c97b700
--- /dev/null
+++ b/libcaribou/column-model.vala
@@ -0,0 +1,25 @@
+namespace Caribou {
+    public class ColumnModel : Object {
+        Gee.ArrayList<KeyModel> keys;
+
+        public ColumnModel () {
+            keys = new Gee.ArrayList<KeyModel> ();
+        }
+
+        internal void add_key (KeyModel key) {
+            keys.add (key);
+        }
+
+        public KeyModel get_key (int index) {
+            return keys.get (index);
+        }
+
+        public KeyModel[] get_keys () {
+            return (KeyModel[]) keys.to_array ();
+        }
+
+        public KeyModel first_key () {
+            return keys.first();
+        }
+   }
+}
diff --git a/libcaribou/group-model.vala b/libcaribou/group-model.vala
index 564dbd8..965cb96 100644
--- a/libcaribou/group-model.vala
+++ b/libcaribou/group-model.vala
@@ -23,7 +23,7 @@ namespace Caribou {
                 return group;
         }
 
-        public void add_level (string lname, LevelModel level) {
+        internal void add_level (string lname, LevelModel level) {
             levels.insert (lname, level);
             level.level_toggled.connect(on_level_toggled);
             if (level.mode == "default") {
diff --git a/libcaribou/json-deserializer.vala b/libcaribou/json-deserializer.vala
index e136042..818655c 100644
--- a/libcaribou/json-deserializer.vala
+++ b/libcaribou/json-deserializer.vala
@@ -46,7 +46,7 @@ namespace Caribou {
             throw new IOError.NOT_FOUND (
                 "Could not find layout file for %s %s", group, variant);                       }
 
-        public static void load_group (GroupModel group) {
+        public static bool load_group (GroupModel group) {
             Json.Parser parser = new Json.Parser ();
 
             try {
@@ -55,8 +55,10 @@ namespace Caribou {
                 create_levels_from_json (group, parser.get_root ());
             } catch (GLib.Error e) {
                 stdout.printf ("Failed to load JSON: %s\n", e.message);
-                return;
+                return false;
             }
+
+            return true;
         }
 
         public static void create_levels_from_json (GroupModel group,
@@ -74,26 +76,32 @@ namespace Caribou {
                     mode = json_level.get_string_member ("mode");
 
                 Json.Array json_rows = json_level.get_array_member ("rows");
-                LevelModel level = new LevelModel(mode, json_rows.get_length ());
+                LevelModel level = new LevelModel(mode);
+                load_rows (level, json_rows);
 
                 group.add_level(levelname, level);
-
-                load_rows (level, json_rows);
             }
         }
 
         public static void load_rows (LevelModel level, Json.Array json_rows) {
-            uint i,j;
-
-            for (i=0;i<level.n_rows;i++) {
-                Json.Array json_keys = json_rows.get_array_element (i);
-                uint nkeys = json_keys.get_length ();
-                for (j=0;j<nkeys;j++) {
-                    Json.Object json_key = json_keys.get_object_element (j);
-                    level.add_key (i, load_key (json_key));
+            for (int i=0;i<json_rows.get_length ();i++) {
+                Json.Array json_children = json_rows.get_array_element (i);
+                unowned Json.Node child = json_children.get_element(0);
+                if (child.get_node_type () == Json.NodeType.OBJECT) {
+                    for (int j=0;j<json_children.get_length ();j++) {
+                        Json.Object json_key = json_children.get_object_element (j);
+                        level.add_key (i, 0, load_key (json_key));
+                    }
+                } else if (child.get_node_type () == Json.NodeType.ARRAY) {
+                    for (int k=0;k<json_children.get_length ();k++) {
+                        Json.Array json_keys = json_children.get_array_element (k);
+                        for (int j=0;j<json_keys.get_length ();j++) {
+                            Json.Object json_key = json_keys.get_object_element (j);
+                            level.add_key (i, k, load_key (json_key));
+                        }
+                    }
                 }
             }
-
         }
 
         public static KeyModel load_key (Json.Object json_key) {
diff --git a/libcaribou/key-model.vala b/libcaribou/key-model.vala
index 90f3625..a4d437f 100644
--- a/libcaribou/key-model.vala
+++ b/libcaribou/key-model.vala
@@ -12,7 +12,7 @@ namespace Caribou {
 
         private uint hold_tid;
         private XAdapter xadapter;
-        private List<KeyModel> _extended_keys;
+        private Gee.ArrayList<KeyModel> extended_keys;
 
         public signal void key_pressed ();
         public signal void key_released ();
@@ -24,12 +24,13 @@ namespace Caribou {
             this.name = name;
             xadapter = XAdapter.get_default();
             keyval = Gdk.keyval_from_name (name);
+            extended_keys = new Gee.ArrayList<KeyModel> ();
         }
 
-        public void add_subkey (string name) {
+        internal void add_subkey (string name) {
             KeyModel key = new KeyModel (name);
             key.key_clicked.connect(on_subkey_clicked);
-            _extended_keys.append (key);
+            extended_keys.add (key);
         }
 
         private void on_subkey_clicked () {
@@ -58,14 +59,14 @@ namespace Caribou {
 
         private bool on_key_held () {
             hold_tid = 0;
-            if (_extended_keys.length () != 0)
+            if (extended_keys.size != 0)
                 show_subkeys = true;
             key_hold ();
             return false;
         }
 
-        public unowned List<KeyModel> get_extended_keys () {
-            return _extended_keys;
+        public KeyModel[] get_extended_keys () {
+            return (KeyModel[]) extended_keys.to_array ();
         }
     }
 }
\ No newline at end of file
diff --git a/libcaribou/keyboard-model.vala b/libcaribou/keyboard-model.vala
index db19dd9..0e2fd1e 100644
--- a/libcaribou/keyboard-model.vala
+++ b/libcaribou/keyboard-model.vala
@@ -29,8 +29,8 @@ namespace Caribou {
 
         private void populate_group (string group, string variant) {
             GroupModel grp = new GroupModel (group, variant);
-            groups.insert (GroupModel.create_group_name (group, variant), grp);
-            JsonDeserializer.load_group (grp);
+            if (JsonDeserializer.load_group (grp))
+                groups.insert (GroupModel.create_group_name (group, variant), grp);
         }
 
         public string[] get_groups () {
diff --git a/libcaribou/level-model.vala b/libcaribou/level-model.vala
index 321a355..c80490e 100644
--- a/libcaribou/level-model.vala
+++ b/libcaribou/level-model.vala
@@ -3,31 +3,36 @@ using GLib;
 namespace Caribou {
     public class LevelModel : GLib.Object {
         public signal void level_toggled (string new_level);
-
         public string mode { get; private set; default = ""; }
-        public int n_rows {
-            get {
-                return _rows.length;
-            }
-        }
 
-        private RowModel[] _rows;
+        private Gee.ArrayList<RowModel> rows;
 
-        public LevelModel (string mode, uint nrows) {
-            uint i;
+        public LevelModel (string mode) {
             this.mode = mode;
-            _rows = new RowModel[nrows];
-            for (i=0;i<nrows;i++)
-                _rows[i] = new RowModel ();
+            rows = new Gee.ArrayList<RowModel> ();
         }
 
-        public void add_key (uint rownum, KeyModel key) {
+        internal void add_key (int rownum, int colnum, KeyModel key) {
+            int rowindex = rownum;
+            RowModel row = null;
+
+            if (rownum < 0)
+                rowindex = rows.size + rownum;
+
+            if (rownum >= rows.size) {
+                row = new RowModel ();
+                rows.add(row);
+            } else {
+                row = rows[rowindex];
+            }
+
+            row.add_key (colnum, key);
+
             key.key_clicked.connect (on_key_clicked);
-            _rows[rownum].add_key (key);
         }
 
         public RowModel[] get_rows () {
-            return _rows;
+            return (RowModel[]) rows.to_array ();
         }
 
         private void on_key_clicked (KeyModel key) {
@@ -37,5 +42,16 @@ namespace Caribou {
                 level_toggled ("default");
         }
 
+        public KeyModel[] get_keys () {
+            Gee.ArrayList<KeyModel> keys = new Gee.ArrayList<KeyModel> ();
+            foreach (RowModel row in rows) {
+                KeyModel[] row_keys = row.get_keys();
+                foreach (KeyModel key in row_keys) {
+                    keys.add(key);
+                }
+            }
+
+            return (KeyModel[]) keys.to_array ();
+        }
     }
 }
\ No newline at end of file
diff --git a/libcaribou/row-model.vala b/libcaribou/row-model.vala
index de4456c..291d76c 100644
--- a/libcaribou/row-model.vala
+++ b/libcaribou/row-model.vala
@@ -1,22 +1,43 @@
 namespace Caribou {
     public class RowModel : GLib.Object {
 
-        List<KeyModel> keys;
+        Gee.ArrayList<ColumnModel> columns;
 
         public RowModel () {
-            keys = new List<KeyModel> ();
+            columns = new Gee.ArrayList<ColumnModel> ();
         }
 
-        public void add_key (KeyModel key) {
-            keys.append (key);
+        internal void add_key (int colnum, KeyModel key) {
+            int colindex = colnum;
+            ColumnModel column = null;
+
+            if (colnum < 0)
+                colindex = columns.size + colnum;
+
+            if (colnum >= columns.size) {
+                column = new ColumnModel ();
+                columns.add(column);
+            } else {
+                column = columns[colindex];
+            }
+
+            column.add_key (key);
         }
 
-        public KeyModel get_key (uint index) {
-            return keys.nth (index).data;
+        public KeyModel[] get_keys () {
+            Gee.ArrayList<KeyModel> keys = new Gee.ArrayList<KeyModel> ();
+            foreach (ColumnModel column in columns) {
+                KeyModel[] col_keys = column.get_keys();
+                foreach (KeyModel key in col_keys) {
+                    keys.add(key);
+                }
+            }
+            return (KeyModel[]) keys.to_array ();
         }
 
-        public unowned List<weak KeyModel> get_keys () {
-            return keys;
+        public ColumnModel[] get_columns () {
+            return (ColumnModel[]) columns.to_array ();
         }
+
     }
 }



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]