summaryrefslogtreecommitdiffstats
path: root/kooka/kookapref.cpp
blob: bc467bfde5e2fad35f0857d93bc37794bf88a683 (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
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
/***************************************************************************
                  kookapref.cpp  -  Kookas preferences dialog
                             -------------------
    begin                : Wed Jan 5 2000
    copyright            : (C) 2000 by Klaas Freitag
    email                :
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *  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 "kookapref.h"
#include "img_saver.h"

#include <klocale.h>
#include <kiconloader.h>
#include <kconfig.h>
#include <kdebug.h>
#include <knuminput.h>
#include <kcolorbutton.h>
#include <kstandarddirs.h>

#include <layout.h>
#include <tqtooltip.h>
#include <tqvgroupbox.h>
#include <tqgrid.h>
#include <tqcheckbox.h>
#include <tqstringlist.h>

#include <devselector.h>
#include "config.h"
#include "thumbview.h"
#include "imageselectline.h"
#include "kscanslider.h"
#include "ksaneocr.h"

#include <kmessagebox.h>
#include <tqbuttongroup.h>
#include <tqradiobutton.h>
#include <kurlrequester.h>

KookaPreferences::KookaPreferences()
    : KDialogBase(IconList, i18n("Preferences"),
                  Help|Default|Ok|Apply|Cancel, Ok )
{
    // this is the base class for your preferences dialog.  it is now
    // a Treelist dialog.. but there are a number of other
    // possibilities (including Tab, Swallow, and just Plain)
    konf = KGlobal::config ();

    setupStartupPage();
    setupSaveFormatPage();
    setupThumbnailPage();
    setupOCRPage();
}

void KookaPreferences::setupOCRPage()
{
    konf->setGroup( CFG_GROUP_OCR_DIA );

    TQFrame *page = addPage( i18n("OCR"), i18n("Optical Character Recognition" ),
			    BarIcon("ocrImage", KIcon::SizeMedium ) );

    TQVBoxLayout *top = new TQVBoxLayout( page, 0, spacingHint() );

    bool haveGocr = false;
    bool haveOcrad = false;
    bool haveKadmos = false;

    /*
     * Switch ocr engines
     */
    TQButtonGroup *engGroup = new TQButtonGroup( 1, Qt::Horizontal, i18n("OCR Engine to Use"), page );
    m_gocrBut   = new TQRadioButton( i18n("GOCR engine")  , engGroup );
    m_kadmosBut = new TQRadioButton( i18n("KADMOS engine"), engGroup );
    m_ocradBut  = new TQRadioButton( i18n("OCRAD engine"), engGroup );
    m_kadmosBut->setChecked(false);
    m_gocrBut->setChecked(false);
    m_ocradBut->setChecked(false);
    top->addWidget( engGroup );

    /*
     * GOCR Option Box
     */
    TQVGroupBox *gp = new TQVGroupBox( i18n("GOCR OCR"), page );
    m_urlReqGocr = binaryCheckBox( gp, "gocr" );
    connect( m_urlReqGocr, TQT_SIGNAL( textChanged( const TQString& )),
             this, TQT_SLOT( slCheckOnGOCR( const TQString& )));
    TQString cmdGocr = tryFindBinary( "gocr", CFG_GOCR_BINARY );
    kdDebug(28000) << "Found gocr command: " << cmdGocr << endl;
    m_gocrBut->setEnabled(false);
    if( !cmdGocr.isEmpty() )
    {
        /* Found the command */
        m_urlReqGocr->setURL( cmdGocr );
        m_gocrBut->setEnabled(true);
        haveGocr = true;
    }
    top->addWidget( gp );

    /*
     * OCRAD Option Box
     */
    gp = new TQVGroupBox( i18n("OCRAD OCR"), page );
    m_urlReqOcrad = binaryCheckBox( gp, "ocrad" );
    connect( m_urlReqOcrad, TQT_SIGNAL( textChanged( const TQString& )),
             this, TQT_SLOT( slCheckOnOCRAD( const TQString& )));
    TQString cmdOcrad = tryFindBinary( "ocrad", CFG_OCRAD_BINARY );
    kdDebug(28000) << "Found ocrad command: " << cmdOcrad << endl;
    m_ocradBut->setEnabled(false);
    if( !cmdOcrad.isEmpty() )
    {
        /* Found the command */
        m_urlReqOcrad->setURL( cmdOcrad );
        m_ocradBut->setEnabled(true);
        haveOcrad = true;
    }
    top->addWidget( gp );

    /*
     * Global Kadmos Options
     */
    TQVGroupBox *kgp = new TQVGroupBox( i18n("KADMOS OCR"), page );

#ifdef HAVE_KADMOS
    (void) new TQLabel( i18n("The KADMOS OCR engine is available"), kgp);
    m_kadmosBut->setChecked(true);
    m_kadmosBut->setEnabled(true);
    haveKadmos = true;
#else
    (void) new TQLabel( i18n("The KADMOS OCR engine is not available in this version of Kooka"), kgp );
    m_kadmosBut->setEnabled(false);
#endif
    top->addWidget( kgp );
    TQWidget *spaceEater = new TQWidget( page );
    spaceEater->setSizePolicy( TQSizePolicy( TQSizePolicy::Ignored, TQSizePolicy::Ignored ));
    top->addWidget( spaceEater );

    /*
     *  Now read the config value CFG_OCR_ENGINE and set the radios to the value if available
     */
    TQString useEngine = konf->readEntry( CFG_OCR_ENGINE, "ocrad" );
    if( useEngine != "notFound" )
    {
        if( useEngine == "gocr" && haveGocr )
	{
            m_gocrBut->setChecked(true);
	    m_prevOCREngine = "gocr";
	}
        else if( useEngine == "ocrad" && haveOcrad )
	{
            m_ocradBut->setChecked(true);
	    m_prevOCREngine = "ocrad";
	}
        else if( useEngine == "kadmos" && haveKadmos )
	{
            m_kadmosBut->setChecked(true);
	    m_prevOCREngine = "kadmos";
	}
    }
}

KURLRequester* KookaPreferences::binaryCheckBox( TQWidget *parent, const TQString& program )
{
    TQHBox *hbox = new TQHBox( parent );

    (void) new TQLabel( i18n("Select the %1 binary to use:").arg( program ), hbox );
    KURLRequester* urlRequester = new KURLRequester( parent );
    urlRequester->setMode( KFile::File | KFile::ExistingOnly | KFile::LocalOnly );

    TQToolTip::add( urlRequester,
                   i18n( "Enter the path to %1, the optical-character-recognition "
			 "command line tool.").arg(program));
    return urlRequester;
}


TQString KookaPreferences::tryFindGocr( void )
{
   return( tryFindBinary( "gocr", CFG_GOCR_BINARY ) );
}

TQString KookaPreferences::tryFindBinary( const TQString& bin, const TQString& configKey )
{

    /* First check the config files for an entry */
    KConfig *cfg = KGlobal::config();
    cfg->setGroup(CFG_GROUP_OCR_DIA);
    TQString res = cfg->readPathEntry( configKey /* CFG_GOCR_BINARY */, "notFound" );

    if( res != "notFound" )
    {
        TQFileInfo fi( res );
        if( fi.exists() && fi.isExecutable() && !fi.isDir() && res.contains(bin) )
        {
            return res;
        }
    }

    res = TQString();

    TQStringList locations;
    locations.append( "/usr/bin/" + bin );
    locations.append( "/bin/" + bin );
    locations.append( "/usr/X11R6/bin/"+bin );
    locations.append( "/usr/local/bin/"+bin );

    for ( TQStringList::Iterator it = locations.begin(); it != locations.end(); ++it )
    {
        TQString cmd = *it;
        kdDebug(28000) << "checking command " << cmd << endl;
        TQFileInfo fi( cmd );
        if( fi.exists() && fi.isExecutable() && !fi.isDir())
        {
            res  = cmd;
            kdDebug(28000) << "found  command " << res << endl;
            break;
        }
    }

    return( res );
}


void KookaPreferences::slCheckOnGOCR( const TQString& cmd )
{
    if( checkOCRBinIntern( cmd, "gocr", false ))
    {
        // cmd exists and is executable
        m_gocrBut->setEnabled( true );
    }
    else
    {
        m_gocrBut->setEnabled( false );
    }
}

void KookaPreferences::slCheckOnOCRAD( const TQString& cmd )
{
    if( checkOCRBinIntern( cmd, "ocrad", false ))
    {
        // cmd exists and is executable
        m_ocradBut->setEnabled( true );
    }
    else
    {
        m_ocradBut->setEnabled( false );
    }
}

#if 0
void KookaPreferences::checkOCRBinarySilent( const TQString& cmd )
{
    // checkOCRBinIntern( cmd, this->sender(), false);
}
#endif
bool KookaPreferences::checkOCRBinIntern( const TQString& cmd, const TQString& tool, bool show_msg )
{
   if( ! cmd.contains( tool )) return false;

   bool ret = true;
   TQFileInfo fi( cmd );
   if( ! fi.exists() )
   {
      if( show_msg )
      KMessageBox::sorry( this, i18n( "The path does not lead to a valid binary.\n"
				      "Please check your installation and/or install the program."),
			  i18n("OCR Software Not Found") );
      ret = false;
   }
   else
   {
      /* File exists, check if not dir and executable */
      if( fi.isDir() || (! fi.isExecutable()) )
      {
	 if( show_msg )
	    KMessageBox::sorry( this, i18n( "The program exists, but is not executable.\n"
					    "Please check your installation and/or install the binary properly."),
				i18n("OCR Software Not Executable") );
	 ret = false;
      }
   }

   return ret;
}



void KookaPreferences::setupStartupPage()
{

    /* startup options */
    konf->setGroup( GROUP_STARTUP );

    TQFrame *page = addPage( i18n("Startup"), i18n("Kooka Startup Preferences" ),
			    BarIcon("gear", KIcon::SizeMedium ) );
    TQVBoxLayout *top = new TQVBoxLayout( page, 0, spacingHint() );
    /* Description-Label */
    top->addWidget( new TQLabel( i18n("Note that changing these options will affect Kooka's next start!"), page ));

    /* Query for network scanner (Checkbox) */
    cbNetQuery = new TQCheckBox( i18n("Query network for available scanners"),
				page,  "CB_NET_TQUERY" );
    TQToolTip::add( cbNetQuery,
		   i18n( "Check this if you want a network query for available scanners.\nNote that this does not mean a query over the entire network but only the stations configured for SANE!" ));
    cbNetQuery->setChecked( ! (konf->readBoolEntry( STARTUP_ONLY_LOCAL, false )) );


    /* Show scanner selection box on startup (Checkbox) */
    cbShowScannerSelection = new TQCheckBox( i18n("Show the scanner selection box on next startup"),
					    page,  "CB_SHOW_SELECTION" );
    TQToolTip::add( cbShowScannerSelection,
		   i18n( "Check this if you once checked 'do not show the scanner selection on startup',\nbut you want to see it again." ));

    cbShowScannerSelection->setChecked( !konf->readBoolEntry( STARTUP_SKIP_ASK, false ));

    /* Read startup image on startup (Checkbox) */
    cbReadStartupImage = new TQCheckBox( i18n("Load the last image into the viewer on startup"),
					    page,  "CB_LOAD_ON_START" );
    TQToolTip::add( cbReadStartupImage,
		   i18n( "Check this if you want Kooka to load the last selected image into the viewer on startup.\nIf your images are large, that might slow down Kooka's start." ));
    cbReadStartupImage->setChecked( konf->readBoolEntry( STARTUP_READ_IMAGE, true));

    /* -- */

    top->addWidget( cbNetQuery );
    top->addWidget( cbShowScannerSelection );
    top->addWidget( cbReadStartupImage );

    top->addStretch(10);

}

void KookaPreferences::setupSaveFormatPage( )
{
   konf->setGroup( OP_FILE_GROUP );
   TQFrame *page = addPage( i18n("Image Saving"), i18n("Configure Image Save Assistant" ),
			    BarIcon("filesave", KIcon::SizeMedium ) );
   TQVBoxLayout *top = new TQVBoxLayout( page, 0, spacingHint() );

   /* Skip the format asking if a format entry  exists */
   cbSkipFormatAsk = new TQCheckBox( i18n("Always display image save assistant"),
				     page,  "CB_IMGASSIST_TQUERY" );
   cbSkipFormatAsk->setChecked( konf->readBoolEntry( OP_FILE_ASK_FORMAT, true  ));
   TQToolTip::add( cbSkipFormatAsk, i18n("Check this if you want to see the image save assistant even if there is a default format for the image type." ));
   top->addWidget( cbSkipFormatAsk );

   cbFilenameAsk = new TQCheckBox( i18n("Ask for filename when saving file"),
                    page,  "CB_ASK_FILENAME" );
   cbFilenameAsk->setChecked( konf->readBoolEntry( OP_ASK_FILENAME, false));
   TQToolTip::add( cbFilenameAsk, i18n("Check this if you want to enter a filename when an image has been scanned." ));
   top->addWidget( cbFilenameAsk );



   top->addStretch(10);
}

void KookaPreferences::setupThumbnailPage()
{
   konf->setGroup( THUMB_GROUP );

   TQFrame *page = addPage( i18n("Thumbnail View"), i18n("Thumbnail Gallery View" ),
			    BarIcon("thumbnail", KIcon::SizeMedium ) );
   TQVBoxLayout *top = new TQVBoxLayout( page, 0, spacingHint() );

   top->addWidget( new TQLabel( i18n("Here you can configure the appearance of the thumbnail view of your scan picture gallery."),page ));

   /* Backgroundimage */
   KStandardDirs stdDir;
   TQString bgImg = konf->readPathEntry( BG_WALLPAPER );
   if( bgImg.isEmpty() )
      bgImg = stdDir.findResource( "data", STD_TILE_IMG );

   /* image file selector */
   TQVGroupBox *hgb1 = new TQVGroupBox( i18n("Thumbview Background" ), page );
   m_tileSelector = new ImageSelectLine( hgb1, i18n("Select background image:"));
   kdDebug(28000) << "Setting tile url " << bgImg << endl;
   m_tileSelector->setURL( KURL(bgImg) );

   top->addWidget( hgb1 );

   /* Add the Boxes to configure size, framestyle and background */
   TQVGroupBox *hgb2 = new TQVGroupBox( i18n("Thumbnail Size" ), page );
   TQVGroupBox *hgb3 = new TQVGroupBox( i18n("Thumbnail Frame" ), page );

   /* Thumbnail size */
   int w = konf->readNumEntry( PIXMAP_WIDTH, 100);
   int h = konf->readNumEntry( PIXMAP_HEIGHT, 120 );
   TQGrid *lGrid = new TQGrid( 2, hgb2 );
   lGrid->setSpacing( 2 );
   TQLabel *l1 = new TQLabel( i18n("Thumbnail maximum &width:"), lGrid );
   m_thumbWidth = new KIntNumInput( w, lGrid );
   m_thumbWidth->setMinValue(1);
   l1->setBuddy( m_thumbWidth );

   lGrid->setSpacing( 4 );
   l1 = new TQLabel( i18n("Thumbnail maximum &height:"), lGrid );
   m_thumbHeight = new KIntNumInput( m_thumbWidth, h, lGrid );
   m_thumbHeight->setMinValue(1);
   l1->setBuddy( m_thumbHeight );

   /* Frame Stuff */
   int frameWidth = konf->readNumEntry( THUMB_MARGIN, 3 );
   TQColor col1    = konf->readColorEntry( MARGIN_COLOR1, &(colorGroup().base()));
   TQColor col2    = konf->readColorEntry( MARGIN_COLOR2, &(colorGroup().foreground()));

   TQGrid *fGrid = new TQGrid( 2, hgb3 );
   fGrid->setSpacing( 2 );
   l1 = new TQLabel(i18n("Thumbnail &frame width:"), fGrid );
   m_frameWidth = new KIntNumInput( frameWidth, fGrid );
   m_frameWidth->setMinValue(0);
   l1->setBuddy( m_frameWidth );

   l1 = new TQLabel(i18n("Frame color &1: "), fGrid );
   m_colButt1 = new KColorButton( col1, fGrid );
   l1->setBuddy( m_colButt1 );

   l1 = new TQLabel(i18n("Frame color &2: "), fGrid );
   m_colButt2 = new KColorButton( col2, fGrid );
   l1->setBuddy( m_colButt2 );
   /* TODO: Gradient type */

   top->addWidget( hgb2, 10);
   top->addWidget( hgb3, 10);
   top->addStretch(10);

}


void KookaPreferences::slotOk( void )
{
  slotApply();
  accept();

}


void KookaPreferences::slotApply( void )
{
    /* ** startup options ** */

   /** write the global one, to read from libkscan also */
   konf->setGroup(TQString::fromLatin1(GROUP_STARTUP));
   bool cbVal = !(cbShowScannerSelection->isChecked());
   kdDebug(28000) << "Writing for " << STARTUP_SKIP_ASK << ": " << cbVal << endl;
   konf->writeEntry( STARTUP_SKIP_ASK, cbVal, true, true ); /* global flag goes to kdeglobals */

   /* only search for local (=non-net) scanners ? */
   konf->writeEntry( STARTUP_ONLY_LOCAL,  !cbNetQuery->isChecked(), true, true ); /* global */

   /* Should kooka open the last displayed image in the viewer ? */
   if( cbReadStartupImage )
      konf->writeEntry( STARTUP_READ_IMAGE, cbReadStartupImage->isChecked());

    /* ** Image saver option(s) ** */
    konf->setGroup( OP_FILE_GROUP );
    bool showFormatAssist = cbSkipFormatAsk->isChecked();
    konf->writeEntry( OP_FILE_ASK_FORMAT, showFormatAssist );
    konf->writeEntry( OP_ASK_FILENAME, cbFilenameAsk->isChecked() );

    /* ** Thumbnail options ** */
    konf->setGroup( THUMB_GROUP );
    konf->writeEntry( PIXMAP_WIDTH, m_thumbWidth->value() );
    konf->writeEntry( PIXMAP_HEIGHT, m_thumbHeight->value() );
    konf->writeEntry( THUMB_MARGIN, m_frameWidth->value() );
    konf->writeEntry( MARGIN_COLOR1, m_colButt1->color());
    konf->writeEntry( MARGIN_COLOR2, m_colButt2->color());

    KURL bgUrl = m_tileSelector->selectedURL().url();
    bgUrl.setProtocol("");
    kdDebug(28000) << "Writing tile-pixmap " << bgUrl.prettyURL() << endl;
    konf->writePathEntry( BG_WALLPAPER, bgUrl.url() );

    /* ** OCR Options ** */
    konf->setGroup( CFG_GROUP_OCR_DIA );
    TQString eng( "gocr" );

    if( m_ocradBut->isChecked() )
        eng = "ocrad";

    if( m_kadmosBut && m_kadmosBut->isChecked() )
        eng = "kadmos";

    if( eng != m_prevOCREngine )
    {
        // selection of the ocr engine has changed. Popup button.
        KMessageBox::sorry( this, i18n( "The OCR engine settings were changed.\n"
                                        "Note that Kooka needs to be restarted to change the OCR engine."),
                            i18n("OCR Engine Change") );
    }

    konf->writeEntry(CFG_OCR_ENGINE, eng );

    TQString path = m_urlReqGocr->url();
    if( ! path.isEmpty() )
        konf->writePathEntry( CFG_GOCR_BINARY, path );

    path = m_urlReqOcrad->url();
    if( ! path.isEmpty() )
        konf->writePathEntry( CFG_OCRAD_BINARY, path );

    konf->sync();

    emit dataSaved();
}

void KookaPreferences::slotDefault( void )
{
    cbNetQuery->setChecked( true );
    cbShowScannerSelection->setChecked( true);
    cbReadStartupImage->setChecked( true);
    cbSkipFormatAsk->setChecked( true  );
    KStandardDirs stdDir;
    TQString bgImg = stdDir.findResource( "data", STD_TILE_IMG );
    m_tileSelector->setURL( KURL(bgImg) );
    m_thumbWidth->setValue( 100 );
    m_thumbHeight->setValue( 120 );
    TQColor col1    = TQColor( colorGroup().base());
    TQColor col2    = TQColor( colorGroup().foreground());

    m_frameWidth->setValue( 3 );
    m_colButt1->setColor( col1 );
    m_colButt2->setColor( col2 );
    m_gocrBut->setChecked(true);
}



#include "kookapref.moc"