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   mWidget->mEdit->setText( mWidget->mCategories->currentItem()->text( 0 ) );
00080 }
00081 
00082 void CategoryEditDialog::slotTextChanged(const QString &text)
00083 {
00084   QListViewItem *item = mWidget->mCategories->currentItem();
00085   if ( item ) {
00086     item->setText( 0, text );
00087   }
00088 }
00089 
00090 void CategoryEditDialog::add()
00091 {
00092   if ( !mWidget->mEdit->text().isEmpty() ) {
00093     QListViewItem *newItem = new QListViewItem( mWidget->mCategories, "" );
00094     // FIXME: Use a better string once string changes are allowed again
00095 //                                                i18n("New category") );
00096     mWidget->mCategories->setSelected( newItem, true );
00097     mWidget->mCategories->ensureItemVisible( newItem );
00098     mWidget->mButtonRemove->setEnabled( mWidget->mCategories->childCount()>0 );
00099   }
00100 }
00101 
00102 void CategoryEditDialog::remove()
00103 {
00104   if (mWidget->mCategories->currentItem()) {
00105     delete mWidget->mCategories->currentItem();
00106     mWidget->mCategories->setSelected( mWidget->mCategories->currentItem(), true );
00107     mWidget->mButtonRemove->setEnabled( mWidget->mCategories->childCount()>0 );
00108   }
00109 }
00110 
00111 void CategoryEditDialog::slotOk()
00112 {
00113   slotApply();
00114   accept();
00115 }
00116 
00117 void CategoryEditDialog::slotApply()
00118 {
00119   mPrefs->mCustomCategories.clear();
00120 
00121   QListViewItem *item = mWidget->mCategories->firstChild();
00122   while ( item ) {
00123     mPrefs->mCustomCategories.append( item->text(0) );
00124     item = item->nextSibling();
00125   }
00126   mPrefs->writeConfig();
00127 
00128   emit categoryConfigChanged();
00129 }
00130 
00131 void CategoryEditDialog::slotCancel()
00132 {
00133   reload();
00134   KDialogBase::slotCancel();
00135 }
00136 
00137 void CategoryEditDialog::editItem( QListViewItem *item )
00138 {
00139   mWidget->mEdit->setText( item->text(0) );
00140   mWidget->mButtonRemove->setEnabled( true );
00141 }
00142 
00143 void CategoryEditDialog::reload() 
00144 {
00145   fillList();
00146 }
00147 
00148 #include "categoryeditdialog.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys