File indexing completed on 2024-06-23 05:33:17
0001 /*************************************************************************** 0002 * Copyright (C) 2016 by Renaud Guezennec * 0003 * http://www.rolisteam.org/contact * 0004 * * 0005 * rolisteam is free software; you can redistribute it and/or modify * 0006 * it under the terms of the GNU General Public License as published by * 0007 * the Free Software Foundation; either version 2 of the License, or * 0008 * (at your option) any later version. * 0009 * * 0010 * This program is distributed in the hope that it will be useful, * 0011 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 0012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 0013 * GNU General Public License for more details. * 0014 * * 0015 * You should have received a copy of the GNU General Public License * 0016 * along with this program; if not, write to the * 0017 * Free Software Foundation, Inc., * 0018 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * 0019 ***************************************************************************/ 0020 #include "pdfmanager.h" 0021 #include "ui_pdfmanager.h" 0022 0023 #include <QCheckBox> 0024 #include <QFileDialog> 0025 #include <QGroupBox> 0026 #include <QPdfDocument> 0027 #include <QPdfPageNavigator> 0028 #include <QPdfView> 0029 #include <QPushButton> 0030 #include <QScrollArea> 0031 #include <QScrollBar> 0032 #include <QSet> 0033 //#include <QtPdf/QPdfPageNavigation> 0034 0035 PdfManager::PdfManager(QWidget* parent) : QDialog(parent), ui(new Ui::PdfManager), m_document(new QPdfDocument()) 0036 { 0037 ui->setupUi(this); 0038 0039 connect(ui->m_browseFile, &QPushButton::clicked, this, 0040 [this]() 0041 { 0042 auto filename= QFileDialog::getOpenFileName(this, tr("Load background from PDF"), QDir::homePath(), 0043 tr("PDF files (*.pdf)")); 0044 if(filename.isEmpty()) 0045 return; 0046 0047 setPdfPath(filename); 0048 }); 0049 0050 connect(ui->m_resoGroupBox, &QGroupBox::toggled, this, 0051 [this](bool checked) 0052 { 0053 ui->m_widthBox->setEnabled(checked); 0054 ui->m_heightBox->setEnabled(checked); 0055 }); 0056 0057 ui->m_pdfView->setDocument(m_document.get()); 0058 0059 ui->m_resoGroupBox->setChecked(false); 0060 ui->m_allPagesCb->setChecked(true); 0061 0062 ui->m_pdfView->setPageMode(QPdfView::PageMode::MultiPage); 0063 ui->m_pdfView->setZoomMode(QPdfView::ZoomMode::FitToWidth); 0064 0065 connect(ui->m_allPagesCb, &QCheckBox::toggled, this, 0066 [this](bool checked) 0067 { 0068 if(checked) 0069 { 0070 ui->m_currentPageCb->setChecked(false); 0071 ui->m_pagesCb->setChecked(false); 0072 } 0073 }); 0074 connect(ui->m_currentPageCb, &QCheckBox::toggled, this, 0075 [this](bool checked) 0076 { 0077 if(checked) 0078 { 0079 ui->m_allPagesCb->setChecked(false); 0080 ui->m_pagesCb->setChecked(false); 0081 } 0082 }); 0083 connect(ui->m_pagesCb, &QCheckBox::toggled, this, 0084 [this](bool checked) 0085 { 0086 if(checked) 0087 { 0088 ui->m_allPagesCb->setChecked(false); 0089 ui->m_currentPageCb->setChecked(false); 0090 } 0091 }); 0092 0093 connect(ui->m_widthBox, &QSpinBox::valueChanged, this, 0094 [this](int value) 0095 { 0096 if(!ui->m_resoGroupBox->isChecked()) 0097 return; 0098 0099 auto sizeF= m_document->pagePointSize(currentPage()); 0100 auto r= sizeF.height() / sizeF.width(); 0101 const QSignalBlocker blocker(ui->m_heightBox); 0102 ui->m_heightBox->setValue(value * r); 0103 }); 0104 0105 connect(ui->m_heightBox, &QSpinBox::valueChanged, this, 0106 [this](int value) 0107 { 0108 if(!ui->m_resoGroupBox->isChecked()) 0109 return; 0110 0111 auto sizeF= m_document->pagePointSize(currentPage()); 0112 auto r= sizeF.width() / sizeF.height(); 0113 const QSignalBlocker blocker(ui->m_widthBox); 0114 ui->m_widthBox->setValue(value * r); 0115 }); 0116 0117 // connect(ui->m_pdfView->viewport(), &QWidget::size); 0118 0119 // connect(ui->spinBox, &QSpinBox::valueChanged, this, &PdfManager::resolutionChanged); 0120 // connect(ui->horizontalSlider,SIGNAL(valueChanged(int)),this,SIGNAL(resolutionChanged())); 0121 /*connect(ui->buttonBox->button(QDialogButtonBox::Apply), &QPushButton::clicked, [=] { emit apply(); }); 0122 connect(ui->buttonBox->button(QDialogButtonBox::Ok), &QPushButton::clicked, [=] { 0123 accept(); 0124 // emit accepted(); 0125 });*/ 0126 updateSizeParameter(); 0127 } 0128 0129 PdfManager::~PdfManager() 0130 { 0131 delete ui; 0132 } 0133 0134 int PdfManager::getWidth() 0135 { 0136 return ui->m_widthBox->value(); 0137 } 0138 0139 int PdfManager::getHeight() 0140 { 0141 return ui->m_heightBox->value(); 0142 } 0143 0144 void PdfManager::setHeight(int h) 0145 { 0146 ui->m_heightBox->setValue(h); 0147 } 0148 0149 void PdfManager::setWidth(int w) 0150 { 0151 ui->m_widthBox->setValue(w); 0152 } 0153 0154 const QString PdfManager::pdfPath() const 0155 { 0156 return ui->m_pathEdit->text(); 0157 } 0158 0159 void PdfManager::setPdfPath(const QString& newPdfPath) 0160 { 0161 if(pdfPath() == newPdfPath) 0162 return; 0163 0164 ui->m_pathEdit->setText(newPdfPath); 0165 m_document->load(newPdfPath); 0166 emit pdfPathChanged(); 0167 updateSizeParameter(); 0168 } 0169 0170 const QList<QImage> PdfManager::images() const 0171 { 0172 QList<QImage> m_images; 0173 0174 bool grayScale= ui->m_grayScale->isChecked(); 0175 0176 QSize areaSize= ui->m_pdfView->viewport()->size(); 0177 QSize widgetSize= ui->m_pdfView->size(); 0178 0179 auto v= ui->m_pdfView->verticalScrollBar()->pageStep(); 0180 auto h= ui->m_pdfView->horizontalScrollBar()->pageStep(); 0181 0182 // Compute Size 0183 QSize size= QSize(ui->m_widthBox->value(), ui->m_heightBox->value()); 0184 0185 // Compute pages 0186 QList<int> pages; 0187 0188 if(ui->m_allPagesCb->isChecked()) 0189 { 0190 for(auto i= 0; i < m_document->pageCount(); ++i) 0191 pages.append(i); 0192 } 0193 else if(ui->m_currentPageCb->isChecked()) 0194 { 0195 auto nav= ui->m_pdfView->pageNavigator(); 0196 if(nav) 0197 pages.append(nav->currentPage()); 0198 } 0199 else if(ui->m_pagesCb->isChecked()) 0200 { 0201 auto pagesList= ui->m_pageLine->text(); 0202 auto lst= pagesList.split(";"); 0203 QRegularExpression range; 0204 range.setPattern("(\\d+)-(\\d+)"); 0205 for(const auto& line : lst) 0206 { 0207 auto match= range.match(line); 0208 if(match.hasMatch()) 0209 { 0210 auto start= match.captured(1).toInt(); 0211 auto end= match.captured(2).toInt(); 0212 0213 if(end < start) 0214 continue; 0215 0216 for(auto i= start; i <= end; ++i) 0217 { 0218 pages.append(i); 0219 } 0220 } 0221 else 0222 { 0223 bool ok; 0224 auto v= line.toInt(&ok); 0225 if(ok) 0226 pages.append(v); 0227 } 0228 } 0229 } 0230 0231 // Compute images 0232 QPdfDocumentRenderOptions options; 0233 if(grayScale) 0234 options.setRenderFlags(QPdfDocumentRenderOptions::RenderFlag::Grayscale); 0235 0236 options.setScaledSize(size); 0237 for(auto page : pages) 0238 { 0239 auto img= m_document->render(page, size, options); 0240 0241 if(!img.isNull()) 0242 m_images.append(img); 0243 } 0244 0245 return m_images; 0246 } 0247 0248 void PdfManager::updateSizeParameter() 0249 { 0250 if(ui->m_resoGroupBox->isChecked()) 0251 return; 0252 0253 auto sizeF= m_document->pagePointSize(currentPage()) * (1. + 1. / 3.); 0254 ui->m_widthBox->setValue(sizeF.width()); 0255 ui->m_heightBox->setValue(sizeF.height()); 0256 } 0257 0258 int PdfManager::currentPage() const 0259 { 0260 auto nav= ui->m_pdfView->pageNavigator(); 0261 if(!nav) 0262 return 0; 0263 return nav->currentPage(); 0264 }