src/hbwidgets/editors/hbtextedit_p.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 //
       
    27 //  W A R N I N G
       
    28 //  -------------
       
    29 //
       
    30 // This file is not part of the Hb API.  It exists purely as an
       
    31 // implementation detail.  This file may change from version to
       
    32 // version without notice, or even be removed.
       
    33 //
       
    34 // We mean it.
       
    35 //
       
    36 #include <QTextBlock>
       
    37 #include "hbtextedit_p.h"
       
    38 #include "hbtextedit.h"
       
    39 #include "hbcolorscheme.h"
       
    40 #include <QtDebug>
       
    41 
       
    42 HbTextEditPrivate::HbTextEditPrivate () :
       
    43     HbAbstractEditPrivate(),
       
    44     mShowTextBaseLine(true)
       
    45 {
       
    46 }
       
    47 
       
    48 HbTextEditPrivate::~HbTextEditPrivate ()
       
    49 {
       
    50 }
       
    51 
       
    52 void HbTextEditPrivate::init()
       
    53 {
       
    54     Q_Q(HbTextEdit);
       
    55 
       
    56     mTextBaseLinePen.setColor(Qt::magenta);
       
    57     mTextBaseLinePen.setCapStyle(Qt::RoundCap);
       
    58     mTextBaseLinePen.setWidthF((qreal)1.0);
       
    59 
       
    60     q->setScrollable(true);
       
    61     doc->documentLayout()->document()->setDocumentMargin(0);
       
    62     q->setBackgroundItem(HbStyle::P_TextEdit_frame_normal);
       
    63 }
       
    64 
       
    65 void HbTextEditPrivate::updatePaletteFromTheme()
       
    66 {
       
    67     HbAbstractEditPrivate::updatePaletteFromTheme();
       
    68     Q_Q(HbTextEdit);
       
    69 
       
    70     QColor textColor = HbColorScheme::color("qtc_textedit_normal");
       
    71     QColor selectedColor = HbColorScheme::color("qtc_textedit_selected");
       
    72     QColor selectedBackground = HbColorScheme::color("qtc_textedit_marker_normal");
       
    73     QPalette pal = q->palette();
       
    74 
       
    75     if (textColor.isValid()) {
       
    76         pal.setColor(QPalette::Text, textColor);
       
    77     }
       
    78 
       
    79     if (selectedColor.isValid()) {
       
    80         pal.setColor(QPalette::HighlightedText, selectedColor);
       
    81     }
       
    82 
       
    83     if (selectedBackground.isValid()) {
       
    84         pal.setColor(QPalette::Highlight, selectedBackground);
       
    85     }
       
    86     q->setPalette(pal);
       
    87 }
       
    88 
       
    89 void HbTextEditPrivate::updateEditingSize()
       
    90 {
       
    91     Q_Q(HbTextEdit);
       
    92     if (scrollArea) {
       
    93         scrollArea->setMinimumHeight(QFontMetrics(q->font()).height());
       
    94         scrollArea->setPreferredHeight(doc->documentLayout()->documentSize().height());
       
    95         scrollArea->setMaximumHeight(QWIDGETSIZE_MAX);
       
    96     }
       
    97 }
       
    98 
       
    99 /*
       
   100  * \reimp
       
   101  *
       
   102  */
       
   103 void HbTextEditPrivate::drawContentBackground(QPainter *painter,
       
   104                                               const QStyleOptionGraphicsItem &) const
       
   105 {
       
   106     if(mShowTextBaseLine) {
       
   107         drawTextBaseLines(painter);
       
   108     }
       
   109 }
       
   110 
       
   111 /*
       
   112  * @proto
       
   113  * this method draws base lines for text
       
   114  */
       
   115 void HbTextEditPrivate::drawTextBaseLines(QPainter *painter) const
       
   116 {
       
   117     const QRegion clipReg = painter->clipRegion();
       
   118 
       
   119     painter->setPen(mTextBaseLinePen);
       
   120 
       
   121     const QAbstractTextDocumentLayout* const docLayout = doc->documentLayout();
       
   122 
       
   123     QRectF lineRect;
       
   124     qreal lastAscent((qreal)0.0);
       
   125 
       
   126     // iterate through paragraphs
       
   127     QTextBlock textBlock = doc->begin();
       
   128     for(;textBlock.isValid(); textBlock = textBlock.next() ) {
       
   129         QRectF blockRect = docLayout->blockBoundingRect(textBlock);
       
   130 
       
   131         if(!clipReg.intersects(blockRect.toRect())) {
       
   132             continue; // dont try to draw unneeded paragraphs
       
   133         }
       
   134         // iterate through lines of paragraph
       
   135         const int n = textBlock.layout()->lineCount();
       
   136         for( int i=0; i<n; ++i ){
       
   137             QTextLine line = textBlock.layout()->lineAt(i);
       
   138 
       
   139             lineRect = line.rect();
       
   140             lineRect.translate(blockRect.topLeft());
       
   141             lastAscent = line.ascent();
       
   142             drawBaseLineAt(painter, lineRect.left(), lineRect.right(), lineRect.top()+lastAscent);
       
   143         } // end of for loop: iterate through lines
       
   144 
       
   145     } // end of for loop: iterate through paragraphs
       
   146 
       
   147     // try draw extra lines where ther is no text enymore
       
   148     if(!lineRect.isEmpty()) { // last paragraph was drawn
       
   149         lineRect.translate(0,lineRect.height());
       
   150         while(lineRect.top()<=canvas->size().height()) {
       
   151             drawBaseLineAt(painter, lineRect.left(), lineRect.right(), lineRect.top()+lastAscent);
       
   152             lineRect.translate(0,lineRect.height());
       
   153         }
       
   154     }
       
   155 }
       
   156 
       
   157 /*
       
   158  * @proto
       
   159  * draws on \a painter single base line for one line text bounded by \a rect
       
   160  */
       
   161 void HbTextEditPrivate::drawBaseLineAt(QPainter *painter,
       
   162                                            qreal x1, qreal x2, qreal y) const
       
   163 {
       
   164     painter->drawLine(QPointF(x1,y), QPointF(x2,y));
       
   165 }