[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [41210] trunk/blender: Context menu ' Edit Source' operator no longer needs to be enabled as a build option, improved this so the python file: line lookups are only done when the operator runs ( previously this was done for every button, every draw when the build option was enabled).

Campbell Barton ideasman42 at gmail.com
Sun Oct 23 06:13:59 CEST 2011


Revision: 41210
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=41210
Author:   campbellbarton
Date:     2011-10-23 04:13:56 +0000 (Sun, 23 Oct 2011)
Log Message:
-----------
Context menu 'Edit Source' operator no longer needs to be enabled as a build option, improved this so the python file:line lookups are only done when the operator runs (previously this was done for every button, every draw when the build option was enabled).

Perhaps this should be hidden when not running with --debug, easy to change. 

Modified Paths:
--------------
    trunk/blender/CMakeLists.txt
    trunk/blender/source/blender/editors/include/UI_interface.h
    trunk/blender/source/blender/editors/interface/interface.c
    trunk/blender/source/blender/editors/interface/interface_handlers.c
    trunk/blender/source/blender/editors/interface/interface_intern.h
    trunk/blender/source/blender/editors/interface/interface_ops.c
    trunk/blender/source/blender/windowmanager/intern/wm_operators.c

Modified: trunk/blender/CMakeLists.txt
===================================================================
--- trunk/blender/CMakeLists.txt	2011-10-23 01:06:38 UTC (rev 41209)
+++ trunk/blender/CMakeLists.txt	2011-10-23 04:13:56 UTC (rev 41210)
@@ -213,10 +213,7 @@
 option(WITH_ASSERT_ABORT "Call abort() when raising an assertion through BLI_assert()" OFF)
 mark_as_advanced(WITH_ASSERT_ABORT)
 
-option(WITH_PYTHON_UI_INFO "Allow navigating to UI source from the context menu" OFF)
-mark_as_advanced(WITH_PYTHON_UI_INFO)
 
-
 if(APPLE)
 	if(NOT CMAKE_OSX_ARCHITECTURES)
 		set(CMAKE_OSX_ARCHITECTURES x86_64 CACHE STRING
@@ -1330,10 +1327,6 @@
 	add_definitions(-DWITH_ASSERT_ABORT)
 endif()
 
-if(WITH_PYTHON_UI_INFO)
-	add_definitions(-DWITH_PYTHON_UI_INFO)
-endif()
-
 # message(STATUS "Using CFLAGS: ${CMAKE_C_FLAGS}")
 # message(STATUS "Using CXXFLAGS: ${CMAKE_CXX_FLAGS}")
 

Modified: trunk/blender/source/blender/editors/include/UI_interface.h
===================================================================
--- trunk/blender/source/blender/editors/include/UI_interface.h	2011-10-23 01:06:38 UTC (rev 41209)
+++ trunk/blender/source/blender/editors/include/UI_interface.h	2011-10-23 04:13:56 UTC (rev 41210)
@@ -787,6 +787,7 @@
 void UI_buttons_operatortypes(void);
 
 /* Helpers for Operators */
+uiBut *uiContextActiveButton(const struct bContext *C);
 void uiContextActiveProperty(const struct bContext *C, struct PointerRNA *ptr, struct PropertyRNA **prop, int *index);
 void uiContextActivePropertyHandle(struct bContext *C);
 void uiContextAnimUpdate(const struct bContext *C);
@@ -817,5 +818,9 @@
 #define IFACE_(msgid) UI_translate_do_iface(msgid)
 #define TIP_(msgid) UI_translate_do_tooltip(msgid)
 
+/* UI_OT_editsource helpers */
+int  UI_editsource_enable_check(void);
+void UI_editsource_active_but_test(uiBut *but);
+
 #endif /*  UI_INTERFACE_H */
 

Modified: trunk/blender/source/blender/editors/interface/interface.c
===================================================================
--- trunk/blender/source/blender/editors/interface/interface.c	2011-10-23 01:06:38 UTC (rev 41209)
+++ trunk/blender/source/blender/editors/interface/interface.c	2011-10-23 04:13:56 UTC (rev 41210)
@@ -2562,23 +2562,10 @@
 	if(block->curlayout)
 		ui_layout_add_but(block->curlayout, but);
 
-#ifdef WITH_PYTHON_UI_INFO
-	{
-		extern void PyC_FileAndNum_Safe(const char **filename, int *lineno);
-
-		const char *fn;
-		int lineno= -1;
-		PyC_FileAndNum_Safe(&fn, &lineno);
-		if (lineno != -1) {
-			BLI_strncpy(but->py_dbg_fn, fn, sizeof(but->py_dbg_fn));
-			but->py_dbg_ln= lineno;
-		}
-		else {
-			but->py_dbg_fn[0]= '\0';
-			but->py_dbg_ln= -1;
-		}
+	/* if the 'UI_OT_editsource' is running, extract the source info from the button  */
+	if (UI_editsource_enable_check()) {
+		UI_editsource_active_but_test(but);
 	}
-#endif /* WITH_PYTHON_UI_INFO */
 
 	return but;
 }

Modified: trunk/blender/source/blender/editors/interface/interface_handlers.c
===================================================================
--- trunk/blender/source/blender/editors/interface/interface_handlers.c	2011-10-23 01:06:38 UTC (rev 41209)
+++ trunk/blender/source/blender/editors/interface/interface_handlers.c	2011-10-23 04:13:56 UTC (rev 41210)
@@ -4443,17 +4443,9 @@
 		}
 	}
 
-#ifdef WITH_PYTHON_UI_INFO
-	if (but->py_dbg_ln != -1) {
-		PointerRNA ptr_props;
+	/* perhaps we should move this into (G.f & G_DEBUG) - campbell */
+	uiItemFullO(layout, "UI_OT_editsource", "Edit Source", ICON_NONE, NULL, WM_OP_INVOKE_DEFAULT, 0);
 
-		WM_operator_properties_create(&ptr_props, "WM_OT_text_edit");
-		RNA_string_set(&ptr_props, "filepath", but->py_dbg_fn);
-		RNA_int_set(&ptr_props, "line", but->py_dbg_ln);
-		uiItemFullO(layout, "WM_OT_text_edit", "Edit Source", ICON_NONE, ptr_props.data, WM_OP_EXEC_DEFAULT, 0);
-	}
-#endif /* WITH_PYTHON_UI_INFO */
-
 	uiPupMenuEnd(C, pup);
 
 	return 1;
@@ -5146,9 +5138,10 @@
 	}
 }
 
-static uiBut *ui_context_rna_button_active(const bContext *C)
+/* returns the active button with an optional checking function */
+static uiBut *ui_context_button_active(const bContext *C, int (*but_check_cb)(uiBut *))
 {
-	uiBut *rnabut= NULL;
+	uiBut *but_found= NULL;
 
 	ARegion *ar= CTX_wm_region(C);
 
@@ -5166,28 +5159,42 @@
 			}
 		}
 
-		if(activebut && activebut->rnapoin.data) {
+		if(activebut && (but_check_cb == NULL || but_check_cb(activebut))) {
 			uiHandleButtonData *data= activebut->active;
 
-			rnabut= activebut;
+			but_found= activebut;
 
 			/* recurse into opened menu, like colorpicker case */
 			if(data && data->menu && (ar != data->menu->region)) {
 				ar = data->menu->region;
 			}
 			else {
-				return rnabut;
+				return but_found;
 			}
 		}
 		else {
 			/* no active button */
-			return rnabut;
+			return but_found;
 		}
 	}
 
-	return rnabut;
+	return but_found;
 }
 
+static int ui_context_rna_button_active_test(uiBut *but)
+{
+	return (but->rnapoin.data != NULL);
+}
+static uiBut *ui_context_rna_button_active(const bContext *C)
+{
+	return ui_context_button_active(C, ui_context_rna_button_active_test);
+}
+
+uiBut *uiContextActiveButton(const struct bContext *C)
+{
+	return ui_context_button_active(C, NULL);
+}
+
 /* helper function for insert keyframe, reset to default, etc operators */
 void uiContextActiveProperty(const bContext *C, struct PointerRNA *ptr, struct PropertyRNA **prop, int *index)
 {

Modified: trunk/blender/source/blender/editors/interface/interface_intern.h
===================================================================
--- trunk/blender/source/blender/editors/interface/interface_intern.h	2011-10-23 01:06:38 UTC (rev 41209)
+++ trunk/blender/source/blender/editors/interface/interface_intern.h	2011-10-23 04:13:56 UTC (rev 41210)
@@ -252,11 +252,6 @@
 	
 		/* pointer back */
 	uiBlock *block;
-
-#ifdef WITH_PYTHON_UI_INFO
-	char py_dbg_fn[240];
-	int py_dbg_ln;
-#endif
 };
 
 struct uiBlock {

Modified: trunk/blender/source/blender/editors/interface/interface_ops.c
===================================================================
--- trunk/blender/source/blender/editors/interface/interface_ops.c	2011-10-23 01:06:38 UTC (rev 41209)
+++ trunk/blender/source/blender/editors/interface/interface_ops.c	2011-10-23 04:13:56 UTC (rev 41210)
@@ -61,6 +61,10 @@
 #include "WM_api.h"
 #include "WM_types.h"
 
+/* only for UI_OT_editsource */
+#include "ED_screen.h"
+#include "BKE_main.h"
+#include "BLI_ghash.h"
 
 
 /* ********************************************************** */
@@ -474,6 +478,240 @@
 	ot->exec= reports_to_text_exec;
 }
 
+
+/* ------------------------------------------------------------------------- */
+/* EditSource Utility funcs and operator,
+ * note, this includes itility functions and button matching checks */
+
+struct uiEditSourceStore {
+	uiBut but_orig;
+	GHash *hash;
+} uiEditSourceStore;
+
+struct uiEditSourceButStore {
+	char py_dbg_fn[240];
+	int py_dbg_ln;
+} uiEditSourceButStore;
+
+/* should only ever be set while the edit source operator is running */
+struct uiEditSourceStore *ui_editsource_info= NULL;
+
+int  UI_editsource_enable_check(void)
+{
+	return (ui_editsource_info != NULL);
+}
+
+static void ui_editsource_active_but_set(uiBut *but)
+{
+	BLI_assert(ui_editsource_info == NULL);
+
+	ui_editsource_info= MEM_callocN(sizeof(uiEditSourceStore), __func__);
+	memcpy(&ui_editsource_info->but_orig, but, sizeof(uiBut));
+
+	ui_editsource_info->hash = BLI_ghash_new(BLI_ghashutil_ptrhash,
+	                                         BLI_ghashutil_ptrcmp,
+	                                         __func__);
+}
+
+static void ui_editsource_active_but_clear(void)
+{
+	BLI_ghash_free(ui_editsource_info->hash, NULL, (GHashValFreeFP)MEM_freeN);
+	MEM_freeN(ui_editsource_info);
+	ui_editsource_info= NULL;
+}
+
+static int ui_editsource_uibut_match(uiBut *but_a, uiBut *but_b)
+{
+#if 0
+	printf("matching buttons: '%s' == '%s'\n",
+	       but_a->drawstr, but_b->drawstr);
+#endif
+
+	/* this just needs to be a 'good-enough' comparison so we can know beyond
+	 * reasonable doubt that these buttons are the same between redraws.
+	 * if this fails it only means edit-source fails - campbell */
+	if(     (but_a->x1 == but_b->x1) &&
+	        (but_a->x2 == but_b->x2) &&
+	        (but_a->y1 == but_b->y1) &&
+	        (but_a->y2 == but_b->y2) &&
+	        (but_a->type == but_b->type) &&
+	        (but_a->rnaprop == but_b->rnaprop) &&
+	        (but_a->optype == but_b->optype) &&
+	        (but_a->unit_type == but_b->unit_type) &&
+	        strncmp(but_a->drawstr, but_b->drawstr, UI_MAX_DRAW_STR) == 0
+	) {
+		return TRUE;
+	}
+	else {
+		return FALSE;
+	}
+}
+
+void UI_editsource_active_but_test(uiBut *but)
+{
+	extern void PyC_FileAndNum_Safe(const char **filename, int *lineno);
+
+	struct uiEditSourceButStore *but_store= MEM_callocN(sizeof(uiEditSourceButStore), __func__);
+
+	const char *fn;
+	int lineno= -1;
+
+#if 0
+	printf("comparing buttons: '%s' == '%s'\n",
+	       but->drawstr, ui_editsource_info->but_orig.drawstr);
+#endif
+
+	PyC_FileAndNum_Safe(&fn, &lineno);
+
+	if (lineno != -1) {
+		BLI_strncpy(but_store->py_dbg_fn, fn,
+					sizeof(but_store->py_dbg_fn));
+		but_store->py_dbg_ln= lineno;
+	}
+	else {
+		but_store->py_dbg_fn[0]= '\0';
+		but_store->py_dbg_ln= -1;
+	}
+
+	BLI_ghash_insert(ui_editsource_info->hash, but, but_store);
+}
+
+/* editsource operator component */
+
+static ScrArea *biggest_text_view(bContext *C)
+{
+	bScreen *sc= CTX_wm_screen(C);
+	ScrArea *sa, *big= NULL;
+	int size, maxsize= 0;
+
+	for(sa= sc->areabase.first; sa; sa= sa->next) {
+		if(sa->spacetype==SPACE_TEXT) {
+			size= sa->winx * sa->winy;
+			if(size > maxsize) {
+				maxsize= size;
+				big= sa;
+			}
+		}
+	}
+	return big;
+}
+
+static int editsource_text_edit(bContext *C, wmOperator *op,
+                                char filepath[240], int line)
+{
+	struct Main *bmain= CTX_data_main(C);
+	Text *text;
+
+	for (text=bmain->text.first; text; text=text->id.next) {
+		if (text->name && BLI_path_cmp(text->name, filepath) == 0) {
+			break;
+		}
+	}
+
+	if (text == NULL) {
+		text= add_text(filepath, bmain->name);
+	}
+
+	if (text == NULL) {
+		BKE_reportf(op->reports, RPT_WARNING,
+		            "file: '%s' can't be opened", filepath);
+		return OPERATOR_CANCELLED;
+	}
+	else {
+		/* naughty!, find text area to set, not good behavior
+		 * but since this is a dev tool lets allow it - campbell */
+		ScrArea *sa= biggest_text_view(C);
+		if(sa) {
+			SpaceText *st= sa->spacedata.first;
+			st->text= text;
+		}
+		else {
+			BKE_reportf(op->reports, RPT_INFO,

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list