~ubuntu-branches/ubuntu/quantal/kiten/quantal-proposed

« back to all changes in this revision

Viewing changes to kanjibrowser/kanjibrowserview.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2011-12-16 13:14:44 UTC
  • mfrom: (1.1.4)
  • Revision ID: package-import@ubuntu.com-20111216131444-fxt8pt2pha54qmdu
Tags: 4:4.7.90-0ubuntu1
New upstream beta release

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*****************************************************************************
 
2
 * This file is part of Kiten, a KDE Japanese Reference Tool                 *
 
3
 * Copyright (C) 2011 Daniel E. Moctezuma <democtezuma@gmail.com>            *
 
4
 *                                                                           *
 
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.          *
 
9
 *                                                                           *
 
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.                          *
 
14
 *                                                                           *
 
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
 *****************************************************************************/
 
20
 
 
21
#include "kanjibrowserview.h"
 
22
 
 
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"
 
29
 
 
30
#include <KAction>
 
31
#include <KActionCollection>
 
32
#include <KConfigSkeleton>
 
33
#include <KDebug>
 
34
#include <KMessageBox>
 
35
 
 
36
KanjiBrowserView::KanjiBrowserView( QWidget *parent )
 
37
: QWidget( parent )
 
38
, _currentKanji( 0 )
 
39
{
 
40
  setupUi( this );
 
41
  loadSettings();
 
42
}
 
43
 
 
44
KanjiBrowserView::~KanjiBrowserView()
 
45
{
 
46
 
 
47
}
 
48
 
 
49
void KanjiBrowserView::changeGrade( const int grade )
 
50
{
 
51
  _currentGradeList.clear();
 
52
 
 
53
  // Indexex of items in the ComboBox:
 
54
  //   All Jouyou Kanji Grades: 0
 
55
  //   Grade 1: 1
 
56
  //   Grade 2: 2
 
57
  //   .
 
58
  //   .
 
59
  //   .
 
60
  //   Not in Jouyou list: ComboBox->count() - 1
 
61
 
 
62
  if( grade == AllJouyouGrades )
 
63
  {
 
64
    // Add the all the grades found in our list.
 
65
    foreach( const int grd, _gradeList )
 
66
    {
 
67
      _currentGradeList << grd;
 
68
    }
 
69
  }
 
70
  // Here the user selected "Not in Jouyou list".
 
71
  else if( grade == ( _grades->count() - 1 ) )
 
72
  {
 
73
    // Only show the kanji with grade 0 (not in Jouyou).
 
74
    _currentGradeList << 0;
 
75
  }
 
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 )
 
80
  {
 
81
    _currentGradeList << ( grade + 1 );
 
82
  }
 
83
  // Show the kanji with the selected grade.
 
84
  else
 
85
  {
 
86
    _currentGradeList << grade;
 
87
  }
 
88
 
 
89
  // Reload our KListWidget widget.
 
90
  reloadKanjiList();
 
91
}
 
92
 
 
93
void KanjiBrowserView::changeStrokeCount( const int strokes )
 
94
{
 
95
  _currentStrokesList.clear();
 
96
 
 
97
  // Indexes of items in the ComboBox:
 
98
  //   No stroke limit: 0
 
99
  //   1 stroke: 1
 
100
  //   2 strokes: 2
 
101
  //   .
 
102
  //   .
 
103
  //   .
 
104
 
 
105
  // We don't need to filter any kanji by stroke number.
 
106
  if( strokes == NoStrokeLimit )
 
107
  {
 
108
    // Add all the strokes found to our the list.
 
109
    foreach( const int stroke, _strokesList )
 
110
    {
 
111
      _currentStrokesList << stroke;
 
112
    }
 
113
  }
 
114
  // Show the kanji with the selected number of strokes.
 
115
  else
 
116
  {
 
117
    _currentStrokesList << strokes;
 
118
  }
 
119
 
 
120
  // Reload our KListWidget widget.
 
121
  reloadKanjiList();
 
122
}
 
123
 
 
124
void KanjiBrowserView::changeToInfoPage()
 
125
{
 
126
  _stackedWidget->setCurrentIndex( Info );
 
127
}
 
128
 
 
129
void KanjiBrowserView::changeToListPage()
 
130
{
 
131
  _stackedWidget->setCurrentIndex( List );
 
132
}
 
133
 
 
134
QString KanjiBrowserView::convertToCSS( const QFont &font )
 
135
{
 
136
  QString weight;
 
137
  switch( font.weight() )
 
138
  {
 
139
    case QFont::Light:
 
140
      weight = "lighter";
 
141
      break;
 
142
    case QFont::Normal:
 
143
      weight = "normal";
 
144
      break;
 
145
    case QFont::Bold:
 
146
      weight = "bold";
 
147
      break;
 
148
  }
 
149
 
 
150
  QString style;
 
151
  switch( font.style() )
 
152
  {
 
153
    case QFont::StyleNormal:
 
154
      style = "normal";
 
155
      break;
 
156
    case QFont::StyleItalic:
 
157
      style = "italic";
 
158
      break;
 
159
    case QFont::StyleOblique:
 
160
      style = "oblique";
 
161
      break;
 
162
  }
 
163
 
 
164
  return QString( "font-family:\"%1\";"
 
165
                  "font-size:%2px;"
 
166
                  "font-weight:%3;"
 
167
                  "font-style:%4;" ).arg( font.family() )
 
168
                                    .arg( font.pointSizeF() )
 
169
                                    .arg( weight )
 
170
                                    .arg( style );
 
171
}
 
172
 
 
173
void KanjiBrowserView::loadSettings()
 
174
{
 
175
  _kanjiList->setFont( KanjiBrowserConfigSkeleton::self()->kanjiListFont() );
 
176
  _kanjiSize = KanjiBrowserConfigSkeleton::self()->kanjiSize();
 
177
  _kanaFont  = KanjiBrowserConfigSkeleton::self()->kanaFont();
 
178
  _labelFont = KanjiBrowserConfigSkeleton::self()->labelFont();
 
179
 
 
180
  // Reload the Kanji Information page with the new font changes.
 
181
  if( ! _currentKanji == 0 )
 
182
  {
 
183
    showKanjiInformation( _currentKanji );
 
184
  }
 
185
}
 
186
 
 
187
void KanjiBrowserView::reloadKanjiList()
 
188
{
 
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.
 
192
  QStringList list;
 
193
  foreach( const int strokes, _currentStrokesList )
 
194
  {
 
195
    foreach( const int grade, _currentGradeList )
 
196
    {
 
197
      list.append( _kanji.keys( qMakePair( grade, strokes ) ) );
 
198
    }
 
199
  }
 
200
 
 
201
  _kanjiList->clear();
 
202
  _kanjiList->addItems( list );
 
203
 
 
204
  // Update our status bar with the number of kanji filtered.
 
205
  statusBarChanged( i18n( "%1 kanji found", _kanjiList->count() ) );
 
206
}
 
207
 
 
208
void KanjiBrowserView::searchKanji( QListWidgetItem *item )
 
209
{
 
210
  if(   _currentKanji != NULL
 
211
      && item->text() == _currentKanji->getWord() )
 
212
  {
 
213
    return;
 
214
  }
 
215
 
 
216
  _goToKanjiInfo->setText( i18n( "About %1", item->text() ) );
 
217
 
 
218
  Entry *result = _parent->_dictFileKanjidic->doSearch( DictQuery( item->text() ) )->first();
 
219
  EntryKanjidic *kanji = static_cast<EntryKanjidic*>( result );
 
220
  _currentKanji = kanji;
 
221
 
 
222
  showKanjiInformation( kanji );
 
223
}
 
224
 
 
225
void KanjiBrowserView::setupView(   KanjiBrowser *parent
 
226
                                  , const QHash< QString, QPair<int, int> > &kanji
 
227
                                  , QList<int> &kanjiGrades
 
228
                                  , QList<int> &strokeCount )
 
229
{
 
230
  if( kanji.isEmpty() || kanjiGrades.isEmpty() || strokeCount.isEmpty() )
 
231
  {
 
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." ) );
 
235
    return;
 
236
  }
 
237
 
 
238
  _parent = parent;
 
239
  _kanji = kanji;
 
240
  _gradeList = kanjiGrades;
 
241
  _strokesList = strokeCount;
 
242
 
 
243
  KAction *goToKanjiList = _parent->actionCollection()->addAction( "kanji_list" );
 
244
  goToKanjiList->setText( i18n( "Kanji &List" ) );
 
245
 
 
246
  _goToKanjiInfo = _parent->actionCollection()->addAction( "kanji_info" );
 
247
  _goToKanjiInfo->setText( i18n( "Kanji &Information" ) );
 
248
 
 
249
  _grades->addItem( i18n( "All Jouyou Kanji grades" ) );
 
250
  foreach( const int &grade, kanjiGrades )
 
251
  {
 
252
    // Grades 9 and above are considered Jinmeiyou.
 
253
    if( grade >= Jinmeiyou )
 
254
    {
 
255
      _grades->addItem( i18n( "Grade %1 (Jinmeiyou)", grade ) );
 
256
    }
 
257
    else
 
258
    {
 
259
      _grades->addItem( i18n( "Grade %1", grade ) );
 
260
    }
 
261
  }
 
262
  _grades->addItem( i18n( "Not in Jouyou list" ) );
 
263
 
 
264
  _strokes->addItem( i18n( "No stroke limit" ) );
 
265
  foreach( const int &stroke, strokeCount )
 
266
  {
 
267
    _strokes->addItem( i18np( "%1 stroke", "%1 strokes", stroke ) );
 
268
  }
 
269
 
 
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() ) );
 
282
 
 
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 );
 
290
 
 
291
  kDebug() << "Initial setup succeeded!" << endl;
 
292
}
 
293
 
 
294
void KanjiBrowserView::showKanjiInformation( const EntryKanjidic *kanji )
 
295
{
 
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() );
 
300
 
 
301
  QString text;
 
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>" );
 
307
 
 
308
  // Put the kanji.
 
309
  text.append( QString( "<table><tr><td><p class=\"kanji\">%1</p></td>" )
 
310
                        .arg( kanji->getWord() ) );
 
311
 
 
312
  // Now the kanji grades and number of strokes.
 
313
  text.append( "<td>" );
 
314
  if( ! kanji->getKanjiGrade().isEmpty() )
 
315
  {
 
316
    text.append( QString( "<p class=\"label\">%1 %2</p></br>" )
 
317
                          .arg( i18n( "Grade:" ) )
 
318
                          .arg( kanji->getKanjiGrade() ) );
 
319
  }
 
320
  text.append( QString( "<p class=\"label\">%1 %2</p></td></tr></table>" )
 
321
                        .arg( i18n( "Strokes:" ) )
 
322
                        .arg( kanji->getStrokesCount() ) );
 
323
 
 
324
  // Onyomi readings.
 
325
  if( ! kanji->getOnyomiReadingsList().isEmpty() )
 
326
  {
 
327
    text.append( QString( "<p class=\"label\">%1"
 
328
                          "<span class=\"kana\">%2</span></p></br>" )
 
329
                          .arg( i18n( "Onyomi: " ) )
 
330
                          .arg( kanji->getOnyomiReadings() ) );
 
331
  }
 
332
 
 
333
  // Kunyomi readings.
 
334
  if( ! kanji->getKunyomiReadingsList().isEmpty() )
 
335
  {
 
336
    text.append( QString( "<p class=\"label\">%1"
 
337
                          "<span class=\"kana\">%2</span></p></br>" )
 
338
                          .arg( i18n( "Kunyomi: " ) )
 
339
                          .arg( kanji->getKunyomiReadings() ) );
 
340
  }
 
341
 
 
342
  // Special readings used in names.
 
343
  if( ! kanji->getInNamesReadingsList().isEmpty() )
 
344
  {
 
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() ) );
 
349
  }
 
350
 
 
351
  // Reading used as radical.
 
352
  if( ! kanji->getAsRadicalReadingsList().isEmpty() )
 
353
  {
 
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() ) );
 
358
  }
 
359
 
 
360
  // Meanings
 
361
  text.append( "<p class=\"label\">" );
 
362
  if( kanji->getMeaningsList().count() == 1 )
 
363
  {
 
364
    text.append( i18n( "Meaning: ") );
 
365
  }
 
366
  else
 
367
  {
 
368
    text.append( i18n( "Meanings: " ) );
 
369
  }
 
370
  text.append( QString( "<span class=\"kana\">%1</span></p>" )
 
371
                        .arg( kanji->getMeanings() ) );
 
372
 
 
373
  // Close remaining tags and set the HTML text.
 
374
  text.append( "</body></html>" );
 
375
  _kanjiInformation->setHtml( text );
 
376
}
 
377
 
 
378
#include "kanjibrowserview.moc"