File indexing completed on 2024-05-26 04:33:34

0001 /*
0002  *  SPDX-FileCopyrightText: 2006 Cyrille Berger <cberger@cberger.net>
0003  *
0004  *  SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 
0007 #include "kis_pdf_import_widget.h"
0008 
0009 #define UNSTABLE_POPPLER_QT4
0010 // poppler's headers
0011 #include <poppler-qt5.h>
0012 
0013 // Qt's headers
0014 #include <QRadioButton>
0015 
0016 // KDE's headers
0017 #include <kis_debug.h>
0018 
0019 // For ceil()
0020 #include <math.h>
0021 
0022 // For KoAspectButton
0023 #include <klocalizedstring.h>
0024 
0025 
0026 
0027 KisPDFImportWidget::KisPDFImportWidget(Poppler::Document* pdfDoc, QWidget * parent)
0028         : QWidget(parent)
0029         , m_pdfDoc(pdfDoc)
0030         , m_keepAspect(true)
0031 {
0032     setupUi(this);
0033     m_pages.push_back(0); // The first page is selected
0034     updateMaxCanvasSize();
0035 
0036     for (int i = 1; i <= m_pdfDoc->numPages(); i++) {
0037         listPages->addItem(QString::number(i));
0038     }
0039 
0040     connect(intWidth, SIGNAL(valueChanged(int)), this, SLOT(updateHRes()));
0041     connect(intHeight, SIGNAL(valueChanged(int)), this, SLOT(updateHVer()));
0042     connect(intResolution, SIGNAL(valueChanged(int)), this, SLOT(updateResolution()));
0043     connect(intHeight, SIGNAL(valueChanged(int)), this, SLOT(heightAspectRatio()));
0044     connect(intWidth, SIGNAL(valueChanged(int)), this, SLOT(widthAspectRatio()));
0045     connect(boolAllPages, SIGNAL(toggled(bool)), this, SLOT(selectAllPages(bool)));
0046     connect(boolFirstPage, SIGNAL(toggled(bool)), this, SLOT(selectFirstPage(bool)));
0047     connect(boolSelectionPage, SIGNAL(toggled(bool)), this, SLOT(selectSelectionOfPages(bool)));
0048     connect(listPages, SIGNAL(itemSelectionChanged()), this, SLOT(updateSelectionOfPages()));
0049     connect(pixelAspectRatioBtn, SIGNAL(keepAspectRatioChanged(bool)), this, SLOT(slotAspectChanged(bool)));
0050 
0051 }
0052 
0053 
0054 KisPDFImportWidget::~KisPDFImportWidget()
0055 {
0056 }
0057 
0058 void KisPDFImportWidget::selectAllPages(bool v)
0059 {
0060     if (v) {
0061         if (listPages->selectedItems().count() != 0){
0062             listPages->clearSelection();
0063             boolAllPages->toggle();
0064         }
0065         m_pages.clear();
0066         for (int i = 0; i < m_pdfDoc->numPages(); i++) {
0067             m_pages.push_back(i);
0068         }
0069         updateMaxCanvasSize();
0070     }
0071 }
0072 void KisPDFImportWidget::selectFirstPage(bool v)
0073 {
0074     if (v) {
0075         if (listPages->selectedItems().count() != 0){
0076             listPages->clearSelection();
0077             boolFirstPage->toggle();
0078         }
0079         m_pages.clear();
0080         m_pages.push_back(0); // The first page is selected
0081         updateMaxCanvasSize();
0082     }
0083 }
0084 void KisPDFImportWidget::selectSelectionOfPages(bool v)
0085 {
0086     if (v) {
0087         updateSelectionOfPages();
0088         updateMaxCanvasSize();
0089     }
0090 
0091 }
0092 
0093 void KisPDFImportWidget::updateSelectionOfPages()
0094 {
0095     if (! boolSelectionPage->isChecked()) boolSelectionPage->toggle();
0096     m_pages.clear();
0097     for (int i = 0; i < m_pdfDoc->numPages(); i++) {
0098         if (listPages->item(i)->isSelected()) m_pages.push_back(i);
0099     }
0100     updateMaxCanvasSize();
0101 }
0102 
0103 
0104 void KisPDFImportWidget::updateMaxCanvasSize()
0105 {
0106     m_maxWidthInch = 0., m_maxHeightInch = 0.;
0107     for (QList<int>::const_iterator it = m_pages.constBegin(); it != m_pages.constEnd(); ++it) {
0108         Poppler::Page *p = m_pdfDoc->page(*it);
0109         QSizeF size = p->pageSizeF();
0110         if (size.width() > m_maxWidthInch) {
0111             m_maxWidthInch = size.width();
0112         }
0113         if (size.height() > m_maxHeightInch) {
0114             m_maxHeightInch = size.height();
0115         }
0116     }
0117     m_maxWidthInch /= 72.;
0118     m_maxHeightInch /= 72.;
0119     dbgFile << m_maxWidthInch << "" << m_maxHeightInch;
0120     updateResolution();
0121 }
0122 
0123 void KisPDFImportWidget::updateResolution()
0124 {
0125     int resolution = intResolution->value();
0126     intWidth->blockSignals(true);
0127     intWidth->setValue((int) ceil(m_maxWidthInch * resolution));
0128     intWidth->blockSignals(false);
0129     intHeight->blockSignals(true);
0130     intHeight->setValue((int) ceil(m_maxHeightInch * resolution));
0131     intHeight->blockSignals(false);
0132 }
0133 
0134 void KisPDFImportWidget::updateHRes()
0135 {
0136     intResolution->blockSignals(true);
0137     intResolution->setValue((int)(intWidth->value() / m_maxWidthInch));
0138     intResolution->blockSignals(false);
0139 }
0140 void KisPDFImportWidget::updateHVer()
0141 {
0142     intResolution->blockSignals(true);
0143     intResolution->setValue((int)(intHeight->value() / m_maxHeightInch));
0144     intResolution->blockSignals(false);
0145 }
0146 void KisPDFImportWidget::heightAspectRatio()
0147 {
0148     intWidth->blockSignals(true);
0149     if(m_keepAspect)
0150     {
0151         intWidth->setValue((int) ceil(((intHeight->value() * m_maxWidthInch * 1.) / m_maxHeightInch * 1.) + 0.5));
0152     }
0153     intWidth->blockSignals(false);
0154 }
0155 void KisPDFImportWidget::widthAspectRatio()
0156 {
0157     intHeight->blockSignals(true);
0158     if(m_keepAspect)
0159     {
0160         intHeight->setValue((int) ceil(((intWidth->value() * m_maxHeightInch * 1.) / m_maxWidthInch * 1.) + 0.5));
0161     }
0162     intHeight->blockSignals(false);
0163 }
0164 void KisPDFImportWidget::slotAspectChanged(bool keep)
0165 {
0166     pixelAspectRatioBtn->blockSignals(true);
0167     pixelAspectRatioBtn->setKeepAspectRatio(keep);
0168     pixelAspectRatioBtn->blockSignals(false);
0169 
0170     m_keepAspect = keep;
0171 
0172     if (keep)
0173     {
0174         heightAspectRatio();
0175     }
0176 }
0177