mirror of
https://github.com/YACReader/yacreader
synced 2026-03-01 10:22:58 -05:00
Implement a small cache to avoid scaling continuously while painting
This commit is contained in:
committed by
Luis Ángel San Martín
parent
0bd291ba98
commit
edbbc3c577
@ -4,6 +4,8 @@
|
||||
#include <QWidget>
|
||||
#include <QSize>
|
||||
#include <QVector>
|
||||
#include <QHash>
|
||||
#include <QList>
|
||||
|
||||
#include "themable.h"
|
||||
|
||||
@ -33,8 +35,61 @@ protected:
|
||||
void applyTheme(const Theme &theme) override;
|
||||
|
||||
private:
|
||||
struct ScaledPageCacheEntry {
|
||||
qint64 sourceCacheKey = 0;
|
||||
QSize sourceSize;
|
||||
QSize targetSize;
|
||||
QImage scaledImage;
|
||||
};
|
||||
|
||||
struct ScaledPageCache {
|
||||
int effectiveWidth = -1;
|
||||
QHash<int, ScaledPageCacheEntry> pages;
|
||||
|
||||
void invalidateAll()
|
||||
{
|
||||
effectiveWidth = -1;
|
||||
pages.clear();
|
||||
}
|
||||
|
||||
void invalidateForWidth(int width)
|
||||
{
|
||||
if (effectiveWidth != width) {
|
||||
effectiveWidth = width;
|
||||
pages.clear();
|
||||
}
|
||||
}
|
||||
|
||||
void invalidatePage(int pageIndex)
|
||||
{
|
||||
pages.remove(pageIndex);
|
||||
}
|
||||
|
||||
void keepOnlyRange(int minPageIndex, int maxPageIndex)
|
||||
{
|
||||
if (pages.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
QList<int> keysToRemove;
|
||||
keysToRemove.reserve(pages.size());
|
||||
for (auto it = pages.constBegin(); it != pages.constEnd(); ++it) {
|
||||
if (it.key() < minPageIndex || it.key() > maxPageIndex) {
|
||||
keysToRemove.append(it.key());
|
||||
}
|
||||
}
|
||||
|
||||
for (int key : keysToRemove) {
|
||||
pages.remove(key);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const QImage *scaledImageForPaint(int pageIndex, const QImage *source, const QSize &targetSize, int effectiveWidth);
|
||||
|
||||
Render *render = nullptr;
|
||||
ContinuousViewModel *continuousViewModel = nullptr;
|
||||
ScaledPageCache scaledPageCache;
|
||||
};
|
||||
|
||||
#endif // CONTINUOUS_PAGE_WIDGET_H
|
||||
|
||||
Reference in New Issue
Block a user