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 							 -------------------
9 	begin                : Sat Mar 7 2015
10 	copyright            : (C) 2015 by Franz Schmid
11 	email                : Franz.Schmid@altmuehlnet.de
12  ***************************************************************************/
13 #include "commonstrings.h"
14 
15 #include "importsvm.h"
16 #include "importsvmplugin.h"
17 #include "prefscontext.h"
18 #include "prefsfile.h"
19 #include "prefsmanager.h"
20 #include "scpage.h"
21 #include "scraction.h"
22 #include "scribuscore.h"
23 #include "undomanager.h"
24 #include "util_formats.h"
25 
26 #include "ui/customfdialog.h"
27 #include "ui/scmwmenumanager.h"
28 
importsvm_getPluginAPIVersion()29 int importsvm_getPluginAPIVersion()
30 {
31 	return PLUGIN_API_VERSION;
32 }
33 
importsvm_getPlugin()34 ScPlugin* importsvm_getPlugin()
35 {
36 	ImportSvmPlugin* plug = new ImportSvmPlugin();
37 	Q_CHECK_PTR(plug);
38 	return plug;
39 }
40 
importsvm_freePlugin(ScPlugin * plugin)41 void importsvm_freePlugin(ScPlugin* plugin)
42 {
43 	ImportSvmPlugin* plug = qobject_cast<ImportSvmPlugin*>(plugin);
44 	Q_ASSERT(plug);
45 	delete plug;
46 }
47 
ImportSvmPlugin()48 ImportSvmPlugin::ImportSvmPlugin() :
49 	importAction(new ScrAction(ScrAction::DLL, "", QKeySequence(), this))
50 {
51 	// Set action info in languageChange, so we only have to do it in one
52 	// place. This includes registering file format support.
53 	registerFormats();
54 	languageChange();
55 }
56 
languageChange()57 void ImportSvmPlugin::languageChange()
58 {
59 	importAction->setText( tr("Import SVM..."));
60 	FileFormat* fmt = getFormatByExt("svm");
61 	fmt->trName = tr("SVM");
62 	fmt->filter = tr("SVM (*.svm *.svm)");
63 }
64 
~ImportSvmPlugin()65 ImportSvmPlugin::~ImportSvmPlugin()
66 {
67 	unregisterAll();
68 }
69 
fullTrName() const70 QString ImportSvmPlugin::fullTrName() const
71 {
72 	return QObject::tr("SVM Importer");
73 }
74 
75 
getAboutData() const76 const ScActionPlugin::AboutData* ImportSvmPlugin::getAboutData() const
77 {
78 	AboutData* about = new AboutData;
79 	about->authors = "Franz Schmid <franz@scribus.info>";
80 	about->shortDescription = tr("Imports SVM Files");
81 	about->description = tr("Imports most SVM files into the current document, converting their vector data into Scribus objects.");
82 	about->license = "GPL";
83 	Q_CHECK_PTR(about);
84 	return about;
85 }
86 
deleteAboutData(const AboutData * about) const87 void ImportSvmPlugin::deleteAboutData(const AboutData* about) const
88 {
89 	Q_ASSERT(about);
90 	delete about;
91 }
92 
registerFormats()93 void ImportSvmPlugin::registerFormats()
94 {
95 	FileFormat fmt(this);
96 	fmt.trName = tr("SVM");
97 	fmt.filter = tr("SVM (*.svm *.svm)");
98 	fmt.formatId = 0;
99 	fmt.fileExtensions = QStringList() << "svm";
100 	fmt.load = true;
101 	fmt.save = false;
102 	fmt.thumb = true;
103 	fmt.mimeTypes = QStringList(); // MIME types
104 	fmt.priority = 64; // Priority
105 	registerFormat(fmt);
106 }
107 
fileSupported(QIODevice *,const QString & fileName) const108 bool ImportSvmPlugin::fileSupported(QIODevice* /* file */, const QString & fileName) const
109 {
110 	return true;
111 }
112 
loadFile(const QString & fileName,const FileFormat &,int flags,int)113 bool ImportSvmPlugin::loadFile(const QString & fileName, const FileFormat &, int flags, int /*index*/)
114 {
115 	// There's only one format to handle, so we just call import(...)
116 	return import(fileName, flags);
117 }
118 
import(QString fileName,int flags)119 bool ImportSvmPlugin::import(QString fileName, int flags)
120 {
121 	if (!checkFlags(flags))
122 		return false;
123 	if (fileName.isEmpty())
124 	{
125 		flags |= lfInteractive;
126 		PrefsContext* prefs = PrefsManager::instance().prefsFile->getPluginContext("importsvm");
127 		QString wdir = prefs->get("wdir", ".");
128 		CustomFDialog diaf(ScCore->primaryMainWindow(), wdir, QObject::tr("Open"), tr("All Supported Formats")+" (*.svm *.SVM);;All Files (*)");
129 		if (diaf.exec())
130 		{
131 			fileName = diaf.selectedFile();
132 			prefs->set("wdir", fileName.left(fileName.lastIndexOf("/")));
133 		}
134 		else
135 			return true;
136 	}
137 	if (m_Doc == nullptr)
138 		m_Doc=ScCore->primaryMainWindow()->doc;
139 	UndoTransaction* activeTransaction = nullptr;
140 	bool emptyDoc = (m_Doc == nullptr);
141 	bool hasCurrentPage = (m_Doc && m_Doc->currentPage());
142 	TransactionSettings trSettings;
143 	trSettings.targetName   = hasCurrentPage ? m_Doc->currentPage()->getUName() : "";
144 	trSettings.targetPixmap = Um::IImageFrame;
145 	trSettings.actionName   = Um::ImportSVM;
146 	trSettings.description  = fileName;
147 	trSettings.actionPixmap = Um::IXFIG;
148 	if (emptyDoc || !(flags & lfInteractive) || !(flags & lfScripted))
149 		UndoManager::instance()->setUndoEnabled(false);
150 	if (UndoManager::undoEnabled())
151 		activeTransaction = new UndoTransaction(UndoManager::instance()->beginTransaction(trSettings));
152 	SvmPlug *dia = new SvmPlug(m_Doc, flags);
153 	Q_CHECK_PTR(dia);
154 	dia->import(fileName, trSettings, flags, !(flags & lfScripted));
155 	if (activeTransaction)
156 	{
157 		activeTransaction->commit();
158 		delete activeTransaction;
159 		activeTransaction = nullptr;
160 	}
161 	if (emptyDoc || !(flags & lfInteractive) || !(flags & lfScripted))
162 		UndoManager::instance()->setUndoEnabled(true);
163 	delete dia;
164 	return true;
165 }
166 
readThumbnail(const QString & fileName)167 QImage ImportSvmPlugin::readThumbnail(const QString& fileName)
168 {
169 	if (fileName.isEmpty())
170 		return QImage();
171 	UndoManager::instance()->setUndoEnabled(false);
172 	m_Doc = nullptr;
173 	SvmPlug *dia = new SvmPlug(m_Doc, lfCreateThumbnail);
174 	Q_CHECK_PTR(dia);
175 	QImage ret = dia->readThumbnail(fileName);
176 	UndoManager::instance()->setUndoEnabled(true);
177 	delete dia;
178 	return ret;
179 }
180