1
/*****************************************************************************
2
* This file is part of Kiten, a KDE Japanese Reference Tool *
3
* Copyright (C) 2011 Daniel E. Moctezuma <democtezuma@gmail.com> *
5
* This library is free software; you can redistribute it and/or *
6
* modify it under the terms of the GNU Library General Public *
7
* License as published by the Free Software Foundation; either *
8
* version 2 of the License, or (at your option) any later version. *
10
* This library is distributed in the hope that it will be useful, *
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
13
* Library General Public License for more details. *
15
* You should have received a copy of the GNU Library General Public License *
16
* along with this library; see the file COPYING.LIB. If not, write to *
17
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, *
18
* Boston, MA 02110-1301, USA. *
19
*****************************************************************************/
21
#include "kanjibrowserview.h"
23
#include "DictKanjidic/dictfilekanjidic.h"
24
#include "DictKanjidic/entrykanjidic.h"
25
#include "dictquery.h"
26
#include "entrylist.h"
27
#include "kanjibrowser.h"
28
#include "kanjibrowserconfig.h"
31
#include <KActionCollection>
32
#include <KConfigSkeleton>
34
#include <KMessageBox>
36
KanjiBrowserView::KanjiBrowserView( QWidget *parent )
44
KanjiBrowserView::~KanjiBrowserView()
49
void KanjiBrowserView::changeGrade( const int grade )
51
_currentGradeList.clear();
53
// Indexex of items in the ComboBox:
54
// All Jouyou Kanji Grades: 0
60
// Not in Jouyou list: ComboBox->count() - 1
62
if( grade == AllJouyouGrades )
64
// Add the all the grades found in our list.
65
foreach( const int grd, _gradeList )
67
_currentGradeList << grd;
70
// Here the user selected "Not in Jouyou list".
71
else if( grade == ( _grades->count() - 1 ) )
73
// Only show the kanji with grade 0 (not in Jouyou).
74
_currentGradeList << 0;
76
// It seems KANJIDIC doesn't have a G7 (grade 7) kanji.
77
// If the user selects G8 or above, we need to add 1 to the grade
78
// because the index (from the ComboBox) and the grade will be different.
79
else if( grade >= Grade7 )
81
_currentGradeList << ( grade + 1 );
83
// Show the kanji with the selected grade.
86
_currentGradeList << grade;
89
// Reload our KListWidget widget.
93
void KanjiBrowserView::changeStrokeCount( const int strokes )
95
_currentStrokesList.clear();
97
// Indexes of items in the ComboBox:
105
// We don't need to filter any kanji by stroke number.
106
if( strokes == NoStrokeLimit )
108
// Add all the strokes found to our the list.
109
foreach( const int stroke, _strokesList )
111
_currentStrokesList << stroke;
114
// Show the kanji with the selected number of strokes.
117
_currentStrokesList << strokes;
120
// Reload our KListWidget widget.
124
void KanjiBrowserView::changeToInfoPage()
126
_stackedWidget->setCurrentIndex( Info );
129
void KanjiBrowserView::changeToListPage()
131
_stackedWidget->setCurrentIndex( List );
134
QString KanjiBrowserView::convertToCSS( const QFont &font )
137
switch( font.weight() )
151
switch( font.style() )
153
case QFont::StyleNormal:
156
case QFont::StyleItalic:
159
case QFont::StyleOblique:
164
return QString( "font-family:\"%1\";"
167
"font-style:%4;" ).arg( font.family() )
168
.arg( font.pointSizeF() )
173
void KanjiBrowserView::loadSettings()
175
_kanjiList->setFont( KanjiBrowserConfigSkeleton::self()->kanjiListFont() );
176
_kanjiSize = KanjiBrowserConfigSkeleton::self()->kanjiSize();
177
_kanaFont = KanjiBrowserConfigSkeleton::self()->kanaFont();
178
_labelFont = KanjiBrowserConfigSkeleton::self()->labelFont();
180
// Reload the Kanji Information page with the new font changes.
181
if( ! _currentKanji == 0 )
183
showKanjiInformation( _currentKanji );
187
void KanjiBrowserView::reloadKanjiList()
189
// Grade and strokes lists have the information of
190
// which kanji we are going to filter.
191
// We just iterate on them to actually do the filtering.
193
foreach( const int strokes, _currentStrokesList )
195
foreach( const int grade, _currentGradeList )
197
list.append( _kanji.keys( qMakePair( grade, strokes ) ) );
202
_kanjiList->addItems( list );
204
// Update our status bar with the number of kanji filtered.
205
statusBarChanged( i18n( "%1 kanji found", _kanjiList->count() ) );
208
void KanjiBrowserView::searchKanji( QListWidgetItem *item )
210
if( _currentKanji != NULL
211
&& item->text() == _currentKanji->getWord() )
216
_goToKanjiInfo->setText( i18n( "About %1", item->text() ) );
218
Entry *result = _parent->_dictFileKanjidic->doSearch( DictQuery( item->text() ) )->first();
219
EntryKanjidic *kanji = static_cast<EntryKanjidic*>( result );
220
_currentKanji = kanji;
222
showKanjiInformation( kanji );
225
void KanjiBrowserView::setupView( KanjiBrowser *parent
226
, const QHash< QString, QPair<int, int> > &kanji
227
, QList<int> &kanjiGrades
228
, QList<int> &strokeCount )
230
if( kanji.isEmpty() || kanjiGrades.isEmpty() || strokeCount.isEmpty() )
232
kDebug() << "One or more of our lists are empty (kanji, grades, strokes)." << endl;
233
kDebug() << "Could not load the view properly." << endl;
234
KMessageBox::error( this, i18n( "Could not load the necessary kanji information." ) );
240
_gradeList = kanjiGrades;
241
_strokesList = strokeCount;
243
KAction *goToKanjiList = _parent->actionCollection()->addAction( "kanji_list" );
244
goToKanjiList->setText( i18n( "Kanji &List" ) );
246
_goToKanjiInfo = _parent->actionCollection()->addAction( "kanji_info" );
247
_goToKanjiInfo->setText( i18n( "Kanji &Information" ) );
249
_grades->addItem( i18n( "All Jouyou Kanji grades" ) );
250
foreach( const int &grade, kanjiGrades )
252
// Grades 9 and above are considered Jinmeiyou.
253
if( grade >= Jinmeiyou )
255
_grades->addItem( i18n( "Grade %1 (Jinmeiyou)", grade ) );
259
_grades->addItem( i18n( "Grade %1", grade ) );
262
_grades->addItem( i18n( "Not in Jouyou list" ) );
264
_strokes->addItem( i18n( "No stroke limit" ) );
265
foreach( const int &stroke, strokeCount )
267
_strokes->addItem( i18np( "%1 stroke", "%1 strokes", stroke ) );
270
connect( _grades, SIGNAL( currentIndexChanged( int ) ),
271
this, SLOT( changeGrade( int ) ) );
272
connect( _strokes, SIGNAL( currentIndexChanged( int ) ),
273
this, SLOT( changeStrokeCount( int ) ) );
274
connect( _kanjiList, SIGNAL( executed( QListWidgetItem* ) ),
275
this, SLOT( searchKanji( QListWidgetItem* ) ) );
276
connect( _kanjiList, SIGNAL( executed( QListWidgetItem* ) ),
277
_goToKanjiInfo, SIGNAL( triggered() ) );
278
connect( goToKanjiList, SIGNAL( triggered() ),
279
this, SLOT( changeToListPage() ) );
280
connect( _goToKanjiInfo, SIGNAL( triggered() ),
281
this, SLOT( changeToInfoPage() ) );
283
// Set the current grade (Grade 1).
284
_grades->setCurrentIndex( 1 );
285
// Set the current number of strokes (No stroke limit).
286
// NOTE: we change from '1 stroke' to 'No stroke limit'
287
// to let the ComboBox notice the change and do the filter.
288
_strokes->setCurrentIndex( 1 );
289
_strokes->setCurrentIndex( NoStrokeLimit );
291
kDebug() << "Initial setup succeeded!" << endl;
294
void KanjiBrowserView::showKanjiInformation( const EntryKanjidic *kanji )
296
// This font is shipped with Kiten and should not be changed as it shows
297
// the stroke order of a kanji.
298
QFont kanjiFont( "KanjiStrokeOrders" );
299
kanjiFont.setPointSizeF( _kanjiSize.toReal() );
302
text.append( "<html><body><style>" );
303
text.append( QString( ".kanji { %1 }" ).arg( convertToCSS( kanjiFont ) ) );
304
text.append( QString( ".label { %1 }" ).arg( convertToCSS( _labelFont ) ) );
305
text.append( QString( ".kana { %1 }" ).arg( convertToCSS( _kanaFont ) ) );
306
text.append( "</style>" );
309
text.append( QString( "<table><tr><td><p class=\"kanji\">%1</p></td>" )
310
.arg( kanji->getWord() ) );
312
// Now the kanji grades and number of strokes.
313
text.append( "<td>" );
314
if( ! kanji->getKanjiGrade().isEmpty() )
316
text.append( QString( "<p class=\"label\">%1 %2</p></br>" )
317
.arg( i18n( "Grade:" ) )
318
.arg( kanji->getKanjiGrade() ) );
320
text.append( QString( "<p class=\"label\">%1 %2</p></td></tr></table>" )
321
.arg( i18n( "Strokes:" ) )
322
.arg( kanji->getStrokesCount() ) );
325
if( ! kanji->getOnyomiReadingsList().isEmpty() )
327
text.append( QString( "<p class=\"label\">%1"
328
"<span class=\"kana\">%2</span></p></br>" )
329
.arg( i18n( "Onyomi: " ) )
330
.arg( kanji->getOnyomiReadings() ) );
334
if( ! kanji->getKunyomiReadingsList().isEmpty() )
336
text.append( QString( "<p class=\"label\">%1"
337
"<span class=\"kana\">%2</span></p></br>" )
338
.arg( i18n( "Kunyomi: " ) )
339
.arg( kanji->getKunyomiReadings() ) );
342
// Special readings used in names.
343
if( ! kanji->getInNamesReadingsList().isEmpty() )
345
text.append( QString( "<p class=\"label\">%1"
346
"<span class=\"kana\">%2</span></p></br>" )
347
.arg( i18n( "In names: " ) )
348
.arg( kanji->getInNamesReadings() ) );
351
// Reading used as radical.
352
if( ! kanji->getAsRadicalReadingsList().isEmpty() )
354
text.append( QString( "<p class=\"label\">%1"
355
"<span class=\"kana\">%2</span></p></br>" )
356
.arg( i18n( "As radical: " ) )
357
.arg( kanji->getAsRadicalReadings() ) );
361
text.append( "<p class=\"label\">" );
362
if( kanji->getMeaningsList().count() == 1 )
364
text.append( i18n( "Meaning: ") );
368
text.append( i18n( "Meanings: " ) );
370
text.append( QString( "<span class=\"kana\">%1</span></p>" )
371
.arg( kanji->getMeanings() ) );
373
// Close remaining tags and set the HTML text.
374
text.append( "</body></html>" );
375
_kanjiInformation->setHtml( text );
378
#include "kanjibrowserview.moc"