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 <qstringlist.h>
00024 #include <qlineedit.h>
00025 #include <qlistview.h>
00026 #include <qlayout.h>
00027 #include <qheader.h>
00028 #include <qpushbutton.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     QListView *mView;
00041     QPushButton *mAddButton;
00042     QPushButton *mEditButton;
00043     QPushButton *mDeleteButton;
00044 };
00045 
00046 CategoryEditDialog::CategoryEditDialog( KPimPrefs *prefs, QWidget* parent,
00047                                         const char* name, bool modal )
00048   : KDialogBase::KDialogBase( parent, name, modal,
00049     i18n("Edit Categories"), Ok|Apply|Cancel|Help, Ok, true ),
00050     mPrefs( prefs ), d( new Private )
00051 {
00052   QWidget *widget = new QWidget( this );
00053   setMainWidget( widget );
00054 
00055   QGridLayout *layout = new QGridLayout( widget, 4, 2, marginHint(), spacingHint() );
00056 
00057   d->mView = new QListView( widget );
00058   d->mView->addColumn( "" );
00059   d->mView->header()->hide();
00060 
00061   layout->addMultiCellWidget( d->mView, 0, 3, 0, 0 );
00062 
00063   d->mAddButton = new QPushButton( i18n( "Add" ), widget );
00064   layout->addWidget( d->mAddButton, 0, 1 );
00065 
00066   d->mEditButton = new QPushButton( i18n( "Edit" ), widget );
00067   layout->addWidget( d->mEditButton, 1, 1 );
00068 
00069   d->mDeleteButton = new QPushButton( i18n( "Remove" ), widget );
00070   layout->addWidget( d->mDeleteButton, 2, 1 );
00071 
00072 
00073   fillList();
00074 
00075   connect( d->mAddButton, SIGNAL( clicked() ), this, SLOT( add() ) );
00076   connect( d->mEditButton, SIGNAL( clicked() ), this, SLOT( edit() ) );
00077   connect( d->mDeleteButton, SIGNAL( clicked() ), this, SLOT( remove() ) );
00078 }
00079 
00080 /*
00081  *  Destroys the object and frees any allocated resources
00082  */
00083 CategoryEditDialog::~CategoryEditDialog()
00084 {
00085   delete d;
00086 }
00087 
00088 void CategoryEditDialog::fillList()
00089 {
00090   d->mView->clear();
00091   QStringList::Iterator it;
00092   bool categoriesExist=false;
00093   for ( it = mPrefs->mCustomCategories.begin();
00094         it != mPrefs->mCustomCategories.end(); ++it ) {
00095 
00096     QListViewItem *item = new QListViewItem( d->mView, *it );
00097     item->setRenameEnabled( 0, true );
00098 
00099     categoriesExist = true;
00100   }
00101 
00102   d->mEditButton->setEnabled( categoriesExist );
00103   d->mDeleteButton->setEnabled( categoriesExist );
00104   d->mView->setSelected( d->mView->firstChild(), true );
00105 }
00106 
00107 void CategoryEditDialog::add()
00108 {
00109   if ( d->mView->firstChild() )
00110     d->mView->setCurrentItem( d->mView->firstChild() );
00111 
00112   QListViewItem *item = new QListViewItem( d->mView, i18n( "New category" ) );
00113   item->setRenameEnabled( 0, true );
00114 
00115   d->mView->setSelected( item, true );
00116   d->mView->ensureItemVisible( item );
00117   item->startRename( 0 );
00118 
00119   bool itemCount = d->mView->childCount() > 0;
00120   d->mEditButton->setEnabled( itemCount );
00121   d->mDeleteButton->setEnabled( itemCount );
00122 }
00123 
00124 void CategoryEditDialog::edit()
00125 {
00126   if ( d->mView->currentItem() )
00127     d->mView->currentItem()->startRename( 0 );
00128 }
00129 
00130 void CategoryEditDialog::remove()
00131 {
00132   if ( d->mView->currentItem() ) {
00133     delete d->mView->currentItem();
00134 
00135     d->mView->setSelected( d->mView->currentItem(), true );
00136 
00137     bool itemCount = d->mView->childCount() > 0;
00138     d->mEditButton->setEnabled( itemCount );
00139     d->mDeleteButton->setEnabled( itemCount );
00140   }
00141 }
00142 
00143 void CategoryEditDialog::slotOk()
00144 {
00145   slotApply();
00146   accept();
00147 }
00148 
00149 void CategoryEditDialog::slotApply()
00150 {
00151   mPrefs->mCustomCategories.clear();
00152 
00153   QListViewItem *item = d->mView->firstChild();
00154   while ( item ) {
00155     if ( !item->text( 0 ).isEmpty() )
00156       mPrefs->mCustomCategories.append( item->text( 0 ) );
00157     item = item->nextSibling();
00158   }
00159   mPrefs->writeConfig();
00160 
00161   emit categoryConfigChanged();
00162 }
00163 
00164 void CategoryEditDialog::slotCancel()
00165 {
00166   reload();
00167   KDialogBase::slotCancel();
00168 }
00169 
00170 void CategoryEditDialog::reload()
00171 {
00172   fillList();
00173 }
00174 
00175 #include "categoryeditdialog.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys