summaryrefslogtreecommitdiffabout
path: root/kaddressbook/filtereditdialog.cpp
Unidiff
Diffstat (limited to 'kaddressbook/filtereditdialog.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--kaddressbook/filtereditdialog.cpp302
1 files changed, 302 insertions, 0 deletions
diff --git a/kaddressbook/filtereditdialog.cpp b/kaddressbook/filtereditdialog.cpp
new file mode 100644
index 0000000..063585a
--- a/dev/null
+++ b/kaddressbook/filtereditdialog.cpp
@@ -0,0 +1,302 @@
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18
19 As a special exception, permission is given to link this program
20 with any edition of Qt, and distribute the resulting executable,
21 without including the source code for Qt in the source distribution.
22*/
23
24/*
25Enhanced Version of the file for platform independent KDE tools.
26Copyright (c) 2004 Ulf Schenk
27
28$Id$
29*/
30
31#include <qbuttongroup.h>
32#include <qhbox.h>
33#include <qlabel.h>
34#include <qlayout.h>
35#include <qpushbutton.h>
36#include <qradiobutton.h>
37#include <qregexp.h>
38#include <qstring.h>
39#include <qtoolbutton.h>
40#include <qtooltip.h>
41#include <qwidget.h>
42
43#include <kapplication.h>
44#include <kbuttonbox.h>
45#include <kdebug.h>
46#include <kiconloader.h>
47#include <klineedit.h>
48#include <klistbox.h>
49#include <klistview.h>
50#include <klocale.h>
51#include <kglobal.h>
52
53#include "kabprefs.h"
54#include "filtereditdialog.h"
55
56FilterEditDialog::FilterEditDialog( QWidget *parent, const char *name )
57 : KDialogBase( Plain, i18n( "Edit Address Book Filter" ),
58 Help | Ok | Cancel, Ok, parent, name, /*US false*/ true, true )
59{
60 initGUI();
61
62 QStringList cats = KABPrefs::instance()->mCustomCategories;
63
64 QStringList::Iterator iter;
65 for ( iter = cats.begin(); iter != cats.end(); ++iter )
66 mCategoriesView->insertItem( new QCheckListItem( mCategoriesView, (*iter), QCheckListItem::CheckBox ) );
67 filterNameTextChanged( mNameEdit->text() );
68}
69
70FilterEditDialog::~FilterEditDialog()
71{
72}
73
74void FilterEditDialog::setFilter( const Filter &filter )
75{
76 mNameEdit->setText( filter.name() );
77
78 QStringList categories = filter.categories();
79 QListViewItem *item = mCategoriesView->firstChild();
80 while ( item != 0 ) {
81 if ( categories.contains( item->text( 0 ) ) ) {
82 QCheckListItem *checkItem = static_cast<QCheckListItem*>( item );
83 checkItem->setOn( true );
84 }
85
86 item = item->nextSibling();
87 }
88
89 if ( filter.matchRule() == Filter::Matching )
90 mMatchRuleGroup->setButton( 0 );
91 else
92 mMatchRuleGroup->setButton( 1 );
93}
94
95Filter FilterEditDialog::filter()
96{
97 Filter filter;
98
99 filter.setName( mNameEdit->text() );
100
101 QStringList categories;
102 QListViewItem *item = mCategoriesView->firstChild();
103 while ( item != 0 ) {
104 QCheckListItem *checkItem = static_cast<QCheckListItem*>( item );
105 if ( checkItem->isOn() )
106 categories.append( item->text( 0 ) );
107
108 item = item->nextSibling();
109 }
110 filter.setCategories( categories );
111
112 if ( mMatchRuleGroup->find( 0 )->isOn() )
113 filter.setMatchRule( Filter::Matching );
114 else
115 filter.setMatchRule( Filter::NotMatching );
116
117 return filter;
118}
119
120void FilterEditDialog::initGUI()
121{
122#ifndef KAB_EMBEDDED
123 resize( 490, 300 );
124#else //KAB_EMBEDDED
125 resize( KMIN(KGlobal::getDesktopWidth()-10, 490), KMIN(KGlobal::getDesktopHeight()-50, 300));
126#endif //KAB_EMBEDDED
127
128
129 QWidget *page = plainPage();
130 QLabel *label;
131
132 QGridLayout *topLayout = new QGridLayout( page, 3, 2, 0, spacingHint() );
133
134 label = new QLabel( i18n( "Name" ), page );
135 mNameEdit = new KLineEdit( page );
136 mNameEdit->setFocus();
137 topLayout->addWidget( label, 0, 0 );
138 topLayout->addWidget( mNameEdit, 0, 1 );
139 connect( mNameEdit, SIGNAL( textChanged( const QString& ) ),
140 SLOT( filterNameTextChanged( const QString&) ) );
141
142 mCategoriesView = new KListView( page );
143 mCategoriesView->addColumn( i18n( "Categories" ) );
144 topLayout->addMultiCellWidget( mCategoriesView, 1, 1, 0, 1 );
145
146 mMatchRuleGroup = new QButtonGroup( page );
147 mMatchRuleGroup->setExclusive( true );
148
149 QBoxLayout *gbLayout = new QVBoxLayout( mMatchRuleGroup );
150 gbLayout->setSpacing( KDialog::spacingHint() );
151 gbLayout->setMargin( KDialog::marginHint() );
152
153 QRadioButton *radio = new QRadioButton( i18n( "Show only contacts matching\n the selected categories" ), mMatchRuleGroup );
154 radio->setChecked( true );
155 mMatchRuleGroup->insert( radio );
156 gbLayout->addWidget( radio );
157
158 radio = new QRadioButton( i18n( "Show all contacts except those\n matching the selected categories" ), mMatchRuleGroup );
159 mMatchRuleGroup->insert( radio );
160 gbLayout->addWidget( radio );
161
162 topLayout->addMultiCellWidget( mMatchRuleGroup, 2, 2, 0, 1 );
163}
164
165void FilterEditDialog::filterNameTextChanged( const QString &text )
166{
167 enableButtonOK( !text.isEmpty() );
168}
169
170void FilterEditDialog::slotHelp()
171{
172#ifndef KAB_EMBEDDED
173 kapp->invokeHelp( "using-filters" );
174#endif //KAB_EMBEDDED
175}
176
177FilterDialog::FilterDialog( QWidget *parent, const char *name )
178 : KDialogBase( Plain, i18n( "Edit Address Book Filters" ),
179 Ok | Cancel, Ok, parent, name, /*US false*/true, true )
180{
181 initGUI();
182}
183
184FilterDialog::~FilterDialog()
185{
186}
187
188void FilterDialog::setFilters( const Filter::List &list )
189{
190 mFilterList.clear();
191 mInternalFilterList.clear();
192
193 Filter::List::ConstIterator it;
194 for ( it = list.begin(); it != list.end(); ++it ) {
195 if ( (*it).isInternal() )
196 mInternalFilterList.append( *it );
197 else
198 mFilterList.append( *it );
199 }
200
201 refresh();
202}
203
204Filter::List FilterDialog::filters() const
205{
206 Filter::List list = mFilterList + mInternalFilterList;
207 return list;
208}
209
210void FilterDialog::add()
211{
212#ifndef KAB_EMBEDDED
213 FilterEditDialog dlg( this );
214#else //KAB_EMBEDDED
215 FilterEditDialog dlg( this->parentWidget() );
216#endif //KAB_EMBEDDED
217
218 if ( dlg.exec() )
219 mFilterList.append( dlg.filter() );
220
221 refresh();
222
223 mFilterListBox->setCurrentItem( mFilterListBox->count() - 1 );
224}
225
226void FilterDialog::edit()
227{
228 FilterEditDialog dlg( this );
229
230 uint pos = mFilterListBox->currentItem();
231
232 dlg.setFilter( mFilterList[ pos ] );
233
234 if ( dlg.exec() ) {
235 mFilterList.remove( mFilterList.at( pos ) );
236 mFilterList.insert( mFilterList.at( pos ), dlg.filter() );
237 }
238
239 refresh();
240
241 mFilterListBox->setCurrentItem( pos );
242}
243
244void FilterDialog::remove()
245{
246 mFilterList.remove( mFilterList.at( mFilterListBox->currentItem() ) );
247
248 selectionChanged( 0 );
249
250 refresh();
251}
252
253void FilterDialog::refresh()
254{
255 mFilterListBox->clear();
256
257 Filter::List::Iterator iter;
258 for ( iter = mFilterList.begin(); iter != mFilterList.end(); ++iter )
259 mFilterListBox->insertItem( (*iter).name() );
260}
261
262void FilterDialog::selectionChanged( QListBoxItem *item )
263{
264 bool state = ( item != 0 );
265
266 mEditButton->setEnabled( state );
267 mRemoveButton->setEnabled( state );
268}
269
270void FilterDialog::initGUI()
271{
272#ifndef KAB_EMBEDDED
273 resize( 330, 200 );
274#else //KAB_EMBEDDED
275 resize( KMIN(KGlobal::getDesktopWidth()-10, 330), KMIN(KGlobal::getDesktopHeight()-50, 200));
276#endif //KAB_EMBEDDED
277
278 QWidget *page = plainPage();
279
280 QGridLayout *topLayout = new QGridLayout( page, 1, 2, 0, spacingHint() );
281
282 mFilterListBox = new KListBox( page );
283 topLayout->addWidget( mFilterListBox, 0, 0 );
284 connect( mFilterListBox, SIGNAL( selectionChanged( QListBoxItem * ) ),
285 SLOT( selectionChanged( QListBoxItem * ) ) );
286 connect( mFilterListBox, SIGNAL( doubleClicked ( QListBoxItem * ) ),
287 SLOT( edit() ) );
288
289 KButtonBox *buttonBox = new KButtonBox( page, Vertical );
290 buttonBox->addButton( i18n( "&Add..." ), this, SLOT( add() ) );
291 mEditButton = buttonBox->addButton( i18n( "&Edit..." ), this, SLOT( edit() ) );
292 mEditButton->setEnabled( false );
293 mRemoveButton = buttonBox->addButton( i18n( "&Remove" ), this, SLOT( remove() ) );
294 mRemoveButton->setEnabled( false );
295
296 buttonBox->layout();
297 topLayout->addWidget( buttonBox, 0, 1 );
298}
299
300#ifndef KAB_EMBEDDED
301#include "filtereditdialog.moc"
302#endif //KAB_EMBEDDED