activityfw/testapplications/nftapps/lptapp/basewidget.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 "basewidget.h"
       
    18 #include <QRect>
       
    19 #include <QPainter>
       
    20 #include <QPainterPath>
       
    21 #include <QPolygon>
       
    22 #include <hbgridview.h>
       
    23 #include <QStandardItemModel>
       
    24 #include <HbScrollArea>
       
    25 #include <QPalette>
       
    26 #include <HbInstance>
       
    27 #include <f32file.h>
       
    28 
       
    29 
       
    30 
       
    31 
       
    32 basewidget::basewidget(QObject* activityStorage, QObject* activityManager, QTextStream* stream, QGraphicsItem *parent)
       
    33     : HbWidget(parent), mActivityStorage(activityStorage), mActivityManager(activityManager), mStream(stream),
       
    34       mPixmap(NULL),mByteArray(NULL),
       
    35       mItemsCounter(0), mTimesCounter(0),
       
    36       mSaveVariant(NULL), mSaveMetadata(NULL),
       
    37       mAction("none")
       
    38 {
       
    39 
       
    40     
       
    41     mStartTestButton = new HbPushButton("Start");
       
    42     mChangeTestButton = new HbPushButton("Change Test");
       
    43     mBreakButton = new HbPushButton("Break");
       
    44     
       
    45     mTestCheckBox = new HbCheckBox("Delete after Save");
       
    46     mTestCheckBox->setCheckState(Qt::Checked);
       
    47     
       
    48     mItemsEdid = new HbLineEdit("100");
       
    49     mItemsEdid->setObjectName("ItemsEditLine");     
       
    50     mQItemsValidator = new QIntValidator(this); 
       
    51     mQItemsValidator->setRange(0, 1000000);
       
    52     mItemsValidator = new HbValidator(this);
       
    53     mItemsValidator->addField(mQItemsValidator, "100");
       
    54     mItemsEdid->setValidator(mItemsValidator);
       
    55     
       
    56     mTimesEdid = new HbLineEdit("100");
       
    57     mTimesEdid->setObjectName("TimesEditLine");     
       
    58     mQTimesValidator = new QIntValidator(this); 
       
    59     mQTimesValidator->setRange(0, 1000000);
       
    60     mTimesValidator = new HbValidator(this);
       
    61     mTimesValidator->addField(mQTimesValidator, "100");
       
    62     mTimesEdid->setValidator(mTimesValidator);
       
    63     
       
    64     mKBytesEdid = new HbLineEdit("10");
       
    65     mKBytesEdid->setObjectName("KBytesEditLine");     
       
    66     mQKBytesValidator = new QIntValidator(this); 
       
    67     mQKBytesValidator->setRange(0, 1000000);
       
    68     mKBytesValidator = new HbValidator(this);
       
    69     mKBytesValidator->addField(mQKBytesValidator, "10");
       
    70     mKBytesEdid->setValidator(mKBytesValidator);
       
    71     
       
    72     mClientMemBeforeLabel = new HbLabel("ClMemBef");
       
    73     mClientMemAfterLabel = new HbLabel("ClMemAft");
       
    74     mClientMemDiffLabel = new HbLabel("ClMemDiff");
       
    75     mDbMemBeforeLabel = new HbLabel("DbMemBef");
       
    76     mDbMemAfterLabel = new HbLabel("DbMemAft");
       
    77     mDbMemDiffLabel = new HbLabel("DbMemDiff");
       
    78     mActionLabel = new HbLabel("action");
       
    79     mIterLabel = new HbLabel("iter");
       
    80     mTimesLabel = new HbLabel("times");
       
    81     mStatusLabel = new HbLabel("Status");
       
    82     mStatusValueLabel = new HbLabel("None");
       
    83     mTimesEditLabel = new HbLabel("Times");
       
    84     mBytesEditLabel = new HbLabel("KBytes[KB]");
       
    85     mItemsEditLabel = new HbLabel("Items");
       
    86         
       
    87     
       
    88     mGridLayout = new QGraphicsGridLayout();
       
    89     mGridLayout->addItem(mClientMemBeforeLabel, 0, 0, 1, 2);
       
    90     mGridLayout->addItem(mClientMemAfterLabel, 0, 2, 1, 2);
       
    91     mGridLayout->addItem(mClientMemDiffLabel, 0, 4, 1, 2);
       
    92     mGridLayout->addItem(mDbMemBeforeLabel, 1, 0, 1, 2);
       
    93     mGridLayout->addItem(mDbMemAfterLabel, 1, 2, 1, 2);
       
    94     mGridLayout->addItem(mDbMemDiffLabel, 1, 4, 1, 2);
       
    95     mGridLayout->addItem(mActionLabel, 2, 0, 1, 2);
       
    96     mGridLayout->addItem(mIterLabel, 2, 2, 1, 2);
       
    97     mGridLayout->addItem(mTimesLabel, 2, 4, 1, 2);       
       
    98     mGridLayout->addItem(mStatusLabel, 3, 0, 1, 2);
       
    99     mGridLayout->addItem(mStatusValueLabel, 3, 2, 1, 4);
       
   100     mGridLayout->addItem(mTimesEditLabel, 4, 0, 1, 2);
       
   101     mGridLayout->addItem(mTimesEdid, 4, 2, 1, 4);
       
   102     mGridLayout->addItem(mItemsEditLabel, 5, 0, 1, 2);
       
   103     mGridLayout->addItem(mItemsEdid, 5, 2, 1, 4);
       
   104     mGridLayout->addItem(mBytesEditLabel, 6, 0, 1, 2);
       
   105     mGridLayout->addItem(mKBytesEdid, 6, 2, 1, 4);
       
   106     mGridLayout->addItem(mBytesEditLabel, 7, 0, 1, 2);
       
   107     mGridLayout->addItem(mKBytesEdid, 7, 2, 1, 4);
       
   108     mGridLayout->addItem(mChangeTestButton, 8, 0, 1, 2);
       
   109     mGridLayout->addItem(mStartTestButton, 8, 2, 1, 4);
       
   110     mGridLayout->addItem(mBreakButton, 9, 0, 1, 2);
       
   111     mGridLayout->addItem(mTestCheckBox, 9, 2, 1, 4);
       
   112             
       
   113     setLayout(mGridLayout);
       
   114     
       
   115     mTimer = new QTimer(this);
       
   116     
       
   117     connect(mChangeTestButton, SIGNAL(released()), this, SIGNAL(viewChangedRequest()));
       
   118     connect(mStartTestButton, SIGNAL(released()), this, SLOT(test()));
       
   119     connect(mTimer, SIGNAL(timeout()), this, SLOT(timeout()));
       
   120     connect(mBreakButton, SIGNAL(released()), this, SLOT(breakTest()));
       
   121 }
       
   122 
       
   123 basewidget::~basewidget()
       
   124 {
       
   125     delete mQItemsValidator;
       
   126     delete mItemsValidator;
       
   127     delete mQTimesValidator;
       
   128     delete mTimesValidator;
       
   129     delete mQKBytesValidator;
       
   130     delete mKBytesValidator;
       
   131 }
       
   132 
       
   133 
       
   134 bool basewidget::TestCheckBoxChecked() const
       
   135 {
       
   136     if( mTestCheckBox->checkState() == Qt::Checked ) {
       
   137         return true;
       
   138     }
       
   139     return false;
       
   140 }
       
   141 
       
   142 int basewidget::Bytes() const
       
   143 {
       
   144    int kb = Edit2Int(mKBytesEdid);
       
   145    return kb*1024;
       
   146 }
       
   147 
       
   148 int basewidget::Times() const
       
   149 {
       
   150     return Edit2Int(mTimesEdid);
       
   151 }
       
   152 
       
   153 int basewidget::Items() const
       
   154 {
       
   155     return Edit2Int(mItemsEdid);
       
   156 }
       
   157 
       
   158 int basewidget::Edit2Int(const HbLineEdit* lineedit) const
       
   159 {
       
   160     int retvalue = 0;
       
   161     bool ok = false;
       
   162     QString edittext = lineedit->text();
       
   163     retvalue = edittext.toInt(&ok);
       
   164     if(ok) {
       
   165         return retvalue;
       
   166     }
       
   167     return 0;
       
   168 }
       
   169 
       
   170 void basewidget::prepareSave()
       
   171 {
       
   172     clearSaveData();
       
   173     int bytes = Bytes();    
       
   174     mByteArray = new QByteArray(bytes, 'a');
       
   175     
       
   176     HbMainWindow *mainWindow = hbInstance->allMainWindows().first();
       
   177     mPixmap = new QPixmap(QPixmap::grabWidget(mainWindow, mainWindow->rect()));
       
   178     
       
   179     mSaveVariant = new QVariant;
       
   180     *mSaveVariant = *mByteArray;
       
   181     delete mByteArray;
       
   182     mByteArray = NULL;
       
   183 
       
   184     
       
   185     mSaveMetadata = new QVariantHash;
       
   186     mSaveMetadata->insert(ActivityScreenshotKeyword, *mPixmap);
       
   187     delete mPixmap;
       
   188     mPixmap = NULL;
       
   189     
       
   190 }
       
   191 
       
   192 void basewidget::clearSaveData()
       
   193 {
       
   194     delete mByteArray;
       
   195     mByteArray = NULL;
       
   196     delete mPixmap;
       
   197     mPixmap = NULL;
       
   198     delete mSaveVariant;
       
   199     mSaveVariant = NULL;
       
   200     delete mSaveMetadata;
       
   201     mSaveMetadata = NULL;
       
   202 }
       
   203 
       
   204 bool basewidget::saveActivity(int name)
       
   205 {
       
   206     QString display;
       
   207     display.setNum(name);
       
   208     mSaveMetadata->insert(ActivityApplicationName, display);
       
   209     bool retok, ok;
       
   210     try {
       
   211         ok = QMetaObject::invokeMethod(mActivityStorage, "saveActivity", Q_RETURN_ARG(bool, retok), Q_ARG(QString, display), Q_ARG(QVariant, *mSaveVariant), Q_ARG(QVariantHash, *mSaveMetadata));
       
   212     }
       
   213     catch(...) {
       
   214         return false;
       
   215     }
       
   216 
       
   217     return ok && retok;
       
   218 }
       
   219 
       
   220 bool basewidget::removeActivity(const QString& name)
       
   221 {
       
   222     bool retok, ok;
       
   223     try {
       
   224         ok = QMetaObject::invokeMethod(mActivityStorage, "removeActivity", Q_RETURN_ARG(bool, retok), Q_ARG(QString, name));
       
   225     }
       
   226     catch(...) {
       
   227         return false;
       
   228     }
       
   229     return ok && retok;
       
   230 }
       
   231 
       
   232 bool basewidget::activities()
       
   233 {
       
   234     bool ok;
       
   235     try {
       
   236         ok = QMetaObject::invokeMethod(mActivityStorage, "allActivities", Q_RETURN_ARG(QStringList, mActivities));
       
   237     }
       
   238     catch(...) {
       
   239         return false;
       
   240     }
       
   241     return ok;   
       
   242 }
       
   243 
       
   244 bool basewidget::privateData(const QString& name)
       
   245 {
       
   246     QVariant data;
       
   247     bool ok;
       
   248     try {
       
   249         ok = QMetaObject::invokeMethod(mActivityStorage, "activityData", Q_RETURN_ARG(QVariant, data), Q_ARG(QString, name));
       
   250     }
       
   251     catch(...) {
       
   252         return false;
       
   253     }
       
   254     return ok;
       
   255 }
       
   256 
       
   257 bool basewidget::getThumbnail(const QString& name)
       
   258 {
       
   259     QVariantHash metadata;
       
   260     bool ok;
       
   261     try {
       
   262         ok = QMetaObject::invokeMethod(mActivityStorage, "activityMetaData", Q_RETURN_ARG(QVariantHash, metadata), Q_ARG(QString, name));
       
   263     }
       
   264     catch(...) {
       
   265         return false;
       
   266     }
       
   267     if(!ok) {
       
   268         return false;
       
   269     }
       
   270     QString pixfile = metadata.value(ActivityScreenshotKeyword).toString();
       
   271     ok = QMetaObject::invokeMethod(mActivityManager, "getThumbnail", Q_ARG(QSize, QSize(128, 128)), Q_ARG(QString, pixfile), Q_ARG(void*, NULL));
       
   272     return ok;
       
   273 }
       
   274 
       
   275 int basewidget::dbSize()
       
   276 {
       
   277     _LIT(KFilePath, "c:\\private\\200267b4\\activity.db"); 
       
   278     TInt fileSize = 0; 
       
   279     RFs fs;
       
   280     TInt err = fs.Connect();
       
   281     if (err == KErrNone) { 
       
   282         TEntry entry;
       
   283         err = fs.Entry(KFilePath, entry);
       
   284         if (err == KErrNone) {
       
   285             fileSize = entry.iSize;
       
   286         }
       
   287     }         
       
   288     fs.Close();
       
   289     return fileSize;
       
   290 }
       
   291 
       
   292 int basewidget::ClientHeapSize()
       
   293 {
       
   294     RHeap& heap = User::Heap();
       
   295     TInt all=0;
       
   296     heap.AllocSize(all);
       
   297     return all;
       
   298 }
       
   299 
       
   300 void basewidget::enableControls(bool enable)
       
   301 {
       
   302     mStartTestButton->setEnabled(enable);
       
   303     mChangeTestButton->setEnabled(enable);
       
   304     mItemsEdid->setEnabled(enable);
       
   305     mTimesEdid->setEnabled(enable);
       
   306     mKBytesEdid->setEnabled(enable);
       
   307     mTestCheckBox->setEnabled(enable);
       
   308 }
       
   309 
       
   310 void basewidget::timeout()
       
   311 {
       
   312     
       
   313 }
       
   314 
       
   315 void basewidget::logFirst()
       
   316 {
       
   317     int memclient = ClientHeapSize();
       
   318     int memdb = dbSize();
       
   319     mClientMemBefore = memclient;
       
   320     mDbMemBefore = memdb;
       
   321     
       
   322     QString clinetKB;
       
   323     clinetKB.setNum(memclient/1024.0, 'f', 2);
       
   324     mClientMemBeforeLabel->setPlainText(clinetKB);
       
   325     
       
   326     QString dbKB;
       
   327     dbKB.setNum(memdb/1024.0, 'f', 2);
       
   328     mDbMemBeforeLabel->setPlainText(dbKB);
       
   329     
       
   330     *mStream<<"Client heap first: ";
       
   331     *mStream<<memclient<<"\n";
       
   332     *mStream<<"Db size first: ";
       
   333     *mStream<<memdb<<"\n";
       
   334     mStream->flush();
       
   335 }
       
   336 
       
   337 void basewidget::logLast()
       
   338 {
       
   339     int memclient = ClientHeapSize();
       
   340     int memdb = dbSize();
       
   341     int clientdiff = memclient - mClientMemBefore;
       
   342     int dbdiff = memdb - mDbMemBefore;
       
   343     
       
   344     QString clinetKB;
       
   345     clinetKB.setNum(memclient/1024.0, 'f', 2);
       
   346     mClientMemAfterLabel->setPlainText(clinetKB);
       
   347     
       
   348     QString dbKB;
       
   349     dbKB.setNum(memdb/1024.0, 'f', 2);
       
   350     mDbMemAfterLabel->setPlainText(dbKB);
       
   351     
       
   352     QString clientDiffKb;
       
   353     clientDiffKb.setNum(clientdiff/1024.0, 'f', 2);
       
   354     mClientMemDiffLabel->setPlainText(clientDiffKb);
       
   355     
       
   356     
       
   357     QString dbDiffKb;
       
   358     dbDiffKb.setNum(dbdiff/1024.0, 'f', 2);
       
   359     mDbMemDiffLabel->setPlainText(dbDiffKb);
       
   360     
       
   361     *mStream<<"Client heap last: ";
       
   362     *mStream<<memclient<<"\n";
       
   363     *mStream<<"Db size last: ";
       
   364     *mStream<<memdb<<"\n";
       
   365     
       
   366     *mStream<<"Client heap difference: ";
       
   367     *mStream<<clientdiff<<"\n";
       
   368     *mStream<<"Db size defference: ";
       
   369     *mStream<<dbdiff<<"\n";
       
   370     mStream->flush();
       
   371 }
       
   372 
       
   373 void basewidget::logBaseMessage()
       
   374 {
       
   375     *mStream <<"action: ";
       
   376     *mStream<<mAction<<"\n";
       
   377     *mStream <<"itemCounter: ";
       
   378     *mStream<<mItemsCounter<<"\n";
       
   379     *mStream <<"TimeCounter: ";
       
   380     *mStream<<mTimesCounter<<"\n";
       
   381     mStream->flush();
       
   382 }
       
   383 
       
   384 void basewidget::logErrorMessage(const QString& mess)
       
   385 {
       
   386     QString messlog("*Fail: ");
       
   387     messlog += mess;
       
   388     messlog += "*\n";
       
   389     *mStream<<messlog;
       
   390     logBaseMessage();
       
   391 }
       
   392 
       
   393 void basewidget::updateItemLabel(int item)
       
   394 {
       
   395     QString num;
       
   396     num.setNum(item);
       
   397     mIterLabel->setPlainText(num);
       
   398 }
       
   399 
       
   400 void basewidget::updateTimeLabel(int time)
       
   401 {
       
   402     QString num;
       
   403     num.setNum(time);
       
   404     mTimesLabel->setPlainText(num);
       
   405     
       
   406 }
       
   407 
       
   408 void basewidget::updateActionLabel()
       
   409 {
       
   410     mActionLabel->setPlainText(mAction);
       
   411 }
       
   412 
       
   413 bool basewidget::checkActNum()
       
   414 {
       
   415     int getact = mActivities.count();
       
   416     int expectedact = Items();
       
   417     if (getact != expectedact) {
       
   418         *mStream<<"*Fail: Unexpected activities number got*\n";
       
   419         *mStream<<"Got: "<<getact<<", but expected: "<<expectedact<<"\n";
       
   420         return false;
       
   421     }
       
   422     return true;        
       
   423 }
       
   424 
       
   425 void basewidget::endTest(int reason)
       
   426 {
       
   427     mTimer->stop();
       
   428     if(reason == 0) {
       
   429        mStatusValueLabel->setPlainText("End"); 
       
   430     }
       
   431     else if(reason == 1) {
       
   432        mStatusValueLabel->setPlainText("Error"); 
       
   433       }
       
   434     else if(reason == 2) {
       
   435        mStatusValueLabel->setPlainText("Break"); 
       
   436       }
       
   437     emit testEnded(reason);  
       
   438 }
       
   439 
       
   440 void basewidget::releaseMem()
       
   441 {
       
   442     mActivities.clear();
       
   443     clearSaveData();
       
   444 }
       
   445 
       
   446 void basewidget::setError(const QString& mess)
       
   447 {
       
   448     releaseMem();
       
   449     logErrorMessage(mess);
       
   450     if( mAction != "predel" && mAction != "presave" ) {
       
   451         logLast();
       
   452     }
       
   453     endTest(1);
       
   454     enableControls(true);
       
   455 }
       
   456 
       
   457 void basewidget::endNormally()
       
   458 {
       
   459     releaseMem();
       
   460     logLast();    
       
   461     endTest(0);
       
   462     enableControls(true);
       
   463 }
       
   464 
       
   465 void basewidget::breakTest()
       
   466 {
       
   467     releaseMem();
       
   468     endTest(2);
       
   469     *mStream<<"Breaked\n";
       
   470     mStream->flush();
       
   471     enableControls(true);
       
   472 }
       
   473 
       
   474 void basewidget::logSettings()
       
   475 {
       
   476     *mStream <<"items: ";
       
   477     *mStream<<Items()<<"\n";
       
   478     *mStream <<"Times: ";
       
   479     *mStream<<Times()<<"\n";
       
   480     *mStream <<"Checkbox: ";
       
   481     *mStream<<TestCheckBoxChecked()<<"\n";
       
   482     mStream->flush();   
       
   483 }
       
   484 
       
   485 void basewidget::clearLabels()
       
   486 {
       
   487     mClientMemBeforeLabel->clear();
       
   488     mClientMemAfterLabel->clear();
       
   489     mClientMemDiffLabel->clear();
       
   490     mDbMemBeforeLabel->clear();
       
   491     mDbMemAfterLabel->clear();
       
   492     mDbMemDiffLabel->clear();    
       
   493     mIterLabel->clear();
       
   494     mTimesLabel->clear();    
       
   495     mStatusValueLabel->clear();
       
   496 }
       
   497 
       
   498 void basewidget::test()
       
   499 {
       
   500     updateActionLabel();
       
   501     clearLabels();
       
   502     mTimer->start(1);
       
   503 }