browsercore/appfw/ThumbnailEngine/TnEngineScaler.cpp
branchGCC_SURGE
changeset 8 2e16851ffecd
parent 2 bf4420e9fa4d
parent 6 1c3b8676e58c
equal deleted inserted replaced
2:bf4420e9fa4d 8:2e16851ffecd
     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 */
       
    17 
       
    18 
       
    19 #include <QTimer>
       
    20 #include <QPainter>
       
    21 #include "TnEngineScaler.h"
       
    22 
       
    23 namespace WRT {
       
    24 
       
    25 TnEngineScaler::TnEngineScaler(MTnEngineScalerCallback& callback)
       
    26 : m_callback(&callback)
       
    27 {
       
    28     m_resultBitmap = 0;
       
    29 }
       
    30 
       
    31 void TnEngineScaler::init()
       
    32 {
       
    33     m_scalingTimer = new QTimer();
       
    34     connect(m_scalingTimer, SIGNAL(timeout()), this, SLOT(run()));
       
    35 }
       
    36 
       
    37 TnEngineScaler* TnEngineScaler::initWithCallback(MTnEngineScalerCallback& callback)
       
    38 {
       
    39     TnEngineScaler* self = new TnEngineScaler(callback);
       
    40     self->init();
       
    41     return self;
       
    42 }
       
    43 
       
    44 TnEngineScaler::~TnEngineScaler()
       
    45 {
       
    46     cancel();
       
    47     delete m_resultBitmap;
       
    48     delete m_scalingTimer;
       
    49 }
       
    50 
       
    51 void TnEngineScaler::startScaling(QPixmap& source, const QRect& targetRect)
       
    52 {
       
    53     // cancel outstanding request
       
    54     cancel();
       
    55     if (!m_resultBitmap || m_resultBitmap->size() != targetRect.size()) {
       
    56         deleteResultBitmap();
       
    57         m_resultBitmap = new QPixmap(targetRect.size());
       
    58     }
       
    59     *m_resultBitmap = source.scaled(targetRect.size(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
       
    60     m_targetRect = targetRect;
       
    61     m_scalingTimer->start(20);
       
    62 }
       
    63 
       
    64 void TnEngineScaler::deleteResultBitmap()
       
    65 {
       
    66     delete m_resultBitmap;
       
    67     m_resultBitmap = 0;
       
    68 }
       
    69 
       
    70 void TnEngineScaler::cancel()
       
    71 {
       
    72     m_scalingTimer->stop();
       
    73     deleteResultBitmap();
       
    74 }
       
    75 
       
    76 void TnEngineScaler::run()
       
    77 {
       
    78     m_scalingTimer->stop();
       
    79     m_callback->scalingCompleted(*m_resultBitmap, m_targetRect);
       
    80     // if the callback called StartScalingL(), we must not delete the bitmap
       
    81     if (!isActive()) {
       
    82         deleteResultBitmap();
       
    83     }
       
    84 }
       
    85 
       
    86 bool TnEngineScaler::isActive()
       
    87 {
       
    88     return m_scalingTimer->isActive();
       
    89 }
       
    90 
       
    91 }
       
    92