lib

KoCompletionDia.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2005 Thomas Zander <zander@kde.org>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License version 2 as published by the Free Software Foundation.
7
8 This library is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 Library General Public License for more details.
12
13 You should have received a copy of the GNU Library General Public License
14 along with this library; see the file COPYING.LIB. If not, write to
15 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16 * Boston, MA 02110-1301, USA.
17*/
18
19#include "KoCompletionDia.h"
20#include "KoAutoFormat.h"
21
22#include <tdelocale.h>
23#include <tdemessagebox.h>
24#include <kinputdialog.h>
25#include <kcompletion.h>
26#include <tdeconfig.h>
27#include <kdebug.h>
28#include <tqlayout.h>
29#include <tqvbox.h>
30#include <tqcheckbox.h>
31#include <tqpushbutton.h>
32#include <tqspinbox.h>
33#include <tqcombobox.h>
34#include <tqgroupbox.h>
35#include <tqwhatsthis.h>
36
37KoCompletionDia::KoCompletionDia( TQWidget *parent, const char *name, KoAutoFormat * autoFormat )
38 : KDialogBase( parent, name , true, i18n( "Completion" ), Ok|Cancel|User1,
39 Ok, true, KGuiItem( i18n( "&Reset" ), "edit-undo" ) )
40{
41 TQVBox *page = makeVBoxMainWidget();
42 m_widget = new KoCompletion(page, autoFormat);
43 m_widget->layout()->setMargin(0);
44 connect( this, TQ_SIGNAL( user1Clicked() ), m_widget, TQ_SLOT(slotResetConf()));
45 setButtonWhatsThis(Ok,i18n("This will save your options."));
46 setButtonWhatsThis(Cancel,i18n("This will abort all changes."));
47 setButtonWhatsThis(User1,i18n("This will reset to the state after you clicked on the Make Default button."));
48}
49
50void KoCompletionDia::slotOk()
51{
52 m_widget->saveSettings();
54}
55
56KoCompletion::KoCompletion(TQWidget *parent, KoAutoFormat *autoFormat) : KoCompletionBase(parent),
57 m_autoFormat( *autoFormat ),
58 m_docAutoFormat( autoFormat )
59{
60 connect(cbAllowCompletion, TQ_SIGNAL(toggled ( bool )), this, TQ_SLOT( changeButtonStatus()));
61 TQStringList lst;
62 lst << i18n( "Enter" );
63 lst << i18n( "Tab" );
64 lst << i18n( "Space" );
65 lst << i18n( "End" );
66 lst << i18n( "Right" );
67 m_completionKeyAction->insertStringList( lst );
68
69 connect( m_lbListCompletion, TQ_SIGNAL( selected ( const TQString & ) ), this, TQ_SLOT( slotCompletionWordSelected( const TQString & )));
70 connect( m_lbListCompletion, TQ_SIGNAL( highlighted ( const TQString & ) ), this, TQ_SLOT( slotCompletionWordSelected( const TQString & )));
71
72 connect( pbAddCompletionEntry, TQ_SIGNAL( clicked() ), this, TQ_SLOT( slotAddCompletionEntry()));
73 connect( pbRemoveCompletionEntry, TQ_SIGNAL( clicked() ), this, TQ_SLOT( slotRemoveCompletionEntry()));
74 connect( pbSaveCompletionEntry, TQ_SIGNAL( clicked() ), this, TQ_SLOT( slotSaveCompletionEntry()));
75
76 slotResetConf(); // aka load config
77 changeButtonStatus();
78}
79
80void KoCompletion::changeButtonStatus() {
81 bool state = cbAllowCompletion->isChecked();
82
83 completionBox->setEnabled( state);
84 cbAddCompletionWord->setEnabled( state );
85 pbAddCompletionEntry->setEnabled( state );
86 m_lbListCompletion->setEnabled( state );
87 state = state && (m_lbListCompletion->count()!=0 && !m_lbListCompletion->currentText().isEmpty());
88 pbRemoveCompletionEntry->setEnabled( state );
89}
90
91void KoCompletion::slotResetConf() {
92 cbAllowCompletion->setChecked( m_autoFormat.getConfigCompletion());
93 cbShowToolTip->setChecked( m_autoFormat.getConfigToolTipCompletion());
94 cbAddCompletionWord->setChecked( m_autoFormat.getConfigAddCompletionWord());
95 m_lbListCompletion->clear();
96 m_listCompletion = m_docAutoFormat->listCompletion();
97 m_lbListCompletion->insertStringList( m_listCompletion );
98 m_lbListCompletion->sort();
99 if( m_listCompletion.isEmpty() || m_lbListCompletion->currentText().isEmpty())
100 pbRemoveCompletionEntry->setEnabled( false );
101 m_minWordLength->setValue ( m_docAutoFormat->getConfigMinWordLength() );
102 m_maxNbWordCompletion->setValue ( m_docAutoFormat->getConfigNbMaxCompletionWord() );
103 cbAppendSpace->setChecked( m_autoFormat.getConfigAppendSpace() );
104
105 switch( m_docAutoFormat->getConfigKeyAction() )
106 {
107 case KoAutoFormat::Enter:
108 m_completionKeyAction->setCurrentItem( 0 );
109 break;
110 case KoAutoFormat::Tab:
111 m_completionKeyAction->setCurrentItem( 1 );
112 break;
113 case KoAutoFormat::Space:
114 m_completionKeyAction->setCurrentItem( 2 );
115 break;
116 case KoAutoFormat::End:
117 m_completionKeyAction->setCurrentItem( 3 );
118 break;
119 case KoAutoFormat::Right:
120 m_completionKeyAction->setCurrentItem( 4 );
121 break;
122 default:
123 m_completionKeyAction->setCurrentItem( 0 );
124 }
125 changeButtonStatus();
126}
127
128void KoCompletion::slotAddCompletionEntry() {
129 bool ok;
130 TQString const newWord = KInputDialog::getText( i18n("Add Completion Entry"), i18n("Enter entry:"), TQString(), &ok, this ).lower();
131 if ( ok )
132 {
133 if ( !m_listCompletion.contains( newWord ))
134 {
135 m_listCompletion.append( newWord );
136 m_lbListCompletion->insertItem( newWord );
137 pbRemoveCompletionEntry->setEnabled( !m_lbListCompletion->currentText().isEmpty() );
138 m_lbListCompletion->sort();
139 }
140
141 }
142}
143
144void KoCompletion::slotRemoveCompletionEntry() {
145 TQString text = m_lbListCompletion->currentText();
146 if( !text.isEmpty() )
147 {
148 m_listCompletion.remove( text );
149 m_lbListCompletion->removeItem( m_lbListCompletion->currentItem () );
150 if( m_lbListCompletion->count()==0 )
151 pbRemoveCompletionEntry->setEnabled( false );
152 }
153}
154
155void KoCompletion::slotCompletionWordSelected( const TQString & word) {
156 pbRemoveCompletionEntry->setEnabled( !word.isEmpty() );
157}
158
159void KoCompletion::saveSettings() {
160 m_docAutoFormat->configCompletion( cbAllowCompletion->isChecked());
161 m_docAutoFormat->configToolTipCompletion( cbShowToolTip->isChecked());
162 m_docAutoFormat->configAppendSpace( cbAppendSpace->isChecked() );
163 m_docAutoFormat->configMinWordLength( m_minWordLength->value() );
164 m_docAutoFormat->configNbMaxCompletionWord( m_maxNbWordCompletion->value () );
165 m_docAutoFormat->configAddCompletionWord( cbAddCompletionWord->isChecked());
166
167 m_docAutoFormat->getCompletion()->setItems( m_listCompletion );
168 m_docAutoFormat->updateMaxWords();
169 switch( m_completionKeyAction->currentItem() ) {
170 case 1:
171 m_docAutoFormat->configKeyCompletionAction( KoAutoFormat::Tab );
172 break;
173 case 2:
174 m_docAutoFormat->configKeyCompletionAction( KoAutoFormat::Space );
175 break;
176 case 3:
177 m_docAutoFormat->configKeyCompletionAction( KoAutoFormat::End );
178 break;
179 case 4:
180 m_docAutoFormat->configKeyCompletionAction( KoAutoFormat::Right );
181 break;
182 case 0:
183 default:
184 m_docAutoFormat->configKeyCompletionAction( KoAutoFormat::Enter );
185 }
186
187 // Save to config file
188 m_docAutoFormat->saveConfig();
189}
190
191void KoCompletion::slotSaveCompletionEntry() {
192 TDEConfig config("kofficerc");
193 TDEConfigGroupSaver cgs( &config, "Completion Word" );
194 config.writeEntry( "list", m_listCompletion );
195 config.sync();
196 KMessageBox::information( this, i18n(
197 "Completion list saved.\nIt will be used for all documents "
198 "from now on."), i18n("Completion List Saved") );
199}
200
201#include "KoCompletionDia.moc"
virtual void slotOk()
static TQString getText(const TQString &caption, const TQString &label, const TQString &value=TQString::null, bool *ok=0, TQWidget *parent=0, const char *name=0, TQValidator *validator=0, const TQString &mask=TQString::null)
static void information(TQWidget *parent, const TQString &text, const TQString &caption=TQString::null, const TQString &dontShowAgainName=TQString::null, int options=Notify)
Completion configuration widget.
const char * name(StdAction id)