messagingapp/msgui/unifiedviewer/src/unihighlighter.cpp
branchGCC_SURGE
changeset 47 5b14749788d7
parent 35 a32b19fb291e
parent 44 36f374c67aa8
equal deleted inserted replaced
35:a32b19fb291e 47:5b14749788d7
     1 /*
       
     2  * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3  * All rights reserved.
       
     4  * This component and the accompanying materials are made available
       
     5  * under the terms of "Eclipse Public License v1.0"
       
     6  * which accompanies this distribution, and is available
       
     7  * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8  *
       
     9  * Initial Contributors:
       
    10  * Nokia Corporation - initial contribution.
       
    11  *
       
    12  * Contributors:
       
    13  *
       
    14  * Description: Enables parsing and change font parameters for email/url
       
    15  *
       
    16  */
       
    17 #include "unihighlighter.h"
       
    18 
       
    19 #include <QtGui>
       
    20 
       
    21 //---------------------------------------------------------------
       
    22 //UniHighlighter :: UniHighlighter
       
    23 // @see header file
       
    24 //---------------------------------------------------------------
       
    25 UniHighlighter::UniHighlighter(QTextDocument *parent) :
       
    26     QSyntaxHighlighter(parent)
       
    27 {
       
    28     HighlightingRule rule;
       
    29 
       
    30     emailFormat.setFontWeight(QFont::Bold);
       
    31     rule.pattern = QRegExp(
       
    32         "\\b[a-zA-Z0-9._%-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,4}\\b");
       
    33     rule.format = emailFormat;
       
    34     highlightingRules.append(rule);
       
    35 
       
    36     urlFormat.setAnchor(true);
       
    37     urlFormat.setFontUnderline(true);
       
    38     rule.pattern
       
    39             = QRegExp(
       
    40                 "\\b((https?|ftp|gopher|telnet|file|notes|ms-help):((//)|(\\\\))+[\\w\\d:#@%/;$()~_?\\+-=\\\\.&]*)\\b");
       
    41     rule.format = urlFormat;
       
    42     highlightingRules.append(rule);
       
    43 }
       
    44 
       
    45 //---------------------------------------------------------------
       
    46 //UniHighlighter :: highlightBlock
       
    47 // @see header file
       
    48 //---------------------------------------------------------------
       
    49 void UniHighlighter::highlightBlock(const QString &text)
       
    50 {
       
    51     foreach (const HighlightingRule &rule, highlightingRules) 
       
    52         {
       
    53             QRegExp expression(rule.pattern);
       
    54             int index = text.indexOf(expression);
       
    55             while (index >= 0)
       
    56             {
       
    57                 int length = expression.matchedLength();
       
    58                 setFormat(index, length, rule.format);
       
    59                 index = text.indexOf(expression, index + length);
       
    60             }
       
    61         }
       
    62 }
       
    63 
       
    64 // EOF