Back to index

scribus-ng  1.3.4.dfsg+svn20071115
export.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 "export.h"
00008 #include "dialog.h"
00009 #include "export.moc"
00010 #include <qpixmap.h>
00011 #include <qstring.h>
00012 #include <qdir.h>
00013 #include <qcursor.h>
00014 
00015 #include "scmessagebox.h"
00016 #include "scribus.h"
00017 #include "scraction.h"
00018 #include "menumanager.h"
00019 #include "util.h"
00020 #include "commonstrings.h"
00021 #include "scpaths.h"
00022 
00023 int scribusexportpixmap_getPluginAPIVersion()
00024 {
00025        return PLUGIN_API_VERSION;
00026 }
00027 
00028 ScPlugin* scribusexportpixmap_getPlugin()
00029 {
00030        PixmapExportPlugin* plug = new PixmapExportPlugin();
00031        Q_CHECK_PTR(plug);
00032        return plug;
00033 }
00034 
00035 void scribusexportpixmap_freePlugin(ScPlugin* plugin)
00036 {
00037        PixmapExportPlugin* plug = dynamic_cast<PixmapExportPlugin*>(plugin);
00038        Q_ASSERT(plug);
00039        delete plug;
00040 }
00041 
00042 PixmapExportPlugin::PixmapExportPlugin() : ScActionPlugin()
00043 {
00044        // Set action info in languageChange, so we only have to do
00045        // it in one place. This includes registering file formats.
00046        languageChange();
00047 }
00048 
00049 PixmapExportPlugin::~PixmapExportPlugin()
00050 {
00051 };
00052 
00053 void PixmapExportPlugin::languageChange()
00054 {
00055        // Note that we leave the unused members unset. They'll be initialised
00056        // with their default ctors during construction.
00057        // Action name
00058        m_actionInfo.name = "ExportAsImage";
00059        // Action text for menu, including accel
00060        m_actionInfo.text = tr("Save as &Image...");
00061        // Keyboard shortcut
00062        m_actionInfo.keySequence = "CTRL+SHIFT+E";
00063        // Menu
00064        m_actionInfo.menu = "FileExport";
00065        m_actionInfo.enabledOnStartup = true;
00066 }
00067 
00068 const QString PixmapExportPlugin::fullTrName() const
00069 {
00070        return tr("Export As Image");
00071 }
00072 
00073 const ScActionPlugin::AboutData* PixmapExportPlugin::getAboutData() const
00074 {
00075        AboutData* about = new AboutData;
00076        Q_CHECK_PTR(about);
00077        about->authors = QString::fromUtf8("Petr Van\xc4\x9bk <petr@scribus.info>");
00078        about->shortDescription = tr("Export As Image");
00079        about->description = tr("Exports selected pages as bitmap images.");
00080        // about->version
00081        // about->releaseDate
00082        // about->copyright
00083        about->license = "GPL";
00084        return about;
00085 }
00086 
00087 void PixmapExportPlugin::deleteAboutData(const AboutData* about) const
00088 {
00089        Q_ASSERT(about);
00090        delete about;
00091 }
00092 
00093 bool PixmapExportPlugin::run(ScribusDoc* doc, QString target)
00094 {
00095        Q_ASSERT(target.isEmpty());
00096        Q_ASSERT(!doc->masterPageMode());
00097        ExportBitmap *ex = new ExportBitmap();
00098        ExportForm *dia = new ExportForm(doc->scMW(), doc, ex->pageDPI, ex->quality, ex->bitmapType);
00099 
00100        // interval widgets handling
00101        QString tmp;
00102        dia->RangeVal->setText(tmp.setNum(doc->currentPageNumber()+1));
00103        // main "loop"
00104        if (dia->exec()==QDialog::Accepted)
00105        {
00106               QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
00107               std::vector<int> pageNs;
00108               ex->pageDPI = dia->DPIBox->value();
00109               ex->enlargement = dia->EnlargementBox->value();
00110               ex->quality = dia->QualityBox->value();
00111               ex->exportDir = ScPaths::separatorsToSlashes(dia->OutputDirectory->text());
00112               ex->bitmapType = dia->bitmapType;
00113               doc->scMW()->mainWindowProgressBar->reset();
00114               bool res;
00115               if (dia->OnePageRadio->isChecked())
00116                      res = ex->exportCurrent(doc);
00117               else
00118               {
00119                      if (dia->AllPagesRadio->isChecked())
00120                             parsePagesString("*", &pageNs, doc->DocPages.count());
00121                      else
00122                             parsePagesString(dia->RangeVal->text(), &pageNs, doc->DocPages.count());
00123                      res = ex->exportInterval(doc, pageNs);
00124               }
00125               doc->scMW()->mainWindowProgressBar->reset();
00126               QApplication::restoreOverrideCursor();
00127               if (!res)
00128               {
00129                      QMessageBox::warning(doc->scMW(),  tr("Save as Image"), tr("Error writing the output file(s)."));
00130                      doc->scMW()->setStatusBarInfoText( tr("Error writing the output file(s)."));
00131               }
00132               else
00133                      doc->scMW()->setStatusBarInfoText( tr("Export successful"));
00134        }
00135        // clean the trash
00136        delete ex;
00137        delete dia;
00138        return true;
00139 }
00140 
00141 
00142 ExportBitmap::ExportBitmap()
00143 {
00144        pageDPI = 72;
00145        quality = 100;
00146        enlargement = 100.0;
00147        exportDir = QDir::currentDirPath();
00148        bitmapType = QString("PNG");
00149        overwrite = false;
00150 }
00151 
00152 QString ExportBitmap::getFileName(ScribusDoc* doc, uint pageNr)
00153 {
00154        return QDir::cleanDirPath(QDir::convertSeparators(exportDir + "/" + getFileNameByPage(doc, pageNr, bitmapType.lower())));
00155 }
00156 
00157 ExportBitmap::~ExportBitmap()
00158 {
00159 }
00160 
00161 bool ExportBitmap::exportPage(ScribusDoc* doc, uint pageNr, bool single = true)
00162 {
00163        uint over = 0;
00164        QString fileName(getFileName(doc, pageNr));
00165 
00166        if (!doc->Pages->at(pageNr))
00167               return false;
00168        Page* page = doc->Pages->at(pageNr);
00169 
00170        /* a little magic here - I need to compute the "maxGr" value...
00171        * We need to know the right size of the page for landscape,
00172        * portrait and user defined sizes.
00173        */
00174        double pixmapSize = (page->height() > page->width()) ? page->height() : page->width();
00175        QImage im(doc->view()->PageToPixmap(pageNr, qRound(pixmapSize * enlargement * (pageDPI / 72.0) / 100.0), false));
00176        int dpm = qRound(100.0 / 2.54 * pageDPI);
00177        im.setDotsPerMeterY(dpm);
00178        im.setDotsPerMeterX(dpm);
00179        if (QFile::exists(fileName) && !overwrite)
00180        {
00181               QString fn = QDir::convertSeparators(fileName);
00182               QApplication::restoreOverrideCursor();
00183               over = ScMessageBox::question(doc->scMW(), tr("File exists. Overwrite?"),
00184                             fn +"\n"+ tr("exists already. Overwrite?"),
00185                             CommonStrings::trYes, CommonStrings::trNo,
00186                             // hack for multiple overwritting (petr) 
00187                             (single==true) ? QString::null : tr("All"), 0, 0);
00188               QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
00189               if (over == 0)
00190                      return im.save(fileName, bitmapType, quality);
00191               if (over == 2)
00192                      overwrite = true;
00193        }
00194        return im.save(fileName, bitmapType, quality);
00195 }
00196 
00197 bool ExportBitmap::exportCurrent(ScribusDoc* doc)
00198 {
00199        return exportPage(doc, doc->currentPageNumber(), true);
00200 }
00201 
00202 bool ExportBitmap::exportInterval(ScribusDoc* doc, std::vector<int> &pageNs)
00203 {
00204        doc->scMW()->mainWindowProgressBar->setTotalSteps(pageNs.size());
00205        for (uint a = 0; a < pageNs.size(); ++a)
00206        {
00207               doc->scMW()->mainWindowProgressBar->setProgress(a);
00208               if (!exportPage(doc, pageNs[a]-1, false))
00209                      return false;
00210        }
00211        return true;
00212 }