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
#include <QtGui>
#include "UBWebPluginPDFWidget.h"
#include "XPDFRenderer.h"
UBWebPluginPDFWidget::UBWebPluginPDFWidget(const QUrl &url, QWidget *parent)
: UBWebPluginWidget(url, parent)
, mPreviousPageButton(this)
, mNextPageButton(this)
, mPreviousPageAction(0)
, mNextPageAction(0)
, mRenderer(0)
, mScale(1.5)
, mPageNumber(1)
{
QIcon previousPageIcon;
QIcon nextPageIcon;
previousPageIcon.addFile(":/images/toolbar/previousPage.png", QSize(32, 32), QIcon::Normal);
nextPageIcon.addFile(":/images/toolbar/nextPage.png", QSize(32, 32), QIcon::Normal);
mPreviousPageAction = new QAction(previousPageIcon, QString(), this);
mNextPageAction = new QAction(nextPageIcon, QString(), this);
connect(mPreviousPageAction, SIGNAL(triggered()), this, SLOT(previousPage()));
connect(mNextPageAction, SIGNAL(triggered()), this, SLOT(nextPage()));
mPreviousPageButton.setFixedSize(32, 32);
mNextPageButton.setFixedSize(32, 32);
mPreviousPageButton.setDefaultAction(mPreviousPageAction);
mNextPageButton.setDefaultAction(mNextPageAction);
mNextPageButton.move(mPreviousPageButton.x() + mPreviousPageButton.width() + 10, mNextPageButton.y());
}
UBWebPluginPDFWidget::~UBWebPluginPDFWidget()
{
if (mRenderer)
{
delete mRenderer;
}
}
void UBWebPluginPDFWidget::handleFile(const QString &filePath)
{
mRenderer = new XPDFRenderer(filePath);
}
void UBWebPluginPDFWidget::keyReleaseEvent(QKeyEvent *keyEvent)
{
// TOOD: why is it not called?
switch (keyEvent->key())
{
case Qt::Key_Right:
if (nextPage())
{
keyEvent->accept();
}
break;
case Qt::Key_Left:
if (previousPage())
{
keyEvent->accept();
}
break;
default:
break;
}
}
bool UBWebPluginPDFWidget::previousPage()
{
if (mPageNumber > 1)
{
mPageNumber--;
update();
return true;
}
return false;
}
bool UBWebPluginPDFWidget::nextPage()
{
if (mPageNumber < mRenderer->pageCount())
{
mPageNumber++;
update();
return true;
}
return false;
}
void UBWebPluginPDFWidget::zoomIn()
{
mScale *= 1.41;
update();
}
void UBWebPluginPDFWidget::zoomOut()
{
mScale /= 1.41;
update();
}
void UBWebPluginPDFWidget::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
if (!mRenderer)
{
// PDF not yet downloaded
return;
}
if (!mRenderer->isValid())
{
qWarning("UBWebPluginPDFWidget::paintEvent: Invalid renderer");
return;
}
QSizeF pageSize = mRenderer->pageSizeF(mPageNumber);
painter.translate((geometry().width() - (pageSize.width() * mScale)) / 2, 0);
painter.scale(mScale, mScale);
mRenderer->render(&painter, mPageNumber, event->rect());
painter.setPen(QPen(Qt::gray, 1));
painter.drawRect(0, 0, pageSize.width(), pageSize.height());
}
QString UBWebPluginPDFWidget::title() const
{
if (mRenderer)
{
return mRenderer->title();
}
else
{
return UBWebPluginWidget::title();
}
}