-
Notifications
You must be signed in to change notification settings - Fork 0
/
imageview.py
executable file
·448 lines (356 loc) · 15.7 KB
/
imageview.py
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
#!/usr/bin/env python
# vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4
import copy
from gi.repository import Gdk, GdkPixbuf, GObject, Gtk, GtkSource, Pango
from ielementblock import ElementBlockInterface
from imagemodel import ImageModel
from imagepropwin import ImagePropWin
class ImageView(Gtk.Layout, ElementBlockInterface):
__gtype_name__ = 'ImageView'
__gsignals__ = copy.copy(ElementBlockInterface.__gsignals__)
def __init__(self, **args):
Gtk.Layout.__init__(self, **args)
self.set_size_request(600, 50)
self.set_hexpand(False)
self.set_border_width(10) # for debug only
self.button1_active = False
#self.add_events(Gdk.EventMask.KEY_PRESS_MASK)
#self.add_events(Gdk.EventMask.BUTTON_PRESS_MASK)
#self.set_property('can-focus', True)
self.childwidget = None
self.child_newx = 0
self.child_newy = 0
self.childwidget_posx = 0
self.childwidget_posy = 0
#img = Gtk.Image.new_from_file("oshw-logo-800-px.png")
#img.connect('button-press-event', self.do_button_press_event)
#self.set_events(0)
#self.add_events(Gdk.EventMask.EXPOSURE_MASK)
self.add_events(Gdk.EventMask.BUTTON_PRESS_MASK)
self.add_events(Gdk.EventMask.BUTTON_RELEASE_MASK)
self.add_events(Gdk.EventMask.POINTER_MOTION_MASK)
#self.add_events(Gdk.EventMask.BUTTON_MOTION_MASK)
#self.add_events(Gdk.EventMask.KEY_PRESS_MASK)
#print("init: get_events = ", self.get_events())
self.set_can_default(True)
self.set_can_focus(True)
#img.set_property('can-focus', True)
#vp = Gtk.Viewport()
#vp.set_size_request(50, 50)
#vp.add(img)
#vp.show()
#self.add(vp)
#self.add(img)
self.model = None
self.show_all()
def do_draw(self, ctx):
#print('do_draw')
ctx.save()
rect = self.get_allocation()
ctx.translate(-rect.x, -rect.y) # to match Gtk absolute coord
# Draw a background
#ctx.set_source_rgb(1, 1, 1) # white
##ctx.set_source_rgb(0.5, 0.5, 0.5) # grey50
#ctx.paint()
#ctx.set_source_rgb(0, 0, 0) # black
# ctx.select_font_face("Sans", cairo.FONT_SLANT_NORMAL,
# cairo.FONT_WEIGHT_NORMAL)
# ctx.set_font_size(12)
# ctx.move_to(10, 20)
# ctx.show_text("This is hello")
# Cross for layout debugging
#ctx.save()
# if not self.childwidget:
# if not (self.model and self.model.get_image()):
# ctx.set_line_width(1)
# ctx.move_to(rect.x, rect.y)
# ctx.line_to(rect.x + rect.width, rect.y + rect.height)
# ctx.move_to(rect.x + rect.width, rect.y)
# ctx.line_to(rect.x, rect.y + rect.height)
# ctx.stroke()
#ctx.restore()
## Horizontal line to outline subdoc title
#ctx.save()
#for docid, child in self.childrens_title.items():
# if not child.get_visible():
# continue
# parent_rect = self.get_allocation()
# rect = child.get_allocation()
# ctx.set_line_width(2)
# ctx.set_source_rgb(0, 0, 0) # black
# ctx.move_to(rect.x, rect.y + rect.height)
# ctx.line_to(rect.x + parent_rect.width, rect.y + rect.height)
# ctx.stroke()
# #print (rect.x, rect.y, rect.width, rect.height)
#ctx.restore()
#ctx.set_source_rgb(0, 0, 0) # black
#ctx.save()
#ctx.move_to(10, 50)
##super(Gtk.Container, self).do_draw(self.image, ctx)
##self.image.do_draw(self.image.get_parent_window(), ctx)
##ctx.show_text("This is hello")
#self.image.draw(ctx)
#ctx.restore()
#self.queue_draw()
#ctx.show_text(self.txt_tst_buf)
ctx.restore()
Gtk.Layout.do_draw(self, ctx)
# #ctx = Gdk.cairo_create(self.get_bin_window())
# ctx.save()
# # Cross for layout debugging
# rect = self.get_allocation()
# ctx.translate(-rect.x, -rect.y) # to match Gtk absolute coord
# ctx.set_line_width(1)
# ctx.move_to(rect.x, rect.y)
# ctx.line_to(rect.x + rect.width/2, rect.y + rect.height)
# ctx.move_to(rect.x + rect.width/2, rect.y)
# ctx.line_to(rect.x, rect.y + rect.height)
# ctx.stroke()
#Gdk.Window.invalidate_rect(self.get_window(), rect, True)
# self.queue_draw()
# #Gdk.Window.invalidate_rect(self.get_bin_window(), rect, True)
# #Gdk.Window.process_updates(self.get_bin_window(), True)
# ctx.restore()
return True
def do_button_press_event(self, event):
#print(str(event.type))
#print("[x, y] = ", event.x, event.y)
if event.type == Gdk.EventType.BUTTON_PRESS:
print("button_press:")
self.button1_startx = event.x #self.childwidget.get_property('x')
self.button1_starty = event.y #self.childwidget.get_property('y')
self.button1_active = True
self.button1_action, _bis_, self.button1_dir = self._get_action_from_pointer_pos(event)
print("\taction =", self.button1_action)
self.grab_focus()
elif event.type == Gdk.EventType._2BUTTON_PRESS:
print("double click")
self.dialog_edit_image_property()
return True
def do_button_release_event(self, event):
if event.type == Gdk.EventType.BUTTON_RELEASE:
print("button_release:")
if self.button1_active and self.button1_action == 'move':
self.childwidget_posx = self.child_newx
self.childwidget_posy = self.child_newy
self.button1_active = False
def _get_action_from_pointer_pos(self, event):
'''
return a (str, cursor) tuple : ('crop', 'resize' or 'move', gdk cursor)
'''
rect = self.get_allocation()
direction = None
# FIXME: be sure 10pix is always possible.
if event.x < 10 and event.y > 10 and event.y < rect.height - 10:
#print('left handle for crop')
cursor = Gdk.Cursor(Gdk.CursorType.LEFT_TEE)
action = 'crop'
direction = 'wl' # width, left
elif event.x > rect.width - 10 and event.y > 10 and event.y < rect.height - 10:
#print('right handle for crop')
cursor = Gdk.Cursor(Gdk.CursorType.RIGHT_TEE)
action = 'crop'
direction = 'wr' # witdh, right
elif event.y < 10 and event.x > 10 and event.x < rect.width - 10:
#print('top handle for crop')
cursor = Gdk.Cursor(Gdk.CursorType.TOP_TEE)
action = 'crop'
direction = 'ht' # height, top
elif event.y > rect.height - 10 and event.x > 10 and event.x < rect.width - 10:
#print('bottom handle for crop')
cursor = Gdk.Cursor(Gdk.CursorType.BOTTOM_TEE)
action = 'crop'
direction = 'hb' # height, bottom
elif event.x < 10 and event.y < 10:
#print('corner for resize')
cursor = Gdk.Cursor(Gdk.CursorType.TOP_LEFT_CORNER)
action = 'resize'
elif event.x > rect.width - 10 and event.y < 10:
#print('corner for resize')
cursor = Gdk.Cursor(Gdk.CursorType.TOP_RIGHT_CORNER)
action = 'resize'
elif event.x < 10 and event.y > rect.height - 10:
#print('corner for resize')
cursor = Gdk.Cursor(Gdk.CursorType.BOTTOM_LEFT_CORNER)
action = 'resize'
elif event.x > rect.width - 10 and event.y > rect.height - 10:
#print('corner for resize')
cursor = Gdk.Cursor(Gdk.CursorType.BOTTOM_RIGHT_CORNER)
action = 'resize'
else:
cursor = Gdk.Cursor(Gdk.CursorType.ARROW)
action = 'move'
return (action, cursor, direction)
def do_focus_in_event(self, event):
propwin = ImagePropWin.get_instance()
propwin.set_model(self.model)
#Gtk.Widget.do_focus_in_event(self, event)
def do_motion_notify_event(self, event):
action, cursor, direction = self._get_action_from_pointer_pos(event)
if self.button1_active:
if self.button1_action == 'move':
#print("motion but1 notify [%d,%d]" % (event.x, event.y))
self.child_newx = self.childwidget_posx + (event.x - self.button1_startx)
self.child_newy = self.childwidget_posy + (event.y - self.button1_starty)
self.model.set_image_coord(self.child_newx, self.child_newy)
#self.move(self.childwidget,
# self.child_newx,
# self.child_newy)
elif self.button1_action == 'resize':
dX = int(event.x - self.button1_startx)
dY = int(event.y - self.button1_starty)
#print("resize, [dX, dY] = ", dX, dY)
rect = self.get_allocation()
pixbuf = self.img_orig.get_pixbuf()
new_pixbuf = pixbuf.scale_simple(
rect.width + dX, rect.height + dY, GdkPixbuf.InterpType.BILINEAR)
new_img = Gtk.Image.new_from_pixbuf(new_pixbuf)
new_img.show()
self.remove(self.childwidget)
self.add(new_img)
self.childwidget = new_img
#del pixbuf
self.set_size_request(rect.width + dX, rect.height + dY)
self.button1_startx = event.x
self.button1_starty = event.y
elif self.button1_action == 'crop':
rect = self.get_allocation()
dX = int(event.x - self.button1_startx)
dY = int(event.y - self.button1_starty)
#print("crop, [dX, dY] = ", dX, dY)
# w, _dont_care_ = self.img.get_preferred_width()
# h, _dont_care_ = self.img.get_preferred_height()
# # prevent crop not to be an expand:
# if (self.button1_dir == 'wl' and rect.width - dX > w) \
# or (self.button1_dir == 'wr' and rect.width + dX > w) \
# or (self.button1_dir == 'ht' and rect.height - dY > h) \
# or (self.button1_dir == 'hb' and rect.height + dY > h):
# return
# on top and left crop, we need to move the child in layout
if self.button1_dir == 'wl':
self.childwidget_posx -= dX
self.move(self.childwidget,
self.childwidget_posx, self.childwidget_posy)
elif self.button1_dir == 'ht':
self.childwidget_posy -= dY
self.move(self.childwidget,
self.childwidget_posx, self.childwidget_posy)
# Do the crop
if self.button1_dir == 'wl':
# width crop
self.set_size_request(rect.width - dX, rect.height)
self.button1_startx = event.x
elif self.button1_dir == 'wr':
self.set_size_request(rect.width + dX, rect.height)
self.button1_startx = event.x
elif self.button1_dir == 'ht':
# height crop
self.set_size_request(rect.width, rect.height - dY)
self.button1_starty = event.y
elif self.button1_dir == 'hb':
self.set_size_request(rect.width, rect.height + dY)
self.button1_starty = event.y
else:
# do_motion_notify_event() while button1 inactive.
pass
#rect = self.get_allocation()
#if event.x < 10 and event.y > 10 and event.y < rect.height - 10:
# #print('left handle for crop')
# rect = self.get_allocation()
# #Gdk.Window.process_all_updates()#self.get_window(), True)
# #ctx = Gdk.cairo_create(self.get_bin_window())
# ctx = Gdk.cairo_create(self.get_window())
# Gdk.Window.invalidate_rect(self.get_window(), rect, True)
# Gdk.Window.process_all_updates()#self.get_window(), True)
# ctx.save()
# # Cross for layout debugging
# Gdk.Window.invalidate_rect(self.get_window(), rect, True)
# ctx.translate(-rect.x, -rect.y) # to match Gtk absolute coord
# ctx.set_line_width(1)
# ctx.move_to(rect.x, rect.y)
# ctx.line_to(rect.x + rect.width/2, rect.y + rect.height)
# ctx.move_to(rect.x + rect.width/2, rect.y)
# ctx.line_to(rect.x, rect.y + rect.height)
# ctx.stroke()
gdk_window = self.get_window()
gdk_window.set_cursor(cursor)
def do_key_press_event(self, event):
key = Gdk.keyval_name(event.keyval)
# debug string :
print("keypress: get_events = ", self.get_events())
print(event.keyval)
print(key + str(event.state), event.state)
if key =='BackSpace':
# let the subview handle the destruction...
return False
return True
# ElementBlockInterface implementation
def get_content_as_text(self):
# buf = self.get_buffer()
# assert(buf is not None)
# return buf.get_content_as_text()
pass
def set_content_from_html(self, text):
# buf = self.get_buffer()
# assert(buf is not None)
# return buf.set_content_from_html(text)
pass
def get_content_as_html(self):
# buf = self.get_buffer()
# assert(buf is not None)
# return buf.get_content_as_html()
pass
def get_serialize_tag_name(self):
return 'img'
def get_element_serialized(self):
return self.model.get_filename()
def set_element_serialized(self, data):
assert(self.model is not None)
self.model.load_from_file(data)
def is_deletable(self):
# buf = self.get_buffer()
# start = buf.get_start_iter()
# end = buf.get_end_iter()
# return start.compare(end) == 0
return True
def set_model(self, model):
self.model = model
self.model.connect('image-changed', self.on_model_image_changed)
self.model.connect('move', self.on_model_image_move)
self.queue_draw()
def on_model_image_changed(self, model):
#print('model-image-changed')
if self.childwidget:
self.remove(self.childwidget)
img = model.get_image()
img.reparent(self)
self.add(img)
self.childwidget = img
w, _dont_care_ = img.get_preferred_width()
h, _dont_care_ = img.get_preferred_height()
#self.set_hexpand(False)
#self.set_vexpand(False)
self.set_size_request(w, h)
# ugly hack
self.img_orig = img
# ugly hack
self.queue_draw()
def on_model_image_move(self, model, x, y):
x, y = model.get_image_coord()
self.move(self.childwidget, x, y)
def get_model(self):
return self.model
def dialog_edit_image_property(self):
propwin = ImagePropWin.get_instance()
if not self.model:
self.model = ImageModel()
propwin.set_model(self.model)
if not propwin.is_visible():
propwin.show()
# Move window near the main window app
parent = self.get_parent_window().get_effective_parent()
_dont_care_, parent_x, parent_y = parent.get_origin()
parent_w = parent.get_width()
parent_h = parent.get_height()
propwin.move(parent_x + parent_w, parent_y)
GObject.type_register(ImageView)