Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Launcher.py Launches the program in Sugar environment
- #
- # Copyright (C) 2011 Laurent BERNABE
- #
- # 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 the Free Software
- # Foundation; either version 2 of the License, or
- # (at your option) any later version.
- #
- # This program is distributed in the hope that it will
- # be useful, but WITHOUT ANY WARRANTY; without even
- # the implied warranty of MERCHANTABILITY or FITNESS FOR
- # A PARTICULAR PURPOSE. See the GNU General Public
- # License for more details.
- #
- # You should have received a copy of the GNU General
- # Public License along with this program; if not, write
- # to the Free Software Foundation, Inc., 51 Franklin
- # St, Fifth Floor, Boston, MA 02110-1301 USA
- from gettext import gettext as _
- from sugar.activity import activity
- from MenuActions import MenuActions
- from TheDrawingAreaEventBox import TheDrawingAreaEventBox
- import pygtk
- pygtk.require('2.0')
- import gtk
- # this snippet is taken from the moon activity project
- try:
- # For Sugar >= 0.86
- from sugar.graphics.toolbarbox import ToolbarButton, ToolbarBox, ToolButton
- from sugar.activity.widgets import ActivityToolbarButton
- except ImportError:
- # For Sugar < 0.86
- pass
- class Launcher(activity.Activity):
- '''
- Launcher for sugar bundle
- '''
- def __init__(self, handle):
- '''
- Constructor
- '''
- activity.Activity.__init__(self, handle)
- # Be carefull on the callings order !!!
- # => self.__wrapped_drawing_area is not defined before
- # self.__createAndSetCanvas() call
- # => self.__createAndSetToolbox need self.__menu_actions
- self.__createAndSetCanvas()
- self.__menu_actions = MenuActions(self.__wrapped_drawing_area)
- self.__createAndSetToolbox()
- def __createAndSetToolbox(self):
- '''
- Creates and sets the tool box
- '''
- # this snippet is inspirated by the Moon activity source code
- try:
- # For sugar >= 0.86
- toolbar_box = ToolbarBox()
- activity_button = ActivityToolbarButton(self)
- toolbar_box.toolbar.insert(activity_button, 0)
- separator = gtk.SeparatorToolItem()
- toolbar_box.toolbar.insert(separator, -1)
- parameters_selectors = gtk.ToolItem()
- parameters_selectors_box = gtk.VBox(spacing = 4)
- line_thickness_box = gtk.HBox(spacing = 8)
- line_thickness_label = gtk.Label("Line thickness (pixels)")
- line_thickness_box.add(line_thickness_label)
- #-----------------
- line_thickness_selector = gtk.HScale()
- line_thickness_selector.set_range(1,50)
- line_thickness_selector.set_increments(1,10)
- line_thickness_box.add(line_thickness_selector)
- animation_speed_box = gtk.HBox(spacing = 8)
- animation_speed_label = gtk.Label("Animation speed (milliseconds)")
- animation_speed_box.add(animation_speed_label)
- #-----------------------
- animation_speed_selector = gtk.HScale()
- animation_speed_selector.set_range(1,1000)
- animation_speed_selector.set_increments(10, 100)
- animation_speed_box.add(animation_speed_selector)
- parameters_selectors_box.add(line_thickness_box)
- parameters_selectors_box.add(animation_speed_box)
- parameters_selectors.add(parameters_selectors_box)
- toolbar_box.toolbar.insert(parameters_selectors, -1)
- playButton = ToolButton('gtk-media-play')
- toolbar_box.toolbar.insert(playButton, -1)
- toolbar_box.show_all()
- self.set_toolbar_box(toolbar_box)
- except NameError:
- # For sugar < O.86
- toolbox = activity.ActivityToolbox(self)
- self.__addDrawingToolbarIn(toolbox)
- self.set_toolbox(toolbox)
- toolbox.show_all()
- def __createAndSetCanvas(self):
- '''
- Creates and set the Canvas
- '''
- self.__wrapped_drawing_area = TheDrawingAreaEventBox()
- self.set_canvas(self.__wrapped_drawing_area)
- self.show_all()
- def __manageDrawingWrite(self, menu_item):
- '''
- Manages drawing->write menu
- '''
- if menu_item.get_active() :
- self.__menu_actions.manageDrawingWrite()
- def __manageDrawingRead(self, menu_item):
- '''
- Manages drawing->read menu
- '''
- if menu_item.get_active() :
- self.__menu_actions.manageDrawingRead()
- def __addDrawingToolbarIn(self, toolbox):
- '''
- Creates and add Drawing toolbar in toolbox
- => __addDrawingToolbarIn(toolbox)
- '''
- drawing_toolbar = gtk.Toolbar()
- drawing_toolbar.set_style(gtk.TOOLBAR_ICONS)
- #----------------------
- drawing_toolbar_write_button = gtk.RadioToolButton(None, gtk.STOCK_MEDIA_RECORD)
- drawing_toolbar_write_button.set_active(True)
- drawing_toolbar_write_button.connect("clicked", self.__manageDrawingWrite )
- drawing_toolbar.insert(drawing_toolbar_write_button, 0)
- #---------------------
- drawing_toolbar_read_button = gtk.RadioToolButton(drawing_toolbar_write_button, gtk.STOCK_MEDIA_PLAY)
- drawing_toolbar_read_button.set_active(False)
- drawing_toolbar_read_button.connect("clicked", self.__manageDrawingRead )
- drawing_toolbar.insert(drawing_toolbar_read_button, 1)
- #--------------------
- toolbox.add_toolbar(_('Drawing'), drawing_toolbar)
- drawing_toolbar.show_all()
- def read_file(self, file_path):
- '''
- Manages sugar activity resume from entry journal
- '''
- read_text_file = open(file_path, 'r')
- self.__wrapped_drawing_area.readFiguresFromTextFile(read_text_file)
- read_text_file.close()
- def write_file(self, file_path):
- '''
- Manages sugar activity journal entry updating
- '''
- self.metadata['mime_type'] = 'application/x-graph'
- saved_text_file = open(file_path, 'w')
- self.__wrapped_drawing_area.saveFiguresInTextFile(saved_text_file)
- saved_text_file.close()
Add Comment
Please, Sign In to add comment