Warning, file /education/kiten/lib/kromajiedit.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: 2001 Jason Katz-Brown <jason@katzbrown.com>
0004     SPDX-FileCopyrightText: 2006 Joseph Kerian <jkerian@gmail.com>
0005 
0006     SPDX-License-Identifier: LGPL-2.0-or-later
0007 */
0008 
0009 #include "kromajiedit.h"
0010 
0011 #include <KLocalizedString>
0012 #include <KMessageBox>
0013 
0014 #include <QAction>
0015 #include <QActionGroup>
0016 #include <QApplication>
0017 #include <QFile>
0018 #include <QKeyEvent>
0019 #include <QMenu>
0020 #include <QStandardPaths>
0021 #include <QTextCodec>
0022 #include <QTextStream>
0023 
0024 KRomajiEdit::KRomajiEdit(QWidget *parent)
0025     : KLineEdit(parent) // KDE4 CHANGE
0026 {
0027     m_kana = "unset";
0028 
0029     QString romkana = QStandardPaths::locate(QStandardPaths::GenericDataLocation, QStringLiteral("kiten/romkana.cnv"));
0030     if (romkana.isNull()) {
0031         KMessageBox::error(nullptr, i18n("Romaji information file not installed, so Romaji conversion cannot be used."));
0032         return;
0033     }
0034 
0035     QFile f(romkana);
0036 
0037     if (!f.open(QIODevice::ReadOnly)) {
0038         KMessageBox::error(nullptr, i18n("Romaji information could not be loaded, so Romaji conversion cannot be used."));
0039     }
0040 
0041     QTextStream t(&f);
0042     t.setCodec(QTextCodec::codecForName("eucJP"));
0043     while (!t.atEnd()) // KDE4 CHANGE
0044     {
0045         QString s = t.readLine();
0046 
0047         QChar first = s.at(0);
0048         if (first == QLatin1Char('#')) // comment!
0049         {
0050             // nothing
0051         } else if (first == QLatin1Char('$')) // header
0052         {
0053             if (m_kana == "unset") {
0054                 m_kana = "hiragana";
0055             } else {
0056                 m_kana = "katakana";
0057             }
0058         } else // body
0059         {
0060             QStringList things(s.split(QLatin1Char(' ')));
0061             QString thekana(things.first());
0062             QString romaji(/* KDE4 CHANGE: * */ things.at(1));
0063 
0064             if (m_kana == "hiragana") {
0065                 m_hiragana[romaji] = thekana;
0066             } else if (m_kana == "katakana") {
0067                 m_katakana[romaji] = thekana;
0068             }
0069         }
0070     }
0071     f.close();
0072 
0073     m_kana = "english";
0074 }
0075 
0076 KRomajiEdit::~KRomajiEdit()
0077 {
0078 }
0079 
0080 QMenu *KRomajiEdit::createPopupMenu()
0081 {
0082     QMenu *menu;
0083     menu = new QMenu();
0084     // TODO: Get the basic editing options in here from a KLineEdit popup menu (or elsewhere?)
0085     menu->addSeparator();
0086 
0087     // Put our action group together
0088     QActionGroup *group = new QActionGroup(menu);
0089 
0090     QAction *temp;
0091     temp = new QAction(i18nc("@option:radio selects english translation", "English"), group);
0092     temp->setCheckable(true);
0093     menu->addAction(temp);
0094     if (m_kana == "english") {
0095         temp->setChecked(true);
0096     } else {
0097         temp->setChecked(false);
0098     }
0099 
0100     temp = new QAction(i18nc("@option:radio selects japanese translation", "Kana"), group);
0101     temp->setCheckable(true);
0102     menu->addAction(temp);
0103     if (m_kana == "kana") {
0104         temp->setChecked(true);
0105     } else {
0106         temp->setChecked(false);
0107     }
0108 
0109     connect(group, &QActionGroup::triggered, this, &KRomajiEdit::setKana);
0110 
0111     Q_EMIT aboutToShowContextMenu(menu);
0112     return menu;
0113 }
0114 
0115 // TODO allow editing not only at end
0116 void KRomajiEdit::keyPressEvent(QKeyEvent *e)
0117 {
0118     bool shift = qApp->keyboardModifiers() & Qt::ShiftModifier; // KDE4 CHANGE
0119     QString ji = e->text();
0120 
0121     if (shift && e->key() == Qt::Key_Space) // switch hiragana/english //KDE4 CHANGE
0122     {
0123         if (m_kana == "hiragana") {
0124             m_kana = "english";
0125         } else if (m_kana == "english") {
0126             m_kana = "hiragana";
0127         }
0128 
0129         return;
0130     }
0131 
0132     if (m_kana == "english" || ji.isEmpty()) {
0133         KLineEdit::keyPressEvent(e);
0134         return;
0135     }
0136 
0137     if (shift) // shift for katakana
0138     {
0139         if (m_kana == "hiragana") {
0140             m_kana = "katakana";
0141         }
0142     }
0143 
0144     // kdDebug() << "--------------------\n";
0145 
0146     QString curEng;
0147     QString curKana;
0148     QString text = this->text();
0149 
0150     int i;
0151     unsigned int len = text.length();
0152     // kdDebug() << "length = " << len << endl;
0153     for (i = len - 1; i >= 0; i--) {
0154         QChar at = text.at(i);
0155 
0156         // kdDebug() << "at = " << QString(at) << endl;
0157 
0158         if (at.row() == 0 && at != QLatin1Char('.')) {
0159             // kdDebug() << "prepending " << QString(at) << endl;
0160             curEng.prepend(at);
0161         } else {
0162             break;
0163         }
0164     }
0165     i++;
0166 
0167     // kdDebug() << "i is " << i << ", length is " << len << endl;
0168     curKana = text.left(i);
0169 
0170     ji.prepend(curEng);
0171     ji = ji.toLower();
0172     // kdDebug() << "ji = " << ji << endl;
0173 
0174     QString replace;
0175 
0176     // kdDebug () << "kana is " << m_kana << endl;
0177     if (m_kana == "hiragana") {
0178         replace = m_hiragana[ji];
0179     } else if (m_kana == "katakana") {
0180         replace = m_katakana[ji];
0181     }
0182 
0183     // kdDebug() << "replace = " << replace << endl;
0184 
0185     if (!(replace.isEmpty())) // if (replace has something in it) //KDE4 CHANGE
0186     {
0187         // kdDebug() << "replace isn't empty\n";
0188 
0189         setText(curKana + replace);
0190 
0191         if (m_kana == "katakana") {
0192             m_kana = "hiragana";
0193         }
0194 
0195         return;
0196     } else {
0197         // kdDebug() << "replace is empty\n";
0198         QString farRight(ji.right(ji.length() - 1));
0199         // kdDebug() << "ji = " << ji << endl;
0200         // kdDebug() << "farRight = " << farRight << endl;
0201 
0202         // test if we need small tsu
0203         if (ji.at(0) == farRight.at(0)) // if two letters are same, and we can add a twoletter length kana
0204         {
0205             if (m_kana == "hiragana") {
0206                 setText(curKana + m_hiragana[ji.at(0) == QLatin1Char('n') ? "n'" : "t-"] + farRight.at(0));
0207             } else {
0208                 setText(curKana + m_katakana[ji.at(0) == QLatin1Char('n') ? "n'" : "t-"] + farRight.at(0));
0209             }
0210 
0211             if (m_kana == "katakana") {
0212                 m_kana = "hiragana";
0213             }
0214 
0215             return;
0216         }
0217 
0218         // test for hanging n
0219         QString newkana;
0220         if (m_kana == "hiragana") {
0221             newkana = m_hiragana[farRight];
0222             // kdDebug() << "newkana = " << newkana << endl;
0223             if (ji.at(0) == QLatin1Char('n') && !(newkana.isEmpty())) // KDE4 CHANGE
0224             {
0225                 // kdDebug() << "doing the n thing\n";
0226 
0227                 setText(curKana + m_hiragana[QStringLiteral("n'")] + newkana);
0228 
0229                 if (m_kana == "katakana") {
0230                     m_kana = "hiragana";
0231                 }
0232 
0233                 return;
0234             }
0235         } else {
0236             newkana = m_katakana[farRight];
0237             if (ji.at(0) == QLatin1Char('n') && !newkana.isEmpty()) // KDE4 CHANGE
0238             {
0239                 // kdDebug() << "doing the n thing - katakana\n";
0240 
0241                 setText(curKana + m_katakana[QStringLiteral("n'")] + newkana);
0242 
0243                 if (m_kana == "katakana") {
0244                     m_kana = "hiragana";
0245                 }
0246 
0247                 return;
0248             }
0249         }
0250     }
0251 
0252     if (e->key() == Qt::Key_Return || e->key() == Qt::Key_Enter) // take care of pending n //KDE4 CHANGE
0253     {
0254         if (m_kana == "hiragana") {
0255             if (text[len - 1] == QLatin1Char('n')) {
0256                 setText(curKana + m_hiragana[QStringLiteral("n'")]);
0257             }
0258         } else {
0259             if (text[len - 1] == QLatin1Char('N')) {
0260                 setText(curKana + m_katakana[QStringLiteral("n'")]);
0261             }
0262         }
0263     }
0264 
0265     KLineEdit::keyPressEvent(e); // don't think we'll get here :)
0266 }
0267 
0268 // This is the slot for the menu
0269 void KRomajiEdit::setKana(QAction *action)
0270 {
0271     if (action->text() == QLatin1String("Kana")) {
0272         m_kana = "hiragana";
0273     }
0274     if (action->text() == QLatin1String("English")) {
0275         m_kana = "english";
0276     }
0277 }