100 |
100 |
101 //Initialize the default values |
101 //Initialize the default values |
102 const QIcon::Mode HbIconItem::defaultMode = QIcon::Normal; |
102 const QIcon::Mode HbIconItem::defaultMode = QIcon::Normal; |
103 const QIcon::State HbIconItem::defaultState = QIcon::Off; |
103 const QIcon::State HbIconItem::defaultState = QIcon::Off; |
104 const Qt::AspectRatioMode HbIconItem::defaultAspectRatioMode = Qt::KeepAspectRatio; |
104 const Qt::AspectRatioMode HbIconItem::defaultAspectRatioMode = Qt::KeepAspectRatio; |
105 const Qt::Alignment HbIconItem::defaultAlignment = Qt::AlignCenter; |
105 |
|
106 // Note: No center aligning by default to prevent interesting rounding issues |
|
107 // in certain cases (e.g. 50.25x50.25 sized icon item would lead to having an |
|
108 // icon sized 50x50 drawn at (0.12499, 0.12499) which may or may not fit visually |
|
109 // the other primitives of the same widget). |
|
110 const Qt::Alignment HbIconItem::defaultAlignment = 0; |
106 |
111 |
107 bool HbIconItemPrivate::outlinesEnabled = false; |
112 bool HbIconItemPrivate::outlinesEnabled = false; |
108 |
113 |
109 HbIconItemPrivate::HbIconItemPrivate(const HbIcon &icon) : |
114 HbIconItemPrivate::HbIconItemPrivate(const HbIcon &icon) : |
110 mIcon(icon), |
115 mIcon(icon), |
111 mAnimator(), |
116 mAnimator(), |
112 mAlignment(HbIconItem::defaultAlignment), |
117 mAlignment(HbIconItem::defaultAlignment), |
113 mAspectRatioMode(HbIconItem::defaultAspectRatioMode), |
118 mAspectRatioMode(HbIconItem::defaultAspectRatioMode), |
114 mState(HbIconItem::defaultState), |
119 mState(HbIconItem::defaultState), |
115 mMode(HbIconItem::defaultMode) |
120 mMode(HbIconItem::defaultMode), |
|
121 mClearCachedRect(true) |
116 { |
122 { |
117 q_ptr = 0; |
123 q_ptr = 0; |
118 } |
124 } |
119 |
125 |
120 HbIconItemPrivate::~HbIconItemPrivate () |
126 HbIconItemPrivate::~HbIconItemPrivate() |
121 { |
127 { |
122 } |
128 } |
123 |
129 |
124 void HbIconItemPrivate::updateIconItem() |
130 void HbIconItemPrivate::updateIconItem() |
125 { |
131 { |
126 Q_Q( HbIconItem ); |
132 Q_Q(HbIconItem); |
|
133 if (!mIcon.isNull()) { |
|
134 // This must be done before the setIcon() call below due to the |
|
135 // possibility of detaching. Doing it afterwards would lead to |
|
136 // 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. |
|
138 HbIconPrivate::d_ptr_detached(&mIcon)->setThemedColor(mThemedColor); |
|
139 } |
127 const QRectF boundingRect = q->rect(); |
140 const QRectF boundingRect = q->rect(); |
128 if (!boundingRect.size().isEmpty()) { |
141 if (!boundingRect.size().isEmpty()) { |
129 mIconRect = boundingRect; |
142 mIconRect = boundingRect; |
130 mIcon.setSize(mIconRect.size()); |
143 mIcon.setSize(mIconRect.size()); |
131 mAnimator.setIcon(mIcon); |
144 mAnimator.setIcon(mIcon); |
132 q->update(); |
145 q->update(); |
133 } |
146 } |
134 } |
147 } |
135 |
148 |
|
149 void HbIconItemPrivate::updateIconParams() |
|
150 { |
|
151 Q_Q(HbIconItem); |
|
152 if (mIconRect.isValid()) { |
|
153 if (!mIcon.isNull()) { |
|
154 HbIconPrivate::d_ptr_detached(&mIcon)->setThemedColor(mThemedColor); |
|
155 } |
|
156 mAnimator.setIcon(mIcon); |
|
157 } |
|
158 q->update(); |
|
159 } |
|
160 |
|
161 void HbIconItemPrivate::recalculateBoundingRect() const |
|
162 { |
|
163 Q_Q(const HbIconItem); |
|
164 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; |
|
170 } |
|
171 |
|
172 void HbIconItemPrivate::setThemedColor(const QColor &color) |
|
173 { |
|
174 mThemedColor = color; |
|
175 updateIconItem(); |
|
176 } |
|
177 |
136 /*! |
178 /*! |
137 Constructs a new HbIconItem with \a iconName and \a parent. |
179 Constructs a new HbIconItem with \a iconName and \a parent. |
138 \param iconName the name of the icon. |
180 \param iconName the name of the icon. |
139 See HbIcon for a description of how the icon name and the icon image filename are related. |
181 See HbIcon for a description of how the icon name and the icon image filename are related. |
140 \param parent the graphics item parent of this icon. |
182 \param parent the graphics item parent of this icon. |
141 */ |
183 */ |
142 HbIconItem::HbIconItem(const QString &iconName, QGraphicsItem *parent) : |
184 HbIconItem::HbIconItem(const QString &iconName, QGraphicsItem *parent) : |
143 HbWidgetBase(*new HbIconItemPrivate(iconName), parent) |
185 HbWidgetBase(*new HbIconItemPrivate(iconName), parent) |
144 { |
186 { |
145 Q_D( HbIconItem ); |
187 Q_D(HbIconItem); |
146 d->q_ptr = this; |
188 d->q_ptr = this; |
147 // Set this graphics item to be updated on icon animations |
189 // Set this graphics item to be updated on icon animations |
148 d->mAnimator.setGraphicsItem(this); |
190 d->mAnimator.setGraphicsItem(this); |
149 HbOogmWatcher::instance()->registerIconItem(this); |
191 HbOogmWatcher::instance()->registerIconItem(this); |
150 } |
192 } |
169 \param parent the graphics item parent of this HbIconItem. |
211 \param parent the graphics item parent of this HbIconItem. |
170 */ |
212 */ |
171 HbIconItem::HbIconItem(QGraphicsItem *parent) : |
213 HbIconItem::HbIconItem(QGraphicsItem *parent) : |
172 HbWidgetBase(*new HbIconItemPrivate(QString()), parent) |
214 HbWidgetBase(*new HbIconItemPrivate(QString()), parent) |
173 { |
215 { |
174 Q_D( HbIconItem ); |
216 Q_D(HbIconItem); |
175 d->q_ptr = this; |
217 d->q_ptr = this; |
176 // Set this graphics item to be updated on icon animations |
218 // Set this graphics item to be updated on icon animations |
177 d->mAnimator.setGraphicsItem(this); |
219 d->mAnimator.setGraphicsItem(this); |
178 HbOogmWatcher::instance()->registerIconItem(this); |
220 HbOogmWatcher::instance()->registerIconItem(this); |
179 } |
221 } |
180 |
222 |
181 |
223 |
182 /*! |
224 /*! |
183 \internal |
225 \internal |
184 */ |
226 */ |
185 HbIconItem::HbIconItem(HbIconItemPrivate &dd, QGraphicsItem * parent) : |
227 HbIconItem::HbIconItem(HbIconItemPrivate &dd, QGraphicsItem *parent) : |
186 HbWidgetBase(dd, parent) |
228 HbWidgetBase(dd, parent) |
187 { |
229 { |
188 // Set this graphics item to be updated on icon animations |
230 // Set this graphics item to be updated on icon animations |
189 dd.mAnimator.setGraphicsItem(this); |
231 dd.mAnimator.setGraphicsItem(this); |
190 HbOogmWatcher::instance()->registerIconItem(this); |
232 HbOogmWatcher::instance()->registerIconItem(this); |
193 /*! |
235 /*! |
194 Destructor. |
236 Destructor. |
195 */ |
237 */ |
196 HbIconItem::~HbIconItem() |
238 HbIconItem::~HbIconItem() |
197 { |
239 { |
198 HbOogmWatcher::instance()->unregisterIconItem(this); |
240 HbOogmWatcher *w = HbOogmWatcher::instance(); |
|
241 if (w) { |
|
242 w->unregisterIconItem(this); |
|
243 } |
199 } |
244 } |
200 |
245 |
201 /*! |
246 /*! |
202 \return the HbIcon associated with this HbIconItem instance. |
247 \return the HbIcon associated with this HbIconItem instance. |
203 |
248 |
204 \sa setIcon |
249 \sa setIcon |
205 */ |
250 */ |
206 HbIcon HbIconItem::icon() const |
251 HbIcon HbIconItem::icon() const |
207 { |
252 { |
208 Q_D(const HbIconItem ); |
253 Q_D(const HbIconItem); |
209 return d->mIcon; |
254 return d->mIcon; |
210 } |
255 } |
211 |
256 |
212 /*! |
257 /*! |
213 Sets the HbIcon instance associated with this HbIconItem. |
258 Sets the HbIcon instance associated with this HbIconItem. |
214 |
259 |
|
260 Calling any function on \a icon after this one may not have any effect on the |
|
261 icon displayed by the HbIconItem. Use the setters in HbIconItem instead. Of course |
|
262 the settings set on \a icon before calling setIcon() will all be taken into account. |
|
263 |
|
264 The icon-specific parameters (flags, color, mirroring mode) set via the HbIconItem |
|
265 setters before are lost as this function causes the entire underlying icon to be |
|
266 replaced with a new one. |
|
267 |
215 \param icon the HbIcon instance that this HbIconItem displays. |
268 \param icon the HbIcon instance that this HbIconItem displays. |
216 |
269 |
217 When \a takeIconSettings is false, the following settings are not |
270 \sa icon() |
218 taken (ignored) from \a icon: flags, color, mirroring mode. Instead, |
271 */ |
219 the previous values set via HbIconItem's setters are used. |
272 void HbIconItem::setIcon(const HbIcon &icon, bool reserved) |
220 |
273 { |
221 \sa icon |
274 Q_UNUSED(reserved); |
222 */ |
275 Q_D(HbIconItem); |
223 void HbIconItem::setIcon(const HbIcon &icon, bool takeIconSettings) |
|
224 { |
|
225 Q_D(HbIconItem ); |
|
226 if (d->mIcon != icon) { |
276 if (d->mIcon != icon) { |
227 if (takeIconSettings) { |
277 d->mIcon = icon; |
228 d->mIcon = icon; |
|
229 } else { |
|
230 // Must preserve settings like flags, colors, etc. In this case the |
|
231 // settings made previously through HbIconItem take precedence over the |
|
232 // newly set HbIcon's own settings. |
|
233 HbIcon::Flags prevFlags = d->mIcon.flags(); |
|
234 QColor prevColor = d->mIcon.color(); |
|
235 HbIcon::MirroringMode prevMirroringMode = d->mIcon.mirroringMode(); |
|
236 d->mIcon = icon; |
|
237 d->mIcon.setFlags(prevFlags); |
|
238 d->mIcon.setColor(prevColor); |
|
239 d->mIcon.setMirroringMode(prevMirroringMode); |
|
240 } |
|
241 d->updateIconItem(); |
278 d->updateIconItem(); |
242 } |
279 } |
243 } |
280 } |
244 |
281 |
245 /*! |
282 /*! |
411 \sa HbIcon::setIconName() |
445 \sa HbIcon::setIconName() |
412 \sa iconName |
446 \sa iconName |
413 */ |
447 */ |
414 void HbIconItem::setIconName(const QString &iconName, QIcon::Mode mode, QIcon::State state) |
448 void HbIconItem::setIconName(const QString &iconName, QIcon::Mode mode, QIcon::State state) |
415 { |
449 { |
416 Q_D( HbIconItem ); |
450 Q_D(HbIconItem); |
417 d->mIcon.setIconName(iconName, mode, state); |
451 d->mIcon.setIconName(iconName, mode, state); |
418 d->updateIconItem(); |
452 d->updateIconItem(); |
419 } |
453 } |
420 |
454 |
421 /*! |
455 /*! |
422 Sets the new icon color for the HbIconItem. Note that the color is just |
456 Sets the new icon color for the HbIconItem. Note that the color |
423 stored but not actually used if the HbIcon::Colorized flag is not set and the |
457 is just stored but not actually used if the HbIcon::Colorized flag |
424 icon is not a mono icon from the theme. |
458 is not set and the icon is not a mono icon from the theme. |
425 |
459 |
426 \param color to be set. |
460 See HbIcon::setColor() for more information on colorization of mono |
427 \sa HbIconItem::color(), HbIcon::setColor() |
461 icons in widgets. |
|
462 |
|
463 \param color to be set. |
|
464 \sa HbIconItem::color(), HbIcon::setColor() |
428 */ |
465 */ |
429 void HbIconItem::setColor(const QColor &color) |
466 void HbIconItem::setColor(const QColor &color) |
430 { |
467 { |
431 Q_D( HbIconItem ); |
468 Q_D(HbIconItem); |
432 if (d->mIcon.color() != color) { |
469 if (d->mIcon.color() != color) { |
433 d->mIcon.setColor(color); |
470 d->mIcon.setColor(color); |
434 if (d->mIconRect.isValid()) |
471 d->updateIconParams(); |
435 d->mAnimator.setIcon(d->mIcon); |
|
436 update(); |
|
437 } |
472 } |
438 } |
473 } |
439 |
474 |
440 /*! |
475 /*! |
441 * Returns the color of the HbIconItem. |
476 * Returns the color of the HbIconItem. |
442 * \sa HbIconItem::setColor() |
477 * \sa HbIconItem::setColor() |
443 */ |
478 */ |
444 QColor HbIconItem::color() const |
479 QColor HbIconItem::color() const |
445 { |
480 { |
446 Q_D(const HbIconItem ); |
481 Q_D(const HbIconItem); |
447 return d->mIcon.color(); |
482 return d->mIcon.color(); |
448 } |
483 } |
449 |
484 |
450 /*! |
485 /*! |
451 Returns the default size of the icon. |
486 Returns the default size of the icon. |
452 |
487 |
453 For raster images this is the original size of the image. |
488 For raster images this is the original size of the image. |
454 |
489 |
455 \sa HbIcon::defaultSize() |
490 \sa HbIcon::defaultSize() |
456 */ |
491 */ |
457 QSizeF HbIconItem::defaultSize() const |
492 QSizeF HbIconItem::defaultSize() const |
458 { |
493 { |
459 Q_D(const HbIconItem ); |
494 Q_D(const HbIconItem); |
460 return d->mIcon.defaultSize(); |
495 return d->mIcon.defaultSize(); |
461 } |
496 } |
462 |
497 |
463 /*! |
498 /*! |
464 \return the size of this HbIconItem |
499 \return the size of this HbIconItem |
475 |
510 |
476 \sa setAspectRatioMode |
511 \sa setAspectRatioMode |
477 */ |
512 */ |
478 Qt::AspectRatioMode HbIconItem::aspectRatioMode() const |
513 Qt::AspectRatioMode HbIconItem::aspectRatioMode() const |
479 { |
514 { |
480 Q_D(const HbIconItem ); |
515 Q_D(const HbIconItem); |
481 return d->mAspectRatioMode; |
516 return d->mAspectRatioMode; |
482 } |
517 } |
483 |
518 |
484 /*! |
519 /*! |
485 \return the icon's alignment. |
520 \return the icon's alignment. |
486 |
521 |
487 \sa setAlignment |
522 \sa setAlignment |
488 */ |
523 */ |
489 Qt::Alignment HbIconItem::alignment() const |
524 Qt::Alignment HbIconItem::alignment() const |
490 { |
525 { |
491 Q_D(const HbIconItem ); |
526 Q_D(const HbIconItem); |
492 return d->mAlignment; |
527 return d->mAlignment; |
493 } |
528 } |
494 |
529 |
495 /*! |
530 /*! |
496 \return the icon's mode. |
531 \return the icon's mode. |
497 |
532 |
498 \sa setMode |
533 \sa setMode |
499 */ |
534 */ |
500 QIcon::Mode HbIconItem::mode() const |
535 QIcon::Mode HbIconItem::mode() const |
501 { |
536 { |
502 Q_D(const HbIconItem ); |
537 Q_D(const HbIconItem); |
503 return d->mMode; |
538 return d->mMode; |
504 } |
539 } |
505 |
540 |
506 /*! |
541 /*! |
507 \return the icon's state. |
542 \return the icon's state. |
508 |
543 |
509 \sa setState |
544 \sa setState |
510 */ |
545 */ |
511 QIcon::State HbIconItem::state() const |
546 QIcon::State HbIconItem::state() const |
512 { |
547 { |
513 Q_D(const HbIconItem ); |
548 Q_D(const HbIconItem); |
514 return d->mState; |
549 return d->mState; |
515 } |
550 } |
516 |
551 |
517 /*! |
552 /*! |
518 \return the icon's name. |
553 \return the icon's name. |
592 void HbIconItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) |
627 void HbIconItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) |
593 { |
628 { |
594 Q_UNUSED(widget) |
629 Q_UNUSED(widget) |
595 Q_UNUSED(option) |
630 Q_UNUSED(option) |
596 Q_D(HbIconItem); |
631 Q_D(HbIconItem); |
597 const QRectF rect(boundingRect()); |
632 if (d->mClearCachedRect){ |
598 if(!rect.isEmpty()){ |
633 d->recalculateBoundingRect(); |
599 if (d->mIconRect != rect) { |
634 } |
600 d->mIconRect = rect; |
635 if (!d->mBoundingRect.isEmpty()){ |
|
636 if (d->mIconRect != d->mBoundingRect) { |
|
637 d->mIconRect = d->mBoundingRect; |
|
638 if (!d->mIcon.isNull()) { |
|
639 HbIconPrivate::d_ptr_detached(&d->mIcon)->setThemedColor(d->mThemedColor); |
|
640 } |
601 d->mIcon.setSize(d->mIconRect.size()); |
641 d->mIcon.setSize(d->mIconRect.size()); |
602 d->mAnimator.setIcon(d->mIcon); |
642 d->mAnimator.setIcon(d->mIcon); |
603 } |
643 } |
604 painter->fillRect(rect, d->mBrush); |
644 if (d->mBrush != Qt::NoBrush) { |
605 d->mAnimator.paint(painter, rect, |
645 painter->fillRect(d->mBoundingRect, d->mBrush); |
|
646 } |
|
647 d->mAnimator.paint(painter, d->mBoundingRect, |
606 d->mAspectRatioMode, d->mAlignment, |
648 d->mAspectRatioMode, d->mAlignment, |
607 d->mMode, d->mState); |
649 d->mMode, d->mState); |
608 } |
650 } |
609 if (HbIconItemPrivate::outlinesEnabled) { |
651 if (HbIconItemPrivate::outlinesEnabled) { |
610 painter->setBrush(QBrush(QColor(0, 255, 0, 50))); |
652 painter->setBrush(QBrush(QColor(0, 255, 0, 50))); |
611 painter->drawRect(contentsRect()); |
653 painter->drawRect(contentsRect()); |
612 } |
654 } |
|
655 } |
|
656 |
|
657 /*! |
|
658 \reimp |
|
659 */ |
|
660 QRectF HbIconItem::boundingRect() const |
|
661 |
|
662 { |
|
663 Q_D(const HbIconItem); |
|
664 if (d->mClearCachedRect) { |
|
665 d->recalculateBoundingRect(); |
|
666 } |
|
667 return d->mAdjustedRect; |
|
668 } |
|
669 |
|
670 /*! |
|
671 \reimp |
|
672 */ |
|
673 void HbIconItem::setGeometry(const QRectF& rect) |
|
674 { |
|
675 Q_D(HbIconItem); |
|
676 d->mClearCachedRect = true; |
|
677 HbWidgetBase::setGeometry(rect); |
613 } |
678 } |
614 |
679 |
615 /*! |
680 /*! |
616 \reimp |
681 \reimp |
617 */ |
682 */ |