perfapps/perfmon/ui/hb/app/src/datapopup.cpp
changeset 48 da3ec8478e66
child 53 819e59dfc032
equal deleted inserted replaced
47:11fa016241a4 48:da3ec8478e66
       
     1 /*
       
     2 * Copyright (c) 2010 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 <HbView>
       
    19 #include <HbEvent>
       
    20 #include <QFontMetrics>
       
    21 
       
    22 #include "datapopup.h"
       
    23 #include "enginewrapper.h"
       
    24 #include "popupdatacontainer.h"
       
    25 
       
    26 DataPopup::DataPopup(EngineWrapper &engine) :
       
    27         mEngine(engine),
       
    28         mPopupCreated(false),
       
    29         mPopupVisible(false)
       
    30 {
       
    31     connect(&mEngine, SIGNAL(samplesUpdated()), this, SLOT(updateSamples()));
       
    32     connect(&mEngine, SIGNAL(settingsUpdated()), this, SLOT(updateSettings()));
       
    33     connect(this, SIGNAL(dataReceived(QVariantMap)), this, SLOT(triggerAction(QVariantMap)));
       
    34 }
       
    35 
       
    36 void DataPopup::show()
       
    37 {
       
    38     mPopupVisible = true;
       
    39     if (!mPopupCreated &&
       
    40         mEngine.sampleEntries().length() &&
       
    41         mEngine.settings().dataPopupSources().enabledIndexes().length())
       
    42     {
       
    43         bool anyData = false;
       
    44         foreach (QVariant index, mEngine.settings().dataPopupSources().enabledIndexes())
       
    45             anyData = anyData || mEngine.sampleEntries().at(index.toInt()).sampleCount();
       
    46 
       
    47         if (anyData) {
       
    48             mPopupCreated = HbDeviceDialog::show("com.nokia.rnd.perfmondatapopup/1.0",
       
    49                                                  collectParams());
       
    50         }
       
    51     }
       
    52 }
       
    53 
       
    54 void DataPopup::hide()
       
    55 {
       
    56     mPopupVisible = false;
       
    57     if (mPopupCreated) {
       
    58         mPopupCreated = !cancel();
       
    59     }
       
    60 }
       
    61 
       
    62 void DataPopup::updateSamples()
       
    63 {
       
    64     updateData();
       
    65 }
       
    66 
       
    67 void DataPopup::updateSettings()
       
    68 {
       
    69     // mEngine.settings().dataPopupVisibility can only be changed from
       
    70     // main window, so we may assume the window is in foreground
       
    71     updateVisibility(true);
       
    72     updateData();
       
    73 }
       
    74 
       
    75 void DataPopup::updateVisibility(bool foreground)
       
    76 {
       
    77     if (mEngine.settings().dataPopupVisibility() == EDataPopupVisbilityAlwaysOn ||
       
    78         (mEngine.settings().dataPopupVisibility() == EDataPopupVisbilityBackgroundOnly && !foreground)) {
       
    79 
       
    80         show();
       
    81     } else {
       
    82         hide();
       
    83     }
       
    84 }
       
    85 
       
    86 void DataPopup::triggerAction(QVariantMap data)
       
    87 {
       
    88     if (data.contains("mouseEvent") && data["mouseEvent"].toString() == "press") {
       
    89         emit clicked();
       
    90 
       
    91         // data popup was clicked, move it to other position
       
    92         mEngine.settings().setDataPopupLocation(
       
    93                 EDataPopupLocationBottomMiddle - mEngine.settings().dataPopupLocation());
       
    94         mEngine.updateSettings();
       
    95     }
       
    96 }
       
    97 
       
    98 QVariantMap DataPopup::collectParams() const
       
    99 {
       
   100     QVariantMap result;
       
   101 
       
   102     // add location param
       
   103     result["location"] = mEngine.settings().dataPopupLocation();
       
   104 
       
   105     // add lines param
       
   106     QStringList lines;
       
   107     QList<SampleEntry> entries = mEngine.sampleEntries();
       
   108     int posCounter(ESourceCPU);
       
   109 
       
   110     if (mEngine.settings().dataPopupSources().isEnabled(posCounter))
       
   111     {
       
   112         // loop all CPUs
       
   113         for (TInt i = mEngine.CPU0PositionInSamples(); i < mEngine.CPU0PositionInSamples() + mEngine.AmountOfCPUs(); i++)
       
   114         {
       
   115             // check samples available
       
   116             if (entries.at(i).sampleCount() > 0)
       
   117             {
       
   118                 const SampleEntry &entry = entries.at(i);
       
   119                 const SampleData &sample = entry.sample(0);
       
   120 
       
   121                 double perc = sample.mSize > 0 ?
       
   122                               100. - 100. * sample.mFree / sample.mSize : 0;
       
   123 
       
   124                 QString text = tr("%1 %2%").arg(entry.description()).
       
   125                                arg(perc, 0, 'f', 0);
       
   126                 lines.append(text);
       
   127             }
       
   128         }
       
   129     }
       
   130 
       
   131     posCounter++;
       
   132 
       
   133     // draw RAM and Drive values
       
   134     for (TInt i = mEngine.RAMPositionInSamples(); i < mEngine.PowerPositionInSamples(); i++)
       
   135     {
       
   136         // check if this setting has been enabled and it has some data
       
   137         if (mEngine.settings().dataPopupSources().isEnabled(posCounter) && entries.at(i).sampleCount() > 0)
       
   138         {
       
   139             const SampleEntry &entry = entries.at(i);
       
   140             const SampleData &sample = entry.sample(0);
       
   141 
       
   142             QString text = tr("%1 free %L2%3").arg(entry.description()).
       
   143                            arg(sample.mFree).arg(entry.unitShort());
       
   144             lines.append(text);
       
   145         }
       
   146 
       
   147         posCounter++;
       
   148     }
       
   149 
       
   150     // draw power value
       
   151     // check if this setting has been enabled and it has some data
       
   152     if (mEngine.settings().dataPopupSources().isEnabled(posCounter) && entries.at(mEngine.PowerPositionInSamples()).sampleCount() > 0)
       
   153     {
       
   154         const SampleEntry &entry = entries.at(mEngine.PowerPositionInSamples());
       
   155         const SampleData &sample = entry.sample(0);
       
   156 
       
   157         QString text = tr("%1 %L2%3").arg(entry.description()).
       
   158                        arg(sample.mSize - sample.mFree).arg(entry.unitShort());
       
   159         lines.append(text);
       
   160     }
       
   161 
       
   162     result["lines"] = lines;
       
   163 
       
   164     return result;
       
   165 }
       
   166 
       
   167 void DataPopup::updateData()
       
   168 {
       
   169     if (!mPopupVisible)
       
   170         return;
       
   171 
       
   172     if (!mPopupCreated) {
       
   173         show();
       
   174     } else {
       
   175         HbDeviceDialog::update(collectParams());
       
   176     }
       
   177 }