ginebra2/ProgressBarItem.cpp
author hgs
Fri, 06 Aug 2010 17:23:08 -0400
changeset 9 b39122337a00
parent 3 0954f5dd2cd0
permissions -rw-r--r--
201031
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3
hgs
parents:
diff changeset
     1
/*
hgs
parents:
diff changeset
     2
* Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
hgs
parents:
diff changeset
     3
* All rights reserved.
hgs
parents:
diff changeset
     4
*
hgs
parents:
diff changeset
     5
* This program is free software: you can redistribute it and/or modify
hgs
parents:
diff changeset
     6
* it under the terms of the GNU Lesser General Public License as published by
hgs
parents:
diff changeset
     7
* the Free Software Foundation, version 2.1 of the License.
hgs
parents:
diff changeset
     8
*
hgs
parents:
diff changeset
     9
* This program is distributed in the hope that it will be useful,
hgs
parents:
diff changeset
    10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
hgs
parents:
diff changeset
    11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
hgs
parents:
diff changeset
    12
* GNU Lesser General Public License for more details.
hgs
parents:
diff changeset
    13
*
hgs
parents:
diff changeset
    14
* You should have received a copy of the GNU Lesser General Public License
hgs
parents:
diff changeset
    15
* along with this program.  If not,
hgs
parents:
diff changeset
    16
* see "http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html/".
hgs
parents:
diff changeset
    17
*
hgs
parents:
diff changeset
    18
* Description:
hgs
parents:
diff changeset
    19
*
hgs
parents:
diff changeset
    20
*/
hgs
parents:
diff changeset
    21
#include <QtGui>
hgs
parents:
diff changeset
    22
#include "ProgressBarItem.h"
hgs
parents:
diff changeset
    23
#include "GWebContentView.h"
hgs
parents:
diff changeset
    24
#include "ContentViewDelegate.h"
hgs
parents:
diff changeset
    25
#include "ChromeSnippet.h"
hgs
parents:
diff changeset
    26
#include "webpagecontroller.h"
hgs
parents:
diff changeset
    27
hgs
parents:
diff changeset
    28
#include <QDebug>
hgs
parents:
diff changeset
    29
hgs
parents:
diff changeset
    30
namespace GVA {
hgs
parents:
diff changeset
    31
hgs
parents:
diff changeset
    32
  ProgressBarItem::ProgressBarItem(ChromeSnippet * snippet, QGraphicsItem* parent)
hgs
parents:
diff changeset
    33
    : NativeChromeItem(snippet, parent),
hgs
parents:
diff changeset
    34
      m_progress(0)
hgs
parents:
diff changeset
    35
  {
hgs
parents:
diff changeset
    36
    //Set indicator color from element css
hgs
parents:
diff changeset
    37
    QString cssColor = m_snippet->element().styleProperty("color", QWebElement::ComputedStyle);
hgs
parents:
diff changeset
    38
    CSSToQColor(cssColor, m_color);
hgs
parents:
diff changeset
    39
hgs
parents:
diff changeset
    40
    connectAll();
hgs
parents:
diff changeset
    41
  }
hgs
parents:
diff changeset
    42
hgs
parents:
diff changeset
    43
  ProgressBarItem:: ~ProgressBarItem()
hgs
parents:
diff changeset
    44
  {
hgs
parents:
diff changeset
    45
hgs
parents:
diff changeset
    46
  }
hgs
parents:
diff changeset
    47
hgs
parents:
diff changeset
    48
  void ProgressBarItem::onProgress(int progress)
hgs
parents:
diff changeset
    49
  {
hgs
parents:
diff changeset
    50
      setProgress(((qreal)progress)/100);
hgs
parents:
diff changeset
    51
  }
hgs
parents:
diff changeset
    52
hgs
parents:
diff changeset
    53
  void ProgressBarItem::setProgress(qreal progress)
hgs
parents:
diff changeset
    54
  {
hgs
parents:
diff changeset
    55
    //QApplication::processEvents(QEventLoop::ExcludeSocketNotifiers);
hgs
parents:
diff changeset
    56
    //qDebug() << "ProgressBarItem::onProgress: " << progress;
hgs
parents:
diff changeset
    57
    if (m_progress != progress){
hgs
parents:
diff changeset
    58
      m_progress = progress;
hgs
parents:
diff changeset
    59
      update();
hgs
parents:
diff changeset
    60
    }
hgs
parents:
diff changeset
    61
  }
hgs
parents:
diff changeset
    62
hgs
parents:
diff changeset
    63
  void ProgressBarItem::onStart() {
hgs
parents:
diff changeset
    64
    onProgress(0);
hgs
parents:
diff changeset
    65
  }
hgs
parents:
diff changeset
    66
hgs
parents:
diff changeset
    67
  void ProgressBarItem::onFinished(bool ok) {
hgs
parents:
diff changeset
    68
    Q_UNUSED(ok)
hgs
parents:
diff changeset
    69
    onProgress(0);
hgs
parents:
diff changeset
    70
  }
hgs
parents:
diff changeset
    71
hgs
parents:
diff changeset
    72
  void ProgressBarItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* opt, QWidget* widget)
hgs
parents:
diff changeset
    73
  {
hgs
parents:
diff changeset
    74
    Q_UNUSED(opt)
hgs
parents:
diff changeset
    75
    Q_UNUSED(widget)
hgs
parents:
diff changeset
    76
hgs
parents:
diff changeset
    77
    //painter->fillRect(QRectF(0,0, geometry().width(), geometry().height()), Qt::blue);
hgs
parents:
diff changeset
    78
    if (m_progress > 0) {
hgs
parents:
diff changeset
    79
        QSizeF s = size();
hgs
parents:
diff changeset
    80
        qreal minWidth = s.width()/10;
hgs
parents:
diff changeset
    81
        painter->fillRect(QRectF(0,0, minWidth + (s.width() - minWidth)*m_progress, s.height()), m_color);
hgs
parents:
diff changeset
    82
    }
hgs
parents:
diff changeset
    83
    NativeChromeItem::paint(painter, opt, widget);
hgs
parents:
diff changeset
    84
  }
hgs
parents:
diff changeset
    85
hgs
parents:
diff changeset
    86
  void ProgressBarItem::connectAll() {
hgs
parents:
diff changeset
    87
    WebPageController* pageController = WebPageController::getSingleton();
hgs
parents:
diff changeset
    88
    if (pageController){
hgs
parents:
diff changeset
    89
      connect(pageController, SIGNAL(loadStarted()), this, SLOT(onStart()));
hgs
parents:
diff changeset
    90
      connect(pageController, SIGNAL(loadProgress(int)), this, SLOT(onProgress(int)));
hgs
parents:
diff changeset
    91
      connect(pageController, SIGNAL(loadFinished(bool)), this, SLOT(onFinished(bool)));
hgs
parents:
diff changeset
    92
    }
hgs
parents:
diff changeset
    93
  }
hgs
parents:
diff changeset
    94
hgs
parents:
diff changeset
    95
} // end of namespace GVA
hgs
parents:
diff changeset
    96
hgs
parents:
diff changeset
    97