activityfw/testapplications/activitytestfigure/viewmanager.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 #include "viewmanager.h"
       
    18 #include <qservicemanager.h>
       
    19 #include <HbInstance>
       
    20 #include <QStringList>
       
    21 #include <QCoreApplication>
       
    22 #include <hbinstance.h>
       
    23 #include <hbapplication.h>
       
    24 #include <hbactivitymanager.h>
       
    25 #include <hbmainwindow.h>
       
    26 #include "squrewidget.h"
       
    27 #include "trianglewidget.h"
       
    28 #include "circlewidget.h"
       
    29 #include "figurewidget.h"
       
    30 #include <QEventLoop>
       
    31 
       
    32 QTM_USE_NAMESPACE
       
    33 #define hbApp qobject_cast<HbApplication*>(qApp)
       
    34 
       
    35 viewmanager::viewmanager(HbApplication *hbapp):
       
    36     HbMainWindow(), mHbApp(hbapp)
       
    37 {
       
    38 
       
    39     connect(hbApp->activityManager(), SIGNAL(activityRequested(QString)), this, SLOT(loadActivityData(QString)));
       
    40     
       
    41 
       
    42     mSw = new squrewidget();
       
    43     mTw = new trianglewidget();
       
    44     mCw = new circlewidget();
       
    45     mSView = addView(mSw);
       
    46     mSView->setTitle("Squre");
       
    47     mTView = addView(mTw);
       
    48     mTView->setTitle("Triangle");
       
    49     mCView = addView(mCw);
       
    50     mCView->setTitle("Circle");
       
    51     totriangle();
       
    52     connect(mSw, SIGNAL(totriangle()), this, SLOT(totriangle()));
       
    53     connect(mSw, SIGNAL(tocircle()), this, SLOT(tocircle()));
       
    54 
       
    55     connect(mTw, SIGNAL(tocircle()), this, SLOT(tocircle()));
       
    56     connect(mTw, SIGNAL(tosqure()), this, SLOT(tosqure()));
       
    57 
       
    58     connect(mCw, SIGNAL(tosqure()), this, SLOT(tosqure()));
       
    59     connect(mCw, SIGNAL(totriangle()), this, SLOT(totriangle()));
       
    60 
       
    61     connect(mSw, SIGNAL(save()), this, SLOT(save()));
       
    62     connect(mTw, SIGNAL(save()), this, SLOT(save()));
       
    63     connect(mCw, SIGNAL(save()), this, SLOT(save()));
       
    64     connect(mSw, SIGNAL(updateAct()), this, SLOT(updateAct()));
       
    65     connect(mTw, SIGNAL(updateAct()), this, SLOT(updateAct()));
       
    66     connect(mCw, SIGNAL(updateAct()), this, SLOT(updateAct()));
       
    67     connect(mSw, SIGNAL(del()), this, SLOT(del()));
       
    68     connect(mTw, SIGNAL(del()), this, SLOT(del()));
       
    69     connect(mCw, SIGNAL(del()), this, SLOT(del())); 
       
    70 
       
    71 }
       
    72 
       
    73 viewmanager::~viewmanager()
       
    74 {
       
    75 	
       
    76 }
       
    77 
       
    78 void viewmanager::totriangle()
       
    79 {
       
    80     setCurrentView(mTView);
       
    81     mStatename = "triangle";
       
    82 }
       
    83 
       
    84 void viewmanager::tosqure()
       
    85 {
       
    86     setCurrentView(mSView);
       
    87     mStatename = "squre";
       
    88 }
       
    89 
       
    90 void viewmanager::tocircle()
       
    91 {
       
    92     setCurrentView(mCView);
       
    93     mStatename = "circle";
       
    94 }
       
    95 
       
    96 
       
    97 void viewmanager::save()
       
    98 {
       
    99     QString color;
       
   100     figurewidget* widget = currentWidget();
       
   101     color = widget->color();
       
   102 
       
   103     QStringList list;
       
   104     list.append(mStatename);
       
   105     list.append(color);
       
   106     mSaveVariant.clear();
       
   107     mSaveVariant = list;
       
   108 
       
   109     HbMainWindow *mainWindow = hbInstance->allMainWindows().first();
       
   110     QPixmap screenshot = QPixmap::grabWidget(mainWindow, mainWindow->rect());
       
   111     QVariantHash metadata;
       
   112     metadata.insert("screenshot", screenshot);
       
   113 
       
   114 	bool ok = hbApp->activityManager()->addActivity(mStatename, mSaveVariant, metadata);	
       
   115     if (!ok) {
       
   116         widget->setFailedFlag();
       
   117     }
       
   118     else {
       
   119         widget->clearFailedFlag();
       
   120     }
       
   121 }
       
   122 
       
   123 void viewmanager::updateAct()
       
   124 {
       
   125     QString color;
       
   126     figurewidget* widget = currentWidget();
       
   127     color = widget->color();
       
   128 
       
   129     QStringList list;
       
   130     list.append(mStatename);
       
   131     list.append(color);
       
   132     mSaveVariant.clear();
       
   133     mSaveVariant = list;
       
   134 
       
   135     HbMainWindow *mainWindow = hbInstance->allMainWindows().first();
       
   136     QPixmap screenshot = QPixmap::grabWidget(mainWindow, mainWindow->rect());
       
   137     QVariantHash metadata;
       
   138     metadata.insert("screenshot", screenshot);
       
   139 
       
   140 	bool ok = hbApp->activityManager()->updateActivity(mStatename, mSaveVariant, metadata);	
       
   141     if (!ok) {
       
   142         widget->setFailedFlag();
       
   143     }
       
   144     else {
       
   145         widget->clearFailedFlag();
       
   146     }
       
   147 }
       
   148 
       
   149 void viewmanager::del()
       
   150 {
       
   151 	bool ok = hbApp->activityManager()->removeActivity(mStatename);		
       
   152 	figurewidget* widget = currentWidget();   
       
   153 	if (!ok) {
       
   154         widget->setFailedFlag();
       
   155     }
       
   156     else {
       
   157         widget->clearFailedFlag();
       
   158     }
       
   159 }
       
   160 
       
   161 void viewmanager::loadActivityData(const QString &name)
       
   162 {
       
   163     QVariant data = hbApp->activityManager()->activityData( name );
       
   164 	loadActivityData( data );				
       
   165 }
       
   166 
       
   167 void viewmanager::loadActivityData(const QVariant &data)
       
   168 {
       
   169     HbView *oldView = currentView();
       
   170     QStringList list = data.toStringList();
       
   171     QString statename = list.at(0);
       
   172     QString color = list.at(1);
       
   173     if (statename == "squre") {
       
   174         mSw->setcolor(color);
       
   175         tosqure();
       
   176         mSw->update();
       
   177     } else if (statename == "triangle") {
       
   178         mTw->setcolor(color);
       
   179         totriangle();
       
   180         mTw->update();
       
   181     } else if (statename == "circle") {
       
   182         mCw->setcolor(color);
       
   183         tocircle();
       
   184         mCw->update();
       
   185     } else {
       
   186         Q_ASSERT(false);
       
   187     }
       
   188     
       
   189     if (currentView() != oldView) {
       
   190         QEventLoop loop;
       
   191         loop.connect(this, SIGNAL(viewReady()), SLOT(quit()));
       
   192         loop.exec();
       
   193     }
       
   194 }
       
   195 
       
   196 figurewidget* viewmanager::currentWidget()
       
   197 	{
       
   198 	HbView* view = currentView();
       
   199 	QGraphicsWidget* gwidget = view->widget();
       
   200 	figurewidget* lwidget = qobject_cast<figurewidget*>(gwidget);
       
   201 	}