/*
* 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:
*
*/
#include "iconsview.h"
#include <QGraphicsLinearLayout>
#include <QGraphicsGridLayout>
#include <QStandardItem>
#include <QStandardItemModel>
#include <QSignalMapper>
#include <hblistwidget.h>
#include <hblabel.h>
#include <hbpushbutton.h>
#include <hbinstance.h>
#include <hbapplication.h>
#include <hbactivitymanager.h>
#include <hbmainwindow.h>
#include <QDebug>
#define hbApp qobject_cast<HbApplication*>(qApp)
IconsView::IconsView(QGraphicsItem *parent) : HbView(parent)
{
setTitle("Activities -Hb Model");
mModel = new QStandardItemModel(this);
for (int i=1; i<=8; ++i) {
addItemToModel(i);
}
mList = new HbListView(this);
mList->setModel(mModel);
QGraphicsGridLayout *buttonsLayout = new QGraphicsGridLayout();
HbPushButton *addItemButton = new HbPushButton("Add item");
connect(addItemButton, SIGNAL(clicked()), this, SLOT(addItem()));
buttonsLayout->addItem(addItemButton, 0, 0);
HbPushButton *removeItemButtom = new HbPushButton("Remove item");
connect(removeItemButtom, SIGNAL(clicked()), this, SLOT(removeItem()));
buttonsLayout->addItem(removeItemButtom, 0, 1);
HbPushButton *saveActivityAButton = new HbPushButton("Save State A");
buttonsLayout->addItem(saveActivityAButton, 1, 0);
HbPushButton *saveActivityBButton = new HbPushButton("Save State B");
buttonsLayout->addItem(saveActivityBButton, 1, 1);
buttonsLayout->setRowFixedHeight(0, 50);
buttonsLayout->setRowFixedHeight(1, 50);
QSignalMapper *saveActivityMapper = new QSignalMapper(this);
saveActivityMapper->setMapping(saveActivityAButton, "State A");
saveActivityMapper->setMapping(saveActivityBButton, "State B");
connect(saveActivityAButton, SIGNAL(clicked()), saveActivityMapper, SLOT(map()));
connect(saveActivityBButton, SIGNAL(clicked()), saveActivityMapper, SLOT(map()));
connect(saveActivityMapper, SIGNAL(mapped(QString)), this, SLOT(saveActivity(QString)));
mItemCountLabel = new HbLabel();
mItemCountLabel->setAlignment(Qt::AlignCenter);
mItemCountLabel->setMaximumHeight(30);
updateCountLabel();
QGraphicsLinearLayout *mainLayout = new QGraphicsLinearLayout(Qt::Vertical, this);
mainLayout->addItem(mItemCountLabel);
mainLayout->addItem(mList);
mainLayout->addItem(buttonsLayout);
connect(hbApp->activityManager(), SIGNAL(activityRequested(QString)), this, SLOT(loadActivity(QString)));
bool ok = hbApp->activityManager()->waitActivity();
if (!ok) {
//qFatal("Subscribe failed");
}
setLayout(mainLayout);
}
IconsView::~IconsView()
{
}
void IconsView::addItemToModel(int number)
{
QStandardItem *item = new QStandardItem(QIcon(QString(":data\\img%1").arg(number)), QString("Image %1").arg(number));
mModel->appendRow(item);
}
void IconsView::addItem()
{
addItemToModel(qrand() % 8 + 1);
updateCountLabel();
}
void IconsView::removeItem()
{
if (mModel->rowCount() > 0) {
mModel->removeRow(mModel->rowCount() - 1, QModelIndex());
}
updateCountLabel();
}
void IconsView::updateCountLabel()
{
mItemCountLabel->setPlainText(QString("List contains %1 items").arg(mModel->rowCount()));
}
QVariant IconsView::generateActivityData()
{
QByteArray serializedModel;
{
QDataStream stream(&serializedModel, QIODevice::WriteOnly | QIODevice::Append);
for (int row = 0; row < mModel->rowCount(); ++row) {
stream << *mModel->item(row);
}
}
return serializedModel;
}
void IconsView::saveActivity(const QString &name)
{
HbMainWindow *mainWindow = hbInstance->allMainWindows().value(0);
QVariantHash metadata;
metadata.insert("screenshot", QPixmap::grabWidget(mainWindow, mainWindow->rect()));
bool ok = hbApp->activityManager()->removeActivity(name);
if (!ok) {
//qFatal("Remove failed" );
}
ok = hbApp->activityManager()->addActivity(name, generateActivityData(), metadata);
if (!ok) {
qFatal("Add failed");
}
}
void IconsView::loadActivity(const QString &name)
{
bool ok = hbApp->activityManager()->waitActivity();
if (!ok) {
//qFatal("Resubscribe failed");
}
QVariant data = hbApp->activityManager()->activityData(name);
loadActivity(data);
}
void IconsView::loadActivity(const QVariant &data)
{
mModel->clear();
QByteArray serializedModel = data.toByteArray();
{
QDataStream stream(&serializedModel, QIODevice::ReadOnly);
while (!stream.atEnd()) {
QStandardItem *item = new QStandardItem();
stream >> *item;
mModel->appendRow(item);
}
}
updateCountLabel();
}