ui/viewmanagement/viewmanager/src/glxviewmanager.cpp
author Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
Fri, 19 Mar 2010 09:28:59 +0200
changeset 23 74c9f037fd5d
child 24 99ad1390cd33
permissions -rw-r--r--
Revision: 201007 Kit: 201011

/*
* 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:   ?Description
*
*/



#include <glxviewmanager.h>
#include <glxview.h>
#include <glxviewsfactory.h>
#include <glxeffectengine.h>
#include <hbmainwindow.h>
#include <glxexternalutility.h>
#include <glxloggerenabler.h>
#include <glxmenumanager.h>
#include <glxcommandhandlers.hrh>
#include <glxplugincommandid.hrh>

#include <hbaction.h>
#include <hbtoolbar.h>
#include <hbmenu.h>
#include <QDebug>


GlxViewManager::GlxViewManager() : mBackAction( NULL ), mMenuManager( NULL ), mEffectEngine( NULL ), 
            mViewToolBar( NULL ), mMarkingToolBar( NULL ), mMenu( NULL )
{
    qDebug("GlxViewManager::GlxViewManager() ");
    PERFORMANCE_ADV ( viewMgrD1, "main window creation time" ) {
        //check the case when application launch through some other application (view plugin)
        mMainWindow = GlxExternalUtility::instance()->getMainWindow();
        if(mMainWindow == NULL)	{
            mMainWindow = new HbMainWindow();
        }
    } //end PERFORMANCE_ADV
    
    //mMainWindow->setOptimizationFlag(QGraphicsView::DontSavePainterState);
}

void GlxViewManager::setupItems(int subState)
{
    mMenuManager = new GlxMenuManager();
    addBackSoftKeyAction(); 
    createToolBar();
    addConnection();
    mView->addToolBar(mViewToolBar);
    mMenuManager->CreateViewMenu( mView->viewId(), mView->menu(), ( 0 == mModel->rowCount() ),subState);
}

void GlxViewManager::launchApplication(qint32 id, QAbstractItemModel *model)
{
    mModel = model;
    PERFORMANCE_ADV ( viewMgrD1, "View Creation time" ) {
        mView = resolveView(id);
    }
    mView->activate();
    //mView->show();
    
    PERFORMANCE_ADV ( viewMgrD3, "Set Model time")  
        mView->setModel(mModel);
        
    PERFORMANCE_ADV( viewMgrD4, "View Display time") {
        mMainWindow->setCurrentView(mView, false);
        mMainWindow->showFullScreen();           
    }    
}

void GlxViewManager::handleMenuAction(qint32 commandId)
{
    emit actionTriggered(commandId);
}

void GlxViewManager::handleAction()
{
    HbAction *action = qobject_cast<HbAction*>(sender());
    qint32 commandId = action->data().toInt();
    emit actionTriggered(commandId);
}

void GlxViewManager::addBackSoftKeyAction ( )
{
    qDebug("GlxViewManager::addBackSoftKeyAction ");
    //create the back soft key action and set the data
    mBackAction = new HbAction(Hb::BackAction, this);
    mBackAction->setData(EGlxCmdBack);
    
    //remove the current secondary soft key for main window and set the new created one
    HbAction *action = mMainWindow->softKeyAction(Hb::SecondarySoftKey);
    mMainWindow->removeSoftKeyAction(Hb::SecondarySoftKey,action);
    mMainWindow->addSoftKeyAction(Hb::SecondarySoftKey, mBackAction);
}

Qt::Orientation GlxViewManager::orientation() const
{
    return mMainWindow->orientation();
}


void GlxViewManager::launchView(qint32 id, QAbstractItemModel *model)
{
    qDebug("GlxViewManager::launchView Id = %d ", id);
    mModel = model;
    deActivateView();
    
    PERFORMANCE_ADV ( viewMgrD1, "View Creation time" ) {
        mView = resolveView(id);
    }
    
    activateView();
}

void GlxViewManager::launchView (qint32 id, QAbstractItemModel *model, GlxEffect effect, GlxViewEffect viewEffect)
{
    qDebug("GlxViewManager::launchView Id = %d effect %d view effect %d", id, effect, viewEffect);

    //In the case of no animation is play during the view transition just call launch view and return
    if ( viewEffect == NO_VIEW ) {
        return launchView(id, model);
    }
    
    //create and registered the effect
    if ( mEffectEngine == NULL ) { 
        mEffectEngine = new GlxSlideShowEffectEngine();
        mEffectEngine->registerTransitionEffect();
        connect( mEffectEngine, SIGNAL( effectFinished() ), this, SLOT( effectFinished() ) );
    }
    
    QList< QGraphicsItem * > itemList;
    QGraphicsItem *item = NULL;
    itemList.clear();
    
    //partially clean the view so that animation run smoothly
    GlxView *curr_view = (GlxView *) mMainWindow->currentView();
    curr_view->resetView();
    
    mView = resolveView(id);
    //partially initialise the view so that animation run smoothly
    mView->initializeView( model);
    mModel = model; 

    if ( viewEffect == CURRENT_VIEW || viewEffect == BOTH_VIEW ) { 
        item = curr_view->getAnimationItem(effect);
        if ( item ) {
            itemList.append(item);
            item = NULL;
        }
    }
    
    if ( viewEffect == LAUNCH_VIEW || viewEffect == BOTH_VIEW ) {
        item = mView->getAnimationItem(effect);
        if ( item ) {
            //increase the z value and show the view to shown the view animation
            mView->setZValue(curr_view->zValue() + 2);
            mView->show();
            item->show();        
            itemList.append(item);
        }
    }
    
    //error check
    if ( itemList.count() > 0 ) {
        mEffectEngine->runEffect(itemList, effect);
        mMainWindow->grabMouse();
    }
    else {
        deActivateView();
        activateView();       
    }    
}

//to be called only when the photos plugin was activated by external means
void GlxViewManager::deactivateCurrentView()
{
    GlxView *curr_view = (GlxView *) mMainWindow->currentView();
    if ( curr_view ) {
        curr_view->deActivate() ;
    }
}

void GlxViewManager::updateToolBarIcon(int id)
{
    QString path;
    int toolBarActionId = (int) GLX_ALL_ACTION_ID;
    int count = mActionList.count();
    
    qDebug("GlxViewManager::updateToolBarIcon() action ID list %d count %d", id, count);
    
    for ( int i = 0; i < count ; i++ ) {
        qDebug("GlxViewManager::updateToolBarIcon() toolBarActionId %d value %d", toolBarActionId, ( id & toolBarActionId ) );
        //check and get the icon path
        if ( ( id & toolBarActionId ) == toolBarActionId ) {
            path = mSelIconPathList.at(i);
        }
        else {
            path = mDefaultIconPathList.at(i);
        }
        //to get it the next action id to verify it is selecter or not
        toolBarActionId = toolBarActionId << 1; 
        HbIcon icon(path);
        mActionList[i]->setIcon(icon) ;
    }
}

void GlxViewManager::enterMarkingMode(qint32 viewId)
{
    GlxView *view = findView ( viewId );
    qDebug("GlxViewManager::enterMarkingMode view ID %d", viewId);
    
    //In the case of first time create the marking mode menu( Mark All, Un Mark All )
    if( mMenu == NULL ) {
        mMenu = new HbMenu();
        mMenuManager->createMarkingModeMenu(mMenu);
    }
    
    if ( mMarkingToolBar == NULL) {
        createMarkingModeToolBar(); //Marking mode tool bar is different from normal mode tool bar
    }
    
    if ( view ) { 
        view->enableMarking();
        HbMenu *menu = view->takeMenu(); //Take the owner ship of current menu
        view->setMenu(mMenu); //Set the marking mode menu
        mMenu = menu;
        view->takeToolBar();
        view->addToolBar(mMarkingToolBar);
    }
    qDebug("GlxViewManager::enterMarkingMode view ID %d exit", viewId);
}

void GlxViewManager::exitMarkingMode(qint32 viewId)
{
    GlxView *view = findView ( viewId );
    qDebug("GlxViewManager::exitMarkingMode view ID %d", viewId);
    if ( view ) { 
        //createToolBar(); //Marking mode tool bar is different from normal mode tool bar
        view->disableMarking(); 
        HbMenu *menu = view->takeMenu(); //Take the owner ship of current menu
        view->setMenu(mMenu); //Set the view menu option
        mMenu = menu;
        view->takeToolBar();
        view->addToolBar(mViewToolBar);
    }
    qDebug("GlxViewManager::exitMarkingMode view ID %d exit", viewId);
}

void GlxViewManager::handleUserAction(qint32 viewId, qint32 commandId)
{
    GlxView *view = findView ( viewId );
    qDebug("GlxViewManager::handleUserAction view ID %d command id %d", viewId, commandId);
    if ( view ) { 
        view->handleUserAction(commandId);
    }    
}

QItemSelectionModel *  GlxViewManager::getSelectionModel(qint32 viewId) 
{ 
    GlxView *view = findView ( viewId );
    if ( view ) { 
        return view->getSelectionModel();
    }
    return NULL;
}

GlxView * GlxViewManager::resolveView(qint32 id)
{
    qDebug("GlxViewManager::resolveView %d", id);
    GlxView *view = findView ( id );
    if ( view ) {
        return view ;
    }
    
    view = GlxViewsFactory::createView(id, mMainWindow);
    if ( view ) {
        //view->addToolBar(mViewToolBar);
        connect ( view, SIGNAL(actionTriggered(qint32 )), this, SLOT(actionProcess(qint32 )), Qt::QueuedConnection );
        connect ( view, SIGNAL(itemSpecificMenuTriggered(qint32,QPointF ) ), this, SLOT( itemSpecificMenuTriggered(qint32,QPointF ) ));
        mViewList.append(view);
        mMainWindow->addView(view);
    }
    return view;
}

void GlxViewManager::itemSpecificMenuTriggered(qint32 viewId,QPointF pos)
{
    mMenuManager->ShowItemSpecificMenu(viewId,pos);
}

void GlxViewManager::cancelTimer()
{
    emit externalCommand(EGlxPluginCmdUserActivity);
}

void GlxViewManager::effectFinished( )
{
    qDebug("GlxViewManager::EffectFinished");
    mMainWindow->releaseMouse();
    deActivateView();
    activateView(); 
}

GlxView * GlxViewManager::findView(qint32 id)
{
    qDebug("GlxViewManager::findView Id = %d ", id);
    int count = mViewList.count();
    
    for ( int i = 0 ; i < count; i++) {
        if ( mViewList.at(i)->compare(id) ) {
            return mViewList.at(i);
        }
    }
    //qDebug("GlxViewManager::findView view = %u ", view);
    return NULL;
}

void GlxViewManager::deActivateView()
{
    qDebug("GlxViewManager::deActivateCurrentView()");
    
    GlxView *view = (GlxView *) mMainWindow->currentView();
    if ( view ){
        view->deActivate() ;
        //view->hide(); //To:do is it required
    }
    if (mView && view) {
        mView->setZValue(view->zValue());
    }
}

void GlxViewManager::activateView()
{
    qDebug("GlxViewManager::activateView()");
        
    PERFORMANCE_ADV ( viewMgrD2, "View Activation time") {
        mView->addToolBar(mViewToolBar);
        mView->activate();
        mView->show();
        //check and create the view menu
        if ( mView->menu()->isEmpty() ) {
            mMenuManager->CreateViewMenu( mView->viewId(), mView->menu(), ( 0 == mModel->rowCount() ));
        }
    }        
      
    PERFORMANCE_ADV ( viewMgrD3, "Set Model time")  
        mView->setModel(mModel);
        
    PERFORMANCE_ADV( viewMgrD4, "View Display time") {
        mMainWindow->setCurrentView(mView, false);
        mMainWindow->showFullScreen();           
    }
}

void GlxViewManager::createActions()
{
    qDebug("GlxViewManager::createActions() " );
    mActionList.clear();
    mSelIconPathList.clear();
    mDefaultIconPathList.clear();    
    
    //create the All tool bar button action
    HbAction* allAction = new HbAction(this);
    allAction->setData(EGlxCmdAllGridOpen);
    mActionList.append(allAction);
    mSelIconPathList.append(QString(":/data/All_selected.png"));
    mDefaultIconPathList.append(QString(":/data/All_default.png"));

    //create the Album tool bar button action
    HbAction* albumAction = new HbAction(this);
    albumAction->setData(EGlxCmdAlbumListOpen);
    mActionList.append(albumAction);
    mSelIconPathList.append(QString(":/data/Albums_selected.png"));
    mDefaultIconPathList.append(QString(":/data/Albums_default.png"));
    
    //create the album tool bar button action
    HbAction* cameraAction = new HbAction(this);
    cameraAction->setData(EGlxCmdCameraOpen);
    mActionList.append(cameraAction);
    mSelIconPathList.append(QString(":/data/camera_selected.png"));
    mDefaultIconPathList.append(QString(":/data/camera_default.png"));
    
    //create the ovi tool bar button action
    HbAction* oviAction = new HbAction(this);
    oviAction->setData(EGlxCmdOviOpen);
    mActionList.append(oviAction);
    mSelIconPathList.append(QString(":/data/ovi_selected.png"));
    mDefaultIconPathList.append(QString(":/data/ovi_default.png"));
}

void GlxViewManager::createMarkingModeActions()
{
    mMarkingActionList.clear();
    
    //create the ok tool bar button action
    HbAction* selectAction = new HbAction("Ok", this);
    selectAction->setData(EGlxCmdSelect);
    mMarkingActionList.append(selectAction);
    connect( selectAction, SIGNAL(triggered( )), this, SLOT(handleAction( )), Qt::QueuedConnection );
    mMarkingToolBar->addAction( selectAction );
    
    //create the cancel tool bar button action
    HbAction* cancelAction = new HbAction("Cancel", this);
    cancelAction->setData(EGlxCmdCancel);
    mMarkingActionList.append(cancelAction); 
    connect( cancelAction, SIGNAL(triggered( )), this, SLOT(handleAction( )), Qt::QueuedConnection ); 
    mMarkingToolBar->addAction( cancelAction );
}


void GlxViewManager::createToolBar()
{
    qDebug("GlxViewManager::createToolBar() " );
    
    mViewToolBar = new HbToolBar();
    mViewToolBar->setOrientation( Qt::Horizontal );
    mViewToolBar->setVisible(true);
    mViewToolBar->clearActions();
    
    createActions();    
    
    qDebug("GlxViewManager::createToolBar() clear the action" );
    int count = mActionList.count();
    for ( int i = 0; i < count; i++ ) {
        connect( mActionList.at(i), SIGNAL(triggered( )), this, SLOT(handleAction( )) );
        mViewToolBar->addAction( mActionList.at(i) );      
    }
    qDebug("GlxViewManager::createToolBar() exit" );
}

void GlxViewManager::createMarkingModeToolBar()
{
    qDebug("GlxViewManager::createMarkingModeToolBar() " );
    mMarkingToolBar = new HbToolBar();
    mMarkingToolBar->setOrientation( Qt::Horizontal );
    mMarkingToolBar->setVisible(true);            
    mMarkingToolBar->clearActions();
    
    createMarkingModeActions();
}

void GlxViewManager::addConnection()
{    
    if ( mMenuManager )
        connect(mMenuManager, SIGNAL( commandTriggered(qint32 ) ), this, SLOT( handleMenuAction(qint32 ) ));
    if ( mBackAction )
        connect(mBackAction, SIGNAL( triggered() ), this, SLOT( handleAction() ));
        
    if ( mEffectEngine )  {
        connect( mEffectEngine, SIGNAL( effectFinished() ), this, SLOT( effectFinished() ) );
    }        
}

void GlxViewManager::removeConnection()
{
    int count = mActionList.count();
    for ( int i = 0; i < count; i++ ) {
        disconnect( mActionList.at(i), SIGNAL(triggered( )), this, SLOT(handleAction( )) );  
    }
    
    count = mMarkingActionList.count();
    for ( int i = 0; i < count; i++ ) {
        disconnect( mMarkingActionList.at(i), SIGNAL(triggered( )), this, SLOT(handleAction( )) );  
    }
    
    count = mViewList.count();
    for ( int i = 0; i < count; i++ ) {
        disconnect ( mViewList.at(i), SIGNAL(actionTriggered(qint32 )), this, SLOT(actionProcess(qint32 )) );
        disconnect ( mViewList.at(i), SIGNAL(itemSpecificMenuTriggered(qint32, QPointF ) ), this, SLOT( itemSpecificMenuTriggered(qint32, QPointF ) ));
    }
	   
    if ( mMenuManager )
        disconnect(mMenuManager, SIGNAL( commandTriggered(qint32 ) ), this, SLOT( handleMenuAction(qint32 ) ));
    if ( mBackAction )
        disconnect(mBackAction, SIGNAL( triggered() ), this, SLOT( handleAction() ));
        
    if ( mEffectEngine )  {
        disconnect( mEffectEngine, SIGNAL( effectFinished() ), this, SLOT( effectFinished() ) );
    }
}

void GlxViewManager::destroyView(qint32 id)
{
    qDebug("GlxViewManager::destroyView ");
    GlxView *view = findView ( id );
    mViewList.removeOne(view);
    delete view;
}

void GlxViewManager::actionProcess(qint32 id)
{
    qDebug("GlxViewManager::actionProcess action Id = %d ", id);
    emit actionTriggered(id);
}

GlxViewManager::~GlxViewManager()
{
    qDebug("GlxViewManager::~GlxViewManager");
	
    removeConnection();
    delete mMenuManager;
    qDebug("GlxViewManager::~GlxViewManager deleted menu manager");
    
    while( mViewList.isEmpty( ) == FALSE){
        delete mViewList.takeLast() ;
    }
    qDebug("GlxViewManager::~GlxViewManager view deleted");
    
    while( mActionList.isEmpty() == FALSE) {
        delete mActionList.takeLast();
    }  
	
    while ( mMarkingActionList.isEmpty() == FALSE ) {
        delete mMarkingActionList.takeLast();
    }
    qDebug("GlxViewManager::~GlxViewManager delete action list");
    
    delete mBackAction;
    delete mViewToolBar;
    delete mMarkingToolBar;
    delete mMenu;
    qDebug("GlxViewManager::~GlxViewManager delete toolbar");
    
    mSelIconPathList.clear();
    mDefaultIconPathList.clear();
    qDebug("GlxViewManager::~GlxViewManager clear path list");
    
    if ( mEffectEngine ) {
        mEffectEngine->deregistertransitionEffect();
        delete mEffectEngine;
    }
 
    if( mMainWindow != GlxExternalUtility::instance()->getMainWindow() ){
        qDebug("GlxViewManager::~GlxViewManager remove view");
        delete mMainWindow;
    }
    
    qDebug("GlxViewManager::~GlxViewManager Exit");
}