activityfw/testapplications/newtestappset/acttestfigure/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 #include <afactivitystorage.h>
       
    33 #include <afactivation.h>
       
    34 #include "afstorageglobals.h"
       
    35 
       
    36 QTM_USE_NAMESPACE
       
    37 
       
    38 
       
    39 viewmanager::viewmanager(HbApplication *hbapp):
       
    40     HbMainWindow(), mHbApp(hbapp)
       
    41 {
       
    42 
       
    43     mActivityStorage = new AfActivityStorage;
       
    44     mActivation = new AfActivation;
       
    45     
       
    46     qRegisterMetaType<Af::ActivationReason>("Af::ActivationReason");
       
    47     connect(mActivation, SIGNAL(activated(Af::ActivationReason, QString, QVariantHash)), this, SLOT(loadActivityData(Af::ActivationReason, QString, QVariantHash)));
       
    48     
       
    49     
       
    50 
       
    51     mSw = new squrewidget();
       
    52     mTw = new trianglewidget();
       
    53     mCw = new circlewidget();
       
    54     mSView = addView(mSw);
       
    55     mSView->setTitle("Squre");
       
    56     mTView = addView(mTw);
       
    57     mTView->setTitle("Triangle");
       
    58     mCView = addView(mCw);
       
    59     mCView->setTitle("Circle");
       
    60     totriangle();
       
    61     connect(mSw, SIGNAL(totriangle()), this, SLOT(totriangle()));
       
    62     connect(mSw, SIGNAL(tocircle()), this, SLOT(tocircle()));
       
    63 
       
    64     connect(mTw, SIGNAL(tocircle()), this, SLOT(tocircle()));
       
    65     connect(mTw, SIGNAL(tosqure()), this, SLOT(tosqure()));
       
    66 
       
    67     connect(mCw, SIGNAL(tosqure()), this, SLOT(tosqure()));
       
    68     connect(mCw, SIGNAL(totriangle()), this, SLOT(totriangle()));
       
    69 
       
    70     connect(mSw, SIGNAL(save()), this, SLOT(save()));
       
    71     connect(mTw, SIGNAL(save()), this, SLOT(save()));
       
    72     connect(mCw, SIGNAL(save()), this, SLOT(save()));
       
    73     connect(mSw, SIGNAL(del()), this, SLOT(del()));
       
    74     connect(mTw, SIGNAL(del()), this, SLOT(del()));
       
    75     connect(mCw, SIGNAL(del()), this, SLOT(del())); 
       
    76     
       
    77     //startup case    
       
    78     Af::ActivationReason reason = mActivation->reason();    
       
    79     QString actName;
       
    80     QVariantHash parameters;
       
    81     if (reason == Af::ActivationReasonActivity) {
       
    82         actName = mActivation->name();
       
    83         parameters = mActivation->parameters();        
       
    84         QVariant data = mActivityStorage->activityData(actName);
       
    85         loadActivityData(data, true);
       
    86     }
       
    87     //startup case end
       
    88 
       
    89 }
       
    90 
       
    91 viewmanager::~viewmanager()
       
    92 {
       
    93 	
       
    94     delete mActivityStorage;
       
    95     delete mActivation;
       
    96 }
       
    97 
       
    98 void viewmanager::totriangle()
       
    99 {
       
   100     setCurrentView(mTView);
       
   101     mStatename = "triangle";
       
   102 }
       
   103 
       
   104 void viewmanager::tosqure()
       
   105 {
       
   106     setCurrentView(mSView);
       
   107     mStatename = "squre";
       
   108 }
       
   109 
       
   110 void viewmanager::tocircle()
       
   111 {
       
   112     setCurrentView(mCView);
       
   113     mStatename = "circle";
       
   114 }
       
   115 
       
   116 
       
   117 void viewmanager::save()
       
   118 {
       
   119     QString color;
       
   120     figurewidget* widget = currentWidget();
       
   121     color = widget->color();
       
   122 
       
   123     QStringList list;
       
   124     list.append(mStatename);
       
   125     list.append(color);
       
   126     mSaveVariant.clear();
       
   127     mSaveVariant = list;
       
   128     QString visible = widget->visibility();
       
   129     QString persistent = widget->persistent();
       
   130     QString name = widget->name();
       
   131     bool delBefore = widget->delBeforeSave();
       
   132     
       
   133     HbMainWindow *mainWindow = hbInstance->allMainWindows().first();
       
   134     QPixmap screenshot = QPixmap::grabWidget(mainWindow, mainWindow->rect());
       
   135     QVariantHash metadata;
       
   136     metadata.insert("screenshot", screenshot);
       
   137     if ( visible == "true" )
       
   138         {
       
   139         metadata.insert(ActivityVisibility, true);    
       
   140         }
       
   141     else if ( visible == "false" )
       
   142         {
       
   143         metadata.insert(ActivityVisibility, false);    
       
   144         }
       
   145     
       
   146     if ( persistent == "true" )
       
   147         {
       
   148         metadata.insert(ActivityPersistence, true);    
       
   149         }
       
   150     else if ( persistent == "false" )
       
   151         {
       
   152         metadata.insert(ActivityPersistence, false);    
       
   153         }
       
   154     
       
   155     if ( name != "none" )
       
   156         {
       
   157         metadata.insert(ActivityApplicationName, name);    
       
   158         }
       
   159         
       
   160     if (delBefore) {
       
   161         bool ok = mActivityStorage->removeActivity(mStatename);
       
   162     }
       
   163 	bool ok = mActivityStorage->saveActivity(mStatename, mSaveVariant, metadata);	
       
   164     if (!ok) {
       
   165         widget->setFailedFlag();
       
   166     }
       
   167     else {
       
   168         widget->clearFailedFlag();
       
   169     }
       
   170 }
       
   171 
       
   172 
       
   173 void viewmanager::del()
       
   174 {
       
   175 	bool ok = mActivityStorage->removeActivity(mStatename);		
       
   176 	figurewidget* widget = currentWidget();   
       
   177 	if (!ok) {
       
   178         widget->setFailedFlag();
       
   179     }
       
   180     else {
       
   181         widget->clearFailedFlag();
       
   182     }
       
   183 }
       
   184 
       
   185 void viewmanager::loadActivityData(Af::ActivationReason reason, const QString &name, QVariantHash parameter)
       
   186 {
       
   187     QVariant data = mActivityStorage->activityData( name );
       
   188 	loadActivityData( data, false );				
       
   189 }
       
   190 
       
   191 void viewmanager::loadActivityData(const QVariant &data, bool startup)
       
   192 {
       
   193     HbView *oldView = currentView();
       
   194     QStringList list = data.toStringList();
       
   195     QString statename = list.at(0);
       
   196     QString color = list.at(1);
       
   197     if (statename == "squre") {
       
   198         mSw->setcolor(color);
       
   199         tosqure();
       
   200         mSw->update();
       
   201     } else if (statename == "triangle") {
       
   202         mTw->setcolor(color);
       
   203         totriangle();
       
   204         mTw->update();
       
   205     } else if (statename == "circle") {
       
   206         mCw->setcolor(color);
       
   207         tocircle();
       
   208         mCw->update();
       
   209     } else {
       
   210         Q_ASSERT(false);
       
   211     }
       
   212     
       
   213     if (currentView() != oldView && !startup) {
       
   214         QEventLoop loop;
       
   215         loop.connect(this, SIGNAL(viewReady()), SLOT(quit()));
       
   216         loop.exec();
       
   217     }
       
   218 }
       
   219 
       
   220 figurewidget* viewmanager::currentWidget()
       
   221 	{
       
   222 	HbView* view = currentView();
       
   223 	QGraphicsWidget* gwidget = view->widget();
       
   224 	figurewidget* lwidget = qobject_cast<figurewidget*>(gwidget);
       
   225 	}