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 <qheader.h>
00027 #include <qpushbutton.h>
00028 #include <klocale.h>
00029 
00030 #include "kpimprefs.h"
00031 
00032 #include "categoryeditdialog_base.h"
00033 #include "categoryeditdialog.h"
00034 
00035 using namespace KPIM;
00036 
00037 CategoryEditDialog::CategoryEditDialog( KPimPrefs *prefs, QWidget* parent,
00038                                         const char* name, bool modal )
00039   : KDialogBase::KDialogBase( parent, name, modal,
00040     i18n("Edit Categories"), Ok|Apply|Cancel|Help, Ok, true ),
00041     mPrefs( prefs )  
00042 {
00043   mWidget = new CategoryEditDialog_base( this, "CategoryEdit" );
00044   mWidget->mCategories->header()->hide();
00045   setMainWidget( mWidget );
00046 
00047   fillList();
00048   
00049   connect( mWidget->mCategories, SIGNAL( selectionChanged( QListViewItem * )),
00050            SLOT( editItem( QListViewItem * )) );
00051   connect( mWidget->mEdit, SIGNAL( textChanged( const QString & )),
00052            this, SLOT( slotTextChanged( const QString & )));
00053   connect( mWidget->mButtonAdd, SIGNAL( clicked() ),
00054            this, SLOT( add() ) );
00055   connect( mWidget->mButtonRemove, SIGNAL( clicked() ),
00056            this, SLOT( remove() ) );
00057 }
00058 
00059 /*
00060  *  Destroys the object and frees any allocated resources
00061  */
00062 CategoryEditDialog::~CategoryEditDialog()
00063 {
00064     // no need to delete child widgets, Qt does it all for us
00065 }
00066 
00067 void CategoryEditDialog::fillList()
00068 {
00069   mWidget->mCategories->clear();
00070   QStringList::Iterator it;
00071   bool categoriesExist=false;
00072   for ( it = mPrefs->mCustomCategories.begin();
00073         it != mPrefs->mCustomCategories.end(); ++it ) {
00074     new QListViewItem( mWidget->mCategories, *it );
00075     categoriesExist = true;
00076   }
00077   mWidget->mButtonRemove->setEnabled( categoriesExist );
00078   mWidget->mCategories->setSelected( mWidget->mCategories->firstChild(), true );
00079 }
00080 
00081 void CategoryEditDialog::slotTextChanged(const QString &text)
00082 {
00083   QListViewItem *item = mWidget->mCategories->currentItem();
00084   if ( item ) {
00085     item->setText( 0, text );
00086   }
00087 }
00088 
00089 void CategoryEditDialog::add()
00090 {
00091   if ( !mWidget->mEdit->text().isEmpty() ) {
00092     QListViewItem *newItem = new QListViewItem( mWidget->mCategories, "" );
00093     // FIXME: Use a better string once string changes are allowed again
00094 //                                                i18n("New category") );
00095     mWidget->mCategories->setSelected( newItem, true );
00096     mWidget->mCategories->ensureItemVisible( newItem );
00097     mWidget->mButtonRemove->setEnabled( mWidget->mCategories->childCount()>0 );
00098   }
00099 }
00100 
00101 void CategoryEditDialog::remove()
00102 {
00103   if (mWidget->mCategories->currentItem()) {
00104     delete mWidget->mCategories->currentItem();
00105     mWidget->mCategories->setSelected( mWidget->mCategories->currentItem(), true );
00106     mWidget->mButtonRemove->setEnabled( mWidget->mCategories->childCount()>0 );
00107   }
00108 }
00109 
00110 void CategoryEditDialog::slotOk()
00111 {
00112   slotApply();
00113   accept();
00114 }
00115 
00116 void CategoryEditDialog::slotApply()
00117 {
00118   mPrefs->mCustomCategories.clear();
00119 
00120   QListViewItem *item = mWidget->mCategories->firstChild();
00121   while ( item ) {
00122     mPrefs->mCustomCategories.append( item->text(0) );
00123     item = item->nextSibling();
00124   }
00125   mPrefs->writeConfig();
00126 
00127   emit categoryConfigChanged();
00128 }
00129 
00130 void CategoryEditDialog::slotCancel()
00131 {
00132   reload();
00133   KDialogBase::slotCancel();
00134 }
00135 
00136 void CategoryEditDialog::editItem( QListViewItem *item )
00137 {
00138   mWidget->mEdit->setText( item->text(0) );
00139   mWidget->mButtonRemove->setEnabled( true );
00140 }
00141 
00142 void CategoryEditDialog::reload() 
00143 {
00144   fillList();
00145 }
00146 
00147 #include "categoryeditdialog.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys