void ImageViewer::preview(){ QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"), QDir::currentPath()); QFile file(fileName); QString str; if (file.open(QFile::ReadOnly)) { QByteArray data = file.readAll(); QTextCodec *codec = Qt::codecForHtml(data); str = codec->toUnicode(data); if (Qt::mightBeRichText(str)) { doc->setHtml(str); } else { str = QString::fromLocal8Bit(data); doc->setHtml(str); } } file.close(); QColor color(0xff, 0xff, 0xff); QPixmap preview(width(), height()); QPainter p(&preview); p.setPen(color); p.fillRect(0, 0, width(), height(), color); (void)doc->documentLayout(); // make sure that there is a layout // QRectF body = QRectF(QPointF(0, 0), size); QPointF pageNumberPos; extern int qt_defaultDpi(); qreal sourceDpiX = qt_defaultDpi(); qreal sourceDpiY = sourceDpiX; QPaintDevice *dev = doc->documentLayout()->paintDevice(); if (dev) { sourceDpiX = dev->logicalDpiX(); sourceDpiY = dev->logicalDpiY(); } // scale to dpi // QSizeF scaledPageSize = doc->pageSize; //const QSizeF printerPageSize(preview.width(), preview.height()); QAbstractTextDocumentLayout *layout = doc->documentLayout(); layout->setPaintDevice(p.device()); const int dpiy = p.device()->logicalDpiY(); //const int margin = (int) ((2/2.54)*dpiy); // 2 cm margins const int margin = 0; QTextFrameFormat fmt = doc->rootFrame()->frameFormat(); fmt.setMargin(margin); doc->rootFrame()->setFrameFormat(fmt); QRectF body = QRectF(0, 0, p.device()->width(), p.device()->height()); pageNumberPos = QPointF(body.width() - margin, body.height() - margin + QFontMetrics(doc->defaultFont(), p.device()).ascent() + 5 * p.device()->logicalDpiY() / 72); QFont font(doc->defaultFont()); font.setPointSize(8); // we define 10pt to be a nice base size for printing doc->setDefaultFont(font); doc->setPageSize(body.size()); QRectF view(0, 0, body.width(), body.height()); QAbstractTextDocumentLayout::PaintContext ctx; p.setClipRect(view); ctx.clip = view; layout->draw(&p,ctx); imageLabel->setPixmap(preview); scaleFactor = 1.0; printAct->setEnabled(true); fitToWindowAct->setEnabled(true); updateActions(); if (!fitToWindowAct->isChecked()) imageLabel->adjustSize(); delete doc;
frame ->resize(width() * 3, height() * 2);
scrollArea->setWidgetResizable(true);
{ m_printer->setFullPage(true); QPrintDialog dlg(m_printer, this); if (dlg.exec() != QDialog::Accepted) return; } m_printer->setFromTo(1, 1); m_preview->setDocument(ui.textEdit->document()); m_preview->updatePreview(); updateScaleEdit();
m_preview->paintTo(m_printer);