• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • kdeedu
  • Sitemap
  • Contact Us
 

kiten/lib

kromajiedit.cpp

Go to the documentation of this file.
00001 /* This file is part of Kiten, a KDE Japanese Reference Tool...
00002  Copyright (C) 2001  Jason Katz-Brown <jason@katzbrown.com>
00003            (C) 2006  Joseph Kerian <jkerian@gmail.com>
00004 
00005    This library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Library General Public
00007    License as published by the Free Software Foundation; either
00008    version 2 of the License, or (at your option) any later version.
00009 
00010    This library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Library General Public License for more details.
00014 
00015    You should have received a copy of the GNU Library General Public License
00016    along with this library; see the file COPYING.LIB.  If not, write to
00017    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00018    Boston, MA 02110-1301, USA.
00019 */
00020 
00021 #include "kromajiedit.h"
00022 
00023 #include <kdebug.h>
00024 #include <kstandarddirs.h>
00025 #include <kmessagebox.h>
00026 #include <klocale.h>
00027 
00028 #include <QtCore/QMap>
00029 #include <QtCore/QByteArray>
00030 #include <QtCore/QTextCodec>
00031 #include <QtCore/QTextStream>
00032 #include <QtCore/QFile>
00033 #include <QtGui/QApplication>
00034 #include <QtGui/QAction>
00035 #include <QtGui/QKeyEvent>
00036 #include <QtGui/QMenu>
00037 
00038 KRomajiEdit::KRomajiEdit(QWidget *parent, const char *name)
00039     : KLineEdit(name, parent) //KDE4 CHANGE
00040 {
00041     kana = "unset";
00042 
00043     KStandardDirs *dirs = KGlobal::dirs();
00044     QString romkana = dirs->findResource("data", "kiten/romkana.cnv");
00045     if (romkana.isNull())
00046     {
00047         KMessageBox::error(0, i18n("Romaji information file not installed, so Romaji conversion cannot be used."));
00048         return;
00049     }
00050 
00051     QFile f(romkana);
00052     
00053     if (!f.open(QIODevice::ReadOnly))
00054     {
00055         KMessageBox::error(0, i18n("Romaji information could not be loaded, so Romaji conversion cannot be used."));
00056     }
00057 
00058     QTextStream t(&f);
00059     t.setCodec(QTextCodec::codecForName("eucJP"));
00060     while (!t.atEnd()) //KDE4 CHANGE
00061     {
00062         QString s = t.readLine();
00063 
00064         QChar first = s.at(0);
00065         if (first == '#') // comment!
00066         {
00067             // nothing
00068         }
00069         else if (first == '$') // header
00070         {
00071             if (kana == "unset")
00072                 kana = "hiragana";
00073             else
00074                 kana = "katakana";
00075         }
00076         else // body
00077         {
00078             QStringList things(s.split(QChar(' ')));
00079             QString thekana(things.first());
00080             QString romaji(/* KDE4 CHANGE: * */things.at(1));
00081 
00082             if (kana == "hiragana")
00083                 hiragana[romaji] = thekana;
00084             else if (kana == "katakana")
00085                 katakana[romaji] = thekana;
00086         }
00087     }
00088     f.close();
00089 
00090     kana = "english";
00091 }
00092 
00093 KRomajiEdit::~KRomajiEdit()
00094 {
00095 }
00096 
00097 // This is the slot for the menu
00098 void KRomajiEdit::setKana(QAction *action) {
00099     if(action->text() == "Kana")
00100         kana = "hiragana";
00101     if(action->text() == "English")
00102         kana = "english";
00103 }
00104 
00105 // TODO allow editing not only at end
00106 void KRomajiEdit::keyPressEvent(QKeyEvent *e)
00107 {
00108     bool shift = qApp->keyboardModifiers() & Qt::ShiftModifier; //KDE4 CHANGE
00109     QString ji = e->text();
00110 
00111     if (shift && e->key() == Qt::Key_Space) // switch hiragana/english //KDE4 CHANGE
00112     {
00113         if (kana == "hiragana")
00114             kana = "english";
00115         else if (kana == "english")
00116             kana = "hiragana";
00117 
00118         return;
00119     }
00120 
00121     if (kana == "english" || ji.isEmpty())
00122     {
00123         KLineEdit::keyPressEvent(e);
00124         return;
00125     }
00126 
00127     if (shift) // shift for katakana
00128     {
00129         if (kana == "hiragana")
00130             kana = "katakana";
00131     }
00132 
00133     //kdDebug() << "--------------------\n";
00134 
00135     QString curEng;
00136     QString curKana;
00137     QString _text = text();
00138 
00139     int i;
00140     unsigned int len = _text.length();
00141     //kdDebug() << "length = " << len << endl;
00142     for (i = len - 1; i >= 0; i--)
00143     {
00144         QChar at = _text.at(i);
00145 
00146         //kdDebug() << "at = " << QString(at) << endl;
00147 
00148         if (at.row() == 0 && at != '.')
00149         {
00150             //kdDebug() << "prepending " << QString(at) << endl;
00151             curEng.prepend(at);
00152         }
00153         else
00154             break;
00155     }
00156     i++;
00157 
00158     //kdDebug() << "i is " << i << ", length is " << len << endl;
00159     curKana = _text.left(i);
00160 
00161     ji.prepend(curEng);
00162     ji = ji.toLower();
00163     //kdDebug() << "ji = " << ji << endl;
00164 
00165     QString replace;
00166 
00167     //kdDebug () << "kana is " << kana << endl;
00168     if (kana == "hiragana")
00169         replace = hiragana[ji];
00170     else if (kana == "katakana")
00171         replace = katakana[ji];
00172 
00173     //kdDebug() << "replace = " << replace << endl;
00174 
00175     if (!(replace.isEmpty())) // if (replace has something in it) //KDE4 CHANGE
00176     {
00177         //kdDebug() << "replace isn't empty\n";
00178 
00179         setText(curKana + replace);
00180 
00181         if (kana == "katakana")
00182             kana = "hiragana";
00183         return;
00184     }
00185     else
00186     {
00187         //kdDebug() << "replace is empty\n";
00188         QString farRight(ji.right(ji.length() - 1));
00189         //kdDebug() << "ji = " << ji << endl;
00190         //kdDebug() << "farRight = " << farRight << endl;
00191 
00192         // test if we need small tsu
00193         if (ji.at(0) == farRight.at(0)) // if two letters are same, and we can add a twoletter length kana
00194         {
00195             if (kana == "hiragana")
00196                 setText(curKana + hiragana[ji.at(0) == 'n'? "n'" : "t-"] + farRight.at(0));
00197             else
00198                 setText(curKana + katakana[ji.at(0) == 'n'? "n'" : "t-"] + farRight.at(0));
00199 
00200             if (kana == "katakana")
00201                 kana = "hiragana";
00202             return;
00203         }
00204 
00205         // test for hanging n
00206         QString newkana;
00207         if (kana == "hiragana")
00208         {
00209             newkana = hiragana[farRight];
00210             //kdDebug() << "newkana = " << newkana << endl;
00211             if (ji.at(0) == 'n' && !(newkana.isEmpty())) //KDE4 CHANGE
00212             {
00213                 //kdDebug() << "doing the n thing\n";
00214                 
00215                 setText(curKana + hiragana["n'"] + newkana);
00216 
00217                 if (kana == "katakana")
00218                     kana = "hiragana";
00219                 return;
00220             }
00221         }
00222         else
00223         {
00224             newkana = katakana[farRight];
00225             if (ji.at(0) == 'n' && !newkana.isEmpty()) //KDE4 CHANGE
00226             {
00227                 //kdDebug() << "doing the n thing - katakana\n";
00228                 
00229                 setText(curKana + katakana["n'"] + newkana);
00230 
00231                 if (kana == "katakana")
00232                     kana = "hiragana";
00233                 return;
00234             }
00235         }
00236     }
00237 
00238     if ( e->key() == Qt::Key_Return || e->key() == Qt::Key_Enter ) // take care of pending n //KDE4 CHANGE
00239     {
00240         if (kana == "hiragana")
00241         {
00242             if (_text[len-1] == 'n')
00243                 setText(curKana + hiragana["n'"]);
00244         }
00245         else
00246         {
00247             if (_text[len-1] == 'N')
00248                 setText(curKana + katakana["n'"]);
00249         }
00250     }
00251 
00252     KLineEdit::keyPressEvent(e); // don't think we'll get here :)
00253 }
00254 
00255 QMenu *KRomajiEdit::createPopupMenu()
00256 {
00257     QMenu *menu;
00258     menu = new QMenu();
00259     //TODO: Get the basic editing options in here from a KLineEdit popup menu (or elsewhere?)
00260     menu->addSeparator();
00261 
00262     //Put our action group together
00263     QActionGroup *group = new QActionGroup(menu);
00264     
00265     QAction *temp;
00266     temp = new QAction(i18nc("@option:radio selects english translation", "English"),group);
00267     temp->setCheckable(true);
00268     menu->addAction(temp);
00269     if(kana == "english")
00270         temp->setChecked(true);
00271     else
00272         temp->setChecked(false);
00273 
00274     temp = new QAction(i18nc("@option:radio selects japanese translation", "Kana"),group);
00275     temp->setCheckable(true);
00276     menu->addAction(temp);
00277     if(kana == "kana")
00278         temp->setChecked(true);
00279     else
00280         temp->setChecked(false);
00281 
00282     connect(group, SIGNAL(triggered(QAction*)), SLOT(setKana(QAction*)));
00283     
00284    emit aboutToShowContextMenu(menu);
00285    return menu;
00286 }
00287 
00288 #include "kromajiedit.moc"

kiten/lib

Skip menu "kiten/lib"
  • Main Page
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Class Members
  • Related Pages

kdeedu

Skip menu "kdeedu"
  • kalzium
  • kanagram
  • kig
  •   lib
  • klettres
  • kstars
  • libkdeedu
  •   keduvocdocument
  •   docs
  •   src
  • parley
  •   stepcore
Generated for kdeedu by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal