homescreenapp/stateplugins/hshomescreenstateplugin/src/hsselectbackgroundstate.cpp
changeset 39 4e8ebe173323
parent 36 cdae8c6c3876
child 46 23b5d6a29cce
equal deleted inserted replaced
36:cdae8c6c3876 39:4e8ebe173323
    15 *
    15 *
    16 */
    16 */
    17 
    17 
    18 #include <QDir>
    18 #include <QDir>
    19 #include <QFileInfo>
    19 #include <QFileInfo>
       
    20 #include <QImageReader>
    20 
    21 
    21 #include <HbMainWindow>
    22 #include <HbMainWindow>
    22 #include <HbView>
    23 #include <HbView>
    23 
    24 
    24 #include "hsselectbackgroundstate.h"
    25 #include "hsselectbackgroundstate.h"
    25 #include "hsscene.h"
    26 #include "hsscene.h"
    26 #include "hsdomainmodeldatastructures.h"
    27 #include "hsdomainmodeldatastructures.h"
    27 #include "hswallpaper.h"
    28 #include "hswallpaper.h"
    28 #include "hsdatabase.h"
    29 #include "hsdatabase.h"
    29 #include "hshomescreenstatecommon.h"
    30 #include "hshomescreenstatecommon.h"
    30 
    31 #include "hswallpaperhelper.h"
       
    32 
       
    33 #ifdef Q_OS_SYMBIAN
       
    34 #include "hsimagefetcherclient.h"
       
    35 #else
    31 #include "xqaiwgetimageclient.h"
    36 #include "xqaiwgetimageclient.h"
    32 #include "xqaiwcommon.h"
    37 #include "xqaiwcommon.h"
    33 
    38 #endif
    34 /*! 
    39 /*! 
    35     \class HsSelectBackgroundState
    40     \class HsSelectBackgroundState
    36     \ingroup group_hshomescreenstateprovider
    41     \ingroup group_hshomescreenstateprovider
    37     \brief Implements imagefetcher event listening and handles those events.
    42     \brief Implements imagefetcher event listening and handles those events.
    38 
    43 
    46 HsSelectBackgroundState::HsSelectBackgroundState(QState *parent):
    51 HsSelectBackgroundState::HsSelectBackgroundState(QState *parent):
    47     QState(parent),
    52     QState(parent),
    48     mImageFetcher(0),
    53     mImageFetcher(0),
    49     mSourceView(0)
    54     mSourceView(0)
    50 {
    55 {
       
    56 #ifdef Q_OS_SYMBIAN
       
    57     mImageFetcher = new HsImageFetcherClient(this);
       
    58 #else    
    51     mImageFetcher = new XQAIWGetImageClient;
    59     mImageFetcher = new XQAIWGetImageClient;
       
    60 #endif
    52     connect(this, SIGNAL(entered()), SLOT(action_selectWallpaper()));
    61     connect(this, SIGNAL(entered()), SLOT(action_selectWallpaper()));
    53     connect(this, SIGNAL(exited()), SLOT(action_disconnectImageFetcher()));
    62     connect(this, SIGNAL(exited()), SLOT(action_disconnectImageFetcher()));
    54 }
    63 }
    55 
    64 
    56 /*!
    65 /*!
    58     
    67     
    59 */
    68 */
    60 HsSelectBackgroundState::~HsSelectBackgroundState()
    69 HsSelectBackgroundState::~HsSelectBackgroundState()
    61 {
    70 {
    62     delete mImageFetcher;
    71     delete mImageFetcher;
    63 }
       
    64 
       
    65 /*!
       
    66     \internal
       
    67     Scales and crops (if needed) \a image for given \a orientation
       
    68     and saves it to \a path. Return true on successful.
       
    69 */
       
    70 bool HsSelectBackgroundState::saveImage(QImage &image, const QString &path, Qt::Orientation orientation) 
       
    71 {
       
    72     QRect rect;
       
    73     if (orientation == Qt::Vertical) {
       
    74         rect.setRect(0, 0, (2* 360) + HSBOUNDARYEFFECT, 640);
       
    75     } else {
       
    76         rect.setRect(0, 0, (2 * 640) + HSBOUNDARYEFFECT, 360);
       
    77     }
       
    78 
       
    79     if (image.rect().contains(rect)) {        
       
    80         rect.moveCenter(image.rect().center());
       
    81         return image.copy(rect).save(path);
       
    82     } else {
       
    83         QImage scaledImage = image.scaledToWidth(rect.width());        
       
    84         rect.moveCenter(scaledImage.rect().center());
       
    85         return scaledImage.copy(rect).save(path);
       
    86     }
       
    87 }
    72 }
    88 
    73 
    89 /*!
    74 /*!
    90     \internal
    75     \internal
    91     Connects to image fetcher and launches "remote" ui from photos
    76     Connects to image fetcher and launches "remote" ui from photos
    93  */
    78  */
    94 void HsSelectBackgroundState::action_selectWallpaper()
    79 void HsSelectBackgroundState::action_selectWallpaper()
    95 {
    80 {
    96     mSourceView = HsScene::mainWindow()->currentView();
    81     mSourceView = HsScene::mainWindow()->currentView();
    97 
    82 
       
    83 #ifdef Q_OS_SYMBIAN    
       
    84     connect(mImageFetcher, SIGNAL(fetchCompleted(const QString&)),
       
    85             this, SLOT(fetchCompleted(const QString&)));
       
    86     connect(mImageFetcher, SIGNAL(fetchFailed(int, const QString&)),
       
    87             this, SLOT(fetchFailed(int, const QString&)));
       
    88     mImageFetcher->fetch();
       
    89 #else
    98     connect(mImageFetcher, SIGNAL(fetchComplete(QStringList)),
    90     connect(mImageFetcher, SIGNAL(fetchComplete(QStringList)),
    99             SLOT(onFetchComplete(QStringList)));
    91             SLOT(onFetchComplete(QStringList)));
   100     connect(mImageFetcher, SIGNAL(fetchFailed(int)),
    92     connect(mImageFetcher, SIGNAL(fetchFailed(int)),
   101             SLOT(onFetchFailed(int)));
    93             SLOT(onFetchFailed(int)));
   102     
       
   103     mImageFetcher->fetch(QVariantMap(), SelectionSingle);
    94     mImageFetcher->fetch(QVariantMap(), SelectionSingle);
       
    95 #endif    
   104 }
    96 }
   105 
    97 
   106 /*!
    98 /*!
   107     \internal
    99     \internal
   108     disconnects photos image fetcher services slots.
   100     disconnects photos image fetcher services slots.
   114     mImageFetcher->disconnect(this);
   106     mImageFetcher->disconnect(this);
   115 }
   107 }
   116 
   108 
   117 /*!
   109 /*!
   118     \internal
   110     \internal
   119     Called when user has selected an image    
   111     Called when user has selected an image on emulator or HW
       
   112 */
       
   113 #ifdef Q_OS_SYMBIAN
       
   114 void HsSelectBackgroundState::fetchCompleted(const QString& imageFileName)
       
   115 {
       
   116     QStringList imageFileNameAsList(imageFileName);
       
   117     onFetchComplete(imageFileNameAsList);
       
   118 }
       
   119 #endif
       
   120 
       
   121 /*!
       
   122     \internal
       
   123     Called when user has selected an image 
   120 */
   124 */
   121 void HsSelectBackgroundState::onFetchComplete(QStringList imageStringList)
   125 void HsSelectBackgroundState::onFetchComplete(QStringList imageStringList)
   122 {
   126 {
   123     HsScene *scene = HsScene::instance();
   127     HsScene *scene = HsScene::instance();
   124         
   128         
   149     QString portraitPath = HsWallpaper::wallpaperPath(
   153     QString portraitPath = HsWallpaper::wallpaperPath(
   150         Qt::Vertical, QString(), fileExtension);
   154         Qt::Vertical, QString(), fileExtension);
   151     QString landscapePath = HsWallpaper::wallpaperPath(
   155     QString landscapePath = HsWallpaper::wallpaperPath(
   152         Qt::Horizontal, QString(), fileExtension);
   156         Qt::Horizontal, QString(), fileExtension);
   153     
   157     
   154     QImage image(imageStringList.first());
   158     QRect portraitRect = QRect(0, 0, (2 * 360) + HSBOUNDARYEFFECT, 640);
   155     
   159     QRect landscapeRect = QRect(0, 0, (2 * 640) + HSBOUNDARYEFFECT, 360);
   156     if (saveImage(image, portraitPath, Qt::Vertical) && 
   160     QRect sourceRect; // left empty to signal we want to use full size image as source
   157         saveImage(image, landscapePath, Qt::Horizontal)) {
   161     
       
   162     QImage portraitImage = HsWallpaperHelper::processImage(imageStringList.first(),
       
   163             portraitRect, sourceRect);
       
   164     
       
   165     QImage landscapeImage = HsWallpaperHelper::processImage(imageStringList.first(),
       
   166             landscapeRect, sourceRect);
       
   167     
       
   168     if (!portraitImage.isNull() && !landscapeImage.isNull()) {
       
   169         portraitImage.save(portraitPath);
   158         sceneData.portraitWallpaper = portraitPath;
   170         sceneData.portraitWallpaper = portraitPath;
       
   171         
       
   172         landscapeImage.save(landscapePath);
   159         sceneData.landscapeWallpaper = landscapePath;
   173         sceneData.landscapeWallpaper = landscapePath;
       
   174         
   160         if (db->updateScene(sceneData)) {
   175         if (db->updateScene(sceneData)) {
   161             scene->wallpaper()->setImagesById(QString(), fileInfo.suffix());
   176             scene->wallpaper()->setImagesById(QString(), fileInfo.suffix());
   162         }
   177         }
   163     }
   178     }
   164    
   179     else {
       
   180         // display some error note here
       
   181     }
       
   182   
   165     emit event_waitInput();
   183     emit event_waitInput();
   166 }
   184 }
   167 
   185 
   168 /*!
   186 /*!
   169     \internal
   187     \internal
   172 void HsSelectBackgroundState::onFetchFailed(int error)
   190 void HsSelectBackgroundState::onFetchFailed(int error)
   173 {
   191 {
   174     Q_UNUSED(error)
   192     Q_UNUSED(error)
   175     emit event_waitInput();
   193     emit event_waitInput();
   176 }
   194 }
       
   195 
       
   196 /*!
       
   197     \internal
       
   198     Called when selection of background image fails on emulator or HW
       
   199 */
       
   200 #ifdef Q_OS_SYMBIAN
       
   201 void HsSelectBackgroundState::fetchFailed(int error, const QString& errorString)
       
   202 {
       
   203     Q_UNUSED(errorString)
       
   204     onFetchFailed(error);        
       
   205 }
       
   206 #endif