[eog-plugins] [pythonconsole] Port to new plugin API



commit 700cd484af09cd0a6960b84f296a01ff1337692d
Author: Felix Riemann <friemann gnome org>
Date:   Fri Mar 18 17:08:46 2011 +0100

    [pythonconsole] Port to new plugin API

 configure.ac                                       |    2 +-
 plugins/pythonconsole/Makefile.am                  |    6 +-
 plugins/pythonconsole/console.py                   |   56 +++++++++----------
 ....desktop.in => pythonconsole.plugin.desktop.in} |    2 +-
 plugins/pythonconsole/pythonconsole.py             |   49 ++++++++---------
 5 files changed, 54 insertions(+), 61 deletions(-)
---
diff --git a/configure.ac b/configure.ac
index 1b346fc..9e42be5 100644
--- a/configure.ac
+++ b/configure.ac
@@ -213,7 +213,7 @@ then
 fi
 
 # Disable plugins that are not yet ported to eog-3.0
-UNPORTED_PLUGINS="postasa pythonconsole"
+UNPORTED_PLUGINS="postasa"
 for pl in $UNPORTED_PLUGINS
 do
 	undef_plugin "$pl" "not yet ported to 3.0"
diff --git a/plugins/pythonconsole/Makefile.am b/plugins/pythonconsole/Makefile.am
index b163b06..1a8943e 100644
--- a/plugins/pythonconsole/Makefile.am
+++ b/plugins/pythonconsole/Makefile.am
@@ -1,14 +1,14 @@
 # Python Console plugin
 plugindir = $(libdir)/eog/plugins
-plugin_in_files = pythonconsole.eog-plugin.desktop.in
+plugin_in_files = pythonconsole.plugin.desktop.in
 
 plugin_PYTHON = \
 	pythonconsole.py \
 	console.py
 
-%.eog-plugin: %.eog-plugin.desktop.in $(INTLTOOL_MERGE) $(wildcard $(top_srcdir)/po/*po) ; $(AM_V_GEN)$(INTLTOOL_MERGE) $(top_srcdir)/po $< $@ -d -u -c $(top_builddir)/po/.intltool-merge-cache
+%.plugin: %.plugin.desktop.in $(INTLTOOL_MERGE) $(wildcard $(top_srcdir)/po/*po) ; $(AM_V_GEN)$(INTLTOOL_MERGE) $(top_srcdir)/po $< $@ -d -u -c $(top_builddir)/po/.intltool-merge-cache
 
-plugin_DATA = $(plugin_in_files:.eog-plugin.desktop.in=.eog-plugin)
+plugin_DATA = $(plugin_in_files:.plugin.desktop.in=.plugin)
 
 EXTRA_DIST = $(plugin_in_files)
 
diff --git a/plugins/pythonconsole/console.py b/plugins/pythonconsole/console.py
index 467e1ab..aae35fb 100644
--- a/plugins/pythonconsole/console.py
+++ b/plugins/pythonconsole/console.py
@@ -28,22 +28,20 @@ import string
 import sys
 import re
 import traceback
-import gobject
-import gtk
-import pango
+from gi.repository import GObject, Gdk, Gtk, Pango
 
 __all__ = ('PythonConsole', 'OutFile')
 
-class PythonConsole(gtk.ScrolledWindow):
+class PythonConsole(Gtk.ScrolledWindow):
 	def __init__(self, namespace = {}):
-		gtk.ScrolledWindow.__init__(self)
+		Gtk.ScrolledWindow.__init__(self)
 
-		self.set_policy(gtk.POLICY_NEVER, gtk.POLICY_AUTOMATIC);
-		self.set_shadow_type(gtk.SHADOW_IN)
-		self.view = gtk.TextView()
-		self.view.modify_font(pango.FontDescription('Monospace'))
+		self.set_policy(Gtk.PolicyType.NEVER, Gtk.PolicyType.AUTOMATIC);
+		self.set_shadow_type(Gtk.ShadowType.IN)
+		self.view = Gtk.TextView()
+		self.view.modify_font(Pango.FontDescription('Monospace'))
 		self.view.set_editable(True)
-		self.view.set_wrap_mode(gtk.WRAP_WORD_CHAR)
+		self.view.set_wrap_mode(Gtk.WrapMode.WORD_CHAR)
 		self.add(self.view)
 		self.view.show()
 
@@ -80,16 +78,16 @@ class PythonConsole(gtk.ScrolledWindow):
 		
  		
 	def __key_press_event_cb(self, view, event):
-		if event.keyval == gtk.keysyms.d and event.state == gtk.gdk.CONTROL_MASK:
+		if event.keyval == Gdk.KEY_d and event.state == Gdk.ModifierType.CONTROL_MASK:
 			self.destroy()
 		
-		elif event.keyval == gtk.keysyms.Return and event.state == gtk.gdk.CONTROL_MASK:
+		elif event.keyval == Gdk.KEY_Return and event.state == Gdk.ModifierType.CONTROL_MASK:
 			# Get the command
 			buffer = view.get_buffer()
 			inp_mark = buffer.get_mark("input")
 			inp = buffer.get_iter_at_mark(inp_mark)
 			cur = buffer.get_end_iter()
-			line = buffer.get_text(inp, cur)
+			line = buffer.get_text(inp, cur, False)
 			self.current_command = self.current_command + line + "\n"
 			self.history_add(line)
 
@@ -106,10 +104,10 @@ class PythonConsole(gtk.ScrolledWindow):
 				cur = buffer.get_end_iter()
 				
 			buffer.place_cursor(cur)
-			gobject.idle_add(self.scroll_to_end)
+			GObject.idle_add(self.scroll_to_end)
 			return True
 		
-		elif event.keyval == gtk.keysyms.Return:
+		elif event.keyval == Gdk.KEY_Return:
 			# Get the marks
 			buffer = view.get_buffer()
 			lin_mark = buffer.get_mark("input-line")
@@ -118,7 +116,7 @@ class PythonConsole(gtk.ScrolledWindow):
 			# Get the command line
 			inp = buffer.get_iter_at_mark(inp_mark)
 			cur = buffer.get_end_iter()
-			line = buffer.get_text(inp, cur)
+			line = buffer.get_text(inp, cur, False)
 			self.current_command = self.current_command + line + "\n"
 			self.history_add(line)
 
@@ -150,35 +148,35 @@ class PythonConsole(gtk.ScrolledWindow):
 			cur = buffer.get_end_iter()
 			buffer.move_mark(inp_mark, cur)
 			buffer.place_cursor(cur)
-			gobject.idle_add(self.scroll_to_end)
+			GObject.idle_add(self.scroll_to_end)
 			return True
 
-		elif event.keyval == gtk.keysyms.KP_Down or event.keyval == gtk.keysyms.Down:
+		elif event.keyval == Gdk.KEY_KP_Down or event.keyval == Gdk.KEY_Down:
 			# Next entry from history
 			view.emit_stop_by_name("key_press_event")
 			self.history_down()
-			gobject.idle_add(self.scroll_to_end)
+			GObject.idle_add(self.scroll_to_end)
 			return True
 
-		elif event.keyval == gtk.keysyms.KP_Up or event.keyval == gtk.keysyms.Up:
+		elif event.keyval == Gdk.KEY_KP_Up or event.keyval == Gdk.KEY_Up:
 			# Previous entry from history
 			view.emit_stop_by_name("key_press_event")
 			self.history_up()
-			gobject.idle_add(self.scroll_to_end)
+			GObject.idle_add(self.scroll_to_end)
 			return True
 
-		elif event.keyval == gtk.keysyms.KP_Left or event.keyval == gtk.keysyms.Left or \
-		     event.keyval == gtk.keysyms.BackSpace:
+		elif event.keyval == Gdk.KEY_KP_Left or event.keyval == Gdk.KEY_Left or \
+		     event.keyval == Gdk.KEY_BackSpace:
 			buffer = view.get_buffer()
 			inp = buffer.get_iter_at_mark(buffer.get_mark("input"))
 			cur = buffer.get_iter_at_mark(buffer.get_insert())
 			return inp.compare(cur) == 0
 
-		elif event.keyval == gtk.keysyms.Home:
+		elif event.keyval == Gdk.KEY_Home:
 			# Go to the begin of the command instead of the begin of the line
 			buffer = view.get_buffer()
 			inp = buffer.get_iter_at_mark(buffer.get_mark("input"))
-			if event.state == gtk.gdk.SHIFT_MASK:
+			if event.state == Gdk.ModifierType.SHIFT_MASK:
 				buffer.move_mark_by_name("insert", inp)
 			else:
 				buffer.place_cursor(inp)
@@ -193,7 +191,7 @@ class PythonConsole(gtk.ScrolledWindow):
 		buffer = self.view.get_buffer()
 		inp = buffer.get_iter_at_mark(buffer.get_mark("input"))
 		cur = buffer.get_end_iter()
-		return buffer.get_text(inp, cur)
+		return buffer.get_text(inp, cur, False)
 	
 	def set_command_line(self, command):
 		buffer = self.view.get_buffer()
@@ -225,7 +223,7 @@ class PythonConsole(gtk.ScrolledWindow):
 	
 	def scroll_to_end(self):
 		iter = self.view.get_buffer().get_end_iter()
-		self.view.scroll_to_iter(iter, 0.0)
+		self.view.scroll_to_iter(iter, 0.0, False, 0.5, 0.5)
 		return False
 
 	def write(self, text, tag = None):
@@ -234,7 +232,7 @@ class PythonConsole(gtk.ScrolledWindow):
 			buffer.insert(buffer.get_end_iter(), text)
 		else:
 			buffer.insert_with_tags(buffer.get_end_iter(), text, tag)
-		gobject.idle_add(self.scroll_to_end)
+		GObject.idle_add(self.scroll_to_end)
  	
  	def eval(self, command, display_command = False):
 		buffer = self.view.get_buffer()
@@ -257,7 +255,7 @@ class PythonConsole(gtk.ScrolledWindow):
 		buffer.insert(cur, ">>> ")
 		cur = buffer.get_end_iter()
 		buffer.move_mark_by_name("input", cur)
-		self.view.scroll_to_iter(buffer.get_end_iter(), 0.0)
+		self.view.scroll_to_iter(buffer.get_end_iter(), 0.0, False, 0.5, 0.5)
 	
  	def __run(self, command):
 		sys.stdout, self.stdout = self.stdout, sys.stdout
diff --git a/plugins/pythonconsole/pythonconsole.eog-plugin.desktop.in b/plugins/pythonconsole/pythonconsole.plugin.desktop.in
similarity index 95%
rename from plugins/pythonconsole/pythonconsole.eog-plugin.desktop.in
rename to plugins/pythonconsole/pythonconsole.plugin.desktop.in
index a9018df..e4e486c 100644
--- a/plugins/pythonconsole/pythonconsole.eog-plugin.desktop.in
+++ b/plugins/pythonconsole/pythonconsole.plugin.desktop.in
@@ -1,4 +1,4 @@
-[Eog Plugin]
+[Plugin]
 Loader=python
 Module=pythonconsole
 IAge=2
diff --git a/plugins/pythonconsole/pythonconsole.py b/plugins/pythonconsole/pythonconsole.py
index 4f9dae3..ca9a878 100644
--- a/plugins/pythonconsole/pythonconsole.py
+++ b/plugins/pythonconsole/pythonconsole.py
@@ -24,8 +24,7 @@
 #     Copyright (C), 2005 Adam Hooper <adamh densi com>
 #     Copyrignt (C), 2005 Raphaël Slinckx
 
-import gtk
-import eog
+from gi.repository import GObject, Gtk, Eog
 from console import PythonConsole
 
 ui_str = """
@@ -40,37 +39,39 @@ ui_str = """
 	</ui>
 	"""
 
-class PythonConsolePlugin(eog.Plugin):
+class PythonConsolePlugin(GObject.Object, Eog.WindowActivatable):
+
+	# Override EogWindowActivatable's window property
+	window = GObject.property(type=Eog.Window)
+	action_group = None
 
 	def __init__(self):
-		eog.Plugin.__init__(self)
+		GObject.Object.__init__(self)
 		self.console_window = None
+		ui_id = 0
 			
-	def activate(self, window):
-		data = dict()
-		ui_manager = window.get_ui_manager()
-		data['group'] = gtk.ActionGroup('PythonConsole')
-		data['group'].add_actions([('PythonConsole', None, 'P_ython Console', None, None, self.console_cb)], window)
-		ui_manager.insert_action_group(data['group'], 0)
-		data['ui_id'] = ui_manager.add_ui_from_string(ui_str)
-		window.set_data('PythonConsolePluginInfo', data)
-		window.connect('delete-event', self.self_deactivate)
+	def do_activate(self):
+		ui_manager = self.window.get_ui_manager()
+		self.action_group = Gtk.ActionGroup('PythonConsole')
+		self.action_group.add_actions([('PythonConsole', None, 'P_ython Console', None, None, self.console_cb)], self.window)
+		ui_manager.insert_action_group(self.action_group, 0)
+		self.ui_id = ui_manager.add_ui_from_string(ui_str)
 	
-	def deactivate(self, window):
-		data = window.get_data('PythonConsolePluginInfo')
-		ui_manager = window.get_ui_manager()
-		ui_manager.remove_ui(data['ui_id'])
-		ui_manager.remove_action_group(data['group'])
+	def do_deactivate(self):
+		ui_manager = self.window.get_ui_manager()
+		ui_manager.remove_ui(self.ui_id)
+		self.ui_id = 0
+		ui_manager.remove_action_group(self.action_group)
+		self.action_group = None
 		ui_manager.ensure_update()
-		window.set_data("PythonConsolePluginInfo", None)
 		if self.console_window is not None:
 			self.console_window.destroy()
 		
 	def console_cb(self, action, window):
 		if not self.console_window:
-			self.console_window = gtk.Window()
+			self.console_window = Gtk.Window()
 			console = PythonConsole(namespace = {'__builtins__' : __builtins__,
-		    	                                 'eog' : eog,
+							     'eog' : Eog,
 		        	                             'window' : window})
 			console.set_size_request(600, 400)
 			console.eval('print "You can access the main window through ' \
@@ -91,10 +92,4 @@ class PythonConsolePlugin(eog.Plugin):
 		window.destroy()
 		self.console_window = None
 
-# This is a workaround for a ref-counting problem with python plugins.
-# This deactivates the plugin once the window that was used to activate
-# it is closed. Breaks multi-window-usability though.
-# See bug #460781 for more information.
-	def self_deactivate(self, window, event):
-		self.deactivate(window)
 



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