87 |
91 |
88 class HbLabelPrivate: public HbWidgetPrivate { |
92 class HbLabelPrivate: public HbWidgetPrivate { |
89 Q_DECLARE_PUBLIC(HbLabel) |
93 Q_DECLARE_PUBLIC(HbLabel) |
90 |
94 |
91 public: |
95 public: |
92 HbLabelPrivate (); |
96 HbLabelPrivate(); |
93 ~HbLabelPrivate (); |
97 ~HbLabelPrivate(); |
|
98 |
|
99 void init(); |
|
100 void clear(); |
94 |
101 |
95 void clearAll(); |
102 void clearAll(); |
96 |
103 |
97 void setText(const QString &text, HbStyle::Primitive primitiveId); |
104 void setText(const QString &text, HbStyle::PrimitiveType primitiveId); |
98 void setIcon(const HbIcon &icon); |
105 void setIcon(const HbIcon &icon); |
99 |
106 |
100 void updatePrimitives (); |
107 void updatePrimitives (); |
101 void createPrimitives (); |
108 void createPrimitives (); |
102 |
109 |
103 //shared between icon and text |
110 //shared between icon and text |
104 Qt::Alignment mAlignment; |
111 HbStyleValue<Qt::Alignment> mAlignment; |
105 |
112 |
106 // text section |
113 // text section |
107 QString mText; |
114 HbStyleValue<QString> mText; |
108 Qt::TextElideMode mElideMode; |
115 HbStyleValue<Qt::TextElideMode> mElideMode; |
109 Hb::TextWrapping mTextWrapping; |
116 HbStyleValue<Hb::TextWrapping> mTextWrapping; |
110 QColor mColor; |
117 HbStyleValue<QColor> mColor; |
|
118 HbStyleValue<int> mMaxLines; |
111 |
119 |
112 // icon section |
120 // icon section |
113 HbIcon mIcon; |
121 HbStyleValue<HbIcon> mIcon; |
114 Qt::AspectRatioMode mAspectRatioMode; |
122 HbStyleValue<Qt::AspectRatioMode> mAspectRatioMode; |
115 |
123 |
116 // primitive handling |
124 // primitive handling |
117 QGraphicsItem *mPrimitiveItem; |
125 QGraphicsObject *mPrimitiveItem; |
118 HbStyle::Primitive mActivePrimitive; |
126 |
|
127 HbStyle::PrimitiveType mActivePrimitive; |
119 }; |
128 }; |
120 |
129 |
121 HbLabelPrivate::HbLabelPrivate() : |
130 HbLabelPrivate::HbLabelPrivate() : |
122 HbWidgetPrivate(), |
131 HbWidgetPrivate(), |
123 mAlignment(Qt::AlignLeft | Qt::AlignVCenter), |
|
124 mText(QString()), |
|
125 mElideMode(Qt::ElideRight), |
|
126 mTextWrapping(Hb::TextNoWrap), |
|
127 mAspectRatioMode(Qt::KeepAspectRatio), |
|
128 mPrimitiveItem(0), |
132 mPrimitiveItem(0), |
129 mActivePrimitive(HbStyle::P_None) |
133 mActivePrimitive(HbStyle::PT_None) |
130 { |
134 { |
131 } |
135 } |
|
136 |
|
137 void HbLabelPrivate::init() |
|
138 { |
|
139 Q_Q(HbLabel); |
|
140 q->setFlag(QGraphicsItem::ItemClipsChildrenToShape, true); |
|
141 } |
|
142 |
|
143 void HbLabelPrivate::clear() |
|
144 { |
|
145 // no implementation needed |
|
146 } |
|
147 |
132 |
148 |
133 void HbLabelPrivate::clearAll() |
149 void HbLabelPrivate::clearAll() |
134 { |
150 { |
135 if (mPrimitiveItem) { |
151 if (mPrimitiveItem) { |
136 delete mPrimitiveItem; |
152 delete mPrimitiveItem; |
137 mPrimitiveItem = 0; |
153 mPrimitiveItem = 0; |
138 mActivePrimitive = HbStyle::P_None; |
154 mActivePrimitive = HbStyle::PT_None; |
139 } |
155 } |
140 |
156 |
141 mText.clear(); |
157 mText.clear(); |
142 mIcon.clear(); |
158 mIcon.clear(); |
143 } |
159 } |
144 |
160 |
145 void HbLabelPrivate::setText(const QString &text, HbStyle::Primitive primitiveId) |
161 void HbLabelPrivate::setText(const QString &text, HbStyle::PrimitiveType primitiveId) |
146 { |
162 { |
147 Q_Q(HbLabel); |
163 Q_Q(HbLabel); |
148 |
164 |
149 if (text.isNull()) { |
165 if (text.isNull()) { |
150 clearAll(); |
166 clearAll(); |
199 { |
215 { |
200 Q_Q(HbLabel); |
216 Q_Q(HbLabel); |
201 |
217 |
202 Q_ASSERT(mPrimitiveItem==0); |
218 Q_ASSERT(mPrimitiveItem==0); |
203 |
219 |
204 if (mActivePrimitive != HbStyle::P_None) { |
220 switch (mActivePrimitive) { |
205 mPrimitiveItem = q->style()->createPrimitive(mActivePrimitive, q); |
221 case HbStyle::PT_None: |
|
222 break; |
|
223 |
|
224 case HbStyle::PT_IconItem: |
|
225 mPrimitiveItem = q->style()->createPrimitive(mActivePrimitive, QString("icon"),q); |
|
226 break; |
|
227 |
|
228 case HbStyle::PT_TextItem: // no break |
|
229 case HbStyle::PT_RichTextItem: |
|
230 mPrimitiveItem = q->style()->createPrimitive(mActivePrimitive, QString("text"),q); |
|
231 break; |
|
232 |
|
233 default: |
|
234 Q_ASSERT(0); |
206 } |
235 } |
207 } |
236 } |
208 |
237 |
209 void HbLabelPrivate::updatePrimitives() |
238 void HbLabelPrivate::updatePrimitives() |
210 { |
239 { |
211 Q_Q(HbLabel); |
240 Q_Q(HbLabel); |
212 |
241 |
213 if (mActivePrimitive != HbStyle::P_None) { |
242 if (mActivePrimitive != HbStyle::PT_None) { |
214 Q_ASSERT(mActivePrimitive == HbStyle::P_Label_icon |
243 Q_ASSERT(mActivePrimitive == HbStyle::PT_IconItem |
215 || mActivePrimitive == HbStyle::P_Label_richtext |
244 || mActivePrimitive == HbStyle::PT_RichTextItem |
216 || mActivePrimitive == HbStyle::P_Label_text); |
245 || mActivePrimitive == HbStyle::PT_TextItem); |
217 |
246 |
218 HbStyleOptionLabel option; |
247 switch (mActivePrimitive) { |
219 q->initStyleOption(&option); |
248 case HbStyle::PT_IconItem: { |
220 q->style()->updatePrimitive(mPrimitiveItem, mActivePrimitive, &option); |
249 HbStyleIconPrimitiveData data; |
|
250 // set common data: |
|
251 data.alignment = mAlignment; |
|
252 |
|
253 // set icon data: |
|
254 data.aspectRatioMode = mAspectRatioMode; |
|
255 data.icon = mIcon; |
|
256 |
|
257 q->style()->updatePrimitive(mPrimitiveItem, |
|
258 &data); |
|
259 } |
|
260 break; |
|
261 case HbStyle::PT_TextItem: { |
|
262 HbStyleTextPrimitiveData data; |
|
263 |
|
264 // set common data: |
|
265 data.alignment = mAlignment; |
|
266 |
|
267 // set text common data: |
|
268 data.text = mText; |
|
269 data.textColor = mColor; |
|
270 data.textWrapping = mTextWrapping; |
|
271 |
|
272 // plain text specyfic: |
|
273 data.elideMode = mElideMode; |
|
274 data.maximumLines = mMaxLines; |
|
275 |
|
276 q->style()->updatePrimitive(mPrimitiveItem, |
|
277 &data); |
|
278 } |
|
279 break; |
|
280 |
|
281 case HbStyle::PT_RichTextItem: { |
|
282 HbStyleRichTextPrimitiveData data; |
|
283 |
|
284 // set common data: |
|
285 data.alignment = mAlignment; |
|
286 |
|
287 // set text common data: |
|
288 data.text = mText; |
|
289 data.defaultColor = mColor; |
|
290 data.textWrappingMode = mTextWrapping; |
|
291 |
|
292 q->style()->updatePrimitive(mPrimitiveItem, |
|
293 &data); |
|
294 } |
|
295 break; |
|
296 |
|
297 case 0: { |
|
298 } |
|
299 break; |
|
300 |
|
301 default: |
|
302 Q_ASSERT(0); |
|
303 } |
221 } |
304 } |
222 } |
305 } |
223 |
306 |
224 /*! |
307 /*! |
225 Constructs the label with a given \a parent. |
308 Constructs the label with a given \a parent. |
226 \param parent - the parent graphics item. |
309 \param parent - the parent graphics item. |
227 */ |
310 */ |
228 HbLabel::HbLabel(QGraphicsItem *parent) : |
311 HbLabel::HbLabel(QGraphicsItem *parent) : |
229 HbWidget(*new HbLabelPrivate, parent) |
312 HbWidget(*new HbLabelPrivate, parent) |
230 { |
313 { |
231 setFlag(QGraphicsItem::ItemClipsChildrenToShape, true); |
314 Q_D(HbLabel); |
|
315 d->init(); |
232 } |
316 } |
233 |
317 |
234 /*! |
318 /*! |
235 \internal |
319 \internal |
236 */ |
320 */ |
237 HbLabel::HbLabel(HbLabelPrivate &dd, QGraphicsItem * parent) : |
321 HbLabel::HbLabel(HbLabelPrivate &dd, QGraphicsItem * parent) : |
238 HbWidget(dd, parent) |
322 HbWidget(dd, parent) |
239 { |
323 { |
240 setFlag(QGraphicsItem::ItemClipsChildrenToShape, true); |
324 Q_D(HbLabel); |
|
325 d->init(); |
241 } |
326 } |
242 |
327 |
243 /*! |
328 /*! |
244 Constructs the label with a given \a text and \a parent. This constructor is a convenience for |
329 Constructs the label with a given \a text and \a parent. This constructor is a convenience for |
245 common use case to have a label with plain text content. Using this contructor you do not need |
330 common use case to have a label with plain text content. Using this contructor you do not need |
307 void HbLabel::setElideMode (Qt::TextElideMode elideMode) |
394 void HbLabel::setElideMode (Qt::TextElideMode elideMode) |
308 { |
395 { |
309 Q_D(HbLabel); |
396 Q_D(HbLabel); |
310 if (elideMode != d->mElideMode) { |
397 if (elideMode != d->mElideMode) { |
311 d->mElideMode = elideMode; |
398 d->mElideMode = elideMode; |
312 if (!d->mText.isNull()) { |
399 if (d->mText.isSet()) { |
313 updatePrimitives(); |
400 updatePrimitives(); |
314 } |
401 } |
315 } |
402 } |
316 } |
403 } |
317 |
404 |
318 /*! |
405 /*! |
319 Default elide mode is Qt::ElideRight. |
406 By default this method returns invalid value to indicate that |
|
407 eliding is controlled by CSS (setElideMode was not used). |
|
408 In CSS elide by default is set to Qt::ElideRight. |
|
409 |
320 \return the elide mode of the text. |
410 \return the elide mode of the text. |
321 |
411 |
322 \sa HbLabel::setElideMode() |
412 \sa HbLabel::setElideMode() |
323 */ |
413 */ |
324 Qt::TextElideMode HbLabel::elideMode() const |
414 Qt::TextElideMode HbLabel::elideMode() const |
325 { |
415 { |
326 Q_D(const HbLabel); |
416 Q_D(const HbLabel); |
327 return d->mElideMode; |
417 if (d->mElideMode.isSet()) { |
|
418 return d->mElideMode; |
|
419 } else { |
|
420 return (Qt::TextElideMode)-1; |
|
421 } |
328 } |
422 } |
329 |
423 |
330 /*! |
424 /*! |
331 Sets the text wrapping mode to \a mode. |
425 Sets the text wrapping mode to \a mode. |
332 \param mode - wrapping mode |
426 \param mode - wrapping mode |
336 void HbLabel::setTextWrapping(Hb::TextWrapping mode) |
430 void HbLabel::setTextWrapping(Hb::TextWrapping mode) |
337 { |
431 { |
338 Q_D(HbLabel); |
432 Q_D(HbLabel); |
339 if (d->mTextWrapping != mode) { |
433 if (d->mTextWrapping != mode) { |
340 d->mTextWrapping = mode; |
434 d->mTextWrapping = mode; |
341 if (!d->mText.isNull()) { |
435 if (d->mText.isSet()) { |
342 updatePrimitives(); |
436 updatePrimitives(); |
343 } |
437 } |
344 } |
438 } |
345 } |
439 } |
346 |
440 |
347 /*! |
441 /*! |
348 \return the label's current text wrapping mode. |
442 \return the label's current text wrapping mode. |
349 Default value is NoWrap. |
443 Default value is Hb::TextNoWrap. |
350 |
444 |
351 \sa setTextWrapping() |
445 \sa setTextWrapping() |
352 */ |
446 */ |
353 Hb::TextWrapping HbLabel::textWrapping() const |
447 Hb::TextWrapping HbLabel::textWrapping() const |
354 { |
448 { |
355 Q_D(const HbLabel); |
449 Q_D(const HbLabel); |
356 return d->mTextWrapping; |
450 |
|
451 if (d->mTextWrapping.isSet()) { |
|
452 return d->mTextWrapping; |
|
453 } else { |
|
454 return Hb::TextNoWrap; |
|
455 } |
357 } |
456 } |
358 |
457 |
359 /*! |
458 /*! |
360 Sets the icon displayed by this label. |
459 Sets the icon displayed by this label. |
361 Removes any existing text from the label. |
460 Removes any existing text from the label. |
458 void HbLabel::setAlignment(Qt::Alignment alignment) |
567 void HbLabel::setAlignment(Qt::Alignment alignment) |
459 { |
568 { |
460 Q_D(HbLabel); |
569 Q_D(HbLabel); |
461 if (d->mAlignment != alignment) { |
570 if (d->mAlignment != alignment) { |
462 d->mAlignment = alignment; |
571 d->mAlignment = alignment; |
463 if (d->mActivePrimitive!=HbStyle::P_None) { |
572 if (alignment == 0) { |
|
573 d->mAlignment.clear(); |
|
574 } |
|
575 if (d->mActivePrimitive!=HbStyle::PT_None) { |
464 updatePrimitives(); |
576 updatePrimitives(); |
465 } |
577 } |
466 } |
578 } |
467 } |
579 } |
468 |
580 |
469 /*! |
581 /*! |
470 \return the alignment. Default alignment is 'Qt::AlignLeft | Qt::AlignVCenter' |
582 \return the alignment. Default alignment is '0' indicating that nothing was |
|
583 set (so CSS cotrols alignment). |
471 |
584 |
472 \sa HbLabel::setAlignment() |
585 \sa HbLabel::setAlignment() |
473 */ |
586 */ |
474 Qt::Alignment HbLabel::alignment() const |
587 Qt::Alignment HbLabel::alignment() const |
475 { |
588 { |
476 Q_D(const HbLabel); |
589 Q_D(const HbLabel); |
477 return d->mAlignment; |
590 if (d->mAlignment.isSet()) { |
|
591 return d->mAlignment; |
|
592 } else { |
|
593 return 0; |
|
594 } |
478 } |
595 } |
479 |
596 |
480 /*! |
597 /*! |
481 \return true if both text and icon are empty; otherwise returns false. |
598 \return true if both text and icon are empty; otherwise returns false. |
482 |
599 |
488 \sa clear() |
605 \sa clear() |
489 */ |
606 */ |
490 bool HbLabel::isEmpty() const |
607 bool HbLabel::isEmpty() const |
491 { |
608 { |
492 Q_D(const HbLabel); |
609 Q_D(const HbLabel); |
493 return d->mActivePrimitive == HbStyle::P_None; |
610 return d->mActivePrimitive == HbStyle::PT_None; |
494 } |
611 } |
495 |
612 |
496 /*! |
613 /*! |
497 |
614 |
498 \deprecated HbLabel::primitive(HbStyle::Primitive) |
615 \deprecated HbLabel::primitive(HbStyle::Primitive) |
499 is deprecated. |
616 is deprecated. |
500 |
617 |
501 Returns a pointer to the QGraphicsItem primitive used by this label. |
618 Returns a pointer to the QGraphicsItem primitive used by this label. |
502 \param primitive - the type of graphics primitive required. |
619 \param primitive - the type of graphics primitive required. |
503 HbLabel supports HbStyle::P_Label_text and HbStyle::P_Label_icon. |
620 HbLabel supports HbStyle::PT_TextItem and HbStyle::PT_IconItem. |
504 \return the QGraphicsItem used by the label. It is 0 if type \a primitive not currently in use. |
621 \return the QGraphicsItem used by the label. It is 0 if type \a primitive not currently in use. |
505 It is also 0 if the text or icon object is empty. |
622 It is also 0 if the text or icon object is empty. |
506 |
623 |
507 \reimp |
624 \reimp |
508 |
625 |
509 \sa isEmpty() |
626 \sa isEmpty() |
510 */ |
627 */ |
511 QGraphicsItem * HbLabel::primitive(HbStyle::Primitive primitive) const |
628 QGraphicsItem * HbLabel::primitive(HbStyle::Primitive primitive) const |
512 { |
629 { |
513 Q_D(const HbLabel); |
630 Q_D(const HbLabel); |
514 if (primitive == d->mActivePrimitive) { |
631 switch ((HbStylePrivate::Primitive)primitive) { |
|
632 case HbStylePrivate::P_Label_icon: |
|
633 if (d->mActivePrimitive != HbStyle::PT_IconItem) { |
|
634 break; |
|
635 } |
515 return d->mPrimitiveItem; |
636 return d->mPrimitiveItem; |
516 } |
637 |
517 return HbWidget::primitive(primitive); |
638 case HbStylePrivate::P_Label_text: |
|
639 if (d->mActivePrimitive != HbStyle::PT_TextItem) { |
|
640 break; |
|
641 } |
|
642 return d->mPrimitiveItem; |
|
643 |
|
644 case HbStylePrivate::P_Label_richtext: |
|
645 if (d->mActivePrimitive != HbStyle::PT_RichTextItem) { |
|
646 break; |
|
647 } |
|
648 return d->mPrimitiveItem; |
|
649 |
|
650 default: |
|
651 return HbWidget::primitive(primitive); |
|
652 } |
|
653 return 0; |
518 } |
654 } |
519 |
655 |
520 /*! |
656 /*! |
521 Initializes \a option with the values from this HbLabel. |
657 Initializes \a option with the values from this HbLabel. |
522 HbStyleOptionLabel is used by HbStyle to perform changes in appearance |
658 HbStyleOptionLabel is used by HbStyle to perform changes in appearance |
529 |
665 |
530 HbWidget::initStyleOption(option); |
666 HbWidget::initStyleOption(option); |
531 |
667 |
532 option->alignment = d->mAlignment; |
668 option->alignment = d->mAlignment; |
533 |
669 |
534 if (!d->mText.isNull()) { |
670 if (d->mText.isSet()) { |
535 option->text = d->mText; |
671 option->text = d->mText; |
536 option->elideMode = d->mElideMode; |
672 option->elideMode = d->mElideMode; |
537 option->textWrapMode = d->mTextWrapping; |
673 option->textWrapMode = d->mTextWrapping; |
538 option->color = d->mColor; |
674 option->color = d->mColor; |
539 } |
675 option->maximumLines = d->mMaxLines; |
540 |
676 } |
541 if (!d->mIcon.isNull()) { |
677 |
|
678 if (d->mIcon.isSet()) { |
542 option->icon = d->mIcon; |
679 option->icon = d->mIcon; |
543 option->aspectRatioMode = d->mAspectRatioMode; |
680 option->aspectRatioMode = d->mAspectRatioMode; |
544 } |
681 } |
545 } |
682 } |
546 |
683 |
547 QSizeF HbLabel::sizeHint ( Qt::SizeHint which, const QSizeF & constraint ) const |
684 QSizeF HbLabel::sizeHint ( Qt::SizeHint which, const QSizeF & constraint ) const |
548 { |
685 { |
549 if (isEmpty()) { |
686 if (isEmpty() && which!=Qt::MaximumSize) { |
550 return QSizeF(0,0); |
687 return QSizeF(0,0); |
551 } |
688 } |
552 return HbWidget::sizeHint(which,constraint); |
689 return HbWidget::sizeHint(which,constraint); |
553 } |
690 } |
554 |
691 |
612 Returns color of text or invalid value if theme color is used. |
749 Returns color of text or invalid value if theme color is used. |
613 */ |
750 */ |
614 QColor HbLabel::textColor() const |
751 QColor HbLabel::textColor() const |
615 { |
752 { |
616 Q_D(const HbLabel); |
753 Q_D(const HbLabel); |
617 return d->mColor; |
754 |
|
755 if (d->mColor.isSet()) { |
|
756 return d->mColor; |
|
757 } else { |
|
758 return QColor(); |
|
759 } |
|
760 } |
|
761 |
|
762 /*! |
|
763 If plain text is used (\sa setPlainText) this will set maximum number of lines |
|
764 to be visible in label. |
|
765 Zero or negative value disables the feature. |
|
766 |
|
767 \sa maximumLines() |
|
768 */ |
|
769 void HbLabel::setMaximumLines(int maxLines) |
|
770 { |
|
771 Q_D(HbLabel); |
|
772 |
|
773 maxLines = qMax(maxLines, 0); |
|
774 if (d->mMaxLines != maxLines) { |
|
775 d->mMaxLines = maxLines; |
|
776 if (d->mActivePrimitive == HbStyle::PT_TextItem) { |
|
777 updatePrimitives(); |
|
778 } |
|
779 } |
|
780 } |
|
781 |
|
782 /*! |
|
783 Returns maximum number of lines which can be visible in label when |
|
784 plain text is used (\sa setPlainText). |
|
785 Zero value means that there is no limitation. |
|
786 |
|
787 \sa setMaximumLines(int) |
|
788 */ |
|
789 int HbLabel::maximumLines() const |
|
790 { |
|
791 Q_D(const HbLabel); |
|
792 |
|
793 if (d->mMaxLines.isSet()) { |
|
794 return d->mMaxLines; |
|
795 } else { |
|
796 return 0; |
|
797 } |
618 } |
798 } |
619 |
799 |
620 #include "moc_hblabel.cpp" |
800 #include "moc_hblabel.cpp" |