ganeswidgets/tsrc/fute/HgWidgetTest/src/hgtestview.cpp
author Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
Mon, 19 Apr 2010 14:40:06 +0300
changeset 0 89c329efa980
child 1 e48454f237ca
permissions -rw-r--r--
Revision: 201011 Kit: 201015

/*
* Copyright (c) 2010 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:  Another view for test application.
*
*/
#include <qgraphicslinearlayout.h>
#include <qgraphicssceneresizeevent>
#include <hbaction.h>
#include <hbinstance.h>
#include <hblabel.h>
#include <hbmainwindow.h>
#include "hgtestview.h"
#include "trace.h"

HgTestView::HgTestView(const QString &title1, const QString &title2, const QPixmap &pixmap, QGraphicsItem *parent) :
    HbView(parent), mPixmap(pixmap)
{
    FUNC_LOG;

    QList<HbMainWindow *> mainWindows = hbInstance->allMainWindows();
    if (mainWindows.count() > 0)
    {
        HbMainWindow *primaryWindow = mainWindows[0];

        setTitle(primaryWindow->currentView()->title());
        setItemVisible(Hb::AllItems, true); // ensure that all needed view items stay visible

        // set view as parent because action will be removed on view exit
        mBackAction = new HbAction(Hb::BackAction, this);
        primaryWindow->addSoftKeyAction(Hb::SecondarySoftKey, mBackAction);
        connect(mBackAction, SIGNAL(triggered()), SLOT(closeView()));

        QGraphicsLinearLayout *layout = new QGraphicsLinearLayout(Qt::Vertical);
        HANDLE_ERROR_NULL(layout);
        if (layout)
        {
            HbLabel *title1Label = new HbLabel(title1);
            title1Label->setAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
            layout->addItem(title1Label);

            HbLabel *title2Label = new HbLabel(title2);
            title2Label->setAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
            layout->addItem(title2Label);

            mIconLabel = new HbLabel;
            HANDLE_ERROR_NULL(mIconLabel);
            if (mIconLabel)
            {
                mIconLabel->setIcon(HbIcon(mPixmap));
                mIconLabel->setAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
                layout->addItem(mIconLabel);
            }
            setLayout(layout);
        }
    }
}

void HgTestView::closeView()
{
    FUNC_LOG;

    QList<HbMainWindow *> mainWindows = hbInstance->allMainWindows();
    if (mainWindows.count() > 0)
    {
        HbMainWindow *primaryWindow = mainWindows[0];
        primaryWindow->removeView(this);
        primaryWindow->setViewSwitchingEnabled(true);
        primaryWindow->removeSoftKeyAction(Hb::SecondarySoftKey, mBackAction); // restores original action
    }
    delete this; // ownership transferred back from HbMainWindow
}

void HgTestView::resizeEvent(QGraphicsSceneResizeEvent *event)
{
    FUNC_LOG;

    if (mIconLabel && event)
    {
        QSize iconSize(event->newSize().width()-5, event->newSize().height()-20);
        mIconLabel->setIcon(HbIcon(mPixmap.scaled(iconSize, Qt::KeepAspectRatio)));
    }
}