-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSave_Image_Thread.cc
540 lines (475 loc) · 11.6 KB
/
Save_Image_Thread.cc
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
/* Save_Image_Thread
HiROC CVS ID: $Id: Save_Image_Thread.cc,v 1.8 2012/06/15 01:16:07 castalia Exp $
Copyright (C) 2011 Arizona Board of Regents on behalf of the
Planetary Image Research Laboratory, Lunar and Planetary Laboratory at
the University of Arizona.
This library is free software; you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License, version 2.1,
as published by the Free Software Foundation.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this library; if not, write to the Free Software Foundation,
Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
*******************************************************************************/
#include "Save_Image_Thread.hh"
#include "Plastic_Image.hh"
#include "Activity_Indicator.hh"
#include "HiView_Utilities.hh"
#include <QDialog>
#include <QHBoxLayout>
#include <QLabel>
#include <QMessageBox>
#if defined (DEBUG_SECTION)
/* DEBUG_SECTION controls
DEBUG_SECTION report selection options.
Define any of the following options to obtain the desired debug reports:
*/
#define DEBUG_OFF 0
#define DEBUG_ALL -1
#define DEBUG_CONSTRUCTORS (1 << 0)
#define DEBUG_SLOTS (1 << 1)
#define DEBUG_SIGNALS (1 << 2)
#define DEBUG_RUN (1 << 3)
#define DEBUG_DEFAULT DEBUG_ALL
#if (DEBUG_SECTION +0) == 0
#undef DEBUG_SECTION
#define DEBUG_SECTION DEBUG_OFF
#endif
#include <iostream>
#include <iomanip>
using std::clog;
using std::endl;
using std::boolalpha;
#endif // DEBUG_SECTION
namespace UA::HiRISE
{
/*==============================================================================
Constants
*/
const char* const
Save_Image_Thread::ID =
"UA::HiRISE::Save_Image_Thread ($Revision: 1.8 $ $Date: 2012/06/15 01:16:07 $)";
/*==============================================================================
Constructors
*/
Save_Image_Thread::Save_Image_Thread
(
QObject* parent
)
: QThread (parent),
Dialog (NULL),
Message (NULL),
Saved (false)
{
setObjectName ("Save_Image_Thread");
#if ((DEBUG_SECTION) & DEBUG_CONSTRUCTORS)
clog << ">>> Save_Image_Thread" << endl;
#endif
connect (this,
SIGNAL (finished ()),
SLOT (thread_finished ()));
/*connect (this,
SIGNAL (terminated ()),
SLOT (thread_terminated ()));*/
#if ((DEBUG_SECTION) & DEBUG_CONSTRUCTORS)
clog << ">>> Save_Image_Thread" << endl;
#endif
}
Save_Image_Thread::~Save_Image_Thread ()
{
cancel ();
if (Dialog)
delete Dialog;
}
/*==============================================================================
Accessors
*/
Save_Image_Thread::Save_Sequence
Save_Image_Thread::save_sequence () const
{
QMutexLocker
sequence_lock (&Sequence_Lock);
return Sequence;
}
bool
Save_Image_Thread::image_saved () const
{
QMutexLocker
sequence_lock (&Sequence_Lock);
return Saved;
}
QString
Save_Image_Thread::failure_message () const
{
QMutexLocker
sequence_lock (&Sequence_Lock);
return Failure_Message;
}
void
Save_Image_Thread::invalidate ()
{
QMutexLocker
sequence_lock (&Sequence_Lock);
Image = NULL;
Pathname.clear ();
Format.clear ();
}
/*==============================================================================
Run the thread
*/
void
Save_Image_Thread::run ()
{
#if ((DEBUG_SECTION) & DEBUG_RUN)
LOCKED_LOGGING ((
clog << ">>> Save_Image_Thread::run" << endl));
#endif
Sequence_Lock.lock ();
Saved = false;
Failure_Message.clear ();
if (Image &&
! Pathname.isEmpty () &&
! Format.isEmpty ())
{
setTerminationEnabled (true);
Sequence = SAVE_IMAGE_RENDER;
// Copy so the user can change them while the thread is running.
Plastic_Image
*plastic_image = Image;
QString
pathname (Pathname),
format (Format),
message;
#if ((DEBUG_SECTION) & DEBUG_RUN)
LOCKED_LOGGING ((
clog << " Save_Image_Thread::run: SAVE_IMAGE_RENDER" << endl));
#endif
// Release the sequence lock while rendering the image.
Sequence_Lock.unlock ();
// Slight pause to enable widgets to be shown.
msleep (1000);
bool
saved = false,
rendered = false;
try {rendered = plastic_image->update ();}
catch (Plastic_Image::Render_Exception& except)
{message = QString::fromStdString (except.message ());}
catch (...)
{message = tr ("Unexpected exception!");}
Sequence_Lock.lock ();
if (Sequence == SAVE_IMAGE_DONE)
{
// User canceled.
if (message.isEmpty ())
message = tr ("Image save canceled during rendering.");
#if ((DEBUG_SECTION) & DEBUG_RUN)
LOCKED_LOGGING ((
clog << " " << message << endl));
#endif
rendered = false;
}
if (rendered)
{
Sequence = SAVE_IMAGE_WRITE;
#if ((DEBUG_SECTION) & DEBUG_RUN)
LOCKED_LOGGING ((
clog << " Save_Image_Thread::run: SAVE_IMAGE_WRITE" << endl));
#endif
// Release the sequence lock while writing the image.
Sequence_Lock.unlock ();
if (! (saved = plastic_image->save (pathname, format.toLatin1 ())))
message = tr ("Writing the image file did not succeed.");
Sequence_Lock.lock ();
#if ((DEBUG_SECTION) & DEBUG_RUN)
LOCKED_LOGGING ((
clog << " Save_Image_Thread::run: SAVE_IMAGE_DONE" << endl));
#endif
}
else
{
if (message.isEmpty ())
message = tr ("Image rendering did not complete.");
#if ((DEBUG_SECTION) & DEBUG_RUN)
LOCKED_LOGGING ((
clog << " " << message << endl));
#endif
}
// Still locked.
Sequence = SAVE_IMAGE_DONE;
Saved = saved;
if (! message.isEmpty ())
Failure_Message = message;
}
else
{
if (! Image)
Failure_Message = tr ("No image to save has been specified.");
if (Pathname.isEmpty ())
{
if (! Failure_Message.isEmpty ())
Failure_Message += '\n';
Failure_Message = tr ("No save file pathname has been specified.");
}
if (Format.isEmpty ())
{
if (! Failure_Message.isEmpty ())
Failure_Message += '\n';
Failure_Message += tr ("No image file format has been specified.");
}
#if ((DEBUG_SECTION) & DEBUG_RUN)
LOCKED_LOGGING ((
clog << " " << Failure_Message << endl));
#endif
}
Sequence_Lock.unlock ();
#if ((DEBUG_SECTION) & DEBUG_RUN)
LOCKED_LOGGING ((
clog << "<<< Save_Image_Thread::run" << endl));
#endif
}
bool
Save_Image_Thread::image
(
Plastic_Image* plastic_image,
const QString& pathname,
const QString& format
)
{
#if ((DEBUG_SECTION) & DEBUG_RUN)
clog << ">>> Save_Image_Thread::image:" << endl
<< " plastic_";
if (plastic_image)
clog << *plastic_image << endl;
else
clog << "image NULL" << endl;
clog << " pathname = " << pathname << endl
<< " format = " << format << endl;
#endif
QMutexLocker
sequence_lock (&Sequence_Lock);
if (plastic_image)
Image = plastic_image;
if (! pathname.isEmpty ())
Pathname = pathname;
if (! format.isEmpty ())
Format = format;
#if ((DEBUG_SECTION) & DEBUG_RUN)
clog << " current save info -" << endl
<< " plastic_";
if (Image)
clog << *Image << endl;
else
clog << "image NULL" << endl;
clog << " Pathname = " << Pathname << endl
<< " Format = " << Format << endl;
#endif
bool
ready =
(Image &&
! Pathname.isEmpty () &&
! Format.isEmpty ());
#if ((DEBUG_SECTION) & DEBUG_RUN)
clog << "<<< Save_Image_Thread::image: " << boolalpha << ready << endl;
#endif
return ready;
}
bool
Save_Image_Thread::save_image
(
Plastic_Image* plastic_image,
const QString& pathname,
const QString& format
)
{
#if ((DEBUG_SECTION) & DEBUG_RUN)
clog << ">>> Save_Image_Thread::save_image:" << endl;
#endif
bool
started = false;
// Update the image save information.
if (image (plastic_image, pathname, format) &&
! isRunning ())
{
if (! Dialog)
{
#if ((DEBUG_SECTION) & DEBUG_RUN)
clog << " Dialog construction ..." << endl;
#endif
Dialog = new QDialog;
Dialog->setWindowTitle (tr ("Saving Image"));
Dialog->setModal (false);
Dialog->setMinimumSize (100, 50);
Dialog_Layout = new QHBoxLayout (Dialog);
Dialog_Layout->addWidget (Activity = new Activity_Indicator,
0, Qt::AlignCenter);
Dialog_Layout->addWidget (Format_Label = new QLabel,
1, Qt::AlignLeft | Qt::AlignVCenter);
Dialog_Layout->addWidget (Pathname_Label = new QLabel,
100, Qt::AlignLeft | Qt::AlignVCenter);
connect (Activity,
SIGNAL (button_clicked (int)),
SLOT (cancel ()));
connect (this,
SIGNAL (canceled (bool)),
SLOT (save_canceled (bool)));
}
Format_Label->setText (Format + tr (" file:"));
QString
pathname (wrapped_pathname (Pathname, 0, Dialog->font ()));
Qt::Alignment
alignment = Qt::AlignLeft | Qt::AlignVCenter;
if (pathname.contains ('\n'))
alignment = Qt::AlignLeft | Qt::AlignTop;
Pathname_Label->setText (pathname);
Dialog_Layout->setAlignment (Pathname_Label, alignment);
Activity->start_delay (0);
Dialog->show ();
Activity->state (Activity_Indicator::STATE_1);
// Start the thread.
#if ((DEBUG_SECTION) & DEBUG_RUN)
clog << " start ..." << endl;
#endif
start ();
started = true;
}
#if ((DEBUG_SECTION) & DEBUG_RUN)
clog << ">>> Save_Image_Thread::save_image:" << boolalpha << started << endl;
#endif
return started;
}
/*==============================================================================
Qt slots
*/
bool
Save_Image_Thread::show_dialog
(
bool enabled
)
{
bool
shown = false;
if (Dialog)
{
if (enabled)
{
if (save_sequence () != SAVE_IMAGE_DONE)
{
Dialog->show ();
shown = true;
}
}
else
{
if (Message &&
Message->isVisible ())
Message->hide ();
Dialog->hide ();
}
}
return shown;
}
void
Save_Image_Thread::thread_finished ()
{
if (Dialog)
{
Activity->state (Activity_Indicator::STATE_OFF);
if (Message &&
Message->isVisible ())
Message->hide ();
Dialog->hide ();
}
// >>> SIGNAL <<<
#if ((DEBUG_SECTION) & (DEBUG_RUN | DEBUG_SIGNALS))
LOCKED_LOGGING ((
clog << "^^^ Save_Image_Thread::thread_finished: "
"emit done " << boolalpha << Saved << endl));
#endif
emit done (Saved);
}
void
Save_Image_Thread::thread_terminated ()
{
if (Dialog &&
Dialog->isVisible ())
{
Activity->state (Activity_Indicator::STATE_OFF);
Dialog->hide ();
}
Sequence_Lock.lock ();
Sequence = SAVE_IMAGE_DONE;
Sequence_Lock.unlock ();
// >>> SIGNAL <<<
#if ((DEBUG_SECTION) & (DEBUG_RUN | DEBUG_SIGNALS))
LOCKED_LOGGING ((
clog << "^^^ Save_Image_Thread::thread_terminated: "
"emit done false " << endl));
#endif
emit done (false);
}
bool
Save_Image_Thread::cancel ()
{
#if ((DEBUG_SECTION) & DEBUG_RUN)
LOCKED_LOGGING ((
clog << ">>> Save_Image_Thread::cancel" << endl));
#endif
bool
completed = true;
if (isRunning ())
{
/* Hold the Sequence_Lock
which prevents the thread from advancing from rendering to writing
and prevents the thread from hold the lock while terminated.
*/
Sequence_Lock.lock ();
if (Sequence == SAVE_IMAGE_RENDER)
{
// Cancel rendering in progress.
Image->cancel_update ();
// Let the thread know it's done.
Sequence = SAVE_IMAGE_DONE;
}
else
if (Sequence == SAVE_IMAGE_WRITE)
// Can't cancel file writing.
completed = false;
Sequence_Lock.unlock ();
}
// >>> SIGNAL <<<
#if ((DEBUG_SECTION) & (DEBUG_RUN | DEBUG_SIGNALS))
LOCKED_LOGGING ((
clog << "^^^ Save_Image_Thread::cancel: "
"emit canceled " << boolalpha << completed << endl));
#endif
emit canceled (completed);
#if ((DEBUG_SECTION) & DEBUG_RUN)
LOCKED_LOGGING ((
clog << "<<< Save_Image_Thread::cancel" << boolalpha << completed << endl));
#endif
return completed;
}
void
Save_Image_Thread::save_canceled
(
bool completed
)
{
if (! completed &&
Dialog)
{
if (! Message)
{
Message =
new QMessageBox (QMessageBox::Information, tr ("Writing Image"),
tr ("The image file is being written.\nPlease wait ...."),
QMessageBox::Ok, Dialog);
Message->setModal (false);
}
Message->show ();
}
}
} // namespace UA::HiRISE