[gedit-latex/remove-prefs: 2/3] GObjectify preferences (and remove IPreferencesMonitor)



commit aa7a1517ab0122907ee21637510ffaae31c9a2af
Author: John Stowers <john stowers gmail com>
Date:   Mon Jun 27 10:08:23 2011 +1200

    GObjectify preferences (and remove IPreferencesMonitor)

 latex/latex/editor.py         |   13 +++--------
 latex/preferences/__init__.py |   44 ++++++++--------------------------------
 latex/views.py                |    9 +++----
 3 files changed, 17 insertions(+), 49 deletions(-)
---
diff --git a/latex/latex/editor.py b/latex/latex/editor.py
index eaf6d2d..bdf09ed 100644
--- a/latex/latex/editor.py
+++ b/latex/latex/editor.py
@@ -44,10 +44,10 @@ from validator import LaTeXValidator
 from dialogs import ChooseMasterDialog
 
 from . import LaTeXSource, PropertyFile
-from ..preferences import Preferences, IPreferencesMonitor
+from ..preferences import Preferences
 
 
-class LaTeXEditor(Editor, IIssueHandler, IPreferencesMonitor):
+class LaTeXEditor(Editor, IIssueHandler):
 	
 	_log = getLogger("LaTeXEditor")
 	
@@ -73,7 +73,7 @@ class LaTeXEditor(Editor, IIssueHandler, IPreferencesMonitor):
 		self._context = context
 		
 		self._preferences = Preferences()
-		self._preferences.register_monitor(self)	# listen to 'Show...InOutline' settings
+		self._preferences.connect("preferences-changed", self._on_preferences_changed)
 
 		self.register_marker_type("latex-error", self._preferences.get("ErrorBackgroundColor"))
 		self.register_marker_type("latex-warning", self._preferences.get("WarningBackgroundColor"))
@@ -101,9 +101,7 @@ class LaTeXEditor(Editor, IIssueHandler, IPreferencesMonitor):
 		self.__parse()
 		self.__update_neighbors()
 	
-	def _on_value_changed(self, key, new_value):
-		# see preferences.IPreferencesMonitor._on_value_changed
-		
+	def _on_preferences_changed(self, prefs, key, new_value):
 		if key in ["ShowLabelsInOutline", "ShowTablesInOutline", "ShowGraphicsInOutline"]:
 			# regenerate outline model
 			if self._document_is_master:
@@ -409,9 +407,6 @@ class LaTeXEditor(Editor, IIssueHandler, IPreferencesMonitor):
 		return self._file
 
 	def destroy(self):
-		# stop listening preferences
-		self._preferences.remove_monitor(self)
-
 		# unreference the window context
 		del self._context
 		
diff --git a/latex/preferences/__init__.py b/latex/preferences/__init__.py
index 9b5cea4..8e49acb 100644
--- a/latex/preferences/__init__.py
+++ b/latex/preferences/__init__.py
@@ -44,19 +44,14 @@ def str_to_bool(x):
 	else:
 		print "str_to_bool: unsupported type %s" % str(type(x))
 
+ singleton
+class Preferences(GObject.GObject):
 
-class IPreferencesMonitor(object):
-	"""
-	This is not a real interface as classes don't have to implement all
-	methods
-	"""
-	def _on_value_changed(self, key, new_value):
-		"""
-		A simple key-value-pair has changed
-		"""
+	__gsignals__ = {
+		"preferences-changed": (
+			GObject.SignalFlags.RUN_LAST, None, [str, str]),
+	}
 
- singleton
-class Preferences(object):
 	"""
 	A simple map storing preferences as key-value-pairs
 	"""
@@ -64,28 +59,12 @@ class Preferences(object):
 	_log = getLogger("Preferences")
 	
 	def __init__(self):
-		self.__monitors = []
+		GObject.GObject.__init__(self)
 		self.__preferences_changed = False
 		self.__preferences = ElementTree.parse(
 						find_resource("preferences.xml", MODE_READWRITE)).getroot()
 		self._log.debug("Constructed")
 	
-	def register_monitor(self, monitor):
-		"""
-		Register an object monitoring the preferences
-		
-		@param monitor: an object implementing IPreferencesMonitor 
-		"""
-		self.__monitors.append(monitor)
-		
-	def remove_monitor(self, monitor):
-		"""
-		Remove a monitor
-		
-		@raise ValueError: if monitor is not found
-		"""
-		del self.__monitors[self.__monitors.index(monitor)]
-	
 	def get(self, key, default_value=None):
 		"""
 		Return the value for a given key
@@ -124,19 +103,14 @@ class Preferences(object):
 		value_element.text = str(value)
 		
 		self.__preferences_changed = True
-		
-		for monitor in self.__monitors:
-			monitor._on_value_changed(key, value)
+		self.emit("preferences-changed", str(key), str(value))
 	
 	def save(self):
 		"""
 		Save the preferences to XML
 		"""
 		if self.__preferences_changed:
+			self.__preferences_changed = False
 			self._log.debug("Saving preferences...")
-		
 			tree = ElementTree.ElementTree(self.__preferences)
 			tree.write(find_resource("preferences.xml", MODE_READWRITE), encoding="utf-8")
-			
-			self.__preferences_changed = False
-
diff --git a/latex/views.py b/latex/views.py
index 9052624..3fcaec4 100644
--- a/latex/views.py
+++ b/latex/views.py
@@ -25,14 +25,14 @@ views
 from gi.repository import Gtk, GdkPixbuf
 from logging import getLogger
 
-from preferences import Preferences, IPreferencesMonitor
+from preferences import Preferences
 from base.resources import find_resource
 from base import View, BottomView
 from issues import Issue
 from util import escape
 
 
-class IssueView(BottomView, IPreferencesMonitor):
+class IssueView(BottomView):
 	"""
 	"""
 	
@@ -51,6 +51,7 @@ class IssueView(BottomView, IPreferencesMonitor):
 		self._log.debug("init")
 		
 		self._preferences = Preferences()
+		self._preferences.connect("preferences-changed", self._on_preferences_changed)
 		self._show_tasks = self._preferences.get_bool("IssuesShowTasks", True)
 		self._show_warnings = self._preferences.get_bool("IssuesShowWarnings", True)
 		
@@ -130,7 +131,6 @@ class IssueView(BottomView, IPreferencesMonitor):
 		ctx.add_class("inline-toolbar")
 		
 		self._issues = []
-		self._preferences.register_monitor(self)
 		
 		self._log.debug("init finished")
 	
@@ -147,7 +147,7 @@ class IssueView(BottomView, IPreferencesMonitor):
 			#~ self._context.active_editor.select(issue.start, issue.end)
 		self._editor.select(issue.start, issue.end)
 	
-	def _on_value_changed(self, key, value):
+	def _on_preferences_changed(self, prefs, key, value):
 		if key == "IssuesShowWarnings" or key == "IssuesShowTasks":
 			# update filter
 			self._store.clear()
@@ -202,7 +202,6 @@ class IssueView(BottomView, IPreferencesMonitor):
 		
 	def destroy(self):
 		del self._editor
-		self._preferences.remove_monitor(self)
 		for obj in self._handlers:
 			obj.disconnect(self._handlers[obj])
 		BottomView.destroy(self)



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