59 //Create new icon, set position and size for it |
59 //Create new icon, set position and size for it |
60 HbIconItem *icon = new HbIconItem("qtg_large_phonebook", this); |
60 HbIconItem *icon = new HbIconItem("qtg_large_phonebook", this); |
61 icon->setPos(10,150); |
61 icon->setPos(10,150); |
62 icon->setSize(icon->defaultSize()); |
62 icon->setSize(icon->defaultSize()); |
63 \endcode |
63 \endcode |
64 |
64 |
65 Example of how to add HbIconItem to a layout. |
65 Example of how to add HbIconItem to a layout. |
66 \code |
66 \code |
67 HbButton *button = new HbButton("Button 1"); |
67 HbButton *button = new HbButton("Button 1"); |
68 HbIconItem *icon = new HbIconItem("qtg_large_phonebook"); |
68 HbIconItem *icon = new HbIconItem("qtg_large_phonebook"); |
69 QGraphicsGridLayout *layout = new QGraphicsGridLayout(); |
69 QGraphicsGridLayout *layout = new QGraphicsGridLayout(); |
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) |
116 { |
121 { |
117 q_ptr = 0; |
122 q_ptr = 0; |
118 } |
123 } |
119 |
124 |
120 HbIconItemPrivate::~HbIconItemPrivate () |
125 HbIconItemPrivate::~HbIconItemPrivate() |
121 { |
126 { |
122 } |
127 } |
123 |
128 |
124 void HbIconItemPrivate::updateIconItem() |
129 void HbIconItemPrivate::updateIconItem() |
125 { |
130 { |
126 Q_Q(HbIconItem); |
131 Q_Q(HbIconItem); |
|
132 if (!mIcon.isNull()) { |
|
133 // This must be done before the setIcon() call below due to the |
|
134 // possibility of detaching. Doing it afterwards would lead to |
|
135 // colorization errors as the themed color might potentially be set for |
|
136 // a different icon engine, not for the one that is used in painting. |
|
137 HbIconPrivate::d_ptr_detached(&mIcon)->setThemedColor(mThemedColor); |
|
138 } |
127 const QRectF boundingRect = q->rect(); |
139 const QRectF boundingRect = q->rect(); |
128 if (!boundingRect.size().isEmpty()) { |
140 if (!boundingRect.size().isEmpty()) { |
129 mIconRect = boundingRect; |
141 mIconRect = boundingRect; |
130 mIcon.setSize(mIconRect.size()); |
142 mIcon.setSize(mIconRect.size()); |
131 mAnimator.setIcon(mIcon); |
143 mAnimator.setIcon(mIcon); |
132 q->update(); |
144 q->update(); |
133 } |
145 } |
134 if (!mIcon.isNull() && HbIconPrivate::d_ptr(&mIcon)->themedColor() != mThemedColor) { |
146 } |
135 HbIconPrivate::d_ptr_detached(&mIcon)->setThemedColor(mThemedColor); |
147 |
136 } |
148 void HbIconItemPrivate::updateIconParams() |
|
149 { |
|
150 Q_Q(HbIconItem); |
|
151 if (mIconRect.isValid()) { |
|
152 if (!mIcon.isNull()) { |
|
153 HbIconPrivate::d_ptr_detached(&mIcon)->setThemedColor(mThemedColor); |
|
154 } |
|
155 mAnimator.setIcon(mIcon); |
|
156 } |
|
157 q->update(); |
137 } |
158 } |
138 |
159 |
139 void HbIconItemPrivate::setThemedColor(const QColor &color) |
160 void HbIconItemPrivate::setThemedColor(const QColor &color) |
140 { |
161 { |
141 mThemedColor = color; |
162 mThemedColor = color; |
142 updateIconItem(); |
163 updateIconItem(); |
143 } |
164 } |
144 |
165 |
145 /*! |
166 /*! |
146 Constructs a new HbIconItem with \a iconName and \a parent. |
167 Constructs a new HbIconItem with \a iconName and \a parent. |
147 \param iconName the name of the icon. |
168 \param iconName the name of the icon. |
148 See HbIcon for a description of how the icon name and the icon image filename are related. |
169 See HbIcon for a description of how the icon name and the icon image filename are related. |
149 \param parent the graphics item parent of this icon. |
170 \param parent the graphics item parent of this icon. |
150 */ |
171 */ |
151 HbIconItem::HbIconItem(const QString &iconName, QGraphicsItem *parent) : |
172 HbIconItem::HbIconItem(const QString &iconName, QGraphicsItem *parent) : |
152 HbWidgetBase(*new HbIconItemPrivate(iconName), parent) |
173 HbWidgetBase(*new HbIconItemPrivate(iconName), parent) |
153 { |
174 { |
154 Q_D( HbIconItem ); |
175 Q_D(HbIconItem); |
155 d->q_ptr = this; |
176 d->q_ptr = this; |
156 // Set this graphics item to be updated on icon animations |
177 // Set this graphics item to be updated on icon animations |
157 d->mAnimator.setGraphicsItem(this); |
178 d->mAnimator.setGraphicsItem(this); |
158 HbOogmWatcher::instance()->registerIconItem(this); |
179 HbOogmWatcher::instance()->registerIconItem(this); |
159 } |
180 } |
178 \param parent the graphics item parent of this HbIconItem. |
199 \param parent the graphics item parent of this HbIconItem. |
179 */ |
200 */ |
180 HbIconItem::HbIconItem(QGraphicsItem *parent) : |
201 HbIconItem::HbIconItem(QGraphicsItem *parent) : |
181 HbWidgetBase(*new HbIconItemPrivate(QString()), parent) |
202 HbWidgetBase(*new HbIconItemPrivate(QString()), parent) |
182 { |
203 { |
183 Q_D( HbIconItem ); |
204 Q_D(HbIconItem); |
184 d->q_ptr = this; |
205 d->q_ptr = this; |
185 // Set this graphics item to be updated on icon animations |
206 // Set this graphics item to be updated on icon animations |
186 d->mAnimator.setGraphicsItem(this); |
207 d->mAnimator.setGraphicsItem(this); |
187 HbOogmWatcher::instance()->registerIconItem(this); |
208 HbOogmWatcher::instance()->registerIconItem(this); |
188 } |
209 } |
189 |
210 |
190 |
211 |
191 /*! |
212 /*! |
192 \internal |
213 \internal |
193 */ |
214 */ |
194 HbIconItem::HbIconItem(HbIconItemPrivate &dd, QGraphicsItem * parent) : |
215 HbIconItem::HbIconItem(HbIconItemPrivate &dd, QGraphicsItem *parent) : |
195 HbWidgetBase(dd, parent) |
216 HbWidgetBase(dd, parent) |
196 { |
217 { |
197 // Set this graphics item to be updated on icon animations |
218 // Set this graphics item to be updated on icon animations |
198 dd.mAnimator.setGraphicsItem(this); |
219 dd.mAnimator.setGraphicsItem(this); |
199 HbOogmWatcher::instance()->registerIconItem(this); |
220 HbOogmWatcher::instance()->registerIconItem(this); |
425 \param color to be set. |
446 \param color to be set. |
426 \sa HbIconItem::color(), HbIcon::setColor() |
447 \sa HbIconItem::color(), HbIcon::setColor() |
427 */ |
448 */ |
428 void HbIconItem::setColor(const QColor &color) |
449 void HbIconItem::setColor(const QColor &color) |
429 { |
450 { |
430 Q_D( HbIconItem ); |
451 Q_D(HbIconItem); |
431 if (d->mIcon.color() != color) { |
452 if (d->mIcon.color() != color) { |
432 d->mIcon.setColor(color); |
453 d->mIcon.setColor(color); |
433 if (d->mIconRect.isValid()) |
454 d->updateIconParams(); |
434 d->mAnimator.setIcon(d->mIcon); |
|
435 update(); |
|
436 } |
455 } |
437 } |
456 } |
438 |
457 |
439 /*! |
458 /*! |
440 * Returns the color of the HbIconItem. |
459 * Returns the color of the HbIconItem. |
441 * \sa HbIconItem::setColor() |
460 * \sa HbIconItem::setColor() |
442 */ |
461 */ |
443 QColor HbIconItem::color() const |
462 QColor HbIconItem::color() const |
444 { |
463 { |
445 Q_D(const HbIconItem ); |
464 Q_D(const HbIconItem); |
446 return d->mIcon.color(); |
465 return d->mIcon.color(); |
447 } |
466 } |
448 |
467 |
449 /*! |
468 /*! |
450 Returns the default size of the icon. |
469 Returns the default size of the icon. |
451 |
470 |
452 For raster images this is the original size of the image. |
471 For raster images this is the original size of the image. |
453 |
472 |
454 \sa HbIcon::defaultSize() |
473 \sa HbIcon::defaultSize() |
455 */ |
474 */ |
456 QSizeF HbIconItem::defaultSize() const |
475 QSizeF HbIconItem::defaultSize() const |
457 { |
476 { |
458 Q_D(const HbIconItem ); |
477 Q_D(const HbIconItem); |
459 return d->mIcon.defaultSize(); |
478 return d->mIcon.defaultSize(); |
460 } |
479 } |
461 |
480 |
462 /*! |
481 /*! |
463 \return the size of this HbIconItem |
482 \return the size of this HbIconItem |
474 |
493 |
475 \sa setAspectRatioMode |
494 \sa setAspectRatioMode |
476 */ |
495 */ |
477 Qt::AspectRatioMode HbIconItem::aspectRatioMode() const |
496 Qt::AspectRatioMode HbIconItem::aspectRatioMode() const |
478 { |
497 { |
479 Q_D(const HbIconItem ); |
498 Q_D(const HbIconItem); |
480 return d->mAspectRatioMode; |
499 return d->mAspectRatioMode; |
481 } |
500 } |
482 |
501 |
483 /*! |
502 /*! |
484 \return the icon's alignment. |
503 \return the icon's alignment. |
485 |
504 |
486 \sa setAlignment |
505 \sa setAlignment |
487 */ |
506 */ |
488 Qt::Alignment HbIconItem::alignment() const |
507 Qt::Alignment HbIconItem::alignment() const |
489 { |
508 { |
490 Q_D(const HbIconItem ); |
509 Q_D(const HbIconItem); |
491 return d->mAlignment; |
510 return d->mAlignment; |
492 } |
511 } |
493 |
512 |
494 /*! |
513 /*! |
495 \return the icon's mode. |
514 \return the icon's mode. |
496 |
515 |
497 \sa setMode |
516 \sa setMode |
498 */ |
517 */ |
499 QIcon::Mode HbIconItem::mode() const |
518 QIcon::Mode HbIconItem::mode() const |
500 { |
519 { |
501 Q_D(const HbIconItem ); |
520 Q_D(const HbIconItem); |
502 return d->mMode; |
521 return d->mMode; |
503 } |
522 } |
504 |
523 |
505 /*! |
524 /*! |
506 \return the icon's state. |
525 \return the icon's state. |
507 |
526 |
508 \sa setState |
527 \sa setState |
509 */ |
528 */ |
510 QIcon::State HbIconItem::state() const |
529 QIcon::State HbIconItem::state() const |
511 { |
530 { |
512 Q_D(const HbIconItem ); |
531 Q_D(const HbIconItem); |
513 return d->mState; |
532 return d->mState; |
514 } |
533 } |
515 |
534 |
516 /*! |
535 /*! |
517 \return the icon's name. |
536 \return the icon's name. |
592 { |
611 { |
593 Q_UNUSED(widget) |
612 Q_UNUSED(widget) |
594 Q_UNUSED(option) |
613 Q_UNUSED(option) |
595 Q_D(HbIconItem); |
614 Q_D(HbIconItem); |
596 const QRectF rect(boundingRect()); |
615 const QRectF rect(boundingRect()); |
597 if (!rect.isEmpty()){ |
616 if (!rect.isEmpty()) { |
598 if (d->mIconRect != rect) { |
617 if (d->mIconRect != rect) { |
599 d->mIconRect = rect; |
618 d->mIconRect = rect; |
|
619 if (!d->mIcon.isNull()) { |
|
620 HbIconPrivate::d_ptr_detached(&d->mIcon)->setThemedColor(d->mThemedColor); |
|
621 } |
600 d->mIcon.setSize(d->mIconRect.size()); |
622 d->mIcon.setSize(d->mIconRect.size()); |
601 d->mAnimator.setIcon(d->mIcon); |
623 d->mAnimator.setIcon(d->mIcon); |
602 } |
624 } |
603 painter->fillRect(rect, d->mBrush); |
625 painter->fillRect(rect, d->mBrush); |
604 d->mAnimator.paint(painter, rect, |
626 d->mAnimator.paint(painter, rect, |
605 d->mAspectRatioMode, d->mAlignment, |
627 d->mAspectRatioMode, d->mAlignment, |
606 d->mMode, d->mState); |
628 d->mMode, d->mState); |
607 } |
629 } |
608 if (HbIconItemPrivate::outlinesEnabled) { |
630 if (HbIconItemPrivate::outlinesEnabled) { |
609 painter->setBrush(QBrush(QColor(0, 255, 0, 50))); |
631 painter->setBrush(QBrush(QColor(0, 255, 0, 50))); |
610 painter->drawRect(contentsRect()); |
632 painter->drawRect(contentsRect()); |
611 } |
633 } |
612 } |
634 } |
613 |
635 |
614 /*! |
636 /*! |
615 \reimp |
637 \reimp |