1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
|
//===========================================
// Lumina Desktop source code
// Copyright (c) 2017, Ken Moore
// Available under the 3-clause BSD license
// See the LICENSE file for full details
//===========================================
#include "mainUI.h"
#include "ui_mainUI.h"
#include <QPainter>
#include <QImage>
#include <QSize>
#include <QFileDialog>
#include <QDebug>
#include <QApplication>
#include <LuminaXDG.h>
MainUI::MainUI() : QMainWindow(), ui(new Ui::MainUI()){
ui->setupUi(this);
this->setWindowTitle(tr("Lumina PDF Viewer"));
this->setWindowIcon( LXDG::findIcon("application-pdf","unknown"));
lastdir = QDir::homePath();
PRINTER = new QPrinter();
WIDGET = new QPrintPreviewWidget(PRINTER, this);
this->setCentralWidget(WIDGET);
connect(WIDGET, SIGNAL(paintRequested(QPrinter*)), this, SLOT(paintOnWidget(QPrinter*)) );
DOC = 0;
PrintDLG = new QPrintDialog(PRINTER,this);
connect(PrintDLG, SIGNAL(accepted(QPrinter*)), this, SLOT(paintOnWidget(QPrinter*)) ); //Can change to PaintToPrinter() later
//Create the other interface widgets
progress = new QProgressBar(this);
progress->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
progress->setFormat("%v/%m (%p%)"); // [current]/[total]
ui->toolBar2->addWidget(progress);
ui->toolBar2->setVisible(false);
//Put the various actions into logical groups
QActionGroup *tmp = new QActionGroup(this);
tmp->setExclusive(true);
tmp->addAction(ui->actionFit_Width);
tmp->addAction(ui->actionFit_Page);
ui->actionFit_Page->setChecked(true);
tmp = new QActionGroup(this);
tmp->setExclusive(true);
tmp->addAction(ui->actionSingle_Page);
tmp->addAction(ui->actionDual_Pages);
tmp->addAction(ui->actionAll_Pages);
ui->actionSingle_Page->setChecked(true);
//Connect up the buttons
connect(ui->actionClose, SIGNAL(triggered()), this, SLOT(close()) );
connect(ui->actionPrint, SIGNAL(triggered()), PrintDLG, SLOT(open()) );
connect(ui->actionFit_Width, SIGNAL(triggered()), WIDGET, SLOT(fitToWidth()) );
connect(ui->actionFit_Page, SIGNAL(triggered()), WIDGET, SLOT(fitInView()) );
connect(ui->actionOpen_PDF, SIGNAL(triggered()), this, SLOT(OpenNewFile()) );
connect(ui->actionSingle_Page, SIGNAL(triggered()), WIDGET, SLOT(setSinglePageViewMode()) );
connect(ui->actionDual_Pages, SIGNAL(triggered()), WIDGET, SLOT(setFacingPagesViewMode()) );
connect(ui->actionAll_Pages, SIGNAL(triggered()), WIDGET, SLOT(setAllPagesViewMode()) );
//Setup all the icons
ui->actionPrint->setIcon( LXDG::findIcon("document-print",""));
ui->actionClose->setIcon( LXDG::findIcon("window-close",""));
ui->actionFit_Width->setIcon(LXDG::findIcon("zoom-fit-width",""));
ui->actionFit_Page->setIcon(LXDG::findIcon("zoom-fit-best",""));
ui->actionOpen_PDF->setIcon(LXDG::findIcon("document-open",""));
ui->actionSingle_Page->setIcon(LXDG::findIcon("view-preview",""));
ui->actionDual_Pages->setIcon(LXDG::findIcon("view-split-left-right",""));
ui->actionAll_Pages->setIcon(LXDG::findIcon("view-grid",""));
}
MainUI::~MainUI(){
}
void MainUI::loadFile(QString path){
if(DOC!=0){
//Clear out the old document first
delete DOC;
DOC=0;
}
qDebug() << "Opening File:" << path;
if(!QFile::exists(path) || path.isEmpty() ){ return; }
DOC = Poppler::Document::load(path);
if(DOC!=0){ this->setWindowTitle(DOC->title()); }
if(this->windowTitle().isEmpty()){ this->setWindowTitle(path.section("/",-1)); }
//Setup the Document
Poppler::Page *PAGE = DOC->page(0);
if(PAGE!=0){
lastdir = path.section("/",0,-2); //save this for later
PRINTER->setPageSize( QPageSize(PAGE->pageSize(), QPageSize::Point) );
switch(PAGE->orientation()){
case Poppler::Page::Landscape:
PRINTER->setOrientation(QPrinter::Landscape); break;
default:
PRINTER->setOrientation(QPrinter::Portrait);
}
delete PAGE;
WIDGET->updatePreview(); //start loading the file preview
}
}
void MainUI::paintOnWidget(QPrinter *PRINTER){
if(DOC==0){ return; }
this->show();
QApplication::processEvents();
int pages = DOC->numPages();
int firstpage = 0;
//qDebug() << "Start Rendering PDF:" << PRINTER->fromPage() << PRINTER->toPage();
if(PRINTER->fromPage() != PRINTER->toPage() || PRINTER->fromPage()!=0){
firstpage = PRINTER->fromPage() - 1;
pages = PRINTER->toPage();
}
qDebug() << " - Generating Pages:" << firstpage << pages;
//Now start painting all the pages onto the widget
QRectF size = PRINTER->pageRect(QPrinter::DevicePixel);
QSize DPI(PRINTER->resolution(),PRINTER->resolution());
QPainter painter(PRINTER);
Poppler::Page *PAGE = 0;
progress->setRange(firstpage, pages-1);
ui->toolBar2->setVisible(true);
for(int i=firstpage; i<pages; i++){
progress->setValue(i);
//qDebug() << "Loading Page:" << i;
QApplication::processEvents();
//Now paint this page on the printer
if(PAGE!=0){ delete PAGE; PRINTER->newPage(); } //this is the start of the next page (not needed for first)
PAGE = DOC->page(i);
if(PAGE!=0){
painter.drawImage(0,0,PAGE->renderToImage(DPI.width(), DPI.height()).scaled(size.width(), size.height(), Qt::KeepAspectRatio, Qt::SmoothTransformation) );
}else{
painter.drawImage(0,0,QImage());
}
//QApplication::processEvents();
}
if(PAGE!=0){ delete PAGE; }
ui->toolBar2->setVisible(false);
}
void MainUI::OpenNewFile(){
//Prompt for a file
QString path = QFileDialog::getOpenFileName(this, tr("Open PDF"), lastdir, tr("PDF Documents (*.pdf)"));
//Now Open it
if(!path.isEmpty()){ loadFile(path); }
}
|