activityfw/testapplications/activitytestanimal/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 
       
    29 QTM_USE_NAMESPACE
       
    30 
       
    31 AnimalWidget::AnimalWidget(QWidget *parent) : QWidget(parent), mActivityClient(NULL)
       
    32 {
       
    33     QServiceManager serviceManager;
       
    34     mActivityClient = serviceManager.loadInterface("com.nokia.qt.activities.ActivityClient");
       
    35     if (!mActivityClient) {
       
    36         qFatal("Cannot initialize critical com.nokia.qt.activities.ActivityClient service.");
       
    37     }
       
    38     connect(mActivityClient, SIGNAL(activityRequested(QString)), this, SLOT(loadActivity(QString)));
       
    39     
       
    40     
       
    41     bool retok = false;
       
    42     bool ok = QMetaObject::invokeMethod(mActivityClient, "waitActivity", Q_RETURN_ARG(bool, retok));
       
    43     if (!ok || !retok) {
       
    44        // qFatal("Subscribe failed");
       
    45     }
       
    46 
       
    47 
       
    48     mAnimalLabel = new QLabel();
       
    49     mFileNameLabel = new QLabel("No image loaded");
       
    50     QPushButton *africanElephantButton = new QPushButton("African elephant");
       
    51     QPushButton *africanTigerButton = new QPushButton("African tiger");
       
    52     QPushButton *indiaElephantButton = new QPushButton("India elephant");
       
    53     QPushButton *siberianTigerButton = new QPushButton("Siberian tiger");
       
    54     QPushButton *saveActivityButton = new QPushButton("Save activity");
       
    55     QPushButton *quitButton = new QPushButton("Quit");
       
    56 
       
    57     QGridLayout *layout = new QGridLayout();
       
    58 
       
    59     layout->addWidget(mAnimalLabel, 0, 0, 1, 2);
       
    60     layout->addWidget(mFileNameLabel, 1, 0, 1, 2);
       
    61     layout->addWidget(africanElephantButton, 2, 0);
       
    62     layout->addWidget(africanTigerButton, 2, 1);
       
    63     layout->addWidget(indiaElephantButton, 3, 0);
       
    64     layout->addWidget(siberianTigerButton, 3, 1);
       
    65     layout->addWidget(saveActivityButton, 4, 0);
       
    66     layout->addWidget(quitButton, 4, 1);
       
    67 
       
    68     connect(saveActivityButton, SIGNAL(clicked()), this, SLOT(saveActivity()));
       
    69     connect(quitButton, SIGNAL(clicked()), qApp, SLOT(quit()));
       
    70 
       
    71     QSignalMapper *mapper = new QSignalMapper(this);
       
    72     mapper->setMapping(africanElephantButton, ":/images/africaelephant.jpg");
       
    73     mapper->setMapping(africanTigerButton, ":/images/africatiger.jpg");
       
    74     mapper->setMapping(indiaElephantButton, ":/images/indiaelephant.jpg");
       
    75     mapper->setMapping(siberianTigerButton, ":/images/syberiantiger.jpg");
       
    76     connect(africanElephantButton, SIGNAL(clicked()), mapper, SLOT(map()));
       
    77     connect(africanTigerButton, SIGNAL(clicked()), mapper, SLOT(map()));
       
    78     connect(indiaElephantButton, SIGNAL(clicked()), mapper, SLOT(map()));
       
    79     connect(siberianTigerButton, SIGNAL(clicked()), mapper, SLOT(map()));
       
    80 
       
    81     connect(mapper, SIGNAL(mapped(QString)), this, SLOT(changeAnimalImage(QString)));
       
    82 
       
    83     setLayout(layout);
       
    84     
       
    85     //startup case    
       
    86     QStringList arglist = qApp->arguments(); 
       
    87     QVariantHash actdata;
       
    88     ok = QMetaObject::invokeMethod(mActivityClient, "parseCommandLine", Q_RETURN_ARG(QVariantHash, actdata), Q_ARG(QStringList, arglist));
       
    89     if (!ok) {
       
    90     //    qFatal("Startup failed");
       
    91     }
       
    92     QString actid = actdata.value("activityname").toString();
       
    93     if ( !actid.isNull() )
       
    94         {
       
    95         QVariant data;
       
    96         ok = QMetaObject::invokeMethod(mActivityClient, "activityData", Q_RETURN_ARG(QVariant, data), Q_ARG(QString, actid));
       
    97         if (!ok) {
       
    98             qFatal("Get data failed");
       
    99         }
       
   100         changeAnimalImage(data.toString());
       
   101         }
       
   102     //startup case end
       
   103 }
       
   104 
       
   105 AnimalWidget::~AnimalWidget()
       
   106 {
       
   107     delete mActivityClient;
       
   108 }
       
   109 
       
   110 void AnimalWidget::changeAnimalImage(const QString &image)
       
   111 {
       
   112     mAnimalLabel->setPixmap(QPixmap(image).scaled(QSize( 200, 300), Qt::KeepAspectRatio));
       
   113     mFileNameLabel->setText(image);
       
   114     mCurrentImage = image;
       
   115 }
       
   116 
       
   117 void AnimalWidget::saveActivity()
       
   118 {
       
   119     QVariantHash metadata;
       
   120     metadata.insert("screenshot", *mAnimalLabel->pixmap());
       
   121     bool retok;
       
   122     bool ok = QMetaObject::invokeMethod(mActivityClient, "removeActivity", Q_RETURN_ARG(bool, retok), Q_ARG(QString, "Activities - pure Qt"));
       
   123     if (!ok) {
       
   124         qFatal("Remove failed");
       
   125     }
       
   126     ok = QMetaObject::invokeMethod(mActivityClient, "addActivity", Q_RETURN_ARG(bool, retok), Q_ARG(QString, "Activities - pure Qt"), Q_ARG(QVariant, QVariant(mCurrentImage)), Q_ARG(QVariantHash, metadata));
       
   127     if (!ok || !retok) {
       
   128         qFatal("Add failed");
       
   129     }
       
   130 }
       
   131 
       
   132 void AnimalWidget::loadActivity(const QString &name)
       
   133 {
       
   134     bool retok;
       
   135     bool ok = QMetaObject::invokeMethod(mActivityClient, "waitActivity", Q_RETURN_ARG(bool, retok));
       
   136     if (!ok || !retok) {
       
   137     //    qFatal("Resubscribe failed");
       
   138     }
       
   139 
       
   140     QVariant data;
       
   141     ok = QMetaObject::invokeMethod(mActivityClient, "activityData", Q_RETURN_ARG(QVariant, data), Q_ARG(QString, name));
       
   142     if (!ok) {
       
   143         qFatal("Get data failed");
       
   144     }
       
   145     changeAnimalImage(data.toString());
       
   146 }