kaddressbook Library API Documentation

filtereditdialog.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 <qbuttongroup.h>
00025 #include <qhbox.h>
00026 #include <qlabel.h>
00027 #include <qlayout.h>
00028 #include <qpushbutton.h>
00029 #include <qradiobutton.h>
00030 #include <qregexp.h>
00031 #include <qstring.h>
00032 #include <qtoolbutton.h>
00033 #include <qtooltip.h>
00034 #include <qwidget.h>
00035 
00036 #include <kapplication.h>
00037 #include <kbuttonbox.h>
00038 #include <kdebug.h>
00039 #include <kiconloader.h>
00040 #include <klineedit.h>
00041 #include <klistbox.h>
00042 #include <klistview.h>
00043 #include <klocale.h>
00044 
00045 #include "kabprefs.h"
00046 #include "filtereditdialog.h"
00047 
00048 FilterEditDialog::FilterEditDialog( QWidget *parent, const char *name )
00049   : KDialogBase( Plain, i18n( "Edit Address Book Filter" ),
00050                  Help | Ok | Cancel, Ok, parent, name, false, true )
00051 {
00052   initGUI();
00053 
00054   QStringList cats = KABPrefs::instance()->mCustomCategories;
00055 
00056   QStringList::Iterator iter;
00057   for ( iter = cats.begin(); iter != cats.end(); ++iter )
00058     mCategoriesView->insertItem( new QCheckListItem( mCategoriesView, (*iter), QCheckListItem::CheckBox ) );
00059   filterNameTextChanged( mNameEdit->text() );
00060 }
00061 
00062 FilterEditDialog::~FilterEditDialog()
00063 {
00064 }
00065 
00066 void FilterEditDialog::setFilter( const Filter &filter )
00067 {
00068   mNameEdit->setText( filter.name() );
00069 
00070   QStringList categories = filter.categories();
00071   QListViewItem *item = mCategoriesView->firstChild();
00072   while ( item != 0 ) {
00073     if ( categories.contains( item->text( 0 ) ) ) {
00074       QCheckListItem *checkItem = static_cast<QCheckListItem*>( item );
00075       checkItem->setOn( true );
00076     }
00077 
00078     item = item->nextSibling();
00079   }
00080 
00081   if ( filter.matchRule() == Filter::Matching )
00082     mMatchRuleGroup->setButton( 0 );
00083   else
00084     mMatchRuleGroup->setButton( 1 );
00085 }
00086 
00087 Filter FilterEditDialog::filter()
00088 {
00089   Filter filter;
00090 
00091   filter.setName( mNameEdit->text() );
00092 
00093   QStringList categories;
00094   QListViewItem *item = mCategoriesView->firstChild();
00095   while ( item != 0 ) {
00096     QCheckListItem *checkItem = static_cast<QCheckListItem*>( item );
00097     if ( checkItem->isOn() )
00098       categories.append( item->text( 0 ) );
00099 
00100     item = item->nextSibling();
00101   }
00102   filter.setCategories( categories );
00103 
00104   if ( mMatchRuleGroup->find( 0 )->isOn() )
00105     filter.setMatchRule( Filter::Matching );
00106   else
00107     filter.setMatchRule( Filter::NotMatching );
00108 
00109   return filter;
00110 }
00111 
00112 void FilterEditDialog::initGUI()
00113 {
00114   resize( 490, 300 );
00115 
00116   QWidget *page = plainPage();
00117   QLabel *label;
00118 
00119   QGridLayout *topLayout = new QGridLayout( page, 3, 2, 0, spacingHint() );
00120 
00121   label = new QLabel( i18n( "Name:" ), page );
00122   mNameEdit = new KLineEdit( page );
00123   mNameEdit->setFocus();
00124   topLayout->addWidget( label, 0, 0 );
00125   topLayout->addWidget( mNameEdit, 0, 1 );
00126   connect( mNameEdit, SIGNAL( textChanged( const QString& ) ),
00127            SLOT( filterNameTextChanged( const QString&) ) );
00128 
00129   mCategoriesView = new KListView( page );
00130   mCategoriesView->addColumn( i18n( "Categories" ) );
00131   topLayout->addMultiCellWidget( mCategoriesView, 1, 1, 0, 1 );
00132 
00133   mMatchRuleGroup = new QButtonGroup( page );
00134   mMatchRuleGroup->setExclusive( true );
00135 
00136   QBoxLayout *gbLayout = new QVBoxLayout( mMatchRuleGroup );
00137   gbLayout->setSpacing( KDialog::spacingHint() );
00138   gbLayout->setMargin( KDialog::marginHint() );
00139 
00140   QRadioButton *radio = new QRadioButton( i18n( "Show only contacts matching the selected categories" ), mMatchRuleGroup );
00141   radio->setChecked( true );
00142   mMatchRuleGroup->insert( radio );
00143   gbLayout->addWidget( radio );
00144 
00145   radio = new QRadioButton( i18n( "Show all contacts except those matching the selected categories" ), mMatchRuleGroup );
00146   mMatchRuleGroup->insert( radio );
00147   gbLayout->addWidget( radio );
00148 
00149   topLayout->addMultiCellWidget( mMatchRuleGroup, 2, 2, 0, 1 );
00150 }
00151 
00152 void FilterEditDialog::filterNameTextChanged( const QString &text )
00153 {
00154   enableButtonOK( !text.isEmpty() );
00155 }
00156 
00157 void FilterEditDialog::slotHelp()
00158 {
00159   kapp->invokeHelp( "using-filters" );
00160 }
00161 
00162 FilterDialog::FilterDialog( QWidget *parent, const char *name )
00163   : KDialogBase( Plain, i18n( "Edit Address Book Filters" ),
00164                  Ok | Cancel, Ok, parent, name, false, true )
00165 {
00166   initGUI();
00167 }
00168 
00169 FilterDialog::~FilterDialog()
00170 {
00171 }
00172 
00173 void FilterDialog::setFilters( const Filter::List &list )
00174 {
00175   mFilterList.clear();
00176   mInternalFilterList.clear();
00177 
00178   Filter::List::ConstIterator it;
00179   for ( it = list.begin(); it != list.end(); ++it ) {
00180     if ( (*it).isInternal() )
00181       mInternalFilterList.append( *it );
00182     else
00183       mFilterList.append( *it );
00184   }
00185 
00186   refresh();
00187 }
00188 
00189 Filter::List FilterDialog::filters() const
00190 {
00191   Filter::List list = mFilterList + mInternalFilterList;
00192   return list;
00193 }
00194 
00195 void FilterDialog::add()
00196 {
00197   FilterEditDialog dlg( this );
00198 
00199   if ( dlg.exec() )
00200     mFilterList.append( dlg.filter() );
00201 
00202   refresh();
00203 
00204   mFilterListBox->setCurrentItem( mFilterListBox->count() - 1 );
00205 }
00206 
00207 void FilterDialog::edit()
00208 {
00209   FilterEditDialog dlg( this );
00210 
00211   uint pos = mFilterListBox->currentItem();
00212 
00213   dlg.setFilter( mFilterList[ pos ] );
00214 
00215   if ( dlg.exec() ) {
00216     mFilterList.remove( mFilterList.at( pos ) );
00217     mFilterList.insert( mFilterList.at( pos ), dlg.filter() );
00218   }
00219 
00220   refresh();
00221 
00222   mFilterListBox->setCurrentItem( pos );
00223 }
00224 
00225 void FilterDialog::remove()
00226 {
00227   mFilterList.remove( mFilterList.at( mFilterListBox->currentItem() ) );
00228 
00229   selectionChanged( 0 );
00230 
00231   refresh();
00232 }
00233 
00234 void FilterDialog::refresh()
00235 {
00236   mFilterListBox->clear();
00237 
00238   Filter::List::Iterator iter;
00239   for ( iter = mFilterList.begin(); iter != mFilterList.end(); ++iter )
00240     mFilterListBox->insertItem( (*iter).name() );
00241 }
00242 
00243 void FilterDialog::selectionChanged( QListBoxItem *item )
00244 {
00245   bool state = ( item != 0 );
00246 
00247   mEditButton->setEnabled( state );
00248   mRemoveButton->setEnabled( state );
00249 }
00250 
00251 void FilterDialog::initGUI()
00252 {
00253   resize( 330, 200 );
00254 
00255   QWidget *page = plainPage();
00256 
00257   QGridLayout *topLayout = new QGridLayout( page, 1, 2, 0, spacingHint() );
00258 
00259   mFilterListBox = new KListBox( page );
00260   topLayout->addWidget( mFilterListBox, 0, 0 );
00261   connect( mFilterListBox, SIGNAL( selectionChanged( QListBoxItem * ) ),
00262            SLOT( selectionChanged( QListBoxItem * ) ) );
00263   connect( mFilterListBox, SIGNAL( doubleClicked ( QListBoxItem * ) ),
00264            SLOT( edit() ) );
00265 
00266   KButtonBox *buttonBox = new KButtonBox( page, Vertical );
00267   buttonBox->addButton( i18n( "&Add..." ), this, SLOT( add() ) );
00268   mEditButton = buttonBox->addButton( i18n( "&Edit..." ), this, SLOT( edit() ) );
00269   mEditButton->setEnabled( false );
00270   mRemoveButton = buttonBox->addButton( i18n( "&Remove" ), this, SLOT( remove() ) );
00271   mRemoveButton->setEnabled( false );
00272 
00273   buttonBox->layout();
00274   topLayout->addWidget( buttonBox, 0, 1 );
00275 }
00276 
00277 #include "filtereditdialog.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