Commit 8563fe87 authored by Philipp Erhardt's avatar Philipp Erhardt

initial

parents
pdf
*.o
.*.swp
*~
#############################################################################
# Makefile for building: pdf
# Generated by qmake (2.01a) (Qt 4.7.4) on: Di. Dez 20 21:22:22 2011
# Project: pdf.pro
# Template: app
# Command: /usr/bin/qmake -o Makefile pdf.pro
#############################################################################
####### Compiler, tools and options
CC = gcc
CXX = g++
DEFINES = -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED
CFLAGS = -m64 -pipe -march=x86-64 -mtune=generic -O2 -pipe -fstack-protector --param=ssp-buffer-size=4 -D_FORTIFY_SOURCE=2 -fno-strict-aliasing -Wall -W -D_REENTRANT $(DEFINES)
CXXFLAGS = -m64 -pipe -march=x86-64 -mtune=generic -O2 -pipe -fstack-protector --param=ssp-buffer-size=4 -D_FORTIFY_SOURCE=2 -fno-strict-aliasing -Wall -W -D_REENTRANT $(DEFINES)
INCPATH = -I/usr/share/qt/mkspecs/linux-g++-64 -I. -I/usr/include/QtCore -I/usr/include/QtGui -I/usr/include -I. -I.
LINK = g++
LFLAGS = -m64 -Wl,-O1,--sort-common,--as-needed,-z,relro,--hash-style=gnu -Wl,-O1
LIBS = $(SUBLIBS) -L/usr/lib -lpoppler-qt4 -lQtGui -lQtCore -lpthread
AR = ar cqs
RANLIB =
QMAKE = /usr/bin/qmake
TAR = tar -cf
COMPRESS = gzip -9f
COPY = cp -f
SED = sed
COPY_FILE = $(COPY)
COPY_DIR = $(COPY) -r
STRIP = strip
INSTALL_FILE = install -m 644 -p
INSTALL_DIR = $(COPY_DIR)
INSTALL_PROGRAM = install -m 755 -p
DEL_FILE = rm -f
SYMLINK = ln -f -s
DEL_DIR = rmdir
MOVE = mv -f
CHK_DIR_EXISTS= test -d
MKDIR = mkdir -p
####### Output directory
OBJECTS_DIR = ./
####### Files
SOURCES = main.cpp \
pdfviewer.cpp \
resourcemanager.cpp moc_pdfviewer.cpp
OBJECTS = main.o \
pdfviewer.o \
resourcemanager.o \
moc_pdfviewer.o
DIST = /usr/share/qt/mkspecs/common/g++.conf \
/usr/share/qt/mkspecs/common/unix.conf \
/usr/share/qt/mkspecs/common/linux.conf \
/usr/share/qt/mkspecs/qconfig.pri \
/usr/share/qt/mkspecs/modules/qt_webkit_version.pri \
/usr/share/qt/mkspecs/features/qt_functions.prf \
/usr/share/qt/mkspecs/features/qt_config.prf \
/usr/share/qt/mkspecs/features/exclusive_builds.prf \
/usr/share/qt/mkspecs/features/default_pre.prf \
/usr/share/qt/mkspecs/features/release.prf \
/usr/share/qt/mkspecs/features/default_post.prf \
/usr/share/qt/mkspecs/features/warn_on.prf \
/usr/share/qt/mkspecs/features/qt.prf \
/usr/share/qt/mkspecs/features/unix/thread.prf \
/usr/share/qt/mkspecs/features/moc.prf \
/usr/share/qt/mkspecs/features/resources.prf \
/usr/share/qt/mkspecs/features/uic.prf \
/usr/share/qt/mkspecs/features/yacc.prf \
/usr/share/qt/mkspecs/features/lex.prf \
/usr/share/qt/mkspecs/features/include_source_dir.prf \
pdf.pro
QMAKE_TARGET = pdf
DESTDIR =
TARGET = pdf
first: all
####### Implicit rules
.SUFFIXES: .o .c .cpp .cc .cxx .C
.cpp.o:
$(CXX) -c $(CXXFLAGS) $(INCPATH) -o "$@" "$<"
.cc.o:
$(CXX) -c $(CXXFLAGS) $(INCPATH) -o "$@" "$<"
.cxx.o:
$(CXX) -c $(CXXFLAGS) $(INCPATH) -o "$@" "$<"
.C.o:
$(CXX) -c $(CXXFLAGS) $(INCPATH) -o "$@" "$<"
.c.o:
$(CC) -c $(CFLAGS) $(INCPATH) -o "$@" "$<"
####### Build rules
all: Makefile $(TARGET)
$(TARGET): $(OBJECTS)
$(LINK) $(LFLAGS) -o $(TARGET) $(OBJECTS) $(OBJCOMP) $(LIBS)
Makefile: pdf.pro /usr/share/qt/mkspecs/linux-g++-64/qmake.conf /usr/share/qt/mkspecs/common/g++.conf \
/usr/share/qt/mkspecs/common/unix.conf \
/usr/share/qt/mkspecs/common/linux.conf \
/usr/share/qt/mkspecs/qconfig.pri \
/usr/share/qt/mkspecs/modules/qt_webkit_version.pri \
/usr/share/qt/mkspecs/features/qt_functions.prf \
/usr/share/qt/mkspecs/features/qt_config.prf \
/usr/share/qt/mkspecs/features/exclusive_builds.prf \
/usr/share/qt/mkspecs/features/default_pre.prf \
/usr/share/qt/mkspecs/features/release.prf \
/usr/share/qt/mkspecs/features/default_post.prf \
/usr/share/qt/mkspecs/features/warn_on.prf \
/usr/share/qt/mkspecs/features/qt.prf \
/usr/share/qt/mkspecs/features/unix/thread.prf \
/usr/share/qt/mkspecs/features/moc.prf \
/usr/share/qt/mkspecs/features/resources.prf \
/usr/share/qt/mkspecs/features/uic.prf \
/usr/share/qt/mkspecs/features/yacc.prf \
/usr/share/qt/mkspecs/features/lex.prf \
/usr/share/qt/mkspecs/features/include_source_dir.prf \
/usr/lib/libQtGui.prl \
/usr/lib/libQtCore.prl
$(QMAKE) -o Makefile pdf.pro
/usr/share/qt/mkspecs/common/g++.conf:
/usr/share/qt/mkspecs/common/unix.conf:
/usr/share/qt/mkspecs/common/linux.conf:
/usr/share/qt/mkspecs/qconfig.pri:
/usr/share/qt/mkspecs/modules/qt_webkit_version.pri:
/usr/share/qt/mkspecs/features/qt_functions.prf:
/usr/share/qt/mkspecs/features/qt_config.prf:
/usr/share/qt/mkspecs/features/exclusive_builds.prf:
/usr/share/qt/mkspecs/features/default_pre.prf:
/usr/share/qt/mkspecs/features/release.prf:
/usr/share/qt/mkspecs/features/default_post.prf:
/usr/share/qt/mkspecs/features/warn_on.prf:
/usr/share/qt/mkspecs/features/qt.prf:
/usr/share/qt/mkspecs/features/unix/thread.prf:
/usr/share/qt/mkspecs/features/moc.prf:
/usr/share/qt/mkspecs/features/resources.prf:
/usr/share/qt/mkspecs/features/uic.prf:
/usr/share/qt/mkspecs/features/yacc.prf:
/usr/share/qt/mkspecs/features/lex.prf:
/usr/share/qt/mkspecs/features/include_source_dir.prf:
/usr/lib/libQtGui.prl:
/usr/lib/libQtCore.prl:
qmake: FORCE
@$(QMAKE) -o Makefile pdf.pro
dist:
@$(CHK_DIR_EXISTS) .tmp/pdf1.0.0 || $(MKDIR) .tmp/pdf1.0.0
$(COPY_FILE) --parents $(SOURCES) $(DIST) .tmp/pdf1.0.0/ && $(COPY_FILE) --parents pdfviewer.h resourcemanager.h .tmp/pdf1.0.0/ && $(COPY_FILE) --parents main.cpp pdfviewer.cpp resourcemanager.cpp .tmp/pdf1.0.0/ && (cd `dirname .tmp/pdf1.0.0` && $(TAR) pdf1.0.0.tar pdf1.0.0 && $(COMPRESS) pdf1.0.0.tar) && $(MOVE) `dirname .tmp/pdf1.0.0`/pdf1.0.0.tar.gz . && $(DEL_FILE) -r .tmp/pdf1.0.0
clean:compiler_clean
-$(DEL_FILE) $(OBJECTS)
-$(DEL_FILE) *~ core *.core
####### Sub-libraries
distclean: clean
-$(DEL_FILE) $(TARGET)
-$(DEL_FILE) Makefile
check: first
mocclean: compiler_moc_header_clean compiler_moc_source_clean
mocables: compiler_moc_header_make_all compiler_moc_source_make_all
compiler_moc_header_make_all: moc_pdfviewer.cpp
compiler_moc_header_clean:
-$(DEL_FILE) moc_pdfviewer.cpp
moc_pdfviewer.cpp: resourcemanager.h \
pdfviewer.h
/usr/bin/moc $(DEFINES) $(INCPATH) pdfviewer.h -o moc_pdfviewer.cpp
compiler_rcc_make_all:
compiler_rcc_clean:
compiler_image_collection_make_all: qmake_image_collection.cpp
compiler_image_collection_clean:
-$(DEL_FILE) qmake_image_collection.cpp
compiler_moc_source_make_all:
compiler_moc_source_clean:
compiler_uic_make_all:
compiler_uic_clean:
compiler_yacc_decl_make_all:
compiler_yacc_decl_clean:
compiler_yacc_impl_make_all:
compiler_yacc_impl_clean:
compiler_lex_make_all:
compiler_lex_clean:
compiler_clean: compiler_moc_header_clean
####### Compile
main.o: main.cpp resourcemanager.h \
pdfviewer.h
$(CXX) -c $(CXXFLAGS) $(INCPATH) -o main.o main.cpp
pdfviewer.o: pdfviewer.cpp pdfviewer.h \
resourcemanager.h
$(CXX) -c $(CXXFLAGS) $(INCPATH) -o pdfviewer.o pdfviewer.cpp
resourcemanager.o: resourcemanager.cpp resourcemanager.h
$(CXX) -c $(CXXFLAGS) $(INCPATH) -o resourcemanager.o resourcemanager.cpp
moc_pdfviewer.o: moc_pdfviewer.cpp
$(CXX) -c $(CXXFLAGS) $(INCPATH) -o moc_pdfviewer.o moc_pdfviewer.cpp
####### Install
install: FORCE
uninstall: FORCE
FORCE:
#include <QApplication>
#include <QImage>
#include <QString>
#include <iostream>
#include "resourcemanager.h"
#include "pdfviewer.h"
int main(int argc, char *argv[]) {
using namespace std;
QApplication app(argc, argv);
if (argc != 2) {
cerr << "usage: " << argv[0] << " <path>" << endl;
return 1;
}
ResourceManager res(argv[1]);
if (res.isNull())
return 1;
PdfViewer pdf(&res);
pdf.show();
return app.exec();
}
TEMPLATE = app
TARGET = pdf
DEPENDPATH += .
INCLUDEPATH += .
# Input
HEADERS += pdfviewer.h resourcemanager.h
SOURCES += main.cpp pdfviewer.cpp resourcemanager.cpp
unix:LIBS += -lpoppler-qt4
#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();
}
}
#ifndef PDFVIEWER_H
#define PDFVIEWER_H
#include <QWidget>
#include <QImage>
#include <QPainter>
#include <QPaintEvent>
#include <QString>
#include <QKeyEvent>
#include <QMouseEvent>
#include <QCoreApplication>
#include <QResizeEvent>
#include <iostream>
#include "resourcemanager.h"
class PdfViewer : public QWidget {
Q_OBJECT
public:
PdfViewer(ResourceManager *res, QWidget *parent = 0);
~PdfViewer();
protected:
void paintEvent(QPaintEvent *event);
void keyPressEvent(QKeyEvent *event);
void mousePressEvent(QMouseEvent *event);
void mouseMoveEvent(QMouseEvent *event);
void resizeEvent(QResizeEvent *event);
void setPage(int newPage, bool relative = false);
void toggleFit();
void scrollPage(int dx, int dy);
private:
ResourceManager *res;
int page;
int pageHeight;
float scroll;
bool fit;
int mx, my;
};
#endif
#include "resourcemanager.h"
using namespace std;
ResourceManager::ResourceManager(QString file) :
basePage(-1) {
doc = Poppler::Document::load(file);
doc->setRenderHint(Poppler::Document::Antialiasing, true);
doc->setRenderHint(Poppler::Document::TextAntialiasing, true);
doc->setRenderHint(Poppler::Document::TextHinting, true);
doc->setRenderHint(Poppler::Document::TextSlightHinting, true);
if (doc == NULL) {
cerr << "failed to open file" << endl;
return;
}
if (doc->isLocked()) {
cerr << "missing password" << endl;
delete doc;
doc = NULL;
return;
}
}
ResourceManager::~ResourceManager() {
delete doc;
}
bool ResourceManager::isNull() const {
return (doc == NULL);
}
QImage *ResourceManager::getPage(int newPage, int newWidth) {
if (width != newWidth) {
basePage = newPage;
width = newWidth;
image[1] = QImage();
return render(0) ? &image[0] : NULL;
}
if (basePage == -1) {
basePage = newPage;
width = newWidth;
return render(0) ? &image[0] : NULL;
}
switch (newPage - basePage) {
case -1:
basePage--;
image[1] = image[0];
return render(0) ? &image[0] : NULL;
break;
case 0:
return &image[0];
break;
case 1:
if (image[1].isNull()) {
return render(1) ? &image[1] : NULL;
} else {
return &image[1];
}
break;
case 2:
basePage++;
image[0] = image[1];
return render(1) ? &image[1] : NULL;
break;
default:
basePage = newPage;
image[1] = QImage();
return render(0) ? &image[0] : NULL;
}
/* if (page == newPage && width == newWidth) {
return &image[0];
}
return &image[0]; */
}
bool ResourceManager::render(int offset) {
using namespace std;
cout << " rerendering page " << (basePage + offset) << endl;
Poppler::Page *p = doc->page(basePage + offset);
if (p== NULL) {
cerr << "failed to load page " << (basePage + offset) << endl;
return false;
}
float dpi = 72.0 * width / p->pageSizeF().width();
image[offset] = p->renderToImage(dpi, dpi);
if (image[offset].isNull()) {
cerr << "failed to render page " << (basePage + offset) << endl;
return false;
}
return true;
}
int ResourceManager::numPages() const {
return doc->numPages();
}
#ifndef RESOURCEMANAGER_H
#define RESOURCEMANAGER_H
#include <poppler/qt4/poppler-qt4.h>
#include <QString>
#include <QImage>
#include <iostream>
class ResourceManager {
public:
ResourceManager(QString file);
~ResourceManager();
bool isNull() const;
QImage *getPage(int newPage, int newWidth);
int numPages() const;
private:
bool render(int offset);
Poppler::Document *doc;
int basePage;
QImage image[2];
int width;
};
#endif
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment