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