activityfw/testapplications/newtestappset/acttestanimal/animalwidget.cpp
changeset 117 c63ee96dbe5f
equal deleted inserted replaced
115:3ab5c078b490 117:c63ee96dbe5f
       
     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 #include "animalwidget.h"
       
    19 
       
    20 #include <QGridLayout>
       
    21 #include <QLabel>
       
    22 #include <QPushButton>
       
    23 #include <QCoreApplication>
       
    24 #include <QSignalMapper>
       
    25 #include <QVariant>
       
    26 #include <QApplication>
       
    27 #include <qservicemanager.h>
       
    28 #include "afstorageglobals.h"
       
    29 
       
    30 QTM_USE_NAMESPACE
       
    31 
       
    32 AnimalWidget::AnimalWidget(QWidget *parent) : QWidget(parent)
       
    33 {
       
    34     QServiceManager serviceManager;
       
    35     mActivityStorage = serviceManager.loadInterface("com.nokia.qt.activities.Storage");
       
    36     if (!mActivityStorage) {
       
    37         qFatal("load Storage plugin failed");
       
    38     }
       
    39 
       
    40     mActivaction = serviceManager.loadInterface("com.nokia.qt.activities.Activation");
       
    41     if (!mActivaction) {
       
    42         qFatal("load Activation plugin failed");
       
    43     }
       
    44   
       
    45     qRegisterMetaType<Af::ActivationReason>("Af::ActivationReason");
       
    46     connect(mActivaction, SIGNAL(activated(Af::ActivationReason, QString, QVariantHash)), this, SLOT(loadActivity(Af::ActivationReason, QString, QVariantHash)));
       
    47     
       
    48 
       
    49     mAnimalLabel = new QLabel();
       
    50     mFileNameLabel = new QLabel("No image loaded");
       
    51     QPushButton *africanElephantButton = new QPushButton("African elephant");
       
    52     QPushButton *africanTigerButton = new QPushButton("African tiger");
       
    53     QPushButton *indiaElephantButton = new QPushButton("India elephant");
       
    54     QPushButton *siberianTigerButton = new QPushButton("Siberian tiger");
       
    55     QPushButton *saveActivityButton = new QPushButton("Save activity");
       
    56     QPushButton *quitButton = new QPushButton("Quit");
       
    57 
       
    58     QGridLayout *layout = new QGridLayout();
       
    59 
       
    60     layout->addWidget(mAnimalLabel, 0, 0, 1, 2);
       
    61     layout->addWidget(mFileNameLabel, 1, 0, 1, 2);
       
    62     layout->addWidget(africanElephantButton, 2, 0);
       
    63     layout->addWidget(africanTigerButton, 2, 1);
       
    64     layout->addWidget(indiaElephantButton, 3, 0);
       
    65     layout->addWidget(siberianTigerButton, 3, 1);
       
    66     layout->addWidget(saveActivityButton, 4, 0);
       
    67     layout->addWidget(quitButton, 4, 1);
       
    68 
       
    69     connect(saveActivityButton, SIGNAL(clicked()), this, SLOT(saveActivity()));
       
    70     connect(quitButton, SIGNAL(clicked()), qApp, SLOT(quit()));
       
    71 
       
    72     QSignalMapper *mapper = new QSignalMapper(this);
       
    73     mapper->setMapping(africanElephantButton, ":/images/africaelephant.jpg");
       
    74     mapper->setMapping(africanTigerButton, ":/images/africatiger.jpg");
       
    75     mapper->setMapping(indiaElephantButton, ":/images/indiaelephant.jpg");
       
    76     mapper->setMapping(siberianTigerButton, ":/images/syberiantiger.jpg");
       
    77     connect(africanElephantButton, SIGNAL(clicked()), mapper, SLOT(map()));
       
    78     connect(africanTigerButton, SIGNAL(clicked()), mapper, SLOT(map()));
       
    79     connect(indiaElephantButton, SIGNAL(clicked()), mapper, SLOT(map()));
       
    80     connect(siberianTigerButton, SIGNAL(clicked()), mapper, SLOT(map()));
       
    81 
       
    82     connect(mapper, SIGNAL(mapped(QString)), this, SLOT(changeAnimalImage(QString)));
       
    83 
       
    84     setLayout(layout);
       
    85     
       
    86     //startup case    
       
    87     Af::ActivationReason reason;
       
    88     bool ok = QMetaObject::invokeMethod(mActivaction, "reason", Q_RETURN_ARG(Af::ActivationReason, reason));
       
    89     if (!ok) {
       
    90         qFatal("Get reason failed");
       
    91     }
       
    92     QString actName;
       
    93     QVariantHash parameters;
       
    94     if (reason == Af::ActivationReasonActivity) {
       
    95         ok = QMetaObject::invokeMethod(mActivaction, "name", Q_RETURN_ARG(QString, actName));
       
    96         if (!ok) {
       
    97             qFatal("Get name failed");
       
    98         }
       
    99         ok = QMetaObject::invokeMethod(mActivaction, "parameters", Q_RETURN_ARG(QVariantHash, parameters));    
       
   100         if (!ok) {
       
   101             qFatal("Get parameter failed");
       
   102         }
       
   103         
       
   104         QVariant data;
       
   105         ok = QMetaObject::invokeMethod(mActivityStorage, "activityData", Q_RETURN_ARG(QVariant, data), Q_ARG(QString, actName));
       
   106         if (!ok) {
       
   107             qFatal("Get data failed");          
       
   108         }
       
   109         changeAnimalImage(data.toString());
       
   110     }
       
   111     //startup case end
       
   112 }
       
   113 
       
   114 AnimalWidget::~AnimalWidget()
       
   115 {
       
   116     delete mActivityStorage;
       
   117     delete mActivaction;
       
   118 }
       
   119 
       
   120 void AnimalWidget::changeAnimalImage(const QString &image)
       
   121 {
       
   122     mAnimalLabel->setPixmap(QPixmap(image).scaled(QSize( 200, 300), Qt::KeepAspectRatio));
       
   123     mFileNameLabel->setText(image);
       
   124     mCurrentImage = image;
       
   125 }
       
   126 
       
   127 void AnimalWidget::saveActivity()
       
   128 {
       
   129     QVariantHash metadata;
       
   130     metadata.insert(ActivityScreenshotKeyword, *mAnimalLabel->pixmap());
       
   131     bool retok;
       
   132     bool ok = QMetaObject::invokeMethod(mActivityStorage, "saveActivity", Q_RETURN_ARG(bool, retok), Q_ARG(QString, "Activities - pure Qt"), Q_ARG(QVariant, QVariant(mCurrentImage)), Q_ARG(QVariantHash, metadata));
       
   133     if (!ok || !retok) {
       
   134         qFatal("Save failed");
       
   135     }
       
   136 }
       
   137 
       
   138 void AnimalWidget::loadActivity(Af::ActivationReason reason, const QString &name, QVariantHash parameter)
       
   139 {
       
   140     QVariant data;
       
   141     bool ok = QMetaObject::invokeMethod(mActivityStorage, "activityData", Q_RETURN_ARG(QVariant, data), Q_ARG(QString, name));
       
   142     if (!ok) {
       
   143         qFatal("Get data failed");
       
   144     }
       
   145     changeAnimalImage(data.toString());
       
   146 }