1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** All rights reserved.
|
---|
5 | ** Contact: Nokia Corporation (qt-info@nokia.com)
|
---|
6 | **
|
---|
7 | ** This file is part of the documentation of the Qt Toolkit.
|
---|
8 | **
|
---|
9 | ** $QT_BEGIN_LICENSE:FDL$
|
---|
10 | ** Commercial Usage
|
---|
11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
12 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
13 | ** Software or, alternatively, in accordance with the terms contained in a
|
---|
14 | ** written agreement between you and Nokia.
|
---|
15 | **
|
---|
16 | ** GNU Free Documentation License
|
---|
17 | ** Alternatively, this file may be used under the terms of the GNU Free
|
---|
18 | ** Documentation License version 1.3 as published by the Free Software
|
---|
19 | ** Foundation and appearing in the file included in the packaging of this
|
---|
20 | ** file.
|
---|
21 | **
|
---|
22 | ** If you have questions regarding the use of this file, please contact
|
---|
23 | ** Nokia at qt-info@nokia.com.
|
---|
24 | ** $QT_END_LICENSE$
|
---|
25 | **
|
---|
26 | ****************************************************************************/
|
---|
27 |
|
---|
28 | /*!
|
---|
29 | \example tools/contiguouscache
|
---|
30 | \title Contiguous Cache Example
|
---|
31 |
|
---|
32 | The Contiguous Cache example shows how to use QContiguousCache to manage memory usage for
|
---|
33 | very large models. In some environments memory is limited and, even when it
|
---|
34 | isn't, users still dislike an application using excessive memory.
|
---|
35 | Using QContiguousCache to manage a list, rather than loading
|
---|
36 | the entire list into memory, allows the application to limit the amount
|
---|
37 | of memory it uses, regardless of the size of the data set it accesses
|
---|
38 |
|
---|
39 | The simplest way to use QContiguousCache is to cache as items are requested. When
|
---|
40 | a view requests an item at row N it is also likely to ask for items at rows near
|
---|
41 | to N.
|
---|
42 |
|
---|
43 | \snippet examples/tools/contiguouscache/randomlistmodel.cpp 0
|
---|
44 |
|
---|
45 | After getting the row, the class determines if the row is in the bounds
|
---|
46 | of the contiguous cache's current range. It would have been equally valid to
|
---|
47 | simply have the following code instead.
|
---|
48 |
|
---|
49 | \code
|
---|
50 | while (row > m_rows.lastIndex())
|
---|
51 | m_rows.append(fetchWord(m_rows.lastIndex()+1);
|
---|
52 | while (row < m_rows.firstIndex())
|
---|
53 | m_rows.prepend(fetchWord(m_rows.firstIndex()-1);
|
---|
54 | \endcode
|
---|
55 |
|
---|
56 | However a list will often jump rows if the scroll bar is used directly, resulting in
|
---|
57 | the code above causing every row between the old and new rows to be fetched.
|
---|
58 |
|
---|
59 | Using QContiguousCache::lastIndex() and QContiguousCache::firstIndex() allows
|
---|
60 | the example to determine what part of the list the cache is currently caching.
|
---|
61 | These values don't represent the indexes into the cache's own memory, but rather
|
---|
62 | a virtual infinite array that the cache represents.
|
---|
63 |
|
---|
64 | By using QContiguousCache::append() and QContiguousCache::prepend() the code ensures
|
---|
65 | that items that may be still on the screen are not lost when the requested row
|
---|
66 | has not moved far from the current cache range. QContiguousCache::insert() can
|
---|
67 | potentially remove more than one item from the cache as QContiguousCache does not
|
---|
68 | allow for gaps. If your cache needs to quickly jump back and forth between
|
---|
69 | rows with significant gaps between them consider using QCache instead.
|
---|
70 |
|
---|
71 | And thats it. A perfectly reasonable cache, using minimal memory for a very large
|
---|
72 | list. In this case the accessor for getting the words into the cache
|
---|
73 | generates random information rather than fixed information. This allows you
|
---|
74 | to see how the cache range is kept for a local number of rows when running the
|
---|
75 | example.
|
---|
76 |
|
---|
77 | \snippet examples/tools/contiguouscache/randomlistmodel.cpp 1
|
---|
78 |
|
---|
79 | It is also worth considering pre-fetching items into the cache outside of the
|
---|
80 | application's paint routine. This can be done either with a separate thread
|
---|
81 | or using a QTimer to incrementally expand the range of the cache prior to
|
---|
82 | rows being requested out of the current cache range.
|
---|
83 | */
|
---|