emailuis/nmailuiwidgets/src/nmeditortextedit.cpp
changeset 47 f83bd4ae1fe3
parent 43 99bcbff212ad
child 48 10eaf342f539
equal deleted inserted replaced
45:780f926bc26c 47:f83bd4ae1fe3
    15 *
    15 *
    16 */
    16 */
    17 
    17 
    18 #include "nmailuiwidgetsheaders.h"
    18 #include "nmailuiwidgetsheaders.h"
    19 
    19 
    20 // Following constants will be removed later when possible
    20 static const QString FILE_PATH_CSS = ":nmeditortextedit.css";
    21 static const double Un = 6.66;
    21 static const QString FILE_PATH_WIDGETML = ":nmeditortextedit.widgetml";
    22 static const double BodyMargin = Un;
       
    23 static const int ChromeHeight = 160;
       
    24 static const double FieldHeightWhenSecondaryFont = 7.46 * Un;
       
    25 static const int GroupBoxTitleHeight = 42;
       
    26 static const double HeightOfTheHeaderOnStartup =
       
    27     2 * FieldHeightWhenSecondaryFont + GroupBoxTitleHeight;
       
    28 
    22 
    29 /*!
    23 /*!
    30     Constructor
    24     Constructor
    31 */
    25 */
    32 NmEditorTextEdit::NmEditorTextEdit(QGraphicsItem *parent) :
    26 NmEditorTextEdit::NmEditorTextEdit(QGraphicsItem *parent) :
    33     HbTextEdit(parent),
    27     HbTextEdit(parent)
    34     mFirstTimeToScrollPosUpdate(true)
       
    35 {
    28 {
    36     NM_FUNCTION;
    29     NM_FUNCTION;
       
    30     
       
    31     HbStyleLoader::registerFilePath(FILE_PATH_WIDGETML);
       
    32     HbStyleLoader::registerFilePath(FILE_PATH_CSS);
       
    33 
       
    34     mCustomTextColor = QPair<bool, QColor>(false, Qt::black);
       
    35     
       
    36     // Enable scrolling using cursor
       
    37     setScrollable(true);
       
    38     scrollArea()->setScrollDirections(Qt::Horizontal);
       
    39 
       
    40     // set background colour to plain white
       
    41     QPixmap whitePixmap(10,10);
       
    42     whitePixmap.fill(Qt::white);
       
    43     QGraphicsPixmapItem *pixmapItem = new QGraphicsPixmapItem (whitePixmap);
       
    44     setBackgroundItem(pixmapItem); 
       
    45     
       
    46     // disables highlight frame for now - new api to set the frame item should be release somewhere wk26
       
    47     setFocusHighlight(HbStyle::P_TextEdit_frame_highlight, HbWidget::FocusHighlightNone);
       
    48     
       
    49     connect(this, SIGNAL(contentsChanged()), this, SLOT(updateCustomTextColor()));
    37 }
    50 }
    38 
    51 
    39 /*!
    52 /*!
    40     Destructor
    53     Destructor
    41 */
    54 */
    42 NmEditorTextEdit::~NmEditorTextEdit()
    55 NmEditorTextEdit::~NmEditorTextEdit()
    43 {
    56 {
    44     NM_FUNCTION;
    57     NM_FUNCTION;
    45 }
       
    46 
       
    47 void NmEditorTextEdit::init(NmBaseViewScrollArea *bgScrollArea)
       
    48 {
       
    49     NM_FUNCTION;
       
    50     
    58     
    51     mPreviousContentsHeight = 0;
    59     HbStyleLoader::unregisterFilePath(FILE_PATH_WIDGETML);
    52     mFirstTime = true;
    60     HbStyleLoader::unregisterFilePath(FILE_PATH_CSS);    
    53     mCustomTextColor = QPair<bool, QColor>(false,Qt::black);
       
    54     mBackgroundScrollArea = bgScrollArea;
       
    55     mHeaderHeight = (int)HeightOfTheHeaderOnStartup;
       
    56     mBgScrollPosition.setX(0);
       
    57     mBgScrollPosition.setY(0);
       
    58     document()->setDocumentMargin(BodyMargin);
       
    59 
       
    60     mScrollArea = this->scrollArea();
       
    61 
       
    62     // Enable scrolling using cursor
       
    63     setScrollable(true);
       
    64     mScrollArea->setScrollDirections(Qt::Horizontal);
       
    65 
       
    66     connect(this, SIGNAL(contentsChanged()), this, SLOT(updateEditorHeight()));
       
    67     connect(this, SIGNAL(cursorPositionChanged(int, int)),
       
    68             this, SLOT(setScrollPosition(int, int)));
       
    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     NM_FUNCTION;
       
    78     
       
    79     QSizeF s = document()->size();
       
    80     return s.height();
       
    81 }
       
    82 
       
    83 /*!
       
    84    This slot updates the editor height. It is called every time when text edit
       
    85    widget content has been changed.
       
    86  */
       
    87 void NmEditorTextEdit::updateEditorHeight()
       
    88 {
       
    89     NM_FUNCTION;
       
    90     
       
    91     // Get current body content height
       
    92     qreal heightOfTheTextEdit = contentHeight();
       
    93 
       
    94     // Check if height is changed
       
    95     if (mPreviousContentsHeight != heightOfTheTextEdit) {
       
    96         mPreviousContentsHeight = heightOfTheTextEdit;
       
    97         setPreferredHeight(heightOfTheTextEdit);
       
    98         setMaximumHeight(heightOfTheTextEdit);
       
    99     }
       
   100     // Inform parent that content height has been changed
       
   101     emit editorContentHeightChanged();
       
   102 }
       
   103 
       
   104 /*!
       
   105     This slot is called when cursor position is changed in body area using
       
   106     'pointing stick' or keyboard. Function will update the scroll position
       
   107     of the content so that cursor does not go outside of the screen or
       
   108     behind the virtual keyboard.
       
   109  */
       
   110 void NmEditorTextEdit::setScrollPosition(int oldPos, int newPos)
       
   111 {
       
   112     NM_FUNCTION;
       
   113     
       
   114     Q_UNUSED(oldPos);
       
   115 
       
   116     if (mFirstTime) {
       
   117         // For some reason content height of the HbTextEdit is wrong
       
   118         // right after construction. That is the reason why this mFirstTime
       
   119         // member is used to set content height bit later.
       
   120         mFirstTime = false;
       
   121         updateEditorHeight();
       
   122     }
       
   123     const QSizeF screenReso = HbDeviceProfile::current().logicalSize();
       
   124     qreal maxHeight = screenReso.height() - ChromeHeight;
       
   125 
       
   126     // Get cursor position coordinates
       
   127     QRectF cursorPosPix = rectForPosition(newPos);
       
   128 
       
   129     // Calculate the screen top and bottom boundaries, this means the part of the
       
   130     // background scroll area which is currently visible.
       
   131     qreal visibleRectTopBoundary;
       
   132     qreal visibleRectBottomBoundary;
       
   133 
       
   134     if (mBgScrollPosition.y() < mHeaderHeight) {
       
   135         // Header is completely or partially visible
       
   136         visibleRectTopBoundary = mHeaderHeight - mBgScrollPosition.y();
       
   137         visibleRectBottomBoundary = maxHeight - visibleRectTopBoundary;
       
   138     }
       
   139     else {
       
   140         // Header is not visible
       
   141         visibleRectTopBoundary = mBgScrollPosition.y() - mHeaderHeight;
       
   142         visibleRectBottomBoundary = visibleRectTopBoundary + maxHeight;
       
   143     }
       
   144 
       
   145     // Do scrolling if cursor is out of the screen boundaries
       
   146     if (cursorPosPix.y() > visibleRectBottomBoundary) {
       
   147         // Do scroll forward
       
   148         mBackgroundScrollArea->scrollContentsTo(
       
   149             QPointF(0,cursorPosPix.y() - maxHeight + mHeaderHeight));
       
   150     }
       
   151     else if (cursorPosPix.y() + mHeaderHeight < mBgScrollPosition.y()) {
       
   152         // Do scroll backward
       
   153         mBackgroundScrollArea->scrollContentsTo(QPointF(0,cursorPosPix.y() + mHeaderHeight));
       
   154     }
       
   155 }
       
   156 
       
   157 /*!
       
   158     This slot is called when background scroll areas scroll position has been shanged.
       
   159 */
       
   160 void NmEditorTextEdit::updateScrollPosition(const QPointF &newPosition)
       
   161 {
       
   162     NM_FUNCTION;
       
   163     
       
   164     // Temporary fix: When this is called for the first time, the editor is scrolled down for 
       
   165     // some reason so this will restore the scroll position.
       
   166     if(mFirstTimeToScrollPosUpdate) {
       
   167         mFirstTimeToScrollPosUpdate = false;
       
   168         mBackgroundScrollArea->scrollContentsTo(QPointF(0,0));        
       
   169     }
       
   170     mBgScrollPosition = newPosition;
       
   171 }
    61 }
   172 
    62 
   173 /*!
    63 /*!
   174 	This slot applies custom text color for user - entered text
    64 	This slot applies custom text color for user - entered text
   175     It does not affect the text originally inserted into editor
    65     It does not affect the text originally inserted into editor
   191                     fmt = tcursor.charFormat();
    81                     fmt = tcursor.charFormat();
   192                     fmt.setForeground(mCustomTextColor.second);
    82                     fmt.setForeground(mCustomTextColor.second);
   193                     tcursor.mergeCharFormat(fmt);
    83                     tcursor.mergeCharFormat(fmt);
   194                 }
    84                 }
   195             }
    85             }
   196         } else {
    86         }
       
    87         else {
   197             fmt = tcursor.charFormat();
    88             fmt = tcursor.charFormat();
   198             fmt.setForeground(mCustomTextColor.second);
    89             fmt.setForeground(mCustomTextColor.second);
   199             tcursor.mergeCharFormat(fmt);
    90             tcursor.mergeCharFormat(fmt);
   200             setTextCursor(tcursor);
    91             setTextCursor(tcursor);
   201         }
    92         }
   202     }
    93     }
   203 }
       
   204 
       
   205 /*!
       
   206     This slot is called when header widget height has been changed. Function performs
       
   207     the repositioning of the body field and resizing of the editor and content area.
       
   208  */
       
   209 void NmEditorTextEdit::setHeaderHeight(int newHeight)
       
   210 {
       
   211     NM_FUNCTION;
       
   212     
       
   213     mHeaderHeight = newHeight;
       
   214     updateEditorHeight();
       
   215 }
    94 }
   216 
    95 
   217 /*!
    96 /*!
   218    Sets flag is custom text color should be used and sets the custom color.
    97    Sets flag is custom text color should be used and sets the custom color.
   219    
    98    
   252 {
   131 {
   253     NM_FUNCTION;
   132     NM_FUNCTION;
   254     
   133     
   255     return mCustomTextColor;
   134     return mCustomTextColor;
   256 }
   135 }
       
   136 
       
   137 /*!
       
   138  *  Returns the calculated rect in item coordinates of the editor for the the given \a position inside a document.
       
   139  */
       
   140 QRectF NmEditorTextEdit::rectForPosition(int position)
       
   141 {
       
   142     return HbTextEdit::rectForPosition(position);
       
   143 }
       
   144