[Sugar-devel] [PATCH] Add-view-source-for-sugar-toolkit

Walter Bender walter at sugarlabs.org
Fri Aug 19 12:50:18 EDT 2011


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

As per feedback from the design team, I have added a radio button to view
the source of the sugar-toolkit to the standard view source toolbar.

---

Note: This is a modified version of an earlier patch that updates the title
displayed in the View Source toolbar when switching between the activity source
and the sugar-toolkit source. This patch also selects activity/activity.py to
display in the viewer by default when looking at the sugar-toolkit source.

Note: In this version of the patch, the TreeView is seeded with env.py. This
patch also includes from cosmetic changes from erikos. It has been rebased to
the latest version of sugar (2011-08-19)

 src/jarabe/view/viewsource.py |  127 +++++++++++++++++++++++++++++++---------
 1 files changed, 98 insertions(+), 29 deletions(-)

diff --git a/src/jarabe/view/viewsource.py b/src/jarabe/view/viewsource.py
index 648e740..63a6977 100644
--- a/src/jarabe/view/viewsource.py
+++ b/src/jarabe/view/viewsource.py
@@ -17,6 +17,7 @@
 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
 import os
+import sys
 import logging
 from gettext import gettext as _
 
@@ -90,8 +91,15 @@ def setup_view_source(activity):
         _logger.debug('Activity without bundle_path nor document_path')
         return
 
+    sugar_toolkit_path = None
+    for path in sys.path:
+        if path.endswith('site-packages'):
+            if os.path.exists(os.path.join(path, 'sugar')):
+                sugar_toolkit_path = os.path.join(path, 'sugar')
+                break
+
     view_source = ViewSource(window_xid, bundle_path, document_path,
-                             activity.get_title())
+                             sugar_toolkit_path, activity.get_title())
     map_activity_to_window[window_xid] = view_source
     view_source.show()
 
@@ -99,10 +107,12 @@ 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_toolkit_path, title):
         gtk.Window.__init__(self)
 
-        logging.debug('ViewSource paths: %r %r', bundle_path, document_path)
+        _logger.debug('ViewSource paths: %r %r %r', bundle_path,
+                      document_path, sugar_toolkit_path)
 
         self.set_decorated(False)
         self.set_position(gtk.WIN_POS_CENTER_ALWAYS)
@@ -113,6 +123,7 @@ class ViewSource(gtk.Window):
         self.set_size_request(width, height)
 
         self._parent_window_xid = window_xid
+        self._sugar_toolkit_path = sugar_toolkit_path
 
         self.connect('realize', self.__realize_cb)
         self.connect('destroy', self.__destroy_cb, document_path)
@@ -122,7 +133,8 @@ class ViewSource(gtk.Window):
         self.add(vbox)
         vbox.show()
 
-        toolbar = Toolbar(title, bundle_path, document_path)
+        toolbar = Toolbar(title, bundle_path, document_path,
+                          sugar_toolkit_path)
         vbox.pack_start(toolbar, expand=False)
         toolbar.connect('stop-clicked', self.__stop_clicked_cb)
         toolbar.connect('source-selected', self.__source_selected_cb)
@@ -132,26 +144,44 @@ 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)
         command = activity_bundle.get_command()
         if len(command.split(' ')) > 1:
-            name = command.split(' ')[1].split('.')[0]
-            file_name = name + '.py'
+            name = command.split(' ')[1].split('.')[-1]
+            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()
+        file_name = 'env.py'
+        self._selected_sugar_file = os.path.join(sugar_toolkit_path, file_name)
+        self._sugar_source_viewer = FileViewer(sugar_toolkit_path, file_name)
+        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)
@@ -178,12 +208,23 @@ 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()
+            self._bundle_source_viewer.hide()
+            self._sugar_source_viewer.hide()
+        elif path == self._sugar_toolkit_path:
+            _logger.debug('_select_source called with sugar toolkit path: %r',
+                          path)
+            self._sugar_source_viewer.set_path(path)
+            self._source_display.file_path = self._selected_sugar_file
+            self._sugar_source_viewer.show()
+            self._bundle_source_viewer.hide()
         else:
-            self._file_viewer.set_path(path)
-            self._source_display.file_path = self._selected_file
-            self._file_viewer.show()
+            _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()
 
     def __destroy_cb(self, window, document_path):
         del map_activity_to_window[self._parent_window_xid]
@@ -198,7 +239,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
 
@@ -269,10 +313,12 @@ class Toolbar(gtk.Toolbar):
                             ([str])),
     }
 
-    def __init__(self, title, bundle_path, document_path):
+    def __init__(self, title, bundle_path, document_path, sugar_toolkit_path):
         gtk.Toolbar.__init__(self)
 
         document_button = None
+        self.bundle_path = bundle_path
+        self.sugar_toolkit_path = sugar_toolkit_path
 
         self._add_separator()
 
@@ -304,11 +350,31 @@ class Toolbar(gtk.Toolbar):
             activity_button.show()
             self._add_separator()
 
-        text = _('View source: %r') % title
-        label = gtk.Label()
-        label.set_markup('<b>%s</b>' % text)
-        label.set_alignment(0, 0.5)
-        self._add_widget(label)
+        if sugar_toolkit_path is not None:
+            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 Toolkit Source')
+            sugar_button.connect('toggled', self.__button_toggled_cb,
+                                 sugar_toolkit_path)
+            self.insert(sugar_button, -1)
+            sugar_button.show()
+            self._add_separator()
+
+        self.activity_title_text = _('View source: %r') % title
+        self.sugar_toolkit_title_text = _('View Sugar toolkit source')
+        self.label = gtk.Label()
+        self.label.set_markup('<b>%s</b>' % self.activity_title_text)
+        self.label.set_alignment(0, 0.5)
+        self._add_widget(self.label)
 
         self._add_separator(True)
 
@@ -344,6 +410,10 @@ class Toolbar(gtk.Toolbar):
     def __button_toggled_cb(self, button, path):
         if button.props.active:
             self.emit('source-selected', path)
+        if path == self.sugar_toolkit_path:
+            self.label.set_markup('<b>%s</b>' % self.sugar_toolkit_title_text)
+        else:  # Use activity title for either bundle path or document path
+            self.label.set_markup('<b>%s</b>' % self.activity_title_text)
 
 
 class FileViewer(gtk.ScrolledWindow):
@@ -386,22 +456,23 @@ 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))
+        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()
         for f in os.listdir(dir_path):
             if f.endswith(_EXCLUDE_EXTENSIONS) or f in _EXCLUDE_NAMES:
                 continue
 
             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)
@@ -441,8 +512,6 @@ class SourceDisplay(gtk.ScrolledWindow):
         self._file_path = None
 
     def _set_file_path(self, file_path):
-        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