taskswitcher/testapplications/tstestpluginmanager/fakeplugin/src/tstestmodel.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 
       
    18 #include "tstestmodel.h"
       
    19 
       
    20 #include <QPainter>
       
    21 #include <QDateTime>
       
    22 #include <QDebug>
       
    23 #include <QTimer>
       
    24 #include "tstestpropertydefs.h"
       
    25 
       
    26 TsTestModel::TsTestModel(QObject *parent) : QObject(parent), mWasValid(false) 
       
    27 {
       
    28     verifyConnection();
       
    29 }
       
    30 
       
    31 TsTestModel::~TsTestModel()
       
    32 {
       
    33     qDeleteAll(mBitmaps);
       
    34 
       
    35 }
       
    36 
       
    37 QList<QVariantHash> TsTestModel::taskList() const
       
    38 {
       
    39     return mData;
       
    40 }
       
    41 
       
    42 QList<QVariantHash> TsTestModel::taskList(int limit) const
       
    43 {
       
    44     qDebug()<<"task list";
       
    45     return mData.mid(0, limit);
       
    46 }
       
    47     
       
    48 bool TsTestModel::openTask(const QVariant &id)
       
    49 {
       
    50     for (QList<QVariantHash>::iterator i = mData.begin(); i != mData.end(); ++i) {
       
    51         if (i->value("TaskId") == id) {
       
    52             if (!i->value("TaskIsRunning").toBool()) {
       
    53                 i->insert("TaskIsRunning", true);
       
    54                 i->insert("TaskCanBeClosed", true);
       
    55             }
       
    56             i->insert("TaskTimestamp", QDateTime::currentDateTime());
       
    57             emit dataChanged();
       
    58             return true;
       
    59         }
       
    60     }
       
    61     return false;
       
    62 }
       
    63 
       
    64 bool TsTestModel::closeTask(const QVariant &id)
       
    65 {
       
    66     
       
    67     for(int i=0; i<mData.count(); i++) {
       
    68         QVariantHash vh = mData.at(i);
       
    69         if(vh.value("TaskId") == id) {
       
    70             if (vh.value("TaskCanBeClosed").toBool()) {
       
    71                 mData.removeAt(i);
       
    72                 if (mBitmaps.count()>i) {
       
    73                     delete mBitmaps.at(i);
       
    74                     mBitmaps.removeAt(i);
       
    75                 }
       
    76             }
       
    77             emit dataChanged();
       
    78             return true;
       
    79         }        
       
    80     }        
       
    81     return false;
       
    82 }
       
    83 
       
    84 void TsTestModel::checkValue()
       
    85 {
       
    86     bool ok;
       
    87     int value = mSubscriber.value().toInt(&ok);
       
    88     if (!ok) {
       
    89         return;
       
    90     }
       
    91     if(value == 0) {
       
    92         return;
       
    93     }
       
    94     if(value == -1) {
       
    95         removeAll();
       
    96         return;
       
    97     }
       
    98 
       
    99     int itemnum = byte(value, 0);
       
   100     if(itemnum == 0) {
       
   101         return;
       
   102     }
       
   103     bool running = byte(value, 1);
       
   104     bool closeable = byte(value, 2);    
       
   105     addItems(itemnum, running, closeable);
       
   106     
       
   107 }
       
   108 
       
   109 void TsTestModel::verifyConnection()
       
   110 {
       
   111     mSubscriber.setPath(QString("%1/%2").arg(TsTestProperty::KTsTestPath).arg(TsTestProperty::KPluginPath));
       
   112     
       
   113     if (mSubscriber.value().isValid()) {
       
   114         if(mWasValid) {
       
   115             QTimer::singleShot(20000, this, SLOT(verifyConnection()));
       
   116             return;
       
   117         }
       
   118         mWasValid = true;
       
   119         connect(&mSubscriber, SIGNAL(contentsChanged()), this, SLOT(checkValue()));
       
   120         // might have missed first value change
       
   121         checkValue();
       
   122     } else {
       
   123         mWasValid = false;
       
   124         QTimer::singleShot(3000, this, SLOT(verifyConnection()));
       
   125     }
       
   126 }
       
   127 
       
   128 void TsTestModel::removeAll() 
       
   129 {
       
   130     if(mData.count()>0) {
       
   131         mData.clear();
       
   132         qDeleteAll(mBitmaps);
       
   133         mBitmaps.clear();
       
   134         emit dataChanged();
       
   135     }
       
   136 }
       
   137 
       
   138 char TsTestModel::byte(int numtoget, int byte)
       
   139 {
       
   140     char ret = 0;
       
   141     if(byte>3 || byte<0) {
       
   142         return ret;
       
   143     }
       
   144     numtoget = numtoget>>8*byte;
       
   145     ret = static_cast<char>(numtoget);
       
   146     
       
   147     return ret;
       
   148     
       
   149 }
       
   150 
       
   151 QColor TsTestModel::generateColor(int item)
       
   152 {
       
   153     int r = item*230%255;
       
   154     int g = item*80%255;
       
   155     int b = item*150%255;
       
   156     
       
   157     QColor c(r, g, b);
       
   158     return c;
       
   159 }
       
   160 
       
   161 QPixmap TsTestModel::generatePixmap(int item)
       
   162 {
       
   163     QColor color = generateColor(item);
       
   164     QPixmap pixmap(128, 128);
       
   165     pixmap.fill(Qt::transparent);
       
   166     {
       
   167         QPainter painter(&pixmap);
       
   168         painter.setBrush(Qt::white);
       
   169         painter.setPen(QPen(color, 2));
       
   170         QFont font;
       
   171         font.setPointSize(8);            
       
   172         painter.setFont(font);
       
   173         QString note;
       
   174         note = tr("Task %1").arg(item);
       
   175         painter.drawText(10, 80, note);
       
   176     }
       
   177     return pixmap;
       
   178 }
       
   179 
       
   180 void TsTestModel::addItem(bool running, bool closeable)
       
   181 {
       
   182     QVariantHash entry;
       
   183     int num = mData.count() + 1;
       
   184     QPixmap pixmap = generatePixmap(num);
       
   185     CFbsBitmap *bitmap = pixmap.toSymbianCFbsBitmap();
       
   186     mBitmaps.append(bitmap);
       
   187     entry.insert("TaskScreenshot", bitmap->Handle());
       
   188     entry.insert("TaskIsRunning", running);
       
   189     entry.insert("TaskCanBeClosed", closeable);
       
   190     
       
   191     entry.insert("TaskId", num);
       
   192     QString name;
       
   193     name = tr("Task %1").arg(num);
       
   194     entry.insert("TaskName", name);
       
   195     
       
   196     entry.insert("TaskTimestamp", QDateTime::currentDateTime());
       
   197     entry.insert("TaskUid", QString("EB002E%1").arg(num, 2, 10, QChar('0')).toUInt(0, 16));
       
   198 
       
   199     mData.append(entry);
       
   200 }
       
   201 
       
   202 void TsTestModel::addItems(int num, bool running, bool closeable)
       
   203 {
       
   204     for(int i=0; i<num; i++) {
       
   205         addItem(running, closeable);
       
   206     }
       
   207 }