DocDiff application: draw differences onto pages

This commit is contained in:
Jakub Melka
2021-10-14 19:12:56 +02:00
parent bafcc36d3f
commit e838f5130a
7 changed files with 537 additions and 306 deletions

View File

@ -52,7 +52,8 @@ MainWindow::MainWindow(QWidget* parent) :
m_diff(nullptr),
m_isChangingProgressStep(false),
m_dontDisplayErrorMessage(false),
m_diffNavigator(nullptr)
m_diffNavigator(nullptr),
m_drawInterface(&m_settings, &m_documentMapper, &m_filteredDiffResult)
{
ui->setupUi(this);
@ -73,6 +74,7 @@ MainWindow::MainWindow(QWidget* parent) :
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);
m_pdfWidget->getDrawWidgetProxy()->registerDrawInterface(&m_drawInterface);
ui->menuView->addSeparator();
ui->menuView->addAction(m_settingsDockWidget->toggleViewAction());
@ -752,255 +754,4 @@ void MainWindow::onProgressFinished()
m_progressTaskbarIndicator->hide();
}
void ComparedDocumentMapper::update(ComparedDocumentMapper::Mode mode,
bool filterDifferences,
const pdf::PDFDiffResult& diff,
const pdf::PDFDocument* leftDocument,
const pdf::PDFDocument* rightDocument,
const pdf::PDFDocument* currentDocument)
{
m_layout.clear();
m_leftPageIndices.clear();
m_rightPageIndices.clear();
m_allLeft = false;
m_allRight = false;
if (!leftDocument || !rightDocument || !currentDocument)
{
return;
}
// Jakub Melka
pdf::PDFDiffResult::PageSequence pageSequence = diff.getPageSequence();
const bool isEmpty = pageSequence.empty();
if (filterDifferences)
{
pdf::PDFDiffResult::PageSequence filteredPageSequence;
std::vector<pdf::PDFInteger> leftPageIndices = diff.getChangedLeftPageIndices();
std::vector<pdf::PDFInteger> rightPageIndices = diff.getChangedRightPageIndices();
for (const pdf::PDFDiffResult::PageSequenceItem& item : pageSequence)
{
const bool isLeftModified = std::binary_search(leftPageIndices.cbegin(), leftPageIndices.cend(), item.leftPage);
const bool isRightModified = std::binary_search(rightPageIndices.cbegin(), rightPageIndices.cend(), item.rightPage);
if (isLeftModified || isRightModified)
{
filteredPageSequence.push_back(item);
}
}
pageSequence = std::move(filteredPageSequence);
}
switch (mode)
{
case ComparedDocumentMapper::Mode::Left:
{
Q_ASSERT(leftDocument == currentDocument);
m_allLeft = true;
double yPos = 0.0;
const pdf::PDFCatalog* catalog = leftDocument->getCatalog();
if (isEmpty)
{
// Just copy all pages
const size_t pageCount = catalog->getPageCount();
for (size_t i = 0; i < pageCount; ++i)
{
QSizeF pageSize = catalog->getPage(i)->getRotatedMediaBoxMM().size();
QRectF rect(-pageSize.width() * 0.5, yPos, pageSize.width(), pageSize.height());
m_layout.emplace_back(0, i, rect);
yPos += pageSize.height() + 5;
}
}
else
{
for (const pdf::PDFDiffResult::PageSequenceItem& item : pageSequence)
{
if (item.leftPage == -1)
{
continue;
}
QSizeF pageSize = catalog->getPage(item.leftPage)->getRotatedMediaBoxMM().size();
QRectF rect(-pageSize.width() * 0.5, yPos, pageSize.width(), pageSize.height());
m_layout.emplace_back(0, item.leftPage, rect);
yPos += pageSize.height() + 5;
}
}
break;
}
case ComparedDocumentMapper::Mode::Right:
{
Q_ASSERT(rightDocument == currentDocument);
m_allRight = true;
double yPos = 0.0;
const pdf::PDFCatalog* catalog = rightDocument->getCatalog();
if (isEmpty)
{
// Just copy all pages
const size_t pageCount = catalog->getPageCount();
for (size_t i = 0; i < pageCount; ++i)
{
QSizeF pageSize = catalog->getPage(i)->getRotatedMediaBoxMM().size();
QRectF rect(-pageSize.width() * 0.5, yPos, pageSize.width(), pageSize.height());
m_layout.emplace_back(0, i, rect);
yPos += pageSize.height() + 5;
}
}
else
{
for (const pdf::PDFDiffResult::PageSequenceItem& item : pageSequence)
{
if (item.rightPage == -1)
{
continue;
}
QSizeF pageSize = catalog->getPage(item.rightPage)->getRotatedMediaBoxMM().size();
QRectF rect(-pageSize.width() * 0.5, yPos, pageSize.width(), pageSize.height());
m_layout.emplace_back(0, item.rightPage, rect);
yPos += pageSize.height() + 5;
}
}
break;
}
case ComparedDocumentMapper::Mode::Combined:
case ComparedDocumentMapper::Mode::Overlay:
{
double yPos = 0.0;
const pdf::PDFCatalog* catalog = currentDocument->getCatalog();
pdf::PDFInteger offset = leftDocument->getCatalog()->getPageCount();
for (const pdf::PDFDiffResult::PageSequenceItem& item : pageSequence)
{
double yAdvance = 0.0;
if (item.leftPage != -1)
{
QSizeF pageSize = catalog->getPage(item.leftPage)->getRotatedMediaBoxMM().size();
QRectF rect;
if (mode == ComparedDocumentMapper::Mode::Combined)
{
rect = QRectF(-pageSize.width() - 5, yPos, pageSize.width(), pageSize.height());
}
else
{
rect = QRectF(-pageSize.width() * 0.5, yPos, pageSize.width(), pageSize.height());
}
m_layout.emplace_back(0, item.leftPage, rect);
yAdvance = pageSize.height() + 5;
m_leftPageIndices[item.leftPage] = item.leftPage;
}
if (item.rightPage != -1)
{
pdf::PDFInteger rightPageIndex = item.rightPage + offset;
QSizeF pageSize = catalog->getPage(rightPageIndex)->getRotatedMediaBoxMM().size();
QRectF rect;
if (mode == ComparedDocumentMapper::Mode::Combined)
{
rect = QRectF(5, yPos, pageSize.width(), pageSize.height());
}
else
{
rect = QRectF(-pageSize.width() * 0.5, yPos, pageSize.width(), pageSize.height());
}
m_layout.emplace_back(0, rightPageIndex, rect);
yAdvance = qMax(yAdvance, pageSize.height() + 5);
m_rightPageIndices[rightPageIndex] = item.rightPage;
}
yPos += yAdvance;
}
break;
}
default:
Q_ASSERT(false);
break;
}
}
pdf::PDFInteger ComparedDocumentMapper::getLeftPageIndex(pdf::PDFInteger pageIndex) const
{
if (m_allLeft)
{
return pageIndex;
}
auto it = m_leftPageIndices.find(pageIndex);
if (it != m_leftPageIndices.cend())
{
return it->second;
}
return -1;
}
pdf::PDFInteger ComparedDocumentMapper::getRightPageIndex(pdf::PDFInteger pageIndex) const
{
if (m_allRight)
{
return pageIndex;
}
auto it = m_rightPageIndices.find(pageIndex);
if (it != m_rightPageIndices.cend())
{
return it->second;
}
return -1;
}
pdf::PDFInteger ComparedDocumentMapper::getPageIndexFromLeftPageIndex(pdf::PDFInteger leftPageIndex) const
{
if (m_allLeft)
{
return leftPageIndex;
}
for (const auto& indexItem : m_leftPageIndices)
{
if (indexItem.second == leftPageIndex)
{
return indexItem.first;
}
}
return -1;
}
pdf::PDFInteger ComparedDocumentMapper::getPageIndexFromRightPageIndex(pdf::PDFInteger rightPageIndex) const
{
if (m_allRight)
{
return rightPageIndex;
}
for (const auto& indexItem : m_rightPageIndices)
{
if (indexItem.second == rightPageIndex)
{
return indexItem.first;
}
}
return -1;
}
} // namespace pdfdocdiff