libkdepim

categoryeditdialog.cpp
00001 /*
00002     This file is part of libkdepim.
00003 
00004     Copyright (c) 2000, 2001, 2002 Cornelius Schumacher <schumacher@kde.org>
00005     Copyright (C) 2003-2004 Reinhold Kainhofer <reinhold@kainhofer.com>
00006 
00007     This library is free software; you can redistribute it and/or
00008     modify it under the terms of the GNU Library General Public
00009     License as published by the Free Software Foundation; either
00010     version 2 of the License, or (at your option) any later version.
00011 
00012     This library is distributed in the hope that it will be useful,
00013     but WITHOUT ANY WARRANTY; without even the implied warranty of
00014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015     Library General Public License for more details.
00016 
00017     You should have received a copy of the GNU Library General Public License
00018     along with this library; see the file COPYING.LIB.  If not, write to
00019     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00020     Boston, MA 02110-1301, USA.
00021 */
00022 
00023 #include <tqstringlist.h>
00024 #include <tqlineedit.h>
00025 #include <tqlistview.h>
00026 #include <tqlayout.h>
00027 #include <tqheader.h>
00028 #include <tqpushbutton.h>
00029 #include <klocale.h>
00030 
00031 #include "kpimprefs.h"
00032 
00033 #include "categoryeditdialog.h"
00034 
00035 using namespace KPIM;
00036 
00037 class CategoryEditDialog::Private
00038 {
00039   public:
00040     TQListView *mView;
00041     TQPushButton *mAddButton;
00042     TQPushButton *mEditButton;
00043     TQPushButton *mDeleteButton;
00044 };
00045 
00046 class CategoryListViewItem : public TQListViewItem
00047 {
00048   public:
00049     CategoryListViewItem( TQListView *view, const TQString &text ) :
00050       TQListViewItem( view, text )
00051     {
00052     }
00053 
00054     void okRename ( int col ) // we need that public to explicitly accept renaming when closing the dialog
00055     {
00056       TQListViewItem::okRename( col );
00057     }
00058 };
00059 
00060 CategoryEditDialog::CategoryEditDialog( KPimPrefs *prefs, TQWidget* parent,
00061                                         const char* name, bool modal )
00062   : KDialogBase::KDialogBase( parent, name, modal,
00063     i18n("Edit Categories"), Ok|Apply|Cancel|Help, Ok, true ),
00064     mPrefs( prefs ), d( new Private )
00065 {
00066   TQWidget *widget = new TQWidget( this );
00067   setMainWidget( widget );
00068 
00069   TQGridLayout *layout = new TQGridLayout( widget, 4, 2, marginHint(), spacingHint() );
00070 
00071   d->mView = new TQListView( widget );
00072   d->mView->addColumn( "" );
00073   d->mView->header()->hide();
00074   d->mView->setDefaultRenameAction( TQListView::Accept );
00075 
00076   layout->addMultiCellWidget( d->mView, 0, 3, 0, 0 );
00077 
00078   d->mAddButton = new TQPushButton( i18n( "Add" ), widget );
00079   layout->addWidget( d->mAddButton, 0, 1 );
00080 
00081   d->mEditButton = new TQPushButton( i18n( "Edit" ), widget );
00082   layout->addWidget( d->mEditButton, 1, 1 );
00083 
00084   d->mDeleteButton = new TQPushButton( i18n( "Remove" ), widget );
00085   layout->addWidget( d->mDeleteButton, 2, 1 );
00086 
00087 
00088   fillList();
00089 
00090   connect( d->mAddButton, TQT_SIGNAL( clicked() ), this, TQT_SLOT( add() ) );
00091   connect( d->mEditButton, TQT_SIGNAL( clicked() ), this, TQT_SLOT( edit() ) );
00092   connect( d->mDeleteButton, TQT_SIGNAL( clicked() ), this, TQT_SLOT( remove() ) );
00093 }
00094 
00095 /*
00096  *  Destroys the object and frees any allocated resources
00097  */
00098 CategoryEditDialog::~CategoryEditDialog()
00099 {
00100   delete d;
00101 }
00102 
00103 void CategoryEditDialog::fillList()
00104 {
00105   d->mView->clear();
00106   TQStringList::Iterator it;
00107   bool categoriesExist=false;
00108   for ( it = mPrefs->mCustomCategories.begin();
00109         it != mPrefs->mCustomCategories.end(); ++it ) {
00110 
00111     TQListViewItem *item = new CategoryListViewItem( d->mView, *it );
00112     item->setRenameEnabled( 0, true );
00113 
00114     categoriesExist = true;
00115   }
00116 
00117   d->mEditButton->setEnabled( categoriesExist );
00118   d->mDeleteButton->setEnabled( categoriesExist );
00119   d->mView->setSelected( d->mView->firstChild(), true );
00120 }
00121 
00122 void CategoryEditDialog::add()
00123 {
00124   if ( d->mView->firstChild() )
00125     d->mView->setCurrentItem( d->mView->firstChild() );
00126 
00127   TQListViewItem *item = new CategoryListViewItem( d->mView, i18n( "New category" ) );
00128   item->setRenameEnabled( 0, true );
00129 
00130   d->mView->setSelected( item, true );
00131   d->mView->ensureItemVisible( item );
00132   item->startRename( 0 );
00133 
00134   bool itemCount = d->mView->childCount() > 0;
00135   d->mEditButton->setEnabled( itemCount );
00136   d->mDeleteButton->setEnabled( itemCount );
00137 }
00138 
00139 void CategoryEditDialog::edit()
00140 {
00141   if ( d->mView->currentItem() )
00142     d->mView->currentItem()->startRename( 0 );
00143 }
00144 
00145 void CategoryEditDialog::remove()
00146 {
00147   if ( d->mView->currentItem() ) {
00148     delete d->mView->currentItem();
00149 
00150     d->mView->setSelected( d->mView->currentItem(), true );
00151 
00152     bool itemCount = d->mView->childCount() > 0;
00153     d->mEditButton->setEnabled( itemCount );
00154     d->mDeleteButton->setEnabled( itemCount );
00155   }
00156 }
00157 
00158 void CategoryEditDialog::slotOk()
00159 {
00160   // accept the currently ongoing rename
00161   if ( d->mView->selectedItem() )
00162     static_cast<CategoryListViewItem*>( d->mView->selectedItem() )->okRename( 0 );
00163   slotApply();
00164   accept();
00165 }
00166 
00167 void CategoryEditDialog::slotApply()
00168 {
00169   mPrefs->mCustomCategories.clear();
00170 
00171   TQListViewItem *item = d->mView->firstChild();
00172   while ( item ) {
00173     if ( !item->text( 0 ).isEmpty() )
00174       mPrefs->mCustomCategories.append( item->text( 0 ) );
00175     item = item->nextSibling();
00176   }
00177   mPrefs->writeConfig();
00178 
00179   emit categoryConfigChanged();
00180 }
00181 
00182 void CategoryEditDialog::slotCancel()
00183 {
00184   reload();
00185   KDialogBase::slotCancel();
00186 }
00187 
00188 void CategoryEditDialog::reload()
00189 {
00190   fillList();
00191 }
00192 
00193 #include "categoryeditdialog.moc"