Ticket #1690: 0001-Retain-font-family-and-font-size.-Fix-1690.patch

File 0001-Retain-font-family-and-font-size.-Fix-1690.patch, 6.4 KB (added by lionaneesh, 11 years ago)

Patch for sugar-0.94, Note: This is a GCI task (http://www.google-melange.com/gci/task/view/google/gci2012/7950210)

  • AbiWordActivity.py

    From 8d09fca35aca1aecdcb1e06205ad315215a4cc29 Mon Sep 17 00:00:00 2001
    From: Aneesh Dogra <lionaneesh@gmail.com>
    Date: Fri, 30 Nov 2012 18:20:32 +0530
    Subject: [PATCH] Retain font-family and font-size. Fix #1690.
    
    Note: self.abiword_canvas.get_selection('text/plain')[1] gives random values
    if the file is empty. Therefore, I used len(self.abiword_canvas.get_content('text/plain')) instead to check for empty files.
    ---
     AbiWordActivity.py | 52 +++++++++++++++++++++++++++++++++++++++++++++++-----
     font.cfg           |  3 +++
     toolbar.py         |  4 ++--
     widgets.py         |  4 ----
     4 files changed, 52 insertions(+), 11 deletions(-)
     create mode 100644 font.cfg
    
    diff --git a/AbiWordActivity.py b/AbiWordActivity.py
    index b272f04..c86ab5e 100644
    a b  
    11# Copyright (C) 2006 by Martin Sevior
    22# Copyright (C) 2006-2007 Marc Maurer <uwog@uwog.net>
    33# Copyright (C) 2007, One Laptop Per Child
     4# Copyright (C) 2012, Aneesh Dogra <lionaneesh@gmail.com>
    45#
    56# This program is free software; you can redistribute it and/or modify
    67# it under the terms of the GNU General Public License as published by
    gobject.threads_init() 
    2728import gtk
    2829import telepathy
    2930import telepathy.client
     31import ConfigParser
    3032
    3133from abiword import Canvas
    3234
    import speech 
    5355from speechtoolbar import SpeechToolbar
    5456
    5557logger = logging.getLogger('write-activity')
     58DEFAULT_CONFIG_PATH = os.path.join(get_bundle_path(), 'font.cfg')
    5659
    5760
    5861class AbiWordActivity(activity.Activity):
    class AbiWordActivity(activity.Activity): 
    6063    def __init__(self, handle):
    6164        activity.Activity.__init__(self, handle)
    6265
     66        # Defaults, only used when no configuration is present
     67        self.font = 'Sans'
     68        self.fontsize = '12'
     69
     70        if os.path.exists('/etc/font.cfg'):
     71            self.font, self.fontsize = \
     72            self._read_configuration('/etc/font.cfg')
     73        elif os.path.exists(DEFAULT_CONFIG_PATH):
     74            self.font, self.fontsize = \
     75            self._read_configuration(DEFAULT_CONFIG_PATH)
     76
    6377        # abiword uses the current directory for all its file dialogs
    6478        os.chdir(os.path.expanduser('~'))
    6579
    class AbiWordActivity(activity.Activity): 
    97111        text_toolbar.props.icon_name = 'format-text'
    98112        text_toolbar.props.label = _('Text')
    99113        toolbar_box.toolbar.insert(text_toolbar, -1)
     114        fontsize_combo = text_toolbar.props.page.font_size_combo.combo
     115
     116        for i, s in enumerate(fontsize_combo._font_sizes):
     117            if s == self.fontsize:
     118                logging.debug("Setting fontsize to: %s, Index: %d", s, i)
     119                fontsize_combo.set_active(i)
     120                break
    100121
    101122        para_toolbar = ToolbarButton()
    102123        para_toolbar.props.page = ParagraphToolbar(self.abiword_canvas)
    class AbiWordActivity(activity.Activity): 
    189210                    self._buddy_joined_cb)
    190211            self._shared_activity.connect('buddy-left', self._buddy_left_cb)
    191212            if self.get_shared():
    192 #                # oh, OK, we've already joined
     213                # oh, OK, we've already joined
    193214                self._joined_cb()
    194215        else:
    195216            # we are creating the activity
    196217            logger.error("We are creating an activity")
    197218
     219    def _read_configuration(self, filepath):
     220        logging.debug('Reading configuration from file %s' % filepath)
     221        fp = open(filepath)
     222        config = ConfigParser.ConfigParser()
     223        config.readfp(fp)
     224        fp.close()
     225        if (config.has_section('Config') and \
     226            config.has_option('Config', 'fontsize')):
     227            fontsize = config.get('Config', 'fontsize')
     228        else:
     229            fontsize = self.fontsize
     230
     231        if (config.has_section('Config') and \
     232            config.has_option('Config', 'font')):
     233            font = config.get('Config', 'font')
     234        else:
     235            font = self.font
     236
     237        return (font, fontsize)
     238
    198239    def __map_activity_event_cb(self, event, activity):
    199240        # set custom keybindings for Write
    200241        # we do it later because have problems if done before - OLPC #11049
    class AbiWordActivity(activity.Activity): 
    205246                keybindings_file, 0, 0)
    206247        # set default font
    207248        self.abiword_canvas.select_all()
    208         # get_selection return content and length
    209         if self.abiword_canvas.get_selection('text/plain')[1] == 0:
    210             logging.error('Setting default font to Sans in new documents')
    211             self.abiword_canvas.set_font_name('Sans')
     249
     250        if len(self.abiword_canvas.get_content('text/plain')) == 0:
     251            logging.debug('Setting default font to %s in new documents' % \
     252                           self.font)
     253            self.abiword_canvas.set_font_name(self.font)
    212254        self.abiword_canvas.moveto_bod()
    213255
    214256    def get_preview(self):
  • new file font.cfg

    diff --git a/font.cfg b/font.cfg
    new file mode 100644
    index 0000000..2e85939
    - +  
     1[Config]
     2font = Sans
     3fontsize = 12
  • toolbar.py

    diff --git a/toolbar.py b/toolbar.py
    index 49c15be..78b9377 100644
    a b class TextToolbar(gtk.Toolbar): 
    420420                self._font_family_cb)
    421421        self.insert(ToolComboBox(self.font_name_combo), -1)
    422422
    423         font_size = ToolComboBox(FontSizeCombo(abiword_canvas))
    424         self.insert(font_size, -1)
     423        self.font_size_combo = ToolComboBox(FontSizeCombo(abiword_canvas))
     424        self.insert(self.font_size_combo, -1)
    425425
    426426        bold = ToggleToolButton('format-text-bold')
    427427        bold.set_tooltip(_('Bold'))
  • widgets.py

    diff --git a/widgets.py b/widgets.py
    index f12544a..c44d2a9 100644
    a b class FontSizeCombo(ComboBox): 
    9898
    9999    def __init__(self, abi):
    100100        ComboBox.__init__(self)
    101 
    102101        self._abi_handler = abi.connect('font-size', self._font_size_cb)
    103 
    104102        self._font_sizes = ['8', '9', '10', '11', '12', '14', '16', '20', \
    105103                            '22', '24', '26', '28', '36', '48', '72']
    106104        self._changed_id = self.connect('changed', self._font_size_changed_cb,
    class FontSizeCombo(ComboBox): 
    108106
    109107        for i, s in enumerate(self._font_sizes):
    110108            self.append_item(i, s, None)
    111             if s == '12':
    112                 self.set_active(i)
    113109
    114110    def _font_size_changed_cb(self, combobox, abi):
    115111        if self.get_active() != -1: