[Sugar-devel] [PATCH] adding sugar source radio button to view source toolbar

Walter Bender walter at sugarlabs.org
Mon Jun 6 14:26:05 EDT 2011


From: Walter Bender <walter.bender at gmail.com>

---
 src/jarabe/view/viewsource.py |  115 ++++++++++++++++++++++++++++++++--------
 1 files changed, 92 insertions(+), 23 deletions(-)

diff --git a/src/jarabe/view/viewsource.py b/src/jarabe/view/viewsource.py
index c1d8f30..5cf4870 100644
--- a/src/jarabe/view/viewsource.py
+++ b/src/jarabe/view/viewsource.py
@@ -1,5 +1,6 @@
 # Copyright (C) 2008 One Laptop Per Child
 # Copyright (C) 2009 Tomeu Vizoso, Simon Schampijer
+# Copyright (C) 2011 Walter Bender
 #
 # 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
@@ -16,6 +17,7 @@
 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
 import os
+import sys
 import subprocess
 import logging
 from gettext import gettext as _
@@ -86,8 +88,16 @@ def setup_view_source(activity):
         _logger.debug('Activity with neither a bundle_path nor a document_path')
         return
 
+    sugar_source_paths = [None]
+    for path in sys.path:
+        if path.endswith('site-packages'):
+            sugar_source_paths = [os.path.join(path, 'jarabe'),
+                                  os.path.join(path, 'sugar')]
+            # Should we add /usr/share/sugar as well?
+            break
+
     view_source = ViewSource(window_xid, bundle_path, document_path,
-                             activity.get_title())
+                             sugar_source_paths, activity.get_title())
     map_activity_to_window[window_xid] = view_source
     view_source.show()
 
@@ -95,7 +105,8 @@ def setup_view_source(activity):
 class ViewSource(gtk.Window):
     __gtype_name__ = 'SugarViewSource'
 
-    def __init__(self, window_xid, bundle_path, document_path, title):
+    def __init__(self, window_xid, bundle_path, document_path,
+                 sugar_source_paths, title):
         gtk.Window.__init__(self)
 
         _logger.debug('ViewSource paths: %r %r', bundle_path, document_path)
@@ -118,7 +129,7 @@ class ViewSource(gtk.Window):
         self.add(vbox)
         vbox.show()
 
-        toolbar = Toolbar(title, bundle_path, document_path)
+        toolbar = Toolbar(title, bundle_path, document_path, sugar_source_paths)
         vbox.pack_start(toolbar, expand=False)
         toolbar.connect('stop-clicked', self.__stop_clicked_cb)
         toolbar.connect('source-selected', self.__source_selected_cb)
@@ -128,7 +139,8 @@ class ViewSource(gtk.Window):
         vbox.pack_start(pane)
         pane.show()
 
-        self._selected_file = None
+        self._selected_bundle_file = None
+        self._selected_sugar_file = None
         file_name = ''
 
         activity_bundle = ActivityBundle(bundle_path)
@@ -138,17 +150,31 @@ class ViewSource(gtk.Window):
             tmppath = command.split(' ')[1].replace('.', '/')
             file_name = tmppath[0:-(len(name) + 1)] + '.py'
             path = os.path.join(activity_bundle.get_path(), file_name)
-            self._selected_file = path
+            self._selected_bundle_file = path
+
+        # Split the tree pane into two vertical panes, one of which
+        # will be hidden
+        tree_panes = gtk.VPaned()
+        tree_panes.show()
+
+        self._bundle_source_viewer = FileViewer(bundle_path, file_name)
+        self._bundle_source_viewer.connect('file-selected',
+                                           self.__file_selected_cb)
+        tree_panes.add1(self._bundle_source_viewer)
+        self._bundle_source_viewer.show()
 
-        self._file_viewer = FileViewer(bundle_path, file_name)
-        self._file_viewer.connect('file-selected', self.__file_selected_cb)
-        pane.add1(self._file_viewer)
-        self._file_viewer.show()
+        self._sugar_source_viewer = FileViewer(sugar_source_paths, None)
+        self._sugar_source_viewer.connect('file-selected',
+                                          self.__file_selected_cb)
+        tree_panes.add2(self._sugar_source_viewer)
+        self._sugar_source_viewer.hide()
+
+        pane.add1(tree_panes)
 
         self._source_display = SourceDisplay()
         pane.add2(self._source_display)
         self._source_display.show()
-        self._source_display.file_path = self._selected_file
+        self._source_display.file_path = self._selected_bundle_file
 
         if document_path is not None:
             self._select_source(document_path)
@@ -175,12 +201,21 @@ class ViewSource(gtk.Window):
 
     def _select_source(self, path):
         if os.path.isfile(path):
+            _logger.debug('_select_source called with file: %r', path)
             self._source_display.file_path = path
-            self._file_viewer.hide()
-        else:
-            self._file_viewer.set_path(path)
-            self._source_display.file_path = self._selected_file
-            self._file_viewer.show()
+            self._bundle_source_viewer.hide()
+            self._sugar_source_viewer.hide()
+        elif os.path.isdir(path):
+            _logger.debug('_select_source called with path: %r', path)
+            self._bundle_source_viewer.set_path(path)
+            self._source_display.file_path = self._selected_bundle_file
+            self._bundle_source_viewer.show()
+            self._sugar_source_viewer.hide()
+        else:  # Sugar source paths
+            _logger.debug('_select_source called with sugar source paths')
+            self._source_display.file_path = self._selected_sugar_file
+            self._bundle_source_viewer.hide()
+            self._sugar_source_viewer.show()
 
     def __destroy_cb(self, window, document_path):
         del map_activity_to_window[self._parent_window_xid]
@@ -195,7 +230,10 @@ class ViewSource(gtk.Window):
     def __file_selected_cb(self, file_viewer, file_path):
         if file_path is not None and os.path.isfile(file_path):
             self._source_display.file_path = file_path
-            self._selected_file = file_path
+            if file_viewer == self._bundle_source_viewer:
+                self._selected_bundle_file = file_path
+            else:
+                self._selected_sugar_file = file_path
         else:
             self._source_display.file_path = None
 
@@ -286,7 +324,7 @@ class Toolbar(gtk.Toolbar):
                             ([str])),
     }
 
-    def __init__(self, title, bundle_path, document_path):
+    def __init__(self, title, bundle_path, document_path, sugar_source_paths):
         gtk.Toolbar.__init__(self)
 
         document_button = None
@@ -322,6 +360,26 @@ class Toolbar(gtk.Toolbar):
             activity_button.show()
             self._add_separator()
 
+        if sugar_source_paths[0] is not None and \
+           os.path.exists(sugar_source_paths[0]):
+            sugar_button = RadioToolButton()
+            icon = Icon(icon_name='computer-xo',
+                        icon_size=gtk.ICON_SIZE_LARGE_TOOLBAR,
+                        fill_color=style.COLOR_TRANSPARENT.get_svg(),
+                        stroke_color=style.COLOR_WHITE.get_svg())
+            sugar_button.set_icon_widget(icon)
+            icon.show()
+            if document_button is not None:
+                sugar_button.props.group = document_button
+            else:
+                sugar_button.props.group = activity_button
+            sugar_button.props.tooltip = _('Sugar Source')
+            sugar_button.connect('toggled', self.__button_toggled_cb,
+                                 sugar_source_paths)
+            self.insert(sugar_button, -1)
+            sugar_button.show()
+            self._add_separator()
+
         text = _('View source: %r') % title
         label = gtk.Label()
         label.set_markup('<b>%s</b>' % text)
@@ -404,20 +462,31 @@ class FileViewer(gtk.ScrolledWindow):
         self.emit('file-selected', None)
         if self._path == path:
             return
-        self._path = path
+
         self._tree_view.set_model(gtk.TreeStore(str, str))
+
+        if type(path) == list:
+            self._path = path[0]
+        else:
+            self._path = path
+
+        self._model = self._tree_view.get_model()
         self._add_dir_to_model(path)
 
     def _add_dir_to_model(self, dir_path, parent=None):
-        model = self._tree_view.get_model()
+        if type(dir_path) == list:
+            for path in dir_path:
+                self._add_dir_to_model(path)
+            return
+
         for f in os.listdir(dir_path):
             if not f.endswith(('.pyc', '.pyo', '.so', '.mo', '~')):
                 full_path = os.path.join(dir_path, f)
                 if os.path.isdir(full_path):
-                    new_iter = model.append(parent, [f, full_path])
+                    new_iter = self._model.append(parent, [f, full_path])
                     self._add_dir_to_model(full_path, new_iter)
                 else:
-                    current_iter = model.append(parent, [f, full_path])
+                    current_iter = self._model.append(parent, [f, full_path])
                     if f == self._initial_filename:
                         selection = self._tree_view.get_selection()
                         selection.select_iter(current_iter)
@@ -457,8 +526,8 @@ class SourceDisplay(gtk.ScrolledWindow):
         self._file_path = None
 
     def _set_file_path(self, file_path):
-        if file_path == self._file_path:
-            return
+        # if file_path == self._file_path:
+        #    return
         self._file_path = file_path
 
         if self._file_path is None:
-- 
1.7.4.4



More information about the Sugar-devel mailing list