[gnome-documents] all: port Documents to GdMainView (1st round)



commit 896623e45adda7be94cdbd7f5f366143def270c7
Author: Cosimo Cecchi <cosimoc gnome org>
Date:   Mon Dec 5 18:35:20 2011 -0500

    all: port Documents to GdMainView (1st round)

 src/Makefile-js.am              |    2 -
 src/embed.js                    |   48 ++---------
 src/iconView.js                 |   98 ---------------------
 src/lib/gd-two-lines-renderer.c |    2 +-
 src/lib/gd-two-lines-renderer.h |    2 +-
 src/listView.js                 |  119 --------------------------
 src/view.js                     |  177 +++++++++++++++++----------------------
 7 files changed, 85 insertions(+), 363 deletions(-)
---
diff --git a/src/Makefile-js.am b/src/Makefile-js.am
index ca6a8b3..00a8351 100644
--- a/src/Makefile-js.am
+++ b/src/Makefile-js.am
@@ -8,8 +8,6 @@ dist_js_DATA = \
     errorBox.js \
     gDataMiner.js \
     global.js \
-    iconView.js \
-    listView.js \
     loadMore.js \
     main.js \
     mainToolbar.js \
diff --git a/src/embed.js b/src/embed.js
index c3f19e1..16f92aa 100644
--- a/src/embed.js
+++ b/src/embed.js
@@ -24,17 +24,17 @@ const Mainloop = imports.mainloop;
 
 const ErrorBox = imports.errorBox;
 const Global = imports.global;
-const IconView = imports.iconView;
-const ListView = imports.listView;
 const LoadMore = imports.loadMore;
 const MainToolbar = imports.mainToolbar;
 const Preview = imports.preview;
 const SpinnerBox = imports.spinnerBox;
 const Tweener = imports.util.tweener;
+const View = imports.view;
 const WindowMode = imports.windowMode;
 
 const Clutter = imports.gi.Clutter;
 const EvView = imports.gi.EvinceView;
+const Gd = imports.gi.Gd;
 const Gio = imports.gi.Gio;
 const Gtk = imports.gi.Gtk;
 const GtkClutter = imports.gi.GtkClutter;
@@ -54,7 +54,6 @@ ViewEmbed.prototype  = {
         this._motionTimeoutId = 0;
         this._queryErrorId = 0;
         this._scrollbarVisibleId = 0;
-        this._viewSettingsId = 0;
 
         this._scrolledWinView = null;
         this._scrolledWinPreview = null;
@@ -196,12 +195,6 @@ ViewEmbed.prototype  = {
             child.destroy();
     },
 
-    _destroyScrollViewChild: function() {
-        let child = this._scrolledWinView.get_child();
-        if (child)
-            child.destroy();
-    },
-
     _destroyPreview: function() {
         if (this._loaderCancellable) {
             this._loaderCancellable.cancel();
@@ -216,19 +209,6 @@ ViewEmbed.prototype  = {
         this._docModel = null;
     },
 
-    _initView: function() {
-        this._destroyScrollViewChild();
-
-        let isList = Global.settings.get_boolean('list-view');
-
-        if (isList)
-            this._view = new ListView.ListView(this);
-        else
-            this._view = new IconView.IconView(this);
-
-        this._scrolledWinView.add(this._view.widget);
-    },
-
     _onActiveItemChanged: function() {
         let doc = Global.documentManager.getActiveItem();
 
@@ -304,28 +284,19 @@ ViewEmbed.prototype  = {
 
         Global.documentManager.setActiveItem(null);
 
-        this._viewSettingsId =
-            Global.settings.connect('changed::list-view',
-                                    Lang.bind(this, this._initView));
         this._queryErrorId =
             Global.errorHandler.connect('query-error',
                                         Lang.bind(this, this._onQueryError));
 
         if (!this._scrolledWinView) {
             let grid = new Gtk.Grid({ orientation: Gtk.Orientation.VERTICAL });
-
-            this._scrolledWinView = new Gtk.ScrolledWindow({ hexpand: true,
-                                                             vexpand: true,
-                                                             shadow_type: Gtk.ShadowType.IN });
-            this._scrolledWinView.get_style_context().set_junction_sides(Gtk.JunctionSides.BOTTOM);
+            this._view = new View.View();
+            this._scrolledWinView = this._view.widget;
             grid.add(this._scrolledWinView);
-            this._scrolledWinView.get_style_context().add_class('documents-scrolledwin');
 
             this._loadMore = new LoadMore.LoadMoreButton();
             grid.add(this._loadMore.widget);
 
-            this._initView();
-
             grid.show_all();
             this._viewPage = this._notebook.append_page(grid, null);
         }
@@ -371,20 +342,13 @@ ViewEmbed.prototype  = {
     },
 
     _onQueryError: function(manager, message, exception) {
-        this._destroyScrollViewChild();
+        this._prepareForPreview();
 
         let errorBox = new ErrorBox.ErrorBox(message, exception.message);
-        this._scrolledWinView.add_with_viewport(errorBox.widget);
+        this._scrolledWinPreview.add_with_viewport(errorBox.widget);
     },
 
     _prepareForPreview: function() {
-        this._view = null;
-
-        if (this._viewSettingsId != 0) {
-            Global.settings.disconnect(this._viewSettingsId);
-            this._viewSettingsId = 0;
-        }
-
         if (this._queryErrorId != 0) {
             Global.errorHandler.disconnect(this._queryErrorId);
             this._queryErrorId = 0;
diff --git a/src/lib/gd-two-lines-renderer.c b/src/lib/gd-two-lines-renderer.c
index 48af124..c69fa0e 100644
--- a/src/lib/gd-two-lines-renderer.c
+++ b/src/lib/gd-two-lines-renderer.c
@@ -489,7 +489,7 @@ gd_two_lines_renderer_init (GdTwoLinesRenderer *self)
                                             GdTwoLinesRendererPrivate);
 }
 
-GdTwoLinesRenderer *
+GtkCellRenderer *
 gd_two_lines_renderer_new (void)
 {
   return g_object_new (GD_TYPE_TWO_LINES_RENDERER, NULL);
diff --git a/src/lib/gd-two-lines-renderer.h b/src/lib/gd-two-lines-renderer.h
index 979fbe8..8dd3e51 100644
--- a/src/lib/gd-two-lines-renderer.h
+++ b/src/lib/gd-two-lines-renderer.h
@@ -68,7 +68,7 @@ struct _GdTwoLinesRendererClass
 
 GType gd_two_lines_renderer_get_type (void) G_GNUC_CONST;
 
-GdTwoLinesRenderer *gd_two_lines_renderer_new (void);
+GtkCellRenderer *gd_two_lines_renderer_new (void);
 
 G_END_DECLS
 
diff --git a/src/view.js b/src/view.js
index cf3d858..d7709da 100644
--- a/src/view.js
+++ b/src/view.js
@@ -19,6 +19,7 @@
  *
  */
 
+const Gd = imports.gi.Gd;
 const Gdk = imports.gi.Gdk;
 const Gtk = imports.gi.Gtk;
 const _ = imports.gettext.gettext;
@@ -111,12 +112,20 @@ View.prototype = {
         this._selectedURNs = null;
         this._updateSelectionId = 0;
 
-        // create renderers
-        this.createRenderers();
-        this.widget.get_style_context().add_class('documents-main-view');
+        this.widget = new Gd.MainView();
 
-        // setup selections view => controller
-        this.connectToSelectionChanged(Lang.bind(this, this._onSelectionChanged));
+        this.widget.connect('item-activated',
+                            Lang.bind(this, this._onItemActivated));
+        this.widget.connect('selection-mode-request',
+                            Lang.bind(this, this._onSelectionModeRequest));
+        this.widget.connect('notify::view-type',
+                            Lang.bind(this, this._onViewTypeChanged));
+
+        // connect to settings change for list/grid view
+        this._viewSettingsId =
+            Global.settings.connect('changed::list-view',
+                                    Lang.bind(this, this._updateTypeForSettings));
+        this._updateTypeForSettings();
 
         // setup selection controller => view
         this._selectionModeId =
@@ -128,27 +137,67 @@ View.prototype = {
             Global.trackerController.connect('query-status-changed',
                                              Lang.bind(this, this._onQueryStatusChanged));
 
-        this.widget.connect('button-release-event',
-                            Lang.bind(this, this._onButtonReleaseEvent));
-        this.widget.connect('button-press-event',
-                            Lang.bind(this, this._onButtonPressEvent));
-        this.widget.connect('destroy', Lang.bind(this,
+        // this will create the model if we're done querying
+        this._onQueryStatusChanged();
+        this.widget.show();
+    },
+
+    _updateTypeForSettings: function() {
+        let isList = Global.settings.get_boolean('list-view');
+        let viewType = Gd.MainViewType.ICON;
+        if (isList)
+            viewType = Gd.MainViewType.LIST;
+
+        this.widget.set_view_type(viewType);
+    },
+
+    _addListRenderers: function() {
+        let listWidget = this.widget.get_generic_view();
+
+        let typeRenderer =
+            new Gtk.CellRendererText({ xpad: 16 });
+        listWidget.add_renderer(typeRenderer, Lang.bind(this,
+            function(col, cell, model, iter) {
+                let urn = model.get_value(iter, Documents.ModelColumns.URN);
+                let doc = Global.documentManager.getItemById(urn);
+
+                typeRenderer.text = doc.typeDescription;
+            }));
+
+        let whereRenderer =
+            new Gtk.CellRendererText({ xpad: 8 });
+        listWidget.add_renderer(whereRenderer, Lang.bind(this,
+            function(col, cell, model, iter) {
+                let urn = model.get_value(iter, Documents.ModelColumns.URN);
+                let doc = Global.documentManager.getItemById(urn);
+
+                whereRenderer.text = doc.sourceName;
+            }));
+    },
+
+    _onViewTypeChanged: function() {
+        if (this.widget.get_view_type() == Gd.MainViewType.LIST)
+            this._addListRenderers();
+
+        // setup selections view => controller
+        let generic = this.widget.get_generic_view();
+        generic.connect('view-selection-changed', Lang.bind(this, this._onSelectionChanged));
+
+        Global.selectionController.freezeSelection(false);
+
+        generic.connect('destroy', Lang.bind(this,
             function() {
                 // save selection when the view is destroyed
                 Global.selectionController.freezeSelection(true);
-
-                if (this._updateSelectionId != 0) {
-                    Mainloop.source_remove(this._updateSelectionId);
-                    this._updateSelectionId = 0;
-                }
-
-                Global.trackerController.disconnect(this._queryId);
-                Global.selectionController.disconnect(this._selectionModeId);
             }));
+    },
 
-        // this will create the model if we're done querying
-        this._onQueryStatusChanged();
-        this.widget.show();
+    _onSelectionModeRequest: function() {
+        Global.selectionController.setSelectionMode(true);
+    },
+
+    _onItemActivated: function(widget, id) {
+        Global.documentManager.setActiveItemById(id);
     },
 
     _onQueryStatusChanged: function() {
@@ -174,12 +223,12 @@ View.prototype = {
     },
 
     _updateSelection: function() {
-        let selectionObject = this.getSelectionObject();
         let selected = Global.selectionController.getSelection().slice(0);
 
         if (!selected.length)
             return;
 
+        let generic = this.widget.get_generic_view();
         let first = true;
         this._treeModel.foreach(Lang.bind(this,
             function(model, path, iter) {
@@ -187,11 +236,11 @@ View.prototype = {
                 let urnIndex = selected.indexOf(urn);
 
                 if (urnIndex != -1) {
-                    selectionObject.select_path(path);
+                    generic.select_path(path);
                     selected.splice(urnIndex, 1);
 
                     if (first) {
-                        this.scrollToPath(path);
+                        generic.scrollToPath(path);
                         first = false;
                     }
                 }
@@ -205,87 +254,15 @@ View.prototype = {
 
     _onSelectionModeChanged: function() {
         let selectionMode = Global.selectionController.getSelectionMode();
-
-        // setup the GtkSelectionMode of the view according to whether or not
-        // the view is in "selection mode"
-        if (selectionMode)
-            this.setSelectionMode(Gtk.SelectionMode.MULTIPLE);
-        else
-            this.setSelectionMode(Gtk.SelectionMode.NONE);
+        this.widget.set_selection_mode(selectionMode);
     },
 
     _onSelectionChanged: function() {
+        let generic = this.widget.get_generic_view();
+
         // update the selection on the controller when the view signals a change
-        let selectedURNs = Utils.getURNsFromPaths(this.getSelection(),
+        let selectedURNs = Utils.getURNsFromPaths(generic.get_selection(),
                                                   this._treeModel);
         Global.selectionController.setSelection(selectedURNs);
-    },
-
-    _onButtonReleaseEvent: function(widget, event) {
-        let button = event.get_button()[1];
-        let modifier = event.get_state()[1];
-        let coords = [ event.get_coords()[1] , event.get_coords()[2] ];
-
-        let selectionMode = Global.selectionController.getSelectionMode();
-        let enteredMode = false;
-
-        // eat double/triple click events
-        let clickCount = event.get_click_count()[1];
-        if (clickCount > 1)
-            return true;
-
-        // don't eat events if we didn't click any path
-        let path = this.getPathAtPos(coords);
-        if (!path)
-            return false;
-
-        if (!selectionMode) {
-            if ((button == 3) ||
-                ((button == 1) && (modifier & Gdk.ModifierType.CONTROL_MASK))) {
-                Global.selectionController.setSelectionMode(true);
-                selectionMode = true;
-                enteredMode = true;
-            }
-        }
-
-        if (selectionMode)
-            return this._selectionModeReleaseEvent(event, enteredMode, path);
-        else
-            return this._viewModeReleaseEvent(event, path);
-    },
-
-    _selectionModeReleaseEvent: function(event, enteredMode, path) {
-        let selectionObj = this.getSelectionObject();
-        let isSelected = selectionObj.path_is_selected(path);
-
-        if (isSelected && !enteredMode)
-            selectionObj.unselect_path(path);
-        else if (!isSelected)
-            selectionObj.select_path(path);
-
-        return true;
-    },
-
-    _viewModeReleaseEvent: function(event, path) {
-        this.activateItem(path);
-        return true;
-    },
-
-    _onButtonPressEvent: function(widget, event) {
-        // eat button press events for now; in the future we might
-        // want to hook up support for DnD here
-        return true;
-    },
-
-    // this must be overridden by all implementations
-    createRenderers: function() {
-        throw new Error('Missing implementation of createRenderers in ' + this);
-    },
-
-    activateItem: function(path) {
-        let iter = this._treeModel.get_iter(path)[1];
-        let urn = this._treeModel.get_value(iter, Documents.ModelColumns.URN);
-
-        Global.documentManager.setActiveItemById(urn);
     }
 };



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