File: slideshowshuffle.py

package info (click to toggle)
eog-plugins 44.1-6
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,564 kB
  • sloc: ansic: 2,848; python: 593; makefile: 34; xml: 14
file content (95 lines) | stat: -rw-r--r-- 3,455 bytes parent folder | download | duplicates (9)
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
# Slideshow Shuffle Plugin for Eye of GNOME
# Copyright (C) 2008  Johannes Marbach <jm@rapidrabbit.de>
#
# 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.

from gi.repository import GObject, Gtk, Eog
import random

class SlideshowShufflePlugin(GObject.Object, Eog.WindowActivatable):

    # Override EogWindowActivatable's window property
    window = GObject.property(type=Eog.Window)

    def __init__(self):
        GObject.Object.__init__(self)

    def do_activate(self):
        random.seed()
        self.slideshow = False
        self.state_handler_id = \
            self.window.connect('window-state-event', self.state_changed_cb, self)

    def do_deactivate(self):
        self.window.disconnect(self.state_handler_id)

    # The callback functions are done statically to avoid causing additional
    # references on the window property causing eog to not quit correctly.
    @staticmethod
    def state_changed_cb(window, event, self):
        mode = self.window.get_mode()

        if mode == Eog.WindowMode.SLIDESHOW and not self.slideshow:
            # Slideshow starts
            self.slideshow = True

            # Query position of current image
            if not window.get_image():
                pos = 0
            else:
                pos = window.get_store().get_pos_by_image(window.get_image())

            # Generate random map
            uri_list = [row[2].get_uri_for_display() \
                        for row in window.get_store()]
            self.map = {uri_list.pop(pos): 0}
            supply = list(range(1, len(uri_list) + 1))
            for uri in uri_list:
                self.map[uri] = supply.pop(random.randint(0, len(supply) - 1))

            # Put random sort function in place
            self.window.get_store().\
                set_default_sort_func(self.random_sort_function, self)
        elif mode == Eog.WindowMode.NORMAL and self.slideshow:
            # Slideshow ends
            self.slideshow = False

            # Put alphabetic sort function in place
            self.window.get_store().\
                set_default_sort_func(self.alphabetic_sort_function)

    @staticmethod
    def random_sort_function(store, iter1, iter2, self):
        pos1 = self.map[store[iter1][2].get_uri_for_display()]
        pos2 = self.map[store[iter2][2].get_uri_for_display()]

        if  pos1 > pos2:
            return 1
        elif pos1 < pos2:
            return -1
        else:
            return 0

    @staticmethod
    def alphabetic_sort_function(store, iter1, iter2, data = None):
        uri1 = store[iter1][2].get_uri_for_display().lower()
        uri2 = store[iter2][2].get_uri_for_display().lower()

        if uri1 > uri2:
            return 1
        elif uri1 < uri2:
            return -1
        else:
            return 0