-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpdqmainwindow.cpp
337 lines (312 loc) · 10.6 KB
/
pdqmainwindow.cpp
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
#include <QDebug>
#include "pdqmainwindow.h"
#include "ui_pdqmainwindow.h"
#include "searchstate.h"
#include "searchdialog.h"
#include "bookmarks.h"
#include "addbookmark.h"
#include "navdialog.h"
#include "textextract.h"
#include "utils.h"
#include <poppler-qt5.h>
#include <QLabel>
#include <QGraphicsScene>
#include <QGraphicsScale>
#include <QGraphicsPixmapItem>
#include <QGraphicsView>
#include <QRect>
#include <QRectF>
#include <QScrollBar>
#include <QKeyEvent>
#include <QClipboard>
PdQMainWindow::PdQMainWindow(QWidget *parent, Config *conf) :
QMainWindow(parent),
pageScene(new QGraphicsScene(this)),
beforeJump(QStack<int>()),
pageNumLabel(new QLabel(this)),
totalPagesLabel(new QLabel(this)),
resolutionLabel(new QLabel(this)),
ui(new Ui::PdQMainWindow),
searchState(SearchState()),
notes(new QList<Note>()),
conf(conf)
{
ui->setupUi(this);
pageNumLabel->setAlignment(Qt::AlignCenter);
QFont pageLabelFont("Sans Serif", 10, QFont::Bold);
pageNumLabel->setFont(pageLabelFont);
pageNumLabel->setStyleSheet("QLabel { background-color : white; }");
QFont resolutionLabelFont("Sans Serif", 8);
ui->toolBar->addWidget(pageNumLabel);
totalPagesLabel->setAlignment(Qt::AlignCenter);
totalPagesLabel->setFont(pageLabelFont);
ui->toolBar->addWidget(totalPagesLabel);
ui->toolBar->addSeparator();
resolutionLabel->setAlignment(Qt::AlignCenter);
resolutionLabel->setFont(resolutionLabelFont);
ui->toolBar->addWidget(resolutionLabel);
}
PdQMainWindow::~PdQMainWindow()
{
delete ui;
delete pageScene;
delete pageNumLabel;
delete totalPagesLabel;
delete resolutionLabel;
delete notes;
}
void PdQMainWindow::loadFile()
{
document = Poppler::Document::load(filename);
document->setRenderHint(Poppler::Document::Antialiasing , true);
document->setRenderHint(Poppler::Document::TextAntialiasing, true);
numPages = document->numPages();
QDomDocument d;
QFile* pdqFile = new QFile(Utils::bookmarksFileName(filename));
Utils::checkBookmarksFile(pdqFile);
Utils::readDocFromFile(d, pdqFile);
delete pdqFile;
delete notes;
notes = Utils::getNotesFromDoc(d);
}
void PdQMainWindow::preparePage(int pagenum)
{
totalPagesLabel->setText(QString::number(numPages));
resolutionLabel->setText(QString::number(conf->dpi));
pageNumLabel->setText(QString::number(pagenum + 1)+"/");
currentPage = document->page(pagenum);
currentPageNum = pagenum;
QSize sz = currentPage->pageSize();
pageSizeX = (sz.width()*conf->dpi)/72;
pageSizeY = (sz.height()*conf->dpi)/72;
QImage image = currentPage->renderToImage(conf->dpi,conf->dpi,0,0,pageSizeX,pageSizeY);
pageScene->clear();
if (conf->invert) {
pageScene->setBackgroundBrush(Qt::black);
//image.invertPixels();
image.invertPixels(QImage::InvertRgba);
}
QPixmap ipix = QPixmap::fromImage(image);
QGraphicsPixmapItem *pageItem = pageScene->addPixmap(ipix);
ui->graphicsView->setScene(pageScene);
ui->graphicsView->mainwin = this;
pageItem->show();
hbar = ui->graphicsView->horizontalScrollBar();
vbar = ui->graphicsView->verticalScrollBar();
links = currentPage->links();
for (int n=0; n < notes->size() ; n++) {
if (notes->at(n).p == currentPageNum) {
//qDebug() << notes->at(n).x ;
//qDebug() << notes->at(n).y ;
int x = static_cast<int>((static_cast<float>(pageSizeX)) * notes->at(n).x) ;
int y = static_cast<int>((static_cast<float>(pageSizeY)) * notes->at(n).y) ;
//qDebug() << x ;
//qDebug() << y ;
QRectF loc = QRectF(x - 8, y - 8, 16, 16);
QGraphicsEllipseItem *ellipse = pageScene->addEllipse(
loc,
QPen(),
QBrush(QColor(notes->at(n).r, notes->at(n).g, notes->at(n).b, 255))
);
ellipse->show();
}
}
}
void PdQMainWindow::keyPressEvent(QKeyEvent *k){
qint8 s = (k->modifiers() & Qt::ShiftModifier) ? 10 : 3;
if (k->key() == Qt::Key_J) {
vbar->setValue(vbar->value() + s);
}
else if (k->key() == Qt::Key_K) {
vbar->setValue(vbar->value() - s);
}
else if (k->key() == Qt::Key_H) {
hbar->setValue(hbar->value() - s);
}
else if (k->key() == Qt::Key_L) {
hbar->setValue(hbar->value() + s);
}
}
void PdQMainWindow::ReloadFile()
{
loadFile();
if (currentPageNum < numPages)
preparePage(currentPageNum);
else
preparePage(numPages - 1);
}
void PdQMainWindow::PrevPage(){
if (currentPageNum > 0) preparePage(currentPageNum - 1);
}
void PdQMainWindow::NextPage(){
if ((currentPageNum + 1) < numPages) preparePage(currentPageNum + 1);
}
void PdQMainWindow::ZoomIn(){
int v = vbar->value();
int h = hbar->value();
delete pageScene;
pageScene = new QGraphicsScene(this);
conf->dpi = conf->dpi + 4;
preparePage(currentPageNum);
hbar->setValue(conf->dpi * h / (conf->dpi - 4));
vbar->setValue(conf->dpi * v / (conf->dpi - 4));
}
void PdQMainWindow::ZoomOut(){
int v = vbar->value();
int h = hbar->value();
delete pageScene;
pageScene = new QGraphicsScene(this);
if (conf->dpi > 4) conf->dpi = conf->dpi - 4;
preparePage(currentPageNum);
hbar->setValue(conf->dpi * h / (conf->dpi + 4));
vbar->setValue(conf->dpi * v / (conf->dpi + 4));
}
void PdQMainWindow::GoBack(){
if (!beforeJump.isEmpty()) {
preparePage(beforeJump.pop());
}
}
void PdQMainWindow::pushCurrentPage(){
beforeJump.push(currentPageNum);
}
bool PdQMainWindow::search(int pagenumber)
{
qreal r = conf->dpi / 72.0;
Poppler::Page* pdfPage = document->page(pagenumber);
QList<QRectF> locations = pdfPage->search(searchState.searchTerm, Poppler::Page::IgnoreCase);
int s = locations.size();
if (searchState.justReversed) {
//qDebug() << "-- reversed --";
searchState.searchResultsShown = s + 1 - searchState.searchResultsShown;
}
int n = ((searchState.isOnReverse) ? (s - 1 - searchState.searchResultsShown) : searchState.searchResultsShown) ;
if ((n >= 0) && (n < s)) {
preparePage(pagenumber);
QRectF loc = QRectF(r * (locations[n].x()), r * (locations[n].y()), r * (locations[n].width()), r * (locations[n].height()));
QGraphicsEllipseItem *ellipse = pageScene->addEllipse(loc, QPen(), QBrush(QColor(255, 255, 0, 64)));
ellipse->show();
searchState.searchResultsShown++;
return true;
}
else {
searchState.searchResultsShown = 0;
return false;
}
}
void PdQMainWindow::searchForward()
{
int pageNow = currentPageNum;
searchState.searching = true;
searchState.justReversed = searchState.isOnReverse;
searchState.isOnReverse = false;
int n = 0;
while (n < numPages) {
if (search((pageNow + n) % numPages)) { break ; }
searchState.justReversed = false;
n++ ;
}
if (n == numPages) { searchState.searching = false ; }
}
void PdQMainWindow::searchBack()
{
int pageNow = currentPageNum;
searchState.searching = true;
searchState.justReversed = !searchState.isOnReverse;
searchState.isOnReverse = true;
int n = 0;
while (n < numPages) {
int m = pageNow - n;
if (search(( m >= 0 )? m : (numPages + m))) { break ; }
searchState.justReversed = false;
n++ ;
}
if (n == numPages) { searchState.searching = false ; }
}
void PdQMainWindow::Search(QString term, bool directionIsBackwards){
searchState.searchTerm = term;
searchState.searchStartPage = currentPageNum;
searchState.searchResultsShown = 0;
searchState.isOnReverse = directionIsBackwards;
searchState.justReversed = false;
if (directionIsBackwards) { searchBack(); } else { searchForward(); }
}
void PdQMainWindow::StartSearching(){
if (searchState.searching) {
searchForward();
} else {
//qDebug() << "Starting Searching";
SearchDialog* dialog = new SearchDialog(this, false);
connect(dialog, SIGNAL(search(QString,bool)), this, SLOT(Search(QString,bool)));
dialog->show();
}
}
void PdQMainWindow::StartSearchingInReverse(){
if (searchState.searching) {
searchBack();
} else {
//qDebug() << "Start Searching in reverse";
SearchDialog* dialog = new SearchDialog(this, true);
connect(dialog, SIGNAL(search(QString,bool)), this, SLOT(Search(QString,bool)));
dialog->show();
}
}
void PdQMainWindow::StopSearching(){
if (searchState.searching) {
searchState.searching = false;
preparePage(searchState.searchStartPage);
}
}
void PdQMainWindow::GotIt(){
searchState.searching = false;
beforeJump.push(searchState.searchStartPage);
preparePage(currentPageNum);
}
void PdQMainWindow::OpenBookmarks(){
Bookmarks* bm = new Bookmarks(this, Utils::bookmarksFileName(filename));
QObject::connect(bm, SIGNAL(openPage(int)), this, SLOT(GoToBookmark(int)));
bm->show();
}
void PdQMainWindow::GoToBookmark(int n){
beforeJump.push(currentPageNum);
if (n < numPages) preparePage(n);
}
void PdQMainWindow::AddNewBookmark(){
AddBookmark* abm = new AddBookmark(this, Utils::bookmarksFileName(filename), currentPageNum);
abm->show();
}
void PdQMainWindow::Navigate(){
NavDialog* dialog = new NavDialog(this);
connect(dialog, SIGNAL(showPage(int)), this, SLOT(ShowPage(int)));
connect(dialog, SIGNAL(pushCurrentPage()), this, SLOT(PushCurrentPage()));
dialog->show();
}
void PdQMainWindow::PushCurrentPage(){
beforeJump.push(currentPageNum);
}
void PdQMainWindow::ShowPage(int pagenumber){
if (pagenumber < numPages) {
preparePage(pagenumber);
}
}
void PdQMainWindow::ShowTextExtract(){
TextExtract* txtxtr = new TextExtract(this, currentPage->text(QRectF()));
txtxtr->show();
}
void PdQMainWindow::AddNewNote(int p, qreal x, qreal y, int r, int g, int b, QString txt) {
QDomDocument doc;
QFile* pdqFile = new QFile(Utils::bookmarksFileName(filename));
Utils::readDocFromFile(doc, pdqFile);
Note newnote = Note(p,x,y,r,g,b,txt);
QDomElement nn = newnote.asElementOf(doc);
QDomNode root = doc.documentElement();
if (doc.elementsByTagName("notes").isEmpty()) root.appendChild(doc.createElement("notes"));
QDomNodeList notesElems = doc.elementsByTagName("notes");
notesElems.at(0).appendChild(nn);
Utils::writeDocToFile(doc, pdqFile);
delete pdqFile;
ReloadFile();
}
void PdQMainWindow::CopyPathToPDF() {
QClipboard *clipboard = QApplication::clipboard();
clipboard->setText(filename);
}