File: filedialog.py

package info (click to toggle)
sugar-pippy-activity 70%2B20160925~dfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 1,492 kB
  • ctags: 1,126
  • sloc: python: 8,015; makefile: 27
file content (151 lines) | stat: -rw-r--r-- 4,565 bytes parent folder | download | duplicates (2)
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
# -*- coding: utf-8 -*-
# Copyright (C) 2014 Walter Bender
# Copyright (C) 2014 Ignacio Rodríguez <ignacio@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 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 St, Fifth Floor, Boston, MA  02110-1301  USA

from gi.repository import Gtk, Gdk
from sugar3.graphics.toolbarbox import ToolbarBox
from sugar3.graphics.toolbutton import ToolButton
from gettext import gettext as _
import os


class FileDialog(Gtk.Dialog):
    """
    Simple file dialog:

    from filedialog import FileDialog
    dialog = FileDialog(['Graphics', '/path/of/graphics'])
    dialog.run()
    path = dialog.get_path()
    """

    def __init__(self, dirs, window=None, button=None):
        Gtk.Dialog.__init__(self, flags=Gtk.DialogFlags.DESTROY_WITH_PARENT)

        self.example_path = None
        self.expanders = []
        self.dirs = dirs
        self.button = button

        x, y = (Gdk.Screen.width() / 1.5, Gdk.Screen.height() / 1.5)
        self.set_size_request(x, y)

        toolbox = self.build_toolbar()
        expands = self.build_scroll()

        self.vbox.pack_start(toolbox, False, False, 0)

        self.vbox.pack_start(expands, True, True, 5)

        self.set_decorated(False)
        self.set_skip_pager_hint(True)
        self.set_skip_taskbar_hint(True)
        self.set_resizable(False)
        self.set_modal(True)

        self.modify_bg(Gtk.StateType.NORMAL, Gdk.color_parse('#F3EEEE'))
        self.show_all()

    def get_path(self):
        return self.example_path

    def _destroy(self, widget, sample=False):
        if sample:
            self.example_path = widget.get_tooltip_text()

        if self.button:
            self.button.set_icon_name("pippy-openoff")

        self.destroy()

    def build_toolbar(self):
        toolbox = ToolbarBox()
        toolbar = toolbox.toolbar

        label = Gtk.Label(_('Choose an example to open'))
        label.modify_fg(Gtk.StateType.NORMAL,
                        Gdk.color_parse('white'))

        item = Gtk.ToolItem()
        item.add(label)

        close = ToolButton('entry-cancel')
        close.connect('clicked', self._destroy)

        separator = Gtk.SeparatorToolItem()
        separator.props.draw = False
        separator.set_expand(True)

        toolbar.insert(item, -1)
        toolbar.insert(separator, -1)
        toolbar.insert(close, -1)

        toolbox.set_size_request(-1, 35)

        return toolbox

    def build_scroll(self):
        scroll = Gtk.ScrolledWindow()
        scroll.set_policy(Gtk.PolicyType.AUTOMATIC,
                          Gtk.PolicyType.AUTOMATIC)

        vbox = Gtk.VBox()
        scroll.add_with_viewport(vbox)

        dirs = self.dirs
        for dir_ in dirs:
            dir_path = dir_[1]
            dir_name = dir_[0]
            expand = self.build_expand(dir_path, dir_name)
            if not expand:
                continue
            vbox.pack_start(expand, False, False, 2)
            expand.show()
            self.expanders.append(expand)

        return scroll

    def build_expand(self, path, name):
        if not os.path.exists(path):
            return None

        expander = Gtk.Expander()
        expander.set_label(name)
        expander.modify_fg(Gtk.StateType.NORMAL,
                           Gdk.color_parse('black'))

        vbox = Gtk.VBox()
        for root, dirs, files in os.walk(path):

            if not files:
                return None

            for _file in files:
                if _file.endswith('~'):
                    continue
                entry = {"name": _(_file.capitalize()),
                         "path": os.path.join(path, _file)}

                button = Gtk.Button(entry['name'])
                button.set_tooltip_text(entry['path'])
                button.set_has_tooltip(False)
                button.connect('clicked', self._destroy, True)
                vbox.pack_start(button, False, False, 1)

        expander.add(vbox)

        return expander