activityfw/testapplications/nftapps/lowmemoryapp/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 <QImage>
       
    27 #include <QColor>
       
    28 #include <HbInstance>
       
    29 #include <f32file.h>
       
    30 #include <qglobal.h>
       
    31 
       
    32 const int KDataDataSize = 2*1024*1024; // 2 MB
       
    33 const int KThumbDataSize = 10; // 10 B
       
    34 const int KMaxHeapMem = 16*1024*1024; // 16 MB
       
    35 
       
    36 
       
    37 basewidget::basewidget(QObject* activityStorage, QObject* activityManager, QTextStream* stream, QGraphicsItem *parent)
       
    38     : HbWidget(parent), mActivityStorage(activityStorage), mActivityManager(activityManager), mStream(stream),
       
    39       mPixmap(NULL),mByteArray(NULL),
       
    40       mSaveVariant(NULL), mSaveMetadata(NULL),
       
    41       mAlloc(0)
       
    42 {
       
    43 
       
    44     
       
    45     mSaveButton = new HbPushButton("Save");
       
    46     mThumbButton = new HbPushButton("Thumb");
       
    47     mDataButton = new HbPushButton("Data");
       
    48     
       
    49             
       
    50     mKBytesEdid = new HbLineEdit("1024");
       
    51     mKBytesEdid->setObjectName("KBytesEditLine");     
       
    52     mQKBytesValidator = new QIntValidator(this); 
       
    53     mQKBytesValidator->setRange(40, KMaxHeapMem);
       
    54     mKBytesValidator = new HbValidator(this);
       
    55     mKBytesValidator->addField(mQKBytesValidator, "3000");
       
    56     mKBytesEdid->setValidator(mKBytesValidator);
       
    57 
       
    58     
       
    59     mStatusValueLabel = new HbLabel("None status, none message");    
       
    60     mBytesEditLabel = new HbLabel("Free KBytes[KB]");
       
    61     mCatchLabel = new HbLabel("Catches");
       
    62         
       
    63     
       
    64     mGridLayout = new QGraphicsGridLayout();    
       
    65     mGridLayout->addItem(mStatusValueLabel, 0, 0, 1, 6);
       
    66     mGridLayout->addItem(mCatchLabel, 1, 0, 1, 6);
       
    67     mGridLayout->addItem(mBytesEditLabel, 2, 0, 1, 2);
       
    68     mGridLayout->addItem(mKBytesEdid, 2, 2, 1, 4);
       
    69     mGridLayout->addItem(mSaveButton, 3, 0, 1, 2);
       
    70     mGridLayout->addItem(mDataButton, 3, 2, 1, 2);
       
    71     mGridLayout->addItem(mThumbButton, 3, 4, 1, 2);            
       
    72     setLayout(mGridLayout);
       
    73         
       
    74     
       
    75     connect(mSaveButton, SIGNAL(released()), this, SLOT(save()));
       
    76     connect(mThumbButton, SIGNAL(released()), this, SLOT(thumb()));    
       
    77     connect(mDataButton, SIGNAL(released()), this, SLOT(data()));
       
    78 }
       
    79 
       
    80 basewidget::~basewidget()
       
    81 {
       
    82     delete mQKBytesValidator;
       
    83     delete mKBytesValidator;
       
    84 }
       
    85 
       
    86 
       
    87 
       
    88 
       
    89 int basewidget::Bytes() const
       
    90 {
       
    91    int kb = Edit2Int(mKBytesEdid);
       
    92    return kb*1024;
       
    93 }
       
    94 
       
    95 int basewidget::Edit2Int(const HbLineEdit* lineedit) const
       
    96 {
       
    97     int retvalue = 0;
       
    98     bool ok = false;
       
    99     QString edittext = lineedit->text();
       
   100     retvalue = edittext.toInt(&ok);
       
   101     if(ok) {
       
   102         return retvalue;
       
   103     }
       
   104     return 0;
       
   105 }
       
   106 
       
   107 void basewidget::prepareDataSave()
       
   108 {
       
   109     clearSaveData();
       
   110     int bytes = KDataDataSize;    
       
   111     mByteArray = new QByteArray(bytes, 'a');
       
   112     
       
   113     HbMainWindow *mainWindow = hbInstance->allMainWindows().first();
       
   114     mPixmap = new QPixmap(QPixmap::grabWidget(mainWindow, mainWindow->rect()));
       
   115     
       
   116     mSaveVariant = new QVariant;
       
   117     *mSaveVariant = *mByteArray;
       
   118     delete mByteArray;
       
   119     mByteArray = NULL;
       
   120 
       
   121     
       
   122     mSaveMetadata = new QVariantHash;
       
   123     mSaveMetadata->insert(ActivityScreenshotKeyword, *mPixmap);
       
   124     delete mPixmap;
       
   125     mPixmap = NULL;    
       
   126 }
       
   127 
       
   128 void basewidget::prepareThumbSave()
       
   129 {
       
   130     clearSaveData();
       
   131     int bytes = KThumbDataSize;    
       
   132     mByteArray = new QByteArray(bytes, 'a');
       
   133     
       
   134     QImage img = QImage(1000, 1000, QImage::Format_ARGB32);
       
   135     img.fill(1);
       
   136     for( int i=0; i<img.width(); i++) {
       
   137         for(int j=0; j<img.height(); j++) {        
       
   138             int index = i*j;
       
   139             img.setPixel(i, j, qRgba((10*index+104*i)%255, (200*index+6*j)%255, (150*index+34*i)%255, (80*index+40*j)%255));
       
   140         }
       
   141     }
       
   142     mPixmap = new QPixmap(QPixmap::fromImage(img));
       
   143     bool ok = mPixmap->save("c:\\ala.bmp");
       
   144     
       
   145     mSaveVariant = new QVariant;
       
   146     *mSaveVariant = *mByteArray;
       
   147     delete mByteArray;
       
   148     mByteArray = NULL;
       
   149 
       
   150     
       
   151     mSaveMetadata = new QVariantHash;
       
   152     mSaveMetadata->insert(ActivityScreenshotKeyword, *mPixmap);
       
   153     delete mPixmap;
       
   154     mPixmap = NULL;       
       
   155 }
       
   156 
       
   157 void basewidget::clearSaveData()
       
   158 {
       
   159     delete mByteArray;
       
   160     mByteArray = NULL;
       
   161     delete mPixmap;
       
   162     mPixmap = NULL;
       
   163     delete mSaveVariant;
       
   164     mSaveVariant = NULL;
       
   165     delete mSaveMetadata;
       
   166     mSaveMetadata = NULL;
       
   167 }
       
   168 
       
   169 bool basewidget::saveActivity(int name)
       
   170 {
       
   171     QString display;
       
   172     display.setNum(name);
       
   173     mSaveMetadata->insert(ActivityApplicationName, display);
       
   174     bool retok, ok;
       
   175     try {
       
   176         ok = QMetaObject::invokeMethod(mActivityStorage, "saveActivity", Q_RETURN_ARG(bool, retok), Q_ARG(QString, display), Q_ARG(QVariant, *mSaveVariant), Q_ARG(QVariantHash, *mSaveMetadata));
       
   177     }
       
   178     catch(...) {
       
   179         logCatchError("save");
       
   180         return false;
       
   181     }
       
   182 
       
   183     return ok && retok;
       
   184 }
       
   185 
       
   186 bool basewidget::removeActivity(const QString& name)
       
   187 {
       
   188     bool retok, ok;
       
   189     try {
       
   190         ok = QMetaObject::invokeMethod(mActivityStorage, "removeActivity", Q_RETURN_ARG(bool, retok), Q_ARG(QString, name));
       
   191     }
       
   192     catch(...) {
       
   193         logCatchError("remove");
       
   194         return false;
       
   195     }
       
   196     return ok && retok;
       
   197 }
       
   198 
       
   199 bool basewidget::activities()
       
   200 {
       
   201     bool ok;
       
   202     try {
       
   203         ok = QMetaObject::invokeMethod(mActivityStorage, "allActivities", Q_RETURN_ARG(QStringList, mActivities));
       
   204     }
       
   205     catch(...) {
       
   206         logCatchError("activities");
       
   207         return false;
       
   208     }
       
   209     return ok;   
       
   210 }   
       
   211 
       
   212 bool basewidget::privateData(const QString& name)
       
   213 {
       
   214     QVariant data;
       
   215     bool ok;
       
   216     try {
       
   217         ok = QMetaObject::invokeMethod(mActivityStorage, "activityData", Q_RETURN_ARG(QVariant, data), Q_ARG(QString, name));
       
   218     }
       
   219     catch(...) {
       
   220         logCatchError("data");
       
   221         return false;
       
   222     }
       
   223     const char* type = data.typeName();
       
   224     QString dtype(type);    
       
   225     logDataType(dtype);
       
   226     return ok;
       
   227 }
       
   228 
       
   229 bool basewidget::getThumbnail(const QString& name)
       
   230 {
       
   231     QVariantHash metadata;
       
   232     bool ok;
       
   233     try {
       
   234         ok = QMetaObject::invokeMethod(mActivityStorage, "activityMetaData", Q_RETURN_ARG(QVariantHash, metadata), Q_ARG(QString, name));
       
   235     }
       
   236     catch(...) {
       
   237         logCatchError("thumbnail");
       
   238         return false;
       
   239     }
       
   240     if(!ok) {
       
   241         return false;
       
   242     }
       
   243     QString pixfile = metadata.value(ActivityScreenshotKeyword).toString();
       
   244     ok = QMetaObject::invokeMethod(mActivityManager, "getThumbnail", Q_ARG(QSize, QSize(1000, 1000)), Q_ARG(QString, pixfile), Q_ARG(void*, NULL));
       
   245     return ok;
       
   246 }
       
   247 
       
   248 
       
   249 
       
   250 int basewidget::ClientHeapSize()
       
   251 {
       
   252     RHeap& heap = User::Heap();
       
   253     TInt all=0;
       
   254     heap.AllocSize(all);
       
   255     return all;
       
   256 }
       
   257 
       
   258 
       
   259 
       
   260 
       
   261 void basewidget::logErrorMessage(const QString& mess)
       
   262 {
       
   263     QString messlog("*Fail: ");
       
   264     messlog += mess;
       
   265     messlog += "*\n";
       
   266     *mStream<<messlog;
       
   267     mStream->flush();
       
   268     mStatusValueLabel->setPlainText("Fail");
       
   269 }
       
   270 
       
   271 
       
   272 
       
   273 void basewidget::releaseMem()
       
   274 {
       
   275     mActivities.clear();
       
   276     clearSaveData();
       
   277     releaseAlloced();
       
   278 }
       
   279 
       
   280 
       
   281 void basewidget::save()
       
   282 {
       
   283     releaseMem();
       
   284     bool ok = removeActivity("0");
       
   285     logAction("save");    
       
   286     prepareDataSave();
       
   287     alloc();
       
   288     ok = saveActivity(0);
       
   289     releaseMem();
       
   290     logSaveResult(ok);
       
   291 }
       
   292 
       
   293 void basewidget::thumb()
       
   294 {
       
   295     releaseMem();
       
   296     disconnect( mActivityManager, SIGNAL(thumbnailReady(QPixmap, void *)), this, SLOT(thumbnailReady(QPixmap, void *)) );
       
   297     bool ok = removeActivity("0");
       
   298     logAction("thumb");    
       
   299     prepareThumbSave();
       
   300     ok = saveActivity(0);
       
   301     if(!ok) {
       
   302         logErrorMessage("save");
       
   303         return;
       
   304     }    
       
   305     clearSaveData();
       
   306     ok = activities();
       
   307     if(!ok || mActivities.count()==0) {
       
   308         logErrorMessage("activities");
       
   309         return;
       
   310     }    
       
   311     QString name = mActivities.at(0);
       
   312     alloc();
       
   313     ok = getThumbnail(name);
       
   314     if(!ok) {
       
   315         logErrorMessage("getthumb");
       
   316         return;
       
   317     }  
       
   318     connect( mActivityManager, SIGNAL(thumbnailReady(QPixmap, void *)), this, SLOT(thumbnailReady(QPixmap, void *)) );
       
   319     
       
   320 }
       
   321 
       
   322 void basewidget::data()
       
   323 {
       
   324     releaseMem();
       
   325     bool ok = removeActivity("0");
       
   326     logAction("data");    
       
   327     prepareDataSave();    
       
   328     ok = saveActivity(0);
       
   329     if(!ok) {
       
   330         logErrorMessage("save");
       
   331         return;
       
   332     }    
       
   333     ok = activities();
       
   334     if(!ok || mActivities.count()==0) {
       
   335         logErrorMessage("activities");
       
   336         return;
       
   337     }    
       
   338     QString name = mActivities.at(0);    
       
   339     alloc();
       
   340     ok = privateData(name);
       
   341     if(!ok) {
       
   342         logErrorMessage("data");
       
   343         return;
       
   344     }  
       
   345     releaseMem();    
       
   346 }
       
   347 
       
   348 void basewidget::thumbnailReady(QPixmap pixmap, void* ptr)
       
   349 {
       
   350     releaseMem();
       
   351     disconnect( mActivityManager, SIGNAL(thumbnailReady(QPixmap, void *)), this, SLOT(thumbnailReady(QPixmap, void *)) );
       
   352     logPixmapSize(pixmap);
       
   353 }
       
   354 
       
   355 void basewidget::logCatchError(const QString& mess)
       
   356 {
       
   357     QString msg = tr("Catch err: %1").arg(mess);
       
   358     mCatchLabel->setPlainText(msg);
       
   359     *mStream<<msg<<"\n";
       
   360     mStream->flush();
       
   361 }
       
   362 
       
   363 void basewidget::logDataType(const QString& type)
       
   364 {
       
   365     QString msg = tr("Data type: %1").arg(type);
       
   366     mStatusValueLabel->setPlainText(msg);
       
   367     *mStream<<msg<<"\n";
       
   368     mStream->flush();    
       
   369 }
       
   370 
       
   371 void basewidget::logPixmapSize(const QPixmap& pix)
       
   372 {
       
   373     QSize s = pix.size();
       
   374     QString msg = tr("Pix size: %1x%2").arg(s.width()).arg(s.height());
       
   375     mStatusValueLabel->setPlainText(msg);
       
   376     *mStream<<msg<<"\n";
       
   377     mStream->flush();    
       
   378 }
       
   379 
       
   380 void basewidget::logAction(const QString& mess)
       
   381 {
       
   382     mStatusValueLabel->clear();    
       
   383     mCatchLabel->clear();
       
   384     QString msg = tr("Action: %1").arg(mess);    
       
   385     *mStream<<msg<<"\n";
       
   386     QString bytemsg = tr("Free Bytes: %1").arg(Bytes());
       
   387     *mStream<<bytemsg<<"\n";
       
   388     mStream->flush();
       
   389 }
       
   390 
       
   391 void basewidget::logSaveResult(bool res)
       
   392 {
       
   393     QString msg = tr("Save: %1").arg(res);
       
   394     mStatusValueLabel->setPlainText(msg);
       
   395     *mStream<<msg<<"\n";
       
   396     mStream->flush();
       
   397 }
       
   398 
       
   399 bool basewidget::alloc()
       
   400 {
       
   401     releaseAlloced();
       
   402     int tobefreebytes = Bytes();    
       
   403     int all = ClientHeapSize();
       
   404     
       
   405     int nowfree = KMaxHeapMem - all;
       
   406     int toalloc = nowfree - tobefreebytes;
       
   407     
       
   408     bool ok = false;
       
   409     if( toalloc>=0 ) {
       
   410         ok = true;
       
   411         try {
       
   412             mAlloc = new char[toalloc];
       
   413         }
       
   414         catch(...) {
       
   415             ok = false;
       
   416         }
       
   417     }
       
   418     if(!mAlloc) {
       
   419         ok = false;
       
   420     }
       
   421     
       
   422     if(!ok) {
       
   423         QString msg = tr("Alloc Failed");
       
   424         mCatchLabel->setPlainText(msg);
       
   425         *mStream<<msg<<"\n";
       
   426         mStream->flush();
       
   427     }
       
   428     return ok;
       
   429 }
       
   430 
       
   431 void basewidget::releaseAlloced()
       
   432 {
       
   433     delete mAlloc;
       
   434     mAlloc = NULL;
       
   435 }