From 8975a18d93d492ccb374cfe6269ee16adaa04020 Mon Sep 17 00:00:00 2001 From: Jakub Melka Date: Sat, 9 Oct 2021 18:13:34 +0200 Subject: [PATCH] DocDiff application: view documents --- Pdf4QtDocDiff/differencesdockwidget.cpp | 14 ++- Pdf4QtDocDiff/differencesdockwidget.h | 2 + Pdf4QtDocDiff/mainwindow.cpp | 113 ++++++++++++++++++++++-- Pdf4QtDocDiff/mainwindow.h | 16 ++++ Pdf4QtDocDiff/mainwindow.ui | 5 +- Pdf4QtLib/sources/pdfdiff.cpp | 26 ++++++ Pdf4QtLib/sources/pdfdiff.h | 12 +++ 7 files changed, 174 insertions(+), 14 deletions(-) diff --git a/Pdf4QtDocDiff/differencesdockwidget.cpp b/Pdf4QtDocDiff/differencesdockwidget.cpp index f6948f8..33df982 100644 --- a/Pdf4QtDocDiff/differencesdockwidget.cpp +++ b/Pdf4QtDocDiff/differencesdockwidget.cpp @@ -76,11 +76,13 @@ QSize DifferenceItemDelegate::sizeHint(const QStyleOptionViewItem& option, } DifferencesDockWidget::DifferencesDockWidget(QWidget* parent, + pdf::PDFDiffResult* unfilteredDiffResult, pdf::PDFDiffResult* diffResult, pdf::PDFDiffResultNavigator* diffNavigator, Settings* settings) : QDockWidget(parent), ui(new Ui::DifferencesDockWidget), + m_unfilteredDiffResult(unfilteredDiffResult), m_diffResult(diffResult), m_diffNavigator(diffNavigator), m_settings(settings), @@ -190,7 +192,17 @@ void DifferencesDockWidget::update() }; const size_t differenceCount = m_diffResult->getDifferencesCount(); - ui->infoTextLabel->setText(tr("%1 Differences").arg(differenceCount)); + const size_t unfilteredDifferenceCount = m_unfilteredDiffResult->getDifferencesCount(); + const size_t hiddenDifferences = unfilteredDifferenceCount - differenceCount; + + if (hiddenDifferences > 0) + { + ui->infoTextLabel->setText(tr("%1 Differences (+%2 hidden)").arg(differenceCount).arg(hiddenDifferences)); + } + else + { + ui->infoTextLabel->setText(tr("%1 Differences").arg(differenceCount)); + } pdf::PDFInteger lastLeftPageIndex = -1; pdf::PDFInteger lastRightPageIndex = -1; diff --git a/Pdf4QtDocDiff/differencesdockwidget.h b/Pdf4QtDocDiff/differencesdockwidget.h index c364666..13f1131 100644 --- a/Pdf4QtDocDiff/differencesdockwidget.h +++ b/Pdf4QtDocDiff/differencesdockwidget.h @@ -59,6 +59,7 @@ class DifferencesDockWidget : public QDockWidget public: explicit DifferencesDockWidget(QWidget* parent, + pdf::PDFDiffResult* unfilteredDiffResult, pdf::PDFDiffResult* diffResult, pdf::PDFDiffResultNavigator* diffNavigator, Settings* settings); @@ -76,6 +77,7 @@ private: QColor getColorForIndex(size_t index) const; QModelIndex findResultIndex(size_t index) const; + pdf::PDFDiffResult* m_unfilteredDiffResult; pdf::PDFDiffResult* m_diffResult; pdf::PDFDiffResultNavigator* m_diffNavigator; Settings* m_settings; diff --git a/Pdf4QtDocDiff/mainwindow.cpp b/Pdf4QtDocDiff/mainwindow.cpp index d8ddc84..7824caf 100644 --- a/Pdf4QtDocDiff/mainwindow.cpp +++ b/Pdf4QtDocDiff/mainwindow.cpp @@ -24,6 +24,7 @@ #include "pdfwidgetutils.h" #include "pdfdocumentreader.h" +#include "pdfdrawspacecontroller.h" #include #include @@ -31,6 +32,7 @@ #include #include #include +#include namespace pdfdocdiff { @@ -41,8 +43,11 @@ MainWindow::MainWindow(QWidget* parent) : m_progress(new pdf::PDFProgress(this)), m_taskbarButton(new QWinTaskbarButton(this)), m_progressTaskbarIndicator(nullptr), + m_cmsManager(nullptr), + m_pdfWidget(nullptr), m_settingsDockWidget(nullptr), m_differencesDockWidget(nullptr), + m_optionalContentActivity(nullptr), m_diff(nullptr), m_isChangingProgressStep(false), m_dontDisplayErrorMessage(false), @@ -58,9 +63,16 @@ MainWindow::MainWindow(QWidget* parent) : m_settingsDockWidget = new SettingsDockWidget(this); addDockWidget(Qt::LeftDockWidgetArea, m_settingsDockWidget);; - m_differencesDockWidget = new DifferencesDockWidget(this, &m_filteredDiffResult, &m_diffNavigator, &m_settings); + m_differencesDockWidget = new DifferencesDockWidget(this, &m_diffResult, &m_filteredDiffResult, &m_diffNavigator, &m_settings); addDockWidget(Qt::LeftDockWidgetArea, m_differencesDockWidget); + ui->documentFrame->setLayout(new QVBoxLayout); + + m_cmsManager = new pdf::PDFCMSManager(this); + m_pdfWidget = new pdf::PDFWidget(m_cmsManager, pdf::RendererEngine::Software, 1, ui->documentFrame); + m_pdfWidget->getDrawWidgetProxy()->setProgress(m_progress); + ui->documentFrame->layout()->addWidget(m_pdfWidget); + ui->menuView->addSeparator(); ui->menuView->addAction(m_settingsDockWidget->toggleViewAction()); ui->menuView->addAction(m_differencesDockWidget->toggleViewAction()); @@ -86,6 +98,14 @@ MainWindow::MainWindow(QWidget* parent) : ui->actionShow_Pages_with_Differences->setData(int(Operation::ShowPageswithDifferences)); ui->actionSave_Differences_to_XML->setData(int(Operation::SaveDifferencesToXML)); + QActionGroup* actionGroup = new QActionGroup(this); + actionGroup->setExclusionPolicy(QActionGroup::ExclusionPolicy::Exclusive); + actionGroup->addAction(ui->actionView_Differences); + actionGroup->addAction(ui->actionView_Left); + actionGroup->addAction(ui->actionView_Right); + actionGroup->addAction(ui->actionView_Overlay); + ui->actionView_Differences->setChecked(true); + QToolBar* mainToolbar = addToolBar(tr("Main")); mainToolbar->setObjectName("main_toolbar"); mainToolbar->addActions({ ui->actionOpen_Left, ui->actionOpen_Right }); @@ -170,6 +190,8 @@ void MainWindow::onMappedActionTriggered(int actionId) void MainWindow::onComparationFinished() { + clear(false, false); + m_diffResult = m_diff.getResult(); if (!m_dontDisplayErrorMessage) @@ -326,6 +348,7 @@ void MainWindow::performOperation(Operation operation) std::optional document = openDocument(); if (document) { + clear(true, false); m_leftDocument = std::move(*document); const size_t pageCount = m_leftDocument.getCatalog()->getPageCount(); @@ -341,11 +364,10 @@ void MainWindow::performOperation(Operation operation) { ui->leftPageSelectionEdit->clear(); } + + updateViewDocument(); } - else - { - ui->leftPageSelectionEdit->clear(); - } + break; } @@ -357,6 +379,7 @@ void MainWindow::performOperation(Operation operation) std::optional document = openDocument(); if (document) { + clear(false, true); m_rightDocument = std::move(*document); const size_t pageCount = m_rightDocument.getCatalog()->getPageCount(); @@ -372,11 +395,10 @@ void MainWindow::performOperation(Operation operation) { ui->rightPageSelectionEdit->clear(); } + + updateViewDocument(); } - else - { - ui->rightPageSelectionEdit->clear(); - } + break; } @@ -451,6 +473,9 @@ void MainWindow::performOperation(Operation operation) case Operation::ViewLeft: case Operation::ViewRight: case Operation::ViewOverlay: + updateViewDocument(); + break; + case Operation::ShowPageswithDifferences: case Operation::SaveDifferencesToXML: case Operation::CreateCompareReport: @@ -467,6 +492,53 @@ void MainWindow::performOperation(Operation operation) updateActions(); } +void MainWindow::setViewDocument(pdf::PDFDocument* document) +{ + if (document != m_pdfWidget->getDrawWidgetProxy()->getDocument()) + { + m_optionalContentActivity->deleteLater(); + m_optionalContentActivity = nullptr; + + if (document) + { + m_optionalContentActivity = new pdf::PDFOptionalContentActivity(document, pdf::OCUsage::View, this); + } + + if (document) + { + pdf::PDFModifiedDocument modifiedDocument(document, m_optionalContentActivity); + m_pdfWidget->setDocument(modifiedDocument); + } + else + { + m_pdfWidget->setDocument(pdf::PDFModifiedDocument()); + } + } +} + +void MainWindow::clear(bool clearLeftDocument, bool clearRightDocument) +{ + setViewDocument(nullptr); + + if (clearLeftDocument) + { + m_leftDocument = pdf::PDFDocument(); + ui->leftPageSelectionEdit->clear(); + } + + if (clearRightDocument) + { + m_rightDocument = pdf::PDFDocument(); + ui->rightPageSelectionEdit->clear(); + } + + m_diffResult = pdf::PDFDiffResult(); + m_filteredDiffResult = pdf::PDFDiffResult(); + m_diffNavigator.update(); + + updateAll(false); +} + void MainWindow::updateAll(bool resetFilters) { if (resetFilters) @@ -479,6 +551,7 @@ void MainWindow::updateAll(bool resetFilters) } updateFilteredResult(); + updateViewDocument(); } void MainWindow::updateFilteredResult() @@ -498,6 +571,28 @@ void MainWindow::updateFilteredResult() updateActions(); } +void MainWindow::updateViewDocument() +{ + pdf::PDFDocument* document = nullptr; + + if (ui->actionView_Left->isChecked()) + { + document = &m_leftDocument; + } + + if (ui->actionView_Right->isChecked()) + { + document = &m_rightDocument; + } + + if (ui->actionView_Differences->isChecked() || ui->actionView_Overlay->isChecked()) + { + document = &m_combinedDocument; + } + + setViewDocument(document); +} + std::optional MainWindow::openDocument() { QString fileName = QFileDialog::getOpenFileName(this, tr("Select PDF document"), m_settings.directory, tr("PDF document (*.pdf)")); diff --git a/Pdf4QtDocDiff/mainwindow.h b/Pdf4QtDocDiff/mainwindow.h index ca02bbe..60274a4 100644 --- a/Pdf4QtDocDiff/mainwindow.h +++ b/Pdf4QtDocDiff/mainwindow.h @@ -22,6 +22,9 @@ #include "pdfdocument.h" #include "pdfdiff.h" +#include "pdfdrawwidget.h" +#include "pdfcms.h" +#include "pdfoptionalcontent.h" #include #include @@ -95,8 +98,17 @@ private: bool canPerformOperation(Operation operation) const; void performOperation(Operation operation); + void setViewDocument(pdf::PDFDocument* document); + + /// Clears all data, and possibly documents also. + /// View document is set to nullptr. + /// \param clearLeftDocument Clear left document? + /// \param clearRightDocument Clear left document? + void clear(bool clearLeftDocument, bool clearRightDocument); + void updateAll(bool resetFilters); void updateFilteredResult(); + void updateViewDocument(); std::optional openDocument(); @@ -105,8 +117,11 @@ private: pdf::PDFProgress* m_progress; QWinTaskbarButton* m_taskbarButton; QWinTaskbarProgress* m_progressTaskbarIndicator; + pdf::PDFCMSManager* m_cmsManager; + pdf::PDFWidget* m_pdfWidget; SettingsDockWidget* m_settingsDockWidget; DifferencesDockWidget* m_differencesDockWidget; + pdf::PDFOptionalContentActivity* m_optionalContentActivity; Settings m_settings; QSignalMapper m_mapper; @@ -116,6 +131,7 @@ private: pdf::PDFDocument m_leftDocument; pdf::PDFDocument m_rightDocument; + pdf::PDFDocument m_combinedDocument; pdf::PDFDiffResult m_diffResult; pdf::PDFDiffResult m_filteredDiffResult; ///< Difference result with filters applied diff --git a/Pdf4QtDocDiff/mainwindow.ui b/Pdf4QtDocDiff/mainwindow.ui index f10ab54..baeb793 100644 --- a/Pdf4QtDocDiff/mainwindow.ui +++ b/Pdf4QtDocDiff/mainwindow.ui @@ -54,16 +54,13 @@ - + QFrame::Box QFrame::Raised - - 3 - diff --git a/Pdf4QtLib/sources/pdfdiff.cpp b/Pdf4QtLib/sources/pdfdiff.cpp index 7917447..942b895 100644 --- a/Pdf4QtLib/sources/pdfdiff.cpp +++ b/Pdf4QtLib/sources/pdfdiff.cpp @@ -465,6 +465,8 @@ void PDFDiff::performCompare(const std::vector& leftPrepared using AlgorithmLCS = PDFAlgorithmLongestCommonSubsequenceBase; auto modifiedRanges = AlgorithmLCS::getModifiedRanges(pageSequence); + PDFDiffResult::PageSequence resultPageSequence; + resultPageSequence.reserve(pageSequence.size()); // First find all moved pages for (const AlgorithmLCS::SequenceItem& item : pageSequence) @@ -480,7 +482,21 @@ void PDFDiff::performCompare(const std::vector& leftPrepared { result.addPageMoved(leftPreparedPages[item.index1].pageIndex, rightPreparedPages[item.index2].pageIndex); } + + PDFDiffResult::PageSequenceItem pageSequenceItem; + if (item.isLeftValid()) + { + const PDFInteger leftIndex = leftPreparedPages[item.index1].pageIndex; + pageSequenceItem.leftPage = leftIndex; + } + if (item.isRightValid()) + { + const PDFInteger rightIndex = rightPreparedPages[item.index2].pageIndex; + pageSequenceItem.rightPage = rightIndex; + } + resultPageSequence.emplace_back(pageSequenceItem); } + result.setPageSequence(std::move(resultPageSequence)); std::vector textFlowDifferences; @@ -1242,6 +1258,16 @@ void PDFDiffResult::addRectRight(Difference& difference, QRectF rect) m_rects.emplace_back(difference.pageIndex2, rect); } +const PDFDiffResult::PageSequence& PDFDiffResult::getPageSequence() const +{ + return m_pageSequence; +} + +void PDFDiffResult::setPageSequence(PageSequence pageSequence) +{ + m_pageSequence = pageSequence; +} + PDFDiffHelper::Differences PDFDiffHelper::calculateDifferences(const GraphicPieceInfos& left, const GraphicPieceInfos& right, PDFReal epsilon) diff --git a/Pdf4QtLib/sources/pdfdiff.h b/Pdf4QtLib/sources/pdfdiff.h index c96c1e0..ef91dc2 100644 --- a/Pdf4QtLib/sources/pdfdiff.h +++ b/Pdf4QtLib/sources/pdfdiff.h @@ -59,6 +59,14 @@ public: TextRemoved = 0x2000, }; + struct PageSequenceItem + { + PDFInteger leftPage = -1; + PDFInteger rightPage = -1; + }; + + using PageSequence = std::vector; + using RectInfos = std::vector>; void setResult(PDFOperationResult result) { m_result = std::move(result); } @@ -110,6 +118,9 @@ public: bool filterImageDifferences, bool filterShadingDifferences); + const PageSequence& getPageSequence() const; + void setPageSequence(PageSequence pageSequence); + private: friend class PDFDiff; @@ -180,6 +191,7 @@ private: PDFOperationResult m_result; QStringList m_strings; uint32_t m_typeFlags = 0; + PageSequence m_pageSequence; }; /// Class for result navigation, can go to next, or previous result.