← Back to team overview

zim-wiki team mailing list archive

extension of screenshot plugin

 

Hi everybody,
I would like to share a small contribution in shape of a small plugin
accelerating an insertion of images from clipboard. The plugin is based on
template made by Jaap Karssenberg (mainly utilizing a code of
screenshot.py), it also integrates functionality described by SMU on zim-wiki
http://www.zim-wiki.org/wiki/doku.php?id=insert_image_from_clipboard<zim-wiki%20http://www.zim-wiki.org/wiki/doku.php?id=insert_image_from_clipboard>
.

Basically it inserts an image from clipboard to a current position of cursor
(by a shortcut defined to be an "Insert" key). There are also two properties
initially defined to:
Image name = "image_"
Date format = "%Y-%m-%d-%H%M%S")

There are probably better ways how to integrate this basic function (ideally
through a clipboard directly, but since this feature is not present in a
current version and it requires internal changes in the program, I have
decided to create this plugin as the easiest solution, which results to the
same outcome). Thanks to all contributors for improvement of the code and
hopefully, this will make ZIM even more efficient, than it is now.

The code is included in attachments.

NorfCran over ;-)
# -*- coding: utf-8 -*-

# Copyright 2011 NorfCran <norfcran@xxxxxxxxx>

import gtk
import time

from zim.plugins import PluginClass
from zim.gui.widgets import ui_environment

ui_xml = '''
<ui>
	<menubar name='menubar'>
		<menu action='insert_menu'>
			<placeholder name='plugin_items'>
				<menuitem action='insert_clipboard'/>
			</placeholder>
		</menu>
	</menubar>
</ui>
'''

# in oder to provide dynamic key binding assignment the initiation is made in the plugin class
ui_actions = (
	# name, stock id, label, accelerator, tooltip, read only
	('insert_clipboard', None, _('_Image from Clipboard...'), 'Insert', '', False),
		# T: menu item for insert clipboard plugin
)


class InsertClipboardPlugin(PluginClass):
	'''FIXME'''

	plugin_info = {
		'name': _('Insert Image from Clipboard'), # T: plugin name
		'description': _('''\
This plugin  allows an insertion of image from clipboard (directly to a zim page).

The plugin is based on Insert Screenshot made by Jaap Karssenberg and 
inspired by SMU http://www.zim-wiki.org/wiki/doku.php?id=insert_image_from_clipboard.

Default configurable plugin values:
Image name: image_
Date format: %Y-%m-%d-%H%M%S
'''), # T: plugin description
		'author': 'NorfCran',
		'help': 'Plugins:Insert Image from Clipboard',
	}

	plugin_preferences = (
		('image_name', 'string', 'Image name', 'image_'),
		('date_format', 'string', 'Date format', '%Y-%m-%d-%H%M%S'),
		('shortcut', 'string', 'Key Bindings', 'Insert'),
	)

	def __init__(self, ui):
		PluginClass.__init__(self, ui)

	def initialize_ui(self, ui):
#		shortcut = self.preferences['shortcut'] or None
#		if shortcut == None: shortcut = 'Insert'
#		
#		ui_actions = (
#			 name, stock id, label, accelerator, tooltip, read only
#			('insert_clipboard', None, _('_Image from Clipboard...'), shortcut, '', False),
#				 T: menu item for insert clipboard plugin
#		)
#				
		if self.ui.ui_type == 'gtk':
			self.ui.add_actions(ui_actions, self)
			self.ui.add_ui(ui_xml, self)

	def insert_clipboard(self):
		clipboard = gtk.clipboard_get()
		image = clipboard.wait_for_image()
		if (image is not None):
			# possibility to change format of stored image
			image_name = self.preferences['image_name'] or None
			date_format = self.preferences['date_format'] or None
			
			if image_name == None: image_name = 'image_'
			if date_format == None: date_format = '%Y-%m-%d-%H%M%S'
			format = image_name+date_format+".png"
			name = time.strftime(format)
			
			page = self.ui.page
			dir = self.ui.notebook.get_attachments_dir(page)
			dir.touch()
			file = dir.new_file(name)
			image.save(str(file), 'png')
			self.ui.mainwindow.pageview.insert_image(file, interactive=False)