ganeswidgets/src/hgscrollbuffermanager.cpp
author hgs
Wed, 06 Oct 2010 14:53:41 +0300
changeset 20 a60f8b6b1d32
parent 6 1cdcc61142d2
permissions -rw-r--r--
201039
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6
hgs
parents:
diff changeset
     1
/*
hgs
parents:
diff changeset
     2
* Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
hgs
parents:
diff changeset
     3
* All rights reserved.
hgs
parents:
diff changeset
     4
* This component and the accompanying materials are made available
hgs
parents:
diff changeset
     5
* under the terms of "Eclipse Public License v1.0"
hgs
parents:
diff changeset
     6
* which accompanies this distribution, and is available
hgs
parents:
diff changeset
     7
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
hgs
parents:
diff changeset
     8
*
hgs
parents:
diff changeset
     9
* Initial Contributors:
hgs
parents:
diff changeset
    10
* Nokia Corporation - initial contribution.
hgs
parents:
diff changeset
    11
*
hgs
parents:
diff changeset
    12
* Contributors:
hgs
parents:
diff changeset
    13
*
hgs
parents:
diff changeset
    14
* Description:
hgs
parents:
diff changeset
    15
*
hgs
parents:
diff changeset
    16
*/
hgs
parents:
diff changeset
    17
hgs
parents:
diff changeset
    18
hgs
parents:
diff changeset
    19
#include <QTimer>
hgs
parents:
diff changeset
    20
hgs
parents:
diff changeset
    21
#include "hgscrollbuffermanager.h"
hgs
parents:
diff changeset
    22
#include "trace.h"
hgs
parents:
diff changeset
    23
hgs
parents:
diff changeset
    24
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
    25
// HgScrollBufferManager::HgScrollBufferManager()
hgs
parents:
diff changeset
    26
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
    27
//
hgs
parents:
diff changeset
    28
HgScrollBufferManager::HgScrollBufferManager(
hgs
parents:
diff changeset
    29
        int bufferSize,
hgs
parents:
diff changeset
    30
        int bufferTreshold,
hgs
parents:
diff changeset
    31
        int initialPosition,
hgs
parents:
diff changeset
    32
        int totalCount )
hgs
parents:
diff changeset
    33
:   mBufferSize( bufferSize ),
hgs
parents:
diff changeset
    34
    mBufferTreshold( bufferTreshold ),
hgs
parents:
diff changeset
    35
    mBufferPosition( initialPosition ),
hgs
parents:
diff changeset
    36
    mDiff(0),
hgs
parents:
diff changeset
    37
    mTotalCount( totalCount ),
hgs
parents:
diff changeset
    38
    mResetOrdered(false),
hgs
parents:
diff changeset
    39
    mRequestStart(0),
hgs
parents:
diff changeset
    40
    mRequestCount(0),
hgs
parents:
diff changeset
    41
    mReleaseStart(0),
hgs
parents:
diff changeset
    42
    mReleaseCount(0),
hgs
parents:
diff changeset
    43
    mFirstTime(true)
hgs
parents:
diff changeset
    44
    {
hgs
parents:
diff changeset
    45
    init();
hgs
parents:
diff changeset
    46
    }
hgs
parents:
diff changeset
    47
hgs
parents:
diff changeset
    48
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
    49
// HgScrollBufferManager::init()
hgs
parents:
diff changeset
    50
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
    51
//
hgs
parents:
diff changeset
    52
void HgScrollBufferManager::init()
hgs
parents:
diff changeset
    53
    {
hgs
parents:
diff changeset
    54
    mResetOrdered = ETrue;
hgs
parents:
diff changeset
    55
    mTimer.setSingleShot(true);
hgs
parents:
diff changeset
    56
    connect(&mTimer, SIGNAL(timeout()), this, SLOT(timeout()));
hgs
parents:
diff changeset
    57
    }
hgs
parents:
diff changeset
    58
hgs
parents:
diff changeset
    59
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
    60
// HgScrollBufferManager::~HgScrollBufferManager()
hgs
parents:
diff changeset
    61
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
    62
//
hgs
parents:
diff changeset
    63
HgScrollBufferManager::~HgScrollBufferManager()
hgs
parents:
diff changeset
    64
    {
hgs
parents:
diff changeset
    65
    mTimer.stop();
hgs
parents:
diff changeset
    66
    }
hgs
parents:
diff changeset
    67
hgs
parents:
diff changeset
    68
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
    69
// HgScrollBufferManager::resetBuffer()
hgs
parents:
diff changeset
    70
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
    71
//
hgs
parents:
diff changeset
    72
void HgScrollBufferManager::resetBuffer( int aPosition, int totalCount )
hgs
parents:
diff changeset
    73
    {
hgs
parents:
diff changeset
    74
    if( !mResetOrdered )
hgs
parents:
diff changeset
    75
        {
hgs
parents:
diff changeset
    76
        // release Old buffer
hgs
parents:
diff changeset
    77
        mReleaseStart = mBufferPosition;
hgs
parents:
diff changeset
    78
        mReleaseCount = mBufferSize;
hgs
parents:
diff changeset
    79
        }
hgs
parents:
diff changeset
    80
hgs
parents:
diff changeset
    81
    // set position and count
hgs
parents:
diff changeset
    82
    mBufferPosition = aPosition - (mBufferSize / 2);
hgs
parents:
diff changeset
    83
    mTotalCount = totalCount;
hgs
parents:
diff changeset
    84
    mDiff = 0;
hgs
parents:
diff changeset
    85
hgs
parents:
diff changeset
    86
    if( mBufferPosition + mBufferSize > mTotalCount - 1 )
hgs
parents:
diff changeset
    87
        {
hgs
parents:
diff changeset
    88
        mBufferPosition = mTotalCount - mBufferSize;
hgs
parents:
diff changeset
    89
        }
hgs
parents:
diff changeset
    90
hgs
parents:
diff changeset
    91
    if(mBufferPosition < 0 )
hgs
parents:
diff changeset
    92
        {
hgs
parents:
diff changeset
    93
        mBufferPosition = 0;
hgs
parents:
diff changeset
    94
        }
hgs
parents:
diff changeset
    95
hgs
parents:
diff changeset
    96
    //request new Buffer
hgs
parents:
diff changeset
    97
    mRequestStart = mBufferPosition;
hgs
parents:
diff changeset
    98
    mRequestCount = mBufferSize;
hgs
parents:
diff changeset
    99
    mResetOrdered = ETrue;
hgs
parents:
diff changeset
   100
    asyncUpdate();
hgs
parents:
diff changeset
   101
    }
hgs
parents:
diff changeset
   102
hgs
parents:
diff changeset
   103
void HgScrollBufferManager::scrollPositionChanged( int newPosition )
hgs
parents:
diff changeset
   104
    {
hgs
parents:
diff changeset
   105
    // If all the items fit in the buffer no need to move the buffer.
hgs
parents:
diff changeset
   106
    if( mTotalCount <= mBufferSize ) return;
hgs
parents:
diff changeset
   107
hgs
parents:
diff changeset
   108
    bool forceUpdate = EFalse;
hgs
parents:
diff changeset
   109
    newPosition -= mBufferSize / 2; // normalize index to Buffer start
hgs
parents:
diff changeset
   110
hgs
parents:
diff changeset
   111
    if(newPosition < 0)
hgs
parents:
diff changeset
   112
        {
hgs
parents:
diff changeset
   113
        newPosition = 0;
hgs
parents:
diff changeset
   114
        forceUpdate = ETrue;
hgs
parents:
diff changeset
   115
        }
hgs
parents:
diff changeset
   116
    else if( newPosition > mTotalCount - mBufferSize )
hgs
parents:
diff changeset
   117
        {
hgs
parents:
diff changeset
   118
        newPosition = mTotalCount - mBufferSize;
hgs
parents:
diff changeset
   119
        forceUpdate = ETrue;
hgs
parents:
diff changeset
   120
        }
hgs
parents:
diff changeset
   121
hgs
parents:
diff changeset
   122
    mDiff = mBufferPosition - newPosition;
hgs
parents:
diff changeset
   123
hgs
parents:
diff changeset
   124
    // Too large change reset whole buffer
hgs
parents:
diff changeset
   125
    if( mDiff >= mBufferSize || -mDiff >= mBufferSize || mResetOrdered )
hgs
parents:
diff changeset
   126
        {
hgs
parents:
diff changeset
   127
        resetBuffer(newPosition + (mBufferSize/2), mTotalCount );
hgs
parents:
diff changeset
   128
        }
hgs
parents:
diff changeset
   129
    // Move Up
hgs
parents:
diff changeset
   130
    else if( mDiff >= mBufferTreshold )
hgs
parents:
diff changeset
   131
        {
hgs
parents:
diff changeset
   132
        mRequestCount = mDiff;
hgs
parents:
diff changeset
   133
        mReleaseCount = mDiff;
hgs
parents:
diff changeset
   134
        asyncUpdate();
hgs
parents:
diff changeset
   135
        }
hgs
parents:
diff changeset
   136
    // Move Down
hgs
parents:
diff changeset
   137
    else if( -mDiff >= mBufferTreshold )
hgs
parents:
diff changeset
   138
        {
hgs
parents:
diff changeset
   139
        mRequestCount = -mDiff;
hgs
parents:
diff changeset
   140
        mReleaseCount = -mDiff;
hgs
parents:
diff changeset
   141
        asyncUpdate();
hgs
parents:
diff changeset
   142
        }
hgs
parents:
diff changeset
   143
    // Top or bottom has been reached
hgs
parents:
diff changeset
   144
    else if( forceUpdate && mDiff )
hgs
parents:
diff changeset
   145
        {
hgs
parents:
diff changeset
   146
        int diff = mDiff < 0 ? -mDiff : mDiff;
hgs
parents:
diff changeset
   147
        mRequestCount = diff;
hgs
parents:
diff changeset
   148
        mReleaseCount = diff;
hgs
parents:
diff changeset
   149
        asyncUpdate();
hgs
parents:
diff changeset
   150
        }
hgs
parents:
diff changeset
   151
    }
hgs
parents:
diff changeset
   152
hgs
parents:
diff changeset
   153
void HgScrollBufferManager::timeout()
hgs
parents:
diff changeset
   154
{
hgs
parents:
diff changeset
   155
    mFirstTime = false;
hgs
parents:
diff changeset
   156
    
hgs
parents:
diff changeset
   157
    if(mResetOrdered)
hgs
parents:
diff changeset
   158
        {
hgs
parents:
diff changeset
   159
        mResetOrdered = EFalse;
hgs
parents:
diff changeset
   160
        }
hgs
parents:
diff changeset
   161
    else
hgs
parents:
diff changeset
   162
        {
hgs
parents:
diff changeset
   163
        if(mDiff < 0)
hgs
parents:
diff changeset
   164
            {
hgs
parents:
diff changeset
   165
            mReleaseStart = mBufferPosition;
hgs
parents:
diff changeset
   166
            mRequestStart = mBufferPosition + mBufferSize;
hgs
parents:
diff changeset
   167
            }
hgs
parents:
diff changeset
   168
        else if( mDiff > 0)
hgs
parents:
diff changeset
   169
            {
hgs
parents:
diff changeset
   170
            mReleaseStart = mBufferPosition + mBufferSize - mDiff;
hgs
parents:
diff changeset
   171
            mRequestStart = mBufferPosition - mDiff;
hgs
parents:
diff changeset
   172
            }
hgs
parents:
diff changeset
   173
        }
hgs
parents:
diff changeset
   174
hgs
parents:
diff changeset
   175
    // Release
hgs
parents:
diff changeset
   176
    int end = mReleaseStart + mReleaseCount < mTotalCount ?
hgs
parents:
diff changeset
   177
        mReleaseStart + mReleaseCount: mTotalCount;
hgs
parents:
diff changeset
   178
    end--;
hgs
parents:
diff changeset
   179
    if(end >= mReleaseStart )
hgs
parents:
diff changeset
   180
        {
hgs
parents:
diff changeset
   181
        emit releaseItems( mReleaseStart, end );
hgs
parents:
diff changeset
   182
        }
hgs
parents:
diff changeset
   183
hgs
parents:
diff changeset
   184
    mReleaseCount = 0;
hgs
parents:
diff changeset
   185
hgs
parents:
diff changeset
   186
    // Request
hgs
parents:
diff changeset
   187
    end = mRequestStart + mRequestCount < mTotalCount ?
hgs
parents:
diff changeset
   188
        mRequestStart + mRequestCount : mTotalCount;
hgs
parents:
diff changeset
   189
hgs
parents:
diff changeset
   190
    end--;
hgs
parents:
diff changeset
   191
    if(end >= mRequestStart )
hgs
parents:
diff changeset
   192
        {
hgs
parents:
diff changeset
   193
        emit requestItems( mRequestStart, end );
hgs
parents:
diff changeset
   194
        }
hgs
parents:
diff changeset
   195
hgs
parents:
diff changeset
   196
    mRequestCount = 0;
hgs
parents:
diff changeset
   197
hgs
parents:
diff changeset
   198
    // Move Buffer
hgs
parents:
diff changeset
   199
    mBufferPosition -= mDiff;
hgs
parents:
diff changeset
   200
    // Reset Diff
hgs
parents:
diff changeset
   201
    mDiff = 0;
hgs
parents:
diff changeset
   202
}
hgs
parents:
diff changeset
   203
hgs
parents:
diff changeset
   204
bool HgScrollBufferManager::positionInsideBuffer( int position )
hgs
parents:
diff changeset
   205
{
hgs
parents:
diff changeset
   206
    return position >= mBufferPosition && position <= (mBufferPosition+mBufferSize);
hgs
parents:
diff changeset
   207
}
hgs
parents:
diff changeset
   208
hgs
parents:
diff changeset
   209
void HgScrollBufferManager::asyncUpdate()
hgs
parents:
diff changeset
   210
{
hgs
parents:
diff changeset
   211
    if (!mTimer.isActive() && mFirstTime) {
hgs
parents:
diff changeset
   212
        mTimer.start(0);
hgs
parents:
diff changeset
   213
    } else if (!mTimer.isActive()){
hgs
parents:
diff changeset
   214
        timeout();
hgs
parents:
diff changeset
   215
    }
hgs
parents:
diff changeset
   216
}
hgs
parents:
diff changeset
   217
hgs
parents:
diff changeset
   218
void HgScrollBufferManager::currentBuffer(int& bufferStart, int& bufferEnd)
hgs
parents:
diff changeset
   219
{
hgs
parents:
diff changeset
   220
    bufferStart = mBufferPosition;
hgs
parents:
diff changeset
   221
    bufferEnd = mBufferPosition+mBufferSize > mTotalCount-1 ?
hgs
parents:
diff changeset
   222
        mTotalCount-1 : mBufferPosition+mBufferSize;
hgs
parents:
diff changeset
   223
}
hgs
parents:
diff changeset
   224
hgs
parents:
diff changeset
   225
void HgScrollBufferManager::addItems(int start, int end)
hgs
parents:
diff changeset
   226
{
hgs
parents:
diff changeset
   227
    FUNC_LOG;
hgs
parents:
diff changeset
   228
hgs
parents:
diff changeset
   229
    mTotalCount += (end-start+1);
hgs
parents:
diff changeset
   230
    int lastBufferItem = mBufferPosition+mBufferSize-1;
hgs
parents:
diff changeset
   231
hgs
parents:
diff changeset
   232
    if (start < mBufferPosition) {
hgs
parents:
diff changeset
   233
        simpleAddItems(start, end);
hgs
parents:
diff changeset
   234
        // New items push the buffer forward, items inside the buffer do not change
hgs
parents:
diff changeset
   235
    }
hgs
parents:
diff changeset
   236
    // Check buffer higher limit
hgs
parents:
diff changeset
   237
    else if (start <= lastBufferItem && end > lastBufferItem) {
hgs
parents:
diff changeset
   238
        simpleAddItems(start, lastBufferItem);
hgs
parents:
diff changeset
   239
        // Items added after the buffer are ignored
hgs
parents:
diff changeset
   240
    }
hgs
parents:
diff changeset
   241
    else {
hgs
parents:
diff changeset
   242
        simpleAddItems(start, end);
hgs
parents:
diff changeset
   243
    }
hgs
parents:
diff changeset
   244
}
hgs
parents:
diff changeset
   245
hgs
parents:
diff changeset
   246
void HgScrollBufferManager::removeItems(int start, int end)
hgs
parents:
diff changeset
   247
{
hgs
parents:
diff changeset
   248
    FUNC_LOG;
hgs
parents:
diff changeset
   249
hgs
parents:
diff changeset
   250
    int lastBufferItem = mBufferPosition+mBufferSize-1;
hgs
parents:
diff changeset
   251
    int removedItemCount = end-start+1;
hgs
parents:
diff changeset
   252
hgs
parents:
diff changeset
   253
    if (mTotalCount < mBufferSize) {
hgs
parents:
diff changeset
   254
        // Do nothing
hgs
parents:
diff changeset
   255
    }
hgs
parents:
diff changeset
   256
    else if (start > lastBufferItem) {
hgs
parents:
diff changeset
   257
        // Do nothing
hgs
parents:
diff changeset
   258
    }
hgs
parents:
diff changeset
   259
    else if (end < mBufferPosition) {
hgs
parents:
diff changeset
   260
        mTotalCount = mTotalCount-removedItemCount;
hgs
parents:
diff changeset
   261
        simpleRemoveItems(start, end);
hgs
parents:
diff changeset
   262
    }
hgs
parents:
diff changeset
   263
    else if (start < mBufferPosition && end > lastBufferItem) {
hgs
parents:
diff changeset
   264
        mTotalCount = mTotalCount-removedItemCount;
hgs
parents:
diff changeset
   265
        mBufferPosition = qBound(0, mBufferPosition, mTotalCount-mBufferSize);
hgs
parents:
diff changeset
   266
        resetBuffer(mBufferPosition, mTotalCount);
hgs
parents:
diff changeset
   267
    }
hgs
parents:
diff changeset
   268
    // Check buffer higher limit
hgs
parents:
diff changeset
   269
    else if (start <= lastBufferItem && end > lastBufferItem) {
hgs
parents:
diff changeset
   270
        mTotalCount = mTotalCount-(end-lastBufferItem);
hgs
parents:
diff changeset
   271
        simpleRemoveItems(lastBufferItem+1, end);
hgs
parents:
diff changeset
   272
        mTotalCount = mTotalCount-(lastBufferItem-start+1);
hgs
parents:
diff changeset
   273
        simpleRemoveItems(start, lastBufferItem);
hgs
parents:
diff changeset
   274
        // Order does matter
hgs
parents:
diff changeset
   275
        mTotalCount = mTotalCount-(end-lastBufferItem);
hgs
parents:
diff changeset
   276
        simpleRemoveItems(lastBufferItem+1, end);
hgs
parents:
diff changeset
   277
        mTotalCount = mTotalCount-(lastBufferItem-start+1);
hgs
parents:
diff changeset
   278
        simpleRemoveItems(start, lastBufferItem);
hgs
parents:
diff changeset
   279
    }
hgs
parents:
diff changeset
   280
    // Check buffer lower limit
hgs
parents:
diff changeset
   281
    else if (start < mBufferPosition && end >= mBufferPosition) {
hgs
parents:
diff changeset
   282
        // Order does matter
hgs
parents:
diff changeset
   283
        mTotalCount = mTotalCount-(end-mBufferPosition+1);
hgs
parents:
diff changeset
   284
        simpleRemoveItems(mBufferPosition, end);
hgs
parents:
diff changeset
   285
        mTotalCount = mTotalCount-(mBufferPosition-start);
hgs
parents:
diff changeset
   286
        simpleRemoveItems(start, mBufferPosition-1);
hgs
parents:
diff changeset
   287
    }
hgs
parents:
diff changeset
   288
    else {
hgs
parents:
diff changeset
   289
        mTotalCount = mTotalCount-removedItemCount;
hgs
parents:
diff changeset
   290
        simpleRemoveItems(start, end);
hgs
parents:
diff changeset
   291
    }
hgs
parents:
diff changeset
   292
}
hgs
parents:
diff changeset
   293
hgs
parents:
diff changeset
   294
void HgScrollBufferManager::moveItems(int start, int end, int target)
hgs
parents:
diff changeset
   295
{
hgs
parents:
diff changeset
   296
    int lastBufferItem = mBufferPosition+mBufferSize-1;
hgs
parents:
diff changeset
   297
hgs
parents:
diff changeset
   298
    INFO("Move" << start << "-" << end << "to" << target << ",buffer:" << mBufferPosition << "-" << lastBufferItem << "total count:" << mTotalCount);
hgs
parents:
diff changeset
   299
hgs
parents:
diff changeset
   300
    if (mTotalCount < mBufferSize) {
hgs
parents:
diff changeset
   301
        // Do nothing
hgs
parents:
diff changeset
   302
    }
hgs
parents:
diff changeset
   303
    else if (start < mBufferPosition && end > lastBufferItem) {
hgs
parents:
diff changeset
   304
        resetBuffer(mBufferPosition, mTotalCount);
hgs
parents:
diff changeset
   305
    }
hgs
parents:
diff changeset
   306
    else if (start > lastBufferItem && target > lastBufferItem) {
hgs
parents:
diff changeset
   307
        // Do nothing
hgs
parents:
diff changeset
   308
    }
hgs
parents:
diff changeset
   309
    else if (start > lastBufferItem && target < mBufferPosition) {
hgs
parents:
diff changeset
   310
        simpleAddItems(start, end);
hgs
parents:
diff changeset
   311
    }
hgs
parents:
diff changeset
   312
    else if (end < mBufferPosition && target < mBufferPosition) {
hgs
parents:
diff changeset
   313
        // Do nothing
hgs
parents:
diff changeset
   314
    }
hgs
parents:
diff changeset
   315
    else if (end < mBufferPosition && target > lastBufferItem) {
hgs
parents:
diff changeset
   316
        simpleRemoveItems(start, end);
hgs
parents:
diff changeset
   317
    }
hgs
parents:
diff changeset
   318
    else if (start >= mBufferPosition && end <= lastBufferItem &&
hgs
parents:
diff changeset
   319
             target >= mBufferPosition && target <= lastBufferItem) {
hgs
parents:
diff changeset
   320
        // Do nothing
hgs
parents:
diff changeset
   321
    }
hgs
parents:
diff changeset
   322
    else {
hgs
parents:
diff changeset
   323
        // Rare and complicated use cases: reset the whole buffer
hgs
parents:
diff changeset
   324
        resetBuffer(mBufferPosition, mTotalCount);
hgs
parents:
diff changeset
   325
    }
hgs
parents:
diff changeset
   326
}
hgs
parents:
diff changeset
   327
hgs
parents:
diff changeset
   328
void HgScrollBufferManager::flushRequestBuffers()
hgs
parents:
diff changeset
   329
{
hgs
parents:
diff changeset
   330
    FUNC_LOG;
hgs
parents:
diff changeset
   331
hgs
parents:
diff changeset
   332
    qSort(mReleaseBuffer);
hgs
parents:
diff changeset
   333
    int releaseCount = mReleaseBuffer.count();
hgs
parents:
diff changeset
   334
    int lastReleased = -1;
hgs
parents:
diff changeset
   335
    for (int i = 0; i < releaseCount; i++) {
hgs
parents:
diff changeset
   336
        UpdatePair update = mReleaseBuffer.at(i);
hgs
parents:
diff changeset
   337
        emit releaseItems(qMax(lastReleased+1, update.start()), update.end());
hgs
parents:
diff changeset
   338
        lastReleased = update.end();
hgs
parents:
diff changeset
   339
    }
hgs
parents:
diff changeset
   340
    mReleaseBuffer.clear();
hgs
parents:
diff changeset
   341
hgs
parents:
diff changeset
   342
    qSort(mRequestBuffer);
hgs
parents:
diff changeset
   343
    int requestCount = mRequestBuffer.count();
hgs
parents:
diff changeset
   344
    int lastRequested = -1;
hgs
parents:
diff changeset
   345
    for (int i = 0; i < requestCount; i++) {
hgs
parents:
diff changeset
   346
        UpdatePair update = mRequestBuffer.at(i);
hgs
parents:
diff changeset
   347
        emit requestItems(qMax(lastRequested+1, update.start()), update.end());
hgs
parents:
diff changeset
   348
        lastRequested = update.end();
hgs
parents:
diff changeset
   349
    }
hgs
parents:
diff changeset
   350
    mRequestBuffer.clear();
hgs
parents:
diff changeset
   351
}
hgs
parents:
diff changeset
   352
hgs
parents:
diff changeset
   353
int HgScrollBufferManager::changeBufferPosition(int newPos)
hgs
parents:
diff changeset
   354
{
hgs
parents:
diff changeset
   355
    FUNC_LOG;
hgs
parents:
diff changeset
   356
    INFO("Change buffer position to" << newPos << "total count:" << mTotalCount);
hgs
parents:
diff changeset
   357
    HANDLE_ERROR_BOOL((newPos >= 0));
hgs
parents:
diff changeset
   358
    HANDLE_ERROR_BOOL((newPos+mBufferSize <= mTotalCount));
hgs
parents:
diff changeset
   359
hgs
parents:
diff changeset
   360
    int bufferShift = newPos-mBufferPosition;
hgs
parents:
diff changeset
   361
    if (bufferShift > 0) {
hgs
parents:
diff changeset
   362
        mRequestBuffer.shiftRight(mBufferPosition, bufferShift);
hgs
parents:
diff changeset
   363
        mReleaseBuffer.shiftRight(mBufferPosition, bufferShift);
hgs
parents:
diff changeset
   364
    }
hgs
parents:
diff changeset
   365
    else if (bufferShift < 0) {
hgs
parents:
diff changeset
   366
        mRequestBuffer.shiftLeft(mBufferPosition, -bufferShift);
hgs
parents:
diff changeset
   367
        mReleaseBuffer.shiftLeft(mBufferPosition, -bufferShift);
hgs
parents:
diff changeset
   368
    }
hgs
parents:
diff changeset
   369
    mBufferPosition = newPos;
hgs
parents:
diff changeset
   370
    return bufferShift;
hgs
parents:
diff changeset
   371
}
hgs
parents:
diff changeset
   372
hgs
parents:
diff changeset
   373
/**
hgs
parents:
diff changeset
   374
    This function manages only simple item additions: all items are either
hgs
parents:
diff changeset
   375
    outside the buffer or inside it.
hgs
parents:
diff changeset
   376
    Firs call prepare, then update model, then call fecth.
hgs
parents:
diff changeset
   377
*/
hgs
parents:
diff changeset
   378
void HgScrollBufferManager::simpleAddItems(int start, int end)
hgs
parents:
diff changeset
   379
{
hgs
parents:
diff changeset
   380
    FUNC_LOG;
hgs
parents:
diff changeset
   381
hgs
parents:
diff changeset
   382
    int lastBufferItem = mBufferPosition+mBufferSize-1;
hgs
parents:
diff changeset
   383
    int numAddedItems = end-start+1; // [start, end] inclusive
hgs
parents:
diff changeset
   384
hgs
parents:
diff changeset
   385
    if (mTotalCount < mBufferSize) {
hgs
parents:
diff changeset
   386
        appendRequestBuffer(start, numAddedItems);
hgs
parents:
diff changeset
   387
    }
hgs
parents:
diff changeset
   388
    else if (start > lastBufferItem) {
hgs
parents:
diff changeset
   389
        // Do nothing
hgs
parents:
diff changeset
   390
    }
hgs
parents:
diff changeset
   391
    else if (start <= mBufferPosition) {
hgs
parents:
diff changeset
   392
        changeBufferPosition(mBufferPosition+numAddedItems);
hgs
parents:
diff changeset
   393
        // No need to fetch items, the indexes just change
hgs
parents:
diff changeset
   394
    }
hgs
parents:
diff changeset
   395
    else {
hgs
parents:
diff changeset
   396
        // free from end
hgs
parents:
diff changeset
   397
        appendReleaseBuffer(lastBufferItem+1-numAddedItems, numAddedItems);
hgs
parents:
diff changeset
   398
        mReleaseBuffer.shiftRight(start, numAddedItems);
hgs
parents:
diff changeset
   399
        mRequestBuffer.shiftRight(start, numAddedItems);
hgs
parents:
diff changeset
   400
        appendRequestBuffer(start, numAddedItems);
hgs
parents:
diff changeset
   401
    }
hgs
parents:
diff changeset
   402
}
hgs
parents:
diff changeset
   403
hgs
parents:
diff changeset
   404
/**
hgs
parents:
diff changeset
   405
    This function manages only simple item removals: all items are either
hgs
parents:
diff changeset
   406
    outside the buffer or inside it.
hgs
parents:
diff changeset
   407
    Firs call prepare, then update model, then call fecth.
hgs
parents:
diff changeset
   408
*/
hgs
parents:
diff changeset
   409
void HgScrollBufferManager::simpleRemoveItems(int start, int end)
hgs
parents:
diff changeset
   410
{
hgs
parents:
diff changeset
   411
    FUNC_LOG;
hgs
parents:
diff changeset
   412
hgs
parents:
diff changeset
   413
    int lastBufferItem = mBufferPosition+mBufferSize-1;
hgs
parents:
diff changeset
   414
    int numRemovedItems = end-start+1; // [start, end] inclusive
hgs
parents:
diff changeset
   415
hgs
parents:
diff changeset
   416
    if (start > lastBufferItem) {
hgs
parents:
diff changeset
   417
        // Do nothing
hgs
parents:
diff changeset
   418
    }
hgs
parents:
diff changeset
   419
    else if (end < mBufferPosition) {
hgs
parents:
diff changeset
   420
        changeBufferPosition(qMax(0, mBufferPosition-numRemovedItems));
hgs
parents:
diff changeset
   421
        // No need to fetch items, the indexes just change
hgs
parents:
diff changeset
   422
    }
hgs
parents:
diff changeset
   423
    else {
hgs
parents:
diff changeset
   424
        if (mTotalCount < mBufferPosition+mBufferSize) {
hgs
parents:
diff changeset
   425
            // Buffer is at the end of items
hgs
parents:
diff changeset
   426
            int bufferShift = changeBufferPosition(qMax(0, mTotalCount-mBufferSize));
hgs
parents:
diff changeset
   427
            // Fetch from beginning
hgs
parents:
diff changeset
   428
            // Releasing removed items has been done outside this class
hgs
parents:
diff changeset
   429
            appendRequestBuffer(mBufferPosition, qAbs(bufferShift));
hgs
parents:
diff changeset
   430
        }
hgs
parents:
diff changeset
   431
        else {
hgs
parents:
diff changeset
   432
            // Fetch from end
hgs
parents:
diff changeset
   433
            appendRequestBuffer(lastBufferItem+1-numRemovedItems, numRemovedItems);
hgs
parents:
diff changeset
   434
        }
hgs
parents:
diff changeset
   435
    }
hgs
parents:
diff changeset
   436
}
hgs
parents:
diff changeset
   437
hgs
parents:
diff changeset
   438
void HgScrollBufferManager::appendRequestBuffer(int start, int count)
hgs
parents:
diff changeset
   439
{
hgs
parents:
diff changeset
   440
    FUNC_LOG;
hgs
parents:
diff changeset
   441
    INFO("Request items" << start << ":" << count)
hgs
parents:
diff changeset
   442
hgs
parents:
diff changeset
   443
    mRequestBuffer.add(start, count);
hgs
parents:
diff changeset
   444
    mReleaseBuffer.remove(start, count);
hgs
parents:
diff changeset
   445
}
hgs
parents:
diff changeset
   446
hgs
parents:
diff changeset
   447
void HgScrollBufferManager::appendReleaseBuffer(int start, int count)
hgs
parents:
diff changeset
   448
{
hgs
parents:
diff changeset
   449
    FUNC_LOG;
hgs
parents:
diff changeset
   450
    INFO("Release items" << start << ":" << count)
hgs
parents:
diff changeset
   451
hgs
parents:
diff changeset
   452
    mReleaseBuffer.add(start, count);
hgs
parents:
diff changeset
   453
    mRequestBuffer.remove(start, count);
hgs
parents:
diff changeset
   454
}
hgs
parents:
diff changeset
   455
hgs
parents:
diff changeset
   456
UpdatePair::UpdatePair(int start, int count) : mStart(start), mCount(count)
hgs
parents:
diff changeset
   457
{
hgs
parents:
diff changeset
   458
    HANDLE_ERROR_BOOL(mCount > 0);
hgs
parents:
diff changeset
   459
}
hgs
parents:
diff changeset
   460
hgs
parents:
diff changeset
   461
int UpdatePair::start() const
hgs
parents:
diff changeset
   462
{
hgs
parents:
diff changeset
   463
    return mStart;
hgs
parents:
diff changeset
   464
}
hgs
parents:
diff changeset
   465
hgs
parents:
diff changeset
   466
int UpdatePair::end() const
hgs
parents:
diff changeset
   467
{
hgs
parents:
diff changeset
   468
    return mStart+mCount-1;
hgs
parents:
diff changeset
   469
}
hgs
parents:
diff changeset
   470
hgs
parents:
diff changeset
   471
bool UpdatePair::adjacent(int start, int count) const
hgs
parents:
diff changeset
   472
{
hgs
parents:
diff changeset
   473
    if (start+count < mStart) return false;
hgs
parents:
diff changeset
   474
    if (start > mStart+mCount) return false;
hgs
parents:
diff changeset
   475
    return true;
hgs
parents:
diff changeset
   476
}
hgs
parents:
diff changeset
   477
hgs
parents:
diff changeset
   478
bool UpdatePair::contains(const UpdatePair &other) const
hgs
parents:
diff changeset
   479
{
hgs
parents:
diff changeset
   480
    if (other.mStart+other.mCount-1 < mStart) return false;
hgs
parents:
diff changeset
   481
    if (other.mStart > mStart+mCount-1) return false;
hgs
parents:
diff changeset
   482
    return true;
hgs
parents:
diff changeset
   483
}
hgs
parents:
diff changeset
   484
hgs
parents:
diff changeset
   485
void UpdatePair::extend(int start, int count)
hgs
parents:
diff changeset
   486
{
hgs
parents:
diff changeset
   487
    int end = qMax(mStart+mCount, start+count);
hgs
parents:
diff changeset
   488
    mStart = qMin(mStart, start);
hgs
parents:
diff changeset
   489
    mCount = end-mStart;
hgs
parents:
diff changeset
   490
    INFO("Pair extended to:" << mStart << ":" << mCount);
hgs
parents:
diff changeset
   491
}
hgs
parents:
diff changeset
   492
hgs
parents:
diff changeset
   493
void UpdatePair::subtract(int start, int count)
hgs
parents:
diff changeset
   494
{
hgs
parents:
diff changeset
   495
    int end = qMin(mStart+mCount, start+count);
hgs
parents:
diff changeset
   496
    mStart = qMax(mStart, start);
hgs
parents:
diff changeset
   497
    mCount = end-mStart;
hgs
parents:
diff changeset
   498
    INFO("Pair reduced to:" << mStart << ":" << mCount);
hgs
parents:
diff changeset
   499
}
hgs
parents:
diff changeset
   500
hgs
parents:
diff changeset
   501
void UpdatePair::shiftRight(int count)
hgs
parents:
diff changeset
   502
{
hgs
parents:
diff changeset
   503
    mStart += count;
hgs
parents:
diff changeset
   504
    INFO("Pair shifted to:" << mStart << ":" << mCount);
hgs
parents:
diff changeset
   505
}
hgs
parents:
diff changeset
   506
hgs
parents:
diff changeset
   507
void UpdatePair::shiftLeft(int count)
hgs
parents:
diff changeset
   508
{
hgs
parents:
diff changeset
   509
    mStart -= count;
hgs
parents:
diff changeset
   510
    HANDLE_ERROR_BOOL((mStart >= 0));
hgs
parents:
diff changeset
   511
    INFO("Pair shifted to:" << mStart << ":" << mCount);
hgs
parents:
diff changeset
   512
}
hgs
parents:
diff changeset
   513
hgs
parents:
diff changeset
   514
bool UpdatePair::operator== (const UpdatePair &other) const
hgs
parents:
diff changeset
   515
{
hgs
parents:
diff changeset
   516
    return mStart == other.mStart && mCount == other.mCount;
hgs
parents:
diff changeset
   517
}
hgs
parents:
diff changeset
   518
hgs
parents:
diff changeset
   519
bool UpdatePair::operator< (const UpdatePair &other) const
hgs
parents:
diff changeset
   520
{
hgs
parents:
diff changeset
   521
    return (mStart < other.mStart || mStart == other.mStart && mCount < other.mCount);
hgs
parents:
diff changeset
   522
}
hgs
parents:
diff changeset
   523
hgs
parents:
diff changeset
   524
UpdateBuffer::UpdateBuffer()
hgs
parents:
diff changeset
   525
{
hgs
parents:
diff changeset
   526
    FUNC_LOG;
hgs
parents:
diff changeset
   527
}
hgs
parents:
diff changeset
   528
hgs
parents:
diff changeset
   529
void UpdateBuffer::add(int start, int count)
hgs
parents:
diff changeset
   530
{
hgs
parents:
diff changeset
   531
    FUNC_LOG;
hgs
parents:
diff changeset
   532
hgs
parents:
diff changeset
   533
    int itemCount = this->count();
hgs
parents:
diff changeset
   534
    for (int i = 0; i < itemCount; i++) {
hgs
parents:
diff changeset
   535
        if (at(i).contains(UpdatePair(start, count))) {
hgs
parents:
diff changeset
   536
            // It is already there
hgs
parents:
diff changeset
   537
            return;
hgs
parents:
diff changeset
   538
        }
hgs
parents:
diff changeset
   539
        if (at(i).adjacent(start, count)) {
hgs
parents:
diff changeset
   540
            (*this)[i].extend(start, count);
hgs
parents:
diff changeset
   541
            return;
hgs
parents:
diff changeset
   542
        }
hgs
parents:
diff changeset
   543
    }
hgs
parents:
diff changeset
   544
    append(UpdatePair(start, count));
hgs
parents:
diff changeset
   545
}
hgs
parents:
diff changeset
   546
hgs
parents:
diff changeset
   547
void UpdateBuffer::remove(int start, int count)
hgs
parents:
diff changeset
   548
{
hgs
parents:
diff changeset
   549
    FUNC_LOG;
hgs
parents:
diff changeset
   550
hgs
parents:
diff changeset
   551
    int itemCount = this->count();
hgs
parents:
diff changeset
   552
    for (int i = itemCount-1; i >= 0; i--) {
hgs
parents:
diff changeset
   553
        UpdatePair pair = at(i);
hgs
parents:
diff changeset
   554
        UpdatePair comp = UpdatePair(start, count);
hgs
parents:
diff changeset
   555
        if (comp.contains(pair)) {
hgs
parents:
diff changeset
   556
            INFO("Removing pair" << pair.start() << "-" << pair.end());
hgs
parents:
diff changeset
   557
            removeAt(i);
hgs
parents:
diff changeset
   558
        }
hgs
parents:
diff changeset
   559
        else if (pair.contains(comp)) {
hgs
parents:
diff changeset
   560
            // Subtraction from middle is not applicable in mediawall use cases
hgs
parents:
diff changeset
   561
            (*this)[i].subtract(start, count);
hgs
parents:
diff changeset
   562
        }
hgs
parents:
diff changeset
   563
        // Item may be present in multiple pairs.
hgs
parents:
diff changeset
   564
    }
hgs
parents:
diff changeset
   565
}
hgs
parents:
diff changeset
   566
hgs
parents:
diff changeset
   567
void UpdateBuffer::shiftRight(int startingFrom, int amount)
hgs
parents:
diff changeset
   568
{
hgs
parents:
diff changeset
   569
    FUNC_LOG;
hgs
parents:
diff changeset
   570
hgs
parents:
diff changeset
   571
    int itemCount = this->count();
hgs
parents:
diff changeset
   572
    for (int i = 0; i < itemCount; i++) {
hgs
parents:
diff changeset
   573
        if (at(i).start() >= startingFrom) {
hgs
parents:
diff changeset
   574
            (*this)[i].shiftRight(amount);
hgs
parents:
diff changeset
   575
        }
hgs
parents:
diff changeset
   576
    }
hgs
parents:
diff changeset
   577
}
hgs
parents:
diff changeset
   578
hgs
parents:
diff changeset
   579
void UpdateBuffer::shiftLeft(int startingFrom, int amount)
hgs
parents:
diff changeset
   580
{
hgs
parents:
diff changeset
   581
    FUNC_LOG;
hgs
parents:
diff changeset
   582
hgs
parents:
diff changeset
   583
    int itemCount = this->count();
hgs
parents:
diff changeset
   584
    for (int i = 0; i < itemCount; i++) {
hgs
parents:
diff changeset
   585
        if (at(i).start() >= startingFrom) {
hgs
parents:
diff changeset
   586
            (*this)[i].shiftLeft(amount);
hgs
parents:
diff changeset
   587
        }
hgs
parents:
diff changeset
   588
    }
hgs
parents:
diff changeset
   589
}