1 /*
2    Drawpile - a collaborative drawing program.
3 
4    Copyright (C) 2018 Calle Laakkonen
5 
6    Drawpile is free software: you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation, either version 3 of the License, or
9    (at your option) any later version.
10 
11    Drawpile is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15 
16    You should have received a copy of the GNU General Public License
17    along with Drawpile.  If not, see <http://www.gnu.org/licenses/>.
18 */
19 #include "canvassaverrunnable.h"
20 #include "canvasmodel.h"
21 #include "ora/orawriter.h"
22 
23 #include <QImageWriter>
24 
25 namespace canvas {
26 
CanvasSaverRunnable(const CanvasModel * canvas,const QString & filename,QObject * parent)27 CanvasSaverRunnable::CanvasSaverRunnable(const CanvasModel *canvas, const QString &filename, QObject *parent)
28 	: QObject(parent),
29 	  m_layerstack(canvas->layerStack()->clone(this)),
30 	  m_filename(filename)
31 {
32 }
33 
run()34 void CanvasSaverRunnable::run()
35 {
36 	bool ok;
37 	QString errorMessage;
38 
39 	if(m_filename.endsWith(".ora", Qt::CaseInsensitive)) {
40 		// Special case: Save as OpenRaster with all the layers intact.
41 		ok = openraster::saveOpenRaster(m_filename, m_layerstack, &errorMessage);
42 
43 	} else {
44 		// Regular image formats: flatten the image first.
45 		QImageWriter writer(m_filename);
46 		if(!writer.write(m_layerstack->toFlatImage(false, true, false))) {
47 			errorMessage = writer.errorString();
48 			ok = false;
49 
50 		} else
51 			ok = true;
52 	}
53 
54 	if(!ok && errorMessage.isEmpty())
55 		errorMessage = "Unknown Error";
56 
57 	emit saveComplete(errorMessage);
58 }
59 
60 }
61