| 58 | } |
| 59 | |
| 60 | QPixmap TemplatePreviewIcon::pixmap() const |
| 61 | { |
| 62 | if (!d->iconName.isEmpty()) { |
| 63 | // read icon from archive |
| 64 | QScopedPointer<KArchive> templateArchive; |
| 65 | if (QFileInfo(d->archivePath).completeSuffix() == QLatin1String("zip")) { |
| 66 | templateArchive.reset(new KZip(d->archivePath)); |
| 67 | } else { |
| 68 | templateArchive.reset(new KTar(d->archivePath)); |
| 69 | } |
| 70 | |
| 71 | if (templateArchive->open(QIODevice::ReadOnly)) { |
| 72 | const KArchiveFile* iconFile = templateArchive->directory()->file(d->iconName); |
| 73 | if (iconFile) { |
| 74 | QPixmap pixmap; |
| 75 | const bool loadSuccess = pixmap.loadFromData(iconFile->data()); |
| 76 | if (loadSuccess) { |
| 77 | return pixmap; |
| 78 | } |
| 79 | qCWarning(LANGUAGE) << "Could not load preview icon" << d->iconName << "from" << d->archivePath; |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | // support legacy templates with image files installed separately in the filesystem |
| 84 | const QString iconFilePath = QStandardPaths::locate(QStandardPaths::GenericDataLocation, |
| 85 | d->dataDir + d->iconName); |
| 86 | if (!iconFilePath.isEmpty()) { |
| 87 | QPixmap pixmap(iconFilePath); |
| 88 | if (!pixmap.isNull()) { |
| 89 | return pixmap; |
| 90 | } |
| 91 | qCWarning(LANGUAGE) << "Could not load preview icon" << iconFilePath << "as wanted for" << d->archivePath; |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | // try theme icon or default to a kdevelop icon |
| 96 | // QIcon::hasThemeIcon for empty string can yield true with some engines, not wanted here |
| 97 | const bool isThemeIcon = (!d->iconName.isEmpty() && QIcon::hasThemeIcon(d->iconName)); |
| 98 | const QString iconName = isThemeIcon ? d->iconName : QStringLiteral("kdevelop"); |
| 99 | |
| 100 | const QIcon icon = QIcon::fromTheme(iconName); |
| 101 | return icon.pixmap(128, 128); |
| 102 | } |
no test coverage detected