Example #1
0
void ObjectsScene::configurePrinter(QPrinter *printer)
{
  if(!printer)
    throw Exception(ERR_OPR_NOT_ALOC_OBJECT ,__PRETTY_FUNCTION__,__FILE__,__LINE__);

  if(paper_size!=QPrinter::Custom)
   printer->setPaperSize(paper_size);
  else
  {
    #if (QT_VERSION >= QT_VERSION_CHECK(5, 3, 0))
      //The QTBUG-33645 is fixed on Qt 5.3
      QPageLayout pl;
      QPageSize ps;
      ps=QPageSize(QSizeF(custom_paper_size.width(), custom_paper_size.height()), QPageSize::Point, QString(), QPageSize::ExactMatch);
      pl.setPageSize(ps);
      pl.setOrientation(page_orientation==QPrinter::Landscape ? QPageLayout::Landscape : QPageLayout::Portrait);
      printer->setPageSize(pl.pageSize());
    #else
      #warning "Custom page size bug (QTBUG-33645) workaround for Qt 5.2 or lower. NOTE: This issue is fixed on Qt 5.3"
      printer->setPaperSize(QSizeF(custom_paper_size.height(), custom_paper_size.width()), QPrinter::Point);
    #endif
  }

  if(paper_size==QPrinter::Custom)
  {
    if(custom_paper_size.width() > custom_paper_size.height())
      ObjectsScene::page_orientation=QPrinter::Landscape;
    else
      ObjectsScene::page_orientation=QPrinter::Portrait;
  }
  else
     printer->setOrientation(page_orientation);

  printer->setPageMargins(page_margins.left(), page_margins.top(), page_margins.width(), page_margins.height(), QPrinter::Millimeter);
}
Example #2
0
void StrainPipeData::writeToPdf()
{
    if (isEmpty()) populate();
    QString pdfFilename = directory.absoluteFilePath() + "/StrainInfo" + ".pdf";
    QPdfWriter* pdfWriter = new QPdfWriter(pdfFilename);
    pdfWriter->setPageSize(QPageSize(QPageSize::A3));
    pdfWriter->setTitle("Directory iformation");
    print(pdfWriter);
}
Example #3
0
void CollectionPipeData::writeToPdf()
{
    if (!valid()) return;
    if (isEmpty()) populate();
    QString pdfFilename = directory.absoluteFilePath() + "/CollectionData" + ".pdf";
    QPdfWriter* pdfWriter = new QPdfWriter(pdfFilename);
    pdfWriter->setPageSize(QPageSize(QPageSize::A3));
    pdfWriter->setTitle("Collection Directory iformation");
    print(pdfWriter);
}
Example #4
0
void Document::exportPDF(QString fileName)
{
  //    QPdfWriter pdfWriter("/Users/tom/Desktop/qpdfwriter.pdf");
  //    QPdfWriter pdfWriter(fileName);

  QPrinter pdfWriter(QPrinter::HighResolution);
  pdfWriter.setOutputFormat(QPrinter::PdfFormat);
  pdfWriter.setOutputFileName(fileName);
  //    pdfWriter.setMargins();

  pdfWriter.setPageSize(QPageSize(QSizeF(pages[0].width(), pages[0].height()), QPageSize::Point));
  pdfWriter.setPageMargins(QMarginsF(0, 0, 0, 0));
  qreal zoomW = ((qreal)pdfWriter.pageRect().width()) / ((qreal)pdfWriter.paperRect().width());
  qreal zoomH = ((qreal)pdfWriter.pageRect().height()) / ((qreal)pdfWriter.paperRect().height());
  qreal zoom = zoomW;
  if (zoomH < zoomW)
    zoom = zoomH;
  pdfWriter.setResolution(72);
  pdfWriter.pageLayout().setUnits(QPageLayout::Point);
  QPainter painter;

  //    std::cout << "PDF " << pdfWriter.colorCount() << std::endl;

  painter.begin(&pdfWriter);
  painter.setRenderHint(QPainter::Antialiasing, true);
  for (int pageNum = 0; pageNum < pages.size(); ++pageNum)
  {
    if (pages[pageNum].backgroundColor() != QColor("white"))
    {
      QRectF pageRect = pdfWriter.pageRect(QPrinter::Point);
      pageRect.translate(-pageRect.topLeft());
      painter.fillRect(pageRect, pages[pageNum].backgroundColor());
    }
    pages[pageNum].paint(painter, zoom, QRect(0, 0, 0, 0));

    if (pageNum + 1 < pages.size())
    {
      pdfWriter.setPageSize(QPageSize(QSize(pages[pageNum + 1].width(), pages[pageNum + 1].height())));
      pdfWriter.newPage();
    }
  }
  painter.end();
}
Example #5
0
void Stack::initPrinter()
{
	if (m_printer) {
		return;
	}

	m_printer = new QPrinter(QPrinter::HighResolution);
	m_printer->setPageSize(QPageSize(QPageSize::Letter));
	m_printer->setPageOrientation(QPageLayout::Portrait);
	m_printer->setPageMargins(QMarginsF(1.0, 1.0, 1.0, 1.0), QPageLayout::Inch);
}
Example #6
0
QPageSize QPpdPrintDevice::defaultPageSize() const
{
    ppd_choice_t *defaultChoice = ppdFindMarkedChoice(m_ppd, "PageSize");
    if (defaultChoice) {
        ppd_size_t *ppdSize = ppdPageSize(m_ppd, defaultChoice->choice);
        if (ppdSize) {
            // Returned size is in points
            QString key = QString::fromUtf8(ppdSize->name);
            QSize size = QSize(qRound(ppdSize->width), qRound(ppdSize->length));
            QString name = QString::fromUtf8(defaultChoice->text);
            return createPageSize(key, size, name);
        }
    }
    return QPageSize();
}
Example #7
0
void Stack::initPrinter()
{
	if (m_printer) {
		return;
	}

	m_printer = new QPrinter(QPrinter::HighResolution);
#if (QT_VERSION >= QT_VERSION_CHECK(5,3,0))
	m_printer->setPageSize(QPageSize(QPageSize::Letter));
	m_printer->setPageOrientation(QPageLayout::Portrait);
	m_printer->setPageMargins(QMarginsF(1.0, 1.0, 1.0, 1.0), QPageLayout::Inch);
#else
	m_printer->setPageSize(QPrinter::Letter);
	m_printer->setOrientation(QPrinter::Portrait);
	m_printer->setPageMargins(1.0, 1.0, 1.0, 1.0, QPrinter::Inch);
#endif
}
QPageSize QPlatformPrintDevice::supportedPageSize(const QPageSize &pageSize) const
{
    if (!pageSize.isValid())
        return QPageSize();

    if (!m_havePageSizes)
        loadPageSizes();

    // First try match on name and id for case where printer defines same size twice with different names
    // e.g. Windows defines DMPAPER_11X17 and DMPAPER_TABLOID with names "11x17" and "Tabloid", but both
    // map to QPageSize::Tabloid / PPD Key "Tabloid" / ANSI B Tabloid
    if (pageSize.id() != QPageSize::Custom) {
        foreach (const QPageSize &ps, m_pageSizes) {
            if (ps.id() == pageSize.id() && ps.name() == pageSize.name())
                return ps;
        }
    }
Example #9
0
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    QString filepath = QFileDialog::getSaveFileName(nullptr, "Save File", "",
                                                    "PDF files (*.pdf)");
    if (filepath.isEmpty())
        return 1;
    QPdfWriter writer(filepath);
    writer.setPageSize(QPageSize(QPageSize::A4));
    writer.setResolution(300);

    QPainterPath path;
    path.moveTo(0,0);
    path.lineTo(1000,0);
    path.lineTo(1000,1000);
    path.lineTo(0,800);
    path.lineTo(500,100);
    path.lineTo(800,900);
    path.lineTo(300,600);

    QPen pen;
    pen.setWidth(30);
    pen.setJoinStyle(Qt::MiterJoin);

    // The black path on the first page must always be visible in the PDF viewer.
    QPainter p(&writer);
    pen.setMiterLimit(6.0);
    p.setPen(pen);
    p.drawPath(path);

    // If a miter limit below 1.0 is written to the PDF,
    // broken PDF viewers may not show the red path on the second page.
    writer.newPage();
    pen.setMiterLimit(0.6);
    pen.setColor(Qt::red);
    p.setPen(pen);
    p.drawPath(path);

    p.end();
    return 0;
}
void PageMetrics::update(QPageSize::PageSizeId _pageFormat, QMarginsF _mmPageMargins)
{
	m_pageFormat = _pageFormat;

	m_mmPageSize = QPageSize(m_pageFormat).rect(QPageSize::Millimeter).size();
	m_mmPageMargins = _mmPageMargins;

	//
	// Рассчитываем значения в пикселах
	//
	const bool x = true, y = false;
	m_pxPageSize =
			QSizeF(mmToPx(m_mmPageSize.width(), x),
				   mmToPx(m_mmPageSize.height(), y));
	m_pxPageMargins =
			QMarginsF(mmToPx(m_mmPageMargins.left(), x),
					  mmToPx(m_mmPageMargins.top(), y),
					  mmToPx(m_mmPageMargins.right(), x),
					  mmToPx(m_mmPageMargins.bottom(), y)
					  );
}
Example #11
0
void tst_QPdfWriter::basics()
{
    QTemporaryFile file;
    if (!file.open())
        QSKIP("Couldn't open temp file!");
    QPdfWriter writer(file.fileName());

    QCOMPARE(writer.title(), QString());
    writer.setTitle(QString("Test Title"));
    QCOMPARE(writer.title(), QString("Test Title"));

    QCOMPARE(writer.creator(), QString());
    writer.setCreator(QString("Test Creator"));
    QCOMPARE(writer.creator(), QString("Test Creator"));

    QCOMPARE(writer.resolution(), 1200);
    writer.setResolution(600);
    QCOMPARE(writer.resolution(), 600);

    QCOMPARE(writer.pageLayout().pageSize().id(), QPageSize::A4);
    QCOMPARE(writer.pageSize(), QPdfWriter::A4);
    QCOMPARE(writer.pageSizeMM(), QSizeF(210, 297));

    writer.setPageSize(QPageSize(QPageSize::A5));
    QCOMPARE(writer.pageLayout().pageSize().id(), QPageSize::A5);
    QCOMPARE(writer.pageSize(), QPdfWriter::A5);
    QCOMPARE(writer.pageSizeMM(), QSizeF(148, 210));

    writer.setPageSize(QPdfWriter::A3);
    QCOMPARE(writer.pageLayout().pageSize().id(), QPageSize::A3);
    QCOMPARE(writer.pageSize(), QPdfWriter::A3);
    QCOMPARE(writer.pageSizeMM(), QSizeF(297, 420));

    writer.setPageSizeMM(QSize(210, 297));
    QCOMPARE(writer.pageLayout().pageSize().id(), QPageSize::A4);
    QCOMPARE(writer.pageSize(), QPdfWriter::A4);
    QCOMPARE(writer.pageSizeMM(), QSizeF(210, 297));

    QCOMPARE(writer.pageLayout().orientation(), QPageLayout::Portrait);
    writer.setPageOrientation(QPageLayout::Landscape);
    QCOMPARE(writer.pageLayout().orientation(), QPageLayout::Landscape);
    QCOMPARE(writer.pageSizeMM(), QSizeF(210, 297));

    QCOMPARE(writer.pageLayout().margins(), QMarginsF(10, 10, 10, 10));
    QCOMPARE(writer.pageLayout().units(), QPageLayout::Point);
    QCOMPARE(writer.margins().left, 3.53);  // mm
    QCOMPARE(writer.margins().right, 3.53);
    QCOMPARE(writer.margins().top, 3.53);
    QCOMPARE(writer.margins().bottom, 3.53);
    writer.setPageMargins(QMarginsF(20, 20, 20, 20), QPageLayout::Millimeter);
    QCOMPARE(writer.pageLayout().margins(), QMarginsF(20, 20, 20, 20));
    QCOMPARE(writer.pageLayout().units(), QPageLayout::Millimeter);
    QCOMPARE(writer.margins().left, 20.0);
    QCOMPARE(writer.margins().right, 20.0);
    QCOMPARE(writer.margins().top, 20.0);
    QCOMPARE(writer.margins().bottom, 20.0);
    QPdfWriter::Margins margins = {50, 50, 50, 50};
    writer.setMargins(margins);
    QCOMPARE(writer.pageLayout().margins(), QMarginsF(50, 50, 50, 50));
    QCOMPARE(writer.pageLayout().units(), QPageLayout::Millimeter);
    QCOMPARE(writer.margins().left, 50.0);
    QCOMPARE(writer.margins().right, 50.0);
    QCOMPARE(writer.margins().top, 50.0);
    QCOMPARE(writer.margins().bottom, 50.0);

    QCOMPARE(writer.pageLayout().fullRect(QPageLayout::Millimeter), QRectF(0, 0, 297, 210));
    QCOMPARE(writer.pageLayout().paintRect(QPageLayout::Millimeter), QRectF(50, 50, 197, 110));
}
Example #12
0
QPageSize QPrinterInfo::maximumPhysicalPageSize() const
{
    Q_D(const QPrinterInfo);
    return QPageSize(d->m_printDevice.maximumPhysicalPageSize(), QString(), QPageSize::ExactMatch);
}
QPageSize QPlatformPrintDevice::defaultPageSize() const
{
    return QPageSize();
}
Example #14
0
QPageSize QPrintDevice::supportedPageSize(const QSizeF &size, QPageSize::Unit units) const
{
    return isValid() ? d->supportedPageSize(size, units) : QPageSize();
}
Example #15
0
QPageSize QPrintDevice::supportedPageSize(const QSize &pointSize) const
{
    return isValid() ? d->supportedPageSize(pointSize) : QPageSize();
}
Example #16
0
QPageSize QPrintDevice::supportedPageSize(const QString &pageName) const
{
    return isValid() ? d->supportedPageSize(pageName) : QPageSize();
}
Example #17
0
QPageSize QPrintDevice::supportedPageSize(QPageSize::PageSizeId pageSizeId) const
{
    return isValid() ? d->supportedPageSize(pageSizeId) : QPageSize();
}
Example #18
0
void QPdfPrintEngine::setProperty(PrintEnginePropertyKey key, const QVariant &value)
{
    Q_D(QPdfPrintEngine);

    switch (int(key)) {

    // The following keys are properties or derived values and so cannot be set
    case PPK_PageRect:
        break;
    case PPK_PaperRect:
        break;
    case PPK_PaperSources:
        break;
    case PPK_SupportsMultipleCopies:
        break;
    case PPK_SupportedResolutions:
        break;

    // The following keys are settings that are unsupported by the PDF PrintEngine
    case PPK_CustomBase:
        break;

    // The following keys are properties and settings that are supported by the PDF PrintEngine
    case PPK_CollateCopies:
        d->collate = value.toBool();
        break;
    case PPK_ColorMode:
        d->grayscale = (QPrinter::ColorMode(value.toInt()) == QPrinter::GrayScale);
        break;
    case PPK_Creator:
        d->creator = value.toString();
        break;
    case PPK_DocumentName:
        d->title = value.toString();
        break;
    case PPK_FullPage:
        if (value.toBool())
            d->m_pageLayout.setMode(QPageLayout::FullPageMode);
        else
            d->m_pageLayout.setMode(QPageLayout::StandardMode);
        break;
    case PPK_CopyCount: // fallthrough
    case PPK_NumberOfCopies:
        d->copies = value.toInt();
        break;
    case PPK_Orientation:
        d->m_pageLayout.setOrientation(QPageLayout::Orientation(value.toInt()));
        break;
    case PPK_OutputFileName:
        d->outputFileName = value.toString();
        break;
    case PPK_PageOrder:
        d->pageOrder = QPrinter::PageOrder(value.toInt());
        break;
    case PPK_PageSize: {
        QPageSize pageSize = QPageSize(QPageSize::PageSizeId(value.toInt()));
        if (pageSize.isValid())
            d->m_pageLayout.setPageSize(pageSize);
        break;
    }
    case PPK_PaperName: {
        QString name = value.toString();
        for (int i = 0; i <= QPageSize::LastPageSize; ++i) {
            QPageSize pageSize = QPageSize(QPageSize::PageSizeId(i));
            if (name == pageSize.name()) {
                d->m_pageLayout.setPageSize(pageSize);
                break;
            }
        }
        break;
    }
    case PPK_WindowsPageSize:
        d->m_pageLayout.setPageSize(QPageSize(QPageSize::id(value.toInt())));
        break;
    case PPK_PaperSource:
        d->paperSource = QPrinter::PaperSource(value.toInt());
        break;
    case PPK_PrinterName:
        d->printerName = value.toString();
        break;
    case PPK_PrinterProgram:
        d->printProgram = value.toString();
        break;
    case PPK_Resolution:
        d->resolution = value.toInt();
        break;
    case PPK_SelectionOption:
        d->selectionOption = value.toString();
        break;
    case PPK_FontEmbedding:
        d->embedFonts = value.toBool();
        break;
    case PPK_Duplex:
        d->duplex = static_cast<QPrint::DuplexMode>(value.toInt());
        break;
    case PPK_CustomPaperSize:
        d->m_pageLayout.setPageSize(QPageSize(value.toSizeF(), QPageSize::Point));
        break;
    case PPK_PageMargins: {
        QList<QVariant> margins(value.toList());
        Q_ASSERT(margins.size() == 4);
        d->m_pageLayout.setUnits(QPageLayout::Point);
        d->m_pageLayout.setMargins(QMarginsF(margins.at(0).toReal(), margins.at(1).toReal(),
                                             margins.at(2).toReal(), margins.at(3).toReal()));
        break;
    }
    case PPK_QPageSize: {
        QPageSize pageSize = value.value<QPageSize>();
        if (pageSize.isValid())
            d->m_pageLayout.setPageSize(pageSize);
        break;
    }
    case PPK_QPageMargins: {
        QPair<QMarginsF, QPageLayout::Unit> pair = value.value<QPair<QMarginsF, QPageLayout::Unit> >();
        d->m_pageLayout.setUnits(pair.second);
        d->m_pageLayout.setMargins(pair.first);
        break;
    }
    case PPK_QPageLayout: {
        QPageLayout pageLayout = value.value<QPageLayout>();
        if (pageLayout.isValid())
            d->m_pageLayout = pageLayout;
        break;
    }
    // No default so that compiler will complain if new keys added and not handled in this engine
    }
}
/*!
  Sets the size of the a page to \a size.

  \sa setPageSizeMM()
  */
void QPagedPaintDevice::setPageSize(PageSize size)
{
    d->m_pageLayout.setPageSize(QPageSize(QPageSize::PageSizeId(size)));
}
/*!
    Sets the page size to \a size. \a size is specified in millimeters.

    If the size matches a standard QPagedPaintDevice::PageSize then that page
    size will be used, otherwise QPagedPaintDevice::Custom will be set.
*/
void QPagedPaintDevice::setPageSizeMM(const QSizeF &size)
{
    d->m_pageLayout.setPageSize(QPageSize(size, QPageSize::Millimeter));
}
Example #21
0
QPageSize QPrintDevice::defaultPageSize() const
{
    return isValid() ? d->defaultPageSize() : QPageSize();
}
Example #22
0
QPageSize pageSizeForName(const QString& name)
{
  return QPageSize(pageSizeIdForName(name));
}