DocDiff application: differences tree view

This commit is contained in:
Jakub Melka 2021-10-02 15:57:07 +02:00
parent dee2535983
commit d3aa9efcb3
12 changed files with 502 additions and 13 deletions

View File

@ -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

View File

@ -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 <https://www.gnu.org/licenses/>.
#include "differencesdockwidget.h"
#include "ui_differencesdockwidget.h"
#include "pdfdiff.h"
#include "pdfwidgetutils.h"
#include <QTreeWidgetItem>
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<const QTreeWidget*>(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<QTreeWidgetItem*> 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

View File

@ -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 <https://www.gnu.org/licenses/>.
#ifndef DIFFERENCESDOCKWIDGET_H
#define DIFFERENCESDOCKWIDGET_H
#include <QDockWidget>
#include <QItemDelegate>
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

View File

@ -0,0 +1,38 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>DifferencesDockWidget</class>
<widget class="QDockWidget" name="DifferencesDockWidget">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>562</width>
<height>537</height>
</rect>
</property>
<property name="windowTitle">
<string>Differences</string>
</property>
<widget class="QWidget" name="dockWidgetContents">
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QTreeWidget" name="differencesTreeWidget">
<property name="wordWrap">
<bool>true</bool>
</property>
<attribute name="headerVisible">
<bool>false</bool>
</attribute>
<column>
<property name="text">
<string notr="true">1</string>
</property>
</column>
</widget>
</item>
</layout>
</widget>
</widget>
<resources/>
<connections/>
</ui>

View File

@ -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<QColor>();
m_settings.colorAdded = settings.value("colorAdded").value<QColor>();
m_settings.colorRemoved = settings.value("colorRemoved").value<QColor>();
m_settings.colorReplaced = settings.value("colorReplaced").value<QColor>();
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();
}

View File

@ -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<pdf::PDFDocument> 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;

38
Pdf4QtDocDiff/settings.h Normal file
View File

@ -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 <https://www.gnu.org/licenses/>.
#ifndef SETTINGS_H
#define SETTINGS_H
#include <QColor>
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

View File

@ -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 <https://www.gnu.org/licenses/>.
#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

View File

@ -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 <https://www.gnu.org/licenses/>.
#ifndef SETTINGSDOCKWIDGET_H
#define SETTINGSDOCKWIDGET_H
#include <QDockWidget>
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

View File

@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>SettingsDockWidget</class>
<widget class="QDockWidget" name="SettingsDockWidget">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="allowedAreas">
<set>Qt::LeftDockWidgetArea|Qt::RightDockWidgetArea</set>
</property>
<property name="windowTitle">
<string>Settings</string>
</property>
<widget class="QWidget" name="dockWidgetContents"/>
</widget>
<resources/>
<connections/>
</ui>

View File

@ -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,

View File

@ -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; }