emailuis/nmailuiwidgets/src/nmeditortextedit.cpp
changeset 18 578830873419
child 23 2dc6caa42ec3
equal deleted inserted replaced
4:e7aa27f58ae1 18:578830873419
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: Message editor body text field
       
    15 *
       
    16 */
       
    17 
       
    18 #include "nmailuiwidgetsheaders.h"
       
    19 
       
    20 // Following constants will be removed later when possible
       
    21 static const double Un = 6.66;
       
    22 static const double BodyMargin = Un;
       
    23 static const int ChromeHeight = 160;
       
    24 static const double FieldHeightWhenSecondaryFont = 5 * Un;
       
    25 static const int GroupBoxTitleHeight = 42;
       
    26 static const double HeightOfTheHeaderOnStartup =
       
    27     2 * FieldHeightWhenSecondaryFont + GroupBoxTitleHeight;
       
    28 
       
    29 /*!
       
    30     Constructor
       
    31 */
       
    32 NmEditorTextEdit::NmEditorTextEdit(QGraphicsItem *parent) :
       
    33     HbTextEdit(parent)
       
    34 {
       
    35 }
       
    36 
       
    37 /*!
       
    38     Destructor
       
    39 */
       
    40 NmEditorTextEdit::~NmEditorTextEdit()
       
    41 {
       
    42 }
       
    43 
       
    44 void NmEditorTextEdit::init(NmEditorContent *parent, NmBaseViewScrollArea *bgScrollArea)
       
    45 {
       
    46     mPreviousContentsHeight = 0;
       
    47     mFirstTime = true;
       
    48     mCustomTextColor = QPair<bool, QColor>(false,Qt::black);
       
    49     mParent = parent;
       
    50     mBackgroundScrollArea = bgScrollArea;
       
    51     mHeaderHeight = (int)HeightOfTheHeaderOnStartup;
       
    52     mBgScrollPosition.setX(0);
       
    53     mBgScrollPosition.setY(0);
       
    54     document()->setDocumentMargin(BodyMargin);
       
    55 
       
    56     mScrollArea = this->scrollArea();
       
    57     mScrollArea->setLongPressEnabled(true);
       
    58     // Enable scrolling using cursor
       
    59     setScrollable(true);
       
    60     mScrollArea->setScrollDirections(Qt::Horizontal);
       
    61     // Let all mouse events go into background scroll area which handles the scrolling
       
    62     setAcceptedMouseButtons(Qt::NoButton);
       
    63     mScrollArea->setAcceptedMouseButtons(Qt::NoButton);
       
    64 
       
    65     connect(this, SIGNAL(contentsChanged()), this, SLOT(updateEditorHeight()));
       
    66     connect(this, SIGNAL(cursorPositionChanged(int, int)),
       
    67             this, SLOT(setScrollPosition(int, int)));
       
    68     connect(this, SIGNAL(setEditorContentHeight()), mParent, SLOT(setEditorContentHeight()));
       
    69     connect(this, SIGNAL(contentsChanged()), this, SLOT(updateCustomTextColor()));
       
    70 }
       
    71 
       
    72 /*!
       
    73     This function returns the height (pixels) of the body fields document content.
       
    74  */
       
    75 qreal NmEditorTextEdit::contentHeight() const
       
    76 {
       
    77     QSizeF s = document()->size();
       
    78     return s.height();
       
    79 }
       
    80 
       
    81 /*!
       
    82    This slot updates the editor height. It is called every time when text edit
       
    83    widget content has been changed.
       
    84  */
       
    85 void NmEditorTextEdit::updateEditorHeight()
       
    86 {
       
    87     // Get current body content height
       
    88     qreal heightOfTheTextEdit = contentHeight();
       
    89 
       
    90     // Check if height is changed
       
    91     if (mPreviousContentsHeight != heightOfTheTextEdit) {
       
    92         mPreviousContentsHeight = heightOfTheTextEdit;
       
    93         setPreferredHeight(heightOfTheTextEdit);
       
    94         setMaximumHeight(heightOfTheTextEdit);
       
    95     }
       
    96     // Inform parent that content height has been changed
       
    97     emit setEditorContentHeight();
       
    98 }
       
    99 
       
   100 /*!
       
   101     This slot is called when cursor position is changed in body area using
       
   102     'pointing stick' or keyboard. Function will update the scroll position
       
   103     of the content so that cursor does not go outside of the screen or
       
   104     behind the virtual keyboard.
       
   105  */
       
   106 void NmEditorTextEdit::setScrollPosition(int oldPos, int newPos)
       
   107 {
       
   108     Q_UNUSED(oldPos);
       
   109 
       
   110     if (mFirstTime) {
       
   111         // For some reason content height of the HbTextEdit is wrong
       
   112         // right after construction. That is the reason why this mFirstTime
       
   113         // member is used to set content height bit later.
       
   114         mFirstTime = false;
       
   115         updateEditorHeight();
       
   116     }
       
   117     const QSizeF screenReso = HbDeviceProfile::current().logicalSize();
       
   118     qreal maxHeight = screenReso.height() - ChromeHeight;
       
   119 
       
   120     // Get cursor position coordinates
       
   121     QRectF cursorPosPix = rectForPosition(newPos);
       
   122 
       
   123     // Calculate the screen top and bottom boundaries, this means the part of the
       
   124     // background scroll area which is currently visible.
       
   125     qreal visibleRectTopBoundary;
       
   126     qreal visibleRectBottomBoundary;
       
   127 
       
   128     if (mBgScrollPosition.y() < mHeaderHeight) {
       
   129         // Header is completely or partially visible
       
   130         visibleRectTopBoundary = mHeaderHeight - mBgScrollPosition.y();
       
   131         visibleRectBottomBoundary = maxHeight - visibleRectTopBoundary;
       
   132     }
       
   133     else {
       
   134         // Header is not visible
       
   135         visibleRectTopBoundary = mBgScrollPosition.y() - mHeaderHeight;
       
   136         visibleRectBottomBoundary = visibleRectTopBoundary + maxHeight;
       
   137     }
       
   138 
       
   139     // Do scrolling if cursor is out of the screen boundaries
       
   140     if (cursorPosPix.y() > visibleRectBottomBoundary) {
       
   141         // Do scroll forward
       
   142         mBackgroundScrollArea->scrollContentsTo(
       
   143             QPointF(0,cursorPosPix.y() - maxHeight + mHeaderHeight));
       
   144     }
       
   145     else if (cursorPosPix.y() + mHeaderHeight < mBgScrollPosition.y()) {
       
   146         // Do scroll backward
       
   147         mBackgroundScrollArea->scrollContentsTo(QPointF(0,cursorPosPix.y() + mHeaderHeight));
       
   148     }
       
   149 }
       
   150 
       
   151 /*!
       
   152     This slot is called when background scroll areas scroll position has been shanged.
       
   153 */
       
   154 void NmEditorTextEdit::updateScrollPosition(const QPointF &newPosition)
       
   155 {
       
   156     mBgScrollPosition = newPosition;
       
   157 }
       
   158 
       
   159 /*!
       
   160 	This slot applies custom text color for user - entered text
       
   161     It does not affect the text originally inserted into editor
       
   162  */
       
   163 void NmEditorTextEdit::updateCustomTextColor()
       
   164 {
       
   165     if (mCustomTextColor.first) {
       
   166         QTextCursor tcursor = textCursor();
       
   167         QTextCharFormat fmt;
       
   168         int pos = tcursor.position();
       
   169         if (pos > 0) {
       
   170             // If there isn't any user-made selection - apply custom color
       
   171             if (!tcursor.hasSelection() && !tcursor.hasComplexSelection()) {
       
   172                 // Select last added char and set its color to custom
       
   173                 if (tcursor.movePosition(QTextCursor::PreviousCharacter, QTextCursor::KeepAnchor, 1)
       
   174                     && tcursor.hasSelection()) {
       
   175                     fmt = tcursor.charFormat();
       
   176                     fmt.setForeground(mCustomTextColor.second);
       
   177                     tcursor.mergeCharFormat(fmt);
       
   178                 }
       
   179             }
       
   180         } else {
       
   181             fmt = tcursor.charFormat();
       
   182             fmt.setForeground(mCustomTextColor.second);
       
   183             tcursor.mergeCharFormat(fmt);
       
   184             setTextCursor(tcursor);
       
   185         }
       
   186     }
       
   187 }
       
   188 
       
   189 /*!
       
   190     This slot is called when header widget height has been changed. Function performs
       
   191     the repositioning of the body field and resizing of the editor and content area.
       
   192  */
       
   193 void NmEditorTextEdit::setHeaderHeight(int newHeight)
       
   194 {
       
   195     mHeaderHeight = newHeight;
       
   196     updateEditorHeight();
       
   197 }
       
   198 
       
   199 /*!
       
   200     sendMousePressEvent. Function is used to relay mouse press event to base class
       
   201 */
       
   202 void NmEditorTextEdit::sendMousePressEvent(QGraphicsSceneMouseEvent *event)
       
   203 {
       
   204     if (event) {
       
   205         HbAbstractEdit::mousePressEvent(event);
       
   206     }
       
   207 }
       
   208 
       
   209 /*!
       
   210     sendMouseReleaseEvent. Function is used to relay mouse release event to base class
       
   211 */
       
   212 void NmEditorTextEdit::sendMouseReleaseEvent(QGraphicsSceneMouseEvent *event)
       
   213 {
       
   214     if (event) {
       
   215         HbAbstractEdit::mouseReleaseEvent(event);
       
   216     }
       
   217 }
       
   218 
       
   219 /*!
       
   220     sendMouseMoveEvent. Function is used to relay mouse move event to base class
       
   221 */
       
   222 void NmEditorTextEdit::sendMouseMoveEvent(QGraphicsSceneMouseEvent *event)
       
   223 {
       
   224     if (event) {
       
   225         HbAbstractEdit::mouseMoveEvent(event);
       
   226     }
       
   227 }
       
   228 
       
   229 /*!
       
   230     sendLongPressEvent. Opens the context menu of the editor
       
   231 */
       
   232 void NmEditorTextEdit::sendLongPressEvent(const QPointF &point)
       
   233 {
       
   234     showContextMenu(point);
       
   235 }
       
   236 
       
   237 /*!
       
   238    Sets flag is custom text color should be used and sets the custom color.
       
   239    
       
   240    Function does not affect the color of existing content, only text that will be entered later.
       
   241  */
       
   242 void NmEditorTextEdit::setCustomTextColor(const QPair<bool, QColor> &customColor)
       
   243 {
       
   244     mCustomTextColor = customColor;
       
   245 }
       
   246 
       
   247 /*!
       
   248     Reimplemented function \sa NmEditorTextEdit::setCustomTextColor(const QPair<bool, QColor> &customColor).
       
   249     
       
   250     \arg info about using of custom color
       
   251     \arg color to be used as custom. If \arg useCustom is set to false then color is not changed
       
   252  */
       
   253 void NmEditorTextEdit::setCustomTextColor(bool useCustom, const QColor& color)
       
   254 {   
       
   255     mCustomTextColor.first = useCustom;
       
   256     //check and set custom color
       
   257     mCustomTextColor.first ? mCustomTextColor.second = color : 
       
   258                              mCustomTextColor.second = mCustomTextColor.second;
       
   259     
       
   260 }
       
   261 
       
   262 /*!
       
   263  *  Return current custom color and if it must be used.
       
   264  *  
       
   265  *  \return Current custem color.
       
   266  */
       
   267 QPair<bool, QColor> NmEditorTextEdit::customTextColor() const
       
   268 {
       
   269     return mCustomTextColor;
       
   270 }