summaryrefslogtreecommitdiffstats
path: root/kviewshell/documentPageCache.cpp
blob: addaa706b0f9889412865dcb89ee14a448fe1d57 (plain)
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
//
// Class: documentPageCache
//
// Cache that holds a number of pages in a document.
// Part of KDVI- A previewer for TeX DVI files.
//
// (C) 2004 Stefan Kebekus. Distributed under the GPL.

#include <config.h>

#include <kdebug.h>
#include <tqapplication.h>

#include "documentPageCache.h"
#include "documentRenderer.h"
#include "kvsprefs.h"
#include "renderedDocumentPagePixmap.h"


//#define documentPageCache_DEBUG


DocumentPageCache::DocumentPageCache()
  : maxMemory(2*16777216), LRUCache(maxMemory, 200)
{
  LRUCache.setAutoDelete(true);

  resolutionInDPI = 0.0;
  Length w,h;
  w.setLength_in_mm(200);
  h.setLength_in_mm(300);
  userPreferredSize.setPageSize(w,h);
  useDocumentSpecifiedSize = true;
}


DocumentPageCache::~DocumentPageCache()
{
}


void DocumentPageCache::setRenderer(DocumentRenderer *_renderer)
{
  clear();
  renderer = _renderer;
}


SimplePageSize DocumentPageCache::sizeOfPage(const PageNumber& page) const
{
  // Paranoid safety checks
  if (!page.isValid()) {
    kdError(1223) << "DocumentPageCache::sizeOfPage( " <<  page << ") called with invalid page number." << endl;
    return SimplePageSize();
  }
  if (renderer.isNull()) {
    kdError(1223) << "DocumentPageCache::sizeOfPage( " <<  page << ") called when no renderer was set." << endl;
    return SimplePageSize();
  }

  SimplePageSize s = renderer->sizeOfPage(page);
  if (!useDocumentSpecifiedSize)
    s = userPreferredSize;

  if (!s.isValid())
  {
    // If the size is invalid use the size of the first Page in the document
    // as an estimate.
    s = renderer->sizeOfPage(1);
    if (!s.isValid())
      s = userPreferredSize;
  }

  return s;
}

void DocumentPageCache::setResolution(double res)
{
  resolutionInDPI = res;
}

TQSize DocumentPageCache::sizeOfPageInPixel(const PageNumber& pg) const
{
  // Paranoid safety checks
  if (renderer.isNull()) {
    kdError(1223) << "DocumentPageCache::sizeOfPageInPixel( " << pg << " ) called but no renderer was set" << endl;
    return TQSize();
  }
  if (!pg.isValid()) {
    kdError(1223) << "DocumentPageCache::sizeOfPageInPixel( " << pg << " ) called with invalid argument" << endl;
    return TQSize();
  }

  SimplePageSize ps = sizeOfPage(pg);
  if (ps.isValid())
    return ps.sizeInPixel(resolutionInDPI);
  return userPreferredSize.sizeInPixel(resolutionInDPI); 
}


bool DocumentPageCache::isPageCached(const PageNumber& pageNumber, const TQSize& size)
{
  // Paranoid checks
  if (renderer.isNull()) {
    kdError(1223) << "DocumentPageCache::isPageCached(..) called but no renderer was set" << endl;
    return false;
  }
  if (!pageNumber.isValid()) {
    kdError(1223) << "DocumentPageCache::isPageCached( " << pageNumber << " ) called, with invalid argument." << endl;
    return false;
  }
  if (renderer->totalPages() < pageNumber) {
    kdError(1223) << "DocumentPageCache::isPageCached( " << pageNumber
                  << " ) called but document contains only " << renderer->totalPages() << " pages." << endl;
    return false;
  }

  TQString key = createKey(pageNumber, size);

  // Check if the page that we are looking for is in the cache.
  // We are not accessing the page, so we don't want it to be moved into the front.
  RenderedDocumentPagePixmap* page = LRUCache.find(key, false);

  if (page)
    return true;
  else
    return false;
}

TQString DocumentPageCache::createKey(const PageNumber& pageNumber, const TQSize& size)
{
  TQString key;

  key = TQString::number(pageNumber) + ":" +
        TQString::number(size.width()) + ":" + TQString::number(size.height());

  return key;
}

TQString DocumentPageCache::createKey(const PageNumber& pageNumber)
{
  TQSize pageSize = sizeOfPageInPixel(pageNumber);

  TQString key;

  key = TQString::number(pageNumber) + ":" +
        TQString::number(pageSize.width()) + ":" + TQString::number(pageSize.height());

  return key;
}

bool DocumentPageCache::isPageCached(const PageNumber& pageNumber)
{
  // Paranoid checks
  if (renderer.isNull()) {
    kdError(1223) << "DocumentPageCache::isPageCached(..) called but no renderer was set" << endl;
    return false;
  }
  if (!pageNumber.isValid()) {
    kdError(1223) << "DocumentPageCache::isPageCached( " << pageNumber << " ) called, with invalid argument." << endl;
    return false;
  }
  if (renderer->totalPages() < pageNumber) {
    kdError(1223) << "DocumentPageCache::isPageCached( " << pageNumber
                  << " ) called but document contains only " << renderer->totalPages() << " pages." << endl;
    return false;
  }

  return isPageCached(pageNumber, sizeOfPageInPixel(pageNumber));
}

RenderedDocumentPagePixmap* DocumentPageCache::getPage(const PageNumber& pageNr)
{
#ifdef DocumentPageCache_DEBUG
  kdDebug(1223) << "DocumentPageCache::getPage( pageNr=" << pageNr << " )" << endl;
#endif

  // Paranoid checks
  if (renderer.isNull()) {
    kdError(1223) << "DocumentPageCache::getPage(..) called but no renderer was set" << endl;
    return 0;
  }
  if (!pageNr.isValid()) {
    kdError(1223) << "DocumentPageCache::getPage( " << pageNr << " ) called, with invalid argument." << endl;
    return 0;
  }
  if (renderer->totalPages() < pageNr) {
    kdError(1223) << "DocumentPageCache::getPage( " << pageNr << " ) called but document contains only " << renderer->totalPages() << " pages." << endl;
    return 0;
  }

  // First check if the page that we are looking for is in the cache
  RenderedDocumentPagePixmap* page;
  page = LRUCache.find(createKey(pageNr));

  if (page)
    return page;

  // The page was not found in the cache, so we have to make a new
  // page and add this to the cache.
  page = createDocumentPagePixmap();

  // If that failed, issue an error message and quit.
  if (page == 0) {
    kdError(1223) << "DocumentPageCache::getPage(..) cannot allocate DocumentPage structure" << endl;
    return 0;
  }
  
  // Now 'page' contains a point to a page structure that we can
  // use. Add the page to the cache, and apply the renderer to the page.
  page->setPageNumber(pageNr);
  if (!renderer.isNull())
  {
    if (resolutionInDPI > 0.0)
    {
      page->resize(sizeOfPageInPixel(pageNr));
      TQApplication::setOverrideCursor( waitCursor );
      renderer->drawPage(resolutionInDPI, page);
      TQApplication::restoreOverrideCursor();

      // We always set the cache capacity to be at least n times the cost of the page we want to insert.
      // Where n is the number of pages that can be visible at the same time at very high zoomlevels.
      // n depends on the tqlayout mode.
      // If these pages are not all in the cache, scrolling the view becomes very slow, because for each
      // paint event the pages need to be rerendered.
      // We set n for each viewmode differently so that the user is able to reduce memory consuption by
      // switching to a simpler viewmode like Single Page.
      int n = 4;
      switch (KVSPrefs::viewMode())
      {
        case KVSPrefs::EnumViewMode::SinglePage:
          n = 1;
          break;
        case KVSPrefs::EnumViewMode::Continuous:
          n = 2;
          break;
        default:
          n = 4;
      }
      LRUCache.setMaxCost(TQMAX(page->memory() * n, maxMemory));

      if (!LRUCache.insert(createKey(pageNr), page, page->memory()))
      {
        kdError() << "DocumentPageCache::getPage(): inserting pagestructure into the cache failed.\n This should never happen. If you see this message, something is very wrong." << endl;
      }
    }
    else
      kdError(1223) << "DocumentPageCache::getPage() called, but no resolution or negative resolution was set" << endl;
  }

  return page;
}


RenderedDocumentPagePixmap* DocumentPageCache::createDocumentPagePixmap() const
{
 return new RenderedDocumentPagePixmap();
}


void DocumentPageCache::clear()
{
  LRUCache.clear();
}


void DocumentPageCache::setUserPreferredSize(const SimplePageSize& s)
{
  bool sizeChanged = !userPreferredSize.isNearlyEqual(s);
  userPreferredSize = s;

  if (sizeChanged)
    emit(paperSizeChanged());
}


void DocumentPageCache::setUseDocumentSpecifiedSize(bool b)
{
  bool valChanged = (useDocumentSpecifiedSize == b);

  useDocumentSpecifiedSize = b;
  if (valChanged)
    emit(paperSizeChanged());
}


TQPixmap DocumentPageCache::createThumbnail(const PageNumber& pageNr, int width)
{
  // Paranoid checks
  if (renderer.isNull()) {
    kdError(1223) << "DocumentPageCache::createThumbnail(..) called but no renderer was set" << endl;
    thumbnailPage.resize(0,0);
    return thumbnailPage;
  }
  if (renderer->totalPages() < pageNr) {
    kdError(1223) << "DocumentPageCache::createThumbnail( " << pageNr << ", width ) called but document contains only " << renderer->totalPages() << " pages." << endl;
    thumbnailPage.resize(0,0);
    return thumbnailPage;
  }
  if (!pageNr.isValid()) {
    kdError(1223) << "DocumentPageCache::createThumbnail(..) called for page with invalid page specification" << endl;
    thumbnailPage.resize(0,0);
    return thumbnailPage;
  }
  if (!sizeOfPage().isValid()) {
    kdError(1223) << "DocumentPageCache::createThumbnail(..) called for page with invalid size" << endl;
    thumbnailPage.resize(0,0);
    return thumbnailPage;
  }

  thumbnailPage.setPageNumber(pageNr);
  thumbnailPage.resize(width, (int)(width/sizeOfPage(pageNr).aspectRatio() + 0.5 ) );
  renderer->drawThumbnail((double)(width)/sizeOfPage(pageNr).width().getLength_in_inch(), &thumbnailPage);

  if (KVSPrefs::changeColors() && KVSPrefs::renderMode() != KVSPrefs::EnumRenderMode::Paper)
  {
    return thumbnailPage.accessiblePixmap();
  }
  else
  {
    return thumbnailPage;
  }
}

void DocumentPageCache::deselectText() 
{
  userSelection.clear();
  emit textSelected(false);
}

void DocumentPageCache::selectText(const TextSelection& selection)
{
  userSelection = selection;
  emit textSelected(!userSelection.isEmpty());
}

#include "documentPageCache.moc"