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 "importshape.h"
10 #include "importshapeplugin.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
importshape_getPluginAPIVersion()23 int importshape_getPluginAPIVersion()
24 {
25 return PLUGIN_API_VERSION;
26 }
27
importshape_getPlugin()28 ScPlugin* importshape_getPlugin()
29 {
30 ImportShapePlugin* plug = new ImportShapePlugin();
31 Q_CHECK_PTR(plug);
32 return plug;
33 }
34
importshape_freePlugin(ScPlugin * plugin)35 void importshape_freePlugin(ScPlugin* plugin)
36 {
37 ImportShapePlugin* plug = qobject_cast<ImportShapePlugin*>(plugin);
38 Q_ASSERT(plug);
39 delete plug;
40 }
41
ImportShapePlugin()42 ImportShapePlugin::ImportShapePlugin() :
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 ImportShapePlugin::languageChange()
52 {
53 importAction->setText( tr("Import Dia Shapes..."));
54 FileFormat* fmt = getFormatByExt("shape");
55 fmt->trName = tr("Dia Shapes");
56 fmt->filter = tr("Dia Shapes (*.shape *.SHAPE)");
57 }
58
~ImportShapePlugin()59 ImportShapePlugin::~ImportShapePlugin()
60 {
61 unregisterAll();
62 }
63
fullTrName() const64 QString ImportShapePlugin::fullTrName() const
65 {
66 return QObject::tr("Shape Importer");
67 }
68
69
getAboutData() const70 const ScActionPlugin::AboutData* ImportShapePlugin::getAboutData() const
71 {
72 AboutData* about = new AboutData;
73 about->authors = "Franz Schmid <franz@scribus.info>";
74 about->shortDescription = tr("Imports Dia shape Files");
75 about->description = tr("Imports most Dia shapes 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 ImportShapePlugin::deleteAboutData(const AboutData* about) const
82 {
83 Q_ASSERT(about);
84 delete about;
85 }
86
registerFormats()87 void ImportShapePlugin::registerFormats()
88 {
89 FileFormat fmt(this);
90 fmt.trName = tr("Dia Shapes"); // Human readable name
91 fmt.formatId = 0;
92 fmt.filter = tr("Dia shapes (*.shape *.SHAPE)"); // QFileDialog filter
93 fmt.fileExtensions = QStringList() << "shape";
94 fmt.load = true;
95 fmt.save = false;
96 fmt.thumb = true;
97 fmt.colorReading = true;
98 fmt.mimeTypes = QStringList(); // MIME types
99 fmt.priority = 64; // Priority
100 registerFormat(fmt);
101 }
102
fileSupported(QIODevice *,const QString & fileName) const103 bool ImportShapePlugin::fileSupported(QIODevice* /* file */, const QString & fileName) const
104 {
105 return true;
106 }
107
loadFile(const QString & fileName,const FileFormat &,int flags,int)108 bool ImportShapePlugin::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 ImportShapePlugin::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("importshape");
122 QString wdir = prefs->get("wdir", ".");
123 CustomFDialog diaf(ScCore->primaryMainWindow(), wdir, QObject::tr("Open"), tr("All Supported Formats")+" (*.shape *.SHAPE);;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;
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::ImportShape;
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 = UndoManager::instance()->beginTransaction(trSettings);
146 ShapePlug *dia = new ShapePlug(m_Doc, flags);
147 Q_CHECK_PTR(dia);
148 dia->import(fileName, trSettings, flags, !(flags & lfScripted));
149 if (activeTransaction)
150 activeTransaction.commit();
151 if (emptyDoc || !(flags & lfInteractive) || !(flags & lfScripted))
152 UndoManager::instance()->setUndoEnabled(true);
153 delete dia;
154 return true;
155 }
156
readThumbnail(const QString & fileName)157 QImage ImportShapePlugin::readThumbnail(const QString& fileName)
158 {
159 if (fileName.isEmpty())
160 return QImage();
161 UndoManager::instance()->setUndoEnabled(false);
162 m_Doc = nullptr;
163 ShapePlug *dia = new ShapePlug(m_Doc, lfCreateThumbnail);
164 Q_CHECK_PTR(dia);
165 QImage ret = dia->readThumbnail(fileName);
166 UndoManager::instance()->setUndoEnabled(true);
167 delete dia;
168 return ret;
169 }
170