emailuis/nmailuiwidgets/src/nmeditortextedit.cpp
changeset 43 99bcbff212ad
parent 40 2c62ef3caffd
child 47 f83bd4ae1fe3
child 54 997a02608b3a
equal deleted inserted replaced
42:139d4b7b2938 43:99bcbff212ad
    19 
    19 
    20 // Following constants will be removed later when possible
    20 // Following constants will be removed later when possible
    21 static const double Un = 6.66;
    21 static const double Un = 6.66;
    22 static const double BodyMargin = Un;
    22 static const double BodyMargin = Un;
    23 static const int ChromeHeight = 160;
    23 static const int ChromeHeight = 160;
    24 static const double FieldHeightWhenSecondaryFont = 5 * Un;
    24 static const double FieldHeightWhenSecondaryFont = 7.46 * Un;
    25 static const int GroupBoxTitleHeight = 42;
    25 static const int GroupBoxTitleHeight = 42;
    26 static const double HeightOfTheHeaderOnStartup =
    26 static const double HeightOfTheHeaderOnStartup =
    27     2 * FieldHeightWhenSecondaryFont + GroupBoxTitleHeight;
    27     2 * FieldHeightWhenSecondaryFont + GroupBoxTitleHeight;
    28 
    28 
    29 /*!
    29 /*!
    31 */
    31 */
    32 NmEditorTextEdit::NmEditorTextEdit(QGraphicsItem *parent) :
    32 NmEditorTextEdit::NmEditorTextEdit(QGraphicsItem *parent) :
    33     HbTextEdit(parent),
    33     HbTextEdit(parent),
    34     mFirstTimeToScrollPosUpdate(true)
    34     mFirstTimeToScrollPosUpdate(true)
    35 {
    35 {
       
    36     NM_FUNCTION;
    36 }
    37 }
    37 
    38 
    38 /*!
    39 /*!
    39     Destructor
    40     Destructor
    40 */
    41 */
    41 NmEditorTextEdit::~NmEditorTextEdit()
    42 NmEditorTextEdit::~NmEditorTextEdit()
    42 {
    43 {
       
    44     NM_FUNCTION;
    43 }
    45 }
    44 
    46 
    45 void NmEditorTextEdit::init(NmBaseViewScrollArea *bgScrollArea)
    47 void NmEditorTextEdit::init(NmBaseViewScrollArea *bgScrollArea)
    46 {
    48 {
       
    49     NM_FUNCTION;
       
    50     
    47     mPreviousContentsHeight = 0;
    51     mPreviousContentsHeight = 0;
    48     mFirstTime = true;
    52     mFirstTime = true;
    49     mCustomTextColor = QPair<bool, QColor>(false,Qt::black);
    53     mCustomTextColor = QPair<bool, QColor>(false,Qt::black);
    50     mBackgroundScrollArea = bgScrollArea;
    54     mBackgroundScrollArea = bgScrollArea;
    51     mHeaderHeight = (int)HeightOfTheHeaderOnStartup;
    55     mHeaderHeight = (int)HeightOfTheHeaderOnStartup;
    52     mBgScrollPosition.setX(0);
    56     mBgScrollPosition.setX(0);
    53     mBgScrollPosition.setY(0);
    57     mBgScrollPosition.setY(0);
    54     document()->setDocumentMargin(BodyMargin);
    58     document()->setDocumentMargin(BodyMargin);
    55 
    59 
    56     mScrollArea = this->scrollArea();
    60     mScrollArea = this->scrollArea();
    57     mScrollArea->setLongPressEnabled(true);
    61 
    58     // Enable scrolling using cursor
    62     // Enable scrolling using cursor
    59     setScrollable(true);
    63     setScrollable(true);
    60     mScrollArea->setScrollDirections(Qt::Horizontal);
    64     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 
    65     connect(this, SIGNAL(contentsChanged()), this, SLOT(updateEditorHeight()));
    66     connect(this, SIGNAL(contentsChanged()), this, SLOT(updateEditorHeight()));
    66     connect(this, SIGNAL(cursorPositionChanged(int, int)),
    67     connect(this, SIGNAL(cursorPositionChanged(int, int)),
    67             this, SLOT(setScrollPosition(int, int)));
    68             this, SLOT(setScrollPosition(int, int)));
    68     connect(this, SIGNAL(contentsChanged()), this, SLOT(updateCustomTextColor()));
    69     connect(this, SIGNAL(contentsChanged()), this, SLOT(updateCustomTextColor()));
    71 /*!
    72 /*!
    72     This function returns the height (pixels) of the body fields document content.
    73     This function returns the height (pixels) of the body fields document content.
    73  */
    74  */
    74 qreal NmEditorTextEdit::contentHeight() const
    75 qreal NmEditorTextEdit::contentHeight() const
    75 {
    76 {
       
    77     NM_FUNCTION;
       
    78     
    76     QSizeF s = document()->size();
    79     QSizeF s = document()->size();
    77     return s.height();
    80     return s.height();
    78 }
    81 }
    79 
    82 
    80 /*!
    83 /*!
    81    This slot updates the editor height. It is called every time when text edit
    84    This slot updates the editor height. It is called every time when text edit
    82    widget content has been changed.
    85    widget content has been changed.
    83  */
    86  */
    84 void NmEditorTextEdit::updateEditorHeight()
    87 void NmEditorTextEdit::updateEditorHeight()
    85 {
    88 {
       
    89     NM_FUNCTION;
       
    90     
    86     // Get current body content height
    91     // Get current body content height
    87     qreal heightOfTheTextEdit = contentHeight();
    92     qreal heightOfTheTextEdit = contentHeight();
    88 
    93 
    89     // Check if height is changed
    94     // Check if height is changed
    90     if (mPreviousContentsHeight != heightOfTheTextEdit) {
    95     if (mPreviousContentsHeight != heightOfTheTextEdit) {
   102     of the content so that cursor does not go outside of the screen or
   107     of the content so that cursor does not go outside of the screen or
   103     behind the virtual keyboard.
   108     behind the virtual keyboard.
   104  */
   109  */
   105 void NmEditorTextEdit::setScrollPosition(int oldPos, int newPos)
   110 void NmEditorTextEdit::setScrollPosition(int oldPos, int newPos)
   106 {
   111 {
       
   112     NM_FUNCTION;
       
   113     
   107     Q_UNUSED(oldPos);
   114     Q_UNUSED(oldPos);
   108 
   115 
   109     if (mFirstTime) {
   116     if (mFirstTime) {
   110         // For some reason content height of the HbTextEdit is wrong
   117         // For some reason content height of the HbTextEdit is wrong
   111         // right after construction. That is the reason why this mFirstTime
   118         // right after construction. That is the reason why this mFirstTime
   150 /*!
   157 /*!
   151     This slot is called when background scroll areas scroll position has been shanged.
   158     This slot is called when background scroll areas scroll position has been shanged.
   152 */
   159 */
   153 void NmEditorTextEdit::updateScrollPosition(const QPointF &newPosition)
   160 void NmEditorTextEdit::updateScrollPosition(const QPointF &newPosition)
   154 {
   161 {
       
   162     NM_FUNCTION;
       
   163     
   155     // Temporary fix: When this is called for the first time, the editor is scrolled down for 
   164     // Temporary fix: When this is called for the first time, the editor is scrolled down for 
   156     // some reason so this will restore the scroll position.
   165     // some reason so this will restore the scroll position.
   157     if(mFirstTimeToScrollPosUpdate) {
   166     if(mFirstTimeToScrollPosUpdate) {
   158         mFirstTimeToScrollPosUpdate = false;
   167         mFirstTimeToScrollPosUpdate = false;
   159         mBackgroundScrollArea->scrollContentsTo(QPointF(0,0));        
   168         mBackgroundScrollArea->scrollContentsTo(QPointF(0,0));        
   165 	This slot applies custom text color for user - entered text
   174 	This slot applies custom text color for user - entered text
   166     It does not affect the text originally inserted into editor
   175     It does not affect the text originally inserted into editor
   167  */
   176  */
   168 void NmEditorTextEdit::updateCustomTextColor()
   177 void NmEditorTextEdit::updateCustomTextColor()
   169 {
   178 {
       
   179     NM_FUNCTION;
       
   180     
   170     if (mCustomTextColor.first) {
   181     if (mCustomTextColor.first) {
   171         QTextCursor tcursor = textCursor();
   182         QTextCursor tcursor = textCursor();
   172         QTextCharFormat fmt;
   183         QTextCharFormat fmt;
   173         int pos = tcursor.position();
   184         int pos = tcursor.position();
   174         if (pos > 0) {
   185         if (pos > 0) {
   195     This slot is called when header widget height has been changed. Function performs
   206     This slot is called when header widget height has been changed. Function performs
   196     the repositioning of the body field and resizing of the editor and content area.
   207     the repositioning of the body field and resizing of the editor and content area.
   197  */
   208  */
   198 void NmEditorTextEdit::setHeaderHeight(int newHeight)
   209 void NmEditorTextEdit::setHeaderHeight(int newHeight)
   199 {
   210 {
       
   211     NM_FUNCTION;
       
   212     
   200     mHeaderHeight = newHeight;
   213     mHeaderHeight = newHeight;
   201     updateEditorHeight();
   214     updateEditorHeight();
   202 }
       
   203 
       
   204 /*!
       
   205     sendMousePressEvent. Function is used to relay mouse press event to base class
       
   206 */
       
   207 void NmEditorTextEdit::sendMousePressEvent(QGraphicsSceneMouseEvent *event)
       
   208 {
       
   209     if (event) {
       
   210         HbAbstractEdit::mousePressEvent(event);
       
   211     }
       
   212 }
       
   213 
       
   214 /*!
       
   215     sendMouseReleaseEvent. Function is used to relay mouse release event to base class
       
   216 */
       
   217 void NmEditorTextEdit::sendMouseReleaseEvent(QGraphicsSceneMouseEvent *event)
       
   218 {
       
   219     if (event) {
       
   220         HbAbstractEdit::mouseReleaseEvent(event);
       
   221     }
       
   222 }
       
   223 
       
   224 /*!
       
   225     sendMouseMoveEvent. Function is used to relay mouse move event to base class
       
   226 */
       
   227 void NmEditorTextEdit::sendMouseMoveEvent(QGraphicsSceneMouseEvent *event)
       
   228 {
       
   229     if (event) {
       
   230         HbAbstractEdit::mouseMoveEvent(event);
       
   231     }
       
   232 }
       
   233 
       
   234 /*!
       
   235     sendLongPressEvent. Opens the context menu of the editor
       
   236 */
       
   237 void NmEditorTextEdit::sendLongPressEvent(const QPointF &point)
       
   238 {
       
   239     showContextMenu(point);
       
   240 }
   215 }
   241 
   216 
   242 /*!
   217 /*!
   243    Sets flag is custom text color should be used and sets the custom color.
   218    Sets flag is custom text color should be used and sets the custom color.
   244    
   219    
   245    Function does not affect the color of existing content, only text that will be entered later.
   220    Function does not affect the color of existing content, only text that will be entered later.
   246  */
   221  */
   247 void NmEditorTextEdit::setCustomTextColor(const QPair<bool, QColor> &customColor)
   222 void NmEditorTextEdit::setCustomTextColor(const QPair<bool, QColor> &customColor)
   248 {
   223 {
       
   224     NM_FUNCTION;
       
   225     
   249     mCustomTextColor = customColor;
   226     mCustomTextColor = customColor;
   250 }
   227 }
   251 
   228 
   252 /*!
   229 /*!
   253     Reimplemented function \sa NmEditorTextEdit::setCustomTextColor(const QPair<bool, QColor> &customColor).
   230     Reimplemented function \sa NmEditorTextEdit::setCustomTextColor(const QPair<bool, QColor> &customColor).
   255     \arg info about using of custom color
   232     \arg info about using of custom color
   256     \arg color to be used as custom. If \arg useCustom is set to false then color is not changed
   233     \arg color to be used as custom. If \arg useCustom is set to false then color is not changed
   257  */
   234  */
   258 void NmEditorTextEdit::setCustomTextColor(bool useCustom, const QColor& color)
   235 void NmEditorTextEdit::setCustomTextColor(bool useCustom, const QColor& color)
   259 {   
   236 {   
       
   237     NM_FUNCTION;
       
   238     
   260     mCustomTextColor.first = useCustom;
   239     mCustomTextColor.first = useCustom;
   261     //check and set custom color
   240     //check and set custom color
   262     mCustomTextColor.first ? mCustomTextColor.second = color : 
   241     mCustomTextColor.first ? mCustomTextColor.second = color : 
   263                              mCustomTextColor.second = mCustomTextColor.second;
   242                              mCustomTextColor.second = mCustomTextColor.second;
   264     
   243     
   269  *  
   248  *  
   270  *  \return Current custem color.
   249  *  \return Current custem color.
   271  */
   250  */
   272 QPair<bool, QColor> NmEditorTextEdit::customTextColor() const
   251 QPair<bool, QColor> NmEditorTextEdit::customTextColor() const
   273 {
   252 {
       
   253     NM_FUNCTION;
       
   254     
   274     return mCustomTextColor;
   255     return mCustomTextColor;
   275 }
   256 }