/usr/share/pyshared/labyrinth_lib/ImageThought.py is in labyrinth 0.6-0ubuntu3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 | # ImageThought.py
# This file is part of labyrinth
#
# Copyright (C) 2006 - Don Scorgie <Don@Scorgie.org>
#
# labyrinth is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# labyrinth is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with labyrinth; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor,
# Boston, MA 02110-1301 USA
#
# Standard library
import os.path
import xml.dom
import gettext
_ = gettext.gettext
# Gtk stuff
import gtk
import cairo
# Local imports
import BaseThought
import utils
import UndoManager
MODE_EDITING = 0
MODE_IMAGE = 1
MODE_DRAW = 2
UNDO_RESIZE = 0
class ImageThought (BaseThought.ResizableThought):
def __init__ (self, coords, pango_context, thought_number, save, undo, loading, background_color):
super (ImageThought, self).__init__(save, "image_thought", undo, background_color, None)
self.identity = thought_number
margin = utils.margin_required (utils.STYLE_NORMAL)
self.want_move = False
if coords:
self.ul = (coords[0]-margin[0], coords[1] - margin[1])
self.pic_location = coords
else:
self.ul = None
self.button_press = False
if not loading:
self.all_okay = self.open_image ()
else:
self.all_okay = True
def open_image (self, filename = None):
# Present a dialog for the user to choose an image here
if not filename:
fil = gtk.FileFilter ()
fil.set_name("Images")
fil.add_pixbuf_formats ()
dialog = gtk.FileChooserDialog (_("Choose image to insert"), None, gtk.FILE_CHOOSER_ACTION_OPEN, \
(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, gtk.STOCK_OPEN, gtk.RESPONSE_OK))
dialog.add_filter (fil)
res = dialog.run ()
dialog.hide ()
if res != gtk.RESPONSE_OK:
return False
else:
fname = dialog.get_filename()
else:
fname = filename
try:
self.orig_pic = gtk.gdk.pixbuf_new_from_file (fname)
except:
try:
# lets see if file was imported and is already extracted
fname = os.path.join (utils.get_images_dir (), + os.path.basename(filename))
self.orig_pic = gtk.gdk.pixbuf_new_from_file (fname)
except:
return False
self.filename = fname
if not filename:
self.width = self.orig_pic.get_width ()
self.height = self.orig_pic.get_height ()
margin = utils.margin_required (utils.STYLE_NORMAL)
self.lr = (self.pic_location[0]+self.width+margin[2], self.pic_location[1]+self.height+margin[3])
self.pic = self.orig_pic
self.text = fname[fname.rfind('/')+1:fname.rfind('.')]
return True
def draw (self, context):
if len (self.extended_buffer.get_text()) == 0:
utils.draw_thought_outline (context, self.ul, self.lr, self.background_color, self.am_selected, self.am_primary, utils.STYLE_NORMAL)
else:
utils.draw_thought_outline (context, self.ul, self.lr, self.background_color, self.am_selected, self.am_primary, utils.STYLE_EXTENDED_CONTENT)
if self.pic:
context.set_source_pixbuf (self.pic, self.pic_location[0], self.pic_location[1])
context.rectangle (self.pic_location[0], self.pic_location[1], self.width, self.height)
context.fill ()
context.set_source_rgb (0,0,0)
def export (self, context, move_x, move_y):
utils.export_thought_outline (context, self.ul, self.lr, self.background_color, self.am_selected, self.am_primary, utils.STYLE_NORMAL,
(move_x, move_y))
if self.pic:
if hasattr(context, "set_source_pixbuf"):
context.set_source_pixbuf (self.pic, self.pic_location[0]+move_x, self.pic_location[1]+move_y)
elif hasattr(context, "set_source_surface"):
pixel_array = utils.pixbuf_to_cairo (self.pic.get_pixels_array())
image_surface = cairo.ImageSurface.create_for_data(pixel_array, cairo.FORMAT_ARGB32, self.width, self.height, -1)
context.set_source_surface (image_surface, self.pic_location[0]+move_x, self.pic_location[1]+move_y)
context.rectangle (self.pic_location[0]+move_x, self.pic_location[1]+move_y, self.width, self.height)
context.fill ()
context.set_source_rgb (0,0,0)
def want_motion (self):
return self.want_move
def recalc_edges (self):
margin = utils.margin_required (utils.STYLE_NORMAL)
self.pic_location = (self.ul[0]+margin[0], self.ul[1]+margin[1])
self.lr = (self.pic_location[0]+self.width+margin[2], self.pic_location[1]+self.height+margin[3])
def recalc_position (self):
self.recalc_edges ()
def undo_resize (self, action, mode):
self.undo.block ()
if mode == UndoManager.UNDO:
choose = 0
else:
choose = 1
self.ul = action.args[choose][0]
self.width = action.args[choose][1]
self.height = action.args[choose][2]
self.pic = self.orig_pic.scale_simple (int(self.width), int(self.height), gtk.gdk.INTERP_HYPER)
self.recalc_edges ()
self.emit ("update_links")
self.emit ("update_view")
self.undo.unblock ()
def process_button_down (self, event, mode, transformed):
modifiers = gtk.accelerator_get_default_mod_mask ()
self.button_down = True
if event.button == 1:
if event.type == gtk.gdk.BUTTON_PRESS:
self.emit ("select_thought", event.state & modifiers)
self.emit ("update_view")
if mode == MODE_EDITING and self.resizing != self.RESIZE_NONE:
self.orig_size = (self.ul, self.width, self.height)
self.want_move = True
return True
elif event.button == 3:
self.emit ("popup_requested", event, 1)
self.emit ("update_view")
def process_button_release (self, event, unending_link, mode, transformed):
self.button_down = False
if unending_link:
unending_link.set_child (self)
self.emit ("claim_unending_link")
if self.orig_pic:
self.pic = self.orig_pic.scale_simple (int(self.width), int(self.height), gtk.gdk.INTERP_HYPER)
self.emit ("update_view")
if self.want_move:
self.undo.add_undo (UndoManager.UndoAction (self, UNDO_RESIZE, self.undo_resize, \
self.orig_size, (self.ul, self.width, self.height)))
self.want_move = False
def handle_motion (self, event, mode, transformed):
if self.resizing == self.RESIZE_NONE or not self.want_move or not event.state & gtk.gdk.BUTTON1_MASK:
if not event.state & gtk.gdk.BUTTON1_MASK:
return False
elif mode == MODE_EDITING:
self.emit ("create_link", \
(self.ul[0]-((self.ul[0]-self.lr[0]) / 2.), self.ul[1]-((self.ul[1]-self.lr[1]) / 2.)))
return True
diffx = transformed[0] - self.motion_coords[0]
diffy = transformed[1] - self.motion_coords[1]
tmp = self.motion_coords
self.motion_coords = transformed
if self.resizing == self.RESIZE_LEFT:
if self.width - diffx < 10:
self.motion_coords = tmp
return True
self.ul = (self.ul[0]+diffx, self.ul[1])
self.pic_location = (self.pic_location[0]+diffx, self.pic_location[1])
self.width -= diffx
elif self.resizing == self.RESIZE_RIGHT:
if self.width + diffx < 10:
self.motion_coords = tmp
return True
self.lr = (self.lr[0]+diffx, self.lr[1])
self.width += diffx
elif self.resizing == self.RESIZE_TOP:
if self.height - diffy < 10:
self.motion_coords = tmp
return True
self.ul = (self.ul[0], self.ul[1]+diffy)
self.pic_location = (self.pic_location[0], self.pic_location[1]+diffy)
self.height -= diffy
elif self.resizing == self.RESIZE_BOTTOM:
if self.height + diffy < 10:
self.motion_coords = tmp
return True
self.lr = (self.lr[0], self.lr[1]+diffy)
self.height += diffy
elif self.resizing == self.RESIZE_UL:
if self.height - diffy < 10 or self.width - diffx < 10:
self.motion_coords = tmp
return True
self.ul = (self.ul[0]+diffx, self.ul[1]+diffy)
self.pic_location = (self.pic_location[0]+diffx, self.pic_location[1]+diffy)
self.width -= diffx
self.height -= diffy
elif self.resizing == self.RESIZE_UR:
if self.height - diffy < 10 or self.width + diffx < 10:
self.motion_coords = tmp
return True
self.ul = (self.ul[0], self.ul[1]+diffy)
self.lr = (self.lr[0]+diffx, self.lr[1])
self.pic_location = (self.pic_location[0], self.pic_location[1]+diffy)
self.width += diffx
self.height -= diffy
elif self.resizing == self.RESIZE_LL:
if self.height + diffy < 10 or self.width - diffx < 10:
self.motion_coords = tmp
return True
self.ul = (self.ul[0]+diffx, self.ul[1])
self.lr = (self.lr[0], self.lr[1]+diffy)
self.pic_location = (self.pic_location[0]+diffx, self.pic_location[1])
self.width -= diffx
self.height += diffy
elif self.resizing == self.RESIZE_LR:
if self.height + diffy < 10:
self.motion_coords = tmp
return True
if self.width + diffx < 10:
self.motion_coords = tmp
return True
self.lr = (self.lr[0]+diffx, self.lr[1]+diffy)
self.width += diffx
self.height += diffy
if self.orig_pic:
self.pic = self.orig_pic.scale_simple (int(self.width), int(self.height), gtk.gdk.INTERP_NEAREST)
self.emit ("update_links")
self.emit ("update_view")
return True
def update_save (self):
text = self.extended_buffer.get_text ()
if text:
self.extended_buffer.update_save()
else:
try:
self.element.removeChild(self.extended_buffer.element)
except xml.dom.NotFoundErr:
pass
self.element.setAttribute ("ul-coords", str(self.ul))
self.element.setAttribute ("lr-coords", str(self.lr))
self.element.setAttribute ("identity", str(self.identity))
self.element.setAttribute ("background-color", self.background_color.to_string())
self.element.setAttribute ("file", str(self.filename))
self.element.setAttribute ("image_width", str(self.width))
self.element.setAttribute ("image_height", str(self.height))
if self.am_selected:
self.element.setAttribute ("current_root", "true")
else:
try:
self.element.removeAttribute ("current_root")
except xml.dom.NotFoundErr:
pass
if self.am_primary:
self.element.setAttribute ("primary_root", "true")
else:
try:
self.element.removeAttribute ("primary_root")
except xml.dom.NotFoundErr:
pass
return
def load (self, node):
tmp = node.getAttribute ("ul-coords")
self.ul = utils.parse_coords (tmp)
tmp = node.getAttribute ("lr-coords")
self.lr = utils.parse_coords (tmp)
self.filename = node.getAttribute ("file")
self.identity = int (node.getAttribute ("identity"))
try:
tmp = node.getAttribute ("background-color")
self.background_color = gtk.gdk.color_parse(tmp)
except ValueError:
pass
self.width = float(node.getAttribute ("image_width"))
self.height = float(node.getAttribute ("image_height"))
self.am_selected = node.hasAttribute ("current_root")
self.am_primary = node.hasAttribute ("primary_root")
for n in node.childNodes:
if n.nodeName == "Extended":
self.extended_buffer.load(n)
else:
print "Unknown: "+n.nodeName
margin = utils.margin_required (utils.STYLE_NORMAL)
self.pic_location = (self.ul[0]+margin[0], self.ul[1]+margin[1])
self.okay = self.open_image (self.filename)
self.lr = (self.pic_location[0]+self.width+margin[2], self.pic_location[1]+self.height+margin[3])
if not self.okay:
dialog = gtk.MessageDialog (None, gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT,
gtk.MESSAGE_WARNING, gtk.BUTTONS_CLOSE,
_("Error loading file"))
dialog.format_secondary_text (_("%s could not be found. Associated thought will be empty."%self.filename))
dialog.run ()
dialog.hide ()
self.pic = None
self.orig_pic = None
else:
self.pic = self.orig_pic.scale_simple (int(self.width), int(self.height), gtk.gdk.INTERP_HYPER)
return
def change_image_cb(self, widget):
self.open_image()
def get_popup_menu_items(self):
image = gtk.Image()
image.set_from_stock(gtk.STOCK_OPEN, gtk.ICON_SIZE_MENU)
item = gtk.ImageMenuItem(_('Change Image'))
item.set_image(image)
item.connect('activate', self.change_image_cb)
return [item]
|