homescreenapp/hsdomainmodel/src/hswallpaper.cpp
changeset 62 341166945d65
parent 51 4785f57bf3d4
child 69 87476091b3f5
equal deleted inserted replaced
57:2e2dc3d30ca8 62:341166945d65
     1 /*
     1 /*
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
     2 * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
     3 * All rights reserved.
     3 * All rights reserved.
     4 * This component and the accompanying materials are made available
     4 * This component and the accompanying materials are made available
     5 * under the terms of "Eclipse Public License v1.0"
     5 * under the terms of "Eclipse Public License v1.0"
     6 * which accompanies this distribution, and is available
     6 * which accompanies this distribution, and is available
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
    14 * Description:
    14 * Description:
    15 *
    15 *
    16 */
    16 */
    17 
    17 
    18 #include <QDir>
    18 #include <QDir>
       
    19 #include <QFile>
       
    20 #include <QVariantHash>
    19 #include <QGraphicsLinearLayout>
    21 #include <QGraphicsLinearLayout>
       
    22 
       
    23 #include <HbMainWindow>
    20 #include <HbIconItem>
    24 #include <HbIconItem>
    21 #include <HbMainWindow>
    25 
    22 #include "hswallpaper.h"
    26 #include "hswallpaper.h"
    23 #include "hsscene.h"
    27 #include "hsscene.h"
    24 
    28 #include "hspage.h"
       
    29 #include "hswallpaperloader.h"
       
    30 #include "hsconfiguration.h"
       
    31 
       
    32 /*!
       
    33     \class HsWallpaper
       
    34     \ingroup group_hsdomainmodel
       
    35     \brief 
       
    36 */
       
    37 
       
    38 /*!
       
    39 
       
    40 */
    25 HsWallpaper::HsWallpaper(QGraphicsItem *parent)
    41 HsWallpaper::HsWallpaper(QGraphicsItem *parent)
    26   : HbWidget(parent),
    42   : HbWidget(parent),
    27     mOrientation(Qt::Vertical),
    43     mId(-1),
    28     mIconItem(0)
    44     mIsDefaultImage(false),
    29 {
    45     mIconItem(0),
       
    46     mLoader(0)
       
    47 {
       
    48     setFlag(ItemHasNoContents);
       
    49     setSizePolicy(QSizePolicy::Ignored,QSizePolicy::Ignored);
       
    50 
       
    51     mLoader = new HsWallpaperLoader(this);
       
    52     connect(mLoader, SIGNAL(finished()), SLOT(onLoaderFinished()), Qt::UniqueConnection);
       
    53     connect(mLoader, SIGNAL(failed()), SLOT(onLoaderFailed()), Qt::UniqueConnection);
       
    54 
    30     mIconItem = new HbIconItem();
    55     mIconItem = new HbIconItem();
    31     QGraphicsLinearLayout *layout = new QGraphicsLinearLayout();
    56     QGraphicsLinearLayout *layout = new QGraphicsLinearLayout;
    32     layout->setContentsMargins(0, 0, 0, 0);
    57     layout->setContentsMargins(0, 0, 0, 0);
    33     layout->addItem(mIconItem);
    58     layout->addItem(mIconItem);
    34     setLayout(layout);
    59     setLayout(layout);
    35 
    60 
    36     connect(HsScene::mainWindow(),
    61     connect(HsScene::mainWindow(),
    37         SIGNAL(orientationChanged(Qt::Orientation)),
    62         SIGNAL(orientationChanged(Qt::Orientation)),
    38         SLOT(onOrientationChanged(Qt::Orientation)));
    63         SLOT(updateIconItem(Qt::Orientation)));
    39 }
    64 }
    40 
    65 
       
    66 /*!
       
    67 
       
    68 */
    41 HsWallpaper::~HsWallpaper()
    69 HsWallpaper::~HsWallpaper()
    42 {
    70 {
    43 }
    71 }
    44 
    72 
    45 bool HsWallpaper::setImagesById(const QString &id,
    73 /*!
    46                                 const QString &ext)
    74 
    47 {
    75 */
    48     return setImagesByPaths(
    76 void HsWallpaper::setImage(const QString &path)
    49         wallpaperPath(Qt::Horizontal, id, ext),
    77 {
    50         wallpaperPath(Qt::Vertical, id, ext));
    78     if (!path.isEmpty()) {
    51 }
    79         if(mLoader->isRunning()) {
    52 
    80             mLoader->cancel();
    53 bool HsWallpaper::setImagesByPaths(const QString &landscapeImagePath,
    81         }
    54                                    const QString &portraitImagePath)
    82     
    55 {
    83         mLoader->setSourcePath(path);
    56     mLImagePath = landscapeImagePath;
    84         mLoader->setTargets(createTargets(path));
    57     mPImagePath = portraitImagePath;
    85         mLoader->start();
    58     mOrientation = HsScene::orientation();
       
    59 
       
    60     if (mOrientation == Qt::Horizontal) {
       
    61         setLandscapeImage(mLImagePath, true);
       
    62     } else {
    86     } else {
    63         setPortraitImage(mPImagePath, true);
    87         emit imageSetFailed();
    64     }
    88     }
       
    89 }
       
    90 
       
    91 /*!
       
    92 
       
    93 */
       
    94 void HsWallpaper::setDefaultImage()
       
    95 {
       
    96     if (mIsDefaultImage) {
       
    97         return;
       
    98     }
       
    99 
       
   100     QDir dir(wallpaperDirectory());
       
   101     QStringList filters;
       
   102     filters << "default_portrait.*";
       
   103     filters << "default_landscape.*";
       
   104     QFileInfoList infos = dir.entryInfoList(
       
   105         filters, QDir::Files, QDir::Name | QDir::IgnoreCase);
       
   106 
       
   107     Q_ASSERT(infos.count() == 2);
       
   108 
       
   109     mIsDefaultImage = true;
       
   110     mLandscapeImagePath = infos.first().absoluteFilePath();
       
   111     mPortraitImagePath = infos.last().absoluteFilePath();
       
   112 
       
   113     updateIconItem(HsScene::orientation());
       
   114 }
       
   115 
       
   116 /*!
       
   117 
       
   118 */
       
   119 void HsWallpaper::remove()
       
   120 {
       
   121     if (mIsDefaultImage) {
       
   122         mIsDefaultImage = false;
       
   123     } else {
       
   124         QFile::remove(mPortraitImagePath);
       
   125         QFile::remove(mLandscapeImagePath);
       
   126     }
       
   127     mPortraitImagePath.clear();
       
   128     mLandscapeImagePath.clear();
       
   129 }
       
   130 
       
   131 /*!
       
   132 
       
   133 */
       
   134 bool HsWallpaper::setExistingImage()
       
   135 {
       
   136     QDir dir(wallpaperDirectory());
       
   137     QStringList filters;
       
   138     filters << QString("%1_portrait.*").arg(mId);
       
   139     filters << QString("%1_landscape.*").arg(mId);
       
   140     QFileInfoList infos = dir.entryInfoList(
       
   141         filters, QDir::Files, QDir::Name | QDir::IgnoreCase);
       
   142 
       
   143     Q_ASSERT(infos.isEmpty() || infos.count() == 2);
       
   144 
       
   145     if (infos.isEmpty()) {
       
   146         return false;
       
   147     }
       
   148 
       
   149     mIsDefaultImage = false;
       
   150     mLandscapeImagePath = infos.first().absoluteFilePath();
       
   151     mPortraitImagePath = infos.last().absoluteFilePath();
       
   152 
       
   153     updateIconItem(HsScene::orientation());
    65     return true;
   154     return true;
    66 }
   155 }
    67 
   156 
    68 void HsWallpaper::setPortraitImage(const QString &path, bool activate)
   157 QString HsWallpaper::rootDirectory() const
    69 {
       
    70     mPImagePath = path;
       
    71     if (activate) {
       
    72         mIconItem->setIcon(HbIcon(QIcon(path)));
       
    73     }
       
    74 }
       
    75 
       
    76 void HsWallpaper::setLandscapeImage(const QString &path, bool activate)
       
    77 {
       
    78     mLImagePath = path;
       
    79     if (activate) {
       
    80         mIconItem->setIcon(HbIcon(QIcon(path)));
       
    81     }
       
    82 }
       
    83 
       
    84 bool HsWallpaper::removeImages()
       
    85 {
       
    86     if (mLImagePath != wallpaperPath(Qt::Horizontal)) {
       
    87         QFile::remove(mLImagePath);
       
    88         mLImagePath.clear();
       
    89     }
       
    90     if (mPImagePath != wallpaperPath(Qt::Vertical)) {
       
    91         QFile::remove(mPImagePath);
       
    92         mPImagePath.clear();
       
    93     }
       
    94     return true;
       
    95 }
       
    96 
       
    97 QString HsWallpaper::wallpaperDirectory()
       
    98 {
   158 {
    99 #ifdef Q_OS_SYMBIAN
   159 #ifdef Q_OS_SYMBIAN
   100     static QString directory =
   160     QString directory("c:/private/20022f35/wallpapers/");
   101         QDir::toNativeSeparators("c:/private/20022f35/wallpapers/");
       
   102 #else
   161 #else
   103     static QString directory =
   162     QString directory(QDir::currentPath() + "/private/20022f35/wallpapers/");
   104         QDir::toNativeSeparators(QDir::currentPath() + "/private/20022f35/wallpapers/");
       
   105 #endif
   163 #endif
   106 
   164 
       
   165 #ifdef HSDOMAINMODEL_TEST
       
   166     directory = QDir::currentPath() + "/wallpapers/";
       
   167 #endif
   107     return directory;
   168     return directory;
   108 }
   169 }
   109 
   170 
   110 QString HsWallpaper::wallpaperPath(Qt::Orientation orientation,
   171 /*!
   111                                    const QString &id,
   172 
   112                                    const QString &ext)
   173 */
   113 {
   174 void HsWallpaper::onLoaderFinished()
   114     QString orientationString = orientation == Qt::Horizontal ?
   175 {
   115         "_landscape." : "_portrait.";
   176     if (mIsDefaultImage) {
   116     return wallpaperDirectory() + id +
   177         mIsDefaultImage = false;
   117            orientationString + ext;
       
   118 }
       
   119 
       
   120 void HsWallpaper::onOrientationChanged(Qt::Orientation orientation)
       
   121 {
       
   122     if (orientation == Qt::Horizontal) {
       
   123         mIconItem->setIcon(HbIcon(QIcon(mLImagePath)));
       
   124     } else {
   178     } else {
   125         mIconItem->setIcon(HbIcon(QIcon(mPImagePath)));
   179         QFile::remove(mPortraitImagePath);
   126     }
   180         QFile::remove(mLandscapeImagePath);
   127 }
   181     }
       
   182     foreach (QString path, mLoader->targets().keys()) {
       
   183         QString newName = path; newName.remove("temp");
       
   184         QFile::rename(path, newName);
       
   185     }
       
   186     setExistingImage();
       
   187     emit imageSet();
       
   188 }
       
   189 
       
   190 /*!
       
   191 
       
   192 */
       
   193 void HsWallpaper::onLoaderFailed()
       
   194 {
       
   195     foreach (QString path, mLoader->targets().keys()) {
       
   196         QFile::remove(path);
       
   197     }
       
   198     emit imageSetFailed();
       
   199 }
       
   200 
       
   201 /*!
       
   202 
       
   203 */
       
   204 void HsWallpaper::updateIconItem(Qt::Orientation orientation)
       
   205 {
       
   206     if (orientation == Qt::Vertical) {
       
   207         mIconItem->setIcon(HbIcon(QIcon(mPortraitImagePath)));
       
   208     } else {
       
   209         mIconItem->setIcon(HbIcon(QIcon(mLandscapeImagePath)));
       
   210     }
       
   211 }
       
   212 
       
   213 /*!
       
   214 
       
   215 */
       
   216 HsSceneWallpaper::HsSceneWallpaper(HsScene *scene, QGraphicsItem *parent)
       
   217   : HsWallpaper(parent),
       
   218     mScene(0)
       
   219 {
       
   220     setScene(scene);
       
   221 }
       
   222 
       
   223 /*!
       
   224 
       
   225 */
       
   226 HsSceneWallpaper::~HsSceneWallpaper()
       
   227 {
       
   228 }
       
   229 
       
   230 /*!
       
   231 
       
   232 */
       
   233 void HsSceneWallpaper::setScene(HsScene *scene)
       
   234 {
       
   235     if (!scene) {
       
   236         return;
       
   237     }
       
   238 
       
   239     mScene = scene;
       
   240     mId = scene->databaseId();
       
   241     if (!setExistingImage()) {
       
   242         setDefaultImage();
       
   243     }
       
   244 }
       
   245 
       
   246 /*!
       
   247 
       
   248 */
       
   249 QString HsSceneWallpaper::wallpaperDirectory() const
       
   250 {
       
   251     return QDir::toNativeSeparators(rootDirectory() + "scene/");
       
   252 }
       
   253 
       
   254 /*!
       
   255 
       
   256 */
       
   257 QVariantHash HsSceneWallpaper::createTargets(const QString &sourcePath)
       
   258 {
       
   259     QVariantHash targets;
       
   260 
       
   261     QString path = wallpaperDirectory()
       
   262                    + QString("temp%1_").arg(mId)
       
   263                    + QString("%1.")
       
   264                    + QFileInfo(sourcePath).suffix();
       
   265 
       
   266     targets.insert(path.arg("portrait"), QSize((2 * 360) + HSCONFIGURATION_GET(bounceEffect), 640));
       
   267     targets.insert(path.arg("landscape"), QSize((2 * 640) + HSCONFIGURATION_GET(bounceEffect), 360));
       
   268     return targets;
       
   269 }
       
   270 
       
   271 /*!
       
   272  PAGE
       
   273 */
       
   274 HsPageWallpaper::HsPageWallpaper(HsPage *page, QGraphicsItem *parent)
       
   275   : HsWallpaper(parent),
       
   276     mPage(0)
       
   277 {
       
   278     setPage(page);
       
   279 }
       
   280 
       
   281 /*!
       
   282 
       
   283 */
       
   284 HsPageWallpaper::~HsPageWallpaper()
       
   285 {
       
   286 }
       
   287 
       
   288 /*!
       
   289 
       
   290 */
       
   291 void HsPageWallpaper::setPage(HsPage *page)
       
   292 {
       
   293     if (!page) {
       
   294         return;
       
   295     }
       
   296 
       
   297     mPage = page;
       
   298     mId = page->databaseId();
       
   299     if (!setExistingImage()) {
       
   300         setDefaultImage();
       
   301     }
       
   302 }
       
   303 
       
   304 /*!
       
   305 
       
   306 */
       
   307 QString HsPageWallpaper::wallpaperDirectory() const
       
   308 {
       
   309     return QDir::toNativeSeparators(rootDirectory() + "page/");
       
   310 }
       
   311 
       
   312 /*!
       
   313 
       
   314 */
       
   315 QVariantHash HsPageWallpaper::createTargets(const QString &sourcePath)
       
   316 {
       
   317     QVariantHash targets;
       
   318 
       
   319     QString path = wallpaperDirectory()
       
   320                    + QString("temp%1_").arg(mId)
       
   321                    + QString("%1.")
       
   322                    + QFileInfo(sourcePath).suffix();
       
   323 
       
   324     targets.insert(path.arg("portrait"), QSize(360, 640));
       
   325     targets.insert(path.arg("landscape"), QSize(640, 360));
       
   326     return targets;
       
   327 }
       
   328