-rw-r--r-- | inputmethods/handwriting/qimpenchar.cpp | 2 |
1 files changed, 1 insertions, 1 deletions
diff --git a/inputmethods/handwriting/qimpenchar.cpp b/inputmethods/handwriting/qimpenchar.cpp index 9c38ec9..152bfec 100644 --- a/inputmethods/handwriting/qimpenchar.cpp +++ b/inputmethods/handwriting/qimpenchar.cpp | |||
@@ -240,266 +240,266 @@ QDataStream &operator>> (QDataStream &s, QIMPenChar &ws) | |||
240 | s >> *st; | 240 | s >> *st; |
241 | ws.strokes.append( st ); | 241 | ws.strokes.append( st ); |
242 | } | 242 | } |
243 | 243 | ||
244 | return s; | 244 | return s; |
245 | } | 245 | } |
246 | 246 | ||
247 | //=========================================================================== | 247 | //=========================================================================== |
248 | 248 | ||
249 | bool QIMPenCharMatch::operator>( const QIMPenCharMatch &m ) | 249 | bool QIMPenCharMatch::operator>( const QIMPenCharMatch &m ) |
250 | { | 250 | { |
251 | return error > m.error; | 251 | return error > m.error; |
252 | } | 252 | } |
253 | 253 | ||
254 | bool QIMPenCharMatch::operator<( const QIMPenCharMatch &m ) | 254 | bool QIMPenCharMatch::operator<( const QIMPenCharMatch &m ) |
255 | { | 255 | { |
256 | return error < m.error; | 256 | return error < m.error; |
257 | } | 257 | } |
258 | 258 | ||
259 | bool QIMPenCharMatch::operator<=( const QIMPenCharMatch &m ) | 259 | bool QIMPenCharMatch::operator<=( const QIMPenCharMatch &m ) |
260 | { | 260 | { |
261 | return error <= m.error; | 261 | return error <= m.error; |
262 | } | 262 | } |
263 | 263 | ||
264 | //=========================================================================== | 264 | //=========================================================================== |
265 | 265 | ||
266 | /*! | 266 | /*! |
267 | \class QIMPenCharSet qimpenchar.h | 267 | \class QIMPenCharSet qimpenchar.h |
268 | 268 | ||
269 | Maintains a set of related characters. | 269 | Maintains a set of related characters. |
270 | */ | 270 | */ |
271 | 271 | ||
272 | QIMPenCharSet::QIMPenCharSet() | 272 | QIMPenCharSet::QIMPenCharSet() |
273 | { | 273 | { |
274 | chars.setAutoDelete( TRUE ); | 274 | chars.setAutoDelete( TRUE ); |
275 | desc = "Unnamed"; | 275 | desc = "Unnamed"; |
276 | csTitle = "abc"; | 276 | csTitle = "abc"; |
277 | csType = Unknown; | 277 | csType = Unknown; |
278 | maxStrokes = 0; | 278 | maxStrokes = 0; |
279 | } | 279 | } |
280 | 280 | ||
281 | /*! | 281 | /*! |
282 | Construct and load a characters set from file \a fn. | 282 | Construct and load a characters set from file \a fn. |
283 | */ | 283 | */ |
284 | QIMPenCharSet::QIMPenCharSet( const QString &fn ) | 284 | QIMPenCharSet::QIMPenCharSet( const QString &fn ) |
285 | { | 285 | { |
286 | chars.setAutoDelete( TRUE ); | 286 | chars.setAutoDelete( TRUE ); |
287 | desc = "Unnamed"; | 287 | desc = "Unnamed"; |
288 | csTitle = "abc"; | 288 | csTitle = "abc"; |
289 | csType = Unknown; | 289 | csType = Unknown; |
290 | maxStrokes = 0; | 290 | maxStrokes = 0; |
291 | load( fn, System ); | 291 | load( fn, System ); |
292 | } | 292 | } |
293 | 293 | ||
294 | const QString &QIMPenCharSet::filename( Domain d ) const | 294 | const QString &QIMPenCharSet::filename( Domain d ) const |
295 | { | 295 | { |
296 | if ( d == System ) | 296 | if ( d == System ) |
297 | return sysFilename; | 297 | return sysFilename; |
298 | else | 298 | else |
299 | return userFilename; | 299 | return userFilename; |
300 | } | 300 | } |
301 | 301 | ||
302 | void QIMPenCharSet::setFilename( const QString &fn, Domain d ) | 302 | void QIMPenCharSet::setFilename( const QString &fn, Domain d ) |
303 | { | 303 | { |
304 | if ( d == System ) | 304 | if ( d == System ) |
305 | sysFilename = fn; | 305 | sysFilename = fn; |
306 | else if ( d == User ) | 306 | else if ( d == User ) |
307 | userFilename = fn; | 307 | userFilename = fn; |
308 | } | 308 | } |
309 | 309 | ||
310 | /*! | 310 | /*! |
311 | Load a character set from file \a fn. | 311 | Load a character set from file \a fn. |
312 | */ | 312 | */ |
313 | bool QIMPenCharSet::load( const QString &fn, Domain d ) | 313 | bool QIMPenCharSet::load( const QString &fn, Domain d ) |
314 | { | 314 | { |
315 | setFilename( fn, d ); | 315 | setFilename( fn, d ); |
316 | 316 | ||
317 | bool ok = FALSE; | 317 | bool ok = FALSE; |
318 | QFile file( fn ); | 318 | QFile file( fn ); |
319 | if ( file.open( IO_ReadOnly ) ) { | 319 | if ( file.open( IO_ReadOnly ) ) { |
320 | QDataStream ds( &file ); | 320 | QDataStream ds( &file ); |
321 | QString version; | 321 | QString version; |
322 | ds >> version; | 322 | ds >> version; |
323 | ds >> csTitle; | 323 | ds >> csTitle; |
324 | ds >> desc; | 324 | ds >> desc; |
325 | int major = version.mid( 4, 1 ).toInt(); | 325 | int major = version.mid( 4, 1 ).toInt(); |
326 | int minor = version.mid( 6 ).toInt(); | 326 | int minor = version.mid( 6 ).toInt(); |
327 | if ( major >= 1 && minor > 0 ) { | 327 | if ( major >= 1 && minor > 0 ) { |
328 | ds >> (Q_INT8 &)csType; | 328 | ds >> (Q_INT8 &)csType; |
329 | } else { | 329 | } else { |
330 | if ( csTitle == "abc" ) | 330 | if ( csTitle == "abc" ) |
331 | csType = Lower; | 331 | csType = Lower; |
332 | else if ( csTitle == "ABC" ) | 332 | else if ( csTitle == "ABC" ) |
333 | csType = Upper; | 333 | csType = Upper; |
334 | else if ( csTitle == "123" ) | 334 | else if ( csTitle == "123" ) |
335 | csType = Numeric; | 335 | csType = Numeric; |
336 | else if ( fn == "Combining" ) | 336 | else if ( fn == "Combining" ) |
337 | csType = Combining; | 337 | csType = Combining; |
338 | } | 338 | } |
339 | while ( !ds.atEnd() ) { | 339 | while ( !ds.atEnd() ) { |
340 | QIMPenChar *pc = new QIMPenChar; | 340 | QIMPenChar *pc = new QIMPenChar; |
341 | ds >> *pc; | 341 | ds >> *pc; |
342 | if ( d == User ) | 342 | if ( d == User ) |
343 | markDeleted( pc->character() ); // override system | 343 | markDeleted( pc->character() ); // override system |
344 | addChar( pc ); | 344 | addChar( pc ); |
345 | } | 345 | } |
346 | if ( file.status() == IO_Ok ) | 346 | if ( file.status() == IO_Ok ) |
347 | ok = TRUE; | 347 | ok = TRUE; |
348 | } | 348 | } |
349 | 349 | ||
350 | return ok; | 350 | return ok; |
351 | } | 351 | } |
352 | 352 | ||
353 | /*! | 353 | /*! |
354 | Save this character set. | 354 | Save this character set. |
355 | */ | 355 | */ |
356 | bool QIMPenCharSet::save( Domain d ) | 356 | bool QIMPenCharSet::save( Domain d ) |
357 | { | 357 | { |
358 | if ( filename( d ).isEmpty() ) | 358 | if ( filename( d ).isEmpty() ) |
359 | return FALSE; | 359 | return FALSE; |
360 | 360 | ||
361 | bool ok = FALSE; | 361 | bool ok = FALSE; |
362 | 362 | ||
363 | QString fn = filename( d ); | 363 | QString fn = filename( d ); |
364 | QString tmpFn = fn + ".new"; | 364 | QString tmpFn = fn + ".new"; |
365 | QFile file( tmpFn ); | 365 | QFile file( tmpFn ); |
366 | if ( file.open( IO_WriteOnly|IO_Raw ) ) { | 366 | if ( file.open( IO_WriteOnly|IO_Raw ) ) { |
367 | QDataStream ds( &file ); | 367 | QDataStream ds( &file ); |
368 | ds << QString( "QPT 1.1" ); | 368 | ds << QString( "QPT 1.1" ); |
369 | ds << csTitle; | 369 | ds << csTitle; |
370 | ds << desc; | 370 | ds << desc; |
371 | ds << (Q_INT8)csType; | 371 | ds << (Q_INT8)csType; |
372 | QIMPenCharIterator ci( chars ); | 372 | QIMPenCharIterator ci( chars ); |
373 | for ( ; ci.current(); ++ci ) { | 373 | for ( ; ci.current(); ++ci ) { |
374 | QIMPenChar *pc = ci.current(); | 374 | QIMPenChar *pc = ci.current(); |
375 | if ( ( (d == System) && pc->testFlag( QIMPenChar::System ) ) || | 375 | if ( ( (d == System) && pc->testFlag( QIMPenChar::System ) ) || |
376 | ( (d == User) && !pc->testFlag( QIMPenChar::System ) ) ) { | 376 | ( (d == User) && !pc->testFlag( QIMPenChar::System ) ) ) { |
377 | ds << *pc; | 377 | ds << *pc; |
378 | } | 378 | } |
379 | if ( file.status() != IO_Ok ) | 379 | if ( file.status() != IO_Ok ) |
380 | break; | 380 | break; |
381 | } | 381 | } |
382 | if ( file.status() == IO_Ok ) | 382 | if ( file.status() == IO_Ok ) |
383 | ok = TRUE; | 383 | ok = TRUE; |
384 | } | 384 | } |
385 | 385 | ||
386 | if ( ok ) { | 386 | if ( ok ) { |
387 | if ( ::rename( tmpFn.latin1(), fn.latin1() ) < 0 ) { | 387 | if ( ::rename( tmpFn.latin1(), fn.latin1() ) < 0 ) { |
388 | qWarning( "problem renaming file %s to %s, errno: %d", | 388 | qWarning( "problem renaming file %s to %s, errno: %d", |
389 | tmpFn.latin1(), fn.latin1(), errno ); | 389 | tmpFn.latin1(), fn.latin1(), errno ); |
390 | // remove the tmp file, otherwise, it will just lay around... | 390 | // remove the tmp file, otherwise, it will just lay around... |
391 | QFile::remove( tmpFn.latin1() ); | 391 | QFile::remove( tmpFn.latin1() ); |
392 | ok = FALSE; | 392 | ok = FALSE; |
393 | } | 393 | } |
394 | } | 394 | } |
395 | 395 | ||
396 | return ok; | 396 | return ok; |
397 | } | 397 | } |
398 | 398 | ||
399 | QIMPenChar *QIMPenCharSet::at( int i ) | 399 | QIMPenChar *QIMPenCharSet::at( int i ) |
400 | { | 400 | { |
401 | return chars.at(i); | 401 | return chars.at(i); |
402 | } | 402 | } |
403 | 403 | ||
404 | void QIMPenCharSet::markDeleted( uint ch ) | 404 | void QIMPenCharSet::markDeleted( uint ch ) |
405 | { | 405 | { |
406 | QIMPenCharIterator ci( chars ); | 406 | QIMPenCharIterator ci( chars ); |
407 | for ( ; ci.current(); ++ci ) { | 407 | for ( ; ci.current(); ++ci ) { |
408 | QIMPenChar *pc = ci.current(); | 408 | QIMPenChar *pc = ci.current(); |
409 | if ( pc->character() == ch && pc->testFlag( QIMPenChar::System ) ) | 409 | if ( pc->character() == ch && pc->testFlag( QIMPenChar::System ) ) |
410 | pc->setFlag( QIMPenChar::Deleted ); | 410 | pc->setFlag( QIMPenChar::Deleted ); |
411 | } | 411 | } |
412 | } | 412 | } |
413 | 413 | ||
414 | /*! | 414 | /*! |
415 | Find the best matches for \a ch in this character set. | 415 | Find the best matches for \a ch in this character set. |
416 | */ | 416 | */ |
417 | QIMPenCharMatchList QIMPenCharSet::match( QIMPenChar *ch ) | 417 | QIMPenCharMatchList QIMPenCharSet::match( QIMPenChar *ch ) |
418 | { | 418 | { |
419 | QIMPenCharMatchList matches; | 419 | QIMPenCharMatchList matches; |
420 | 420 | ||
421 | QIMPenCharIterator ci( chars ); | 421 | QIMPenCharIterator ci( chars ); |
422 | for ( ; ci.current(); ++ci ) { | 422 | for ( ; ci.current(); ++ci ) { |
423 | QIMPenChar *tmplChar = ci.current(); | 423 | QIMPenChar *tmplChar = ci.current(); |
424 | if ( tmplChar->testFlag( QIMPenChar::Deleted ) ) { | 424 | if ( tmplChar->testFlag( QIMPenChar::Deleted ) ) { |
425 | continue; | 425 | continue; |
426 | } | 426 | } |
427 | int err; | 427 | int err; |
428 | if ( ch->penStrokes().count() <= tmplChar->penStrokes().count() ) { | 428 | if ( ch->penStrokes().count() <= tmplChar->penStrokes().count() ) { |
429 | err = ch->match( tmplChar ); | 429 | err = ch->match( tmplChar ); |
430 | if ( err <= QIMPEN_MATCH_THRESHOLD ) { | 430 | if ( err <= QIMPEN_MATCH_THRESHOLD ) { |
431 | if (tmplChar->penStrokes().count() != ch->penStrokes().count()) | 431 | if (tmplChar->penStrokes().count() != ch->penStrokes().count()) |
432 | err = QIMPEN_MATCH_THRESHOLD; | 432 | err = QMIN(err*3, QIMPEN_MATCH_THRESHOLD); |
433 | QIMPenCharMatchList::Iterator it; | 433 | QIMPenCharMatchList::Iterator it; |
434 | for ( it = matches.begin(); it != matches.end(); ++it ) { | 434 | for ( it = matches.begin(); it != matches.end(); ++it ) { |
435 | if ( (*it).penChar->character() == tmplChar->character() && | 435 | if ( (*it).penChar->character() == tmplChar->character() && |
436 | (*it).penChar->penStrokes().count() == tmplChar->penStrokes().count() ) { | 436 | (*it).penChar->penStrokes().count() == tmplChar->penStrokes().count() ) { |
437 | if ( (*it).error > err ) | 437 | if ( (*it).error > err ) |
438 | (*it).error = err; | 438 | (*it).error = err; |
439 | break; | 439 | break; |
440 | } | 440 | } |
441 | } | 441 | } |
442 | if ( it == matches.end() ) { | 442 | if ( it == matches.end() ) { |
443 | QIMPenCharMatch m; | 443 | QIMPenCharMatch m; |
444 | m.error = err; | 444 | m.error = err; |
445 | m.penChar = tmplChar; | 445 | m.penChar = tmplChar; |
446 | matches.append( m ); | 446 | matches.append( m ); |
447 | } | 447 | } |
448 | } | 448 | } |
449 | } | 449 | } |
450 | } | 450 | } |
451 | qHeapSort( matches ); | 451 | qHeapSort( matches ); |
452 | /* | 452 | /* |
453 | QIMPenCharMatchList::Iterator it; | 453 | QIMPenCharMatchList::Iterator it; |
454 | for ( it = matches.begin(); it != matches.end(); ++it ) { | 454 | for ( it = matches.begin(); it != matches.end(); ++it ) { |
455 | qDebug( "Match: \'%c\', error %d, strokes %d", (*it).penChar->character(), | 455 | qDebug( "Match: \'%c\', error %d, strokes %d", (*it).penChar->character(), |
456 | (*it).error, (*it).penChar->penStrokes().count() ); | 456 | (*it).error, (*it).penChar->penStrokes().count() ); |
457 | } | 457 | } |
458 | */ | 458 | */ |
459 | return matches; | 459 | return matches; |
460 | } | 460 | } |
461 | 461 | ||
462 | /*! | 462 | /*! |
463 | Add a character \a ch to this set. | 463 | Add a character \a ch to this set. |
464 | QIMPenCharSet will delete this character when it is no longer needed. | 464 | QIMPenCharSet will delete this character when it is no longer needed. |
465 | */ | 465 | */ |
466 | void QIMPenCharSet::addChar( QIMPenChar *ch ) | 466 | void QIMPenCharSet::addChar( QIMPenChar *ch ) |
467 | { | 467 | { |
468 | if ( ch->penStrokes().count() > maxStrokes ) | 468 | if ( ch->penStrokes().count() > maxStrokes ) |
469 | maxStrokes = ch->penStrokes().count(); | 469 | maxStrokes = ch->penStrokes().count(); |
470 | chars.append( ch ); | 470 | chars.append( ch ); |
471 | } | 471 | } |
472 | 472 | ||
473 | /*! | 473 | /*! |
474 | Remove a character by reference \a ch from this set. | 474 | Remove a character by reference \a ch from this set. |
475 | QIMPenCharSet will delete this character. | 475 | QIMPenCharSet will delete this character. |
476 | */ | 476 | */ |
477 | void QIMPenCharSet::removeChar( QIMPenChar *ch ) | 477 | void QIMPenCharSet::removeChar( QIMPenChar *ch ) |
478 | { | 478 | { |
479 | chars.remove( ch ); | 479 | chars.remove( ch ); |
480 | } | 480 | } |
481 | 481 | ||
482 | /*! | 482 | /*! |
483 | Move the character up the list of characters. | 483 | Move the character up the list of characters. |
484 | */ | 484 | */ |
485 | void QIMPenCharSet::up( QIMPenChar *ch ) | 485 | void QIMPenCharSet::up( QIMPenChar *ch ) |
486 | { | 486 | { |
487 | int idx = chars.findRef( ch ); | 487 | int idx = chars.findRef( ch ); |
488 | if ( idx > 0 ) { | 488 | if ( idx > 0 ) { |
489 | chars.take(); | 489 | chars.take(); |
490 | chars.insert( idx - 1, ch ); | 490 | chars.insert( idx - 1, ch ); |
491 | } | 491 | } |
492 | } | 492 | } |
493 | 493 | ||
494 | /*! | 494 | /*! |
495 | Move the character down the list of characters. | 495 | Move the character down the list of characters. |
496 | */ | 496 | */ |
497 | void QIMPenCharSet::down( QIMPenChar *ch ) | 497 | void QIMPenCharSet::down( QIMPenChar *ch ) |
498 | { | 498 | { |
499 | int idx = chars.findRef( ch ); | 499 | int idx = chars.findRef( ch ); |
500 | if ( idx >= 0 && idx < (int)chars.count() - 1 ) { | 500 | if ( idx >= 0 && idx < (int)chars.count() - 1 ) { |
501 | chars.take(); | 501 | chars.take(); |
502 | chars.insert( idx + 1, ch ); | 502 | chars.insert( idx + 1, ch ); |
503 | } | 503 | } |
504 | } | 504 | } |
505 | 505 | ||