[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-commits
Subject:    extragear/pim/mailody/src
From:       Tom Albers <toma () kde ! org>
Date:       2009-09-26 14:36:31
Message-ID: 1253975791.146684.31844.nullmailer () svn ! kde ! org
[Download RAW message or body]

SVN commit 1028297 by toma:

Use a QCompleter to create autocompletion when adding an address to the composer, \
using the recent address model.


 M  +17 -56    addresslineedit.cpp  
 M  +0 -49     addresslineedit.h  
 M  +37 -19    composer.cpp  
 M  +0 -2      composer.h  


--- trunk/extragear/pim/mailody/src/addresslineedit.cpp #1028296:1028297
@@ -27,44 +27,14 @@
 #include <kpimutils/email.h>
 
 #include <KColorScheme>
-#include <KDebug>
 
-//---------- Completion class --------------------//
-
 using namespace Mailody;
 
-Completion::Completion( KLineEdit *parent )
-        : KCompletion()
-{
-    m_box = new KCompletionBox( parent );
-}
-
-QString Completion::makeCompletion( const QString &text )
-{
-    QStringList results = substringCompletion( text );
-    if ( !results.isEmpty() ) {
-        m_box->setItems( results );
-        m_box->popup();
-    } else if ( m_box->isVisible() )
-        m_box->hide();
-
-    return QString();
-}
-
 //------------- AddressLineEdit -------------------//
 AddressLineEdit::AddressLineEdit( QWidget *parent )
         : KLineEdit( parent )
         , m_helpText( false )
 {
-    m_completion = new Completion( this );
-    m_completion->setOrder( KCompletion::Sorted );
-    m_completion->setIgnoreCase( true );
-    connect( m_completion->box(), SIGNAL( activated( const QString& ) ),
-             SLOT( slotSetAddress( const QString& ) ) );
-
-    setCompletionObject( m_completion );
-    setAutoDeleteCompletionObject( true );
-
     m_timer = new QTimer( this );
     m_timer->setSingleShot( true );
     connect( m_timer, SIGNAL( timeout() ), SLOT( slotShowHelp() ) );
@@ -72,23 +42,18 @@
 
 void AddressLineEdit::keyPressEvent( QKeyEvent *e )
 {
-    if ( help() && !( e->key() == Qt::Key_Return || e->key() == Qt::Key_Enter ) ) {
+    m_timer->stop();
+    if ( m_helpText && !( e->key() == Qt::Key_Return || e->key() == Qt::Key_Enter ) \
) {  clear();
         setHelp( false );
-        KLineEdit::keyPressEvent( e );
-        validate();
-    } else if ( help() && ( e->key() == Qt::Key_Return || e->key() == Qt::Key_Enter \
) ) +    } else if ( m_helpText && ( e->key() == Qt::Key_Return || e->key() == \
Qt::Key_Enter ) ) {  emit shiftAddress();
-    else if ( !help() && ( e->key() == Qt::Key_Return || e->key() == Qt::Key_Enter ) \
                ) {
-        if ( m_completion->box()->currentItem() && m_completion->boxVisible() ) {
-            emit addAddress( m_completion->box()->currentItem()->text() );
-            m_completion->box()->hide();
-        } else
-            emit addAddress( text() );
+    } else if ( !m_helpText && ( e->key() == Qt::Key_Return || e->key() == \
Qt::Key_Enter ) ) { +        emit addAddress( text() );
     } else {
-        KLineEdit::keyPressEvent( e );
         validate();
     }
+    KLineEdit::keyPressEvent( e );
 }
 
 void AddressLineEdit::mousePressEvent( QMouseEvent *e )
@@ -102,28 +67,24 @@
 
 void AddressLineEdit::validate()
 {
-    static bool oldResult = false;
     KPIMUtils::EmailParseResult result = KPIMUtils::isValidAddress( text() );
-    if ( result == KPIMUtils::AddressOk && !oldResult ) {
-        emit containsValidAddress( true );
-        oldResult = true;
-    } else if ( result != KPIMUtils::AddressOk && oldResult ) {
-        emit containsValidAddress( false );
-        oldResult = false;
-    }
+    emit containsValidAddress( result == KPIMUtils::AddressOk );
 
-    if ( result != KPIMUtils::AddressOk && !help() ) {
-        m_showHelp = emailParseResultToString( result );
+    if ( !help() )
         m_timer->start( 3000 );
-    }
 }
 
 void AddressLineEdit::slotShowHelp()
 {
-    /* seems fragile to me. I don't seem to understand the positioning of this \
                widget */
-    QPoint globalPos( mapToGlobal( mapToParent( pos() ) ) );
-    QPoint pos( globalPos.x(), globalPos.y()+5 );
-    QWhatsThis::showText( pos, m_showHelp );
+    KPIMUtils::EmailParseResult result = KPIMUtils::isValidAddress( text() );
+    if ( result != KPIMUtils::AddressOk && !help() ) {
+        const QString showHelp = emailParseResultToString( result );
+
+        /* seems fragile to me. I don't seem to understand the positioning of this \
widget */ +        QPoint globalPos( mapToGlobal( mapToParent( pos() ) ) );
+        QPoint pos( globalPos.x(), globalPos.y()+5 );
+        QWhatsThis::showText( pos, showHelp );
+    }
 }
 
 void AddressLineEdit::slotSetAddress( const QString &text )
--- trunk/extragear/pim/mailody/src/addresslineedit.h #1028296:1028297
@@ -30,47 +30,7 @@
 namespace Mailody
 {
 
-
 /**
- * @class Completion
- * This class provides the completion for the Composers AddressLineEdit
- * Its reimplemented to show a box to choose the address from.
- * @author Tom Albers <tomalbers@kde.nl>
- */
-class Completion : public KCompletion
-{
-public:
-    /**
-     * Constructor
-     */
-    explicit Completion( KLineEdit* );
-
-    /**
-     * reimplemented to show the completion box
-     */
-    virtual QString makeCompletion( const QString& );
-
-    /**
-     * returns a pointer to the completion box
-     */
-    KCompletionBox *box() const {
-        return m_box;
-    };
-
-    /**
-     * returns true if the box is currently shown
-     */
-    bool boxVisible()     const {
-        return !m_box->isHidden();
-    };
-
-private:
-    // @cond private
-    KCompletionBox *m_box;
-    // @endcond
-};
-
-/**
  * @class AddressLineEdit
  * This class provides Composers KLineEdit with auto completion
  * @author Tom Albers <tomalbers@kde.nl>
@@ -99,13 +59,6 @@
         return m_helpText;
     };
 
-    /**
-     * returns a pointer to the completion object
-     */
-    Completion *completion() const {
-        return m_completion;
-    };
-
 protected:
     /**
      * reimplemented to be able to remove the help text
@@ -119,9 +72,7 @@
 // @cond private
 private:
     bool        m_helpText;
-    Completion  *m_completion;
     QTimer      *m_timer;
-    QString     m_showHelp;
 
 private slots:
     void slotSetAddress( const QString& );
--- trunk/extragear/pim/mailody/src/composer.cpp #1028296:1028297
@@ -35,6 +35,7 @@
 
 // Qt
 #include <QApplication>
+#include <QCompleter>
 #include <QDockWidget>
 #include <QGridLayout>
 #include <QLabel>
@@ -126,7 +127,7 @@
     // signal coming from the dialog with detail info
     connect( tooltip,
              SIGNAL( mailClicked( const QString& ) ),
-             SLOT( slotAddAddressFromAddressBook( const QString& ) ) );
+             SLOT( slotAddAddress( const QString& ) ) );
 
     /* connect( view,
              SIGNAL( customContextMenuRequested( const QPoint & ) ),
@@ -175,7 +176,7 @@
     m_recentbook->setModel( m_recentModel );
 
     connect( m_recentbook,
-             SIGNAL( doubleClicked( const QModelIndex& ) ),
+             SIGNAL( clicked( const QModelIndex& ) ),
              SLOT( slotAddAddressFromRecentBook( const QModelIndex& ) ) );
     connect( m_recentbook,
              SIGNAL( customContextMenuRequested( const QPoint & ) ),
@@ -236,6 +237,31 @@
     QLabel* tolabel = new QLabel( i18n( "Recipient:" )+' ', widg );
     m_edit = new AddressLineEdit( widg );
     tolabel->setBuddy( m_edit );
+
+    QCompleter* completer = new QCompleter( this );
+    completer->setCaseSensitivity( Qt::CaseInsensitive );
+    completer->setCompletionColumn( 0 );
+    completer->setCompletionMode( QCompleter::PopupCompletion );
+    completer->setModel( m_recentModel );
+    completer->setCompletionRole( Qt::DisplayRole );
+
+    // Don't use the setCompleter() function on the lineedit, because
+    // on activation of a completion, it will fill the lineedit with the
+    // value. We don't want that, we just want to add it.
+
+    // Show the completion box under the lineedit, and show it for every change
+    completer->setWidget( m_edit );
+    connect( m_edit, SIGNAL( textChanged( const QString & ) ),
+             completer, SLOT( setCompletionPrefix( const QString & ) ) );
+    connect( m_edit, SIGNAL( textChanged( const QString & ) ),
+             completer, SLOT( complete() ) );
+
+    // On activation of an item, add it as receipient and mark as dirty.
+    connect( completer, SIGNAL( activated( const QString& ) ),
+             SLOT( slotAddAddress( const QString& ) ) );
+    connect( completer, SIGNAL( activated( const QString& ) ),
+             SLOT( slotSetDirty() ) );
+
     connect( m_edit,SIGNAL( shiftAddress() ), SLOT( slotShiftAddress() ) );
     connect( m_edit,SIGNAL( addAddress( const QString& ) ),
              SLOT( slotAddAddress( const QString& ) ) );
@@ -582,7 +608,7 @@
 void Composer::setRcpt( const QString& address, TypeOfAddress addressType )
 {
     m_lastState = addressType;
-    addAddress( address );
+    slotAddAddress( address );
 }
 
 void Composer::setIdentity( KPIMIdentities::Identity identity )
@@ -651,6 +677,7 @@
     if ( m_addressbox->selectedItems().isEmpty() ) {
         m_edit->clear();
         m_edit->setHelp( false );
+        m_edit->blockSignals( false );
         return;
     }
     QTreeWidgetItem* selectedItem = m_addressbox->selectedItems().first();
@@ -679,17 +706,12 @@
     slotUpdateLineEdit();
 }
 
-void Composer::slotAddAddress( const QString& text )
-{
-    addAddress( text );
-}
-
 void Composer::slotAddClicked()
 {
     if ( m_edit->help() )
         slotShiftAddress();
     else
-        addAddress( m_edit->text() );
+        slotAddAddress( m_edit->text() );
 }
 
 void Composer::slotContainsValidAddress( bool active )
@@ -759,7 +781,7 @@
     setDirty( dirty );
 }
 
-void Composer::addAddress( const QString& text )
+void Composer::slotAddAddress( const QString& text )
 {
     if ( text.isEmpty() || text.contains( "@" ) != 1 )
         return;
@@ -767,8 +789,9 @@
     // Check for dups.
     for ( int i = 0; i < m_addressbox->topLevelItemCount() ; ++i ) {
         QTreeWidgetItem* item = m_addressbox->topLevelItem( i );
-        if ( item->text( 0 ).indexOf( text ) != -1 )
+        if ( item->text( 0 ).indexOf( text ) != -1 ) {
             return;
+        }
     }
 
     m_lastInserted = new QTreeWidgetItem( m_addressbox );
@@ -842,14 +865,9 @@
 
 void Composer::slotAddAddressFromAddressBook( const QModelIndex& index )
 {
-    addAddress( index.data( ContactTreeModel::AddressRole ).toString() );
+    slotAddAddress( index.data( ContactTreeModel::AddressRole ).toString() );
 }
 
-void Composer::slotAddAddressFromAddressBook( const QString& address )
-{
-    addAddress( address );
-}
-
 void Composer::slotContextMenuAddressBook()
 {
     /*
@@ -916,8 +934,8 @@
 
     QModelIndex email = m_recentbook->model()->index( lvi.row(), 0, lvi.parent() );
     QModelIndex name = m_recentbook->model()->index( lvi.row(), 1, lvi.parent() );
-    addAddress( m_recentbook->model()->data( name ).toString() + " <" +
-                m_recentbook->model()->data( email ).toString() + '>' );
+    slotAddAddress( m_recentbook->model()->data( name ).toString() + " <" +
+                    m_recentbook->model()->data( email ).toString() + '>' );
 }
 
 void Composer::slotContextMenuRecentBook()
--- trunk/extragear/pim/mailody/src/composer.h #1028296:1028297
@@ -213,7 +213,6 @@
     Akonadi::ItemModel*             m_recentModel;
 
     const char * encoding( const QString& data );
-    void addAddress( const QString& text );
     void showError( const QString& error );
     void addRecipients( SendMessage* msg );
     void placeSignature( QString& );
@@ -237,7 +236,6 @@
     void slotExpandSignature();
 
     void slotAddAddressFromAddressBook( const QModelIndex& );
-    void slotAddAddressFromAddressBook( const QString& );
     void slotContextMenuAddressBook();
     void slotContextMenuAddressList();
 


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic