-
Notifications
You must be signed in to change notification settings - Fork 0
/
pdfdocumentwidget.cpp
253 lines (229 loc) · 7.43 KB
/
pdfdocumentwidget.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
/****************************************************************************
**
** copyright : (C) 2003-2011 by Pascal Brachet
** http://www.xm1math.net/texmaker/
**
** addons by Adrián Yanes Martínez
** Parts of this file come from the documentation of Qt. It was originally
** published as part of Qt Quarterly.
** Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies).
** Contact: Qt Software Information ([email protected])
**
**
****************************************************************************/
#include <QtGui>
#include <QDebug>
#include <QScrollArea>
#include <QContextMenuEvent>
#include <QMenu>
#include <QAction>
#include "pdfdocumentwidget.h"
PdfDocumentWidget::PdfDocumentWidget(QWidget *parent, int page,Poppler::Page *dp)
: QLabel(parent)
{
numPage=page;
doc_page = dp;
scaleFactor = 1.0;
setAlignment(Qt::AlignCenter);
currentLink=0;
setMouseTracking(true);
highlightRemover.setSingleShot(true);
connect(&highlightRemover, SIGNAL(timeout()), this, SLOT(clearHighlight()));
handmode=false;
}
PdfDocumentWidget::~PdfDocumentWidget()
{
delete doc_page;
}
/*Poppler::Page *PdfDocumentWidget::document_page()
{
return doc_page;
}*/
QMatrix PdfDocumentWidget::matrix() const
{
return QMatrix(scaleFactor * physicalDpiX() / 72.0, 0,
0, scaleFactor * physicalDpiY() / 72.0,
0, 0);
}
void PdfDocumentWidget::mousePressEvent(QMouseEvent *event)
{
if (!doc_page) return;
currentLink=0;
clearPaths();
updatePixmap();
//setPage();
foreach (Poppler::Link* link, doc_page->links())
{
QPointF scaledPos((event->pos().x()- (width() - pixmap()->width()) / 2.0) / scaleFactor / physicalDpiX() * 72.0 / doc_page->pageSizeF().width(),(event->pos().y()- (height() - pixmap()->height()) / 2.0) / scaleFactor / physicalDpiY() * 72.0 / doc_page->pageSizeF().height());
if (link->linkArea().contains(scaledPos))
{
currentLink = link;
break;
}
}
if (currentLink==0)
{
setCursor(Qt::ClosedHandCursor);
QApplication::setOverrideCursor(Qt::ClosedHandCursor);
handmode=true;
emit pressOnPoint(event->globalPos());
}
else setCursor(Qt::ArrowCursor);
}
void PdfDocumentWidget::mouseMoveEvent(QMouseEvent *event)
{
if (!doc_page) return;
if (handmode) {setCursor(Qt::ClosedHandCursor);emit moveOnPoint(event->globalPos());}
else {setCursor(Qt::ArrowCursor);}
foreach (Poppler::Link* link, doc_page->links())
{
QPointF scaledPos((event->pos().x()- (width() - pixmap()->width()) / 2.0) / scaleFactor / physicalDpiX() * 72.0 / doc_page->pageSizeF().width(),(event->pos().y()- (height() - pixmap()->height()) / 2.0) / scaleFactor / physicalDpiY() * 72.0 / doc_page->pageSizeF().height());
if (link->linkArea().contains(scaledPos))
{
setCursor(Qt::PointingHandCursor);
break;
}
}
event->accept();
}
void PdfDocumentWidget::mouseReleaseEvent(QMouseEvent *event)
{
if (!doc_page) return;
if (currentLink)
{
QPointF scaledPos((event->pos().x()- (width() - pixmap()->width()) / 2.0) / scaleFactor / physicalDpiX() * 72.0 / doc_page->pageSizeF().width(),(event->pos().y()- (height() - pixmap()->height()) / 2.0) / scaleFactor / physicalDpiY() * 72.0 / doc_page->pageSizeF().height());
if ((currentLink->linkArea().contains(scaledPos)) && (currentLink->linkType()==Poppler::Link::Goto))
{
const Poppler::LinkGoto *gotoLink = dynamic_cast<const Poppler::LinkGoto*>(currentLink);
if (gotoLink)
{
const Poppler::LinkDestination dest = gotoLink->destination();
int destPage=0;
int destLeft=0;
int destTop=0;
if ((dest.pageNumber() > 0)) destPage=dest.pageNumber();
if (dest.isChangeLeft()) destLeft = (int)floor(dest.left() * scaleFactor * physicalDpiX() / 72.0 * doc_page->pageSizeF().width());
if (dest.isChangeTop()) destTop = (int)floor(dest.top() * scaleFactor * physicalDpiY() / 72.0 * doc_page->pageSizeF().height());
emit gotoDest(destPage,destLeft,destTop);
}
}
}
currentLink=0;
handmode=false;
setCursor(Qt::ArrowCursor);
QApplication::restoreOverrideCursor();
if ((event->modifiers() & ~Qt::ShiftModifier) == Qt::ControlModifier)
{
QPointF pagePos((event->pos().x()- (width() - pixmap()->width()) / 2.0) / scaleFactor / physicalDpiX() * 72.0,(event->pos().y()- (height() - pixmap()->height()) / 2.0) / scaleFactor / physicalDpiY() * 72.0 );
emit syncpage(numPage, pagePos);
}
}
void PdfDocumentWidget::contextMenuEvent(QContextMenuEvent *event)
{
if (!doc_page) return;
handmode=false;
setCursor(Qt::ArrowCursor);
QApplication::restoreOverrideCursor();
QMenu *menu = new QMenu(this);
QAction *act = new QAction(tr("Click to jump to the line")+" (ctrl+click)", menu);
//act->setShortcut(Qt::CTRL+Qt::Key_Space);
act->setData(QVariant(event->pos()));
connect(act, SIGNAL(triggered()), this, SLOT(jumpToSourceFromPdf()));
menu->addAction(act);
menu->exec(mapToGlobal(event->pos()));
delete menu;
}
qreal PdfDocumentWidget::scale() const
{
return scaleFactor;
}
void PdfDocumentWidget::updatePixmap()
{
// QColor selBlendColor=QColor("#43ACE8");
QColor selBlendColor=QColor("#FF0000");
QColor blCol = selBlendColor.dark( 140 );
blCol.setAlphaF( 0.2 );
QImage image = doc_page->renderToImage(scaleFactor * physicalDpiX(), scaleFactor * physicalDpiY());
QPainter painter;
if (!searchPath.isEmpty())
{
QRect highlightRect = matrix().mapRect(searchPath.boundingRect()).toRect();
highlightRect.adjust(-3, -2, 3, 0);
QImage highlight = image.copy(highlightRect);
painter.begin(&image);
painter.fillRect(highlightRect, blCol);
painter.setPen( selBlendColor );
painter.drawRect( highlightRect );
painter.end();
}
if (!highlightPath.isEmpty())
{
QRect highlightRect = matrix().mapRect(highlightPath.boundingRect()).toRect();
highlightRect.adjust(-3, -2, 3, 0);
QImage highlight = image.copy(highlightRect);
painter.begin(&image);
painter.fillRect(highlightRect, blCol);
painter.setPen( selBlendColor );
painter.drawRect( highlightRect );
painter.end();
}
setPixmap(QPixmap::fromImage(image));
emit updateDone(numPage);
}
void PdfDocumentWidget::createblankPixmap(int w, int h)
{
QPixmap pix=QPixmap(w,h);
pix.fill(Qt::transparent);
setPixmap(pix);
}
void PdfDocumentWidget::changeScale(qreal scale)
{
if (scaleFactor != scale) {
scaleFactor = scale;
updatePixmap();
}
}
void PdfDocumentWidget::setScale(qreal scale)
{
if (scaleFactor != scale) {
scaleFactor = scale;
}
}
void PdfDocumentWidget::jumpToSourceFromPdf()
{
QAction *act = qobject_cast<QAction*>(sender());
if (act != NULL)
{
QPoint eventPos = act->data().toPoint();
QPointF pagePos((eventPos.x()- (width() - pixmap()->width()) / 2.0) / scaleFactor / physicalDpiX() * 72.0,(eventPos.y()- (height() - pixmap()->height()) / 2.0) / scaleFactor / physicalDpiY() * 72.0 );
emit syncpage(numPage, pagePos);
}
}
void PdfDocumentWidget::setHighlightPath(const QPainterPath& path)
{
highlightRemover.stop();
highlightPath = path;
if (!path.isEmpty())
{
updatePixmap();
highlightRemover.start(2000);
}
}
void PdfDocumentWidget::setSearchPath(const QPainterPath& path)
{
searchPath = path;
if (!path.isEmpty())
{
updatePixmap();
}
}
void PdfDocumentWidget::clearHighlight()
{
highlightPath = QPainterPath();
updatePixmap();
}
void PdfDocumentWidget::clearPaths()
{
searchPath = QPainterPath();
highlightPath = QPainterPath();
}