Back to index

scribus-ng  1.3.4.dfsg+svn20071115
importpsplugin.cpp
Go to the documentation of this file.
00001 /*
00002 For general Scribus (>=1.3.2) copyright and licensing information please refer
00003 to the COPYING file provided with the program. Following this notice may exist
00004 a copyright and/or license notice that predates the release of Scribus 1.3.2
00005 for which a new license (GPL+exception) is in place.
00006 */
00007 #include "importpsplugin.h"
00008 #include "importpsplugin.moc"
00009 #include "importps.h"
00010 #include "scribuscore.h"
00011 #include "page.h"
00012 #include "prefsmanager.h"
00013 #include "prefscontext.h"
00014 #include "prefsfile.h"
00015 #include "undomanager.h"
00016 #include "customfdialog.h"
00017 #include "scraction.h"
00018 #include "menumanager.h"
00019 
00020 int importps_getPluginAPIVersion()
00021 {
00022        return PLUGIN_API_VERSION;
00023 }
00024 
00025 ScPlugin* importps_getPlugin()
00026 {
00027        ImportPSPlugin* plug = new ImportPSPlugin();
00028        Q_CHECK_PTR(plug);
00029        return plug;
00030 }
00031 
00032 void importps_freePlugin(ScPlugin* plugin)
00033 {
00034        ImportPSPlugin* plug = dynamic_cast<ImportPSPlugin*>(plugin);
00035        Q_ASSERT(plug);
00036        delete plug;
00037 }
00038 
00039 ImportPSPlugin::ImportPSPlugin() : LoadSavePlugin(),
00040        importAction(new ScrAction(ScrAction::DLL, QIconSet(), "", QKeySequence(), this, "ImportPS"))
00041 {
00042        // Set action info in languageChange, so we only have to do it in one
00043        // place. This includes registering file format support.
00044        languageChange();
00045 }
00046 
00047 void ImportPSPlugin::addToMainWindowMenu(ScribusMainWindow *mw)
00048 {
00049        importAction->setEnabled(true);
00050        connect( importAction, SIGNAL(activated()), SLOT(import()) );
00051        mw->scrMenuMgr->addMenuItem(importAction, "FileImport");
00052 }
00053 
00054 void ImportPSPlugin::languageChange()
00055 {
00056        importAction->setMenuText( tr("Import &EPS/PS..."));
00057        // (Re)register file format support
00058        unregisterAll();
00059        registerFormats();
00060 }
00061 
00062 ImportPSPlugin::~ImportPSPlugin()
00063 {
00064        unregisterAll();
00065 };
00066 
00067 const QString ImportPSPlugin::fullTrName() const
00068 {
00069        return QObject::tr("PS/EPS Importer");
00070 }
00071 
00072 
00073 const ScActionPlugin::AboutData* ImportPSPlugin::getAboutData() const
00074 {
00075        AboutData* about = new AboutData;
00076        about->authors = "Franz Schmid <franz@scribus.info>";
00077        about->shortDescription = tr("Imports EPS Files");
00078        about->description = tr("Imports most EPS files into the current document,\nconverting their vector data into Scribus objects.");
00079        about->license = "GPL";
00080        Q_CHECK_PTR(about);
00081        return about;
00082 }
00083 
00084 void ImportPSPlugin::deleteAboutData(const AboutData* about) const
00085 {
00086        Q_ASSERT(about);
00087        delete about;
00088 }
00089 
00090 void ImportPSPlugin::registerFormats()
00091 {
00092        QString psName = tr("PostScript");
00093        FileFormat fmt(this);
00094        fmt.trName = psName; // Human readable name
00095        fmt.formatId = FORMATID_PSIMPORT;
00096        fmt.filter = psName + " (*.ps *.PS *.eps *.EPS *.epsi *.EPSI)"; // QFileDialog filter
00097        fmt.nameMatch = QRegExp("\\.(ps|eps|epsi)$", false);
00098        fmt.load = true;
00099        fmt.save = false;
00100        fmt.mimeTypes = QStringList("application/postscript"); // MIME types
00101        fmt.priority = 64; // Priority
00102        registerFormat(fmt);
00103 
00104        QString pdfName = tr("PDF");
00105        FileFormat fmt2(this);
00106        fmt2.trName = pdfName; // Human readable name
00107        fmt2.formatId = FORMATID_PDFIMPORT;
00108        fmt2.filter = pdfName + " (*.pdf *.PDF)"; // QFileDialog filter
00109        fmt2.nameMatch = QRegExp("\\.pdf$", false);
00110        fmt2.load = true;
00111        fmt2.save = false;
00112        fmt2.mimeTypes = QStringList("application/pdf"); // MIME types
00113        fmt2.priority = 64; // Priority
00114        registerFormat(fmt2);
00115 }
00116 
00117 bool ImportPSPlugin::fileSupported(QIODevice* /* file */, const QString & fileName) const
00118 {
00119        // TODO: check for %!PS-Adobe
00120        return true;
00121 }
00122 
00123 bool ImportPSPlugin::loadFile(const QString & fileName, const FileFormat &, int flags, int /*index*/)
00124 {
00125        // There's only one format to handle, so we just call import(...)
00126        return import(fileName, flags);
00127 }
00128 
00129 bool ImportPSPlugin::import(QString fileName, int flags)
00130 {
00131        if (!checkFlags(flags))
00132               return false;
00133        if (!(flags & lfInteractive))
00134               UndoManager::instance()->setUndoEnabled(false);
00135        if( fileName.isEmpty() )
00136        {
00137               flags |= lfInteractive;
00138               PrefsContext* prefs = PrefsManager::instance()->prefsFile->getPluginContext("importps");
00139               QString wdir = prefs->get("wdir", ".");
00140               QString formats = QObject::tr("All Supported Formats (*.eps *.EPS *.epsi *.EPSI *.ps *.PS);;");
00141               formats += "EPS (*.eps *.EPS);;EPSI (*.epsi *.EPSI);;PS (*.ps *.PS);;" + QObject::tr("All Files (*)");
00142               CustomFDialog diaf(ScCore->primaryMainWindow(), wdir, QObject::tr("Open"), formats);
00143               if (diaf.exec())
00144               {
00145                      fileName = diaf.selectedFile();
00146                      prefs->set("wdir", fileName.left(fileName.findRev("/")));
00147               }
00148               else
00149                      return true;
00150        }
00151        m_Doc=ScCore->primaryMainWindow()->doc;
00152        if (UndoManager::undoEnabled() && m_Doc)
00153        {
00154               UndoManager::instance()->beginTransaction(m_Doc->currentPage()->getUName(),
00155                                                                                     Um::IImageFrame,
00156                                                                                     Um::ImportEPS,
00157                                                                                     fileName, Um::IEPS);
00158        }
00159        else if (UndoManager::undoEnabled() && !m_Doc)
00160               UndoManager::instance()->setUndoEnabled(false);
00161        EPSPlug *dia = new EPSPlug(m_Doc, flags);
00162        Q_CHECK_PTR(dia);
00163        dia->import(fileName, flags);
00164        if (UndoManager::undoEnabled())
00165               UndoManager::instance()->commit();
00166        else
00167               UndoManager::instance()->setUndoEnabled(true);
00168        delete dia;
00169        return true;
00170 }