src/hbwidgets/widgets/hblabel.cpp
changeset 21 4633027730f5
parent 7 923ff622b8b9
child 28 b7da29130b0e
equal deleted inserted replaced
7:923ff622b8b9 21:4633027730f5
    35 #include "hbcolorscheme.h"
    35 #include "hbcolorscheme.h"
    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 
    41 
    41 /*!
    42 /*!
    42     @alpha
    43     @alpha
    43     @hbcore
    44     @hbcore
    44 
    45 
    92             HbLabelPrivate ();
    93             HbLabelPrivate ();
    93     ~HbLabelPrivate ();
    94     ~HbLabelPrivate ();
    94 
    95 
    95     void clearAll();
    96     void clearAll();
    96 
    97 
    97     void setText(const QString &text, HbStyle::Primitive primitiveId);
    98     void setText(const QString &text, HbStylePrivate::Primitive primitiveId);
    98     void setIcon(const HbIcon &icon);
    99     void setIcon(const HbIcon &icon);
    99 
   100 
   100     void updatePrimitives ();
   101     void updatePrimitives ();
   101     void createPrimitives ();
   102     void createPrimitives ();
   102 
   103 
   106     // text section
   107     // text section
   107     QString mText;
   108     QString mText;
   108     Qt::TextElideMode mElideMode;
   109     Qt::TextElideMode mElideMode;
   109     Hb::TextWrapping mTextWrapping;
   110     Hb::TextWrapping mTextWrapping;
   110     QColor mColor;
   111     QColor mColor;
       
   112     int mMaxLines;
   111 
   113 
   112     // icon section
   114     // icon section
   113     HbIcon mIcon;
   115     HbIcon mIcon;
   114     Qt::AspectRatioMode mAspectRatioMode;
   116     Qt::AspectRatioMode mAspectRatioMode;
   115 
   117 
   116     // primitive handling
   118     // primitive handling
   117     QGraphicsItem *mPrimitiveItem;
   119     QGraphicsItem *mPrimitiveItem;
   118     HbStyle::Primitive mActivePrimitive;
   120     HbStylePrivate::Primitive mActivePrimitive;
   119 };
   121 };
   120 
   122 
   121 HbLabelPrivate::HbLabelPrivate() :
   123 HbLabelPrivate::HbLabelPrivate() :
   122         HbWidgetPrivate(),
   124         HbWidgetPrivate(),
   123         mAlignment(Qt::AlignLeft | Qt::AlignVCenter),
   125         mAlignment(Qt::AlignLeft | Qt::AlignVCenter),
   124         mText(QString()),
   126         mText(QString()),
   125         mElideMode(Qt::ElideRight),
   127         mElideMode(Qt::ElideRight),
   126         mTextWrapping(Hb::TextNoWrap),
   128         mTextWrapping(Hb::TextNoWrap),
       
   129         mMaxLines(0),
   127         mAspectRatioMode(Qt::KeepAspectRatio),
   130         mAspectRatioMode(Qt::KeepAspectRatio),
   128         mPrimitiveItem(0),
   131         mPrimitiveItem(0),
   129         mActivePrimitive(HbStyle::P_None)
   132         mActivePrimitive(HbStylePrivate::P_None)
   130 {
   133 {
   131 }
   134 }
   132 
   135 
   133 void HbLabelPrivate::clearAll()
   136 void HbLabelPrivate::clearAll()
   134 {
   137 {
   135     if (mPrimitiveItem) {
   138     if (mPrimitiveItem) {
   136         delete mPrimitiveItem;
   139         delete mPrimitiveItem;
   137         mPrimitiveItem = 0;
   140         mPrimitiveItem = 0;
   138         mActivePrimitive = HbStyle::P_None;
   141         mActivePrimitive = HbStylePrivate::P_None;
   139     }
   142     }
   140 
   143 
   141     mText.clear();
   144     mText.clear();
   142     mIcon.clear();
   145     mIcon.clear();
   143 }
   146 }
   144 
   147 
   145 void HbLabelPrivate::setText(const QString &text, HbStyle::Primitive primitiveId)
   148 void HbLabelPrivate::setText(const QString &text, HbStylePrivate::Primitive primitiveId)
   146 {
   149 {
   147     Q_Q(HbLabel);
   150     Q_Q(HbLabel);
   148 
   151 
   149     if (text.isNull()) {
   152     if (text.isNull()) {
   150         clearAll();
   153         clearAll();
   173     if (icon.isNull()) {
   176     if (icon.isNull()) {
   174         clearAll();
   177         clearAll();
   175         return;
   178         return;
   176     }
   179     }
   177 
   180 
   178     if (mActivePrimitive != HbStyle::P_Label_icon) {
   181     if (mActivePrimitive != HbStylePrivate::P_Label_icon) {
   179         clearAll();
   182         clearAll();
   180     }
   183     }
   181 
   184 
   182     if (mIcon != icon) {
   185     if (mIcon != icon) {
   183         mIcon = icon;
   186         mIcon = icon;
   184 
   187 
   185         if (mActivePrimitive != HbStyle::P_Label_icon) {
   188         if (mActivePrimitive != HbStylePrivate::P_Label_icon) {
   186             mActivePrimitive = HbStyle::P_Label_icon;
   189             mActivePrimitive = HbStylePrivate::P_Label_icon;
   187             createPrimitives();
   190             createPrimitives();
   188             q->repolish(); // reconecting new primitive to HbAnchorLayout so it is really needed!
   191             q->repolish(); // reconecting new primitive to HbAnchorLayout so it is really needed!
   189         }
   192         }
   190         q->updatePrimitives();
   193         q->updatePrimitives();
   191     }
   194     }
   199 {
   202 {
   200     Q_Q(HbLabel);
   203     Q_Q(HbLabel);
   201 
   204 
   202     Q_ASSERT(mPrimitiveItem==0);
   205     Q_ASSERT(mPrimitiveItem==0);
   203 
   206 
   204     if (mActivePrimitive != HbStyle::P_None) {
   207     if (mActivePrimitive != HbStylePrivate::P_None) {
   205         mPrimitiveItem = q->style()->createPrimitive(mActivePrimitive, q);
   208         mPrimitiveItem = HbStylePrivate::createPrimitive(mActivePrimitive, q);
   206     }
   209     }
   207 }
   210 }
   208 
   211 
   209 void HbLabelPrivate::updatePrimitives()
   212 void HbLabelPrivate::updatePrimitives()
   210 {
   213 {
   211     Q_Q(HbLabel);
   214     Q_Q(HbLabel);
   212 
   215 
   213     if (mActivePrimitive != HbStyle::P_None) {
   216     if (mActivePrimitive != HbStylePrivate::P_None) {
   214         Q_ASSERT(mActivePrimitive == HbStyle::P_Label_icon
   217         Q_ASSERT(mActivePrimitive == HbStylePrivate::P_Label_icon
   215                  || mActivePrimitive == HbStyle::P_Label_richtext
   218                  || mActivePrimitive == HbStylePrivate::P_Label_richtext
   216                  || mActivePrimitive == HbStyle::P_Label_text);
   219                  || mActivePrimitive == HbStylePrivate::P_Label_text);
   217 
   220 
   218         HbStyleOptionLabel option;
   221         HbStyleOptionLabel option;
   219         q->initStyleOption(&option);
   222         q->initStyleOption(&option);
   220         q->style()->updatePrimitive(mPrimitiveItem, mActivePrimitive, &option);
   223         HbStylePrivate::updatePrimitive(mPrimitiveItem, mActivePrimitive, &option);
   221     }
   224     }
   222 }
   225 }
   223 
   226 
   224 /*!
   227 /*!
   225     Constructs the label with a given \a parent.
   228     Constructs the label with a given \a parent.
   430     \sa setIcon()
   433     \sa setIcon()
   431  */
   434  */
   432 void HbLabel::setPlainText(const QString &text)
   435 void HbLabel::setPlainText(const QString &text)
   433 {
   436 {
   434     Q_D(HbLabel);
   437     Q_D(HbLabel);
   435     d->setText(text, HbStyle::P_Label_text);
   438     d->setText(text, HbStylePrivate::P_Label_text);
   436 }
   439 }
   437 
   440 
   438 /*!
   441 /*!
   439     Sets the label contents to html text containing \a text. Any previous content is cleared.
   442     Sets the label contents to html text containing \a text. Any previous content is cleared.
   440     Does nothing if the string representation is the same as the current contents of the label.
   443     Does nothing if the string representation is the same as the current contents of the label.
   444     \sa setIcon()
   447     \sa setIcon()
   445  */
   448  */
   446 void HbLabel::setHtml(const QString &text)
   449 void HbLabel::setHtml(const QString &text)
   447 {
   450 {
   448     Q_D(HbLabel);
   451     Q_D(HbLabel);
   449     d->setText(text, HbStyle::P_Label_richtext);
   452     d->setText(text, HbStylePrivate::P_Label_richtext);
   450 }
   453 }
   451 
   454 
   452 /*!
   455 /*!
   453     Sets the \a alignment of the label.
   456     Sets the \a alignment of the label.
   454     \param alignment - the new alignment.
   457     \param alignment - the new alignment.
   458 void HbLabel::setAlignment(Qt::Alignment alignment)
   461 void HbLabel::setAlignment(Qt::Alignment alignment)
   459 {
   462 {
   460     Q_D(HbLabel);
   463     Q_D(HbLabel);
   461     if (d->mAlignment != alignment) {
   464     if (d->mAlignment != alignment) {
   462         d->mAlignment = alignment;
   465         d->mAlignment = alignment;
   463         if (d->mActivePrimitive!=HbStyle::P_None) {
   466         if (d->mActivePrimitive!=HbStylePrivate::P_None) {
   464             updatePrimitives();
   467             updatePrimitives();
   465         }
   468         }
   466     }
   469     }
   467 }
   470 }
   468 
   471 
   488     \sa clear()
   491     \sa clear()
   489  */
   492  */
   490 bool HbLabel::isEmpty() const
   493 bool HbLabel::isEmpty() const
   491 {
   494 {
   492     Q_D(const HbLabel);
   495     Q_D(const HbLabel);
   493     return d->mActivePrimitive == HbStyle::P_None;
   496     return d->mActivePrimitive == HbStylePrivate::P_None;
   494 }
   497 }
   495 
   498 
   496 /*!
   499 /*!
   497 
   500 
   498     \deprecated HbLabel::primitive(HbStyle::Primitive)
   501     \deprecated HbLabel::primitive(HbStyle::Primitive)
   499         is deprecated.
   502         is deprecated.
   500 
   503 
   501     Returns a pointer to the QGraphicsItem primitive used by this label.
   504     Returns a pointer to the QGraphicsItem primitive used by this label.
   502     \param primitive - the type of graphics primitive required.
   505     \param primitive - the type of graphics primitive required.
   503     HbLabel supports HbStyle::P_Label_text and HbStyle::P_Label_icon.
   506     HbLabel supports HbStylePrivate::P_Label_text and HbStylePrivate::P_Label_icon.
   504     \return the QGraphicsItem used by the label. It is 0 if type \a primitive not currently in use.
   507     \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.
   508     It is also 0 if the text or icon object is empty.
   506 
   509 
   507     \reimp
   510     \reimp
   508 
   511 
   509     \sa isEmpty()
   512     \sa isEmpty()
   510  */
   513  */
   511 QGraphicsItem * HbLabel::primitive(HbStyle::Primitive primitive) const
   514 QGraphicsItem * HbLabel::primitive(HbStyle::Primitive primitive) const
   512 {
   515 {
   513     Q_D(const HbLabel);
   516     Q_D(const HbLabel);
   514     if (primitive == d->mActivePrimitive) {
   517     if ((HbStylePrivate::Primitive)primitive == d->mActivePrimitive) {
   515         return d->mPrimitiveItem;
   518         return d->mPrimitiveItem;
   516     }
   519     }
   517     return HbWidget::primitive(primitive);
   520     return HbWidget::primitive(primitive);
   518 }
   521 }
   519 
   522 
   534     if (!d->mText.isNull()) {
   537     if (!d->mText.isNull()) {
   535         option->text = d->mText;
   538         option->text = d->mText;
   536         option->elideMode = d->mElideMode;
   539         option->elideMode = d->mElideMode;
   537         option->textWrapMode = d->mTextWrapping;
   540         option->textWrapMode = d->mTextWrapping;
   538         option->color = d->mColor;
   541         option->color = d->mColor;
       
   542         option->maximumLines = d->mMaxLines;
   539     }
   543     }
   540 
   544 
   541     if (!d->mIcon.isNull()) {
   545     if (!d->mIcon.isNull()) {
   542         option->icon = d->mIcon;
   546         option->icon = d->mIcon;
   543         option->aspectRatioMode = d->mAspectRatioMode;
   547         option->aspectRatioMode = d->mAspectRatioMode;
   574     Plain text accessor. Returns empty string if not set.
   578     Plain text accessor. Returns empty string if not set.
   575  */
   579  */
   576 QString HbLabel::plainText() const
   580 QString HbLabel::plainText() const
   577 {
   581 {
   578     Q_D(const HbLabel);
   582     Q_D(const HbLabel);
   579     if (d->mActivePrimitive == HbStyle::P_Label_text) {
   583     if (d->mActivePrimitive == HbStylePrivate::P_Label_text) {
   580         return d->mText;
   584         return d->mText;
   581     }
   585     }
   582     return QString();
   586     return QString();
   583 }
   587 }
   584 
   588 
   586     Rich text text accessor. Returns empty string if not set.
   590     Rich text text accessor. Returns empty string if not set.
   587  */
   591  */
   588 QString HbLabel::html() const
   592 QString HbLabel::html() const
   589 {
   593 {
   590     Q_D(const HbLabel);
   594     Q_D(const HbLabel);
   591     if (d->mActivePrimitive == HbStyle::P_Label_richtext) {
   595     if (d->mActivePrimitive == HbStylePrivate::P_Label_richtext) {
   592         return d->mText;
   596         return d->mText;
   593     }
   597     }
   594     return QString();
   598     return QString();
   595 }
   599 }
   596 
   600 
   615 {
   619 {
   616     Q_D(const HbLabel);
   620     Q_D(const HbLabel);
   617     return d->mColor;
   621     return d->mColor;
   618 }
   622 }
   619 
   623 
       
   624 /*!
       
   625     If plain text is used (\sa setPlainText) this will set maximum number of lines
       
   626     to be visible in label.
       
   627     Zero or negative value disables the feature.
       
   628 
       
   629     \sa maximumLines()
       
   630  */
       
   631 void HbLabel::setMaximumLines(int maxLines)
       
   632 {
       
   633     Q_D(HbLabel);
       
   634 
       
   635     maxLines = qMax(maxLines, 0);
       
   636     if (d->mMaxLines != maxLines) {
       
   637         d->mMaxLines = maxLines;
       
   638         if (d->mActivePrimitive == HbStylePrivate::P_Label_text) {
       
   639             updatePrimitives();
       
   640         }
       
   641     }
       
   642 }
       
   643 
       
   644 /*!
       
   645     Returns maximum number of lines which can be visible in label when
       
   646     plain text is used (\sa setPlainText).
       
   647     Zero value means that there is no limitation.
       
   648 
       
   649     \sa setMaximumLines(int)
       
   650  */
       
   651 int HbLabel::maximumLines() const
       
   652 {
       
   653     return d_func()->mMaxLines;
       
   654 }
       
   655 
   620 #include "moc_hblabel.cpp"
   656 #include "moc_hblabel.cpp"