Skip to content

use Qt window title as default savefig filename #908

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Jul 9, 2012
Prev Previous commit
Add window title management to MacOSX backend
  • Loading branch information
jkseppan authored and mspacek committed Jul 5, 2012
commit 23deb00669fb215976b446176993db2d17e81e61
6 changes: 4 additions & 2 deletions lib/matplotlib/backends/backend_macosx.py
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,8 @@ def zoomy(self, direction):
self.canvas.invalidate()

def save_figure(self, *args):
filename = _macosx.choose_save_file('Save the figure')
filename = _macosx.choose_save_file('Save the figure',
self.canvas.get_default_filename())
if filename is None: # Cancel
return
self.canvas.print_figure(filename)
Expand All @@ -469,7 +470,8 @@ def set_cursor(self, cursor):
_macosx.set_cursor(cursor)

def save_figure(self, *args):
filename = _macosx.choose_save_file('Save the figure')
filename = _macosx.choose_save_file('Save the figure',
self.canvas.get_default_filename())
if filename is None: # Cancel
return
self.canvas.print_figure(filename)
Expand Down
70 changes: 69 additions & 1 deletion src/_macosx.m
Original file line number Diff line number Diff line change
Expand Up @@ -3827,6 +3827,56 @@ static void _data_provider_release(void* info, const void* data, size_t size)
return Py_None;
}

static PyObject*
FigureManager_set_window_title(FigureManager* self,
PyObject *args, PyObject *kwds)
{
char* title;
if(!PyArg_ParseTuple(args, "es", "UTF-8", &title))
return NULL;

Window* window = self->window;
if(window)
{
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
NSString* ns_title = [[NSString alloc]
initWithCString: title
encoding: NSUTF8StringEncoding];
[window setTitle: ns_title];
[pool release];
}
PyMem_Free(title);
Py_INCREF(Py_None);
return Py_None;
}

static PyObject*
FigureManager_get_window_title(FigureManager* self)
{
Window* window = self->window;
PyObject* result = NULL;
if(window)
{
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
NSString* title = [window title];
if (title) {
const char* cTitle = [title UTF8String];
#if PY3K || (PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION >= 6)
result = PyUnicode_FromString(cTitle);
#else
result = PyString_FromString(cTitle);
#endif
}
[pool release];
}
if (result) {
return result;
} else {
Py_INCREF(Py_None);
return Py_None;
}
}

static PyMethodDef FigureManager_methods[] = {
{"show",
(PyCFunction)FigureManager_show,
Expand All @@ -3838,6 +3888,16 @@ static void _data_provider_release(void* info, const void* data, size_t size)
METH_NOARGS,
"Closes the window associated with the figure manager."
},
{"set_window_title",
(PyCFunction)FigureManager_set_window_title,
METH_VARARGS,
"Sets the title of the window associated with the figure manager."
},
{"get_window_title",
(PyCFunction)FigureManager_get_window_title,
METH_NOARGS,
"Returns the title of the window associated with the figure manager."
},
{NULL} /* Sentinel */
};

Expand Down Expand Up @@ -4806,11 +4866,19 @@ -(void)save_figure:(id)sender
{
int result;
const char* title;
if(!PyArg_ParseTuple(args, "s", &title)) return NULL;
char* default_filename;
if(!PyArg_ParseTuple(args, "ses", &title, "UTF-8", &default_filename))
return NULL;

NSSavePanel* panel = [NSSavePanel savePanel];
[panel setTitle: [NSString stringWithCString: title
encoding: NSASCIIStringEncoding]];
NSString* ns_default_filename =
[[NSString alloc]
initWithCString: default_filename
encoding: NSUTF8StringEncoding];
PyMem_Free(default_filename);
[panel setNameFieldStringValue: ns_default_filename];
result = [panel runModal];
if (result == NSOKButton)
{
Expand Down