1 /***************************************************************************
2 wordcompletionwidget.cpp - description
4 begin : Tue Apr 29 2003
5 copyright : (C) 2002 by Gunnar Schmi Dt
6 email : kmouth@schmi-dt.de
7 ***************************************************************************/
9 /***************************************************************************
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
16 ***************************************************************************/
20 #include <qcheckbox.h>
21 #include <qradiobutton.h>
22 #include <qlineedit.h>
23 #include <qcombobox.h>
24 #include <qtextcodec.h>
25 #include <qwhatsthis.h>
27 #include <klistview.h>
28 #include <klineedit.h>
29 #include <kurlrequester.h>
31 #include <kcombobox.h>
33 #include <kapplication.h>
34 #include <kstandarddirs.h>
35 #include <kprogress.h>
38 #include "dictionarycreationwizard.h"
39 #include "klanguagebutton.h"
40 #include "creationsourceui.h"
41 #include "creationsourcedetailsui.h"
42 #include "kdedocsourceui.h"
45 DictionaryCreationWizard::DictionaryCreationWizard (QWidget
*parent
, const char *name
,
46 QStringList dictionaryNames
, QStringList dictionaryFiles
,
47 QStringList dictionaryLanguages
)
48 : KWizard (parent
, name
)
52 creationSource
= new CreationSourceUI (this, "source page");
53 addPage (creationSource
, i18n("Source of New Dictionary (1)"));
54 setHelpEnabled (creationSource
, false);
55 setFinishEnabled (creationSource
, false);
57 fileWidget
= new CreationSourceDetailsUI (this, "file source page");
58 addPage (fileWidget
, i18n("Source of New Dictionary (2)"));
59 buildCodecCombo (fileWidget
->encodingCombo
);
61 dirWidget
= new CreationSourceDetailsUI (this, "directory source page");
62 addPage (dirWidget
, i18n("Source of New Dictionary (2)"));
63 dirWidget
->urlLabel
->setText (i18n("&Directory:"));
64 QWhatsThis::add (dirWidget
->urlLabel
, i18n("With this input field you specify which directory you want to load for creating the new dictionary."));
65 dirWidget
->url
->setMode(KFile::Directory
);
66 QWhatsThis::add (dirWidget
->url
, i18n("With this input field you specify which directory you want to load for creating the new dictionary."));
67 buildCodecCombo (dirWidget
->encodingCombo
);
69 kdeDocWidget
= new KDEDocSourceUI (this, "KDE documentation source page");
70 addPage (kdeDocWidget
, i18n("Source of New Dictionary (2)"));
72 mergeWidget
= new MergeWidget (this, "merge source page", dictionaryNames
, dictionaryFiles
, dictionaryLanguages
);
73 addPage (mergeWidget
, i18n("Source of New Dictionary (2)"));
75 connect (creationSource
->fileButton
, SIGNAL (toggled(bool)), this, SLOT(calculateAppropriate(bool)) );
76 connect (creationSource
->directoryButton
,SIGNAL(toggled(bool)), this, SLOT(calculateAppropriate(bool)) );
77 connect (creationSource
->kdeDocButton
, SIGNAL (toggled(bool)), this, SLOT(calculateAppropriate(bool)) );
78 connect (creationSource
->mergeButton
, SIGNAL (toggled(bool)), this, SLOT(calculateAppropriate(bool)) );
79 connect (creationSource
->emptyButton
, SIGNAL (toggled(bool)), this, SLOT(calculateAppropriate(bool)) );
81 calculateAppropriate (true);
84 DictionaryCreationWizard::~DictionaryCreationWizard () {
86 removePage (fileWidget
); delete fileWidget
;
87 removePage (dirWidget
); delete dirWidget
;
88 removePage (kdeDocWidget
); delete kdeDocWidget
;
89 removePage (mergeWidget
); delete mergeWidget
;
92 void DictionaryCreationWizard::buildCodecList () {
93 codecList
= new QPtrList
<QTextCodec
>;
96 for (i
= 0; (codec
= QTextCodec::codecForIndex(i
)); i
++)
97 codecList
->append (codec
);
100 void DictionaryCreationWizard::buildCodecCombo (QComboBox
*combo
) {
101 QString local
= i18n("Local")+" (";
102 local
+= QTextCodec::codecForLocale()->name();
104 combo
->insertItem (local
, 0);
105 combo
->insertItem (i18n("Latin1"), 1);
106 combo
->insertItem (i18n("Unicode"), 2);
107 for (uint i
= 0; i
< codecList
->count(); i
++ )
108 combo
->insertItem (codecList
->at(i
)->name(), i
+3);
111 void DictionaryCreationWizard::calculateAppropriate (bool) {
112 if (creationSource
->mergeButton
->isChecked()) {
113 setFinishEnabled (creationSource
, false);
114 removePage (fileWidget
);
115 removePage (dirWidget
);
116 removePage (kdeDocWidget
);
117 addPage (mergeWidget
, i18n("Source of New Dictionary (2)"));
118 setHelpEnabled (mergeWidget
, false);
119 setFinishEnabled (mergeWidget
, true);
121 else if (creationSource
->emptyButton
->isChecked()) {
122 removePage (fileWidget
);
123 removePage (dirWidget
);
124 removePage (kdeDocWidget
);
125 removePage (mergeWidget
);
126 setFinishEnabled (creationSource
, true);
128 else if (creationSource
->fileButton
->isChecked()) {
129 setFinishEnabled (creationSource
, false);
130 removePage (dirWidget
);
131 removePage (kdeDocWidget
);
132 removePage (mergeWidget
);
133 addPage (fileWidget
, i18n("Source of New Dictionary (2)"));
134 setHelpEnabled (fileWidget
, false);
135 setFinishEnabled (fileWidget
, true);
137 else if (creationSource
->directoryButton
->isChecked()) {
138 setFinishEnabled (creationSource
, false);
139 removePage (fileWidget
);
140 removePage (kdeDocWidget
);
141 removePage (mergeWidget
);
142 addPage (dirWidget
, i18n("Source of New Dictionary (2)"));
143 setHelpEnabled (dirWidget
, false);
144 setFinishEnabled (dirWidget
, true);
146 else { // creationSource->kdeDocButton must be checked
147 setFinishEnabled (creationSource
, false);
148 removePage (fileWidget
);
149 removePage (dirWidget
);
150 removePage (mergeWidget
);
151 addPage (kdeDocWidget
, i18n("Source of New Dictionary (2)"));
152 setHelpEnabled (kdeDocWidget
, false);
153 setFinishEnabled (kdeDocWidget
, true);
157 QString
DictionaryCreationWizard::createDictionary() {
158 WordList::WordMap map
;
159 QString dicFile
= "";
160 KProgressDialog
*pdlg
= WordList::progressDialog();
162 if (creationSource
->mergeButton
->isChecked()) {
163 map
= WordList::mergeFiles (mergeWidget
->mergeParameters(), pdlg
);
164 dicFile
= QString::null
;
166 else if (creationSource
->emptyButton
->isChecked()) {
167 dicFile
= QString::null
;
169 else if (creationSource
->fileButton
->isChecked()) {
170 QString filename
= fileWidget
->url
->url();
171 int encoding
= fileWidget
->encodingCombo
->currentItem();
172 if (fileWidget
->spellCheckBox
->isChecked())
173 dicFile
= fileWidget
->ooDictURL
->url();
176 map
= WordList::parseFile (filename
, QTextStream::Locale
, 0, pdlg
);
179 map
= WordList::parseFile (filename
, QTextStream::Latin1
, 0, pdlg
);
182 map
= WordList::parseFile (filename
, QTextStream::Unicode
, 0, pdlg
);
185 map
= WordList::parseFile (filename
, (QTextStream::Encoding
)0, codecList
->at(encoding
-3), pdlg
);
188 else if (creationSource
->directoryButton
->isChecked()) {
189 QString directory
= dirWidget
->url
->url();
190 int encoding
= dirWidget
->encodingCombo
->currentItem();
191 if (dirWidget
->spellCheckBox
->isChecked())
192 dicFile
= dirWidget
->ooDictURL
->url();
195 map
= WordList::parseDir (directory
, QTextStream::Locale
, 0, pdlg
);
198 map
= WordList::parseDir (directory
, QTextStream::Latin1
, 0, pdlg
);
201 map
= WordList::parseDir (directory
, QTextStream::Unicode
, 0, pdlg
);
204 map
= WordList::parseDir (directory
, (QTextStream::Encoding
)0, codecList
->at(encoding
-3), pdlg
);
207 else { // creationSource->kdeDocButton must be checked
208 QString language
= kdeDocWidget
->languageButton
->currentTag();
209 if (kdeDocWidget
->spellCheckBox
->isChecked())
210 dicFile
= kdeDocWidget
->ooDictURL
->url();
211 map
= WordList::parseKDEDoc (language
, pdlg
);
214 if (!dicFile
.isEmpty() && !dicFile
.isNull())
215 map
= WordList::spellCheck (map
, dicFile
, pdlg
);
221 QString dictionaryFile
;
224 filename
= QString("wordcompletion%1.dict").arg(dictnumber
);
225 dictionaryFile
= KApplication::kApplication()->dirs()->findResource("appdata", filename
);
227 while (KStandardDirs::exists(dictionaryFile
));
229 dictionaryFile
= KApplication::kApplication()->dirs()->saveLocation ("appdata", "/") + filename
;
230 if (WordList::saveWordList (map
, dictionaryFile
))
236 QString
DictionaryCreationWizard::name() {
237 if (creationSource
->mergeButton
->isChecked()) {
238 return i18n("Merge result");
240 else if (creationSource
->emptyButton
->isChecked()) {
241 return i18n("In the sense of a blank word list", "Empty list");
243 else if (creationSource
->fileButton
->isChecked()) {
244 return fileWidget
->url
->url();
246 else if (creationSource
->directoryButton
->isChecked()) {
247 return dirWidget
->url
->url();
249 else { // creationSource->kdeDocButton must be checked
250 return i18n("KDE Documentation");
254 QString
DictionaryCreationWizard::language() {
255 if (creationSource
->mergeButton
->isChecked()) {
256 return mergeWidget
->language();
258 else if (creationSource
->emptyButton
->isChecked()) {
259 if (KGlobal::locale())
260 return KGlobal::locale()->language();
262 return KLocale::defaultLanguage();
264 else if (creationSource
->fileButton
->isChecked()) {
265 return fileWidget
->languageButton
->currentTag();
267 else if (creationSource
->directoryButton
->isChecked()) {
268 return dirWidget
->languageButton
->currentTag();
270 else { // creationSource->kdeDocButton must be checked
271 return kdeDocWidget
->languageButton
->currentTag();
275 /***************************************************************************/
277 MergeWidget::MergeWidget(KWizard
*parent
, const char *name
,
278 QStringList dictionaryNames
, QStringList dictionaryFiles
,
279 QStringList dictionaryLanguages
)
280 : QScrollView (parent
, name
) {
281 dictionaries
.setAutoDelete (false);
282 weights
.setAutoDelete (false);
284 QWidget
*contents
= new QWidget(viewport());
286 QGridLayout
*layout
= new QGridLayout (contents
);
287 setResizePolicy (QScrollView::AutoOneFit
);
288 layout
->setColStretch (0, 0);
289 layout
->setColStretch (1, 1);
292 QStringList::Iterator nIt
= dictionaryNames
.begin();
293 QStringList::Iterator fIt
= dictionaryFiles
.begin();
294 QStringList::Iterator lIt
= dictionaryLanguages
.begin();
295 for (; nIt
!= dictionaryNames
.end(); ++nIt
, ++fIt
, ++lIt
) {
296 QCheckBox
*checkbox
= new QCheckBox(*nIt
, contents
);
297 KIntNumInput
*numInput
= new KIntNumInput(contents
);
298 layout
->addWidget (checkbox
, row
, 0);
299 layout
->addWidget (numInput
, row
, 1);
301 checkbox
->setChecked (true);
302 numInput
->setRange (1, 100, 10, true);
303 numInput
->setValue (100);
304 connect (checkbox
, SIGNAL (toggled(bool)), numInput
, SLOT(setEnabled(bool)));
306 dictionaries
.insert(*fIt
, checkbox
);
307 weights
.insert(*fIt
, numInput
);
308 languages
[*fIt
] = *lIt
;
313 MergeWidget::~MergeWidget() {
316 QMap
<QString
, int> MergeWidget::mergeParameters () {
317 QMap
<QString
, int> files
;
318 QDictIterator
<QCheckBox
> it(dictionaries
);
319 for (; it
.current(); ++it
) {
320 if (it
.current()->isChecked()) {
321 QString name
= it
.currentKey();
322 QString dictionaryFile
= KApplication::kApplication()->dirs()->findResource("appdata", name
);
323 files
[dictionaryFile
] = weights
[name
]->value();
330 QString
MergeWidget::language () {
331 QDictIterator
<QCheckBox
> it(dictionaries
);
332 for (; it
.current(); ++it
) {
333 if (it
.current()->isChecked()) {
334 return languages
[it
.currentKey()];
338 return QString::null
;
341 /***************************************************************************/
343 CompletionWizardWidget::CompletionWizardWidget (KWizard
*parent
, const char *name
)
344 : KDEDocSourceUI (parent
, name
) {
347 CompletionWizardWidget::~CompletionWizardWidget() {
350 void CompletionWizardWidget::ok (KConfig
*config
) {
351 WordList::WordMap map
;
352 KProgressDialog
*pdlg
= WordList::progressDialog();
354 QString language
= languageButton
->currentTag();
355 map
= WordList::parseKDEDoc (language
, pdlg
);
357 if (spellCheckBox
->isChecked())
358 map
= WordList::spellCheck (map
, ooDictURL
->url(), pdlg
);
364 QString dictionaryFile
;
366 dictionaryFile
= KApplication::kApplication()->dirs()->saveLocation ("appdata", "/") + "wordcompletion1.dict";
367 if (WordList::saveWordList (map
, dictionaryFile
)) {
368 config
->setGroup("Dictionary 0");
369 config
->writeEntry ("Filename", "wordcompletion1.dict");
370 config
->writeEntry ("Name", i18n("Default"));
371 config
->writeEntry ("Language", language
);
376 #include "dictionarycreationwizard.moc"