1 /*
2 For general Scribus (>=1.3.2) copyright and licensing information please refer
3 to the COPYING file provided with the program. Following this notice may exist
4 a copyright and/or license notice that predates the release of Scribus 1.3.2
5 for which a new license (GPL+exception) is in place.
6 */
7 
8 #include <QByteArray>
9 #include <QCursor>
10 #include <QDrag>
11 #include <QFile>
12 #include <QList>
13 #include <QMimeData>
14 #include <QRegExp>
15 #include <QStack>
16 #include <QDebug>
17 
18 #include <cstdlib>
19 #include <cstdio>
20 
21 #include "importwpg.h"
22 
23 #include "commonstrings.h"
24 #include "loadsaveplugin.h"
25 #include "pagesize.h"
26 #include "prefscontext.h"
27 #include "prefsfile.h"
28 #include "prefsmanager.h"
29 #include "prefstable.h"
30 #include "rawimage.h"
31 #include "scclocale.h"
32 #include "sccolorengine.h"
33 #include "scconfig.h"
34 #include "scmimedata.h"
35 #include "scpaths.h"
36 #include "scpattern.h"
37 #include "scribusXml.h"
38 #include "scribuscore.h"
39 #include "scribusdoc.h"
40 #include "scribusview.h"
41 #include "sctextstream.h"
42 #include "selection.h"
43 #include "third_party/wpg/WPGStreamImplementation.h"
44 #include "ui/customfdialog.h"
45 #include "ui/missing.h"
46 #include "ui/multiprogressdialog.h"
47 #include "ui/propertiespalette.h"
48 #include "undomanager.h"
49 #include "util.h"
50 #include "util_formats.h"
51 #include "util_math.h"
52 
ScrPainter()53 ScrPainter::ScrPainter()
54 {
55 }
56 
startGraphics(double width,double height)57 void ScrPainter::startGraphics(double width, double height)
58 {
59 	CurrColorFill = "Black";
60 	CurrFillShade = 100.0;
61 	CurrColorStroke = "Black";
62 	CurrStrokeShade = 100.0;
63 	CurrStrokeTrans = 0.0;
64 	CurrFillTrans = 0.0;
65 	Coords.resize(0);
66 	Coords.svgInit();
67 	LineW = 1.0;
68 	lineJoin = Qt::MiterJoin;
69 	lineEnd = Qt::FlatCap;
70 	fillrule = true;
71 	gradientAngle = 0.0;
72 	isGradient = false;
73 	fillSet = false;
74 	strokeSet = false;
75 	currentGradient = VGradient(VGradient::linear);
76 	currentGradient.clearStops();
77 	currentGradient.setRepeatMethod( VGradient::none );
78 	dashArray.clear();
79 	if (flags & LoadSavePlugin::lfCreateDoc)
80 	{
81 		m_Doc->setPage(72 * width, 72 * height, 0, 0, 0, 0, 0, 0, false, false);
82 		if (width > height)
83 			m_Doc->setPageOrientation(1);
84 		else
85 			m_Doc->setPageOrientation(0);
86 		m_Doc->setPageSize("Custom");
87 		m_Doc->changePageProperties(0, 0, 0, 0, 72 * height, 72 * width, 72 * height, 72 * width, m_Doc->pageOrientation(), m_Doc->pageSize(), m_Doc->currentPage()->pageNr(), false);
88 	}
89 	firstLayer = true;
90 }
91 
endGraphics()92 void ScrPainter::endGraphics()
93 {
94 }
95 
startLayer(unsigned int id)96 void ScrPainter::startLayer(unsigned int id)
97 {
98 	if (flags & LoadSavePlugin::lfCreateDoc)
99 	{
100 		if (!firstLayer)
101 			m_Doc->addLayer(QString("Layer %1").arg(id), true);
102 		firstLayer = false;
103 	}
104 }
105 
endLayer(unsigned int id)106 void ScrPainter::endLayer(unsigned int id)
107 {
108 }
109 
setPen(const libwpg::WPGPen & pen)110 void ScrPainter::setPen(const libwpg::WPGPen& pen)
111 {
112 	LineW = 72 * pen.width;
113 	ScColor tmp;
114 	ColorList::Iterator it;
115 	CurrColorStroke = "Black";
116 	CurrStrokeShade = 100.0;
117 	int Rc, Gc, Bc;
118 	Rc = pen.foreColor.red;
119 	Gc = pen.foreColor.green;
120 	Bc = pen.foreColor.blue;
121 	tmp.setRgbColor(Rc, Gc, Bc);
122 	tmp.setSpotColor(false);
123 	tmp.setRegistrationColor(false);
124 	QString newColorName = "FromWPG"+tmp.name();
125 	QString fNam = m_Doc->PageColors.tryAddColor(newColorName, tmp);
126 	if (fNam == newColorName)
127 		importedColors.append(newColorName);
128 	CurrColorStroke = fNam;
129 	CurrStrokeTrans = pen.foreColor.alpha / 255.0;
130 	if (!pen.solid)
131 	{
132 		dashArray.clear();
133 		for (unsigned i = 0; i < pen.dashArray.count(); i++)
134 		{
135 			dashArray.append(pen.dashArray.at(i)*LineW);
136 		}
137 	}
138 	switch (pen.joinstyle)
139 	{
140 		case 1:
141 			lineJoin = Qt::BevelJoin;
142 			break;
143 		case 2:
144 			lineJoin = Qt::MiterJoin;
145 			break;
146 		case 3:
147 			lineJoin = Qt::RoundJoin;
148 			break;
149 		default:
150 			lineJoin = Qt::MiterJoin;
151 			break;
152 	}
153 	switch (pen.capstyle)
154 	{
155 		case 0:
156 			lineEnd = Qt::FlatCap;
157 			break;
158 		case 1:
159 			lineEnd = Qt::RoundCap;
160 			break;
161 		case 2:
162 			lineEnd = Qt::SquareCap;
163 			break;
164 		default:
165 			lineEnd = Qt::FlatCap;
166 			break;
167 	}
168 	strokeSet = true;
169 }
170 
setBrush(const libwpg::WPGBrush & brush)171 void ScrPainter::setBrush(const libwpg::WPGBrush& brush)
172 {
173 	ScColor tmp;
174 	ColorList::Iterator it;
175 	CurrColorFill = "Black";
176 	CurrFillShade = 100.0;
177 	int Rc, Gc, Bc;
178 	if (brush.style == libwpg::WPGBrush::Solid)
179 	{
180 		Rc = brush.foreColor.red;
181 		Gc = brush.foreColor.green;
182 		Bc = brush.foreColor.blue;
183 		tmp.setRgbColor(Rc, Gc, Bc);
184 		tmp.setSpotColor(false);
185 		tmp.setRegistrationColor(false);
186 		QString newColorName = "FromWPG"+tmp.name();
187 		QString fNam = m_Doc->PageColors.tryAddColor(newColorName, tmp);
188 		if (fNam == newColorName)
189 			importedColors.append(newColorName);
190 		CurrColorFill = fNam;
191 		CurrFillTrans = brush.foreColor.alpha / 255.0;
192 	}
193 	else if (brush.style == libwpg::WPGBrush::Gradient)
194 	{
195 		gradientAngle = brush.gradient.angle();
196 		isGradient = true;
197 		currentGradient = VGradient(VGradient::linear);
198 		currentGradient.clearStops();
199 		for (unsigned c = 0; c < brush.gradient.count(); c++)
200 		{
201 			QString currStopColor = CommonStrings::None;
202 			Rc = brush.gradient.stopColor(c).red;
203 			Gc = brush.gradient.stopColor(c).green;
204 			Bc = brush.gradient.stopColor(c).blue;
205 			tmp.setRgbColor(Rc, Gc, Bc);
206 			tmp.setSpotColor(false);
207 			tmp.setRegistrationColor(false);
208 			QString newColorName = "FromWPG"+tmp.name();
209 			QString fNam = m_Doc->PageColors.tryAddColor(newColorName, tmp);
210 			if (fNam == newColorName)
211 				importedColors.append(newColorName);
212 			currStopColor = fNam;
213 			const ScColor& gradC = m_Doc->PageColors[currStopColor];
214 			double pos = qBound(0.0, fabs(brush.gradient.stopOffset(c)), 1.0);
215 			currentGradient.addStop( ScColorEngine::getRGBColor(gradC, m_Doc), pos, 0.5, 1.0, currStopColor, 100 );
216 		}
217 	}
218 	else if (brush.style == libwpg::WPGBrush::NoBrush)
219 		CurrColorFill = CommonStrings::None;
220 	fillSet = true;
221 }
222 
setFillRule(FillRule rule)223 void ScrPainter::setFillRule(FillRule rule)
224 {
225 	fillrule = (rule != libwpg::WPGPaintInterface::WindingFill);
226 //	qDebug() << "Fill Rule " << fillrule;
227 }
228 
drawRectangle(const libwpg::WPGRect & rect,double rx,double ry)229 void ScrPainter::drawRectangle(const libwpg::WPGRect& rect, double rx, double ry)
230 {
231 	int z = m_Doc->itemAdd(PageItem::Polygon, PageItem::Rectangle, baseX, baseY, rect.width() * 72.0, rect.height() * 72.0, LineW, CurrColorFill, CurrColorStroke);
232 	PageItem *ite = m_Doc->Items->at(z);
233 	if ((rx > 0) && (ry > 0))
234 	{
235 		ite->setCornerRadius(qMax(72*rx, 72*ry));
236 		ite->SetFrameRound();
237 		m_Doc->setRedrawBounding(ite);
238 	}
239 	QTransform mm = QTransform();
240 	mm.translate(72*rect.x1, 72*rect.y1);
241 	ite->PoLine.map(mm);
242 	ite->PoLine.translate(m_Doc->currentPage()->xOffset(), m_Doc->currentPage()->yOffset());
243 	finishItem(ite);
244 //	qDebug() << "draw Rect";
245 }
246 
drawEllipse(const libwpg::WPGPoint & center,double rx,double ry)247 void ScrPainter::drawEllipse(const libwpg::WPGPoint& center, double rx, double ry)
248 {
249 	int z = m_Doc->itemAdd(PageItem::Polygon, PageItem::Ellipse, baseX, baseY, rx * 144.0, ry * 144.0, LineW, CurrColorFill, CurrColorStroke);
250 	PageItem *ite = m_Doc->Items->at(z);
251 	QTransform mm = QTransform();
252 	mm.translate(72*(center.x - rx), 72*(center.y - ry));
253 	ite->PoLine.map(mm);
254 	ite->PoLine.translate(m_Doc->currentPage()->xOffset(), m_Doc->currentPage()->yOffset());
255 	finishItem(ite);
256 //	qDebug() << "draw Ellipse";
257 }
258 
drawPolygon(const libwpg::WPGPointArray & vertices,bool closed)259 void ScrPainter::drawPolygon(const libwpg::WPGPointArray& vertices, bool closed)
260 {
261 	if (vertices.count() < 2)
262 		return;
263 	Coords.resize(0);
264 	Coords.svgInit();
265 	PageItem *ite;
266 	Coords.svgMoveTo(72 * vertices[0].x, 72 * vertices[0].y);
267 	for (unsigned i = 1; i < vertices.count(); i++)
268 	{
269 		Coords.svgLineTo(72 * vertices[i].x, 72 * vertices[i].y);
270 	}
271 	if (closed)
272 		Coords.svgClosePath();
273 	if (!Coords.empty())
274 	{
275 		int z;
276 		if (closed)
277 			z = m_Doc->itemAdd(PageItem::Polygon, PageItem::Unspecified, baseX, baseY, 10, 10, LineW, CurrColorFill, CurrColorStroke);
278 		else
279 			z = m_Doc->itemAdd(PageItem::PolyLine, PageItem::Unspecified, baseX, baseY, 10, 10, LineW, CommonStrings::None, CurrColorStroke);
280 		ite = m_Doc->Items->at(z);
281 		ite->PoLine = Coords.copy();
282 		ite->PoLine.translate(m_Doc->currentPage()->xOffset(), m_Doc->currentPage()->yOffset());
283 		finishItem(ite);
284 	}
285 //	qDebug() << "draw Polygon";
286 }
287 
drawPath(const libwpg::WPGPath & path)288 void ScrPainter::drawPath(const libwpg::WPGPath& path)
289 {
290 	Coords.resize(0);
291 	Coords.svgInit();
292 	PageItem *ite;
293 	for (unsigned i = 0; i < path.count(); i++)
294 	{
295 		libwpg::WPGPathElement element = path.element(i);
296 		libwpg::WPGPoint point = element.point;
297 		switch(element.type)
298 		{
299 			case libwpg::WPGPathElement::MoveToElement:
300 				Coords.svgMoveTo(72 * point.x, 72 * point.y);
301 				break;
302 			case libwpg::WPGPathElement::LineToElement:
303 				Coords.svgLineTo(72 * point.x, 72 * point.y);
304 				break;
305 			case libwpg::WPGPathElement::CurveToElement:
306 				Coords.svgCurveToCubic(72*element.extra1.x, 72*element.extra1.y, 72*element.extra2.x, 72*element.extra2.y, 72 * point.x, 72 * point.y);
307 				break;
308 			default:
309 				break;
310 		}
311 	}
312 	if (!Coords.empty())
313 	{
314 		int z;
315 		if (fillSet)
316 		{
317 			if (!path.filled)
318 				CurrColorFill = CommonStrings::None;
319 		}
320 		if (strokeSet)
321 		{
322 			if (!path.framed)
323 				CurrColorStroke = CommonStrings::None;
324 		}
325 		if (path.closed)
326 		{
327 			Coords.svgClosePath();
328 			z = m_Doc->itemAdd(PageItem::Polygon, PageItem::Unspecified, baseX, baseY, 10, 10, LineW, CurrColorFill, CurrColorStroke);
329 		}
330 		else
331 			z = m_Doc->itemAdd(PageItem::PolyLine, PageItem::Unspecified, baseX, baseY, 10, 10, LineW, CurrColorFill, CurrColorStroke);
332 		ite = m_Doc->Items->at(z);
333 		ite->PoLine = Coords.copy();
334 		ite->PoLine.translate(m_Doc->currentPage()->xOffset(), m_Doc->currentPage()->yOffset());
335 		finishItem(ite);
336 	}
337 //	qDebug() << "draw Path" << CurrFillTrans;
338 }
339 
finishItem(PageItem * ite)340 void ScrPainter::finishItem(PageItem* ite)
341 {
342 	ite->ClipEdited = true;
343 	ite->FrameType = 3;
344 	ite->setFillShade(CurrFillShade);
345 	ite->setFillEvenOdd(fillrule);
346 	ite->setLineShade(CurrStrokeShade);
347 	ite->setLineJoin(lineJoin);
348 	ite->setLineEnd(lineEnd);
349 	ite->DashValues = dashArray;
350 	FPoint wh = getMaxClipF(&ite->PoLine);
351 	ite->setWidthHeight(wh.x(),wh.y());
352 	ite->setTextFlowMode(PageItem::TextFlowDisabled);
353 	m_Doc->adjustItemSize(ite);
354 	ite->OldB2 = ite->width();
355 	ite->OldH2 = ite->height();
356 	if (isGradient)
357 	{
358 		ite->fill_gradient = currentGradient;
359 		ite->GrType = Gradient_Linear;
360 		QTransform m1;
361 		m1.rotate(-gradientAngle);
362 		ite->GrStartX = 0;
363 		ite->GrStartY = 0;
364 		QPointF target = m1.map(QPointF(0.0, ite->height()));
365 		ite->GrEndX = target.x();
366 		ite->GrEndY = target.y();
367 	}
368 	else
369 	{
370 		ite->setFillTransparency(CurrFillTrans);
371 		ite->setLineTransparency(CurrStrokeTrans);
372 	}
373 	ite->updateClip();
374 	Elements.append(ite);
375 	Coords.resize(0);
376 	Coords.svgInit();
377 }
378 
drawBitmap(const libwpg::WPGBitmap & bitmap,double hres,double vres)379 void ScrPainter::drawBitmap(const libwpg::WPGBitmap& bitmap, double hres, double vres)
380 {
381 	QImage image = QImage(bitmap.width(), bitmap.height(), QImage::Format_RGB32);
382 	for (int x = 0; x < bitmap.width(); x++)
383 	{
384 		for (int y = 0; y < bitmap.height(); y++)
385 		{
386 			libwpg::WPGColor color = bitmap.pixel(x, y);
387 			image.setPixel(x, y, qRgb(color.red, color.green, color.blue));
388 		}
389 	}
390 	double w = (bitmap.rect.x2 - bitmap.rect.x1) * 72.0;
391 	double h = (bitmap.rect.y2 - bitmap.rect.y1) * 72.0;
392 	int z = m_Doc->itemAdd(PageItem::ImageFrame, PageItem::Unspecified, bitmap.rect.x1 * 72 + baseX, bitmap.rect.y1 * 72 + baseY, w, h, 1, m_Doc->itemToolPrefs().imageFillColor, m_Doc->itemToolPrefs().imageStrokeColor);
393 	PageItem *ite = m_Doc->Items->at(z);
394 	QTemporaryFile *tempFile = new QTemporaryFile(QDir::tempPath() + "/scribus_temp_wpg_XXXXXX.png");
395 	tempFile->setAutoRemove(false);
396 	tempFile->open();
397 	QString fileName = getLongPathName(tempFile->fileName());
398 	tempFile->close();
399 	delete tempFile;
400 	ite->isTempFile = true;
401 	ite->isInlineImage = true;
402 	image.setDotsPerMeterX ((int) (hres / 0.0254));
403 	image.setDotsPerMeterY ((int) (vres / 0.0254));
404 	image.save(fileName, "PNG");
405 	m_Doc->loadPict(fileName, ite);
406 	ite->setImageScalingMode(false, false);
407 	ite->moveBy(m_Doc->currentPage()->xOffset(), m_Doc->currentPage()->yOffset());
408 	finishItem(ite);
409 //	qDebug() << "drawBitmap";
410 }
411 
drawImageObject(const libwpg::WPGBinaryData &)412 void ScrPainter::drawImageObject(const libwpg::WPGBinaryData& /*binaryData*/)
413 {
414 //	qDebug() << "drawBinaryData";
415 }
416 
417 
WpgPlug(ScribusDoc * doc,int flags)418 WpgPlug::WpgPlug(ScribusDoc* doc, int flags)
419 {
420 	baseX = baseY = 0;
421 	docWidth = docHeight = 1;
422 
423 	tmpSel = new Selection(this, false);
424 	m_Doc = doc;
425 	importerFlags = flags;
426 	interactive = (flags & LoadSavePlugin::lfInteractive);
427 	progressDialog = nullptr;
428 	cancel = false;
429 }
430 
readThumbnail(const QString & fName)431 QImage WpgPlug::readThumbnail(const QString& fName)
432 {
433 	QFileInfo fi = QFileInfo(fName);
434 	double b, h;
435 	b = PrefsManager::instance().appPrefs.docSetupPrefs.pageWidth;
436 	h = PrefsManager::instance().appPrefs.docSetupPrefs.pageHeight;
437 	docWidth = b;
438 	docHeight = h;
439 	progressDialog = nullptr;
440 	m_Doc = new ScribusDoc();
441 	m_Doc->setup(0, 1, 1, 1, 1, "Custom", "Custom");
442 	m_Doc->setPage(docWidth, docHeight, 0, 0, 0, 0, 0, 0, false, false);
443 	m_Doc->addPage(0);
444 	m_Doc->setGUI(false, ScCore->primaryMainWindow(), nullptr);
445 	baseX = m_Doc->currentPage()->xOffset();
446 	baseY = m_Doc->currentPage()->yOffset();
447 	Elements.clear();
448 	m_Doc->setLoading(true);
449 	m_Doc->DoDrawing = false;
450 	m_Doc->scMW()->setScriptRunning(true);
451 	QString CurDirP = QDir::currentPath();
452 	QDir::setCurrent(fi.path());
453 	if (convert(fName))
454 	{
455 		tmpSel->clear();
456 		QDir::setCurrent(CurDirP);
457 		if (Elements.count() > 1)
458 			m_Doc->groupObjectsList(Elements);
459 		m_Doc->DoDrawing = true;
460 		m_Doc->m_Selection->delaySignalsOn();
461 		QImage tmpImage;
462 		if (Elements.count() > 0)
463 		{
464 			for (int dre=0; dre<Elements.count(); ++dre)
465 			{
466 				tmpSel->addItem(Elements.at(dre), true);
467 			}
468 			tmpSel->setGroupRect();
469 			double xs = tmpSel->width();
470 			double ys = tmpSel->height();
471 			tmpImage = Elements.at(0)->DrawObj_toImage(500);
472 			tmpImage.setText("XSize", QString("%1").arg(xs));
473 			tmpImage.setText("YSize", QString("%1").arg(ys));
474 		}
475 		m_Doc->scMW()->setScriptRunning(false);
476 		m_Doc->setLoading(false);
477 		m_Doc->m_Selection->delaySignalsOff();
478 		delete m_Doc;
479 		return tmpImage;
480 	}
481 	QDir::setCurrent(CurDirP);
482 	m_Doc->DoDrawing = true;
483 	m_Doc->scMW()->setScriptRunning(false);
484 	delete m_Doc;
485 	return QImage();
486 }
487 
import(const QString & fNameIn,const TransactionSettings & trSettings,int flags,bool showProgress)488 bool WpgPlug::import(const QString& fNameIn, const TransactionSettings& trSettings, int flags, bool showProgress)
489 {
490 	bool success = false;
491 	interactive = (flags & LoadSavePlugin::lfInteractive);
492 	importerFlags = flags;
493 	cancel = false;
494 	double b, h;
495 	bool ret = false;
496 	QFileInfo fi = QFileInfo(fNameIn);
497 	if ( !ScCore->usingGUI() )
498 	{
499 		interactive = false;
500 		showProgress = false;
501 	}
502 	if ( showProgress )
503 	{
504 		ScribusMainWindow* mw=(m_Doc==nullptr) ? ScCore->primaryMainWindow() : m_Doc->scMW();
505 		progressDialog = new MultiProgressDialog( tr("Importing: %1").arg(fi.fileName()), CommonStrings::tr_Cancel, mw );
506 		QStringList barNames, barTexts;
507 		barNames << "GI";
508 		barTexts << tr("Analyzing File:");
509 		QList<bool> barsNumeric;
510 		barsNumeric << false;
511 		progressDialog->addExtraProgressBars(barNames, barTexts, barsNumeric);
512 		progressDialog->setOverallTotalSteps(3);
513 		progressDialog->setOverallProgress(0);
514 		progressDialog->setProgress("GI", 0);
515 		progressDialog->show();
516 		connect(progressDialog, SIGNAL(canceled()), this, SLOT(cancelRequested()));
517 		qApp->processEvents();
518 	}
519 	else
520 		progressDialog = nullptr;
521 /* Set default Page to size defined in Preferences */
522 	b = 0.0;
523 	h = 0.0;
524 	if (progressDialog)
525 	{
526 		progressDialog->setOverallProgress(1);
527 		qApp->processEvents();
528 	}
529 	if (b == 0.0)
530 		b = PrefsManager::instance().appPrefs.docSetupPrefs.pageWidth;
531 	if (h == 0.0)
532 		h = PrefsManager::instance().appPrefs.docSetupPrefs.pageHeight;
533 	docWidth = b;
534 	docHeight = h;
535 	baseX = 0;
536 	baseY = 0;
537 	if (!interactive || (flags & LoadSavePlugin::lfInsertPage))
538 	{
539 		m_Doc->setPage(docWidth, docHeight, 0, 0, 0, 0, 0, 0, false, false);
540 		m_Doc->addPage(0);
541 		m_Doc->view()->addPage(0, true);
542 		baseX = 0;
543 		baseY = 0;
544 	}
545 	else
546 	{
547 		if (!m_Doc || (flags & LoadSavePlugin::lfCreateDoc))
548 		{
549 			m_Doc=ScCore->primaryMainWindow()->doFileNew(docWidth, docHeight, 0, 0, 0, 0, 0, 0, false, false, 0, false, 0, 1, "Custom", true);
550 			ScCore->primaryMainWindow()->HaveNewDoc();
551 			ret = true;
552 			baseX = 0;
553 			baseY = 0;
554 			baseX = m_Doc->currentPage()->xOffset();
555 			baseY = m_Doc->currentPage()->yOffset();
556 		}
557 	}
558 	if ((!ret) && (interactive))
559 	{
560 		baseX = m_Doc->currentPage()->xOffset();
561 		baseY = m_Doc->currentPage()->yOffset();
562 	}
563 	if ((ret) || (!interactive))
564 	{
565 		if (docWidth > docHeight)
566 			m_Doc->setPageOrientation(1);
567 		else
568 			m_Doc->setPageOrientation(0);
569 		m_Doc->setPageSize("Custom");
570 	}
571 	if ((!(flags & LoadSavePlugin::lfLoadAsPattern)) && (m_Doc->view() != nullptr))
572 		m_Doc->view()->deselectItems();
573 	Elements.clear();
574 	m_Doc->setLoading(true);
575 	m_Doc->DoDrawing = false;
576 	if ((!(flags & LoadSavePlugin::lfLoadAsPattern)) && (m_Doc->view() != nullptr))
577 		m_Doc->view()->updatesOn(false);
578 	m_Doc->scMW()->setScriptRunning(true);
579 	qApp->setOverrideCursor(QCursor(Qt::WaitCursor));
580 	QString CurDirP = QDir::currentPath();
581 	QDir::setCurrent(fi.path());
582 	if (convert(fNameIn))
583 	{
584 		tmpSel->clear();
585 		QDir::setCurrent(CurDirP);
586 		if ((Elements.count() > 1) && (!(importerFlags & LoadSavePlugin::lfCreateDoc)))
587 			m_Doc->groupObjectsList(Elements);
588 		m_Doc->DoDrawing = true;
589 		m_Doc->scMW()->setScriptRunning(false);
590 		m_Doc->setLoading(false);
591 		qApp->changeOverrideCursor(QCursor(Qt::ArrowCursor));
592 		if ((Elements.count() > 0) && (!ret) && (interactive))
593 		{
594 			if (flags & LoadSavePlugin::lfScripted)
595 			{
596 				bool loadF = m_Doc->isLoading();
597 				m_Doc->setLoading(false);
598 				m_Doc->changed();
599 				m_Doc->setLoading(loadF);
600 				if (!(flags & LoadSavePlugin::lfLoadAsPattern))
601 				{
602 					m_Doc->m_Selection->delaySignalsOn();
603 					for (int dre=0; dre<Elements.count(); ++dre)
604 					{
605 						m_Doc->m_Selection->addItem(Elements.at(dre), true);
606 					}
607 					m_Doc->m_Selection->delaySignalsOff();
608 					m_Doc->m_Selection->setGroupRect();
609 					if (m_Doc->view() != nullptr)
610 						m_Doc->view()->updatesOn(true);
611 				}
612 			}
613 			else
614 			{
615 				m_Doc->DragP = true;
616 				m_Doc->DraggedElem = nullptr;
617 				m_Doc->DragElements.clear();
618 				m_Doc->m_Selection->delaySignalsOn();
619 				for (int dre=0; dre<Elements.count(); ++dre)
620 				{
621 					tmpSel->addItem(Elements.at(dre), true);
622 				}
623 				tmpSel->setGroupRect();
624 				ScElemMimeData* md = ScriXmlDoc::writeToMimeData(m_Doc, tmpSel);
625 				m_Doc->itemSelection_DeleteItem(tmpSel);
626 				m_Doc->view()->updatesOn(true);
627 				m_Doc->m_Selection->delaySignalsOff();
628 				// We must copy the TransationSettings object as it is owned
629 				// by handleObjectImport method afterwards
630 				TransactionSettings* transacSettings = new TransactionSettings(trSettings);
631 				m_Doc->view()->handleObjectImport(md, transacSettings);
632 				m_Doc->DragP = false;
633 				m_Doc->DraggedElem = nullptr;
634 				m_Doc->DragElements.clear();
635 			}
636 		}
637 		else
638 		{
639 			m_Doc->changed();
640 			m_Doc->reformPages();
641 			if (!(flags & LoadSavePlugin::lfLoadAsPattern))
642 				m_Doc->view()->updatesOn(true);
643 		}
644 		success = true;
645 	}
646 	else
647 	{
648 		QDir::setCurrent(CurDirP);
649 		m_Doc->DoDrawing = true;
650 		m_Doc->scMW()->setScriptRunning(false);
651 		m_Doc->view()->updatesOn(true);
652 		qApp->changeOverrideCursor(QCursor(Qt::ArrowCursor));
653 	}
654 	if (interactive)
655 		m_Doc->setLoading(false);
656 	//CB If we have a gui we must refresh it if we have used the progressbar
657 	if (!(flags & LoadSavePlugin::lfLoadAsPattern))
658 	{
659 		if ((showProgress) && (!interactive))
660 			m_Doc->view()->DrawNew();
661 	}
662 	qApp->restoreOverrideCursor();
663 	return success;
664 }
665 
~WpgPlug()666 WpgPlug::~WpgPlug()
667 {
668 	delete progressDialog;
669 	delete tmpSel;
670 }
671 
convert(const QString & fn)672 bool WpgPlug::convert(const QString& fn)
673 {
674 	importedColors.clear();
675 
676 	QFile file(fn);
677 	if (!file.exists())
678 	{
679 		qDebug() << "File " << QFile::encodeName(fn).data() << " does not exist";
680 		return false;
681 	}
682 	if (!file.open( QIODevice::ReadOnly))
683 	{
684 		qDebug() << "Cannot open file " << QFile::encodeName(fn).data();
685 		return false;
686 	}
687 	QByteArray ba = file.readAll();
688 	file.close();
689 
690 	libwpg::WPGMemoryStream input(ba.constData(), ba.size());
691 	if (!libwpg::WPGraphics::isSupported(&input))
692 	{
693 		fprintf(stderr, "ERROR: Unsupported file format (unsupported version) or file is encrypted!\n");
694 		return false;
695 	}
696 	ScrPainter painter;
697 	painter.m_Doc = m_Doc;
698 	painter.baseX = baseX;
699 	painter.baseY = baseY;
700 	painter.flags = importerFlags;
701 	libwpg::WPGraphics::parse(&input, &painter);
702 	Elements = painter.Elements;
703 	importedColors = painter.importedColors;
704 	if (Elements.count() == 0)
705 	{
706 		if (importedColors.count() != 0)
707 		{
708 			for (int cd = 0; cd < importedColors.count(); cd++)
709 			{
710 				m_Doc->PageColors.remove(importedColors[cd]);
711 			}
712 		}
713 	}
714 	if (progressDialog)
715 		progressDialog->close();
716 	return true;
717 }
718