src/hbcore/primitives/hbiconitem.cpp
changeset 21 4633027730f5
parent 7 923ff622b8b9
child 23 e6ad4ef83b23
equal deleted inserted replaced
7:923ff622b8b9 21:4633027730f5
    26 #include "hbiconitem.h"
    26 #include "hbiconitem.h"
    27 #include "hbiconitem_p.h"
    27 #include "hbiconitem_p.h"
    28 #include "hbiconanimator.h"
    28 #include "hbiconanimator.h"
    29 #include "hbicon_p.h"
    29 #include "hbicon_p.h"
    30 #include "hboogmwatcher_p.h"
    30 #include "hboogmwatcher_p.h"
       
    31 #include "hbinstance_p.h"
    31 
    32 
    32 #include <QPainter>
    33 #include <QPainter>
    33 #include <QStyleOptionGraphicsItem>
    34 #include <QStyleOptionGraphicsItem>
    34 
    35 
    35 /*!
    36 /*!
   116     mAnimator(),
   117     mAnimator(),
   117     mAlignment(HbIconItem::defaultAlignment),
   118     mAlignment(HbIconItem::defaultAlignment),
   118     mAspectRatioMode(HbIconItem::defaultAspectRatioMode),
   119     mAspectRatioMode(HbIconItem::defaultAspectRatioMode),
   119     mState(HbIconItem::defaultState),
   120     mState(HbIconItem::defaultState),
   120     mMode(HbIconItem::defaultMode),
   121     mMode(HbIconItem::defaultMode),
   121     mClearCachedRect(true)
   122     mClearCachedRect(true),
       
   123     mIconScalingEnabled(true)
   122 {
   124 {
   123     q_ptr = 0;
   125     q_ptr = 0;
   124 }
   126 }
   125 
   127 
   126 HbIconItemPrivate::~HbIconItemPrivate()
   128 HbIconItemPrivate::~HbIconItemPrivate()
   138         HbIconPrivate::d_ptr_detached(&mIcon)->setThemedColor(mThemedColor);
   140         HbIconPrivate::d_ptr_detached(&mIcon)->setThemedColor(mThemedColor);
   139     }
   141     }
   140     const QRectF boundingRect = q->rect();
   142     const QRectF boundingRect = q->rect();
   141     if (!boundingRect.size().isEmpty()) {
   143     if (!boundingRect.size().isEmpty()) {
   142         mIconRect = boundingRect;
   144         mIconRect = boundingRect;
   143         mIcon.setSize(mIconRect.size());
   145         mIcon.setSize(mIconScalingEnabled ? mIconRect.size() : mIcon.defaultSize());
   144         mAnimator.setIcon(mIcon);
   146         mAnimator.setIcon(mIcon);
   145         q->update();
   147         q->update();
   146     }
   148     }
   147 }
   149 }
   148 
   150 
   160 
   162 
   161 void HbIconItemPrivate::recalculateBoundingRect() const
   163 void HbIconItemPrivate::recalculateBoundingRect() const
   162 {
   164 {
   163     Q_Q(const HbIconItem);
   165     Q_Q(const HbIconItem);
   164     mBoundingRect = q->HbWidgetBase::boundingRect();
   166     mBoundingRect = q->HbWidgetBase::boundingRect();
   165     //workaround for qt bug http://bugreports.qt.nokia.com/browse/QTBUG-8820
       
   166     mAdjustedRect.setWidth(qMax(qreal(0),mBoundingRect.width() - qreal(2.0)));
       
   167     mAdjustedRect.setHeight(qMax(qreal(0),mBoundingRect.height() - qreal(2.0)));
       
   168     //workaround ends
       
   169     mClearCachedRect = false;
   167     mClearCachedRect = false;
   170 }
   168 }
   171 
   169 
   172 void HbIconItemPrivate::setThemedColor(const QColor &color)
   170 void HbIconItemPrivate::setThemedColor(const QColor &color)
   173 {
   171 {
   296 }
   294 }
   297 
   295 
   298 /*!
   296 /*!
   299  Sets the alignment for the icon.
   297  Sets the alignment for the icon.
   300 
   298 
   301  If this method is not called, the icon uses its default alignment.
   299  By default no aligning is used.
   302  The default alignment can be defined in the icon definition file, otherwise it defaults to Qt::AlignCenter.
   300 
   303  \param alignment the new icon alignment.gnCenter.
   301  \param alignment the new icon alignment
   304 
   302 
   305  \sa alignment
   303  \sa alignment
   306  */
   304  */
   307 void HbIconItem::setAlignment(Qt::Alignment alignment)
   305 void HbIconItem::setAlignment(Qt::Alignment alignment)
   308 {
   306 {
   315 }
   313 }
   316 
   314 
   317 /*!
   315 /*!
   318  Sets the aspect ratio mode for the icon.
   316  Sets the aspect ratio mode for the icon.
   319 
   317 
   320  If this method is not called, the icon uses its default aspect ratio mode.
   318  It defaults to Qt::KeepAspectRatio.
   321  The default mode can be defined in the icon definition file, otherwise it defaults to Qt::KeepAspectRatio.
   319 
   322  \param aspectRatio the new icon aspect ratio.
   320  \param aspectRatio the new icon aspect ratio.
   323 
   321 
   324  \sa aspectRatioMode
   322  \sa aspectRatioMode
   325  */
   323  */
   326 void HbIconItem::setAspectRatioMode(Qt::AspectRatioMode aspectRatioMode)
   324 void HbIconItem::setAspectRatioMode(Qt::AspectRatioMode aspectRatioMode)
   432         d->mBrush = brush;
   430         d->mBrush = brush;
   433         update();
   431         update();
   434     }
   432     }
   435 }
   433 }
   436 
   434 
       
   435 /*!
       
   436     Enables or disables icon scaling.
       
   437 
       
   438     By default the setting is true, i.e. scaling is enabled.
       
   439    
       
   440     When enabled, the size of the icon will be set to match
       
   441     the item's size, so scaling may occur (by taking the aspect
       
   442     ratio setting into account).
       
   443 
       
   444     Scaling should only be disabled when the icon item is used
       
   445     with raster graphics, for which upscaling is not desired.
       
   446     
       
   447     When disabled, the size of the icon will be the default size
       
   448     of its source, i.e. in case of raster graphics the original dimensions
       
   449     of the image. It is recommended to set the center alignment too in this case.
       
   450     The size of the icon item is not affected, the content will just not cover the
       
   451     entire area. Note however that when the icon's default size is bigger than the
       
   452     size of the item displaying it then downscaling will still occur.
       
   453     
       
   454     \sa iconScaling
       
   455 */
       
   456 void HbIconItem::setIconScaling(bool enabled)
       
   457 {
       
   458     Q_D(HbIconItem);
       
   459     if (d->mIconScalingEnabled != enabled) {
       
   460         d->mIconScalingEnabled = enabled;
       
   461         d->updateIconItem();
       
   462     }
       
   463 }
       
   464     
   437 /*!
   465 /*!
   438  Sets the new icon name for this HbIconItem for \a mode and \a state. If iconName is already set in
   466  Sets the new icon name for this HbIconItem for \a mode and \a state. If iconName is already set in
   439  HbIconItem then it will be replaced with this new iconName.
   467  HbIconItem then it will be replaced with this new iconName.
   440  \param  iconName the icon name.
   468  \param  iconName the icon name.
   441  \param  mode  mode of the  icon.
   469  \param  mode  mode of the  icon.
   442  \param  state state of the icon.
   470  \param  state state of the icon.
   443 
   471 
   444 
       
   445  \sa HbIcon::setIconName()
   472  \sa HbIcon::setIconName()
   446  \sa iconName
   473  \sa iconName
   447  */
   474  */
   448 void HbIconItem::setIconName(const QString &iconName, QIcon::Mode mode, QIcon::State state)
   475 void HbIconItem::setIconName(const QString &iconName, QIcon::Mode mode, QIcon::State state)
   449 {
   476 {
   608 {
   635 {
   609     Q_D(const HbIconItem);
   636     Q_D(const HbIconItem);
   610     return d->mBrush;
   637     return d->mBrush;
   611 }
   638 }
   612 
   639 
       
   640 /*!
       
   641   \return the current setting for icon scaling. By default it is true (i.e. enabled).
       
   642   
       
   643   \sa setIconScaling
       
   644  */
       
   645 bool HbIconItem::iconScaling() const
       
   646 {
       
   647     Q_D(const HbIconItem);
       
   648     return d->mIconScalingEnabled;
       
   649 }
       
   650     
   613 /*!
   651 /*!
   614  * Refer HbIcon::isNull()
   652  * Refer HbIcon::isNull()
   615  */
   653  */
   616 bool HbIconItem::isNull() const
   654 bool HbIconItem::isNull() const
   617 {
   655 {
   636         if (d->mIconRect != d->mBoundingRect) {
   674         if (d->mIconRect != d->mBoundingRect) {
   637             d->mIconRect = d->mBoundingRect;
   675             d->mIconRect = d->mBoundingRect;
   638             if (!d->mIcon.isNull()) {
   676             if (!d->mIcon.isNull()) {
   639                 HbIconPrivate::d_ptr_detached(&d->mIcon)->setThemedColor(d->mThemedColor);
   677                 HbIconPrivate::d_ptr_detached(&d->mIcon)->setThemedColor(d->mThemedColor);
   640             }
   678             }
   641             d->mIcon.setSize(d->mIconRect.size());
   679             d->mIcon.setSize(d->mIconScalingEnabled ? d->mIconRect.size() : d->mIcon.defaultSize());
   642             d->mAnimator.setIcon(d->mIcon);
   680             d->mAnimator.setIcon(d->mIcon);
   643         }
   681         }
   644         if (d->mBrush != Qt::NoBrush) {
   682         if (d->mBrush != Qt::NoBrush) {
   645             painter->fillRect(d->mBoundingRect, d->mBrush);
   683             painter->fillRect(d->mBoundingRect, d->mBrush);
   646         }
   684         }
   662 {
   700 {
   663     Q_D(const HbIconItem);
   701     Q_D(const HbIconItem);
   664     if (d->mClearCachedRect) {
   702     if (d->mClearCachedRect) {
   665         d->recalculateBoundingRect();
   703         d->recalculateBoundingRect();
   666     }
   704     }
   667     return d->mAdjustedRect;
   705     return d->mBoundingRect;
   668 }
   706 }
   669 
   707 
   670 /*!
   708 /*!
   671  \reimp
   709  \reimp
   672  */
   710  */
   699    \reimp
   737    \reimp
   700 */
   738 */
   701 QVariant HbIconItem::itemChange(GraphicsItemChange change, const QVariant &value)
   739 QVariant HbIconItem::itemChange(GraphicsItemChange change, const QVariant &value)
   702 {
   740 {
   703     Q_D(HbIconItem);
   741     Q_D(HbIconItem);
   704     if (QGraphicsItem::ItemEnabledHasChanged == change) {
   742     if (change == QGraphicsItem::ItemEnabledHasChanged) {
   705         d->mMode = value.toBool() ? QIcon::Normal : QIcon::Disabled;
   743         d->mMode = value.toBool() ? QIcon::Normal : QIcon::Disabled;
       
   744     } else if (change == QGraphicsItem::ItemVisibleHasChanged) {
       
   745         if (!value.toBool() && HbInstancePrivate::d_ptr()->mDropHiddenIconData) {
       
   746             d->clearStoredIconContent();
       
   747         }
   706     }
   748     }
   707 
   749 
   708     return HbWidgetBase::itemChange(change, value);
   750     return HbWidgetBase::itemChange(change, value);
   709 }
   751 }
   710 
   752