tests/benchmarks/uimodels/GraphicsViewBenchmark/widgets/iconitem.cpp
changeset 3 41300fa6a67c
equal deleted inserted replaced
2:56cd8111b7f7 3:41300fa6a67c
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     5 **
       
     6 ** This file is part of the examples of the Qt Toolkit.
       
     7 **
       
     8 ** $QT_BEGIN_LICENSE:LGPL$
       
     9 ** No Commercial Usage
       
    10 ** This file contains pre-release code and may not be distributed.
       
    11 ** You may use this file in accordance with the terms and conditions
       
    12 ** contained in the either Technology Preview License Agreement or the
       
    13 ** Beta Release License Agreement.
       
    14 **
       
    15 ** GNU Lesser General Public License Usage
       
    16 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    17 ** General Public License version 2.1 as published by the Free Software
       
    18 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    19 ** packaging of this file.  Please review the following information to
       
    20 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    21 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    22 **
       
    23 ** In addition, as a special exception, Nokia gives you certain
       
    24 ** additional rights. These rights are described in the Nokia Qt LGPL
       
    25 ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
       
    26 ** package.
       
    27 **
       
    28 ** GNU General Public License Usage
       
    29 ** Alternatively, this file may be used under the terms of the GNU
       
    30 ** General Public License version 3.0 as published by the Free Software
       
    31 ** Foundation and appearing in the file LICENSE.GPL included in the
       
    32 ** packaging of this file.  Please review the following information to
       
    33 ** ensure the GNU General Public License version 3.0 requirements will be
       
    34 ** met: http://www.gnu.org/copyleft/gpl.html.
       
    35 **
       
    36 ** If you are unsure which license is appropriate for your use, please
       
    37 ** contact the sales department at http://qt.nokia.com/contact.
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #include <QtGui>
       
    43 #include <QSvgRenderer>
       
    44 
       
    45 #if (QT_VERSION >= 0x040600)
       
    46 #include <QGraphicsEffect>
       
    47 #endif
       
    48 
       
    49 #include "iconitem.h"
       
    50 
       
    51 IconItem::IconItem(const QString &filename, QGraphicsItem *parent) 
       
    52   : GvbWidget(parent)
       
    53   , m_filename(filename)
       
    54   , m_rotation(0.0)
       
    55 #if (QT_VERSION >= 0x040600)
       
    56   , m_opacityEffect(0)
       
    57 #endif
       
    58   , m_smoothTransformation(false)
       
    59 {
       
    60     setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
       
    61     setContentsMargins(0,0,0,0);
       
    62     setPreferredSize(58,58);
       
    63 }
       
    64 
       
    65 IconItem::~IconItem()
       
    66 {
       
    67 }
       
    68 
       
    69 void IconItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
       
    70 {
       
    71     Q_UNUSED(option);
       
    72     Q_UNUSED(widget);
       
    73 
       
    74     reload();
       
    75 
       
    76     const QPointF c = boundingRect().center();
       
    77     painter->translate(c.x(), c.y());
       
    78     painter->rotate(m_rotation);
       
    79     painter->translate(-c.x(), -c.y());
       
    80 
       
    81     if (m_smoothTransformation)
       
    82         painter->setRenderHints(QPainter::SmoothPixmapTransform);
       
    83 
       
    84     painter->drawPixmap(0,0, m_pixmap);
       
    85 }
       
    86 
       
    87 QSizeF IconItem::sizeHint(Qt::SizeHint which, 
       
    88     const QSizeF &constraint) const
       
    89 {
       
    90     switch (which)
       
    91     {
       
    92     case Qt::MinimumSize:
       
    93     case Qt::PreferredSize:
       
    94     case Qt::MaximumSize:
       
    95         return m_pixmap.size();
       
    96 
       
    97     default:
       
    98         return GvbWidget::sizeHint(which, constraint);
       
    99     }
       
   100 }
       
   101 
       
   102 void IconItem::reload()
       
   103 {
       
   104     const QSize iconSize = size().toSize();
       
   105     if (iconSize.width() == 0 || iconSize.height()  == 0)
       
   106         return;
       
   107 
       
   108     const QString key = m_filename+QString::number(iconSize.width())+QString::number(iconSize.height());
       
   109     if (QPixmapCache::find(key, m_pixmap))
       
   110         return;
       
   111 
       
   112     if (m_filename.endsWith(".svg", Qt::CaseInsensitive))
       
   113     {
       
   114         m_pixmap = QPixmap(iconSize);
       
   115         m_pixmap.fill(Qt::transparent);
       
   116         QSvgRenderer doc(m_filename);
       
   117         QPainter painter(&m_pixmap);
       
   118         painter.setViewport(0, 0, iconSize.width(), iconSize.height());
       
   119         doc.render(&painter);
       
   120     }
       
   121     else
       
   122     {
       
   123         m_pixmap = QPixmap(m_filename).scaled(iconSize);
       
   124     }
       
   125 
       
   126     QPixmapCache::insert(key, m_pixmap);
       
   127     updateGeometry();
       
   128 }
       
   129 
       
   130 QString IconItem::fileName() const
       
   131 {
       
   132     return m_filename;
       
   133 }
       
   134 
       
   135 void IconItem::setFileName(const QString &filename)
       
   136 {
       
   137     if( m_filename != filename) {
       
   138         m_filename = filename;
       
   139         reload();
       
   140     }
       
   141 }
       
   142 
       
   143 #if (QT_VERSION >= 0x040600)
       
   144 void IconItem::setOpacityEffectEnabled(const bool enable)
       
   145 {
       
   146     if (!m_opacityEffect)
       
   147     {
       
   148         QRadialGradient gradient(0.5, 0.5, 1.0);
       
   149         gradient.setCoordinateMode(QGradient::ObjectBoundingMode);
       
   150         gradient.setColorAt(0.0, QColor(0,0,0, 255));
       
   151         gradient.setColorAt(0.46, QColor(0,0,0, 255));
       
   152         gradient.setColorAt(0.62, QColor(0,0,0, 0));
       
   153     
       
   154         m_opacityEffect = new QGraphicsOpacityEffect;
       
   155         m_opacityEffect->setOpacityMask(gradient);
       
   156         m_opacityEffect->setOpacity(1.0);
       
   157         this->setGraphicsEffect(m_opacityEffect);
       
   158     }
       
   159     m_opacityEffect->setEnabled(enable);
       
   160 }
       
   161 
       
   162 bool IconItem::isOpacityEffectEnabled() const
       
   163 {
       
   164     if (m_opacityEffect)
       
   165         return m_opacityEffect->isEnabled();
       
   166 
       
   167     return false;
       
   168 }
       
   169 #endif