Print3D: remove option to rename the category
[blender-addons.git] / development_icon_get.py
blobcbeeea9aa2983e548fe1d8d32b661a50cd0367e1
1 # ##### BEGIN GPL LICENSE BLOCK #####
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.
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.
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.
17 # ##### END GPL LICENSE BLOCK #####
19 # <pep8 compliant>
22 bl_info = {
23 "name": "Icon Viewer",
24 "description": "Click an icon to copy its name to the clipboard",
25 "author": "roaoao",
26 "version": (1, 4, 0),
27 "blender": (2, 80, 0),
28 "location": "Search Menu > Icon Viewer, Text Editor > Properties",
29 "wiki_url": "https://wiki.blender.org/index.php/Extensions:2.6"
30 "/Py/Scripts/Development/Display_All_Icons",
31 "category": "Development"
34 import bpy
35 import math
36 from bpy.props import (
37 BoolProperty,
38 StringProperty,
41 DPI = 72
42 POPUP_PADDING = 10
43 PANEL_PADDING = 44
44 WIN_PADDING = 32
45 ICON_SIZE = 20
46 HISTORY_SIZE = 100
47 HISTORY = []
50 def ui_scale():
51 prefs = bpy.context.user_preferences.system
52 return prefs.dpi * prefs.pixel_size / DPI
55 def prefs():
56 return bpy.context.user_preferences.addons[__name__].preferences
59 class Icons:
60 def __init__(self, is_popup=False):
61 self._filtered_icons = None
62 self._filter = ""
63 self.filter = ""
64 self.selected_icon = ""
65 self.is_popup = is_popup
67 @property
68 def filter(self):
69 return self._filter
71 @filter.setter
72 def filter(self, value):
73 if self._filter == value:
74 return
76 self._filter = value
77 self.update()
79 @property
80 def filtered_icons(self):
81 if self._filtered_icons is None:
82 self._filtered_icons = []
83 icon_filter = self._filter.upper()
84 self.filtered_icons.clear()
85 pr = prefs()
87 icons = bpy.types.UILayout.bl_rna.functions[
88 "prop"].parameters["icon"].enum_items.keys()
89 for icon in icons:
90 if icon == 'NONE' or \
91 icon_filter and icon_filter not in icon or \
92 not pr.show_brush_icons and "BRUSH_" in icon and \
93 icon != 'BRUSH_DATA' or \
94 not pr.show_matcap_icons and "MATCAP_" in icon or \
95 not pr.show_event_icons and (
96 "EVENT_" in icon or "MOUSE_" in icon
97 ) or \
98 not pr.show_colorset_icons and "COLORSET_" in icon:
99 continue
100 self._filtered_icons.append(icon)
102 return self._filtered_icons
104 @property
105 def num_icons(self):
106 return len(self.filtered_icons)
108 def update(self):
109 if self._filtered_icons is not None:
110 self._filtered_icons.clear()
111 self._filtered_icons = None
113 def draw(self, layout, num_cols=0, icons=None):
114 if icons:
115 filtered_icons = reversed(icons)
116 else:
117 filtered_icons = self.filtered_icons
119 column = layout.column(align=True)
120 row = column.row(align=True)
121 row.alignment = 'CENTER'
123 selected_icon = self.selected_icon if self.is_popup else \
124 bpy.context.window_manager.clipboard
125 col_idx = 0
126 for i, icon in enumerate(filtered_icons):
127 p = row.operator(
128 IV_OT_icon_select.bl_idname, text="",
129 icon=icon, emboss=icon == selected_icon)
130 p.icon = icon
131 p.force_copy_on_select = not self.is_popup
133 col_idx += 1
134 if col_idx > num_cols - 1:
135 if icons:
136 break
137 col_idx = 0
138 if i < len(filtered_icons) - 1:
139 row = column.row(align=True)
140 row.alignment = 'CENTER'
142 if col_idx != 0 and not icons and i >= num_cols:
143 for _ in range(num_cols - col_idx):
144 row.label(text="", icon='BLANK1')
146 if not filtered_icons:
147 row.label(text="No icons were found")
150 class IV_Preferences(bpy.types.AddonPreferences):
151 bl_idname = __name__
153 panel_icons = Icons()
154 popup_icons = Icons(is_popup=True)
156 def update_icons(self, context):
157 self.panel_icons.update()
158 self.popup_icons.update()
160 def set_panel_filter(self, value):
161 self.panel_icons.filter = value
163 panel_filter: StringProperty(
164 description="Filter",
165 default="",
166 get=lambda s: s.panel_icons.filter,
167 set=set_panel_filter,
168 options={'TEXTEDIT_UPDATE'})
169 show_panel_icons: BoolProperty(
170 name="Show Icons",
171 description="Show icons", default=True)
172 show_history: BoolProperty(
173 name="Show History",
174 description="Show history", default=True)
175 show_brush_icons: BoolProperty(
176 name="Show Brush Icons",
177 description="Show brush icons", default=True,
178 update=update_icons)
179 show_matcap_icons: BoolProperty(
180 name="Show Matcap Icons",
181 description="Show matcap icons", default=True,
182 update=update_icons)
183 show_event_icons: BoolProperty(
184 name="Show Event Icons",
185 description="Show event icons", default=True,
186 update=update_icons)
187 show_colorset_icons: BoolProperty(
188 name="Show Colorset Icons",
189 description="Show colorset icons", default=True,
190 update=update_icons)
191 copy_on_select: BoolProperty(
192 name="Copy Icon On Click",
193 description="Copy icon on click", default=True)
194 close_on_select: BoolProperty(
195 name="Close Popup On Click",
196 description=(
197 "Close the popup on click.\n"
198 "Not supported by some windows (User Preferences, Render)"
200 default=False)
201 auto_focus_filter: BoolProperty(
202 name="Auto Focus Input Field",
203 description="Auto focus input field", default=True)
204 show_panel: BoolProperty(
205 name="Show Panel",
206 description="Show the panel in the Text Editor", default=True)
207 show_header: BoolProperty(
208 name="Show Header",
209 description="Show the header in the Python Console",
210 default=True)
212 def draw(self, context):
213 layout = self.layout
214 row = layout.row()
215 row.scale_y = 1.5
216 row.operator(IV_OT_icons_show.bl_idname)
218 row = layout.row()
220 col = row.column(align=True)
221 col.label(text="Icons:")
222 col.prop(self, "show_matcap_icons")
223 col.prop(self, "show_brush_icons")
224 col.prop(self, "show_colorset_icons")
225 col.prop(self, "show_event_icons")
226 col.separator()
227 col.prop(self, "show_history")
229 col = row.column(align=True)
230 col.label(text="Popup:")
231 col.prop(self, "auto_focus_filter")
232 col.prop(self, "copy_on_select")
233 if self.copy_on_select:
234 col.prop(self, "close_on_select")
236 col = row.column(align=True)
237 col.label(text="Panel:")
238 col.prop(self, "show_panel")
239 if self.show_panel:
240 col.prop(self, "show_panel_icons")
242 col.separator()
243 col.label(text="Header:")
244 col.prop(self, "show_header")
247 class IV_PT_icons(bpy.types.Panel):
248 bl_space_type = "TEXT_EDITOR"
249 bl_region_type = "UI"
250 bl_label = "Icon Viewer"
252 @staticmethod
253 def tag_redraw():
254 wm = bpy.context.window_manager
255 if not wm:
256 return
258 for w in wm.windows:
259 for a in w.screen.areas:
260 if a.type == 'TEXT_EDITOR':
261 for r in a.regions:
262 if r.type == 'UI':
263 r.tag_redraw()
265 def draw(self, context):
266 pr = prefs()
267 row = self.layout.row(align=True)
268 if pr.show_panel_icons:
269 row.prop(pr, "panel_filter", text="", icon='VIEWZOOM')
270 else:
271 row.operator(IV_OT_icons_show.bl_idname)
272 row.operator(
273 IV_OT_panel_menu_call.bl_idname, text="", icon='COLLAPSEMENU')
275 _, y0 = context.region.view2d.region_to_view(0, 0)
276 _, y1 = context.region.view2d.region_to_view(0, 10)
277 region_scale = 10 / abs(y0 - y1)
279 num_cols = max(
281 (context.region.width - PANEL_PADDING) //
282 math.ceil(ui_scale() * region_scale * ICON_SIZE))
284 col = None
285 if HISTORY and pr.show_history:
286 col = self.layout.column(align=True)
287 pr.panel_icons.draw(col.box(), num_cols, HISTORY)
289 if pr.show_panel_icons:
290 col = col or self.layout.column(align=True)
291 pr.panel_icons.draw(col.box(), num_cols)
293 @classmethod
294 def poll(cls, context):
295 return prefs().show_panel
298 class IV_HT_icons(bpy.types.Header):
299 bl_space_type = 'CONSOLE'
301 def draw(self, context):
302 if not prefs().show_header:
303 return
304 layout = self.layout
305 layout.separator()
306 layout.operator(IV_OT_icons_show.bl_idname)
309 class IV_OT_panel_menu_call(bpy.types.Operator):
310 bl_idname = "iv.panel_menu_call"
311 bl_label = ""
312 bl_description = "Menu"
313 bl_options = {'INTERNAL'}
315 def menu(self, menu, context):
316 pr = prefs()
317 layout = menu.layout
318 layout.prop(pr, "show_panel_icons")
319 layout.prop(pr, "show_history")
321 if not pr.show_panel_icons:
322 return
324 layout.separator()
325 layout.prop(pr, "show_matcap_icons")
326 layout.prop(pr, "show_brush_icons")
327 layout.prop(pr, "show_colorset_icons")
328 layout.prop(pr, "show_event_icons")
330 def execute(self, context):
331 context.window_manager.popup_menu(self.menu, title="Icon Viewer")
332 return {'FINISHED'}
335 class IV_OT_icon_select(bpy.types.Operator):
336 bl_idname = "iv.icon_select"
337 bl_label = ""
338 bl_description = "Select the icon"
339 bl_options = {'INTERNAL'}
341 icon: StringProperty()
342 force_copy_on_select: BoolProperty()
344 def execute(self, context):
345 pr = prefs()
346 pr.popup_icons.selected_icon = self.icon
347 if pr.copy_on_select or self.force_copy_on_select:
348 context.window_manager.clipboard = self.icon
349 self.report({'INFO'}, self.icon)
351 if pr.close_on_select and IV_OT_icons_show.instance:
352 IV_OT_icons_show.instance.close()
354 if pr.show_history:
355 if self.icon in HISTORY:
356 HISTORY.remove(self.icon)
357 if len(HISTORY) >= HISTORY_SIZE:
358 HISTORY.pop(0)
359 HISTORY.append(self.icon)
360 return {'FINISHED'}
363 class IV_OT_icons_show(bpy.types.Operator):
364 bl_idname = "iv.icons_show"
365 bl_label = "Icon Viewer"
366 bl_description = "Icon viewer"
367 bl_property = "filter_auto_focus"
369 instance = None
371 def set_filter(self, value):
372 prefs().popup_icons.filter = value
374 def set_selected_icon(self, value):
375 if IV_OT_icons_show.instance:
376 IV_OT_icons_show.instance.auto_focusable = False
378 filter_auto_focus: StringProperty(
379 description="Filter",
380 get=lambda s: prefs().popup_icons.filter,
381 set=set_filter,
382 options={'TEXTEDIT_UPDATE', 'SKIP_SAVE'})
383 filter: StringProperty(
384 description="Filter",
385 get=lambda s: prefs().popup_icons.filter,
386 set=set_filter,
387 options={'TEXTEDIT_UPDATE'})
388 selected_icon: StringProperty(
389 description="Selected Icon",
390 get=lambda s: prefs().popup_icons.selected_icon,
391 set=set_selected_icon)
393 def get_num_cols(self, num_icons):
394 return round(1.3 * math.sqrt(num_icons))
396 def draw_header(self, layout):
397 pr = prefs()
398 header = layout.box()
399 header = header.split(factor=0.75) if self.selected_icon else \
400 header.row()
401 row = header.row(align=True)
402 row.prop(pr, "show_matcap_icons", text="", icon='SMOOTH')
403 row.prop(pr, "show_brush_icons", text="", icon='BRUSH_DATA')
404 row.prop(pr, "show_colorset_icons", text="", icon='COLOR')
405 row.prop(pr, "show_event_icons", text="", icon='HAND')
406 row.separator()
408 row.prop(
409 pr, "copy_on_select", text="",
410 icon='BORDER_RECT', toggle=True)
411 if pr.copy_on_select:
412 sub = row.row(align=True)
413 if bpy.context.window.screen.name == "temp":
414 sub.alert = True
415 sub.prop(
416 pr, "close_on_select", text="",
417 icon='RESTRICT_SELECT_OFF', toggle=True)
418 row.prop(
419 pr, "auto_focus_filter", text="",
420 icon='OUTLINER_DATA_FONT', toggle=True)
421 row.separator()
423 if self.auto_focusable and pr.auto_focus_filter:
424 row.prop(self, "filter_auto_focus", text="", icon='VIEWZOOM')
425 else:
426 row.prop(self, "filter", text="", icon='VIEWZOOM')
428 if self.selected_icon:
429 row = header.row()
430 row.prop(self, "selected_icon", text="", icon=self.selected_icon)
432 def draw(self, context):
433 pr = prefs()
434 col = self.layout
435 self.draw_header(col)
437 history_num_cols = int(
438 (self.width - POPUP_PADDING) / (ui_scale() * ICON_SIZE))
439 num_cols = min(
440 self.get_num_cols(len(pr.popup_icons.filtered_icons)),
441 history_num_cols)
443 subcol = col.column(align=True)
445 if HISTORY and pr.show_history:
446 pr.popup_icons.draw(subcol.box(), history_num_cols, HISTORY)
448 pr.popup_icons.draw(subcol.box(), num_cols)
450 def close(self):
451 bpy.context.window.screen = bpy.context.window.screen
453 def check(self, context):
454 return True
456 def cancel(self, context):
457 IV_OT_icons_show.instance = None
458 IV_PT_icons.tag_redraw()
460 def execute(self, context):
461 if not IV_OT_icons_show.instance:
462 return {'CANCELLED'}
463 IV_OT_icons_show.instance = None
465 pr = prefs()
466 if self.selected_icon and not pr.copy_on_select:
467 context.window_manager.clipboard = self.selected_icon
468 self.report({'INFO'}, self.selected_icon)
469 pr.popup_icons.selected_icon = ""
471 IV_PT_icons.tag_redraw()
472 return {'FINISHED'}
474 def invoke(self, context, event):
475 pr = prefs()
476 pr.popup_icons.selected_icon = ""
477 pr.popup_icons.filter = ""
478 IV_OT_icons_show.instance = self
479 self.auto_focusable = True
481 num_cols = self.get_num_cols(len(pr.popup_icons.filtered_icons))
482 self.width = min(
483 ui_scale() * (num_cols * ICON_SIZE + POPUP_PADDING),
484 context.window.width - WIN_PADDING)
486 return context.window_manager.invoke_props_dialog(
487 self, width=self.width)
490 classes = [
491 IV_PT_icons,
492 IV_HT_icons,
493 IV_OT_panel_menu_call,
494 IV_OT_icon_select,
495 IV_OT_icons_show,
496 IV_Preferences,
500 def register():
501 if bpy.app.background:
502 return
504 for cls in classes:
505 bpy.utils.register_class(cls)
508 def unregister():
509 if bpy.app.background:
510 return
512 for cls in classes:
513 bpy.utils.unregister_class(cls)