File: saveSnapshot.cpp

package info (click to toggle)
libqglviewer 2.6.3%2Bdfsg2-9
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 9,460 kB
  • sloc: cpp: 26,447; sh: 14; makefile: 11
file content (638 lines) | stat: -rw-r--r-- 21,279 bytes parent folder | download | duplicates (2)
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
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
/****************************************************************************

 Copyright (C) 2002-2014 Gilles Debunne. All rights reserved.

 This file is part of the QGLViewer library version 2.6.3.

 http://www.libqglviewer.com - contact@libqglviewer.com

 This file may be used under the terms of the GNU General Public License 
 versions 2.0 or 3.0 as published by the Free Software Foundation and
 appearing in the LICENSE file included in the packaging of this file.
 In addition, as a special exception, Gilles Debunne gives you certain 
 additional rights, described in the file GPL_EXCEPTION in this package.

 libQGLViewer uses dual licensing. Commercial/proprietary software must
 purchase a libQGLViewer Commercial License.

 This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.

*****************************************************************************/

#include "qglviewer.h"

#ifndef NO_VECTORIAL_RENDER
#  include "ui_VRenderInterface.h"
#  include "VRender/VRender.h"
#endif

#include "ui_ImageInterface.h"

// Output format list
# include <QImageWriter>

#include <qfileinfo.h>
#include <qfiledialog.h>
#include <qmessagebox.h>
#include <qapplication.h>
#include <qmap.h>
#include <qinputdialog.h>
#include <qprogressdialog.h>
#include <qcursor.h>

using namespace std;

////// Static global variables - local to this file //////
// List of available output file formats, formatted for QFileDialog.
static QString formats;
// Converts QFileDialog resulting format to Qt snapshotFormat.
static QMap<QString, QString> Qtformat;
// Converts Qt snapshotFormat to QFileDialog menu string.
static QMap<QString, QString> FDFormatString;
// Converts snapshotFormat to file extension
static QMap<QString, QString> extension;


/*! Sets snapshotFileName(). */
void QGLViewer::setSnapshotFileName(const QString& name)
{
	snapshotFileName_ = QFileInfo(name).absoluteFilePath();
}

#ifndef DOXYGEN
const QString& QGLViewer::snapshotFilename() const
{
	qWarning("snapshotFilename is deprecated. Use snapshotFileName() (uppercase N) instead.");
	return snapshotFileName();
}
#endif


/*! Opens a dialog that displays the different available snapshot formats.

Then calls setSnapshotFormat() with the selected one (unless the user cancels).

Returns \c false if the user presses the Cancel button and \c true otherwise. */
bool QGLViewer::openSnapshotFormatDialog()
{
	bool ok = false;
	QStringList list = formats.split(";;", QString::SkipEmptyParts);
	int current = list.indexOf(FDFormatString[snapshotFormat()]);
	QString format = QInputDialog::getItem(this, "Snapshot format", "Select a snapshot format", list, current, false, &ok);
	if (ok)
		setSnapshotFormat(Qtformat[format]);
	return ok;
}


// Finds all available Qt output formats, so that they can be available in
// saveSnapshot dialog. Initialize snapshotFormat() to the first one.
void QGLViewer::initializeSnapshotFormats()
{
	QList<QByteArray> list = QImageWriter::supportedImageFormats();
	QStringList formatList;
	for (int i=0; i < list.size(); ++i)
		formatList << QString(list.at(i).toUpper());
	//        qWarning("Available image formats: ");
	//        QStringList::Iterator it = formatList.begin();
	//        while( it != formatList.end() )
	//  	      qWarning((*it++).);  QT4 change this. qWarning no longer accepts QString

#ifndef NO_VECTORIAL_RENDER
	// We add the 3 vectorial formats to the list
	formatList += "EPS";
	formatList += "PS";
	formatList += "XFIG";
#endif

	// Check that the interesting formats are available and add them in "formats"
	// Unused formats: XPM XBM PBM PGM
	QStringList QtText, MenuText, Ext;
	QtText += "JPEG";	MenuText += "JPEG (*.jpg)";		Ext += "jpg";
	QtText += "PNG";	MenuText += "PNG (*.png)";      Ext += "png";
	QtText += "EPS";	MenuText += "Encapsulated Postscript (*.eps)";	Ext += "eps";
	QtText += "PS";	    MenuText += "Postscript (*.ps)"; Ext += "ps";
	QtText += "PPM";	MenuText += "24bit RGB Bitmap (*.ppm)";	Ext += "ppm";
	QtText += "BMP";	MenuText += "Windows Bitmap (*.bmp)";	Ext += "bmp";
	QtText += "XFIG";	MenuText += "XFig (*.fig)";		Ext += "fig";

	QStringList::iterator itText = QtText.begin();
	QStringList::iterator itMenu = MenuText.begin();
	QStringList::iterator itExt  = Ext.begin();

	while (itText != QtText.end())
	{
		//QMessageBox::information(this, "Snapshot ", "Trying format\n"+(*itText));
		if (formatList.contains((*itText)))
		{
			//QMessageBox::information(this, "Snapshot ", "Recognized format\n"+(*itText));
			if (formats.isEmpty())
				setSnapshotFormat(*itText);
			else
				formats += ";;";
			formats += (*itMenu);
			Qtformat[(*itMenu)]  = (*itText);
			FDFormatString[(*itText)]  = (*itMenu);
			extension[(*itText)] = (*itExt);
		}
		// Synchronize parsing
		itText++;
		itMenu++;
		itExt++;
	}
}

// Returns false if the user refused to use the fileName
static bool checkFileName(QString& fileName, QWidget* widget, const QString& snapshotFormat)
{
	if (fileName.isEmpty())
		return false;

	// Check that extension has been provided
	QFileInfo info(fileName);

	if (info.suffix().isEmpty())
	{
		// No extension given. Silently add one
		if (fileName.right(1) != ".")
			fileName += ".";
		fileName += extension[snapshotFormat];
		info.setFile(fileName);
	}
	else if (info.suffix() != extension[snapshotFormat])
	{
		// Extension is not appropriate. Propose a modification
		QString modifiedName = info.absolutePath() + '/' + info.baseName() + "." + extension[snapshotFormat];
		QFileInfo modifInfo(modifiedName);
		int i=(QMessageBox::warning(widget,"Wrong extension",
									info.fileName()+" has a wrong extension.\nSave as "+modifInfo.fileName()+" instead ?",
									QMessageBox::Yes,
									QMessageBox::No,
									QMessageBox::Cancel));
		if (i==QMessageBox::Cancel)
			return false;

		if (i==QMessageBox::Yes)
		{
			fileName = modifiedName;
			info.setFile(fileName);
		}
	}

	return true;
}

#ifndef NO_VECTORIAL_RENDER
// static void drawVectorial(void* param)
void drawVectorial(void* param)
{
	( (QGLViewer*) param )->drawVectorial();
}

#ifndef DOXYGEN
class ProgressDialog
{
public:
	static void showProgressDialog(QGLWidget* parent);
	static void updateProgress(float progress, const QString& stepString);
	static void hideProgressDialog();

private:
	static QProgressDialog* progressDialog;
};

QProgressDialog* ProgressDialog::progressDialog = NULL;

void ProgressDialog::showProgressDialog(QGLWidget* parent)
{
	progressDialog = new QProgressDialog(parent);
	progressDialog->setWindowTitle("Image rendering progress");
	progressDialog->setMinimumSize(300, 40);
	progressDialog->setCancelButton(NULL);
	progressDialog->show();
}

void ProgressDialog::updateProgress(float progress, const QString& stepString)
{
	progressDialog->setValue(int(progress*100));
	QString message(stepString);
	if (message.length() > 33)
		message = message.left(17) + "..." + message.right(12);
	progressDialog->setLabelText(message);
	progressDialog->update();
	qApp->processEvents();
}

void ProgressDialog::hideProgressDialog()
{
	progressDialog->close();
	delete progressDialog;
	progressDialog = NULL;
}

class VRenderInterface: public QDialog, public Ui::VRenderInterface
{
public: VRenderInterface(QWidget *parent) : QDialog(parent) { setupUi(this); }
};

#endif //DOXYGEN

// Pops-up a vectorial output option dialog box and save to fileName
// Returns -1 in case of Cancel, 0 for success and (todo) error code in case of problem.
static int saveVectorialSnapshot(const QString& fileName, QGLWidget* widget, const QString& snapshotFormat)
{
	static VRenderInterface* VRinterface = NULL;

	if (!VRinterface)
		VRinterface = new VRenderInterface(widget);


	// Configure interface according to selected snapshotFormat
	if (snapshotFormat == "XFIG")
	{
		VRinterface->tightenBBox->setEnabled(false);
		VRinterface->colorBackground->setEnabled(false);
	}
	else
	{
		VRinterface->tightenBBox->setEnabled(true);
		VRinterface->colorBackground->setEnabled(true);
	}

	if (VRinterface->exec() == QDialog::Rejected)
		return -1;

	vrender::VRenderParams vparams;
	vparams.setFilename(fileName);

	if (snapshotFormat == "EPS")	vparams.setFormat(vrender::VRenderParams::EPS);
	if (snapshotFormat == "PS")	vparams.setFormat(vrender::VRenderParams::PS);
	if (snapshotFormat == "XFIG")	vparams.setFormat(vrender::VRenderParams::XFIG);

	vparams.setOption(vrender::VRenderParams::CullHiddenFaces, !(VRinterface->includeHidden->isChecked()));
	vparams.setOption(vrender::VRenderParams::OptimizeBackFaceCulling, VRinterface->cullBackFaces->isChecked());
	vparams.setOption(vrender::VRenderParams::RenderBlackAndWhite, VRinterface->blackAndWhite->isChecked());
	vparams.setOption(vrender::VRenderParams::AddBackground, VRinterface->colorBackground->isChecked());
	vparams.setOption(vrender::VRenderParams::TightenBoundingBox, VRinterface->tightenBBox->isChecked());

	switch (VRinterface->sortMethod->currentIndex())
	{
		case 0: vparams.setSortMethod(vrender::VRenderParams::NoSorting); 		break;
		case 1: vparams.setSortMethod(vrender::VRenderParams::BSPSort); 		break;
		case 2: vparams.setSortMethod(vrender::VRenderParams::TopologicalSort); 	break;
		case 3: vparams.setSortMethod(vrender::VRenderParams::AdvancedTopologicalSort);	break;
		default:
			qWarning("VRenderInterface::saveVectorialSnapshot: Unknown SortMethod");
	}

	vparams.setProgressFunction(&ProgressDialog::updateProgress);
	ProgressDialog::showProgressDialog(widget);
	widget->makeCurrent();
	widget->raise();
	vrender::VectorialRender(drawVectorial, (void*) widget, vparams);
	ProgressDialog::hideProgressDialog();
	widget->setCursor(QCursor(Qt::ArrowCursor));

	// Should return vparams.error(), but this is currently not set.
	return 0;
}
#endif // NO_VECTORIAL_RENDER


class ImageInterface: public QDialog, public Ui::ImageInterface
{
public: ImageInterface(QWidget *parent) : QDialog(parent) { setupUi(this); }
};


// Pops-up an image settings dialog box and save to fileName.
// Returns false in case of problem.
bool QGLViewer::saveImageSnapshot(const QString& fileName)
{
	static ImageInterface* imageInterface = NULL;

	if (!imageInterface)
		imageInterface = new ImageInterface(this);

	imageInterface->imgWidth->setValue(width());
	imageInterface->imgHeight->setValue(height());

	imageInterface->imgQuality->setValue(snapshotQuality());

	if (imageInterface->exec() == QDialog::Rejected)
		return true;

	// Hide closed dialog
	qApp->processEvents();

	setSnapshotQuality(imageInterface->imgQuality->value());

	QColor previousBGColor = backgroundColor();
	if (imageInterface->whiteBackground->isChecked())
		setBackgroundColor(Qt::white);

	QSize finalSize(imageInterface->imgWidth->value(), imageInterface->imgHeight->value());

	qreal oversampling = imageInterface->oversampling->value();
	QSize subSize(int(this->width()/oversampling), int(this->height()/oversampling));

	qreal aspectRatio = width() / static_cast<qreal>(height());
	qreal newAspectRatio = finalSize.width() / static_cast<qreal>(finalSize.height());

	qreal zNear = camera()->zNear();
	qreal zFar = camera()->zFar();

	qreal xMin, yMin;
	bool expand = imageInterface->expandFrustum->isChecked();
	if (camera()->type() == qglviewer::Camera::PERSPECTIVE)
		if ((expand && (newAspectRatio>aspectRatio)) || (!expand && (newAspectRatio<aspectRatio)))
		{
			yMin = zNear * tan(camera()->fieldOfView() / 2.0);
			xMin = newAspectRatio * yMin;
		}
		else
		{
			xMin = zNear * tan(camera()->fieldOfView() / 2.0) * aspectRatio;
			yMin = xMin / newAspectRatio;
		}
	else
	{
		camera()->getOrthoWidthHeight(xMin, yMin);
		if ((expand && (newAspectRatio>aspectRatio)) || (!expand && (newAspectRatio<aspectRatio)))
			xMin = newAspectRatio * yMin;
		else
			yMin = xMin / newAspectRatio;
	}

	QImage image(finalSize.width(), finalSize.height(), QImage::Format_ARGB32);

	if (image.isNull())
	{
		QMessageBox::warning(this, "Image saving error",
							 "Unable to create resulting image",
							 QMessageBox::Ok, QMessageBox::NoButton);
		return false;
	}

	// ProgressDialog disabled since it interfers with the screen grabing mecanism on some platforms. Too bad.
	// ProgressDialog::showProgressDialog(this);

	qreal scaleX = subSize.width() / static_cast<qreal>(finalSize.width());
	qreal scaleY = subSize.height() / static_cast<qreal>(finalSize.height());

	qreal deltaX = 2.0 * xMin * scaleX;
	qreal deltaY = 2.0 * yMin * scaleY;

	int nbX = finalSize.width() / subSize.width();
	int nbY = finalSize.height() / subSize.height();

	// Extra subimage on the right/bottom border(s) if needed
	if (nbX * subSize.width() < finalSize.width())
		nbX++;
	if (nbY * subSize.height() < finalSize.height())
		nbY++;

	makeCurrent();

	// tileRegion_ is used by startScreenCoordinatesSystem to appropriately set the local
	// coordinate system when tiling
	tileRegion_ = new TileRegion();
	qreal tileXMin, tileWidth, tileYMin, tileHeight;
	if ((expand && (newAspectRatio>aspectRatio)) || (!expand && (newAspectRatio<aspectRatio)))
	{
		qreal tileTotalWidth = newAspectRatio * height();
		tileXMin = (width() - tileTotalWidth) / 2.0;
		tileWidth = tileTotalWidth * scaleX;
		tileYMin = 0.0;
		tileHeight = height() * scaleY;
		tileRegion_->textScale = 1.0 / scaleY;
	}
	else
	{
		qreal tileTotalHeight = width() / newAspectRatio;
		tileYMin = (height() - tileTotalHeight) / 2.0;
		tileHeight = tileTotalHeight * scaleY;
		tileXMin = 0.0;
		tileWidth = width() * scaleX;
		tileRegion_->textScale = 1.0 / scaleX;
	}

	int count=0;
	for (int i=0; i<nbX; i++)
		for (int j=0; j<nbY; j++)
		{
			preDraw();

			// Change projection matrix
			glMatrixMode(GL_PROJECTION);
			glLoadIdentity();
			if (camera()->type() == qglviewer::Camera::PERSPECTIVE)
				glFrustum(-xMin + i*deltaX, -xMin + (i+1)*deltaX, yMin - (j+1)*deltaY, yMin - j*deltaY, zNear, zFar);
			else
				glOrtho(-xMin + i*deltaX, -xMin + (i+1)*deltaX, yMin - (j+1)*deltaY, yMin - j*deltaY, zNear, zFar);
			glMatrixMode(GL_MODELVIEW);

			tileRegion_->xMin = tileXMin + i * tileWidth;
			tileRegion_->xMax = tileXMin + (i+1) * tileWidth;
			tileRegion_->yMin = tileYMin + j * tileHeight;
			tileRegion_->yMax = tileYMin + (j+1) * tileHeight;

			draw();
			postDraw();

			// ProgressDialog::hideProgressDialog();
			// qApp->processEvents();

			QImage snapshot = grabFrameBuffer(true);

			// ProgressDialog::showProgressDialog(this);
			// ProgressDialog::updateProgress(count / (qreal)(nbX*nbY),
			// "Generating image ["+QString::number(count)+"/"+QString::number(nbX*nbY)+"]");
			// qApp->processEvents();

			QImage subImage = snapshot.scaled(subSize, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);

			// Copy subImage in image
			for (int ii=0; ii<subSize.width(); ii++)
			{
				int fi = i*subSize.width() + ii;
				if (fi == image.width())
					break;
				for (int jj=0; jj<subSize.height(); jj++)
				{
					int fj = j*subSize.height() + jj;
					if (fj == image.height())
						break;
					image.setPixel(fi, fj, subImage.pixel(ii,jj));
				}
			}
			count++;
		}

	bool saveOK = image.save(fileName, snapshotFormat().toLatin1().constData(), snapshotQuality());

	// ProgressDialog::hideProgressDialog();
	// setCursor(QCursor(Qt::ArrowCursor));

	delete tileRegion_;
	tileRegion_ = NULL;

	if (imageInterface->whiteBackground->isChecked())
		setBackgroundColor(previousBGColor);

	return saveOK;
}


/*! Saves a snapshot of the current image displayed by the widget.

 Options are set using snapshotFormat(), snapshotFileName() and snapshotQuality(). For non vectorial
 image formats, the image size is equal to the current viewer's dimensions (see width() and
 height()). See snapshotFormat() for details on supported formats.

 If \p automatic is \c false (or if snapshotFileName() is empty), a file dialog is opened to ask for
 the file name.

 When \p automatic is \c true, the file name is set to \c NAME-NUMBER, where \c NAME is
 snapshotFileName() and \c NUMBER is snapshotCounter(). The snapshotCounter() is automatically
 incremented after each snapshot saving. This is useful to create videos from your application:
 \code
 void Viewer::init()
 {
   resize(720, 576); // PAL DV format (use 720x480 for NTSC DV)
   connect(this, SIGNAL(drawFinished(bool)), SLOT(saveSnapshot(bool)));
 }
 \endcode
 Then call draw() in a loop (for instance using animate() and/or a camera() KeyFrameInterpolator
 replay) to create your image sequence.

 If you want to create a Quicktime VR panoramic sequence, simply use code like this:
 \code
 void Viewer::createQuicktime()
 {
   const int nbImages = 36;
   for (int i=0; i<nbImages; ++i)
	 {
	   camera()->setOrientation(2.0*M_PI/nbImages, 0.0); // Theta-Phi orientation
	   showEntireScene();
	   update(); // calls draw(), which emits drawFinished(), which calls saveSnapshot()
	 }
 }
 \endcode

 If snapshotCounter() is negative, no number is appended to snapshotFileName() and the
 snapshotCounter() is not incremented. This is useful to force the creation of a file, overwriting
 the previous one.

 When \p overwrite is set to \c false (default), a window asks for confirmation if the file already
 exists. In \p automatic mode, the snapshotCounter() is incremented (if positive) until a
 non-existing file name is found instead. Otherwise the file is overwritten without confirmation.

 The VRender library was written by Cyril Soler (Cyril dot Soler at imag dot fr). If the generated
 PS or EPS file is not properly displayed, remove the anti-aliasing option in your postscript viewer.

 \note In order to correctly grab the frame buffer, the QGLViewer window is raised in front of
 other windows by this method. */
void QGLViewer::saveSnapshot(bool automatic, bool overwrite)
{
	// Ask for file name
	if (snapshotFileName().isEmpty() || !automatic)
	{
		QString fileName;
		QString selectedFormat = FDFormatString[snapshotFormat()];
		fileName = QFileDialog::getSaveFileName(this, "Choose a file name to save under", snapshotFileName(), formats, &selectedFormat,
												overwrite?QFileDialog::DontConfirmOverwrite:QFlags<QFileDialog::Option>(0));
		setSnapshotFormat(Qtformat[selectedFormat]);

		if (checkFileName(fileName, this, snapshotFormat()))
			setSnapshotFileName(fileName);
		else
			return;
	}

	QFileInfo fileInfo(snapshotFileName());

	if ((automatic) && (snapshotCounter() >= 0))
	{
		// In automatic mode, names have a number appended
		const QString baseName = fileInfo.baseName();
		QString count;
		count.sprintf("%.04d", snapshotCounter_++);
		QString suffix;
		suffix = fileInfo.suffix();
		if (suffix.isEmpty())
			suffix = extension[snapshotFormat()];
		fileInfo.setFile(fileInfo.absolutePath()+ '/' + baseName + '-' + count + '.' + suffix);

		if (!overwrite)
			while (fileInfo.exists())
			{
				count.sprintf("%.04d", snapshotCounter_++);
				fileInfo.setFile(fileInfo.absolutePath() + '/' +baseName + '-' + count + '.' + fileInfo.suffix());
			}
	}

	bool saveOK;
#ifndef NO_VECTORIAL_RENDER
	if ( (snapshotFormat() == "EPS") || (snapshotFormat() == "PS") || (snapshotFormat() == "XFIG") )
		// Vectorial snapshot. -1 means cancel, 0 is ok, >0 (should be) an error
		saveOK = (saveVectorialSnapshot(fileInfo.filePath(), this, snapshotFormat()) <= 0);
	else
#endif
		if (automatic)
		{
			QImage snapshot = frameBufferSnapshot();
			saveOK = snapshot.save(fileInfo.filePath(), snapshotFormat().toLatin1().constData(), snapshotQuality());
		}
		else
			saveOK = saveImageSnapshot(fileInfo.filePath());

	if (!saveOK)
		QMessageBox::warning(this, "Snapshot problem", "Unable to save snapshot in\n"+fileInfo.filePath());
}

QImage QGLViewer::frameBufferSnapshot()
{
	// Viewer must be on top of other windows.
	makeCurrent();
	raise();
	// Hack: Qt has problems if the frame buffer is grabbed after QFileDialog is displayed.
	// We grab the frame buffer before, even if it might be not necessary (vectorial rendering).
	// The problem could not be reproduced on a simple example to submit a Qt bug.
	// However, only grabs the backgroundImage in the eponym example. May come from the driver.
	return grabFrameBuffer(true);
}

/*! Same as saveSnapshot(), except that it uses \p fileName instead of snapshotFileName().

 If \p fileName is empty, opens a file dialog to select the name.

 Snapshot settings are set from snapshotFormat() and snapshotQuality().

 Asks for confirmation when \p fileName already exists and \p overwrite is \c false (default).

 \attention If \p fileName is a char* (as is "myFile.jpg"), it may be casted into a \c bool, and the
 other saveSnapshot() method may be used instead. Pass QString("myFile.jpg") as a parameter to
 prevent this. */
void QGLViewer::saveSnapshot(const QString& fileName, bool overwrite)
{
	const QString previousName = snapshotFileName();
	const int previousCounter = snapshotCounter();
	setSnapshotFileName(fileName);
	setSnapshotCounter(-1);
	saveSnapshot(true, overwrite);
	setSnapshotFileName(previousName);
	setSnapshotCounter(previousCounter);
}

/*! Takes a snapshot of the current display and pastes it to the clipboard.

This action is activated by the KeyboardAction::SNAPSHOT_TO_CLIPBOARD enum, binded to \c Ctrl+C by default.
*/
void QGLViewer::snapshotToClipboard()
{
	QClipboard *cb = QApplication::clipboard();
	cb->setImage(frameBufferSnapshot());
}