Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/frames_list.py
blob: c61c6731e8c749bb32e443c911e9070155120e0e (plain)
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# images_list.py
#
# Copyright 2012 S. Daniel Francis <francis@sugarlabs.org>,
#                Agustin Zubiaga <aguz@sugarlabs.org>
#
# 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 3 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 gtk


class FramesList(gtk.ScrolledWindow):

    def __init__(self):
        super(FramesList, self).__init__()

        self._animation = None
        self._animation_connected = False

        self.set_size_request(225, -1)
        self.set_policy(gtk.POLICY_NEVER, gtk.POLICY_AUTOMATIC)

        self.store = gtk.ListStore(int, gtk.gdk.Pixbuf)
        self.treeview = gtk.TreeView(self.store)

        self.set_vadjustment(self.treeview.get_vadjustment())
        self.selection = self.treeview.get_selection()
        self.selection.connect("changed", self._selection_changed_cb)

        column = gtk.TreeViewColumn("Frame")
        self.treeview.append_column(column)

        num_cell = gtk.CellRendererText()
        preview_cell = gtk.CellRendererPixbuf()

        column.pack_start(num_cell)
        column.pack_start(preview_cell)
        column.add_attribute(num_cell, 'text', 0)
        column.add_attribute(preview_cell, 'pixbuf', 1)

        self.treeview.show()
        self.add(self.treeview)
        self.frames = 0

    def remove_selected_frame(self):
        model, _iter = self.selection.get_selected()
        model.remove(_iter)
        count = 1
        for i in self.store:
            print i
            selected = False
            if i[0] != count:
                i[0] = count
                if not selected:
                    self.selection.select_iter(_iter)
                    selected = True
            count += 1
        self.frames = count - 1

    def _select_frame(self, widget, index):
        self.selection.select_iter(self.store.get_iter(index))
        selected_path = self.store.get_path(self.selection.get_selected()[1])
        self.treeview.scroll_to_cell(selected_path)

    def add_frame(self, pixbuf):
        self.frames += 1
        width = pixbuf.get_width()
        height = pixbuf.get_height()
        preview_pixbuf = pixbuf.scale_simple(200,
                                             200 * height / width,
                                             gtk.gdk.INTERP_TILES)
        _iter = self.store.append([self.frames, preview_pixbuf])
        self.selection.select_iter(_iter)
        if self._animation and not self._animation_connected:
            self._animation.connect("current-frame-updated", self._select_frame)
            self._animation_connected = True

    def _selection_changed_cb(self, selection):
        model, _iter = selection.get_selected()
        self._animation.set_pos(self.store.get_value(_iter, 0) - 1)