perfmon/ui/hb/app/src/graphdatacontainer.cpp
branchRCL_3
changeset 22 fad26422216a
parent 21 b3cee849fa46
child 23 f8280f3bfeb7
equal deleted inserted replaced
21:b3cee849fa46 22:fad26422216a
     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 "graphdatacontainer.h"
       
    19 #include "enginewrapper.h"
       
    20 
       
    21 #include <QPainter>
       
    22 #include <QFontMetrics>
       
    23 
       
    24 const int amountOfMicroSecondsFitsScreen = 20 * 1000000;
       
    25 const int microToSecondMultiplier = 1000000;
       
    26 
       
    27 GraphDataContainer::GraphDataContainer(const EngineWrapper& engine, QGraphicsItem *parent) :
       
    28     DataContainer(engine, parent)
       
    29 {
       
    30     HbFontSpec spec(HbFontSpec::Secondary);
       
    31     mFont = spec.font();
       
    32     mFont.setPixelSize(12);
       
    33 }
       
    34 
       
    35 void GraphDataContainer::paint (QPainter *painter,
       
    36     const QStyleOptionGraphicsItem *option,
       
    37     QWidget *widget)
       
    38 {
       
    39     Q_UNUSED(option);
       
    40     Q_UNUSED(widget);
       
    41 
       
    42     // set proper font and prepare font metrics for text width / height calculation
       
    43     painter->setFont(mFont);
       
    44     QFontMetricsF metrics(mFont);
       
    45 
       
    46     // fill background with black color
       
    47     painter->fillRect(rect(), Qt::black);
       
    48 
       
    49     // calculate time factor
       
    50     qreal scaleFactor = rect().width() / (qreal) amountOfMicroSecondsFitsScreen;
       
    51 
       
    52     // calculate area height which is used to draw the grpahs
       
    53     qreal drawAreaHeight = rect().height() - 2 * metrics.height();
       
    54 
       
    55 
       
    56     const QList<SampleEntry> sampleEntries = engine().sampleEntries();
       
    57 
       
    58     // check if sample array has been constructed
       
    59     if (sampleEntries.length())
       
    60     {
       
    61         // draw vertical time lines first
       
    62         int verticalBarPeriodInSecs = engine().settings().graphVerticalBarPeriod();
       
    63 
       
    64         if (verticalBarPeriodInSecs >= 1 && sampleEntries.first().sampleCount() > 0)
       
    65         {
       
    66             // get time from the first sample
       
    67             const SampleData& firstSample = sampleEntries.first().sample(0);
       
    68             qint64 currentMicroSeconds = firstSample.mTimeFromStart;
       
    69 
       
    70             // calculate amount of microseconds exceeding value by using the modulo operator
       
    71             int remainderInMicroSeconds = currentMicroSeconds % (verticalBarPeriodInSecs * 1000000);
       
    72 
       
    73             // calculate first x pos
       
    74             qreal vbarXpos = rect().width() - (remainderInMicroSeconds * scaleFactor);
       
    75 
       
    76             // calculate the amount in seconds
       
    77             int barSeconds = (currentMicroSeconds - remainderInMicroSeconds) / microToSecondMultiplier;
       
    78 
       
    79             // continue drawing periodically the vertical lines
       
    80             while (vbarXpos >= 0 && barSeconds >= 0)
       
    81             {
       
    82                 // draw vertical line
       
    83                 painter->setPen(Qt::darkRed);
       
    84                 painter->drawLine(QPointF(vbarXpos, metrics.height() + 1),
       
    85                                   QPointF(vbarXpos, rect().height() - metrics.height()));
       
    86 
       
    87                 // draw seconds value
       
    88                 painter->setPen(Qt::darkGray);
       
    89                 QString secsText = QString("%1s").arg(barSeconds);
       
    90                 QPointF secsPos(vbarXpos - metrics.width(secsText) / 2,
       
    91                                 rect().height());
       
    92                 painter->drawText(secsPos, secsText);
       
    93 
       
    94                 // calculate new position
       
    95                 vbarXpos -= verticalBarPeriodInSecs * 1000000 * scaleFactor;
       
    96                 barSeconds -= verticalBarPeriodInSecs;
       
    97             }
       
    98         }
       
    99 
       
   100         // draw the basic grid
       
   101         painter->setPen(Qt::darkGray);
       
   102 
       
   103         qreal axisY = metrics.height();
       
   104         painter->drawLine(QPointF(0, axisY), QPointF(rect().width(), axisY)); // upper line
       
   105         painter->drawText(QPointF(0, axisY), tr("100%"));
       
   106 
       
   107         axisY = rect().height() / 2;
       
   108         painter->drawLine(QPointF(0, axisY), QPointF(rect().width(), axisY)); // mid line
       
   109         painter->drawText(QPointF(0, axisY), tr("50%"));
       
   110 
       
   111         axisY = rect().height() - metrics.height();
       
   112         painter->drawLine(QPointF(0, axisY), QPointF(rect().width(), axisY)); // bottom line
       
   113         painter->drawText(QPointF(0, axisY), tr("0%"));
       
   114 
       
   115         int c = 0;
       
   116         // draw graphs for each sampled type
       
   117         for (int i=0; i<sampleEntries.length(); i++)
       
   118         {
       
   119             // check if this setting has been enabled and it has some data
       
   120             if (engine().settings().graphSources().isEnabled(i) &&
       
   121                 sampleEntries.at(i).sampleCount() > 0)
       
   122             {
       
   123                 // set pen color for the graph
       
   124                 painter->setPen(sampleEntries.at(i).graphColor());
       
   125 
       
   126                 // remember the position where drawing started
       
   127                 qreal currentXPos(rect().width()); // start drawing from right
       
   128                 qreal currentYPos(0.);
       
   129 
       
   130                 // draw samples
       
   131                 for (int j=0; j<sampleEntries.at(i).sampleCount()-1; j++)
       
   132                 {
       
   133                     const SampleData& currentSample = sampleEntries.at(i).sample(j);
       
   134                     const SampleData& previousSample = sampleEntries.at(i).sample(j+1);
       
   135 
       
   136                     // calculate X position for previous (j+1)
       
   137                     qreal previousXPos = currentXPos -
       
   138                         qAbs(previousSample.mTimeFromStart - currentSample.mTimeFromStart) * scaleFactor;
       
   139 
       
   140 
       
   141                     // calculate initial Y position
       
   142                     if (j==0)
       
   143                     {
       
   144                         currentYPos = currentSample.mSize > 0 ?
       
   145                             drawAreaHeight * currentSample.mFree / currentSample.mSize + metrics.height() :
       
   146                             rect().height() - metrics.height();
       
   147                     }
       
   148 
       
   149                     // calculate Y position for previous (j+1)
       
   150                     qreal previousYPos = previousSample.mSize > 0 ?
       
   151                         drawAreaHeight * previousSample.mFree / previousSample.mSize + metrics.height() :
       
   152                         rect().height() - metrics.height();
       
   153 
       
   154 
       
   155                     // draw a line between the previous and current
       
   156                     painter->drawLine(QPointF(previousXPos, previousYPos),
       
   157                                       QPointF(currentXPos, currentYPos));
       
   158 
       
   159                     // draw current value in %
       
   160                     if (j==0) // draw the value of first sample
       
   161                     {
       
   162                         qreal perc = currentSample.mSize > 0 ?
       
   163                                      100. - 100. * currentSample.mFree / currentSample.mSize : 0;
       
   164                         QString percText = QString ("%1 %2%").
       
   165                                            arg(sampleEntries.at(i).description()).
       
   166                                            arg(perc, 0, 'f', 0);
       
   167 
       
   168                         painter->drawText(QPointF(0, metrics.height() * (c + 2)),
       
   169                                           percText);
       
   170                         c++;
       
   171                     }
       
   172 
       
   173 
       
   174                     // stop drawing if we have run out of space
       
   175                     if (previousXPos < 0)
       
   176                         break;
       
   177 
       
   178                     // remeber previous values
       
   179                     currentXPos = previousXPos;
       
   180                     currentYPos = previousYPos;
       
   181                 }
       
   182 
       
   183 
       
   184             }
       
   185         }
       
   186 
       
   187     }
       
   188 }