kaddressbook

filtereditdialog.cpp
1/*
2 This file is part of KAddressBook.
3 Copyright (c) 2002 Mike Pilone <mpilone@slac.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18
19 As a special exception, permission is given to link this program
20 with any edition of TQt, and distribute the resulting executable,
21 without including the source code for TQt in the source distribution.
22*/
23
24#include <tqbuttongroup.h>
25#include <tqhbox.h>
26#include <tqlabel.h>
27#include <tqlayout.h>
28#include <tqpushbutton.h>
29#include <tqradiobutton.h>
30#include <tqregexp.h>
31#include <tqstring.h>
32#include <tqtoolbutton.h>
33#include <tqtooltip.h>
34#include <tqwidget.h>
35
36#include <tdeapplication.h>
37#include <kbuttonbox.h>
38#include <kdebug.h>
39#include <kiconloader.h>
40#include <klineedit.h>
41#include <tdelistbox.h>
42#include <tdelistview.h>
43#include <tdelocale.h>
44
45#include "kabprefs.h"
46#include "filtereditdialog.h"
47
48FilterEditDialog::FilterEditDialog( TQWidget *parent, const char *name )
49 : KDialogBase( Plain, i18n( "Edit Address Book Filter" ),
50 Help | Ok | Cancel, Ok, parent, name, false, true )
51{
52 initGUI();
53
54 const TQStringList cats = KABPrefs::instance()->customCategories();
55
56 TQStringList::ConstIterator it;
57 for ( it = cats.begin(); it != cats.end(); ++it )
58 mCategoriesView->insertItem( new TQCheckListItem( mCategoriesView, *it, TQCheckListItem::CheckBox ) );
59
60 filterNameTextChanged( mNameEdit->text() );
61}
62
63FilterEditDialog::~FilterEditDialog()
64{
65}
66
67void FilterEditDialog::setFilter( const Filter &filter )
68{
69 mNameEdit->setText( filter.name() );
70
71 TQStringList categories = filter.categories();
72 TQListViewItem *item = mCategoriesView->firstChild();
73 while ( item != 0 ) {
74 if ( categories.contains( item->text( 0 ) ) ) {
75 TQCheckListItem *checkItem = static_cast<TQCheckListItem*>( item );
76 checkItem->setOn( true );
77 }
78
79 item = item->nextSibling();
80 }
81
82 if ( filter.matchRule() == Filter::Matching )
83 mMatchRuleGroup->setButton( 0 );
84 else
85 mMatchRuleGroup->setButton( 1 );
86}
87
88Filter FilterEditDialog::filter()
89{
90 Filter filter;
91
92 filter.setName( mNameEdit->text() );
93
94 TQStringList categories;
95 TQListViewItem *item = mCategoriesView->firstChild();
96 while ( item != 0 ) {
97 TQCheckListItem *checkItem = static_cast<TQCheckListItem*>( item );
98 if ( checkItem->isOn() )
99 categories.append( item->text( 0 ) );
100
101 item = item->nextSibling();
102 }
103 filter.setCategories( categories );
104
105 if ( mMatchRuleGroup->find( 0 )->isOn() )
106 filter.setMatchRule( Filter::Matching );
107 else
108 filter.setMatchRule( Filter::NotMatching );
109
110 return filter;
111}
112
113void FilterEditDialog::initGUI()
114{
115 resize( 490, 300 );
116
117 TQWidget *page = plainPage();
118 TQLabel *label;
119
120 TQGridLayout *topLayout = new TQGridLayout( page, 3, 2, 0, spacingHint() );
121
122 label = new TQLabel( i18n( "Name:" ), page );
123 mNameEdit = new KLineEdit( page );
124 mNameEdit->setFocus();
125 topLayout->addWidget( label, 0, 0 );
126 topLayout->addWidget( mNameEdit, 0, 1 );
127 connect( mNameEdit, TQ_SIGNAL( textChanged( const TQString& ) ),
128 TQ_SLOT( filterNameTextChanged( const TQString&) ) );
129
130 mCategoriesView = new TDEListView( page );
131 mCategoriesView->addColumn( i18n( "Category" ) );
132 mCategoriesView->setFullWidth( true );
133 topLayout->addMultiCellWidget( mCategoriesView, 1, 1, 0, 1 );
134
135 mMatchRuleGroup = new TQButtonGroup( page );
136 mMatchRuleGroup->setExclusive( true );
137
138 TQBoxLayout *gbLayout = new TQVBoxLayout( mMatchRuleGroup );
139 gbLayout->setSpacing( KDialog::spacingHint() );
140 gbLayout->setMargin( KDialog::marginHint() );
141
142 TQRadioButton *radio = new TQRadioButton( i18n( "Show only contacts matching the selected categories" ), mMatchRuleGroup );
143 radio->setChecked( true );
144 mMatchRuleGroup->insert( radio );
145 gbLayout->addWidget( radio );
146
147 radio = new TQRadioButton( i18n( "Show all contacts except those matching the selected categories" ), mMatchRuleGroup );
148 mMatchRuleGroup->insert( radio );
149 gbLayout->addWidget( radio );
150
151 topLayout->addMultiCellWidget( mMatchRuleGroup, 2, 2, 0, 1 );
152}
153
154void FilterEditDialog::filterNameTextChanged( const TQString &text )
155{
156 enableButtonOK( !text.isEmpty() );
157}
158
159void FilterEditDialog::slotHelp()
160{
161 tdeApp->invokeHelp( "using-filters" );
162}
163
164FilterDialog::FilterDialog( TQWidget *parent, const char *name )
165 : KDialogBase( Plain, i18n( "Edit Address Book Filters" ),
166 Ok | Cancel, Ok, parent, name, false, true )
167{
168 initGUI();
169}
170
171FilterDialog::~FilterDialog()
172{
173}
174
175void FilterDialog::setFilters( const Filter::List &list )
176{
177 mFilterList.clear();
178 mInternalFilterList.clear();
179
180 Filter::List::ConstIterator it;
181 for ( it = list.begin(); it != list.end(); ++it ) {
182 if ( (*it).isInternal() )
183 mInternalFilterList.append( *it );
184 else
185 mFilterList.append( *it );
186 }
187
188 refresh();
189}
190
191Filter::List FilterDialog::filters() const
192{
193 Filter::List list = mFilterList + mInternalFilterList;
194 return list;
195}
196
197void FilterDialog::add()
198{
199 FilterEditDialog dlg( this );
200
201 if ( dlg.exec() )
202 mFilterList.append( dlg.filter() );
203
204 refresh();
205
206 mFilterListBox->setCurrentItem( mFilterListBox->count() - 1 );
207}
208
209void FilterDialog::edit()
210{
211 FilterEditDialog dlg( this );
212
213 uint pos = mFilterListBox->currentItem();
214
215 dlg.setFilter( mFilterList[ pos ] );
216
217 if ( dlg.exec() ) {
218 mFilterList.remove( mFilterList.at( pos ) );
219 mFilterList.insert( mFilterList.at( pos ), dlg.filter() );
220 }
221
222 refresh();
223
224 mFilterListBox->setCurrentItem( pos );
225}
226
227void FilterDialog::remove()
228{
229 mFilterList.remove( mFilterList.at( mFilterListBox->currentItem() ) );
230
231 selectionChanged( 0 );
232
233 refresh();
234}
235
236void FilterDialog::refresh()
237{
238 mFilterListBox->clear();
239
240 Filter::List::ConstIterator it;
241 for ( it = mFilterList.begin(); it != mFilterList.end(); ++it )
242 mFilterListBox->insertItem( (*it).name() );
243}
244
245void FilterDialog::selectionChanged( TQListBoxItem *item )
246{
247 bool state = ( item != 0 );
248
249 mEditButton->setEnabled( state );
250 mRemoveButton->setEnabled( state );
251}
252
253void FilterDialog::initGUI()
254{
255 resize( 330, 200 );
256
257 TQWidget *page = plainPage();
258
259 TQGridLayout *topLayout = new TQGridLayout( page, 1, 2, 0, spacingHint() );
260
261 mFilterListBox = new TDEListBox( page );
262 topLayout->addWidget( mFilterListBox, 0, 0 );
263 connect( mFilterListBox, TQ_SIGNAL( selectionChanged( TQListBoxItem * ) ),
264 TQ_SLOT( selectionChanged( TQListBoxItem * ) ) );
265 connect( mFilterListBox, TQ_SIGNAL( doubleClicked ( TQListBoxItem * ) ),
266 TQ_SLOT( edit() ) );
267
268 KButtonBox *buttonBox = new KButtonBox( page, TQt::Vertical );
269 buttonBox->addButton( i18n( "&Add..." ), this, TQ_SLOT( add() ) );
270 mEditButton = buttonBox->addButton( i18n( "&Edit..." ), this, TQ_SLOT( edit() ) );
271 mEditButton->setEnabled( false );
272 mRemoveButton = buttonBox->addButton( i18n( "&Remove" ), this, TQ_SLOT( remove() ) );
273 mRemoveButton->setEnabled( false );
274
275 buttonBox->layout();
276 topLayout->addWidget( buttonBox, 0, 1 );
277}
278
279#include "filtereditdialog.moc"
Filter for AddressBook related objects (Addressees)
Definition: filter.h:40
MatchRule matchRule() const
Definition: filter.cpp:211
void setName(const TQString &name)
Set the name of the filter.
Definition: filter.cpp:47
const TQStringList & categories() const
Definition: filter.cpp:119
void setCategories(const TQStringList &list)
Set the list of categories.
Definition: filter.cpp:112
const TQString & name() const
Definition: filter.cpp:54
void setMatchRule(MatchRule rule)
Sets the filter rule.
Definition: filter.cpp:204