~ubuntu-branches/ubuntu/precise/kiten/precise

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
/*****************************************************************************
 * This file is part of Kiten, a KDE Japanese Reference Tool                 *
 * Copyright (C) 2011 Daniel E. Moctezuma <democtezuma@gmail.com>            *
 *                                                                           *
 * This library is free software; you can redistribute it and/or             *
 * modify it under the terms of the GNU Library General Public               *
 * License as published by the Free Software Foundation; either              *
 * version 2 of the License, or (at your option) any later version.          *
 *                                                                           *
 * This library 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         *
 * Library General Public License for more details.                          *
 *                                                                           *
 * You should have received a copy of the GNU Library General Public License *
 * along with this library; see the file COPYING.LIB.  If not, write to      *
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,      *
 * Boston, MA 02110-1301, USA.                                               *
 *****************************************************************************/

#include "kanjibrowserview.h"

#include "DictKanjidic/dictfilekanjidic.h"
#include "DictKanjidic/entrykanjidic.h"
#include "dictquery.h"
#include "entrylist.h"
#include "kanjibrowser.h"
#include "kanjibrowserconfig.h"

#include <KAction>
#include <KActionCollection>
#include <KConfigSkeleton>
#include <KDebug>
#include <KMessageBox>

KanjiBrowserView::KanjiBrowserView( QWidget *parent )
: QWidget( parent )
, _currentKanji( 0 )
{
  setupUi( this );
  loadSettings();
}

KanjiBrowserView::~KanjiBrowserView()
{

}

void KanjiBrowserView::changeGrade( const int grade )
{
  _currentGradeList.clear();

  // Indexex of items in the ComboBox:
  //   All Jouyou Kanji Grades: 0
  //   Grade 1: 1
  //   Grade 2: 2
  //   .
  //   .
  //   .
  //   Not in Jouyou list: ComboBox->count() - 1

  if( grade == AllJouyouGrades )
  {
    // Add the all the grades found in our list.
    foreach( const int grd, _gradeList )
    {
      _currentGradeList << grd;
    }
  }
  // Here the user selected "Not in Jouyou list".
  else if( grade == ( _grades->count() - 1 ) )
  {
    // Only show the kanji with grade 0 (not in Jouyou).
    _currentGradeList << 0;
  }
  // It seems KANJIDIC doesn't have a G7 (grade 7) kanji.
  // If the user selects G8 or above, we need to add 1 to the grade
  // because the index (from the ComboBox) and the grade will be different.
  else if( grade >= Grade7 )
  {
    _currentGradeList << ( grade + 1 );
  }
  // Show the kanji with the selected grade.
  else
  {
    _currentGradeList << grade;
  }

  // Reload our QListWidget widget.
  reloadKanjiList();
}

void KanjiBrowserView::changeStrokeCount( const int strokes )
{
  _currentStrokesList.clear();

  // Indexes of items in the ComboBox:
  //   No stroke limit: 0
  //   1 stroke: 1
  //   2 strokes: 2
  //   .
  //   .
  //   .

  // We don't need to filter any kanji by stroke number.
  if( strokes == NoStrokeLimit )
  {
    // Add all the strokes found to our the list.
    foreach( const int stroke, _strokesList )
    {
      _currentStrokesList << stroke;
    }
  }
  // Show the kanji with the selected number of strokes.
  else
  {
    _currentStrokesList << strokes;
  }

  // Reload our QListWidget widget.
  reloadKanjiList();
}

void KanjiBrowserView::changeToInfoPage()
{
  _stackedWidget->setCurrentIndex( Info );
}

void KanjiBrowserView::changeToListPage()
{
  _stackedWidget->setCurrentIndex( List );
}

QString KanjiBrowserView::convertToCSS( const QFont &font )
{
  QString weight;
  switch( font.weight() )
  {
    case QFont::Light:
      weight = "lighter";
      break;
    case QFont::Normal:
      weight = "normal";
      break;
    case QFont::Bold:
      weight = "bold";
      break;
  }

  QString style;
  switch( font.style() )
  {
    case QFont::StyleNormal:
      style = "normal";
      break;
    case QFont::StyleItalic:
      style = "italic";
      break;
    case QFont::StyleOblique:
      style = "oblique";
      break;
  }

  return QString( "font-family:\"%1\";"
                  "font-size:%2px;"
                  "font-weight:%3;"
                  "font-style:%4;" ).arg( font.family() )
                                    .arg( font.pointSizeF() )
                                    .arg( weight )
                                    .arg( style );
}

void KanjiBrowserView::loadSettings()
{
  _kanjiList->setFont( KanjiBrowserConfigSkeleton::self()->kanjiListFont() );
  _kanjiSize = KanjiBrowserConfigSkeleton::self()->kanjiSize();
  _kanaFont  = KanjiBrowserConfigSkeleton::self()->kanaFont();
  _labelFont = KanjiBrowserConfigSkeleton::self()->labelFont();

  // Reload the Kanji Information page with the new font changes.
  if( ! _currentKanji == 0 )
  {
    showKanjiInformation( _currentKanji );
  }
}

void KanjiBrowserView::reloadKanjiList()
{
  // Grade and strokes lists have the information of
  // which kanji we are going to filter.
  // We just iterate on them to actually do the filtering.
  QStringList list;
  foreach( const int strokes, _currentStrokesList )
  {
    foreach( const int grade, _currentGradeList )
    {
      list.append( _kanji.keys( qMakePair( grade, strokes ) ) );
    }
  }

  _kanjiList->clear();
  _kanjiList->addItems( list );

  // Update our status bar with the number of kanji filtered.
  statusBarChanged( i18np( "%1 kanji found", "%1 kanji found", _kanjiList->count() ) );
}

void KanjiBrowserView::searchKanji( QListWidgetItem *item )
{
  if(   _currentKanji != NULL
      && item->text() == _currentKanji->getWord() )
  {
    return;
  }

  _goToKanjiInfo->setText( i18n( "About %1", item->text() ) );

  Entry *result = _parent->_dictFileKanjidic->doSearch( DictQuery( item->text() ) )->first();
  EntryKanjidic *kanji = static_cast<EntryKanjidic*>( result );
  _currentKanji = kanji;

  showKanjiInformation( kanji );
}

void KanjiBrowserView::setupView(   KanjiBrowser *parent
                                  , const QHash< QString, QPair<int, int> > &kanji
                                  , QList<int> &kanjiGrades
                                  , QList<int> &strokeCount )
{
  if( kanji.isEmpty() || kanjiGrades.isEmpty() || strokeCount.isEmpty() )
  {
    kDebug() << "One or more of our lists are empty (kanji, grades, strokes)." << endl;
    kDebug() << "Could not load the view properly." << endl;
    KMessageBox::error( this, i18n( "Could not load the necessary kanji information." ) );
    return;
  }

  _parent = parent;
  _kanji = kanji;
  _gradeList = kanjiGrades;
  _strokesList = strokeCount;

  KAction *goToKanjiList = _parent->actionCollection()->addAction( "kanji_list" );
  goToKanjiList->setText( i18n( "Kanji &List" ) );

  _goToKanjiInfo = _parent->actionCollection()->addAction( "kanji_info" );
  _goToKanjiInfo->setText( i18n( "Kanji &Information" ) );

  _grades->addItem( i18n( "All Jouyou Kanji grades" ) );
  foreach( const int &grade, kanjiGrades )
  {
    // Grades 9 and above are considered Jinmeiyou.
    if( grade >= Jinmeiyou )
    {
      _grades->addItem( i18n( "Grade %1 (Jinmeiyou)", grade ) );
    }
    else
    {
      _grades->addItem( i18n( "Grade %1", grade ) );
    }
  }
  _grades->addItem( i18n( "Not in Jouyou list" ) );

  _strokes->addItem( i18n( "No stroke limit" ) );
  foreach( const int &stroke, strokeCount )
  {
    _strokes->addItem( i18np( "%1 stroke", "%1 strokes", stroke ) );
  }

  connect( _grades, SIGNAL( currentIndexChanged( int ) ),
              this,   SLOT( changeGrade( int ) ) );
  connect( _strokes, SIGNAL( currentIndexChanged( int ) ),
               this,   SLOT( changeStrokeCount( int ) ) );
  connect( _kanjiList, SIGNAL( itemClicked( QListWidgetItem* ) ),
                 this,   SLOT( searchKanji( QListWidgetItem* ) ) );
  connect( _kanjiList, SIGNAL( itemClicked( QListWidgetItem* ) ),
           _goToKanjiInfo, SIGNAL( triggered() ) );
  connect( goToKanjiList, SIGNAL( triggered() ),
                    this,   SLOT( changeToListPage() ) );
  connect( _goToKanjiInfo, SIGNAL( triggered() ),
                     this,   SLOT( changeToInfoPage() ) );

  // Set the current grade (Grade 1).
  _grades->setCurrentIndex( 1 );
  // Set the current number of strokes (No stroke limit).
  // NOTE: we change from '1 stroke' to 'No stroke limit'
  // to let the ComboBox notice the change and do the filter.
  _strokes->setCurrentIndex( 1 );
  _strokes->setCurrentIndex( NoStrokeLimit );

  kDebug() << "Initial setup succeeded!" << endl;
}

void KanjiBrowserView::showKanjiInformation( const EntryKanjidic *kanji )
{
  // This font is shipped with Kiten and should not be changed as it shows
  // the stroke order of a kanji.
  QFont kanjiFont( "KanjiStrokeOrders" );
  kanjiFont.setPointSizeF( _kanjiSize.toReal() );

  QString text;
  text.append( "<html><body><style>" );
  text.append( QString( ".kanji { %1 }" ).arg( convertToCSS( kanjiFont ) ) );
  text.append( QString( ".label { %1 }" ).arg( convertToCSS( _labelFont ) ) );
  text.append( QString( ".kana  { %1 }" ).arg( convertToCSS( _kanaFont ) ) );
  text.append( "</style>" );

  // Put the kanji.
  text.append( QString( "<table><tr><td><p class=\"kanji\">%1</p></td>" )
                        .arg( kanji->getWord() ) );

  // Now the kanji grades and number of strokes.
  text.append( "<td>" );
  if( ! kanji->getKanjiGrade().isEmpty() )
  {
    text.append( QString( "<p class=\"label\">%1 %2</p></br>" )
                          .arg( i18n( "Grade:" ) )
                          .arg( kanji->getKanjiGrade() ) );
  }
  text.append( QString( "<p class=\"label\">%1 %2</p></td></tr></table>" )
                        .arg( i18n( "Strokes:" ) )
                        .arg( kanji->getStrokesCount() ) );

  // Onyomi readings.
  if( ! kanji->getOnyomiReadingsList().isEmpty() )
  {
    text.append( QString( "<p class=\"label\">%1"
                          "<span class=\"kana\">%2</span></p></br>" )
                          .arg( i18n( "Onyomi: " ) )
                          .arg( kanji->getOnyomiReadings() ) );
  }

  // Kunyomi readings.
  if( ! kanji->getKunyomiReadingsList().isEmpty() )
  {
    text.append( QString( "<p class=\"label\">%1"
                          "<span class=\"kana\">%2</span></p></br>" )
                          .arg( i18n( "Kunyomi: " ) )
                          .arg( kanji->getKunyomiReadings() ) );
  }

  // Special readings used in names.
  if( ! kanji->getInNamesReadingsList().isEmpty() )
  {
    text.append( QString( "<p class=\"label\">%1"
                          "<span class=\"kana\">%2</span></p></br>" )
                          .arg( i18n( "In names: " ) )
                          .arg( kanji->getInNamesReadings() ) );
  }

  // Reading used as radical.
  if( ! kanji->getAsRadicalReadingsList().isEmpty() )
  {
    text.append( QString( "<p class=\"label\">%1"
                          "<span class=\"kana\">%2</span></p></br>" )
                          .arg( i18n( "As radical: " ) )
                          .arg( kanji->getAsRadicalReadings() ) );
  }

  // Meanings
  text.append( "<p class=\"label\">" );
  if( kanji->getMeaningsList().count() == 1 )
  {
    text.append( i18n( "Meaning: ") );
  }
  else
  {
    text.append( i18n( "Meanings: " ) );
  }
  text.append( QString( "<span class=\"kana\">%1</span></p>" )
                        .arg( kanji->getMeanings() ) );

  // Close remaining tags and set the HTML text.
  text.append( "</body></html>" );
  _kanjiInformation->setHtml( text );
}

#include "kanjibrowserview.moc"