! Renders the plot to a pixmap and returns it. The plot is sized to \a width and \a height in pixels and scaled with \a scale. (width 100 and scale 2.0 lead to a full resolution pixmap with width 200.) \see toPainter, saveRastered, saveBmp, savePng, saveJpg, savePdf */
| 15660 | \see toPainter, saveRastered, saveBmp, savePng, saveJpg, savePdf |
| 15661 | */ |
| 15662 | QPixmap QCustomPlot::toPixmap(int width, int height, double scale) |
| 15663 | { |
| 15664 | // this method is somewhat similar to toPainter. Change something here, and a change in toPainter might be necessary, too. |
| 15665 | int newWidth, newHeight; |
| 15666 | if (width == 0 || height == 0) |
| 15667 | { |
| 15668 | newWidth = this->width(); |
| 15669 | newHeight = this->height(); |
| 15670 | } else |
| 15671 | { |
| 15672 | newWidth = width; |
| 15673 | newHeight = height; |
| 15674 | } |
| 15675 | int scaledWidth = qRound(scale*newWidth); |
| 15676 | int scaledHeight = qRound(scale*newHeight); |
| 15677 | |
| 15678 | QPixmap result(scaledWidth, scaledHeight); |
| 15679 | result.fill(mBackgroundBrush.style() == Qt::SolidPattern ? mBackgroundBrush.color() : Qt::transparent); // if using non-solid pattern, make transparent now and draw brush pattern later |
| 15680 | QCPPainter painter; |
| 15681 | painter.begin(&result); |
| 15682 | if (painter.isActive()) |
| 15683 | { |
| 15684 | QRect oldViewport = viewport(); |
| 15685 | setViewport(QRect(0, 0, newWidth, newHeight)); |
| 15686 | painter.setMode(QCPPainter::pmNoCaching); |
| 15687 | if (!qFuzzyCompare(scale, 1.0)) |
| 15688 | { |
| 15689 | if (scale > 1.0) // for scale < 1 we always want cosmetic pens where possible, because else lines might disappear for very small scales |
| 15690 | painter.setMode(QCPPainter::pmNonCosmetic); |
| 15691 | painter.scale(scale, scale); |
| 15692 | } |
| 15693 | if (mBackgroundBrush.style() != Qt::SolidPattern && mBackgroundBrush.style() != Qt::NoBrush) // solid fills were done a few lines above with QPixmap::fill |
| 15694 | painter.fillRect(mViewport, mBackgroundBrush); |
| 15695 | draw(&painter); |
| 15696 | setViewport(oldViewport); |
| 15697 | painter.end(); |
| 15698 | } else // might happen if pixmap has width or height zero |
| 15699 | { |
| 15700 | qDebug() << Q_FUNC_INFO << "Couldn't activate painter on pixmap"; |
| 15701 | return QPixmap(); |
| 15702 | } |
| 15703 | return result; |
| 15704 | } |
| 15705 | |
| 15706 | /*! |
| 15707 | Renders the plot using the passed \a painter. |