[Sugar-devel] [PATCH] add view source for Sugar toolkit

Walter Bender walter at sugarlabs.org
Mon Jul 4 13:07:30 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. Since
we are restricting ourselves to just the Sugar toolkit, the patch is a
simplified version of an earlier patch, which had appended multiple paths to
the treeview.

---
 src/jarabe/view/viewsource.py |  110 ++++++++++++++++++++++++++++++++---------
 1 files changed, 86 insertions(+), 24 deletions(-)

diff --git a/src/jarabe/view/viewsource.py b/src/jarabe/view/viewsource.py
index 5f3abab..17f0bf6 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 _
 
@@ -89,8 +90,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()
 
@@ -98,10 +106,14 @@ 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._sugar_toolkit_path = sugar_toolkit_path
 
         self.set_decorated(False)
         self.set_position(gtk.WIN_POS_CENTER_ALWAYS)
@@ -121,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)
@@ -131,26 +144,42 @@ 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._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._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._sugar_source_viewer = FileViewer(sugar_toolkit_path, 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)
@@ -177,12 +206,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._bundle_source_viewer.hide()
+            self._sugar_source_viewer.show()
         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]
@@ -197,7 +237,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
 
@@ -268,7 +311,7 @@ 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
@@ -303,6 +346,25 @@ class Toolbar(gtk.Toolbar):
             activity_button.show()
             self._add_separator()
 
+        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()
+
         text = _('View source: %r') % title
         label = gtk.Label()
         label.set_markup('<b>%s</b>' % text)
@@ -385,20 +447,22 @@ 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 not f.endswith(_EXCLUDE):
                 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)
@@ -438,8 +502,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