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

« back to all changes in this revision

Viewing changes to lib/kromajiedit.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Harald Sitter
  • Date: 2011-07-10 11:23:47 UTC
  • Revision ID: james.westby@ubuntu.com-20110710112347-ykfhtvam3kgssspo
Tags: upstream-4.6.90+repack
ImportĀ upstreamĀ versionĀ 4.6.90+repack

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) 2001 Jason Katz-Brown <jason@katzbrown.com>                 *
 
4
 * Copyright (C) 2006 Joseph Kerian <jkerian@gmail.com>                      *
 
5
 *                                                                           *
 
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.          *
 
10
 *                                                                           *
 
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.                          *
 
15
 *                                                                           *
 
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
 *****************************************************************************/
 
21
 
 
22
#include "kromajiedit.h"
 
23
 
 
24
#include <KDebug>
 
25
#include <KLocale>
 
26
#include <KMessageBox>
 
27
#include <KStandardDirs>
 
28
 
 
29
#include <QAction>
 
30
#include <QApplication>
 
31
#include <QByteArray>
 
32
#include <QFile>
 
33
#include <QKeyEvent>
 
34
#include <QMap>
 
35
#include <QMenu>
 
36
#include <QTextCodec>
 
37
#include <QTextStream>
 
38
 
 
39
KRomajiEdit::KRomajiEdit( QWidget *parent, const char *name )
 
40
: KLineEdit( name, parent ) //KDE4 CHANGE
 
41
{
 
42
  m_kana = "unset";
 
43
 
 
44
  KStandardDirs *dirs = KGlobal::dirs();
 
45
  QString romkana = dirs->findResource( "data", "kiten/romkana.cnv" );
 
46
  if ( romkana.isNull() )
 
47
  {
 
48
    KMessageBox::error( 0, i18n( "Romaji information file not installed, so Romaji conversion cannot be used." ) );
 
49
    return;
 
50
  }
 
51
 
 
52
  QFile f( romkana );
 
53
 
 
54
  if ( ! f.open( QIODevice::ReadOnly ) )
 
55
  {
 
56
    KMessageBox::error( 0, i18n( "Romaji information could not be loaded, so Romaji conversion cannot be used." ) );
 
57
  }
 
58
 
 
59
  QTextStream t( &f );
 
60
  t.setCodec( QTextCodec::codecForName( "eucJP" ) );
 
61
  while ( ! t.atEnd() ) //KDE4 CHANGE
 
62
  {
 
63
    QString s = t.readLine();
 
64
 
 
65
    QChar first = s.at( 0 );
 
66
    if ( first == '#' ) // comment!
 
67
    {
 
68
      // nothing
 
69
    }
 
70
    else if ( first == '$' ) // header
 
71
    {
 
72
      if ( m_kana == "unset" )
 
73
      {
 
74
        m_kana = "hiragana";
 
75
      }
 
76
      else
 
77
      {
 
78
        m_kana = "katakana";
 
79
      }
 
80
    }
 
81
    else // body
 
82
    {
 
83
      QStringList things( s.split( QChar( ' ' ) ) );
 
84
      QString thekana( things.first() );
 
85
      QString romaji( /* KDE4 CHANGE: * */things.at( 1 ) );
 
86
 
 
87
      if ( m_kana == "hiragana" )
 
88
      {
 
89
        m_hiragana[ romaji ] = thekana;
 
90
      }
 
91
      else if ( m_kana == "katakana" )
 
92
      {
 
93
        m_katakana[ romaji ] = thekana;
 
94
      }
 
95
    }
 
96
  }
 
97
  f.close();
 
98
 
 
99
  m_kana = "english";
 
100
}
 
101
 
 
102
KRomajiEdit::~KRomajiEdit()
 
103
{
 
104
}
 
105
 
 
106
QMenu *KRomajiEdit::createPopupMenu()
 
107
{
 
108
  QMenu *menu;
 
109
  menu = new QMenu();
 
110
  //TODO: Get the basic editing options in here from a KLineEdit popup menu (or elsewhere?)
 
111
  menu->addSeparator();
 
112
 
 
113
  //Put our action group together
 
114
  QActionGroup *group = new QActionGroup( menu );
 
115
 
 
116
  QAction *temp;
 
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" )
 
121
  {
 
122
    temp->setChecked( true );
 
123
  }
 
124
  else
 
125
  {
 
126
    temp->setChecked( false );
 
127
  }
 
128
 
 
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" )
 
133
  {
 
134
    temp->setChecked( true );
 
135
  }
 
136
  else
 
137
  {
 
138
    temp->setChecked( false );
 
139
  }
 
140
 
 
141
  connect( group, SIGNAL( triggered( QAction* ) ),
 
142
                    SLOT(   setKana( QAction* ) ) );
 
143
 
 
144
  emit aboutToShowContextMenu( menu );
 
145
  return menu;
 
146
}
 
147
 
 
148
// TODO allow editing not only at end
 
149
void KRomajiEdit::keyPressEvent( QKeyEvent *e )
 
150
{
 
151
  bool shift = qApp->keyboardModifiers() & Qt::ShiftModifier; //KDE4 CHANGE
 
152
  QString ji = e->text();
 
153
 
 
154
  if ( shift && e->key() == Qt::Key_Space ) // switch hiragana/english //KDE4 CHANGE
 
155
  {
 
156
    if ( m_kana == "hiragana" )
 
157
    {
 
158
      m_kana = "english";
 
159
    }
 
160
    else if ( m_kana == "english" )
 
161
    {
 
162
      m_kana = "hiragana";
 
163
    }
 
164
 
 
165
    return;
 
166
  }
 
167
 
 
168
  if ( m_kana == "english" || ji.isEmpty() )
 
169
  {
 
170
    KLineEdit::keyPressEvent( e );
 
171
    return;
 
172
  }
 
173
 
 
174
  if ( shift ) // shift for katakana
 
175
  {
 
176
    if (m_kana == "hiragana")
 
177
    {
 
178
      m_kana = "katakana";
 
179
    }
 
180
  }
 
181
 
 
182
  //kdDebug() << "--------------------\n";
 
183
 
 
184
  QString curEng;
 
185
  QString curKana;
 
186
  QString text = this->text();
 
187
 
 
188
  int i;
 
189
  unsigned int len = text.length();
 
190
  //kdDebug() << "length = " << len << endl;
 
191
  for ( i = len - 1; i >= 0; i-- )
 
192
  {
 
193
    QChar at = text.at( i );
 
194
 
 
195
    //kdDebug() << "at = " << QString(at) << endl;
 
196
 
 
197
    if ( at.row() == 0 && at != '.' )
 
198
    {
 
199
      //kdDebug() << "prepending " << QString(at) << endl;
 
200
      curEng.prepend(at);
 
201
    }
 
202
    else
 
203
    {
 
204
      break;
 
205
    }
 
206
  }
 
207
  i++;
 
208
 
 
209
  //kdDebug() << "i is " << i << ", length is " << len << endl;
 
210
  curKana = text.left( i );
 
211
 
 
212
  ji.prepend( curEng );
 
213
  ji = ji.toLower();
 
214
  //kdDebug() << "ji = " << ji << endl;
 
215
 
 
216
  QString replace;
 
217
 
 
218
  //kdDebug () << "kana is " << m_kana << endl;
 
219
  if ( m_kana == "hiragana" )
 
220
  {
 
221
    replace = m_hiragana[ ji ];
 
222
  }
 
223
  else if ( m_kana == "katakana" )
 
224
  {
 
225
    replace = m_katakana[ ji ];
 
226
  }
 
227
 
 
228
  //kdDebug() << "replace = " << replace << endl;
 
229
 
 
230
  if ( ! ( replace.isEmpty() ) ) // if (replace has something in it) //KDE4 CHANGE
 
231
  {
 
232
    //kdDebug() << "replace isn't empty\n";
 
233
 
 
234
    setText( curKana + replace );
 
235
 
 
236
    if ( m_kana == "katakana" )
 
237
    {
 
238
      m_kana = "hiragana";
 
239
    }
 
240
 
 
241
    return;
 
242
  }
 
243
  else
 
244
  {
 
245
    //kdDebug() << "replace is empty\n";
 
246
    QString farRight( ji.right( ji.length() - 1 ) );
 
247
    //kdDebug() << "ji = " << ji << endl;
 
248
    //kdDebug() << "farRight = " << farRight << endl;
 
249
 
 
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
 
252
    {
 
253
      if ( m_kana == "hiragana" )
 
254
      {
 
255
        setText( curKana + m_hiragana[ ji.at( 0 ) == 'n' ? "n'" : "t-" ] + farRight.at( 0 ) );
 
256
      }
 
257
      else
 
258
      {
 
259
        setText( curKana + m_katakana[ ji.at( 0 ) == 'n' ? "n'" : "t-" ] + farRight.at( 0 ) );
 
260
      }
 
261
 
 
262
      if ( m_kana == "katakana" )
 
263
      {
 
264
        m_kana = "hiragana";
 
265
      }
 
266
 
 
267
      return;
 
268
    }
 
269
 
 
270
    // test for hanging n
 
271
    QString newkana;
 
272
    if ( m_kana == "hiragana" )
 
273
    {
 
274
      newkana = m_hiragana[ farRight ];
 
275
      //kdDebug() << "newkana = " << newkana << endl;
 
276
      if ( ji.at( 0 ) == 'n' && ! ( newkana.isEmpty() ) ) //KDE4 CHANGE
 
277
      {
 
278
        //kdDebug() << "doing the n thing\n";
 
279
 
 
280
        setText( curKana + m_hiragana[ "n'" ] + newkana );
 
281
 
 
282
        if ( m_kana == "katakana" )
 
283
        {
 
284
          m_kana = "hiragana";
 
285
        }
 
286
 
 
287
        return;
 
288
      }
 
289
    }
 
290
    else
 
291
    {
 
292
      newkana = m_katakana[ farRight ];
 
293
      if ( ji.at( 0 ) == 'n' && ! newkana.isEmpty() ) //KDE4 CHANGE
 
294
      {
 
295
        //kdDebug() << "doing the n thing - katakana\n";
 
296
 
 
297
        setText( curKana + m_katakana[ "n'" ] + newkana );
 
298
 
 
299
        if ( m_kana == "katakana" )
 
300
        {
 
301
          m_kana = "hiragana";
 
302
        }
 
303
 
 
304
        return;
 
305
      }
 
306
    }
 
307
  }
 
308
 
 
309
  if ( e->key() == Qt::Key_Return || e->key() == Qt::Key_Enter ) // take care of pending n //KDE4 CHANGE
 
310
  {
 
311
    if ( m_kana == "hiragana" )
 
312
    {
 
313
      if ( text[ len - 1 ] == 'n' )
 
314
      {
 
315
        setText( curKana + m_hiragana[ "n'" ] );
 
316
      }
 
317
    }
 
318
    else
 
319
    {
 
320
      if ( text[ len - 1 ] == 'N' )
 
321
      {
 
322
        setText( curKana + m_katakana[ "n'" ] );
 
323
      }
 
324
    }
 
325
  }
 
326
 
 
327
  KLineEdit::keyPressEvent( e ); // don't think we'll get here :)
 
328
}
 
329
 
 
330
// This is the slot for the menu
 
331
void KRomajiEdit::setKana( QAction *action )
 
332
{
 
333
  if( action->text() == "Kana" )
 
334
  {
 
335
    m_kana = "hiragana";
 
336
  }
 
337
  if( action->text() == "English" )
 
338
  {
 
339
    m_kana = "english";
 
340
  }
 
341
}
 
342
 
 
343
#include "kromajiedit.moc"