[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [15582] branches/soc-2008-quorn: Any script can now register a unique key combination as part of its bpy header .

Ian Thompson quornian at googlemail.com
Tue Jul 15 09:04:31 CEST 2008


Revision: 15582
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=15582
Author:   quorn
Date:     2008-07-15 09:04:31 +0200 (Tue, 15 Jul 2008)

Log Message:
-----------
Any script can now register a unique key combination as part of its bpy header. For a supported space type, the user may press this shortcut to invoke the script.

Space types that are to support shortcuts like this should call BPY_menu_do_shortcut(...) from the event queue read method (See winqreadtextspace in drawtext.c for example)

Modified Paths:
--------------
    branches/soc-2008-quorn/release/scripts/textplugin_suggest.py
    branches/soc-2008-quorn/source/blender/include/BIF_keyval.h
    branches/soc-2008-quorn/source/blender/python/BPY_extern.h
    branches/soc-2008-quorn/source/blender/python/BPY_interface.c
    branches/soc-2008-quorn/source/blender/python/BPY_menus.c
    branches/soc-2008-quorn/source/blender/python/BPY_menus.h
    branches/soc-2008-quorn/source/blender/src/drawtext.c
    branches/soc-2008-quorn/source/blender/src/keyval.c

Modified: branches/soc-2008-quorn/release/scripts/textplugin_suggest.py
===================================================================
--- branches/soc-2008-quorn/release/scripts/textplugin_suggest.py	2008-07-15 05:33:12 UTC (rev 15581)
+++ branches/soc-2008-quorn/release/scripts/textplugin_suggest.py	2008-07-15 07:04:31 UTC (rev 15582)
@@ -3,6 +3,7 @@
 Name: 'Suggest'
 Blender: 243
 Group: 'TextPlugin'
+Shortcut: 'Ctrl+Space'
 Tooltip: 'Suggests completions for the word at the cursor in a python script'
 """
 
@@ -72,6 +73,70 @@
 	return line[c-a:c].split('.')
 
 
+def getImports(txt):
+	imports = []
+	
+	# Unfortunately, tokenize may fail if the script leaves brackets or strings
+	# open. For now we return an empty list until I have a better idea. Maybe
+	# parse manually.
+	try:
+		tokens = getTokens(txt)
+	except:
+		return []
+	
+	for i in range(1, len(tokens)):
+		
+		# Handle all import statements
+		if tokens[i-1][TK_TOKEN] == 'import':
+			
+			# Find 'from' if it exists
+			fr = -1
+			for a in range(1, i):
+				if tokens[i-a][TK_TYPE] == token.NEWLINE: break
+				if tokens[i-a][TK_TOKEN] == 'from':
+					fr = i-a
+					break
+			
+			# Handle: import ___[.___][,___[.___]]
+			if fr<0:
+				parent = ''
+			
+			# Handle: from ___[.___] import ___[,___]
+			else: # fr>=0:
+				parent = ''.join([t[TK_TOKEN] for t in tokens[fr+1:i-1]])
+			
+			module = ''
+			while i < len(tokens)-1:
+				if tokens[i][TK_TYPE] == token.NAME:
+				
+					# Get the module name
+					module = module + tokens[i][TK_TOKEN]
+					
+					if tokens[i+1][TK_TOKEN] == '.':
+						module += '.'
+						i += 1
+					else:
+						# Add the module name and parent to the dict
+						imports.append((module, parent))
+						module = ''
+					
+				elif tokens[i][TK_TOKEN]!=',':
+					break
+				
+				i += 1
+				
+	# Process imports for: from ___ import *
+	for imp,frm in imports:
+		print imp, frm
+		if frm == '':
+			try: __import__(imp)
+			except: print '^ERR^'
+		else:
+			try: __import__(frm, globals(), locals(), [imp])
+			except: print '^ERR^'
+	
+
+
 # Returns a list of tuples of symbol names and their types (name, type) where
 # type is one of:
 #   m (module/class)  Has its own members (includes classes)

Modified: branches/soc-2008-quorn/source/blender/include/BIF_keyval.h
===================================================================
--- branches/soc-2008-quorn/source/blender/include/BIF_keyval.h	2008-07-15 05:33:12 UTC (rev 15581)
+++ branches/soc-2008-quorn/source/blender/include/BIF_keyval.h	2008-07-15 07:04:31 UTC (rev 15582)
@@ -31,6 +31,9 @@
 #define BIF_KEYVAL_H
 
 char *key_event_to_string(unsigned short event);
+int decode_key_string(char *str, unsigned short *key, unsigned short *qual);
 
 #endif
 
+
+

Modified: branches/soc-2008-quorn/source/blender/python/BPY_extern.h
===================================================================
--- branches/soc-2008-quorn/source/blender/python/BPY_extern.h	2008-07-15 05:33:12 UTC (rev 15581)
+++ branches/soc-2008-quorn/source/blender/python/BPY_extern.h	2008-07-15 07:04:31 UTC (rev 15582)
@@ -91,6 +91,8 @@
 
 	int BPY_txt_do_python_Text( struct Text *text );
 	int BPY_menu_do_python( short menutype, int event );
+	int BPY_menu_do_shortcut( short menutype, unsigned short key, unsigned short modifiers );
+	int BPY_menu_invoke( struct BPyMenu *pym, short menutype );
 	void BPY_run_python_script( char *filename );
 	int BPY_run_script(struct Script *script);
 	void BPY_free_compiled_text( struct Text *text );

Modified: branches/soc-2008-quorn/source/blender/python/BPY_interface.c
===================================================================
--- branches/soc-2008-quorn/source/blender/python/BPY_interface.c	2008-07-15 05:33:12 UTC (rev 15581)
+++ branches/soc-2008-quorn/source/blender/python/BPY_interface.c	2008-07-15 07:04:31 UTC (rev 15582)
@@ -972,8 +972,38 @@
 *****************************************************************************/
 int BPY_menu_do_python( short menutype, int event )
 {
+	BPyMenu *pym;
+	pym = BPyMenu_GetEntry( menutype, ( short ) event );
+	return BPY_menu_invoke( pym, menutype );
+}
+	
+/****************************************************************************
+* Description: This function executes the script by its shortcut.
+* Notes:	It is called by the ui code in src/???.c when a user presses an
+*		unassigned key combination. Scripts are searched in the BPyMenuTable,
+*		using the given menutype and event values to know which one to invoke.
+*****************************************************************************/
+int BPY_menu_do_shortcut( short menutype, unsigned short key, unsigned short qual )
+{
+	BPyMenu *pym;
+	pym = BPyMenu_GetEntry( menutype, 0 );
+
+	while ( pym ) {
+		if ( pym->key && pym->key == key && pym->qual == qual ) {
+			return BPY_menu_invoke( pym, menutype );
+		}
+		pym = pym->next;
+	}
+	
+	return 0;
+}
+
+/****************************************************************************
+* Description: This function executes the script described by a menu item.
+*****************************************************************************/
+int BPY_menu_invoke( BPyMenu *pym, short menutype )
+{
 	char *argstr = NULL;
-	BPyMenu *pym;
 	BPySubMenu *pysm;
 	char scriptname[21];
 	Script *script = NULL;
@@ -981,8 +1011,6 @@
 	PyGILState_STATE gilstate;
 	char filestr[FILE_MAX];
 
-	pym = BPyMenu_GetEntry( menutype, ( short ) event );
-
 	if( !pym )
 		return 0;
 

Modified: branches/soc-2008-quorn/source/blender/python/BPY_menus.c
===================================================================
--- branches/soc-2008-quorn/source/blender/python/BPY_menus.c	2008-07-15 05:33:12 UTC (rev 15581)
+++ branches/soc-2008-quorn/source/blender/python/BPY_menus.c	2008-07-15 07:04:31 UTC (rev 15582)
@@ -42,6 +42,7 @@
 #endif
 #include "BKE_global.h"
 #include "BKE_utildefines.h"
+#include "BIF_keyval.h"
 #include "BLI_blenlib.h"
 #include "MEM_guardedalloc.h"
 #include "DNA_userdef_types.h"	/* for U.pythondir */
@@ -333,6 +334,23 @@
 	return;
 }
 
+static void bpymenu_set_shortcut( BPyMenu * pymenu, char *combi )
+{
+	unsigned short key, qual;
+
+	if( !pymenu )
+		return;
+
+	if (!decode_key_string(combi, &key, &qual)) {
+		return; /* TODO: Print some error */
+	}
+
+	pymenu->key = key;
+	pymenu->qual = qual;
+
+	return;
+}
+
 /* bpymenu_AddEntry:
  * try to find an existing pymenu entry with the given type and name;
  * if found, update it with new info, otherwise create a new one and fill it.
@@ -693,6 +711,7 @@
  * # Blender: <code>short int</code> (minimal Blender version)
  * # Group: 'group name' (defines menu)
  * # Submenu: 'submenu name' related_1word_arg
+ * # Shortcut: Modifier+Key (optional shortcut combination for supported groups)
  * # Tooltip: 'tooltip for the menu'
  * # \"\"\"
  *
@@ -801,13 +820,19 @@
 					if ((matches == 3) && (strstr(head, "Submenu:") != NULL)) {
 						bpymenu_AddSubEntry(scriptMenu, middle, tail);
 					} else {
-						/* Tooltip: 'tooltip for the menu */
+						/* Shortcut: 'key+combination' */
 						matches = sscanf(line, "%[^']'%[^']'%c", head, middle, tail);
-						if ((matches == 3) && ((strstr(head, "Tooltip:") != NULL) ||
-							(strstr(head, "Tip:") != NULL))) {
-							bpymenu_set_tooltip(scriptMenu, middle);
+						if ((matches == 3) && (strstr(head, "Shortcut:") != NULL)) {
+							bpymenu_set_shortcut(scriptMenu, middle);
+						} else {
+							/* Tooltip: 'tooltip for the menu */
+							matches = sscanf(line, "%[^']'%[^']'%c", head, middle, tail);
+							if ((matches == 3) && ((strstr(head, "Tooltip:") != NULL) ||
+								(strstr(head, "Tip:") != NULL))) {
+								bpymenu_set_tooltip(scriptMenu, middle);
+							}
+							parser_state = 0;
 						}
-						parser_state = 0;
 					}
 					break;
 

Modified: branches/soc-2008-quorn/source/blender/python/BPY_menus.h
===================================================================
--- branches/soc-2008-quorn/source/blender/python/BPY_menus.h	2008-07-15 05:33:12 UTC (rev 15581)
+++ branches/soc-2008-quorn/source/blender/python/BPY_menus.h	2008-07-15 07:04:31 UTC (rev 15582)
@@ -59,6 +59,7 @@
 	char *name;
 	char *filename;
 	char *tooltip;
+	unsigned short key, qual;	/* Registered shortcut key */
 	short version;		/* Blender version */
 	int dir;		/* 0: default, 1: U.pythondir */
 	struct BPySubMenu *submenus;

Modified: branches/soc-2008-quorn/source/blender/src/drawtext.c
===================================================================
--- branches/soc-2008-quorn/source/blender/src/drawtext.c	2008-07-15 05:33:12 UTC (rev 15581)
+++ branches/soc-2008-quorn/source/blender/src/drawtext.c	2008-07-15 07:04:31 UTC (rev 15582)
@@ -81,6 +81,7 @@
 #include "BSE_filesel.h"
 
 #include "BPY_extern.h"
+#include "BPY_menus.h"
 
 #include "mydevice.h"
 #include "blendef.h" 
@@ -1833,10 +1834,17 @@
 			if (st->showsyntax) get_format_string(st);
 			pop_space_text(st);
 			do_draw= 1;
-			do_suggest= 1;
+			if (suggesting && ispunct(ascii)) {
+				confirm_suggestion(text);
+				if (st->showsyntax) get_format_string(st);
+				do_suggest= 0;
+			} else {
+				do_suggest= 1;
+			}
 		}
 	} else if (val) {
-		do_suggest= -1;
+		do_suggest= -1; /* Note that the default label sets this to 0,
+						so -1 only applies to the explicit cases below */
 		switch (event) {
 		case AKEY:
 			if (G.qual & LR_ALTKEY) {
@@ -2102,6 +2110,12 @@
 		case ESCKEY:
 			do_suggest= -1;
 			break;
+		case SPACEKEY:
+			if (suggesting) {
+				confirm_suggestion(text);
+				if (st->showsyntax) get_format_string(st);
+			}
+			break;
 		case TABKEY:
 			if (text && text->id.lib) {
 				error_libdata();
@@ -2300,6 +2314,12 @@
 		}
 	}
 
+	if (event && val) {
+		if (BPY_menu_do_shortcut(PYMENU_TEXTPLUGIN, event, G.qual)) {
+			do_draw= 1;
+		}
+	}
+
 	if (last_check_time < PIL_check_seconds_timer() - 1.0) {
 		switch (txt_file_modified(text)) {
 		case 1:

Modified: branches/soc-2008-quorn/source/blender/src/keyval.c
===================================================================
--- branches/soc-2008-quorn/source/blender/src/keyval.c	2008-07-15 05:33:12 UTC (rev 15581)
+++ branches/soc-2008-quorn/source/blender/src/keyval.c	2008-07-15 07:04:31 UTC (rev 15582)
@@ -27,6 +27,11 @@
  * ***** END GPL LICENSE BLOCK *****
  */
 
+#include "stdio.h"
+#include "ctype.h"
+#include "string.h"
+
+#include "BKE_global.h"
 #include "BLI_blenlib.h"
 #include "BLI_arithb.h"
 #include "BIF_keyval.h"
@@ -349,3 +354,193 @@
 	
 	return "";
 }
+
+/* 
+ * Decodes key combination strings [qual1+[qual2+[...]]]keyname

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list