logsui/logscntfinder/src/logspredictive12keytranslator.cpp
changeset 17 90fe74753f71
parent 15 76d2cf7a585e
child 21 2f0af9ba7665
equal deleted inserted replaced
15:76d2cf7a585e 17:90fe74753f71
    15 *
    15 *
    16 */
    16 */
    17 #include <QObject>
    17 #include <QObject>
    18 #include <QLocale>
    18 #include <QLocale>
    19 #include <QHash>
    19 #include <QHash>
       
    20 #include <QRegExp>
    20 #include <hbinputkeymapfactory.h>
    21 #include <hbinputkeymapfactory.h>
    21 #include <hbinputkeymap.h>
    22 #include <hbinputkeymap.h>
    22 #include <hbinputsettingproxy.h>
    23 #include <hbinputsettingproxy.h>
    23 
    24 
    24 #include "logspredictive12keytranslator.h"
    25 #include "logspredictive12keytranslator.h"
    25 #include "logslogger.h"
    26 #include "logslogger.h"
    26 
    27 
    27 const QChar ZeroSepar('0');
    28 const QChar ZeroSepar('0');
    28 
    29 const int NotAssigned = -1;
    29 
    30 
    30 // -----------------------------------------------------------------------------
    31 // -----------------------------------------------------------------------------
    31 // LogsPredictive12KeyTranslator::LogsPredictive12KeyTranslator()
    32 // LogsPredictive12KeyTranslator::LogsPredictive12KeyTranslator()
    32 // -----------------------------------------------------------------------------
    33 // -----------------------------------------------------------------------------
    33 //
    34 //
    54 ~LogsPredictive12KeyTranslator()" )
    55 ~LogsPredictive12KeyTranslator()" )
    55     
    56     
    56 }
    57 }
    57 
    58 
    58 // -----------------------------------------------------------------------------
    59 // -----------------------------------------------------------------------------
    59 // LogsPredictive12KeyTranslator::LogsPredictive12KeyTranslator()
    60 // LogsPredictive12KeyTranslator::patternTokens()
    60 // -----------------------------------------------------------------------------
    61 // -----------------------------------------------------------------------------
    61 //
    62 //
    62 QStringList LogsPredictive12KeyTranslator::patternTokens( const QString& pattern ) const
    63 QStringList LogsPredictive12KeyTranslator::patternTokens( const QString& pattern ) const
    63 {
    64 {
       
    65         
    64     LOGS_QDEBUG( "logs [FINDER] -> LogsPredictive12KeyTranslator::\
    66     LOGS_QDEBUG( "logs [FINDER] -> LogsPredictive12KeyTranslator::\
    65 patternTokens()" )
    67 patternTokens()" )
    66     LOGS_QDEBUG_2( "logs [FINDER] pattern ", pattern );
    68     LOGS_QDEBUG_2( "logs [FINDER] pattern ", pattern );
    67 
    69     QString car;
    68     QStringList target = pattern.split( ZeroSepar, QString::SkipEmptyParts );
    70     QString cdr;
    69     if ( target.length() > 1 ) {
    71     
    70         LOGS_QDEBUG( "logs [FINDER] has separator(s) " )
    72     QStringList target;
    71         QString& first = target[0];
    73     splitPattern( pattern, car, cdr );
    72         QString& last = target[target.length()-1];
    74     if ( car.length() ) {
    73         padWithZeros( first, pattern, 0 );
    75         target.append( car );
    74         padWithZeros( last, pattern, last.length() );
    76         if ( cdr.length() ) {
    75     } else if ( target.length() == 1 && //0280 -> 028
    77             target.append( cdr );
    76                 pattern[pattern.length()-1] == ZeroSepar ) {
    78         }
    77         LOGS_QDEBUG( "logs [FINDER] no separators, trailing zero(s) " )
       
    78         QString& first = target[0];
       
    79         padWithZeros( first, pattern, 0 );
       
    80     } else if ( target.length() == 0 ) {
       
    81         LOGS_QDEBUG( "logs [FINDER] only separators " )
       
    82         target.append( ZeroSepar );
       
    83     }
    79     }
    84     LOGS_QDEBUG( "logs [FINDER] <- LogsPredictive12KeyTranslator::\
    80     LOGS_QDEBUG( "logs [FINDER] <- LogsPredictive12KeyTranslator::\
    85 patternTokens()" )
    81 patternTokens()" )
    86     return target;
    82     return target;
    87 }
    83 }
       
    84 
       
    85 // -----------------------------------------------------------------------------
       
    86 // LogsPredictive12KeyTranslator::splitPattern()
       
    87 // -----------------------------------------------------------------------------
       
    88 //
       
    89 void LogsPredictive12KeyTranslator::splitPattern( const QString& pattern, 
       
    90                                                   QString& car, QString& cdr ) const
       
    91 {
       
    92     car = "";
       
    93     cdr = "";
       
    94     
       
    95     QChar current;
       
    96     QChar previous;
       
    97     int splitStart = NotAssigned;
       
    98     int splitEnd = NotAssigned;
       
    99     int index = 0;
       
   100     while( splitEnd == NotAssigned && index < pattern.length() ) {
       
   101         current = pattern[index];
       
   102         splitStart = splitStart == NotAssigned &&
       
   103                     ( previous != ZeroSepar && previous != QChar() ) && 
       
   104                     current == ZeroSepar ? 
       
   105                         index : splitStart;
       
   106         splitEnd = splitStart != NotAssigned && 
       
   107                    previous == ZeroSepar && 
       
   108                    current != ZeroSepar ?
       
   109                       index : splitEnd;
       
   110         previous = current;
       
   111         index++;
       
   112     }
       
   113     
       
   114     if ( splitStart != NotAssigned && splitEnd != NotAssigned ) {
       
   115         car = pattern.left( splitStart );
       
   116         cdr = pattern.right( pattern.length() - splitEnd );  
       
   117     } else {
       
   118         car = pattern; 
       
   119     }
       
   120 }
       
   121 
       
   122 // -----------------------------------------------------------------------------
       
   123 // LogsPredictive12KeyTranslator::trimPattern()
       
   124 // -----------------------------------------------------------------------------
       
   125 //
       
   126 QString& LogsPredictive12KeyTranslator::trimPattern( QString& pattern, 
       
   127                                                      bool tailOnly ) const
       
   128 {
       
   129     QRegExp lead("^0*");//remove leading zeros
       
   130     QRegExp trail("0*$");//remove trailing zeros
       
   131     
       
   132     if ( pattern.length() ) {
       
   133         if ( !tailOnly ) {
       
   134             pattern.remove( lead );
       
   135         }
       
   136         
       
   137         pattern.remove( trail );
       
   138         
       
   139         if( !pattern.length() ) {
       
   140             pattern += ZeroSepar;
       
   141         }
       
   142     }
       
   143     return pattern;
       
   144 }
       
   145 
    88 
   146 
    89 // -----------------------------------------------------------------------------
   147 // -----------------------------------------------------------------------------
    90 // LogsPredictive12KeyTranslator::hasPatternSeparators()
   148 // LogsPredictive12KeyTranslator::hasPatternSeparators()
    91 // -----------------------------------------------------------------------------
   149 // -----------------------------------------------------------------------------
    92 //
   150 //
   110     }
   168     }
   111     return mappedKey ? mappedKey->keycode : QChar();
   169     return mappedKey ? mappedKey->keycode : QChar();
   112 }
   170 }
   113 
   171 
   114 
   172 
   115 
       
   116 // -----------------------------------------------------------------------------
       
   117 // LogsPredictive12KeyTranslator::padWithLeadingZeros()
       
   118 // -----------------------------------------------------------------------------
       
   119 //
       
   120 void LogsPredictive12KeyTranslator::padWithZeros( QString& token, 
       
   121                                      const QString& source, int padIndex ) const
       
   122 {
       
   123     const QChar* content = source.data();
       
   124     int index = !padIndex ? 0 : source.length()-1;
       
   125             
       
   126     while( index >= 0 && index < source.length() ) {
       
   127         if ( content[ index ] == ZeroSepar ) {
       
   128             token.insert( padIndex, ZeroSepar );
       
   129             index = !padIndex ? index+1 : index-1;
       
   130         } else {
       
   131             index = -1;
       
   132         }
       
   133     }
       
   134 }
       
   135