summaryrefslogtreecommitdiffstats
path: root/kooka/kocrbase.cpp
blob: 0de5c88c9ccdc5a8db981c13fa4f29daf8fb02e3 (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
/***************************************************************************
                     kocrbase.cpp - base dialog for ocr
                             -------------------
    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 <kio/job.h>
#include <kio/previewjob.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 <kactivelabel.h>
#include <tqhbox.h>
#include <tqvbox.h>

#include "resource.h"
#include "kocrbase.h"
#include "ksaneocr.h"
#include "kookaimage.h"

#include <kscanslider.h>
#include <kstandarddirs.h>
#include <kfilemetainfo.h>
#include <ksconfig.h>
#include <tqstringlist.h>
#include <tqcolor.h>
#include <tqgrid.h>
#include <tqsizepolicy.h>
#include <tqgroupbox.h>
#include <tqcheckbox.h>

KOCRBase::KOCRBase( TQWidget *parent, KSpellConfig *spellConfig,
                    KDialogBase::DialogType face )
   :KDialogBase( face, i18n("Optical Character Recognition"),
		 User2|Close|User1, User1, parent,0, false, true,
		 KGuiItem( i18n("Start OCR" ), "launch",
			   i18n("Start the Optical Character Recognition process" )),
                 KGuiItem( i18n("Cancel" ), "stopocr",
			   i18n("Stop the OCR Process" ))),
    m_animation(0L),
    m_metaBox(0L),
    m_imgHBox(0L),
    m_previewPix(0L),
    m_currImg(0L),
    m_spellConfig(spellConfig),
    m_wantSpellCfg(true),
    m_userWantsSpellCheck(true),
    m_cbWantCheck(0L),
    m_gbSpellOpts(0L)
{
    kdDebug(28000) << "OCR Base Dialog!" << endl;
    // Layout-Boxes

    KConfig *konf = KGlobal::config ();
    KConfigGroupSaver gs( konf, CFG_OCR_KSPELL );
    m_userWantsSpellCheck = konf->readBoolEntry(CFG_WANT_KSPELL, true);

    /* Connect signals which disable the fields and store the configuration */
    connect( this, TQT_SIGNAL( user1Clicked()), this, TQT_SLOT( writeConfig()));
    connect( this, TQT_SIGNAL( user1Clicked()), this, TQT_SLOT( startOCR() ));
    connect( this, TQT_SIGNAL( user2Clicked()), this, TQT_SLOT( stopOCR() ));
    m_previewSize.setWidth(200);
    m_previewSize.setHeight(300);

    enableButton( User1, true );   /* start ocr */
    enableButton( User2, false );  /* Cancel    */
    enableButton( Close, true );
}


KAnimWidget* KOCRBase::getAnimation(TQWidget *parent)
{
   if( ! m_animation )
   {
      m_animation = new KAnimWidget( TQString("kde"), 48, parent, "ANIMATION" );
   }
   return( m_animation );
}

EngineError KOCRBase::setupGui()
{
    ocrIntro();
    imgIntro();
    if( m_wantSpellCfg ) spellCheckIntro();

    return ENG_OK;
}

void KOCRBase::imgIntro()
{
    m_imgPage = addVBoxPage( i18n("Image") );
    (void) new TQLabel( i18n("Image Information"), m_imgPage );

    // Caption - Label and image
    m_imgHBox = new TQHBox( m_imgPage );

    m_imgHBox->setSpacing( KDialog::spacingHint());

    m_previewPix = new TQLabel( m_imgHBox );
    m_previewPix->setPixmap(TQPixmap());
    m_previewPix->setFixedSize(m_previewSize);
    m_previewPix->setAlignment( TQt::AlignCenter );
    m_previewPix->setFrameStyle( TQFrame::Panel | TQFrame::Sunken );
    // m_previewPix->resize(m_previewSize);

    /* See introduceImage where the meta box is filled with data from the
     * incoming widget.
     */
    m_metaBox = new TQVBox( m_imgHBox );
}

/*
 * This creates a Tab OCR
 */
void KOCRBase::ocrIntro( )
{
    m_ocrPage = addVBoxPage( i18n("OCR") );

    // Caption - Label and image
    /* labelstring */
    (void) new TQLabel( i18n("<b>Starting Optical Character Recognition with %1</b><p>").
                       arg( ocrEngineName() ), m_ocrPage );
    // Find the kadmos logo and display if available
    KStandardDirs stdDir;
    TQString logo = stdDir.findResource( "data", "kooka/pics/" + ocrEngineLogo() );

    kdDebug(28000)<< "Reading logo " << logo << endl;
    TQPixmap pix;
    TQWidget *pa = m_ocrPage;

    if( pix.load( logo ))
    {
        TQHBox *hb_cap = new TQHBox( m_ocrPage );
        hb_cap->setSpacing( KDialog::spacingHint());

        TQLabel *imgLab = new TQLabel( hb_cap );
        imgLab->setAlignment( TQt::AlignHCenter | TQt::AlignTop  );
        imgLab->setPixmap( pix );
        pa = hb_cap;
    }

    (void) new KActiveLabel( ocrEngineDesc(), pa );
}


void KOCRBase::spellCheckIntro()
{
    m_spellchkPage = addVBoxPage( i18n("Spell-checking") );

    /* Want the spell checking at all? Checkbox here */
    TQGroupBox *gb1 = new TQGroupBox( 1, Qt::Horizontal, i18n("OCR Post Processing"), m_spellchkPage );
    m_cbWantCheck = new TQCheckBox( i18n("Enable spell-checking for validation of the OCR result"),
                                   gb1 );
    /* Spellcheck options */
    m_gbSpellOpts = new TQGroupBox( 1, Qt::Horizontal, i18n("Spell-Check Options"),
                                   m_spellchkPage );

    KSpellConfig *sCfg = new KSpellConfig( m_gbSpellOpts, "SPELLCHK", m_spellConfig, false );
    /* A space eater */
    TQWidget *spaceEater = new TQWidget(m_spellchkPage);
    spaceEater->setSizePolicy( TQSizePolicy( TQSizePolicy::Ignored, TQSizePolicy::Ignored ));

    /* connect toggle button */
    connect( m_cbWantCheck, TQT_SIGNAL(toggled(bool)), this, TQT_SLOT(slWantSpellcheck(bool)));
    m_cbWantCheck->setChecked( m_userWantsSpellCheck );
    m_gbSpellOpts->setEnabled( m_userWantsSpellCheck );
    m_spellConfig = sCfg;

    connect( sCfg, TQT_SIGNAL(configChanged()),
            this, TQT_SLOT(slSpellConfigChanged()));
}

void KOCRBase::slSpellConfigChanged()
{
    kdDebug(28000) << "Spellcheck config changed" << endl;
}



void KOCRBase::stopAnimation()
{
   if( m_animation )
      m_animation->stop();
}

void KOCRBase::startAnimation()
{
   if( m_animation )
      m_animation->start();
}

KOCRBase::~KOCRBase()
{

}

void KOCRBase::introduceImage( KookaImage* img)
{
    if( ! (img && img->isFileBound()) ) return;
    KFileMetaInfo info = img->fileMetaInfo();
    TQStringList groups;
    if ( info.isValid() )
         groups = info.preferredGroups();

    delete m_metaBox;
    m_metaBox = new TQVBox( m_imgHBox );

    /* Start to create a preview job for the thumb */
    KURL::List li(img->url());
    KIO::Job *m_job = KIO::filePreview(li, m_previewSize.width(),
                                       m_previewSize.height());

    if( m_job )
    {
        connect( m_job, TQT_SIGNAL( result( KIO::Job * )),
                 this, TQT_SLOT( slPreviewResult( KIO::Job * )));
        connect( m_job, TQT_SIGNAL( gotPreview( const KFileItem*, const TQPixmap& )),
                 TQT_SLOT( slGotPreview( const KFileItem*, const TQPixmap& ) ));
         /* KIO::Jo result is called in any way: Success, Failed, Error,
          * thus connecting the failed is not really necessary.
          */
    }

    for ( TQStringList::Iterator it = groups.begin(); it != groups.end(); ++it )
    {
        TQString theGroup(*it);

        kdDebug(29000) << "handling the group " << theGroup << endl;

        TQStringList keys = info.group(theGroup).supportedKeys();

        if( keys.count() > 0 )
        {
            // info.groupInfo( theGroup )->translatedName()
            // FIXME: howto get the translated group name?
            TQLabel *lGroup = new TQLabel( theGroup, m_metaBox );
            lGroup->setBackgroundColor( TQColor(gray));
            lGroup->setMargin( KDialog::spacingHint());

            TQGrid *nGrid = new TQGrid( 2,  m_metaBox );
            nGrid->setSpacing( KDialog::spacingHint());
            for ( TQStringList::Iterator keyIt = keys.begin(); keyIt != keys.end(); ++keyIt )
            {
                KFileMetaInfoItem item = info.item(*keyIt);
                TQString itKey = item.translatedKey();
                if( itKey.isEmpty() )
                    itKey = item.key();
                if( ! itKey.isEmpty() )
                {
                    (void) new TQLabel( item.translatedKey() + ": ", nGrid );
                    (void) new TQLabel( item.string(), nGrid );
                    kdDebug(29000) << "hasKey " << *keyIt << endl;
                }
            }
        }
    }
    TQWidget *spaceEater = new TQWidget( m_metaBox );
    spaceEater->setSizePolicy( TQSizePolicy( TQSizePolicy::Ignored, TQSizePolicy::Ignored ));
    m_metaBox->show();
}

void KOCRBase::slPreviewResult(KIO::Job *job )
{
    // nothing
    if( job && job->error() > 0 )
   {
      kdDebug(28000) << "Thumbnail Creation ERROR: " << job->errorString() << endl;
      job->showErrorDialog( 0 );
   }
}

void KOCRBase::slGotPreview( const KFileItem*, const TQPixmap& newPix )
{
    kdDebug(28000) << "Got the preview" << endl;
    m_previewPix->setPixmap(newPix);

    if( m_previewPix && m_currImg )
    {
        m_previewPix->setPixmap(newPix);
    }
}


void KOCRBase::writeConfig()
{

}

bool KOCRBase::wantSpellCheck()
{
    return m_userWantsSpellCheck;
}

void KOCRBase::startOCR()
{
    /* en- and disable the buttons */
    kdDebug(28000) << "Base: Starting OCR" << endl;

    enableFields(false);
    enableButton( User1, false );   /* Start OCR */
    enableButton( User2, true );    /* Stop OCR */
    enableButton( Close, true );

    startAnimation();
}

void KOCRBase::stopOCR()
{
    enableFields(true);

    enableButton( User1, true );   /* start ocr */
    enableButton( User2, false );  /* Cancel    */
    enableButton( Close, true );

    stopAnimation();

}

void KOCRBase::enableFields(bool)
{

}

void KOCRBase::slWantSpellcheck( bool wantIt )
{
    if( m_gbSpellOpts )
    {
        m_gbSpellOpts->setEnabled( wantIt );
    }
    m_userWantsSpellCheck = wantIt;

    KConfig *konf = KGlobal::config ();
    KConfigGroupSaver gs( konf, CFG_OCR_KSPELL );
    konf->writeEntry( CFG_WANT_KSPELL, wantIt );
}

/* The End ;) */
#include "kocrbase.moc"