b3e7ca62ed143dc5e6763252af859d6dc045c1f0
[blender.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 from bpy.types import Header, Panel, Menu, UIList
21
22
23 class FILEBROWSER_HT_header(Header):
24     bl_space_type = 'FILE_BROWSER'
25
26     def draw(self, context):
27         layout = self.layout
28
29         st = context.space_data
30         params = st.params
31
32         if st.active_operator is None:
33             layout.template_header()
34
35         layout.menu("FILEBROWSER_MT_view")
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         layout.operator_context = 'EXEC_DEFAULT'
44         layout.operator("file.directory_new", icon='NEWFOLDER', text="")
45
46         layout.operator_context = 'INVOKE_DEFAULT'
47
48         # can be None when save/reload with a file selector open
49         if params:
50             is_lib_browser = params.use_library_browsing
51
52             layout.prop(params, "display_type", expand=True, text="")
53             layout.prop(params, "sort_method", expand=True, text="")
54             layout.prop(params, "show_hidden", text="", icon='FILE_HIDDEN')
55
56         layout.separator_spacer()
57
58         layout.template_running_jobs()
59
60         if params:
61             layout.prop(params, "use_filter", text="", icon='FILTER')
62
63             row = layout.row(align=True)
64             row.active = params.use_filter
65             row.prop(params, "use_filter_folder", text="")
66
67             if params.filter_glob:
68                 # if st.active_operator and hasattr(st.active_operator, "filter_glob"):
69                 #     row.prop(params, "filter_glob", text="")
70                 row.label(text=params.filter_glob)
71             else:
72                 row.prop(params, "use_filter_blender", text="")
73                 row.prop(params, "use_filter_backup", text="")
74                 row.prop(params, "use_filter_image", text="")
75                 row.prop(params, "use_filter_movie", text="")
76                 row.prop(params, "use_filter_script", text="")
77                 row.prop(params, "use_filter_font", text="")
78                 row.prop(params, "use_filter_sound", text="")
79                 row.prop(params, "use_filter_text", text="")
80
81             if is_lib_browser:
82                 row.prop(params, "use_filter_blendid", text="")
83                 if params.use_filter_blendid:
84                     row.separator()
85                     row.prop(params, "filter_id_category", text="")
86
87             row.separator()
88             row.prop(params, "filter_search", text="", icon='VIEWZOOM')
89
90
91 class FILEBROWSER_UL_dir(UIList):
92     def draw_item(self, _context, layout, _data, item, icon, _active_data, active_propname, _index):
93         direntry = item
94         # space = context.space_data
95         icon = 'NONE'
96         if active_propname == "system_folders_active":
97             icon = 'DISK_DRIVE'
98         if active_propname == "system_bookmarks_active":
99             icon = 'BOOKMARKS'
100         if active_propname == "bookmarks_active":
101             icon = 'BOOKMARKS'
102         if active_propname == "recent_folders_active":
103             icon = 'FILE_FOLDER'
104
105         if self.layout_type in {'DEFAULT', 'COMPACT'}:
106             row = layout.row(align=True)
107             row.enabled = direntry.is_valid
108             # Non-editable entries would show grayed-out, which is bad in this specific case, so switch to mere label.
109             if direntry.is_property_readonly("name"):
110                 row.label(text=direntry.name, icon=icon)
111             else:
112                 row.prop(direntry, "name", text="", emboss=False, icon=icon)
113
114         elif self.layout_type == 'GRID':
115             layout.alignment = 'CENTER'
116             layout.prop(direntry, "path", text="")
117
118
119 class FILEBROWSER_PT_system_folders(Panel):
120     bl_space_type = 'FILE_BROWSER'
121     bl_region_type = 'TOOLS'
122     bl_category = "Bookmarks"
123     bl_label = "System"
124
125     def draw(self, context):
126         layout = self.layout
127         space = context.space_data
128
129         if space.system_folders:
130             row = layout.row()
131             row.template_list("FILEBROWSER_UL_dir", "system_folders", space, "system_folders",
132                               space, "system_folders_active", item_dyntip_propname="path", rows=1, maxrows=10)
133
134
135 class FILEBROWSER_PT_system_bookmarks(Panel):
136     bl_space_type = 'FILE_BROWSER'
137     bl_region_type = 'TOOLS'
138     bl_category = "Bookmarks"
139     bl_label = "System Bookmarks"
140
141     @classmethod
142     def poll(cls, context):
143         return not context.preferences.filepaths.hide_system_bookmarks
144
145     def draw(self, context):
146         layout = self.layout
147         space = context.space_data
148
149         if space.system_bookmarks:
150             row = layout.row()
151             row.template_list("FILEBROWSER_UL_dir", "system_bookmarks", space, "system_bookmarks",
152                               space, "system_bookmarks_active", item_dyntip_propname="path", rows=1, maxrows=10)
153
154
155 class FILEBROWSER_MT_bookmarks_context_menu(Menu):
156     bl_label = "Bookmarks Specials"
157
158     def draw(self, _context):
159         layout = self.layout
160         layout.operator("file.bookmark_cleanup", icon='X', text="Cleanup")
161
162         layout.separator()
163         layout.operator("file.bookmark_move", icon='TRIA_UP_BAR', text="Move To Top").direction = 'TOP'
164         layout.operator("file.bookmark_move", icon='TRIA_DOWN_BAR', text="Move To Bottom").direction = 'BOTTOM'
165
166
167 class FILEBROWSER_PT_bookmarks(Panel):
168     bl_space_type = 'FILE_BROWSER'
169     bl_region_type = 'TOOLS'
170     bl_category = "Bookmarks"
171     bl_label = "Bookmarks"
172
173     def draw(self, context):
174         layout = self.layout
175         space = context.space_data
176
177         if space.bookmarks:
178             row = layout.row()
179             num_rows = len(space.bookmarks)
180             row.template_list("FILEBROWSER_UL_dir", "bookmarks", space, "bookmarks",
181                               space, "bookmarks_active", item_dyntip_propname="path",
182                               rows=(2 if num_rows < 2 else 4), maxrows=10)
183
184             col = row.column(align=True)
185             col.operator("file.bookmark_add", icon='ADD', text="")
186             col.operator("file.bookmark_delete", icon='REMOVE', text="")
187             col.menu("FILEBROWSER_MT_bookmarks_context_menu", icon='DOWNARROW_HLT', text="")
188
189             if num_rows > 1:
190                 col.separator()
191                 col.operator("file.bookmark_move", icon='TRIA_UP', text="").direction = 'UP'
192                 col.operator("file.bookmark_move", icon='TRIA_DOWN', text="").direction = 'DOWN'
193         else:
194             layout.operator("file.bookmark_add", icon='ADD')
195
196
197 class FILEBROWSER_PT_recent_folders(Panel):
198     bl_space_type = 'FILE_BROWSER'
199     bl_region_type = 'TOOLS'
200     bl_category = "Bookmarks"
201     bl_label = "Recent"
202
203     @classmethod
204     def poll(cls, context):
205         return not context.preferences.filepaths.hide_recent_locations
206
207     def draw(self, context):
208         layout = self.layout
209         space = context.space_data
210
211         if space.recent_folders:
212             row = layout.row()
213             row.template_list("FILEBROWSER_UL_dir", "recent_folders", space, "recent_folders",
214                               space, "recent_folders_active", item_dyntip_propname="path", rows=1, maxrows=10)
215
216             col = row.column(align=True)
217             col.operator("file.reset_recent", icon='X', text="")
218
219
220 class FILEBROWSER_PT_advanced_filter(Panel):
221     bl_space_type = 'FILE_BROWSER'
222     bl_region_type = 'TOOLS'
223     bl_category = "Filter"
224     bl_label = "Advanced Filter"
225
226     @classmethod
227     def poll(cls, context):
228         # only useful in append/link (library) context currently...
229         return context.space_data.params.use_library_browsing
230
231     def draw(self, context):
232         layout = self.layout
233         space = context.space_data
234         params = space.params
235
236         if params and params.use_library_browsing:
237             layout.prop(params, "use_filter_blendid")
238             if params.use_filter_blendid:
239                 layout.separator()
240                 col = layout.column()
241                 col.prop(params, "filter_id")
242
243
244 class FILEBROWSER_MT_view(Menu):
245     bl_label = "View"
246
247     def draw(self, context):
248         layout = self.layout
249         st = context.space_data
250         params = st.params
251
252         layout.prop_menu_enum(params, "display_size")
253         layout.prop_menu_enum(params, "recursion_level")
254
255         layout.separator()
256
257         layout.menu("INFO_MT_area")
258
259
260 classes = (
261     FILEBROWSER_HT_header,
262     FILEBROWSER_UL_dir,
263     FILEBROWSER_PT_system_folders,
264     FILEBROWSER_PT_system_bookmarks,
265     FILEBROWSER_MT_bookmarks_context_menu,
266     FILEBROWSER_PT_bookmarks,
267     FILEBROWSER_PT_recent_folders,
268     FILEBROWSER_PT_advanced_filter,
269     FILEBROWSER_MT_view,
270 )
271
272 if __name__ == "__main__":  # only for live edit.
273     from bpy.utils import register_class
274     for cls in classes:
275         register_class(cls)