src/hbwidgets/editors/hbtextedit.cpp
changeset 0 16d8024aca5e
child 1 f7ac710697a9
equal deleted inserted replaced
-1:000000000000 0:16d8024aca5e
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2008-2010 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (developer.feedback@nokia.com)
       
     6 **
       
     7 ** This file is part of the HbWidgets module of the UI Extensions for Mobile.
       
     8 **
       
     9 ** GNU Lesser General Public License Usage
       
    10 ** This file may be used under the terms of the GNU Lesser General Public
       
    11 ** License version 2.1 as published by the Free Software Foundation and
       
    12 ** appearing in the file LICENSE.LGPL included in the packaging of this file.
       
    13 ** Please review the following information to ensure the GNU Lesser General
       
    14 ** Public License version 2.1 requirements will be met:
       
    15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    16 **
       
    17 ** In addition, as a special exception, Nokia gives you certain additional
       
    18 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    20 **
       
    21 ** If you have questions regarding the use of this file, please contact
       
    22 ** Nokia at developer.feedback@nokia.com.
       
    23 **
       
    24 ****************************************************************************/
       
    25 
       
    26 #include "hbtextedit.h"
       
    27 #include "hbtextedit_p.h"
       
    28 
       
    29 #include <QTextCursor>
       
    30 
       
    31 #ifdef HB_TEXT_MEASUREMENT_UTILITY
       
    32 #include "hbtextmeasurementutility_p.h"
       
    33 #include "hbfeaturemanager_p.h"
       
    34 #endif //HB_TEXT_MEASUREMENT_UTILITY
       
    35 
       
    36 /*!
       
    37 \class HbTextEdit
       
    38 \brief Multiline rich-text editor supporting HTML-style tags and WYSIWYG editing.
       
    39 @proto
       
    40 @hbwidgets
       
    41 
       
    42 Internally it uses QTextDocument to handle the content. It is also exposed in the API and it can be manipulated directly.
       
    43 HbTextEdit also has one cursor that can be controlled with the API.
       
    44 
       
    45 HbTextEdit can load either plaintext or HTML formatted files. See Qt documentation about available formatting.
       
    46 
       
    47  */
       
    48 /*!
       
    49  Constructs an empty HbLineEdit with parent \a parent.
       
    50  */
       
    51 HbTextEdit::HbTextEdit (QGraphicsItem *parent) :
       
    52     HbAbstractEdit(*new HbTextEditPrivate, parent)
       
    53 {
       
    54     Q_D(HbTextEdit);
       
    55     d->q_ptr = this;
       
    56 
       
    57     d->init();
       
    58 }
       
    59 
       
    60 /*!
       
    61  Constructs a HbTextEdit with parent \a parent.
       
    62  The text edit will display the text \a text.
       
    63  */
       
    64 HbTextEdit::HbTextEdit (const QString &text, QGraphicsItem *parent) :
       
    65     HbAbstractEdit(*new HbTextEditPrivate, parent)
       
    66 {
       
    67     Q_D(HbTextEdit);
       
    68     d->q_ptr = this;
       
    69 
       
    70     d->init();
       
    71 
       
    72     setPlainText(text);
       
    73 }
       
    74 
       
    75 HbTextEdit::HbTextEdit (HbTextEditPrivate &dd, QGraphicsItem *parent) :
       
    76     HbAbstractEdit(dd, parent)
       
    77 {
       
    78     Q_D(HbTextEdit);
       
    79     d->q_ptr = this;
       
    80 
       
    81     d->init();
       
    82 }
       
    83 
       
    84 /*!
       
    85  Destructor.
       
    86  */
       
    87 HbTextEdit::~HbTextEdit ()
       
    88 {
       
    89 }
       
    90 
       
    91 /*!
       
    92     \reimp
       
    93  */
       
    94 int HbTextEdit::type () const
       
    95 {
       
    96     return Type;
       
    97 }
       
    98 
       
    99 /*!
       
   100     Replaces the document used in the editor.
       
   101 
       
   102     This allows
       
   103  */
       
   104 void HbTextEdit::setDocument(QTextDocument *document)
       
   105 {
       
   106     HbAbstractEdit::setDocument(document);
       
   107 }
       
   108 
       
   109 QTextDocument *HbTextEdit::document() const
       
   110 {
       
   111     return HbAbstractEdit::document();
       
   112 }
       
   113 
       
   114 void HbTextEdit::setTextCursor(const QTextCursor &cursor)
       
   115 {
       
   116     HbAbstractEdit::setTextCursor(cursor);
       
   117 }
       
   118 
       
   119 QTextCursor HbTextEdit::textCursor() const
       
   120 {
       
   121     return HbAbstractEdit::textCursor();
       
   122 }
       
   123 
       
   124 /*!
       
   125     \reimp
       
   126  */
       
   127 QString HbTextEdit::toHtml() const
       
   128 {
       
   129     return HbAbstractEdit::toHtml();
       
   130 }
       
   131 
       
   132 /*!
       
   133     \reimp
       
   134  */
       
   135 void HbTextEdit::setHtml(const QString &text)
       
   136 {
       
   137     HbAbstractEdit::setHtml(text);
       
   138 }
       
   139 
       
   140 /*!
       
   141     \reimp
       
   142  */
       
   143 QString HbTextEdit::toPlainText () const
       
   144 {
       
   145     return HbAbstractEdit::toPlainText();
       
   146 }
       
   147 
       
   148 /*!
       
   149     \reimp
       
   150  */
       
   151 void HbTextEdit::setPlainText (const QString &text)
       
   152 {
       
   153     QString txt( text );
       
   154 #ifdef HB_TEXT_MEASUREMENT_UTILITY
       
   155     if ( HbFeatureManager::instance()->featureStatus( HbFeatureManager::TextMeasurement ) ) {
       
   156         if (text.endsWith(QChar(LOC_TEST_END))) {
       
   157             int index = text.indexOf(QChar(LOC_TEST_START));
       
   158             setProperty( HbTextMeasurementUtilityNameSpace::textIdPropertyName,  text.mid(index + 1, text.indexOf(QChar(LOC_TEST_END)) - index - 1) );
       
   159             setProperty( HbTextMeasurementUtilityNameSpace::textMaxLines, -1 );
       
   160             txt = text.left(index);
       
   161         } else {
       
   162             setProperty( HbTextMeasurementUtilityNameSpace::textIdPropertyName,  QVariant::Invalid );
       
   163         }
       
   164     }
       
   165 #endif //HB_TEXT_MEASUREMENT_UTILITY
       
   166     HbAbstractEdit::setPlainText(txt);
       
   167 }
       
   168 
       
   169 /*!
       
   170     \reimp
       
   171  */
       
   172 void HbTextEdit::resizeEvent(QGraphicsSceneResizeEvent *event)
       
   173 {
       
   174     HbAbstractEdit::resizeEvent(event);
       
   175 
       
   176     document()->setTextWidth(primitive(HbStyle::P_Edit_text)->boundingRect().width());
       
   177 }
       
   178 
       
   179 
       
   180 /*!
       
   181     \reimp
       
   182     reads custom parameters.
       
   183  */
       
   184 void HbTextEdit::polish( HbStyleParameters& params )
       
   185 {
       
   186     Q_D(HbTextEdit);
       
   187 
       
   188     const QString KTextRowLinesColorCSSName   = "line-color";
       
   189     const QString KTextRowLinesWidthCSSName   = "line-width";
       
   190     const QString KTextRowLinesEnabledCSSName = "line-enabled";
       
   191 
       
   192     // ------ adding css parameters ------
       
   193     params.addParameter(KTextRowLinesColorCSSName, Qt::magenta);
       
   194     params.addParameter(KTextRowLinesWidthCSSName, 0.0);
       
   195     params.addParameter(KTextRowLinesEnabledCSSName);
       
   196 
       
   197     HbAbstractEdit::polish(params);
       
   198 
       
   199     // ------ interpreting css parameters ------
       
   200     QVariant param = params.value( KTextRowLinesEnabledCSSName );
       
   201     d->mShowTextBaseLine = param.toBool();
       
   202 
       
   203     param = params.value(KTextRowLinesColorCSSName);
       
   204     if(param.canConvert(QVariant::Color)) {
       
   205         d->mTextBaseLinePen.setColor(param.value<QColor>());
       
   206     }
       
   207 
       
   208     param = params.value(KTextRowLinesWidthCSSName);
       
   209     if(param.canConvert(QVariant::Double)) {
       
   210         d->mTextBaseLinePen.setWidthF(qMax(param.toDouble(), 0.0));
       
   211     }
       
   212 }
       
   213 
       
   214 /*!
       
   215     \reimp
       
   216  */
       
   217 void HbTextEdit::focusOutEvent(QFocusEvent * event)
       
   218 {
       
   219     HbAbstractEdit::focusOutEvent(event);
       
   220     setBackgroundItem(HbStyle::P_TextEdit_frame_normal);
       
   221 }
       
   222 
       
   223 /*!
       
   224     \reimp
       
   225  */
       
   226 void HbTextEdit::focusInEvent(QFocusEvent * event)
       
   227 {
       
   228     HbAbstractEdit::focusInEvent(event);
       
   229     setBackgroundItem(HbStyle::P_TextEdit_frame_highlight);
       
   230 }
       
   231