emailuis/nmailuiwidgets/src/nmhtmllineedit.cpp
author hgs
Thu, 30 Sep 2010 11:43:07 +0300
changeset 74 6c59112cfd31
parent 68 83cc6bae1de8
child 76 38bf5461e270
permissions -rw-r--r--
201039
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
18
578830873419 Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
     1
/*
578830873419 Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
     2
* Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
578830873419 Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
     3
* All rights reserved.
578830873419 Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
     4
* This component and the accompanying materials are made available
578830873419 Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
     5
* under the terms of "Eclipse Public License v1.0"
578830873419 Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
     6
* which accompanies this distribution, and is available
578830873419 Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
     7
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
578830873419 Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
     8
*
578830873419 Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
     9
* Initial Contributors:
578830873419 Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    10
* Nokia Corporation - initial contribution.
578830873419 Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    11
*
578830873419 Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    12
* Contributors:
578830873419 Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    13
*
578830873419 Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    14
* Description: Message editor header widget
578830873419 Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    15
*
578830873419 Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    16
*/
578830873419 Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    17
578830873419 Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    18
#include "nmailuiwidgetsheaders.h"
74
hgs
parents: 68
diff changeset
    19
hgs
parents: 68
diff changeset
    20
static const int NmLabelIndentUsePreviousValue = 0;
hgs
parents: 68
diff changeset
    21
18
578830873419 Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    22
/*
578830873419 Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    23
#include "nmhtmllineedit.h"
578830873419 Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    24
*/
578830873419 Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    25
/*!
578830873419 Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    26
    Constructor
578830873419 Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    27
 */
578830873419 Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    28
NmHtmlLineEdit::NmHtmlLineEdit(QGraphicsItem *parent) :
74
hgs
parents: 68
diff changeset
    29
    HbLineEdit(parent),
hgs
parents: 68
diff changeset
    30
    mIndent(0)
18
578830873419 Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    31
{
43
hgs
parents: 18
diff changeset
    32
    NM_FUNCTION;
48
10eaf342f539 201025_1
hgs
parents: 43
diff changeset
    33
10eaf342f539 201025_1
hgs
parents: 43
diff changeset
    34
    // Disable scrolling so that baunch effect works correctly
10eaf342f539 201025_1
hgs
parents: 43
diff changeset
    35
    HbAbstractEdit::setScrollable(false);
10eaf342f539 201025_1
hgs
parents: 43
diff changeset
    36
    HbAbstractEdit::scrollArea()->setScrollDirections(0);
18
578830873419 Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    37
}
578830873419 Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    38
578830873419 Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    39
/*!
578830873419 Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    40
    Destructor
578830873419 Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    41
 */
578830873419 Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    42
NmHtmlLineEdit::~NmHtmlLineEdit()
578830873419 Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    43
{
43
hgs
parents: 18
diff changeset
    44
    NM_FUNCTION;
18
578830873419 Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    45
}
578830873419 Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    46
578830873419 Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    47
/*!
578830873419 Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    48
    Replaces the document used in the editor.
578830873419 Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    49
578830873419 Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    50
    This allows
578830873419 Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    51
 */
578830873419 Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    52
void NmHtmlLineEdit::setDocument(QTextDocument *document)
578830873419 Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    53
{
43
hgs
parents: 18
diff changeset
    54
    NM_FUNCTION;
hgs
parents: 18
diff changeset
    55
    
18
578830873419 Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    56
    HbAbstractEdit::setDocument(document);
578830873419 Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    57
}
578830873419 Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    58
578830873419 Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    59
QTextDocument *NmHtmlLineEdit::document() const
578830873419 Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    60
{
43
hgs
parents: 18
diff changeset
    61
    NM_FUNCTION;
hgs
parents: 18
diff changeset
    62
    
18
578830873419 Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    63
    return HbAbstractEdit::document();
578830873419 Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    64
}
578830873419 Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    65
578830873419 Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    66
void NmHtmlLineEdit::setTextCursor(const QTextCursor &cursor)
578830873419 Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    67
{
43
hgs
parents: 18
diff changeset
    68
    NM_FUNCTION;
hgs
parents: 18
diff changeset
    69
    
18
578830873419 Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    70
    HbAbstractEdit::setTextCursor(cursor);
578830873419 Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    71
}
578830873419 Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    72
578830873419 Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    73
QTextCursor NmHtmlLineEdit::textCursor() const
578830873419 Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    74
{
43
hgs
parents: 18
diff changeset
    75
    NM_FUNCTION;
hgs
parents: 18
diff changeset
    76
    
18
578830873419 Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    77
    return HbAbstractEdit::textCursor();
578830873419 Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    78
}
578830873419 Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    79
578830873419 Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    80
QString NmHtmlLineEdit::toHtml() const
578830873419 Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    81
{
43
hgs
parents: 18
diff changeset
    82
    NM_FUNCTION;
hgs
parents: 18
diff changeset
    83
    
18
578830873419 Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    84
    return HbAbstractEdit::toHtml();
578830873419 Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    85
}
578830873419 Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    86
578830873419 Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    87
void NmHtmlLineEdit::setHtml(const QString &text)
578830873419 Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    88
{
43
hgs
parents: 18
diff changeset
    89
    NM_FUNCTION;
hgs
parents: 18
diff changeset
    90
    
18
578830873419 Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    91
    HbAbstractEdit::setHtml(text);
74
hgs
parents: 68
diff changeset
    92
    // Resetting the indent is called because setting new content lose old indention set. 
hgs
parents: 68
diff changeset
    93
    setIndentForLabel(NmLabelIndentUsePreviousValue);    
18
578830873419 Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    94
}
578830873419 Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    95
578830873419 Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    96
QString NmHtmlLineEdit::toPlainText () const
578830873419 Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    97
{
43
hgs
parents: 18
diff changeset
    98
    NM_FUNCTION;
hgs
parents: 18
diff changeset
    99
    
18
578830873419 Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
   100
    return HbAbstractEdit::toPlainText();
578830873419 Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
   101
}
578830873419 Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
   102
578830873419 Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
   103
void NmHtmlLineEdit::setPlainText (const QString &text)
578830873419 Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
   104
{
43
hgs
parents: 18
diff changeset
   105
    NM_FUNCTION;
hgs
parents: 18
diff changeset
   106
    
18
578830873419 Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
   107
    HbAbstractEdit::setPlainText(text);
74
hgs
parents: 68
diff changeset
   108
    // Resetting the indent is called because setting new content lose old indention set. 
hgs
parents: 68
diff changeset
   109
    setIndentForLabel(NmLabelIndentUsePreviousValue);    
18
578830873419 Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
   110
}
48
10eaf342f539 201025_1
hgs
parents: 43
diff changeset
   111
10eaf342f539 201025_1
hgs
parents: 43
diff changeset
   112
/*!
68
hgs
parents: 48
diff changeset
   113
 *  Returns true if this widget has the input focus.
hgs
parents: 48
diff changeset
   114
 */
hgs
parents: 48
diff changeset
   115
bool NmHtmlLineEdit::hasInputFocus() const
hgs
parents: 48
diff changeset
   116
{
hgs
parents: 48
diff changeset
   117
    NM_FUNCTION;
hgs
parents: 48
diff changeset
   118
hgs
parents: 48
diff changeset
   119
    bool ret = false;
hgs
parents: 48
diff changeset
   120
    
hgs
parents: 48
diff changeset
   121
    HbInputMethod* inputMethod = HbInputMethod::activeInputMethod();
hgs
parents: 48
diff changeset
   122
    
hgs
parents: 48
diff changeset
   123
    if (inputMethod) {
hgs
parents: 48
diff changeset
   124
        HbInputFocusObject *focusObject = inputMethod->focusObject();
hgs
parents: 48
diff changeset
   125
        if (focusObject) {
hgs
parents: 48
diff changeset
   126
            ret = (focusObject->object() == this);
hgs
parents: 48
diff changeset
   127
        }
hgs
parents: 48
diff changeset
   128
    }
hgs
parents: 48
diff changeset
   129
    
hgs
parents: 48
diff changeset
   130
    return ret; 
hgs
parents: 48
diff changeset
   131
}
hgs
parents: 48
diff changeset
   132
hgs
parents: 48
diff changeset
   133
hgs
parents: 48
diff changeset
   134
/*!
74
hgs
parents: 68
diff changeset
   135
 *  Returns the rectangle for the cursor including the predictive text area.  
hgs
parents: 68
diff changeset
   136
 *  HbAbstractEditPrivate::ensurePositionVisible() uses the same algorithm.
48
10eaf342f539 201025_1
hgs
parents: 43
diff changeset
   137
 */
10eaf342f539 201025_1
hgs
parents: 43
diff changeset
   138
QRectF NmHtmlLineEdit::rectForCursorPosition() const
10eaf342f539 201025_1
hgs
parents: 43
diff changeset
   139
{
10eaf342f539 201025_1
hgs
parents: 43
diff changeset
   140
    NM_FUNCTION;
10eaf342f539 201025_1
hgs
parents: 43
diff changeset
   141
    
74
hgs
parents: 68
diff changeset
   142
    int cursorPos = cursorPosition();
hgs
parents: 68
diff changeset
   143
    QRectF rect = rectForPosition(cursorPos);
hgs
parents: 68
diff changeset
   144
    QTextDocument *doc = document();
hgs
parents: 68
diff changeset
   145
    if (doc) {
hgs
parents: 68
diff changeset
   146
        rect.adjust(0, -doc->documentMargin(), 0, doc->documentMargin());
hgs
parents: 68
diff changeset
   147
        const QTextBlock block = doc->findBlock(cursorPos);
hgs
parents: 68
diff changeset
   148
        QTextLayout *blockLayout = block.layout();
hgs
parents: 68
diff changeset
   149
        if (block.isValid() && blockLayout) {
hgs
parents: 68
diff changeset
   150
            if (blockLayout->preeditAreaText().length()) {
hgs
parents: 68
diff changeset
   151
                // Adjust cursor rect so that predictive text will be also visible
hgs
parents: 68
diff changeset
   152
                rect.adjust(0, 0, boundingRect().width() / 2, 0);
hgs
parents: 68
diff changeset
   153
            }
hgs
parents: 68
diff changeset
   154
        }
hgs
parents: 68
diff changeset
   155
    }
hgs
parents: 68
diff changeset
   156
hgs
parents: 68
diff changeset
   157
    return rect;
48
10eaf342f539 201025_1
hgs
parents: 43
diff changeset
   158
}
74
hgs
parents: 68
diff changeset
   159
hgs
parents: 68
diff changeset
   160
/*!
hgs
parents: 68
diff changeset
   161
    Set margin for the first line. This will create empty space
hgs
parents: 68
diff changeset
   162
    into begining of the edit field to reserve enough space for the
hgs
parents: 68
diff changeset
   163
    label text e.g. 'To:'.
hgs
parents: 68
diff changeset
   164
 */
hgs
parents: 68
diff changeset
   165
void NmHtmlLineEdit::setIndentForLabel(const qreal indent)
hgs
parents: 68
diff changeset
   166
{
hgs
parents: 68
diff changeset
   167
    NM_FUNCTION;
hgs
parents: 68
diff changeset
   168
	
hgs
parents: 68
diff changeset
   169
    // Check that should we use previous value or set new value.
hgs
parents: 68
diff changeset
   170
    // Previous value is used when edit field content is changed.
hgs
parents: 68
diff changeset
   171
    if (indent != NmLabelIndentUsePreviousValue) {
hgs
parents: 68
diff changeset
   172
        mIndent = indent;
hgs
parents: 68
diff changeset
   173
    }
hgs
parents: 68
diff changeset
   174
    QTextCursor cursor(this->document());
hgs
parents: 68
diff changeset
   175
    cursor.select(QTextCursor::BlockUnderCursor);
hgs
parents: 68
diff changeset
   176
    QTextBlockFormat format;
hgs
parents: 68
diff changeset
   177
    format.setTextIndent(mIndent);
hgs
parents: 68
diff changeset
   178
    cursor.mergeBlockFormat(format);
hgs
parents: 68
diff changeset
   179
}
hgs
parents: 68
diff changeset
   180