| 1 |
/* |
| 2 |
* Copyright (C) 2008-2009, Pino Toscano <[email protected]> |
| 3 |
* Copyright (C) 2008, 2019, 2022, 2025, Albert Astals Cid <[email protected]> |
| 4 |
* Copyright (C) 2009, Shawn Rutledge <[email protected]> |
| 5 |
* Copyright (C) 2013, Fabio D'Urso <[email protected]> |
| 6 |
* Copyright (C) 2020, Oliver Sander <[email protected]> |
| 7 |
* Copyright (C) 2021, Mahmoud Khalil <[email protected]> |
| 8 |
* |
| 9 |
* This program is free software; you can redistribute it and/or modify |
| 10 |
* it under the terms of the GNU General Public License as published by |
| 11 |
* the Free Software Foundation; either version 2, or (at your option) |
| 12 |
* any later version. |
| 13 |
* |
| 14 |
* This program is distributed in the hope that it will be useful, |
| 15 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 |
* GNU General Public License for more details. |
| 18 |
* |
| 19 |
* You should have received a copy of the GNU General Public License |
| 20 |
* along with this program; if not, write to the Free Software |
| 21 |
* Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. |
| 22 |
*/ |
| 23 |
|
| 24 |
#include "viewer.h" |
| 25 |
|
| 26 |
#include "embeddedfiles.h" |
| 27 |
#include "fonts.h" |
| 28 |
#include "info.h" |
| 29 |
#include "metadata.h" |
| 30 |
#include "navigationtoolbar.h" |
| 31 |
#include "optcontent.h" |
| 32 |
#include "pageview.h" |
| 33 |
#include "permissions.h" |
| 34 |
#include "thumbnails.h" |
| 35 |
#include "toc.h" |
| 36 |
|
| 37 |
#include <poppler-qt5.h> |
| 38 |
|
| 39 |
#include <QtCore/QDir> |
| 40 |
#include <QtWidgets/QAction> |
| 41 |
#include <QtWidgets/QApplication> |
| 42 |
#include <QtWidgets/QFileDialog> |
| 43 |
#include <QtWidgets/QInputDialog> |
| 44 |
#include <QtWidgets/QMenu> |
| 45 |
#include <QtWidgets/QMenuBar> |
| 46 |
#include <QtWidgets/QMessageBox> |
| 47 |
|
| 48 |
#include <functional> |
| 49 |
|
| 50 |
PdfViewer::PdfViewer(QWidget *parent) : QMainWindow(parent), m_currentPage(0), m_doc(nullptr) |
| 51 |
{ |
| 52 |
setWindowTitle(tr("Poppler-Qt5 Demo")); |
| 53 |
|
| 54 |
// setup the menus |
| 55 |
QMenu *fileMenu = menuBar()->addMenu(tr("&File")); |
| 56 |
m_fileOpenAct = fileMenu->addAction(tr("&Open"), this, &PdfViewer::slotOpenFile); |
| 57 |
m_fileOpenAct->setShortcut(static_cast<int>(Qt::CTRL) + Qt::Key_O); |
| 58 |
fileMenu->addSeparator(); |
| 59 |
m_fileSaveCopyAct = fileMenu->addAction(tr("&Save a Copy..."), this, &PdfViewer::slotSaveCopy); |
| 60 |
m_fileSaveCopyAct->setShortcut(Qt::CTRL + Qt::SHIFT + Qt::Key_S); |
| 61 |
m_fileSaveCopyAct->setEnabled(false); |
| 62 |
fileMenu->addSeparator(); |
| 63 |
QAction *act = fileMenu->addAction(tr("&Quit"), qApp, &QApplication::closeAllWindows); |
| 64 |
act->setShortcut(static_cast<int>(Qt::CTRL) + Qt::Key_Q); |
| 65 |
|
| 66 |
QMenu *viewMenu = menuBar()->addMenu(tr("&View")); |
| 67 |
|
| 68 |
QMenu *settingsMenu = menuBar()->addMenu(tr("&Settings")); |
| 69 |
m_settingsTextAAAct = settingsMenu->addAction(tr("Text Antialias")); |
| 70 |
m_settingsTextAAAct->setCheckable(true); |
| 71 |
connect(m_settingsTextAAAct, &QAction::toggled, this, &PdfViewer::slotToggleTextAA); |
| 72 |
m_settingsGfxAAAct = settingsMenu->addAction(tr("Graphics Antialias")); |
| 73 |
m_settingsGfxAAAct->setCheckable(true); |
| 74 |
connect(m_settingsGfxAAAct, &QAction::toggled, this, &PdfViewer::slotToggleGfxAA); |
| 75 |
QMenu *settingsRenderMenu = settingsMenu->addMenu(tr("Render Backend")); |
| 76 |
m_settingsRenderBackendGrp = new QActionGroup(settingsRenderMenu); |
| 77 |
m_settingsRenderBackendGrp->setExclusive(true); |
| 78 |
act = settingsRenderMenu->addAction(tr("Splash")); |
| 79 |
act->setCheckable(true); |
| 80 |
act->setChecked(true); |
| 81 |
act->setData(QVariant::fromValue(0)); |
| 82 |
m_settingsRenderBackendGrp->addAction(act); |
| 83 |
act = settingsRenderMenu->addAction(tr("QPainter")); |
| 84 |
act->setCheckable(true); |
| 85 |
act->setData(QVariant::fromValue(1)); |
| 86 |
m_settingsRenderBackendGrp->addAction(act); |
| 87 |
connect(m_settingsRenderBackendGrp, &QActionGroup::triggered, this, &PdfViewer::slotRenderBackend); |
| 88 |
|
| 89 |
QMenu *helpMenu = menuBar()->addMenu(tr("&Help")); |
| 90 |
act = helpMenu->addAction(tr("&About"), this, &PdfViewer::slotAbout); |
| 91 |
act = helpMenu->addAction(tr("About &Qt"), this, &PdfViewer::slotAboutQt); |
| 92 |
|
| 93 |
NavigationToolBar *navbar = new NavigationToolBar(this); |
| 94 |
addToolBar(navbar); |
| 95 |
m_observers.append(navbar); |
| 96 |
|
| 97 |
PageView *view = new PageView(this); |
| 98 |
setCentralWidget(view); |
| 99 |
m_observers.append(view); |
| 100 |
|
| 101 |
InfoDock *infoDock = new InfoDock(this); |
| 102 |
addDockWidget(Qt::LeftDockWidgetArea, infoDock); |
| 103 |
infoDock->hide(); |
| 104 |
viewMenu->addAction(infoDock->toggleViewAction()); |
| 105 |
m_observers.append(infoDock); |
| 106 |
|
| 107 |
TocDock *tocDock = new TocDock(this); |
| 108 |
addDockWidget(Qt::LeftDockWidgetArea, tocDock); |
| 109 |
tocDock->hide(); |
| 110 |
viewMenu->addAction(tocDock->toggleViewAction()); |
| 111 |
m_observers.append(tocDock); |
| 112 |
|
| 113 |
FontsDock *fontsDock = new FontsDock(this); |
| 114 |
addDockWidget(Qt::LeftDockWidgetArea, fontsDock); |
| 115 |
fontsDock->hide(); |
| 116 |
viewMenu->addAction(fontsDock->toggleViewAction()); |
| 117 |
m_observers.append(fontsDock); |
| 118 |
|
| 119 |
PermissionsDock *permissionsDock = new PermissionsDock(this); |
| 120 |
addDockWidget(Qt::LeftDockWidgetArea, permissionsDock); |
| 121 |
permissionsDock->hide(); |
| 122 |
viewMenu->addAction(permissionsDock->toggleViewAction()); |
| 123 |
m_observers.append(permissionsDock); |
| 124 |
|
| 125 |
ThumbnailsDock *thumbnailsDock = new ThumbnailsDock(this); |
| 126 |
addDockWidget(Qt::LeftDockWidgetArea, thumbnailsDock); |
| 127 |
thumbnailsDock->hide(); |
| 128 |
viewMenu->addAction(thumbnailsDock->toggleViewAction()); |
| 129 |
m_observers.append(thumbnailsDock); |
| 130 |
|
| 131 |
EmbeddedFilesDock *embfilesDock = new EmbeddedFilesDock(this); |
| 132 |
addDockWidget(Qt::BottomDockWidgetArea, embfilesDock); |
| 133 |
embfilesDock->hide(); |
| 134 |
viewMenu->addAction(embfilesDock->toggleViewAction()); |
| 135 |
m_observers.append(embfilesDock); |
| 136 |
|
| 137 |
MetadataDock *metadataDock = new MetadataDock(this); |
| 138 |
addDockWidget(Qt::BottomDockWidgetArea, metadataDock); |
| 139 |
metadataDock->hide(); |
| 140 |
viewMenu->addAction(metadataDock->toggleViewAction()); |
| 141 |
m_observers.append(metadataDock); |
| 142 |
|
| 143 |
OptContentDock *optContentDock = new OptContentDock(this); |
| 144 |
addDockWidget(Qt::LeftDockWidgetArea, optContentDock); |
| 145 |
optContentDock->hide(); |
| 146 |
viewMenu->addAction(optContentDock->toggleViewAction()); |
| 147 |
m_observers.append(optContentDock); |
| 148 |
|
| 149 |
Q_FOREACH (DocumentObserver *obs, m_observers) { |
| 150 |
obs->m_viewer = this; |
| 151 |
} |
| 152 |
|
| 153 |
connect(navbar, &NavigationToolBar::zoomChanged, view, &PageView::slotZoomChanged); |
| 154 |
connect(navbar, &NavigationToolBar::rotationChanged, view, &PageView::slotRotationChanged); |
| 155 |
|
| 156 |
// activate AA by default |
| 157 |
m_settingsTextAAAct->setChecked(true); |
| 158 |
m_settingsGfxAAAct->setChecked(true); |
| 159 |
} |
| 160 |
|
| 161 |
PdfViewer::~PdfViewer() |
| 162 |
{ |
| 163 |
closeDocument(); |
| 164 |
} |
| 165 |
|
| 166 |
QSize PdfViewer::sizeHint() const |
| 167 |
{ |
| 168 |
return QSize(500, 600); |
| 169 |
} |
| 170 |
|
| 171 |
void PdfViewer::loadDocument(const QString &file) |
| 172 |
{ |
| 173 |
// resetting xrefReconstructed each time we load new document |
| 174 |
xrefReconstructed = false; |
| 175 |
Poppler::Document *newdoc = Poppler::Document::load(file); |
| 176 |
if (!newdoc) { |
| 177 |
QMessageBox msgbox(QMessageBox::Critical, tr("Open Error"), tr("Cannot open:\n") + file, QMessageBox::Ok, this); |
| 178 |
msgbox.exec(); |
| 179 |
return; |
| 180 |
} |
| 181 |
|
| 182 |
while (newdoc->isLocked()) { |
| 183 |
bool ok = true; |
| 184 |
QString password = QInputDialog::getText(this, tr("Document Password"), tr("Please insert the password of the document:"), QLineEdit::Password, QString(), &ok); |
| 185 |
if (!ok) { |
| 186 |
delete newdoc; |
| 187 |
return; |
| 188 |
} |
| 189 |
newdoc->unlock(password.toLatin1(), password.toLatin1()); |
| 190 |
} |
| 191 |
|
| 192 |
closeDocument(); |
| 193 |
|
| 194 |
m_doc = newdoc; |
| 195 |
|
| 196 |
m_doc->setRenderHint(Poppler::Document::TextAntialiasing, m_settingsTextAAAct->isChecked()); |
| 197 |
m_doc->setRenderHint(Poppler::Document::Antialiasing, m_settingsGfxAAAct->isChecked()); |
| 198 |
m_doc->setRenderBackend((Poppler::Document::RenderBackend)m_settingsRenderBackendGrp->checkedAction()->data().toInt()); |
| 199 |
if (m_doc->xrefWasReconstructed()) { |
| 200 |
xrefReconstructedHandler(m_doc); |
| 201 |
} else { |
| 202 |
std::function<void()> cb = [this]() { xrefReconstructedHandler(m_doc); }; |
| 203 |
|
| 204 |
m_doc->setXRefReconstructedCallback(cb); |
| 205 |
} |
| 206 |
|
| 207 |
Q_FOREACH (DocumentObserver *obs, m_observers) { |
| 208 |
obs->documentLoaded(); |
| 209 |
obs->pageChanged(0); |
| 210 |
} |
| 211 |
|
| 212 |
m_fileSaveCopyAct->setEnabled(true); |
| 213 |
} |
| 214 |
|
| 215 |
void PdfViewer::closeDocument() |
| 216 |
{ |
| 217 |
if (!m_doc) { |
| 218 |
return; |
| 219 |
} |
| 220 |
|
| 221 |
Q_FOREACH (DocumentObserver *obs, m_observers) { |
| 222 |
obs->documentClosed(); |
| 223 |
} |
| 224 |
|
| 225 |
m_currentPage = 0; |
| 226 |
delete m_doc; |
| 227 |
m_doc = nullptr; |
| 228 |
|
| 229 |
m_fileSaveCopyAct->setEnabled(false); |
| 230 |
} |
| 231 |
|
| 232 |
void PdfViewer::xrefReconstructedHandler(Poppler::Document * /*doc*/) |
| 233 |
{ |
| 234 |
if (!xrefReconstructed) { |
| 235 |
QMessageBox msgbox(QMessageBox::Critical, tr("File may be corrupted"), tr("The PDF may be broken but we're still showing something, contents may not be correct"), QMessageBox::Ok, this); |
| 236 |
msgbox.exec(); |
| 237 |
|
| 238 |
xrefReconstructed = true; |
| 239 |
} |
| 240 |
} |
| 241 |
|
| 242 |
void PdfViewer::slotOpenFile() |
| 243 |
{ |
| 244 |
QString fileName = QFileDialog::getOpenFileName(this, tr("Open PDF Document"), QDir::homePath(), tr("PDF Documents (*.pdf)")); |
| 245 |
if (fileName.isEmpty()) { |
| 246 |
return; |
| 247 |
} |
| 248 |
|
| 249 |
loadDocument(fileName); |
| 250 |
} |
| 251 |
|
| 252 |
void PdfViewer::slotSaveCopy() |
| 253 |
{ |
| 254 |
if (!m_doc) { |
| 255 |
return; |
| 256 |
} |
| 257 |
|
| 258 |
QString fileName = QFileDialog::getSaveFileName(this, tr("Save Copy"), QDir::homePath(), tr("PDF Documents (*.pdf)")); |
| 259 |
if (fileName.isEmpty()) { |
| 260 |
return; |
| 261 |
} |
| 262 |
|
| 263 |
Poppler::PDFConverter *converter = m_doc->pdfConverter(); |
| 264 |
converter->setOutputFileName(fileName); |
| 265 |
converter->setPDFOptions(converter->pdfOptions() & ~Poppler::PDFConverter::WithChanges); |
| 266 |
if (!converter->convert()) { |
| 267 |
QMessageBox msgbox(QMessageBox::Critical, tr("Save Error"), tr("Cannot export to:\n%1").arg(fileName), QMessageBox::Ok, this); |
| 268 |
} |
| 269 |
delete converter; |
| 270 |
} |
| 271 |
|
| 272 |
void PdfViewer::slotAbout() |
| 273 |
{ |
| 274 |
QMessageBox::about(this, tr("About Poppler-Qt5 Demo"), tr("This is a demo of the Poppler-Qt5 library.")); |
| 275 |
} |
| 276 |
|
| 277 |
void PdfViewer::slotAboutQt() |
| 278 |
{ |
| 279 |
QMessageBox::aboutQt(this); |
| 280 |
} |
| 281 |
|
| 282 |
void PdfViewer::slotToggleTextAA(bool value) |
| 283 |
{ |
| 284 |
if (!m_doc) { |
| 285 |
return; |
| 286 |
} |
| 287 |
|
| 288 |
m_doc->setRenderHint(Poppler::Document::TextAntialiasing, value); |
| 289 |
|
| 290 |
Q_FOREACH (DocumentObserver *obs, m_observers) { |
| 291 |
obs->pageChanged(m_currentPage); |
| 292 |
} |
| 293 |
} |
| 294 |
|
| 295 |
void PdfViewer::slotToggleGfxAA(bool value) |
| 296 |
{ |
| 297 |
if (!m_doc) { |
| 298 |
return; |
| 299 |
} |
| 300 |
|
| 301 |
m_doc->setRenderHint(Poppler::Document::Antialiasing, value); |
| 302 |
|
| 303 |
Q_FOREACH (DocumentObserver *obs, m_observers) { |
| 304 |
obs->pageChanged(m_currentPage); |
| 305 |
} |
| 306 |
} |
| 307 |
|
| 308 |
void PdfViewer::slotRenderBackend(QAction *act) |
| 309 |
{ |
| 310 |
if (!m_doc || !act) { |
| 311 |
return; |
| 312 |
} |
| 313 |
|
| 314 |
m_doc->setRenderBackend((Poppler::Document::RenderBackend)act->data().toInt()); |
| 315 |
|
| 316 |
Q_FOREACH (DocumentObserver *obs, m_observers) { |
| 317 |
obs->pageChanged(m_currentPage); |
| 318 |
} |
| 319 |
} |
| 320 |
|
| 321 |
void PdfViewer::setPage(int page) |
| 322 |
{ |
| 323 |
Q_FOREACH (DocumentObserver *obs, m_observers) { |
| 324 |
obs->pageChanged(page); |
| 325 |
} |
| 326 |
|
| 327 |
m_currentPage = page; |
| 328 |
} |
| 329 |
|
| 330 |
int PdfViewer::page() const |
| 331 |
{ |
| 332 |
return m_currentPage; |
| 333 |
} |