From 6553d941df5aa537afbbeb39fd6f55bfef61abd7 Mon Sep 17 00:00:00 2001 From: Walter Bender Date: Wed, 08 May 2013 18:41:34 +0000 Subject: consolidation of sharing/saving/copying code --- diff --git a/TurtleArt/tawindow.py b/TurtleArt/tawindow.py index b556bdf..109149b 100644 --- a/TurtleArt/tawindow.py +++ b/TurtleArt/tawindow.py @@ -152,7 +152,9 @@ class TurtleArtWindow(): self.mouse_y = 0 self.update_counter = 0 self.running_blocks = False - self.saving_macro = False + self.saving_blocks = False + self.copying_blocks = False + self.sharing_blocks = False try: locale.setlocale(locale.LC_NUMERIC, '') @@ -1374,10 +1376,13 @@ before making changes to your Turtle Blocks program')) # From the sprite at x, y, look for a corresponding block blk = self.block_list.spr_to_block(spr) ''' If we were copying and didn't click on a block... ''' - if self.running_sugar and \ - (self.activity.copying or self.activity.sharing_blocks): + if self.copying_blocks or self.sharing_blocks or self.saving_blocks: if blk is None or blk.type != 'block': - self.activity.restore_cursor() + self.parent.get_window().set_cursor( + gtk.gdk.Cursor(gtk.gdk.LEFT_PTR)) + self.copying_blocks = False + self.sharing_blocks = False + self.saving_blocks = False if blk is not None: if blk.type == 'block': self.selected_blk = blk @@ -1873,48 +1878,59 @@ before making changes to your Turtle Blocks program')) for blk in self.drag_group: if blk.status != 'collapsed': blk.spr.set_layer(TOP_LAYER) - if self.saving_macro: + if self.copying_blocks or self.sharing_blocks or \ + self.saving_blocks: for blk in self.drag_group: if blk.status != 'collapsed': blk.highlight() self.block_operation = 'copying' data = self.assemble_data_to_save(False, False) - i = find_hat(data) - if i is not None and data[i][4][1] is not None: - try: - name = str(data[data[i][4][1]][1][1]) - except: - name = 'macro%d' % (int(uniform(0, 10000))) - debug_output('saving macro %s' % (name), - self.running_sugar) - if not os.path.exists(self.macros_path): - try: - os.makedirs(self.macros_path) - except OSError, exc: - if exc.errno == errno.EEXIST: - pass - else: - raise - data_to_file(data, os.path.join(self.macros_path, - '%s.tb' % (name))) - self.parent.get_window().set_cursor( - gtk.gdk.Cursor(gtk.gdk.LEFT_PTR)) - self.saving_macro = False - if self.running_sugar and \ - (self.activity.copying or self.activity.sharing_blocks): - for blk in self.drag_group: - if blk.status != 'collapsed': - blk.highlight() - self.block_operation = 'copying' - if self.activity.copying: - self.activity.send_to_clipboard() - else: - self.activity.share_blocks() + if data is not []: + if self.saving_blocks: + debug_output('Serialize blocks and save.', + self.running_sugar) + i = find_hat(data) + if i is not None and data[i][4][1] is not None: + try: + name = str(data[data[i][4][1]][1][1]) + except: + name = 'macro%d' % (int(uniform(0, 10000))) + debug_output('saving macro %s' % (name), + self.running_sugar) + if not os.path.exists(self.macros_path): + try: + os.makedirs(self.macros_path) + except OSError, exc: + if exc.errno == errno.EEXIST: + pass + else: + raise + data_to_file(data, os.path.join(self.macros_path, + '%s.tb' % (name))) + elif self.copying_blocks: + clipboard = gtk.Clipboard() + debug_output('Serialize blocks and copy to clipboard', + self.running_sugar) + text = data_to_string(data) + clipboard.set_text(text) + elif self.sharing(): + debug_output('Serialize blocks and send as event', + self.running_sugar) + text = data_to_string(data) + event = 'B|%s' % (data_to_string([self.nick, text])) + self.send_event(event) + self.paste_offset = 20 + + self.parent.get_window().set_cursor( + gtk.gdk.Cursor(gtk.gdk.LEFT_PTR)) + self.saving_blocks = False + if self.running_sugar and self._sharing and \ hasattr(self.activity, 'share_button'): self.activity.share_button.set_tooltip( _('Share selected blocks')) + if len(blk.spr.labels) > 0: self._saved_string = blk.spr.labels[0] self._saved_action_name = self._saved_string diff --git a/TurtleArtActivity.py b/TurtleArtActivity.py index 49e4a39..2867cb8 100644 --- a/TurtleArtActivity.py +++ b/TurtleArtActivity.py @@ -1090,9 +1090,9 @@ class TurtleArtActivity(activity.Activity): self._old_cursor = self.get_window().get_cursor() else: self._old_cursor = None - self.copying = False - self.sharing_blocks = False - self.tw.saving_macro = False + self.tw.copying_blocks = False + self.tw.sharing_blocks = False + self.tw.saving_blocks = False def _setup_sharing(self): ''' Setup the Collabora stack. ''' @@ -1344,8 +1344,9 @@ in order to use the plugin.')) def restore_cursor(self): ''' No longer copying or sharing, so restore standard cursor. ''' - self.copying = False - self.sharing_blocks = False + self.tw.copying_blocks = False + self.tw.sharing_blocks = False + self.tw.saving_blocks = False if hasattr(self, 'get_window'): if hasattr(self.get_window(), 'get_cursor'): self.get_window().set_cursor(self._old_cursor) @@ -1354,11 +1355,11 @@ in order to use the plugin.')) def _copy_cb(self, button): ''' Copy to the clipboard. ''' - if self.copying: - self.copying = False + if self.tw.copying_blocks: + self.tw.copying_blocks = False self.restore_cursor() else: - self.copying = True + self.tw.copying_blocks = True if hasattr(self, 'get_window'): if hasattr(self.get_window(), 'get_cursor'): self._old_cursor = self.get_window().get_cursor() @@ -1366,30 +1367,19 @@ in order to use the plugin.')) def _save_macro_cb(self, button): ''' Save stack macros_path ''' - if self.tw.saving_macro: - self.tw.saving_macro = False + if self.tw.saving_blocks: + self.tw.saving_blocks = False self.restore_cursor() else: - self.tw.saving_macro = True + self.tw.saving_blocks = True if hasattr(self, 'get_window'): if hasattr(self.get_window(), 'get_cursor'): self._old_cursor = self.get_window().get_cursor() self.get_window().set_cursor(gtk.gdk.Cursor(gtk.gdk.HAND1)) - def send_to_clipboard(self): - ''' Send selected stack to clipboard. ''' - self.restore_cursor() - clipboard = gtk.Clipboard() - _logger.debug('Serialize the project and copy to clipboard.') - data = self.tw.assemble_data_to_save(False, False) - if data is not []: - text = data_to_string(data) - clipboard.set_text(text) - self.tw.paste_offset = 20 - def _paste_cb(self, button): ''' Paste from the clipboard. ''' - if self.copying: + if self.tw.copying_blocks: self.restore_cursor() clipboard = gtk.Clipboard() _logger.debug('Paste to the project.') @@ -1412,28 +1402,15 @@ in order to use the plugin.')) def _share_cb(self, button): ''' Share a stack of blocks. ''' - if self.sharing_blocks: + if self.tw.sharing_blocks: self.restore_cursor() else: - self.sharing_blocks = True + self.tw.sharing_blocks = True if hasattr(self, 'get_window'): if hasattr(self.get_window(), 'get_cursor'): self._old_cursor = self.get_window().get_cursor() self.get_window().set_cursor(gtk.gdk.Cursor(gtk.gdk.HAND1)) - def share_blocks(self): - ''' Share selected stack. ''' - if not self.tw.sharing(): - return - _logger.debug('Serialize a stack and send as event.') - self.restore_cursor() - data = self.tw.assemble_data_to_save(False, False) - if data is not []: - text = data_to_string(data) - event = 'B|%s' % (data_to_string([self.tw.nick, text])) # Paste - self.tw.send_event(event) - self.tw.paste_offset = 20 - def _add_label(self, string, toolbar, width=None): ''' Add a label to a toolbar. ''' label = gtk.Label(string) diff --git a/turtleblocks.py b/turtleblocks.py index 7cf5afd..36a03de 100755 --- a/turtleblocks.py +++ b/turtleblocks.py @@ -49,12 +49,10 @@ sys.argv[1:] = [] # Execution of import gst cannot see '--help' or '-h' import gettext from TurtleArt.taconstants import (OVERLAY_LAYER, DEFAULT_TURTLE_COLORS, - TAB_LAYER, SUFFIX, MACROS) -from TurtleArt.tautils import (data_to_string, data_from_string, listify, - data_from_file, get_save_name, hat_on_top) + TAB_LAYER, SUFFIX) +from TurtleArt.tautils import (data_from_string, get_save_name) from TurtleArt.tawindow import TurtleArtWindow from TurtleArt.taexportlogo import save_logo -from TurtleArt.tapalette import make_palette from util.menubuilder import MenuBuilder @@ -66,7 +64,6 @@ class TurtleMain(): '/usr/local/share/sugar/activities/TurtleArt.activity' _ICON_SUBPATH = 'images/turtle.png' _GNOME_PLUGIN_SUBPATH = 'gnome_plugins' - _MACROS_SUBPATH = 'macros' def __init__(self): self._abspath = os.path.abspath('.') @@ -170,14 +167,6 @@ class TurtleMain(): if not exists(dpath): makedirs(dpath) - def _do_save_macro_cb(self, widget): - if self.tw.saving_macro: - self.win.get_window().set_cursor(gtk.gdk.Cursor(gtk.gdk.LEFT_PTR)) - self.tw.saving_macro = False - else: - self.win.get_window().set_cursor(gtk.gdk.Cursor(gtk.gdk.HAND1)) - self.tw.saving_macro = True - def _start_gtk(self): ''' Get a main window set up. ''' self.win.connect('configure_event', self.tw.update_overlay_position) @@ -609,16 +598,31 @@ class TurtleMain(): self.tw.stop_button() self.tw.display_coordinates() + def _do_save_macro_cb(self, widget): + ''' Callback for save stack button. ''' + self.tw.copying_blocks = False + if self.tw.saving_blocks: + self.win.get_window().set_cursor(gtk.gdk.Cursor(gtk.gdk.LEFT_PTR)) + self.tw.saving_blocks = False + else: + self.win.get_window().set_cursor(gtk.gdk.Cursor(gtk.gdk.HAND1)) + self.tw.saving_blocks = True + def _do_copy_cb(self, button): ''' Callback for copy button. ''' - clipBoard = gtk.Clipboard() - data = self.tw.assemble_data_to_save(False, False) - if data is not []: - text = data_to_string(data) - clipBoard.set_text(text) + self.tw.saving_blocks = False + if self.tw.copying_blocks: + self.win.get_window().set_cursor(gtk.gdk.Cursor(gtk.gdk.LEFT_PTR)) + self.tw.copying_blocks = False + else: + self.win.get_window().set_cursor(gtk.gdk.Cursor(gtk.gdk.HAND1)) + self.tw.copying_blocks = True def _do_paste_cb(self, button): ''' Callback for paste button. ''' + self.tw.copying_blocks = False + self.tw.saving_blocks = False + self.win.get_window().set_cursor(gtk.gdk.Cursor(gtk.gdk.LEFT_PTR)) clipBoard = gtk.Clipboard() text = clipBoard.wait_for_text() if text is not None: -- cgit v0.9.1