PDF4QT/Pdf4QtLib/sources/pdfselectpagesdialog.cpp

137 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
//
2021-08-10 19:22:56 +02:00
// This file is part of PDF4QT.
2020-12-27 17:44:53 +01:00
//
2021-08-10 19:22:56 +02:00
// PDF4QT is free software: you can redistribute it and/or modify
2020-12-27 17:44:53 +01:00
// 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
//
2021-08-10 19:22:56 +02:00
// PDF4QT is distributed in the hope that it will be useful,
2020-12-27 17:44:53 +01:00
// 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
2021-08-10 19:22:56 +02:00
// along with PDF4QT. If not, see <https://www.gnu.org/licenses/>.
2020-12-27 17:44:53 +01:00
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();
2021-08-12 19:51:30 +02:00
pdf::PDFWidgetUtils::style(this);
2020-12-27 17:44:53 +01:00
}
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