1
/*****************************************************************************
2
* This file is part of Kiten, a KDE Japanese Reference Tool *
3
* Copyright (C) 2001 Jason Katz-Brown <jason@katzbrown.com> *
4
* Copyright (C) 2006 Joseph Kerian <jkerian@gmail.com> *
6
* This library is free software; you can redistribute it and/or *
7
* modify it under the terms of the GNU Library General Public *
8
* License as published by the Free Software Foundation; either *
9
* version 2 of the License, or (at your option) any later version. *
11
* This library is distributed in the hope that it will be useful, *
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
14
* Library General Public License for more details. *
16
* You should have received a copy of the GNU Library General Public License *
17
* along with this library; see the file COPYING.LIB. If not, write to *
18
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, *
19
* Boston, MA 02110-1301, USA. *
20
*****************************************************************************/
22
#include "kromajiedit.h"
26
#include <KMessageBox>
27
#include <KStandardDirs>
30
#include <QApplication>
37
#include <QTextStream>
39
KRomajiEdit::KRomajiEdit( QWidget *parent, const char *name )
40
: KLineEdit( name, parent ) //KDE4 CHANGE
44
KStandardDirs *dirs = KGlobal::dirs();
45
QString romkana = dirs->findResource( "data", "kiten/romkana.cnv" );
46
if ( romkana.isNull() )
48
KMessageBox::error( 0, i18n( "Romaji information file not installed, so Romaji conversion cannot be used." ) );
54
if ( ! f.open( QIODevice::ReadOnly ) )
56
KMessageBox::error( 0, i18n( "Romaji information could not be loaded, so Romaji conversion cannot be used." ) );
60
t.setCodec( QTextCodec::codecForName( "eucJP" ) );
61
while ( ! t.atEnd() ) //KDE4 CHANGE
63
QString s = t.readLine();
65
QChar first = s.at( 0 );
66
if ( first == '#' ) // comment!
70
else if ( first == '$' ) // header
72
if ( m_kana == "unset" )
83
QStringList things( s.split( QChar( ' ' ) ) );
84
QString thekana( things.first() );
85
QString romaji( /* KDE4 CHANGE: * */things.at( 1 ) );
87
if ( m_kana == "hiragana" )
89
m_hiragana[ romaji ] = thekana;
91
else if ( m_kana == "katakana" )
93
m_katakana[ romaji ] = thekana;
102
KRomajiEdit::~KRomajiEdit()
106
QMenu *KRomajiEdit::createPopupMenu()
110
//TODO: Get the basic editing options in here from a KLineEdit popup menu (or elsewhere?)
111
menu->addSeparator();
113
//Put our action group together
114
QActionGroup *group = new QActionGroup( menu );
117
temp = new QAction( i18nc( "@option:radio selects english translation", "English" ), group );
118
temp->setCheckable( true );
119
menu->addAction( temp );
120
if( m_kana == "english" )
122
temp->setChecked( true );
126
temp->setChecked( false );
129
temp = new QAction( i18nc( "@option:radio selects japanese translation", "Kana" ), group );
130
temp->setCheckable( true );
131
menu->addAction( temp );
132
if( m_kana == "kana" )
134
temp->setChecked( true );
138
temp->setChecked( false );
141
connect( group, SIGNAL( triggered( QAction* ) ),
142
SLOT( setKana( QAction* ) ) );
144
emit aboutToShowContextMenu( menu );
148
// TODO allow editing not only at end
149
void KRomajiEdit::keyPressEvent( QKeyEvent *e )
151
bool shift = qApp->keyboardModifiers() & Qt::ShiftModifier; //KDE4 CHANGE
152
QString ji = e->text();
154
if ( shift && e->key() == Qt::Key_Space ) // switch hiragana/english //KDE4 CHANGE
156
if ( m_kana == "hiragana" )
160
else if ( m_kana == "english" )
168
if ( m_kana == "english" || ji.isEmpty() )
170
KLineEdit::keyPressEvent( e );
174
if ( shift ) // shift for katakana
176
if (m_kana == "hiragana")
182
//kdDebug() << "--------------------\n";
186
QString text = this->text();
189
unsigned int len = text.length();
190
//kdDebug() << "length = " << len << endl;
191
for ( i = len - 1; i >= 0; i-- )
193
QChar at = text.at( i );
195
//kdDebug() << "at = " << QString(at) << endl;
197
if ( at.row() == 0 && at != '.' )
199
//kdDebug() << "prepending " << QString(at) << endl;
209
//kdDebug() << "i is " << i << ", length is " << len << endl;
210
curKana = text.left( i );
212
ji.prepend( curEng );
214
//kdDebug() << "ji = " << ji << endl;
218
//kdDebug () << "kana is " << m_kana << endl;
219
if ( m_kana == "hiragana" )
221
replace = m_hiragana[ ji ];
223
else if ( m_kana == "katakana" )
225
replace = m_katakana[ ji ];
228
//kdDebug() << "replace = " << replace << endl;
230
if ( ! ( replace.isEmpty() ) ) // if (replace has something in it) //KDE4 CHANGE
232
//kdDebug() << "replace isn't empty\n";
234
setText( curKana + replace );
236
if ( m_kana == "katakana" )
245
//kdDebug() << "replace is empty\n";
246
QString farRight( ji.right( ji.length() - 1 ) );
247
//kdDebug() << "ji = " << ji << endl;
248
//kdDebug() << "farRight = " << farRight << endl;
250
// test if we need small tsu
251
if ( ji.at( 0 ) == farRight.at( 0 ) ) // if two letters are same, and we can add a twoletter length kana
253
if ( m_kana == "hiragana" )
255
setText( curKana + m_hiragana[ ji.at( 0 ) == 'n' ? "n'" : "t-" ] + farRight.at( 0 ) );
259
setText( curKana + m_katakana[ ji.at( 0 ) == 'n' ? "n'" : "t-" ] + farRight.at( 0 ) );
262
if ( m_kana == "katakana" )
270
// test for hanging n
272
if ( m_kana == "hiragana" )
274
newkana = m_hiragana[ farRight ];
275
//kdDebug() << "newkana = " << newkana << endl;
276
if ( ji.at( 0 ) == 'n' && ! ( newkana.isEmpty() ) ) //KDE4 CHANGE
278
//kdDebug() << "doing the n thing\n";
280
setText( curKana + m_hiragana[ "n'" ] + newkana );
282
if ( m_kana == "katakana" )
292
newkana = m_katakana[ farRight ];
293
if ( ji.at( 0 ) == 'n' && ! newkana.isEmpty() ) //KDE4 CHANGE
295
//kdDebug() << "doing the n thing - katakana\n";
297
setText( curKana + m_katakana[ "n'" ] + newkana );
299
if ( m_kana == "katakana" )
309
if ( e->key() == Qt::Key_Return || e->key() == Qt::Key_Enter ) // take care of pending n //KDE4 CHANGE
311
if ( m_kana == "hiragana" )
313
if ( text[ len - 1 ] == 'n' )
315
setText( curKana + m_hiragana[ "n'" ] );
320
if ( text[ len - 1 ] == 'N' )
322
setText( curKana + m_katakana[ "n'" ] );
327
KLineEdit::keyPressEvent( e ); // don't think we'll get here :)
330
// This is the slot for the menu
331
void KRomajiEdit::setKana( QAction *action )
333
if( action->text() == "Kana" )
337
if( action->text() == "English" )
343
#include "kromajiedit.moc"