[baobab/wip/grid-layout: 7/8] Implement grid layout for location view



commit 5c24ee34d9c5eeb532955296e65065f9b45d41a5
Author: Stefano Facchini <stefano facchini gmail com>
Date:   Wed Apr 11 22:49:41 2012 +0200

    Implement grid layout for location view

 src/baobab-location-widget.vala |  173 ++++++++++++++++++++++++++++++++++----
 src/baobab-main-window.ui       |    4 +-
 src/baobab-window.vala          |   14 ++-
 3 files changed, 166 insertions(+), 25 deletions(-)
---
diff --git a/src/baobab-location-widget.vala b/src/baobab-location-widget.vala
index d669077..89aed4b 100644
--- a/src/baobab-location-widget.vala
+++ b/src/baobab-location-widget.vala
@@ -20,8 +20,112 @@
 
 namespace Baobab {
 
+    public class LocationView : Gtk.Container {
+        private int column_width = -1;
+        private int row_height = -1;
+        
+        List<Gtk.Widget> children = null;
+
+        public LocationView () {
+            set_has_window (false);
+        }
+
+        protected override void get_preferred_width (out int minimum, out int natural) {
+            if (column_width < 0) {
+                minimum = 0;
+                natural = 0;
+            } else {
+                minimum = column_width;
+                natural = column_width;
+            }
+        }
+
+        protected override void get_preferred_height (out int minimum, out int natural) {
+            if (row_height < 0) {
+                minimum = 0;
+                natural = 0;
+            } else {
+                minimum = row_height;
+                natural = row_height;
+            }
+        }
+
+        protected override void size_allocate (Gtk.Allocation alloc) {
+            if (children.length () == 0) {
+                return;
+            }
+
+            set_allocation (alloc);
+
+            int columns = alloc.width / column_width;
+
+            int col = 0, row = 0;
+            foreach (var child in children) {
+                var child_alloc = Gtk.Allocation ();
+                child_alloc.x = alloc.x + col * column_width;
+                child_alloc.y = alloc.y + row * row_height;
+                child_alloc.width = column_width;
+                child_alloc.height = row_height;
+                    
+                child.size_allocate (child_alloc);
+
+                if (++col >= columns) {
+                    col = 0;
+                    row++;
+                }
+            }
+        }
+
+        protected override void forall_internal (bool include_internals, Gtk.Callback cb) {
+            unowned List<Gtk.Widget> list = children;
+            while (list != null) {
+                Gtk.Widget child = list.data;
+                list = list.next;
+
+                cb (child);
+            }
+        }
+
+        protected override void remove (Gtk.Widget widget) {
+            foreach (var child in children) {
+                if (child == widget) {
+                    widget.unparent ();
+                    children.remove (widget);
+
+                    queue_resize ();
+
+                    break;
+                }
+            }
+        }
+
+        protected override void add (Gtk.Widget widget) {
+            int width, height;
+            widget.get_preferred_width (null, out width);
+            widget.get_preferred_height (null, out height);
+
+            column_width = int.max (column_width, width);
+            row_height = int.max (row_height, height);
+
+            widget.set_parent (this);
+
+            children.append (widget);
+        }
+
+        public void remove_all () {
+            foreach (var child in children) {
+                child.destroy ();
+            }
+
+            children = null;
+            queue_resize ();
+        }
+    }
+
     public abstract class BaseLocationWidget : Gtk.Grid {
 
+        protected const int ICON_SIZE = 128;
+
         protected static Gtk.SizeGroup name_size_group = null;
         protected static Gtk.SizeGroup mount_point_size_group = null;
         protected static Gtk.SizeGroup size_size_group = null;
@@ -42,7 +146,7 @@ namespace Baobab {
         }
 
         public BaseLocationWidget () {
-            orientation = Gtk.Orientation.HORIZONTAL;
+            //orientation = Gtk.Orientation.HORIZONTAL;
             column_spacing = 10;
             margin = 6;
         }
@@ -56,50 +160,81 @@ namespace Baobab {
             ensure_size_groups ();
 
             var icon_theme = Gtk.IconTheme.get_default ();
-            var icon_info = icon_theme.lookup_by_gicon (location.icon, 64, 0);
+            var icon_info = icon_theme.lookup_by_gicon (location.icon, BaseLocationWidget.ICON_SIZE, 0);
 
             try {
                 var pixbuf = icon_info.load_icon ();
                 var image = new Gtk.Image.from_pixbuf (pixbuf);
-                add (image);
+                attach (image, 0, 0, 1, 1);
             } catch (Error e) {
                 warning ("Failed to load icon %s: %s", location.icon.to_string(), e.message);
             }
 
             var label = new Gtk.Label (location.name);
-            label.xalign = 0;
+            //label.xalign = 0;
             name_size_group.add_widget (label);
-            add (label);
+            attach (label, 0, 1, 1, 1);
 
-            label = new Gtk.Label (location.mount_point != null ? location.mount_point : "");
-            label.hexpand = true;
-            label.halign = Gtk.Align.CENTER;
-            label.xalign = 0;
+
+            var info_grid = new Gtk.Grid ();
+            info_grid.column_spacing = 10;
+            info_grid.row_spacing = 6;
+            info_grid.valign = Gtk.Align.CENTER;
+
+            label = new Gtk.Label (_("Size"));
+            label.halign = Gtk.Align.END;
             label.get_style_context ().add_class ("dim-label");
-            mount_point_size_group.add_widget (label);
-            add (label);
+            info_grid.attach (label, 1, 0, 1, 1);
 
             label = new Gtk.Label (location.size != null ? format_size (location.size) : "");
-            size_size_group.add_widget (label);
-            add (label);
+            label.halign = Gtk.Align.START;
+            info_grid.attach (label, 2, 0, 1, 1);
+
 
             if (location.used != null) {
+                label = new Gtk.Label (_("Usage"));
+                label.halign = Gtk.Align.END;
+                label.get_style_context ().add_class ("dim-label");
+                info_grid.attach (label, 1, 1, 1, 1);
+
                 var progress = new Gtk.ProgressBar ();
                 progress.valign = Gtk.Align.CENTER;
                 progress.set_fraction ((double) location.used / location.size);
                 used_size_group.add_widget (progress);
-                add (progress);
+                info_grid.attach (progress, 2, 1, 1, 1);
             } else {
-                label = new Gtk.Label (_("Usage unknown"));
+                label = new Gtk.Label ("");
+                info_grid.attach (label, 2, 3, 1, 1);
                 used_size_group.add_widget (label);
-                add (label);
             }
 
+            label = new Gtk.Label (_("Mounted at"));
+            label.halign = Gtk.Align.END;
+            label.get_style_context ().add_class ("dim-label");
+            info_grid.attach (label, 1, 2, 1, 1);
+
+            label = new Gtk.Label (location.mount_point != null ? location.mount_point : "not mounted");
+            //label.hexpand = true;
+            label.halign = Gtk.Align.START;
+            label.xalign = 0;
+            label.max_width_chars = 20;
+            label.ellipsize = Pango.EllipsizeMode.END;
+            if (location.mount_point != null)
+                label.set_tooltip_text (location.mount_point);
+            mount_point_size_group.add_widget (label);
+            info_grid.attach (label, 2, 2, 1, 1);
+
+            attach (info_grid, 1, 0, 1, 1);
+
+            //size_size_group.add_widget (label);
+            //add (label);
+
             string button_label = location.mount_point != null ? _("Scan") : _("Mount and scan");
             var button = new Gtk.Button.with_label (button_label);
             button.valign = Gtk.Align.CENTER;
+            button.halign = Gtk.Align.END;
             button_size_group.add_widget (button);
-            add (button);
+            attach (button, 1, 1, 1, 1);
 
             button.clicked.connect (() => { action (location); });
 
@@ -113,7 +248,7 @@ namespace Baobab {
             base ();
 
             var icon_theme = Gtk.IconTheme.get_default ();
-            var icon_info = icon_theme.lookup_icon ("folder", 64, 0);
+            var icon_info = icon_theme.lookup_icon ("folder", ICON_SIZE, 0);
 
             try {
                 var pixbuf = icon_info.load_icon ();
@@ -137,7 +272,7 @@ namespace Baobab {
             base ();
 
             var icon_theme = Gtk.IconTheme.get_default ();
-            var icon_info = icon_theme.lookup_icon ("folder-remote", 64, 0);
+            var icon_info = icon_theme.lookup_icon ("folder-remote", ICON_SIZE, 0);
 
             try {
                 var pixbuf = icon_info.load_icon ();
diff --git a/src/baobab-main-window.ui b/src/baobab-main-window.ui
index 1e1f3a3..f284251 100644
--- a/src/baobab-main-window.ui
+++ b/src/baobab-main-window.ui
@@ -148,7 +148,7 @@
             <property name="can_focus">False</property>
             <property name="visible">True</property>
             <property name="orientation">vertical</property>
-            <child>
+            <!--<child>
               <object class="GtkScrolledWindow" id="volume-scrolled-window">
                 <property name="visible">True</property>
                 <property name="vexpand">True</property>
@@ -165,7 +165,7 @@
                   </object>
                 </child>
               </object>
-            </child>
+            </child>-->
            </object>
         </child>
         <child>
diff --git a/src/baobab-window.vala b/src/baobab-window.vala
index e87d014..e468cb5 100644
--- a/src/baobab-window.vala
+++ b/src/baobab-window.vala
@@ -32,7 +32,7 @@ namespace Baobab {
         Gtk.Label infobar_secondary;
         Gtk.TreeView treeview;
         Gtk.Notebook chart_notebook;
-        Gtk.Grid location_view;
+        LocationView location_view;
         Gtk.Paned horizontal_paned;
         Chart rings_chart;
         Chart treemap_chart;
@@ -124,10 +124,10 @@ namespace Baobab {
             rings_chart = builder.get_object ("rings-chart") as Chart;
             treemap_chart = builder.get_object ("treemap-chart") as Chart;
             spinner = builder.get_object ("spinner") as Gtk.Spinner;
-            location_view = builder.get_object ("location-view") as Gtk.Grid;
+            //location_view = builder.get_object ("location-view") as Gtk.Grid;
             horizontal_paned = builder.get_object ("hpaned") as Gtk.Paned;
 
-            setup_home_page ();
+            setup_home_page (builder);
             setup_treeview (builder);
 
             var infobar_close_button = builder.get_object ("infobar-close-button") as Gtk.Button;
@@ -384,7 +384,13 @@ namespace Baobab {
             location_view.show_all ();
         }
 
-        void setup_home_page () {
+        void setup_home_page (Gtk.Builder builder) {
+            location_view = new LocationView ();
+            location_view.hexpand = true;
+            location_view.vexpand = true;
+            var home_page = builder.get_object ("home-page") as Gtk.Grid;
+            home_page.add (location_view);
+
             location_monitor = LocationMonitor.get ();
             location_monitor.changed.connect (() => { update_locations (); });
             update_locations ();



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