Gui: improve Gui.addIcon(), add Gui.isIconCached()

The motivation of this patch is to make it easy for Python workbench
cache its own icons (possibly generated at runtime), saving the trouble
of pre-compiling the icons as binary resources.

* Gui.addIcon() now support a third argument as format. Default format
  is 'XPM' as before. The intention is to allow user to cache other
  format of icon image, like 'PNG'.

* ViewProviderPythonFeature::getIcon() now checks if the given string
  argument is a key to a cached icon.
This commit is contained in:
Zheng, Lei
2019-09-02 10:42:46 +08:00
committed by wmayer
parent 250906d530
commit f8e1d455e0
3 changed files with 59 additions and 43 deletions

View File

@@ -327,40 +327,45 @@ QIcon ViewProviderPythonFeatureImp::getIcon() const
if(ret.isNone())
return QIcon();
PythonWrapper wrap;
wrap.loadGuiModule();
wrap.loadWidgetsModule();
QIcon *picon = wrap.toQIcon(ret.ptr());
if(picon)
return *picon;
if(ret.isString()) {
std::string content = Py::String(ret).as_std_string("utf-8");
QPixmap icon;
if (BitmapFactory().findPixmapInCache(content.c_str(), icon))
return icon;
std::string content = Py::String(ret).as_std_string("utf-8");
QPixmap icon;
// 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<strlen; j++)
ary[j]=content[j];
// Make sure to remove crap around the XPM data
QList<QByteArray> lines = ary.split('\n');
QByteArray buffer;
buffer.reserve(ary.size()+lines.size());
for (QList<QByteArray>::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<strlen; j++)
ary[j]=content[j];
// Make sure to remove crap around the XPM data
QList<QByteArray> lines = ary.split('\n');
QByteArray buffer;
buffer.reserve(ary.size()+lines.size());
for (QList<QByteArray>::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&) {