homescreenapp/hsutils/src/hswallpaperhelper.cpp
changeset 60 30f14686fb04
parent 55 03646e8da489
child 61 2b1b11a301d2
equal deleted inserted replaced
55:03646e8da489 60:30f14686fb04
     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:  Homescreen wallpaper helper class.
       
    15 *
       
    16 */
       
    17 
       
    18 #include <QString>
       
    19 #include <QImageReader>
       
    20 
       
    21 #include "hswallpaperhelper.h"
       
    22 
       
    23 
       
    24 /*!
       
    25     \class HsWallpaperHelper
       
    26     \ingroup group_hsapplication
       
    27     \brief Homescreen wallpaper helper class.
       
    28     Implements image scaling and cropping services. 
       
    29 */
       
    30 
       
    31 /*!
       
    32     \internal
       
    33     Scales and crops (if needed) \a sourcePath image using \a targetRect. 
       
    34     Centers target rect automatically if a\ centerTarget is true.
       
    35     Pass empty a\ sourceRect to use full size source image as starting point. 
       
    36     Returns processed image or null image if operation fails.
       
    37 */
       
    38 QImage HsWallpaperHelper::processImage(const QString &sourcePath,
       
    39                                        const QRect &targetRect,
       
    40                                        const QRect &sourceRect,
       
    41                                        bool centerTarget) 
       
    42 {
       
    43     QImageReader imageReader(sourcePath);
       
    44     
       
    45     QRect tempTargetRect = targetRect;
       
    46     QRect tempSourceRect = sourceRect;
       
    47     
       
    48     if (imageReader.canRead()) {
       
    49         QSize sourceSize = imageReader.size();
       
    50         if (tempSourceRect.isEmpty()) {
       
    51             // If sourceRect not defined, uses full size image as source.
       
    52             tempSourceRect.setRect(0, 0, sourceSize.width(), sourceSize.height());
       
    53         }
       
    54         sourceSize.scale(tempTargetRect.width(), tempTargetRect.height(), 
       
    55             Qt::KeepAspectRatioByExpanding);
       
    56         imageReader.setScaledSize(sourceSize);
       
    57 
       
    58         if (centerTarget) {
       
    59             tempTargetRect.moveCenter(QPoint(sourceSize.width() / 2, sourceSize.height() / 2));
       
    60         }
       
    61         imageReader.setScaledClipRect(tempTargetRect);
       
    62         
       
    63         return imageReader.read();
       
    64     }
       
    65     return QImage(); // returns null QImage
       
    66 }