PDF4QT/Pdf4QtLib/sources/pdfselectpagesdialog.cpp

136 lines
3.8 KiB
C++
Raw Normal View History

2021-04-30 20:12:10 +02:00
// Copyright (C) 2020-2021 Jakub Melka
2020-12-27 17:44:53 +01:00
//
// 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
2021-04-30 20:12:10 +02:00
// with the written consent of the copyright owner, any later version.
2020-12-27 17:44:53 +01:00
//
// 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/>.
2020-12-30 17:06:13 +01:00
#include "pdfselectpagesdialog.h"
#include "ui_pdfselectpagesdialog.h"
2020-12-27 17:44:53 +01:00
#include "pdfwidgetutils.h"
#include <QMessageBox>
2020-12-30 17:06:13 +01:00
namespace pdf
2020-12-27 17:44:53 +01:00
{
2020-12-30 17:06:13 +01:00
PDFSelectPagesDialog::PDFSelectPagesDialog(QString windowTitle,
QString groupBoxTitle,
pdf::PDFInteger pageCount,
const std::vector<pdf::PDFInteger>& visiblePages,
QWidget* parent) :
2020-12-27 17:44:53 +01:00
QDialog(parent),
2020-12-30 17:06:13 +01:00
ui(new Ui::PDFSelectPagesDialog),
2020-12-27 17:44:53 +01:00
m_pageCount(pageCount),
m_visiblePages(visiblePages)
{
ui->setupUi(this);
2020-12-30 17:06:13 +01:00
setWindowTitle(windowTitle);
ui->selectPagesGroupBox->setTitle(groupBoxTitle);
2020-12-27 17:44:53 +01:00
for (pdf::PDFInteger& pageIndex : m_visiblePages)
{
++pageIndex;
}
m_evenPages.reserve(pageCount / 2);
m_oddPages.reserve(pageCount / 2 + 1);
for (pdf::PDFInteger i = 1; i <= pageCount; ++i)
{
if (i % 2 == 1)
{
m_oddPages.push_back(i);
}
else
{
m_evenPages.push_back(i);
}
}
2020-12-30 17:06:13 +01:00
connect(ui->customPageRangeRadioButton, &QRadioButton::toggled, this, &PDFSelectPagesDialog::updateUi);
2020-12-27 17:44:53 +01:00
setMinimumWidth(pdf::PDFWidgetUtils::scaleDPI_x(this, 400));
updateUi();
}
2020-12-30 17:06:13 +01:00
PDFSelectPagesDialog::~PDFSelectPagesDialog()
2020-12-27 17:44:53 +01:00
{
delete ui;
}
2020-12-30 17:06:13 +01:00
std::vector<pdf::PDFInteger> PDFSelectPagesDialog::getSelectedPages() const
2020-12-27 17:44:53 +01:00
{
std::vector<pdf::PDFInteger> result;
if (ui->visiblePagesRadioButton->isChecked())
{
result = m_visiblePages;
}
else if (ui->allPagesRadioButton->isChecked())
{
result.resize(m_pageCount, 0);
std::iota(result.begin(), result.end(), 1);
}
else if (ui->evenPagesRadioButton->isChecked())
{
result = m_evenPages;
}
else if (ui->oddPagesRadioButton->isChecked())
{
result = m_oddPages;
}
else if (ui->customPageRangeRadioButton->isChecked())
{
QString errorMessage;
result = pdf::PDFClosedIntervalSet::parse(1, m_pageCount, ui->customPageRangeEdit->text(), &errorMessage).unfold();
}
else
{
Q_ASSERT(false);
}
return result;
}
2020-12-30 17:06:13 +01:00
void PDFSelectPagesDialog::updateUi()
2020-12-27 17:44:53 +01:00
{
ui->customPageRangeEdit->setEnabled(ui->customPageRangeRadioButton->isChecked());
}
2020-12-30 17:06:13 +01:00
void PDFSelectPagesDialog::accept()
2020-12-27 17:44:53 +01:00
{
if (ui->customPageRangeRadioButton->isChecked())
{
QString errorMessage;
pdf::PDFClosedIntervalSet::parse(1, m_pageCount, ui->customPageRangeEdit->text(), &errorMessage);
if (!errorMessage.isEmpty())
{
QMessageBox::critical(this, tr("Error"), errorMessage);
return;
}
}
if (getSelectedPages().empty())
{
QMessageBox::critical(this, tr("Error"), tr("Selected page range is empty."));
return;
}
QDialog::accept();
}
2020-12-30 17:06:13 +01:00
} // namespace pdf