activityfw/testapplications/newtestappset/acttestanimal/animalwidget.cpp
author Jaakko Haukipuro (Nokia-MS/Oulu) <Jaakko.Haukipuro@nokia.com>
Thu, 16 Sep 2010 12:11:40 +0100
changeset 117 c63ee96dbe5f
permissions -rw-r--r--
Missing activityfw and taskswitcher components - fix for Bug 3670

/*
* Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
* All rights reserved.
* This component and the accompanying materials are made available
* under the terms of "Eclipse Public License v1.0"
* which accompanies this distribution, and is available
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
*
* Initial Contributors:
* Nokia Corporation - initial contribution.
*
* Contributors:
*
* Description:
*
*/

#include "animalwidget.h"

#include <QGridLayout>
#include <QLabel>
#include <QPushButton>
#include <QCoreApplication>
#include <QSignalMapper>
#include <QVariant>
#include <QApplication>
#include <qservicemanager.h>
#include "afstorageglobals.h"

QTM_USE_NAMESPACE

AnimalWidget::AnimalWidget(QWidget *parent) : QWidget(parent)
{
    QServiceManager serviceManager;
    mActivityStorage = serviceManager.loadInterface("com.nokia.qt.activities.Storage");
    if (!mActivityStorage) {
        qFatal("load Storage plugin failed");
    }

    mActivaction = serviceManager.loadInterface("com.nokia.qt.activities.Activation");
    if (!mActivaction) {
        qFatal("load Activation plugin failed");
    }
  
    qRegisterMetaType<Af::ActivationReason>("Af::ActivationReason");
    connect(mActivaction, SIGNAL(activated(Af::ActivationReason, QString, QVariantHash)), this, SLOT(loadActivity(Af::ActivationReason, QString, QVariantHash)));
    

    mAnimalLabel = new QLabel();
    mFileNameLabel = new QLabel("No image loaded");
    QPushButton *africanElephantButton = new QPushButton("African elephant");
    QPushButton *africanTigerButton = new QPushButton("African tiger");
    QPushButton *indiaElephantButton = new QPushButton("India elephant");
    QPushButton *siberianTigerButton = new QPushButton("Siberian tiger");
    QPushButton *saveActivityButton = new QPushButton("Save activity");
    QPushButton *quitButton = new QPushButton("Quit");

    QGridLayout *layout = new QGridLayout();

    layout->addWidget(mAnimalLabel, 0, 0, 1, 2);
    layout->addWidget(mFileNameLabel, 1, 0, 1, 2);
    layout->addWidget(africanElephantButton, 2, 0);
    layout->addWidget(africanTigerButton, 2, 1);
    layout->addWidget(indiaElephantButton, 3, 0);
    layout->addWidget(siberianTigerButton, 3, 1);
    layout->addWidget(saveActivityButton, 4, 0);
    layout->addWidget(quitButton, 4, 1);

    connect(saveActivityButton, SIGNAL(clicked()), this, SLOT(saveActivity()));
    connect(quitButton, SIGNAL(clicked()), qApp, SLOT(quit()));

    QSignalMapper *mapper = new QSignalMapper(this);
    mapper->setMapping(africanElephantButton, ":/images/africaelephant.jpg");
    mapper->setMapping(africanTigerButton, ":/images/africatiger.jpg");
    mapper->setMapping(indiaElephantButton, ":/images/indiaelephant.jpg");
    mapper->setMapping(siberianTigerButton, ":/images/syberiantiger.jpg");
    connect(africanElephantButton, SIGNAL(clicked()), mapper, SLOT(map()));
    connect(africanTigerButton, SIGNAL(clicked()), mapper, SLOT(map()));
    connect(indiaElephantButton, SIGNAL(clicked()), mapper, SLOT(map()));
    connect(siberianTigerButton, SIGNAL(clicked()), mapper, SLOT(map()));

    connect(mapper, SIGNAL(mapped(QString)), this, SLOT(changeAnimalImage(QString)));

    setLayout(layout);
    
    //startup case    
    Af::ActivationReason reason;
    bool ok = QMetaObject::invokeMethod(mActivaction, "reason", Q_RETURN_ARG(Af::ActivationReason, reason));
    if (!ok) {
        qFatal("Get reason failed");
    }
    QString actName;
    QVariantHash parameters;
    if (reason == Af::ActivationReasonActivity) {
        ok = QMetaObject::invokeMethod(mActivaction, "name", Q_RETURN_ARG(QString, actName));
        if (!ok) {
            qFatal("Get name failed");
        }
        ok = QMetaObject::invokeMethod(mActivaction, "parameters", Q_RETURN_ARG(QVariantHash, parameters));    
        if (!ok) {
            qFatal("Get parameter failed");
        }
        
        QVariant data;
        ok = QMetaObject::invokeMethod(mActivityStorage, "activityData", Q_RETURN_ARG(QVariant, data), Q_ARG(QString, actName));
        if (!ok) {
            qFatal("Get data failed");          
        }
        changeAnimalImage(data.toString());
    }
    //startup case end
}

AnimalWidget::~AnimalWidget()
{
    delete mActivityStorage;
    delete mActivaction;
}

void AnimalWidget::changeAnimalImage(const QString &image)
{
    mAnimalLabel->setPixmap(QPixmap(image).scaled(QSize( 200, 300), Qt::KeepAspectRatio));
    mFileNameLabel->setText(image);
    mCurrentImage = image;
}

void AnimalWidget::saveActivity()
{
    QVariantHash metadata;
    metadata.insert(ActivityScreenshotKeyword, *mAnimalLabel->pixmap());
    bool retok;
    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));
    if (!ok || !retok) {
        qFatal("Save failed");
    }
}

void AnimalWidget::loadActivity(Af::ActivationReason reason, const QString &name, QVariantHash parameter)
{
    QVariant data;
    bool ok = QMetaObject::invokeMethod(mActivityStorage, "activityData", Q_RETURN_ARG(QVariant, data), Q_ARG(QString, name));
    if (!ok) {
        qFatal("Get data failed");
    }
    changeAnimalImage(data.toString());
}