export.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007 #include "export.h"
00008 #include "dialog.h"
00009
00010 #include <QPixmap>
00011 #include <QString>
00012 #include <QDir>
00013 #include <QCursor>
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
00045
00046 languageChange();
00047 }
00048
00049 PixmapExportPlugin::~PixmapExportPlugin()
00050 {
00051 };
00052
00053 void PixmapExportPlugin::languageChange()
00054 {
00055
00056
00057
00058 m_actionInfo.name = "ExportAsImage";
00059
00060 m_actionInfo.text = tr("Save as &Image...");
00061
00062 m_actionInfo.keySequence = "CTRL+SHIFT+E";
00063
00064 m_actionInfo.menu = "FileExport";
00065 m_actionInfo.enabledOnStartup = true;
00066 m_actionInfo.needsNumObjects = -1;
00067 }
00068
00069 const QString PixmapExportPlugin::fullTrName() const
00070 {
00071 return tr("Export As Image");
00072 }
00073
00074 const ScActionPlugin::AboutData* PixmapExportPlugin::getAboutData() const
00075 {
00076 AboutData* about = new AboutData;
00077 Q_CHECK_PTR(about);
00078 about->authors = QString::fromUtf8("Petr Van\xc4\x9bk <petr@scribus.info>");
00079 about->shortDescription = tr("Export As Image");
00080 about->description = tr("Exports selected pages as bitmap images.");
00081
00082
00083
00084 about->license = "GPL";
00085 return about;
00086 }
00087
00088 void PixmapExportPlugin::deleteAboutData(const AboutData* about) const
00089 {
00090 Q_ASSERT(about);
00091 delete about;
00092 }
00093
00094 bool PixmapExportPlugin::run(ScribusDoc* doc, QString target)
00095 {
00096 Q_ASSERT(target.isEmpty());
00097 Q_ASSERT(!doc->masterPageMode());
00098 ExportBitmap *ex = new ExportBitmap();
00099 ExportForm *dia = new ExportForm(doc->scMW(), doc, ex->pageDPI, ex->quality, ex->bitmapType);
00100
00101
00102 QString tmp;
00103 dia->rangeVal->setText(tmp.setNum(doc->currentPageNumber()+1));
00104
00105 if (dia->exec()==QDialog::Accepted)
00106 {
00107 QApplication::changeOverrideCursor(QCursor(Qt::WaitCursor));
00108 std::vector<int> pageNs;
00109 ex->pageDPI = dia->DPIBox->value();
00110 ex->enlargement = dia->enlargementBox->value();
00111 ex->quality = dia->qualityBox->value();
00112 ex->exportDir = ScPaths::separatorsToSlashes(dia->outputDirectory->text());
00113 ex->bitmapType = dia->bitmapType->currentText();
00114 doc->scMW()->mainWindowProgressBar->reset();
00115 bool res;
00116 if (dia->onePageRadio->isChecked())
00117 res = ex->exportCurrent(doc);
00118 else
00119 {
00120 if (dia->allPagesRadio->isChecked())
00121 parsePagesString("*", &pageNs, doc->DocPages.count());
00122 else
00123 parsePagesString(dia->rangeVal->text(), &pageNs, doc->DocPages.count());
00124 res = ex->exportInterval(doc, pageNs);
00125 }
00126 doc->scMW()->mainWindowProgressBar->reset();
00127 QApplication::changeOverrideCursor(Qt::ArrowCursor);
00128
00129 if (res)
00130 doc->scMW()->setStatusBarInfoText( tr("Export successful"));
00131 }
00132
00133 delete ex;
00134 delete dia;
00135 return true;
00136 }
00137
00138
00139 ExportBitmap::ExportBitmap()
00140 {
00141 pageDPI = 72;
00142 quality = 100;
00143 enlargement = 100.0;
00144 exportDir = QDir::currentPath();
00145 bitmapType = QString("PNG");
00146 overwrite = false;
00147 }
00148
00149 QString ExportBitmap::getFileName(ScribusDoc* doc, uint pageNr)
00150 {
00151 return QDir::cleanPath(QDir::convertSeparators(exportDir + "/" + getFileNameByPage(doc, pageNr, bitmapType.toLower())));
00152 }
00153
00154 ExportBitmap::~ExportBitmap()
00155 {
00156 }
00157
00158 bool ExportBitmap::exportPage(ScribusDoc* doc, uint pageNr, bool single = true)
00159 {
00160 uint over = 0;
00161 bool saved = false, doFileSave = true;
00162 QString fileName(getFileName(doc, pageNr));
00163
00164 if (!doc->Pages->at(pageNr))
00165 return false;
00166 Page* page = doc->Pages->at(pageNr);
00167
00168
00169
00170
00171
00172 double pixmapSize = (page->height() > page->width()) ? page->height() : page->width();
00173 QImage im(doc->view()->PageToPixmap(pageNr, qRound(pixmapSize * enlargement * (pageDPI / 72.0) / 100.0), false));
00174 if (im.isNull())
00175 {
00176 QMessageBox::warning(doc->scMW(), tr("Save as Image"), tr("Insufficient memory for this image size."));
00177 doc->scMW()->setStatusBarInfoText( tr("Insufficient memory for this image size."));
00178 return false;
00179 }
00180 int dpm = qRound(100.0 / 2.54 * pageDPI);
00181 im.setDotsPerMeterY(dpm);
00182 im.setDotsPerMeterX(dpm);
00183 if (QFile::exists(fileName) && !overwrite)
00184 {
00185 doFileSave = false;
00186 QString fn = QDir::convertSeparators(fileName);
00187
00188 QApplication::changeOverrideCursor(Qt::ArrowCursor);
00189 over = QMessageBox::question(doc->scMW(), tr("File exists. Overwrite?"),
00190 fn +"\n"+ tr("exists already. Overwrite?"),
00191
00192 (single == true) ? QMessageBox::Yes | QMessageBox::No : QMessageBox::Yes | QMessageBox::No | QMessageBox::YesToAll);
00193 QApplication::changeOverrideCursor(QCursor(Qt::WaitCursor));
00194 if (over == QMessageBox::Yes || over == QMessageBox::YesToAll)
00195 doFileSave = true;
00196 if (over == QMessageBox::YesToAll)
00197 overwrite = true;
00198 }
00199 if (doFileSave)
00200 saved = im.save(fileName, bitmapType.toLocal8Bit().constData(), quality);
00201 if (!saved && doFileSave)
00202 {
00203 QMessageBox::warning(doc->scMW(), tr("Save as Image"), tr("Error writing the output file(s)."));
00204 doc->scMW()->setStatusBarInfoText( tr("Error writing the output file(s)."));
00205 }
00206 return saved;
00207 }
00208
00209 bool ExportBitmap::exportCurrent(ScribusDoc* doc)
00210 {
00211 return exportPage(doc, doc->currentPageNumber(), true);
00212 }
00213
00214 bool ExportBitmap::exportInterval(ScribusDoc* doc, std::vector<int> &pageNs)
00215 {
00216 doc->scMW()->mainWindowProgressBar->setMaximum(pageNs.size());
00217 for (uint a = 0; a < pageNs.size(); ++a)
00218 {
00219 doc->scMW()->mainWindowProgressBar->setValue(a);
00220 if (!exportPage(doc, pageNs[a]-1, false))
00221 return false;
00222 }
00223 return true;
00224 }