1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
|
/**
This file is part of Kiten, a KDE Japanese Reference Tool...
Copyright (C) 2001 Jason Katz-Brown <jason@katzbrown.com>
(C) 2005 Paul Temple <paul.temple@gmx.net>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
USA
**/
#include <kapplication.h>
#include <kconfig.h>
#include <kdebug.h>
#include <kdialog.h>
#include <klistbox.h>
#include <klocale.h>
#include <kmessagebox.h>
#include <kpushbutton.h>
#include <kstandarddirs.h>
#include <qbuttongroup.h>
#include <qcheckbox.h>
#include <qfile.h>
#include <qlabel.h>
#include <qlayout.h>
#include <qspinbox.h>
#include <qtextcodec.h>
#include <qtooltip.h>
#include "kitenconfig.h"
#include "rad.h"
Rad::Rad() : QObject()
{
loaded = false;
}
void Rad::load()
{
if (loaded)
return;
KStandardDirs *dirs = KGlobal::dirs();
QString radkfile = dirs->findResource("data", "kiten/radkfile");
if (radkfile.isNull())
{
KMessageBox::error(0, i18n("Kanji radical information file not installed, so radical searching cannot be used."));
return;
}
QFile f(radkfile);
if (!f.open(IO_ReadOnly))
{
KMessageBox::error(0, i18n("Kanji radical information could not be loaded, so radical searching cannot be used."));
}
QTextStream t(&f);
t.setCodec(QTextCodec::codecForName("eucJP"));
Radical cur;
while (!t.eof())
{
QString s = t.readLine();
QChar first = s.at(0);
if (first == '#') // comment!
{
// nothing
}
else if (first == '$') // header
{
// save previous one
if( !cur.kanji().isNull() )
list.append(cur);
//first entry is trim(last 4 chars).. <rad><space><strokes>
unsigned int strokes = s.right(2).toUInt();
QString radical = QString(s.at(2));
cur = Radical(radical, strokes);
}
else // continuation
{
cur.addKanji(s);
}
}
// we gotta append the last one!!
// this nagged jasonkb for a bit wondering why fue wasn't showing up ;)
list.append(cur);
f.close();
loaded = true;
}
QStringList Rad::radByStrokes(unsigned int strokes)
{
load();
QStringList ret;
bool hadOne = false;
QValueListIterator<Radical> it = list.begin();
do
{
if ((*it).strokes() == strokes)
{
ret.append((*it).radical());
hadOne = true;
}
else if(hadOne) // shortcut because it's a sorted list
{
return ret;
}
}
while (++it != list.end());
return ret;
}
QStringList Rad::kanjiByRad(const QString &text)
{
//kdDebug() << "kanjiByRad, text is " << text << endl;
load();
QStringList ret;
QValueListIterator<Radical> it;
for (it = list.begin(); it != list.end() && (*it).radical() != text; ++it)
{
//kdDebug() << "kanjiByRad, looping, radical is " << (*it).radical() << endl;
}
QString kanji = (*it).kanji();
for (unsigned i = 0; i < kanji.length(); ++i)
{
//kdDebug() << "kanjiByRad, i is " << i << endl;
ret.append(QString(kanji.at(i)));
}
return ret;
}
QStringList Rad::kanjiByRad(const QStringList &list)
{
//kdDebug() << "kanjiByRad (list version)\n";
QStringList ret;
QValueList<QStringList> lists;
for (QStringList::ConstIterator it = list.begin(); it != list.end(); ++it)
{
//kdDebug() << "loading radical " << *it << endl;
lists.append(kanjiByRad(*it));
}
QStringList first = lists.first();
lists.pop_front();
for (QStringList::Iterator kit = first.begin(); kit != first.end(); ++kit)
{
//kdDebug() << "kit is " << *kit << endl;
QValueList<bool> outcomes;
for (QValueList<QStringList>::Iterator it = lists.begin(); it != lists.end(); ++it)
{
//kdDebug() << "looping through lists\n";
outcomes.append((*it).contains(*kit) > 0);
}
const bool containsBool = false;
if ((outcomes.contains(containsBool) < 1))
{
//kdDebug() << "appending " << *kit << endl;
ret.append(*kit);
}
else
{
//kdDebug() << "not appending " << *kit << endl;
}
}
return ret;
}
Radical Rad::radByKanji(const QString &text)
{
load();
QString ret;
QValueListIterator<Radical> it;
for (it = list.end(); it != list.begin() && (*it).kanji().find(text) == -1; --it);
return (*it);
}
unsigned int Rad::strokesByRad(const QString &text)
{
load();
QValueListIterator<Radical> it;
for(it = list.begin(); it != list.end() && (*it).radical() != text; ++it);
return (*it).strokes();
}
Rad::~Rad()
{
}
///////////////////////////////////////////////
RadWidget::RadWidget(Rad *_rad, QWidget *parent, const char *name) : QWidget(parent, name)
{
hotlistNum = 3;
rad = _rad;
QHBoxLayout *hlayout = new QHBoxLayout(this, KDialog::marginHint(), KDialog::spacingHint());
QVBoxLayout *vlayout = new QVBoxLayout(hlayout, KDialog::spacingHint());
hotlistGroup = new QButtonGroup(1, Horizontal, i18n("Hotlist"), this);
//hotlistGroup->setRadioButtonExclusive(true);
vlayout->addWidget(hotlistGroup);
Config* config = Config::self();
hotlist = config->hotlist();
while (hotlist.size() > hotlistNum)
hotlist.pop_front();
for (unsigned int i = 0; i < hotlistNum; ++i)
{
if (i >= hotlistNum)
break;
hotlistGroup->insert(new KPushButton(*hotlist.at(i), hotlistGroup), i);
}
connect(hotlistGroup, SIGNAL(clicked(int)), SLOT(hotlistClicked(int)));
QVBoxLayout *layout = new QVBoxLayout(vlayout, KDialog::spacingHint());
totalStrokes = new QCheckBox(i18n("Search by total strokes"), this);
connect(totalStrokes, SIGNAL(clicked()), this, SLOT(totalClicked()));
layout->addWidget(totalStrokes);
QHBoxLayout *strokesLayout = new QHBoxLayout(layout, KDialog::spacingHint());
totalSpin = new QSpinBox(1, 30, 1, this);
strokesLayout->addWidget(totalSpin);
strokesLayout->addStretch();
totalErrLabel = new QLabel(i18n("+/-"), this);
strokesLayout->addWidget(totalErrLabel);
totalErrSpin = new QSpinBox(0, 15, 1, this);
strokesLayout->addWidget(totalErrSpin);
ok = new KPushButton(i18n("&Look Up"), this);
ok->setEnabled(false);
connect(ok, SIGNAL(clicked()), SLOT(apply()));
layout->addWidget(ok);
cancel = new KPushButton( KStdGuiItem::cancel(), this );
connect(cancel, SIGNAL(clicked()), SLOT(close()));
layout->addWidget(cancel);
QVBoxLayout *middlevLayout = new QVBoxLayout(hlayout, KDialog::spacingHint());
strokesSpin = new QSpinBox(1, 17, 1, this);
QToolTip::add(strokesSpin, i18n("Show radicals having this number of strokes"));
middlevLayout->addWidget(strokesSpin);
List = new KListBox(this);
middlevLayout->addWidget(List);
connect(List, SIGNAL(executed(QListBoxItem *)), this, SLOT(executed(QListBoxItem *)));
connect(strokesSpin, SIGNAL(valueChanged(int)), this, SLOT(updateList(int)));
QVBoxLayout *rightvlayout = new QVBoxLayout(hlayout, KDialog::spacingHint());
selectedList = new KListBox(this);
rightvlayout->addWidget(selectedList);
connect(selectedList, SIGNAL(selectionChanged()), this, SLOT(selectionChanged()));
remove = new KPushButton(i18n("&Remove"), this);
rightvlayout->addWidget(remove);
connect(remove, SIGNAL(clicked()), this, SLOT(removeSelected()));
remove->setEnabled(false);
clear = new KPushButton(KStdGuiItem::clear(), this);
rightvlayout->addWidget(clear);
connect(clear, SIGNAL(clicked()), this, SLOT(clearSelected()));
clear->setEnabled(false);
setCaption(kapp->makeStdCaption(i18n("Radical Selector")));
strokesSpin->setValue(config->strokes());
strokesSpin->setFocus();
totalSpin->setValue(config->totalStrokes());
totalErrSpin->setValue(config->totalStrokesErrorMargin());
totalStrokes->setChecked(config->searchByTotal());
// make sure the right parts of the total stroke
// selection system are enabled
totalClicked();
// initially show the list of radicals to choose from
updateList(strokesSpin->value());
}
RadWidget::~RadWidget()
{
}
void RadWidget::hotlistClicked(int num)
{
addToSelected(*hotlist.at(num));
}
void RadWidget::executed(QListBoxItem *item)
{
addToSelected(item->text());
}
void RadWidget::clearSelected()
{
selectedList->clear();
selected.clear();
numChanged();
}
void RadWidget::removeSelected()
{
int currentItem = selectedList->currentItem();
if (currentItem != -1)
{
selectedList->removeItem(currentItem);
selected.remove(selected.at(currentItem));
numChanged();
selectionChanged();
}
}
void RadWidget::numChanged()
{
ok->setEnabled(selectedList->count() > 0);
clear->setEnabled(selectedList->count() > 0);
}
void RadWidget::addRadical(const QString &radical)
{
addToSelected(radical);
}
void RadWidget::addToSelected(const QString &text)
{
if (!text.isNull() && !selected.contains(text))
{
selectedList->insertItem(text);
selected.append(text);
numChanged();
selectionChanged();
}
}
void RadWidget::selectionChanged()
{
//kdDebug() << "selectionChanged()" << endl;
remove->setEnabled(selectedList->currentItem() != -1);
}
void RadWidget::updateList(int strokes)
{
List->clear();
List->insertStringList(rad->radByStrokes(static_cast<unsigned int>(strokes)));
}
void RadWidget::apply()
{
if (selected.count() < 1)
return;
emit set(selected, totalStrokes->isChecked() ? totalSpin->value() : 0, totalErrSpin->value());
Config* config = Config::self();
config->setStrokes(strokesSpin->value());
config->setTotalStrokes(totalSpin->value());
config->setTotalStrokesErrorMargin(totalErrSpin->value());
config->setSearchByTotal(totalStrokes->isChecked());
for (QStringList::Iterator it = selected.begin(); it != selected.end(); ++it)
{
if (hotlist.find(*it) == hotlist.end())
{
if (hotlist.size() >= hotlistNum)
hotlist.pop_front(); // stupid stl functions in Qt .. ;)
hotlist.append(*it);
config->setHotlist(hotlist);
}
}
config->writeConfig();
close();
}
void RadWidget::totalClicked()
{
bool enable = totalStrokes->isChecked();
totalSpin->setEnabled(enable);
totalErrSpin->setEnabled(enable);
totalErrLabel->setEnabled(enable);
}
//////////////////////////////////////////////
Radical::Radical(QString text, unsigned int strokes)
{
_Radical = text;
Strokes = strokes;
}
void Radical::addKanji(const QString &text)
{
Kanji.append(text);
}
#include "rad.moc"
|