int QPrinterProto::logicalDpiY() const { QPrinter *item = qscriptvalue_cast<QPrinter*>(thisObject()); if (item) return item->logicalDpiY(); return 0; }
void printerInvoice::printf(const QString &html) { QPrinter p; QTextDocument doc; QFont font = doc.defaultFont(); font.setBold(true); font.setPointSize(font.pointSize()); doc.setDefaultFont(font); QSizeF s = QSizeF(p.logicalDpiX() * 7.5, p.logicalDpiY() * 7.5); doc.setPageSize(s); p.setOutputFormat(QPrinter::NativeFormat); doc.setHtml(html); doc.print(&p); }
void Matrix::exportVector(const QString& fileName, int res, bool color, bool keepAspect, QPrinter::PageSize pageSize) { if (d_view_type != ImageView) return; if ( fileName.isEmpty() ){ QMessageBox::critical(this, tr("QtiPlot - Error"), tr("Please provide a valid file name!")); return; } QPrinter printer; printer.setCreator("QtiPlot"); printer.setFullPage(true); if (res) printer.setResolution(res); printer.setOutputFileName(fileName); if (fileName.contains(".eps")) printer.setOutputFormat(QPrinter::PostScriptFormat); if (color) printer.setColorMode(QPrinter::Color); else printer.setColorMode(QPrinter::GrayScale); int cols = numCols(); int rows = numRows(); QRect rect = QRect(0, 0, cols, rows); if (pageSize == QPrinter::Custom) printer.setPageSize(Graph::minPageSize(printer, rect)); else printer.setPageSize(pageSize); double aspect = (double)cols/(double)rows; if (aspect < 1) printer.setOrientation(QPrinter::Portrait); else printer.setOrientation(QPrinter::Landscape); if (keepAspect){// export should preserve aspect ratio double page_aspect = double(printer.width())/double(printer.height()); if (page_aspect > aspect){ int margin = (int) ((0.1/2.54)*printer.logicalDpiY()); // 1 mm margins int height = printer.height() - 2*margin; int width = int(height*aspect); int x = (printer.width()- width)/2; rect = QRect(x, margin, width, height); } else if (aspect >= page_aspect){ int margin = (int) ((0.1/2.54)*printer.logicalDpiX()); // 1 mm margins int width = printer.width() - 2*margin; int height = int(width/aspect); int y = (printer.height()- height)/2; rect = QRect(margin, y, width, height); } } else { int x_margin = (int) ((0.1/2.54)*printer.logicalDpiX()); // 1 mm margins int y_margin = (int) ((0.1/2.54)*printer.logicalDpiY()); // 1 mm margins int width = printer.width() - 2*x_margin; int height = printer.height() - 2*y_margin; rect = QRect(x_margin, y_margin, width, height); } QPainter paint(&printer); paint.drawImage(rect, d_matrix_model->renderImage()); paint.end(); }
void Matrix::print(const QString& fileName) { QPrinter printer; printer.setColorMode (QPrinter::GrayScale); if (!fileName.isEmpty()){ printer.setCreator("QtiPlot"); printer.setOutputFormat(QPrinter::PdfFormat); printer.setOutputFileName(fileName); } else { QPrintDialog printDialog(&printer, applicationWindow()); if (printDialog.exec() != QDialog::Accepted) return; } printer.setFullPage( true ); QPainter p; if ( !p.begin(&printer ) ) return; // paint on printer int dpiy = printer.logicalDpiY(); const int margin = (int) ( (1/2.54)*dpiy ); // 1 cm margins if (d_view_type == ImageView){ p.drawImage (printer.pageRect(), d_matrix_model->renderImage()); return; } QHeaderView *vHeader = d_table_view->verticalHeader(); int rows = numRows(); int cols = numCols(); int height = margin; int i, vertHeaderWidth = vHeader->width(); int right = margin + vertHeaderWidth; // print header p.setFont(QFont()); QString header_label = d_matrix_model->headerData(0, Qt::Horizontal).toString(); QRect br = p.boundingRect(br, Qt::AlignCenter, header_label); p.drawLine(right, height, right, height+br.height()); QRect tr(br); for(i=0; i<cols; i++){ int w = d_table_view->columnWidth(i); tr.setTopLeft(QPoint(right,height)); tr.setWidth(w); tr.setHeight(br.height()); header_label = d_matrix_model->headerData(i, Qt::Horizontal).toString(); p.drawText(tr, Qt::AlignCenter, header_label,-1); right += w; p.drawLine(right, height, right, height+tr.height()); if (right >= printer.width()-2*margin ) break; } p.drawLine(margin + vertHeaderWidth, height, right-1, height);//first horizontal line height += tr.height(); p.drawLine(margin, height, right-1, height); // print table values for(i=0;i<rows;i++){ right = margin; QString cell_text = d_matrix_model->headerData(i, Qt::Horizontal).toString()+"\t"; tr = p.boundingRect(tr, Qt::AlignCenter, cell_text); p.drawLine(right, height, right, height+tr.height()); br.setTopLeft(QPoint(right,height)); br.setWidth(vertHeaderWidth); br.setHeight(tr.height()); p.drawText(br,Qt::AlignCenter,cell_text,-1); right += vertHeaderWidth; p.drawLine(right, height, right, height+tr.height()); for(int j=0; j<cols; j++){ int w = d_table_view->columnWidth (j); cell_text = text(i,j)+"\t"; tr = p.boundingRect(tr,Qt::AlignCenter,cell_text); br.setTopLeft(QPoint(right,height)); br.setWidth(w); br.setHeight(tr.height()); p.drawText(br, Qt::AlignCenter, cell_text, -1); right += w; p.drawLine(right, height, right, height+tr.height()); if (right >= printer.width()-2*margin ) break; } height += br.height(); p.drawLine(margin, height, right-1, height); if (height >= printer.height()-margin ){ printer.newPage(); height = margin; p.drawLine(margin, height, right, height); } } }
void MultiLayer::printAllLayers(QPainter *painter) { if (!painter) return; QPrinter *printer = (QPrinter *)painter->device(); QRect paperRect = ((QPrinter *)painter->device())->paperRect(); QRect canvasRect = canvas->rect(); QRect pageRect = printer->pageRect(); QRect cr = canvasRect; // cropmarks rectangle if (d_scale_on_print) { int margin = (int)((1 / 2.54) * printer->logicalDpiY()); // 1 cm margins double scaleFactorX = (double)(paperRect.width() - 2 * margin) / (double)canvasRect.width(); double scaleFactorY = (double)(paperRect.height() - 2 * margin) / (double)canvasRect.height(); if (d_print_cropmarks) { cr.moveTo(QPoint(margin + int(cr.x() * scaleFactorX), margin + int(cr.y() * scaleFactorY))); cr.setWidth(int(cr.width() * scaleFactorX)); cr.setHeight(int(cr.height() * scaleFactorX)); } for (int i = 0; i < (int)graphsList.count(); i++) { Graph *gr = (Graph *)graphsList.at(i); Plot *myPlot = gr->plotWidget(); QPoint pos = gr->pos(); pos = QPoint(margin + int(pos.x() * scaleFactorX), margin + int(pos.y() * scaleFactorY)); int width = int(myPlot->frameGeometry().width() * scaleFactorX); int height = int(myPlot->frameGeometry().height() * scaleFactorY); gr->print(painter, QRect(pos, QSize(width, height))); } } else { int x_margin = (pageRect.width() - canvasRect.width()) / 2; int y_margin = (pageRect.height() - canvasRect.height()) / 2; if (d_print_cropmarks) cr.moveTo(x_margin, y_margin); for (int i = 0; i < (int)graphsList.count(); i++) { Graph *gr = (Graph *)graphsList.at(i); Plot *myPlot = (Plot *)gr->plotWidget(); QPoint pos = gr->pos(); pos = QPoint(x_margin + pos.x(), y_margin + pos.y()); gr->print(painter, QRect(pos, myPlot->size())); } } if (d_print_cropmarks) { cr.addCoords(-1, -1, 2, 2); painter->save(); painter->setPen(QPen(QColor(Qt::black), 0.5, Qt::DashLine)); painter->drawLine(paperRect.left(), cr.top(), paperRect.right(), cr.top()); painter->drawLine(paperRect.left(), cr.bottom(), paperRect.right(), cr.bottom()); painter->drawLine(cr.left(), paperRect.top(), cr.left(), paperRect.bottom()); painter->drawLine(cr.right(), paperRect.top(), cr.right(), paperRect.bottom()); painter->restore(); } }