keymap editor
authorMartin Poirier <theeth@yahoo.com>
Thu, 17 Dec 2009 03:32:33 +0000 (03:32 +0000)
committerMartin Poirier <theeth@yahoo.com>
Thu, 17 Dec 2009 03:32:33 +0000 (03:32 +0000)
New unique ID per keymap item (unique inside their keymap) for default and configuration keymaps.
This allows restoring a single user defined kmi to its previous (default or config) values instead of having to restore the whole keymap.
The restore item button is disabled for kmi added by the users (they don't have an ID).

Also fixes a bug in the rna function for add keymap item (parameter order was incorrect, messing adding back saved configurations).

release/scripts/ui/space_userpref.py
source/blender/makesdna/DNA_windowmanager_types.h
source/blender/makesrna/intern/rna_wm.c
source/blender/makesrna/intern/rna_wm_api.c
source/blender/windowmanager/WM_api.h
source/blender/windowmanager/intern/wm_keymap.c

index ed4d02cd91984615f11b15a828dbe18bb671af2b..d50c38f0e201414e0ad39e00e2d39c5b4d89c260 100644 (file)
@@ -1365,6 +1365,7 @@ class USERPREF_PT_input(bpy.types.Panel):
         else:
             row.label()
 
+        row.operator("wm.keyitem_restore", text="", icon='BACK')
         row.operator("wm.keyitem_remove", text="", icon='X')
 
         # Expanded, additional event settings
@@ -1666,7 +1667,25 @@ class WM_OT_keymap_restore(bpy.types.Operator):
 
         return ('FINISHED',)
 
+class WM_OT_keyitem_restore(bpy.types.Operator):
+    "Restore key map item."
+    bl_idname = "wm.keyitem_restore"
+    bl_label = "Restore Key Map Item"
 
+    def poll(self, context):
+        kmi = context.keyitem
+        km = context.keymap
+        return km and kmi and kmi.id != 0
+
+    def execute(self, context):
+        wm = context.manager
+        kmi = context.keyitem
+        km = context.keymap
+
+        km.restore_item_to_default(kmi)
+
+        return ('FINISHED',)
+    
 class WM_OT_keyitem_add(bpy.types.Operator):
     "Add key map item."
     bl_idname = "wm.keyitem_add"
@@ -1699,4 +1718,4 @@ bpy.ops.add(WM_OT_keymap_edit)
 bpy.ops.add(WM_OT_keymap_restore)
 bpy.ops.add(WM_OT_keyitem_add)
 bpy.ops.add(WM_OT_keyitem_remove)
-
+bpy.ops.add(WM_OT_keyitem_restore)
index c17ae1c53c8890c06c0d3640a811cc6d45ef5735..9e47a6f1d89b7a13d9b6e3cd75048ea70f73474c 100644 (file)
@@ -262,7 +262,9 @@ typedef struct wmKeyMapItem {
        short flag;
 
        /* runtime */
-       short maptype, pad[2];                  /* keymap editor */
+       short maptype;                                  /* keymap editor */
+       short id;                                               /* unique identifier */
+       short pad;
        struct PointerRNA *ptr;                 /* rna pointer to access properties */
 } wmKeyMapItem;
 
@@ -281,7 +283,7 @@ typedef struct wmKeyMap {
        short regionid;         /* see above */
        
        short flag;                     /* general flags */
-       short pad;
+       short kmi_id;           /* last kmi id */
        
        /* runtime */
        int (*poll)(struct bContext *); /* verify if enabled in the current context */
index 7809c3a8c347382cd37e3e68e249f76acebaf3c9..09adb642e583c2a5fa57d388b56f8521b84e9b10 100644 (file)
@@ -944,6 +944,11 @@ static void rna_def_keyconfig(BlenderRNA *brna)
        RNA_def_property_enum_funcs(prop, NULL, NULL, "rna_KeyMapItem_value_itemf");
        RNA_def_property_ui_text(prop, "Value", "");
 
+       prop= RNA_def_property(srna, "id", PROP_INT, PROP_NONE);
+       RNA_def_property_int_sdna(prop, NULL, "id");
+       RNA_def_property_clear_flag(prop, PROP_EDITABLE);
+       RNA_def_property_ui_text(prop, "id", "ID of the item.");
+
        prop= RNA_def_property(srna, "any", PROP_BOOLEAN, PROP_NONE);
        RNA_def_property_boolean_funcs(prop, "rna_KeyMapItem_any_getf", "rna_KeyMapItem_any_setf");
        RNA_def_property_ui_text(prop, "Any", "Any modifier keys pressed.");
index 47d15c875960d7bba94a2842d7b85802ba5d96a4..94a689c9f1cac8a4ec0810d8415bd78d5029105d 100644 (file)
@@ -113,7 +113,12 @@ static wmKeyMapItem *rna_KeyMap_add_modal_item(wmKeyMap *km, bContext *C, Report
        return WM_modalkeymap_add_item(km, type, value, modifier, keymodifier, propvalue);
 }
 
-static wmKeyMapItem *rna_KeyMap_add_item(wmKeyMap *km, ReportList *reports, char *idname, int type, int any, int value, int shift, int ctrl, int alt, int oskey, int keymodifier)
+static void rna_keymap_restore_item_to_default(wmKeyMap *km, bContext *C, wmKeyMapItem *kmi)
+{
+       WM_keymap_restore_item_to_default(C, km, kmi);
+}
+
+static wmKeyMapItem *rna_KeyMap_add_item(wmKeyMap *km, ReportList *reports, char *idname, int type, int value, int any, int shift, int ctrl, int alt, int oskey, int keymodifier)
 {
 //     wmWindowManager *wm = CTX_wm_manager(C);
        int modifier= 0;
@@ -276,6 +281,11 @@ void RNA_api_keymap(StructRNA *srna)
        RNA_def_function_return(func, parm);
 
        func= RNA_def_function(srna, "restore_to_default", "WM_keymap_restore_to_default");
+
+       func= RNA_def_function(srna, "restore_item_to_default", "rna_keymap_restore_item_to_default");
+       RNA_def_function_flag(func, FUNC_USE_CONTEXT);
+       parm= RNA_def_pointer(func, "item", "KeyMapItem", "Item", "");
+       RNA_def_property_flag(parm, PROP_REQUIRED);
 }
 
 #endif
index 66ede3f6ba613168aab3109f1fdfc10a33aeb090..e90360fde456b851349c596d75cd64236047bfcd 100644 (file)
@@ -114,6 +114,7 @@ int                  WM_keymap_user_init(struct wmWindowManager *wm, struct wmKeyMap *keymap);
 wmKeyMap       *WM_keymap_copy_to_user(struct wmKeyMap *keymap);
 void           WM_keymap_restore_to_default(struct wmKeyMap *keymap);
 void           WM_keymap_properties_reset(struct wmKeyMapItem *kmi);
+void           WM_keymap_restore_item_to_default(struct bContext *C, struct wmKeyMap *keymap, struct wmKeyMapItem *kmi);
 
 wmKeyMap       *WM_modalkeymap_add(struct wmKeyConfig *keyconf, char *idname, struct EnumPropertyItem *items);
 wmKeyMap       *WM_modalkeymap_get(struct wmKeyConfig *keyconf, char *idname);
index fe5b42a1841721ce1f46bd0b57e7d8d5687e009a..e1f812ee45fa3019428dd3f92867036bc250e529 100644 (file)
@@ -201,6 +201,12 @@ wmKeyMapItem *WM_keymap_add_item(wmKeyMap *keymap, char *idname, int type, int v
 
        keymap_event_set(kmi, type, val, modifier, keymodifier);
        keymap_properties_set(kmi);
+
+       if ((keymap->flag & KEYMAP_USER) == 0) {
+               keymap->kmi_id++;
+               kmi->id = keymap->kmi_id;
+       }
+
        return kmi;
 }
 
@@ -291,6 +297,11 @@ wmKeyMapItem *WM_modalkeymap_add_item(wmKeyMap *km, int type, int val, int modif
        
        keymap_event_set(kmi, type, val, modifier, keymodifier);
 
+       if ((km->flag & KEYMAP_USER) == 0) {
+               km->kmi_id++;
+               kmi->id = km->kmi_id;
+       }
+
        return kmi;
 }
 
@@ -545,6 +556,55 @@ wmKeyMap *WM_keymap_copy_to_user(wmKeyMap *keymap)
        return usermap;
 }
 
+void WM_keymap_restore_item_to_default(bContext *C, wmKeyMap *keymap, wmKeyMapItem *kmi)
+{
+       wmWindowManager *wm = CTX_wm_manager(C);
+       wmKeyConfig *keyconf;
+       wmKeyMap *km = NULL;
+
+       /* look in user key config */
+       keyconf= wm_keyconfig_list_find(&wm->keyconfigs, U.keyconfigstr);
+       if(keyconf) {
+               km= WM_keymap_list_find(&keyconf->keymaps, keymap->idname, keymap->spaceid, keymap->regionid);
+       }
+
+       if (!km) {
+               /* or from default */
+               km= WM_keymap_list_find(&wm->defaultconf->keymaps, keymap->idname, keymap->spaceid, keymap->regionid);
+       }
+
+       if (km) {
+               wmKeyMapItem *orig;
+
+               for (orig = km->items.first; orig; orig = orig->next) {
+                       if (orig->id == kmi->id)
+                               break;
+               }
+
+               if (orig) {
+                       if(strcmp(orig->idname, kmi->idname) != 0) {
+                               BLI_strncpy(kmi->idname, orig->idname, sizeof(kmi->idname));
+
+                               WM_keymap_properties_reset(kmi);
+                       }
+                       kmi->properties= IDP_CopyProperty(orig->properties);
+                       kmi->ptr->data= kmi->properties;
+
+                       kmi->propvalue = orig->propvalue;
+                       kmi->type = orig->type;
+                       kmi->val = orig->val;
+                       kmi->shift = orig->shift;
+                       kmi->ctrl = orig->ctrl;
+                       kmi->alt = orig->alt;
+                       kmi->oskey = orig->oskey;
+                       kmi->keymodifier = orig->keymodifier;
+                       kmi->maptype = orig->maptype;
+
+               }
+
+       }
+}
+
 void WM_keymap_restore_to_default(wmKeyMap *keymap)
 {
        wmKeyMap *usermap;