-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnsrpagescache.cpp
145 lines (113 loc) · 2.94 KB
/
nsrpagescache.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include "nsrpagescache.h"
/* Maximum cache size in bytes by default (100 MB) */
#define NSR_CORE_PAGES_CACHE_MAX_STORAGE 104857600
NSRPagesCache::NSRPagesCache (QObject *parent) :
QObject (parent),
_usedMemory (0),
_maxMemory (NSR_CORE_PAGES_CACHE_MAX_STORAGE)
{
}
NSRPagesCache::~NSRPagesCache ()
{
_hash.clear ();
_pages.clear ();
}
bool
NSRPagesCache::isPageExists (int number) const
{
return _hash.contains (number);
}
NSRRenderedPage
NSRPagesCache::getPage (int number) const
{
return _hash.value (number);
}
void NSRPagesCache::setMaxMemory (qint64 maxMemory)
{
if (maxMemory < 0 || maxMemory == _maxMemory)
return;
while (_usedMemory > _maxMemory && !_pages.isEmpty ()) {
int deqPage = _pages.takeFirst ();
NSRRenderedPage rpage = _hash.take (deqPage);
_usedMemory -= (rpage.getSize().width () * rpage.getSize().height () * 4 +
rpage.getText().size () * 2);
}
}
void
NSRPagesCache::addPage (const NSRRenderedPage& page)
{
qint64 newSize;
int deqPage;
if (!page.isValid ())
return;
newSize = page.getSize().width () * page.getSize().height () * 4 +
page.getText().size () * 2;
if (newSize > _maxMemory)
return;
_hash.take (page.getNumber ());
_pages.removeAll (page.getNumber ());
while ((_usedMemory + newSize) > _maxMemory && !_pages.isEmpty ()) {
if (qAbs (page.getNumber () - _pages.first ()) <
qAbs (page.getNumber () - _pages.last ()))
deqPage = _pages.takeLast ();
else
deqPage = _pages.takeFirst ();
NSRRenderedPage rpage = _hash.take (deqPage);
_usedMemory -= (rpage.getSize().width () * rpage.getSize().height () * 4 +
rpage.getText().size () * 2);
}
NSRRenderedPage newPage = page;
newPage.setRenderReason (NSRRenderRequest::NSR_RENDER_REASON_NAVIGATION);
newPage.setCached (true);
_pages.append (newPage.getNumber ());
qSort (_pages);
_hash.insert (newPage.getNumber (), newPage);
_usedMemory += newSize;
}
void
NSRPagesCache::removePage (int number)
{
if (number <= 0)
return;
NSRRenderedPage page = _hash.take (number);
_pages.removeAll (number);
if (!page.isValid ())
return;
_usedMemory -= (page.getSize().width () * page.getSize().height () * 4 +
page.getText().size () * 2);
}
void
NSRPagesCache::clearStorage ()
{
_hash.clear ();
_pages.clear ();
_usedMemory = 0;
}
void
NSRPagesCache::updatePagePositions (int number,
const QPointF& pos,
const QPointF& textPos)
{
NSRRenderedPage page = _hash.value (number);
if (!page.isValid ())
return;
page.setLastPosition (pos);
page.setLastTextPosition (textPos);
_hash.insert (number, page);
}
void
NSRPagesCache::removePagesWithoutImages ()
{
int count = _pages.count ();
for (int i = count - 1; i >= 0; --i)
if (!_hash.value(_pages.at (i)).isImageValid ())
removePage (_pages.at (i));
}
void
NSRPagesCache::removePagesWithImages ()
{
int count = _pages.count ();
for (int i = count - 1; i >= 0; --i)
if (_hash.value(_pages.at (i)).isImageValid ())
removePage (_pages.at (i));
}