[meld] preferences: Update font preference handling to use GSettings



commit 51b4e26ef0e08c806ff82c071c380c1a7d7cefb2
Author: Kai Willadsen <kai willadsen gmail com>
Date:   Tue Nov 26 19:24:31 2013 +1000

    preferences: Update font preference handling to use GSettings

 data/org.gnome.meld.gschema.xml |   11 +++++++++++
 data/ui/preferences.ui          |    2 --
 meld/filediff.py                |   17 ++++++++++-------
 meld/patchdialog.py             |   16 +++++++---------
 meld/preferences.py             |   30 ++++++------------------------
 meld/settings.py                |   20 ++++++++++++++++----
 meld/ui/vcdialogs.py            |    8 ++++----
 7 files changed, 54 insertions(+), 50 deletions(-)
---
diff --git a/data/org.gnome.meld.gschema.xml b/data/org.gnome.meld.gschema.xml
index 09e8e2e..38cf1ab 100644
--- a/data/org.gnome.meld.gschema.xml
+++ b/data/org.gnome.meld.gschema.xml
@@ -45,6 +45,17 @@
           <summary>Highlight current line</summary>
           <description>If true, the line containing the cursor will be highlighted in file 
comparisons.</description>
       </key>
+      <key name="use-system-font" type="b">
+          <default>true</default>
+          <summary>Use the system default monospace font</summary>
+          <description>If false, the defined custom font will be used instead of the system monospace 
font.</description>
+      </key>
+      <key name="custom-font" type="s">
+          <default>"monospace, 14"</default>
+          <summary>Custom font</summary>
+          <description>The custom font to use, stored as a string and parsed as a Pango font 
description</description>
+      </key>
+
 
       <!-- External helper properties -->
       <key name="use-system-editor" type="b">
diff --git a/data/ui/preferences.ui b/data/ui/preferences.ui
index 8ea2c9c..5e68322 100644
--- a/data/ui/preferences.ui
+++ b/data/ui/preferences.ui
@@ -152,7 +152,6 @@
                                 <property name="xalign">0</property>
                                 <property name="active">True</property>
                                 <property name="draw_indicator">True</property>
-                                <signal name="toggled" handler="on_checkbutton_default_font_toggled" 
swapped="no"/>
                               </object>
                               <packing>
                                 <property name="expand">False</property>
@@ -187,7 +186,6 @@
                                     <property name="receives_default">False</property>
                                     <property name="font_name">Monospace 12</property>
                                     <property name="use_font">True</property>
-                                    <signal name="font-set" handler="on_fontpicker_font_set" swapped="no"/>
                                   </object>
                                   <packing>
                                     <property name="expand">True</property>
diff --git a/meld/filediff.py b/meld/filediff.py
index b514ef8..8e46428 100644
--- a/meld/filediff.py
+++ b/meld/filediff.py
@@ -47,7 +47,6 @@ from .ui import findbar
 from .ui import gnomeglade
 
 from meld.settings import meldsettings, settings
-from .meldapp import app
 from .util.compat import text_type
 from .util.sourceviewer import LanguageManager
 
@@ -364,6 +363,8 @@ class FileDiff(melddoc.MeldDoc, gnomeglade.Component):
         settings.bind('highlight-current-line', self, 'highlight-current-line',
                       Gio.SettingsBindFlags.DEFAULT)
 
+        meldsettings.connect('changed', self.on_setting_changed)
+
     def get_keymask(self):
         return self._keymask
     def set_keymask(self, value):
@@ -814,20 +815,22 @@ class FileDiff(melddoc.MeldDoc, gnomeglade.Component):
         self.deleted_lines_pending = -1
 
     def load_font(self):
-        fontdesc = Pango.FontDescription(self.prefs.get_current_font())
         context = self.textview0.get_pango_context()
-        metrics = context.get_metrics(fontdesc, context.get_language())
+        metrics = context.get_metrics(meldsettings.font,
+                                      context.get_language())
         line_height_points = metrics.get_ascent() + metrics.get_descent()
         self.pixels_per_line = line_height_points // 1024
         for i in range(3):
-            self.textview[i].override_font(fontdesc)
+            self.textview[i].override_font(meldsettings.font)
         for i in range(2):
             self.linkmap[i].queue_draw()
 
-    def on_preference_changed(self, key, value):
-        if key == "use_custom_font" or key == "custom_font":
+    def on_setting_changed(self, settings, key):
+        if key == 'font':
             self.load_font()
-        elif key == "show_whitespace":
+
+    def on_preference_changed(self, key, value):
+        if key == "show_whitespace":
             for v in self.textview:
                 v.set_draw_spaces(value)
         elif key == "edit_wrap_lines":
diff --git a/meld/patchdialog.py b/meld/patchdialog.py
index eb6920e..3cbf2ba 100644
--- a/meld/patchdialog.py
+++ b/meld/patchdialog.py
@@ -21,11 +21,11 @@ from gettext import gettext as _
 import os
 
 from gi.repository import Gtk
-from gi.repository import Pango
 from gi.repository import GtkSource
 
 from .ui import gnomeglade
 
+from meld.settings import meldsettings
 from .util.compat import text_type
 from .util.sourceviewer import LanguageManager
 
@@ -36,8 +36,6 @@ class PatchDialog(gnomeglade.Component):
         gnomeglade.Component.__init__(self, "patch-dialog.ui", "patchdialog")
 
         self.widget.set_transient_for(filediff.widget.get_toplevel())
-        self.prefs = filediff.prefs
-        self.prefs.notify_add(self.on_preference_changed)
         self.filediff = filediff
 
         buf = GtkSource.Buffer()
@@ -46,8 +44,7 @@ class PatchDialog(gnomeglade.Component):
         buf.set_language(lang)
         buf.set_highlight_syntax(True)
 
-        fontdesc = Pango.FontDescription(self.prefs.get_current_font())
-        self.textview.modify_font(fontdesc)
+        self.textview.modify_font(meldsettings.font)
         self.textview.set_editable(False)
 
         self.index_map = {self.left_radiobutton: (0, 1),
@@ -59,10 +56,11 @@ class PatchDialog(gnomeglade.Component):
             self.label3.hide()
             self.hbox2.hide()
 
-    def on_preference_changed(self, key, value):
-        if key == "use_custom_font" or key == "custom_font":
-            fontdesc = Pango.FontDescription(self.prefs.get_current_font())
-            self.textview.modify_font(fontdesc)
+        meldsettings.connect('changed', self.on_setting_changed)
+
+    def on_setting_changed(self, setting, key):
+        if key == "font":
+            self.textview.modify_font(meldsettings.font)
 
     def on_buffer_selection_changed(self, radiobutton):
         if not radiobutton.get_active():
diff --git a/meld/preferences.py b/meld/preferences.py
index 82007bb..28ed3d0 100644
--- a/meld/preferences.py
+++ b/meld/preferences.py
@@ -28,7 +28,7 @@ from .ui import gnomeglade
 from .ui import listwidget
 from .util import prefs
 
-from meld.settings import settings, interface_settings
+from meld.settings import settings
 
 
 TIMESTAMP_RESOLUTION_PRESETS = [('1ns (ext4)', 1),
@@ -171,6 +171,8 @@ class PreferencesDialog(gnomeglade.Component):
         self.prefs = prefs
 
         bindings = [
+            ('use-system-font', self.checkbutton_default_font, 'active'),
+            ('custom-font', self.fontpicker, 'font'),
             ('indent-width', self.spinbutton_tabsize, 'value'),
             ('insert-spaces-instead-of-tabs', self.checkbutton_spaces_instead_of_tabs, 'active'),
             ('highlight-current-line', self.checkbutton_highlight_current_line, 'active'),
@@ -194,15 +196,10 @@ class PreferencesDialog(gnomeglade.Component):
         settings.bind(
             'use-system-editor', self.custom_edit_command_entry, 'sensitive',
             Gio.SettingsBindFlags.DEFAULT | Gio.SettingsBindFlags.INVERT_BOOLEAN)
+        settings.bind(
+            'use-system-font', self.fontpicker, 'sensitive',
+            Gio.SettingsBindFlags.DEFAULT | Gio.SettingsBindFlags.INVERT_BOOLEAN)
 
-        if not self.prefs.use_custom_font:
-            self.checkbutton_default_font.set_active(True)
-            self.fontpicker.set_sensitive(False)
-        else:
-            self.checkbutton_default_font.set_active(False)
-            self.fontpicker.set_sensitive(True)
-            self.fontpicker.set_font_name(self.prefs.custom_font)
-        self.fontpicker.set_font_name( self.prefs.custom_font )
         self.checkbutton_show_whitespace.set_active(
             self.prefs.show_whitespace)
         # TODO: This doesn't restore the state of character wrapping when word
@@ -242,14 +239,6 @@ class PreferencesDialog(gnomeglade.Component):
 
         self.widget.show()
 
-    def on_fontpicker_font_set(self, picker):
-        self.prefs.custom_font = picker.get_font_name()
-
-    def on_checkbutton_default_font_toggled(self, button):
-        use_custom = not button.get_active()
-        self.fontpicker.set_sensitive(use_custom)
-        self.prefs.use_custom_font = use_custom
-
     def on_checkbutton_wrap_text_toggled(self, button):
         if not self.checkbutton_wrap_text.get_active():
             self.prefs.edit_wrap_lines = 0
@@ -283,8 +272,6 @@ class MeldPreferences(prefs.Preferences):
     defaults = {
         "window_size_x": prefs.Value(prefs.INT, 600),
         "window_size_y": prefs.Value(prefs.INT, 600),
-        "use_custom_font": prefs.Value(prefs.BOOL,0),
-        "custom_font": prefs.Value(prefs.STRING,"monospace, 14"),
         "show_whitespace": prefs.Value(prefs.BOOL, False),
         "edit_wrap_lines" : prefs.Value(prefs.INT, 0),
         "text_codecs": prefs.Value(prefs.STRING, "utf8 latin1"),
@@ -300,8 +287,3 @@ class MeldPreferences(prefs.Preferences):
 
     def __init__(self):
         super(MeldPreferences, self).__init__("/apps/meld", self.defaults)
-
-    def get_current_font(self):
-        if self.use_custom_font:
-            return self.custom_font
-        return interface_settings.get_string('monospace-font-name')
diff --git a/meld/settings.py b/meld/settings.py
index 0f4d001..2955139 100644
--- a/meld/settings.py
+++ b/meld/settings.py
@@ -16,6 +16,7 @@
 
 from gi.repository import Gio
 from gi.repository import GObject
+from gi.repository import Pango
 
 import meld.conf
 import meld.filters
@@ -35,16 +36,16 @@ class MeldSettings(GObject.GObject):
     """Handler for settings that can't easily be bound to object properties"""
 
     __gsignals__ = {
-        'file-filters-changed': (GObject.SignalFlags.RUN_FIRST,
-                                 None, ()),
-        'text-filters-changed': (GObject.SignalFlags.RUN_FIRST,
-                                 None, ()),
+        'file-filters-changed': (GObject.SignalFlags.RUN_FIRST, None, ()),
+        'text-filters-changed': (GObject.SignalFlags.RUN_FIRST, None, ()),
+        'changed': (GObject.SignalFlags.RUN_FIRST, None, (str,)),
     }
 
     def __init__(self):
         GObject.GObject.__init__(self)
         self.on_setting_changed(settings, 'filename-filters')
         self.on_setting_changed(settings, 'text-filters')
+        self.on_setting_changed(settings, 'use-system-font')
         settings.connect('changed', self.on_setting_changed)
 
     def on_setting_changed(self, settings, key):
@@ -56,6 +57,9 @@ class MeldSettings(GObject.GObject):
             self.text_filters = self._filters_from_gsetting(
                 'text-filters', meld.filters.FilterEntry.REGEX)
             self.emit('text-filters-changed')
+        elif key in ('use-system-font', 'custom-font'):
+            self.font = self._current_font_from_gsetting()
+            self.emit('changed', 'font')
 
     def _filters_from_gsetting(self, key, filt_type):
         filter_params = settings.get_value(key)
@@ -65,4 +69,12 @@ class MeldSettings(GObject.GObject):
         ]
         return filters
 
+    def _current_font_from_gsetting(self, *args):
+        if settings.get_boolean('use-system-font'):
+            font_string = interface_settings.get_string('monospace-font-name')
+        else:
+            font_string = settings.get_string('custom-font')
+        return Pango.FontDescription(font_string)
+
+
 meldsettings = MeldSettings()
diff --git a/meld/ui/vcdialogs.py b/meld/ui/vcdialogs.py
index d1cbb75..a2b16f8 100644
--- a/meld/ui/vcdialogs.py
+++ b/meld/ui/vcdialogs.py
@@ -28,7 +28,7 @@ from gi.repository import Gtk
 from gi.repository import Pango
 
 from meld.misc import commonprefix
-from meld.settings import settings
+from meld.settings import meldsettings, settings
 from meld.ui.gnomeglade import Component
 
 
@@ -67,8 +67,7 @@ class CommitDialog(GObject.GObject, Component):
         self.changedfiles.set_text("(in %s)\n%s" %
                                    (topdir, "\n".join(to_commit)))
 
-        fontdesc = Pango.FontDescription(self.parent.prefs.get_current_font())
-        self.textview.modify_font(fontdesc)
+        self.textview.modify_font(meldsettings.font)
         commit_prefill = self.parent.vc.get_commit_message_prefill()
         if commit_prefill:
             buf = self.textview.get_buffer()
@@ -78,7 +77,8 @@ class CommitDialog(GObject.GObject, Component):
         # Try and make the textview wide enough for a standard 80-character
         # commit message.
         context = self.textview.get_pango_context()
-        metrics = context.get_metrics(fontdesc, context.get_language())
+        metrics = context.get_metrics(meldsettings.font,
+                                      context.get_language())
         char_width = metrics.get_approximate_char_width() / Pango.SCALE
         self.scrolledwindow1.set_size_request(80 * char_width, -1)
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]