pdfviewer.cpp 2.43 KB
Newer Older
Philipp Erhardt's avatar
initial  
Philipp Erhardt committed
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
#include "pdfviewer.h"

PdfViewer::PdfViewer(ResourceManager *_res, QWidget *parent) :
		QWidget(parent),
		res(_res),
		page(0),
		scroll(0.0),
		fit(false) {
	setFocusPolicy(Qt::StrongFocus);
}

PdfViewer::~PdfViewer() {
}

void PdfViewer::paintEvent(QPaintEvent * /*event*/) {
	using namespace std;
	cout << "redraw" << endl;
	QPainter painter(this);
	for (int i = 0; i < 2; ++i) {
		QImage *img = res->getPage(page + i, width());
		if (img != NULL) {
			pageHeight = img->height();
			QRect target(0, 0, width(), height());
			QRect source(0, scroll * pageHeight - (img->height() + 2) * i, width(), height());
			painter.drawImage(target, *img, source);
		} else {
			cout << "null: " << page << " " << i << endl;
		}
	}
}

void PdfViewer::keyPressEvent(QKeyEvent *event) {
	switch (event->key()) {
		case Qt::Key_Q:
		case Qt::Key_Escape:
			QCoreApplication::exit(0);
			break;
		case Qt::Key_Space:
		case Qt::Key_Down:
			scroll = 0;
			setPage(1, true);
			break;
		case Qt::Key_Backspace:
		case Qt::Key_Up:
			scroll = 0;
			setPage(-1, true);
			break;
		case Qt::Key_J:
			scrollPage(0, -50);
			break;
		case Qt::Key_K:
			scrollPage(0, 50);
			break;
		case Qt::Key_G:
			scroll = 0;
			setPage(0);
			break;
		case Qt::Key_B:
			scroll = 0;
			setPage(res->numPages() - 1);
			break;
		case Qt::Key_1:
			toggleFit();
			break;
		default:
			QWidget::keyPressEvent(event);
			break;
	}
}

void PdfViewer::mousePressEvent(QMouseEvent *event) {
	if (event->button() == Qt::LeftButton) {
		mx = event->x();
		my = event->y();
	}
}

void PdfViewer::mouseMoveEvent(QMouseEvent *event) {
	scrollPage(event->x() - mx, event->y() - my);
	mx = event->x();
	my = event->y();
}

void PdfViewer::resizeEvent(QResizeEvent * /*event*/) {
	update();
}

void PdfViewer::setPage(int newPage, bool relative) {
	if (relative) {
		newPage += page;
	}
	if (newPage < 0) {
		newPage = 0;
		scroll = 0;
	} else if (newPage >= res->numPages()) {
		newPage = res->numPages() - 1;
	}

	if (page != newPage) {
		QString str = "page ";
		str.append(QString::number(page));
		str.append("/");
		str.append(QString::number(res->numPages()));
		setWindowTitle(str);
		page = newPage;
		update();
	}
}

void PdfViewer::toggleFit() {
	fit = not fit;
	update();
}

void PdfViewer::scrollPage(int dx, int dy) {
	if (dy == 0)
		return;

	scroll -= (float) dy / pageHeight;
	if (scroll < 0) {
		scroll++;
		setPage(-1, true);
	} else if (scroll >= 1) {
		scroll--;
		setPage(1, true);
	} else {
		update();
	}
}