PDF4QT/Pdf4QtPageMaster/selectoutlinetoregroupdialo...

163 lines
5.5 KiB
C++
Raw Normal View History

2021-09-27 11:14:20 +02:00
// 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/>.
2023-12-13 20:14:33 +01:00
#include "selectoutlinetoregroupdialog.h"
#include "ui_selectoutlinetoregroupdialog.h"
2021-09-27 11:14:20 +02:00
#include "pdfitemmodels.h"
#include "pdfwidgetutils.h"
#include <QMenu>
2024-03-16 17:02:44 +01:00
namespace pdfpagemaster
2021-09-27 11:14:20 +02:00
{
2023-12-13 20:14:33 +01:00
SelectOutlineToRegroupDialog::SelectOutlineToRegroupDialog(const pdf::PDFDocument* document, QWidget* parent) :
2021-09-27 11:14:20 +02:00
QDialog(parent),
2023-12-13 20:14:33 +01:00
ui(new Ui::SelectOutlineToRegroupDialog),
2021-09-27 11:14:20 +02:00
m_document(document),
m_model(nullptr)
{
ui->setupUi(this);
2024-03-16 17:02:44 +01:00
QIcon bookmarkIcon(":/pdfpagemaster/resources/bookmark.svg");
2021-09-27 11:14:20 +02:00
m_model = new pdf::PDFSelectableOutlineTreeItemModel(qMove(bookmarkIcon), this);
2023-12-13 20:14:33 +01:00
ui->outlineView->setModel(m_model);
ui->outlineView->header()->hide();
2021-09-27 11:14:20 +02:00
m_model->setDocument(pdf::PDFModifiedDocument(const_cast<pdf::PDFDocument*>(document), nullptr));
2023-12-13 20:14:33 +01:00
ui->outlineView->expandToDepth(2);
ui->outlineView->setContextMenuPolicy(Qt::CustomContextMenu);
connect(ui->outlineView, &QTreeView::customContextMenuRequested, this, &SelectOutlineToRegroupDialog::onViewContextMenuRequested);
2021-09-27 11:14:20 +02:00
QSize size = pdf::PDFWidgetUtils::scaleDPI(this, QSize(400, 600));
setMinimumSize(size);
pdf::PDFWidgetUtils::style(this);
}
2023-12-13 20:14:33 +01:00
SelectOutlineToRegroupDialog::~SelectOutlineToRegroupDialog()
2021-09-27 11:14:20 +02:00
{
delete ui;
}
2023-12-13 20:14:33 +01:00
std::vector<const pdf::PDFOutlineItem*> SelectOutlineToRegroupDialog::getSelectedOutlineItems() const
2021-09-27 11:14:20 +02:00
{
return m_model->getSelectedItems();
}
2023-12-13 20:14:33 +01:00
void SelectOutlineToRegroupDialog::onViewContextMenuRequested(const QPoint& pos)
2021-09-27 11:14:20 +02:00
{
QMenu menu;
2023-12-13 20:14:33 +01:00
menu.addAction(tr("Select All"), this, &SelectOutlineToRegroupDialog::selectAll);
menu.addAction(tr("Deselect All"), this, &SelectOutlineToRegroupDialog::deselectAll);
menu.addAction(tr("Invert Selection"), this, &SelectOutlineToRegroupDialog::invertSelection);
2021-09-27 11:14:20 +02:00
menu.addSeparator();
2023-12-13 20:14:33 +01:00
menu.addAction(tr("Select Level 1"), this, &SelectOutlineToRegroupDialog::selectLevel1);
menu.addAction(tr("Select Level 2"), this, &SelectOutlineToRegroupDialog::selectLevel2);
2021-09-27 11:14:20 +02:00
2023-12-13 20:14:33 +01:00
QModelIndex index = ui->outlineView->indexAt(pos);
2021-09-27 11:14:20 +02:00
if (index.isValid())
{
m_menuIndex = index;
menu.addSeparator();
2023-12-13 20:14:33 +01:00
menu.addAction(tr("Select subtree"), this, &SelectOutlineToRegroupDialog::selectSubtree);
menu.addAction(tr("Deselect subtree"), this, &SelectOutlineToRegroupDialog::deselectSubtree);
2021-09-27 11:14:20 +02:00
}
2023-12-13 20:14:33 +01:00
menu.exec(ui->outlineView->mapToGlobal(pos));
2021-09-27 11:14:20 +02:00
}
2023-12-13 20:14:33 +01:00
void SelectOutlineToRegroupDialog::manipulateTree(const QModelIndex& index,
2021-09-27 11:14:20 +02:00
const std::function<void (QModelIndex)>& manipulator)
{
if (index.isValid())
{
manipulator(index);
}
const int count = m_model->rowCount(index);
for (int i = 0; i < count; ++i)
{
QModelIndex childIndex = m_model->index(i, 0, index);
manipulateTree(childIndex, manipulator);
}
}
2023-12-13 20:14:33 +01:00
std::function<void (QModelIndex)> SelectOutlineToRegroupDialog::createCheckByDepthManipulator(int targetDepth) const
2021-09-27 11:14:20 +02:00
{
auto manipulator = [this, targetDepth](QModelIndex index)
{
int depth = 1;
QModelIndex parentIndex = index.parent();
while (parentIndex.isValid())
{
++depth;
parentIndex = parentIndex.parent();
}
if (depth == targetDepth)
{
m_model->setData(index, Qt::Checked, Qt::CheckStateRole);
}
};
return manipulator;
}
2023-12-13 20:14:33 +01:00
void SelectOutlineToRegroupDialog::selectAll()
2021-09-27 11:14:20 +02:00
{
2023-12-13 20:14:33 +01:00
manipulateTree(ui->outlineView->rootIndex(), [this](QModelIndex index) { m_model->setData(index, Qt::Checked, Qt::CheckStateRole); });
2021-09-27 11:14:20 +02:00
}
2023-12-13 20:14:33 +01:00
void SelectOutlineToRegroupDialog::deselectAll()
2021-09-27 11:14:20 +02:00
{
2023-12-13 20:14:33 +01:00
manipulateTree(ui->outlineView->rootIndex(), [this](QModelIndex index) { m_model->setData(index, Qt::Unchecked, Qt::CheckStateRole); });
2021-09-27 11:14:20 +02:00
}
2023-12-13 20:14:33 +01:00
void SelectOutlineToRegroupDialog::invertSelection()
2021-09-27 11:14:20 +02:00
{
auto manipulator = [this](QModelIndex index)
{
const bool isChecked = index.data(Qt::CheckStateRole).toInt() == Qt::Checked;
m_model->setData(index, isChecked ? Qt::Unchecked : Qt::Checked, Qt::CheckStateRole);
};
2023-12-13 20:14:33 +01:00
manipulateTree(ui->outlineView->rootIndex(), manipulator);
2021-09-27 11:14:20 +02:00
}
2023-12-13 20:14:33 +01:00
void SelectOutlineToRegroupDialog::selectLevel1()
2021-09-27 11:14:20 +02:00
{
2023-12-13 20:14:33 +01:00
manipulateTree(ui->outlineView->rootIndex(), createCheckByDepthManipulator(1));
2021-09-27 11:14:20 +02:00
}
2023-12-13 20:14:33 +01:00
void SelectOutlineToRegroupDialog::selectLevel2()
2021-09-27 11:14:20 +02:00
{
2023-12-13 20:14:33 +01:00
manipulateTree(ui->outlineView->rootIndex(), createCheckByDepthManipulator(2));
2021-09-27 11:14:20 +02:00
}
2023-12-13 20:14:33 +01:00
void SelectOutlineToRegroupDialog::selectSubtree()
2021-09-27 11:14:20 +02:00
{
manipulateTree(m_menuIndex, [this](QModelIndex index) { m_model->setData(index, Qt::Checked, Qt::CheckStateRole); });
}
2023-12-13 20:14:33 +01:00
void SelectOutlineToRegroupDialog::deselectSubtree()
2021-09-27 11:14:20 +02:00
{
manipulateTree(m_menuIndex, [this](QModelIndex index) { m_model->setData(index, Qt::Unchecked, Qt::CheckStateRole); });
}
2024-03-16 17:02:44 +01:00
} // namespace pdfpagemaster