kpdf_pagewidget.cc 2.88 KB
Newer Older
1 2
#include "QOutputDevPixmap.h"

3
#include <qcursor.h>
4
#include <qpainter.h>
5
#include <qmutex.h>
6 7 8 9 10 11

#include "PDFDoc.h"

#include "kpdf_pagewidget.h"
#include "kpdf_pagewidget.moc"

12
#include "utils.h"
13

14
namespace KPDF
15
{
16
	PageWidget::PageWidget(QWidget* parent, const char* name)
Laurent Montel's avatar
Laurent Montel committed
17
		: QScrollView(parent, name, WRepaintNoErase),
18 19 20 21 22 23 24 25
	m_doc(0),
	m_zoomFactor( 1.0 ),
	m_currentPage( 1 ),
	m_pressedAction( 0 )
	{
		m_outputdev = new QOutputDevPixmap();
		setMouseTracking(true);
	}
26

Laurent Montel's avatar
Laurent Montel committed
27
	void
28 29 30 31 32
		PageWidget::setPDFDocument(PDFDoc* doc)
		{
			m_doc = doc;
			updatePixmap();
		}
33

Laurent Montel's avatar
Laurent Montel committed
34
	void
35 36 37 38
		PageWidget::setPixelsPerPoint(float ppp)
		{
			m_ppp = ppp;
		}
39

Laurent Montel's avatar
Laurent Montel committed
40
	void
41 42 43 44
		PageWidget::contentsMousePressEvent(QMouseEvent* e)
		{
			if (m_doc == 0)
				return;
45

46 47
			m_pressedAction = m_doc->findLink(e->x()/m_ppp, e->y()/m_ppp);
		}
48

Laurent Montel's avatar
Laurent Montel committed
49
	void
50 51 52 53
		PageWidget::contentsMouseReleaseEvent(QMouseEvent* e)
		{
			if (m_doc == 0)
				return;
54

55 56 57
			LinkAction* action = m_doc->findLink(e->x()/m_ppp, e->y()/m_ppp);
			if (action == m_pressedAction)
				emit linkClicked(action);
58

59 60
			m_pressedAction = 0;
		}
61

62 63 64 65 66
	void
		PageWidget::contentsMouseMoveEvent(QMouseEvent* e)
		{
			if (m_doc == 0)
				return;
67

68 69 70 71 72 73 74 75 76 77 78
			LinkAction* action = m_doc->findLink(e->x()/m_ppp, e->y()/m_ppp);
			setCursor(action != 0 ? Qt::PointingHandCursor : Qt::ArrowCursor);
		}

	void PageWidget::drawContents ( QPainter *p, int clipx, int clipy, int clipw, int cliph )
	{
		QPixmap * m_pixmap = NULL;
		if (m_outputdev)
			m_pixmap = m_outputdev->getPixmap();
		if ( m_pixmap != NULL && ! m_pixmap->isNull() )
			p->drawPixmap ( clipx, clipy, *m_pixmap, clipx, clipy, clipw, cliph );
Laurent Montel's avatar
Laurent Montel committed
79
		else
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
			p->fillRect ( clipx, clipy, clipw, cliph, white );
	}

	void PageWidget::nextPage()
	{
		setPage( getPage() + 1);
	};

	void PageWidget::previousPage()
	{
		setPage( getPage() - 1 );
	};

	void PageWidget::zoomIn()
	{
		m_zoomFactor += 0.1;
		updatePixmap();
	};

	void PageWidget::zoomOut()
	{
		m_zoomFactor -= 0.1;
		updatePixmap();
	};

	void PageWidget::setPage(int page)
	{
107 108 109 110 111 112 113 114 115 116 117
            static QMutex mutex;

            mutex.lock();
            if (m_doc)
            {
                m_currentPage = max(0, min( page, m_doc->getNumPages()));
            } else {
                m_currentPage = 0;
            }
            updatePixmap();
            mutex.unlock();
118 119 120 121
	};

	void PageWidget::updatePixmap()
	{
Laurent Montel's avatar
Laurent Montel committed
122 123
            if ( m_doc )
            {
124 125 126 127 128 129 130 131 132 133 134
		const double pageWidth  = m_doc->getPageWidth (m_currentPage) * m_zoomFactor;
		const double pageHeight = m_doc->getPageHeight(m_currentPage) * m_zoomFactor;

		// Pixels per point when the zoomFactor is 1.
		const float basePpp  = QPaintDevice::x11AppDpiX() / 72.0;

		const float ppp = basePpp * m_zoomFactor; // pixels per point

		m_doc->displayPage(m_outputdev, m_currentPage, int(ppp * 72.0), 0, true);

		resizeContents ( m_outputdev->getPixmap()->width ( ), m_outputdev->getPixmap()->height ( ));
Laurent Montel's avatar
Laurent Montel committed
135

136
		viewport()->update();
Laurent Montel's avatar
Laurent Montel committed
137
            }
138
	}
139 140 141
}

// vim:ts=2:sw=2:tw=78:et