homescreenapp/stateplugins/hshomescreenstateplugin/src/hsselectbackgroundstate.cpp
changeset 39 4e8ebe173323
parent 36 cdae8c6c3876
child 46 23b5d6a29cce
--- a/homescreenapp/stateplugins/hshomescreenstateplugin/src/hsselectbackgroundstate.cpp	Fri Apr 16 14:54:01 2010 +0300
+++ b/homescreenapp/stateplugins/hshomescreenstateplugin/src/hsselectbackgroundstate.cpp	Mon May 03 12:24:59 2010 +0300
@@ -17,6 +17,7 @@
 
 #include <QDir>
 #include <QFileInfo>
+#include <QImageReader>
 
 #include <HbMainWindow>
 #include <HbView>
@@ -27,10 +28,14 @@
 #include "hswallpaper.h"
 #include "hsdatabase.h"
 #include "hshomescreenstatecommon.h"
+#include "hswallpaperhelper.h"
 
+#ifdef Q_OS_SYMBIAN
+#include "hsimagefetcherclient.h"
+#else
 #include "xqaiwgetimageclient.h"
 #include "xqaiwcommon.h"
-
+#endif
 /*! 
     \class HsSelectBackgroundState
     \ingroup group_hshomescreenstateprovider
@@ -48,7 +53,11 @@
     mImageFetcher(0),
     mSourceView(0)
 {
+#ifdef Q_OS_SYMBIAN
+    mImageFetcher = new HsImageFetcherClient(this);
+#else    
     mImageFetcher = new XQAIWGetImageClient;
+#endif
     connect(this, SIGNAL(entered()), SLOT(action_selectWallpaper()));
     connect(this, SIGNAL(exited()), SLOT(action_disconnectImageFetcher()));
 }
@@ -64,30 +73,6 @@
 
 /*!
     \internal
-    Scales and crops (if needed) \a image for given \a orientation
-    and saves it to \a path. Return true on successful.
-*/
-bool HsSelectBackgroundState::saveImage(QImage &image, const QString &path, Qt::Orientation orientation) 
-{
-    QRect rect;
-    if (orientation == Qt::Vertical) {
-        rect.setRect(0, 0, (2* 360) + HSBOUNDARYEFFECT, 640);
-    } else {
-        rect.setRect(0, 0, (2 * 640) + HSBOUNDARYEFFECT, 360);
-    }
-
-    if (image.rect().contains(rect)) {        
-        rect.moveCenter(image.rect().center());
-        return image.copy(rect).save(path);
-    } else {
-        QImage scaledImage = image.scaledToWidth(rect.width());        
-        rect.moveCenter(scaledImage.rect().center());
-        return scaledImage.copy(rect).save(path);
-    }
-}
-
-/*!
-    \internal
     Connects to image fetcher and launches "remote" ui from photos
     from which user can select background image
  */
@@ -95,12 +80,19 @@
 {
     mSourceView = HsScene::mainWindow()->currentView();
 
+#ifdef Q_OS_SYMBIAN    
+    connect(mImageFetcher, SIGNAL(fetchCompleted(const QString&)),
+            this, SLOT(fetchCompleted(const QString&)));
+    connect(mImageFetcher, SIGNAL(fetchFailed(int, const QString&)),
+            this, SLOT(fetchFailed(int, const QString&)));
+    mImageFetcher->fetch();
+#else
     connect(mImageFetcher, SIGNAL(fetchComplete(QStringList)),
             SLOT(onFetchComplete(QStringList)));
     connect(mImageFetcher, SIGNAL(fetchFailed(int)),
             SLOT(onFetchFailed(int)));
-    
     mImageFetcher->fetch(QVariantMap(), SelectionSingle);
+#endif    
 }
 
 /*!
@@ -116,7 +108,19 @@
 
 /*!
     \internal
-    Called when user has selected an image    
+    Called when user has selected an image on emulator or HW
+*/
+#ifdef Q_OS_SYMBIAN
+void HsSelectBackgroundState::fetchCompleted(const QString& imageFileName)
+{
+    QStringList imageFileNameAsList(imageFileName);
+    onFetchComplete(imageFileNameAsList);
+}
+#endif
+
+/*!
+    \internal
+    Called when user has selected an image 
 */
 void HsSelectBackgroundState::onFetchComplete(QStringList imageStringList)
 {
@@ -151,17 +155,31 @@
     QString landscapePath = HsWallpaper::wallpaperPath(
         Qt::Horizontal, QString(), fileExtension);
     
-    QImage image(imageStringList.first());
+    QRect portraitRect = QRect(0, 0, (2 * 360) + HSBOUNDARYEFFECT, 640);
+    QRect landscapeRect = QRect(0, 0, (2 * 640) + HSBOUNDARYEFFECT, 360);
+    QRect sourceRect; // left empty to signal we want to use full size image as source
+    
+    QImage portraitImage = HsWallpaperHelper::processImage(imageStringList.first(),
+            portraitRect, sourceRect);
     
-    if (saveImage(image, portraitPath, Qt::Vertical) && 
-        saveImage(image, landscapePath, Qt::Horizontal)) {
+    QImage landscapeImage = HsWallpaperHelper::processImage(imageStringList.first(),
+            landscapeRect, sourceRect);
+    
+    if (!portraitImage.isNull() && !landscapeImage.isNull()) {
+        portraitImage.save(portraitPath);
         sceneData.portraitWallpaper = portraitPath;
+        
+        landscapeImage.save(landscapePath);
         sceneData.landscapeWallpaper = landscapePath;
+        
         if (db->updateScene(sceneData)) {
             scene->wallpaper()->setImagesById(QString(), fileInfo.suffix());
         }
     }
-   
+    else {
+        // display some error note here
+    }
+  
     emit event_waitInput();
 }
 
@@ -174,3 +192,15 @@
     Q_UNUSED(error)
     emit event_waitInput();
 }
+
+/*!
+    \internal
+    Called when selection of background image fails on emulator or HW
+*/
+#ifdef Q_OS_SYMBIAN
+void HsSelectBackgroundState::fetchFailed(int error, const QString& errorString)
+{
+    Q_UNUSED(errorString)
+    onFetchFailed(error);        
+}
+#endif
\ No newline at end of file