emailuis/nmailuiwidgets/src/nmeditortextedit.cpp
changeset 54 997a02608b3a
parent 30 759dc5235cdb
child 59 16ed8d08d0b1
equal deleted inserted replaced
53:bf7eb7911fc5 54:997a02608b3a
    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     // Disable HbTextEdit scrolling. Background scroll area will handle scrolling.
       
    37     setScrollable(false);
       
    38     scrollArea()->setScrollDirections(0);
       
    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     // Create custom palette based on the current one
       
    50     QPalette testPalette = QApplication::palette();
       
    51 
       
    52     // Sets the selection background colour
       
    53     testPalette.setColor(QPalette::Active, QPalette::Highlight, Qt::cyan);
       
    54     testPalette.setColor(QPalette::Inactive, QPalette::Highlight, Qt::cyan);
       
    55 
       
    56     // Sets the link and visited link colours
       
    57     testPalette.setColor(QPalette::Active, QPalette::Link, Qt::darkBlue);
       
    58     testPalette.setColor(QPalette::Inactive, QPalette::Link, Qt::darkBlue);
       
    59     testPalette.setColor(QPalette::Active, QPalette::LinkVisited, Qt::darkMagenta);
       
    60     testPalette.setColor(QPalette::Inactive, QPalette::LinkVisited, Qt::darkMagenta);
       
    61 
       
    62     // Update custom palette for this widget
       
    63     setPalette(testPalette);
       
    64 
       
    65     connect(this, SIGNAL(contentsChanged()), this, SLOT(updateCustomTextColor()));
    37 }
    66 }
    38 
    67 
    39 /*!
    68 /*!
    40     Destructor
    69     Destructor
    41 */
    70 */
    42 NmEditorTextEdit::~NmEditorTextEdit()
    71 NmEditorTextEdit::~NmEditorTextEdit()
    43 {
    72 {
    44     NM_FUNCTION;
    73     NM_FUNCTION;
    45 }
       
    46 
       
    47 void NmEditorTextEdit::init(NmBaseViewScrollArea *bgScrollArea)
       
    48 {
       
    49     NM_FUNCTION;
       
    50     
    74     
    51     mPreviousContentsHeight = 0;
    75     HbStyleLoader::unregisterFilePath(FILE_PATH_WIDGETML);
    52     mFirstTime = true;
    76     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 }
    77 }
   172 
    78 
   173 /*!
    79 /*!
   174 	This slot applies custom text color for user - entered text
    80 	This slot applies custom text color for user - entered text
   175     It does not affect the text originally inserted into editor
    81     It does not affect the text originally inserted into editor
   191                     fmt = tcursor.charFormat();
    97                     fmt = tcursor.charFormat();
   192                     fmt.setForeground(mCustomTextColor.second);
    98                     fmt.setForeground(mCustomTextColor.second);
   193                     tcursor.mergeCharFormat(fmt);
    99                     tcursor.mergeCharFormat(fmt);
   194                 }
   100                 }
   195             }
   101             }
   196         } else {
   102         }
       
   103         else {
   197             fmt = tcursor.charFormat();
   104             fmt = tcursor.charFormat();
   198             fmt.setForeground(mCustomTextColor.second);
   105             fmt.setForeground(mCustomTextColor.second);
   199             tcursor.mergeCharFormat(fmt);
   106             tcursor.mergeCharFormat(fmt);
   200             setTextCursor(tcursor);
   107             setTextCursor(tcursor);
   201         }
   108         }
   202     }
   109     }
   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 }
   110 }
   216 
   111 
   217 /*!
   112 /*!
   218    Sets flag is custom text color should be used and sets the custom color.
   113    Sets flag is custom text color should be used and sets the custom color.
   219    
   114    
   252 {
   147 {
   253     NM_FUNCTION;
   148     NM_FUNCTION;
   254     
   149     
   255     return mCustomTextColor;
   150     return mCustomTextColor;
   256 }
   151 }
       
   152 
       
   153 /*!
       
   154  *  Returns the rectangle for the cursor.
       
   155  */
       
   156 QRectF NmEditorTextEdit::rectForCursorPosition() const
       
   157 {
       
   158     NM_FUNCTION;
       
   159     
       
   160     return HbTextEdit::rectForPosition(cursorPosition());
       
   161 }
       
   162