- use outliner colors (with subtle stripes) for report so you can see divisions between operators with wrapping.

- added class option for PyOperators __register__ so you can set if py operators are logged in the console.
- PyOperators was refcounting in a more readable but incorrect way. in some cases would be possible to crash so better not drop the reference before using the value.
- console zoom operator was registering which meant zooming in to see some text would push it away :)
This commit is contained in:
Campbell Barton 2009-07-16 07:11:46 +00:00
parent 513dcf7b46
commit 88e3e8c1c9
4 changed files with 80 additions and 35 deletions

@ -87,6 +87,7 @@ class CONSOLE_OT_exec(bpy.types.Operator):
Operator documentatuon text, will be used for the operator tooltip and python docs. Operator documentatuon text, will be used for the operator tooltip and python docs.
''' '''
__label__ = "Console Execute" __label__ = "Console Execute"
__register__ = True
# Both prompts must be the same length # Both prompts must be the same length
PROMPT = '>>> ' PROMPT = '>>> '
@ -362,6 +363,7 @@ class CONSOLE_OT_autocomplete(bpy.types.Operator):
Operator documentatuon text, will be used for the operator tooltip and python docs. Operator documentatuon text, will be used for the operator tooltip and python docs.
''' '''
__label__ = "Console Autocomplete" __label__ = "Console Autocomplete"
__register__ = True
def poll(self, context): def poll(self, context):
return context.space_data.type == 'PYTHON' return context.space_data.type == 'PYTHON'

@ -71,44 +71,57 @@ static void console_font_begin(SpaceConsole *sc)
BLF_size(sc->lheight, 72); BLF_size(sc->lheight, 72);
} }
static void console_line_color(int type) static void console_line_color(unsigned char *fg, int type)
{ {
switch(type){ switch(type) {
case CONSOLE_LINE_OUTPUT: case CONSOLE_LINE_OUTPUT:
glColor4ub(96, 128, 255, 255); fg[0]=96; fg[1]=128; fg[2]=255;
break; break;
case CONSOLE_LINE_INPUT: case CONSOLE_LINE_INPUT:
glColor4ub(255, 255, 255, 255); fg[0]=255; fg[1]=255; fg[2]=255;
break; break;
case CONSOLE_LINE_INFO: case CONSOLE_LINE_INFO:
glColor4ub(0, 170, 0, 255); fg[0]=0; fg[1]=170; fg[2]=0;
break; break;
case CONSOLE_LINE_ERROR: case CONSOLE_LINE_ERROR:
glColor4ub(220, 96, 96, 255); fg[0]=220; fg[1]=96; fg[2]=96;
break; break;
} }
} }
static void console_report_color(int type) static void console_report_color(unsigned char *fg, int type)
{ {
if(type & RPT_ERROR_ALL) return glColor4ub(220, 0, 0, 255); /*
if(type & RPT_WARNING_ALL) return glColor4ub(220, 96, 96, 255); if (type & RPT_ERROR_ALL) { fg[0]=220; fg[1]=0; fg[2]=0; }
if(type & RPT_OPERATOR_ALL) return glColor4ub(96, 128, 255, 255); else if (type & RPT_WARNING_ALL) { fg[0]=220; fg[1]=96; fg[2]=96; }
if(type & RPT_INFO_ALL) return glColor4ub(0, 170, 0, 255); else if (type & RPT_OPERATOR_ALL) { fg[0]=96; fg[1]=128; fg[2]=255; }
if(type & RPT_DEBUG_ALL) return glColor4ub(196, 196, 196, 255); else if (type & RPT_INFO_ALL) { fg[0]=0; fg[1]=170; fg[2]=0; }
return glColor4ub(196, 196, 196, 255); /* unknown */ else if (type & RPT_DEBUG_ALL) { fg[0]=196; fg[1]=196; fg[2]=196; }
else { fg[0]=196; fg[1]=196; fg[2]=196; }
*/
fg[0]=0; fg[1]=0; fg[2]=0;
} }
/* return 0 if the last line is off the screen /* return 0 if the last line is off the screen
* should be able to use this for any string type */ * should be able to use this for any string type */
static int console_draw_string(char *str, int str_len, int console_width, int lheight, int ymax, int *x, int *y) static int console_draw_string(char *str, int str_len, int console_width, int lheight, unsigned char *fg, unsigned char *bg, int winx, int winy, int *x, int *y)
{ {
int rct_ofs= lheight/4;
if(str_len > console_width) { /* wrap? */ if(str_len > console_width) { /* wrap? */
int tot_lines = (str_len/console_width)+1; /* total number of lines for wrapping */ int tot_lines = (str_len/console_width)+1; /* total number of lines for wrapping */
char *line_stride= str + ((tot_lines-1) * console_width); /* advance to the last line and draw it first */ char *line_stride= str + ((tot_lines-1) * console_width); /* advance to the last line and draw it first */
char eol; /* baclup the end of wrapping */ char eol; /* baclup the end of wrapping */
if(bg) {
glColor3ub(bg[0], bg[1], bg[2]);
glRecti(0, *y-rct_ofs, winx, (*y+(lheight*tot_lines))+rct_ofs);
}
glColor3ub(fg[0], fg[1], fg[2]);
/* last part needs no clipping */ /* last part needs no clipping */
BLF_position(*x, *y, 0); (*y) += lheight; BLF_position(*x, *y, 0); (*y) += lheight;
BLF_draw(line_stride); BLF_draw(line_stride);
@ -124,15 +137,23 @@ static int console_draw_string(char *str, int str_len, int console_width, int lh
line_stride[console_width] = eol; /* restore */ line_stride[console_width] = eol; /* restore */
/* check if were out of view bounds */ /* check if were out of view bounds */
if(*y > ymax) if(*y > winy)
return 0; return 0;
} }
} }
else { /* simple, no wrap */ else { /* simple, no wrap */
if(bg) {
glColor3ub(bg[0], bg[1], bg[2]);
glRecti(0, *y-rct_ofs, winx, *y+lheight-rct_ofs);
}
glColor3ub(fg[0], fg[1], fg[2]);
BLF_position(*x, *y, 0); (*y) += lheight; BLF_position(*x, *y, 0); (*y) += lheight;
BLF_draw(str); BLF_draw(str);
if(*y > ymax) if(*y > winy)
return 0; return 0;
} }
@ -140,6 +161,7 @@ static int console_draw_string(char *str, int str_len, int console_width, int lh
} }
#define CONSOLE_DRAW_MARGIN 8 #define CONSOLE_DRAW_MARGIN 8
#define CONSOLE_LINE_MARGIN 6
void console_text_main(struct SpaceConsole *sc, struct ARegion *ar, ReportList *reports) void console_text_main(struct SpaceConsole *sc, struct ARegion *ar, ReportList *reports)
{ {
@ -149,7 +171,7 @@ void console_text_main(struct SpaceConsole *sc, struct ARegion *ar, ReportList *
int x,y; int x,y;
int cwidth; int cwidth;
int console_width; /* number of characters that fit into the width of the console (fixed width) */ int console_width; /* number of characters that fit into the width of the console (fixed width) */
unsigned char fg[3];
console_font_begin(sc); console_font_begin(sc);
cwidth = BLF_fixed_width(); cwidth = BLF_fixed_width();
@ -163,7 +185,8 @@ void console_text_main(struct SpaceConsole *sc, struct ARegion *ar, ReportList *
int prompt_len= strlen(sc->prompt); int prompt_len= strlen(sc->prompt);
/* text */ /* text */
console_line_color(CONSOLE_LINE_INPUT); console_line_color(fg, CONSOLE_LINE_INPUT);
glColor3ub(fg[0], fg[1], fg[2]);
/* command line */ /* command line */
if(prompt_len) { if(prompt_len) {
@ -174,7 +197,8 @@ void console_text_main(struct SpaceConsole *sc, struct ARegion *ar, ReportList *
BLF_draw(cl->line); BLF_draw(cl->line);
/* cursor */ /* cursor */
console_line_color(CONSOLE_LINE_ERROR); /* lazy */ console_line_color(fg, CONSOLE_LINE_ERROR); /* lazy */
glColor3ub(fg[0], fg[1], fg[2]);
glRecti(x+(cwidth*cl->cursor) -1, y-2, x+(cwidth*cl->cursor) +1, y+sc->lheight-2); glRecti(x+(cwidth*cl->cursor) -1, y-2, x+(cwidth*cl->cursor) +1, y+sc->lheight-2);
x= x_orig; /* remove prompt offset */ x= x_orig; /* remove prompt offset */
@ -182,9 +206,9 @@ void console_text_main(struct SpaceConsole *sc, struct ARegion *ar, ReportList *
y += sc->lheight; y += sc->lheight;
for(cl= sc->scrollback.last; cl; cl= cl->prev) { for(cl= sc->scrollback.last; cl; cl= cl->prev) {
console_line_color(cl->type); console_line_color(fg, cl->type);
if(!console_draw_string(cl->line, cl->len, console_width, sc->lheight, ar->winy + sc->lheight, &x, &y)) if(!console_draw_string(cl->line, cl->len, console_width, sc->lheight+CONSOLE_LINE_MARGIN, fg, NULL, ar->winx, ar->winy, &x, &y))
break; /* past the y limits */ break; /* past the y limits */
} }
@ -192,20 +216,28 @@ void console_text_main(struct SpaceConsole *sc, struct ARegion *ar, ReportList *
else { else {
Report *report; Report *report;
int report_mask= 0; int report_mask= 0;
int bool= 0;
unsigned char bg[3] = {114, 114, 114};
glClearColor(120.0/255.0, 120.0/255.0, 120.0/255.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
/* convert our display toggles into a flag compatible with BKE_report flags */ /* convert our display toggles into a flag compatible with BKE_report flags */
if(sc->rpt_mask & CONSOLE_RPT_DEBUG) report_mask |= RPT_DEBUG_ALL; if(sc->rpt_mask & CONSOLE_RPT_DEBUG) report_mask |= RPT_DEBUG_ALL;
if(sc->rpt_mask & CONSOLE_RPT_INFO) report_mask |= RPT_INFO_ALL; if(sc->rpt_mask & CONSOLE_RPT_INFO) report_mask |= RPT_INFO_ALL;
if(sc->rpt_mask & CONSOLE_RPT_OP) report_mask |= RPT_OPERATOR_ALL; if(sc->rpt_mask & CONSOLE_RPT_OP) report_mask |= RPT_OPERATOR_ALL;
if(sc->rpt_mask & CONSOLE_RPT_WARN) report_mask |= RPT_WARNING_ALL; if(sc->rpt_mask & CONSOLE_RPT_WARN) report_mask |= RPT_WARNING_ALL;
if(sc->rpt_mask & CONSOLE_RPT_ERR) report_mask |= RPT_ERROR_ALL; if(sc->rpt_mask & CONSOLE_RPT_ERR) report_mask |= RPT_ERROR_ALL;
for(report=reports->list.last; report; report=report->prev) { for(report=reports->list.last; report; report=report->prev) {
if(report->type & report_mask) { if(report->type & report_mask) {
console_report_color(report->type); console_report_color(fg, report->type);
if(!console_draw_string(report->message, strlen(report->message), console_width, sc->lheight, ar->winy + sc->lheight, &x, &y)) if(!console_draw_string(report->message, strlen(report->message), console_width, sc->lheight+CONSOLE_LINE_MARGIN, fg, bool?bg:NULL, ar->winx, ar->winy, &x, &y))
break; /* past the y limits */ break; /* past the y limits */
y+=CONSOLE_LINE_MARGIN;
bool = !(bool);
} }
} }

@ -562,7 +562,7 @@ void CONSOLE_OT_zoom(wmOperatorType *ot)
ot->exec= zoom_exec; ot->exec= zoom_exec;
/* flags */ /* flags */
ot->flag= OPTYPE_REGISTER; /* ot->flag= OPTYPE_REGISTER; */ /* super annoying */
/* properties */ /* properties */
RNA_def_int(ot->srna, "delta", 0, 0, INT_MAX, "Delta", "Scale the view font.", 0, 1000); RNA_def_int(ot->srna, "delta", 0, 0, INT_MAX, "Delta", "Scale the view font.", 0, 1000);

@ -46,6 +46,7 @@
#define PYOP_ATTR_UINAME "__label__" #define PYOP_ATTR_UINAME "__label__"
#define PYOP_ATTR_IDNAME "__name__" /* use pythons class name */ #define PYOP_ATTR_IDNAME "__name__" /* use pythons class name */
#define PYOP_ATTR_DESCRIPTION "__doc__" /* use pythons docstring */ #define PYOP_ATTR_DESCRIPTION "__doc__" /* use pythons docstring */
#define PYOP_ATTR_REGISTER "__register__" /* True/False. if this python operator should be registered */
static struct BPY_flag_def pyop_ret_flags[] = { static struct BPY_flag_def pyop_ret_flags[] = {
{"RUNNING_MODAL", OPERATOR_RUNNING_MODAL}, {"RUNNING_MODAL", OPERATOR_RUNNING_MODAL},
@ -210,8 +211,8 @@ static int PYTHON_OT_generic(int mode, bContext *C, wmOperator *op, wmEvent *eve
/* get class name */ /* get class name */
item= PyObject_GetAttrString(py_class, PYOP_ATTR_IDNAME); item= PyObject_GetAttrString(py_class, PYOP_ATTR_IDNAME);
Py_DECREF(item);
strcpy(class_name, _PyUnicode_AsString(item)); strcpy(class_name, _PyUnicode_AsString(item));
Py_DECREF(item);
fprintf(stderr, "%s's %s returned %s\n", class_name, mode == PYOP_EXEC ? "execute" : "invoke", flag_str); fprintf(stderr, "%s's %s returned %s\n", class_name, mode == PYOP_EXEC ? "execute" : "invoke", flag_str);
} }
@ -246,14 +247,14 @@ void PYTHON_OT_wrapper(wmOperatorType *ot, void *userdata)
/* identifiers */ /* identifiers */
item= PyObject_GetAttrString(py_class, PYOP_ATTR_IDNAME); item= PyObject_GetAttrString(py_class, PYOP_ATTR_IDNAME);
Py_DECREF(item);
ot->idname= _PyUnicode_AsString(item); ot->idname= _PyUnicode_AsString(item);
Py_DECREF(item);
item= PyObject_GetAttrString(py_class, PYOP_ATTR_UINAME); item= PyObject_GetAttrString(py_class, PYOP_ATTR_UINAME);
if (item) { if (item) {
Py_DECREF(item);
ot->name= _PyUnicode_AsString(item); ot->name= _PyUnicode_AsString(item);
Py_DECREF(item);
} }
else { else {
ot->name= ot->idname; ot->name= ot->idname;
@ -261,8 +262,8 @@ void PYTHON_OT_wrapper(wmOperatorType *ot, void *userdata)
} }
item= PyObject_GetAttrString(py_class, PYOP_ATTR_DESCRIPTION); item= PyObject_GetAttrString(py_class, PYOP_ATTR_DESCRIPTION);
Py_DECREF(item);
ot->description= (item && PyUnicode_Check(item)) ? _PyUnicode_AsString(item):""; ot->description= (item && PyUnicode_Check(item)) ? _PyUnicode_AsString(item):"";
Py_DECREF(item);
/* api callbacks, detailed checks dont on adding */ /* api callbacks, detailed checks dont on adding */
if (PyObject_HasAttrString(py_class, "invoke")) if (PyObject_HasAttrString(py_class, "invoke"))
@ -274,13 +275,22 @@ void PYTHON_OT_wrapper(wmOperatorType *ot, void *userdata)
ot->pyop_data= userdata; ot->pyop_data= userdata;
/* flags */
item= PyObject_GetAttrString(py_class, PYOP_ATTR_REGISTER);
if (item) {
ot->flag= PyObject_IsTrue(item)!=0 ? OPTYPE_REGISTER:0;
Py_DECREF(item);
}
else {
ot->flag= OPTYPE_REGISTER; /* unspesified, leave on for now to help debug */
PyErr_Clear();
}
props= PyObject_GetAttrString(py_class, PYOP_ATTR_PROP); props= PyObject_GetAttrString(py_class, PYOP_ATTR_PROP);
if (props) { if (props) {
PyObject *dummy_args = PyTuple_New(0); PyObject *dummy_args = PyTuple_New(0);
int i; int i;
Py_DECREF(props);
for(i=0; i<PyList_Size(props); i++) { for(i=0; i<PyList_Size(props); i++) {
PyObject *py_func_ptr, *py_kw, *py_srna_cobject, *py_ret; PyObject *py_func_ptr, *py_kw, *py_srna_cobject, *py_ret;
@ -310,6 +320,7 @@ void PYTHON_OT_wrapper(wmOperatorType *ot, void *userdata)
// expect a tuple with a CObject and a dict // expect a tuple with a CObject and a dict
} }
Py_DECREF(dummy_args); Py_DECREF(dummy_args);
Py_DECREF(props);
} else { } else {
PyErr_Clear(); PyErr_Clear();
} }
@ -340,16 +351,16 @@ PyObject *PYOP_wrap_add(PyObject *self, PyObject *py_class)
// in python would be... // in python would be...
//PyObject *optype = PyObject_GetAttrString(PyObject_GetAttrString(PyDict_GetItemString(PyEval_GetGlobals(), "bpy"), "types"), "Operator"); //PyObject *optype = PyObject_GetAttrString(PyObject_GetAttrString(PyDict_GetItemString(PyEval_GetGlobals(), "bpy"), "types"), "Operator");
base_class = PyObject_GetAttrStringArgs(PyDict_GetItemString(PyEval_GetGlobals(), "bpy"), 2, "types", "Operator"); base_class = PyObject_GetAttrStringArgs(PyDict_GetItemString(PyEval_GetGlobals(), "bpy"), 2, "types", "Operator");
Py_DECREF(base_class);
if(BPY_class_validate("Operator", py_class, base_class, pyop_class_attr_values, NULL) < 0) { if(BPY_class_validate("Operator", py_class, base_class, pyop_class_attr_values, NULL) < 0) {
return NULL; /* BPY_class_validate sets the error */ return NULL; /* BPY_class_validate sets the error */
} }
Py_DECREF(base_class);
/* class name is used for operator ID - this can be changed later if we want */ /* class name is used for operator ID - this can be changed later if we want */
item= PyObject_GetAttrString(py_class, PYOP_ATTR_IDNAME); item= PyObject_GetAttrString(py_class, PYOP_ATTR_IDNAME);
Py_DECREF(item);
idname = _PyUnicode_AsString(item); idname = _PyUnicode_AsString(item);
Py_DECREF(item);
/* remove if it already exists */ /* remove if it already exists */
if ((ot=WM_operatortype_exists(idname))) { if ((ot=WM_operatortype_exists(idname))) {
@ -362,7 +373,6 @@ PyObject *PYOP_wrap_add(PyObject *self, PyObject *py_class)
/* If we have properties set, check its a list of dicts */ /* If we have properties set, check its a list of dicts */
item= PyObject_GetAttrString(py_class, PYOP_ATTR_PROP); item= PyObject_GetAttrString(py_class, PYOP_ATTR_PROP);
if (item) { if (item) {
Py_DECREF(item);
for(i=0; i<PyList_Size(item); i++) { for(i=0; i<PyList_Size(item); i++) {
PyObject *py_args = PyList_GET_ITEM(item, i); PyObject *py_args = PyList_GET_ITEM(item, i);
PyObject *py_func_ptr, *py_kw; /* place holders */ PyObject *py_func_ptr, *py_kw; /* place holders */
@ -372,6 +382,7 @@ PyObject *PYOP_wrap_add(PyObject *self, PyObject *py_class)
return NULL; return NULL;
} }
} }
Py_DECREF(item);
} }
else { else {
PyErr_Clear(); PyErr_Clear();