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 #include "categoryselectdialog.h"
33 
34 #include "categoryeditdialog.h"
35 
36 using namespace KPIM;
37 
38 class CategoryEditDialog::Private
39 {
40  public:
41  TQListView *mView;
42  TQPushButton *mAddButton;
43  TQPushButton *mEditButton;
44  TQPushButton *mDeleteButton;
45 };
46 
47 class CategoryListViewItem : public TQListViewItem
48 {
49  public:
50  CategoryListViewItem( TQListView *view, const TQString &text ) :
51  TQListViewItem( view, text )
52  {
53  }
54 
55  void okRename ( int col ) // we need that public to explicitly accept renaming when closing the dialog
56  {
57  TQListViewItem::okRename( col );
58  }
59 };
60 
61 CategoryEditDialog::CategoryEditDialog( KPimPrefs *prefs, TQWidget* parent,
62  const char* name, bool modal )
63  : KDialogBase::KDialogBase( parent, name, modal,
64  i18n("Edit Categories"), Ok|Apply|Cancel|Help, Ok, true ),
65  mPrefs( prefs ), d( new Private )
66 {
67  TQWidget *widget = new TQWidget( this );
68  setMainWidget( widget );
69 
70  TQGridLayout *layout = new TQGridLayout( widget, 4, 2, marginHint(), spacingHint() );
71 
72  d->mView = new TQListView( widget );
73  d->mView->addColumn( "" );
74  d->mView->header()->hide();
75  d->mView->setDefaultRenameAction( TQListView::Accept );
76 
77  layout->addMultiCellWidget( d->mView, 0, 3, 0, 0 );
78 
79  d->mAddButton = new TQPushButton( i18n( "Add" ), widget );
80  layout->addWidget( d->mAddButton, 0, 1 );
81 
82  d->mEditButton = new TQPushButton( i18n( "Edit" ), widget );
83  layout->addWidget( d->mEditButton, 1, 1 );
84 
85  d->mDeleteButton = new TQPushButton( i18n( "Remove" ), widget );
86  layout->addWidget( d->mDeleteButton, 2, 1 );
87 
88 
89  fillList();
90 
91  connect( d->mAddButton, TQT_SIGNAL( clicked() ), this, TQT_SLOT( add() ) );
92  connect( d->mEditButton, TQT_SIGNAL( clicked() ), this, TQT_SLOT( edit() ) );
93  connect( d->mDeleteButton, TQT_SIGNAL( clicked() ), this, TQT_SLOT( remove() ) );
94 }
95 
96 /*
97  * Destroys the object and frees any allocated resources
98  */
99 CategoryEditDialog::~CategoryEditDialog()
100 {
101  delete d;
102 }
103 
104 void CategoryEditDialog::fillList()
105 {
106  d->mView->clear();
107  TQStringList::Iterator it;
108  bool categoriesExist=false;
109  for ( it = mPrefs->mCustomCategories.begin();
110  it != mPrefs->mCustomCategories.end(); ++it ) {
111 
112  TQListViewItem *item = new CategoryListViewItem( d->mView, *it );
113  item->setRenameEnabled( 0, true );
114 
115  categoriesExist = true;
116  }
117 
118  d->mEditButton->setEnabled( categoriesExist );
119  d->mDeleteButton->setEnabled( categoriesExist );
120  d->mView->setSelected( d->mView->firstChild(), true );
121 }
122 
123 void CategoryEditDialog::add()
124 {
125  if ( d->mView->firstChild() )
126  d->mView->setCurrentItem( d->mView->firstChild() );
127 
128  TQListViewItem *item = new CategoryListViewItem( d->mView, i18n( "New category" ) );
129  item->setRenameEnabled( 0, true );
130 
131  d->mView->setSelected( item, true );
132  d->mView->ensureItemVisible( item );
133  item->startRename( 0 );
134 
135  bool itemCount = d->mView->childCount() > 0;
136  d->mEditButton->setEnabled( itemCount );
137  d->mDeleteButton->setEnabled( itemCount );
138 }
139 
140 void CategoryEditDialog::edit()
141 {
142  if ( d->mView->currentItem() )
143  d->mView->currentItem()->startRename( 0 );
144 }
145 
146 void CategoryEditDialog::remove()
147 {
148  if ( d->mView->currentItem() ) {
149  delete d->mView->currentItem();
150 
151  d->mView->setSelected( d->mView->currentItem(), true );
152 
153  bool itemCount = d->mView->childCount() > 0;
154  d->mEditButton->setEnabled( itemCount );
155  d->mDeleteButton->setEnabled( itemCount );
156  }
157 }
158 
159 void CategoryEditDialog::slotOk()
160 {
161  // accept the currently ongoing rename
162  if ( d->mView->selectedItem() )
163  static_cast<CategoryListViewItem*>( d->mView->selectedItem() )->okRename( 0 );
164  slotApply();
165  accept();
166 }
167 
168 void CategoryEditDialog::slotApply()
169 {
170  mPrefs->mCustomCategories.clear();
171 
172  TQListViewItem *item = d->mView->firstChild();
173  while ( item ) {
174  if ( !item->text( 0 ).isEmpty() )
175  mPrefs->mCustomCategories.append( item->text( 0 ) );
176  item = item->nextSibling();
177  }
178  mPrefs->writeConfig();
179 
180  emit categoryConfigChanged();
181 
182  if(dynamic_cast<CategorySelectDialog*>(mPrefs->toNotify))
183  ((CategorySelectDialog*)mPrefs->toNotify)->updateCategoryConfig();
184 }
185 
186 void CategoryEditDialog::slotCancel()
187 {
188  reload();
189  KDialogBase::slotCancel();
190 }
191 
192 void CategoryEditDialog::reload()
193 {
194  fillList();
195 }
196 
197 #include "categoryeditdialog.moc"
TDEPIM classes for drag and drop of mails.