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., 675 Mass Ave, Cambridge, MA 02139, 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 <qregexp.h>
00030 #include <qradiobutton.h>
00031 #include <qstring.h>
00032 #include <qtoolbutton.h>
00033 #include <qtooltip.h>
00034 #include <qwidget.h>
00035 
00036 #include <kbuttonbox.h>
00037 #include <kdebug.h>
00038 #include <kiconloader.h>
00039 #include <klineedit.h>
00040 #include <klistbox.h>
00041 #include <klistview.h>
00042 #include <klocale.h>
00043 
00044 #include "filtereditdialog.h"
00045 #include "kabprefs.h"
00046 
00047 FilterEditDialog::FilterEditDialog( QWidget *parent, const char *name )
00048   : KDialogBase( Plain, i18n("Edit Address Book Filter"),
00049                 Ok | Cancel, Ok, parent, name, false )
00050 {
00051   initGUI();
00052 
00053   QStringList cats = KABPrefs::instance()->mCustomCategories;
00054 
00055   QStringList::Iterator iter;
00056   for ( iter = cats.begin(); iter != cats.end(); ++iter )
00057     mCategoriesView->insertItem( new QCheckListItem( mCategoriesView, (*iter), QCheckListItem::CheckBox ) );
00058   filterNameTextChanged( mNameEdit->text());
00059 }
00060 
00061 FilterEditDialog::~FilterEditDialog()
00062 {
00063 }
00064 
00065 void FilterEditDialog::setFilter( const Filter &filter )
00066 {
00067   mNameEdit->setText( filter.name() );
00068 
00069   QStringList categories = filter.categories();
00070   QListViewItem *item = mCategoriesView->firstChild();
00071   while ( item != 0 ) {
00072     if ( categories.contains( item->text( 0 ) ) ) {
00073       QCheckListItem *checkItem = dynamic_cast<QCheckListItem*>( item );
00074       if ( checkItem )
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 = dynamic_cast<QCheckListItem*>( item );
00097     if ( checkItem && 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 );
00120   topLayout->setSpacing( spacingHint() );
00121   topLayout->setMargin( marginHint() );
00122 
00123   label = new QLabel( i18n( "Name" ), page );
00124   mNameEdit = new KLineEdit( page );
00125   topLayout->addWidget( label, 0, 0 );
00126   topLayout->addWidget( mNameEdit, 0, 1 );
00127   connect (mNameEdit, SIGNAL(textChanged ( const QString & )), this, 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( 20 );
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 FilterDialog::FilterDialog(QWidget *parent, const char *name)
00158   : KDialogBase(Plain, i18n("Edit Address Book Filters"),
00159                 Ok | Cancel, Ok, parent, name, false)
00160 {
00161   initGUI();
00162 }
00163 
00164 FilterDialog::~FilterDialog()
00165 {
00166 }
00167 
00168 void FilterDialog::setFilters(const Filter::List &list)
00169 {
00170   mFilterList.clear();
00171   mFilterList = list;
00172 
00173   refresh();
00174 }
00175 
00176 Filter::List &FilterDialog::filters()
00177 {
00178   return mFilterList;
00179 }
00180 
00181 void FilterDialog::add()
00182 {
00183   FilterEditDialog dlg( this );
00184 
00185   if ( dlg.exec() )
00186     mFilterList.append( dlg.filter() );
00187 
00188   refresh();
00189 
00190   mFilterListBox->setCurrentItem( mFilterListBox->count() - 1 );
00191 }
00192 
00193 void FilterDialog::edit()
00194 {
00195   FilterEditDialog dlg( this );
00196 
00197   uint pos = mFilterListBox->currentItem();
00198 
00199   dlg.setFilter( mFilterList[ pos ] );
00200 
00201   if ( dlg.exec() ) {
00202     mFilterList.remove( mFilterList.at( pos ) );
00203     mFilterList.insert( mFilterList.at( pos ), dlg.filter() );
00204   }
00205 
00206   refresh();
00207 
00208   mFilterListBox->setCurrentItem( pos );
00209 }
00210 
00211 void FilterDialog::remove()
00212 {
00213   mFilterList.remove( mFilterList.at( mFilterListBox->currentItem() ) );
00214 
00215   selectionChanged( 0 );
00216 
00217   refresh();
00218 }
00219 
00220 void FilterDialog::refresh()
00221 {
00222   mFilterListBox->clear();
00223 
00224   Filter::List::Iterator iter;
00225   for ( iter = mFilterList.begin(); iter != mFilterList.end(); ++iter )
00226     mFilterListBox->insertItem( (*iter).name() );
00227 }
00228 
00229 void FilterDialog::selectionChanged( QListBoxItem *item )
00230 {
00231   bool state = ( item != 0 );
00232 
00233   mEditButton->setEnabled( state );
00234   mRemoveButton->setEnabled( state );
00235 }
00236 
00237 void FilterDialog::initGUI()
00238 {
00239   resize( 330, 200 );
00240 
00241   QWidget *page = plainPage();
00242 
00243   QGridLayout *topLayout = new QGridLayout( page, 1, 2 );
00244   topLayout->setSpacing( spacingHint() );
00245   topLayout->setMargin( marginHint() );
00246 
00247   mFilterListBox = new KListBox( page, "mFilterListBox" );
00248   topLayout->addWidget( mFilterListBox, 0, 0 );
00249   connect( mFilterListBox, SIGNAL( selectionChanged( QListBoxItem * ) ),
00250            SLOT( selectionChanged( QListBoxItem * ) ) );
00251   connect( mFilterListBox, SIGNAL( doubleClicked ( QListBoxItem * ) ),
00252            SLOT( edit() ) );
00253 
00254   KButtonBox *buttonBox = new KButtonBox( page, Vertical );
00255   buttonBox->addButton( i18n( "&Add..." ), this, SLOT( add() ) );
00256   mEditButton = buttonBox->addButton( i18n( "&Edit..." ), this, SLOT( edit() ) );
00257   mEditButton->setEnabled( false );
00258   mRemoveButton = buttonBox->addButton( i18n( "&Remove" ), this, SLOT( remove() ) );
00259   mRemoveButton->setEnabled( false );
00260 
00261   buttonBox->layout();
00262   topLayout->addWidget( buttonBox, 0, 1 );
00263 }
00264 
00265 #include "filtereditdialog.moc"
KDE Logo
This file is part of the documentation for kdelibs Version 3.1.5.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Sun Feb 15 11:40:37 2004 by doxygen 1.3.5 written by Dimitri van Heesch, © 1997-2001