-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmonet.py
501 lines (333 loc) · 15.7 KB
/
monet.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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# This program 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.
#
# This program 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 this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
import os
import math
import gobject
import gtk
import clutter
import cluttergtk
import optparse
import cream
ICON_SIZE = 32
ICON_SPACING = 5
CONTROL_BOX_MARGIN = 8
def rounded_rectangle(cr, x, y, w, h, r=20):
if r >= h / 2.0:
r = h / 2.0
cr.arc(x + r, y + r, r, math.pi, -.5 * math.pi)
cr.arc(x + w - r, y + r, r, -.5 * math.pi, 0 * math.pi)
cr.arc(x + w - r, y + h - r, r, 0 * math.pi, .5 * math.pi)
cr.arc(x + r, y + h - r, r, .5 * math.pi, math.pi)
cr.close_path()
class Image(clutter.Texture):
def __init__(self, path, load=False):
clutter.Texture.__init__(self)
self.path = path
self.loaded = False
self.aspect_ratio = 1
self.set_position(0, 0)
if load:
self.load()
def load(self, async=False):
if self.loaded:
return
self.loaded = True
if async:
self.set_load_async(True)
self.connect('load-finished', self.load_finished_cb)
self.set_from_file(self.path)
def load_finished_cb(self, *args):
self.aspect_ratio = float(self.get_width()) / float(self.get_height())
def get_aspect_ratio(self):
return self.aspect_ratio
def fade_in(self):
self.fade(255)
return False
def fade_out(self):
self.fade(0)
return False
def fade(self, opacity, duration=400):
self.animate(clutter.AnimationMode(clutter.LINEAR), duration, 'opacity', opacity)
class StartScreen(clutter.Group):
__gsignals__ = {
'show-open-dialog': (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, ())
}
def __init__(self):
clutter.Group.__init__(self)
self.set_reactive(True)
self.connect('button-release-event', lambda *args: self.emit('show-open-dialog'))
self.icon = Image('data/images/monet.svg')
self.icon.load()
self.label = clutter.Text()
self.label.set_color(clutter.Color(255, 255, 255, 200))
self.label.set_font_name('Sans 9')
self.label.set_text("Click to open files...")
self.add(self.icon)
self.add(self.label)
def set_size(self, width, height):
clutter.Group.set_size(self, width, height)
self.icon.set_position((width - self.icon.get_width()) / 2, (height - self.icon.get_height() - self.label.get_height() - 10) / 2)
self.label.set_position((width - self.label.get_width()) / 2, (height - self.icon.get_height() - self.label.get_height() - 10) / 2 + self.icon.get_height() + 10)
def fade_in(self):
self.fade(255)
return False
def fade_out(self):
self.fade(0)
return False
def fade(self, opacity, duration=400):
self.animate(clutter.AnimationMode(clutter.LINEAR), duration, 'opacity', opacity)
class ControlAreaBackground(clutter.CairoTexture):
def __init__(self):
clutter.CairoTexture.__init__(self, 3*ICON_SIZE + 2*ICON_SPACING + 2*CONTROL_BOX_MARGIN, ICON_SIZE + 2*CONTROL_BOX_MARGIN)
ctx = self.cairo_create()
rounded_rectangle(ctx, 0, 0, 3*ICON_SIZE + 2*ICON_SPACING + 2*CONTROL_BOX_MARGIN, ICON_SIZE + 2*CONTROL_BOX_MARGIN, 10)
ctx.set_source_rgba(0, 0, 0, .6)
ctx.fill()
ctx.set_line_width(1)
rounded_rectangle(ctx, .5, .5, 3*ICON_SIZE + 2*ICON_SPACING + 2*CONTROL_BOX_MARGIN - 1, ICON_SIZE + 2*CONTROL_BOX_MARGIN - 1, 10)
ctx.set_source_rgba(1, 1, 1, .2)
ctx.stroke()
class ControlArea(clutter.Group):
""" A class representing the small box containing the control buttons. """
__gsignals__ = {
'previous-image': (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, ()),
'next-image': (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, ()),
'toggle-fullscreen': (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, ()),
}
def __init__(self):
clutter.Group.__init__(self)
# Enable UI events...
self.set_reactive(True)
# Add the background:
self.background = ControlAreaBackground()
self.add(self.background)
# Add the icons...
self.icon_previous = Image('data/images/previous.png', load=True)
self.icon_previous.set_size(ICON_SIZE, ICON_SIZE)
self.icon_previous.set_position(CONTROL_BOX_MARGIN, CONTROL_BOX_MARGIN)
self.icon_previous.set_opacity(150)
self.add(self.icon_previous)
self.icon_fullscreen = Image('data/images/fullscreen.png', load=True)
self.icon_fullscreen.set_size(ICON_SIZE, ICON_SIZE)
self.icon_fullscreen.set_position(CONTROL_BOX_MARGIN + ICON_SIZE + ICON_SPACING, CONTROL_BOX_MARGIN)
self.icon_fullscreen.set_opacity(150)
self.add(self.icon_fullscreen)
self.icon_next = Image('data/images/next.png', load=True)
self.icon_next.set_size(ICON_SIZE, ICON_SIZE)
self.icon_next.set_position(CONTROL_BOX_MARGIN + 2*ICON_SIZE + 2*ICON_SPACING, CONTROL_BOX_MARGIN)
self.icon_next.set_opacity(150)
self.add(self.icon_next)
self.icon_previous.set_reactive(True)
self.icon_previous.connect('button-release-event', lambda *args: self.emit('previous-image'))
self.icon_previous.connect('enter-event', lambda *args: self.icon_previous.fade(255, duration=200))
self.icon_previous.connect('leave-event', lambda *args: self.icon_previous.fade(150, duration=200))
self.icon_next.set_reactive(True)
self.icon_next.connect('button-release-event', lambda *args: self.emit('next-image'))
self.icon_next.connect('enter-event', lambda *args: self.icon_next.fade(255, duration=200))
self.icon_next.connect('leave-event', lambda *args: self.icon_next.fade(150, duration=200))
self.icon_fullscreen.set_reactive(True)
self.icon_fullscreen.connect('button-release-event', lambda *args: self.emit('toggle-fullscreen'))
self.icon_fullscreen.connect('button-release-event', lambda *args: self.icon_fullscreen.fade(150, duration=200) if self.icon_fullscreen.get_opacity() != 200 else dir())
self.icon_fullscreen.connect('enter-event', lambda *args: self.icon_fullscreen.fade(255, duration=200))
self.icon_fullscreen.connect('leave-event', lambda *args: self.icon_fullscreen.fade(150, duration=200))
def fade_in(self):
""" Fade in the control area... """
self.animate(clutter.AnimationMode(clutter.LINEAR), 400, 'opacity', 255)
return False
def fade_out(self):
""" Fade out the control area... """
self.animate(clutter.AnimationMode(clutter.LINEAR), 400, 'opacity', 0)
return False
class ImageView(cluttergtk.Embed):
"""
A subclass of cluttergtk.Embed managing the displayed images
including animations, etc. and the basic interaction through
the control area.
"""
__gsignals__ = {
'show-open-dialog': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE, ()),
'toggle-fullscreen': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE, ())
}
def __init__(self):
cluttergtk.Embed.__init__(self)
self.timeouts = {
'control-area-fade-out': None
}
# Control the size of the surface and the images...
self.connect('size-allocate', self.size_allocate_cb)
# Initialize...
self.realize()
self.stage = self.get_stage()
self.stage.set_color(clutter.color_from_string('black'))
# Add some images...
# TODO: Implement file dialog!
self.images = []
self.animate_transition = True
# Show the first image...
self.current_image = None
# Initialize the control area...
self.control_area = ControlArea()
# Keep it above the images:
self.control_area.set_depth(1)
# Connect to the events from the control area...
self.control_area.connect('previous-image', lambda *args: self.show_image(max(self.current_image - 1, 0), animate=self.animate_transition))
self.control_area.connect('next-image', lambda *args: self.show_image(min(self.current_image + 1, len(self.images) - 1), animate=self.animate_transition))
self.control_area.connect('toggle-fullscreen', lambda *args: (self.timeouts.__setitem__('control-area-fade-out', gobject.timeout_add(2000, lambda: (self.control_area.fade_out()))), self.emit('toggle-fullscreen')))
self.control_area.connect('enter-event', self.control_area_enter_event_cb)
self.control_area.connect('leave-event', self.control_area_leave_event_cb)
# Add the control area to the UI...
self.stage.add(self.control_area)
self.control_area.set_opacity(0)
# Add the start screen:
self.start_screen = StartScreen()
self.stage.add(self.start_screen)
# Fade in the control area on mouse movements...
self.stage.connect('motion-event', self.motion_event_cb)
self.start_screen.connect('show-open-dialog', lambda *args: self.emit('show-open-dialog'))
def set_animate_transition(self, value):
self.animate_transition = value
def hide_start_screen(self, animate=True):
if animate:
self.start_screen.fade_out()
else:
self.start_screen.set_opacity(0)
def add_image(self, path):
self.images.append(Image(path))
if self.current_image == None:
self.show_image(0, animate=True)
def load_image(self, img, async=False):
""" Load the image with the given ID... """
self.images[img].load(async)
def show_image(self, img, animate=True):
"""
Show the given image.
:param img: The ID of the image.
:type img: `int`
:param animate: Animate the transition?
:type animate: `bool`
"""
if self.current_image == None and self.start_screen.get_opacity() != 0:
self.start_screen.fade_out()
# Load the image...
self.load_image(img)
# Preload the next image...
try:
self.load_image(img + 1, async=True)
except:
pass
if img == self.current_image:
return False
if self.current_image != None:
old_image = self.images[self.current_image]
else:
old_image = None
self.current_image = img
if not self.images[self.current_image].get_parent():
self.stage.add(self.images[self.current_image])
if animate:
self.images[self.current_image].set_opacity(0)
self.images[self.current_image].fade_in()
if old_image:
old_image.fade_out()
else:
old_image.set_opacity(0)
self.images[self.current_image].set_opacity(255)
self.calculate_image_position(self.images[self.current_image])
return False
def motion_event_cb(self, stage, event):
if self.current_image == None:
return
self.control_area.fade_in()
if self.timeouts['control-area-fade-out']:
gobject.source_remove(self.timeouts['control-area-fade-out'])
self.timeouts['control-area-fade-out'] = None
if self.timeouts['control-area-fade-out'] != False:
self.timeouts['control-area-fade-out'] = gobject.timeout_add(2000, lambda: self.control_area.fade_out())
def control_area_enter_event_cb(self, control_area, event):
if self.timeouts['control-area-fade-out']:
gobject.source_remove(self.timeouts['control-area-fade-out'])
self.timeouts['control-area-fade-out'] = False
def control_area_leave_event_cb(self, control_area, event):
self.timeouts['control-area-fade-out'] = gobject.timeout_add(2000, lambda: self.control_area.fade_out())
def calculate_image_position(self, image):
image_aspect_ratio = image.get_aspect_ratio()
view_aspect_ratio = float(self.allocation.width) / float(self.allocation.height)
if image_aspect_ratio >= view_aspect_ratio:
image.set_size(self.allocation.width, self.allocation.width / image_aspect_ratio)
image.set_position(0, (self.allocation.height - self.allocation.width / image_aspect_ratio) / 2)
else:
image.set_size(self.allocation.height * image_aspect_ratio, self.allocation.height)
image.set_position((self.allocation.width - self.allocation.height * image_aspect_ratio) / 2, 0)
def size_allocate_cb(self, source, allocation):
self.stage.set_size(allocation.width, allocation.height)
self.start_screen.set_size(allocation.width, allocation.height)
if self.current_image != None:
self.calculate_image_position(self.images[self.current_image])
self.control_area.set_position((allocation.width - self.control_area.get_size()[0]) / 2, allocation.height - self.control_area.get_size()[1] - 30)
class Monet(cream.Module):
""" Main class of Monet. """
def __init__(self):
cream.Module.__init__(self)
self.fullscreen = False
# Initialize the GUI...
self.window = gtk.Window()
self.window.set_title("Monet")
self.view = ImageView()
self.view.set_animate_transition(self.config.animate_transition)
self.view.connect('show-open-dialog', self.show_open_dialog_cb)
self.view.connect('toggle-fullscreen', self.toggle_fullscreen_cb)
self.window.add(self.view)
self.window.set_size_request(800, 480)
self.window.show_all()
self.window.connect('destroy', lambda *args: self.quit())
parser = optparse.OptionParser()
parser.add_option("--fs", "--fullscreen", action="store_true", dest="fullscreen", help="Start Monet in fullscreen mode...")
(self.options, self.args) = parser.parse_args()
if self.options.fullscreen:
self.set_fullscreen(True)
if len(self.args):
self.view.hide_start_screen(animate=False)
path = self.args[0]
for i in os.listdir(path):
if i.endswith('.png') or i.endswith('.JPG'):
self.view.add_image(os.path.join(path, i))
print "RUNNING"
def toggle_fullscreen_cb(self, view):
self.set_fullscreen(not self.fullscreen)
def set_fullscreen(self, fullscreen):
if self.fullscreen == fullscreen:
return
self.fullscreen = fullscreen
if self.fullscreen:
self.window.fullscreen()
else:
self.window.unfullscreen()
def show_open_dialog_cb(self, view):
dialog = gtk.FileChooserDialog(title="Select a folder...",
parent=None,
action=gtk.FILE_CHOOSER_ACTION_SELECT_FOLDER,
buttons=(gtk.STOCK_CANCEL, gtk.RESPONSE_REJECT, gtk.STOCK_OPEN, gtk.RESPONSE_ACCEPT))
res = dialog.run()
if res == gtk.RESPONSE_ACCEPT:
path = dialog.get_filename()
for i in os.listdir(path):
if i.endswith('.png') or i.endswith('.jpg'):
self.view.add_image(os.path.join(path, i))
dialog.hide()
if __name__ == '__main__':
monet = Monet()
monet.main()