36 #include "hbwidget_p.h" |
36 #include "hbwidget_p.h" |
37 #include "hbstyleoptionlabel_p.h" |
37 #include "hbstyleoptionlabel_p.h" |
38 #include "hbwidgetbase.h" |
38 #include "hbwidgetbase.h" |
39 #include "hblabel.h" |
39 #include "hblabel.h" |
40 #include "hbstyle_p.h" |
40 #include "hbstyle_p.h" |
|
41 #include "hbstyletextprimitivedata.h" |
|
42 #include "hbstylerichtextprimitivedata.h" |
|
43 #include "hbstyleiconprimitivedata.h" |
41 |
44 |
42 /*! |
45 /*! |
43 @alpha |
46 @alpha |
44 @hbcore |
47 @hbcore |
45 |
48 |
88 |
91 |
89 class HbLabelPrivate: public HbWidgetPrivate { |
92 class HbLabelPrivate: public HbWidgetPrivate { |
90 Q_DECLARE_PUBLIC(HbLabel) |
93 Q_DECLARE_PUBLIC(HbLabel) |
91 |
94 |
92 public: |
95 public: |
93 HbLabelPrivate (); |
96 HbLabelPrivate(); |
94 ~HbLabelPrivate (); |
97 ~HbLabelPrivate(); |
95 |
98 |
96 void clearAll(); |
99 void clearAll(); |
97 |
100 |
98 void setText(const QString &text, HbStylePrivate::Primitive primitiveId); |
101 void setText(const QString &text, HbStyle::PrimitiveType primitiveId); |
99 void setIcon(const HbIcon &icon); |
102 void setIcon(const HbIcon &icon); |
100 |
103 |
101 void updatePrimitives (); |
104 void updatePrimitives (); |
102 void createPrimitives (); |
105 void createPrimitives (); |
103 |
106 |
104 //shared between icon and text |
107 //shared between icon and text |
105 Qt::Alignment mAlignment; |
108 HbStyleValue<Qt::Alignment> mAlignment; |
106 |
109 |
107 // text section |
110 // text section |
108 QString mText; |
111 HbStyleValue<QString> mText; |
109 Qt::TextElideMode mElideMode; |
112 HbStyleValue<Qt::TextElideMode> mElideMode; |
110 Hb::TextWrapping mTextWrapping; |
113 HbStyleValue<Hb::TextWrapping> mTextWrapping; |
111 QColor mColor; |
114 HbStyleValue<QColor> mColor; |
112 int mMaxLines; |
115 HbStyleValue<int> mMaxLines; |
113 |
116 |
114 // icon section |
117 // icon section |
115 HbIcon mIcon; |
118 HbStyleValue<HbIcon> mIcon; |
116 Qt::AspectRatioMode mAspectRatioMode; |
119 HbStyleValue<Qt::AspectRatioMode> mAspectRatioMode; |
117 |
120 |
118 // primitive handling |
121 // primitive handling |
119 QGraphicsItem *mPrimitiveItem; |
122 QGraphicsObject *mPrimitiveItem; |
120 HbStylePrivate::Primitive mActivePrimitive; |
123 |
|
124 HbStyle::PrimitiveType mActivePrimitive; |
121 }; |
125 }; |
122 |
126 |
123 HbLabelPrivate::HbLabelPrivate() : |
127 HbLabelPrivate::HbLabelPrivate() : |
124 HbWidgetPrivate(), |
128 HbWidgetPrivate(), |
125 mAlignment(Qt::AlignLeft | Qt::AlignVCenter), |
|
126 mText(QString()), |
|
127 mElideMode(Qt::ElideRight), |
|
128 mTextWrapping(Hb::TextNoWrap), |
|
129 mMaxLines(0), |
|
130 mAspectRatioMode(Qt::KeepAspectRatio), |
|
131 mPrimitiveItem(0), |
129 mPrimitiveItem(0), |
132 mActivePrimitive(HbStylePrivate::P_None) |
130 mActivePrimitive(HbStyle::PT_None) |
133 { |
131 { |
134 } |
132 } |
135 |
133 |
136 void HbLabelPrivate::clearAll() |
134 void HbLabelPrivate::clearAll() |
137 { |
135 { |
138 if (mPrimitiveItem) { |
136 if (mPrimitiveItem) { |
139 delete mPrimitiveItem; |
137 delete mPrimitiveItem; |
140 mPrimitiveItem = 0; |
138 mPrimitiveItem = 0; |
141 mActivePrimitive = HbStylePrivate::P_None; |
139 mActivePrimitive = HbStyle::PT_None; |
142 } |
140 } |
143 |
141 |
144 mText.clear(); |
142 mText.clear(); |
145 mIcon.clear(); |
143 mIcon.clear(); |
146 } |
144 } |
147 |
145 |
148 void HbLabelPrivate::setText(const QString &text, HbStylePrivate::Primitive primitiveId) |
146 void HbLabelPrivate::setText(const QString &text, HbStyle::PrimitiveType primitiveId) |
149 { |
147 { |
150 Q_Q(HbLabel); |
148 Q_Q(HbLabel); |
151 |
149 |
152 if (text.isNull()) { |
150 if (text.isNull()) { |
153 clearAll(); |
151 clearAll(); |
202 { |
200 { |
203 Q_Q(HbLabel); |
201 Q_Q(HbLabel); |
204 |
202 |
205 Q_ASSERT(mPrimitiveItem==0); |
203 Q_ASSERT(mPrimitiveItem==0); |
206 |
204 |
207 if (mActivePrimitive != HbStylePrivate::P_None) { |
205 switch (mActivePrimitive) { |
208 mPrimitiveItem = HbStylePrivate::createPrimitive(mActivePrimitive, q); |
206 case HbStyle::PT_None: |
|
207 break; |
|
208 |
|
209 case HbStyle::PT_IconItem: |
|
210 mPrimitiveItem = q->style()->createPrimitive(mActivePrimitive, QString("icon"),q); |
|
211 break; |
|
212 |
|
213 case HbStyle::PT_TextItem: // no break |
|
214 case HbStyle::PT_RichTextItem: |
|
215 mPrimitiveItem = q->style()->createPrimitive(mActivePrimitive, QString("text"),q); |
|
216 break; |
|
217 |
|
218 default: |
|
219 Q_ASSERT(0); |
209 } |
220 } |
210 } |
221 } |
211 |
222 |
212 void HbLabelPrivate::updatePrimitives() |
223 void HbLabelPrivate::updatePrimitives() |
213 { |
224 { |
214 Q_Q(HbLabel); |
225 Q_Q(HbLabel); |
215 |
226 |
216 if (mActivePrimitive != HbStylePrivate::P_None) { |
227 if (mActivePrimitive != HbStyle::PT_None) { |
217 Q_ASSERT(mActivePrimitive == HbStylePrivate::P_Label_icon |
228 Q_ASSERT(mActivePrimitive == HbStyle::PT_IconItem |
218 || mActivePrimitive == HbStylePrivate::P_Label_richtext |
229 || mActivePrimitive == HbStyle::PT_RichTextItem |
219 || mActivePrimitive == HbStylePrivate::P_Label_text); |
230 || mActivePrimitive == HbStyle::PT_TextItem); |
220 |
231 |
221 HbStyleOptionLabel option; |
232 switch (mActivePrimitive) { |
222 q->initStyleOption(&option); |
233 case HbStyle::PT_IconItem: { |
223 HbStylePrivate::updatePrimitive(mPrimitiveItem, mActivePrimitive, &option); |
234 HbStyleIconPrimitiveData data; |
|
235 // set common data: |
|
236 data.alignment = mAlignment; |
|
237 |
|
238 // set icon data: |
|
239 data.aspectRatioMode = mAspectRatioMode; |
|
240 data.icon = mIcon; |
|
241 |
|
242 q->style()->updatePrimitive(mPrimitiveItem, |
|
243 &data); |
|
244 } |
|
245 break; |
|
246 case HbStyle::PT_TextItem: { |
|
247 HbStyleTextPrimitiveData data; |
|
248 |
|
249 // set common data: |
|
250 data.alignment = mAlignment; |
|
251 |
|
252 // set text common data: |
|
253 data.text = mText; |
|
254 data.textColor = mColor; |
|
255 data.textWrapping = mTextWrapping; |
|
256 |
|
257 // plain text specyfic: |
|
258 data.elideMode = mElideMode; |
|
259 data.maximumLines = mMaxLines; |
|
260 |
|
261 q->style()->updatePrimitive(mPrimitiveItem, |
|
262 &data); |
|
263 } |
|
264 break; |
|
265 |
|
266 case HbStyle::PT_RichTextItem: { |
|
267 HbStyleRichTextPrimitiveData data; |
|
268 |
|
269 // set common data: |
|
270 data.alignment = mAlignment; |
|
271 |
|
272 // set text common data: |
|
273 data.text = mText; |
|
274 data.defaultColor = mColor; |
|
275 data.textWrappingMode = mTextWrapping; |
|
276 |
|
277 q->style()->updatePrimitive(mPrimitiveItem, |
|
278 &data); |
|
279 } |
|
280 break; |
|
281 |
|
282 case 0: { |
|
283 } |
|
284 break; |
|
285 |
|
286 default: |
|
287 Q_ASSERT(0); |
|
288 } |
224 } |
289 } |
225 } |
290 } |
226 |
291 |
227 /*! |
292 /*! |
228 Constructs the label with a given \a parent. |
293 Constructs the label with a given \a parent. |
325 \sa HbLabel::setElideMode() |
390 \sa HbLabel::setElideMode() |
326 */ |
391 */ |
327 Qt::TextElideMode HbLabel::elideMode() const |
392 Qt::TextElideMode HbLabel::elideMode() const |
328 { |
393 { |
329 Q_D(const HbLabel); |
394 Q_D(const HbLabel); |
330 return d->mElideMode; |
395 if (d->mElideMode.isSet()) { |
|
396 return d->mElideMode; |
|
397 } else { |
|
398 return Qt::ElideRight; |
|
399 } |
331 } |
400 } |
332 |
401 |
333 /*! |
402 /*! |
334 Sets the text wrapping mode to \a mode. |
403 Sets the text wrapping mode to \a mode. |
335 \param mode - wrapping mode |
404 \param mode - wrapping mode |
339 void HbLabel::setTextWrapping(Hb::TextWrapping mode) |
408 void HbLabel::setTextWrapping(Hb::TextWrapping mode) |
340 { |
409 { |
341 Q_D(HbLabel); |
410 Q_D(HbLabel); |
342 if (d->mTextWrapping != mode) { |
411 if (d->mTextWrapping != mode) { |
343 d->mTextWrapping = mode; |
412 d->mTextWrapping = mode; |
344 if (!d->mText.isNull()) { |
413 if (d->mText.isSet()) { |
345 updatePrimitives(); |
414 updatePrimitives(); |
346 } |
415 } |
347 } |
416 } |
348 } |
417 } |
349 |
418 |
350 /*! |
419 /*! |
351 \return the label's current text wrapping mode. |
420 \return the label's current text wrapping mode. |
352 Default value is NoWrap. |
421 Default value is Hb::TextNoWrap. |
353 |
422 |
354 \sa setTextWrapping() |
423 \sa setTextWrapping() |
355 */ |
424 */ |
356 Hb::TextWrapping HbLabel::textWrapping() const |
425 Hb::TextWrapping HbLabel::textWrapping() const |
357 { |
426 { |
358 Q_D(const HbLabel); |
427 Q_D(const HbLabel); |
359 return d->mTextWrapping; |
428 |
|
429 if (d->mTextWrapping.isSet()) { |
|
430 return d->mTextWrapping; |
|
431 } else { |
|
432 return Hb::TextNoWrap; |
|
433 } |
360 } |
434 } |
361 |
435 |
362 /*! |
436 /*! |
363 Sets the icon displayed by this label. |
437 Sets the icon displayed by this label. |
364 Removes any existing text from the label. |
438 Removes any existing text from the label. |
419 \sa setAspectRatio() |
498 \sa setAspectRatio() |
420 */ |
499 */ |
421 Qt::AspectRatioMode HbLabel::aspectRatioMode() const |
500 Qt::AspectRatioMode HbLabel::aspectRatioMode() const |
422 { |
501 { |
423 Q_D(const HbLabel); |
502 Q_D(const HbLabel); |
424 return d->mAspectRatioMode; |
503 |
|
504 if (d->mAspectRatioMode.isSet()) { |
|
505 return d->mAspectRatioMode; |
|
506 } else { |
|
507 return Qt::KeepAspectRatio; |
|
508 } |
425 } |
509 } |
426 |
510 |
427 /*! |
511 /*! |
428 Sets the label contents to plain text containing \a text. Any previous content is cleared. |
512 Sets the label contents to plain text containing \a text. Any previous content is cleared. |
429 Does nothing if the string representation is the same as the current contents of the label. |
513 Does nothing if the string representation is the same as the current contents of the label. |
461 void HbLabel::setAlignment(Qt::Alignment alignment) |
545 void HbLabel::setAlignment(Qt::Alignment alignment) |
462 { |
546 { |
463 Q_D(HbLabel); |
547 Q_D(HbLabel); |
464 if (d->mAlignment != alignment) { |
548 if (d->mAlignment != alignment) { |
465 d->mAlignment = alignment; |
549 d->mAlignment = alignment; |
466 if (d->mActivePrimitive!=HbStylePrivate::P_None) { |
550 if (alignment == 0) { |
|
551 d->mAlignment.clear(); |
|
552 } |
|
553 if (d->mActivePrimitive!=HbStyle::PT_None) { |
467 updatePrimitives(); |
554 updatePrimitives(); |
468 } |
555 } |
469 } |
556 } |
470 } |
557 } |
471 |
558 |
472 /*! |
559 /*! |
473 \return the alignment. Default alignment is 'Qt::AlignLeft | Qt::AlignVCenter' |
560 \return the alignment. Default alignment is '0' indicating that nothing was |
|
561 set (so CSS cotrols alignment). |
474 |
562 |
475 \sa HbLabel::setAlignment() |
563 \sa HbLabel::setAlignment() |
476 */ |
564 */ |
477 Qt::Alignment HbLabel::alignment() const |
565 Qt::Alignment HbLabel::alignment() const |
478 { |
566 { |
479 Q_D(const HbLabel); |
567 Q_D(const HbLabel); |
480 return d->mAlignment; |
568 if (d->mAlignment.isSet()) { |
|
569 return d->mAlignment; |
|
570 } else { |
|
571 return 0; |
|
572 } |
481 } |
573 } |
482 |
574 |
483 /*! |
575 /*! |
484 \return true if both text and icon are empty; otherwise returns false. |
576 \return true if both text and icon are empty; otherwise returns false. |
485 |
577 |
491 \sa clear() |
583 \sa clear() |
492 */ |
584 */ |
493 bool HbLabel::isEmpty() const |
585 bool HbLabel::isEmpty() const |
494 { |
586 { |
495 Q_D(const HbLabel); |
587 Q_D(const HbLabel); |
496 return d->mActivePrimitive == HbStylePrivate::P_None; |
588 return d->mActivePrimitive == HbStyle::PT_None; |
497 } |
589 } |
498 |
590 |
499 /*! |
591 /*! |
500 |
592 |
501 \deprecated HbLabel::primitive(HbStyle::Primitive) |
593 \deprecated HbLabel::primitive(HbStyle::Primitive) |
502 is deprecated. |
594 is deprecated. |
503 |
595 |
504 Returns a pointer to the QGraphicsItem primitive used by this label. |
596 Returns a pointer to the QGraphicsItem primitive used by this label. |
505 \param primitive - the type of graphics primitive required. |
597 \param primitive - the type of graphics primitive required. |
506 HbLabel supports HbStylePrivate::P_Label_text and HbStylePrivate::P_Label_icon. |
598 HbLabel supports HbStyle::PT_TextItem and HbStyle::PT_IconItem. |
507 \return the QGraphicsItem used by the label. It is 0 if type \a primitive not currently in use. |
599 \return the QGraphicsItem used by the label. It is 0 if type \a primitive not currently in use. |
508 It is also 0 if the text or icon object is empty. |
600 It is also 0 if the text or icon object is empty. |
509 |
601 |
510 \reimp |
602 \reimp |
511 |
603 |
512 \sa isEmpty() |
604 \sa isEmpty() |
513 */ |
605 */ |
514 QGraphicsItem * HbLabel::primitive(HbStyle::Primitive primitive) const |
606 QGraphicsItem * HbLabel::primitive(HbStyle::Primitive primitive) const |
515 { |
607 { |
516 Q_D(const HbLabel); |
608 Q_D(const HbLabel); |
517 if ((HbStylePrivate::Primitive)primitive == d->mActivePrimitive) { |
609 switch ((HbStylePrivate::Primitive)primitive) { |
|
610 case HbStylePrivate::P_Label_icon: |
|
611 if (d->mActivePrimitive != HbStyle::PT_IconItem) { |
|
612 break; |
|
613 } |
518 return d->mPrimitiveItem; |
614 return d->mPrimitiveItem; |
519 } |
615 |
520 return HbWidget::primitive(primitive); |
616 case HbStylePrivate::P_Label_text: |
|
617 if (d->mActivePrimitive != HbStyle::PT_TextItem) { |
|
618 break; |
|
619 } |
|
620 return d->mPrimitiveItem; |
|
621 |
|
622 case HbStylePrivate::P_Label_richtext: |
|
623 if (d->mActivePrimitive != HbStyle::PT_RichTextItem) { |
|
624 break; |
|
625 } |
|
626 return d->mPrimitiveItem; |
|
627 |
|
628 default: |
|
629 return HbWidget::primitive(primitive); |
|
630 } |
|
631 return 0; |
521 } |
632 } |
522 |
633 |
523 /*! |
634 /*! |
524 Initializes \a option with the values from this HbLabel. |
635 Initializes \a option with the values from this HbLabel. |
525 HbStyleOptionLabel is used by HbStyle to perform changes in appearance |
636 HbStyleOptionLabel is used by HbStyle to perform changes in appearance |
532 |
643 |
533 HbWidget::initStyleOption(option); |
644 HbWidget::initStyleOption(option); |
534 |
645 |
535 option->alignment = d->mAlignment; |
646 option->alignment = d->mAlignment; |
536 |
647 |
537 if (!d->mText.isNull()) { |
648 if (d->mText.isSet()) { |
538 option->text = d->mText; |
649 option->text = d->mText; |
539 option->elideMode = d->mElideMode; |
650 option->elideMode = d->mElideMode; |
540 option->textWrapMode = d->mTextWrapping; |
651 option->textWrapMode = d->mTextWrapping; |
541 option->color = d->mColor; |
652 option->color = d->mColor; |
542 option->maximumLines = d->mMaxLines; |
653 option->maximumLines = d->mMaxLines; |
543 } |
654 } |
544 |
655 |
545 if (!d->mIcon.isNull()) { |
656 if (d->mIcon.isSet()) { |
546 option->icon = d->mIcon; |
657 option->icon = d->mIcon; |
547 option->aspectRatioMode = d->mAspectRatioMode; |
658 option->aspectRatioMode = d->mAspectRatioMode; |
548 } |
659 } |
549 } |
660 } |
550 |
661 |