Warning, file /education/kiten/kanjibrowser/kanjibrowser.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).
0001 /* 0002 This file is part of Kiten, a KDE Japanese Reference Tool 0003 SPDX-FileCopyrightText: 2011 Daniel E. Moctezuma <democtezuma@gmail.com> 0004 0005 SPDX-License-Identifier: LGPL-2.0-or-later 0006 */ 0007 0008 #include "kanjibrowser.h" 0009 0010 #include "ui_preferences.h" 0011 0012 #include "DictKanjidic/dictfilekanjidic.h" 0013 #include "kanjibrowserconfig.h" 0014 #include "kanjibrowserview.h" 0015 #include "kitenmacros.h" 0016 0017 #include <QStatusBar> 0018 0019 #include <KActionCollection> 0020 #include <KConfigDialog> 0021 #include <KLocalizedString> 0022 #include <KStandardAction> 0023 0024 KanjiBrowser::KanjiBrowser() 0025 : KXmlGuiWindow() 0026 , _dictFileKanjidic(0) 0027 { 0028 // Read the configuration file. 0029 _config = KanjiBrowserConfigSkeleton::self(); 0030 _config->load(); 0031 0032 statusBar()->show(); 0033 0034 // Add some actions. 0035 KStandardAction::quit(this, SLOT(close()), actionCollection()); 0036 KStandardAction::preferences(this, SLOT(showPreferences()), actionCollection()); 0037 0038 _view = new KanjiBrowserView(this->parentWidget()); 0039 connect(_view, &KanjiBrowserView::statusBarChanged, this, &KanjiBrowser::changeStatusBar); 0040 // Load the necessary information and setup the view. 0041 loadKanji(); 0042 0043 setCentralWidget(_view); 0044 setObjectName(QStringLiteral("kanjibrowser")); 0045 0046 setupGUI(Default, QStringLiteral("kanjibrowserui.rc")); 0047 } 0048 0049 KanjiBrowser::~KanjiBrowser() 0050 { 0051 if (_dictFileKanjidic) { 0052 delete _dictFileKanjidic; 0053 } 0054 } 0055 0056 void KanjiBrowser::changeStatusBar(const QString &text) 0057 { 0058 statusBar()->showMessage(text); 0059 } 0060 0061 void KanjiBrowser::loadKanji() 0062 { 0063 if (_dictFileKanjidic) { 0064 return; 0065 } 0066 0067 qDebug() << "Loading kanji..."; 0068 0069 QString dictionary = QStandardPaths::locate(QStandardPaths::GenericDataLocation, QStringLiteral("kiten/kanjidic")); 0070 _dictFileKanjidic = new DictFileKanjidic(); 0071 _dictFileKanjidic->loadSettings(); 0072 _dictFileKanjidic->loadDictionary(dictionary, KANJIDIC); 0073 0074 // Parse the contents of KANJIDIC that we need to create the view. 0075 // We need: 0076 // - Kanji 0077 // - Grade 0078 // - Number of strokes 0079 QRegExp gradeMatch("^G\\d+"); 0080 QRegExp strokeMatch("^S\\d+"); 0081 QList<int> gradeList; 0082 QList<int> strokeList; 0083 QHash<QString, QPair<int, int>> kanjiList; 0084 foreach (const QString &line, _dictFileKanjidic->dumpDictionary()) { 0085 // All the kanji without grade will have Grade 0, making easy to 0086 // manage that information in KanjiBrowserView. 0087 int grade = 0; 0088 int strokes = 0; 0089 QStringList gradeSection = line.split(' ', Qt::SkipEmptyParts).filter(gradeMatch); 0090 QStringList strokesSection = line.split(' ', Qt::SkipEmptyParts).filter(strokeMatch); 0091 0092 // There are some kanji without grade (example: those not in Jouyou list). 0093 if (!gradeSection.isEmpty()) { 0094 // In KANJIDIC the grade/stroke section is: G# (for grade) 0095 // S# (for strokes) 0096 // where # can be one or more digits. 0097 // We remove the first character and convert the remaining ones to int. 0098 grade = gradeSection.first().remove(0, 1).toInt(); 0099 gradeList << grade; 0100 } 0101 0102 strokes = strokesSection.first().remove(0, 1).toInt(); 0103 strokeList << strokes; 0104 0105 // Insert the extracted information into our QHash. 0106 kanjiList.insert(line[0], qMakePair(grade, strokes)); 0107 } 0108 0109 // This conversion from QList to QSet to QList, is to remove duplicated items. 0110 std::sort(gradeList.begin(), gradeList.end()); 0111 gradeList.erase(std::unique(gradeList.begin(), gradeList.end()), gradeList.end()); 0112 0113 std::sort(strokeList.begin(), strokeList.end()); 0114 strokeList.erase(std::unique(strokeList.begin(), strokeList.end()), strokeList.end()); 0115 qDebug() << "Max. grade:" << gradeList.last(); 0116 qDebug() << "Max. stroke count:" << strokeList.last(); 0117 0118 // Finally setup the view. 0119 _view->setupView(this, kanjiList, gradeList, strokeList); 0120 } 0121 0122 void KanjiBrowser::showPreferences() 0123 { 0124 if (KConfigDialog::showDialog(QStringLiteral("settings"))) { 0125 return; 0126 } 0127 0128 QWidget *preferences = new QWidget(); 0129 Ui::preferences layout; 0130 layout.setupUi(preferences); 0131 0132 KConfigDialog *dialog = new KConfigDialog(this, QStringLiteral("settings"), KanjiBrowserConfigSkeleton::self()); 0133 dialog->addPage(preferences, i18n("Settings"), QStringLiteral("help-contents")); 0134 connect(dialog, &KConfigDialog::settingsChanged, _view, &KanjiBrowserView::loadSettings); 0135 dialog->show(); 0136 }