lib

KoCompletionDia.cpp

00001 /* This file is part of the KDE project
00002    Copyright (C) 2005 Thomas Zander <zander@kde.org>
00003 
00004    This library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License version 2 as published by the Free Software Foundation.
00007 
00008    This library is distributed in the hope that it will be useful,
00009    but WITHOUT ANY WARRANTY; without even the implied warranty of
00010    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011    Library General Public License for more details.
00012 
00013    You should have received a copy of the GNU Library General Public License
00014    along with this library; see the file COPYING.LIB.  If not, write to
00015    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00016  * Boston, MA 02110-1301, USA.
00017 */
00018 
00019 #include "KoCompletionDia.h"
00020 #include "KoAutoFormat.h"
00021 
00022 #include <klocale.h>
00023 #include <kmessagebox.h>
00024 #include <kinputdialog.h>
00025 #include <kcompletion.h>
00026 #include <kconfig.h>
00027 #include <kdebug.h>
00028 #include <qlayout.h>
00029 #include <qvbox.h>
00030 #include <qcheckbox.h>
00031 #include <qpushbutton.h>
00032 #include <qspinbox.h>
00033 #include <qcombobox.h>
00034 #include <qgroupbox.h>
00035 #include <qwhatsthis.h>
00036 
00037 KoCompletionDia::KoCompletionDia( QWidget *parent, const char *name, KoAutoFormat * autoFormat )
00038     : KDialogBase( parent, name , true, i18n( "Completion" ), Ok|Cancel|User1,
00039       Ok, true, KGuiItem( i18n( "&Reset" ), "undo" ) )
00040 {
00041     QVBox *page = makeVBoxMainWidget();
00042     m_widget = new KoCompletion(page, autoFormat);
00043     m_widget->layout()->setMargin(0);
00044     connect( this, SIGNAL( user1Clicked() ), m_widget, SLOT(slotResetConf()));
00045     setButtonWhatsThis(Ok,i18n("This will save your options."));
00046     setButtonWhatsThis(Cancel,i18n("This will abort all changes."));
00047     setButtonWhatsThis(User1,i18n("This will reset to the state after you clicked on the Make Default button."));
00048 }
00049 
00050 void KoCompletionDia::slotOk()
00051 {
00052     m_widget->saveSettings();
00053     KDialogBase::slotOk();
00054 }
00055 
00056 KoCompletion::KoCompletion(QWidget *parent, KoAutoFormat *autoFormat) : KoCompletionBase(parent),
00057       m_autoFormat( *autoFormat ),
00058       m_docAutoFormat( autoFormat )
00059 {
00060     connect(cbAllowCompletion, SIGNAL(toggled ( bool )), this, SLOT( changeButtonStatus()));
00061     QStringList lst;
00062     lst << i18n( "Enter" );
00063     lst << i18n( "Tab" );
00064     lst << i18n( "Space" );
00065     lst << i18n( "End" );
00066     lst << i18n( "Right" );
00067     m_completionKeyAction->insertStringList( lst );
00068 
00069     connect( m_lbListCompletion, SIGNAL( selected ( const QString & ) ), this, SLOT( slotCompletionWordSelected( const QString & )));
00070     connect( m_lbListCompletion, SIGNAL( highlighted ( const QString & ) ), this, SLOT( slotCompletionWordSelected( const QString & )));
00071 
00072     connect( pbAddCompletionEntry, SIGNAL( clicked() ), this, SLOT( slotAddCompletionEntry()));
00073     connect( pbRemoveCompletionEntry, SIGNAL( clicked() ), this, SLOT( slotRemoveCompletionEntry()));
00074     connect( pbSaveCompletionEntry, SIGNAL( clicked() ), this, SLOT( slotSaveCompletionEntry()));
00075 
00076     slotResetConf(); // aka load config
00077     changeButtonStatus();
00078 }
00079 
00080 void KoCompletion::changeButtonStatus() {
00081     bool state = cbAllowCompletion->isChecked();
00082 
00083     completionBox->setEnabled( state);
00084     cbAddCompletionWord->setEnabled( state );
00085     pbAddCompletionEntry->setEnabled( state );
00086     m_lbListCompletion->setEnabled( state );
00087     state = state && (m_lbListCompletion->count()!=0 && !m_lbListCompletion->currentText().isEmpty());
00088     pbRemoveCompletionEntry->setEnabled( state );
00089 }
00090 
00091 void KoCompletion::slotResetConf() {
00092     cbAllowCompletion->setChecked( m_autoFormat.getConfigCompletion());
00093     cbShowToolTip->setChecked( m_autoFormat.getConfigToolTipCompletion());
00094     cbAddCompletionWord->setChecked( m_autoFormat.getConfigAddCompletionWord());
00095     m_lbListCompletion->clear();
00096     m_listCompletion = m_docAutoFormat->listCompletion();
00097     m_lbListCompletion->insertStringList( m_listCompletion );
00098     m_lbListCompletion->sort();
00099     if( m_listCompletion.isEmpty() || m_lbListCompletion->currentText().isEmpty())
00100         pbRemoveCompletionEntry->setEnabled( false );
00101     m_minWordLength->setValue ( m_docAutoFormat->getConfigMinWordLength() );
00102     m_maxNbWordCompletion->setValue ( m_docAutoFormat->getConfigNbMaxCompletionWord() );
00103     cbAppendSpace->setChecked( m_autoFormat.getConfigAppendSpace() );
00104 
00105     switch( m_docAutoFormat->getConfigKeyAction() )
00106     {
00107     case KoAutoFormat::Enter:
00108         m_completionKeyAction->setCurrentItem( 0 );
00109         break;
00110     case KoAutoFormat::Tab:
00111         m_completionKeyAction->setCurrentItem( 1 );
00112         break;
00113     case KoAutoFormat::Space:
00114         m_completionKeyAction->setCurrentItem( 2 );
00115         break;
00116     case KoAutoFormat::End:
00117         m_completionKeyAction->setCurrentItem( 3 );
00118         break;
00119     case KoAutoFormat::Right:
00120         m_completionKeyAction->setCurrentItem( 4 );
00121         break;
00122     default:
00123         m_completionKeyAction->setCurrentItem( 0 );
00124     }
00125     changeButtonStatus();
00126 }
00127 
00128 void KoCompletion::slotAddCompletionEntry() {
00129     bool ok;
00130     QString const newWord = KInputDialog::getText( i18n("Add Completion Entry"), i18n("Enter entry:"), QString::null, &ok, this ).lower();
00131     if ( ok )
00132     {
00133         if ( !m_listCompletion.contains( newWord ))
00134         {
00135             m_listCompletion.append( newWord );
00136             m_lbListCompletion->insertItem( newWord );
00137             pbRemoveCompletionEntry->setEnabled( !m_lbListCompletion->currentText().isEmpty() );
00138             m_lbListCompletion->sort();
00139         }
00140 
00141     }
00142 }
00143 
00144 void KoCompletion::slotRemoveCompletionEntry() {
00145     QString text = m_lbListCompletion->currentText();
00146     if( !text.isEmpty() )
00147     {
00148         m_listCompletion.remove( text );
00149         m_lbListCompletion->removeItem( m_lbListCompletion->currentItem () );
00150         if( m_lbListCompletion->count()==0 )
00151             pbRemoveCompletionEntry->setEnabled( false );
00152     }
00153 }
00154 
00155 void KoCompletion::slotCompletionWordSelected( const QString & word) {
00156     pbRemoveCompletionEntry->setEnabled( !word.isEmpty() );
00157 }
00158 
00159 void KoCompletion::saveSettings() {
00160     m_docAutoFormat->configCompletion( cbAllowCompletion->isChecked());
00161     m_docAutoFormat->configToolTipCompletion( cbShowToolTip->isChecked());
00162     m_docAutoFormat->configAppendSpace( cbAppendSpace->isChecked() );
00163     m_docAutoFormat->configMinWordLength( m_minWordLength->value() );
00164     m_docAutoFormat->configNbMaxCompletionWord( m_maxNbWordCompletion->value () );
00165     m_docAutoFormat->configAddCompletionWord( cbAddCompletionWord->isChecked());
00166 
00167     m_docAutoFormat->getCompletion()->setItems( m_listCompletion );
00168     m_docAutoFormat->updateMaxWords();
00169     switch( m_completionKeyAction->currentItem() ) {
00170         case 1:
00171             m_docAutoFormat->configKeyCompletionAction( KoAutoFormat::Tab );
00172             break;
00173         case 2:
00174             m_docAutoFormat->configKeyCompletionAction( KoAutoFormat::Space );
00175             break;
00176         case 3:
00177             m_docAutoFormat->configKeyCompletionAction( KoAutoFormat::End );
00178             break;
00179         case 4:
00180             m_docAutoFormat->configKeyCompletionAction( KoAutoFormat::Right );
00181             break;
00182         case 0:
00183         default:
00184             m_docAutoFormat->configKeyCompletionAction( KoAutoFormat::Enter );
00185     }
00186 
00187     // Save to config file
00188     m_docAutoFormat->saveConfig();
00189 }
00190 
00191 void KoCompletion::slotSaveCompletionEntry() {
00192     KConfig config("kofficerc");
00193     KConfigGroupSaver cgs( &config, "Completion Word" );
00194     config.writeEntry( "list", m_listCompletion );
00195     config.sync();
00196     KMessageBox::information( this, i18n(
00197             "Completion list saved.\nIt will be used for all documents "
00198             "from now on."), i18n("Completion List Saved") );
00199 }
00200 
00201 #include "KoCompletionDia.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys