emailuis/nmailui/src/nmeditorcontent.cpp
author hgs
Tue, 19 Oct 2010 12:30:59 +0300
changeset 79 2508e95245f2
parent 74 6c59112cfd31
permissions -rw-r--r--
201041_1
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
/*
51
hgs
parents: 49
diff changeset
     2
* Copyright (c) 2009 - 2010 Nokia Corporation and/or its subsidiary(-ies).
18
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 contents 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 "nmuiheaders.h"
578830873419 Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    19
578830873419 Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    20
// Layout
578830873419 Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    21
static const char *NMUI_EDITOR_BODY = "BodyTextEdit";
47
hgs
parents: 44
diff changeset
    22
static const char *NMUI_EDITOR_SCROLL_AREA = "scrollArea";
hgs
parents: 44
diff changeset
    23
static const char *NMUI_EDITOR_SCROLL_AREA_CONTENTS = "scrollAreaContents";
18
578830873419 Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    24
47
hgs
parents: 44
diff changeset
    25
// Regular expression for selecting img tags with "cid" in the mail.
hgs
parents: 44
diff changeset
    26
static const char *NMUI_EDITOR_REMOVE_EMBD_IMAGES_REG = 
48
10eaf342f539 201025_1
hgs
parents: 47
diff changeset
    27
    "(<img[^<]+(src\\s*=\\s*)(.{0,1}cid)([^<]+)(>\\s*|/>\\s*|></img>\\s*))";
47
hgs
parents: 44
diff changeset
    28
	
18
578830873419 Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    29
/*!
578830873419 Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    30
    Constructor
578830873419 Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    31
*/
47
hgs
parents: 44
diff changeset
    32
NmEditorContent::NmEditorContent(QObject *parent,
20
ecc8def7944a Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents: 18
diff changeset
    33
                                 HbDocumentLoader *documentLoader,
47
hgs
parents: 44
diff changeset
    34
                                 QNetworkAccessManager &manager,
hgs
parents: 44
diff changeset
    35
                                 NmApplication &application) :
hgs
parents: 44
diff changeset
    36
    QObject(parent),
48
10eaf342f539 201025_1
hgs
parents: 47
diff changeset
    37
    mHeader(NULL),
18
578830873419 Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    38
    mEditorWidget(NULL),
47
hgs
parents: 44
diff changeset
    39
    mScrollArea(NULL),
hgs
parents: 44
diff changeset
    40
    mScrollAreaContents(NULL),
51
hgs
parents: 49
diff changeset
    41
    mApplication(application),
hgs
parents: 49
diff changeset
    42
    mNeedForWidthAdjustment(false)
18
578830873419 Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    43
{
43
hgs
parents: 23
diff changeset
    44
    NM_FUNCTION;
18
578830873419 Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    45
47
hgs
parents: 44
diff changeset
    46
    // Construct container for the header widgets
51
hgs
parents: 49
diff changeset
    47
    mHeader = new NmEditorHeader(this, application, documentLoader);
18
578830873419 Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    48
578830873419 Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    49
    // Get pointer to body text area handling widget
578830873419 Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    50
    mEditorWidget = qobject_cast<NmEditorTextEdit *>(documentLoader->findWidget(NMUI_EDITOR_BODY));
51
hgs
parents: 49
diff changeset
    51
hgs
parents: 49
diff changeset
    52
    // Enable the emoticons.
hgs
parents: 49
diff changeset
    53
    mEditorWidget->setSmileysEnabled(true);
hgs
parents: 49
diff changeset
    54
20
ecc8def7944a Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents: 18
diff changeset
    55
    // Set body editor to use NmEditorTextDocument
ecc8def7944a Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents: 18
diff changeset
    56
    NmEditorTextDocument *textDocument = new NmEditorTextDocument(manager);
ecc8def7944a Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents: 18
diff changeset
    57
    mEditorWidget->setDocument(textDocument); 
ecc8def7944a Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents: 18
diff changeset
    58
    textDocument->setParent(mEditorWidget); // ownership changes
ecc8def7944a Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents: 18
diff changeset
    59
47
hgs
parents: 44
diff changeset
    60
    mScrollArea = qobject_cast<NmBaseViewScrollArea *>
49
00c7ae862740 201025_2
hgs
parents: 48
diff changeset
    61
        (documentLoader->findWidget(NMUI_EDITOR_SCROLL_AREA));
51
hgs
parents: 49
diff changeset
    62
    
48
10eaf342f539 201025_1
hgs
parents: 47
diff changeset
    63
    mScrollArea->setScrollDirections(Qt::Vertical | Qt::Horizontal);
51
hgs
parents: 49
diff changeset
    64
    mScrollArea->setClampingStyle(HbScrollArea::BounceBackClamping);
hgs
parents: 49
diff changeset
    65
        
23
2dc6caa42ec3 Revision: 201017
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents: 20
diff changeset
    66
    // Enable style picker menu item.
18
578830873419 Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    67
    mEditorWidget->setFormatDialog(new HbFormatDialog());
578830873419 Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    68
47
hgs
parents: 44
diff changeset
    69
    mScrollAreaContents =
hgs
parents: 44
diff changeset
    70
        qobject_cast<HbWidget *>(documentLoader->findWidget(NMUI_EDITOR_SCROLL_AREA_CONTENTS));
hgs
parents: 44
diff changeset
    71
    
18
578830873419 Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    72
    // Create signal slot connections
578830873419 Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    73
    createConnections();
51
hgs
parents: 49
diff changeset
    74
hgs
parents: 49
diff changeset
    75
    // The following line is necessary in terms of being able to add emoticons
hgs
parents: 49
diff changeset
    76
    // (smileys) to an empty document (mail content). Otherwise the private
hgs
parents: 49
diff changeset
    77
    // pointer of the QTextDocument which the smiley engine has is NULL and
hgs
parents: 49
diff changeset
    78
    // inserting a smiley will lead to an error.
hgs
parents: 49
diff changeset
    79
    mEditorWidget->setPlainText("");
74
hgs
parents: 68
diff changeset
    80
    
hgs
parents: 68
diff changeset
    81
    QCoreApplication::instance()->installEventFilter(this); // see eventFilter()
18
578830873419 Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    82
}
578830873419 Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    83
578830873419 Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    84
/*!
578830873419 Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    85
    Destructor
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
NmEditorContent::~NmEditorContent()
578830873419 Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    88
{
43
hgs
parents: 23
diff changeset
    89
    NM_FUNCTION;
74
hgs
parents: 68
diff changeset
    90
hgs
parents: 68
diff changeset
    91
    QCoreApplication::instance()->removeEventFilter(this); // see eventFilter()
18
578830873419 Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    92
}
578830873419 Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    93
578830873419 Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    94
/*!
62
hgs
parents: 51
diff changeset
    95
    Sets the body content. If reply envelopw is present, reply header is generated and set to 
hgs
parents: 51
diff changeset
    96
    editor. Reply envelope ownership is not transferred here.
18
578830873419 Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    97
 */
62
hgs
parents: 51
diff changeset
    98
void NmEditorContent::setBodyContent(NmUiEditorStartMode editorStartMode,
hgs
parents: 51
diff changeset
    99
                                     const NmMessage *originalMessage, 
hgs
parents: 51
diff changeset
   100
                                     const QString *signature)
18
578830873419 Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
   101
{
43
hgs
parents: 23
diff changeset
   102
    NM_FUNCTION;
44
hgs
parents: 43
diff changeset
   103
    
hgs
parents: 43
diff changeset
   104
    QString bodyContent;
62
hgs
parents: 51
diff changeset
   105
    
hgs
parents: 51
diff changeset
   106
    // first insert the signature
hgs
parents: 51
diff changeset
   107
    if (signature) {
hgs
parents: 51
diff changeset
   108
        bodyContent.append("<html><body><br><br>");
hgs
parents: 51
diff changeset
   109
        bodyContent.append(*signature);
hgs
parents: 51
diff changeset
   110
        bodyContent.append("<br></body></html>");
hgs
parents: 51
diff changeset
   111
    }
hgs
parents: 51
diff changeset
   112
    
51
hgs
parents: 49
diff changeset
   113
	QTextCursor cursor(mEditorWidget->document());
44
hgs
parents: 43
diff changeset
   114
    
47
hgs
parents: 44
diff changeset
   115
    // Create the "reply" header (also for forward message)
62
hgs
parents: 51
diff changeset
   116
	// sets the font color of the reply header and the original body text to black
74
hgs
parents: 68
diff changeset
   117
    if ((editorStartMode == NmUiEditorReply || editorStartMode == NmUiEditorReplyAll || 
hgs
parents: 68
diff changeset
   118
        editorStartMode == NmUiEditorForward) && originalMessage) {
51
hgs
parents: 49
diff changeset
   119
		bodyContent.append(QString("<style type=\"text/css\">* { color: black; }</style>"));
62
hgs
parents: 51
diff changeset
   120
        bodyContent.append(NmUtilities::createReplyHeader(originalMessage->envelope()));
44
hgs
parents: 43
diff changeset
   121
    }
hgs
parents: 43
diff changeset
   122
    
43
hgs
parents: 23
diff changeset
   123
    // Check which part is present. Html or plain text part. We use the original message parts.
62
hgs
parents: 51
diff changeset
   124
    const NmMessagePart *htmlPart = NULL;
hgs
parents: 51
diff changeset
   125
    const NmMessagePart *plainPart = NULL;
hgs
parents: 51
diff changeset
   126
    if (originalMessage) {
hgs
parents: 51
diff changeset
   127
        htmlPart = originalMessage->htmlBodyPart();
hgs
parents: 51
diff changeset
   128
        plainPart = originalMessage->plainTextBodyPart();
hgs
parents: 51
diff changeset
   129
    }
44
hgs
parents: 43
diff changeset
   130
 
47
hgs
parents: 44
diff changeset
   131
    if (htmlPart) {
68
hgs
parents: 66
diff changeset
   132
        QString bodyText(htmlPart->textContent());
74
hgs
parents: 68
diff changeset
   133
        if (editorStartMode == NmUiEditorReply || editorStartMode == NmUiEditorReplyAll || 
hgs
parents: 68
diff changeset
   134
                editorStartMode == NmUiEditorForward) {
68
hgs
parents: 66
diff changeset
   135
            convertBodyStylesToDivision(bodyText);
hgs
parents: 66
diff changeset
   136
        }
hgs
parents: 66
diff changeset
   137
        
74
hgs
parents: 68
diff changeset
   138
        if(editorStartMode == NmUiEditorReply || editorStartMode == NmUiEditorReplyAll ) {
68
hgs
parents: 66
diff changeset
   139
            removeEmbeddedImages(bodyText);
47
hgs
parents: 44
diff changeset
   140
        }
68
hgs
parents: 66
diff changeset
   141
        bodyContent.append(bodyText);
20
ecc8def7944a Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents: 18
diff changeset
   142
    }
ecc8def7944a Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents: 18
diff changeset
   143
    else if (plainPart) {
62
hgs
parents: 51
diff changeset
   144
        // Plain text part was present, set it to HbTextEdit as HTML
hgs
parents: 51
diff changeset
   145
        bodyContent.append(QString("<html><body><p>"));
44
hgs
parents: 43
diff changeset
   146
        bodyContent.append(plainPart->textContent());
62
hgs
parents: 51
diff changeset
   147
        bodyContent.append(QString("</p></body></html>"));
18
578830873419 Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
   148
    }
62
hgs
parents: 51
diff changeset
   149
    cursor.insertHtml(bodyContent);
hgs
parents: 51
diff changeset
   150
51
hgs
parents: 49
diff changeset
   151
    // Update of the body width is done when next contentChanged signal comes from the body.
hgs
parents: 49
diff changeset
   152
    mNeedForWidthAdjustment = true;
hgs
parents: 49
diff changeset
   153
	cursor.clearSelection();
hgs
parents: 49
diff changeset
   154
	cursor.setPosition(0);
62
hgs
parents: 51
diff changeset
   155
	cursor.insertHtml(QString("<html><body></body></html>"));
74
hgs
parents: 68
diff changeset
   156
	mEditorWidget->moveCursor(QTextCursor::Start, QTextCursor::MoveAnchor);
hgs
parents: 68
diff changeset
   157
	QMetaObject::invokeMethod(this, "ensureCursorVisibility", Qt::QueuedConnection);
20
ecc8def7944a Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents: 18
diff changeset
   158
}  
18
578830873419 Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
   159
578830873419 Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
   160
/*!
578830873419 Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
   161
   This method creates all needed signal-slot connections
578830873419 Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
   162
 */
578830873419 Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
   163
void NmEditorContent::createConnections()
578830873419 Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
   164
{
43
hgs
parents: 23
diff changeset
   165
    NM_FUNCTION;
hgs
parents: 23
diff changeset
   166
    
18
578830873419 Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
   167
    // Signal for setting HbTextEdit widgets html content
578830873419 Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
   168
    connect(this, SIGNAL(setHtml(QString)),
44
hgs
parents: 43
diff changeset
   169
            mEditorWidget, SLOT(setHtml(QString)), Qt::QueuedConnection);
47
hgs
parents: 44
diff changeset
   170
18
578830873419 Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
   171
    // Signal for setting HbTextEdit widgets plain text content
578830873419 Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
   172
    connect(this, SIGNAL(setPlainText(QString)),
44
hgs
parents: 43
diff changeset
   173
            mEditorWidget, SLOT(setPlainText(QString)), Qt::QueuedConnection);
47
hgs
parents: 44
diff changeset
   174
18
578830873419 Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
   175
    // Inform text edit widget that header height has been changed
48
10eaf342f539 201025_1
hgs
parents: 47
diff changeset
   176
    connect(mHeader, SIGNAL(headerHeightChanged(int)), this, SLOT(setEditorContentHeight()),
47
hgs
parents: 44
diff changeset
   177
        Qt::QueuedConnection);
hgs
parents: 44
diff changeset
   178
hgs
parents: 44
diff changeset
   179
    // we are interested in the document's height changes
hgs
parents: 44
diff changeset
   180
    connect(mEditorWidget->document()->documentLayout(), SIGNAL(documentSizeChanged(QSizeF)), this,
hgs
parents: 44
diff changeset
   181
        SLOT(setEditorContentHeight()), Qt::QueuedConnection);
hgs
parents: 44
diff changeset
   182
48
10eaf342f539 201025_1
hgs
parents: 47
diff changeset
   183
    // listen to the parent's (NmEditorView) size changes which happen eg. when VKB is opened/closed
10eaf342f539 201025_1
hgs
parents: 47
diff changeset
   184
    connect(parent(), SIGNAL(sizeChanged()), this, SLOT(ensureCursorVisibility()),
10eaf342f539 201025_1
hgs
parents: 47
diff changeset
   185
        Qt::QueuedConnection);
51
hgs
parents: 49
diff changeset
   186
hgs
parents: 49
diff changeset
   187
    // Listen scroll posion change signals for header reposition.
hgs
parents: 49
diff changeset
   188
    connect(mScrollArea, SIGNAL(scrollPositionChanged(QPointF)),
62
hgs
parents: 51
diff changeset
   189
       		this, SLOT(repositHeader(QPointF)));
hgs
parents: 51
diff changeset
   190
    
51
hgs
parents: 49
diff changeset
   191
    // Listen content change signal for body widget width adjustment.
hgs
parents: 49
diff changeset
   192
    connect(mEditorWidget->document(), SIGNAL(contentsChanged()), this, 
hgs
parents: 49
diff changeset
   193
        SLOT(setEditorContentWidth()), Qt::QueuedConnection);
18
578830873419 Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
   194
}
578830873419 Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
   195
578830873419 Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
   196
/*!
578830873419 Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
   197
    Return pointer to the email body text edit widget
578830873419 Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
   198
 */
47
hgs
parents: 44
diff changeset
   199
NmEditorTextEdit *NmEditorContent::editor() const
18
578830873419 Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
   200
{
43
hgs
parents: 23
diff changeset
   201
    NM_FUNCTION;
hgs
parents: 23
diff changeset
   202
    
18
578830873419 Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
   203
    return mEditorWidget;
578830873419 Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
   204
}
578830873419 Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
   205
578830873419 Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
   206
/*!
578830873419 Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
   207
    Return pointer to the header widget
578830873419 Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
   208
 */
47
hgs
parents: 44
diff changeset
   209
NmEditorHeader *NmEditorContent::header() const
18
578830873419 Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
   210
{
43
hgs
parents: 23
diff changeset
   211
    NM_FUNCTION;
hgs
parents: 23
diff changeset
   212
    
48
10eaf342f539 201025_1
hgs
parents: 47
diff changeset
   213
    return mHeader;
18
578830873419 Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
   214
}
578830873419 Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
   215
47
hgs
parents: 44
diff changeset
   216
/*!
hgs
parents: 44
diff changeset
   217
    This slot is called when header widget height has been changed. Function performs
hgs
parents: 44
diff changeset
   218
    the repositioning of the body field and resizing of the editor and content area.
hgs
parents: 44
diff changeset
   219
 */
hgs
parents: 44
diff changeset
   220
void NmEditorContent::setEditorContentHeight()
hgs
parents: 44
diff changeset
   221
{
hgs
parents: 44
diff changeset
   222
    NM_FUNCTION;
hgs
parents: 44
diff changeset
   223
    
66
hgs
parents: 62
diff changeset
   224
    HbStyle* style = mScrollAreaContents->style();
hgs
parents: 62
diff changeset
   225
    
hgs
parents: 62
diff changeset
   226
    if (style) {
hgs
parents: 62
diff changeset
   227
        // the height of the margin between the title bar and the header
hgs
parents: 62
diff changeset
   228
        qreal topMargin = 0;
hgs
parents: 62
diff changeset
   229
        style->parameter("hb-param-margin-gene-top", topMargin);
hgs
parents: 62
diff changeset
   230
        
hgs
parents: 62
diff changeset
   231
        // header height
hgs
parents: 62
diff changeset
   232
        qreal headerHeight = mHeader->headerHeight();
hgs
parents: 62
diff changeset
   233
    
hgs
parents: 62
diff changeset
   234
        // body area editor's document height with margins added
hgs
parents: 62
diff changeset
   235
        qreal documentHeightAndMargins = mEditorWidget->document()->size().height() +
hgs
parents: 62
diff changeset
   236
            (mEditorWidget->document()->documentMargin() * 2);
47
hgs
parents: 44
diff changeset
   237
    
66
hgs
parents: 62
diff changeset
   238
        // chrome height
hgs
parents: 62
diff changeset
   239
        qreal chromeHeight = 0;
hgs
parents: 62
diff changeset
   240
        style->parameter("hb-param-widget-chrome-height", chromeHeight);
hgs
parents: 62
diff changeset
   241
        
hgs
parents: 62
diff changeset
   242
        // screen height
hgs
parents: 62
diff changeset
   243
        qreal screenHeight = mApplication.screenSize().height();
47
hgs
parents: 44
diff changeset
   244
    
66
hgs
parents: 62
diff changeset
   245
        // set min size for the body area so that at least the screen area is always filled
hgs
parents: 62
diff changeset
   246
        qreal bodyAreaMinSize = screenHeight - chromeHeight - topMargin - headerHeight;
hgs
parents: 62
diff changeset
   247
        
hgs
parents: 62
diff changeset
   248
        qreal bodyAreaSize = fmax(bodyAreaMinSize, documentHeightAndMargins);
hgs
parents: 62
diff changeset
   249
        
hgs
parents: 62
diff changeset
   250
        mScrollAreaContents->setPreferredHeight(topMargin + headerHeight + bodyAreaSize);
hgs
parents: 62
diff changeset
   251
    }
47
hgs
parents: 44
diff changeset
   252
}
hgs
parents: 44
diff changeset
   253
hgs
parents: 44
diff changeset
   254
/*!
51
hgs
parents: 49
diff changeset
   255
    This slot is used to set width for the body field.
hgs
parents: 49
diff changeset
   256
    For some reason HbTextEdit does not set it's width, so we need to se it here.
hgs
parents: 49
diff changeset
   257
    This slot can be removed if HbTextEdit is fixed.
hgs
parents: 49
diff changeset
   258
 */
hgs
parents: 49
diff changeset
   259
void NmEditorContent::setEditorContentWidth()
hgs
parents: 49
diff changeset
   260
{
hgs
parents: 49
diff changeset
   261
    NM_FUNCTION;
hgs
parents: 49
diff changeset
   262
    
hgs
parents: 49
diff changeset
   263
    if (mNeedForWidthAdjustment) {
hgs
parents: 49
diff changeset
   264
        mNeedForWidthAdjustment = false;
hgs
parents: 49
diff changeset
   265
        mScrollAreaContents->setPreferredWidth(mEditorWidget->document()->idealWidth());
hgs
parents: 49
diff changeset
   266
    }
hgs
parents: 49
diff changeset
   267
}
hgs
parents: 49
diff changeset
   268
hgs
parents: 49
diff changeset
   269
/*!
48
10eaf342f539 201025_1
hgs
parents: 47
diff changeset
   270
    This slot is called when the cursor visibility has to be ensured ie. the scroll position is 
74
hgs
parents: 68
diff changeset
   271
    adjusted so that the cursor can be seen. For defining exatcly what area is ensured the 
hgs
parents: 68
diff changeset
   272
    same algorithm that HbAbstractEditPrivate::ensurePositionVisible() uses is used here. 
48
10eaf342f539 201025_1
hgs
parents: 47
diff changeset
   273
*/
10eaf342f539 201025_1
hgs
parents: 47
diff changeset
   274
void NmEditorContent::ensureCursorVisibility()
47
hgs
parents: 44
diff changeset
   275
{
hgs
parents: 44
diff changeset
   276
    NM_FUNCTION;
68
hgs
parents: 66
diff changeset
   277
    
hgs
parents: 66
diff changeset
   278
    QGraphicsItem *focused = NULL;
hgs
parents: 66
diff changeset
   279
    QRectF localRect; // set to empty
48
10eaf342f539 201025_1
hgs
parents: 47
diff changeset
   280
    
68
hgs
parents: 66
diff changeset
   281
    // find out which widget has the input focus and get the cursor position ractangle
hgs
parents: 66
diff changeset
   282
    if (mHeader->toEdit() && mHeader->toEdit()->hasInputFocus()) {
hgs
parents: 66
diff changeset
   283
        focused = mHeader->toEdit();
hgs
parents: 66
diff changeset
   284
        localRect = mHeader->toEdit()->rectForCursorPosition();
hgs
parents: 66
diff changeset
   285
    }
hgs
parents: 66
diff changeset
   286
    else if (mHeader->ccEdit() && mHeader->ccEdit()->hasInputFocus()) {
hgs
parents: 66
diff changeset
   287
        focused = mHeader->ccEdit();
hgs
parents: 66
diff changeset
   288
        localRect = mHeader->ccEdit()->rectForCursorPosition();
hgs
parents: 66
diff changeset
   289
    }
hgs
parents: 66
diff changeset
   290
    else if (mHeader->bccEdit() && mHeader->bccEdit()->hasInputFocus()) {
hgs
parents: 66
diff changeset
   291
        focused = mHeader->bccEdit();
hgs
parents: 66
diff changeset
   292
        localRect = mHeader->bccEdit()->rectForCursorPosition();
hgs
parents: 66
diff changeset
   293
    }
hgs
parents: 66
diff changeset
   294
    else if (mHeader->subjectEdit() && mHeader->subjectEdit()->hasInputFocus()) {
hgs
parents: 66
diff changeset
   295
        focused = mHeader->subjectEdit();
hgs
parents: 66
diff changeset
   296
        localRect = mHeader->subjectEdit()->rectForCursorPosition();
hgs
parents: 66
diff changeset
   297
    }
hgs
parents: 66
diff changeset
   298
    else if (mEditorWidget->hasInputFocus()) {
hgs
parents: 66
diff changeset
   299
        focused = mEditorWidget;
hgs
parents: 66
diff changeset
   300
        localRect = mEditorWidget->rectForCursorPosition();
hgs
parents: 66
diff changeset
   301
    }
hgs
parents: 66
diff changeset
   302
hgs
parents: 66
diff changeset
   303
    // ensure that the cursor position is visible
hgs
parents: 66
diff changeset
   304
    if (focused && !localRect.isEmpty()) {
74
hgs
parents: 68
diff changeset
   305
        QRectF rect = focused->mapRectToItem(mScrollAreaContents, localRect);
hgs
parents: 68
diff changeset
   306
        mScrollArea->ensureVisible(rect.center(), rect.width(), rect.height() / 2 );
47
hgs
parents: 44
diff changeset
   307
    }
hgs
parents: 44
diff changeset
   308
}
hgs
parents: 44
diff changeset
   309
/*!
hgs
parents: 44
diff changeset
   310
    Removes embedded images from the message body
hgs
parents: 44
diff changeset
   311
 */
hgs
parents: 44
diff changeset
   312
void NmEditorContent::removeEmbeddedImages(QString &bodyContent)
hgs
parents: 44
diff changeset
   313
{
hgs
parents: 44
diff changeset
   314
    NM_FUNCTION;
hgs
parents: 44
diff changeset
   315
 
hgs
parents: 44
diff changeset
   316
    QRegExp regExp(NMUI_EDITOR_REMOVE_EMBD_IMAGES_REG, Qt::CaseInsensitive);
hgs
parents: 44
diff changeset
   317
    bodyContent.remove(regExp);
48
10eaf342f539 201025_1
hgs
parents: 47
diff changeset
   318
}
62
hgs
parents: 51
diff changeset
   319
hgs
parents: 51
diff changeset
   320
/*!
hgs
parents: 51
diff changeset
   321
    This slot is called when scroll position has been changed.
hgs
parents: 51
diff changeset
   322
    Function create translation object which is used to set new position for
hgs
parents: 51
diff changeset
   323
    header so that header stays visible when body is scrolled horizontally.
hgs
parents: 51
diff changeset
   324
 */
hgs
parents: 51
diff changeset
   325
void NmEditorContent::repositHeader(const QPointF &scrollPosition)
hgs
parents: 51
diff changeset
   326
{
hgs
parents: 51
diff changeset
   327
    NM_FUNCTION;
hgs
parents: 51
diff changeset
   328
    
hgs
parents: 51
diff changeset
   329
    // Get the layout's left margin
hgs
parents: 51
diff changeset
   330
    qreal margin = 0;
66
hgs
parents: 62
diff changeset
   331
    HbInstance::instance()->style()->parameter("hb-param-margin-gene-left", margin);
hgs
parents: 62
diff changeset
   332
62
hgs
parents: 51
diff changeset
   333
    // Calculate header width. (Screen width minus left and right margins.
hgs
parents: 51
diff changeset
   334
    qreal headerWidth = mApplication.screenSize().width() - margin - margin;
hgs
parents: 51
diff changeset
   335
hgs
parents: 51
diff changeset
   336
    // Create translation object for header position adjustment.
hgs
parents: 51
diff changeset
   337
    QRectF editorBodyRect = mEditorWidget->geometry();
hgs
parents: 51
diff changeset
   338
    QTransform tr;
74
hgs
parents: 68
diff changeset
   339
hgs
parents: 68
diff changeset
   340
    qreal bodyWidth = editorBodyRect.width();
hgs
parents: 68
diff changeset
   341
    if ( bodyWidth < mApplication.screenSize().width() ) {
hgs
parents: 68
diff changeset
   342
    	    bodyWidth = mApplication.screenSize().width();
hgs
parents: 68
diff changeset
   343
    }
hgs
parents: 68
diff changeset
   344
hgs
parents: 68
diff changeset
   345
    qreal leftMovementThreshold( bodyWidth - headerWidth);
hgs
parents: 68
diff changeset
   346
62
hgs
parents: 51
diff changeset
   347
    if (scrollPosition.x() < 0) {
hgs
parents: 51
diff changeset
   348
        // Left side positioning. Allow left side baunch effect.
hgs
parents: 51
diff changeset
   349
        tr.translate(editorBodyRect.topLeft().x() - margin ,0);
hgs
parents: 51
diff changeset
   350
    }
hgs
parents: 51
diff changeset
   351
    else if (scrollPosition.x() >= 0 && scrollPosition.x() < leftMovementThreshold) {
hgs
parents: 51
diff changeset
   352
        // Middle area positioning
hgs
parents: 51
diff changeset
   353
        tr.translate(scrollPosition.x() ,0);
hgs
parents: 51
diff changeset
   354
    }
hgs
parents: 51
diff changeset
   355
    else {
hgs
parents: 51
diff changeset
   356
        // Right side positioning. Allow right side baunch effect.
hgs
parents: 51
diff changeset
   357
        tr.translate(editorBodyRect.topLeft().x() + leftMovementThreshold - margin ,0);
hgs
parents: 51
diff changeset
   358
    }
hgs
parents: 51
diff changeset
   359
    // Call header to perform the translation which moves hader to new position.
hgs
parents: 51
diff changeset
   360
    mHeader->repositHeader(tr);
hgs
parents: 51
diff changeset
   361
}
hgs
parents: 51
diff changeset
   362
68
hgs
parents: 66
diff changeset
   363
/*!
hgs
parents: 66
diff changeset
   364
    Removes body bgcolor tag from content and creates DIV from it.
hgs
parents: 66
diff changeset
   365
 */
hgs
parents: 66
diff changeset
   366
void NmEditorContent::convertBodyStylesToDivision(QString &bodyContent)
hgs
parents: 66
diff changeset
   367
{
hgs
parents: 66
diff changeset
   368
    NM_FUNCTION;
hgs
parents: 66
diff changeset
   369
    convertBGColorToStyle(bodyContent);
hgs
parents: 66
diff changeset
   370
  
hgs
parents: 66
diff changeset
   371
    // Make division from the head style body tag
hgs
parents: 66
diff changeset
   372
    createDivisionFromHead(bodyContent);
hgs
parents: 66
diff changeset
   373
}
hgs
parents: 66
diff changeset
   374
hgs
parents: 66
diff changeset
   375
/*!
hgs
parents: 66
diff changeset
   376
 *  Creates div.reply from the html header styles (if there is any) and applies this 
hgs
parents: 66
diff changeset
   377
 *  style to the message by surrounding the message with the div.reply element.
hgs
parents: 66
diff changeset
   378
 *  
hgs
parents: 66
diff changeset
   379
 *  Example:
hgs
parents: 66
diff changeset
   380
 *  <html><head>
hgs
parents: 66
diff changeset
   381
 *  <style type="text/css">
hgs
parents: 66
diff changeset
   382
 *  body {
hgs
parents: 66
diff changeset
   383
 *  color: #FF0000;
hgs
parents: 66
diff changeset
   384
 *  background-color: #ffffff }
hgs
parents: 66
diff changeset
   385
 *  h3{ color: blue }
hgs
parents: 66
diff changeset
   386
 *  </style></head>
hgs
parents: 66
diff changeset
   387
 *  <body>
hgs
parents: 66
diff changeset
   388
 *  <h3>This is bigger text.</h3>
hgs
parents: 66
diff changeset
   389
 *  This is the body text.
hgs
parents: 66
diff changeset
   390
 *  </body></html>
hgs
parents: 66
diff changeset
   391
 *  
hgs
parents: 66
diff changeset
   392
 *      ---->
hgs
parents: 66
diff changeset
   393
 *      
hgs
parents: 66
diff changeset
   394
 *  <html><head>
hgs
parents: 66
diff changeset
   395
 *  <style type="text/css">
hgs
parents: 66
diff changeset
   396
 *  div.reply {
hgs
parents: 66
diff changeset
   397
 *  color: #FF0000;
hgs
parents: 66
diff changeset
   398
 *  background-color: #ffffff }
hgs
parents: 66
diff changeset
   399
 *  h3 { color: blue }
hgs
parents: 66
diff changeset
   400
 *  </style></head>
hgs
parents: 66
diff changeset
   401
 *  <body>
hgs
parents: 66
diff changeset
   402
 *  <div class="reply">
hgs
parents: 66
diff changeset
   403
 *  <h3>This is bigger text.</h3>
hgs
parents: 66
diff changeset
   404
 *  This is the body text.
hgs
parents: 66
diff changeset
   405
 *  </div></body></html> 
hgs
parents: 66
diff changeset
   406
 *  
hgs
parents: 66
diff changeset
   407
 */
hgs
parents: 66
diff changeset
   408
void NmEditorContent::createDivisionFromHead(QString &bodyContent)
hgs
parents: 66
diff changeset
   409
{
hgs
parents: 66
diff changeset
   410
    NM_FUNCTION;
hgs
parents: 66
diff changeset
   411
    
hgs
parents: 66
diff changeset
   412
    // Regular expression string for searching <head> part that contains <style>
hgs
parents: 66
diff changeset
   413
    QString bodyStyleDefined(
hgs
parents: 66
diff changeset
   414
        "<head"
hgs
parents: 66
diff changeset
   415
        "(\\s)*"    // 0-* white spaces, browsers seem to accept this too.
hgs
parents: 66
diff changeset
   416
        ">"
hgs
parents: 66
diff changeset
   417
        "(.)*"      // 0-* any characters
hgs
parents: 66
diff changeset
   418
        "<style"
hgs
parents: 66
diff changeset
   419
        "(.)*"      // 0-* any characters
hgs
parents: 66
diff changeset
   420
        "</head"
hgs
parents: 66
diff changeset
   421
        "(\\s)*"    // 0-* white spaces
hgs
parents: 66
diff changeset
   422
        ">");
hgs
parents: 66
diff changeset
   423
    QRegExp bodyStyleDefinedRegExp(bodyStyleDefined, Qt::CaseInsensitive);
hgs
parents: 66
diff changeset
   424
    bodyStyleDefinedRegExp.setMinimal(true);
hgs
parents: 66
diff changeset
   425
    
hgs
parents: 66
diff changeset
   426
    QString bodyStartReplacement("<body>\n<div");
hgs
parents: 66
diff changeset
   427
    
74
hgs
parents: 68
diff changeset
   428
    if (bodyContent.contains(bodyStyleDefinedRegExp)) {
68
hgs
parents: 66
diff changeset
   429
        QString headPartString = bodyStyleDefinedRegExp.cap(0);
hgs
parents: 66
diff changeset
   430
        QString headBodyStyleString("body(\\s)*");
hgs
parents: 66
diff changeset
   431
        QRegExp bodyStyleReplacementRegExp(headBodyStyleString, Qt::CaseInsensitive);
hgs
parents: 66
diff changeset
   432
        
74
hgs
parents: 68
diff changeset
   433
        if (headPartString.contains(bodyStyleReplacementRegExp)) {
68
hgs
parents: 66
diff changeset
   434
            headPartString.replace(bodyStyleReplacementRegExp, "div.reply ");
hgs
parents: 66
diff changeset
   435
            bodyContent.replace(bodyStyleDefinedRegExp, headPartString);
hgs
parents: 66
diff changeset
   436
            
hgs
parents: 66
diff changeset
   437
            bodyStartReplacement = "<body>\n<div class=\"reply\"";
hgs
parents: 66
diff changeset
   438
        }
hgs
parents: 66
diff changeset
   439
    }
hgs
parents: 66
diff changeset
   440
    
hgs
parents: 66
diff changeset
   441
    convertBodyToDiv(bodyContent, bodyStartReplacement);
hgs
parents: 66
diff changeset
   442
}
hgs
parents: 66
diff changeset
   443
hgs
parents: 66
diff changeset
   444
/*!
hgs
parents: 66
diff changeset
   445
 *  Renames <body...></body> to <div...></div> and creates new <body> element.
hgs
parents: 66
diff changeset
   446
 *  
hgs
parents: 66
diff changeset
   447
 *  Example:
hgs
parents: 66
diff changeset
   448
 *  <body style="background: #00ff00; color: red; border: solid">
hgs
parents: 66
diff changeset
   449
 *  This is the body text.
hgs
parents: 66
diff changeset
   450
 *  </body>
hgs
parents: 66
diff changeset
   451
 *
hgs
parents: 66
diff changeset
   452
 *      ---->
hgs
parents: 66
diff changeset
   453
 *  
hgs
parents: 66
diff changeset
   454
 *  <body>    
hgs
parents: 66
diff changeset
   455
 *  <div style="background: #00ff00; color: red; border: solid">
hgs
parents: 66
diff changeset
   456
 *  This is the body text.
hgs
parents: 66
diff changeset
   457
 *  </div>
hgs
parents: 66
diff changeset
   458
 *  </body>
hgs
parents: 66
diff changeset
   459
 */
hgs
parents: 66
diff changeset
   460
void NmEditorContent::convertBodyToDiv(QString &bodyContent, const QString &replacementString)
hgs
parents: 66
diff changeset
   461
{
hgs
parents: 66
diff changeset
   462
    NM_FUNCTION;
hgs
parents: 66
diff changeset
   463
    
hgs
parents: 66
diff changeset
   464
    QString bodyStart("<body");
hgs
parents: 66
diff changeset
   465
    QString bodyEnd("</body>");
hgs
parents: 66
diff changeset
   466
    QString bodyEndReplacement("</div></body>");
hgs
parents: 66
diff changeset
   467
    QRegExp bodyStartRegExp(bodyStart, Qt::CaseInsensitive);
hgs
parents: 66
diff changeset
   468
    QRegExp bodyEndRegExp(bodyEnd, Qt::CaseInsensitive);
hgs
parents: 66
diff changeset
   469
    
hgs
parents: 66
diff changeset
   470
    bodyContent.replace(bodyStartRegExp, replacementString);
hgs
parents: 66
diff changeset
   471
    bodyContent.replace(bodyEndRegExp, bodyEndReplacement);    
hgs
parents: 66
diff changeset
   472
}
hgs
parents: 66
diff changeset
   473
hgs
parents: 66
diff changeset
   474
/*!
hgs
parents: 66
diff changeset
   475
 *  Converts "body bgcolor=#color" attribute to "body style=background: #color".
hgs
parents: 66
diff changeset
   476
 *  
hgs
parents: 66
diff changeset
   477
 *  Example:
hgs
parents: 66
diff changeset
   478
 *  <body bgcolor="blue"></body> 
hgs
parents: 66
diff changeset
   479
 *  
hgs
parents: 66
diff changeset
   480
 *              ----->
hgs
parents: 66
diff changeset
   481
 *  
hgs
parents: 66
diff changeset
   482
 *  <body style="background:blue"></div></body>
hgs
parents: 66
diff changeset
   483
 */
hgs
parents: 66
diff changeset
   484
void NmEditorContent::convertBGColorToStyle(QString &bodyContent)
hgs
parents: 66
diff changeset
   485
{
hgs
parents: 66
diff changeset
   486
    NM_FUNCTION;
hgs
parents: 66
diff changeset
   487
    
74
hgs
parents: 68
diff changeset
   488
    QString bgColorInBodyFetchString("<body"
hgs
parents: 68
diff changeset
   489
        "[^<]+"             // 1...* any character except '<'
hgs
parents: 68
diff changeset
   490
        "(bgcolor"
hgs
parents: 68
diff changeset
   491
        "(\\s|)"            // White space or nothing
hgs
parents: 68
diff changeset
   492
        "="
hgs
parents: 68
diff changeset
   493
        "(\\s|)"            // White space or nothing
hgs
parents: 68
diff changeset
   494
        "(\"|)"             // '"' or nothing
hgs
parents: 68
diff changeset
   495
        "(#|))"             // '#' or nothing
hgs
parents: 68
diff changeset
   496
        "*>");              // 0...* Any character + >
68
hgs
parents: 66
diff changeset
   497
    QRegExp bgColorInBodyRegExp(bgColorInBodyFetchString, Qt::CaseInsensitive);
hgs
parents: 66
diff changeset
   498
    
74
hgs
parents: 68
diff changeset
   499
    if (bodyContent.contains(bgColorInBodyRegExp)) {
68
hgs
parents: 66
diff changeset
   500
        // There can be only one meaningful bgcolor, the first one.
hgs
parents: 66
diff changeset
   501
        QString bgColorString = bgColorInBodyRegExp.cap(0);
hgs
parents: 66
diff changeset
   502
        
hgs
parents: 66
diff changeset
   503
        // Extract the color code from the string
hgs
parents: 66
diff changeset
   504
        QString colorCode(bgColorString);
hgs
parents: 66
diff changeset
   505
        QRegExp removeBeginningRegExp(
hgs
parents: 66
diff changeset
   506
            "<body"
hgs
parents: 66
diff changeset
   507
            "[^<]+"         // 1...* any character except '<'
hgs
parents: 66
diff changeset
   508
            "bgcolor"
hgs
parents: 66
diff changeset
   509
            "(\\s|)"        // White space or nothing
hgs
parents: 66
diff changeset
   510
            "="
hgs
parents: 66
diff changeset
   511
            "(\\s|)"        // White space or nothing
hgs
parents: 66
diff changeset
   512
            "(\"|)"         // '"' or nothing
hgs
parents: 66
diff changeset
   513
            "(#|)",         // '#' or nothing
hgs
parents: 66
diff changeset
   514
            Qt::CaseInsensitive);
74
hgs
parents: 68
diff changeset
   515
        
68
hgs
parents: 66
diff changeset
   516
        colorCode.remove(removeBeginningRegExp);
hgs
parents: 66
diff changeset
   517
        
hgs
parents: 66
diff changeset
   518
        QRegExp removeEndRegExp(""
hgs
parents: 66
diff changeset
   519
            "((\"|\\s)"     // '"' or white space
hgs
parents: 66
diff changeset
   520
            "([^<]*)"       // 0...* any characters except '<'
hgs
parents: 66
diff changeset
   521
            ">)"            // '>'           
hgs
parents: 66
diff changeset
   522
            "|>");          // ... or nothing before this, just '>'
74
hgs
parents: 68
diff changeset
   523
        
68
hgs
parents: 66
diff changeset
   524
        colorCode.remove(removeEndRegExp);
hgs
parents: 66
diff changeset
   525
        
74
hgs
parents: 68
diff changeset
   526
        QString plainBgColorFetchString("bgcolor"
hgs
parents: 68
diff changeset
   527
            "(\\s|)*"       // 0...* White space or nothing
hgs
parents: 68
diff changeset
   528
            "="             
hgs
parents: 68
diff changeset
   529
            "(\\s|)*"       // 0...* White space or nothing
hgs
parents: 68
diff changeset
   530
            "[^\\s]+"       // 1...* any character except white space
hgs
parents: 68
diff changeset
   531
            "(\\s|(?=>))"); // White space or follower by '>'
hgs
parents: 68
diff changeset
   532
        
68
hgs
parents: 66
diff changeset
   533
        QString bgColorReplacement("style=\"background: #"+colorCode+"\" ");
hgs
parents: 66
diff changeset
   534
        QRegExp plainBgColorFetchRegExp(plainBgColorFetchString, Qt::CaseInsensitive);
hgs
parents: 66
diff changeset
   535
        
hgs
parents: 66
diff changeset
   536
        // Create temporary string for bgcolor
hgs
parents: 66
diff changeset
   537
        bgColorString.replace(plainBgColorFetchRegExp, bgColorReplacement);
hgs
parents: 66
diff changeset
   538
        
hgs
parents: 66
diff changeset
   539
        // Copy the contents of the temporary string to the message
hgs
parents: 66
diff changeset
   540
        bodyContent.replace(bgColorInBodyRegExp, bgColorString);
hgs
parents: 66
diff changeset
   541
    }    
hgs
parents: 66
diff changeset
   542
}
74
hgs
parents: 68
diff changeset
   543
hgs
parents: 68
diff changeset
   544
/*!
hgs
parents: 68
diff changeset
   545
 *  Listen to the input method events (eg. cursor position changes, preedit text changes etc.) and 
hgs
parents: 68
diff changeset
   546
 *  invoke ensureCursorVisibility. Qt FW does not automatically ensure the visiblity of a text in a
hgs
parents: 68
diff changeset
   547
 *  edit widget in our custom scroll area. Filtering events is the only way to get events of the
hgs
parents: 68
diff changeset
   548
 *  preedit text changes.
hgs
parents: 68
diff changeset
   549
 */
hgs
parents: 68
diff changeset
   550
bool NmEditorContent::eventFilter(QObject *obj, QEvent *event)
hgs
parents: 68
diff changeset
   551
{
hgs
parents: 68
diff changeset
   552
    // let the FW handle the event first
hgs
parents: 68
diff changeset
   553
    bool ret = QObject::eventFilter(obj, event);
hgs
parents: 68
diff changeset
   554
    
hgs
parents: 68
diff changeset
   555
    if (event && event->type() == QEvent::InputMethod) {
hgs
parents: 68
diff changeset
   556
        QMetaObject::invokeMethod(this, "ensureCursorVisibility", Qt::QueuedConnection);
hgs
parents: 68
diff changeset
   557
    }
hgs
parents: 68
diff changeset
   558
    
hgs
parents: 68
diff changeset
   559
    return ret;
hgs
parents: 68
diff changeset
   560
}
hgs
parents: 68
diff changeset
   561