-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainWindow.cpp
414 lines (333 loc) · 12.1 KB
/
mainWindow.cpp
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
/***************************************************************************
* Copyright (с) 2009 by Andrey Cherepanov <[email protected]> *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 3 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include "mainWindow.h"
#include <QFileDialog>
#include <QGraphicsPixmapItem>
MainWindow::MainWindow( QStringList *names )
: QMainWindow( 0, 0 )
{
// Set files list
files = names;
// Set initial values
if( names->count() > 0 )
sourceImageFile = names->at( 0 ); // Use first passed file name
else
sourceImageFile.clear();
// Create configuration dialog
config = new Settings( this );
// Prepare temporary file for result
QTemporaryFile tempFile;
tempFile.setFileTemplate( QDir::tempPath() + QDir::separator() + QString( "cuneiform-result-XXXXXX.html" ) );
tempFile.open();
resultFile = tempFile.fileName();
tempFile.close();
config->setOutputFile( resultFile );
// Set main window content
form = new Ui_MainWindow();
form->setupUi( this );
// Set scene
form->source->setScene( &scene );
// Set up process
process = new Backend( config );
connect( process, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(onProcessFinish()) );
connect( process, SIGNAL(readyReadStandardOutput()), this, SLOT(onProcessHasOutput()) );
connect( process, SIGNAL(readyReadStandardError()), this, SLOT(onProcessHasErrors()) );
// Set actions
// TODO Disable action if image is not loaded
// Open image
connect( form->actionOpen, SIGNAL(triggered()), this, SLOT(OpenImage()) );
// Recognize text
connect( form->actionRecognizeText, SIGNAL(triggered()), this, SLOT(RecognizeText()) );
// Save result text
connect( form->actionSave, SIGNAL(triggered()), this, SLOT(SaveText()) );
// Exit. TODO confirm exit without saving recognized text
connect( form->actionExit, SIGNAL(triggered()), this, SLOT(onWindowClose()) );
// Open configuration dialog
connect( form->actionConfigure, SIGNAL(triggered()), config, SLOT(exec()) );
connect( config, SIGNAL(accepted()), this, SLOT(onSettingsSave()) );
connect( config, SIGNAL(rejected()), this, SLOT(onSettingsCancel()) );
// About dialog
connect( form->actionAbout, SIGNAL(triggered()), this, SLOT(About()) );
// On window close
connect( this, SIGNAL(destroyed()), this, SLOT(onWindowClose()) );
// Process progress dialog
progressDialog = new QMessageBox( this );
progressDialog->setModal( true );
progressDialog->setWindowTitle( tr("Process") );
progressDialog->setText( tr("Recognition is in progress.\nPlease, wait...") );
progressDialog->addButton( QMessageBox::Cancel );
connect( progressDialog, SIGNAL(rejected()), this, SLOT(onProcessStop()) );
// Set render text action disabled
form->actionRecognizeText->setDisabled( true );
// Render specified image file
if( ! sourceImageFile.isEmpty() ) {
renderImage();
}
}
// Main window desctructor
MainWindow::~MainWindow()
{
emit onWindowClose();
}
// On main window close
void MainWindow::onWindowClose() {
//qDebug() << "onWindowClose():";
// Stop recognition process
if ( process->state() == QProcess::Running) {
process->terminate();
process->waitForFinished(3000);
}
close();
}
// Select image file
void MainWindow::OpenImage() {
// Open standart open dialog
QFileDialog dialog( this );
dialog.setNameFilter( tr("Images (*.png *.jpg *.jpeg *.bmp *.tif *.tiff)") + QString( ";;" ) + tr("All files (*)") );
dialog.setWindowTitle( tr("Open image") );
dialog.setAcceptMode( QFileDialog::AcceptOpen );
QStringList fileNames;
if (dialog.exec())
fileNames = dialog.selectedFiles();
if( fileNames.count() > 0 ) {
sourceImageFile = fileNames.at( 0 );
//QMessageBox::information( this, tr("Opened file"), sourceImageFile, QMessageBox::Ok );
}
// Render image
if( ! sourceImageFile.isEmpty() ) {
renderImage();
}
}
// Recognize text from current opened image
void MainWindow::RecognizeText() {
// Run recognition process
emit onProcessStart();
}
// Save recognized text
void MainWindow::SaveText() {
int ret=1;
form->statusBar->showMessage( tr("Saving result file...") );
// Open standart save dialog
QString savedFile( "" );
QFileDialog dialog( this );
// Different extensions for different formats
QString defExt;
QString defFilter;
if( config->getFormat() == QString( "html" ) || config->getFormat() == QString( "hocr" ) ) {
defFilter = tr("HTML documents (*.htm *.html)");
defExt = QString( "html" );
} else {
if( config->getFormat() == QString( "rtf" ) ) {
defFilter = tr("RTF documents (*.rtf)");
defExt = QString( "rtf" );
} else {
defFilter = tr("Text files (*.txt)");
defExt = QString( "txt" );
}
}
dialog.setNameFilter( defFilter + QString( ";;" ) + tr("All files (*)") );
dialog.setDefaultSuffix( defExt );
dialog.setWindowTitle( tr("Save result") );
dialog.setAcceptMode( QFileDialog::AcceptSave );
dialog.setConfirmOverwrite( false );
QStringList fileNames;
if ( dialog.exec() )
fileNames = dialog.selectedFiles();
if( fileNames.count() > 0 ) {
savedFile = fileNames.at( 0 );
}
//qDebug() << qPrintable( tr("Saved file: %1 (%2)").arg( savedFile ).arg( savedFile.isEmpty() ) );
if( savedFile.isEmpty() ) {
form->statusBar->showMessage( QString( "" ) );
return;
}
// Check for existance anf confirm overwrite if needed
QFile f( savedFile );
if( f.exists() ) {
ret = QMessageBox::question( this, tr("Overwrite file?"),
tr("File with name '%1' is exist.\n"
"Do you really overwrite this file?").arg( f.fileName() ),
QMessageBox::Yes | QMessageBox::No );
if( ret == QMessageBox::Yes ) {
f.remove();
} else {
form->statusBar->showMessage( QString( "" ) );
ret = 0;
}
}
// Really save to specified file
if( ret ) {
f.open( QFile::WriteOnly | QFile::Truncate );
// Check for open error
if( f.error() ) {
QMessageBox::critical( this, tr("Error open"),
tr("Unable to open file '%1' for save.").arg( f.fileName() ),
QMessageBox::Ok );
form->statusBar->showMessage( tr("Unable to open file '%1' for save.").arg( f.fileName() ) );
return;
}
// Get content from the result field
QString content;
if( config->getFormat() == QString( "html" ) || config->getFormat() == QString( "hocr" ) ) {
content = form->result->toHtml();
} else {
content = form->result->toPlainText();
}
// Write to file
QTextStream stream( &f );
// Set UTF-8 codec for write stream
stream.setCodec( QTextCodec::codecForName( "UTF-8" ) );
stream << content;
// Check for write error
if( f.error() ) {
QMessageBox::critical( this, tr("Error save"),
tr("Unable to save file '%1'.").arg( f.fileName() ),
QMessageBox::Ok );
form->statusBar->showMessage( tr("Unable to save file '%1'.").arg( f.fileName() ) );
return;
}
f.close();
form->statusBar->showMessage( tr("File '%1' is saved successful.").arg( f.fileName() ) );
}
}
// Render specified image
void MainWindow::renderImage() {
QPixmap img;
QGraphicsPixmapItem *page;
//qDebug() << "Load image" << qPrintable( sourceImageFile );
scene.clear();
// Load image
form->statusBar->showMessage( tr("Loading image '%1'...").arg( sourceImageFile) );
if( ! img.load( sourceImageFile ) ) {
form->statusBar->showMessage( tr("Unable to open file '%1'.").arg( sourceImageFile) );
QMessageBox::warning( this, tr("Error open file"),
tr("Unable to open file '%1'.").arg( sourceImageFile ),
QMessageBox::Ok );
return;
}
page = scene.addPixmap( img );
//page = scene.addPixmap( QPixmap("icons/tool-open.png") );
page->setOffset( 5, 5 );
// Show image
form->source->show();
form->statusBar->showMessage( tr("Image '%1' is loaded.").arg( sourceImageFile ) );
// Set render text action enabled
form->actionRecognizeText->setDisabled( false );
config->setInputFile( sourceImageFile );
}
// On process start
void MainWindow::onProcessStart() {
if( sourceImageFile.isEmpty() ) {
QMessageBox::warning( this, tr("Image is not loaded"),
tr("Image is not loaded. Please, load scanned image."),
QMessageBox::Ok );
return;
}
if( ! process->onStart() ) {
QMessageBox::critical( this, tr("Error start"),
tr("Could not start Cuneiform executables. Check your installation.") );
return;
}
form->statusBar->showMessage( tr("Recognition is processing...") );
form->result->clear();
progressDialog->exec();
}
// On process finish
void MainWindow::onProcessFinish() {
QString err;
process->onFinish();
// Process result
err = process->getError();
if( ! err.isEmpty() ) {
// Show error message
progressDialog->hide();
QString errText = tr("Cuneiform error:\n%1").arg( err ); // TODO parse and localize error description
form->statusBar->showMessage( errText );
QMessageBox::warning( this, tr("Error recognition"),
errText,
QMessageBox::Ok );
return;
} else {
// Show result
// TODO Discern formats
form->result->clear();
QFile f( resultFile );
f.open( QIODevice::ReadOnly );
QTextStream stream( &f );
// Set UTF-8 codec for read stream
stream.setCodec( QTextCodec::codecForName( "UTF-8" ) );
QString content = stream.readAll();
f.close();
f.remove();
if( config->getFormat() == QString( "html" ) || config->getFormat() == QString( "hocr" ) ) {
form->result->setHtml( content );
} else {
form->result->setPlainText( content );
}
}
progressDialog->hide();
form->statusBar->showMessage( tr("Recognition is complete.") );
}
// Stop process
void MainWindow::onProcessStop() {
process->kill();
progressDialog->hide();
form->statusBar->showMessage( tr("Recognition is canceled by user.") );
}
// On data in stdout
void MainWindow::onProcessHasOutput() {
process->onReadOutput();
}
// On data in stderr
void MainWindow::onProcessHasErrors() {
process->onReadErrors();
}
// Save settings
void MainWindow::onSettingsSave() {
config->save();
}
// Save settings
void MainWindow::onSettingsCancel() {
config->load();
}
// Show dialog about application
// TODO set big application icon
// TODO show cuneiform version
void MainWindow::About() {
QMessageBox aboutDialog( this );
aboutDialog.setIconPixmap( QPixmap(QString::fromUtf8(":/small/icons/cuneiform-qt.png")) );
aboutDialog.setWindowTitle( tr("About Cuneiform-Qt") );
aboutDialog.setText(
tr("<b>Cuneiform-Qt:</b><br />"
" Graphical interface for Cuneiform OCR system.<br />"
" Version: %1<br />"
" Copyright © Andrey Cherepanov <[email protected]>, 2009<br />"
" Web-site: <a href='%2'>%2</a><br />"
"<br />"
"<b>Cuneiform:</b><br />"
" Copyright © Cognitive technologies<br />"
" Web-site: <a href='https://launchpad.net/cuneiform-linux'>https://launchpad.net/cuneiform-linux</a><br />"
).arg(
QString( VERSION ) ).arg( // Application version
QApplication::organizationDomain() ) // Application site
);
aboutDialog.setStandardButtons( QMessageBox::Ok );
aboutDialog.setDefaultButton( QMessageBox::Ok );
aboutDialog.exec();
}