From d3aa9efcb3ee2f2de3b16178ab56948c4e0dd7b5 Mon Sep 17 00:00:00 2001 From: Jakub Melka Date: Sat, 2 Oct 2021 15:57:07 +0200 Subject: [PATCH] DocDiff application: differences tree view --- Pdf4QtDocDiff/Pdf4QtDocDiff.pro | 13 +- Pdf4QtDocDiff/differencesdockwidget.cpp | 167 ++++++++++++++++++++++++ Pdf4QtDocDiff/differencesdockwidget.h | 74 +++++++++++ Pdf4QtDocDiff/differencesdockwidget.ui | 38 ++++++ Pdf4QtDocDiff/mainwindow.cpp | 29 ++++ Pdf4QtDocDiff/mainwindow.h | 12 +- Pdf4QtDocDiff/settings.h | 38 ++++++ Pdf4QtDocDiff/settingsdockwidget.cpp | 36 +++++ Pdf4QtDocDiff/settingsdockwidget.h | 45 +++++++ Pdf4QtDocDiff/settingsdockwidget.ui | 23 ++++ Pdf4QtLib/sources/pdfdiff.cpp | 30 ++++- Pdf4QtLib/sources/pdfdiff.h | 10 ++ 12 files changed, 502 insertions(+), 13 deletions(-) create mode 100644 Pdf4QtDocDiff/differencesdockwidget.cpp create mode 100644 Pdf4QtDocDiff/differencesdockwidget.h create mode 100644 Pdf4QtDocDiff/differencesdockwidget.ui create mode 100644 Pdf4QtDocDiff/settings.h create mode 100644 Pdf4QtDocDiff/settingsdockwidget.cpp create mode 100644 Pdf4QtDocDiff/settingsdockwidget.h create mode 100644 Pdf4QtDocDiff/settingsdockwidget.ui diff --git a/Pdf4QtDocDiff/Pdf4QtDocDiff.pro b/Pdf4QtDocDiff/Pdf4QtDocDiff.pro index 8fa4b56..58c624f 100644 --- a/Pdf4QtDocDiff/Pdf4QtDocDiff.pro +++ b/Pdf4QtDocDiff/Pdf4QtDocDiff.pro @@ -43,16 +43,23 @@ INSTALLS += application SOURCES += \ aboutdialog.cpp \ + differencesdockwidget.cpp \ main.cpp \ - mainwindow.cpp + mainwindow.cpp \ + settingsdockwidget.cpp FORMS += \ aboutdialog.ui \ - mainwindow.ui + differencesdockwidget.ui \ + mainwindow.ui \ + settingsdockwidget.ui HEADERS += \ aboutdialog.h \ - mainwindow.h + differencesdockwidget.h \ + mainwindow.h \ + settings.h \ + settingsdockwidget.h RESOURCES += \ resources.qrc diff --git a/Pdf4QtDocDiff/differencesdockwidget.cpp b/Pdf4QtDocDiff/differencesdockwidget.cpp new file mode 100644 index 0000000..4476f94 --- /dev/null +++ b/Pdf4QtDocDiff/differencesdockwidget.cpp @@ -0,0 +1,167 @@ +// Copyright (C) 2021 Jakub Melka +// +// This file is part of PDF4QT. +// +// PDF4QT is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// with the written consent of the copyright owner, any later version. +// +// PDF4QT is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with PDF4QT. If not, see . + +#include "differencesdockwidget.h" +#include "ui_differencesdockwidget.h" + +#include "pdfdiff.h" +#include "pdfwidgetutils.h" + +#include + +namespace pdfdocdiff +{ + +DifferenceItemDelegate::DifferenceItemDelegate(QObject* parent) : + BaseClass(parent) +{ + +} + +void DifferenceItemDelegate::paint(QPainter* painter, + const QStyleOptionViewItem& option, + const QModelIndex& index) const +{ + BaseClass::paint(painter, option, index); +} + +QSize DifferenceItemDelegate::sizeHint(const QStyleOptionViewItem& option, + const QModelIndex& index) const +{ + if (!option.rect.isValid()) + { + // Jakub Melka: Why this? We need to use text wrapping. Unfortunately, + // standard delegate needs correct text rectangle (at least rectangle width), + // for word wrap calculation. So we must manually calculate rectangle width. + // Of course, we cant use visualRect of the tree widget, because of cyclical + // dependence. + QStyleOptionViewItem adjustedOption = option; + const QTreeWidget* treeWidget = qobject_cast(option.widget); + int xOffset = treeWidget->columnViewportPosition(index.column()); + int height = option.fontMetrics.lineSpacing(); + int yOffset = 0; + int width = treeWidget->columnWidth(index.column()); + + int level = treeWidget->rootIsDecorated() ? 1 : 0; + QModelIndex currentIndex = index.parent(); + while (currentIndex.isValid()) + { + ++level; + currentIndex = currentIndex.parent(); + } + + xOffset += level * treeWidget->indentation(); + adjustedOption.rect = QRect(xOffset, yOffset, width - xOffset, height); + return BaseClass::sizeHint(adjustedOption, index); + } + + return BaseClass::sizeHint(option, index); +} + +DifferencesDockWidget::DifferencesDockWidget(QWidget *parent) : + QDockWidget(parent), + ui(new Ui::DifferencesDockWidget), + m_diffResult(nullptr), + m_diffNavigator(nullptr) +{ + ui->setupUi(this); + + ui->differencesTreeWidget->setItemDelegate(new DifferenceItemDelegate(this)); + + setMinimumWidth(pdf::PDFWidgetUtils::scaleDPI_x(this, 120)); +} + +DifferencesDockWidget::~DifferencesDockWidget() +{ + delete ui; +} + +void DifferencesDockWidget::update() +{ + ui->differencesTreeWidget->clear(); + + QList topItems; + + QLocale locale; + + if (m_diffResult && !m_diffResult->isSame()) + { + const size_t differenceCount = m_diffResult->getDifferencesCount(); + + pdf::PDFInteger lastLeftPageIndex = -1; + pdf::PDFInteger lastRightPageIndex = -1; + + for (size_t i = 0; i < differenceCount; ++i) + { + pdf::PDFInteger pageIndexLeft = m_diffResult->getLeftPage(i); + pdf::PDFInteger pageIndexRight = m_diffResult->getRightPage(i); + + if (lastLeftPageIndex != pageIndexLeft || + lastRightPageIndex != pageIndexRight || + topItems.empty()) + { + // Create new top level item + QStringList captionParts; + captionParts << QString("#%1:").arg(topItems.size() + 1); + + if (pageIndexLeft == pageIndexRight) + { + captionParts << tr("Page %1").arg(locale.toString(pageIndexLeft + 1)); + } + else + { + if (pageIndexLeft != -1) + { + captionParts << tr("Left %1").arg(locale.toString(pageIndexLeft + 1)); + } + + if (pageIndexRight != -1) + { + captionParts << tr("Right %1").arg(locale.toString(pageIndexRight + 1)); + } + } + + QTreeWidgetItem* item = new QTreeWidgetItem(QStringList() << captionParts.join(" ")); + topItems << item; + + lastLeftPageIndex = pageIndexLeft; + lastRightPageIndex = pageIndexRight; + } + + Q_ASSERT(!topItems.isEmpty()); + QTreeWidgetItem* parent = topItems.back(); + + QTreeWidgetItem* item = new QTreeWidgetItem(parent, QStringList() << m_diffResult->getMessage(i)); + item->setData(0, Qt::UserRole, i); + } + } + + ui->differencesTreeWidget->addTopLevelItems(topItems); + ui->differencesTreeWidget->expandAll(); +} + +void DifferencesDockWidget::setDiffResult(pdf::PDFDiffResult* diffResult) +{ + m_diffResult = diffResult; +} + +void DifferencesDockWidget::setDiffNavigator(pdf::PDFDiffResultNavigator* diffNavigator) +{ + m_diffNavigator = diffNavigator; +} + +} // namespace pdfdocdiff diff --git a/Pdf4QtDocDiff/differencesdockwidget.h b/Pdf4QtDocDiff/differencesdockwidget.h new file mode 100644 index 0000000..e9594ff --- /dev/null +++ b/Pdf4QtDocDiff/differencesdockwidget.h @@ -0,0 +1,74 @@ +// Copyright (C) 2021 Jakub Melka +// +// This file is part of PDF4QT. +// +// PDF4QT is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// with the written consent of the copyright owner, any later version. +// +// PDF4QT is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with PDF4QT. If not, see . + +#ifndef DIFFERENCESDOCKWIDGET_H +#define DIFFERENCESDOCKWIDGET_H + +#include +#include + +namespace Ui +{ +class DifferencesDockWidget; +} + +namespace pdf +{ +class PDFDiffResult; +class PDFDiffResultNavigator; +} + +namespace pdfdocdiff +{ + +class DifferenceItemDelegate : public QItemDelegate +{ + Q_OBJECT + +private: + using BaseClass = QItemDelegate; + +public: + DifferenceItemDelegate(QObject* parent); + + virtual void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const override; + virtual QSize sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const override; +}; + +class DifferencesDockWidget : public QDockWidget +{ + Q_OBJECT + +public: + explicit DifferencesDockWidget(QWidget* parent); + virtual ~DifferencesDockWidget() override; + + void update(); + + void setDiffResult(pdf::PDFDiffResult* diffResult); + void setDiffNavigator(pdf::PDFDiffResultNavigator* diffNavigator); + +private: + Ui::DifferencesDockWidget* ui; + + pdf::PDFDiffResult* m_diffResult; + pdf::PDFDiffResultNavigator* m_diffNavigator; +}; + +} // namespace pdfdocdiff + +#endif // DIFFERENCESDOCKWIDGET_H diff --git a/Pdf4QtDocDiff/differencesdockwidget.ui b/Pdf4QtDocDiff/differencesdockwidget.ui new file mode 100644 index 0000000..057265f --- /dev/null +++ b/Pdf4QtDocDiff/differencesdockwidget.ui @@ -0,0 +1,38 @@ + + + DifferencesDockWidget + + + + 0 + 0 + 562 + 537 + + + + Differences + + + + + + + true + + + false + + + + 1 + + + + + + + + + + diff --git a/Pdf4QtDocDiff/mainwindow.cpp b/Pdf4QtDocDiff/mainwindow.cpp index 96cc91a..9ce99cf 100644 --- a/Pdf4QtDocDiff/mainwindow.cpp +++ b/Pdf4QtDocDiff/mainwindow.cpp @@ -19,6 +19,8 @@ #include "ui_mainwindow.h" #include "aboutdialog.h" +#include "differencesdockwidget.h" +#include "settingsdockwidget.h" #include "pdfwidgetutils.h" #include "pdfdocumentreader.h" @@ -39,6 +41,8 @@ MainWindow::MainWindow(QWidget* parent) : m_progress(new pdf::PDFProgress(this)), m_taskbarButton(new QWinTaskbarButton(this)), m_progressTaskbarIndicator(nullptr), + m_settingsDockWidget(nullptr), + m_differencesDockWidget(nullptr), m_diff(nullptr), m_isChangingProgressStep(false), m_dontDisplayErrorMessage(false), @@ -51,6 +55,18 @@ MainWindow::MainWindow(QWidget* parent) : // Initialize task bar progress m_progressTaskbarIndicator = m_taskbarButton->progress(); + m_settingsDockWidget = new SettingsDockWidget(this); + addDockWidget(Qt::LeftDockWidgetArea, m_settingsDockWidget);; + + m_differencesDockWidget = new DifferencesDockWidget(this); + m_differencesDockWidget->setDiffResult(&m_filteredDiffResult); + m_differencesDockWidget->setDiffNavigator(&m_diffNavigator); + addDockWidget(Qt::LeftDockWidgetArea, m_differencesDockWidget); + + ui->menuView->addSeparator(); + ui->menuView->addAction(m_settingsDockWidget->toggleViewAction()); + ui->menuView->addAction(m_differencesDockWidget->toggleViewAction()); + ui->actionGet_Source->setData(int(Operation::GetSource)); ui->actionAbout->setData(int(Operation::About)); ui->actionOpen_Left->setData(int(Operation::OpenLeft)); @@ -217,6 +233,10 @@ void MainWindow::loadSettings() settings.beginGroup("Settings"); m_settings.directory = settings.value("directory").toString(); + m_settings.colorPageMove = settings.value("colorPageMove").value(); + m_settings.colorAdded = settings.value("colorAdded").value(); + m_settings.colorRemoved = settings.value("colorRemoved").value(); + m_settings.colorReplaced = settings.value("colorReplaced").value(); settings.endGroup(); } @@ -230,6 +250,10 @@ void MainWindow::saveSettings() settings.beginGroup("Settings"); settings.setValue("directory", m_settings.directory); + settings.setValue("colorPageMove", m_settings.colorPageMove); + settings.setValue("colorAdded", m_settings.colorAdded); + settings.setValue("colorRemoved", m_settings.colorRemoved); + settings.setValue("colorReplaced", m_settings.colorReplaced); settings.endGroup(); } @@ -461,6 +485,11 @@ void MainWindow::updateFilteredResult() ui->actionFilter_Shading->isChecked()); m_diffNavigator.update(); + if (m_differencesDockWidget) + { + m_differencesDockWidget->update(); + } + updateActions(); } diff --git a/Pdf4QtDocDiff/mainwindow.h b/Pdf4QtDocDiff/mainwindow.h index 6a275e8..1f87928 100644 --- a/Pdf4QtDocDiff/mainwindow.h +++ b/Pdf4QtDocDiff/mainwindow.h @@ -18,6 +18,8 @@ #ifndef PDFDOCDIFF_MAINWINDOW_H #define PDFDOCDIFF_MAINWINDOW_H +#include "settings.h" + #include "pdfdocument.h" #include "pdfdiff.h" @@ -34,6 +36,9 @@ class MainWindow; namespace pdfdocdiff { +class SettingsDockWidget; +class DifferencesDockWidget; + class MainWindow : public QMainWindow { Q_OBJECT @@ -94,16 +99,13 @@ private: std::optional openDocument(); - struct Settings - { - QString directory; - }; - Ui::MainWindow* ui; pdf::PDFProgress* m_progress; QWinTaskbarButton* m_taskbarButton; QWinTaskbarProgress* m_progressTaskbarIndicator; + SettingsDockWidget* m_settingsDockWidget; + DifferencesDockWidget* m_differencesDockWidget; Settings m_settings; QSignalMapper m_mapper; diff --git a/Pdf4QtDocDiff/settings.h b/Pdf4QtDocDiff/settings.h new file mode 100644 index 0000000..ab95102 --- /dev/null +++ b/Pdf4QtDocDiff/settings.h @@ -0,0 +1,38 @@ +// Copyright (C) 2021 Jakub Melka +// +// This file is part of PDF4QT. +// +// PDF4QT is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// with the written consent of the copyright owner, any later version. +// +// PDF4QT is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with PDF4QT. If not, see . + +#ifndef SETTINGS_H +#define SETTINGS_H + +#include + +namespace pdfdocdiff +{ + +struct Settings +{ + QString directory; + QColor colorPageMove = QColor(35, 145, 255); + QColor colorAdded = QColor(125, 250, 0); + QColor colorRemoved = QColor(255, 50, 50); + QColor colorReplaced = QColor(255, 120, 30); +}; + +} // namespace pdfdocdiff + + +#endif // SETTINGS_H diff --git a/Pdf4QtDocDiff/settingsdockwidget.cpp b/Pdf4QtDocDiff/settingsdockwidget.cpp new file mode 100644 index 0000000..710179a --- /dev/null +++ b/Pdf4QtDocDiff/settingsdockwidget.cpp @@ -0,0 +1,36 @@ +// Copyright (C) 2021 Jakub Melka +// +// This file is part of PDF4QT. +// +// PDF4QT is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// with the written consent of the copyright owner, any later version. +// +// PDF4QT is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with PDF4QT. If not, see . + +#include "settingsdockwidget.h" +#include "ui_settingsdockwidget.h" + +namespace pdfdocdiff +{ + +SettingsDockWidget::SettingsDockWidget(QWidget *parent) : + QDockWidget(parent), + ui(new Ui::SettingsDockWidget) +{ + ui->setupUi(this); +} + +SettingsDockWidget::~SettingsDockWidget() +{ + delete ui; +} + +} // namespace pdfdocdiff diff --git a/Pdf4QtDocDiff/settingsdockwidget.h b/Pdf4QtDocDiff/settingsdockwidget.h new file mode 100644 index 0000000..b969247 --- /dev/null +++ b/Pdf4QtDocDiff/settingsdockwidget.h @@ -0,0 +1,45 @@ +// Copyright (C) 2021 Jakub Melka +// +// This file is part of PDF4QT. +// +// PDF4QT is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// with the written consent of the copyright owner, any later version. +// +// PDF4QT is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with PDF4QT. If not, see . + +#ifndef SETTINGSDOCKWIDGET_H +#define SETTINGSDOCKWIDGET_H + +#include + +namespace Ui +{ +class SettingsDockWidget; +} + +namespace pdfdocdiff +{ + +class SettingsDockWidget : public QDockWidget +{ + Q_OBJECT + +public: + explicit SettingsDockWidget(QWidget* parent); + virtual ~SettingsDockWidget() override; + +private: + Ui::SettingsDockWidget* ui; +}; + +} // namespace pdfdocdiff + +#endif // SETTINGSDOCKWIDGET_H diff --git a/Pdf4QtDocDiff/settingsdockwidget.ui b/Pdf4QtDocDiff/settingsdockwidget.ui new file mode 100644 index 0000000..4af6bdf --- /dev/null +++ b/Pdf4QtDocDiff/settingsdockwidget.ui @@ -0,0 +1,23 @@ + + + SettingsDockWidget + + + + 0 + 0 + 400 + 300 + + + + Qt::LeftDockWidgetArea|Qt::RightDockWidgetArea + + + Settings + + + + + + diff --git a/Pdf4QtLib/sources/pdfdiff.cpp b/Pdf4QtLib/sources/pdfdiff.cpp index 0a75c14..6589f52 100644 --- a/Pdf4QtLib/sources/pdfdiff.cpp +++ b/Pdf4QtLib/sources/pdfdiff.cpp @@ -1085,7 +1085,7 @@ QString PDFDiffResult::getMessage(size_t index) const switch (difference.type) { case Type::PageMoved: - return PDFDiff::tr("Page no. %1 from old document has been moved to a new document at page no. %2.").arg(difference.pageIndex1 + 1).arg(difference.pageIndex2 + 1); + return PDFDiff::tr("Page no. %1 was moved to a page no. %2.").arg(difference.pageIndex1 + 1).arg(difference.pageIndex2 + 1); case Type::PageAdded: return PDFDiff::tr("Page no. %1 was added.").arg(difference.pageIndex2 + 1); @@ -1106,16 +1106,16 @@ QString PDFDiffResult::getMessage(size_t index) const return PDFDiff::tr("Removed shading from page %1.").arg(difference.pageIndex1 + 1); case Type::AddedTextCharContent: - return PDFDiff::tr("Added text character from page %1.").arg(difference.pageIndex2 + 1); + return PDFDiff::tr("Added text character to page %1.").arg(difference.pageIndex2 + 1); case Type::AddedVectorGraphicContent: - return PDFDiff::tr("Added vector graphics from page %1.").arg(difference.pageIndex2 + 1); + return PDFDiff::tr("Added vector graphics to page %1.").arg(difference.pageIndex2 + 1); case Type::AddedImageContent: - return PDFDiff::tr("Added image from page %1.").arg(difference.pageIndex2 + 1); + return PDFDiff::tr("Added image to page %1.").arg(difference.pageIndex2 + 1); case Type::AddedShadingContent: - return PDFDiff::tr("Added shading from page %1.").arg(difference.pageIndex2 + 1); + return PDFDiff::tr("Added shading to page %1.").arg(difference.pageIndex2 + 1); case Type::TextAdded: return PDFDiff::tr("Text '%1' has been added to page %2.").arg(m_strings[difference.textAddedIndex]).arg(difference.pageIndex2 + 1); @@ -1134,6 +1134,26 @@ QString PDFDiffResult::getMessage(size_t index) const return QString(); } +PDFInteger PDFDiffResult::getLeftPage(size_t index) const +{ + if (index >= m_differences.size()) + { + return -1; + } + + return m_differences[index].pageIndex1; +} + +PDFInteger PDFDiffResult::getRightPage(size_t index) const +{ + if (index >= m_differences.size()) + { + return -1; + } + + return m_differences[index].pageIndex2; +} + PDFDiffResult PDFDiffResult::filter(bool filterPageMoveDifferences, bool filterTextDifferences, bool filterVectorGraphicsDifferences, diff --git a/Pdf4QtLib/sources/pdfdiff.h b/Pdf4QtLib/sources/pdfdiff.h index 163cb8d..36e9118 100644 --- a/Pdf4QtLib/sources/pdfdiff.h +++ b/Pdf4QtLib/sources/pdfdiff.h @@ -77,6 +77,16 @@ public: /// \param index Index QString getMessage(size_t index) const; + /// Returns index of left page (or -1, if difference occured + /// only on a right page) + /// \param index Index + PDFInteger getLeftPage(size_t index) const; + + /// Returns index of right page (or -1, if difference occured + /// only on a left page) + /// \param index Index + PDFInteger getRightPage(size_t index) const; + bool hasPageMoveDifferences() const { return m_typeFlags & FLAGS_PAGE_MOVE; } bool hasTextDifferences() const { return m_typeFlags & FLAGS_TEXT; } bool hasVectorGraphicsDifferences() const { return m_typeFlags & FLAGS_VECTOR_GRAPHICS; }