src/hbcore/primitives/hbiconitem.cpp
changeset 34 ed14f46c0e55
parent 7 923ff622b8b9
equal deleted inserted replaced
31:7516d6d86cf5 34:ed14f46c0e55
    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"
       
    32 #include "hbiconengine_p.h"
    31 
    33 
    32 #include <QPainter>
    34 #include <QPainter>
    33 #include <QStyleOptionGraphicsItem>
    35 #include <QStyleOptionGraphicsItem>
    34 
    36 
    35 /*!
    37 /*!
   109 // the other primitives of the same widget).
   111 // the other primitives of the same widget).
   110 const Qt::Alignment HbIconItem::defaultAlignment = 0;
   112 const Qt::Alignment HbIconItem::defaultAlignment = 0;
   111 
   113 
   112 bool HbIconItemPrivate::outlinesEnabled = false;
   114 bool HbIconItemPrivate::outlinesEnabled = false;
   113 
   115 
       
   116 static void asyncReady(void *param)
       
   117 {
       
   118     HbIconItem *item = static_cast<HbIconItem *>(param);
       
   119     HbIconItemPrivate *d = HbIconItemPrivate::d_ptr(item);
       
   120     bool doUpdate = d->mAsyncFilter ? !d->mAsyncFilter(item, d->mAsyncFilterParam) : true;
       
   121     if (doUpdate) {
       
   122         item->update();
       
   123     }
       
   124 }
       
   125 
   114 HbIconItemPrivate::HbIconItemPrivate(const HbIcon &icon) :
   126 HbIconItemPrivate::HbIconItemPrivate(const HbIcon &icon) :
   115     mIcon(icon),
   127     mIcon(icon),
   116     mAnimator(),
   128     mAnimator(),
   117     mAlignment(HbIconItem::defaultAlignment),
   129     mAlignment(HbIconItem::defaultAlignment),
   118     mAspectRatioMode(HbIconItem::defaultAspectRatioMode),
   130     mAspectRatioMode(HbIconItem::defaultAspectRatioMode),
   119     mState(HbIconItem::defaultState),
   131     mState(HbIconItem::defaultState),
   120     mMode(HbIconItem::defaultMode),
   132     mMode(HbIconItem::defaultMode),
   121     mClearCachedRect(true)
   133     mClearCachedRect(true),
       
   134     mIconScalingEnabled(true),
       
   135     mAsync(false),
       
   136     mAsyncFilter(0),
       
   137     mAsyncFilterParam(0)
   122 {
   138 {
   123     q_ptr = 0;
   139     q_ptr = 0;
   124 }
   140 }
   125 
   141 
   126 HbIconItemPrivate::~HbIconItemPrivate()
   142 HbIconItemPrivate::~HbIconItemPrivate()
   133     if (!mIcon.isNull()) {
   149     if (!mIcon.isNull()) {
   134         // This must be done before the setIcon() call below due to the
   150         // This must be done before the setIcon() call below due to the
   135         // possibility of detaching. Doing it afterwards would lead to
   151         // possibility of detaching. Doing it afterwards would lead to
   136         // colorization errors as the themed color might potentially be set for
   152         // colorization errors as the themed color might potentially be set for
   137         // a different icon engine, not for the one that is used in painting.
   153         // a different icon engine, not for the one that is used in painting.
   138         HbIconPrivate::d_ptr_detached(&mIcon)->setThemedColor(mThemedColor);
   154         HbIconPrivate *p = HbIconPrivate::d_ptr_detached(&mIcon);
       
   155         p->setThemedColor(mThemedColor);
       
   156         p->setAsync(mAsync, asyncReady, q);
   139     }
   157     }
   140     const QRectF boundingRect = q->rect();
   158     const QRectF boundingRect = q->rect();
   141     if (!boundingRect.size().isEmpty()) {
   159     if (!boundingRect.size().isEmpty()) {
   142         mIconRect = boundingRect;
   160         mIconRect = boundingRect;
   143         mIcon.setSize(mIconRect.size());
   161         mIcon.setSize(mIconScalingEnabled ? mIconRect.size() : mIcon.defaultSize());
   144         mAnimator.setIcon(mIcon);
   162         mAnimator.setIcon(mIcon);
   145         q->update();
   163         q->update();
   146     }
   164     }
   147 }
   165 }
   148 
   166 
   149 void HbIconItemPrivate::updateIconParams()
   167 void HbIconItemPrivate::updateIconParams()
   150 {
   168 {
   151     Q_Q(HbIconItem);
   169     Q_Q(HbIconItem);
   152     if (mIconRect.isValid()) {
   170     if (mIconRect.isValid()) {
   153         if (!mIcon.isNull()) {
   171         if (!mIcon.isNull()) {
   154             HbIconPrivate::d_ptr_detached(&mIcon)->setThemedColor(mThemedColor);
   172             HbIconPrivate *p = HbIconPrivate::d_ptr_detached(&mIcon);
       
   173             p->setThemedColor(mThemedColor);
       
   174             p->setAsync(mAsync, asyncReady, q);
   155         }
   175         }
   156         mAnimator.setIcon(mIcon);
   176         mAnimator.setIcon(mIcon);
   157     }
   177     }
   158     q->update();
   178     q->update();
   159 }
   179 }
   160 
   180 
   161 void HbIconItemPrivate::recalculateBoundingRect() const
   181 void HbIconItemPrivate::recalculateBoundingRect() const
   162 {
   182 {
   163     Q_Q(const HbIconItem);
   183     Q_Q(const HbIconItem);
   164     mBoundingRect = q->HbWidgetBase::boundingRect();
   184     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;
   185     mClearCachedRect = false;
   170 }
   186 }
   171 
   187 
   172 void HbIconItemPrivate::setThemedColor(const QColor &color)
   188 void HbIconItemPrivate::setThemedColor(const QColor &color)
   173 {
   189 {
   174     mThemedColor = color;
   190     mThemedColor = color;
   175     updateIconItem();
   191     updateIconItem();
       
   192 }
       
   193 
       
   194 /*!
       
   195   Requests clearStoredIconContent() for the currently set icon.
       
   196 */
       
   197 void HbIconItemPrivate::clearStoredIconContent()
       
   198 {
       
   199     HbIconPrivate::d_ptr(&mIcon)->clearStoredIconContent();
       
   200 }
       
   201 
       
   202 /*!
       
   203   Installs a filter for the async-load-ready callbacks.
       
   204 
       
   205   When non-null, the filter is called before invoking update() on the
       
   206   icon item, and if the filter returns true, the update() call is
       
   207   suppressed.
       
   208 */
       
   209 void HbIconItemPrivate::setAsyncCallbackFilter(AsyncCallbackFilter filter, void *filterParam)
       
   210 {
       
   211     mAsyncFilter = filter;
       
   212     mAsyncFilterParam = filterParam;
   176 }
   213 }
   177 
   214 
   178 /*!
   215 /*!
   179  Constructs a new HbIconItem with \a iconName and \a parent.
   216  Constructs a new HbIconItem with \a iconName and \a parent.
   180  \param iconName the name of the icon.
   217  \param iconName the name of the icon.
   296 }
   333 }
   297 
   334 
   298 /*!
   335 /*!
   299  Sets the alignment for the icon.
   336  Sets the alignment for the icon.
   300 
   337 
   301  If this method is not called, the icon uses its default alignment.
   338  By default no aligning is used.
   302  The default alignment can be defined in the icon definition file, otherwise it defaults to Qt::AlignCenter.
   339 
   303  \param alignment the new icon alignment.gnCenter.
   340  \param alignment the new icon alignment
   304 
   341 
   305  \sa alignment
   342  \sa alignment
   306  */
   343  */
   307 void HbIconItem::setAlignment(Qt::Alignment alignment)
   344 void HbIconItem::setAlignment(Qt::Alignment alignment)
   308 {
   345 {
   309     Q_D(HbIconItem);
   346     Q_D(HbIconItem);
   310     d->setApiProtectionFlag(HbWidgetBasePrivate::AC_IconAlign, true);
   347     d->setApiProtectionFlag(HbWidgetBasePrivate::AC_IconAlign, true);
       
   348     alignment = d->combineAlignment(alignment, d->mAlignment);
   311     if (d->mAlignment != alignment) {
   349     if (d->mAlignment != alignment) {
   312         d->mAlignment = alignment;
   350         d->mAlignment = alignment;
   313         update();
   351         update();
   314     }
   352     }
   315 }
   353 }
   316 
   354 
   317 /*!
   355 /*!
   318  Sets the aspect ratio mode for the icon.
   356  Sets the aspect ratio mode for the icon.
   319 
   357 
   320  If this method is not called, the icon uses its default aspect ratio mode.
   358  It defaults to Qt::KeepAspectRatio.
   321  The default mode can be defined in the icon definition file, otherwise it defaults to Qt::KeepAspectRatio.
   359 
   322  \param aspectRatio the new icon aspect ratio.
   360  \param aspectRatio the new icon aspect ratio.
   323 
   361 
   324  \sa aspectRatioMode
   362  \sa aspectRatioMode
   325  */
   363  */
   326 void HbIconItem::setAspectRatioMode(Qt::AspectRatioMode aspectRatioMode)
   364 void HbIconItem::setAspectRatioMode(Qt::AspectRatioMode aspectRatioMode)
   429     Q_D(HbIconItem);
   467     Q_D(HbIconItem);
   430     d->setApiProtectionFlag(HbWidgetBasePrivate::AC_IconBrush, true);
   468     d->setApiProtectionFlag(HbWidgetBasePrivate::AC_IconBrush, true);
   431     if (d->mBrush != brush) {
   469     if (d->mBrush != brush) {
   432         d->mBrush = brush;
   470         d->mBrush = brush;
   433         update();
   471         update();
       
   472     }
       
   473 }
       
   474 
       
   475 /*!
       
   476     Enables or disables icon scaling.
       
   477 
       
   478     By default the setting is true, i.e. scaling is enabled.
       
   479    
       
   480     When enabled, the size of the icon will be set to match
       
   481     the item's size, so scaling may occur (by taking the aspect
       
   482     ratio setting into account).
       
   483 
       
   484     Scaling should only be disabled when the icon item is used
       
   485     with raster graphics, for which upscaling is not desired.
       
   486     
       
   487     When disabled, the size of the icon will be the default size
       
   488     of its source, i.e. in case of raster graphics the original dimensions
       
   489     of the image. It is recommended to set the center alignment too in this case.
       
   490     The size of the icon item is not affected, the content will just not cover the
       
   491     entire area. Note however that when the icon's default size is bigger than the
       
   492     size of the item displaying it then downscaling will still occur.
       
   493     
       
   494     \sa iconScaling
       
   495 */
       
   496 void HbIconItem::setIconScaling(bool enabled)
       
   497 {
       
   498     Q_D(HbIconItem);
       
   499     if (d->mIconScalingEnabled != enabled) {
       
   500         d->mIconScalingEnabled = enabled;
       
   501         d->updateIconItem();
       
   502     }
       
   503 }
       
   504 
       
   505 /*!
       
   506   This function can be used to enable asynchronous icon data retrieval.
       
   507 
       
   508   It has no effect if called after the first paint so it needs to be called
       
   509   immediately after creating the HbIconItem instance.
       
   510 
       
   511   While the data is not available empty content will be painted.
       
   512 
       
   513   \sa async
       
   514  */
       
   515 void HbIconItem::setAsync(bool async)
       
   516 {
       
   517     Q_D(HbIconItem);
       
   518     if (d->mAsync != async) {
       
   519         d->mAsync = async;
       
   520         d->updateIconItem();
   434     }
   521     }
   435 }
   522 }
   436 
   523 
   437 /*!
   524 /*!
   438  Sets the new icon name for this HbIconItem for \a mode and \a state. If iconName is already set in
   525  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.
   526  HbIconItem then it will be replaced with this new iconName.
   440  \param  iconName the icon name.
   527  \param  iconName the icon name.
   441  \param  mode  mode of the  icon.
   528  \param  mode  mode of the  icon.
   442  \param  state state of the icon.
   529  \param  state state of the icon.
   443 
   530 
   444 
       
   445  \sa HbIcon::setIconName()
   531  \sa HbIcon::setIconName()
   446  \sa iconName
   532  \sa iconName
   447  */
   533  */
   448 void HbIconItem::setIconName(const QString &iconName, QIcon::Mode mode, QIcon::State state)
   534 void HbIconItem::setIconName(const QString &iconName, QIcon::Mode mode, QIcon::State state)
   449 {
   535 {
   609     Q_D(const HbIconItem);
   695     Q_D(const HbIconItem);
   610     return d->mBrush;
   696     return d->mBrush;
   611 }
   697 }
   612 
   698 
   613 /*!
   699 /*!
       
   700   \return the current setting for icon scaling. By default it is true (i.e. enabled).
       
   701   
       
   702   \sa setIconScaling
       
   703  */
       
   704 bool HbIconItem::iconScaling() const
       
   705 {
       
   706     Q_D(const HbIconItem);
       
   707     return d->mIconScalingEnabled;
       
   708 }
       
   709 
       
   710 /*!
       
   711   \return the current setting for asynchronous icon data retrieval.
       
   712   By default the value is false (i.e. disabled).
       
   713 
       
   714   \sa setAsync
       
   715  */
       
   716 bool HbIconItem::async() const
       
   717 {
       
   718     Q_D(const HbIconItem);
       
   719     return d->mAsync;
       
   720 }
       
   721 
       
   722 /*!
   614  * Refer HbIcon::isNull()
   723  * Refer HbIcon::isNull()
   615  */
   724  */
   616 bool HbIconItem::isNull() const
   725 bool HbIconItem::isNull() const
   617 {
   726 {
   618     Q_D(const HbIconItem);
   727     Q_D(const HbIconItem);
   627 void HbIconItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
   736 void HbIconItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
   628 {
   737 {
   629     Q_UNUSED(widget)
   738     Q_UNUSED(widget)
   630     Q_UNUSED(option)
   739     Q_UNUSED(option)
   631     Q_D(HbIconItem);
   740     Q_D(HbIconItem);
       
   741 
       
   742     if (d->mIcon.isNull() && d->mBrush == Qt::NoBrush) {
       
   743         return;
       
   744     }
       
   745 
   632     if (d->mClearCachedRect){
   746     if (d->mClearCachedRect){
   633         d->recalculateBoundingRect();
   747         d->recalculateBoundingRect();
   634     }
   748     }
       
   749 
   635     if (!d->mBoundingRect.isEmpty()){
   750     if (!d->mBoundingRect.isEmpty()){
   636         if (d->mIconRect != d->mBoundingRect) {
   751         if (d->mIconRect != d->mBoundingRect) {
   637             d->mIconRect = d->mBoundingRect;
   752             d->mIconRect = d->mBoundingRect;
   638             if (!d->mIcon.isNull()) {
   753             if (!d->mIcon.isNull()) {
   639                 HbIconPrivate::d_ptr_detached(&d->mIcon)->setThemedColor(d->mThemedColor);
   754                 HbIconPrivate *p = HbIconPrivate::d_ptr_detached(&d->mIcon);
       
   755                 p->setThemedColor(d->mThemedColor);
       
   756                 p->setAsync(d->mAsync, asyncReady, this);
   640             }
   757             }
   641             d->mIcon.setSize(d->mIconRect.size());
   758             d->mIcon.setSize(d->mIconScalingEnabled ? d->mIconRect.size() : d->mIcon.defaultSize());
   642             d->mAnimator.setIcon(d->mIcon);
   759             d->mAnimator.setIcon(d->mIcon);
   643         }
   760         }
   644         if (d->mBrush != Qt::NoBrush) {
   761         if (d->mBrush != Qt::NoBrush) {
   645             painter->fillRect(d->mBoundingRect, d->mBrush);
   762             painter->fillRect(d->mBoundingRect, d->mBrush);
   646         }
   763         }
   647         d->mAnimator.paint(painter, d->mBoundingRect,
   764         d->mAnimator.paint(painter,
   648                            d->mAspectRatioMode, d->mAlignment,
   765                            d->mBoundingRect,
       
   766                            d->mAspectRatioMode,
       
   767                            QStyle::visualAlignment(layoutDirection(), d->mAlignment),
   649                            d->mMode, d->mState);
   768                            d->mMode, d->mState);
   650     }
   769     }
       
   770 
   651     if (HbIconItemPrivate::outlinesEnabled) {
   771     if (HbIconItemPrivate::outlinesEnabled) {
   652         painter->setBrush(QBrush(QColor(0, 255, 0, 50)));
   772         painter->setBrush(QBrush(QColor(0, 255, 0, 50)));
   653         painter->drawRect(contentsRect());
   773         painter->drawRect(contentsRect());
   654     }
   774     }
   655 }
   775 }
   662 {
   782 {
   663     Q_D(const HbIconItem);
   783     Q_D(const HbIconItem);
   664     if (d->mClearCachedRect) {
   784     if (d->mClearCachedRect) {
   665         d->recalculateBoundingRect();
   785         d->recalculateBoundingRect();
   666     }
   786     }
   667     return d->mAdjustedRect;
   787     return d->mBoundingRect;
   668 }
   788 }
   669 
   789 
   670 /*!
   790 /*!
   671  \reimp
   791  \reimp
   672  */
   792  */
   699    \reimp
   819    \reimp
   700 */
   820 */
   701 QVariant HbIconItem::itemChange(GraphicsItemChange change, const QVariant &value)
   821 QVariant HbIconItem::itemChange(GraphicsItemChange change, const QVariant &value)
   702 {
   822 {
   703     Q_D(HbIconItem);
   823     Q_D(HbIconItem);
   704     if (QGraphicsItem::ItemEnabledHasChanged == change) {
   824     if (change == QGraphicsItem::ItemEnabledHasChanged) {
   705         d->mMode = value.toBool() ? QIcon::Normal : QIcon::Disabled;
   825         d->mMode = value.toBool() ? QIcon::Normal : QIcon::Disabled;
       
   826     } else if (change == QGraphicsItem::ItemVisibleHasChanged) {
       
   827         if (!value.toBool() && HbInstancePrivate::d_ptr()->mDropHiddenIconData) {
       
   828             d->clearStoredIconContent();
       
   829         }
   706     }
   830     }
   707 
   831 
   708     return HbWidgetBase::itemChange(change, value);
   832     return HbWidgetBase::itemChange(change, value);
   709 }
   833 }
   710 
   834 
   718 {
   842 {
   719     Q_D(HbIconItem);
   843     Q_D(HbIconItem);
   720     return d->mAnimator;
   844     return d->mAnimator;
   721 }
   845 }
   722 
   846 
   723 /*!
       
   724   Requests clearStoredIconContent() for the currently set icon.
       
   725  */
       
   726 void HbIconItemPrivate::clearStoredIconContent()
       
   727 {
       
   728     HbIconPrivate::d_ptr(&mIcon)->clearStoredIconContent();
       
   729 }
       
   730 
       
   731 // end of file
   847 // end of file