summaryrefslogtreecommitdiffstats
path: root/kooka/kocrkadmos.cpp
blob: 013b5e1a2a88d9a6c5dffa9ed6a321ee892c5242 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
/***************************************************************************
                          kocrstartdia.cpp  -  description
                             -------------------
    begin                : Fri Now 10 2000
    copyright            : (C) 2000 by Klaas Freitag
    email                : freitag@suse.de
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *  This file may be distributed and/or modified under the terms of the    *
 *  GNU General Public License version 2 as published by the Free Software *
 *  Foundation and appearing in the file COPYING included in the           *
 *  packaging of this file.                                                *
 *
 *  As a special exception, permission is given to link this program       *
 *  with any version of the KADMOS ocr/icr engine of reRecognition GmbH,   *
 *  Kreuzlingen and distribute the resulting executable without            *
 *  including the source code for KADMOS in the source distribution.       *
 *
 *  As a special exception, permission is given to link this program       *
 *  with any edition of TQt, and distribute the resulting executable,       *
 *  without including the source code for TQt in the source distribution.   *
 *                                                                         *
 ***************************************************************************/

#include <tqlayout.h>
#include <tqlabel.h>
#include <tqfileinfo.h>
#include <tqtooltip.h>
#include <tqvbox.h>
#include <tqdict.h>
#include <tqdir.h>
#include <tqmap.h>
#include <tqbuttongroup.h>
#include <tqradiobutton.h>

#include <kapplication.h>
#include <kconfig.h>
#include <kglobal.h>
#include <kdebug.h>
#include <klocale.h>
#include <kanimwidget.h>
#include <kseparator.h>
#include <kmessagebox.h>

#include "resource.h"
#include "ksaneocr.h"  // TODO: Really needed?
#include "kocrkadmos.h"
#include "kocrkadmos.moc"

#include <kscanslider.h>
#include <tqcheckbox.h>
#include <kstandarddirs.h>
#include <tqstringlist.h>


/* defines for konfig-reading */
#define CFG_GROUP_KADMOS "Kadmos"
#define CFG_KADMOS_CLASSIFIER_PATH "classifierPath"
#define CFG_KADMOS_CLASSIFIER      "classifier"


#define CNTRY_CZ i18n( "Czech Republic, Slovakia")
#define CNTRY_GB i18n( "Great Britain, USA" )

KadmosDialog::KadmosDialog( TQWidget *parent, KSpellConfig *spellConfig )
    :KOCRBase( parent, spellConfig, KDialogBase::Tabbed ),
     m_cbNoise(0),
     m_cbAutoscale(0),
     m_haveNorm(false)
{
   kdDebug(28000) << "Starting KOCR-Start-Dialog!" << endl;
   // Layout-Boxes
   findClassifiers();
}

TQString KadmosDialog::ocrEngineLogo() const
{
    return "kadmoslogo.png";
}

TQString KadmosDialog::ocrEngineName() const
{
    return i18n("KADMOS OCR/ICR");
}

TQString KadmosDialog::ocrEngineDesc() const
{
    return i18n("This version of Kooka was linked with the <I>KADMOS OCR/ICR engine</I>, a "
                "commercial engine for optical character recognition.<P>"
                "Kadmos is a product of <B>re Recognition AG</B><BR>"
                "For more information about Kadmos OCR see "
                "<A HREF=http://www.rerecognition.com>"
                "http://www.rerecognition.com</A>");
}


EngineError KadmosDialog::findClassifiers()
{
    findClassifierPath();

    KLocale *locale = TDEGlobal::locale();
    TQStringList allCountries = locale->allLanguagesTwoAlpha ();
    for ( TQStringList::Iterator it = allCountries.begin();
          it != allCountries.end(); ++it )
    {
        m_longCountry2short[locale->twoAlphaToCountryName(*it)] = *it;
    }
    m_longCountry2short[i18n("European Countries")] = "eu";
    m_longCountry2short[ CNTRY_CZ ] = "cz";
    m_longCountry2short[ CNTRY_GB ] = "us";

    TQStringList lst;

    /* custom Path */
    if( ! m_customClassifierPath.isEmpty() )
    {
        TQDir dir( m_customClassifierPath );

        TQStringList lst1 = dir.entryList( "ttf*.rec" );

        for ( TQStringList::Iterator it = lst1.begin(); it != lst1.end(); ++it )
        {
            lst << m_customClassifierPath + *it;
        }

        lst1 = dir.entryList( "hand*.rec" );

        for ( TQStringList::Iterator it = lst1.begin(); it != lst1.end(); ++it )
        {
            lst << m_customClassifierPath + *it;
        }

        lst1 = dir.entryList( "norm*.rec" );

        for ( TQStringList::Iterator it = lst1.begin(); it != lst1.end(); ++it )
        {
            lst << m_customClassifierPath + *it;
        }
    }
    else
    {
        /* standard location */
        KStandardDirs stdDir;
        kdDebug(28000) << "Starting to read resource" << endl;

        lst = stdDir.findAllResources( "data",
                                       "kooka/classifiers/*.rec",
                                       true,   /* recursive */
                                       true ); /* uniqu */
    }


    /* no go through lst and sort out hand-, ttf- and norm classifier */
    for ( TQStringList::Iterator it = lst.begin(); it != lst.end(); ++it )
    {
        TQFileInfo fi( *it);
        TQString name = fi.fileName().lower();

        kdDebug(28000) << "Checking file " << *it << endl;

        if( name.startsWith( "ttf" ) )
        {
            TQString lang = name.mid(3,2);
            if( allCountries.contains(lang) )
            {
                TQString lngCountry = locale->twoAlphaToCountryName(lang);
                if( lngCountry.isEmpty() )
                    lngCountry = name;
                m_ttfClassifier << lngCountry;
                kdDebug(28000) << "ttf: Insert country " << lngCountry << endl;
            }
            else if( lang == "cz" )
            {
                m_ttfClassifier << CNTRY_CZ;
            }
            else if( lang == "us" )
            {
                m_ttfClassifier << CNTRY_GB;
            }
            else
            {
                m_ttfClassifier << name;
                kdDebug(28000) << "ttf: Unknown country" << endl;
            }
        }
        else if( name.startsWith( "hand" ) )
        {
            TQString lang = name.mid(4,2);
            if( allCountries.contains(lang) )
            {
                TQString lngCountry = locale->twoAlphaToCountryName(lang);
                if( lngCountry.isEmpty() )
                    lngCountry = name;
                m_handClassifier << lngCountry;
            }
            else if( lang == "cz" )
            {
                m_handClassifier << i18n( "Czech Republic, Slovakia");
            }
            else if( lang == "us" )
            {
                m_handClassifier << i18n( "Great Britain, USA" );
            }
            else
            {
                kdDebug(28000) << "Hand: Unknown country " << lang << endl;
                m_handClassifier << name;
            }
        }
        else if( name.startsWith( "norm" ))
        {
            m_haveNorm = true;
        }

        kdDebug(28000) << "Found classifier: " << *it << endl;
        m_classifierPath << *it;
    }

    if( m_handClassifier.count()+m_ttfClassifier.count()>0 )
    {
        /* There are classifiers */
        return ENG_OK;
    }
    else
    {
        /* Classifier are missing */
        return ENG_DATA_MISSING;
    }
}


EngineError KadmosDialog::findClassifierPath()
{
    KStandardDirs stdDir;
    EngineError err = ENG_OK;

    TDEConfig *conf = TDEGlobal::config ();
    TDEConfigGroupSaver gs( conf, CFG_GROUP_KADMOS );

    m_customClassifierPath = conf->readPathEntry( CFG_KADMOS_CLASSIFIER_PATH );
#if 0
    if( m_customClassifierPath == "NotFound" )
    {
        /* Wants the classifiers from the standard kde paths */
       KMessageBox::error(0, i18n("The classifier files for KADMOS could not be found.\n"
				  "OCR with KADMOS will not be possible!\n\n"
				  "Change the OCR engine in the preferences dialog."),
			  i18n("Installation Error") );
    }
    else
    {
        m_classifierPath = customPath;
    }
#endif
    return err;

}


EngineError KadmosDialog::setupGui()
{

    EngineError err = KOCRBase::setupGui();

    // setupPreprocessing( addVBoxPage(   i18n("Preprocessing")));
    // setupSegmentation(  addVBoxPage(   i18n("Segmentation")));
    // setupClassification( addVBoxPage( i18n("Classification")));

    /* continue page setup on the first page */
    TQVBox *page = ocrPage();

    //Qt::Horizontal line
    (void) new KSeparator( KSeparator::HLine, page);

    // FIXME: dynamic classifier reading.

    (void) new TQLabel( i18n("Please classify the font type and language of the text on the image:"),
		       page );
    TQHBox *locBox = new TQHBox( page );
    m_bbFont = new TQButtonGroup(1, Qt::Horizontal, i18n("Font Type Selection"), locBox);

    m_rbMachine = new TQRadioButton( i18n("Machine print"), m_bbFont );
    m_rbHand    = new TQRadioButton( i18n("Hand writing"),  m_bbFont );
    m_rbNorm    = new TQRadioButton( i18n("Norm font"),     m_bbFont );

    m_gbLang = new TQGroupBox(1, Qt::Horizontal, i18n("Country"), locBox);


    m_cbLang = new TQComboBox( m_gbLang );
    m_cbLang->setCurrentText( KLocale::defaultCountry() );

    connect( m_bbFont, TQT_SIGNAL(clicked(int)), this, TQT_SLOT(slFontChanged(int) ));
    m_rbMachine->setChecked(true);

    /* --- */
    TQHBox *innerBox = new TQHBox( page );
    innerBox->setSpacing( KDialog::spacingHint());

    TQButtonGroup *cbGroup = new TQButtonGroup( 1, Qt::Horizontal, i18n("OCR Modifier"), innerBox );
    TQ_CHECK_PTR(cbGroup);

    m_cbNoise = new TQCheckBox( i18n( "Enable automatic noise reduction" ), cbGroup );
    m_cbAutoscale = new TQCheckBox( i18n( "Enable automatic scaling"), cbGroup );

    getAnimation(innerBox);
    // (void) new TQWidget ( page );

    if( err != ENG_OK )
    {
       enableFields(false);
       enableButton(User1, false );
    }

    if( m_ttfClassifier.count() == 0 )
    {
        m_rbMachine->setEnabled(false);
    }
    if( m_handClassifier.count() == 0 )
    {
        m_rbHand->setEnabled(false);
    }
    if( !m_haveNorm )
        m_rbNorm->setEnabled(false);

    if( (m_ttfClassifier.count() + m_handClassifier.count()) == 0 && ! m_haveNorm )
    {
        KMessageBox::error(0, i18n("The classifier files for KADMOS could not be found.\n"
                                   "OCR with KADMOS will not be possible!\n\n"
                                   "Change the OCR engine in the preferences dialog."),
                           i18n("Installation Error") );
        err = ENG_BAD_SETUP;
    }
    else
        slFontChanged( 0 ); // Load machine print font language list
    return err;
}

void KadmosDialog::slFontChanged( int id )
{
    m_cbLang->clear();

    TDEConfig *conf = TDEGlobal::config ();
    TDEConfigGroupSaver gs( conf, CFG_GROUP_KADMOS );



    m_customClassifierPath = conf->readPathEntry( CFG_KADMOS_CLASSIFIER_PATH );

    bool enable = true;

    if( id == 0 )  /* Machine Print */
    {
        m_cbLang->insertStringList( m_ttfClassifier );
    }
    else if( id == 1 ) /* Hand Writing */
    {
        m_cbLang->insertStringList( m_handClassifier );
    }
    else if( id == 2 ) /* Norm Font */
    {
        enable = false;
    }
    m_cbLang->setEnabled( enable );
}


void KadmosDialog::setupPreprocessing( TQVBox*  )
{

}

void KadmosDialog::setupSegmentation(  TQVBox* )
{

}

void KadmosDialog::setupClassification( TQVBox* )
{

}
/*
 * returns the complete path of the classifier selected in the
 * GUI in the parameter path. The result value indicates if there
 * was one found.
 */

bool KadmosDialog::getSelClassifier( TQString& path ) const
{
    TQString classifier = getSelClassifierName();

    TQString cmplPath;
    /*
     * Search the complete path for the classifier file name
     * returned from the getSelClassifierName method
     */
    for ( TQStringList::ConstIterator it = m_classifierPath.begin();
          it != m_classifierPath.end(); ++it )
    {
        TQFileInfo fi( *it );
        if( fi.fileName() == classifier )
        {
            cmplPath = *it;
            break;
        }
    }

    bool res = true;

    if( cmplPath.isEmpty() )
    {
        /* hm, no path was found */
	kdDebug(28000) << "ERR; The entire path is empty, joking?" << endl;
        res = false;
    }
    else
    {
        /* Check if the classifier exists on the HD. If not, return an empty string */
        TQFileInfo fi(cmplPath);

        if( res && ! fi.exists() )
        {
            kdDebug(28000) << "Classifier file does not exist" << endl;
            path = i18n("Classifier file %1 does not exist").arg(classifier);
            res = false;
        }

        if( res && ! fi.isReadable() )
        {
            kdDebug(28000) << "Classifier file could not be read" << endl;
            path = i18n("Classifier file %1 is not readable").arg(classifier);
            res = false;
        }

        if( res )
            path = cmplPath;
    }
    return res;
}

TQString KadmosDialog::getSelClassifierName() const
{
     TQButton *butt = m_bbFont->selected();

     TQString fType, rType;

     if( butt )
     {
	int fontTypeID = m_bbFont->id(butt);
	if( fontTypeID == 0 )
	   fType = "ttf";
	else if( fontTypeID == 1 )
	   fType = "hand";
	else if( fontTypeID == 2 )
	   fType = "norm";
	else
	   kdDebug(28000) << "ERR: Wrong Font Type ID" << endl;
     }

     /* Get the long text from the combo box */
     TQString selLang = m_cbLang->currentText();
     TQString trans;
     if( fType != "norm" && m_longCountry2short.contains( selLang ))
     {
         TQString langType = m_longCountry2short[selLang];
         trans = fType+langType+".rec";
     }
     else
     {
         if( selLang.endsWith( ".rec" ))
         {
             /* can be a undetected */
             trans = selLang;
         }
	 else if( fType == "norm" )
	 {
	     trans = "norm.rec";
	 }
         else
             kdDebug(28000) << "ERROR: Not a valid classifier" << endl;
     }
     kdDebug(28000) << "Returning trans. "<< trans << endl;
     return( trans );
}

bool KadmosDialog::getAutoScale()
{
    return( m_cbAutoscale ? m_cbAutoscale->isChecked() : false );
}

bool KadmosDialog::getNoiseReduction()
{
    return( m_cbNoise ? m_cbNoise->isChecked() : false );

}

KadmosDialog::~KadmosDialog()
{

}

void KadmosDialog::writeConfig( void )
{

}


void KadmosDialog::enableFields( bool state )
{
   kdDebug(28000) << "About to disable the entry fields" << endl;
   m_cbNoise->setEnabled( state );
   m_cbAutoscale->setEnabled( state );

   m_bbFont->setEnabled( state );
   m_gbLang->setEnabled( state );
}


/* The End ;) */