activityfw/testapplications/nftapps/lptapp/basewidget.cpp
changeset 117 c63ee96dbe5f
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/activityfw/testapplications/nftapps/lptapp/basewidget.cpp	Thu Sep 16 12:11:40 2010 +0100
@@ -0,0 +1,503 @@
+/*
+* 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 "basewidget.h"
+#include <QRect>
+#include <QPainter>
+#include <QPainterPath>
+#include <QPolygon>
+#include <hbgridview.h>
+#include <QStandardItemModel>
+#include <HbScrollArea>
+#include <QPalette>
+#include <HbInstance>
+#include <f32file.h>
+
+
+
+
+basewidget::basewidget(QObject* activityStorage, QObject* activityManager, QTextStream* stream, QGraphicsItem *parent)
+    : HbWidget(parent), mActivityStorage(activityStorage), mActivityManager(activityManager), mStream(stream),
+      mPixmap(NULL),mByteArray(NULL),
+      mItemsCounter(0), mTimesCounter(0),
+      mSaveVariant(NULL), mSaveMetadata(NULL),
+      mAction("none")
+{
+
+    
+    mStartTestButton = new HbPushButton("Start");
+    mChangeTestButton = new HbPushButton("Change Test");
+    mBreakButton = new HbPushButton("Break");
+    
+    mTestCheckBox = new HbCheckBox("Delete after Save");
+    mTestCheckBox->setCheckState(Qt::Checked);
+    
+    mItemsEdid = new HbLineEdit("100");
+    mItemsEdid->setObjectName("ItemsEditLine");     
+    mQItemsValidator = new QIntValidator(this); 
+    mQItemsValidator->setRange(0, 1000000);
+    mItemsValidator = new HbValidator(this);
+    mItemsValidator->addField(mQItemsValidator, "100");
+    mItemsEdid->setValidator(mItemsValidator);
+    
+    mTimesEdid = new HbLineEdit("100");
+    mTimesEdid->setObjectName("TimesEditLine");     
+    mQTimesValidator = new QIntValidator(this); 
+    mQTimesValidator->setRange(0, 1000000);
+    mTimesValidator = new HbValidator(this);
+    mTimesValidator->addField(mQTimesValidator, "100");
+    mTimesEdid->setValidator(mTimesValidator);
+    
+    mKBytesEdid = new HbLineEdit("10");
+    mKBytesEdid->setObjectName("KBytesEditLine");     
+    mQKBytesValidator = new QIntValidator(this); 
+    mQKBytesValidator->setRange(0, 1000000);
+    mKBytesValidator = new HbValidator(this);
+    mKBytesValidator->addField(mQKBytesValidator, "10");
+    mKBytesEdid->setValidator(mKBytesValidator);
+    
+    mClientMemBeforeLabel = new HbLabel("ClMemBef");
+    mClientMemAfterLabel = new HbLabel("ClMemAft");
+    mClientMemDiffLabel = new HbLabel("ClMemDiff");
+    mDbMemBeforeLabel = new HbLabel("DbMemBef");
+    mDbMemAfterLabel = new HbLabel("DbMemAft");
+    mDbMemDiffLabel = new HbLabel("DbMemDiff");
+    mActionLabel = new HbLabel("action");
+    mIterLabel = new HbLabel("iter");
+    mTimesLabel = new HbLabel("times");
+    mStatusLabel = new HbLabel("Status");
+    mStatusValueLabel = new HbLabel("None");
+    mTimesEditLabel = new HbLabel("Times");
+    mBytesEditLabel = new HbLabel("KBytes[KB]");
+    mItemsEditLabel = new HbLabel("Items");
+        
+    
+    mGridLayout = new QGraphicsGridLayout();
+    mGridLayout->addItem(mClientMemBeforeLabel, 0, 0, 1, 2);
+    mGridLayout->addItem(mClientMemAfterLabel, 0, 2, 1, 2);
+    mGridLayout->addItem(mClientMemDiffLabel, 0, 4, 1, 2);
+    mGridLayout->addItem(mDbMemBeforeLabel, 1, 0, 1, 2);
+    mGridLayout->addItem(mDbMemAfterLabel, 1, 2, 1, 2);
+    mGridLayout->addItem(mDbMemDiffLabel, 1, 4, 1, 2);
+    mGridLayout->addItem(mActionLabel, 2, 0, 1, 2);
+    mGridLayout->addItem(mIterLabel, 2, 2, 1, 2);
+    mGridLayout->addItem(mTimesLabel, 2, 4, 1, 2);       
+    mGridLayout->addItem(mStatusLabel, 3, 0, 1, 2);
+    mGridLayout->addItem(mStatusValueLabel, 3, 2, 1, 4);
+    mGridLayout->addItem(mTimesEditLabel, 4, 0, 1, 2);
+    mGridLayout->addItem(mTimesEdid, 4, 2, 1, 4);
+    mGridLayout->addItem(mItemsEditLabel, 5, 0, 1, 2);
+    mGridLayout->addItem(mItemsEdid, 5, 2, 1, 4);
+    mGridLayout->addItem(mBytesEditLabel, 6, 0, 1, 2);
+    mGridLayout->addItem(mKBytesEdid, 6, 2, 1, 4);
+    mGridLayout->addItem(mBytesEditLabel, 7, 0, 1, 2);
+    mGridLayout->addItem(mKBytesEdid, 7, 2, 1, 4);
+    mGridLayout->addItem(mChangeTestButton, 8, 0, 1, 2);
+    mGridLayout->addItem(mStartTestButton, 8, 2, 1, 4);
+    mGridLayout->addItem(mBreakButton, 9, 0, 1, 2);
+    mGridLayout->addItem(mTestCheckBox, 9, 2, 1, 4);
+            
+    setLayout(mGridLayout);
+    
+    mTimer = new QTimer(this);
+    
+    connect(mChangeTestButton, SIGNAL(released()), this, SIGNAL(viewChangedRequest()));
+    connect(mStartTestButton, SIGNAL(released()), this, SLOT(test()));
+    connect(mTimer, SIGNAL(timeout()), this, SLOT(timeout()));
+    connect(mBreakButton, SIGNAL(released()), this, SLOT(breakTest()));
+}
+
+basewidget::~basewidget()
+{
+    delete mQItemsValidator;
+    delete mItemsValidator;
+    delete mQTimesValidator;
+    delete mTimesValidator;
+    delete mQKBytesValidator;
+    delete mKBytesValidator;
+}
+
+
+bool basewidget::TestCheckBoxChecked() const
+{
+    if( mTestCheckBox->checkState() == Qt::Checked ) {
+        return true;
+    }
+    return false;
+}
+
+int basewidget::Bytes() const
+{
+   int kb = Edit2Int(mKBytesEdid);
+   return kb*1024;
+}
+
+int basewidget::Times() const
+{
+    return Edit2Int(mTimesEdid);
+}
+
+int basewidget::Items() const
+{
+    return Edit2Int(mItemsEdid);
+}
+
+int basewidget::Edit2Int(const HbLineEdit* lineedit) const
+{
+    int retvalue = 0;
+    bool ok = false;
+    QString edittext = lineedit->text();
+    retvalue = edittext.toInt(&ok);
+    if(ok) {
+        return retvalue;
+    }
+    return 0;
+}
+
+void basewidget::prepareSave()
+{
+    clearSaveData();
+    int bytes = Bytes();    
+    mByteArray = new QByteArray(bytes, 'a');
+    
+    HbMainWindow *mainWindow = hbInstance->allMainWindows().first();
+    mPixmap = new QPixmap(QPixmap::grabWidget(mainWindow, mainWindow->rect()));
+    
+    mSaveVariant = new QVariant;
+    *mSaveVariant = *mByteArray;
+    delete mByteArray;
+    mByteArray = NULL;
+
+    
+    mSaveMetadata = new QVariantHash;
+    mSaveMetadata->insert(ActivityScreenshotKeyword, *mPixmap);
+    delete mPixmap;
+    mPixmap = NULL;
+    
+}
+
+void basewidget::clearSaveData()
+{
+    delete mByteArray;
+    mByteArray = NULL;
+    delete mPixmap;
+    mPixmap = NULL;
+    delete mSaveVariant;
+    mSaveVariant = NULL;
+    delete mSaveMetadata;
+    mSaveMetadata = NULL;
+}
+
+bool basewidget::saveActivity(int name)
+{
+    QString display;
+    display.setNum(name);
+    mSaveMetadata->insert(ActivityApplicationName, display);
+    bool retok, ok;
+    try {
+        ok = QMetaObject::invokeMethod(mActivityStorage, "saveActivity", Q_RETURN_ARG(bool, retok), Q_ARG(QString, display), Q_ARG(QVariant, *mSaveVariant), Q_ARG(QVariantHash, *mSaveMetadata));
+    }
+    catch(...) {
+        return false;
+    }
+
+    return ok && retok;
+}
+
+bool basewidget::removeActivity(const QString& name)
+{
+    bool retok, ok;
+    try {
+        ok = QMetaObject::invokeMethod(mActivityStorage, "removeActivity", Q_RETURN_ARG(bool, retok), Q_ARG(QString, name));
+    }
+    catch(...) {
+        return false;
+    }
+    return ok && retok;
+}
+
+bool basewidget::activities()
+{
+    bool ok;
+    try {
+        ok = QMetaObject::invokeMethod(mActivityStorage, "allActivities", Q_RETURN_ARG(QStringList, mActivities));
+    }
+    catch(...) {
+        return false;
+    }
+    return ok;   
+}
+
+bool basewidget::privateData(const QString& name)
+{
+    QVariant data;
+    bool ok;
+    try {
+        ok = QMetaObject::invokeMethod(mActivityStorage, "activityData", Q_RETURN_ARG(QVariant, data), Q_ARG(QString, name));
+    }
+    catch(...) {
+        return false;
+    }
+    return ok;
+}
+
+bool basewidget::getThumbnail(const QString& name)
+{
+    QVariantHash metadata;
+    bool ok;
+    try {
+        ok = QMetaObject::invokeMethod(mActivityStorage, "activityMetaData", Q_RETURN_ARG(QVariantHash, metadata), Q_ARG(QString, name));
+    }
+    catch(...) {
+        return false;
+    }
+    if(!ok) {
+        return false;
+    }
+    QString pixfile = metadata.value(ActivityScreenshotKeyword).toString();
+    ok = QMetaObject::invokeMethod(mActivityManager, "getThumbnail", Q_ARG(QSize, QSize(128, 128)), Q_ARG(QString, pixfile), Q_ARG(void*, NULL));
+    return ok;
+}
+
+int basewidget::dbSize()
+{
+    _LIT(KFilePath, "c:\\private\\200267b4\\activity.db"); 
+    TInt fileSize = 0; 
+    RFs fs;
+    TInt err = fs.Connect();
+    if (err == KErrNone) { 
+        TEntry entry;
+        err = fs.Entry(KFilePath, entry);
+        if (err == KErrNone) {
+            fileSize = entry.iSize;
+        }
+    }         
+    fs.Close();
+    return fileSize;
+}
+
+int basewidget::ClientHeapSize()
+{
+    RHeap& heap = User::Heap();
+    TInt all=0;
+    heap.AllocSize(all);
+    return all;
+}
+
+void basewidget::enableControls(bool enable)
+{
+    mStartTestButton->setEnabled(enable);
+    mChangeTestButton->setEnabled(enable);
+    mItemsEdid->setEnabled(enable);
+    mTimesEdid->setEnabled(enable);
+    mKBytesEdid->setEnabled(enable);
+    mTestCheckBox->setEnabled(enable);
+}
+
+void basewidget::timeout()
+{
+    
+}
+
+void basewidget::logFirst()
+{
+    int memclient = ClientHeapSize();
+    int memdb = dbSize();
+    mClientMemBefore = memclient;
+    mDbMemBefore = memdb;
+    
+    QString clinetKB;
+    clinetKB.setNum(memclient/1024.0, 'f', 2);
+    mClientMemBeforeLabel->setPlainText(clinetKB);
+    
+    QString dbKB;
+    dbKB.setNum(memdb/1024.0, 'f', 2);
+    mDbMemBeforeLabel->setPlainText(dbKB);
+    
+    *mStream<<"Client heap first: ";
+    *mStream<<memclient<<"\n";
+    *mStream<<"Db size first: ";
+    *mStream<<memdb<<"\n";
+    mStream->flush();
+}
+
+void basewidget::logLast()
+{
+    int memclient = ClientHeapSize();
+    int memdb = dbSize();
+    int clientdiff = memclient - mClientMemBefore;
+    int dbdiff = memdb - mDbMemBefore;
+    
+    QString clinetKB;
+    clinetKB.setNum(memclient/1024.0, 'f', 2);
+    mClientMemAfterLabel->setPlainText(clinetKB);
+    
+    QString dbKB;
+    dbKB.setNum(memdb/1024.0, 'f', 2);
+    mDbMemAfterLabel->setPlainText(dbKB);
+    
+    QString clientDiffKb;
+    clientDiffKb.setNum(clientdiff/1024.0, 'f', 2);
+    mClientMemDiffLabel->setPlainText(clientDiffKb);
+    
+    
+    QString dbDiffKb;
+    dbDiffKb.setNum(dbdiff/1024.0, 'f', 2);
+    mDbMemDiffLabel->setPlainText(dbDiffKb);
+    
+    *mStream<<"Client heap last: ";
+    *mStream<<memclient<<"\n";
+    *mStream<<"Db size last: ";
+    *mStream<<memdb<<"\n";
+    
+    *mStream<<"Client heap difference: ";
+    *mStream<<clientdiff<<"\n";
+    *mStream<<"Db size defference: ";
+    *mStream<<dbdiff<<"\n";
+    mStream->flush();
+}
+
+void basewidget::logBaseMessage()
+{
+    *mStream <<"action: ";
+    *mStream<<mAction<<"\n";
+    *mStream <<"itemCounter: ";
+    *mStream<<mItemsCounter<<"\n";
+    *mStream <<"TimeCounter: ";
+    *mStream<<mTimesCounter<<"\n";
+    mStream->flush();
+}
+
+void basewidget::logErrorMessage(const QString& mess)
+{
+    QString messlog("*Fail: ");
+    messlog += mess;
+    messlog += "*\n";
+    *mStream<<messlog;
+    logBaseMessage();
+}
+
+void basewidget::updateItemLabel(int item)
+{
+    QString num;
+    num.setNum(item);
+    mIterLabel->setPlainText(num);
+}
+
+void basewidget::updateTimeLabel(int time)
+{
+    QString num;
+    num.setNum(time);
+    mTimesLabel->setPlainText(num);
+    
+}
+
+void basewidget::updateActionLabel()
+{
+    mActionLabel->setPlainText(mAction);
+}
+
+bool basewidget::checkActNum()
+{
+    int getact = mActivities.count();
+    int expectedact = Items();
+    if (getact != expectedact) {
+        *mStream<<"*Fail: Unexpected activities number got*\n";
+        *mStream<<"Got: "<<getact<<", but expected: "<<expectedact<<"\n";
+        return false;
+    }
+    return true;        
+}
+
+void basewidget::endTest(int reason)
+{
+    mTimer->stop();
+    if(reason == 0) {
+       mStatusValueLabel->setPlainText("End"); 
+    }
+    else if(reason == 1) {
+       mStatusValueLabel->setPlainText("Error"); 
+      }
+    else if(reason == 2) {
+       mStatusValueLabel->setPlainText("Break"); 
+      }
+    emit testEnded(reason);  
+}
+
+void basewidget::releaseMem()
+{
+    mActivities.clear();
+    clearSaveData();
+}
+
+void basewidget::setError(const QString& mess)
+{
+    releaseMem();
+    logErrorMessage(mess);
+    if( mAction != "predel" && mAction != "presave" ) {
+        logLast();
+    }
+    endTest(1);
+    enableControls(true);
+}
+
+void basewidget::endNormally()
+{
+    releaseMem();
+    logLast();    
+    endTest(0);
+    enableControls(true);
+}
+
+void basewidget::breakTest()
+{
+    releaseMem();
+    endTest(2);
+    *mStream<<"Breaked\n";
+    mStream->flush();
+    enableControls(true);
+}
+
+void basewidget::logSettings()
+{
+    *mStream <<"items: ";
+    *mStream<<Items()<<"\n";
+    *mStream <<"Times: ";
+    *mStream<<Times()<<"\n";
+    *mStream <<"Checkbox: ";
+    *mStream<<TestCheckBoxChecked()<<"\n";
+    mStream->flush();   
+}
+
+void basewidget::clearLabels()
+{
+    mClientMemBeforeLabel->clear();
+    mClientMemAfterLabel->clear();
+    mClientMemDiffLabel->clear();
+    mDbMemBeforeLabel->clear();
+    mDbMemAfterLabel->clear();
+    mDbMemDiffLabel->clear();    
+    mIterLabel->clear();
+    mTimesLabel->clear();    
+    mStatusValueLabel->clear();
+}
+
+void basewidget::test()
+{
+    updateActionLabel();
+    clearLabels();
+    mTimer->start(1);
+}