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