|
1 /* |
|
2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 * Version : %version: 1 % |
|
17 */ |
|
18 #include "hgbuffermanager.h" |
|
19 #include <hgwidgets/hgcacheproxymodel.h> |
|
20 |
|
21 |
|
22 HgBufferManager::HgBufferManager( |
|
23 HgBufferManagerObserver* aObserver, |
|
24 int aBufferSize, |
|
25 int aBufferTreshold, |
|
26 int aInitialPosition, |
|
27 int aTotalCount ) |
|
28 : |
|
29 mObserver(aObserver), |
|
30 mBufferSize( aBufferSize ), |
|
31 mBufferTreshold( aBufferTreshold ), |
|
32 mBufferPosition( aInitialPosition ), |
|
33 mTotalCount( aTotalCount ) |
|
34 { |
|
35 ASSERT( mObserver != 0 ); |
|
36 mBufferPosition -= (mBufferSize / 2); |
|
37 |
|
38 if( mBufferPosition + mBufferSize > mTotalCount - 1 ){ |
|
39 mBufferPosition = (mTotalCount - 1) - mBufferSize; |
|
40 } |
|
41 |
|
42 if(mBufferPosition < 0 ){ |
|
43 mBufferPosition = 0; |
|
44 } |
|
45 |
|
46 mDiff = 0; |
|
47 //request Initial Buffer |
|
48 mRequestStart = mBufferPosition; |
|
49 mRequestCount = mBufferSize; |
|
50 |
|
51 calculate(); |
|
52 } |
|
53 |
|
54 |
|
55 HgBufferManager::~HgBufferManager() |
|
56 { |
|
57 } |
|
58 |
|
59 void HgBufferManager::resizeCache(int newSize, int newTreshold) |
|
60 { |
|
61 if (newTreshold != mBufferTreshold){ |
|
62 mBufferTreshold = newTreshold; |
|
63 } |
|
64 |
|
65 if (newSize!=mBufferSize){ |
|
66 // int pos = mBufferPosition + (mBufferSize / 2); |
|
67 |
|
68 int a = Max(0, mBufferPosition + mBufferSize/2 - newSize/2); |
|
69 int b = Min(a + newSize, mTotalCount); |
|
70 if ( b == mTotalCount){ |
|
71 a = mTotalCount - newSize; |
|
72 } |
|
73 |
|
74 int c = Max(0, mBufferPosition); |
|
75 int d = Min(c + mBufferSize, mTotalCount); |
|
76 if ( d == mTotalCount){ |
|
77 c = mTotalCount - mBufferSize; |
|
78 } |
|
79 |
|
80 if ( newSize>mBufferSize){ |
|
81 mObserver->request(a, c-1, HgCacheProxyModel::HgRequestOrderAscending); |
|
82 mObserver->request(d, b-1, HgCacheProxyModel::HgRequestOrderAscending); |
|
83 }else if ( newSize<mBufferSize){ |
|
84 mObserver->release(c, a-1); |
|
85 mObserver->release(b, d); |
|
86 } |
|
87 mBufferPosition = a; |
|
88 mBufferSize = newSize; |
|
89 } |
|
90 } |
|
91 |
|
92 void HgBufferManager::calculate() |
|
93 { |
|
94 HgCacheProxyModel::HgRequestOrder direction = HgCacheProxyModel::HgRequestOrderAscending; |
|
95 |
|
96 if(mResetOrdered){ |
|
97 mResetOrdered = false; |
|
98 } else { |
|
99 if(mDiff < 0){ |
|
100 mReleaseStart = mBufferPosition; |
|
101 mRequestStart = mBufferPosition + mBufferSize; |
|
102 direction = HgCacheProxyModel::HgRequestOrderAscending; |
|
103 } else if( mDiff > 0) { |
|
104 mReleaseStart = mBufferPosition + mBufferSize - mDiff; |
|
105 mRequestStart = mBufferPosition - mDiff; |
|
106 direction = HgCacheProxyModel::HgRequestOrderDescending; |
|
107 } |
|
108 } |
|
109 |
|
110 // Release |
|
111 int end = mReleaseStart + mReleaseCount < mTotalCount ? |
|
112 mReleaseStart + mReleaseCount: mTotalCount; |
|
113 end--; |
|
114 if(end >= mReleaseStart ){ |
|
115 mObserver->release(mReleaseStart, end); |
|
116 } |
|
117 |
|
118 mReleaseCount = 0; |
|
119 |
|
120 // Request |
|
121 end = mRequestStart + mRequestCount < mTotalCount ? |
|
122 mRequestStart + mRequestCount : mTotalCount; |
|
123 |
|
124 end--; |
|
125 if(end >= mRequestStart ){ |
|
126 mObserver->request(mRequestStart, end, direction); |
|
127 } |
|
128 |
|
129 mRequestCount = 0; |
|
130 |
|
131 // Move Buffer |
|
132 mBufferPosition -= mDiff; |
|
133 // Reset Diff |
|
134 mDiff = 0; |
|
135 } |
|
136 |
|
137 // ----------------------------------------------------------------------------- |
|
138 // BufferManager::SetPosition() |
|
139 // ----------------------------------------------------------------------------- |
|
140 // |
|
141 void HgBufferManager::setPosition( int aIndex ) |
|
142 { |
|
143 // If all the items fit in the buffer no need to move the buffer |
|
144 if(mTotalCount <= mBufferSize) |
|
145 return; |
|
146 |
|
147 bool forceUpdate = false; |
|
148 aIndex -= mBufferSize / 2; // normalize index to Buffer start |
|
149 |
|
150 if(aIndex < 0){ |
|
151 aIndex = 0; |
|
152 forceUpdate = true; |
|
153 }else if( aIndex > mTotalCount - mBufferSize ){ |
|
154 aIndex = mTotalCount - mBufferSize; |
|
155 forceUpdate = true; |
|
156 } |
|
157 |
|
158 mDiff = mBufferPosition - aIndex; |
|
159 |
|
160 // Too large change reset whole buffer |
|
161 if( mDiff >= mBufferSize || -mDiff >= mBufferSize || mResetOrdered ) { |
|
162 resetBuffer(aIndex + (mBufferSize/2), mTotalCount); |
|
163 } else if( mDiff >= mBufferTreshold ) { // Move Up |
|
164 mRequestCount = mDiff; |
|
165 mReleaseCount = mDiff; |
|
166 calculate(); |
|
167 } else if ( -mDiff >= mBufferTreshold ) {// Move Down |
|
168 mRequestCount = -mDiff; |
|
169 mReleaseCount = -mDiff; |
|
170 calculate(); |
|
171 } else if( forceUpdate && mDiff ) { // Top or bottom has been reached |
|
172 int diff = mDiff < 0 ? -mDiff : mDiff; |
|
173 mRequestCount = diff; |
|
174 mReleaseCount = diff; |
|
175 calculate(); |
|
176 } |
|
177 } |
|
178 |
|
179 // ----------------------------------------------------------------------------- |
|
180 // BufferManager::ResetBuffer() |
|
181 // ----------------------------------------------------------------------------- |
|
182 // |
|
183 void HgBufferManager::resetBuffer( int aPosition, int aTotalCount) |
|
184 { |
|
185 if( !mResetOrdered ){ |
|
186 // release Old buffer |
|
187 mReleaseStart = mBufferPosition; |
|
188 mReleaseCount = mBufferSize; |
|
189 } |
|
190 |
|
191 // set position and count |
|
192 mBufferPosition = aPosition - (mBufferSize / 2); |
|
193 mTotalCount = aTotalCount; |
|
194 mDiff = 0; |
|
195 |
|
196 if( mBufferPosition + mBufferSize > mTotalCount - 1 ){ |
|
197 mBufferPosition = mTotalCount - mBufferSize; |
|
198 } |
|
199 |
|
200 if(mBufferPosition < 0 ){ |
|
201 mBufferPosition = 0; |
|
202 } |
|
203 |
|
204 //request new Buffer |
|
205 mRequestStart = mBufferPosition; |
|
206 mRequestCount = mBufferSize; |
|
207 mResetOrdered = true; |
|
208 calculate(); |
|
209 } |
|
210 |
|
211 void HgBufferManager::itemCountChanged( int aIndex, |
|
212 bool aRemoved, |
|
213 int aNewTotalCount ) |
|
214 { |
|
215 Q_UNUSED(aIndex); |
|
216 Q_UNUSED(aRemoved); |
|
217 //release all, to make sure that no old items are skipped |
|
218 mObserver->release(0, aNewTotalCount); |
|
219 resetBuffer(mBufferPosition + (mBufferSize / 2), aNewTotalCount); |
|
220 } |
|
221 //eof |