diff --git a/src/Gui/Application.h b/src/Gui/Application.h index be1664ea06..29f279f37b 100644 --- a/src/Gui/Application.h +++ b/src/Gui/Application.h @@ -234,6 +234,7 @@ public: static PyObject* sAddIconPath (PyObject *self,PyObject *args); // adds a path to an icon file static PyObject* sAddIcon (PyObject *self,PyObject *args); // adds an icon to the cache static PyObject* sGetIcon (PyObject *self,PyObject *args); // get an icon from the cache + static PyObject* sIsIconCached (PyObject *self,PyObject *args); // check if an icon is cached static PyObject* sSendActiveView (PyObject *self,PyObject *args); static PyObject* sSendFocusView (PyObject *self,PyObject *args); diff --git a/src/Gui/ApplicationPy.cpp b/src/Gui/ApplicationPy.cpp index 6f0f1729aa..4c8162a9f6 100644 --- a/src/Gui/ApplicationPy.cpp +++ b/src/Gui/ApplicationPy.cpp @@ -102,8 +102,11 @@ PyMethodDef Application::Methods[] = { "addIcon(string, string or list) -> None\n\n" "Add an icon as file name or in XPM format to the system"}, {"getIcon", (PyCFunction) Application::sGetIcon, METH_VARARGS, - "getIcon(string -> QIcon\n\n" + "getIcon(string) -> QIcon\n\n" "Get an icon in the system"}, + {"isIconCached", (PyCFunction) Application::sIsIconCached, METH_VARARGS, + "isIconCached(String) -> Bool\n\n" + "Check if an icon with the given name is cached"}, {"getMainWindow", (PyCFunction) Application::sGetMainWindow, METH_VARARGS, "getMainWindow() -> QMainWindow\n\n" "Return the main window instance"}, @@ -1062,9 +1065,11 @@ PyObject* Application::sAddIconPath(PyObject * /*self*/, PyObject *args) PyObject* Application::sAddIcon(PyObject * /*self*/, PyObject *args) { - char *iconName; - char *pixmap; - if (!PyArg_ParseTuple(args, "ss", &iconName,&pixmap)) + const char *iconName; + const char *content; + Py_ssize_t size = 0; + const char *format = "XPM"; + if (!PyArg_ParseTuple(args, "ss#|s", &iconName,&content,&size,&format)) return NULL; QPixmap icon; @@ -1073,16 +1078,11 @@ PyObject* Application::sAddIcon(PyObject * /*self*/, PyObject *args) return NULL; } - QByteArray ary; - std::string content = pixmap; - int strlen = (int)content.size(); - ary.resize(strlen); - for (int j=0; j lines = ary.split('\n'); - QByteArray buffer; - buffer.reserve(ary.size()+lines.size()); - for (QList::iterator it = lines.begin(); it != lines.end(); ++it) { - QByteArray trim = it->trimmed(); - if (!trim.isEmpty()) { - buffer.append(trim); - buffer.append('\n'); + // Check if the passed string is a filename, otherwise treat as xpm data + QFileInfo fi(QString::fromUtf8(content.c_str())); + if (fi.isFile() && fi.exists()) { + icon.load(fi.absoluteFilePath()); + } else { + QByteArray ary; + int strlen = (int)content.size(); + ary.resize(strlen); + for (int j=0; j lines = ary.split('\n'); + QByteArray buffer; + buffer.reserve(ary.size()+lines.size()); + for (QList::iterator it = lines.begin(); it != lines.end(); ++it) { + QByteArray trim = it->trimmed(); + if (!trim.isEmpty()) { + buffer.append(trim); + buffer.append('\n'); + } } + icon.loadFromData(buffer, "XPM"); } - icon.loadFromData(buffer, "XPM"); - } - if (!icon.isNull()) { - return icon; + if (!icon.isNull()) { + return icon; + } + } else { + PythonWrapper wrap; + wrap.loadGuiModule(); + wrap.loadWidgetsModule(); + QIcon *picon = wrap.toQIcon(ret.ptr()); + if(picon) + return *picon; } } catch (Py::Exception&) {