libtdepim

categoryeditdialog.cpp
1/*
2 This file is part of libtdepim.
3
4 Copyright (c) 2000, 2001, 2002 Cornelius Schumacher <schumacher@kde.org>
5 Copyright (C) 2003-2004 Reinhold Kainhofer <reinhold@kainhofer.com>
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Library General Public
9 License as published by the Free Software Foundation; either
10 version 2 of the License, or (at your option) any later version.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Library General Public License for more details.
16
17 You should have received a copy of the GNU Library General Public License
18 along with this library; see the file COPYING.LIB. If not, write to
19 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA.
21*/
22
23#include <tqstringlist.h>
24#include <tqlineedit.h>
25#include <tqlistview.h>
26#include <tqlayout.h>
27#include <tqheader.h>
28#include <tqpushbutton.h>
29#include <tdelocale.h>
30
31#include "kpimprefs.h"
32
33#include "categoryeditdialog.h"
34
35using namespace KPIM;
36
37class CategoryEditDialog::Private
38{
39 public:
40 TQListView *mView;
41 TQPushButton *mAddButton;
42 TQPushButton *mEditButton;
43 TQPushButton *mDeleteButton;
44};
45
46class CategoryListViewItem : public TQListViewItem
47{
48 public:
49 CategoryListViewItem( TQListView *view, const TQString &text ) :
50 TQListViewItem( view, text )
51 {
52 }
53
54 void okRename ( int col ) // we need that public to explicitly accept renaming when closing the dialog
55 {
56 TQListViewItem::okRename( col );
57 }
58};
59
60CategoryEditDialog::CategoryEditDialog( KPimPrefs *prefs, TQWidget* parent,
61 const char* name, bool modal )
62 : KDialogBase::KDialogBase( parent, name, modal,
63 i18n("Edit Categories"), Ok|Apply|Cancel|Help, Ok, true ),
64 mPrefs( prefs ), d( new Private )
65{
66 TQWidget *widget = new TQWidget( this );
67 setMainWidget( widget );
68
69 TQGridLayout *layout = new TQGridLayout( widget, 4, 2, marginHint(), spacingHint() );
70
71 d->mView = new TQListView( widget );
72 d->mView->addColumn( "" );
73 d->mView->header()->hide();
74 d->mView->setDefaultRenameAction( TQListView::Accept );
75
76 layout->addMultiCellWidget( d->mView, 0, 3, 0, 0 );
77
78 d->mAddButton = new TQPushButton( i18n( "Add" ), widget );
79 layout->addWidget( d->mAddButton, 0, 1 );
80
81 d->mEditButton = new TQPushButton( i18n( "Edit" ), widget );
82 layout->addWidget( d->mEditButton, 1, 1 );
83
84 d->mDeleteButton = new TQPushButton( i18n( "Remove" ), widget );
85 layout->addWidget( d->mDeleteButton, 2, 1 );
86
87
88 fillList();
89
90 connect( d->mAddButton, TQ_SIGNAL( clicked() ), this, TQ_SLOT( add() ) );
91 connect( d->mEditButton, TQ_SIGNAL( clicked() ), this, TQ_SLOT( edit() ) );
92 connect( d->mDeleteButton, TQ_SIGNAL( clicked() ), this, TQ_SLOT( remove() ) );
93}
94
95/*
96 * Destroys the object and frees any allocated resources
97 */
98CategoryEditDialog::~CategoryEditDialog()
99{
100 delete d;
101}
102
103void CategoryEditDialog::fillList()
104{
105 d->mView->clear();
106 TQStringList::Iterator it;
107 bool categoriesExist=false;
108 for ( it = mPrefs->mCustomCategories.begin();
109 it != mPrefs->mCustomCategories.end(); ++it ) {
110
111 TQListViewItem *item = new CategoryListViewItem( d->mView, *it );
112 item->setRenameEnabled( 0, true );
113
114 categoriesExist = true;
115 }
116
117 d->mEditButton->setEnabled( categoriesExist );
118 d->mDeleteButton->setEnabled( categoriesExist );
119 d->mView->setSelected( d->mView->firstChild(), true );
120}
121
122void CategoryEditDialog::add()
123{
124 if ( d->mView->firstChild() )
125 d->mView->setCurrentItem( d->mView->firstChild() );
126
127 TQListViewItem *item = new CategoryListViewItem( d->mView, i18n( "New category" ) );
128 item->setRenameEnabled( 0, true );
129
130 d->mView->setSelected( item, true );
131 d->mView->ensureItemVisible( item );
132 item->startRename( 0 );
133
134 bool itemCount = d->mView->childCount() > 0;
135 d->mEditButton->setEnabled( itemCount );
136 d->mDeleteButton->setEnabled( itemCount );
137}
138
139void CategoryEditDialog::edit()
140{
141 if ( d->mView->currentItem() )
142 d->mView->currentItem()->startRename( 0 );
143}
144
145void CategoryEditDialog::remove()
146{
147 if ( d->mView->currentItem() ) {
148 delete d->mView->currentItem();
149
150 d->mView->setSelected( d->mView->currentItem(), true );
151
152 bool itemCount = d->mView->childCount() > 0;
153 d->mEditButton->setEnabled( itemCount );
154 d->mDeleteButton->setEnabled( itemCount );
155 }
156}
157
158void CategoryEditDialog::slotOk()
159{
160 // accept the currently ongoing rename
161 if ( d->mView->selectedItem() )
162 static_cast<CategoryListViewItem*>( d->mView->selectedItem() )->okRename( 0 );
163 slotApply();
164 accept();
165}
166
167void CategoryEditDialog::slotApply()
168{
169 mPrefs->mCustomCategories.clear();
170
171 TQListViewItem *item = d->mView->firstChild();
172 while ( item ) {
173 if ( !item->text( 0 ).isEmpty() )
174 mPrefs->mCustomCategories.append( item->text( 0 ) );
175 item = item->nextSibling();
176 }
177 mPrefs->writeConfig();
178
179 emit categoryConfigChanged();
180}
181
182void CategoryEditDialog::slotCancel()
183{
184 reload();
185 KDialogBase::slotCancel();
186}
187
188void CategoryEditDialog::reload()
189{
190 fillList();
191}
192
193#include "categoryeditdialog.moc"
TDEPIM classes for drag and drop of mails.