kaddressbook

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