bool UBImportPDF::addFileToDocument(UBDocumentProxy* pDocument, const QFile& pFile) { QString documentName = QFileInfo(pFile.fileName()).completeBaseName(); QUuid uuid = QUuid::createUuid(); QString filepath = UBPersistenceManager::persistenceManager()->addPdfFileToDocument(pDocument, pFile.fileName(), uuid); PDFRenderer *pdfRenderer = PDFRenderer::rendererForUuid(uuid, pDocument->persistencePath() + "/" + filepath); // renderer is automatically deleted when not used anymore if (!pdfRenderer->isValid()) { UBApplication::showMessage(tr("PDF import failed.")); return false; } int documentPageCount = pDocument->pageCount(); if (documentPageCount == 1 && UBPersistenceManager::persistenceManager()->loadDocumentScene(pDocument, 0)->isEmpty()) { documentPageCount = 0; } int pdfPageCount = pdfRenderer->pageCount(); for(int pdfPageNumber = 1; pdfPageNumber <= pdfPageCount; pdfPageNumber++) { int pageIndex = documentPageCount + (pdfPageNumber - 1); UBApplication::showMessage(tr("Importing page %1 of %2").arg(pdfPageNumber).arg(pdfPageCount), true); UBGraphicsScene* scene = 0; if (pageIndex == 0) { scene = UBPersistenceManager::persistenceManager()->loadDocumentScene(pDocument, pageIndex); } else { scene = UBPersistenceManager::persistenceManager()->createDocumentSceneAt(pDocument, pageIndex); } scene->setBackground(false, false); UBGraphicsPDFItem *pdfItem = new UBGraphicsPDFItem(pdfRenderer, pdfPageNumber); // deleted by the scene scene->addItem(pdfItem); pdfItem->setPos(-pdfItem->boundingRect().width() / 2, -pdfItem->boundingRect().height() / 2); scene->setAsBackgroundObject(pdfItem, false, false); scene->setNominalSize(pdfItem->boundingRect().width(), pdfItem->boundingRect().height()); UBPersistenceManager::persistenceManager()->persistDocumentScene(pDocument, scene, pageIndex); } UBApplication::showMessage(tr("PDF import successful.")); return true; }
void UBGraphicsGroupContainerItem::removeFromGroup(QGraphicsItem *item) { if (!item) { qDebug() << "can't specify the item because of the null pointer"; } UBGraphicsScene *groupScene = scene(); if (groupScene) { groupScene->addItemToDeletion(item); } pRemoveFromGroup(item); }
void UBToolWidget::initialize() { if (!sClosePixmap) sClosePixmap = new QPixmap(":/images/close.svg"); if(!sUnpinPixmap) sUnpinPixmap = new QPixmap(":/images/unpin.svg"); UBGraphicsScene *wscene = dynamic_cast<UBGraphicsScene *>(mToolWidget->scene()); if (wscene) { wscene->removeItemFromDeletion(mToolWidget); wscene->removeItem(mToolWidget); } mWebView = new QWebView(this); QPalette palette = mWebView->page()->palette(); palette.setBrush(QPalette::Base, QBrush(Qt::transparent)); mWebView->page()->setPalette(palette); mWebView->installEventFilter(this); mFrameWidth = UBSettings::settings()->objectFrameWidth; mContentMargin = sClosePixmap->width() / 2 + mFrameWidth; setLayout(new QVBoxLayout()); layout()->setContentsMargins(mContentMargin, mContentMargin, mContentMargin, mContentMargin); layout()->addWidget(mWebView); setFixedSize(mToolWidget->boundingRect().width() + mContentMargin * 2, mToolWidget->boundingRect().height() + mContentMargin * 2); connect(mWebView->page()->mainFrame(), &QWebFrame::javaScriptWindowObjectCleared, this, &UBToolWidget::javaScriptWindowObjectCleared); mWebView->load(mToolWidget->mainHtml()); mWebView->setAcceptDrops(false); mWebView->settings()->setAttribute(QWebSettings::PluginsEnabled, true); mWebView->setAttribute(Qt::WA_OpaquePaintEvent, false); connect(UBApplication::boardController, SIGNAL(activeSceneChanged()), this, SLOT(javaScriptWindowObjectCleared())); }
void UBGraphicsGroupContainerItem::destroy() { UBGraphicsScene *groupScene = scene(); foreach (QGraphicsItem *item, childItems()) { if (groupScene) { groupScene->addItemToDeletion(item); } pRemoveFromGroup(item); item->setFlag(QGraphicsItem::ItemIsSelectable, true); item->setFlag(QGraphicsItem::ItemIsFocusable, true); } remove(); }
QVariant UBGraphicsCurtainItemDelegate::itemChange(QGraphicsItem::GraphicsItemChange change, const QVariant &value) { if (change == QGraphicsItem::ItemZValueHasChanged) { // mFrame->setZValue(mDelegated->zValue() + 1); // foreach(DelegateButton* button, mButtons) // { // button->setZValue(mDelegated->zValue() + 2); // button->setZValue(mDelegated->zValue() + 2); // } } if (change == QGraphicsItem::ItemVisibleHasChanged) { UBGraphicsScene* ubScene = qobject_cast<UBGraphicsScene*>(mDelegated->scene()); if(ubScene) ubScene->setModified(true); } return UBGraphicsItemDelegate::itemChange(change, value); }
bool UBSvgSubsetRasterizer::rasterizeToFile(const QString& filename) { if (QFile::exists(filename)) { if (!QFile::remove(filename)) return false; } UBGraphicsScene* scene = UBSvgSubsetAdaptor::loadScene(mDocument, mPageIndex); if (!scene) return false; QRectF sceneRect = scene->normalizedSceneRect(); qreal width = sceneRect.width(); qreal height = sceneRect.height(); QImage image(width, height, QImage::Format_ARGB32); QRectF imageRect(0, 0, width, height); QPainter painter(&image); painter.setRenderHint(QPainter::Antialiasing, true); painter.setRenderHint(QPainter::SmoothPixmapTransform, true); if (scene->isDarkBackground()) { painter.fillRect(imageRect, Qt::black); } else { painter.fillRect(imageRect, Qt::white); } scene->setRenderingQuality(UBItem::RenderingQualityHigh); scene->setRenderingContext(UBGraphicsScene::NonScreen); scene->render(&painter, imageRect, sceneRect, Qt::KeepAspectRatio); scene->setRenderingQuality(UBItem::RenderingQualityNormal); scene->setRenderingContext(UBGraphicsScene::Screen); bool success = image.save(filename, "JPG", 100); delete scene; return success; }
QColor UBGraphicsCurtainItem::opaqueControlColor() const { UBGraphicsScene* pScene = static_cast<UBGraphicsScene*>(QGraphicsRectItem::scene()); return pScene->isDarkBackground() ? sDarkBackgroundOpaqueControlColor : sOpaqueControlColor; }
QColor UBGraphicsCurtainItem::drawColor() const { UBGraphicsScene* pScene = static_cast<UBGraphicsScene*>(QGraphicsRectItem::scene()); return pScene->isDarkBackground() ? sDarkBackgroundDrawColor : sDrawColor; }
void UBExportFullPDF::saveOverlayPdf(UBDocumentProxy* pDocumentProxy, const QString& filename) { if (!pDocumentProxy || filename.length() == 0 || pDocumentProxy->pageCount() == 0) return; //PDF qDebug() << "exporting document to PDF Merger" << filename; QPrinter pdfPrinter; pdfPrinter.setOutputFormat(QPrinter::PdfFormat); pdfPrinter.setResolution(UBSettings::settings()->pdfResolution->get().toInt()); pdfPrinter.setOutputFileName(filename); pdfPrinter.setFullPage(true); QPainter* pdfPainter = 0; for(int pageIndex = 0 ; pageIndex < pDocumentProxy->pageCount(); pageIndex++) { UBGraphicsScene* scene = UBPersistenceManager::persistenceManager()->loadDocumentScene(pDocumentProxy, pageIndex); // set background to white, no grid for PDF output bool isDark = scene->isDarkBackground(); bool isCrossed = scene->isCrossedBackground(); scene->setBackground(false, false); // set high res rendering scene->setRenderingQuality(UBItem::RenderingQualityHigh); scene->setRenderingContext(UBGraphicsScene::PdfExport); QSize pageSize = scene->nominalSize(); UBGraphicsPDFItem *pdfItem = qgraphicsitem_cast<UBGraphicsPDFItem*>(scene->backgroundObject()); if (pdfItem) mHasPDFBackgrounds = true; pdfPrinter.setPaperSize(QSizeF(pageSize.width()*mScaleFactor, pageSize.height()*mScaleFactor), QPrinter::Point); if (!pdfPainter) pdfPainter = new QPainter(&pdfPrinter); if (pageIndex != 0) pdfPrinter.newPage(); //render to PDF scene->setDrawingMode(true); scene->render(pdfPainter, QRectF(), scene->normalizedSceneRect()); //restore screen rendering quality scene->setRenderingContext(UBGraphicsScene::Screen); scene->setRenderingQuality(UBItem::RenderingQualityNormal); //restore background state scene->setDrawingMode(false); scene->setBackground(isDark, isCrossed); } if (pdfPainter) delete pdfPainter; }
void UBExportFullPDF::persistsDocument(UBDocumentProxy* pDocumentProxy, const QString& filename) { QFile file(filename); if (file.exists()) file.remove(); QString overlayName = filename; overlayName.replace(".pdf", "_overlay.pdf"); QFile previousOverlay(overlayName); if (previousOverlay.exists()) previousOverlay.remove(); mHasPDFBackgrounds = false; saveOverlayPdf(pDocumentProxy, overlayName); if (!mHasPDFBackgrounds) { QFile f(overlayName); f.rename(filename); } else { Merger merger; try { merger.addOverlayDocument(QFile::encodeName(overlayName).constData()); MergeDescription mergeInfo; int existingPageCount = pDocumentProxy->pageCount(); for(int pageIndex = 0 ; pageIndex < existingPageCount; pageIndex++) { UBGraphicsScene* scene = UBPersistenceManager::persistenceManager()->loadDocumentScene(pDocumentProxy, pageIndex); UBGraphicsPDFItem *pdfItem = qgraphicsitem_cast<UBGraphicsPDFItem*>(scene->backgroundObject()); QSize pageSize = scene->nominalSize(); if (pdfItem) { QString pdfName = UBPersistenceManager::objectDirectory + "/" + pdfItem->fileUuid().toString() + ".pdf"; QString backgroundPath = pDocumentProxy->persistencePath() + "/" + pdfName; qDebug() << "scene->itemsBoundingRect()" << scene->itemsBoundingRect(); qDebug() << "pdfItem->boundingRect()" << pdfItem->boundingRect(); qDebug() << "pdfItem->sceneBoundingRect()" << pdfItem->sceneBoundingRect(); MergePageDescription pageDescription(pageSize.width() * mScaleFactor, pageSize.height() * mScaleFactor, pdfItem->pageNumber(), QFile::encodeName(backgroundPath).constData(), TransformationDescription(), pageIndex + 1, TransformationDescription(), false, false); mergeInfo.push_back(pageDescription); merger.addBaseDocument(QFile::encodeName(backgroundPath).constData()); } else { MergePageDescription pageDescription(pageSize.width() * mScaleFactor, pageSize.height() * mScaleFactor, 0, "", TransformationDescription(), pageIndex + 1, TransformationDescription(), false, true); mergeInfo.push_back(pageDescription); } } merger.merge(QFile::encodeName(overlayName).constData(), mergeInfo); merger.saveMergedDocumentsAs(QFile::encodeName(filename).constData()); } catch(Exception e) { qDebug() << "PdfMerger failed to merge documents to " << filename << " - Exception : " << e.what(); // default to raster export UBExportPDF::persistsDocument(pDocumentProxy, filename); } if (!UBApplication::app()->isVerbose()) { QFile::remove(overlayName); } } }
bool UBExportPDF::persistsDocument(UBDocumentProxy* pDocumentProxy, const QString& filename) { QPrinter pdfPrinter; qDebug() << "exporting document to PDF" << filename; pdfPrinter.setOutputFormat(QPrinter::PdfFormat); pdfPrinter.setResolution(UBSettings::settings()->pdfResolution->get().toInt()); pdfPrinter.setOutputFileName(filename); pdfPrinter.setFullPage(true); //need to calculate screen resolution QDesktopWidget* desktop = UBApplication::desktop(); int dpiCommon = (desktop->physicalDpiX() + desktop->physicalDpiY()) / 2; float scaleFactor = 72.0f / dpiCommon; QPainter pdfPainter; bool painterNeedsBegin = true; int existingPageCount = pDocumentProxy->pageCount(); for(int pageIndex = 0 ; pageIndex < existingPageCount; pageIndex++) { UBGraphicsScene* scene = UBPersistenceManager::persistenceManager()->loadDocumentScene(pDocumentProxy, pageIndex); UBApplication::showMessage(tr("Exporting page %1 of %2").arg(pageIndex + 1).arg(existingPageCount)); // set background to white, no crossing for PDF output bool isDark = scene->isDarkBackground(); bool isCrossed = scene->isCrossedBackground(); scene->setBackground(false, false); QSize pageSize = scene->nominalSize(); // set high res rendering scene->setRenderingQuality(UBItem::RenderingQualityHigh); scene->setRenderingContext(UBGraphicsScene::NonScreen); //setting page size to appropriate value pdfPrinter.setPaperSize(QSizeF(pageSize.width()*scaleFactor, pageSize.height()*scaleFactor), QPrinter::Point); if(painterNeedsBegin) painterNeedsBegin = !pdfPainter.begin(&pdfPrinter); //render to PDF scene->render(&pdfPainter, QRectF(), scene->normalizedSceneRect()); if (pageIndex < existingPageCount - 1) pdfPrinter.newPage(); //restore screen rendering quality scene->setRenderingContext(UBGraphicsScene::Screen); scene->setRenderingQuality(UBItem::RenderingQualityNormal); //restore background state scene->setBackground(isDark, isCrossed); } if(!painterNeedsBegin) pdfPainter.end(); return true; }