cf7669e74a4cac30830985ffcd1df9cb2298b127
[blender-staging.git] / release / scripts / startup / bl_ui / space_filebrowser.py
1 # ##### BEGIN GPL LICENSE BLOCK #####
2 #
3 #  This program is free software; you can redistribute it and/or
4 #  modify it under the terms of the GNU General Public License
5 #  as published by the Free Software Foundation; either version 2
6 #  of the License, or (at your option) any later version.
7 #
8 #  This program is distributed in the hope that it will be useful,
9 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
10 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 #  GNU General Public License for more details.
12 #
13 #  You should have received a copy of the GNU General Public License
14 #  along with this program; if not, write to the Free Software Foundation,
15 #  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 #
17 # ##### END GPL LICENSE BLOCK #####
18
19 # <pep8 compliant>
20 import bpy
21 from bpy.types import Header, Panel, Menu
22
23
24 class FILEBROWSER_HT_header(Header):
25     bl_space_type = 'FILE_BROWSER'
26
27     def draw(self, context):
28         layout = self.layout
29
30         st = context.space_data
31
32         layout.template_header()
33
34         row = layout.row()
35         row.separator()
36
37         row = layout.row(align=True)
38         row.operator("file.previous", text="", icon='BACK')
39         row.operator("file.next", text="", icon='FORWARD')
40         row.operator("file.parent", text="", icon='FILE_PARENT')
41         row.operator("file.refresh", text="", icon='FILE_REFRESH')
42
43         row = layout.row()
44         row.separator()
45
46         row = layout.row(align=True)
47         layout.operator_context = 'EXEC_DEFAULT'
48         row.operator("file.directory_new", icon='NEWFOLDER')
49
50         layout.operator_context = 'INVOKE_DEFAULT'
51         params = st.params
52
53         # can be None when save/reload with a file selector open
54         if params:
55             layout.prop(params, "display_type", expand=True, text="")
56             layout.prop(params, "sort_method", expand=True, text="")
57
58             layout.prop(params, "show_hidden", text="", icon='FILE_HIDDEN')
59             layout.prop(params, "use_filter", text="", icon='FILTER')
60
61             row = layout.row(align=True)
62             row.active = params.use_filter
63
64             row.prop(params, "use_filter_folder", text="")
65
66             if params.filter_glob:
67                 #if st.active_operator and hasattr(st.active_operator, "filter_glob"):
68                 #    row.prop(params, "filter_glob", text="")
69                 row.label(params.filter_glob)
70             else:
71                 row.prop(params, "use_filter_blender", text="")
72                 row.prop(params, "use_filter_backup", text="")
73                 row.prop(params, "use_filter_image", text="")
74                 row.prop(params, "use_filter_movie", text="")
75                 row.prop(params, "use_filter_script", text="")
76                 row.prop(params, "use_filter_font", text="")
77                 row.prop(params, "use_filter_sound", text="")
78                 row.prop(params, "use_filter_text", text="")
79
80             row.separator()
81             row.prop(params, "filter_search", text="", icon='VIEWZOOM')
82
83
84 class FILEBROWSER_UL_dir(bpy.types.UIList):
85     def draw_item(self, context, layout, data, item, icon, active_data, active_propname, index):
86         direntry = item
87         space = context.space_data
88         icon = 'NONE'
89         if active_propname == "system_folders_active":
90             icon = 'DISK_DRIVE'
91         if active_propname == "system_bookmarks_active":
92             icon = 'BOOKMARKS'
93         if active_propname == "bookmarks_active":
94             icon = 'BOOKMARKS'
95         if active_propname == "recent_folders_active":
96             icon = 'FILE_FOLDER'
97
98         if self.layout_type in {'DEFAULT', 'COMPACT'}:
99             row = layout.row(align=True)
100             row.enabled = direntry.is_valid
101             # Non-editable entries would show grayed-out, which is bad in this specific case, so switch to mere label.
102             if direntry.is_property_readonly('name'):
103                 row.label(text=direntry.name, icon=icon)
104             else:
105                 row.prop(direntry, "name", text="", emboss=False, icon=icon)
106
107         elif self.layout_type in {'GRID'}:
108             layout.alignment = 'CENTER'
109             layout.prop(direntry, "path", text="")
110
111
112 class FILEBROWSER_PT_system_folders(Panel):
113     bl_space_type = 'FILE_BROWSER'
114     bl_region_type = 'TOOLS'
115     bl_category = "Bookmarks"
116     bl_label = "System"
117
118     def draw(self, context):
119         layout = self.layout
120         space = context.space_data
121
122         if space.system_folders:
123             row = layout.row()
124             row.template_list("FILEBROWSER_UL_dir", "system_folders", space, "system_folders",
125                               space, "system_folders_active", item_dyntip_propname="path", rows=1, maxrows=10)
126
127 class FILEBROWSER_PT_system_bookmarks(Panel):
128     bl_space_type = 'FILE_BROWSER'
129     bl_region_type = 'TOOLS'
130     bl_category = "Bookmarks"
131     bl_label = "System Bookmarks"
132
133     @classmethod
134     def poll(cls, context):
135         return not context.user_preferences.filepaths.hide_system_bookmarks
136
137     def draw(self, context):
138         layout = self.layout
139         space = context.space_data
140
141         if space.system_bookmarks:
142             row = layout.row()
143             row.template_list("FILEBROWSER_UL_dir", "system_bookmarks", space, "system_bookmarks",
144                               space, "system_bookmarks_active", item_dyntip_propname="path", rows=1, maxrows=10)
145
146
147 class FILEBROWSER_MT_bookmarks_specials(Menu):
148     bl_label = "Bookmarks Specials"
149
150     def draw(self, context):
151         layout = self.layout
152         layout.operator("file.bookmark_cleanup", icon='X', text="Cleanup")
153
154         layout.separator()
155         layout.operator("file.bookmark_move", icon='TRIA_UP_BAR', text="Move To Top").direction = 'TOP'
156         layout.operator("file.bookmark_move", icon='TRIA_DOWN_BAR', text="Move To Bottom").direction = 'BOTTOM'
157
158
159 class FILEBROWSER_PT_bookmarks(Panel):
160     bl_space_type = 'FILE_BROWSER'
161     bl_region_type = 'TOOLS'
162     bl_category = "Bookmarks"
163     bl_label = "Bookmarks"
164
165     def draw(self, context):
166         layout = self.layout
167         space = context.space_data
168
169         if space.bookmarks:
170             row = layout.row()
171             num_rows = len(space.bookmarks)
172             row.template_list("FILEBROWSER_UL_dir", "bookmarks", space, "bookmarks",
173                               space, "bookmarks_active", item_dyntip_propname="path",
174                               rows=(2 if num_rows < 2 else 4), maxrows=10)
175
176             col = row.column(align=True)
177             col.operator("file.bookmark_add", icon='ZOOMIN', text="")
178             col.operator("file.bookmark_delete", icon='ZOOMOUT', text="")
179             col.menu("FILEBROWSER_MT_bookmarks_specials", icon='DOWNARROW_HLT', text="")
180
181             if num_rows > 1:
182                 col.separator()
183                 col.operator("file.bookmark_move", icon='TRIA_UP', text="").direction = 'UP'
184                 col.operator("file.bookmark_move", icon='TRIA_DOWN', text="").direction = 'DOWN'
185         else:
186             layout.operator("file.bookmark_add", icon='ZOOMIN')
187
188
189 class FILEBROWSER_PT_recent_folders(Panel):
190     bl_space_type = 'FILE_BROWSER'
191     bl_region_type = 'TOOLS'
192     bl_category = "Bookmarks"
193     bl_label = "Recent"
194
195     @classmethod
196     def poll(cls, context):
197         return not context.user_preferences.filepaths.hide_recent_locations
198
199     def draw(self, context):
200         layout = self.layout
201         space = context.space_data
202
203         if space.recent_folders:
204             row = layout.row()
205             row.template_list("FILEBROWSER_UL_dir", "recent_folders", space, "recent_folders",
206                               space, "recent_folders_active", item_dyntip_propname="path", rows=1, maxrows=10)
207
208             col = row.column(align=True)
209             col.operator("file.reset_recent", icon='X', text="")
210
211
212 if __name__ == "__main__":  # only for live edit.
213     bpy.utils.register_module(__name__)