[dconf] editor: Show key range in key description



commit adb33f2bb3f57e66bd607892467d3234e9ddb04b
Author: Robert Ancell <robert ancell canonical com>
Date:   Thu Mar 10 15:16:15 2011 +1100

    editor: Show key range in key description

 editor/dconf-editor.vala |   36 +++++++++++++++++++------
 editor/dconf-model.vala  |   50 ++++++++++++++++++++++++++++++++++++
 editor/dconf-schema.vala |   59 +++++++++++++++++++++---------------------
 editor/dconf-view.vala   |   64 +++++++++++++++++++++++-----------------------
 4 files changed, 139 insertions(+), 70 deletions(-)
---
diff --git a/editor/dconf-editor.vala b/editor/dconf-editor.vala
index d3b1e63..eb64233 100644
--- a/editor/dconf-editor.vala
+++ b/editor/dconf-editor.vala
@@ -80,20 +80,38 @@ class ConfigurationEditor
             key_tree_view.get_selection().select_iter(iter);
     }
 
-    private string type_to_description(string type)
+    private string key_to_description(Key key)
     {
-        switch(type)
+        switch(key.schema.type)
         {
+        case "y":
+        case "n":
+        case "q":
         case "i":
-           return "Integer";
+        case "u":
+        case "x":
+        case "t":
+        case "d":
+            Variant min, max;
+            if (key.schema.range != null)
+            {
+                min = key.schema.range.min;
+                max = key.schema.range.max;
+            }
+            else
+            {
+                min = key.get_min();
+                max = key.get_max();
+            }
+            return "Integer [%s..%s]".printf(min.print(false), max.print(false));
         case "b":
-           return "Boolean";
+            return "Boolean";
         case "s":
-           return "String";
-        case "enum":
-           return "Enumeration";
+            return "String";
+        case "<enum>":
+            return "Enumeration";
         default:
-           return type;
+            return key.schema.type;
         }
     }
 
@@ -129,7 +147,7 @@ class ConfigurationEditor
                     summary = selected_key.schema.summary;
                 if (selected_key.schema.description != null)
                     description = selected_key.schema.description;
-                type = type_to_description(selected_key.schema.type);
+                type = key_to_description(selected_key);
                 default_value = selected_key.schema.default_value.print(false);
             }
             else
diff --git a/editor/dconf-model.vala b/editor/dconf-model.vala
index d731fdd..960b369 100644
--- a/editor/dconf-model.vala
+++ b/editor/dconf-model.vala
@@ -61,6 +61,56 @@ public class Key : GLib.Object
         }
     }
 
+    public Variant? get_min()
+    {
+        switch (value.classify ())
+        {
+        case Variant.Class.BYTE:
+            return new Variant.byte(0);
+        case Variant.Class.INT16:
+            return new Variant.int16(int16.MIN);
+        case Variant.Class.UINT16:
+            return new Variant.uint16(uint16.MIN);
+        case Variant.Class.INT32:
+            return new Variant.int32(int32.MIN);
+        case Variant.Class.UINT32:
+            return new Variant.uint32(uint32.MIN);
+        case Variant.Class.INT64:
+            return new Variant.int64(int64.MIN);
+        case Variant.Class.UINT64:
+            return new Variant.uint64(uint64.MIN);
+        case Variant.Class.DOUBLE:
+            return new Variant.double(double.MIN);
+        default:
+            return null;
+        }
+    }
+
+    public Variant? get_max()
+    {
+        switch (value.classify ())
+        {
+        case Variant.Class.BYTE:
+            return new Variant.byte(255);
+        case Variant.Class.INT16:
+            return new Variant.int16(int16.MAX);
+        case Variant.Class.UINT16:
+            return new Variant.uint16(uint16.MAX);
+        case Variant.Class.INT32:
+            return new Variant.int32(int32.MAX);
+        case Variant.Class.UINT32:
+            return new Variant.uint32(uint32.MAX);
+        case Variant.Class.INT64:
+            return new Variant.int64(int64.MAX);
+        case Variant.Class.UINT64:
+            return new Variant.uint64(uint64.MAX);
+        case Variant.Class.DOUBLE:
+            return new Variant.double(double.MAX);
+        default:
+            return null;
+        }
+    }
+
     public bool is_default
     {
         get { update_value(); return _value == null; }
diff --git a/editor/dconf-schema.vala b/editor/dconf-schema.vala
index 7bb538f..c67759b 100644
--- a/editor/dconf-schema.vala
+++ b/editor/dconf-schema.vala
@@ -5,12 +5,13 @@ public class SchemaKey
     public string type;
     public Variant default_value;
     public SchemaValueRange? range;
+    public SchemaValueRange type_range;
     public string? enum_name;
     public string? summary;
     public string? description;
     public string? gettext_domain;
 
-    public SchemaKey(Xml.Node* node, Schema schema, string? gettext_domain)
+    public SchemaKey.from_xml(Xml.Node* node, Schema schema, string? gettext_domain)
     {
         this.schema = schema;
         this.gettext_domain = gettext_domain;
@@ -51,7 +52,7 @@ public class SchemaKey
             else if (child->name == "description")
                description = child->children->content;
             else if (child->name == "range")
-               range = new SchemaValueRange (type, child);
+               range = new SchemaValueRange.from_xml(type, child);
             else if (child->type != Xml.ElementType.TEXT_NODE && child->type != Xml.ElementType.COMMENT_NODE)
                 warning ("Unknown child tag in <key>, <%s>", child->name);
         }
@@ -79,34 +80,34 @@ public class SchemaEnumValue : GLib.Object
 
 public class SchemaValueRange
 {
-   public Variant min;
-   public Variant max;
-
-   public SchemaValueRange(string type, Xml.Node* node)
-   {
+    public Variant min;
+    public Variant max;
+   
+    public SchemaValueRange.from_xml(string type, Xml.Node* node)
+    {
         for (var prop = node->properties; prop != null; prop = prop->next)
         {
             if (prop->name == "min")
             {
-               try
-               {
-                   min = Variant.parse(new VariantType(type), prop->children->content);
-               }
-               catch (VariantParseError e)
-               {
-                   // ...
-               }
+                try
+                {
+                    min = Variant.parse(new VariantType(type), prop->children->content);
+                }
+                catch (VariantParseError e)
+                {
+                    // ...
+                }
             }
             else if (prop->name == "max")
             {
-               try
-               {
-                   max = Variant.parse(new VariantType(type), prop->children->content);
-               }
-               catch (VariantParseError e)
-               {
-                   // ...
-               }
+                try
+                {
+                    max = Variant.parse(new VariantType(type), prop->children->content);
+                }
+                catch (VariantParseError e)
+                {
+                    // ...
+                }
             }
             else
                 warning ("Unknown property in <range>, %s", prop->name);
@@ -114,7 +115,7 @@ public class SchemaValueRange
         
         //if (min == null || max == null)
         //    ?
-   }
+    }
 }
 
 public class SchemaEnum
@@ -123,7 +124,7 @@ public class SchemaEnum
     public string id;
     public GLib.List<SchemaEnumValue> values = new GLib.List<SchemaEnumValue>();
 
-    public SchemaEnum(SchemaList list, Xml.Node* node)
+    public SchemaEnum.from_xml(SchemaList list, Xml.Node* node)
     {
         this.list = list;
 
@@ -177,7 +178,7 @@ public class Schema
     public string? path;
     public GLib.HashTable<string, SchemaKey> keys = new GLib.HashTable<string, SchemaKey>(str_hash, str_equal);
 
-    public Schema(SchemaList list, Xml.Node* node, string? gettext_domain)
+    public Schema.from_xml(SchemaList list, Xml.Node* node, string? gettext_domain)
     {
         this.list = list;
 
@@ -200,7 +201,7 @@ public class Schema
         {
             if (child->name != "key")
                continue;
-            var key = new SchemaKey(child, this, gettext_domain);
+            var key = new SchemaKey.from_xml(child, this, gettext_domain);
             keys.insert(key.name, key);
         }
     }
@@ -235,7 +236,7 @@ public class SchemaList
         {
             if (node->name == "schema")
             {
-                Schema schema = new Schema(this, node, gettext_domain);
+                Schema schema = new Schema.from_xml(this, node, gettext_domain);
                 if (schema.path == null)
                 {
                     // FIXME: What to do here?
@@ -251,7 +252,7 @@ public class SchemaList
             }
             else if (node->name == "enum")
             {
-                SchemaEnum enum = new SchemaEnum(this, node);
+                SchemaEnum enum = new SchemaEnum.from_xml(this, node);
                 enums.insert(enum.id, enum);
             }
             else if (node->type != Xml.ElementType.TEXT_NODE)
diff --git a/editor/dconf-view.vala b/editor/dconf-view.vala
index 65bf441..a71f129 100644
--- a/editor/dconf-view.vala
+++ b/editor/dconf-view.vala
@@ -37,13 +37,17 @@ private class KeyValueRenderer: Gtk.CellRenderer
             case "t":
             case "d":
                 spin_renderer.text = key.value.print(false);
-                double min, max;
-                var v = get_variant_as_double(key.value, out min, out max);
+                var v = get_variant_as_double(key.value);
+                double min = 0.0, max = 0.0;
                 if (key.schema.range != null)
                 {
-                    double t;
-                    min = get_variant_as_double(key.schema.range.min, out t, out t);
-                    max = get_variant_as_double(key.schema.range.max, out t, out t);
+                    min = get_variant_as_double(key.schema.range.min);
+                    max = get_variant_as_double(key.schema.range.max);
+                }
+                else
+                {
+                    min = get_variant_as_double(key.get_min());
+                    max = get_variant_as_double(key.get_max());
                 }
                 spin_renderer.adjustment = new Gtk.Adjustment(v, min, max, 1, 0, 0);
                 spin_renderer.digits = 0;
@@ -57,45 +61,30 @@ private class KeyValueRenderer: Gtk.CellRenderer
         }
     }
 
-    private static double get_variant_as_double(Variant value, out double min, out double max)
+    private static double get_variant_as_double(Variant value)
     {
+        if (value == null)
+            return 0.0;
+
         switch (value.classify ())
         {
         case Variant.Class.BYTE:
-            min = 0.0;
-            max = 255.0;
             return (double)value.get_byte();
         case Variant.Class.INT16:
-            min = int16.MIN;
-            max = int16.MAX;
             return (double)value.get_int16();
         case Variant.Class.UINT16:
-            min = uint16.MIN;
-            max = uint16.MAX;
             return (double)value.get_uint16();
         case Variant.Class.INT32:
-            min = int32.MIN;
-            max = int32.MAX;
             return (double)value.get_int32();
         case Variant.Class.UINT32:
-            min = uint32.MAX;
-            max = uint32.MIN;
             return (double)value.get_uint32();
         case Variant.Class.INT64:
-            min = int64.MIN;
-            max = int64.MAX;
             return (double)value.get_int64();
         case Variant.Class.UINT64:
-            min = uint64.MIN;
-            max = uint64.MAX;
             return (double)value.get_uint64();
         case Variant.Class.DOUBLE:
-            min = double.MIN;
-            max = double.MAX;
             return value.get_double();
         default:
-            min = 0.0;
-            max = 0.0;
             return 0.0;
         }
     }
@@ -246,22 +235,33 @@ private class KeyValueRenderer: Gtk.CellRenderer
     private void spin_edited_cb(Gtk.CellRendererText renderer, string path, string text)
     {
         Key key = get_key_from_path(path);
-        if (key.type_string == "y")
+        switch (key.type_string)
+        {
+        case "y":
             key.value = new Variant.byte((uchar)text.to_int());
-        else if (key.type_string == "n")
+            break;
+        case "n":
             key.value = new Variant.int16((int16)text.to_int());
-        else if (key.type_string == "q")
+            break;
+        case "q":
             key.value = new Variant.uint16((uint16)text.to_int());
-        else if (key.type_string == "i")
+            break;
+        case "i":
             key.value = new Variant.int32(text.to_int());
-        else if (key.type_string == "u")
+            break;
+        case "u":
             key.value = new Variant.uint32(text.to_int());
-        else if (key.type_string == "x")
+            break;
+        case "x":
             key.value = new Variant.int64(text.to_int());
-        else if (key.type_string == "t")
+            break;
+        case "t":
             key.value = new Variant.uint64(text.to_int());
-        else if (key.type_string == "d")
+            break;
+        case "d":
             key.value = new Variant.double(text.to_double());
+            break;
+        }
     }
 }
 



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