kaddressbook Library API Documentation

emaileditwidget.cpp

00001 /*
00002     This file is part of KAddressBook.
00003     Copyright (c) 2002 Mike Pilone <mpilone@slac.com>
00004 
00005     This program is free software; you can redistribute it and/or modify
00006     it under the terms of the GNU General Public License as published by
00007     the Free Software Foundation; either version 2 of the License, or
00008     (at your option) any later version.
00009 
00010     This program is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00013     GNU General Public License for more details.
00014 
00015     You should have received a copy of the GNU General Public License
00016     along with this program; if not, write to the Free Software
00017     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00018 
00019     As a special exception, permission is given to link this program
00020     with any edition of Qt, and distribute the resulting executable,
00021     without including the source code for Qt in the source distribution.
00022 */
00023 
00024 #include <qcheckbox.h>
00025 #include <qlabel.h>
00026 #include <qlayout.h>
00027 #include <qpainter.h>
00028 #include <qpushbutton.h>
00029 #include <qstring.h>
00030 #include <qtoolbutton.h>
00031 #include <qtooltip.h>
00032 
00033 #include <kaccelmanager.h>
00034 #include <kconfig.h>
00035 #include <kcombobox.h>
00036 #include <kdebug.h>
00037 #include <kdialog.h>
00038 #include <kiconloader.h>
00039 #include <klineedit.h>
00040 #include <klocale.h>
00041 #include <kmessagebox.h>
00042 
00043 #include "emaileditwidget.h"
00044 
00045 class EmailItem : public QListBoxText
00046 {
00047   public:
00048     EmailItem( QListBox *parent, const QString &text, bool preferred )
00049       : QListBoxText( parent, text ), mPreferred( preferred )
00050     {}
00051 
00052     void setPreferred( bool preferred ) { mPreferred = preferred; }
00053     bool preferred() const { return mPreferred; }
00054 
00055     void setText( const QString &text )
00056     {
00057       QListBoxText::setText( text );
00058     }
00059 
00060   protected:
00061     virtual void paint( QPainter *p )
00062     {
00063       if ( mPreferred ) {
00064         QFont font = p->font();
00065         font.setBold( true );
00066         p->setFont( font );
00067       }
00068 
00069       QListBoxText::paint( p );
00070     }
00071 
00072   private:
00073     bool mPreferred;
00074 };
00075 
00076 EmailEditWidget::EmailEditWidget( QWidget *parent, const char *name )
00077   : QWidget( parent, name )
00078 {
00079   QGridLayout *topLayout = new QGridLayout( this, 2, 2, KDialog::marginHint(),
00080                                             KDialog::spacingHint() );
00081 
00082   QLabel *label = new QLabel( i18n( "Email:" ), this );
00083   topLayout->addWidget( label, 0, 0 );
00084 
00085   mEmailEdit = new KLineEdit( this );
00086   connect( mEmailEdit, SIGNAL( textChanged( const QString& ) ),
00087            SLOT( textChanged( const QString& ) ) );
00088   connect( mEmailEdit, SIGNAL( textChanged( const QString& ) ),
00089            SIGNAL( modified() ) );
00090   label->setBuddy( mEmailEdit );
00091   topLayout->addWidget( mEmailEdit, 0, 1 );
00092 
00093   mEditButton = new QPushButton( i18n( "Edit Email Addresses..." ), this);
00094   connect( mEditButton, SIGNAL( clicked() ), SLOT( edit() ) );
00095   topLayout->addMultiCellWidget( mEditButton, 1, 1, 0, 1 );
00096 
00097   topLayout->activate();
00098 }
00099 
00100 EmailEditWidget::~EmailEditWidget()
00101 {
00102 }
00103 
00104 void EmailEditWidget::setReadOnly( bool readOnly )
00105 {
00106   mEmailEdit->setReadOnly( readOnly );
00107   mEditButton->setEnabled( !readOnly );
00108 }
00109 
00110 void EmailEditWidget::setEmails( const QStringList &list )
00111 {
00112   mEmailList = list;
00113 
00114   bool blocked = mEmailEdit->signalsBlocked();
00115   mEmailEdit->blockSignals( true );
00116   if ( list.count() > 0 )
00117     mEmailEdit->setText( list[ 0 ] );
00118   else
00119     mEmailEdit->setText( "" );
00120   mEmailEdit->blockSignals( blocked );
00121 }
00122 
00123 QStringList EmailEditWidget::emails()
00124 {
00125   if ( mEmailEdit->text().isEmpty() ) {
00126     if ( mEmailList.count() > 0 )
00127       mEmailList.remove( mEmailList.begin() );
00128   } else {
00129     if ( mEmailList.count() > 0 )
00130       mEmailList.remove( mEmailList.begin() );
00131 
00132     mEmailList.prepend( mEmailEdit->text() );
00133   }
00134 
00135   return mEmailList;
00136 }
00137 
00138 void EmailEditWidget::edit()
00139 {
00140   EmailEditDialog dlg( mEmailList, this );
00141 
00142   if ( dlg.exec() ) {
00143     if ( dlg.changed() ) {
00144       mEmailList = dlg.emails();
00145       mEmailEdit->setText( mEmailList[ 0 ] );
00146       emit modified();
00147     }
00148   }
00149 }
00150 
00151 void EmailEditWidget::textChanged( const QString &text )
00152 {
00153   if ( mEmailList.count() > 0 )
00154     mEmailList.remove( mEmailList.begin() );
00155 
00156   mEmailList.prepend( text );
00157 }
00158 
00159 
00160 EmailEditDialog::EmailEditDialog( const QStringList &list, QWidget *parent,
00161                                   const char *name )
00162   : KDialogBase( KDialogBase::Plain, i18n( "Edit Email Addresses" ),
00163                  KDialogBase::Ok | KDialogBase::Cancel, KDialogBase::Help,
00164                  parent, name, true ), mIsEdit( false )
00165 {
00166   QWidget *page = plainPage();
00167 
00168   QGridLayout *topLayout = new QGridLayout( page, 4, 3, marginHint(),
00169                                             spacingHint() );
00170 
00171   QLabel *label = new QLabel( i18n( "Email address:" ), page );
00172   topLayout->addWidget( label, 0, 0 );
00173 
00174   mEmailEdit = new KLineEdit( page );
00175   label->setBuddy( mEmailEdit );
00176   topLayout->addWidget( mEmailEdit, 0, 1 );
00177   connect( mEmailEdit, SIGNAL( textChanged( const QString& ) ),
00178            SLOT( emailChanged() ) );
00179 
00180   mAddButton = new QPushButton( i18n( "Add" ), page );
00181   mAddButton->setEnabled( false );
00182   connect( mAddButton, SIGNAL( clicked() ), SLOT( add() ) );
00183   topLayout->addWidget( mAddButton, 0, 2 );
00184 
00185   mEmailListBox = new QListBox( page );
00186 
00187   // Make sure there is room for the scrollbar
00188   mEmailListBox->setMinimumHeight( mEmailListBox->sizeHint().height() + 30 );
00189   connect( mEmailListBox, SIGNAL( highlighted( int ) ),
00190            SLOT( selectionChanged( int ) ) );
00191   connect( mEmailListBox, SIGNAL( selected( int ) ),
00192            SLOT( edit() ) );
00193   topLayout->addMultiCellWidget( mEmailListBox, 1, 3, 0, 1 );
00194 
00195   mEditButton = new QPushButton( i18n( "Change" ), page );
00196   connect( mEditButton, SIGNAL( clicked() ), SLOT( edit() ) );
00197   topLayout->addWidget( mEditButton, 1, 2 );
00198 
00199   mRemoveButton = new QPushButton( i18n( "Remove" ), page );
00200   connect( mRemoveButton, SIGNAL( clicked() ), SLOT( remove() ) );
00201   topLayout->addWidget( mRemoveButton, 2, 2 );
00202 
00203   mStandardButton = new QPushButton( i18n( "Set Standard" ), page );
00204   connect( mStandardButton, SIGNAL( clicked() ), SLOT( standard() ) );
00205   topLayout->addWidget( mStandardButton, 3, 2 );
00206 
00207   topLayout->activate();
00208 
00209   QStringList items = list;
00210   if ( items.remove( "" ) > 0 )
00211     mChanged = true;
00212   else
00213     mChanged = false;
00214 
00215   QStringList::Iterator it;
00216   bool preferred = true;
00217   for ( it = items.begin(); it != items.end(); ++it ) {
00218     new EmailItem( mEmailListBox, *it, preferred );
00219     preferred = false;
00220   }
00221 
00222   // set default state
00223   selectionChanged( -1 );
00224   mEmailEdit->setFocus();
00225   KAcceleratorManager::manage( this );
00226 
00227   actionButton( Ok )->setDefault( true );
00228 }
00229 
00230 EmailEditDialog::~EmailEditDialog()
00231 {
00232 }
00233 
00234 QStringList EmailEditDialog::emails() const
00235 {
00236   QStringList emails;
00237 
00238   for ( uint i = 0; i < mEmailListBox->count(); ++i ) {
00239     EmailItem *item = static_cast<EmailItem*>( mEmailListBox->item( i ) );
00240     if ( (int)i == mEmailListBox->currentItem() )
00241       emails.prepend( item->text() );
00242     else
00243       emails.append( item->text() );
00244   }
00245 
00246   return emails;
00247 }
00248 
00249 void EmailEditDialog::add()
00250 {
00251   // check if item already available, ignore if so...
00252   for ( uint i = 0; i < mEmailListBox->count(); ++i ) {
00253     if ( mEmailListBox->text( i ) == mEmailEdit->text() ) {
00254       mEmailEdit->clear();
00255       mEmailEdit->setFocus();
00256       return;
00257     }
00258   }
00259 
00260   if ( !mIsEdit ) {
00261     new EmailItem( mEmailListBox, mEmailEdit->text(),
00262                    (mEmailListBox->count() == 0) );
00263   } else {
00264     EmailItem *item = static_cast<EmailItem*>( mEmailListBox->item( mEditPos ) );
00265     item->setText( mEmailEdit->text() );
00266     mIsEdit = false;
00267     mEmailListBox->triggerUpdate( true );
00268   }
00269 
00270   mEmailEdit->clear();
00271   mEmailEdit->setFocus();
00272 
00273   mChanged = true;
00274 }
00275 
00276 void EmailEditDialog::edit()
00277 {
00278   mIsEdit = true;
00279   mEditPos = mEmailListBox->currentItem();
00280   mEmailEdit->setText( mEmailListBox->currentText() );
00281   mEmailEdit->setFocus();
00282 }
00283 
00284 void EmailEditDialog::remove()
00285 {
00286   QString address = mEmailListBox->currentText();
00287 
00288   QString text = i18n( "<qt>Are you sure that you want to remove the email address <b>%1</b>?</qt>" ).arg( address );
00289   QString caption = i18n( "Confirm Remove" );
00290 
00291   if ( KMessageBox::questionYesNo( this, text, caption ) == KMessageBox::Yes ) {
00292     EmailItem *item = static_cast<EmailItem*>( mEmailListBox->item( mEmailListBox->currentItem() ) );
00293 
00294     bool preferred = item->preferred();
00295     mEmailListBox->removeItem( mEmailListBox->currentItem() );
00296     mEmailEdit->clear();
00297     if ( preferred ) {
00298       item = dynamic_cast<EmailItem*>( mEmailListBox->item( 0 ) );
00299       if ( item )
00300         item->setPreferred( true );
00301     }
00302     mChanged = true;
00303   }
00304 }
00305 
00306 bool EmailEditDialog::changed() const
00307 {
00308   return mChanged;
00309 }
00310 
00311 void EmailEditDialog::standard()
00312 {
00313   for ( uint i = 0; i < mEmailListBox->count(); ++i ) {
00314     EmailItem *item = static_cast<EmailItem*>( mEmailListBox->item( i ) );
00315     if ( (int)i == mEmailListBox->currentItem() )
00316       item->setPreferred( true );
00317     else
00318       item->setPreferred( false );
00319   }
00320 
00321   mEmailListBox->triggerUpdate( true );
00322 
00323   mChanged = true;
00324 }
00325 
00326 void EmailEditDialog::selectionChanged( int index )
00327 {
00328   bool value = ( index >= 0 ); // An item is selected
00329 
00330   mRemoveButton->setEnabled( value );
00331   mEditButton->setEnabled( value );
00332   mStandardButton->setEnabled( value );
00333 }
00334 
00335 void EmailEditDialog::emailChanged()
00336 {
00337   bool state = mEmailEdit->text().contains( '@' );
00338 
00339   mAddButton->setEnabled( state );
00340 
00341   if ( state ) {
00342     actionButton( Ok )->setDefault( false );
00343   } else {
00344     actionButton( Ok )->setDefault( true );
00345   }
00346 
00347   mAddButton->setDefault( state );
00348 }
00349 
00350 #include "emaileditwidget.moc"
KDE Logo
This file is part of the documentation for kaddressbook Library Version 3.2.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sat May 1 11:38:51 2004 by doxygen 1.2.15 written by Dimitri van Heesch, © 1997-2003