phonebookui/cntlistmodel/cntdisplaytextformatter.cpp
changeset 81 640d30f4fb64
equal deleted inserted replaced
77:c18f9fa7f42e 81:640d30f4fb64
       
     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:  
       
    15 *
       
    16 */
       
    17 
       
    18 #include "cntdisplaytextformatter.h"
       
    19 #include <qcontactdetailfilter.h>
       
    20 #include <qcontactdisplaylabel.h>
       
    21 #include <hbcolorscheme.h>
       
    22 #include <cntdebug.h>
       
    23 #include <QStringList>
       
    24 
       
    25 CntHTMLDisplayTextFormatter::CntHTMLDisplayTextFormatter()
       
    26 {
       
    27 }
       
    28 
       
    29 CntHTMLDisplayTextFormatter::~CntHTMLDisplayTextFormatter()
       
    30 {
       
    31 }
       
    32    
       
    33 QString CntHTMLDisplayTextFormatter::formattedText( const QString aText, const QContactFilter& aCriteria )
       
    34 {
       
    35     CNT_LOG_ARGS( "filter:" << aText )
       
    36     
       
    37     QString foundPattern;
       
    38     QString pattern;
       
    39     QString tempDash;
       
    40     QString tempSpace;
       
    41     const QChar dash = '-';
       
    42     const QChar space = ' ';
       
    43     
       
    44     if ( aCriteria.type() == QContactFilter::ContactDetailFilter ) 
       
    45     {
       
    46         const QContactDetailFilter& filter = static_cast<const QContactDetailFilter&>( aCriteria );
       
    47         if ( filter.detailDefinitionName() == QContactDisplayLabel::DefinitionName && 
       
    48              filter.matchFlags() & QContactFilter::MatchStartsWith )
       
    49         {
       
    50             QString formattedText;
       
    51             // go through the words (e.g. Lastname, Firstname) and apply list of pattern to them.
       
    52             foreach ( QString text, aText.split(QRegExp("\\s+"), QString::SkipEmptyParts) )
       
    53             {
       
    54                 foundPattern = "";
       
    55                 bool match( false );
       
    56                 // go through every search criteria word
       
    57                 foreach (pattern, filter.value().toStringList())
       
    58                 {
       
    59                     tempDash = pattern;
       
    60                     tempSpace = pattern;
       
    61                     tempDash.insert(0, dash);
       
    62                     tempSpace.insert(0, space);
       
    63                     
       
    64                     if ( text.startsWith(pattern, Qt::CaseInsensitive) )
       
    65                     {
       
    66                         match = true;
       
    67                         if (pattern.length() > foundPattern.length())
       
    68                             foundPattern = pattern;
       
    69                     }
       
    70                 }
       
    71                 
       
    72                 // if no match found, original text is returned
       
    73                 if ( !match )
       
    74                     formattedText.append( text );
       
    75                 // if match is found then the longest variation of the pattern is high lighted, e.g. "a ab" 
       
    76                 else
       
    77                 {
       
    78                     insertTag( text, foundPattern.length() );
       
    79                     formattedText.append( text );
       
    80                 }
       
    81                 
       
    82                 // put spaces back between words (split() looses them)
       
    83                 formattedText.append( " " );
       
    84             }
       
    85             return formattedText.trimmed();
       
    86         }
       
    87     }
       
    88     return aText;
       
    89 }
       
    90 
       
    91 void CntHTMLDisplayTextFormatter::insertTag( QString& aText, int aChars )
       
    92 {
       
    93     QColor highlight = HbColorScheme::color("qtc_lineedit_marker_normal");
       
    94     QColor color = HbColorScheme::color("qtc_lineedit_selected");
       
    95     
       
    96     QString start = QString(TAG_START).arg( highlight.name().toUpper() ).arg(color.name().toUpper());
       
    97     aText.prepend( start );
       
    98     aText.insert( start.length() + aChars, TAG_END );
       
    99 }
       
   100 // End of File