ImportHelper: Common methods for FileHandler drag and drop support #119774

Merged
Jesse Yurkovich merged 2 commits from deadpin/blender:bpy_extras into main 2024-03-29 01:23:02 +01:00
1 changed files with 23 additions and 0 deletions
Showing only changes of commit 22e203e9cc - Show all commits

View File

@ -102,6 +102,16 @@ class ImportHelper:
context.window_manager.fileselect_add(self)
return {'RUNNING_MODAL'}
def invoke_popup(self, context, confirm_text="Ok"):
deadpin marked this conversation as resolved Outdated

I think it would be better to follow the c++ implementation and use bl_label as confirmation text by default rather than “Ok”:

    def invoke_popup(self, context, confirm_text=""):
        if not confirm_text:
            confirm_text = self.bl_label
I think it would be better to follow the c++ implementation and use `bl_label` as confirmation text by default rather than “Ok”: ```python def invoke_popup(self, context, confirm_text=""): if not confirm_text: confirm_text = self.bl_label ```
if self.properties.is_property_set("filepath"):
title = self.filepath
if len(self.files) > 1:
title = "Import %d files" % len(self.files)
deadpin marked this conversation as resolved Outdated

This should be translated:

@@ -23,7 +23,10 @@ from bpy.props import (
     EnumProperty,
     StringProperty,
 )
-from bpy.app.translations import pgettext_data as data_
+from bpy.app.translations import (
+    pgettext_iface as iface_,
+    pgettext_data as data_,
+    )
 
 
 def _check_axis_conversion(op):
@@ -106,9 +109,11 @@ class ImportHelper:
         if self.properties.is_property_set("filepath"):
             title = self.filepath
             if len(self.files) > 1:
-                title = "Import %d files" % len(self.files)
-            context.window_manager.invoke_props_dialog(self, confirm_text=confirm_text, title=title)
-            return {'RUNNING_MODAL'}
+                title = iface_("Import {} files").format(len(self.files))
+            confirm_text = iface_(confirm_text)
+            return context.window_manager.invoke_props_dialog(
+                self, confirm_text=confirm_text, title=title, translate=False
+            )
         context.window_manager.fileselect_add(self)
         return {'RUNNING_MODAL'}

I’m using str.format() simply to reuse the existing translation message from the c++ implementation.

This should be translated: ```diff @@ -23,7 +23,10 @@ from bpy.props import ( EnumProperty, StringProperty, ) -from bpy.app.translations import pgettext_data as data_ +from bpy.app.translations import ( + pgettext_iface as iface_, + pgettext_data as data_, + ) def _check_axis_conversion(op): @@ -106,9 +109,11 @@ class ImportHelper: if self.properties.is_property_set("filepath"): title = self.filepath if len(self.files) > 1: - title = "Import %d files" % len(self.files) - context.window_manager.invoke_props_dialog(self, confirm_text=confirm_text, title=title) - return {'RUNNING_MODAL'} + title = iface_("Import {} files").format(len(self.files)) + confirm_text = iface_(confirm_text) + return context.window_manager.invoke_props_dialog( + self, confirm_text=confirm_text, title=title, translate=False + ) context.window_manager.fileselect_add(self) return {'RUNNING_MODAL'} ``` I’m using `str.format()` simply to reuse the existing translation message from the c++ implementation.
context.window_manager.invoke_props_dialog(self, confirm_text=confirm_text, title=title)
return {'RUNNING_MODAL'}
context.window_manager.fileselect_add(self)
return {'RUNNING_MODAL'}
def check(self, _context):
return _check_axis_conversion(self)
@ -394,6 +404,19 @@ def unpack_face_list(list_of_tuples):
return flat_ls
def poll_file_object_drop(context):
"""
A default implementation for FileHandler poll_drop methods. Allows for both the 3D Viewport and
the Outliner (in ViewLayer display mode) to be targets for file drag and drop.
"""
area = context.area
if not area:
return False
is_v3d = area.type == 'VIEW_3D'
is_outliner_view_layer = area.type == 'OUTLINER' and area.spaces.active.display_mode == 'VIEW_LAYER'
return is_v3d or is_outliner_view_layer
path_reference_mode = EnumProperty(
name="Path Mode",
description="Method used to reference paths",