From fc2763a5492b2028a2133e17422b4236a5df8370 Mon Sep 17 00:00:00 2001 From: Gonzalo Odiard Date: Fri, 14 Jun 2013 22:01:30 +0000 Subject: Replace the combo used to change the font by a button with a palette This is needed to use with touch. As the combo was removed, is not needed hide the labels with the screen rotated anymore. The RoundBox containing the buttons in the CardEditors was replaced by a HBox due to size concerns. The list of fonts is read from the configuration files. Signed-off-by: Gonzalo Odiard --- diff --git a/createcardpanel.py b/createcardpanel.py index 0b7a17b..031b393 100644 --- a/createcardpanel.py +++ b/createcardpanel.py @@ -31,14 +31,14 @@ from sugar.graphics.icon import Icon from sugar.graphics.palette import Palette from sugar.graphics.toggletoolbutton import ToggleToolButton from sugar.graphics.toolcombobox import ToolComboBox -from fontcombobox import FontComboBox +from fontcombobox import FontButton from port import chooser import theme import speak.espeak import speak.widgets import speak.face -from port.roundbox import RoundBox +#from port.roundbox import RoundBox import model _logger = logging.getLogger('memorize-activity') @@ -112,12 +112,8 @@ class CreateCardPanel(gtk.EventBox): if not self._activity.portrait_mode: self.card_box = gtk.HBox() - self.cardeditor1.show_labels() - self.cardeditor2.show_labels() else: self.card_box = gtk.VBox() - self.cardeditor1.hide_labels() - self.cardeditor2.hide_labels() self.card_box.pack_start(self.cardeditor1) self.card_box.pack_start(self.cardeditor2) @@ -137,12 +133,8 @@ class CreateCardPanel(gtk.EventBox): if not self._activity.portrait_mode: self.card_box = gtk.HBox() - self.cardeditor1.show_labels() - self.cardeditor2.show_labels() else: self.card_box = gtk.VBox() - self.cardeditor1.hide_labels() - self.cardeditor2.hide_labels() self.card_box.pack_start(self.cardeditor1) self.card_box.pack_start(self.cardeditor2) @@ -343,7 +335,7 @@ class CardEditor(gtk.EventBox): self.textentry.connect('changed', self.update_text) box.pack_start(self.textentry, False) - toolbar = RoundBox() + toolbar = gtk.HBox() browsepicture = ToolButton( icon_name='import_picture', @@ -367,24 +359,14 @@ class CardEditor(gtk.EventBox): else: self.usespeak = None - self.font_combo = FontComboBox() - self.id_font_changed = self.font_combo.connect("changed", - self.__font_changed_cb) - self.font_combo.set_font_name(model.DEFAULT_FONT) - - box.pack_start(self.font_combo, True, True, 0) + self.fontbutton = FontButton() + toolbar.add(self.fontbutton) + self.id_font_changed = self.fontbutton.connect( + 'changed', self.__font_changed_cb) box.pack_start(toolbar, True, True, 0) self.add(box) - def hide_labels(self): - self.previewlabel.hide() - self.textlabel.hide() - - def show_labels(self): - self.previewlabel.show() - self.textlabel.show() - def __font_changed_cb(self, widget): font = widget.get_font_name() logging.error('Selected font %s', font) @@ -393,9 +375,9 @@ class CardEditor(gtk.EventBox): self.emit('change-font', font) def set_font_name(self, font_name): - self.font_combo.handler_block(self.id_font_changed) - self.font_combo.set_font_name(font_name) - self.font_combo.handler_unblock(self.id_font_changed) + self.fontbutton.handler_block(self.id_font_changed) + self.fontbutton.set_font_name(font_name) + self.fontbutton.handler_unblock(self.id_font_changed) def update_text(self, entry): self.card.change_text(entry.get_text()) @@ -503,7 +485,7 @@ class CardEditor(gtk.EventBox): return self.snd def get_font_name(self): - return self.font_combo.get_font_name() + return self.fontbutton.get_font_name() def clean(self): self.textentry.set_text('') diff --git a/fontcombobox.py b/fontcombobox.py index 58f9140..62e79e8 100644 --- a/fontcombobox.py +++ b/fontcombobox.py @@ -15,51 +15,119 @@ # 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 -import gtk -FONT_BLACKLIST = ['cmex10', 'cmmi10', 'cmr10', 'cmsy10', 'esint10', 'eufm10', - 'msam10', 'msbm10', 'rsfs10', 'wasy10'] +import os +from gettext import gettext as _ +from gobject import SIGNAL_RUN_FIRST, TYPE_PYOBJECT +import gio -class FontComboBox(gtk.ComboBox): +from sugar.graphics.menuitem import MenuItem +from sugar.graphics.toolbutton import ToolButton +from sugar import env + +DEFAULT_FONTS = ['Sans', 'Serif', 'Monospace'] +USER_FONTS_FILE_PATH = env.get_profile_path('fonts') +GLOBAL_FONTS_FILE_PATH = '/etc/sugar_fonts' + + +class FontButton(ToolButton): + + __gsignals__ = { + 'changed': (SIGNAL_RUN_FIRST, None, []), + } def __init__(self): - gtk.ComboBox.__init__(self) - font_renderer = gtk.CellRendererText() - self.pack_start(font_renderer) - self.add_attribute(font_renderer, 'text', 0) - self.add_attribute(font_renderer, 'font', 0) - font_model = gtk.ListStore(str) + ToolButton.__init__(self, icon_name='font-text', + tooltip=_('Select font')) + self.connect('clicked', self.__font_selection_cb) context = self.get_pango_context() font_index = 0 - self.faces = {} + self._init_font_list() + + self._font_name = 'Sans' + font_names = [] for family in context.list_families(): name = family.get_name() - if name not in FONT_BLACKLIST: - font_model.append([name]) - font_faces = [] - for face in family.list_faces(): - face_name = face.get_face_name() - font_faces.append(face_name) - self.faces[name] = font_faces - - sorter = gtk.TreeModelSort(font_model) - sorter.set_sort_column_id(0, gtk.SORT_ASCENDING) - self.set_model(sorter) + if name in self._font_white_list: + font_names.append(name) + + for font_name in sorted(font_names): + menu_item = MenuItem(font_name) + markup = '%s' % (font_name, font_name) + menu_item.get_children()[0].set_markup(markup) + menu_item.connect('activate', self.__menu_activated, font_name) + self.props.palette.menu.append(menu_item) + menu_item.show() + self.show() + def __font_selection_cb(self, widget): + if self.props.palette: + if not self.props.palette.is_up(): + self.props.palette.popup(immediate=True, + state=self.props.palette.SECONDARY) + else: + self.props.palette.popdown(immediate=True) + return + + def __menu_activated(self, menu, font_name): + self._font_name = font_name + self.emit('changed') + def set_font_name(self, font_name): - count = 0 - tree_iter = self.get_model().get_iter_first() - while tree_iter is not None: - value = self.get_model().get_value(tree_iter, 0) - if value == font_name: - self.set_active(count) - count = count + 1 - tree_iter = self.get_model().iter_next(tree_iter) + self._font_name = font_name def get_font_name(self): - tree_iter = self.get_active_iter() - return self.get_model().get_value(tree_iter, 0) + return self._font_name + + def _init_font_list(self): + self._font_white_list = [] + self._font_white_list.extend(DEFAULT_FONTS) + + # check if there are a user configuration file + if not os.path.exists(USER_FONTS_FILE_PATH): + # verify if exists a file in /etc + if os.path.exists(GLOBAL_FONTS_FILE_PATH): + shutil.copy(GLOBAL_FONTS_FILE_PATH, USER_FONTS_FILE_PATH) + + if os.path.exists(USER_FONTS_FILE_PATH): + # get the font names in the file to the white list + fonts_file = open(USER_FONTS_FILE_PATH) + # get the font names in the file to the white list + for line in fonts_file: + self._font_white_list.append(line.strip()) + # monitor changes in the file + gio_fonts_file = gio.File(USER_FONTS_FILE_PATH) + self.monitor = gio_fonts_file.monitor_file() + self.monitor.set_rate_limit(5000) + self.monitor.connect('changed', self._reload_fonts) + + def _reload_fonts(self, monitor, gio_file, other_file, event): + if event != gio.FILE_MONITOR_EVENT_CHANGES_DONE_HINT: + return + self._font_white_list = [] + self._font_white_list.extend(DEFAULT_FONTS) + fonts_file = open(USER_FONTS_FILE_PATH) + for line in fonts_file: + self._font_white_list.append(line.strip()) + # update the menu + for child in self.props.palette.menu.get_children(): + self.props.palette.menu.remove(child) + child = None + context = self.get_pango_context() + tmp_list = [] + for family in context.list_families(): + name = family.get_name() + if name in self._font_white_list: + tmp_list.append(name) + for font_name in sorted(tmp_list): + menu_item = MenuItem(font_name) + markup = '%s' % (font_name, font_name) + menu_item.get_children()[0].set_markup(markup) + menu_item.connect('activate', self.__menu_activated, font_name) + self.props.palette.menu.append(menu_item) + menu_item.show() + return False diff --git a/icons/font-text.svg b/icons/font-text.svg new file mode 100644 index 0000000..ad3f9fa --- /dev/null +++ b/icons/font-text.svg @@ -0,0 +1,32 @@ + + + +image/svg+xmlFF + \ No newline at end of file -- cgit v0.9.1