loloof64

LearningWriting Activity : stock icon problem

Mar 9th, 2012
369
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.44 KB | None | 0 0
  1. # Launcher.py    Launches the program in Sugar environment
  2. #
  3. # Copyright (C) 2011 Laurent BERNABE
  4. #
  5. # This program is free software; you can redistribute it
  6. # and/or modify it under the terms of the GNU General
  7. # Public License as published by the Free Software
  8. # Foundation; either version 2 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will
  12. # be useful, but WITHOUT ANY WARRANTY; without even
  13. # the implied warranty of MERCHANTABILITY or FITNESS FOR
  14. # A PARTICULAR PURPOSE.  See the GNU General Public
  15. # License for more details.
  16. #
  17. # You should have received a copy of the GNU General
  18. # Public License along with this program; if not, write
  19. # to the Free Software Foundation, Inc., 51 Franklin
  20. # St, Fifth Floor, Boston, MA 02110-1301  USA
  21.  
  22. from gettext import gettext as _
  23. from sugar.activity import activity
  24.  
  25. from MenuActions import MenuActions
  26.  
  27. from TheDrawingAreaEventBox import TheDrawingAreaEventBox
  28.  
  29. import pygtk
  30. pygtk.require('2.0')
  31. import gtk
  32.  
  33. # this snippet is taken from the moon activity project
  34. try:
  35.     # For Sugar >= 0.86
  36.     from sugar.graphics.toolbarbox import ToolbarButton, ToolbarBox, ToolButton
  37.     from sugar.activity.widgets import ActivityToolbarButton
  38. except ImportError:
  39.     # For Sugar < 0.86
  40.     pass
  41.  
  42. class Launcher(activity.Activity):
  43.     '''
  44.    Launcher for sugar bundle
  45.    '''
  46.  
  47.     def __init__(self, handle):
  48.         '''
  49.        Constructor
  50.        '''
  51.         activity.Activity.__init__(self, handle)
  52.         # Be carefull on the callings order !!!
  53.         # => self.__wrapped_drawing_area is not defined before
  54.         # self.__createAndSetCanvas() call
  55.         # => self.__createAndSetToolbox need self.__menu_actions
  56.         self.__createAndSetCanvas()
  57.         self.__menu_actions = MenuActions(self.__wrapped_drawing_area)
  58.         self.__createAndSetToolbox()
  59.        
  60.     def __createAndSetToolbox(self):
  61.         '''
  62.        Creates and sets the tool box
  63.        '''
  64.         # this snippet is inspirated by the Moon activity source code
  65.         try:
  66.             # For sugar >= 0.86
  67.             toolbar_box = ToolbarBox()
  68.             activity_button = ActivityToolbarButton(self)
  69.             toolbar_box.toolbar.insert(activity_button, 0)
  70.             separator = gtk.SeparatorToolItem()
  71.             toolbar_box.toolbar.insert(separator, -1)
  72.            
  73.             parameters_selectors = gtk.ToolItem()
  74.             parameters_selectors_box = gtk.VBox(spacing = 4)
  75.            
  76.             line_thickness_box = gtk.HBox(spacing = 8)
  77.             line_thickness_label = gtk.Label("Line thickness (pixels)")
  78.             line_thickness_box.add(line_thickness_label)
  79.             #-----------------
  80.             line_thickness_selector = gtk.HScale()
  81.             line_thickness_selector.set_range(1,50)
  82.             line_thickness_selector.set_increments(1,10)
  83.             line_thickness_box.add(line_thickness_selector)
  84.            
  85.             animation_speed_box = gtk.HBox(spacing = 8)
  86.             animation_speed_label = gtk.Label("Animation speed (milliseconds)")
  87.             animation_speed_box.add(animation_speed_label)
  88.             #-----------------------
  89.             animation_speed_selector = gtk.HScale()
  90.             animation_speed_selector.set_range(1,1000)
  91.             animation_speed_selector.set_increments(10, 100)
  92.             animation_speed_box.add(animation_speed_selector)
  93.            
  94.             parameters_selectors_box.add(line_thickness_box)
  95.             parameters_selectors_box.add(animation_speed_box)
  96.             parameters_selectors.add(parameters_selectors_box)
  97.             toolbar_box.toolbar.insert(parameters_selectors, -1)
  98.            
  99.             playButton = ToolButton('gtk-media-play')
  100.             toolbar_box.toolbar.insert(playButton, -1)
  101.            
  102.             toolbar_box.show_all()
  103.             self.set_toolbar_box(toolbar_box)
  104.            
  105.         except NameError:
  106.             # For sugar < O.86
  107.             toolbox = activity.ActivityToolbox(self)
  108.             self.__addDrawingToolbarIn(toolbox)
  109.             self.set_toolbox(toolbox)
  110.             toolbox.show_all()
  111.        
  112.     def __createAndSetCanvas(self):
  113.         '''
  114.        Creates and set the Canvas
  115.        '''
  116.         self.__wrapped_drawing_area = TheDrawingAreaEventBox()
  117.         self.set_canvas(self.__wrapped_drawing_area)
  118.         self.show_all()
  119.    
  120.     def __manageDrawingWrite(self, menu_item):
  121.         '''
  122.        Manages drawing->write menu
  123.        '''
  124.         if menu_item.get_active() :
  125.             self.__menu_actions.manageDrawingWrite()
  126.        
  127.     def __manageDrawingRead(self, menu_item):
  128.         '''
  129.        Manages drawing->read menu
  130.        '''
  131.         if menu_item.get_active() :
  132.             self.__menu_actions.manageDrawingRead()
  133.        
  134.     def __addDrawingToolbarIn(self, toolbox):
  135.         '''
  136.        Creates and add Drawing toolbar in toolbox
  137.        => __addDrawingToolbarIn(toolbox)
  138.        '''
  139.         drawing_toolbar = gtk.Toolbar()
  140.         drawing_toolbar.set_style(gtk.TOOLBAR_ICONS)
  141.         #----------------------
  142.         drawing_toolbar_write_button = gtk.RadioToolButton(None, gtk.STOCK_MEDIA_RECORD)
  143.         drawing_toolbar_write_button.set_active(True)
  144.         drawing_toolbar_write_button.connect("clicked", self.__manageDrawingWrite )
  145.         drawing_toolbar.insert(drawing_toolbar_write_button, 0)
  146.         #---------------------
  147.         drawing_toolbar_read_button = gtk.RadioToolButton(drawing_toolbar_write_button, gtk.STOCK_MEDIA_PLAY)
  148.         drawing_toolbar_read_button.set_active(False)
  149.         drawing_toolbar_read_button.connect("clicked", self.__manageDrawingRead )
  150.         drawing_toolbar.insert(drawing_toolbar_read_button, 1)
  151.         #--------------------
  152.         toolbox.add_toolbar(_('Drawing'), drawing_toolbar)
  153.         drawing_toolbar.show_all()
  154.    
  155.     def read_file(self, file_path):
  156.         '''
  157.        Manages sugar activity resume from entry journal
  158.        '''
  159.         read_text_file = open(file_path, 'r')
  160.         self.__wrapped_drawing_area.readFiguresFromTextFile(read_text_file)
  161.         read_text_file.close()
  162.        
  163.     def write_file(self, file_path):
  164.         '''
  165.        Manages sugar activity journal entry updating
  166.        '''
  167.         self.metadata['mime_type'] = 'application/x-graph'
  168.         saved_text_file = open(file_path, 'w')
  169.         self.__wrapped_drawing_area.saveFiguresInTextFile(saved_text_file)
  170.         saved_text_file.close()
Add Comment
Please, Sign In to add comment