[gedit-latex] Clean up IconAction



commit 53e255284f55d479620245f931e7c09f1b4e41e0
Author: John Stowers <john stowers gmail com>
Date:   Thu Aug 18 09:43:23 2011 +1200

    Clean up IconAction

 latex/base/action.py   |   54 +++++++++++++++++++++++++--
 latex/latex/actions.py |   95 ++++++++++++------------------------------------
 latex/util.py          |   50 -------------------------
 3 files changed, 73 insertions(+), 126 deletions(-)
---
diff --git a/latex/base/action.py b/latex/base/action.py
index cc4f4d3..6424d60 100644
--- a/latex/base/action.py
+++ b/latex/base/action.py
@@ -18,7 +18,11 @@
 # this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
 # Street, Fifth Floor, Boston, MA  02110-1301, USA
 
-from gi.repository import Gtk
+from uuid import uuid1
+from gi.repository import Gtk, GdkPixbuf
+
+from ..base.file import File
+from ..base.resources import Resources
 
 class GeditLaTeXPlugin_MenuToolAction(Gtk.Action):
     __gtype_name__ = "GeditLaTeXPlugin_MenuToolAction"
@@ -34,7 +38,7 @@ class Action(object):
     menu_tool_action = False    # if True a MenuToolAction is created and hooked for this action
                                 # instead of Gtk.Action
 
-    extensions = [None]            # a list of file extensions for which this action should be enabled
+    extensions = [None]         # a list of file extensions for which this action should be enabled
                                 # [None] indicates that this action is to be enabled for all extensions
 
     def __init__(self, *args, **kwargs):
@@ -82,7 +86,49 @@ class Action(object):
         self._internal_action.disconnect(self._handler)
         action_group.remove_action(self._internal_action)
 
-    #~ def __del__(self):
-        #~ print "Properly destroyed Action %s" % self
+
+class IconAction(Action):
+    """
+    A utility class for creating actions with a custom icon instead of
+    a gtk stock id.
+
+    The subclass must provide a field 'icon'.
+    """
+
+    icon_name = None   
+    __stock_id = None
+
+    def __init__(self, *args, **kwargs):
+        self.__icon_factory = kwargs["icon_factory"]
+        self.__icon = None
+
+    @property
+    def icon(self):
+        """
+        Return a File object for the icon to use
+        """
+        if not self.__icon:
+            assert(self.icon_name)
+            self.__icon = File(Resources().get_icon("%s.png" % self.icon_name)) 
+        return self.__icon
+
+    def __init_stock_id(self):
+        #
+        # generate a new stock id
+        #
+        self.__stock_id = str(uuid1())
+        self.__icon_factory.add(
+                self.__stock_id,
+                Gtk.IconSet.new_from_pixbuf(
+                    GdkPixbuf.Pixbuf.new_from_file(self.icon.path)))
+
+    @property
+    def stock_id(self):
+        if self.icon:
+            if not self.__stock_id:
+                self.__init_stock_id()
+            return self.__stock_id
+        else:
+            return None
 
 # ex:ts=4:et:
diff --git a/latex/latex/actions.py b/latex/latex/actions.py
index d5394f8..2e486e0 100644
--- a/latex/latex/actions.py
+++ b/latex/latex/actions.py
@@ -26,11 +26,8 @@ from gi.repository import Gtk
 from logging import getLogger
 
 from ..base import Template
-from ..base.action import Action
-from ..base.file import File
-from ..base.resources import Resources
+from ..base.action import Action, IconAction
 from ..preferences import Preferences
-from ..util import IconAction
 from ..issues import MockIssueHandler
 from ..tools import ToolRunner
 from .editor import LaTeXEditor
@@ -40,6 +37,8 @@ from .dialogs import UseBibliographyDialog, InsertGraphicsDialog, InsertTableDia
                     NewDocumentDialog, ChooseMasterDialog
 from . import LaTeXSource
 
+LOG = getLogger(__name__)
+
 class LaTeXAction(Action):
     extensions = Preferences().get("latex-extensions").split(",")
 
@@ -53,21 +52,14 @@ class LaTeXTemplateAction(LaTeXIconAction):
     Utility base class for quickly defining Actions inserting a LaTeX template
     """
     accelerator = None
-
-    icon_name = None
     template_source = None
     packages = []
 
-    @property
-    def icon(self):
-        return File(Resources().get_icon("%s.png" % self.icon_name))
-
     def activate(self, context):
         context.active_editor.insert(LaTeXSource(Template(self.template_source), self.packages))
 
 
 class LaTeXMenuAction(LaTeXAction):
-
     label = "LaTeX"
     stock_id = None
     accelerator = None
@@ -83,25 +75,23 @@ class LaTeXNewAction(Action):
     accelerator = "<Ctrl><Alt>N"
     tooltip = "Create a new LaTeX document"
 
-    _dialog = None
+    dialog = None
 
     def activate(self, context):
-        if not self._dialog:
-            self._dialog = NewDocumentDialog()
+        if not self.dialog:
+            self.dialog = NewDocumentDialog()
 
         # we may not open the empty file and insert a Temlate here
         # because WindowContext.activate_editor calls gedit.Window.create_tab_from_uri
         # which is async
 
-        if self._dialog.run() == 1:
-            file = self._dialog.file
-            file.create(self._dialog.source)
+        if self.dialog.run() == 1:
+            file = self.dialog.file
+            file.create(self.dialog.source)
             context.activate_editor(file)
 
 
 class LaTeXChooseMasterAction(LaTeXAction):
-    _log = getLogger("LaTeXChooseMasterAction")
-
     label = "Choose Master Document..."
     stock_id = None
     accelerator = None
@@ -114,25 +104,18 @@ class LaTeXChooseMasterAction(LaTeXAction):
         editor.choose_master_file()
 
 class LaTeXCloseEnvironmentAction(LaTeXIconAction):
-    _log = getLogger("LaTeXCloseEnvironmentAction")
-
     label = "Close Nearest Environment"
     accelerator = "<Ctrl><Alt>E"
     tooltip = "Close the nearest TeX environment at left of the cursor"
-
-    @property
-    def icon(self):
-        return File(Resources().get_icon("close_env.png"))
+    icon_name = "close_env"
 
     def activate(self, context):
         # FIXME: use the document model of the Editor
 
         editor = context.active_editor
-
         assert type(editor) is LaTeXEditor
 
         # push environments on stack and find nearest one to close
-
         try:
             self._stack = []
             self._find_open_environments(LaTeXParser().parse(editor.content_at_left_of_cursor, None, MockIssueHandler()))
@@ -140,9 +123,9 @@ class LaTeXCloseEnvironmentAction(LaTeXIconAction):
             if len(self._stack) > 0:
                 editor.insert("\\end{%s}" % self._stack[-1])
             else:
-                self._log.debug("No environment to close")
+                LOG.debug("No environment to close")
         except ValueError:
-            self._log.debug("Environments are malformed")
+            LOG.info("Environments are malformed")
 
     def _find_open_environments(self, parent_node):
         for node in parent_node:
@@ -171,26 +154,20 @@ class LaTeXCloseEnvironmentAction(LaTeXIconAction):
 
 
 class LaTeXUseBibliographyAction(LaTeXIconAction):
-    _log = getLogger("LaTeXUseBibliographyAction")
-
     label = "Use Bibliography..."
     accelerator = None
     tooltip = "Use Bibliography"
+    icon_name = "bib"
 
-    _dialog = None
-
-    @property
-    def icon(self):
-        return File(Resources().get_icon("bib.png"))
+    dialog = None
 
     def activate(self, context):
-        if not self._dialog:
-            self._dialog = UseBibliographyDialog()
+        if not self.dialog:
+            self.dialog = UseBibliographyDialog()
 
-        source = self._dialog.run_dialog(context.active_editor.edited_file)
+        source = self.dialog.run_dialog(context.active_editor.edited_file)
         if source:
             editor = context.active_editor
-
             assert type(editor) is LaTeXEditor
 
             editor.insert_at_position(source + "\n\n", LaTeXEditor.POSITION_BIBLIOGRAPHY)
@@ -202,10 +179,7 @@ class LaTeXFontFamilyAction(LaTeXIconAction):
     label = "Font Family"
     accelerator = None
     tooltip = "Font Family"
-
-    @property
-    def icon(self):
-        return File(Resources().get_icon("bf.png"))
+    icon_name = "bf"
 
     def activate(self, context):
         pass
@@ -326,10 +300,7 @@ class LaTeXStructureAction(LaTeXIconAction):
     label = "Structure"
     accelerator = None
     tooltip = "Structure"
-
-    @property
-    def icon(self):
-        return File(Resources().get_icon("section.png"))
+    icon_name = "section"
 
     def activate(self, context):
         pass
@@ -391,13 +362,10 @@ class LaTeXGraphicsAction(LaTeXIconAction):
     label = "Insert Graphics"
     accelerator = None
     tooltip = "Insert Graphics"
+    icon_name = "graphics"
 
     dialog = None
 
-    @property
-    def icon(self):
-        return File(Resources().get_icon("graphics.png"))
-
     def activate(self, context):
         if not self.dialog:
             self.dialog = InsertGraphicsDialog()
@@ -410,13 +378,10 @@ class LaTeXTableAction(LaTeXIconAction):
     label = "Insert Table or Matrix"
     accelerator = None
     tooltip = "Insert Table or Matrix"
+    icon_name = "table"
 
     dialog = None
 
-    @property
-    def icon(self):
-        return File(Resources().get_icon("table.png"))
-
     def activate(self, context):
         if not self.dialog:
             self.dialog = InsertTableDialog()
@@ -429,13 +394,10 @@ class LaTeXListingAction(LaTeXIconAction):
     label = "Insert Source Code Listing"
     accelerator = None
     tooltip = "Insert Source Code Listing"
+    icon_name = "listing"
 
     dialog = None
 
-    @property
-    def icon(self):
-        return File(Resources().get_icon("listing.png"))
-
     def activate(self, context):
         if not self.dialog:
             self.dialog = InsertListingDialog()
@@ -468,13 +430,10 @@ class LaTeXBuildImageAction(LaTeXIconAction):
     label = "Build Image"
     accelerator = None
     tooltip = "Build an image from the LaTeX document"
+    icon_name = "build-image"
 
     dialog = None
 
-    @property
-    def icon(self):
-        return File(Resources().get_icon("build-image.png"))
-
     def activate(self, context):
         if not self.dialog:
             self.dialog = BuildImageDialog()
@@ -579,12 +538,4 @@ class LaTeXSaveAsTemplateAction(LaTeXAction):
         fo.write(content)
         fo.close
 
-
-
-
-
-
-
-
-
 # ex:ts=4:et:
diff --git a/latex/util.py b/latex/util.py
index 5fed99e..d6b572f 100644
--- a/latex/util.py
+++ b/latex/util.py
@@ -201,54 +201,4 @@ class GladeInterface(object):
         """
         self.__get_tree().connect_signals(mapping)
 
-
-from uuid import uuid1
-from gi.repository import Gtk, Gdk
-
-from base.action import Action
-
-
-class IconAction(Action):
-    """
-    A utility class for creating actions with a custom icon instead of
-    a gtk stock id.
-
-    The subclass must provide a field 'icon'.
-    """
-
-    __stock_id = None
-
-    def __init__(self, *args, **kwargs):
-        self.__icon_factory = kwargs["icon_factory"]
-
-    @property
-    def icon(self):
-        """
-        Return a File object for the icon to use
-        """
-        raise NotImplementedError
-
-    def __init_stock_id(self):
-        #
-        # generate a new stock id
-        #
-        self.__stock_id = str(uuid1())
-        self.__icon_factory.add(
-                self.__stock_id,
-                Gtk.IconSet.new_from_pixbuf(
-                    GdkPixbuf.Pixbuf.new_from_file(self.icon.path)))
-
-    @property
-    def stock_id(self):
-        if self.icon:
-            if not self.__stock_id:
-                self.__init_stock_id()
-            return self.__stock_id
-        else:
-            return None
-
-
-
-
-
 # ex:ts=4:et:



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