Make the stamps renderer as static as before

Looking for and loading the SVG file isn't cheap so cache it as we did
in the past
remotes/origin/work/bharadwaj/darkmode-button
Albert Astals Cid 5 years ago
parent b3379d331d
commit a408499889
  1. 32
      core/annotations.cpp

@ -165,21 +165,29 @@ QPixmap AnnotationUtils::loadStamp(const QString &nameOrPath, int size, bool kee
{ {
const QString name = nameOrPath.toLower(); const QString name = nameOrPath.toLower();
const QString stampFile = QStandardPaths::locate(QStandardPaths::GenericDataLocation, QStringLiteral("okular/pics/stamps.svg")); static std::unique_ptr<QSvgRenderer> svgStampFile;
if (!stampFile.isEmpty()) { if (!svgStampFile.get()) {
QSvgRenderer r(stampFile); const QString stampFile = QStandardPaths::locate(QStandardPaths::GenericDataLocation, QStringLiteral("okular/pics/stamps.svg"));
if (r.isValid() && r.elementExists(name)) { if (!stampFile.isEmpty()) {
const QSize stampSize = r.boundsOnElement(name).size().toSize(); svgStampFile = std::make_unique<QSvgRenderer>(stampFile);
const QSize pixmapSize = stampSize.scaled(size, size, keepAspectRatio ? Qt::KeepAspectRatioByExpanding : Qt::IgnoreAspectRatio); if (!svgStampFile->isValid()) {
QPixmap pixmap(pixmapSize); svgStampFile.reset();
pixmap.fill(Qt::transparent); }
QPainter p(&pixmap);
r.render(&p, name);
p.end();
return pixmap;
} }
} }
QSvgRenderer *r = svgStampFile.get();
if (r && r->isValid() && r->elementExists(name)) {
const QSize stampSize = r->boundsOnElement(name).size().toSize();
const QSize pixmapSize = stampSize.scaled(size, size, keepAspectRatio ? Qt::KeepAspectRatioByExpanding : Qt::IgnoreAspectRatio);
QPixmap pixmap(pixmapSize);
pixmap.fill(Qt::transparent);
QPainter p(&pixmap);
r->render(&p, name);
p.end();
return pixmap;
}
// _name is a path (do this before loading as icon name to avoid some rare weirdness ) // _name is a path (do this before loading as icon name to avoid some rare weirdness )
QPixmap pixmap; QPixmap pixmap;
pixmap.load(nameOrPath); pixmap.load(nameOrPath);

Loading…
Cancel
Save