messagingapp/msgui/unifiededitor/src/msgunifiededitorlineedit.cpp
changeset 25 84d9eb65b26f
parent 23 238255e8b033
child 27 e4592d119491
equal deleted inserted replaced
23:238255e8b033 25:84d9eb65b26f
    31 const int SNAP_DELAY = 350;
    31 const int SNAP_DELAY = 350;
    32 
    32 
    33 MsgUnifiedEditorLineEdit::MsgUnifiedEditorLineEdit(const QString& label,QGraphicsItem *parent):
    33 MsgUnifiedEditorLineEdit::MsgUnifiedEditorLineEdit(const QString& label,QGraphicsItem *parent):
    34 HbLineEdit(parent),
    34 HbLineEdit(parent),
    35 mSelectionStart(-1),
    35 mSelectionStart(-1),
    36 mSelectionEnd(-1)
    36 mSelectionEnd(-1),
    37 {
    37 mDefaultBehaviour(false)
       
    38 {
       
    39     QString labelStr = label.trimmed();
       
    40     
    38     QTextCursor cursor(this->textCursor());
    41     QTextCursor cursor(this->textCursor());
    39     QTextCharFormat colorFormat(cursor.charFormat());
    42     QTextCharFormat colorFormat(cursor.charFormat());
    40    
    43    
    41     QColor fgColor = colorFormat.foreground().color();     
    44     QColor fgColor = this->palette().color(QPalette::Text);     
    42     fgColor.setAlpha(fadedAlpha);
    45     fgColor.setAlpha(fadedAlpha);
    43     colorFormat.setForeground(fgColor);
    46     colorFormat.setForeground(fgColor);
    44     cursor.insertText(label , colorFormat);
    47     cursor.insertText(labelStr , colorFormat);
    45 
    48 
    46     fgColor.setAlpha(solidAlpha);
    49     fgColor.setAlpha(solidAlpha);
    47     colorFormat.setForeground(fgColor);
    50     colorFormat.setForeground(fgColor);
    48 
    51 
    49     cursor.insertText(" ",colorFormat);    
    52     cursor.insertText(" ",colorFormat);    
    50 
    53 
    51     mLabelExpr.setPattern(QString("^"+label+" $"));
    54     mLabelExpr.setPattern(QString("^"+labelStr+" $"));
    52     mLabel = label+" ";
    55     mLabel = labelStr+" ";
    53 
    56 
    54     connect(this,SIGNAL(selectionChanged(QTextCursor,QTextCursor)),
    57     connect(this,SIGNAL(selectionChanged(QTextCursor,QTextCursor)),
    55             this,SLOT(selectionChanged(QTextCursor,QTextCursor)));
    58             this,SLOT(selectionChanged(QTextCursor,QTextCursor)));
    56     connect(this, SIGNAL(textChanged(QString)), this, SLOT(onTextChanged(QString)));
    59     connect(this, SIGNAL(contentsChanged()), this, SLOT(onContentsChanged()));
    57 }
    60 }
    58 
    61 
    59 MsgUnifiedEditorLineEdit::~MsgUnifiedEditorLineEdit()
    62 MsgUnifiedEditorLineEdit::~MsgUnifiedEditorLineEdit()
    60 {
    63 {
    61 }
    64 }
    62 
    65 
    63 void MsgUnifiedEditorLineEdit::inputMethodEvent(QInputMethodEvent *event)
    66 void MsgUnifiedEditorLineEdit::inputMethodEvent(QInputMethodEvent *event)
    64 {
    67 {
       
    68     //let it go in default way.
       
    69     if(mDefaultBehaviour)
       
    70     {
       
    71         HbAbstractEdit::inputMethodEvent(event);
       
    72         event->accept();
       
    73         return;
       
    74     }
       
    75 
    65     if (!event->commitString().isEmpty() || event->replacementLength())
    76     if (!event->commitString().isEmpty() || event->replacementLength())
    66     {
    77     {
    67         if (event->commitString().contains(expr))
    78         if (event->commitString().contains(expr))
    68         {
    79         {
    69             if(this->text().isEmpty() || this->text().contains(sepAtEnd) || this->text().contains(mLabelExpr))
    80             if(this->text().isEmpty() || this->text().contains(sepAtEnd) || this->text().contains(mLabelExpr))
    93 {
   104 {
    94     QString str = event->text();
   105     QString str = event->text();
    95 
   106 
    96     if(event->key()== Qt::Key_Enter || event->key()== Qt::Key_Return)
   107     if(event->key()== Qt::Key_Enter || event->key()== Qt::Key_Return)
    97     {
   108     {
       
   109         if(mDefaultBehaviour)
       
   110         {
       
   111             HbAbstractEdit::keyReleaseEvent(event);
       
   112             event->accept();
       
   113             return;
       
   114         }
    98         if(this->text().isEmpty() || this->text().contains(sepAtEnd) || this->text().contains(mLabelExpr))
   115         if(this->text().isEmpty() || this->text().contains(sepAtEnd) || this->text().contains(mLabelExpr))
    99         {
   116         {
   100             event->accept();
   117             event->accept();
   101             return;
   118             return;
   102         }
   119         }
   267         return;
   284         return;
   268     }
   285     }
   269 
   286 
   270     if(!str.isEmpty())
   287     if(!str.isEmpty())
   271     {
   288     {
       
   289         if(mDefaultBehaviour)
       
   290         {
       
   291             HbAbstractEdit::keyPressEvent(event);
       
   292             event->accept();
       
   293             return;
       
   294         }
   272         if (str.contains(expr))
   295         if (str.contains(expr))
   273         {
   296         {
   274             if(this->text().isEmpty() || this->text().contains(sepAtEnd) || this->text().contains(mLabelExpr))
   297             if(this->text().isEmpty() || this->text().contains(sepAtEnd) || this->text().contains(mLabelExpr))
   275             {
   298             {
   276                 event->accept();
   299                 event->accept();
   335     event->accept();
   358     event->accept();
   336 }
   359 }
   337 
   360 
   338 void MsgUnifiedEditorLineEdit::setText(const QString& text)
   361 void MsgUnifiedEditorLineEdit::setText(const QString& text)
   339 {
   362 {
   340     QInputMethodEvent e;
   363 
   341     //make sure previous text is complete.
   364     if(!mDefaultBehaviour)
   342     e.setCommitString(";");
   365     {
   343     this->inputMethodEvent(&e);
   366         QInputMethodEvent e;
   344     this->setCursorPosition(this->text().length());
   367         //make sure previous text is complete.
   345 
   368         e.setCommitString(";");
   346     QTextCursor cursor(this->textCursor());
   369         this->inputMethodEvent(&e);
   347     QTextCharFormat colorFormat(cursor.charFormat());
   370         this->setCursorPosition(this->text().length());
   348     QColor fgColor = colorFormat.foreground().color();     
   371 
   349     fgColor.setAlpha(fadedAlpha);
   372         QTextCursor cursor(this->textCursor());
   350     colorFormat.setUnderlineColor(fgColor);
   373         QTextCharFormat colorFormat(cursor.charFormat());
   351 
   374         QColor fgColor = colorFormat.foreground().color();
   352     colorFormat.setFontUnderline(true);  
   375         fgColor.setAlpha(fadedAlpha);
   353     cursor.insertText(text , colorFormat);
   376         colorFormat.setUnderlineColor(fgColor);
   354     colorFormat.setFontUnderline(false);
   377 
   355     
   378         colorFormat.setFontUnderline(true);
   356     cursor.insertText(replacementStr,colorFormat);
   379         cursor.insertText(text , colorFormat);
       
   380         colorFormat.setFontUnderline(false);
       
   381 
       
   382         cursor.insertText(replacementStr,colorFormat);       
       
   383     }
       
   384     else
       
   385     {
       
   386        this->setCursorPosition(this->text().length());
       
   387        QTextCursor cursor(this->textCursor());
       
   388        cursor.insertText(text);
       
   389     }
   357     
   390     
   358     this->setCursorVisibility(Hb::TextCursorHidden);
   391     this->setCursorVisibility(Hb::TextCursorHidden);
   359 }
   392 }
   360 
   393 
   361 QStringList MsgUnifiedEditorLineEdit::addresses()
   394 QStringList MsgUnifiedEditorLineEdit::addresses()
   362 {    
   395 {
   363     QString text = this->text();
   396     QString text = this->content();
   364     text = text.remove(mLabel);
       
   365     QStringList list = text.split(replacementStr,QString::SkipEmptyParts);
   397     QStringList list = text.split(replacementStr,QString::SkipEmptyParts);
   366     //list.removeDuplicates();
       
   367     return list;
   398     return list;
   368 }
   399 }
   369 
   400 
   370 void MsgUnifiedEditorLineEdit::focusInEvent(QFocusEvent* event)
   401 void MsgUnifiedEditorLineEdit::focusInEvent(QFocusEvent* event)
   371 {    
   402 {    
   372     HbAbstractEdit::focusInEvent(event);
   403     HbLineEdit::focusInEvent(event);
   373     this->setCursorVisibility(Hb::TextCursorVisible);
   404     this->setCursorVisibility(Hb::TextCursorVisible);
   374 }
   405 }
   375 
   406 
   376 void MsgUnifiedEditorLineEdit::setHighlight(int currentPos)
   407 void MsgUnifiedEditorLineEdit::setHighlight(int currentPos)
   377 {
   408 {
   430     {
   461     {
   431         this->setTextCursor(oldCursor);
   462         this->setTextCursor(oldCursor);
   432         return;
   463         return;
   433     }
   464     }
   434 
   465 
   435     mSelectionStart  = newCursor.selectionStart();
   466     if(!mDefaultBehaviour)
   436     mSelectionEnd    = newCursor.selectionEnd();
   467         {
   437 
   468         mSelectionStart  = newCursor.selectionStart();
   438     if(mSelectionStart == mSelectionEnd )
   469         mSelectionEnd    = newCursor.selectionEnd();
   439     {
   470 
   440         return;
   471         if(mSelectionStart == mSelectionEnd )
   441     }
   472             {
   442 
   473             return;
   443     mSelectionSnapTimer.start(SNAP_DELAY,this);
   474             }
   444 
   475     
       
   476         mSelectionSnapTimer.start(SNAP_DELAY,this);
       
   477         }
   445 }
   478 }
   446 
   479 
   447 void MsgUnifiedEditorLineEdit::timerEvent(QTimerEvent *event)
   480 void MsgUnifiedEditorLineEdit::timerEvent(QTimerEvent *event)
   448 {
   481 {
   449     //passing event to base class.
   482     //passing event to base class.
   476 
   509 
   477         event->accept();
   510         event->accept();
   478     }
   511     }
   479 }
   512 }
   480 
   513 
   481 void MsgUnifiedEditorLineEdit::onTextChanged(const QString& text)
   514 void MsgUnifiedEditorLineEdit::setDefaultBehaviour(bool defaultBehaviour)
   482 {
   515 {
   483     const QString changedText = const_cast<QString&>(text).remove(mLabel);
   516     mDefaultBehaviour = defaultBehaviour;
   484     emit addressTextChanged(changedText);
   517 }
   485 }
   518 
   486 
   519 QString MsgUnifiedEditorLineEdit::text() const
       
   520 {
       
   521     return HbLineEdit::text();
       
   522 }
       
   523 
       
   524 QString MsgUnifiedEditorLineEdit::content() const
       
   525 {
       
   526     QString text = this->text();
       
   527     text.remove(mLabel);
       
   528     return text;
       
   529 }
       
   530 
       
   531 void MsgUnifiedEditorLineEdit::onContentsChanged()
       
   532 {
       
   533     emit contentsChanged(content());
       
   534 }
   487 // eof
   535 // eof