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 "importpm.h"
10 #include "importpmplugin.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
importpm_getPluginAPIVersion()23 int importpm_getPluginAPIVersion()
24 {
25 return PLUGIN_API_VERSION;
26 }
27
importpm_getPlugin()28 ScPlugin* importpm_getPlugin()
29 {
30 ImportPmPlugin* plug = new ImportPmPlugin();
31 Q_CHECK_PTR(plug);
32 return plug;
33 }
34
importpm_freePlugin(ScPlugin * plugin)35 void importpm_freePlugin(ScPlugin* plugin)
36 {
37 ImportPmPlugin* plug = qobject_cast<ImportPmPlugin*>(plugin);
38 Q_ASSERT(plug);
39 delete plug;
40 }
41
ImportPmPlugin()42 ImportPmPlugin::ImportPmPlugin() :
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 ImportPmPlugin::languageChange()
52 {
53 importAction->setText( tr("Import Pagemaker..."));
54 FileFormat* fmt = getFormatByExt("pmd");
55 fmt->trName = tr("Pagemaker");
56 fmt->filter = tr("Pagemaker (*.pmd *.PMD *.pm *.PM *.pm3 *.PM3 *.pm4 *.PM4 *.pm5 *.PM5 *.pm6 *.PM6 *.p65 *.P65)");
57 }
58
~ImportPmPlugin()59 ImportPmPlugin::~ImportPmPlugin()
60 {
61 unregisterAll();
62 }
63
fullTrName() const64 QString ImportPmPlugin::fullTrName() const
65 {
66 return QObject::tr("Pagemaker Importer");
67 }
68
69
getAboutData() const70 const ScActionPlugin::AboutData* ImportPmPlugin::getAboutData() const
71 {
72 AboutData* about = new AboutData;
73 about->authors = "Franz Schmid <franz@scribus.info>";
74 about->shortDescription = tr("Imports Pagemaker Files");
75 about->description = tr("Imports most Pagemaker 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 ImportPmPlugin::deleteAboutData(const AboutData* about) const
82 {
83 Q_ASSERT(about);
84 delete about;
85 }
86
registerFormats()87 void ImportPmPlugin::registerFormats()
88 {
89 FileFormat fmt(this);
90 fmt.trName = tr("Pagemaker");
91 fmt.filter = tr("Pagemaker (*.pmd *.PMD *.pm *.PM *.pm3 *.PM3 *.pm4 *.PM4 *.pm5 *.PM5 *.pm6 *.PM6 *.p65 *.P65)");
92 fmt.formatId = 0;
93 fmt.fileExtensions = QStringList() << "pmd" << "pm" << "pm3" << "pm4" << "pm5" << "pm6" << "p65";
94 fmt.load = true;
95 fmt.save = false;
96 fmt.thumb = true;
97 fmt.colorReading = true;
98 fmt.mimeTypes = QStringList();
99 fmt.priority = 64; // Priority
100 registerFormat(fmt);
101 }
102
fileSupported(QIODevice *,const QString & fileName) const103 bool ImportPmPlugin::fileSupported(QIODevice* /* file */, const QString & fileName) const
104 {
105 return true;
106 }
107
loadFile(const QString & fileName,const FileFormat &,int flags,int)108 bool ImportPmPlugin::loadFile(const QString & fileName, const FileFormat &, int flags, int /*index*/)
109 {
110 // There's only one format to handle, so we just call import(...)
111 return import(fileName, flags);
112 }
113
import(QString fileName,int flags)114 bool ImportPmPlugin::import(QString fileName, int flags)
115 {
116 if (!checkFlags(flags))
117 return false;
118 if (fileName.isEmpty())
119 {
120 flags |= lfInteractive;
121 PrefsContext* prefs = PrefsManager::instance().prefsFile->getPluginContext("importpm");
122 QString wdir = prefs->get("wdir", ".");
123 CustomFDialog diaf(ScCore->primaryMainWindow(), wdir, QObject::tr("Open"), tr("All Supported Formats")+" (*.pmd *.PMD *.pm *.PM *.pm3 *.PM3 *.pm4 *.PM4 *.pm5 *.PM5 *.pm6 *.PM6 *.p65 *.P65);;All Files (*)");
124 if (diaf.exec())
125 {
126 fileName = diaf.selectedFile();
127 prefs->set("wdir", fileName.left(fileName.lastIndexOf("/")));
128 }
129 else
130 return true;
131 }
132 m_Doc=ScCore->primaryMainWindow()->doc;
133 UndoTransaction* activeTransaction = nullptr;
134 bool emptyDoc = (m_Doc == nullptr);
135 bool hasCurrentPage = (m_Doc && m_Doc->currentPage());
136 TransactionSettings trSettings;
137 trSettings.targetName = hasCurrentPage ? m_Doc->currentPage()->getUName() : "";
138 trSettings.targetPixmap = Um::IImageFrame;
139 trSettings.actionName = Um::ImportPageMaker;
140 trSettings.description = fileName;
141 trSettings.actionPixmap = Um::IXFIG;
142 if (emptyDoc || !(flags & lfInteractive) || !(flags & lfScripted))
143 UndoManager::instance()->setUndoEnabled(false);
144 if (UndoManager::undoEnabled())
145 activeTransaction = new UndoTransaction(UndoManager::instance()->beginTransaction(trSettings));
146 PmPlug *dia = new PmPlug(m_Doc, flags);
147 Q_CHECK_PTR(dia);
148 dia->import(fileName, trSettings, flags, !(flags & lfScripted));
149 if (activeTransaction)
150 {
151 activeTransaction->commit();
152 delete activeTransaction;
153 activeTransaction = nullptr;
154 }
155 if (emptyDoc || !(flags & lfInteractive) || !(flags & lfScripted))
156 UndoManager::instance()->setUndoEnabled(true);
157 delete dia;
158 return true;
159 }
160
readThumbnail(const QString & fileName)161 QImage ImportPmPlugin::readThumbnail(const QString& fileName)
162 {
163 if (fileName.isEmpty())
164 return QImage();
165 UndoManager::instance()->setUndoEnabled(false);
166 m_Doc = nullptr;
167 PmPlug *dia = new PmPlug(m_Doc, lfCreateThumbnail);
168 Q_CHECK_PTR(dia);
169 QImage ret = dia->readThumbnail(fileName);
170 UndoManager::instance()->setUndoEnabled(true);
171 delete dia;
172 return ret;
173 }
174